demand_copy_C_string NUL check
[binutils-gdb.git] / sim / igen / lf.c
1 /* The IGEN simulator generator for GDB, the GNU Debugger.
2
3 Copyright 2002-2021 Free Software Foundation, Inc.
4
5 Contributed by Andrew Cagney.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22
23
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <ctype.h>
27
28 #include "config.h"
29 #include "misc.h"
30 #include "lf.h"
31
32 #include <stdlib.h>
33 #include <string.h>
34
35 struct _lf
36 {
37 FILE *stream;
38 int line_nr; /* nr complete lines written, curr line is line_nr+1 */
39 int indent;
40 int line_blank;
41 const char *name;
42 const char *program;
43 lf_file_references references;
44 lf_file_type type;
45 };
46
47
48 lf *
49 lf_open (char *name,
50 char *real_name,
51 lf_file_references references,
52 lf_file_type type, const char *program)
53 {
54 /* create a file object */
55 lf *new_lf = ZALLOC (lf);
56 ASSERT (new_lf != NULL);
57 new_lf->references = references;
58 new_lf->type = type;
59 new_lf->name = (real_name == NULL ? name : real_name);
60 new_lf->program = program;
61 /* attach to stdout if pipe */
62 if (!strcmp (name, "-"))
63 {
64 new_lf->stream = stdout;
65 }
66 else
67 {
68 /* create a new file */
69 new_lf->stream = fopen (name, "w");
70 if (new_lf->stream == NULL)
71 {
72 perror (name);
73 exit (1);
74 }
75 }
76 return new_lf;
77 }
78
79
80 lf_file_type
81 lf_get_file_type (const lf *file)
82 {
83 return file->type;
84 }
85
86
87 void
88 lf_close (lf *file)
89 {
90 if (file->stream != stdout)
91 {
92 if (fclose (file->stream))
93 {
94 perror ("lf_close.fclose");
95 exit (1);
96 }
97 free (file);
98 }
99 }
100
101
102 int
103 lf_putchr (lf *file, const char chr)
104 {
105 int nr = 0;
106 if (chr == '\n')
107 {
108 file->line_nr += 1;
109 file->line_blank = 1;
110 }
111 else if (file->line_blank)
112 {
113 int pad;
114 for (pad = file->indent; pad > 0; pad--)
115 putc (' ', file->stream);
116 nr += file->indent;
117 file->line_blank = 0;
118 }
119 putc (chr, file->stream);
120 nr += 1;
121 return nr;
122 }
123
124 int
125 lf_write (lf *file, const char *string, int strlen_string)
126 {
127 int nr = 0;
128 int i;
129 for (i = 0; i < strlen_string; i++)
130 nr += lf_putchr (file, string[i]);
131 return nr;
132 }
133
134
135 void
136 lf_indent_suppress (lf *file)
137 {
138 file->line_blank = 0;
139 }
140
141
142 int
143 lf_putstr (lf *file, const char *string)
144 {
145 int nr = 0;
146 const char *chp;
147 if (string != NULL)
148 {
149 for (chp = string; *chp != '\0'; chp++)
150 {
151 nr += lf_putchr (file, *chp);
152 }
153 }
154 return nr;
155 }
156
157 static int
158 do_lf_putunsigned (lf *file, unsigned u)
159 {
160 int nr = 0;
161 if (u > 0)
162 {
163 nr += do_lf_putunsigned (file, u / 10);
164 nr += lf_putchr (file, (u % 10) + '0');
165 }
166 return nr;
167 }
168
169
170 int
171 lf_putint (lf *file, int decimal)
172 {
173 int nr = 0;
174 if (decimal == 0)
175 nr += lf_putchr (file, '0');
176 else if (decimal < 0)
177 {
178 nr += lf_putchr (file, '-');
179 nr += do_lf_putunsigned (file, -decimal);
180 }
181 else if (decimal > 0)
182 {
183 nr += do_lf_putunsigned (file, decimal);
184 }
185 else
186 ASSERT (0);
187 return nr;
188 }
189
190
191 int
192 lf_printf (lf *file, const char *fmt, ...)
193 {
194 int nr = 0;
195 char buf[1024];
196 va_list ap;
197
198 va_start (ap, fmt);
199 vsprintf (buf, fmt, ap);
200 /* FIXME - this is really stuffed but so is vsprintf() on a sun! */
201 ASSERT (strlen (buf) < sizeof (buf));
202 nr += lf_putstr (file, buf);
203 va_end (ap);
204 return nr;
205 }
206
207
208 int
209 lf_print__line_ref (lf *file, line_ref *line)
210 {
211 return lf_print__external_ref (file, line->line_nr, line->file_name);
212 }
213
214 int
215 lf_print__external_ref (lf *file, int line_nr, const char *file_name)
216 {
217 int nr = 0;
218 switch (file->references)
219 {
220 case lf_include_references:
221 lf_indent_suppress (file);
222 nr += lf_putstr (file, "#line ");
223 nr += lf_putint (file, line_nr);
224 nr += lf_putstr (file, " \"");
225 nr += lf_putstr (file, file_name);
226 nr += lf_putstr (file, "\"\n");
227 break;
228 case lf_omit_references:
229 nr += lf_putstr (file, "/* ");
230 nr += lf_putstr (file, file_name);
231 nr += lf_putstr (file, ":");
232 nr += lf_putint (file, line_nr);
233 nr += lf_putstr (file, "*/\n");
234 break;
235 }
236 return nr;
237 }
238
239 int
240 lf_print__internal_ref (lf *file)
241 {
242 int nr = 0;
243 nr += lf_print__external_ref (file, file->line_nr + 2, file->name);
244 /* line_nr == last_line, want to number from next */
245 return nr;
246 }
247
248 void
249 lf_indent (lf *file, int delta)
250 {
251 file->indent += delta;
252 }
253
254
255 int
256 lf_print__gnu_copyleft (lf *file)
257 {
258 int nr = 0;
259 switch (file->type)
260 {
261 case lf_is_c:
262 case lf_is_h:
263 nr += lf_printf (file, "\
264 /* This file is part of GDB.\n\
265 \n\
266 Copyright 2002, 2007 Free Software Foundation, Inc.\n\
267 \n\
268 This program is free software; you can redistribute it and/or modify\n\
269 it under the terms of the GNU General Public License as published by\n\
270 the Free Software Foundation; either version 3 of the License, or\n\
271 (at your option) any later version.\n\
272 \n\
273 This program is distributed in the hope that it will be useful,\n\
274 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
275 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
276 GNU General Public License for more details.\n\
277 \n\
278 You should have received a copy of the GNU General Public License\n\
279 along with this program. If not, see <http://www.gnu.org/licenses/>.\n\
280 \n\
281 --\n\
282 \n\
283 This file was generated by the program %s */\n\
284 ", filter_filename (file->program));
285 break;
286 default:
287 ASSERT (0);
288 break;
289 }
290 return nr;
291 }
292
293
294 int
295 lf_putbin (lf *file, int decimal, int width)
296 {
297 int nr = 0;
298 int bit;
299 ASSERT (width > 0);
300 for (bit = 1 << (width - 1); bit != 0; bit >>= 1)
301 {
302 if (decimal & bit)
303 nr += lf_putchr (file, '1');
304 else
305 nr += lf_putchr (file, '0');
306 }
307 return nr;
308 }
309
310 int
311 lf_print__this_file_is_empty (lf *file, const char *reason)
312 {
313 int nr = 0;
314 switch (file->type)
315 {
316 case lf_is_c:
317 case lf_is_h:
318 nr += lf_printf (file,
319 "/* This generated file (%s) is intentionally left blank",
320 file->name);
321 if (reason != NULL)
322 nr += lf_printf (file, " - %s", reason);
323 nr += lf_printf (file, " */\n");
324 break;
325 default:
326 ERROR ("Bad switch");
327 }
328 return nr;
329 }
330
331 int
332 lf_print__ucase_filename (lf *file)
333 {
334 int nr = 0;
335 const char *chp = file->name;
336 while (*chp != '\0')
337 {
338 char ch = *chp;
339 if (islower (ch))
340 {
341 nr += lf_putchr (file, toupper (ch));
342 }
343 else if (ch == '.')
344 nr += lf_putchr (file, '_');
345 else
346 nr += lf_putchr (file, ch);
347 chp++;
348 }
349 return nr;
350 }
351
352 int
353 lf_print__file_start (lf *file)
354 {
355 int nr = 0;
356 switch (file->type)
357 {
358 case lf_is_h:
359 case lf_is_c:
360 nr += lf_print__gnu_copyleft (file);
361 nr += lf_printf (file, "\n");
362 nr += lf_printf (file, "#ifndef ");
363 nr += lf_print__ucase_filename (file);
364 nr += lf_printf (file, "\n");
365 nr += lf_printf (file, "#define ");
366 nr += lf_print__ucase_filename (file);
367 nr += lf_printf (file, "\n");
368 nr += lf_printf (file, "\n");
369 break;
370 default:
371 ASSERT (0);
372 }
373 return nr;
374 }
375
376
377 int
378 lf_print__file_finish (lf *file)
379 {
380 int nr = 0;
381 switch (file->type)
382 {
383 case lf_is_h:
384 case lf_is_c:
385 nr += lf_printf (file, "\n");
386 nr += lf_printf (file, "#endif /* _");
387 nr += lf_print__ucase_filename (file);
388 nr += lf_printf (file, "_*/\n");
389 break;
390 default:
391 ASSERT (0);
392 }
393 return nr;
394 }
395
396
397 int
398 lf_print__function_type (lf *file,
399 const char *type,
400 const char *prefix, const char *trailing_space)
401 {
402 int nr = 0;
403 nr += lf_printf (file, "%s\\\n(%s)", prefix, type);
404 if (trailing_space != NULL)
405 nr += lf_printf (file, "%s", trailing_space);
406 return nr;
407 }
408
409 int
410 lf_print__function_type_function (lf *file,
411 print_function * print_type,
412 const char *prefix,
413 const char *trailing_space)
414 {
415 int nr = 0;
416 nr += lf_printf (file, "%s\\\n(", prefix);
417 nr += print_type (file);
418 nr += lf_printf (file, ")");
419 if (trailing_space != NULL)
420 nr += lf_printf (file, "%s", trailing_space);
421 return nr;
422 }