gs

painless gist creator
git clone git://edryd.org/gs
Log | Files | Refs | LICENSE

commit f4e483ff225f593b09458eedee302828e64a58b5
parent fd74bf29c24cd7e8ef50622d6ff9267dab699989
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Sat, 28 Mar 2020 13:38:41 -0700

Clean up code, remove unnecessary functions

Diffstat:
gs.c | 75+++++++++++++++++++++++++++++++--------------------------------------------
util.c | 16----------------
util.h | 8--------
3 files changed, 31 insertions(+), 68 deletions(-)

diff --git a/gs.c b/gs.c @@ -11,7 +11,8 @@ #include "util.h" /* defines */ -#define LBUF_SIZE 1024 /* TODO remove BUF_SIZEs */ +/* TODO remove BUF_SIZEs */ +#define LBUF_SIZE 1024 #define BUF_SIZE 100000 #define URL_SIZE 2048 @@ -21,13 +22,6 @@ typedef struct { size_t len; } Str; -/* functions */ -static size_t str_write(void *ptr, size_t size, size_t nmemb, Str *s); -static Str http_post(char *content); -static char *file_str(FILE *fp); -static char *files_js(char *files[], int filec); -static void gs_new(char *files[], int filec); - /* variables */ char *argv0; @@ -46,7 +40,7 @@ str_write(void *ptr, size_t size, size_t nmemb, Str *s) return size*nmemb; } -/* HTTP POST request from content, returning response */ +/* HTTP POST request with content, returning response */ static Str http_post(char *content) { @@ -98,14 +92,15 @@ http_post(char *content) curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); curl_easy_cleanup(curl); curl_global_cleanup(); - if (code != (gist ? 200 : 201)) { /* 200 returned when editing gist, 201 when creating */ + /* 200 returned when editing gist, 201 when creating */ + if (code != (gist ? 200 : 201)) { json_scanf(resstr.ptr, resstr.len, "{message: %Q}", &resmsg); - die(-1, "%s: could not create Gist: %s", argv0, resmsg); + die(-1, "%s: [%d] could not create Gist: %s", + argv0, code, resmsg); free(resmsg); exit(1); } - return resstr; } @@ -127,19 +122,19 @@ file_str(FILE *fp) return str; } -/* turn list of file into returned json string */ +/* turn list of file names into returned json string */ static char * files_js(char *files[], int filec) { - char *fbuf, *js = emalloc(BUF_SIZE*sizeof(char)); /* fbuf: file contents, js: json string returned */ - int i; - FILE *fp = stdin; + char *fbuf; /* file contents */ + char *js = emalloc(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); json_printf(&jout, "{ public: %B,", pub); - if (!desc && !gist) /* when creating new Gist, if no description given, make it empty */ + 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 */ + if (desc) /* only add description as blank if gist is being created */ json_printf(&jout, "description: %Q, ", desc); json_printf(&jout, "files: {"); @@ -147,20 +142,20 @@ files_js(char *files[], int filec) json_printf(&jout, "%Q: %Q", basename(del), NULL); /* add each file */ - for (i = 0; !i || i < filec; i++) { + for (int i = 0; !i || i < filec; i++) { if (filec && !(fp = fopen(files[i], "r"))) die(1, "%s: %s: could not load file", argv0, files[i]); - if (filec) /* set file name if given */ + 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 */ + if (!fname) /* check for file name when using stdin */ die(1, "%s: file name not given", argv0); - if (i || del) /* insert comma if this is another file */ + 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); - efree(fbuf); + free(fbuf); } json_printf(&jout, "} }"); @@ -169,34 +164,18 @@ files_js(char *files[], int filec) } static void -gs_new(char *files[], int filec) -{ - char *js, *url; - Str resstr; - - js = files_js(files, filec); - - resstr = http_post(js); - - json_scanf(resstr.ptr, resstr.len, "{html_url: %Q}", &url); - printf("%s\n", url); - - efree(js); - efree(url); - efree(resstr.ptr); - -} - -static void usage(const int eval) { die(eval, "usage: %s [-pPhv] [-e ID [-D FILE]] [-d DESCRIPTION] [-f FILENAME]\n" - " [-g GITHUB_URL] [-u USER[:PASSWORD] | -U] FILES ...", argv0); + " [-g URL] [-u USER[:PASSWORD] | -U] FILES ...", argv0); } int main(int argc, char *argv[]) { + char *js, *url; + Str resstr; + ARGBEGIN { case 'e': gist = EARGF(usage(1)); @@ -237,7 +216,15 @@ main(int argc, char *argv[]) if (gist && !user) die(1, "%s: cannot edit Gists without user", argv0); - gs_new(argv, argc); + 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; } diff --git a/util.c b/util.c @@ -38,22 +38,6 @@ erealloc(void *p, size_t size) return p; } -char * -estrdup(char *s) -{ - if (!(s = strdup(s))) - die(1, "strdup:"); - - return s; -} - -void -efree(void *p) -{ - if (p) - free(p); -} - void die(int eval, const char *fmt, ...) { diff --git a/util.h b/util.h @@ -1,14 +1,6 @@ /* See LICENSE file for copyright and license details. */ -#define MAX(A, B) ((A) > (B) ? (A) : (B)) -#define MIN(A, B) ((A) < (B) ? (A) : (B)) -#define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B)) -#define LEN(X) (sizeof(X) / sizeof((X)[0])) - void *ecalloc(size_t nmemb, size_t size); void *emalloc(size_t size); void *erealloc(void *p, size_t size); -char *estrdup(char *s); -void efree(void *p); - void die(int eval, const char *fmt, ...);