summaryrefslogtreecommitdiff
path: root/usr.bin/lex/buf.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin/lex/buf.c')
-rw-r--r--usr.bin/lex/buf.c231
1 files changed, 122 insertions, 109 deletions
diff --git a/usr.bin/lex/buf.c b/usr.bin/lex/buf.c
index 9fe8aea9f84..4ccb1d4e830 100644
--- a/usr.bin/lex/buf.c
+++ b/usr.bin/lex/buf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: buf.c,v 1.2 2015/11/19 22:16:43 tedu Exp $ */
+/* $OpenBSD: buf.c,v 1.3 2015/11/19 22:52:40 tedu Exp $ */
/* flex - tool to generate fast lexical analyzers */
@@ -32,55 +32,58 @@
/* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
/* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
/* PURPOSE. */
-
+
+
#include "flexdef.h"
/* 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
* line.
- *
+ *
* 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
* elt_size is sizeof(char*), then the buffer should be treated as a list of
* 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. */
struct Buf userdef_buf; /**< for user #definitions triggered by cmd-line. */
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 m4defs_buf; /**< m4 definitions. List of strings. */
+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)
- return buf;
+ if (!buf || !out)
+ return buf;
- for (i=0; i < buf->nelts; i++){
- const char * s = ((char**)buf->elts)[i];
- if(s)
- fprintf(out, "%s", s);
- }
- return buf;
+ for (i = 0; i < buf->nelts; i++) {
+ const char *s = ((char **) buf->elts)[i];
+ if (s)
+ fprintf(out, "%s", s);
+ }
+ return buf;
}
/* 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;
- size_t tsz;
+ char *t;
+ size_t tsz;
- t = flex_alloc (tsz = strlen (fmt) + strlen (s) + 1);
+ t = flex_alloc(tsz = strlen(fmt) + strlen(s) + 1);
if (!t)
- flexfatal (_("Allocation of buffer to print string failed"));
- snprintf (t, tsz, fmt, s);
- buf = buf_strappend (buf, t);
- flex_free (t);
+ flexfatal(_("Allocation of buffer to print string failed"));
+ snprintf(t, tsz, fmt, s);
+ buf = buf_strappend(buf, t);
+ flex_free(t);
return buf;
}
@@ -90,27 +93,29 @@ struct Buf *buf_prints (struct Buf *buf, const char *fmt, const char *s)
* @param lineno line number
* @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;
- char *dst, *t;
- size_t tsz;
-
- t = flex_alloc (tsz = 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 + snprintf (t, tsz, "#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);
- return buf;
+ const char *src;
+ char *dst, *t;
+ size_t tsz;
+
+ t = flex_alloc(tsz = 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 + snprintf(t, tsz, "#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);
+ return buf;
}
@@ -119,20 +124,22 @@ struct Buf *buf_linedir (struct Buf *buf, const char* filename, int lineno)
* @param @a dest the source buffer
* @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);
- return dest;
+ buf_append(dest, src->elts, src->nelts);
+ return dest;
}
/* 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(buf, str, n)
+ struct Buf *buf;
+ 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. */
buf->nelts--;
@@ -141,25 +148,27 @@ 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(buf, 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" */
-struct Buf *buf_strdefine (buf, str, def)
- struct Buf *buf;
- const char *str;
- const char *def;
+struct Buf *
+buf_strdefine(buf, str, def)
+ struct Buf *buf;
+ const char *str;
+ const char *def;
{
- buf_strappend (buf, "#define ");
- buf_strappend (buf, " ");
- buf_strappend (buf, str);
- buf_strappend (buf, " ");
- buf_strappend (buf, def);
- buf_strappend (buf, "\n");
+ buf_strappend(buf, "#define ");
+ buf_strappend(buf, " ");
+ buf_strappend(buf, str);
+ buf_strappend(buf, " ");
+ buf_strappend(buf, def);
+ buf_strappend(buf, "\n");
return buf;
}
@@ -169,20 +178,21 @@ struct Buf *buf_strdefine (buf, str, def)
* @param val The definition; may be NULL.
* @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";
- char * str;
- size_t strsz;
-
- val = val?val:"";
- str = (char*)flex_alloc(strsz = strlen(fmt) + strlen(def) + strlen(val) + 2);
- if (!str)
- flexfatal (_("Allocation of buffer for m4 def failed"));
-
- snprintf(str, strsz, fmt, def, val);
- buf_append(buf, &str, 1);
- return buf;
+ const char *fmt = "m4_define( [[%s]], [[%s]])m4_dnl\n";
+ char *str;
+ size_t strsz;
+
+ val = val ? val : "";
+ str = (char *) flex_alloc(strsz = strlen(fmt) + strlen(def) + strlen(val) + 2);
+ if (!str)
+ flexfatal(_("Allocation of buffer for m4 def failed"));
+
+ snprintf(str, strsz, fmt, def, val);
+ buf_append(buf, &str, 1);
+ return buf;
}
/** Pushes "m4_undefine([[def]])m4_dnl" to end of buffer.
@@ -190,25 +200,27 @@ struct Buf *buf_m4_define (struct Buf *buf, const char* def, const char* val)
* @param def The m4 symbol to undefine.
* @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";
- char * str;
- size_t strsz;
+ const char *fmt = "m4_undefine( [[%s]])m4_dnl\n";
+ char *str;
+ size_t strsz;
- str = (char*)flex_alloc(strsz = strlen(fmt) + strlen(def) + 2);
- if (!str)
- flexfatal (_("Allocation of buffer for m4 undef failed"));
+ str = (char *) flex_alloc(strsz = strlen(fmt) + strlen(def) + 2);
+ if (!str)
+ flexfatal(_("Allocation of buffer for m4 undef failed"));
- snprintf(str, strsz, fmt, def);
- buf_append(buf, &str, 1);
- return buf;
+ snprintf(str, strsz, fmt, def);
+ buf_append(buf, &str, 1);
+ return buf;
}
/* 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(buf, elem_size)
+ struct Buf *buf;
+ size_t elem_size;
{
buf->elts = (void *) 0;
buf->nelts = 0;
@@ -217,11 +229,12 @@ void buf_init (buf, elem_size)
}
/* frees memory */
-void buf_destroy (buf)
- struct Buf *buf;
+void
+buf_destroy(buf)
+ struct Buf *buf;
{
if (buf && buf->elts)
- flex_free (buf->elts);
+ flex_free(buf->elts);
buf->elts = (void *) 0;
}
@@ -232,12 +245,13 @@ 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(buf, ptr, n_elem)
+ struct Buf *buf;
+ const void *ptr;
+ int n_elem;
{
- int n_alloc = 0;
+ int n_alloc = 0;
if (!ptr || n_elem == 0)
return buf;
@@ -252,23 +266,22 @@ struct Buf *buf_append (buf, ptr, n_elem)
if (((n_alloc * buf->elt_size) % 512) != 0
&& buf->elt_size < 512)
n_alloc +=
- (512 -
- ((n_alloc * buf->elt_size) % 512)) /
- buf->elt_size;
+ (512 -
+ ((n_alloc * buf->elt_size) % 512)) /
+ buf->elt_size;
if (!buf->elts)
buf->elts =
- allocate_array (n_alloc, buf->elt_size);
+ allocate_array(n_alloc, buf->elt_size);
else
buf->elts =
- reallocate_array (buf->elts, n_alloc,
- buf->elt_size);
+ reallocate_array(buf->elts, n_alloc,
+ buf->elt_size);
buf->nmax = n_alloc;
}
-
- memcpy ((char *) buf->elts + buf->nelts * buf->elt_size, ptr,
- n_elem * buf->elt_size);
+ memcpy((char *) buf->elts + buf->nelts * buf->elt_size, ptr,
+ n_elem * buf->elt_size);
buf->nelts += n_elem;
return buf;