1 |
|
|
/* $OpenBSD: buf.c,v 1.7 2015/11/20 18:54:49 tedu 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 |
|
|
|
36 |
|
|
|
37 |
|
|
#include "flexdef.h" |
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 |
|
|
|
58 |
|
|
struct Buf * |
59 |
|
|
buf_print_strings(struct Buf * buf, FILE * out) |
60 |
|
|
{ |
61 |
|
|
int i; |
62 |
|
|
|
63 |
✗✓ |
20 |
if (!buf || !out) |
64 |
|
|
return buf; |
65 |
|
|
|
66 |
✓✓ |
140 |
for (i = 0; i < buf->nelts; i++) { |
67 |
|
60 |
const char *s = ((char **) buf->elts)[i]; |
68 |
✓✗ |
60 |
if (s) |
69 |
|
60 |
fprintf(out, "%s", s); |
70 |
|
|
} |
71 |
|
10 |
return buf; |
72 |
|
10 |
} |
73 |
|
|
|
74 |
|
|
/* Append a "%s" formatted string to a string buffer */ |
75 |
|
|
struct Buf * |
76 |
|
|
buf_prints(struct Buf * buf, const char *fmt, const char *s) |
77 |
|
|
{ |
78 |
|
|
char *t; |
79 |
|
|
size_t tsz; |
80 |
|
|
|
81 |
|
126 |
tsz = strlen(fmt) + strlen(s) + 1; |
82 |
|
63 |
t = malloc(tsz); |
83 |
✗✓ |
63 |
if (!t) |
84 |
|
|
flexfatal(_("Allocation of buffer to print string failed")); |
85 |
|
63 |
snprintf(t, tsz, fmt, s); |
86 |
|
63 |
buf = buf_strappend(buf, t); |
87 |
|
63 |
free(t); |
88 |
|
63 |
return buf; |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
/** Append a line directive to the string buffer. |
92 |
|
|
* @param buf A string buffer. |
93 |
|
|
* @param filename file name |
94 |
|
|
* @param lineno line number |
95 |
|
|
* @return buf |
96 |
|
|
*/ |
97 |
|
|
struct Buf * |
98 |
|
|
buf_linedir(struct Buf * buf, const char *filename, int lineno) |
99 |
|
|
{ |
100 |
|
|
const char *src; |
101 |
|
|
char *dst, *t; |
102 |
|
|
size_t tsz; |
103 |
|
|
|
104 |
|
|
tsz = strlen("#line \"\"\n") + /* constant parts */ |
105 |
|
|
2 * strlen(filename) + /* filename with possibly all backslashes escaped */ |
106 |
|
|
(int) (1 + 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 |
|
|
dst = t + snprintf(t, tsz, "#line %d \"", lineno); |
112 |
|
|
for (src = filename; *src; *dst++ = *src++) |
113 |
|
|
if (*src == '\\') /* escape backslashes */ |
114 |
|
|
*dst++ = '\\'; |
115 |
|
|
*dst++ = '"'; |
116 |
|
|
*dst++ = '\n'; |
117 |
|
|
*dst = '\0'; |
118 |
|
|
buf = buf_strappend(buf, t); |
119 |
|
|
free(t); |
120 |
|
|
return buf; |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
|
124 |
|
|
/** Append the contents of @a src to @a dest. |
125 |
|
|
* @param @a dest the destination buffer |
126 |
|
|
* @param @a dest the source buffer |
127 |
|
|
* @return @a dest |
128 |
|
|
*/ |
129 |
|
|
struct Buf * |
130 |
|
|
buf_concat(struct Buf * dest, const struct Buf * src) |
131 |
|
|
{ |
132 |
|
|
buf_append(dest, src->elts, src->nelts); |
133 |
|
|
return dest; |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
|
137 |
|
|
/* Appends n characters in str to buf. */ |
138 |
|
|
struct Buf * |
139 |
|
|
buf_strnappend(struct Buf *buf, const char *str, int n) |
140 |
|
|
{ |
141 |
|
310 |
buf_append(buf, str, n + 1); |
142 |
|
|
|
143 |
|
|
/* "undo" the '\0' character that buf_append() already copied. */ |
144 |
|
155 |
buf->nelts--; |
145 |
|
|
|
146 |
|
155 |
return buf; |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
/* Appends characters in str to buf. */ |
150 |
|
|
struct Buf * |
151 |
|
|
buf_strappend(struct Buf *buf, const char *str) |
152 |
|
|
{ |
153 |
|
310 |
return buf_strnappend(buf, str, strlen(str)); |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
/* appends "#define str def\n" */ |
157 |
|
|
struct Buf * |
158 |
|
|
buf_strdefine(struct Buf *buf, const char *str, const char *def) |
159 |
|
|
{ |
160 |
|
20 |
buf_strappend(buf, "#define "); |
161 |
|
10 |
buf_strappend(buf, " "); |
162 |
|
10 |
buf_strappend(buf, str); |
163 |
|
10 |
buf_strappend(buf, " "); |
164 |
|
10 |
buf_strappend(buf, def); |
165 |
|
10 |
buf_strappend(buf, "\n"); |
166 |
|
10 |
return buf; |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
/** Pushes "m4_define( [[def]], [[val]])m4_dnl" to end of buffer. |
170 |
|
|
* @param buf A buffer as a list of strings. |
171 |
|
|
* @param def The m4 symbol to define. |
172 |
|
|
* @param val The definition; may be NULL. |
173 |
|
|
* @return buf |
174 |
|
|
*/ |
175 |
|
|
struct Buf * |
176 |
|
|
buf_m4_define(struct Buf * buf, const char *def, const char *val) |
177 |
|
|
{ |
178 |
|
|
const char *fmt = "m4_define( [[%s]], [[%s]])m4_dnl\n"; |
179 |
|
112 |
char *str; |
180 |
|
|
size_t strsz; |
181 |
|
|
|
182 |
|
56 |
val = val ? val : ""; |
183 |
|
56 |
strsz = strlen(fmt) + strlen(def) + strlen(val) + 2; |
184 |
|
56 |
str = malloc(strsz); |
185 |
✗✓ |
56 |
if (!str) |
186 |
|
|
flexfatal(_("Allocation of buffer for m4 def failed")); |
187 |
|
|
|
188 |
|
56 |
snprintf(str, strsz, fmt, def, val); |
189 |
|
56 |
buf_append(buf, &str, 1); |
190 |
|
56 |
return buf; |
191 |
|
56 |
} |
192 |
|
|
|
193 |
|
|
/** Pushes "m4_undefine([[def]])m4_dnl" to end of buffer. |
194 |
|
|
* @param buf A buffer as a list of strings. |
195 |
|
|
* @param def The m4 symbol to undefine. |
196 |
|
|
* @return buf |
197 |
|
|
*/ |
198 |
|
|
struct Buf * |
199 |
|
|
buf_m4_undefine(struct Buf * buf, const char *def) |
200 |
|
|
{ |
201 |
|
|
const char *fmt = "m4_undefine( [[%s]])m4_dnl\n"; |
202 |
|
|
char *str; |
203 |
|
|
size_t strsz; |
204 |
|
|
|
205 |
|
|
strsz = strlen(fmt) + strlen(def) + 2; |
206 |
|
|
str = malloc(strsz); |
207 |
|
|
if (!str) |
208 |
|
|
flexfatal(_("Allocation of buffer for m4 undef failed")); |
209 |
|
|
|
210 |
|
|
snprintf(str, strsz, fmt, def); |
211 |
|
|
buf_append(buf, &str, 1); |
212 |
|
|
return buf; |
213 |
|
|
} |
214 |
|
|
|
215 |
|
|
/* create buf with 0 elements, each of size elem_size. */ |
216 |
|
|
void |
217 |
|
|
buf_init(struct Buf *buf, size_t elem_size) |
218 |
|
|
{ |
219 |
|
320 |
buf->elts = NULL; |
220 |
|
160 |
buf->nelts = 0; |
221 |
|
160 |
buf->elt_size = elem_size; |
222 |
|
160 |
buf->nmax = 0; |
223 |
|
160 |
} |
224 |
|
|
|
225 |
|
|
/* frees memory */ |
226 |
|
|
void |
227 |
|
|
buf_destroy(struct Buf *buf) |
228 |
|
|
{ |
229 |
|
20 |
free(buf->elts); |
230 |
|
10 |
buf->elts = NULL; |
231 |
|
10 |
} |
232 |
|
|
|
233 |
|
|
|
234 |
|
|
/* appends ptr[] to buf, grow if necessary. |
235 |
|
|
* n_elem is number of elements in ptr[], NOT bytes. |
236 |
|
|
* returns buf. |
237 |
|
|
* We grow by mod(512) boundaries. |
238 |
|
|
*/ |
239 |
|
|
|
240 |
|
|
struct Buf * |
241 |
|
|
buf_append(struct Buf *buf, const void *ptr, int n_elem) |
242 |
|
|
{ |
243 |
|
|
int n_alloc = 0; |
244 |
|
|
|
245 |
✗✓ |
506 |
if (!ptr || n_elem == 0) |
246 |
|
|
return buf; |
247 |
|
|
|
248 |
|
|
/* May need to alloc more. */ |
249 |
✓✓ |
253 |
if (n_elem + buf->nelts > buf->nmax) { |
250 |
|
|
|
251 |
|
|
/* exact amount needed... */ |
252 |
|
72 |
n_alloc = (n_elem + buf->nelts) * buf->elt_size; |
253 |
|
|
|
254 |
|
|
/* ...plus some extra */ |
255 |
✓✗ |
144 |
if (((n_alloc * buf->elt_size) % 512) != 0 |
256 |
✓✗ |
144 |
&& buf->elt_size < 512) |
257 |
|
72 |
n_alloc += |
258 |
|
72 |
(512 - |
259 |
|
72 |
((n_alloc * buf->elt_size) % 512)) / |
260 |
|
|
buf->elt_size; |
261 |
|
|
|
262 |
✓✗ |
72 |
if (!buf->elts) |
263 |
|
72 |
buf->elts = |
264 |
|
72 |
allocate_array(n_alloc, buf->elt_size); |
265 |
|
|
else |
266 |
|
|
buf->elts = |
267 |
|
|
reallocate_array(buf->elts, n_alloc, |
268 |
|
|
buf->elt_size); |
269 |
|
|
|
270 |
|
72 |
buf->nmax = n_alloc; |
271 |
|
72 |
} |
272 |
|
506 |
memcpy((char *) buf->elts + buf->nelts * buf->elt_size, ptr, |
273 |
|
253 |
n_elem * buf->elt_size); |
274 |
|
253 |
buf->nelts += n_elem; |
275 |
|
|
|
276 |
|
253 |
return buf; |
277 |
|
253 |
} |