From 24ec67c9f6375d34d88e79981ed8abbe15a78169 Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Sun, 1 May 2011 21:13:32 +0300 Subject: Reindent all, except wordsplit, which is shared with MU. --- src/Makefile.am | 2 +- src/format.c | 269 +++++++------- src/grecs-gram.y | 1086 ++++++++++++++++++++++++++---------------------------- src/grecs-lex.l | 579 ++++++++++++++--------------- src/grecs.h | 180 ++++----- src/list.c | 177 +++++---- src/preproc.c | 1050 +++++++++++++++++++++++++--------------------------- src/text.c | 36 +- 8 files changed, 1631 insertions(+), 1748 deletions(-) (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 2f35f52..759716b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -32,7 +32,7 @@ EXTRA_DIST=grecs-gram.h $(PP_SETUP_FILE) INCLUDES = -I$(srcdir) @GRECS_INCLUDES@ AM_YFLAGS = -dtv -AM_LFLAGS = -dvp +AM_LFLAGS = -d # Use a modified ylwrap implementation that understands the --prefix option. YLWRAP = $(top_srcdir)/$(grex_topdir)grecs/build-aux/ylwrap --prefix yy_grecs diff --git a/src/format.c b/src/format.c index a1aa67d..fc6c8d6 100644 --- a/src/format.c +++ b/src/format.c @@ -26,164 +26,159 @@ const char * grecs_data_type_string (enum grecs_data_type type) { - switch (type) - { - case grecs_type_void: - return "void"; + switch (type) { + case grecs_type_void: + return "void"; - case grecs_type_string: - return "string"; - - case grecs_type_short: - case grecs_type_ushort: - case grecs_type_int: - case grecs_type_uint: - case grecs_type_long: - case grecs_type_ulong: - case grecs_type_size: - /*FIXME case grecs_type_off:*/ - return "number"; - - case grecs_type_time: - return "time"; - - case grecs_type_bool: - return "boolean"; - - case grecs_type_ipv4: - return "IPv4"; - - case grecs_type_cidr: - return "CIDR"; - - case grecs_type_host: - return "hostname"; - - case grecs_type_sockaddr: - return "sock-addr"; - - case grecs_type_section: - return "section"; - } - return "UNKNOWN?"; + case grecs_type_string: + return "string"; + + case grecs_type_short: + case grecs_type_ushort: + case grecs_type_int: + case grecs_type_uint: + case grecs_type_long: + case grecs_type_ulong: + case grecs_type_size: + /*FIXME case grecs_type_off:*/ + return "number"; + + case grecs_type_time: + return "time"; + + case grecs_type_bool: + return "boolean"; + + case grecs_type_ipv4: + return "IPv4"; + + case grecs_type_cidr: + return "CIDR"; + + case grecs_type_host: + return "hostname"; + + case grecs_type_sockaddr: + return "sock-addr"; + + case grecs_type_section: + return "section"; + } + return "UNKNOWN?"; } static void -format_level (FILE *stream, unsigned level) +format_level(FILE *stream, unsigned level) { - while (level--) - fprintf (stream, " "); + while (level--) + fprintf(stream, " "); } void -grecs_format_docstring (FILE *stream, const char *docstring, unsigned level) +grecs_format_docstring(FILE *stream, const char *docstring, unsigned level) { - size_t len = strlen (docstring); - int width = 78 - level * 2; - - if (width < 0) - { - width = 78; - level = 0; - } - - while (len) - { - size_t seglen; - const char *p; - - for (seglen = 0, p = docstring; p < docstring + width && *p; p++) - { - if (*p == '\n') - { - seglen = p - docstring; - break; - } - if (isspace (*p)) - seglen = p - docstring; + size_t len = strlen(docstring); + int width = 78 - level * 2; + + if (width < 0) { + width = 78; + level = 0; } - if (seglen == 0 || *p == 0) - seglen = p - docstring; - - format_level (stream, level); - fprintf (stream, "# "); - fwrite (docstring, seglen, 1, stream); - fputc ('\n', stream); - len -= seglen; - docstring += seglen; - if (*docstring == '\n') - { - docstring++; - len--; + + while (len) { + size_t seglen; + const char *p; + + for (seglen = 0, p = docstring; p < docstring + width && *p; + p++) { + if (*p == '\n') { + seglen = p - docstring; + break; + } + if (isspace(*p)) + seglen = p - docstring; + } + if (seglen == 0 || *p == 0) + seglen = p - docstring; + + format_level(stream, level); + fprintf(stream, "# "); + fwrite(docstring, seglen, 1, stream); + fputc('\n', stream); + len -= seglen; + docstring += seglen; + if (*docstring == '\n') { + docstring++; + len--; + } else + while (*docstring && isspace(*docstring)) { + docstring++; + len--; + } } - else - while (*docstring && isspace (*docstring)) - { - docstring++; - len--; - } - } } void -grecs_format_simple_statement (FILE *stream, struct grecs_keyword *kwp, - unsigned level) +grecs_format_simple_statement(FILE *stream, struct grecs_keyword *kwp, + unsigned level) { - const char *argstr; - - if (kwp->docstring) - grecs_format_docstring (stream, kwp->docstring, level); - format_level (stream, level); - - if (kwp->argname) - argstr = kwp->argname; - else - argstr = N_("arg"); - - if (strchr ("<[", argstr[0])) - fprintf (stream, "%s %s;\n", kwp->ident, gettext (argstr)); - else if (strchr (argstr, ':')) - fprintf (stream, "%s <%s>;\n", kwp->ident, gettext (argstr)); - else - { - fprintf (stream, "%s <%s: ", kwp->ident, gettext (argstr)); - if (GRECS_IS_LIST (kwp->type)) - fprintf (stream, "list of %s", - gettext (grecs_data_type_string (GRECS_TYPE (kwp->type)))); - else - fprintf (stream, "%s", gettext (grecs_data_type_string (kwp->type))); - fprintf (stream, ">;\n"); - } + const char *argstr; + + if (kwp->docstring) + grecs_format_docstring(stream, kwp->docstring, level); + format_level(stream, level); + + if (kwp->argname) + argstr = kwp->argname; + else + argstr = N_("arg"); + + if (strchr("<[", argstr[0])) + fprintf(stream, "%s %s;\n", kwp->ident, gettext(argstr)); + else if (strchr (argstr, ':')) + fprintf (stream, "%s <%s>;\n", kwp->ident, gettext(argstr)); + else { + fprintf(stream, "%s <%s: ", kwp->ident, gettext(argstr)); + if (GRECS_IS_LIST(kwp->type)) + fprintf(stream, "list of %s", + gettext(grecs_data_type_string( + GRECS_TYPE (kwp->type)))); + else + fprintf(stream, "%s", + gettext(grecs_data_type_string(kwp->type))); + fprintf(stream, ">;\n"); + } } void -grecs_format_block_statement (FILE *stream, struct grecs_keyword *kwp, - unsigned level) +grecs_format_block_statement(FILE *stream, struct grecs_keyword *kwp, + unsigned level) { - if (kwp->docstring) - grecs_format_docstring (stream, kwp->docstring, level); - format_level (stream, level); - fprintf (stream, "%s", kwp->ident); - if (kwp->argname) - fprintf (stream, " <%s>", gettext (kwp->argname)); - fprintf (stream, " {\n"); - grecs_format_statement_array (stream, kwp->kwd, 0, level + 1); - format_level (stream, level); - fprintf (stream, "}\n"); + if (kwp->docstring) + grecs_format_docstring(stream, kwp->docstring, level); + format_level(stream, level); + fprintf(stream, "%s", kwp->ident); + if (kwp->argname) + fprintf(stream, " <%s>", gettext(kwp->argname)); + fprintf(stream, " {\n"); + grecs_format_statement_array(stream, kwp->kwd, 0, level + 1); + format_level(stream, level); + fprintf(stream, "}\n"); } void -grecs_format_statement_array (FILE *stream, struct grecs_keyword *kwp, - unsigned n, - unsigned level) +grecs_format_statement_array(FILE *stream, struct grecs_keyword *kwp, + unsigned n, + unsigned level) { - for (; kwp->ident; kwp++, n++) - { - if (n) - fputc ('\n', stream); - if (kwp->type == grecs_type_section) - grecs_format_block_statement (stream, kwp, level); - else - grecs_format_simple_statement (stream, kwp, level); - } + for (; kwp->ident; kwp++, n++) { + if (n) + fputc('\n', stream); + if (kwp->type == grecs_type_section) + grecs_format_block_statement(stream, kwp, level); + else + grecs_format_simple_statement(stream, kwp, level); + } } + + diff --git a/src/grecs-gram.y b/src/grecs-gram.y index 0edea13..f65e2dd 100644 --- a/src/grecs-gram.y +++ b/src/grecs-gram.y @@ -47,13 +47,13 @@ int grecs_error_count; int grecs_default_port = 0; -static void *target_ptr (struct grecs_keyword *kwp, char *base); -static void stmt_begin (struct grecs_keyword *kwp, grecs_value_t tag); -static void stmt_end (struct grecs_keyword *kwp); -static struct grecs_keyword *find_keyword (const char *ident); +static void *target_ptr(struct grecs_keyword *kwp, char *base); +static void stmt_begin(struct grecs_keyword *kwp, grecs_value_t tag); +static void stmt_end(struct grecs_keyword *kwp); +static struct grecs_keyword *find_keyword(const char *ident); -static void process_ident (struct grecs_keyword *kwp, grecs_value_t *value); -static struct grecs_list *simple_list_create (int dispose); +static void process_ident(struct grecs_keyword *kwp, grecs_value_t *value); +static struct grecs_list *simple_list_create(int dispose); %} %union { @@ -85,80 +85,79 @@ stmt : simple simple : ident vallist ';' { - process_ident($1, &$2); + process_ident($1, &$2); } ; block : ident tag { stmt_begin($1, $2); } '{' stmtlist '}' opt_sc { - stmt_end($1); + stmt_end($1); } ; ident : IDENT { - $$ = find_keyword($1); - if (!$$) - grecs_error(&grecs_current_locus, 0, _("Unknown keyword")); + $$ = find_keyword($1); + if (!$$) + grecs_error(&grecs_current_locus, 0, + _("Unknown keyword")); } ; tag : /* empty */ { - $$.type = GRECS_TYPE_STRING; - $$.v.string = NULL; + $$.type = GRECS_TYPE_STRING; + $$.v.string = NULL; } | value ; vallist : vlist { - size_t n; - - if ((n = grecs_list_size ($1)) == 1) - { - $$ = *(grecs_value_t *)grecs_list_index ($1, 0); - } - else - { - size_t i; - struct grecs_list_entry *ep; + size_t n; + + if ((n = grecs_list_size($1)) == 1) { + $$ = *(grecs_value_t *)grecs_list_index($1, 0); + } else { + size_t i; + struct grecs_list_entry *ep; - $$.type = GRECS_TYPE_ARRAY; - $$.v.arg.c = n; - $$.v.arg.v = grecs_malloc (n * sizeof ($$.v.arg.v[0])); - for (i = 0, ep = $1->head; ep; i++, ep = ep->next) - $$.v.arg.v[i] = *(grecs_value_t *)ep->data; - } - grecs_list_free ($1); + $$.type = GRECS_TYPE_ARRAY; + $$.v.arg.c = n; + $$.v.arg.v = grecs_malloc(n * + sizeof($$.v.arg.v[0])); + for (i = 0, ep = $1->head; ep; i++, ep = ep->next) + $$.v.arg.v[i] = *(grecs_value_t *)ep->data; + } + grecs_list_free($1); } ; vlist : value { - $$ = simple_list_create (0); - grecs_list_append ($$, grecs_value_dup (&$1)); + $$ = simple_list_create(0); + grecs_list_append($$, grecs_value_dup(&$1)); } | vlist value { - grecs_list_append ($1, grecs_value_dup (&$2)); + grecs_list_append($1, grecs_value_dup(&$2)); } ; value : string { - $$.type = GRECS_TYPE_STRING; - $$.v.string = $1; + $$.type = GRECS_TYPE_STRING; + $$.v.string = $1; } | list { - $$.type = GRECS_TYPE_LIST; - $$.v.list = $1; + $$.type = GRECS_TYPE_LIST; + $$.v.list = $1; } | MSTRING { - $$.type = GRECS_TYPE_STRING; - $$.v.string = $1; + $$.type = GRECS_TYPE_STRING; + $$.v.string = $1; } ; @@ -169,52 +168,52 @@ string : STRING slist : slist0 { - struct grecs_list_entry *ep; - const void *p; - - grecs_line_begin (); - for (ep = $1->head; ep; ep = ep->next) - grecs_line_add (ep->data, strlen (ep->data)); - $$ = grecs_line_finish (); - grecs_list_free ($1); + struct grecs_list_entry *ep; + const void *p; + + grecs_line_begin(); + for (ep = $1->head; ep; ep = ep->next) + grecs_line_add(ep->data, strlen(ep->data)); + $$ = grecs_line_finish(); + grecs_list_free($1); } ; slist0 : QSTRING { - $$ = simple_list_create (0); - grecs_list_append ($$, $1); + $$ = simple_list_create(0); + grecs_list_append($$, $1); } | slist0 QSTRING { - grecs_list_append ($1, $2); - $$ = $1; + grecs_list_append($1, $2); + $$ = $1; } ; list : '(' ')' { - $$ = NULL; + $$ = NULL; } | '(' values ')' { - $$ = $2; + $$ = $2; } | '(' values ',' ')' { - $$ = $2; + $$ = $2; } ; values : value { - $$ = simple_list_create (0); - grecs_list_append ($$, grecs_value_dup (&$1)); + $$ = simple_list_create(0); + grecs_list_append($$, grecs_value_dup(&$1)); } | values ',' value { - grecs_list_append ($1, grecs_value_dup (&$3)); - $$ = $1; + grecs_list_append($1, grecs_value_dup(&$3)); + $$ = $1; } ; @@ -227,380 +226,356 @@ opt_sc : /* empty */ int yyerror(char *s) { - grecs_error (&grecs_current_locus, 0, "%s", s); - return 0; + grecs_error(&grecs_current_locus, 0, "%s", s); + return 0; } static void -listel_dispose (void *el) +listel_dispose(void *el) { - free (el); + free(el); } static struct grecs_list * -simple_list_create (int dispose) +simple_list_create(int dispose) { - struct grecs_list *lp = grecs_list_create (); - if (dispose) - lp->free_entry = listel_dispose; - return lp; + struct grecs_list *lp = grecs_list_create(); + if (dispose) + lp->free_entry = listel_dispose; + return lp; } int -grecs_vasprintf (char **pbuf, size_t *psize, const char *fmt, va_list ap) +grecs_vasprintf(char **pbuf, size_t *psize, const char *fmt, va_list ap) { - char *buf = *pbuf; - size_t buflen = *psize; - int rc = 0; + char *buf = *pbuf; + size_t buflen = *psize; + int rc = 0; - if (!buf) - { - if (buflen == 0) - buflen = 512; /* Initial allocation */ + if (!buf) { + if (buflen == 0) + buflen = 512; /* Initial allocation */ - buf = calloc (1, buflen); - if (buf == NULL) - return ENOMEM; - } - - for (;;) - { - ssize_t n = vsnprintf (buf, buflen, fmt, ap); - if (n < 0 || n >= buflen || !memchr (buf, '\0', n + 1)) - { - char *newbuf; - size_t newlen = buflen * 2; - if (newlen < buflen) - { - rc = ENOMEM; - break; - } - newbuf = realloc (buf, newlen); - if (newbuf == NULL) - { - rc = ENOMEM; - break; - } - buflen = newlen; - buf = newbuf; + buf = calloc(1, buflen); + if (buf == NULL) + return ENOMEM; } - else - break; - } - - if (rc) - { - if (!*pbuf) - { - /* We made first allocation, now free it */ - free (buf); - buf = NULL; - buflen = 0; + + for (;;) { + ssize_t n = vsnprintf(buf, buflen, fmt, ap); + if (n < 0 || n >= buflen || !memchr(buf, '\0', n + 1)) { + char *newbuf; + size_t newlen = buflen * 2; + if (newlen < buflen) { + rc = ENOMEM; + break; + } + newbuf = realloc(buf, newlen); + if (newbuf == NULL) { + rc = ENOMEM; + break; + } + buflen = newlen; + buf = newbuf; + } else + break; } - } - *pbuf = buf; - *psize = buflen; - return rc; + if (rc) { + if (!*pbuf) { + /* We made first allocation, now free it */ + free(buf); + buf = NULL; + buflen = 0; + } + } + + *pbuf = buf; + *psize = buflen; + return rc; } int -grecs_asprintf (char **pbuf, size_t *psize, const char *fmt, ...) +grecs_asprintf(char **pbuf, size_t *psize, const char *fmt, ...) { - int rc; - va_list ap; - - va_start (ap, fmt); - rc = grecs_vasprintf (pbuf, psize, fmt, ap); - va_end (ap); - return rc; + int rc; + va_list ap; + + va_start(ap, fmt); + rc = grecs_vasprintf(pbuf, psize, fmt, ap); + va_end(ap); + return rc; } void -grecs_warning (grecs_locus_t *locus, int errcode, const char *fmt, ...) +grecs_warning(grecs_locus_t *locus, int errcode, const char *fmt, ...) { - va_list ap; - char *buf = NULL; - size_t size = 0; - - va_start (ap, fmt); - if (grecs_vasprintf (&buf, &size, fmt, ap)) - grecs_alloc_die (); - va_end (ap); - grecs_print_diag (locus, 0, errcode, buf); - free(buf); + va_list ap; + char *buf = NULL; + size_t size = 0; + + va_start(ap, fmt); + if (grecs_vasprintf(&buf, &size, fmt, ap)) + grecs_alloc_die(); + va_end(ap); + grecs_print_diag(locus, 0, errcode, buf); + free(buf); } void -grecs_error (grecs_locus_t *locus, int errcode, const char *fmt, ...) +grecs_error(grecs_locus_t *locus, int errcode, const char *fmt, ...) { - va_list ap; - char *buf = NULL; - size_t size = 0; - - va_start (ap, fmt); - if (grecs_vasprintf (&buf, &size, fmt, ap)) - grecs_alloc_die (); - va_end (ap); - grecs_print_diag (locus, 1, errcode, buf); - free (buf); - grecs_error_count++; + va_list ap; + char *buf = NULL; + size_t size = 0; + + va_start(ap, fmt); + if (grecs_vasprintf(&buf, &size, fmt, ap)) + grecs_alloc_die(); + va_end(ap); + grecs_print_diag(locus, 1, errcode, buf); + free(buf); + grecs_error_count++; } void -grecs_set_keywords (struct grecs_keyword *kwd) +grecs_set_keywords(struct grecs_keyword *kwd) { - config_keywords.kwd = kwd; + config_keywords.kwd = kwd; } int -grecs_parse (const char *name) +grecs_parse(const char *name) { - int rc; - if (grecs_lex_begin (name)) - return 1; - cursect = &config_keywords; - if (sections) - { - grecs_list_free (sections); - sections = NULL; - } - rc = yyparse (); - grecs_lex_end (); - if (grecs_error_count) - rc = 1; - return rc; + int rc; + if (grecs_lex_begin(name)) + return 1; + cursect = &config_keywords; + if (sections) { + grecs_list_free(sections); + sections = NULL; + } + rc = yyparse(); + grecs_lex_end(); + if (grecs_error_count) + rc = 1; + return rc; } void -grecs_gram_trace (int n) +grecs_gram_trace(int n) { - yydebug = n; + yydebug = n; } static void * -target_ptr (struct grecs_keyword *kwp, char *base) +target_ptr(struct grecs_keyword *kwp, char *base) { - if (kwp->varptr) - base = (char*) kwp->varptr + kwp->offset; - else if (base) - base += kwp->offset; - - return base; + if (kwp->varptr) + base = (char*) kwp->varptr + kwp->offset; + else if (base) + base += kwp->offset; + + return base; } static int -fake_callback (enum grecs_callback_command cmd, +fake_callback(enum grecs_callback_command cmd, grecs_locus_t *locus, void *varptr, grecs_value_t *value, void *cb_data) { - return 0; + return 0; } static struct grecs_keyword fake = { - "*", - NULL, - NULL, - grecs_type_void, - NULL, - 0, - fake_callback, - NULL, - &fake + "*", + NULL, + NULL, + grecs_type_void, + NULL, + 0, + fake_callback, + NULL, + &fake }; static void -stmt_begin (struct grecs_keyword *kwp, grecs_value_t tag) +stmt_begin(struct grecs_keyword *kwp, grecs_value_t tag) { - void *target; - - if (!sections) - sections = grecs_list_create (); - grecs_list_push (sections, cursect); - if (kwp) - { - target = target_ptr (kwp, CURRENT_BASE); - cursect = kwp; - if (kwp->callback && kwp->callback (grecs_callback_section_begin, - &grecs_current_locus, /* FIXME */ - target, - &tag, - &kwp->callback_data)) - cursect = &fake; - } - else - /* install "ignore-all" section */ - cursect = kwp; + void *target; + + if (!sections) + sections = grecs_list_create(); + grecs_list_push(sections, cursect); + if (kwp) { + target = target_ptr(kwp, CURRENT_BASE); + cursect = kwp; + if (kwp->callback && + kwp->callback(grecs_callback_section_begin, + &grecs_current_locus, /* FIXME */ + target, + &tag, + &kwp->callback_data)) + cursect = &fake; + } else + /* install "ignore-all" section */ + cursect = kwp; } static void -stmt_end (struct grecs_keyword *kwp) +stmt_end(struct grecs_keyword *kwp) { - grecs_callback_fn callback = NULL; - void *dataptr = NULL; - - if (cursect && cursect->callback) - { - callback = cursect->callback; - dataptr = &cursect->callback_data; - } - - cursect = (struct grecs_keyword *) grecs_list_pop (sections); - if (!cursect) - abort (); - if (callback) - callback (grecs_callback_section_end, - &grecs_current_locus, /* FIXME */ - kwp ? target_ptr (kwp, CURRENT_BASE) : NULL, - NULL, - dataptr); + grecs_callback_fn callback = NULL; + void *dataptr = NULL; + + if (cursect && cursect->callback) { + callback = cursect->callback; + dataptr = &cursect->callback_data; + } + cursect = (struct grecs_keyword *) grecs_list_pop(sections); + if (!cursect) + abort(); + if (callback) + callback(grecs_callback_section_end, + &grecs_current_locus, /* FIXME */ + kwp ? target_ptr(kwp, CURRENT_BASE) : NULL, + NULL, + dataptr); } static struct grecs_keyword * -find_keyword (const char *ident) +find_keyword(const char *ident) { - struct grecs_keyword *kwp; + struct grecs_keyword *kwp; - if (cursect && cursect != &fake) - { - for (kwp = cursect->kwd; kwp->ident; kwp++) - if (strcmp (kwp->ident, ident) == 0) - return kwp; - } - else - { - return &fake; - } - return NULL; + if (cursect && cursect != &fake) { + for (kwp = cursect->kwd; kwp->ident; kwp++) + if (strcmp(kwp->ident, ident) == 0) + return kwp; + } else { + return &fake; + } + return NULL; } static int -string_to_bool (const char *string, int *pval, grecs_locus_t *locus) +string_to_bool(const char *string, int *pval, grecs_locus_t *locus) { - if (strcmp (string, "yes") == 0 - || strcmp (string, "true") == 0 - || strcmp (string, "t") == 0 - || strcmp (string, "1") == 0) - *pval = 1; - else if (strcmp (string, "no") == 0 - || strcmp (string, "false") == 0 - || strcmp (string, "nil") == 0 - || strcmp (string, "0") == 0) - *pval = 0; - else - { - grecs_error (locus, 0, _("%s: not a valid boolean value"), string); - return 1; - } - return 0; + if (strcmp(string, "yes") == 0 + || strcmp(string, "true") == 0 + || strcmp(string, "t") == 0 + || strcmp(string, "1") == 0) + *pval = 1; + else if (strcmp(string, "no") == 0 + || strcmp(string, "false") == 0 + || strcmp(string, "nil") == 0 + || strcmp(string, "0") == 0) + *pval = 0; + else { + grecs_error(locus, 0, + _("%s: not a valid boolean value"), + string); + return 1; + } + return 0; } static int -string_to_host (struct in_addr *in, const char *string, grecs_locus_t *locus) +string_to_host(struct in_addr *in, const char *string, grecs_locus_t *locus) { - if (inet_aton (string, in) == 0) - { - struct hostent *hp; - - hp = gethostbyname (string); - if (hp == NULL) - return 1; - memcpy (in, hp->h_addr, sizeof (struct in_addr)); - } - return 0; + if (inet_aton(string, in) == 0) { + struct hostent *hp; + + hp = gethostbyname(string); + if (hp == NULL) + return 1; + memcpy(in, hp->h_addr, sizeof(struct in_addr)); + } + return 0; } static int -string_to_sockaddr (struct grecs_sockaddr *sp, const char *string, +string_to_sockaddr(struct grecs_sockaddr *sp, const char *string, grecs_locus_t *locus) { - if (string[0] == '/') - { - struct sockaddr_un s_un; - if (strlen (string) >= sizeof (s_un.sun_path)) - { - grecs_error (locus, 0, _("%s: UNIX socket name too long"), string); - return 1; - } - s_un.sun_family = AF_UNIX; - strcpy (s_un.sun_path, string); - sp->len = sizeof (s_un); - sp->sa = grecs_malloc (sp->len); - memcpy (sp->sa, &s_un, sp->len); - } - else - { - char *p = strchr (string, ':'); - size_t len; - struct sockaddr_in sa; - - sa.sin_family = AF_INET; - if (p) - len = p - string; - else - len = strlen (string); - - if (len == 0) - sa.sin_addr.s_addr = INADDR_ANY; - else - { - char *host = grecs_malloc (len + 1); - memcpy (host, string, len); - host[len] = 0; - - if (string_to_host (&sa.sin_addr, host, locus)) - { - grecs_error (locus, 0, - _("%s: not a valid IP address or hostname"), - host); - free (host); - return 1; - } - free (host); - } - - if (p) - { - struct servent *serv; - - p++; - serv = getservbyname (p, "tcp"); - if (serv != NULL) - sa.sin_port = serv->s_port; - else - { - unsigned long l; - char *q; - - /* Not in services, maybe a number? */ - l = strtoul (p, &q, 0); - - if (*q || l > USHRT_MAX) - { - grecs_error (locus, 0, - _("%s: not a valid port number"), p); - return 1; + if (string[0] == '/') { + struct sockaddr_un s_un; + if (strlen(string) >= sizeof(s_un.sun_path)) { + grecs_error(locus, 0, + _("%s: UNIX socket name too long"), + string); + return 1; } - sa.sin_port = htons (l); - } - } - else if (grecs_default_port) - sa.sin_port = grecs_default_port; - else - { - grecs_error (locus, 0, _("missing port number")); - return 1; + s_un.sun_family = AF_UNIX; + strcpy(s_un.sun_path, string); + sp->len = sizeof(s_un); + sp->sa = grecs_malloc(sp->len); + memcpy(sp->sa, &s_un, sp->len); + } else { + char *p = strchr(string, ':'); + size_t len; + struct sockaddr_in sa; + + sa.sin_family = AF_INET; + if (p) + len = p - string; + else + len = strlen(string); + + if (len == 0) + sa.sin_addr.s_addr = INADDR_ANY; + else { + char *host = grecs_malloc(len + 1); + memcpy(host, string, len); + host[len] = 0; + + if (string_to_host(&sa.sin_addr, host, locus)) { + grecs_error(locus, 0, + _("%s: not a valid IP address or hostname"), + host); + free(host); + return 1; + } + free(host); + } + + if (p) { + struct servent *serv; + + p++; + serv = getservbyname(p, "tcp"); + if (serv != NULL) + sa.sin_port = serv->s_port; + else { + unsigned long l; + char *q; + + /* Not in services, maybe a number? */ + l = strtoul(p, &q, 0); + + if (*q || l > USHRT_MAX) { + grecs_error(locus, 0, + _("%s: not a valid port number"), p); + return 1; + } + sa.sin_port = htons(l); + } + } else if (grecs_default_port) + sa.sin_port = grecs_default_port; + else { + grecs_error(locus, 0, _("missing port number")); + return 1; + } + sp->len = sizeof(sa); + sp->sa = grecs_malloc(sp->len); + memcpy(sp->sa, &sa, sp->len); } - sp->len = sizeof (sa); - sp->sa = grecs_malloc (sp->len); - memcpy (sp->sa, &sa, sp->len); - } - return 0; + return 0; } @@ -612,15 +587,15 @@ string_to_sockaddr (struct grecs_sockaddr *sp, const char *string, /* The maximum and minimum values for the integer type T. These macros have undefined behavior if T is signed and has padding bits. */ # define TYPE_MINIMUM(t) \ - ((t) (! TYPE_SIGNED (t) \ + ((t) (! TYPE_SIGNED(t) \ ? (t) 0 \ - : TYPE_SIGNED_MAGNITUDE (t) \ + : TYPE_SIGNED_MAGNITUDE(t) \ ? ~ (t) 0 \ - : ~ TYPE_MAXIMUM (t))) + : ~ TYPE_MAXIMUM(t))) # define TYPE_MAXIMUM(t) \ - ((t) (! TYPE_SIGNED (t) \ + ((t) (! TYPE_SIGNED(t) \ ? (t) -1 \ - : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1))) + : ((((t) 1 << (sizeof(t) * CHAR_BIT - 2)) - 1) * 2 + 1))) # define TYPE_SIGNED_MAGNITUDE(t) ((t) ~ (t) 0 < (t) -1) @@ -642,12 +617,12 @@ string_to_sockaddr (struct grecs_sockaddr *sp, const char *string, break; \ if (x <= sum) \ { \ - grecs_error (loc, 0, _("numeric overflow")); \ + grecs_error(loc, 0, _("numeric overflow")); \ return 1; \ } \ else if (limit && x > limit) \ { \ - grecs_error (loc, 0, _("value out of allowed range")); \ + grecs_error(loc, 0, _("value out of allowed range")); \ return 1; \ } \ sum = x; \ @@ -672,17 +647,17 @@ string_to_sockaddr (struct grecs_sockaddr *sp, const char *string, base = 8; \ } else \ base = 10; \ - STRTONUM (s, type, base, res, limit, loc); \ + STRTONUM(s, type, base, res, limit, loc); \ } #define GETUNUM(str, type, res, loc) \ { \ type tmpres; \ const char *s = str; \ - STRxTONUM (s, type, tmpres, 0, loc); \ + STRxTONUM(s, type, tmpres, 0, loc); \ if (*s) \ { \ - grecs_error (loc, 0, _("not a number (stopped near `%s')"), \ + grecs_error(loc, 0, _("not a number (stopped near `%s')"), \ s); \ return 1; \ } \ @@ -700,16 +675,16 @@ string_to_sockaddr (struct grecs_sockaddr *sp, const char *string, { \ sign = 1; \ s++; \ - limit = TYPE_MINIMUM (type); \ + limit = TYPE_MINIMUM(type); \ limit = - limit; \ } \ else \ { \ sign = 0; \ - limit = TYPE_MAXIMUM (type); \ + limit = TYPE_MAXIMUM(type); \ } \ \ - STRxTONUM (s, unsigned type, tmpres, limit, loc); \ + STRxTONUM(s, unsigned type, tmpres, limit, loc); \ if (*s) \ { \ grecs_error(loc, 0, _("not a number (stopped near `%s')"), s); \ @@ -720,111 +695,111 @@ string_to_sockaddr (struct grecs_sockaddr *sp, const char *string, int -grecs_string_convert (void *target, enum grecs_data_type type, +grecs_string_convert(void *target, enum grecs_data_type type, const char *string, grecs_locus_t *locus) { - switch (type) - { - case grecs_type_void: - abort (); - - case grecs_type_string: - *(const char**)target = string; - break; + switch (type) { + case grecs_type_void: + abort(); - case grecs_type_short: - GETUNUM (string, short, *(short*)target, locus); - break; + case grecs_type_string: + *(const char**)target = string; + break; - case grecs_type_ushort: - GETUNUM (string, unsigned short, *(unsigned short*)target, locus); - break; + case grecs_type_short: + GETUNUM(string, short, *(short*)target, locus); + break; - case grecs_type_bool: - return string_to_bool (string, (int*)target, locus); + case grecs_type_ushort: + GETUNUM(string, unsigned short, *(unsigned short*)target, locus); + break; - case grecs_type_int: - GETSNUM (string, int, *(int*)target, locus); - break; + case grecs_type_bool: + return string_to_bool(string, (int*)target, locus); - case grecs_type_uint: - GETUNUM (string, unsigned int, *(unsigned int*)target, locus); - break; + case grecs_type_int: + GETSNUM(string, int, *(int*)target, locus); + break; - case grecs_type_long: - GETSNUM (string, long, *(long*)target, locus); - break; + case grecs_type_uint: + GETUNUM(string, unsigned int, *(unsigned int*)target, locus); + break; + + case grecs_type_long: + GETSNUM(string, long, *(long*)target, locus); + break; - case grecs_type_ulong: - GETUNUM (string, unsigned long, *(unsigned long*)target, locus); - break; + case grecs_type_ulong: + GETUNUM(string, unsigned long, *(unsigned long*)target, locus); + break; - case grecs_type_size: - GETUNUM (string, size_t, *(size_t*)target, locus); - break; - /*FIXME - case grecs_type_off: - GETSNUM (string, off_t, *(off_t*)target, locus); - break; - */ - case grecs_type_time: - /*FIXME: Use getdate */ - GETUNUM (string, time_t, *(time_t*)target, locus); - break; - - case grecs_type_ipv4: - if (inet_aton (string, (struct in_addr *)target)) - { - grecs_error (locus, 0, _("%s: not a valid IP address"), string); - return 1; - } - break; - - case grecs_type_host: - if (string_to_host ((struct in_addr *)target, string, locus)) - { - grecs_error (locus, 0, - _("%s: not a valid IP address or hostname"), string); - return 1; + case grecs_type_size: + GETUNUM(string, size_t, *(size_t*)target, locus); + break; + /*FIXME + case grecs_type_off: + GETSNUM(string, off_t, *(off_t*)target, locus); + break; + */ + case grecs_type_time: + /*FIXME: Use getdate */ + GETUNUM(string, time_t, *(time_t*)target, locus); + break; + + case grecs_type_ipv4: + if (inet_aton(string, (struct in_addr *)target)) { + grecs_error(locus, 0, _("%s: not a valid IP address"), + string); + return 1; } - break; + break; + + case grecs_type_host: + if (string_to_host((struct in_addr *)target, string, locus)) { + grecs_error(locus, 0, + _("%s: not a valid IP address or hostname"), + string); + return 1; + } + break; - case grecs_type_sockaddr: - return string_to_sockaddr ((struct grecs_sockaddr *)target, string, - locus); + case grecs_type_sockaddr: + return string_to_sockaddr((struct grecs_sockaddr *)target, string, + locus); /* FIXME: */ - case grecs_type_cidr: - grecs_error (locus, 0, _("INTERNAL ERROR at %s:%d"), __FILE__, __LINE__); - abort(); + case grecs_type_cidr: + grecs_error(locus, 0, + _("INTERNAL ERROR at %s:%d"), __FILE__, __LINE__); + abort(); - case grecs_type_section: - grecs_error (locus, 0, - _("invalid use of block statement")); - return 1; - } + case grecs_type_section: + grecs_error(locus, 0, + _("invalid use of block statement")); + return 1; + } return 0; } struct grecs_prop { - size_t size; - int (*cmp) (const void *, const void *); + size_t size; + int (*cmp)(const void *, const void *); }; static int -string_cmp (const void *elt1, const void *elt2) +string_cmp(const void *elt1, const void *elt2) { - return strcmp ((const char *)elt1, (const char *)elt2); + return strcmp((const char *)elt1,(const char *)elt2); } #define __grecs_name_cat__(a,b) a ## b #define NUMCMP(type) __grecs_name_cat__(type,_cmp) #define __DECL_NUMCMP(type,ctype) \ static int \ - NUMCMP(type) (const void *elt1, const void *elt2) \ + NUMCMP(type)(const void *elt1, const void *elt2) \ { \ - return memcmp (elt1, elt2, sizeof (ctype)); \ + return memcmp(elt1, elt2, sizeof(ctype)); \ } #define DECL_NUMCMP(type) __DECL_NUMCMP(type,type) @@ -838,151 +813,140 @@ __DECL_NUMCMP(in_addr, struct in_addr) __DECL_NUMCMP(grecs_sockaddr, struct grecs_sockaddr) struct grecs_prop grecs_prop_tab[] = { - { 0, NULL }, /* grecs_type_void */ - { sizeof (char*), string_cmp }, /* grecs_type_string */ - { sizeof (short), NUMCMP (short) }, /* grecs_type_short */ - { sizeof (unsigned short), NUMCMP (short) }, /* grecs_type_ushort */ - { sizeof (int), NUMCMP (int) }, /* grecs_type_int */ - { sizeof (unsigned int), NUMCMP (int) }, /* grecs_type_uint */ - { sizeof (long), NUMCMP (long) }, /* grecs_type_long */ - { sizeof (unsigned long), NUMCMP (long) }, /* grecs_type_ulong */ - { sizeof (size_t), NUMCMP (size_t) }, /* grecs_type_size */ + { 0, NULL }, /* grecs_type_void */ + { sizeof(char*), string_cmp }, /* grecs_type_string */ + { sizeof(short), NUMCMP(short) }, /* grecs_type_short */ + { sizeof(unsigned short), NUMCMP(short) }, /* grecs_type_ushort */ + { sizeof(int), NUMCMP(int) }, /* grecs_type_int */ + { sizeof(unsigned int), NUMCMP(int) }, /* grecs_type_uint */ + { sizeof(long), NUMCMP(long) }, /* grecs_type_long */ + { sizeof(unsigned long), NUMCMP(long) }, /* grecs_type_ulong */ + { sizeof(size_t), NUMCMP(size_t) }, /* grecs_type_size */ #if 0 - FIXME - { sizeof (off_t), NUMCMP (off_t) }, /* grecs_type_off */ + FIXME + { sizeof(off_t), NUMCMP(off_t) }, /* grecs_type_off */ #endif - { sizeof (time_t), NUMCMP (time_t) }, /* grecs_type_time */ - { sizeof (int), NUMCMP (int) }, /* grecs_type_bool */ - { sizeof (struct in_addr), NUMCMP (in_addr) }, /* grecs_type_ipv4 */ - { 0, NULL }, /* FIXME: grecs_type_cidr */ - { sizeof (struct in_addr), NUMCMP (in_addr) }, /* grecs_type_host */ - { sizeof (struct grecs_sockaddr), NUMCMP (grecs_sockaddr) }, - /* grecs_type_sockaddr */ - { 0, NULL } /* grecs_type_section */ + { sizeof(time_t), NUMCMP(time_t) }, /* grecs_type_time */ + { sizeof(int), NUMCMP(int) }, /* grecs_type_bool */ + { sizeof(struct in_addr), NUMCMP(in_addr) }, /* grecs_type_ipv4 */ + { 0, NULL }, /* FIXME: grecs_type_cidr */ + { sizeof(struct in_addr), NUMCMP(in_addr) }, /* grecs_type_host */ + { sizeof(struct grecs_sockaddr), NUMCMP(grecs_sockaddr) }, + /* grecs_type_sockaddr */ + { 0, NULL } /* grecs_type_section */ }; #define grecs_prop_count \ - (sizeof (grecs_prop_tab) / sizeof (grecs_prop_tab[0])) + (sizeof(grecs_prop_tab) / sizeof(grecs_prop_tab[0])) void -grecs_process_ident (struct grecs_keyword *kwp, grecs_value_t *value, +grecs_process_ident(struct grecs_keyword *kwp, grecs_value_t *value, void *base, grecs_locus_t *locus) { - void *target; + void *target; - if (!kwp) - return; - - target = target_ptr (kwp, (char *) base); - - if (kwp->callback) - kwp->callback (grecs_callback_set_value, - locus, - target, - value, - &kwp->callback_data); - else if (value->type == GRECS_TYPE_ARRAY) - { - grecs_error (locus, 0, - _("too many arguments to `%s'; missing semicolon?"), - kwp->ident); - return; - } - else if (value->type == GRECS_TYPE_LIST) - { - if (GRECS_IS_LIST (kwp->type)) - { - struct grecs_list_entry *ep; - enum grecs_data_type type = GRECS_TYPE (kwp->type); - int num = 1; - struct grecs_list *list; - size_t size; - - if (type >= grecs_prop_count - || (size = grecs_prop_tab[type].size) == 0) - { - grecs_error (locus, 0, - _("INTERNAL ERROR at %s:%d: " - "unhandled data type %d"), - __FILE__, __LINE__, type); - abort (); - } - - list = grecs_list_create (); - list->cmp = grecs_prop_tab[type].cmp; - - for (ep = value->v.list->head; ep; ep = ep->next) - { - const grecs_value_t *vp = ep->data; - - if (vp->type != GRECS_TYPE_STRING) - grecs_error (locus, 0, + if (!kwp) + return; + + target = target_ptr(kwp, (char *) base); + + if (kwp->callback) + kwp->callback(grecs_callback_set_value, + locus, + target, + value, + &kwp->callback_data); + else if (value->type == GRECS_TYPE_ARRAY) { + grecs_error(locus, 0, + _("too many arguments to `%s'; missing semicolon?"), + kwp->ident); + return; + } else if (value->type == GRECS_TYPE_LIST) { + if (GRECS_IS_LIST(kwp->type)) { + struct grecs_list_entry *ep; + enum grecs_data_type type = GRECS_TYPE(kwp->type); + int num = 1; + struct grecs_list *list; + size_t size; + + if (type >= grecs_prop_count + || (size = grecs_prop_tab[type].size) == 0) { + grecs_error(locus, 0, + _("INTERNAL ERROR at %s:%d: " + "unhandled data type %d"), + __FILE__, __LINE__, type); + abort(); + } + + list = grecs_list_create(); + list->cmp = grecs_prop_tab[type].cmp; + + for (ep = value->v.list->head; ep; ep = ep->next) { + const grecs_value_t *vp = ep->data; + + if (vp->type != GRECS_TYPE_STRING) + grecs_error(locus, 0, _("%s: incompatible data type in list item #%d"), - kwp->ident, num); - else if (type == grecs_type_string) - grecs_list_append (list, (void*) vp->v.string); - else - { - void *ptr = grecs_malloc (size); - if (grecs_string_convert (ptr, type, vp->v.string, - locus) == 0) - grecs_list_append (list, ptr); - else - free (ptr); + kwp->ident, num); + else if (type == grecs_type_string) + grecs_list_append(list, + (void*) vp->v.string); + else { + void *ptr = grecs_malloc(size); + if (grecs_string_convert(ptr, + type, + vp->v.string, + locus) == 0) + grecs_list_append(list, ptr); + else + free(ptr); + } + } + *(struct grecs_list**)target = list; + } else { + grecs_error(locus, 0, + _("incompatible data type for `%s'"), + kwp->ident); + return; } - } - *(struct grecs_list**)target = list; - } - else - { - grecs_error (locus, 0, - _("incompatible data type for `%s'"), - kwp->ident); - return; - } - } - else if (GRECS_IS_LIST (kwp->type)) - { - struct grecs_list *list; - enum grecs_data_type type = GRECS_TYPE (kwp->type); - size_t size; - void *ptr; + } else if (GRECS_IS_LIST(kwp->type)) { + struct grecs_list *list; + enum grecs_data_type type = GRECS_TYPE(kwp->type); + size_t size; + void *ptr; - if (type >= grecs_prop_count - || (size = grecs_prop_tab[type].size) == 0) - { - grecs_error (locus, 0, - _("INTERNAL ERROR at %s:%d: unhandled data type %d"), - __FILE__, __LINE__, type); - abort(); - } - - list = grecs_list_create (); - list->cmp = grecs_prop_tab[type].cmp; - list->free_entry = listel_dispose; - if (type == grecs_type_string) - grecs_list_append (list, value->v.string); - else - { - ptr = grecs_malloc (size); - if (grecs_string_convert (ptr, type, value->v.string, locus)) - { - free (ptr); - grecs_list_free (list); - return; - } - grecs_list_append (list, ptr); - } - *(struct grecs_list**)target = list; - } - else - grecs_string_convert (target, GRECS_TYPE (kwp->type), value->v.string, - locus); + if (type >= grecs_prop_count + || (size = grecs_prop_tab[type].size) == 0) { + grecs_error(locus, 0, + _("INTERNAL ERROR at %s:%d: unhandled data type %d"), + __FILE__, __LINE__, type); + abort(); + } + + list = grecs_list_create(); + list->cmp = grecs_prop_tab[type].cmp; + list->free_entry = listel_dispose; + if (type == grecs_type_string) + grecs_list_append(list, value->v.string); + else { + ptr = grecs_malloc(size); + if (grecs_string_convert(ptr, type, value->v.string, locus)) { + free(ptr); + grecs_list_free(list); + return; + } + grecs_list_append(list, ptr); + } + *(struct grecs_list**)target = list; + } else + grecs_string_convert(target, GRECS_TYPE(kwp->type), + value->v.string, + locus); } static void -process_ident (struct grecs_keyword *kwp, grecs_value_t *value) +process_ident(struct grecs_keyword *kwp, grecs_value_t *value) { - grecs_process_ident (kwp, value, CURRENT_BASE, &grecs_current_locus); + grecs_process_ident(kwp, value, CURRENT_BASE, &grecs_current_locus); } diff --git a/src/grecs-lex.l b/src/grecs-lex.l index baf85de..9c01e0d 100644 --- a/src/grecs-lex.l +++ b/src/grecs-lex.l @@ -34,7 +34,7 @@ static char *multiline_delimiter; static size_t multiline_delimiter_len; static int multiline_unescape; /* Unescape here-document contents */ -static int (*char_to_strip) (char); /* Strip matching characters of each +static int (*char_to_strip)(char); /* Strip matching characters of each here-document line */ grecs_locus_t grecs_current_locus; /* Input file location */ @@ -50,26 +50,26 @@ static size_t xlines; static struct grecs_list *line_acc; static struct grecs_list *string_list; -static void multiline_begin (char *); -static void multiline_add (char *); -static char *multiline_strip_tabs (char *text); -static void line_add_unescape_last (char *text, size_t len); -static int ident (void); -static int isemptystr (int off); +static void multiline_begin(char *); +static void multiline_add(char *); +static char *multiline_strip_tabs(char *text); +static void line_add_unescape_last(char *text, size_t len); +static int ident(void); +static int isemptystr(int off); -static void parse_line (char *text, grecs_locus_t *ploc, size_t *pxlines); -static void parse_line_cpp (char *text, grecs_locus_t *ploc, size_t *pxlines); +static void parse_line(char *text, grecs_locus_t *ploc, size_t *pxlines); +static void parse_line_cpp(char *text, grecs_locus_t *ploc, size_t *pxlines); #undef YY_INPUT #define YY_INPUT(buf,result,max_size) \ do \ { \ if (grecs_preprocessor) \ - result = fread (buf, 1, max_size, yyin); \ + result = fread(buf, 1, max_size, yyin); \ else \ result = grecs_preproc_fill_buffer(buf, max_size); \ } \ - while (0) + while(0) %} @@ -82,16 +82,16 @@ P [1-9][0-9]* %% /* C-style comments */ -"/*" BEGIN (COMMENT); +"/*" BEGIN(COMMENT); [^*\n]* /* eat anything that's not a '*' */ "*"+[^*/\n]* /* eat up '*'s not followed by '/'s */ \n ++grecs_current_locus.line; -"*"+"/" BEGIN (INITIAL); +"*"+"/" BEGIN(INITIAL); /* Line directive */ -^[ \t]*#[ \t]*{P}[ \t]+\".*\".*\n { parse_line_cpp (yytext, +^[ \t]*#[ \t]*{P}[ \t]+\".*\".*\n { parse_line_cpp(yytext, &grecs_current_locus, &xlines); } -^[ \t]*#[ \t]*line[ \t].*\n { parse_line (yytext, &grecs_current_locus, +^[ \t]*#[ \t]*line[ \t].*\n { parse_line(yytext, &grecs_current_locus, &xlines); } /* End-of-line comments */ #.*\n { grecs_current_locus.line++; } @@ -99,27 +99,27 @@ P [1-9][0-9]* "//".*\n { grecs_current_locus.line++; } "//".* /* end-of-file comment */; /* Identifiers */ -{ID} return ident (); +{ID} return ident(); /* Strings */ -[a-zA-Z0-9_\.\*/:@-]+ { grecs_line_begin (); - grecs_line_add (yytext, yyleng); - yylval.string = grecs_line_finish (); - return STRING; } +[a-zA-Z0-9_\.\*/:@-]+ { grecs_line_begin(); + grecs_line_add(yytext, yyleng); + yylval.string = grecs_line_finish(); + return STRING; } /* Quoted strings */ -\"[^\\"\n]*\" { grecs_line_begin (); - grecs_line_add (yytext + 1, yyleng - 2); - yylval.string = grecs_line_finish (); +\"[^\\"\n]*\" { grecs_line_begin(); + grecs_line_add(yytext + 1, yyleng - 2); + yylval.string = grecs_line_finish(); return QSTRING; } \"[^\\"\n]*\\. | -\"[^\\"\n]*\\\n { BEGIN (STR); - grecs_line_begin (); - line_add_unescape_last (yytext + 1, yyleng - 1); } +\"[^\\"\n]*\\\n { BEGIN(STR); + grecs_line_begin(); + line_add_unescape_last(yytext + 1, yyleng - 1); } [^\\"\n]*\\. | -\"[^\\"\n]*\\\n { line_add_unescape_last (yytext, yyleng); } +\"[^\\"\n]*\\\n { line_add_unescape_last(yytext, yyleng); } [^\\"\n]*\" { BEGIN(INITIAL); if (yyleng > 1) - grecs_line_add (yytext, yyleng - 1); - yylval.string = grecs_line_finish (); + grecs_line_add(yytext, yyleng - 1); + yylval.string = grecs_line_finish(); return QSTRING; } /* Multiline strings */ "<<"(-" "?)?\\?{ID}[ \t]*#.*\n | @@ -128,402 +128,375 @@ P [1-9][0-9]* "<<"(-" "?)?\"{ID}\"[ \t]*#.*\n | "<<"(-" "?)?\"{ID}\"[ \t]*"//".*\n | "<<"(-" "?)?\"{ID}\"[ \t]*\n { - BEGIN (ML); - multiline_begin (yytext+2); + BEGIN(ML); + multiline_begin(yytext+2); grecs_current_locus.line++; } /* Ignore m4 line statements */ ^"#line ".*\n { grecs_current_locus.line++; } -.*\n { char *p = multiline_strip_tabs (yytext); +.*\n { char *p = multiline_strip_tabs(yytext); - if (!strncmp (p, multiline_delimiter, multiline_delimiter_len) - && isemptystr (p + multiline_delimiter_len - yytext)) - { - free (multiline_delimiter); - multiline_delimiter = NULL; - BEGIN (INITIAL); - yylval.string = grecs_line_finish (); - return MSTRING; - } + if (!strncmp(p, multiline_delimiter, multiline_delimiter_len) + && isemptystr(p + multiline_delimiter_len - yytext)) { + free(multiline_delimiter); + multiline_delimiter = NULL; + BEGIN(INITIAL); + yylval.string = grecs_line_finish(); + return MSTRING; + } grecs_current_locus.line++; - multiline_add (p); } + multiline_add(p); } {WS} ; /* Other tokens */ \n { grecs_current_locus.line++; } [,;{}()] return yytext[0]; -. { if (isascii (yytext[0]) && isprint (yytext[0])) - grecs_error (&grecs_current_locus, 0, _("stray character %c"), yytext[0]); +. { if (isascii(yytext[0]) && isprint(yytext[0])) + grecs_error(&grecs_current_locus, 0, + _("stray character %c"), yytext[0]); else - grecs_error (&grecs_current_locus, 0, _("stray character \\%03o"), + grecs_error(&grecs_current_locus, 0, _("stray character \\%03o"), (unsigned char) yytext[0]); } %% pid_t grecs_preproc_pid; int -yywrap () +yywrap() { - if (yyin) - grecs_preproc_extrn_shutdown (grecs_preproc_pid); - else - grecs_preproc_done (); - grecs_current_locus.file = NULL; - return 1; + if (yyin) + grecs_preproc_extrn_shutdown(grecs_preproc_pid); + else + grecs_preproc_done(); + grecs_current_locus.file = NULL; + return 1; } static void -line_acc_free_entry (void *ptr) +line_acc_free_entry(void *ptr) { - free (ptr); + free(ptr); } int -grecs_lex_begin (const char *name) +grecs_lex_begin(const char *name) { - if (yy_flex_debug > 0) - yy_flex_debug = 0; + if (yy_flex_debug > 0) + yy_flex_debug = 0; - line_acc = grecs_list_create (); - line_acc->free_entry = line_acc_free_entry; - string_list = grecs_list_create (); - - if (grecs_preprocessor) - { - int fd; + line_acc = grecs_list_create(); + line_acc->free_entry = line_acc_free_entry; + string_list = grecs_list_create(); - fd = open (name, O_RDONLY); - if (fd == -1) - { - grecs_error (NULL, errno, _("Cannot open `%s'"), name); - return 1; - } - close (fd); - - yyin = grecs_preproc_extrn_start (name, &grecs_preproc_pid); - if (!yyin) - { - grecs_error (NULL, errno, - _("Unable to start external preprocessor `%s'"), - grecs_preprocessor); - return 1; - } - } - else - return grecs_preproc_init (name); - - return 0; + if (grecs_preprocessor) { + int fd; + + fd = open(name, O_RDONLY); + if (fd == -1) { + grecs_error(NULL, errno, _("Cannot open `%s'"), name); + return 1; + } + close(fd); + + yyin = grecs_preproc_extrn_start(name, &grecs_preproc_pid); + if (!yyin) { + grecs_error(NULL, errno, + _("Unable to start external preprocessor `%s'"), + grecs_preprocessor); + return 1; + } + } else + return grecs_preproc_init(name); + + return 0; } void -grecs_lex_end () +grecs_lex_end() { } static int -isemptystr (int off) +isemptystr(int off) { - for (; yytext[off] && isspace (yytext[off]); off++) - ; - if (yytext[off] == ';') - { - int i; - for (i = off + 1; yytext[i]; i++) - if (!isspace (yytext[i])) - return 0; - yyless (off); - return 1; - } - return yytext[off] == 0; + for (; yytext[off] && isspace(yytext[off]); off++) + ; + if (yytext[off] == ';') { + int i; + for (i = off + 1; yytext[i]; i++) + if (!isspace(yytext[i])) + return 0; + yyless(off); + return 1; + } + return yytext[off] == 0; } char * -multiline_strip_tabs (char *text) +multiline_strip_tabs(char *text) { - if (char_to_strip) - for (; *text && char_to_strip (*text); text++) - ; - return text; + if (char_to_strip) + for (; *text && char_to_strip(*text); text++) + ; + return text; } static int -unquote_char (int c) +unquote_char(int c) { - static char quote_transtab[] = "\\\\\"\"a\ab\bf\fn\nr\rt\tv\v"; - - char *p; - - for (p = quote_transtab; *p; p += 2) - { - if (*p == c) - return p[1]; - } - return -1; + static char quote_transtab[] = "\\\\\"\"a\ab\bf\fn\nr\rt\tv\v"; + + char *p; + + for (p = quote_transtab; *p; p += 2) { + if (*p == c) + return p[1]; + } + return -1; } struct line_acc_entry { - size_t size; + size_t size; }; #define line_acc_ptr(entry) (char*)(entry + 1) static void -line_acc_add_string (const char *str, size_t len) +line_acc_add_string(const char *str, size_t len) { - struct line_acc_entry *ent = grecs_malloc (sizeof (*ent) + len + 1); - char *p = line_acc_ptr (ent); - memcpy (p, str, len); - p[len] = 0; - ent->size = len; - grecs_list_append (line_acc, ent); + struct line_acc_entry *ent = grecs_malloc(sizeof(*ent) + len + 1); + char *p = line_acc_ptr(ent); + memcpy(p, str, len); + p[len] = 0; + ent->size = len; + grecs_list_append(line_acc, ent); } static void -line_acc_add_char (int c) +line_acc_add_char(int c) { - char t = c; - line_acc_add_string (&t, 1); + char t = c; + line_acc_add_string(&t, 1); } static void -list_acc_unescape_char (int c) +list_acc_unescape_char(int c) { - if (c != '\n') - { - int t = unquote_char (c); - if (t != -1) - line_acc_add_char (t); - else - { - grecs_warning(&grecs_current_locus, 0, - _("unknown escape sequence '\\%c'"), - c); - line_acc_add_char (c); + if (c != '\n') { + int t = unquote_char(c); + if (t != -1) + line_acc_add_char(t); + else { + grecs_warning(&grecs_current_locus, 0, + _("unknown escape sequence '\\%c'"), + c); + line_acc_add_char(c); + } } - } } void -grecs_line_add (const char *text, size_t len) +grecs_line_add(const char *text, size_t len) { - line_acc_add_string (text, len); + line_acc_add_string(text, len); } /* Same, but unescapes the last character from yytext */ static void -line_add_unescape_last (char *text, size_t len) +line_add_unescape_last(char *text, size_t len) { - line_acc_add_string (text, len - 2); - list_acc_unescape_char (text[len - 1]); + line_acc_add_string(text, len - 2); + list_acc_unescape_char(text[len - 1]); } static void -multiline_add (char *s) +multiline_add(char *s) { - if (multiline_unescape) - { - for (; *s; s++) - { - if (*s == '\\') - { - list_acc_unescape_char (s[1]); - ++s; - } - else - line_acc_add_char (*s); - } - } - else - grecs_line_add (s, strlen (s)); + if (multiline_unescape) { + for (; *s; s++) { + if (*s == '\\') { + list_acc_unescape_char(s[1]); + ++s; + } else + line_acc_add_char(*s); + } + } else + grecs_line_add(s, strlen(s)); } void -grecs_line_begin () +grecs_line_begin() { /* FIXME: nothing so far. Maybe prepare stk by calling obstack_finish? */ } static int -is_tab (char c) +is_tab(char c) { - return c == '\t'; + return c == '\t'; } static int -is_ws (char c) +is_ws(char c) { - return c == '\t' || c == ' '; + return c == '\t' || c == ' '; } void -multiline_begin (char *p) +multiline_begin(char *p) { - if (*p == '-') - { - if (*++p == ' ') - { - char_to_strip = is_ws; - p++; - } - else - char_to_strip = is_tab; - } - else - char_to_strip = NULL; - if (*p == '\\') - { - p++; - multiline_unescape = 0; - } - else if (*p == '"') - { - char *q; + if (*p == '-') { + if (*++p == ' ') { + char_to_strip = is_ws; + p++; + } else + char_to_strip = is_tab; + } else + char_to_strip = NULL; + if (*p == '\\') { + p++; + multiline_unescape = 0; + } else if (*p == '"') { + char *q; - p++; - multiline_unescape = 0; - q = strchr (p, '"'); - multiline_delimiter_len = q - p; - } - else - { - multiline_delimiter_len = strcspn (p, " \t"); - multiline_unescape = 1; - } - - /* Remove trailing newline */ - multiline_delimiter_len--; - multiline_delimiter = grecs_malloc (multiline_delimiter_len + 1); - memcpy (multiline_delimiter, p, multiline_delimiter_len); - multiline_delimiter[multiline_delimiter_len] = 0; - grecs_line_begin (); + p++; + multiline_unescape = 0; + q = strchr(p, '"'); + multiline_delimiter_len = q - p; + } else { + multiline_delimiter_len = strcspn(p, " \t"); + multiline_unescape = 1; + } + + /* Remove trailing newline */ + multiline_delimiter_len--; + multiline_delimiter = grecs_malloc(multiline_delimiter_len + 1); + memcpy(multiline_delimiter, p, multiline_delimiter_len); + multiline_delimiter[multiline_delimiter_len] = 0; + grecs_line_begin(); } char * -grecs_line_finish () +grecs_line_finish() { - struct grecs_list_entry *ep; - size_t size = 0; - char *str, *p; - - for (ep = line_acc->head; ep; ep = ep->next) - { - struct line_acc_entry *ent = ep->data; - size += ent->size; - } - - str = grecs_malloc (size + 1); - grecs_list_append (string_list, str); - for (ep = line_acc->head, p = str; ep; ep = ep->next) - { - struct line_acc_entry *ent = ep->data; - char *str = line_acc_ptr (ent); - memcpy (p, str, ent->size); - p += ent->size; - } - *p = 0; - grecs_list_clear (line_acc); - return str; + struct grecs_list_entry *ep; + size_t size = 0; + char *str, *p; + + for (ep = line_acc->head; ep; ep = ep->next) { + struct line_acc_entry *ent = ep->data; + size += ent->size; + } + + str = grecs_malloc(size + 1); + grecs_list_append(string_list, str); + for (ep = line_acc->head, p = str; ep; ep = ep->next) { + struct line_acc_entry *ent = ep->data; + char *str = line_acc_ptr(ent); + memcpy(p, str, ent->size); + p += ent->size; + } + *p = 0; + grecs_list_clear(line_acc); + return str; } static int -ident () +ident() { - char *p; - char *str; - size_t len; + char *p; + char *str; + size_t len; - for (p = yytext; *p && isspace (*p); p++) - ; - - len = strlen (p); - str = grecs_malloc (len + 1); - strcpy (str, p); - grecs_list_append (string_list, str); - yylval.string = str; - return IDENT; + for (p = yytext; *p && isspace(*p); p++) + ; + + len = strlen(p); + str = grecs_malloc(len + 1); + strcpy(str, p); + grecs_list_append(string_list, str); + yylval.string = str; + return IDENT; } void -grecs_lex_trace (int n) +grecs_lex_trace(int n) { - yy_flex_debug = -n; + yy_flex_debug = -n; } grecs_value_t * -grecs_value_dup (grecs_value_t *input) +grecs_value_dup(grecs_value_t *input) { - grecs_value_t *ptr = grecs_malloc (sizeof (*ptr)); - grecs_list_append (string_list, ptr); - *ptr = *input; - return ptr; + grecs_value_t *ptr = grecs_malloc(sizeof(*ptr)); + grecs_list_append(string_list, ptr); + *ptr = *input; + return ptr; } static int -assign_locus (grecs_locus_t *ploc, char *name, char *line, size_t *pxlines) +assign_locus(grecs_locus_t *ploc, char *name, char *line, size_t *pxlines) { - char *p; - - if (name) - { - if (pxlines && (!ploc->file || strcmp(name, ploc->file))) - *pxlines = 0; - ploc->file = grecs_install_text (name); - } - ploc->line = strtoul (line, &p, 10) - (pxlines ? *pxlines : 0); - return *p != 0; + char *p; + + if (name) { + if (pxlines && (!ploc->file || strcmp(name, ploc->file))) + *pxlines = 0; + ploc->file = grecs_install_text(name); + } + ploc->line = strtoul(line, &p, 10) - (pxlines ? *pxlines : 0); + return *p != 0; } static void -parse_line (char *text, grecs_locus_t *ploc, size_t *pxlines) +parse_line(char *text, grecs_locus_t *ploc, size_t *pxlines) { - int rc = 1; - struct wordsplit ws; - - if (wordsplit (text, &ws, WRDSF_DEFFLAGS)) - grecs_error (ploc, 0, _("cannot parse #line line")); - else - { - if (ws.ws_wordc == 2) - rc = assign_locus (ploc, NULL, ws.ws_wordv[1], pxlines); - else if (ws.ws_wordc == 3) - rc = assign_locus (ploc, ws.ws_wordv[2], ws.ws_wordv[1], pxlines); - else if (ws.ws_wordc == 4) - { - rc = assign_locus (ploc, ws.ws_wordv[2], ws.ws_wordv[1], 0); - if (rc == 0) - { - char *p; - unsigned long x = strtoul (ws.ws_wordv[3], &p, 10); - rc = *p != 0; - if (rc == 0) - *pxlines = x; - } - } - else - grecs_error (ploc, 0, _("invalid #line statement")); + int rc = 1; + struct wordsplit ws; + + if (wordsplit(text, &ws, WRDSF_DEFFLAGS)) + grecs_error(ploc, 0, _("cannot parse #line line")); + else { + if (ws.ws_wordc == 2) + rc = assign_locus(ploc, NULL, + ws.ws_wordv[1], pxlines); + else if (ws.ws_wordc == 3) + rc = assign_locus(ploc, ws.ws_wordv[2], + ws.ws_wordv[1], pxlines); + else if (ws.ws_wordc == 4) { + rc = assign_locus(ploc, ws.ws_wordv[2], + ws.ws_wordv[1], 0); + if (rc == 0) { + char *p; + unsigned long x = strtoul(ws.ws_wordv[3], + &p, 10); + rc = *p != 0; + if (rc == 0) + *pxlines = x; + } + } else + grecs_error(ploc, 0, _("invalid #line statement")); - if (rc) - grecs_error (ploc, 0, _("malformed #line statement")); - wordsplit_free (&ws); - } + if (rc) + grecs_error(ploc, 0, _("malformed #line statement")); + wordsplit_free(&ws); + } } static void -parse_line_cpp (char *text, grecs_locus_t *ploc, size_t *pxlines) +parse_line_cpp(char *text, grecs_locus_t *ploc, size_t *pxlines) { - struct wordsplit ws; - - if (wordsplit (text, &ws, WRDSF_DEFFLAGS)) - { - grecs_error (ploc, 0, _("cannot parse #line line")); - return; - } - else if (ws.ws_wordc < 3) - grecs_error (ploc, 0, _("invalid #line statement")); - else - { - if (assign_locus (ploc, ws.ws_wordv[2], ws.ws_wordv[1], pxlines)) - grecs_error (ploc, 0, _("malformed #line statement")); - } - wordsplit_free (&ws); + struct wordsplit ws; + + if (wordsplit(text, &ws, WRDSF_DEFFLAGS)) { + grecs_error(ploc, 0, _("cannot parse #line line")); + return; + } else if (ws.ws_wordc < 3) + grecs_error(ploc, 0, _("invalid #line statement")); + else { + if (assign_locus(ploc, ws.ws_wordv[2], + ws.ws_wordv[1], pxlines)) + grecs_error(ploc, 0, _("malformed #line statement")); + } + wordsplit_free(&ws); } diff --git a/src/grecs.h b/src/grecs.h index ec98979..5769736 100644 --- a/src/grecs.h +++ b/src/grecs.h @@ -28,37 +28,37 @@ # endif #endif #ifndef _ -# define _(msgid) gettext (msgid) +# define _(msgid) gettext(msgid) #endif #ifndef N_ # define N_(s) s #endif typedef struct { - char *file; - int line; + char *file; + int line; } grecs_locus_t; extern grecs_locus_t grecs_locus; enum grecs_data_type { - grecs_type_void, - grecs_type_string, - grecs_type_short, - grecs_type_ushort, - grecs_type_int, - grecs_type_uint, - grecs_type_long, - grecs_type_ulong, - grecs_type_size, + grecs_type_void, + grecs_type_string, + grecs_type_short, + grecs_type_ushort, + grecs_type_int, + grecs_type_uint, + grecs_type_long, + grecs_type_ulong, + grecs_type_size, /* grecs_type_off,*/ - grecs_type_time, - grecs_type_bool, - grecs_type_ipv4, - grecs_type_cidr, - grecs_type_host, - grecs_type_sockaddr, - grecs_type_section + grecs_type_time, + grecs_type_bool, + grecs_type_ipv4, + grecs_type_cidr, + grecs_type_host, + grecs_type_sockaddr, + grecs_type_section }; #define GRECS_LIST 0x8000 @@ -67,9 +67,9 @@ enum grecs_data_type { #define GRECS_IS_LIST(c) ((c) & GRECS_LIST) enum grecs_callback_command { - grecs_callback_section_begin, - grecs_callback_section_end, - grecs_callback_set_value + grecs_callback_section_begin, + grecs_callback_section_end, + grecs_callback_set_value }; #define GRECS_TYPE_STRING 0 @@ -89,45 +89,45 @@ struct grecs_list { }; typedef struct grecs_value { - int type; - union { - struct grecs_list *list; - const char *string; - struct { - size_t c; - struct grecs_value *v; - } arg; - } v; + int type; + union { + struct grecs_list *list; + const char *string; + struct { + size_t c; + struct grecs_value *v; + } arg; + } v; } grecs_value_t; -typedef int (*grecs_callback_fn) ( - enum grecs_callback_command cmd, - grecs_locus_t * /* locus */, - void * /* varptr */