diff options
Diffstat (limited to 'buf.c')
-rw-r--r-- | buf.c | 82 |
1 files changed, 43 insertions, 39 deletions
@@ -51,6 +51,7 @@ struct Buf defs_buf; /**< for #define's autogenerated. List of strings. */ struct Buf yydmap_buf; /**< string buffer to hold yydmap elements */ struct Buf m4defs_buf; /**< m4 definitions. List of strings. */ struct Buf top_buf; /**< contains %top code. String buffer. */ +struct Buf bottom_buf; /**< contains %bottom code. String buffer. */ struct Buf *buf_print_strings(struct Buf * buf, FILE* out) { @@ -73,7 +74,7 @@ struct Buf *buf_prints (struct Buf *buf, const char *fmt, const char *s) char *t; size_t tsz; - t = flex_alloc (tsz = strlen (fmt) + strlen (s) + 1); +t = (char*)flex_alloc (tsz = strlen (fmt) + strlen (s) + 1); if (!t) flexfatal (_("Allocation of buffer to print string failed")); snprintf (t, tsz, fmt, s); @@ -90,22 +91,15 @@ struct Buf *buf_prints (struct Buf *buf, const char *fmt, const char *s) */ struct Buf *buf_linedir (struct Buf *buf, const char* filename, int lineno) { - char *dst, *src, *t; - - t = flex_alloc (strlen ("#line \"\"\n") + /* constant parts */ - 2 * strlen (filename) + /* filename with possibly all backslashes escaped */ - (int) (1 + log10 (abs (lineno))) + /* line number */ - 1); /* NUL */ - if (!t) - flexfatal (_("Allocation of buffer for line directive failed")); - for (dst = t + sprintf (t, "#line %d \"", lineno), src = filename; *src; *dst++ = *src++) - if (*src == '\\') /* escape backslashes */ - *dst++ = '\\'; - *dst++ = '"'; - *dst++ = '\n'; - *dst = '\0'; - buf = buf_strappend (buf, t); - flex_free (t); + char *t, *fmt = "#line %d \"%s\"\n"; + size_t tsz; + + if (gen_line_dirs) { + t = (char*)flex_alloc (tsz = strlen (fmt) + strlen (filename) + (int)(1 + log10(lineno>=0?lineno:-lineno)) + 1); + snprintf (t, tsz, fmt, lineno, filename); + buf = buf_strappend (buf, t); + flex_free (t); + } return buf; } @@ -123,10 +117,10 @@ struct Buf *buf_concat(struct Buf* dest, const struct Buf* src) /* Appends n characters in str to buf. */ -struct Buf *buf_strnappend (buf, str, n) - struct Buf *buf; - const char *str; - int n; +struct Buf *buf_strnappend ( + struct Buf *buf, + const char *str, + int n) { buf_append (buf, str, n + 1); @@ -137,18 +131,18 @@ struct Buf *buf_strnappend (buf, str, n) } /* Appends characters in str to buf. */ -struct Buf *buf_strappend (buf, str) - struct Buf *buf; - const char *str; +struct Buf *buf_strappend ( + struct Buf *buf, + const char *str) { return buf_strnappend (buf, str, strlen (str)); } /* appends "#define str def\n" */ -struct Buf *buf_strdefine (buf, str, def) - struct Buf *buf; - const char *str; - const char *def; +struct Buf *buf_strdefine ( + struct Buf *buf, + const char *str, + const char *def) { buf_strappend (buf, "#define "); buf_strappend (buf, " "); @@ -162,7 +156,7 @@ struct Buf *buf_strdefine (buf, str, def) /** Pushes "m4_define( [[def]], [[val]])m4_dnl" to end of buffer. * @param buf A buffer as a list of strings. * @param def The m4 symbol to define. - * @param val The definition; may be NULL. + * @param val The definition; may be NULL. If so, val="1". * @return buf */ struct Buf *buf_m4_define (struct Buf *buf, const char* def, const char* val) @@ -171,7 +165,7 @@ struct Buf *buf_m4_define (struct Buf *buf, const char* def, const char* val) char * str; size_t strsz; - val = val?val:""; + val = val?val:"1"; str = (char*)flex_alloc(strsz = strlen(fmt) + strlen(def) + strlen(val) + 2); if (!str) flexfatal (_("Allocation of buffer for m4 def failed")); @@ -202,9 +196,9 @@ struct Buf *buf_m4_undefine (struct Buf *buf, const char* def) } /* create buf with 0 elements, each of size elem_size. */ -void buf_init (buf, elem_size) - struct Buf *buf; - size_t elem_size; +void buf_init ( + struct Buf *buf, + size_t elem_size) { buf->elts = (void *) 0; buf->nelts = 0; @@ -213,14 +207,24 @@ void buf_init (buf, elem_size) } /* frees memory */ -void buf_destroy (buf) - struct Buf *buf; +void buf_destroy ( + struct Buf *buf) { if (buf && buf->elts) flex_free (buf->elts); buf->elts = (void *) 0; } +/* frees buffer memory for a list of (char*) strings. */ +void buf_free (struct Buf *buf) +{ + int i; + char **strings = (char**)buf->elts; + for (i=0; i<buf->nelts; i++) { + if (strings[i]) flex_free(strings[i]); + } + buf->nelts = 0; +} /* appends ptr[] to buf, grow if necessary. * n_elem is number of elements in ptr[], NOT bytes. @@ -228,10 +232,10 @@ void buf_destroy (buf) * We grow by mod(512) boundaries. */ -struct Buf *buf_append (buf, ptr, n_elem) - struct Buf *buf; - const void *ptr; - int n_elem; +struct Buf *buf_append ( + struct Buf *buf, + const void *ptr, + int n_elem) { int n_alloc = 0; |