| 1 | /* This file is part of the program psim. |
| 2 | |
| 3 | Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au> |
| 4 | |
| 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. |
| 9 | |
| 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. |
| 14 | |
| 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. |
| 18 | |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdarg.h> |
| 24 | #include <ctype.h> |
| 25 | |
| 26 | #include "misc.h" |
| 27 | #include "lf.h" |
| 28 | |
| 29 | #ifdef HAVE_STDLIB_H |
| 30 | #include <stdlib.h> |
| 31 | #endif |
| 32 | |
| 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; |
| 40 | }; |
| 41 | |
| 42 | |
| 43 | lf * |
| 44 | lf_open(char *name, |
| 45 | char *real_name, |
| 46 | int number_lines) |
| 47 | { |
| 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); |
| 55 | |
| 56 | /* attach to stdout if pipe */ |
| 57 | if (!strcmp(name, "-")) { |
| 58 | new_lf->stream = stdout; |
| 59 | } |
| 60 | else { |
| 61 | /* create a new file */ |
| 62 | new_lf->stream = fopen(name, "w"); |
| 63 | ASSERT(new_lf->stream != NULL); |
| 64 | } |
| 65 | return new_lf; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | void |
| 70 | lf_close(lf *file) |
| 71 | { |
| 72 | if (file->stream != stdout) { |
| 73 | if (fclose(file->stream)) { |
| 74 | perror("lf_close.fclose"); |
| 75 | exit(1); |
| 76 | } |
| 77 | free(file); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | |
| 82 | void |
| 83 | lf_putchr(lf *file, |
| 84 | const char chr) |
| 85 | { |
| 86 | if (chr == '\n') { |
| 87 | file->line_nr += 1; |
| 88 | file->line_blank = 1; |
| 89 | } |
| 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; |
| 95 | } |
| 96 | putc(chr, file->stream); |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | lf_indent_suppress(lf *file) |
| 101 | { |
| 102 | file->line_blank = 0; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | void |
| 107 | lf_putstr(lf *file, |
| 108 | const char *string) |
| 109 | { |
| 110 | const char *chp; |
| 111 | if (string != NULL) { |
| 112 | for (chp = string; *chp != '\0'; chp++) { |
| 113 | lf_putchr(file, *chp); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | static void |
| 119 | do_lf_putunsigned(lf *file, |
| 120 | unsigned u) |
| 121 | { |
| 122 | if (u > 0) { |
| 123 | do_lf_putunsigned(file, u / 10); |
| 124 | lf_putchr(file, (u % 10) + '0'); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
| 129 | void |
| 130 | lf_putint(lf *file, |
| 131 | int decimal) |
| 132 | { |
| 133 | if (decimal == 0) |
| 134 | lf_putchr(file, '0'); |
| 135 | else if (decimal < 0) { |
| 136 | lf_putchr(file, '-'); |
| 137 | do_lf_putunsigned(file, -decimal); |
| 138 | } |
| 139 | else if (decimal > 0) { |
| 140 | do_lf_putunsigned(file, decimal); |
| 141 | } |
| 142 | else |
| 143 | ASSERT(0); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | void |
| 148 | lf_printf(lf *file, |
| 149 | const char *fmt, |
| 150 | ...) |
| 151 | { |
| 152 | char buf[1024]; |
| 153 | va_list ap; |
| 154 | |
| 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); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | void |
| 165 | lf_print_c_code(lf *file, char *code) |
| 166 | { |
| 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, '_'); |
| 178 | } |
| 179 | else if (in_bit_field && chp[0] == ':') { |
| 180 | lf_putchr(file, '_'); |
| 181 | } |
| 182 | else if (in_bit_field && *chp == '}') { |
| 183 | lf_putchr(file, '_'); |
| 184 | in_bit_field = 0; |
| 185 | } |
| 186 | else { |
| 187 | lf_putchr(file, *chp); |
| 188 | } |
| 189 | chp++; |
| 190 | } |
| 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++; |
| 196 | } |
| 197 | } |
| 198 | lf_putchr(file, '\n'); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | void |
| 203 | lf_print_c_line_nr(lf *file, |
| 204 | int line_nr, |
| 205 | char *file_name) |
| 206 | { |
| 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"); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | void |
| 218 | lf_print_lf_c_line_nr(lf *file) |
| 219 | { |
| 220 | lf_print_c_line_nr(file, file->line_nr+2, file->file_name); |
| 221 | /* line_nr == last_line, want to number from next */ |
| 222 | } |
| 223 | |
| 224 | void |
| 225 | lf_indent(lf *file, int delta) |
| 226 | { |
| 227 | file->indent += delta; |
| 228 | } |
| 229 | |
| 230 | |
| 231 | void |
| 232 | lf_print_copyleft(lf *file) |
| 233 | { |
| 234 | lf_putstr(file, "\ |
| 235 | /* This file is part of the program psim. |
| 236 | |
| 237 | Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au> |
| 238 | |
| 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. |
| 243 | |
| 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. |
| 248 | |
| 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. |
| 252 | |
| 253 | -- |
| 254 | |
| 255 | This file was generated by the program gen */ |
| 256 | "); |
| 257 | } |
| 258 | |
| 259 | |
| 260 | void |
| 261 | lf_print_binary(lf *file, int decimal, int width) |
| 262 | { |
| 263 | int bit; |
| 264 | ASSERT(width > 0); |
| 265 | |
| 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'); |
| 271 | } |
| 272 | |
| 273 | } |