diff options
Diffstat (limited to 'src/preproc.c')
-rw-r--r-- | src/preproc.c | 258 |
1 files changed, 129 insertions, 129 deletions
diff --git a/src/preproc.c b/src/preproc.c index 4d69602..53bea14 100644 --- a/src/preproc.c +++ b/src/preproc.c @@ -1,4 +1,4 @@ -/* grecs - Gray's Extensible Configuration System +/* argot - Gray's Extensible Configuration System Copyright (C) 2007-2016 Sergey Poznyakoff Grecs is free software; you can redistribute it and/or modify it @@ -17,7 +17,7 @@ #ifdef HAVE_CONFIG_H # include <config.h> #endif -#include <grecs.h> +#include <argot.h> #include <wordsplit.h> #include <sys/types.h> #include <sys/stat.h> @@ -33,8 +33,8 @@ #include <unistd.h> -int grecs_log_to_stderr = 1; -void (*grecs_log_setup_hook) () = NULL; +int argot_log_to_stderr = 1; +void (*argot_log_setup_hook) () = NULL; struct input_file_ident { ino_t i_node; @@ -43,14 +43,14 @@ struct input_file_ident { struct buffer_ctx { struct buffer_ctx *prev; /* Pointer to previous context */ - grecs_locus_t locus; /* Current input location */ + argot_locus_t locus; /* Current input location */ size_t namelen; /* Length of the file name */ size_t xlines; /* Number of #line directives output so far */ struct input_file_ident id; FILE *infile; }; -extern int grecs_grecs__flex_debug; +extern int argot_argot__flex_debug; static struct buffer_ctx *context_stack; static char *linebufbase = NULL; static size_t linebufsize = 0; @@ -73,7 +73,7 @@ static int pop_source (void); static int parse_include (const char *text, int once); ssize_t -grecs_getline(char **pbuf, size_t *psize, FILE *fp) +argot_getline(char **pbuf, size_t *psize, FILE *fp) { char *buf = *pbuf; size_t size = *psize; @@ -81,15 +81,15 @@ grecs_getline(char **pbuf, size_t *psize, FILE *fp) if (!buf) { size = 1; - buf = grecs_malloc(size); + buf = argot_malloc(size); } do { if (off == size - 1) { size_t nsize = 2 * size; if (nsize < size) - grecs_alloc_die(); - buf = grecs_realloc(buf, nsize); + argot_alloc_die(); + buf = argot_realloc(buf, nsize); size = nsize; } if (!fgets(buf + off, size - off, fp)) { @@ -115,7 +115,7 @@ putback(const char *str) len = strlen(str) + 1; if (len > putback_max) { putback_max = len; - putback_buffer = grecs_realloc(putback_buffer, putback_max); + putback_buffer = argot_realloc(putback_buffer, putback_max); } strcpy(putback_buffer, str); putback_size = len - 1; @@ -127,18 +127,18 @@ pp_line_stmt() size_t ls_size; size_t pb_size; - if (grecs_asprintf(&linebufbase, &linebufsize, + if (argot_asprintf(&linebufbase, &linebufsize, "#line %lu \"%s\" %lu\n", (unsigned long) POINT.line, POINT.file, (unsigned long) context_stack->xlines)) - grecs_alloc_die(); + argot_alloc_die(); ls_size = strlen(linebufbase); pb_size = putback_size + ls_size + 1; if (pb_size > putback_max) { putback_max = pb_size; - putback_buffer = grecs_realloc(putback_buffer, putback_max); + putback_buffer = argot_realloc(putback_buffer, putback_max); } context_stack->xlines++; @@ -159,7 +159,7 @@ next_line() if (putback_size) { if (putback_size + 1 > bufsize) { bufsize = putback_size + 1; - linebuf = grecs_realloc(linebuf, bufsize); + linebuf = argot_realloc(linebuf, bufsize); } strcpy(linebuf, putback_buffer); rc = putback_size; @@ -168,13 +168,13 @@ next_line() else if (!context_stack) return 0; else - rc = grecs_getline(&linebuf, &bufsize, INFILE); + rc = argot_getline(&linebuf, &bufsize, INFILE); } while (rc == -1 && pop_source() == 0); return rc; } size_t -grecs_preproc_fill_buffer(char *buf, size_t size) +argot_preproc_fill_buffer(char *buf, size_t size) { size_t bufsize = size; @@ -239,27 +239,27 @@ ctx_lookup(struct stat *st) return ctx; } -const char *grecs_preprocessor = NULL; -static struct grecs_list *grecs_usr_include_path; -static struct grecs_list *grecs_std_include_path; +const char *argot_preprocessor = NULL; +static struct argot_list *argot_usr_include_path; +static struct argot_list *argot_std_include_path; size_t -grecs_include_path_count(int flag) +argot_include_path_count(int flag) { size_t count = 0; - if (flag & GRECS_STD_INCLUDE) - count += grecs_list_size(grecs_std_include_path); - if (flag & GRECS_USR_INCLUDE) - count += grecs_list_size(grecs_usr_include_path); + if (flag & ARGOT_STD_INCLUDE) + count += argot_list_size(argot_std_include_path); + if (flag & ARGOT_USR_INCLUDE) + count += argot_list_size(argot_usr_include_path); return count; } static int -foreach_dir(struct grecs_list *list, int flag, +foreach_dir(struct argot_list *list, int flag, int (*fun)(int, const char *, void *), void *data) { int rc = 0; - struct grecs_list_entry *ep; + struct argot_list_entry *ep; for (ep = list->head; rc == 0 && ep; ep = ep->next) rc = fun(flag, ep->data, data); @@ -267,15 +267,15 @@ foreach_dir(struct grecs_list *list, int flag, } int -grecs_foreach_include_dir(int flag, int (*fun)(int, const char *, void *), +argot_foreach_include_dir(int flag, int (*fun)(int, const char *, void *), void *data) { int rc = 0; - if (flag & GRECS_STD_INCLUDE) - rc = foreach_dir(grecs_std_include_path, GRECS_STD_INCLUDE, fun, data); - if (rc == 0 && (flag & GRECS_USR_INCLUDE)) - rc = foreach_dir(grecs_usr_include_path, GRECS_USR_INCLUDE, fun, data); + if (flag & ARGOT_STD_INCLUDE) + rc = foreach_dir(argot_std_include_path, ARGOT_STD_INCLUDE, fun, data); + if (rc == 0 && (flag & ARGOT_USR_INCLUDE)) + rc = foreach_dir(argot_usr_include_path, ARGOT_USR_INCLUDE, fun, data); return rc; } @@ -289,9 +289,9 @@ struct file_data { }; static int -pp_list_find(struct grecs_list *list, struct file_data *dptr) +pp_list_find(struct argot_list *list, struct file_data *dptr) { - struct grecs_list_entry *ep; + struct argot_list_entry *ep; if (!list) return 0; @@ -300,7 +300,7 @@ pp_list_find(struct grecs_list *list, struct file_data *dptr) size_t size = strlen (dir) + 1 + dptr->namelen + 1; if (size > dptr->buflen) { dptr->buflen = size; - dptr->buf = grecs_realloc(dptr->buf, dptr->buflen); + dptr->buf = argot_realloc(dptr->buf, dptr->buflen); } strcpy(dptr->buf, dir); strcat(dptr->buf, "/"); @@ -313,38 +313,38 @@ pp_list_find(struct grecs_list *list, struct file_data *dptr) static void incl_free(void *data) { - grecs_free(data); + argot_free(data); } void -grecs_include_path_clear() +argot_include_path_clear() { - if (grecs_usr_include_path) - grecs_list_clear(grecs_usr_include_path); - if (grecs_std_include_path) - grecs_list_clear(grecs_std_include_path); + if (argot_usr_include_path) + argot_list_clear(argot_usr_include_path); + if (argot_std_include_path) + argot_list_clear(argot_std_include_path); } void -grecs_include_path_setup_v(char **dirs) +argot_include_path_setup_v(char **dirs) { - if (!grecs_usr_include_path) { - grecs_usr_include_path = grecs_list_create(); - grecs_usr_include_path->free_entry = incl_free; + if (!argot_usr_include_path) { + argot_usr_include_path = argot_list_create(); + argot_usr_include_path->free_entry = incl_free; } - grecs_std_include_path = grecs_list_create(); - grecs_std_include_path->free_entry = incl_free; + argot_std_include_path = argot_list_create(); + argot_std_include_path->free_entry = incl_free; if (dirs) { int i; for (i = 0; dirs[i]; i++) /* FIXME: Element never freed */ - grecs_list_append(grecs_std_include_path, - grecs_strdup(dirs[i])); + argot_list_append(argot_std_include_path, + argot_strdup(dirs[i])); } } void -grecs_include_path_setup(const char *dir, ...) +argot_include_path_setup(const char *dir, ...) { const char *p; char **argv = NULL; @@ -360,29 +360,29 @@ grecs_include_path_setup(const char *dir, ...) argc = 16; else argc += 16; - argv = grecs_realloc(argv, argc * sizeof(argv[0])); + argv = argot_realloc(argv, argc * sizeof(argv[0])); } argv[argi++] = (char*) p; if (!p) break; p = va_arg(ap, const char*); } - grecs_include_path_setup_v(argv); - grecs_free(argv); + argot_include_path_setup_v(argv); + argot_free(argv); va_end(ap); } void -grecs_preproc_add_include_dir(char *dir) +argot_preproc_add_include_dir(char *dir) { - if (!grecs_usr_include_path) { - grecs_usr_include_path = grecs_list_create(); - grecs_usr_include_path->free_entry = incl_free; + if (!argot_usr_include_path) { + argot_usr_include_path = argot_list_create(); + argot_usr_include_path->free_entry = incl_free; } - grecs_list_append(grecs_usr_include_path, grecs_strdup(dir)); + argot_list_append(argot_usr_include_path, argot_strdup(dir)); } -static struct grecs_symtab *incl_sources; +static struct argot_symtab *incl_sources; /* Calculate the hash of a struct input_file_ident. */ static unsigned @@ -415,7 +415,7 @@ source_lookup(struct stat *st) int install = 1; if (!incl_sources) { - incl_sources = grecs_symtab_create( + incl_sources = argot_symtab_create( sizeof(struct input_file_ident), incl_hasher, incl_compare, @@ -423,13 +423,13 @@ source_lookup(struct stat *st) NULL,/*FIXME: alloc*/ NULL); if (!incl_sources) - grecs_alloc_die(); + argot_alloc_die(); } key.i_node = st->st_ino; key.device = st->st_dev; - if (!grecs_symtab_lookup_or_install(incl_sources, &key, &install)) - grecs_alloc_die(); + if (!argot_symtab_lookup_or_install(incl_sources, &key, &install)) + argot_alloc_die(); return !install; } @@ -444,30 +444,30 @@ push_source(const char *name, int once) if (context_stack) { if (rc) { - grecs_error(&LOCUS, errno, + argot_error(&LOCUS, errno, _("Cannot stat `%s'"), name); return 1; } if (POINT.file && STAT_ID_EQ(st, context_stack->id)) { - grecs_error(&LOCUS, 0, _("Recursive inclusion")); + argot_error(&LOCUS, 0, _("Recursive inclusion")); return 1; } if ((ctx = ctx_lookup(&st))) { - grecs_error(&LOCUS, 0, _("Recursive inclusion")); + argot_error(&LOCUS, 0, _("Recursive inclusion")); if (ctx->prev) - grecs_error(&ctx->prev->locus, 0, + argot_error(&ctx->prev->locus, 0, _("`%s' already included here"), name); else - grecs_error(&LOCUS, 0, + argot_error(&LOCUS, 0, _("`%s' already included at top level"), name); return 1; } } else if (rc) { - grecs_error(NULL, errno, _("Cannot stat `%s'"), name); + argot_error(NULL, errno, _("Cannot stat `%s'"), name); return 1; } @@ -476,14 +476,14 @@ push_source(const char *name, int once) fp = fopen(name, "r"); if (!fp) { - grecs_error(context_stack ? &LOCUS : NULL, errno, + argot_error(context_stack ? &LOCUS : NULL, errno, _("Cannot open `%s'"), name); return 1; } /* Push current context */ - ctx = grecs_malloc(sizeof(*ctx)); - ctx->locus.beg.file = grecs_install_text(name); + ctx = argot_malloc(sizeof(*ctx)); + ctx->locus.beg.file = argot_install_text(name); ctx->locus.beg.line = 1; ctx->locus.beg.col = 0; ctx->locus.end.file = NULL; @@ -496,7 +496,7 @@ push_source(const char *name, int once) ctx->prev = context_stack; context_stack = ctx; - if (grecs_grecs__flex_debug) + if (argot_argot__flex_debug) fprintf (stderr, "Processing file `%s'\n", name); pp_line_stmt(); @@ -516,7 +516,7 @@ pop_source() /* Restore previous context */ ctx = context_stack->prev; - grecs_free(context_stack); + argot_free(context_stack); context_stack = ctx; if (include_pos < include_glob.gl_pathc) { @@ -528,14 +528,14 @@ pop_source() } if (!context_stack) { - if (grecs_grecs__flex_debug) + if (argot_argot__flex_debug) fprintf(stderr, "End of input\n"); return 1; } POINT.line++; - if (grecs_grecs__flex_debug) + if (argot_argot__flex_debug) fprintf(stderr, "Resuming file `%s' at line %lu\n", POINT.file, (unsigned long) POINT.line); @@ -545,7 +545,7 @@ pop_source() } char * -grecs_find_include_file(const char *name, int allow_cwd) +argot_find_include_file(const char *name, int allow_cwd) { static char *cwd = "."; struct file_data fd; @@ -556,17 +556,17 @@ grecs_find_include_file(const char *name, int allow_cwd) fd.buflen = 0; fd.found = 0; - if (!grecs_usr_include_path) - grecs_include_path_setup(NULL); + if (!argot_usr_include_path) + argot_include_path_setup(NULL); if (allow_cwd) { - grecs_list_append(grecs_usr_include_path, cwd); - pp_list_find(grecs_usr_include_path, &fd); - grecs_list_remove_tail(grecs_usr_include_path); + argot_list_append(argot_usr_include_path, cwd); + pp_list_find(argot_usr_include_path, &fd); + argot_list_remove_tail(argot_usr_include_path); } else - pp_list_find(grecs_usr_include_path, &fd); + pp_list_find(argot_usr_include_path, &fd); if (!fd.found) { - pp_list_find(grecs_std_include_path, &fd); + pp_list_find(argot_std_include_path, &fd); if (!fd.found) return NULL; } @@ -592,10 +592,10 @@ parse_include(const char *text, int once) int rc = 1; if (wordsplit(text, &ws, WRDSF_DEFFLAGS)) - grecs_error(&LOCUS, 0, _("Cannot parse include line")); + argot_error(&LOCUS, 0, _("Cannot parse include line")); else if (ws.ws_wordc != 2) { wordsplit_free(&ws); - grecs_error(&LOCUS, 0, _("invalid include statement")); + argot_error(&LOCUS, 0, _("invalid include statement")); } else { size_t len; int allow_cwd; @@ -617,18 +617,18 @@ parse_include(const char *text, int once) include_once = once; break; case GLOB_NOSPACE: - grecs_alloc_die(); + argot_alloc_die(); case GLOB_NOMATCH: break; default: - grecs_error(&LOCUS, 0, _("read error")); + argot_error(&LOCUS, 0, _("read error")); } p = NULL; } else if (p[0] != '/') { char *q = p; - p = grecs_find_include_file(q, allow_cwd); + p = argot_find_include_file(q, allow_cwd); if (!p) - grecs_error(&LOCUS, 0, + argot_error(&LOCUS, 0, _("%s: No such file or directory"), q); } @@ -639,28 +639,28 @@ parse_include(const char *text, int once) else if (include_pos < include_glob.gl_pathc) rc = push_source(include_glob.gl_pathv[include_pos++], once); - grecs_free(tmp); + argot_free(tmp); wordsplit_free(&ws); return rc; } int -grecs_preproc_init(const char *name) +argot_preproc_init(const char *name) { return push_source(name, 0); } void -grecs_preproc_done() +argot_preproc_done() { - grecs_symtab_free(incl_sources); + argot_symtab_free(incl_sources); incl_sources = NULL; - grecs_free(linebuf); + argot_free(linebuf); linebuf = NULL; bufsize = 0; - grecs_free(putback_buffer); + argot_free(putback_buffer); putback_buffer = NULL; putback_size = putback_max = 0; @@ -670,51 +670,51 @@ grecs_preproc_done() } int -grecs_preproc_run(const char *config_file, const char *extpp) +argot_preproc_run(const char *config_file, const char *extpp) { size_t i; char buffer[512]; - if (grecs_preproc_init(config_file)) + if (argot_preproc_init(config_file)) return 1; if (extpp) { FILE *outfile; char *setup_file; char *cmd = NULL; - setup_file = grecs_find_include_file("pp-setup", 1); + setup_file = argot_find_include_file("pp-setup", 1); if (setup_file) { size_t size = 0; - if (grecs_asprintf(&cmd, &size, + if (argot_asprintf(&cmd, &size, "%s %s -", extpp, setup_file)) - grecs_alloc_die(); - grecs_free(setup_file); + argot_alloc_die(); + argot_free(setup_file); } else - cmd = grecs_strdup(extpp); + cmd = argot_strdup(extpp); /*FIXME_DEBUG_F1 (2, "Running preprocessor: `%s'", cmd);*/ outfile = popen(cmd, "w"); if (!outfile) { - grecs_error(NULL, errno, + argot_error(NULL, errno, _("Unable to start external preprocessor `%s'"), cmd); - grecs_free(cmd); + argot_free(cmd); return 1; } - while ((i = grecs_preproc_fill_buffer(buffer, sizeof buffer))) + while ((i = argot_preproc_fill_buffer(buffer, sizeof buffer))) fwrite(buffer, 1, i, outfile); pclose(outfile); - grecs_free(cmd); + argot_free(cmd); } else { - while ((i = grecs_preproc_fill_buffer(buffer, sizeof buffer))) + while ((i = argot_preproc_fill_buffer(buffer, sizeof buffer))) fwrite(buffer, 1, i, stdout); } - grecs_preproc_done(); + argot_preproc_done(); return 0; } FILE * -grecs_preproc_extrn_start(const char *file_name, pid_t *ppid) +argot_preproc_extrn_start(const char *file_name, pid_t *ppid) { int pout[2]; pid_t pid; @@ -724,7 +724,7 @@ grecs_preproc_extrn_start(const char *file_name, pid_t *ppid) /*FIXME_DEBUG_F1 (2, "Running preprocessor: `%s'", ppcmd);*/ if (pipe(pout)) { - grecs_error(NULL, errno, "pipe"); + argot_error(NULL, errno, "pipe"); return NULL; } switch (pid = fork()) { @@ -732,7 +732,7 @@ grecs_preproc_extrn_start(const char *file_name, pid_t *ppid) case 0: if (pout[1] != 1) { if (dup2(pout[1], 1) == -1) { - grecs_error(NULL, errno, "dup2"); + argot_error(NULL, errno, "dup2"); exit(127); } } @@ -741,7 +741,7 @@ grecs_preproc_extrn_start(const char *file_name, pid_t *ppid) for (i = getdtablesize(); i > 2; i--) close(i); - if (!grecs_log_to_stderr) { + if (!argot_log_to_stderr) { int p[2]; char *buf = NULL; size_t size = 0; @@ -749,49 +749,49 @@ grecs_preproc_extrn_start(const char *file_name, pid_t *ppid) signal(SIGCHLD, SIG_DFL); if (pipe(p)) { - grecs_error(NULL, errno, "pipe"); + argot_error(NULL, errno, "pipe"); exit(127); } switch (pid = fork()) { /* Grandchild */ case 0: if (p[1] != 2 && dup2(p[1], 2) == -1) { - grecs_error(NULL, errno, "dup2"); + argot_error(NULL, errno, "dup2"); exit(127); } close(p[0]); - if (grecs_preproc_run(file_name, - grecs_preprocessor)) + if (argot_preproc_run(file_name, + argot_preprocessor)) exit(127); exit(0); case -1: /* Fork failed */ - if (grecs_log_setup_hook) - grecs_log_setup_hook(); - grecs_error(NULL, errno, _("Cannot run `%s'"), - grecs_preprocessor); + if (argot_log_setup_hook) + argot_log_setup_hook(); + argot_error(NULL, errno, _("Cannot run `%s'"), + argot_preprocessor); exit(127); default: /* Sub-master */ close (p[1]); fp = fdopen(p[0], "r"); - if (grecs_log_setup_hook) - grecs_log_setup_hook(); - while (grecs_getline(&buf, &size, fp) > 0) - grecs_error(NULL, 0, "%s", buf); + if (argot_log_setup_hook) + argot_log_setup_hook(); + while (argot_getline(&buf, &size, fp) > 0) + argot_error(NULL, 0, "%s", buf); } } else { - grecs_preproc_run(file_name, grecs_preprocessor); + argot_preproc_run(file_name, argot_preprocessor); } exit (0); case -1: /* Fork failed */ - grecs_error(NULL, errno, _("Cannot run `%s'"), - grecs_preprocessor); + argot_error(NULL, errno, _("Cannot run `%s'"), + argot_preprocessor); break; default: @@ -804,7 +804,7 @@ grecs_preproc_extrn_start(const char *file_name, pid_t *ppid) } void -grecs_preproc_extrn_shutdown(pid_t pid) +argot_preproc_extrn_shutdown(pid_t pid) { int status; waitpid(pid, &status, 0); |