* gx sim prototype tweaks
[binutils-gdb.git] / sim / ppc / lf.c
blobba0cc1d8fbc578c12e16ce7957515c06f68186f0
1 /* This file is part of the program psim.
3 Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <ctype.h>
26 #include "misc.h"
27 #include "lf.h"
29 #ifdef HAVE_STDLIB_H
30 #include <stdlib.h>
31 #endif
33 struct _lf {
34 FILE *stream;
35 int line_nr; /* nr complete lines written, curr line is line_nr+1 */
36 int indent;
37 int line_blank;
38 char *file_name;
39 int number_lines;
43 lf *
44 lf_open(char *name,
45 char *real_name,
46 int number_lines)
48 /* create a file object */
49 lf *new_lf = ZALLOC(lf);
50 ASSERT(new_lf != NULL);
51 new_lf->number_lines = number_lines;
52 new_lf->file_name = (real_name == NULL
53 ? name
54 : real_name);
56 /* attach to stdout if pipe */
57 if (!strcmp(name, "-")) {
58 new_lf->stream = stdout;
60 else {
61 /* create a new file */
62 new_lf->stream = fopen(name, "w");
63 ASSERT(new_lf->stream != NULL);
65 return new_lf;
69 void
70 lf_close(lf *file)
72 if (file->stream != stdout) {
73 if (fclose(file->stream)) {
74 perror("lf_close.fclose");
75 exit(1);
77 free(file);
82 void
83 lf_putchr(lf *file,
84 const char chr)
86 if (chr == '\n') {
87 file->line_nr += 1;
88 file->line_blank = 1;
90 else if (file->line_blank) {
91 int pad;
92 for (pad = file->indent; pad > 0; pad--)
93 putc(' ', file->stream);
94 file->line_blank = 0;
96 putc(chr, file->stream);
99 void
100 lf_indent_suppress(lf *file)
102 file->line_blank = 0;
106 void
107 lf_putstr(lf *file,
108 const char *string)
110 const char *chp;
111 if (string != NULL) {
112 for (chp = string; *chp != '\0'; chp++) {
113 lf_putchr(file, *chp);
118 static void
119 do_lf_putunsigned(lf *file,
120 unsigned u)
122 if (u > 0) {
123 do_lf_putunsigned(file, u / 10);
124 lf_putchr(file, (u % 10) + '0');
129 void
130 lf_putint(lf *file,
131 int decimal)
133 if (decimal == 0)
134 lf_putchr(file, '0');
135 else if (decimal < 0) {
136 lf_putchr(file, '-');
137 do_lf_putunsigned(file, -decimal);
139 else if (decimal > 0) {
140 do_lf_putunsigned(file, decimal);
142 else
143 ASSERT(0);
147 void
148 lf_printf(lf *file,
149 const char *fmt,
150 ...)
152 char buf[1024];
153 va_list ap;
155 va_start(ap, fmt);
156 vsprintf(buf, fmt, ap);
157 /* FIXME - this is really stuffed but so is vsprintf() on a sun! */
158 ASSERT(strlen(buf) > 0 && strlen(buf) < sizeof(buf));
159 lf_putstr(file, buf);
160 va_end(ap);
164 void
165 lf_print_c_code(lf *file, char *code)
167 char *chp = code;
168 int in_bit_field = 0;
169 while (*chp != '\0') {
170 if (*chp == '\t')
171 chp++;
172 if (*chp == '#')
173 lf_indent_suppress(file);
174 while (*chp != '\0' && *chp != '\n') {
175 if (chp[0] == '{' && !isspace(chp[1])) {
176 in_bit_field = 1;
177 lf_putchr(file, '_');
179 else if (in_bit_field && chp[0] == ':') {
180 lf_putchr(file, '_');
182 else if (in_bit_field && *chp == '}') {
183 lf_putchr(file, '_');
184 in_bit_field = 0;
186 else {
187 lf_putchr(file, *chp);
189 chp++;
191 if (in_bit_field)
192 error("bit field paren miss match some where\n");
193 if (*chp == '\n') {
194 lf_putchr(file, '\n');
195 chp++;
198 lf_putchr(file, '\n');
202 void
203 lf_print_c_line_nr(lf *file,
204 int line_nr,
205 char *file_name)
207 if (file->number_lines) {
208 lf_indent_suppress(file);
209 lf_putstr(file, "#line ");
210 lf_putint(file, line_nr);
211 lf_putstr(file, " \"");
212 lf_putstr(file, file_name);
213 lf_putstr(file, "\"\n");
217 void
218 lf_print_lf_c_line_nr(lf *file)
220 lf_print_c_line_nr(file, file->line_nr+2, file->file_name);
221 /* line_nr == last_line, want to number from next */
224 void
225 lf_indent(lf *file, int delta)
227 file->indent += delta;
231 void
232 lf_print_copyleft(lf *file)
234 lf_putstr(file, "\
235 /* This file is part of the program psim.
237 Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>
239 This program is free software; you can redistribute it and/or modify
240 it under the terms of the GNU General Public License as published by
241 the Free Software Foundation; either version 2 of the License, or
242 (at your option) any later version.
244 This program is distributed in the hope that it will be useful,
245 but WITHOUT ANY WARRANTY; without even the implied warranty of
246 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
247 GNU General Public License for more details.
249 You should have received a copy of the GNU General Public License
250 along with this program; if not, write to the Free Software
251 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
255 This file was generated by the program gen */
260 void
261 lf_print_binary(lf *file, int decimal, int width)
263 int bit;
264 ASSERT(width > 0);
266 for (bit = 1 << (width-1); bit != 0; bit >>= 1) {
267 if (decimal & bit)
268 lf_putchr(file, '1');
269 else
270 lf_putchr(file, '0');