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