-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgst.c
More file actions
252 lines (220 loc) · 5.99 KB
/
gst.c
File metadata and controls
252 lines (220 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* See LICENSE file for copyright and license details. */
#include <curl/curl.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "extern/arg.h"
#include "extern/frozen.h"
/* defines */
#define LBUF_SIZE 1024
#define BUF_SIZE 100000
#define URL_SIZE 2048
/* typedefs */
typedef struct {
char *ptr;
size_t len;
} Str;
/* variables */
char *argv0;
static char *file_str(FILE *fp);
#include "config.h"
/* used by cURL to write its response to a Str */
static size_t
str_write(void *ptr, size_t size, size_t nmemb, Str *s)
{
size_t nlen = s->len + size*nmemb;
if (!(s->ptr = realloc(s->ptr, nlen+1)))
perror("realloc: "), exit(1);
memcpy(s->ptr+s->len, ptr, size*nmemb);
s->ptr[nlen] = '\0';
s->len = nlen;
return size*nmemb;
}
/* HTTP POST request with content, returning response */
static Str
http_post(char *content)
{
char *resmsg, url[URL_SIZE], *tokenstr;
long code;
CURL *curl;
CURLcode res;
Str resstr;
struct curl_slist *chunk = NULL;
resstr.len = 0;
if (!(resstr.ptr = malloc(resstr.len+1)))
perror("malloc: "), exit(1);
resstr.ptr[0] = '\0';
/* init cURL */
curl_global_init(CURL_GLOBAL_ALL);
if (!(curl = curl_easy_init())) {
curl_global_cleanup();
fprintf(stderr, "%s: cURL: could not init", argv0);
exit(1);
}
/* set cURL options */
if (gist)
/* TODO if given URL split to get ID */
snprintf(url, sizeof(url), "%s/%s", ghurl, gist);
else
strcpy(url, ghurl);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "gst/"VERSION);
if (gist)
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, str_write);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resstr);
if (tfile) {
FILE *f = fopen(tfile, "r");
if (!f) fprintf(stderr, "%s: %s: could not load file", argv0, tfile), exit(1);
token = file_str(f);
fclose(f);
}
if (token) {
if (!(tokenstr = malloc((23+strlen(token))*sizeof(char))))
perror("malloc: "), exit(1);
strcpy(tokenstr, "Authorization: token ");
strcat(tokenstr, token);
chunk = curl_slist_append(chunk, tokenstr);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
free(tokenstr);
if (tfile) free(token);
}
/* run cURL */
if ((res = curl_easy_perform(curl)) != CURLE_OK) {
curl_global_cleanup();
fprintf(stderr, "%s: cURL: %s", argv0, curl_easy_strerror(res));
exit(1);
}
/* response checking and cleanup */
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
curl_easy_cleanup(curl);
curl_global_cleanup();
/* 200 returned when editing gist, 201 when creating */
if (code != (gist ? 200 : 201)) {
json_scanf(resstr.ptr, resstr.len, "{message: %Q}", &resmsg);
fprintf(stderr, "%s: [%d] could not create gist: %s", argv0, code, resmsg);
free(resmsg);
exit(1);
}
return resstr;
}
/* read file fp into returned string */
static char *
file_str(FILE *fp)
{
char buf[LBUF_SIZE]; /* buffer for each chuck */
char *str = calloc(LBUF_SIZE, sizeof(char)); /* complete file */
size_t flen = 1; /* file length, start at 1 for null terminator */
if (!str) perror("calloc: "), exit(1);
/* loop through each LBUF_SIZE chunk in file, append it to str */
while (fgets(buf, LBUF_SIZE, fp)) {
flen += strlen(buf);
str = realloc(str, flen);
if (!str) perror("realloc: "), exit(1);
strncat(str, buf, LBUF_SIZE);
}
return str;
}
/* turn list of file names into returned json string */
static char *
files_js(char *files[], int filec)
{
char *fbuf; /* file contents */
char *js = malloc(BUF_SIZE*sizeof(char)); /* json string returned */
FILE *fp = stdin; /* read from stdin by default if no file is given */
struct json_out jout = JSON_OUT_BUF(js, BUF_SIZE);
if (!js) perror("malloc: "), exit(1);
json_printf(&jout, "{ public: %B,", pub);
if (!desc && !gist) /* when creating new gist if no description given make it empty */
desc = "";
if (desc) /* only add description as blank if gist is being created */
json_printf(&jout, "description: %Q, ", desc);
json_printf(&jout, "files: {");
if (del) /* delete file if given by setting it to NULL */
json_printf(&jout, "%Q: %Q", basename(del), NULL);
/* add each file */
for (int i = 0; !i || i < filec; i++) {
if (filec && !(fp = fopen(files[i], "r")))
fprintf(stderr, "%s: %s: could not load file", argv0, files[i]), exit(1);
if (filec) /* set file name if given */
fname = files[i];
if (!fname && gist) /* don't need file if we are editing gist */
break;
if (!fname) /* check for file name when using stdin */
fprintf(stderr, "%s: file not given", argv0), exit(1);
if (i || del) /* insert comma if this is another file */
json_printf(&jout, ",");
fbuf = file_str(fp);
json_printf(&jout, "%Q: { content: %Q }", basename(fname), fbuf);
fclose(fp);
free(fbuf);
}
json_printf(&jout, "} }");
return js;
}
static void
usage(const int eval)
{
fprintf(stderr, "usage: %s [-pPhv] [-e ID [-D FILE]] [-d DESCRIPTION] [-f FILENAME]\n"
" [-g URL] [-U | -u TOKENFILE | -T TOKEN] FILES ...", argv0);
exit(eval);
}
int
main(int argc, char *argv[])
{
char *js, *url;
Str resstr;
ARGBEGIN {
case 'e':
gist = EARGF(usage(1));
break;
case 'd':
desc = EARGF(usage(1));
break;
case 'D':
del = EARGF(usage(1));
break;
case 'f':
fname = EARGF(usage(1));
break;
case 'g':
ghurl = EARGF(usage(1));
break;
case 'p':
pub = 0;
break;
case 'P':
pub = 1;
break;
case 'T':
token = EARGF(usage(1));
break;
case 'u':
tfile = EARGF(usage(1));
break;
case 'U':
tfile = NULL;
token = NULL;
break;
case 'h':
usage(0);
case 'v':
fprintf(stderr, "%s v%s (c) 2017-2021 Ed van Bruggen\n", argv0, VERSION);
return 0;
default:
usage(1);
} ARGEND;
if (gist && !token && !tfile)
fprintf(stderr, "%s: cannot edit gists without token", argv0), exit(1);
js = files_js(argv, argc);
resstr = http_post(js);
json_scanf(resstr.ptr, resstr.len, "{html_url: %Q}", &url);
puts(url);
free(js);
free(url);
free(resstr.ptr);
return 0;
}