Index: src_plugins/fp_wget/Makefile =================================================================== --- src_plugins/fp_wget/Makefile (nonexistent) +++ src_plugins/fp_wget/Makefile (revision 1427) @@ -0,0 +1,2 @@ +CFLAGS = -Wall -g +tester: tester.o gedasymbols.o fp_wget.o Index: src_plugins/fp_wget/fp_wget.c =================================================================== --- src_plugins/fp_wget/fp_wget.c (nonexistent) +++ src_plugins/fp_wget/fp_wget.c (revision 1427) @@ -0,0 +1,95 @@ +#include +#include +#include "fp_wget.h" + +enum { + FCTX_INVALID = 0, + FCTX_POPEN, + FCTX_FOPEN, + FCTX_NOP +}; + +const char *wget_cmd = "wget -U 'pcb-rnd-fp_wget'"; +int fp_wget_offline = 1; + +static int mkdirp(const char *dir) +{ +/* TODO */ + char buff[8192]; + sprintf(buff, "mkdir -p '%s'", dir); + return system(buff); +} + +int fp_wget_open(const char *url, const char *cache_path, FILE **f, int *fctx, int update) +{ + char *cmd, *upds; + int wl = strlen(wget_cmd), ul = strlen(url), cl = strlen(cache_path); + cmd = malloc(wl+ul*2+cl+32); + + *fctx = FCTX_INVALID; + + if (update) + upds = "-c"; + else + upds = ""; + + if (cache_path == NULL) { + sprintf(cmd, "%s -O - %s '%s'", wget_cmd, upds, url); + if (f == NULL) + goto error; + if (!fp_wget_offline) + *f = popen(cmd, "r"); + if (*f == NULL) + goto error; + *fctx = FCTX_POPEN; + } + else { + char *cdir; + cdir = strstr(url, "://"); + if (cdir == NULL) + goto error; + cdir += 3; + sprintf(cmd, "%s -O '%s/%s' %s '%s'", wget_cmd, cache_path, cdir, upds, url); + if (!fp_wget_offline) + system(cmd); + if (f != NULL) { + char *end; + sprintf(cmd, "%s/%s", cache_path, cdir); + end = strrchr(cmd, '/'); + if (end != NULL) { + *end = '\0'; + if (mkdirp(cmd) != 0) + goto error; + *end = '/'; + } + *f = fopen(cmd, "r"); + if (*f == NULL) + goto error; + *fctx = FCTX_FOPEN; + } + else + *fctx = FCTX_NOP; + } + free(cmd); + return 0; + + error:; + free(cmd); + return -1; +} + +int fp_wget_close(FILE **f, int *fctx) +{ + if (*fctx == FCTX_NOP) + return 0; + + if (*f == NULL) + return -1; + + switch(*fctx) { + case FCTX_POPEN: pclose(*f); *f = NULL; return 0; + case FCTX_FOPEN: fclose(*f); *f = NULL; return 0; + } + + return -1; +} Index: src_plugins/fp_wget/fp_wget.h =================================================================== --- src_plugins/fp_wget/fp_wget.h (nonexistent) +++ src_plugins/fp_wget/fp_wget.h (revision 1427) @@ -0,0 +1,4 @@ +#include + +int fp_wget_open(const char *url, const char *cache_path, FILE **f, int *fctx, int update); +int fp_wget_close(FILE **f, int *fctx); Index: src_plugins/fp_wget/gedasymbols.c =================================================================== --- src_plugins/fp_wget/gedasymbols.c (nonexistent) +++ src_plugins/fp_wget/gedasymbols.c (revision 1427) @@ -0,0 +1,88 @@ +#include +#include +#include +#include "fp_wget.h" + + +#define CGI_URL "http://www.gedasymbols.org/scripts/global_list.cgi" +static const char *url_idx_md5 = CGI_URL "?md5"; +static const char *gedasym_cache = "fp_wget_cache"; +static const char *last_sum_fn = "fp_wget_cache/gedasymbols.last"; + +static char *load_md5_sum(FILE *f) +{ + char *s, sum[64]; + + if (f == NULL) + return NULL; + + *sum = '\0'; + fgets(sum, sizeof(sum), f); + sum[sizeof(sum)-1] = '\0'; + + for(s = sum;; s++) { + if ((*s == '\0') || (isspace(*s))) { + if ((s - sum) == 32) { + *s = '\0'; + return strdup(sum); + } + else + return NULL; + } + if (isdigit(*s)) + continue; + if ((*s >= 'a') && (*s <= 'f')) + continue; + if ((*s >= 'A') && (*s <= 'F')) + continue; + return NULL; + } +} + +static int md5_cmp_free(const char *last_fn, char *md5_last, char *md5_new) +{ + int changed = 0; + + if ((md5_last == NULL) || (strcmp(md5_last, md5_new) != 0)) { + FILE *f; + f = fopen(last_fn, "w"); + fputs(md5_new, f); + fclose(f); + changed = 1; + } + if (md5_last != NULL) + free(md5_last); + free(md5_new); + return changed; +} + + +int gedasym_init() +{ + FILE *f; + int fctx; + char *md5_last, *md5_new; + + if (fp_wget_open(url_idx_md5, gedasym_cache, &f, &fctx, 0) != 0) + return -1; + md5_new = load_md5_sum(f); + fp_wget_close(&f, &fctx); + + if (md5_new == NULL) + return -1; + + f = fopen(last_sum_fn, "r"); + md5_last = load_md5_sum(f); + if (f != NULL) + fclose(f); + + printf("old='%s' new='%s'\n", md5_last, md5_new); + + if (!md5_cmp_free(last_sum_fn, md5_last, md5_new)) { + printf("no chg.\n"); + return 0; + } + + printf("update!\n"); + return 0; +} Index: src_plugins/fp_wget/gedasymbols.h =================================================================== --- src_plugins/fp_wget/gedasymbols.h (nonexistent) +++ src_plugins/fp_wget/gedasymbols.h (revision 1427) @@ -0,0 +1 @@ +int gedasym_init(); Index: src_plugins/fp_wget/tester.c =================================================================== --- src_plugins/fp_wget/tester.c (nonexistent) +++ src_plugins/fp_wget/tester.c (revision 1427) @@ -0,0 +1,5 @@ +#include "gedasymbols.h" +int main() +{ + gedasym_init(); +}