aboutsummaryrefslogtreecommitdiff
path: root/src/preproc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/preproc.c')
-rw-r--r--src/preproc.c222
1 files changed, 90 insertions, 132 deletions
diff --git a/src/preproc.c b/src/preproc.c
index 7ea9442..06924c1 100644
--- a/src/preproc.c
+++ b/src/preproc.c
@@ -34,14 +34,12 @@
int grecs_log_to_stderr = 1;
void (*grecs_log_setup_hook) () = NULL;
-struct input_file_ident
-{
+struct input_file_ident {
ino_t i_node;
dev_t device;
};
-struct buffer_ctx
-{
+struct buffer_ctx {
struct buffer_ctx *prev; /* Pointer to previous context */
grecs_locus_t locus; /* Current input location */
size_t namelen; /* Length of the file name */
@@ -75,19 +73,12 @@ pp_getline (char **pbuf, size_t *psize, FILE *fp)
size_t size = *psize;
ssize_t off = 0;
- do
- {
- size_t len;
-
- if (off == size - 1)
- {
- if (!buf)
- {
+ do {
+ if (off == size - 1) {
+ if (!buf) {
size = 1;
buf = grecs_malloc(size);
- }
- else
- {
+ } else {
size_t nsize = 2 * size;
if (nsize < size)
grecs_alloc_die();
@@ -95,8 +86,7 @@ pp_getline (char **pbuf, size_t *psize, FILE *fp)
size = nsize;
}
}
- if (!fgets (buf + off, size - off, fp))
- {
+ if (!fgets(buf + off, size - off, fp)) {
if (off == 0)
off = -1;
break;
@@ -110,7 +100,6 @@ pp_getline (char **pbuf, size_t *psize, FILE *fp)
return off;
}
-
static void
putback(const char *str)
{
@@ -119,8 +108,7 @@ putback (const char *str)
if (!*str)
return;
len = strlen(str) + 1;
- if (len > putback_max)
- {
+ if (len > putback_max) {
putback_max = len;
putback_buffer = grecs_realloc(putback_buffer, putback_max);
}
@@ -134,7 +122,8 @@ pp_line_stmt ()
size_t ls_size;
size_t pb_size;
- if (grecs_asprintf (&linebufbase, &linebufsize, "#line %lu \"%s\" %lu\n",
+ if (grecs_asprintf(&linebufbase, &linebufsize,
+ "#line %lu \"%s\" %lu\n",
(unsigned long) LOCUS.line,
LOCUS.file, (unsigned long) context_stack->xlines))
grecs_alloc_die();
@@ -142,8 +131,7 @@ pp_line_stmt ()
ls_size = strlen(linebufbase);
pb_size = putback_size + ls_size + 1;
- if (pb_size > putback_max)
- {
+ if (pb_size > putback_max) {
putback_max = pb_size;
putback_buffer = grecs_realloc(putback_buffer, putback_max);
}
@@ -162,12 +150,9 @@ next_line ()
{
ssize_t rc;
- do
- {
- if (putback_size)
- {
- if (putback_size + 1 > bufsize)
- {
+ do {
+ if (putback_size) {
+ if (putback_size + 1 > bufsize) {
bufsize = putback_size + 1;
linebuf = grecs_realloc(linebuf, bufsize);
}
@@ -179,8 +164,7 @@ next_line ()
return 0;
else
rc = pp_getline(&linebuf, &bufsize, INFILE);
- }
- while (rc == -1 && pop_source () == 0);
+ } while (rc == -1 && pop_source() == 0);
return rc;
}
@@ -189,33 +173,27 @@ grecs_preproc_fill_buffer (char *buf, size_t size)
{
size_t bufsize = size;
- while (next_line () > 0)
- {
+ while (next_line() > 0) {
char *p;
size_t len;
int is_line = 0;
for (p = linebuf; *p && isspace(*p); p++)
;
- if (*p == '#')
- {
+ if (*p == '#') {
size_t l;
for (p++; *p && isspace(*p); p++)
;
l = strlen(p);
- if (STRMATCH (p, l, "include_once"))
- {
+ if (STRMATCH(p, l, "include_once")) {
if (parse_include(linebuf, 1))
putback("/*include_once*/\n");
continue;
- }
- else if (STRMATCH (p, l, "include"))
- {
+ } else if (STRMATCH(p, l, "include")) {
if (parse_include(linebuf, 0))
putback("/*include*/\n");
continue;
- }
- else if (STRMATCH (p, l, "line"))
+ } else if (STRMATCH(p, l, "line"))
is_line = 1;
}
@@ -228,8 +206,7 @@ grecs_preproc_fill_buffer (char *buf, size_t size)
buf += len;
size -= len;
- if (size == 0)
- {
+ if (size == 0) {
putback(linebuf + len);
break;
}
@@ -261,8 +238,7 @@ const char *grecs_preprocessor = NULL;
static struct grecs_list *include_path;
static struct grecs_list *std_include_path;
-struct file_data
-{
+struct file_data {
const char *name;
size_t namelen;
char *buf;
@@ -275,12 +251,10 @@ pp_list_find (struct grecs_list *list, struct file_data *dptr)
{
struct grecs_list_entry *ep;
- for (ep = list->head; !dptr->found && ep; ep = ep->next)
- {
+ for (ep = list->head; !dptr->found && ep; ep = ep->next) {
const char *dir = ep->data;
size_t size = strlen (dir) + 1 + dptr->namelen + 1;
- if (size > dptr->buflen)
- {
+ if (size > dptr->buflen) {
dptr->buflen = size;
dptr->buf = grecs_realloc(dptr->buf, dptr->buflen);
}
@@ -292,18 +266,27 @@ pp_list_find (struct grecs_list *list, struct file_data *dptr)
return dptr->found;
}
+static void
+incl_free(void *data)
+{
+ free(data);
+}
+
void
grecs_include_path_setup_v(char **dirs)
{
- if (!include_path)
+ if (!include_path) {
include_path = grecs_list_create();
+ include_path->free_entry = incl_free;
+ }
std_include_path = grecs_list_create();
- if (dirs)
- {
+ std_include_path->free_entry = incl_free;
+ if (dirs) {
int i;
for (i = 0; dirs[i]; i++)
/* FIXME: Element never freed */
- grecs_list_append (std_include_path, grecs_strdup (dirs[i]));
+ grecs_list_append(std_include_path,
+ grecs_strdup(dirs[i]));
}
}
@@ -318,10 +301,8 @@ grecs_include_path_setup (const char *dir, ...)
va_start(ap, dir);
p = dir;
- while (1)
- {
- if (argi == argc)
- {
+ while (1) {
+ if (argi == argc) {
if (argc == 0)
argc = 16;
else
@@ -341,9 +322,11 @@ grecs_include_path_setup (const char *dir, ...)
void
grecs_preproc_add_include_dir(char *dir)
{
- if (!include_path)
+ if (!include_path) {
include_path = grecs_list_create();
- grecs_list_append (include_path, dir);
+ include_path->free_entry = incl_free;
+ }
+ grecs_list_append(include_path, grecs_strdup(dir));
}
static struct grecs_symtab *incl_sources;
@@ -365,21 +348,15 @@ incl_compare (void const *data1, void const *data2)
return !(id1->device == id2->device && id1->i_node == id2->i_node);
}
-static void
-incl_free (void *data)
-{
- free (data);
-}
-
static int
source_lookup(struct stat *st)
{
struct input_file_ident key;
int install = 1;
- if (!incl_sources)
- {
- incl_sources = grecs_symtab_create(sizeof (struct input_file_ident),
+ if (!incl_sources) {
+ incl_sources = grecs_symtab_create(
+ sizeof(struct input_file_ident),
incl_hasher,
incl_compare,
NULL,
@@ -405,34 +382,31 @@ push_source (const char *name, int once)
struct stat st;
int rc = stat(name, &st);
- if (context_stack)
- {
- if (rc)
- {
- grecs_error (&LOCUS, errno, _("Cannot stat `%s'"), name);
+ if (context_stack) {
+ if (rc) {
+ grecs_error(&LOCUS, errno,
+ _("Cannot stat `%s'"), name);
return 1;
}
- if (LOCUS.file && STAT_ID_EQ (st, context_stack->id))
- {
+ if (LOCUS.file && STAT_ID_EQ(st, context_stack->id)) {
grecs_error(&LOCUS, 0, _("Recursive inclusion"));
return 1;
}
- if ((ctx = ctx_lookup (&st)))
- {
+ if ((ctx = ctx_lookup(&st))) {
grecs_error(&LOCUS, 0, _("Recursive inclusion"));
if (ctx->prev)
grecs_error(&ctx->prev->locus, 0,
- _("`%s' already included here"), name);
+ _("`%s' already included here"),
+ name);
else
grecs_error(&LOCUS, 0,
- _("`%s' already included at top level"), name);
+ _("`%s' already included at top level"),
+ name);
return 1;
}
- }
- else if (rc)
- {
+ } else if (rc) {
grecs_error(NULL, errno, _("Cannot stat `%s'"), name);
return 1;
}
@@ -441,8 +415,7 @@ push_source (const char *name, int once)
return -1;
fp = fopen(name, "r");
- if (!fp)
- {
+ if (!fp) {
grecs_error(&LOCUS, errno, _("Cannot open `%s'"), name);
return 1;
}
@@ -482,8 +455,7 @@ pop_source ()
free(context_stack);
context_stack = ctx;
- if (!context_stack)
- {
+ if (!context_stack) {
if (yy_grecs_flex_debug)
fprintf (stderr, "End of input\n");
return 1;
@@ -514,22 +486,19 @@ try_file (const char *name, int allow_cwd, int err_not_found, char **newp)
if (!include_path)
grecs_include_path_setup(NULL);
- if (allow_cwd)
- {
+ if (allow_cwd) {
grecs_list_append(include_path, cwd);
pp_list_find(include_path, &fd);
grecs_list_remove_tail(include_path);
- }
- else
+ } else
pp_list_find(include_path, &fd);
- if (!fd.found)
- {
+ if (!fd.found) {
pp_list_find(std_include_path, &fd);
- if (!fd.found && err_not_found)
- {
- grecs_error (&LOCUS, 0, _("%s: No such file or directory"), name);
+ if (!fd.found && err_not_found) {
+ grecs_error(&LOCUS, 0,
+ _("%s: No such file or directory"), name);
*newp = NULL;
}
}
@@ -548,21 +517,17 @@ parse_include (const char *text, int once)
if (wordsplit(text, &ws, WRDSF_DEFFLAGS))
grecs_error(&LOCUS, 0, _("Cannot parse include line"));
- else if (ws.ws_wordc != 2)
- {
+ else if (ws.ws_wordc != 2) {
wordsplit_free(&ws);
grecs_error(&LOCUS, 0, _("invalid include statement"));
- }
- else
- {
+ } else {
size_t len;
int allow_cwd;
p = ws.ws_wordv[1];
len = strlen (p);
- if (p[0] == '<' && p[len - 1] == '>')
- {
+ if (p[0] == '<' && p[len - 1] == '>') {
allow_cwd = 0;
p[len - 1] = 0;
p++;
@@ -604,25 +569,25 @@ grecs_preproc_run (const char *config_file, const char *extpp)
if (grecs_preproc_init(config_file))
return 1;
- if (extpp)
- {
+ if (extpp) {
FILE *outfile;
char *setup_file;
- char *cmd;
+ char *cmd = NULL;
- if (try_file ("pp-setup", 1, 0, &setup_file))
- {
- asprintf (&cmd, "%s %s -", extpp, setup_file);
+ if (try_file("pp-setup", 1, 0, &setup_file)) {
+ size_t size = 0;
+ if (grecs_asprintf(&cmd, &size,
+ "%s %s -", extpp, setup_file))
+ grecs_alloc_die();
free(setup_file);
- }
- else
+ } else
cmd = grecs_strdup (extpp);
/*FIXME_DEBUG_F1 (2, "Running preprocessor: `%s'", cmd);*/
outfile = popen(cmd, "w");
- if (!outfile)
- {
+ if (!outfile){
grecs_error(NULL, errno,
- _("Unable to start external preprocessor `%s'"), cmd);
+ _("Unable to start external preprocessor `%s'"),
+ cmd);
free(cmd);
return 1;
}
@@ -631,9 +596,7 @@ grecs_preproc_run (const char *config_file, const char *extpp)
fwrite(buffer, 1, i, outfile);
pclose(outfile);
free(cmd);
- }
- else
- {
+ } else {
while ((i = grecs_preproc_fill_buffer(buffer, sizeof buffer)))
fwrite(buffer, 1, i, stdout);
}
@@ -652,12 +615,10 @@ grecs_preproc_extrn_start (const char *file_name, pid_t *ppid)
/*FIXME_DEBUG_F1 (2, "Running preprocessor: `%s'", ppcmd);*/
pipe(pout);
- switch (pid = fork ())
- {
+ switch (pid = fork()) {
/* The child branch. */
case 0:
- if (pout[1] != 1)
- {
+ if (pout[1] != 1) {
close(1);
dup2(pout[1], 1);
}
@@ -666,8 +627,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 (!grecs_log_to_stderr) {
int p[2];
char *buf = NULL;
size_t size = 0;
@@ -675,18 +635,17 @@ grecs_preproc_extrn_start (const char *file_name, pid_t *ppid)
signal(SIGCHLD, SIG_DFL);
pipe(p);
- switch (pid = fork ())
- {
+ switch (pid = fork()) {
/* Grandchild */
case 0:
- if (p[1] != 2)
- {
+ if (p[1] != 2) {
close(2);
dup2(p[1], 2);
}
close(p[0]);
- if (grecs_preproc_run (file_name, grecs_preprocessor))
+ if (grecs_preproc_run(file_name,
+ grecs_preprocessor))
exit(127);
exit(0);
@@ -707,16 +666,15 @@ grecs_preproc_extrn_start (const char *file_name, pid_t *ppid)
while (pp_getline(&buf, &size, fp) > 0)
grecs_error(NULL, 0, "%s", buf);
}
- }
- else
- {
+ } else {
grecs_preproc_run(file_name, grecs_preprocessor);
}
exit (0);
case -1:
/* Fork failed */
- grecs_error (NULL, errno, _("Cannot run `%s'"), grecs_preprocessor);
+ grecs_error(NULL, errno, _("Cannot run `%s'"),
+ grecs_preprocessor);
break;
default:

Return to:

Send suggestions and report system problems to the System administrator.