inpcb: Use netisr_ncpus for listing inpcbs.
[dragonfly.git] / contrib / flex / buf.c
blob74a0ee080f2040228e2288ba11e51807269e01e5
1 /* flex - tool to generate fast lexical analyzers */
3 /* Copyright (c) 1990 The Regents of the University of California. */
4 /* All rights reserved. */
6 /* This code is derived from software contributed to Berkeley by */
7 /* Vern Paxson. */
9 /* The United States Government has rights in this work pursuant */
10 /* to contract no. DE-AC03-76SF00098 between the United States */
11 /* Department of Energy and the University of California. */
13 /* This file is part of flex. */
15 /* Redistribution and use in source and binary forms, with or without */
16 /* modification, are permitted provided that the following conditions */
17 /* are met: */
19 /* 1. Redistributions of source code must retain the above copyright */
20 /* notice, this list of conditions and the following disclaimer. */
21 /* 2. Redistributions in binary form must reproduce the above copyright */
22 /* notice, this list of conditions and the following disclaimer in the */
23 /* documentation and/or other materials provided with the distribution. */
25 /* Neither the name of the University nor the names of its contributors */
26 /* may be used to endorse or promote products derived from this software */
27 /* without specific prior written permission. */
29 /* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
30 /* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
31 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
32 /* PURPOSE. */
34 #include "flexdef.h"
36 /* Take note: The buffer object is sometimes used as a String buffer (one
37 * continuous string), and sometimes used as a list of strings, usually line by
38 * line.
40 * The type is specified in buf_init by the elt_size. If the elt_size is
41 * sizeof(char), then the buffer should be treated as string buffer. If the
42 * elt_size is sizeof(char*), then the buffer should be treated as a list of
43 * strings.
45 * Certain functions are only appropriate for one type or the other.
48 /* global buffers. */
49 struct Buf userdef_buf; /**< for user #definitions triggered by cmd-line. */
50 struct Buf defs_buf; /**< for #define's autogenerated. List of strings. */
51 struct Buf yydmap_buf; /**< string buffer to hold yydmap elements */
52 struct Buf m4defs_buf; /**< m4 definitions. List of strings. */
53 struct Buf top_buf; /**< contains %top code. String buffer. */
55 struct Buf *buf_print_strings(struct Buf * buf, FILE* out)
57 int i;
59 if(!buf || !out)
60 return buf;
62 for (i=0; i < buf->nelts; i++){
63 const char * s = ((char**)buf->elts)[i];
64 if(s)
65 fprintf(out, "%s", s);
67 return buf;
70 /* Append a "%s" formatted string to a string buffer */
71 struct Buf *buf_prints (struct Buf *buf, const char *fmt, const char *s)
73 char *t;
74 size_t tsz;
76 t = flex_alloc (tsz = strlen (fmt) + strlen (s) + 1);
77 if (!t)
78 flexfatal (_("Allocation of buffer to print string failed"));
79 snprintf (t, tsz, fmt, s);
80 buf = buf_strappend (buf, t);
81 flex_free (t);
82 return buf;
85 /** Append a line directive to the string buffer.
86 * @param buf A string buffer.
87 * @param filename file name
88 * @param lineno line number
89 * @return buf
91 struct Buf *buf_linedir (struct Buf *buf, const char* filename, int lineno)
93 char *dst, *src, *t;
94 char *dupe;
96 dupe = flex_alloc (strlen(filename) + 1);
97 strncpy(dupe, filename, strlen(filename));
98 t = flex_alloc (strlen ("#line \"\"\n") + /* constant parts */
99 2 * strlen (filename) + /* filename with possibly all backslashes escaped */
100 (int) (1 + log10 (abs (lineno))) + /* line number */
101 1); /* NUL */
102 if (!t)
103 flexfatal (_("Allocation of buffer for line directive failed"));
104 for (dst = t + sprintf (t, "#line %d \"", lineno), src = dupe; *src; *dst++ = *src++)
105 if (*src == '\\') /* escape backslashes */
106 *dst++ = '\\';
107 *dst++ = '"';
108 *dst++ = '\n';
109 *dst = '\0';
110 buf = buf_strappend (buf, t);
111 flex_free (t);
112 return buf;
116 /** Append the contents of @a src to @a dest.
117 * @param @a dest the destination buffer
118 * @param @a dest the source buffer
119 * @return @a dest
121 struct Buf *buf_concat(struct Buf* dest, const struct Buf* src)
123 buf_append(dest, src->elts, src->nelts);
124 return dest;
128 /* Appends n characters in str to buf. */
129 struct Buf *buf_strnappend (buf, str, n)
130 struct Buf *buf;
131 const char *str;
132 int n;
134 buf_append (buf, str, n + 1);
136 /* "undo" the '\0' character that buf_append() already copied. */
137 buf->nelts--;
139 return buf;
142 /* Appends characters in str to buf. */
143 struct Buf *buf_strappend (buf, str)
144 struct Buf *buf;
145 const char *str;
147 return buf_strnappend (buf, str, strlen (str));
150 /* appends "#define str def\n" */
151 struct Buf *buf_strdefine (buf, str, def)
152 struct Buf *buf;
153 const char *str;
154 const char *def;
156 buf_strappend (buf, "#define ");
157 buf_strappend (buf, " ");
158 buf_strappend (buf, str);
159 buf_strappend (buf, " ");
160 buf_strappend (buf, def);
161 buf_strappend (buf, "\n");
162 return buf;
165 /** Pushes "m4_define( [[def]], [[val]])m4_dnl" to end of buffer.
166 * @param buf A buffer as a list of strings.
167 * @param def The m4 symbol to define.
168 * @param val The definition; may be NULL.
169 * @return buf
171 struct Buf *buf_m4_define (struct Buf *buf, const char* def, const char* val)
173 const char * fmt = "m4_define( [[%s]], [[%s]])m4_dnl\n";
174 char * str;
175 size_t strsz;
177 val = val?val:"";
178 str = (char*)flex_alloc(strsz = strlen(fmt) + strlen(def) + strlen(val) + 2);
179 if (!str)
180 flexfatal (_("Allocation of buffer for m4 def failed"));
182 snprintf(str, strsz, fmt, def, val);
183 buf_append(buf, &str, 1);
184 return buf;
187 /** Pushes "m4_undefine([[def]])m4_dnl" to end of buffer.
188 * @param buf A buffer as a list of strings.
189 * @param def The m4 symbol to undefine.
190 * @return buf
192 struct Buf *buf_m4_undefine (struct Buf *buf, const char* def)
194 const char * fmt = "m4_undefine( [[%s]])m4_dnl\n";
195 char * str;
196 size_t strsz;
198 str = (char*)flex_alloc(strsz = strlen(fmt) + strlen(def) + 2);
199 if (!str)
200 flexfatal (_("Allocation of buffer for m4 undef failed"));
202 snprintf(str, strsz, fmt, def);
203 buf_append(buf, &str, 1);
204 return buf;
207 /* create buf with 0 elements, each of size elem_size. */
208 void buf_init (buf, elem_size)
209 struct Buf *buf;
210 size_t elem_size;
212 buf->elts = (void *) 0;
213 buf->nelts = 0;
214 buf->elt_size = elem_size;
215 buf->nmax = 0;
218 /* frees memory */
219 void buf_destroy (buf)
220 struct Buf *buf;
222 if (buf && buf->elts)
223 flex_free (buf->elts);
224 buf->elts = (void *) 0;
228 /* appends ptr[] to buf, grow if necessary.
229 * n_elem is number of elements in ptr[], NOT bytes.
230 * returns buf.
231 * We grow by mod(512) boundaries.
234 struct Buf *buf_append (buf, ptr, n_elem)
235 struct Buf *buf;
236 const void *ptr;
237 int n_elem;
239 int n_alloc = 0;
241 if (!ptr || n_elem == 0)
242 return buf;
244 /* May need to alloc more. */
245 if (n_elem + buf->nelts > buf->nmax) {
247 /* exact amount needed... */
248 n_alloc = (n_elem + buf->nelts) * buf->elt_size;
250 /* ...plus some extra */
251 if (((n_alloc * buf->elt_size) % 512) != 0
252 && buf->elt_size < 512)
253 n_alloc +=
254 (512 -
255 ((n_alloc * buf->elt_size) % 512)) /
256 buf->elt_size;
258 if (!buf->elts)
259 buf->elts =
260 allocate_array (n_alloc, buf->elt_size);
261 else
262 buf->elts =
263 reallocate_array (buf->elts, n_alloc,
264 buf->elt_size);
266 buf->nmax = n_alloc;
269 memcpy ((char *) buf->elts + buf->nelts * buf->elt_size, ptr,
270 n_elem * buf->elt_size);
271 buf->nelts += n_elem;
273 return buf;
276 /* vim:set tabstop=8 softtabstop=4 shiftwidth=4: */