Blame


1 af9b97b5 2002-05-22 alex /* Copyright (C) 1989, 2000 Aladdin Enterprises. All rights reserved. */
2 af9b97b5 2002-05-22 alex
3 af9b97b5 2002-05-22 alex /* Convert ANSI C function definitions to K&R ("traditional C") syntax */
4 af9b97b5 2002-05-22 alex
5 af9b97b5 2002-05-22 alex /*
6 af9b97b5 2002-05-22 alex ansi2knr is distributed in the hope that it will be useful, but WITHOUT ANY
7 af9b97b5 2002-05-22 alex WARRANTY. No author or distributor accepts responsibility to anyone for the
8 af9b97b5 2002-05-22 alex consequences of using it or for whether it serves any particular purpose or
9 af9b97b5 2002-05-22 alex works at all, unless he says so in writing. Refer to the GNU General Public
10 af9b97b5 2002-05-22 alex License (the "GPL") for full details.
11 af9b97b5 2002-05-22 alex
12 af9b97b5 2002-05-22 alex Everyone is granted permission to copy, modify and redistribute ansi2knr,
13 af9b97b5 2002-05-22 alex but only under the conditions described in the GPL. A copy of this license
14 af9b97b5 2002-05-22 alex is supposed to have been given to you along with ansi2knr so you can know
15 af9b97b5 2002-05-22 alex your rights and responsibilities. It should be in a file named COPYLEFT,
16 af9b97b5 2002-05-22 alex or, if there is no file named COPYLEFT, a file named COPYING. Among other
17 af9b97b5 2002-05-22 alex things, the copyright notice and this notice must be preserved on all
18 af9b97b5 2002-05-22 alex copies.
19 af9b97b5 2002-05-22 alex
20 af9b97b5 2002-05-22 alex We explicitly state here what we believe is already implied by the GPL: if
21 af9b97b5 2002-05-22 alex the ansi2knr program is distributed as a separate set of sources and a
22 af9b97b5 2002-05-22 alex separate executable file which are aggregated on a storage medium together
23 af9b97b5 2002-05-22 alex with another program, this in itself does not bring the other program under
24 af9b97b5 2002-05-22 alex the GPL, nor does the mere fact that such a program or the procedures for
25 af9b97b5 2002-05-22 alex constructing it invoke the ansi2knr executable bring any other part of the
26 af9b97b5 2002-05-22 alex program under the GPL.
27 af9b97b5 2002-05-22 alex */
28 af9b97b5 2002-05-22 alex
29 af9b97b5 2002-05-22 alex /*
30 af9b97b5 2002-05-22 alex * Usage:
31 af9b97b5 2002-05-22 alex ansi2knr [--filename FILENAME] [INPUT_FILE [OUTPUT_FILE]]
32 af9b97b5 2002-05-22 alex * --filename provides the file name for the #line directive in the output,
33 af9b97b5 2002-05-22 alex * overriding input_file (if present).
34 af9b97b5 2002-05-22 alex * If no input_file is supplied, input is read from stdin.
35 af9b97b5 2002-05-22 alex * If no output_file is supplied, output goes to stdout.
36 af9b97b5 2002-05-22 alex * There are no error messages.
37 af9b97b5 2002-05-22 alex *
38 af9b97b5 2002-05-22 alex * ansi2knr recognizes function definitions by seeing a non-keyword
39 af9b97b5 2002-05-22 alex * identifier at the left margin, followed by a left parenthesis, with a
40 af9b97b5 2002-05-22 alex * right parenthesis as the last character on the line, and with a left
41 af9b97b5 2002-05-22 alex * brace as the first token on the following line (ignoring possible
42 af9b97b5 2002-05-22 alex * intervening comments and/or preprocessor directives), except that a line
43 af9b97b5 2002-05-22 alex * consisting of only
44 af9b97b5 2002-05-22 alex * identifier1(identifier2)
45 af9b97b5 2002-05-22 alex * will not be considered a function definition unless identifier2 is
46 af9b97b5 2002-05-22 alex * the word "void", and a line consisting of
47 af9b97b5 2002-05-22 alex * identifier1(identifier2, <<arbitrary>>)
48 af9b97b5 2002-05-22 alex * will not be considered a function definition.
49 af9b97b5 2002-05-22 alex * ansi2knr will recognize a multi-line header provided that no intervening
50 af9b97b5 2002-05-22 alex * line ends with a left or right brace or a semicolon. These algorithms
51 af9b97b5 2002-05-22 alex * ignore whitespace, comments, and preprocessor directives, except that
52 af9b97b5 2002-05-22 alex * the function name must be the first thing on the line. The following
53 af9b97b5 2002-05-22 alex * constructs will confuse it:
54 af9b97b5 2002-05-22 alex * - Any other construct that starts at the left margin and
55 af9b97b5 2002-05-22 alex * follows the above syntax (such as a macro or function call).
56 af9b97b5 2002-05-22 alex * - Some macros that tinker with the syntax of function headers.
57 af9b97b5 2002-05-22 alex */
58 af9b97b5 2002-05-22 alex
59 af9b97b5 2002-05-22 alex /*
60 af9b97b5 2002-05-22 alex * The original and principal author of ansi2knr is L. Peter Deutsch
61 af9b97b5 2002-05-22 alex * <ghost@aladdin.com>. Other authors are noted in the change history
62 af9b97b5 2002-05-22 alex * that follows (in reverse chronological order):
63 af9b97b5 2002-05-22 alex
64 af9b97b5 2002-05-22 alex lpd 2000-04-12 backs out Eggert's changes because of bugs:
65 af9b97b5 2002-05-22 alex - concatlits didn't declare the type of its bufend argument;
66 af9b97b5 2002-05-22 alex - concatlits didn't't recognize when it was inside a comment;
67 af9b97b5 2002-05-22 alex - scanstring could scan backward past the beginning of the string; when
68 af9b97b5 2002-05-22 alex - the check for \ + newline in scanstring was unnecessary.
69 af9b97b5 2002-05-22 alex
70 af9b97b5 2002-05-22 alex 2000-03-05 Paul Eggert <eggert@twinsun.com>
71 af9b97b5 2002-05-22 alex
72 af9b97b5 2002-05-22 alex Add support for concatenated string literals.
73 af9b97b5 2002-05-22 alex * ansi2knr.c (concatlits): New decl.
74 af9b97b5 2002-05-22 alex (main): Invoke concatlits to concatenate string literals.
75 af9b97b5 2002-05-22 alex (scanstring): Handle backslash-newline correctly. Work with
76 af9b97b5 2002-05-22 alex character constants. Fix bug when scanning backwards through
77 af9b97b5 2002-05-22 alex backslash-quote. Check for unterminated strings.
78 af9b97b5 2002-05-22 alex (convert1): Parse character constants, too.
79 af9b97b5 2002-05-22 alex (appendline, concatlits): New functions.
80 af9b97b5 2002-05-22 alex * ansi2knr.1: Document this.
81 af9b97b5 2002-05-22 alex
82 af9b97b5 2002-05-22 alex lpd 1999-08-17 added code to allow preprocessor directives
83 af9b97b5 2002-05-22 alex wherever comments are allowed
84 af9b97b5 2002-05-22 alex lpd 1999-04-12 added minor fixes from Pavel Roskin
85 af9b97b5 2002-05-22 alex <pavel_roskin@geocities.com> for clean compilation with
86 af9b97b5 2002-05-22 alex gcc -W -Wall
87 af9b97b5 2002-05-22 alex lpd 1999-03-22 added hack to recognize lines consisting of
88 af9b97b5 2002-05-22 alex identifier1(identifier2, xxx) as *not* being procedures
89 af9b97b5 2002-05-22 alex lpd 1999-02-03 made indentation of preprocessor commands consistent
90 af9b97b5 2002-05-22 alex lpd 1999-01-28 fixed two bugs: a '/' in an argument list caused an
91 af9b97b5 2002-05-22 alex endless loop; quoted strings within an argument list
92 af9b97b5 2002-05-22 alex confused the parser
93 af9b97b5 2002-05-22 alex lpd 1999-01-24 added a check for write errors on the output,
94 af9b97b5 2002-05-22 alex suggested by Jim Meyering <meyering@ascend.com>
95 af9b97b5 2002-05-22 alex lpd 1998-11-09 added further hack to recognize identifier(void)
96 af9b97b5 2002-05-22 alex as being a procedure
97 af9b97b5 2002-05-22 alex lpd 1998-10-23 added hack to recognize lines consisting of
98 af9b97b5 2002-05-22 alex identifier1(identifier2) as *not* being procedures
99 af9b97b5 2002-05-22 alex lpd 1997-12-08 made input_file optional; only closes input and/or
100 af9b97b5 2002-05-22 alex output file if not stdin or stdout respectively; prints
101 af9b97b5 2002-05-22 alex usage message on stderr rather than stdout; adds
102 af9b97b5 2002-05-22 alex --filename switch (changes suggested by
103 af9b97b5 2002-05-22 alex <ceder@lysator.liu.se>)
104 af9b97b5 2002-05-22 alex lpd 1996-01-21 added code to cope with not HAVE_CONFIG_H and with
105 af9b97b5 2002-05-22 alex compilers that don't understand void, as suggested by
106 af9b97b5 2002-05-22 alex Tom Lane
107 af9b97b5 2002-05-22 alex lpd 1996-01-15 changed to require that the first non-comment token
108 af9b97b5 2002-05-22 alex on the line following a function header be a left brace,
109 af9b97b5 2002-05-22 alex to reduce sensitivity to macros, as suggested by Tom Lane
110 af9b97b5 2002-05-22 alex <tgl@sss.pgh.pa.us>
111 af9b97b5 2002-05-22 alex lpd 1995-06-22 removed #ifndefs whose sole purpose was to define
112 af9b97b5 2002-05-22 alex undefined preprocessor symbols as 0; changed all #ifdefs
113 af9b97b5 2002-05-22 alex for configuration symbols to #ifs
114 af9b97b5 2002-05-22 alex lpd 1995-04-05 changed copyright notice to make it clear that
115 af9b97b5 2002-05-22 alex including ansi2knr in a program does not bring the entire
116 af9b97b5 2002-05-22 alex program under the GPL
117 af9b97b5 2002-05-22 alex lpd 1994-12-18 added conditionals for systems where ctype macros
118 af9b97b5 2002-05-22 alex don't handle 8-bit characters properly, suggested by
119 af9b97b5 2002-05-22 alex Francois Pinard <pinard@iro.umontreal.ca>;
120 af9b97b5 2002-05-22 alex removed --varargs switch (this is now the default)
121 af9b97b5 2002-05-22 alex lpd 1994-10-10 removed CONFIG_BROKETS conditional
122 af9b97b5 2002-05-22 alex lpd 1994-07-16 added some conditionals to help GNU `configure',
123 af9b97b5 2002-05-22 alex suggested by Francois Pinard <pinard@iro.umontreal.ca>;
124 af9b97b5 2002-05-22 alex properly erase prototype args in function parameters,
125 af9b97b5 2002-05-22 alex contributed by Jim Avera <jima@netcom.com>;
126 af9b97b5 2002-05-22 alex correct error in writeblanks (it shouldn't erase EOLs)
127 af9b97b5 2002-05-22 alex lpd 1989-xx-xx original version
128 af9b97b5 2002-05-22 alex */
129 af9b97b5 2002-05-22 alex
130 af9b97b5 2002-05-22 alex /* Most of the conditionals here are to make ansi2knr work with */
131 af9b97b5 2002-05-22 alex /* or without the GNU configure machinery. */
132 af9b97b5 2002-05-22 alex
133 af9b97b5 2002-05-22 alex #if HAVE_CONFIG_H
134 af9b97b5 2002-05-22 alex # include <config.h>
135 af9b97b5 2002-05-22 alex #endif
136 af9b97b5 2002-05-22 alex
137 af9b97b5 2002-05-22 alex #include <stdio.h>
138 af9b97b5 2002-05-22 alex #include <ctype.h>
139 af9b97b5 2002-05-22 alex
140 af9b97b5 2002-05-22 alex #if HAVE_CONFIG_H
141 af9b97b5 2002-05-22 alex
142 af9b97b5 2002-05-22 alex /*
143 af9b97b5 2002-05-22 alex For properly autoconfiguring ansi2knr, use AC_CONFIG_HEADER(config.h).
144 af9b97b5 2002-05-22 alex This will define HAVE_CONFIG_H and so, activate the following lines.
145 af9b97b5 2002-05-22 alex */
146 af9b97b5 2002-05-22 alex
147 af9b97b5 2002-05-22 alex # if STDC_HEADERS || HAVE_STRING_H
148 af9b97b5 2002-05-22 alex # include <string.h>
149 af9b97b5 2002-05-22 alex # else
150 af9b97b5 2002-05-22 alex # include <strings.h>
151 af9b97b5 2002-05-22 alex # endif
152 af9b97b5 2002-05-22 alex
153 af9b97b5 2002-05-22 alex #else /* not HAVE_CONFIG_H */
154 af9b97b5 2002-05-22 alex
155 af9b97b5 2002-05-22 alex /* Otherwise do it the hard way */
156 af9b97b5 2002-05-22 alex
157 af9b97b5 2002-05-22 alex # ifdef BSD
158 af9b97b5 2002-05-22 alex # include <strings.h>
159 af9b97b5 2002-05-22 alex # else
160 af9b97b5 2002-05-22 alex # ifdef VMS
161 af9b97b5 2002-05-22 alex extern int strlen(), strncmp();
162 af9b97b5 2002-05-22 alex # else
163 af9b97b5 2002-05-22 alex # include <string.h>
164 af9b97b5 2002-05-22 alex # endif
165 af9b97b5 2002-05-22 alex # endif
166 af9b97b5 2002-05-22 alex
167 af9b97b5 2002-05-22 alex #endif /* not HAVE_CONFIG_H */
168 af9b97b5 2002-05-22 alex
169 af9b97b5 2002-05-22 alex #if STDC_HEADERS
170 af9b97b5 2002-05-22 alex # include <stdlib.h>
171 af9b97b5 2002-05-22 alex #else
172 af9b97b5 2002-05-22 alex /*
173 af9b97b5 2002-05-22 alex malloc and free should be declared in stdlib.h,
174 af9b97b5 2002-05-22 alex but if you've got a K&R compiler, they probably aren't.
175 af9b97b5 2002-05-22 alex */
176 af9b97b5 2002-05-22 alex # ifdef MSDOS
177 af9b97b5 2002-05-22 alex # include <malloc.h>
178 af9b97b5 2002-05-22 alex # else
179 af9b97b5 2002-05-22 alex # ifdef VMS
180 af9b97b5 2002-05-22 alex extern char *malloc();
181 af9b97b5 2002-05-22 alex extern void free();
182 af9b97b5 2002-05-22 alex # else
183 af9b97b5 2002-05-22 alex extern char *malloc();
184 af9b97b5 2002-05-22 alex extern int free();
185 af9b97b5 2002-05-22 alex # endif
186 af9b97b5 2002-05-22 alex # endif
187 af9b97b5 2002-05-22 alex
188 af9b97b5 2002-05-22 alex #endif
189 af9b97b5 2002-05-22 alex
190 af9b97b5 2002-05-22 alex /* Define NULL (for *very* old compilers). */
191 af9b97b5 2002-05-22 alex #ifndef NULL
192 af9b97b5 2002-05-22 alex # define NULL (0)
193 af9b97b5 2002-05-22 alex #endif
194 af9b97b5 2002-05-22 alex
195 af9b97b5 2002-05-22 alex /*
196 af9b97b5 2002-05-22 alex * The ctype macros don't always handle 8-bit characters correctly.
197 af9b97b5 2002-05-22 alex * Compensate for this here.
198 af9b97b5 2002-05-22 alex */
199 af9b97b5 2002-05-22 alex #ifdef isascii
200 af9b97b5 2002-05-22 alex # undef HAVE_ISASCII /* just in case */
201 af9b97b5 2002-05-22 alex # define HAVE_ISASCII 1
202 af9b97b5 2002-05-22 alex #else
203 af9b97b5 2002-05-22 alex #endif
204 af9b97b5 2002-05-22 alex #if STDC_HEADERS || !HAVE_ISASCII
205 af9b97b5 2002-05-22 alex # define is_ascii(c) 1
206 af9b97b5 2002-05-22 alex #else
207 af9b97b5 2002-05-22 alex # define is_ascii(c) isascii(c)
208 af9b97b5 2002-05-22 alex #endif
209 af9b97b5 2002-05-22 alex
210 af9b97b5 2002-05-22 alex #define is_space(c) (is_ascii(c) && isspace(c))
211 af9b97b5 2002-05-22 alex #define is_alpha(c) (is_ascii(c) && isalpha(c))
212 af9b97b5 2002-05-22 alex #define is_alnum(c) (is_ascii(c) && isalnum(c))
213 af9b97b5 2002-05-22 alex
214 af9b97b5 2002-05-22 alex /* Scanning macros */
215 af9b97b5 2002-05-22 alex #define isidchar(ch) (is_alnum(ch) || (ch) == '_')
216 af9b97b5 2002-05-22 alex #define isidfirstchar(ch) (is_alpha(ch) || (ch) == '_')
217 af9b97b5 2002-05-22 alex
218 af9b97b5 2002-05-22 alex /* Forward references */
219 af9b97b5 2002-05-22 alex char *ppdirforward();
220 af9b97b5 2002-05-22 alex char *ppdirbackward();
221 af9b97b5 2002-05-22 alex char *skipspace();
222 af9b97b5 2002-05-22 alex char *scanstring();
223 af9b97b5 2002-05-22 alex int writeblanks();
224 af9b97b5 2002-05-22 alex int test1();
225 af9b97b5 2002-05-22 alex int convert1();
226 af9b97b5 2002-05-22 alex
227 af9b97b5 2002-05-22 alex /* The main program */
228 af9b97b5 2002-05-22 alex int
229 af9b97b5 2002-05-22 alex main(argc, argv)
230 af9b97b5 2002-05-22 alex int argc;
231 af9b97b5 2002-05-22 alex char *argv[];
232 af9b97b5 2002-05-22 alex { FILE *in = stdin;
233 af9b97b5 2002-05-22 alex FILE *out = stdout;
234 af9b97b5 2002-05-22 alex char *filename = 0;
235 af9b97b5 2002-05-22 alex char *program_name = argv[0];
236 af9b97b5 2002-05-22 alex char *output_name = 0;
237 af9b97b5 2002-05-22 alex #define bufsize 5000 /* arbitrary size */
238 af9b97b5 2002-05-22 alex char *buf;
239 af9b97b5 2002-05-22 alex char *line;
240 af9b97b5 2002-05-22 alex char *more;
241 af9b97b5 2002-05-22 alex char *usage =
242 af9b97b5 2002-05-22 alex "Usage: ansi2knr [--filename FILENAME] [INPUT_FILE [OUTPUT_FILE]]\n";
243 af9b97b5 2002-05-22 alex /*
244 af9b97b5 2002-05-22 alex * In previous versions, ansi2knr recognized a --varargs switch.
245 af9b97b5 2002-05-22 alex * If this switch was supplied, ansi2knr would attempt to convert
246 af9b97b5 2002-05-22 alex * a ... argument to va_alist and va_dcl; if this switch was not
247 af9b97b5 2002-05-22 alex * supplied, ansi2knr would simply drop any such arguments.
248 af9b97b5 2002-05-22 alex * Now, ansi2knr always does this conversion, and we only
249 af9b97b5 2002-05-22 alex * check for this switch for backward compatibility.
250 af9b97b5 2002-05-22 alex */
251 af9b97b5 2002-05-22 alex int convert_varargs = 1;
252 af9b97b5 2002-05-22 alex int output_error;
253 af9b97b5 2002-05-22 alex
254 af9b97b5 2002-05-22 alex while ( argc > 1 && argv[1][0] == '-' ) {
255 af9b97b5 2002-05-22 alex if ( !strcmp(argv[1], "--varargs") ) {
256 af9b97b5 2002-05-22 alex convert_varargs = 1;
257 af9b97b5 2002-05-22 alex argc--;
258 af9b97b5 2002-05-22 alex argv++;
259 af9b97b5 2002-05-22 alex continue;
260 af9b97b5 2002-05-22 alex }
261 af9b97b5 2002-05-22 alex if ( !strcmp(argv[1], "--filename") && argc > 2 ) {
262 af9b97b5 2002-05-22 alex filename = argv[2];
263 af9b97b5 2002-05-22 alex argc -= 2;
264 af9b97b5 2002-05-22 alex argv += 2;
265 af9b97b5 2002-05-22 alex continue;
266 af9b97b5 2002-05-22 alex }
267 af9b97b5 2002-05-22 alex fprintf(stderr, "%s: Unrecognized switch: %s\n", program_name,
268 af9b97b5 2002-05-22 alex argv[1]);
269 af9b97b5 2002-05-22 alex fprintf(stderr, usage);
270 af9b97b5 2002-05-22 alex exit(1);
271 af9b97b5 2002-05-22 alex }
272 af9b97b5 2002-05-22 alex switch ( argc )
273 af9b97b5 2002-05-22 alex {
274 af9b97b5 2002-05-22 alex default:
275 af9b97b5 2002-05-22 alex fprintf(stderr, usage);
276 af9b97b5 2002-05-22 alex exit(0);
277 af9b97b5 2002-05-22 alex case 3:
278 af9b97b5 2002-05-22 alex output_name = argv[2];
279 af9b97b5 2002-05-22 alex out = fopen(output_name, "w");
280 af9b97b5 2002-05-22 alex if ( out == NULL ) {
281 af9b97b5 2002-05-22 alex fprintf(stderr, "%s: Cannot open output file %s\n",
282 af9b97b5 2002-05-22 alex program_name, output_name);
283 af9b97b5 2002-05-22 alex exit(1);
284 af9b97b5 2002-05-22 alex }
285 af9b97b5 2002-05-22 alex /* falls through */
286 af9b97b5 2002-05-22 alex case 2:
287 af9b97b5 2002-05-22 alex in = fopen(argv[1], "r");
288 af9b97b5 2002-05-22 alex if ( in == NULL ) {
289 af9b97b5 2002-05-22 alex fprintf(stderr, "%s: Cannot open input file %s\n",
290 af9b97b5 2002-05-22 alex program_name, argv[1]);
291 af9b97b5 2002-05-22 alex exit(1);
292 af9b97b5 2002-05-22 alex }
293 af9b97b5 2002-05-22 alex if ( filename == 0 )
294 af9b97b5 2002-05-22 alex filename = argv[1];
295 af9b97b5 2002-05-22 alex /* falls through */
296 af9b97b5 2002-05-22 alex case 1:
297 af9b97b5 2002-05-22 alex break;
298 af9b97b5 2002-05-22 alex }
299 af9b97b5 2002-05-22 alex if ( filename )
300 af9b97b5 2002-05-22 alex fprintf(out, "#line 1 \"%s\"\n", filename);
301 af9b97b5 2002-05-22 alex buf = malloc(bufsize);
302 af9b97b5 2002-05-22 alex if ( buf == NULL )
303 af9b97b5 2002-05-22 alex {
304 af9b97b5 2002-05-22 alex fprintf(stderr, "Unable to allocate read buffer!\n");
305 af9b97b5 2002-05-22 alex exit(1);
306 af9b97b5 2002-05-22 alex }
307 af9b97b5 2002-05-22 alex line = buf;
308 af9b97b5 2002-05-22 alex while ( fgets(line, (unsigned)(buf + bufsize - line), in) != NULL )
309 af9b97b5 2002-05-22 alex {
310 af9b97b5 2002-05-22 alex test: line += strlen(line);
311 af9b97b5 2002-05-22 alex switch ( test1(buf) )
312 af9b97b5 2002-05-22 alex {
313 af9b97b5 2002-05-22 alex case 2: /* a function header */
314 af9b97b5 2002-05-22 alex convert1(buf, out, 1, convert_varargs);
315 af9b97b5 2002-05-22 alex break;
316 af9b97b5 2002-05-22 alex case 1: /* a function */
317 af9b97b5 2002-05-22 alex /* Check for a { at the start of the next line. */
318 af9b97b5 2002-05-22 alex more = ++line;
319 af9b97b5 2002-05-22 alex f: if ( line >= buf + (bufsize - 1) ) /* overflow check */
320 af9b97b5 2002-05-22 alex goto wl;
321 af9b97b5 2002-05-22 alex if ( fgets(line, (unsigned)(buf + bufsize - line), in) == NULL )
322 af9b97b5 2002-05-22 alex goto wl;
323 af9b97b5 2002-05-22 alex switch ( *skipspace(ppdirforward(more), 1) )
324 af9b97b5 2002-05-22 alex {
325 af9b97b5 2002-05-22 alex case '{':
326 af9b97b5 2002-05-22 alex /* Definitely a function header. */
327 af9b97b5 2002-05-22 alex convert1(buf, out, 0, convert_varargs);
328 af9b97b5 2002-05-22 alex fputs(more, out);
329 af9b97b5 2002-05-22 alex break;
330 af9b97b5 2002-05-22 alex case 0:
331 af9b97b5 2002-05-22 alex /* The next line was blank or a comment: */
332 af9b97b5 2002-05-22 alex /* keep scanning for a non-comment. */
333 af9b97b5 2002-05-22 alex line += strlen(line);
334 af9b97b5 2002-05-22 alex goto f;
335 af9b97b5 2002-05-22 alex default:
336 af9b97b5 2002-05-22 alex /* buf isn't a function header, but */
337 af9b97b5 2002-05-22 alex /* more might be. */
338 af9b97b5 2002-05-22 alex fputs(buf, out);
339 af9b97b5 2002-05-22 alex strcpy(buf, more);
340 af9b97b5 2002-05-22 alex line = buf;
341 af9b97b5 2002-05-22 alex goto test;
342 af9b97b5 2002-05-22 alex }
343 af9b97b5 2002-05-22 alex break;
344 af9b97b5 2002-05-22 alex case -1: /* maybe the start of a function */
345 af9b97b5 2002-05-22 alex if ( line != buf + (bufsize - 1) ) /* overflow check */
346 af9b97b5 2002-05-22 alex continue;
347 af9b97b5 2002-05-22 alex /* falls through */
348 af9b97b5 2002-05-22 alex default: /* not a function */
349 af9b97b5 2002-05-22 alex wl: fputs(buf, out);
350 af9b97b5 2002-05-22 alex break;
351 af9b97b5 2002-05-22 alex }
352 af9b97b5 2002-05-22 alex line = buf;
353 af9b97b5 2002-05-22 alex }
354 af9b97b5 2002-05-22 alex if ( line != buf )
355 af9b97b5 2002-05-22 alex fputs(buf, out);
356 af9b97b5 2002-05-22 alex free(buf);
357 af9b97b5 2002-05-22 alex if ( output_name ) {
358 af9b97b5 2002-05-22 alex output_error = ferror(out);
359 af9b97b5 2002-05-22 alex output_error |= fclose(out);
360 af9b97b5 2002-05-22 alex } else { /* out == stdout */
361 af9b97b5 2002-05-22 alex fflush(out);
362 af9b97b5 2002-05-22 alex output_error = ferror(out);
363 af9b97b5 2002-05-22 alex }
364 af9b97b5 2002-05-22 alex if ( output_error ) {
365 af9b97b5 2002-05-22 alex fprintf(stderr, "%s: error writing to %s\n", program_name,
366 af9b97b5 2002-05-22 alex (output_name ? output_name : "stdout"));
367 af9b97b5 2002-05-22 alex exit(1);
368 af9b97b5 2002-05-22 alex }
369 af9b97b5 2002-05-22 alex if ( in != stdin )
370 af9b97b5 2002-05-22 alex fclose(in);
371 af9b97b5 2002-05-22 alex return 0;
372 af9b97b5 2002-05-22 alex }
373 af9b97b5 2002-05-22 alex
374 af9b97b5 2002-05-22 alex /*
375 af9b97b5 2002-05-22 alex * Skip forward or backward over one or more preprocessor directives.
376 af9b97b5 2002-05-22 alex */
377 af9b97b5 2002-05-22 alex char *
378 af9b97b5 2002-05-22 alex ppdirforward(p)
379 af9b97b5 2002-05-22 alex char *p;
380 af9b97b5 2002-05-22 alex {
381 af9b97b5 2002-05-22 alex for (; *p == '#'; ++p) {
382 af9b97b5 2002-05-22 alex for (; *p != '\r' && *p != '\n'; ++p)
383 af9b97b5 2002-05-22 alex if (*p == 0)
384 af9b97b5 2002-05-22 alex return p;
385 af9b97b5 2002-05-22 alex if (*p == '\r' && p[1] == '\n')
386 af9b97b5 2002-05-22 alex ++p;
387 af9b97b5 2002-05-22 alex }
388 af9b97b5 2002-05-22 alex return p;
389 af9b97b5 2002-05-22 alex }
390 af9b97b5 2002-05-22 alex char *
391 af9b97b5 2002-05-22 alex ppdirbackward(p, limit)
392 af9b97b5 2002-05-22 alex char *p;
393 af9b97b5 2002-05-22 alex char *limit;
394 af9b97b5 2002-05-22 alex {
395 af9b97b5 2002-05-22 alex char *np = p;
396 af9b97b5 2002-05-22 alex
397 af9b97b5 2002-05-22 alex for (;; p = --np) {
398 af9b97b5 2002-05-22 alex if (*np == '\n' && np[-1] == '\r')
399 af9b97b5 2002-05-22 alex --np;
400 af9b97b5 2002-05-22 alex for (; np > limit && np[-1] != '\r' && np[-1] != '\n'; --np)
401 af9b97b5 2002-05-22 alex if (np[-1] == 0)
402 af9b97b5 2002-05-22 alex return np;
403 af9b97b5 2002-05-22 alex if (*np != '#')
404 af9b97b5 2002-05-22 alex return p;
405 af9b97b5 2002-05-22 alex }
406 af9b97b5 2002-05-22 alex }
407 af9b97b5 2002-05-22 alex
408 af9b97b5 2002-05-22 alex /*
409 af9b97b5 2002-05-22 alex * Skip over whitespace, comments, and preprocessor directives,
410 af9b97b5 2002-05-22 alex * in either direction.
411 af9b97b5 2002-05-22 alex */
412 af9b97b5 2002-05-22 alex char *
413 af9b97b5 2002-05-22 alex skipspace(p, dir)
414 af9b97b5 2002-05-22 alex char *p;
415 af9b97b5 2002-05-22 alex int dir; /* 1 for forward, -1 for backward */
416 af9b97b5 2002-05-22 alex {
417 af9b97b5 2002-05-22 alex for ( ; ; ) {
418 af9b97b5 2002-05-22 alex while ( is_space(*p) )
419 af9b97b5 2002-05-22 alex p += dir;
420 af9b97b5 2002-05-22 alex if ( !(*p == '/' && p[dir] == '*') )
421 af9b97b5 2002-05-22 alex break;
422 af9b97b5 2002-05-22 alex p += dir; p += dir;
423 af9b97b5 2002-05-22 alex while ( !(*p == '*' && p[dir] == '/') ) {
424 af9b97b5 2002-05-22 alex if ( *p == 0 )
425 af9b97b5 2002-05-22 alex return p; /* multi-line comment?? */
426 af9b97b5 2002-05-22 alex p += dir;
427 af9b97b5 2002-05-22 alex }
428 af9b97b5 2002-05-22 alex p += dir; p += dir;
429 af9b97b5 2002-05-22 alex }
430 af9b97b5 2002-05-22 alex return p;
431 af9b97b5 2002-05-22 alex }
432 af9b97b5 2002-05-22 alex
433 af9b97b5 2002-05-22 alex /* Scan over a quoted string, in either direction. */
434 af9b97b5 2002-05-22 alex char *
435 af9b97b5 2002-05-22 alex scanstring(p, dir)
436 af9b97b5 2002-05-22 alex char *p;
437 af9b97b5 2002-05-22 alex int dir;
438 af9b97b5 2002-05-22 alex {
439 af9b97b5 2002-05-22 alex for (p += dir; ; p += dir)
440 af9b97b5 2002-05-22 alex if (*p == '"' && p[-dir] != '\\')
441 af9b97b5 2002-05-22 alex return p + dir;
442 af9b97b5 2002-05-22 alex }
443 af9b97b5 2002-05-22 alex
444 af9b97b5 2002-05-22 alex /*
445 af9b97b5 2002-05-22 alex * Write blanks over part of a string.
446 af9b97b5 2002-05-22 alex * Don't overwrite end-of-line characters.
447 af9b97b5 2002-05-22 alex */
448 af9b97b5 2002-05-22 alex int
449 af9b97b5 2002-05-22 alex writeblanks(start, end)
450 af9b97b5 2002-05-22 alex char *start;
451 af9b97b5 2002-05-22 alex char *end;
452 af9b97b5 2002-05-22 alex { char *p;
453 af9b97b5 2002-05-22 alex for ( p = start; p < end; p++ )
454 af9b97b5 2002-05-22 alex if ( *p != '\r' && *p != '\n' )
455 af9b97b5 2002-05-22 alex *p = ' ';
456 af9b97b5 2002-05-22 alex return 0;
457 af9b97b5 2002-05-22 alex }
458 af9b97b5 2002-05-22 alex
459 af9b97b5 2002-05-22 alex /*
460 af9b97b5 2002-05-22 alex * Test whether the string in buf is a function definition.
461 af9b97b5 2002-05-22 alex * The string may contain and/or end with a newline.
462 af9b97b5 2002-05-22 alex * Return as follows:
463 af9b97b5 2002-05-22 alex * 0 - definitely not a function definition;
464 af9b97b5 2002-05-22 alex * 1 - definitely a function definition;
465 af9b97b5 2002-05-22 alex * 2 - definitely a function prototype (NOT USED);
466 af9b97b5 2002-05-22 alex * -1 - may be the beginning of a function definition,
467 af9b97b5 2002-05-22 alex * append another line and look again.
468 af9b97b5 2002-05-22 alex * The reason we don't attempt to convert function prototypes is that
469 af9b97b5 2002-05-22 alex * Ghostscript's declaration-generating macros look too much like
470 af9b97b5 2002-05-22 alex * prototypes, and confuse the algorithms.
471 af9b97b5 2002-05-22 alex */
472 af9b97b5 2002-05-22 alex int
473 af9b97b5 2002-05-22 alex test1(buf)
474 af9b97b5 2002-05-22 alex char *buf;
475 af9b97b5 2002-05-22 alex { char *p = buf;
476 af9b97b5 2002-05-22 alex char *bend;
477 af9b97b5 2002-05-22 alex char *endfn;
478 af9b97b5 2002-05-22 alex int contin;
479 af9b97b5 2002-05-22 alex
480 af9b97b5 2002-05-22 alex if ( !isidfirstchar(*p) )
481 af9b97b5 2002-05-22 alex return 0; /* no name at left margin */
482 af9b97b5 2002-05-22 alex bend = skipspace(ppdirbackward(buf + strlen(buf) - 1, buf), -1);
483 af9b97b5 2002-05-22 alex switch ( *bend )
484 af9b97b5 2002-05-22 alex {
485 af9b97b5 2002-05-22 alex case ';': contin = 0 /*2*/; break;
486 af9b97b5 2002-05-22 alex case ')': contin = 1; break;
487 af9b97b5 2002-05-22 alex case '{': return 0; /* not a function */
488 af9b97b5 2002-05-22 alex case '}': return 0; /* not a function */
489 af9b97b5 2002-05-22 alex default: contin = -1;
490 af9b97b5 2002-05-22 alex }
491 af9b97b5 2002-05-22 alex while ( isidchar(*p) )
492 af9b97b5 2002-05-22 alex p++;
493 af9b97b5 2002-05-22 alex endfn = p;
494 af9b97b5 2002-05-22 alex p = skipspace(p, 1);
495 af9b97b5 2002-05-22 alex if ( *p++ != '(' )
496 af9b97b5 2002-05-22 alex return 0; /* not a function */
497 af9b97b5 2002-05-22 alex p = skipspace(p, 1);
498 af9b97b5 2002-05-22 alex if ( *p == ')' )
499 af9b97b5 2002-05-22 alex return 0; /* no parameters */
500 af9b97b5 2002-05-22 alex /* Check that the apparent function name isn't a keyword. */
501 af9b97b5 2002-05-22 alex /* We only need to check for keywords that could be followed */
502 af9b97b5 2002-05-22 alex /* by a left parenthesis (which, unfortunately, is most of them). */
503 af9b97b5 2002-05-22 alex { static char *words[] =
504 af9b97b5 2002-05-22 alex { "asm", "auto", "case", "char", "const", "double",
505 af9b97b5 2002-05-22 alex "extern", "float", "for", "if", "int", "long",
506 af9b97b5 2002-05-22 alex "register", "return", "short", "signed", "sizeof",
507 af9b97b5 2002-05-22 alex "static", "switch", "typedef", "unsigned",
508 af9b97b5 2002-05-22 alex "void", "volatile", "while", 0
509 af9b97b5 2002-05-22 alex };
510 af9b97b5 2002-05-22 alex char **key = words;
511 af9b97b5 2002-05-22 alex char *kp;
512 af9b97b5 2002-05-22 alex unsigned len = endfn - buf;
513 af9b97b5 2002-05-22 alex
514 af9b97b5 2002-05-22 alex while ( (kp = *key) != 0 )
515 af9b97b5 2002-05-22 alex { if ( strlen(kp) == len && !strncmp(kp, buf, len) )
516 af9b97b5 2002-05-22 alex return 0; /* name is a keyword */
517 af9b97b5 2002-05-22 alex key++;
518 af9b97b5 2002-05-22 alex }
519 af9b97b5 2002-05-22 alex }
520 af9b97b5 2002-05-22 alex {
521 af9b97b5 2002-05-22 alex char *id = p;
522 af9b97b5 2002-05-22 alex int len;
523 af9b97b5 2002-05-22 alex /*
524 af9b97b5 2002-05-22 alex * Check for identifier1(identifier2) and not
525 af9b97b5 2002-05-22 alex * identifier1(void), or identifier1(identifier2, xxxx).
526 af9b97b5 2002-05-22 alex */
527 af9b97b5 2002-05-22 alex
528 af9b97b5 2002-05-22 alex while ( isidchar(*p) )
529 af9b97b5 2002-05-22 alex p++;
530 af9b97b5 2002-05-22 alex len = p - id;
531 af9b97b5 2002-05-22 alex p = skipspace(p, 1);
532 af9b97b5 2002-05-22 alex if (*p == ',' ||
533 af9b97b5 2002-05-22 alex (*p == ')' && (len != 4 || strncmp(id, "void", 4)))
534 af9b97b5 2002-05-22 alex )
535 af9b97b5 2002-05-22 alex return 0; /* not a function */
536 af9b97b5 2002-05-22 alex }
537 af9b97b5 2002-05-22 alex /*
538 af9b97b5 2002-05-22 alex * If the last significant character was a ), we need to count
539 af9b97b5 2002-05-22 alex * parentheses, because it might be part of a formal parameter
540 af9b97b5 2002-05-22 alex * that is a procedure.
541 af9b97b5 2002-05-22 alex */
542 af9b97b5 2002-05-22 alex if (contin > 0) {
543 af9b97b5 2002-05-22 alex int level = 0;
544 af9b97b5 2002-05-22 alex
545 af9b97b5 2002-05-22 alex for (p = skipspace(buf, 1); *p; p = skipspace(p + 1, 1))
546 af9b97b5 2002-05-22 alex level += (*p == '(' ? 1 : *p == ')' ? -1 : 0);
547 af9b97b5 2002-05-22 alex if (level > 0)
548 af9b97b5 2002-05-22 alex contin = -1;
549 af9b97b5 2002-05-22 alex }
550 af9b97b5 2002-05-22 alex return contin;
551 af9b97b5 2002-05-22 alex }
552 af9b97b5 2002-05-22 alex
553 af9b97b5 2002-05-22 alex /* Convert a recognized function definition or header to K&R syntax. */
554 af9b97b5 2002-05-22 alex int
555 af9b97b5 2002-05-22 alex convert1(buf, out, header, convert_varargs)
556 af9b97b5 2002-05-22 alex char *buf;
557 af9b97b5 2002-05-22 alex FILE *out;
558 af9b97b5 2002-05-22 alex int header; /* Boolean */
559 af9b97b5 2002-05-22 alex int convert_varargs; /* Boolean */
560 af9b97b5 2002-05-22 alex { char *endfn;
561 af9b97b5 2002-05-22 alex char *p;
562 af9b97b5 2002-05-22 alex /*
563 af9b97b5 2002-05-22 alex * The breaks table contains pointers to the beginning and end
564 af9b97b5 2002-05-22 alex * of each argument.
565 af9b97b5 2002-05-22 alex */
566 af9b97b5 2002-05-22 alex char **breaks;
567 af9b97b5 2002-05-22 alex unsigned num_breaks = 2; /* for testing */
568 af9b97b5 2002-05-22 alex char **btop;
569 af9b97b5 2002-05-22 alex char **bp;
570 af9b97b5 2002-05-22 alex char **ap;
571 af9b97b5 2002-05-22 alex char *vararg = 0;
572 af9b97b5 2002-05-22 alex
573 af9b97b5 2002-05-22 alex /* Pre-ANSI implementations don't agree on whether strchr */
574 af9b97b5 2002-05-22 alex /* is called strchr or index, so we open-code it here. */
575 af9b97b5 2002-05-22 alex for ( endfn = buf; *(endfn++) != '('; )
576 af9b97b5 2002-05-22 alex ;
577 af9b97b5 2002-05-22 alex top: p = endfn;
578 af9b97b5 2002-05-22 alex breaks = (char **)malloc(sizeof(char *) * num_breaks * 2);
579 af9b97b5 2002-05-22 alex if ( breaks == NULL )
580 af9b97b5 2002-05-22 alex { /* Couldn't allocate break table, give up */
581 af9b97b5 2002-05-22 alex fprintf(stderr, "Unable to allocate break table!\n");
582 af9b97b5 2002-05-22 alex fputs(buf, out);
583 af9b97b5 2002-05-22 alex return -1;
584 af9b97b5 2002-05-22 alex }
585 af9b97b5 2002-05-22 alex btop = breaks + num_breaks * 2 - 2;
586 af9b97b5 2002-05-22 alex bp = breaks;
587 af9b97b5 2002-05-22 alex /* Parse the argument list */
588 af9b97b5 2002-05-22 alex do
589 af9b97b5 2002-05-22 alex { int level = 0;
590 af9b97b5 2002-05-22 alex char *lp = NULL;
591 af9b97b5 2002-05-22 alex char *rp = NULL;
592 af9b97b5 2002-05-22 alex char *end = NULL;
593 af9b97b5 2002-05-22 alex
594 af9b97b5 2002-05-22 alex if ( bp >= btop )
595 af9b97b5 2002-05-22 alex { /* Filled up break table. */
596 af9b97b5 2002-05-22 alex /* Allocate a bigger one and start over. */
597 af9b97b5 2002-05-22 alex free((char *)breaks);
598 af9b97b5 2002-05-22 alex num_breaks <<= 1;
599 af9b97b5 2002-05-22 alex goto top;
600 af9b97b5 2002-05-22 alex }
601 af9b97b5 2002-05-22 alex *bp++ = p;
602 af9b97b5 2002-05-22 alex /* Find the end of the argument */
603 af9b97b5 2002-05-22 alex for ( ; end == NULL; p++ )
604 af9b97b5 2002-05-22 alex { switch(*p)
605 af9b97b5 2002-05-22 alex {
606 af9b97b5 2002-05-22 alex case ',':
607 af9b97b5 2002-05-22 alex if ( !level ) end = p;
608 af9b97b5 2002-05-22 alex break;
609 af9b97b5 2002-05-22 alex case '(':
610 af9b97b5 2002-05-22 alex if ( !level ) lp = p;
611 af9b97b5 2002-05-22 alex level++;
612 af9b97b5 2002-05-22 alex break;
613 af9b97b5 2002-05-22 alex case ')':
614 af9b97b5 2002-05-22 alex if ( --level < 0 ) end = p;
615 af9b97b5 2002-05-22 alex else rp = p;
616 af9b97b5 2002-05-22 alex break;
617 af9b97b5 2002-05-22 alex case '/':
618 af9b97b5 2002-05-22 alex if (p[1] == '*')
619 af9b97b5 2002-05-22 alex p = skipspace(p, 1) - 1;
620 af9b97b5 2002-05-22 alex break;
621 af9b97b5 2002-05-22 alex case '"':
622 af9b97b5 2002-05-22 alex p = scanstring(p, 1) - 1;
623 af9b97b5 2002-05-22 alex break;
624 af9b97b5 2002-05-22 alex default:
625 af9b97b5 2002-05-22 alex ;
626 af9b97b5 2002-05-22 alex }
627 af9b97b5 2002-05-22 alex }
628 af9b97b5 2002-05-22 alex /* Erase any embedded prototype parameters. */
629 af9b97b5 2002-05-22 alex if ( lp && rp )
630 af9b97b5 2002-05-22 alex writeblanks(lp + 1, rp);
631 af9b97b5 2002-05-22 alex p--; /* back up over terminator */
632 af9b97b5 2002-05-22 alex /* Find the name being declared. */
633 af9b97b5 2002-05-22 alex /* This is complicated because of procedure and */
634 af9b97b5 2002-05-22 alex /* array modifiers. */
635 af9b97b5 2002-05-22 alex for ( ; ; )
636 af9b97b5 2002-05-22 alex { p = skipspace(p - 1, -1);
637 af9b97b5 2002-05-22 alex switch ( *p )
638 af9b97b5 2002-05-22 alex {
639 af9b97b5 2002-05-22 alex case ']': /* skip array dimension(s) */
640 af9b97b5 2002-05-22 alex case ')': /* skip procedure args OR name */
641 af9b97b5 2002-05-22 alex { int level = 1;
642 af9b97b5 2002-05-22 alex while ( level )
643 af9b97b5 2002-05-22 alex switch ( *--p )
644 af9b97b5 2002-05-22 alex {
645 af9b97b5 2002-05-22 alex case ']': case ')':
646 af9b97b5 2002-05-22 alex level++;
647 af9b97b5 2002-05-22 alex break;
648 af9b97b5 2002-05-22 alex case '[': case '(':
649 af9b97b5 2002-05-22 alex level--;
650 af9b97b5 2002-05-22 alex break;
651 af9b97b5 2002-05-22 alex case '/':
652 af9b97b5 2002-05-22 alex if (p > buf && p[-1] == '*')
653 af9b97b5 2002-05-22 alex p = skipspace(p, -1) + 1;
654 af9b97b5 2002-05-22 alex break;
655 af9b97b5 2002-05-22 alex case '"':
656 af9b97b5 2002-05-22 alex p = scanstring(p, -1) + 1;
657 af9b97b5 2002-05-22 alex break;
658 af9b97b5 2002-05-22 alex default: ;
659 af9b97b5 2002-05-22 alex }
660 af9b97b5 2002-05-22 alex }
661 af9b97b5 2002-05-22 alex if ( *p == '(' && *skipspace(p + 1, 1) == '*' )
662 af9b97b5 2002-05-22 alex { /* We found the name being declared */
663 af9b97b5 2002-05-22 alex while ( !isidfirstchar(*p) )
664 af9b97b5 2002-05-22 alex p = skipspace(p, 1) + 1;
665 af9b97b5 2002-05-22 alex goto found;
666 af9b97b5 2002-05-22 alex }
667 af9b97b5 2002-05-22 alex break;
668 af9b97b5 2002-05-22 alex default:
669 af9b97b5 2002-05-22 alex goto found;
670 af9b97b5 2002-05-22 alex }
671 af9b97b5 2002-05-22 alex }
672 af9b97b5 2002-05-22 alex found: if ( *p == '.' && p[-1] == '.' && p[-2] == '.' )
673 af9b97b5 2002-05-22 alex { if ( convert_varargs )
674 af9b97b5 2002-05-22 alex { *bp++ = "va_alist";
675 af9b97b5 2002-05-22 alex vararg = p-2;
676 af9b97b5 2002-05-22 alex }
677 af9b97b5 2002-05-22 alex else
678 af9b97b5 2002-05-22 alex { p++;
679 af9b97b5 2002-05-22 alex if ( bp == breaks + 1 ) /* sole argument */
680 af9b97b5 2002-05-22 alex writeblanks(breaks[0], p);
681 af9b97b5 2002-05-22 alex else
682 af9b97b5 2002-05-22 alex writeblanks(bp[-1] - 1, p);
683 af9b97b5 2002-05-22 alex bp--;
684 af9b97b5 2002-05-22 alex }
685 af9b97b5 2002-05-22 alex }
686 af9b97b5 2002-05-22 alex else
687 af9b97b5 2002-05-22 alex { while ( isidchar(*p) ) p--;
688 af9b97b5 2002-05-22 alex *bp++ = p+1;
689 af9b97b5 2002-05-22 alex }
690 af9b97b5 2002-05-22 alex p = end;
691 af9b97b5 2002-05-22 alex }
692 af9b97b5 2002-05-22 alex while ( *p++ == ',' );
693 af9b97b5 2002-05-22 alex *bp = p;
694 af9b97b5 2002-05-22 alex /* Make a special check for 'void' arglist */
695 af9b97b5 2002-05-22 alex if ( bp == breaks+2 )
696 af9b97b5 2002-05-22 alex { p = skipspace(breaks[0], 1);
697 af9b97b5 2002-05-22 alex if ( !strncmp(p, "void", 4) )
698 af9b97b5 2002-05-22 alex { p = skipspace(p+4, 1);
699 af9b97b5 2002-05-22 alex if ( p == breaks[2] - 1 )
700 af9b97b5 2002-05-22 alex { bp = breaks; /* yup, pretend arglist is empty */
701 af9b97b5 2002-05-22 alex writeblanks(breaks[0], p + 1);
702 af9b97b5 2002-05-22 alex }
703 af9b97b5 2002-05-22 alex }
704 af9b97b5 2002-05-22 alex }
705 af9b97b5 2002-05-22 alex /* Put out the function name and left parenthesis. */
706 af9b97b5 2002-05-22 alex p = buf;
707 af9b97b5 2002-05-22 alex while ( p != endfn ) putc(*p, out), p++;
708 af9b97b5 2002-05-22 alex /* Put out the declaration. */
709 af9b97b5 2002-05-22 alex if ( header )
710 af9b97b5 2002-05-22 alex { fputs(");", out);
711 af9b97b5 2002-05-22 alex for ( p = breaks[0]; *p; p++ )
712 af9b97b5 2002-05-22 alex if ( *p == '\r' || *p == '\n' )
713 af9b97b5 2002-05-22 alex putc(*p, out);
714 af9b97b5 2002-05-22 alex }
715 af9b97b5 2002-05-22 alex else
716 af9b97b5 2002-05-22 alex { for ( ap = breaks+1; ap < bp; ap += 2 )
717 af9b97b5 2002-05-22 alex { p = *ap;
718 af9b97b5 2002-05-22 alex while ( isidchar(*p) )
719 af9b97b5 2002-05-22 alex putc(*p, out), p++;
720 af9b97b5 2002-05-22 alex if ( ap < bp - 1 )
721 af9b97b5 2002-05-22 alex fputs(", ", out);
722 af9b97b5 2002-05-22 alex }
723 af9b97b5 2002-05-22 alex fputs(") ", out);
724 af9b97b5 2002-05-22 alex /* Put out the argument declarations */
725 af9b97b5 2002-05-22 alex for ( ap = breaks+2; ap <= bp; ap += 2 )
726 af9b97b5 2002-05-22 alex (*ap)[-1] = ';';
727 af9b97b5 2002-05-22 alex if ( vararg != 0 )
728 af9b97b5 2002-05-22 alex { *vararg = 0;
729 af9b97b5 2002-05-22 alex fputs(breaks[0], out); /* any prior args */
730 af9b97b5 2002-05-22 alex fputs("va_dcl", out); /* the final arg */
731 af9b97b5 2002-05-22 alex fputs(bp[0], out);
732 af9b97b5 2002-05-22 alex }
733 af9b97b5 2002-05-22 alex else
734 af9b97b5 2002-05-22 alex fputs(breaks[0], out);
735 af9b97b5 2002-05-22 alex }
736 af9b97b5 2002-05-22 alex free((char *)breaks);
737 af9b97b5 2002-05-22 alex return 0;
738 af9b97b5 2002-05-22 alex }