version 1.2, 2015/11/19 22:16:43 |
version 1.3, 2015/11/19 22:52:40 |
|
|
/* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */ |
/* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */ |
/* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */ |
/* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */ |
/* PURPOSE. */ |
/* PURPOSE. */ |
|
|
|
|
#include "flexdef.h" |
#include "flexdef.h" |
|
|
/* Take note: The buffer object is sometimes used as a String buffer (one |
/* Take note: The buffer object is sometimes used as a String buffer (one |
* continuous string), and sometimes used as a list of strings, usually line by |
* continuous string), and sometimes used as a list of strings, usually line by |
* line. |
* line. |
* |
* |
* The type is specified in buf_init by the elt_size. If the elt_size is |
* The type is specified in buf_init by the elt_size. If the elt_size is |
* sizeof(char), then the buffer should be treated as string buffer. If the |
* sizeof(char), then the buffer should be treated as string buffer. If the |
* elt_size is sizeof(char*), then the buffer should be treated as a list of |
* elt_size is sizeof(char*), then the buffer should be treated as a list of |
* strings. |
* strings. |
* |
* |
* Certain functions are only appropriate for one type or the other. |
* Certain functions are only appropriate for one type or the other. |
*/ |
*/ |
|
|
/* global buffers. */ |
/* global buffers. */ |
struct Buf userdef_buf; /**< for user #definitions triggered by cmd-line. */ |
struct Buf userdef_buf; /**< for user #definitions triggered by cmd-line. */ |
struct Buf defs_buf; /**< for #define's autogenerated. List of strings. */ |
struct Buf defs_buf; /**< for #define's autogenerated. List of strings. */ |
struct Buf yydmap_buf; /**< string buffer to hold yydmap elements */ |
struct Buf yydmap_buf; /**< string buffer to hold yydmap elements */ |
struct Buf m4defs_buf; /**< m4 definitions. List of strings. */ |
struct Buf m4defs_buf; /**< m4 definitions. List of strings. */ |
struct Buf top_buf; /**< contains %top code. String buffer. */ |
struct Buf top_buf; /**< contains %top code. String buffer. */ |
|
|
struct Buf *buf_print_strings(struct Buf * buf, FILE* out) |
struct Buf * |
|
buf_print_strings(struct Buf * buf, FILE * out) |
{ |
{ |
int i; |
int i; |
|
|
if(!buf || !out) |
if (!buf || !out) |
return buf; |
return buf; |
|
|
for (i=0; i < buf->nelts; i++){ |
for (i = 0; i < buf->nelts; i++) { |
const char * s = ((char**)buf->elts)[i]; |
const char *s = ((char **) buf->elts)[i]; |
if(s) |
if (s) |
fprintf(out, "%s", s); |
fprintf(out, "%s", s); |
} |
} |
return buf; |
return buf; |
} |
} |
|
|
/* Append a "%s" formatted string to a string buffer */ |
/* Append a "%s" formatted string to a string buffer */ |
struct Buf *buf_prints (struct Buf *buf, const char *fmt, const char *s) |
struct Buf * |
|
buf_prints(struct Buf * buf, const char *fmt, const char *s) |
{ |
{ |
char *t; |
char *t; |
size_t tsz; |
size_t tsz; |
|
|
t = flex_alloc (tsz = strlen (fmt) + strlen (s) + 1); |
t = flex_alloc(tsz = strlen(fmt) + strlen(s) + 1); |
if (!t) |
if (!t) |
flexfatal (_("Allocation of buffer to print string failed")); |
flexfatal(_("Allocation of buffer to print string failed")); |
snprintf (t, tsz, fmt, s); |
snprintf(t, tsz, fmt, s); |
buf = buf_strappend (buf, t); |
buf = buf_strappend(buf, t); |
flex_free (t); |
flex_free(t); |
return buf; |
return buf; |
} |
} |
|
|
|
|
* @param lineno line number |
* @param lineno line number |
* @return buf |
* @return buf |
*/ |
*/ |
struct Buf *buf_linedir (struct Buf *buf, const char* filename, int lineno) |
struct Buf * |
|
buf_linedir(struct Buf * buf, const char *filename, int lineno) |
{ |
{ |
const char *src; |
const char *src; |
char *dst, *t; |
char *dst, *t; |
size_t tsz; |
size_t tsz; |
|
|
t = flex_alloc (tsz = strlen ("#line \"\"\n") + /* constant parts */ |
t = flex_alloc(tsz = strlen("#line \"\"\n") + /* constant parts */ |
2 * strlen (filename) + /* filename with possibly all backslashes escaped */ |
2 * strlen(filename) + /* filename with possibly all |
(int) (1 + log10 (abs (lineno))) + /* line number */ |
* backslashes escaped */ |
1); /* NUL */ |
(int) (1 + log10(abs(lineno))) + /* line number */ |
if (!t) |
1); /* NUL */ |
flexfatal (_("Allocation of buffer for line directive failed")); |
if (!t) |
for (dst = t + snprintf (t, tsz, "#line %d \"", lineno), src = filename; *src; *dst++ = *src++) |
flexfatal(_("Allocation of buffer for line directive failed")); |
if (*src == '\\') /* escape backslashes */ |
for (dst = t + snprintf(t, tsz, "#line %d \"", lineno), src = filename; *src; *dst++ = *src++) |
*dst++ = '\\'; |
if (*src == '\\') /* escape backslashes */ |
*dst++ = '"'; |
*dst++ = '\\'; |
*dst++ = '\n'; |
*dst++ = '"'; |
*dst = '\0'; |
*dst++ = '\n'; |
buf = buf_strappend (buf, t); |
*dst = '\0'; |
flex_free (t); |
buf = buf_strappend(buf, t); |
return buf; |
flex_free(t); |
|
return buf; |
} |
} |
|
|
|
|
|
|
* @param @a dest the source buffer |
* @param @a dest the source buffer |
* @return @a dest |
* @return @a dest |
*/ |
*/ |
struct Buf *buf_concat(struct Buf* dest, const struct Buf* src) |
struct Buf * |
|
buf_concat(struct Buf * dest, const struct Buf * src) |
{ |
{ |
buf_append(dest, src->elts, src->nelts); |
buf_append(dest, src->elts, src->nelts); |
return dest; |
return dest; |
} |
} |
|
|
|
|
/* Appends n characters in str to buf. */ |
/* Appends n characters in str to buf. */ |
struct Buf *buf_strnappend (buf, str, n) |
struct Buf * |
struct Buf *buf; |
buf_strnappend(buf, str, n) |
const char *str; |
struct Buf *buf; |
int n; |
const char *str; |
|
int n; |
{ |
{ |
buf_append (buf, str, n + 1); |
buf_append(buf, str, n + 1); |
|
|
/* "undo" the '\0' character that buf_append() already copied. */ |
/* "undo" the '\0' character that buf_append() already copied. */ |
buf->nelts--; |
buf->nelts--; |
|
|
} |
} |
|
|
/* Appends characters in str to buf. */ |
/* Appends characters in str to buf. */ |
struct Buf *buf_strappend (buf, str) |
struct Buf * |
struct Buf *buf; |
buf_strappend(buf, str) |
const char *str; |
struct Buf *buf; |
|
const char *str; |
{ |
{ |
return buf_strnappend (buf, str, strlen (str)); |
return buf_strnappend(buf, str, strlen(str)); |
} |
} |
|
|
/* appends "#define str def\n" */ |
/* appends "#define str def\n" */ |
struct Buf *buf_strdefine (buf, str, def) |
struct Buf * |
struct Buf *buf; |
buf_strdefine(buf, str, def) |
const char *str; |
struct Buf *buf; |
const char *def; |
const char *str; |
|
const char *def; |
{ |
{ |
buf_strappend (buf, "#define "); |
buf_strappend(buf, "#define "); |
buf_strappend (buf, " "); |
buf_strappend(buf, " "); |
buf_strappend (buf, str); |
buf_strappend(buf, str); |
buf_strappend (buf, " "); |
buf_strappend(buf, " "); |
buf_strappend (buf, def); |
buf_strappend(buf, def); |
buf_strappend (buf, "\n"); |
buf_strappend(buf, "\n"); |
return buf; |
return buf; |
} |
} |
|
|
|
|
* @param val The definition; may be NULL. |
* @param val The definition; may be NULL. |
* @return buf |
* @return buf |
*/ |
*/ |
struct Buf *buf_m4_define (struct Buf *buf, const char* def, const char* val) |
struct Buf * |
|
buf_m4_define(struct Buf * buf, const char *def, const char *val) |
{ |
{ |
const char * fmt = "m4_define( [[%s]], [[%s]])m4_dnl\n"; |
const char *fmt = "m4_define( [[%s]], [[%s]])m4_dnl\n"; |
char * str; |
char *str; |
size_t strsz; |
size_t strsz; |
|
|
val = val?val:""; |
val = val ? val : ""; |
str = (char*)flex_alloc(strsz = strlen(fmt) + strlen(def) + strlen(val) + 2); |
str = (char *) flex_alloc(strsz = strlen(fmt) + strlen(def) + strlen(val) + 2); |
if (!str) |
if (!str) |
flexfatal (_("Allocation of buffer for m4 def failed")); |
flexfatal(_("Allocation of buffer for m4 def failed")); |
|
|
snprintf(str, strsz, fmt, def, val); |
snprintf(str, strsz, fmt, def, val); |
buf_append(buf, &str, 1); |
buf_append(buf, &str, 1); |
return buf; |
return buf; |
} |
} |
|
|
/** Pushes "m4_undefine([[def]])m4_dnl" to end of buffer. |
/** Pushes "m4_undefine([[def]])m4_dnl" to end of buffer. |
|
|
* @param def The m4 symbol to undefine. |
* @param def The m4 symbol to undefine. |
* @return buf |
* @return buf |
*/ |
*/ |
struct Buf *buf_m4_undefine (struct Buf *buf, const char* def) |
struct Buf * |
|
buf_m4_undefine(struct Buf * buf, const char *def) |
{ |
{ |
const char * fmt = "m4_undefine( [[%s]])m4_dnl\n"; |
const char *fmt = "m4_undefine( [[%s]])m4_dnl\n"; |
char * str; |
char *str; |
size_t strsz; |
size_t strsz; |
|
|
str = (char*)flex_alloc(strsz = strlen(fmt) + strlen(def) + 2); |
str = (char *) flex_alloc(strsz = strlen(fmt) + strlen(def) + 2); |
if (!str) |
if (!str) |
flexfatal (_("Allocation of buffer for m4 undef failed")); |
flexfatal(_("Allocation of buffer for m4 undef failed")); |
|
|
snprintf(str, strsz, fmt, def); |
snprintf(str, strsz, fmt, def); |
buf_append(buf, &str, 1); |
buf_append(buf, &str, 1); |
return buf; |
return buf; |
} |
} |
|
|
/* create buf with 0 elements, each of size elem_size. */ |
/* create buf with 0 elements, each of size elem_size. */ |
void buf_init (buf, elem_size) |
void |
struct Buf *buf; |
buf_init(buf, elem_size) |
size_t elem_size; |
struct Buf *buf; |
|
size_t elem_size; |
{ |
{ |
buf->elts = (void *) 0; |
buf->elts = (void *) 0; |
buf->nelts = 0; |
buf->nelts = 0; |
|
|
} |
} |
|
|
/* frees memory */ |
/* frees memory */ |
void buf_destroy (buf) |
void |
struct Buf *buf; |
buf_destroy(buf) |
|
struct Buf *buf; |
{ |
{ |
if (buf && buf->elts) |
if (buf && buf->elts) |
flex_free (buf->elts); |
flex_free(buf->elts); |
buf->elts = (void *) 0; |
buf->elts = (void *) 0; |
} |
} |
|
|
|
|
* We grow by mod(512) boundaries. |
* We grow by mod(512) boundaries. |
*/ |
*/ |
|
|
struct Buf *buf_append (buf, ptr, n_elem) |
struct Buf * |
struct Buf *buf; |
buf_append(buf, ptr, n_elem) |
const void *ptr; |
struct Buf *buf; |
int n_elem; |
const void *ptr; |
|
int n_elem; |
{ |
{ |
int n_alloc = 0; |
int n_alloc = 0; |
|
|
if (!ptr || n_elem == 0) |
if (!ptr || n_elem == 0) |
return buf; |
return buf; |
|
|
if (((n_alloc * buf->elt_size) % 512) != 0 |
if (((n_alloc * buf->elt_size) % 512) != 0 |
&& buf->elt_size < 512) |
&& buf->elt_size < 512) |
n_alloc += |
n_alloc += |
(512 - |
(512 - |
((n_alloc * buf->elt_size) % 512)) / |
((n_alloc * buf->elt_size) % 512)) / |
buf->elt_size; |
buf->elt_size; |
|
|
if (!buf->elts) |
if (!buf->elts) |
buf->elts = |
buf->elts = |
allocate_array (n_alloc, buf->elt_size); |
allocate_array(n_alloc, buf->elt_size); |
else |
else |
buf->elts = |
buf->elts = |
reallocate_array (buf->elts, n_alloc, |
reallocate_array(buf->elts, n_alloc, |
buf->elt_size); |
buf->elt_size); |
|
|
buf->nmax = n_alloc; |
buf->nmax = n_alloc; |
} |
} |
|
memcpy((char *) buf->elts + buf->nelts * buf->elt_size, ptr, |
memcpy ((char *) buf->elts + buf->nelts * buf->elt_size, ptr, |
n_elem * buf->elt_size); |
n_elem * buf->elt_size); |
|
buf->nelts += n_elem; |
buf->nelts += n_elem; |
|
|
return buf; |
return buf; |