indent.c revision 1.5 1 1.5 mrg /* $NetBSD: indent.c,v 1.5 1997/10/18 16:04:33 mrg Exp $ */
2 1.4 tls
3 1.1 cgd /*
4 1.5 mrg * Copyright (c) 1980, 1993
5 1.5 mrg * The Regents of the University of California. All rights reserved.
6 1.5 mrg * Copyright (c) 1976 Board of Trustees of the University of Illinois.
7 1.1 cgd * Copyright (c) 1985 Sun Microsystems, Inc.
8 1.1 cgd * All rights reserved.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd */
38 1.1 cgd
39 1.1 cgd #ifndef lint
40 1.1 cgd char copyright[] =
41 1.1 cgd "@(#) Copyright (c) 1985 Sun Microsystems, Inc.\n\
42 1.5 mrg @(#) Copyright (c) 1976 Board of Trustees of the University of Illinois.\n\
43 1.5 mrg @(#) Copyright (c) 1980, 1993\n\
44 1.5 mrg The Regents of the University of California. All rights reserved.\n";
45 1.1 cgd #endif /* not lint */
46 1.1 cgd
47 1.1 cgd #ifndef lint
48 1.5 mrg #if 0
49 1.5 mrg static char sccsid[] = "@(#)indent.c 5.17 (Berkeley) 6/7/93";
50 1.5 mrg #else
51 1.5 mrg static char rcsid[] = "$NetBSD: indent.c,v 1.5 1997/10/18 16:04:33 mrg Exp $";
52 1.5 mrg #endif
53 1.1 cgd #endif /* not lint */
54 1.1 cgd
55 1.1 cgd #include <sys/param.h>
56 1.1 cgd #include <fcntl.h>
57 1.1 cgd #include <unistd.h>
58 1.1 cgd #include <stdio.h>
59 1.1 cgd #include <stdlib.h>
60 1.1 cgd #include <string.h>
61 1.1 cgd #include "indent_globs.h"
62 1.1 cgd #include "indent_codes.h"
63 1.1 cgd #include <ctype.h>
64 1.3 jtc #include <errno.h>
65 1.1 cgd
66 1.1 cgd char *in_name = "Standard Input"; /* will always point to name of input
67 1.1 cgd * file */
68 1.1 cgd char *out_name = "Standard Output"; /* will always point to name
69 1.1 cgd * of output file */
70 1.1 cgd char bakfile[MAXPATHLEN] = "";
71 1.1 cgd
72 1.1 cgd main(argc, argv)
73 1.1 cgd int argc;
74 1.1 cgd char **argv;
75 1.1 cgd {
76 1.1 cgd
77 1.1 cgd extern int found_err; /* flag set in diag() on error */
78 1.1 cgd int dec_ind; /* current indentation for declarations */
79 1.1 cgd int di_stack[20]; /* a stack of structure indentation levels */
80 1.1 cgd int flushed_nl; /* used when buffering up comments to remember
81 1.1 cgd * that a newline was passed over */
82 1.1 cgd int force_nl; /* when true, code must be broken */
83 1.1 cgd int hd_type; /* used to store type of stmt for if (...),
84 1.1 cgd * for (...), etc */
85 1.1 cgd register int i; /* local loop counter */
86 1.1 cgd int scase; /* set to true when we see a case, so we will
87 1.1 cgd * know what to do with the following colon */
88 1.1 cgd int sp_sw; /* when true, we are in the expressin of
89 1.1 cgd * if(...), while(...), etc. */
90 1.1 cgd int squest; /* when this is positive, we have seen a ?
91 1.1 cgd * without the matching : in a <c>?<s>:<s>
92 1.1 cgd * construct */
93 1.1 cgd register char *t_ptr; /* used for copying tokens */
94 1.1 cgd int type_code; /* the type of token, returned by lexi */
95 1.1 cgd
96 1.1 cgd int last_else = 0; /* true iff last keyword was an else */
97 1.1 cgd
98 1.1 cgd
99 1.1 cgd /*-----------------------------------------------*\
100 1.1 cgd | INITIALIZATION |
101 1.1 cgd \*-----------------------------------------------*/
102 1.1 cgd
103 1.1 cgd
104 1.1 cgd ps.p_stack[0] = stmt; /* this is the parser's stack */
105 1.1 cgd ps.last_nl = true; /* this is true if the last thing scanned was
106 1.1 cgd * a newline */
107 1.1 cgd ps.last_token = semicolon;
108 1.1 cgd combuf = (char *) malloc(bufsize);
109 1.1 cgd labbuf = (char *) malloc(bufsize);
110 1.1 cgd codebuf = (char *) malloc(bufsize);
111 1.1 cgd tokenbuf = (char *) malloc(bufsize);
112 1.1 cgd l_com = combuf + bufsize - 5;
113 1.1 cgd l_lab = labbuf + bufsize - 5;
114 1.1 cgd l_code = codebuf + bufsize - 5;
115 1.1 cgd l_token = tokenbuf + bufsize - 5;
116 1.1 cgd combuf[0] = codebuf[0] = labbuf[0] = ' '; /* set up code, label, and
117 1.1 cgd * comment buffers */
118 1.1 cgd combuf[1] = codebuf[1] = labbuf[1] = '\0';
119 1.1 cgd ps.else_if = 1; /* Default else-if special processing to on */
120 1.1 cgd s_lab = e_lab = labbuf + 1;
121 1.1 cgd s_code = e_code = codebuf + 1;
122 1.1 cgd s_com = e_com = combuf + 1;
123 1.1 cgd s_token = e_token = tokenbuf + 1;
124 1.1 cgd
125 1.1 cgd in_buffer = (char *) malloc(10);
126 1.1 cgd in_buffer_limit = in_buffer + 8;
127 1.1 cgd buf_ptr = buf_end = in_buffer;
128 1.1 cgd line_no = 1;
129 1.1 cgd had_eof = ps.in_decl = ps.decl_on_line = break_comma = false;
130 1.1 cgd sp_sw = force_nl = false;
131 1.1 cgd ps.in_or_st = false;
132 1.1 cgd ps.bl_line = true;
133 1.1 cgd dec_ind = 0;
134 1.1 cgd di_stack[ps.dec_nest = 0] = 0;
135 1.1 cgd ps.want_blank = ps.in_stmt = ps.ind_stmt = false;
136 1.1 cgd
137 1.1 cgd
138 1.1 cgd scase = ps.pcase = false;
139 1.1 cgd squest = 0;
140 1.1 cgd sc_end = 0;
141 1.1 cgd bp_save = 0;
142 1.1 cgd be_save = 0;
143 1.1 cgd
144 1.1 cgd output = 0;
145 1.1 cgd
146 1.1 cgd
147 1.1 cgd
148 1.1 cgd /*--------------------------------------------------*\
149 1.1 cgd | COMMAND LINE SCAN |
150 1.1 cgd \*--------------------------------------------------*/
151 1.1 cgd
152 1.1 cgd #ifdef undef
153 1.1 cgd max_col = 78; /* -l78 */
154 1.1 cgd lineup_to_parens = 1; /* -lp */
155 1.1 cgd ps.ljust_decl = 0; /* -ndj */
156 1.1 cgd ps.com_ind = 33; /* -c33 */
157 1.1 cgd star_comment_cont = 1; /* -sc */
158 1.1 cgd ps.ind_size = 8; /* -i8 */
159 1.1 cgd verbose = 0;
160 1.1 cgd ps.decl_indent = 16; /* -di16 */
161 1.1 cgd ps.indent_parameters = 1; /* -ip */
162 1.1 cgd ps.decl_com_ind = 0; /* if this is not set to some positive value
163 1.1 cgd * by an arg, we will set this equal to
164 1.1 cgd * ps.com_ind */
165 1.1 cgd btype_2 = 1; /* -br */
166 1.1 cgd cuddle_else = 1; /* -ce */
167 1.1 cgd ps.unindent_displace = 0; /* -d0 */
168 1.1 cgd ps.case_indent = 0; /* -cli0 */
169 1.1 cgd format_col1_comments = 1; /* -fc1 */
170 1.1 cgd procnames_start_line = 1; /* -psl */
171 1.1 cgd proc_calls_space = 0; /* -npcs */
172 1.1 cgd comment_delimiter_on_blankline = 1; /* -cdb */
173 1.1 cgd ps.leave_comma = 1; /* -nbc */
174 1.1 cgd #endif
175 1.1 cgd
176 1.1 cgd for (i = 1; i < argc; ++i)
177 1.1 cgd if (strcmp(argv[i], "-npro") == 0)
178 1.1 cgd break;
179 1.1 cgd set_defaults();
180 1.1 cgd if (i >= argc)
181 1.1 cgd set_profile();
182 1.1 cgd
183 1.1 cgd for (i = 1; i < argc; ++i) {
184 1.1 cgd
185 1.1 cgd /*
186 1.1 cgd * look thru args (if any) for changes to defaults
187 1.1 cgd */
188 1.1 cgd if (argv[i][0] != '-') {/* no flag on parameter */
189 1.1 cgd if (input == 0) { /* we must have the input file */
190 1.1 cgd in_name = argv[i]; /* remember name of input file */
191 1.1 cgd input = fopen(in_name, "r");
192 1.1 cgd if (input == 0) /* check for open error */
193 1.1 cgd err(in_name);
194 1.1 cgd continue;
195 1.1 cgd }
196 1.1 cgd else if (output == 0) { /* we have the output file */
197 1.1 cgd out_name = argv[i]; /* remember name of output file */
198 1.1 cgd if (strcmp(in_name, out_name) == 0) { /* attempt to overwrite
199 1.1 cgd * the file */
200 1.1 cgd fprintf(stderr, "indent: input and output files must be different\n");
201 1.1 cgd exit(1);
202 1.1 cgd }
203 1.1 cgd output = fopen(out_name, "w");
204 1.1 cgd if (output == 0) /* check for create error */
205 1.1 cgd err(out_name);
206 1.1 cgd continue;
207 1.1 cgd }
208 1.1 cgd fprintf(stderr, "indent: unknown parameter: %s\n", argv[i]);
209 1.1 cgd exit(1);
210 1.1 cgd }
211 1.1 cgd else
212 1.1 cgd set_option(argv[i]);
213 1.1 cgd } /* end of for */
214 1.1 cgd if (input == 0) {
215 1.1 cgd fprintf(stderr, "indent: usage: indent file [ outfile ] [ options ]\n");
216 1.1 cgd exit(1);
217 1.1 cgd }
218 1.1 cgd if (output == 0)
219 1.1 cgd if (troff)
220 1.1 cgd output = stdout;
221 1.1 cgd else {
222 1.1 cgd out_name = in_name;
223 1.1 cgd bakcopy();
224 1.1 cgd }
225 1.1 cgd if (ps.com_ind <= 1)
226 1.1 cgd ps.com_ind = 2; /* dont put normal comments before column 2 */
227 1.1 cgd if (troff) {
228 1.1 cgd if (bodyf.font[0] == 0)
229 1.1 cgd parsefont(&bodyf, "R");
230 1.1 cgd if (scomf.font[0] == 0)
231 1.1 cgd parsefont(&scomf, "I");
232 1.1 cgd if (blkcomf.font[0] == 0)
233 1.1 cgd blkcomf = scomf, blkcomf.size += 2;
234 1.1 cgd if (boxcomf.font[0] == 0)
235 1.1 cgd boxcomf = blkcomf;
236 1.1 cgd if (stringf.font[0] == 0)
237 1.1 cgd parsefont(&stringf, "L");
238 1.1 cgd if (keywordf.font[0] == 0)
239 1.1 cgd parsefont(&keywordf, "B");
240 1.1 cgd writefdef(&bodyf, 'B');
241 1.1 cgd writefdef(&scomf, 'C');
242 1.1 cgd writefdef(&blkcomf, 'L');
243 1.1 cgd writefdef(&boxcomf, 'X');
244 1.1 cgd writefdef(&stringf, 'S');
245 1.1 cgd writefdef(&keywordf, 'K');
246 1.1 cgd }
247 1.1 cgd if (block_comment_max_col <= 0)
248 1.1 cgd block_comment_max_col = max_col;
249 1.1 cgd if (ps.decl_com_ind <= 0) /* if not specified by user, set this */
250 1.1 cgd ps.decl_com_ind = ps.ljust_decl ? (ps.com_ind <= 10 ? 2 : ps.com_ind - 8) : ps.com_ind;
251 1.1 cgd if (continuation_indent == 0)
252 1.1 cgd continuation_indent = ps.ind_size;
253 1.1 cgd fill_buffer(); /* get first batch of stuff into input buffer */
254 1.1 cgd
255 1.1 cgd parse(semicolon);
256 1.1 cgd {
257 1.1 cgd register char *p = buf_ptr;
258 1.1 cgd register col = 1;
259 1.1 cgd
260 1.1 cgd while (1) {
261 1.1 cgd if (*p == ' ')
262 1.1 cgd col++;
263 1.1 cgd else if (*p == '\t')
264 1.1 cgd col = ((col - 1) & ~7) + 9;
265 1.1 cgd else
266 1.1 cgd break;
267 1.1 cgd p++;
268 1.1 cgd }
269 1.1 cgd if (col > ps.ind_size)
270 1.1 cgd ps.ind_level = ps.i_l_follow = col / ps.ind_size;
271 1.1 cgd }
272 1.1 cgd if (troff) {
273 1.1 cgd register char *p = in_name,
274 1.1 cgd *beg = in_name;
275 1.1 cgd
276 1.1 cgd while (*p)
277 1.1 cgd if (*p++ == '/')
278 1.1 cgd beg = p;
279 1.1 cgd fprintf(output, ".Fn \"%s\"\n", beg);
280 1.1 cgd }
281 1.1 cgd /*
282 1.1 cgd * START OF MAIN LOOP
283 1.1 cgd */
284 1.1 cgd
285 1.1 cgd while (1) { /* this is the main loop. it will go until we
286 1.1 cgd * reach eof */
287 1.1 cgd int is_procname;
288 1.1 cgd
289 1.1 cgd type_code = lexi(); /* lexi reads one token. The actual
290 1.1 cgd * characters read are stored in "token". lexi
291 1.1 cgd * returns a code indicating the type of token */
292 1.1 cgd is_procname = ps.procname[0];
293 1.1 cgd
294 1.1 cgd /*
295 1.1 cgd * The following code moves everything following an if (), while (),
296 1.1 cgd * else, etc. up to the start of the following stmt to a buffer. This
297 1.1 cgd * allows proper handling of both kinds of brace placement.
298 1.1 cgd */
299 1.1 cgd
300 1.1 cgd flushed_nl = false;
301 1.1 cgd while (ps.search_brace) { /* if we scanned an if(), while(),
302 1.1 cgd * etc., we might need to copy stuff
303 1.1 cgd * into a buffer we must loop, copying
304 1.1 cgd * stuff into save_com, until we find
305 1.1 cgd * the start of the stmt which follows
306 1.1 cgd * the if, or whatever */
307 1.1 cgd switch (type_code) {
308 1.1 cgd case newline:
309 1.1 cgd ++line_no;
310 1.1 cgd flushed_nl = true;
311 1.1 cgd case form_feed:
312 1.1 cgd break; /* form feeds and newlines found here will be
313 1.1 cgd * ignored */
314 1.1 cgd
315 1.1 cgd case lbrace: /* this is a brace that starts the compound
316 1.1 cgd * stmt */
317 1.1 cgd if (sc_end == 0) { /* ignore buffering if a comment wasnt
318 1.1 cgd * stored up */
319 1.1 cgd ps.search_brace = false;
320 1.1 cgd goto check_type;
321 1.1 cgd }
322 1.1 cgd if (btype_2) {
323 1.1 cgd save_com[0] = '{'; /* we either want to put the brace
324 1.1 cgd * right after the if */
325 1.1 cgd goto sw_buffer; /* go to common code to get out of
326 1.1 cgd * this loop */
327 1.1 cgd }
328 1.1 cgd case comment: /* we have a comment, so we must copy it into
329 1.1 cgd * the buffer */
330 1.1 cgd if (!flushed_nl || sc_end != 0) {
331 1.1 cgd if (sc_end == 0) { /* if this is the first comment, we
332 1.1 cgd * must set up the buffer */
333 1.1 cgd save_com[0] = save_com[1] = ' ';
334 1.1 cgd sc_end = &(save_com[2]);
335 1.1 cgd }
336 1.1 cgd else {
337 1.1 cgd *sc_end++ = '\n'; /* add newline between
338 1.1 cgd * comments */
339 1.1 cgd *sc_end++ = ' ';
340 1.1 cgd --line_no;
341 1.1 cgd }
342 1.1 cgd *sc_end++ = '/'; /* copy in start of comment */
343 1.1 cgd *sc_end++ = '*';
344 1.1 cgd
345 1.1 cgd for (;;) { /* loop until we get to the end of the comment */
346 1.1 cgd *sc_end = *buf_ptr++;
347 1.1 cgd if (buf_ptr >= buf_end)
348 1.1 cgd fill_buffer();
349 1.1 cgd
350 1.1 cgd if (*sc_end++ == '*' && *buf_ptr == '/')
351 1.1 cgd break; /* we are at end of comment */
352 1.1 cgd
353 1.1 cgd if (sc_end >= &(save_com[sc_size])) { /* check for temp buffer
354 1.1 cgd * overflow */
355 1.1 cgd diag(1, "Internal buffer overflow - Move big comment from right after if, while, or whatever.");
356 1.1 cgd fflush(output);
357 1.1 cgd exit(1);
358 1.1 cgd }
359 1.1 cgd }
360 1.1 cgd *sc_end++ = '/'; /* add ending slash */
361 1.1 cgd if (++buf_ptr >= buf_end) /* get past / in buffer */
362 1.1 cgd fill_buffer();
363 1.1 cgd break;
364 1.1 cgd }
365 1.1 cgd default: /* it is the start of a normal statment */
366 1.1 cgd if (flushed_nl) /* if we flushed a newline, make sure it is
367 1.1 cgd * put back */
368 1.1 cgd force_nl = true;
369 1.1 cgd if (type_code == sp_paren && *token == 'i'
370 1.1 cgd && last_else && ps.else_if
371 1.1 cgd || type_code == sp_nparen && *token == 'e'
372 1.1 cgd && e_code != s_code && e_code[-1] == '}')
373 1.1 cgd force_nl = false;
374 1.1 cgd
375 1.1 cgd if (sc_end == 0) { /* ignore buffering if comment wasnt
376 1.1 cgd * saved up */
377 1.1 cgd ps.search_brace = false;
378 1.1 cgd goto check_type;
379 1.1 cgd }
380 1.1 cgd if (force_nl) { /* if we should insert a nl here, put it into
381 1.1 cgd * the buffer */
382 1.1 cgd force_nl = false;
383 1.1 cgd --line_no; /* this will be re-increased when the nl is
384 1.1 cgd * read from the buffer */
385 1.1 cgd *sc_end++ = '\n';
386 1.1 cgd *sc_end++ = ' ';
387 1.1 cgd if (verbose && !flushed_nl) /* print error msg if the line
388 1.1 cgd * was not already broken */
389 1.1 cgd diag(0, "Line broken");
390 1.1 cgd flushed_nl = false;
391 1.1 cgd }
392 1.1 cgd for (t_ptr = token; *t_ptr; ++t_ptr)
393 1.1 cgd *sc_end++ = *t_ptr; /* copy token into temp buffer */
394 1.1 cgd ps.procname[0] = 0;
395 1.1 cgd
396 1.1 cgd sw_buffer:
397 1.1 cgd ps.search_brace = false; /* stop looking for start of
398 1.1 cgd * stmt */
399 1.1 cgd bp_save = buf_ptr; /* save current input buffer */
400 1.1 cgd be_save = buf_end;
401 1.1 cgd buf_ptr = save_com; /* fix so that subsequent calls to
402 1.1 cgd * lexi will take tokens out of
403 1.1 cgd * save_com */
404 1.1 cgd *sc_end++ = ' ';/* add trailing blank, just in case */
405 1.1 cgd buf_end = sc_end;
406 1.1 cgd sc_end = 0;
407 1.1 cgd break;
408 1.1 cgd } /* end of switch */
409 1.1 cgd if (type_code != 0) /* we must make this check, just in case there
410 1.1 cgd * was an unexpected EOF */
411 1.1 cgd type_code = lexi(); /* read another token */
412 1.1 cgd /* if (ps.search_brace) ps.procname[0] = 0; */
413 1.1 cgd if ((is_procname = ps.procname[0]) && flushed_nl
414 1.1 cgd && !procnames_start_line && ps.in_decl
415 1.1 cgd && type_code == ident)
416 1.1 cgd flushed_nl = 0;
417 1.1 cgd } /* end of while (search_brace) */
418 1.1 cgd last_else = 0;
419 1.1 cgd check_type:
420 1.1 cgd if (type_code == 0) { /* we got eof */
421 1.1 cgd if (s_lab != e_lab || s_code != e_code
422 1.1 cgd || s_com != e_com) /* must dump end of line */
423 1.1 cgd dump_line();
424 1.1 cgd if (ps.tos > 1) /* check for balanced braces */
425 1.1 cgd diag(1, "Stuff missing from end of file.");
426 1.1 cgd
427 1.1 cgd if (verbose) {
428 1.1 cgd printf("There were %d output lines and %d comments\n",
429 1.1 cgd ps.out_lines, ps.out_coms);
430 1.1 cgd printf("(Lines with comments)/(Lines with code): %6.3f\n",
431 1.1 cgd (1.0 * ps.com_lines) / code_lines);
432 1.1 cgd }
433 1.1 cgd fflush(output);
434 1.1 cgd exit(found_err);
435 1.1 cgd }
436 1.1 cgd if (
437 1.1 cgd (type_code != comment) &&
438 1.1 cgd (type_code != newline) &&
439 1.1 cgd (type_code != preesc) &&
440 1.1 cgd (type_code != form_feed)) {
441 1.1 cgd if (force_nl &&
442 1.1 cgd (type_code != semicolon) &&
443 1.1 cgd (type_code != lbrace || !btype_2)) {
444 1.1 cgd /* we should force a broken line here */
445 1.1 cgd if (verbose && !flushed_nl)
446 1.1 cgd diag(0, "Line broken");
447 1.1 cgd flushed_nl = false;
448 1.1 cgd dump_line();
449 1.1 cgd ps.want_blank = false; /* dont insert blank at line start */
450 1.1 cgd force_nl = false;
451 1.1 cgd }
452 1.1 cgd ps.in_stmt = true; /* turn on flag which causes an extra level of
453 1.1 cgd * indentation. this is turned off by a ; or
454 1.1 cgd * '}' */
455 1.1 cgd if (s_com != e_com) { /* the turkey has embedded a comment
456 1.1 cgd * in a line. fix it */
457 1.1 cgd *e_code++ = ' ';
458 1.1 cgd for (t_ptr = s_com; *t_ptr; ++t_ptr) {
459 1.1 cgd CHECK_SIZE_CODE;
460 1.1 cgd *e_code++ = *t_ptr;
461 1.1 cgd }
462 1.1 cgd *e_code++ = ' ';
463 1.1 cgd *e_code = '\0'; /* null terminate code sect */
464 1.1 cgd ps.want_blank = false;
465 1.1 cgd e_com = s_com;
466 1.1 cgd }
467 1.1 cgd }
468 1.1 cgd else if (type_code != comment) /* preserve force_nl thru a comment */
469 1.1 cgd force_nl = false; /* cancel forced newline after newline, form
470 1.1 cgd * feed, etc */
471 1.1 cgd
472 1.1 cgd
473 1.1 cgd
474 1.1 cgd /*-----------------------------------------------------*\
475 1.1 cgd | do switch on type of token scanned |
476 1.1 cgd \*-----------------------------------------------------*/
477 1.1 cgd CHECK_SIZE_CODE;
478 1.1 cgd switch (type_code) { /* now, decide what to do with the token */
479 1.1 cgd
480 1.1 cgd case form_feed: /* found a form feed in line */
481 1.1 cgd ps.use_ff = true; /* a form feed is treated much like a newline */
482 1.1 cgd dump_line();
483 1.1 cgd ps.want_blank = false;
484 1.1 cgd break;
485 1.1 cgd
486 1.1 cgd case newline:
487 1.1 cgd if (ps.last_token != comma || ps.p_l_follow > 0
488 1.1 cgd || !ps.leave_comma || ps.block_init || !break_comma || s_com != e_com) {
489 1.1 cgd dump_line();
490 1.1 cgd ps.want_blank = false;
491 1.1 cgd }
492 1.1 cgd ++line_no; /* keep track of input line number */
493 1.1 cgd break;
494 1.1 cgd
495 1.1 cgd case lparen: /* got a '(' or '[' */
496 1.1 cgd ++ps.p_l_follow; /* count parens to make Healy happy */
497 1.1 cgd if (ps.want_blank && *token != '[' &&
498 1.1 cgd (ps.last_token != ident || proc_calls_space
499 1.1 cgd || (ps.its_a_keyword && (!ps.sizeof_keyword || Bill_Shannon))))
500 1.1 cgd *e_code++ = ' ';
501 1.1 cgd if (ps.in_decl && !ps.block_init)
502 1.1 cgd if (troff && !ps.dumped_decl_indent && !is_procname && ps.last_token == decl) {
503 1.1 cgd ps.dumped_decl_indent = 1;
504 1.1 cgd sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);
505 1.1 cgd e_code += strlen(e_code);
506 1.1 cgd }
507 1.1 cgd else {
508 1.1 cgd while ((e_code - s_code) < dec_ind) {
509 1.1 cgd CHECK_SIZE_CODE;
510 1.1 cgd *e_code++ = ' ';
511 1.1 cgd }
512 1.1 cgd *e_code++ = token[0];
513 1.1 cgd }
514 1.1 cgd else
515 1.1 cgd *e_code++ = token[0];
516 1.1 cgd ps.paren_indents[ps.p_l_follow - 1] = e_code - s_code;
517 1.1 cgd if (sp_sw && ps.p_l_follow == 1 && extra_expression_indent
518 1.1 cgd && ps.paren_indents[0] < 2 * ps.ind_size)
519 1.1 cgd ps.paren_indents[0] = 2 * ps.ind_size;
520 1.1 cgd ps.want_blank = false;
521 1.1 cgd if (ps.in_or_st && *token == '(' && ps.tos <= 2) {
522 1.1 cgd /*
523 1.1 cgd * this is a kluge to make sure that declarations will be
524 1.1 cgd * aligned right if proc decl has an explicit type on it, i.e.
525 1.1 cgd * "int a(x) {..."
526 1.1 cgd */
527 1.1 cgd parse(semicolon); /* I said this was a kluge... */
528 1.1 cgd ps.in_or_st = false; /* turn off flag for structure decl or
529 1.1 cgd * initialization */
530 1.1 cgd }
531 1.1 cgd if (ps.sizeof_keyword)
532 1.1 cgd ps.sizeof_mask |= 1 << ps.p_l_follow;
533 1.1 cgd break;
534 1.1 cgd
535 1.1 cgd case rparen: /* got a ')' or ']' */
536 1.1 cgd rparen_count--;
537 1.1 cgd if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.sizeof_mask) {
538 1.1 cgd ps.last_u_d = true;
539 1.1 cgd ps.cast_mask &= (1 << ps.p_l_follow) - 1;
540 1.1 cgd }
541 1.1 cgd ps.sizeof_mask &= (1 << ps.p_l_follow) - 1;
542 1.1 cgd if (--ps.p_l_follow < 0) {
543 1.1 cgd ps.p_l_follow = 0;
544 1.1 cgd diag(0, "Extra %c", *token);
545 1.1 cgd }
546 1.1 cgd if (e_code == s_code) /* if the paren starts the line */
547 1.1 cgd ps.paren_level = ps.p_l_follow; /* then indent it */
548 1.1 cgd
549 1.1 cgd *e_code++ = token[0];
550 1.1 cgd ps.want_blank = true;
551 1.1 cgd
552 1.1 cgd if (sp_sw && (ps.p_l_follow == 0)) { /* check for end of if
553 1.1 cgd * (...), or some such */
554 1.1 cgd sp_sw = false;
555 1.1 cgd force_nl = true;/* must force newline after if */
556 1.1 cgd ps.last_u_d = true; /* inform lexi that a following
557 1.1 cgd * operator is unary */
558 1.1 cgd ps.in_stmt = false; /* dont use stmt continuation
559 1.1 cgd * indentation */
560 1.1 cgd
561 1.1 cgd parse(hd_type); /* let parser worry about if, or whatever */
562 1.1 cgd }
563 1.1 cgd ps.search_brace = btype_2; /* this should insure that constructs
564 1.1 cgd * such as main(){...} and int[]{...}
565 1.1 cgd * have their braces put in the right
566 1.1 cgd * place */
567 1.1 cgd break;
568 1.1 cgd
569 1.1 cgd case unary_op: /* this could be any unary operation */
570 1.1 cgd if (ps.want_blank)
571 1.1 cgd *e_code++ = ' ';
572 1.1 cgd
573 1.1 cgd if (troff && !ps.dumped_decl_indent && ps.in_decl && !is_procname) {
574 1.1 cgd sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);
575 1.1 cgd ps.dumped_decl_indent = 1;
576 1.1 cgd e_code += strlen(e_code);
577 1.1 cgd }
578 1.1 cgd else {
579 1.1 cgd char *res = token;
580 1.1 cgd
581 1.1 cgd if (ps.in_decl && !ps.block_init) { /* if this is a unary op
582 1.1 cgd * in a declaration, we
583 1.1 cgd * should indent this
584 1.1 cgd * token */
585 1.1 cgd for (i = 0; token[i]; ++i); /* find length of token */
586 1.1 cgd while ((e_code - s_code) < (dec_ind - i)) {
587 1.1 cgd CHECK_SIZE_CODE;
588 1.1 cgd *e_code++ = ' '; /* pad it */
589 1.1 cgd }
590 1.1 cgd }
591 1.1 cgd if (troff && token[0] == '-' && token[1] == '>')
592 1.1 cgd res = "\\(->";
593 1.1 cgd for (t_ptr = res; *t_ptr; ++t_ptr) {
594 1.1 cgd CHECK_SIZE_CODE;
595 1.1 cgd *e_code++ = *t_ptr;
596 1.1 cgd }
597 1.1 cgd }
598 1.1 cgd ps.want_blank = false;
599 1.1 cgd break;
600 1.1 cgd
601 1.1 cgd case binary_op: /* any binary operation */
602 1.1 cgd if (ps.want_blank)
603 1.1 cgd *e_code++ = ' ';
604 1.1 cgd {
605 1.1 cgd char *res = token;
606 1.1 cgd
607 1.1 cgd if (troff)
608 1.1 cgd switch (token[0]) {
609 1.1 cgd case '<':
610 1.1 cgd if (token[1] == '=')
611 1.1 cgd res = "\\(<=";
612 1.1 cgd break;
613 1.1 cgd case '>':
614 1.1 cgd if (token[1] == '=')
615 1.1 cgd res = "\\(>=";
616 1.1 cgd break;
617 1.1 cgd case '!':
618 1.1 cgd if (token[1] == '=')
619 1.1 cgd res = "\\(!=";
620 1.1 cgd break;
621 1.1 cgd case '|':
622 1.1 cgd if (token[1] == '|')
623 1.1 cgd res = "\\(br\\(br";
624 1.1 cgd else if (token[1] == 0)
625 1.1 cgd res = "\\(br";
626 1.1 cgd break;
627 1.1 cgd }
628 1.1 cgd for (t_ptr = res; *t_ptr; ++t_ptr) {
629 1.1 cgd CHECK_SIZE_CODE;
630 1.1 cgd *e_code++ = *t_ptr; /* move the operator */
631 1.1 cgd }
632 1.1 cgd }
633 1.1 cgd ps.want_blank = true;
634 1.1 cgd break;
635 1.1 cgd
636 1.1 cgd case postop: /* got a trailing ++ or -- */
637 1.1 cgd *e_code++ = token[0];
638 1.1 cgd *e_code++ = token[1];
639 1.1 cgd ps.want_blank = true;
640 1.1 cgd break;
641 1.1 cgd
642 1.1 cgd case question: /* got a ? */
643 1.1 cgd squest++; /* this will be used when a later colon
644 1.1 cgd * appears so we can distinguish the
645 1.1 cgd * <c>?<n>:<n> construct */
646 1.1 cgd if (ps.want_blank)
647 1.1 cgd *e_code++ = ' ';
648 1.1 cgd *e_code++ = '?';
649 1.1 cgd ps.want_blank = true;
650 1.1 cgd break;
651 1.1 cgd
652 1.1 cgd case casestmt: /* got word 'case' or 'default' */
653 1.1 cgd scase = true; /* so we can process the later colon properly */
654 1.1 cgd goto copy_id;
655 1.1 cgd
656 1.1 cgd case colon: /* got a ':' */
657 1.1 cgd if (squest > 0) { /* it is part of the <c>?<n>: <n> construct */
658 1.1 cgd --squest;
659 1.1 cgd if (ps.want_blank)
660 1.1 cgd *e_code++ = ' ';
661 1.1 cgd *e_code++ = ':';
662 1.1 cgd ps.want_blank = true;
663 1.1 cgd break;
664 1.1 cgd }
665 1.1 cgd if (ps.in_decl) {
666 1.1 cgd *e_code++ = ':';
667 1.1 cgd ps.want_blank = false;
668 1.1 cgd break;
669 1.1 cgd }
670 1.1 cgd ps.in_stmt = false; /* seeing a label does not imply we are in a
671 1.1 cgd * stmt */
672 1.1 cgd for (t_ptr = s_code; *t_ptr; ++t_ptr)
673 1.1 cgd *e_lab++ = *t_ptr; /* turn everything so far into a label */
674 1.1 cgd e_code = s_code;
675 1.1 cgd *e_lab++ = ':';
676 1.1 cgd *e_lab++ = ' ';
677 1.1 cgd *e_lab = '\0';
678 1.1 cgd
679 1.1 cgd force_nl = ps.pcase = scase; /* ps.pcase will be used by
680 1.1 cgd * dump_line to decide how to
681 1.1 cgd * indent the label. force_nl
682 1.1 cgd * will force a case n: to be
683 1.1 cgd * on a line by itself */
684 1.1 cgd scase = false;
685 1.1 cgd ps.want_blank = false;
686 1.1 cgd break;
687 1.1 cgd
688 1.1 cgd case semicolon: /* got a ';' */
689 1.1 cgd ps.in_or_st = false;/* we are not in an initialization or
690 1.1 cgd * structure declaration */
691 1.1 cgd scase = false; /* these will only need resetting in a error */
692 1.1 cgd squest = 0;
693 1.1 cgd if (ps.last_token == rparen && rparen_count == 0)
694 1.1 cgd ps.in_parameter_declaration = 0;
695 1.1 cgd ps.cast_mask = 0;
696 1.1 cgd ps.sizeof_mask = 0;
697 1.1 cgd ps.block_init = 0;
698 1.1 cgd ps.block_init_level = 0;
699 1.1 cgd ps.just_saw_decl--;
700 1.1 cgd
701 1.1 cgd if (ps.in_decl && s_code == e_code && !ps.block_init)
702 1.1 cgd while ((e_code - s_code) < (dec_ind - 1)) {
703 1.1 cgd CHECK_SIZE_CODE;
704 1.1 cgd *e_code++ = ' ';
705 1.1 cgd }
706 1.1 cgd
707 1.1 cgd ps.in_decl = (ps.dec_nest > 0); /* if we were in a first level
708 1.1 cgd * structure declaration, we
709 1.1 cgd * arent any more */
710 1.1 cgd
711 1.1 cgd if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) {
712 1.1 cgd
713 1.1 cgd /*
714 1.1 cgd * This should be true iff there were unbalanced parens in the
715 1.1 cgd * stmt. It is a bit complicated, because the semicolon might
716 1.1 cgd * be in a for stmt
717 1.1 cgd */
718 1.1 cgd diag(1, "Unbalanced parens");
719 1.1 cgd ps.p_l_follow = 0;
720 1.1 cgd if (sp_sw) { /* this is a check for a if, while, etc. with
721 1.1 cgd * unbalanced parens */
722 1.1 cgd sp_sw = false;
723 1.1 cgd parse(hd_type); /* dont lose the if, or whatever */
724 1.1 cgd }
725 1.1 cgd }
726 1.1 cgd *e_code++ = ';';
727 1.1 cgd ps.want_blank = true;
728 1.1 cgd ps.in_stmt = (ps.p_l_follow > 0); /* we are no longer in the
729 1.1 cgd * middle of a stmt */
730 1.1 cgd
731 1.1 cgd if (!sp_sw) { /* if not if for (;;) */
732 1.1 cgd parse(semicolon); /* let parser know about end of stmt */
733 1.1 cgd force_nl = true;/* force newline after a end of stmt */
734 1.1 cgd }
735 1.1 cgd break;
736 1.1 cgd
737 1.1 cgd case lbrace: /* got a '{' */
738 1.1 cgd ps.in_stmt = false; /* dont indent the {} */
739 1.1 cgd if (!ps.block_init)
740 1.1 cgd force_nl = true;/* force other stuff on same line as '{' onto
741 1.1 cgd * new line */
742 1.1 cgd else if (ps.block_init_level <= 0)
743 1.1 cgd ps.block_init_level = 1;
744 1.1 cgd else
745 1.1 cgd ps.block_init_level++;
746 1.1 cgd
747 1.1 cgd if (s_code != e_code && !ps.block_init) {
748 1.1 cgd if (!btype_2) {
749 1.1 cgd dump_line();
750 1.1 cgd ps.want_blank = false;
751 1.1 cgd }
752 1.1 cgd else if (ps.in_parameter_declaration && !ps.in_or_st) {
753 1.1 cgd ps.i_l_follow = 0;
754 1.1 cgd dump_line();
755 1.1 cgd ps.want_blank = false;
756 1.1 cgd }
757 1.1 cgd }
758 1.1 cgd if (ps.in_parameter_declaration)
759 1.1 cgd prefix_blankline_requested = 0;
760 1.1 cgd
761 1.1 cgd if (ps.p_l_follow > 0) { /* check for preceeding unbalanced
762 1.1 cgd * parens */
763 1.1 cgd diag(1, "Unbalanced parens");
764 1.1 cgd ps.p_l_follow = 0;
765 1.1 cgd if (sp_sw) { /* check for unclosed if, for, etc. */
766 1.1 cgd sp_sw = false;
767 1.1 cgd parse(hd_type);
768 1.1 cgd ps.ind_level = ps.i_l_follow;
769 1.1 cgd }
770 1.1 cgd }
771 1.1 cgd if (s_code == e_code)
772 1.1 cgd ps.ind_stmt = false; /* dont put extra indentation on line
773 1.1 cgd * with '{' */
774 1.1 cgd if (ps.in_decl && ps.in_or_st) { /* this is either a structure
775 1.1 cgd * declaration or an init */
776 1.1 cgd di_stack[ps.dec_nest++] = dec_ind;
777 1.1 cgd /* ? dec_ind = 0; */
778 1.1 cgd }
779 1.1 cgd else {
780 1.1 cgd ps.decl_on_line = false; /* we cant be in the middle of
781 1.1 cgd * a declaration, so dont do
782 1.1 cgd * special indentation of
783 1.1 cgd * comments */
784 1.1 cgd if (blanklines_after_declarations_at_proctop
785 1.1 cgd && ps.in_parameter_declaration)
786 1.1 cgd postfix_blankline_requested = 1;
787 1.1 cgd ps.in_parameter_declaration = 0;
788 1.1 cgd }
789 1.1 cgd dec_ind = 0;
790 1.1 cgd parse(lbrace); /* let parser know about this */
791 1.1 cgd if (ps.want_blank) /* put a blank before '{' if '{' is not at
792 1.1 cgd * start of line */
793 1.1 cgd *e_code++ = ' ';
794 1.1 cgd ps.want_blank = false;
795 1.1 cgd *e_code++ = '{';
796 1.1 cgd ps.just_saw_decl = 0;
797 1.1 cgd break;
798 1.1 cgd
799 1.1 cgd case rbrace: /* got a '}' */
800 1.1 cgd if (ps.p_stack[ps.tos] == decl && !ps.block_init) /* semicolons can be
801 1.1 cgd * omitted in
802 1.1 cgd * declarations */
803 1.1 cgd parse(semicolon);
804 1.1 cgd if (ps.p_l_follow) {/* check for unclosed if, for, else. */
805 1.1 cgd diag(1, "Unbalanced parens");
806 1.1 cgd ps.p_l_follow = 0;
807 1.1 cgd sp_sw = false;
808 1.1 cgd }
809 1.1 cgd ps.just_saw_decl = 0;
810 1.1 cgd ps.block_init_level--;
811 1.1 cgd if (s_code != e_code && !ps.block_init) { /* '}' must be first on
812 1.1 cgd * line */
813 1.1 cgd if (verbose)
814 1.1 cgd diag(0, "Line broken");
815 1.1 cgd dump_line();
816 1.1 cgd }
817 1.1 cgd *e_code++ = '}';
818 1.1 cgd ps.want_blank = true;
819 1.1 cgd ps.in_stmt = ps.ind_stmt = false;
820 1.1 cgd if (ps.dec_nest > 0) { /* we are in multi-level structure
821 1.1 cgd * declaration */
822 1.1 cgd dec_ind = di_stack[--ps.dec_nest];
823 1.1 cgd if (ps.dec_nest == 0 && !ps.in_parameter_declaration)
824 1.1 cgd ps.just_saw_decl = 2;
825 1.1 cgd ps.in_decl = true;
826 1.1 cgd }
827 1.1 cgd prefix_blankline_requested = 0;
828 1.1 cgd parse(rbrace); /* let parser know about this */
829 1.1 cgd ps.search_brace = cuddle_else && ps.p_stack[ps.tos] == ifhead
830 1.1 cgd && ps.il[ps.tos] >= ps.ind_level;
831 1.1 cgd if (ps.tos <= 1 && blanklines_after_procs && ps.dec_nest <= 0)
832 1.1 cgd postfix_blankline_requested = 1;
833 1.1 cgd break;
834 1.1 cgd
835 1.1 cgd case swstmt: /* got keyword "switch" */
836 1.1 cgd sp_sw = true;
837 1.1 cgd hd_type = swstmt; /* keep this for when we have seen the
838 1.1 cgd * expression */
839 1.1 cgd goto copy_id; /* go move the token into buffer */
840 1.1 cgd
841 1.1 cgd case sp_paren: /* token is if, while, for */
842 1.1 cgd sp_sw = true; /* the interesting stuff is done after the
843 1.1 cgd * expression is scanned */
844 1.1 cgd hd_type = (*token == 'i' ? ifstmt :
845 1.1 cgd (*token == 'w' ? whilestmt : forstmt));
846 1.1 cgd
847 1.1 cgd /*
848 1.1 cgd * remember the type of header for later use by parser
849 1.1 cgd */
850 1.1 cgd goto copy_id; /* copy the token into line */
851 1.1 cgd
852 1.1 cgd case sp_nparen: /* got else, do */
853 1.1 cgd ps.in_stmt = false;
854 1.1 cgd if (*token == 'e') {
855 1.1 cgd if (e_code != s_code && (!cuddle_else || e_code[-1] != '}')) {
856 1.1 cgd if (verbose)
857 1.1 cgd diag(0, "Line broken");
858 1.1 cgd dump_line();/* make sure this starts a line */
859 1.1 cgd ps.want_blank = false;
860 1.1 cgd }
861 1.1 cgd force_nl = true;/* also, following stuff must go onto new line */
862 1.1 cgd last_else = 1;
863 1.1 cgd parse(elselit);
864 1.1 cgd }
865 1.1 cgd else {
866 1.1 cgd if (e_code != s_code) { /* make sure this starts a line */
867 1.1 cgd if (verbose)
868 1.1 cgd diag(0, "Line broken");
869 1.1 cgd dump_line();
870 1.1 cgd ps.want_blank = false;
871 1.1 cgd }
872 1.1 cgd force_nl = true;/* also, following stuff must go onto new line */
873 1.1 cgd last_else = 0;
874 1.1 cgd parse(dolit);
875 1.1 cgd }
876 1.1 cgd goto copy_id; /* move the token into line */
877 1.1 cgd
878 1.1 cgd case decl: /* we have a declaration type (int, register,
879 1.1 cgd * etc.) */
880 1.1 cgd parse(decl); /* let parser worry about indentation */
881 1.1 cgd if (ps.last_token == rparen && ps.tos <= 1) {
882 1.1 cgd ps.in_parameter_declaration = 1;
883 1.1 cgd if (s_code != e_code) {
884 1.1 cgd dump_line();
885 1.1 cgd ps.want_blank = 0;
886 1.1 cgd }
887 1.1 cgd }
888 1.1 cgd if (ps.in_parameter_declaration && ps.indent_parameters && ps.dec_nest == 0) {
889 1.1 cgd ps.ind_level = ps.i_l_follow = 1;
890 1.1 cgd ps.ind_stmt = 0;
891 1.1 cgd }
892 1.1 cgd ps.in_or_st = true; /* this might be a structure or initialization
893 1.1 cgd * declaration */
894 1.1 cgd ps.in_decl = ps.decl_on_line = true;
895 1.1 cgd if ( /* !ps.in_or_st && */ ps.dec_nest <= 0)
896 1.1 cgd ps.just_saw_decl = 2;
897 1.1 cgd prefix_blankline_requested = 0;
898 1.1 cgd for (i = 0; token[i++];); /* get length of token */
899 1.1 cgd
900 1.1 cgd /*
901 1.1 cgd * dec_ind = e_code - s_code + (ps.decl_indent>i ? ps.decl_indent
902 1.1 cgd * : i);
903 1.1 cgd */
904 1.1 cgd dec_ind = ps.decl_indent > 0 ? ps.decl_indent : i;
905 1.1 cgd goto copy_id;
906 1.1 cgd
907 1.1 cgd case ident: /* got an identifier or constant */
908 1.1 cgd if (ps.in_decl) { /* if we are in a declaration, we must indent
909 1.1 cgd * identifier */
910 1.1 cgd if (ps.want_blank)
911 1.1 cgd *e_code++ = ' ';
912 1.1 cgd ps.want_blank = false;
913 1.1 cgd if (is_procname == 0 || !procnames_start_line) {
914 1.1 cgd if (!ps.block_init)
915 1.1 cgd if (troff && !ps.dumped_decl_indent) {
916 1.1 cgd sprintf(e_code, "\n.De %dp+\200p\n", dec_ind * 7);
917 1.1 cgd ps.dumped_decl_indent = 1;
918 1.1 cgd e_code += strlen(e_code);
919 1.1 cgd }
920 1.1 cgd else
921 1.1 cgd while ((e_code - s_code) < dec_ind) {
922 1.1 cgd CHECK_SIZE_CODE;
923 1.1 cgd *e_code++ = ' ';
924 1.1 cgd }
925 1.1 cgd }
926 1.1 cgd else {
927 1.1 cgd if (dec_ind && s_code != e_code)
928 1.1 cgd dump_line();
929 1.1 cgd dec_ind = 0;
930 1.1 cgd ps.want_blank = false;
931 1.1 cgd }
932 1.1 cgd }
933 1.1 cgd else if (sp_sw && ps.p_l_follow == 0) {
934 1.1 cgd sp_sw = false;
935 1.1 cgd force_nl = true;
936 1.1 cgd ps.last_u_d = true;
937 1.1 cgd ps.in_stmt = false;
938 1.1 cgd parse(hd_type);
939 1.1 cgd }
940 1.1 cgd copy_id:
941 1.1 cgd if (ps.want_blank)
942 1.1 cgd *e_code++ = ' ';
943 1.1 cgd if (troff && ps.its_a_keyword) {
944 1.1 cgd e_code = chfont(&bodyf, &keywordf, e_code);
945 1.1 cgd for (t_ptr = token; *t_ptr; ++t_ptr) {
946 1.1 cgd CHECK_SIZE_CODE;
947 1.1 cgd *e_code++ = keywordf.allcaps && islower(*t_ptr)
948 1.1 cgd ? toupper(*t_ptr) : *t_ptr;
949 1.1 cgd }
950 1.1 cgd e_code = chfont(&keywordf, &bodyf, e_code);
951 1.1 cgd }
952 1.1 cgd else
953 1.1 cgd for (t_ptr = token; *t_ptr; ++t_ptr) {
954 1.1 cgd CHECK_SIZE_CODE;
955 1.1 cgd *e_code++ = *t_ptr;
956 1.1 cgd }
957 1.1 cgd ps.want_blank = true;
958 1.1 cgd break;
959 1.1 cgd
960 1.1 cgd case period: /* treat a period kind of like a binary
961 1.1 cgd * operation */
962 1.1 cgd *e_code++ = '.'; /* move the period into line */
963 1.1 cgd ps.want_blank = false; /* dont put a blank after a period */
964 1.1 cgd break;
965 1.1 cgd
966 1.1 cgd case comma:
967 1.1 cgd ps.want_blank = (s_code != e_code); /* only put blank after comma
968 1.1 cgd * if comma does not start the
969 1.1 cgd * line */
970 1.1 cgd if (ps.in_decl && is_procname == 0 && !ps.block_init)
971 1.1 cgd while ((e_code - s_code) < (dec_ind - 1)) {
972 1.1 cgd CHECK_SIZE_CODE;
973 1.1 cgd *e_code++ = ' ';
974 1.1 cgd }
975 1.1 cgd
976 1.1 cgd *e_code++ = ',';
977 1.1 cgd if (ps.p_l_follow == 0) {
978 1.1 cgd if (ps.block_init_level <= 0)
979 1.1 cgd ps.block_init = 0;
980 1.1 cgd if (break_comma && (!ps.leave_comma || compute_code_target() + (e_code - s_code) > max_col - 8))
981 1.1 cgd force_nl = true;
982 1.1 cgd }
983 1.1 cgd break;
984 1.1 cgd
985 1.1 cgd case preesc: /* got the character '#' */
986 1.1 cgd if ((s_com != e_com) ||
987 1.1 cgd (s_lab != e_lab) ||
988 1.1 cgd (s_code != e_code))
989 1.1 cgd dump_line();
990 1.1 cgd *e_lab++ = '#'; /* move whole line to 'label' buffer */
991 1.1 cgd {
992 1.1 cgd int in_comment = 0;
993 1.1 cgd int com_start = 0;
994 1.1 cgd char quote = 0;
995 1.1 cgd int com_end = 0;
996 1.1 cgd
997 1.1 cgd while (*buf_ptr == ' ' || *buf_ptr == '\t') {
998 1.1 cgd buf_ptr++;
999 1.1 cgd if (buf_ptr >= buf_end)
1000 1.1 cgd fill_buffer();
1001 1.1 cgd }
1002 1.1 cgd while (*buf_ptr != '\n' || in_comment) {
1003 1.1 cgd CHECK_SIZE_LAB;
1004 1.1 cgd *e_lab = *buf_ptr++;
1005 1.1 cgd if (buf_ptr >= buf_end)
1006 1.1 cgd fill_buffer();
1007 1.1 cgd switch (*e_lab++) {
1008 1.1 cgd case BACKSLASH:
1009 1.1 cgd if (troff)
1010 1.1 cgd *e_lab++ = BACKSLASH;
1011 1.1 cgd if (!in_comment) {
1012 1.1 cgd *e_lab++ = *buf_ptr++;
1013 1.1 cgd if (buf_ptr >= buf_end)
1014 1.1 cgd fill_buffer();
1015 1.1 cgd }
1016 1.1 cgd break;
1017 1.1 cgd case '/':
1018 1.1 cgd if (*buf_ptr == '*' && !in_comment && !quote) {
1019 1.1 cgd in_comment = 1;
1020 1.1 cgd *e_lab++ = *buf_ptr++;
1021 1.1 cgd com_start = e_lab - s_lab - 2;
1022 1.1 cgd }
1023 1.1 cgd break;
1024 1.1 cgd case '"':
1025 1.1 cgd if (quote == '"')
1026 1.1 cgd quote = 0;
1027 1.1 cgd break;
1028 1.1 cgd case '\'':
1029 1.1 cgd if (quote == '\'')
1030 1.1 cgd quote = 0;
1031 1.1 cgd break;
1032 1.1 cgd case '*':
1033 1.1 cgd if (*buf_ptr == '/' && in_comment) {
1034 1.1 cgd in_comment = 0;
1035 1.1 cgd *e_lab++ = *buf_ptr++;
1036 1.1 cgd com_end = e_lab - s_lab;
1037 1.1 cgd }
1038 1.1 cgd break;
1039 1.1 cgd }
1040 1.1 cgd }
1041 1.1 cgd
1042 1.1 cgd while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
1043 1.1 cgd e_lab--;
1044 1.1 cgd if (e_lab - s_lab == com_end && bp_save == 0) { /* comment on
1045 1.1 cgd * preprocessor line */
1046 1.1 cgd if (sc_end == 0) /* if this is the first comment, we
1047 1.1 cgd * must set up the buffer */
1048 1.1 cgd sc_end = &(save_com[0]);
1049 1.1 cgd else {
1050 1.1 cgd *sc_end++ = '\n'; /* add newline between
1051 1.1 cgd * comments */
1052 1.1 cgd *sc_end++ = ' ';
1053 1.1 cgd --line_no;
1054 1.1 cgd }
1055 1.1 cgd bcopy(s_lab + com_start, sc_end, com_end - com_start);
1056 1.1 cgd sc_end += com_end - com_start;
1057 1.1 cgd if (sc_end >= &save_com[sc_size])
1058 1.1 cgd abort();
1059 1.1 cgd e_lab = s_lab + com_start;
1060 1.1 cgd while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
1061 1.1 cgd e_lab--;
1062 1.1 cgd bp_save = buf_ptr; /* save current input buffer */
1063 1.1 cgd be_save = buf_end;
1064 1.1 cgd buf_ptr = save_com; /* fix so that subsequent calls to
1065 1.1 cgd * lexi will take tokens out of
1066 1.1 cgd * save_com */
1067 1.1 cgd *sc_end++ = ' '; /* add trailing blank, just in case */
1068 1.1 cgd buf_end = sc_end;
1069 1.1 cgd sc_end = 0;
1070 1.1 cgd }
1071 1.1 cgd *e_lab = '\0'; /* null terminate line */
1072 1.1 cgd ps.pcase = false;
1073 1.1 cgd }
1074 1.1 cgd
1075 1.1 cgd if (strncmp(s_lab, "#if", 3) == 0) {
1076 1.1 cgd if (blanklines_around_conditional_compilation) {
1077 1.1 cgd register c;
1078 1.1 cgd prefix_blankline_requested++;
1079 1.1 cgd while ((c = getc(input)) == '\n');
1080 1.1 cgd ungetc(c, input);
1081 1.1 cgd }
1082 1.1 cgd if (ifdef_level < sizeof state_stack / sizeof state_stack[0]) {
1083 1.1 cgd match_state[ifdef_level].tos = -1;
1084 1.1 cgd state_stack[ifdef_level++] = ps;
1085 1.1 cgd }
1086 1.1 cgd else
1087 1.1 cgd diag(1, "#if stack overflow");
1088 1.1 cgd }
1089 1.1 cgd else if (strncmp(s_lab, "#else", 5) == 0)
1090 1.1 cgd if (ifdef_level <= 0)
1091 1.1 cgd diag(1, "Unmatched #else");
1092 1.1 cgd else {
1093 1.1 cgd match_state[ifdef_level - 1] = ps;
1094 1.1 cgd ps = state_stack[ifdef_level - 1];
1095 1.1 cgd }
1096 1.1 cgd else if (strncmp(s_lab, "#endif", 6) == 0) {
1097 1.1 cgd if (ifdef_level <= 0)
1098 1.1 cgd diag(1, "Unmatched #endif");
1099 1.1 cgd else {
1100 1.1 cgd ifdef_level--;
1101 1.1 cgd
1102 1.1 cgd #ifdef undef
1103 1.1 cgd /*
1104 1.1 cgd * This match needs to be more intelligent before the
1105 1.1 cgd * message is useful
1106 1.1 cgd */
1107 1.1 cgd if (match_state[ifdef_level].tos >= 0
1108 1.1 cgd && bcmp(&ps, &match_state[ifdef_level], sizeof ps))
1109 1.1 cgd diag(0, "Syntactically inconsistant #ifdef alternatives.");
1110 1.1 cgd #endif
1111 1.1 cgd }
1112 1.1 cgd if (blanklines_around_conditional_compilation) {
1113 1.1 cgd postfix_blankline_requested++;
1114 1.1 cgd n_real_blanklines = 0;
1115 1.1 cgd }
1116 1.1 cgd }
1117 1.1 cgd break; /* subsequent processing of the newline
1118 1.1 cgd * character will cause the line to be printed */
1119 1.1 cgd
1120 1.1 cgd case comment: /* we have gotten a /* this is a biggie */
1121 1.1 cgd if (flushed_nl) { /* we should force a broken line here */
1122 1.1 cgd flushed_nl = false;
1123 1.1 cgd dump_line();
1124 1.1 cgd ps.want_blank = false; /* dont insert blank at line start */
1125 1.1 cgd force_nl = false;
1126 1.1 cgd }
1127 1.1 cgd pr_comment();
1128 1.1 cgd break;
1129 1.1 cgd } /* end of big switch stmt */
1130 1.1 cgd
1131 1.1 cgd *e_code = '\0'; /* make sure code section is null terminated */
1132 1.1 cgd if (type_code != comment && type_code != newline && type_code != preesc)
1133 1.1 cgd ps.last_token = type_code;
1134 1.1 cgd } /* end of main while (1) loop */
1135 1.1 cgd }
1136 1.1 cgd
1137 1.1 cgd /*
1138 1.1 cgd * copy input file to backup file if in_name is /blah/blah/blah/file, then
1139 1.1 cgd * backup file will be ".Bfile" then make the backup file the input and
1140 1.1 cgd * original input file the output
1141 1.1 cgd */
1142 1.1 cgd bakcopy()
1143 1.1 cgd {
1144 1.1 cgd int n,
1145 1.1 cgd bakchn;
1146 1.1 cgd char buff[8 * 1024];
1147 1.1 cgd register char *p;
1148 1.1 cgd
1149 1.1 cgd /* construct file name .Bfile */
1150 1.1 cgd for (p = in_name; *p; p++); /* skip to end of string */
1151 1.1 cgd while (p > in_name && *p != '/') /* find last '/' */
1152 1.1 cgd p--;
1153 1.1 cgd if (*p == '/')
1154 1.1 cgd p++;
1155 1.1 cgd sprintf(bakfile, "%s.BAK", p);
1156 1.1 cgd
1157 1.1 cgd /* copy in_name to backup file */
1158 1.1 cgd bakchn = creat(bakfile, 0600);
1159 1.1 cgd if (bakchn < 0)
1160 1.1 cgd err(bakfile);
1161 1.1 cgd while (n = read(fileno(input), buff, sizeof buff))
1162 1.1 cgd if (write(bakchn, buff, n) != n)
1163 1.1 cgd err(bakfile);
1164 1.1 cgd if (n < 0)
1165 1.1 cgd err(in_name);
1166 1.1 cgd close(bakchn);
1167 1.1 cgd fclose(input);
1168 1.1 cgd
1169 1.1 cgd /* re-open backup file as the input file */
1170 1.1 cgd input = fopen(bakfile, "r");
1171 1.1 cgd if (input == 0)
1172 1.1 cgd err(bakfile);
1173 1.1 cgd /* now the original input file will be the output */
1174 1.1 cgd output = fopen(in_name, "w");
1175 1.1 cgd if (output == 0) {
1176 1.1 cgd unlink(bakfile);
1177 1.1 cgd err(in_name);
1178 1.1 cgd }
1179 1.1 cgd }
1180 1.1 cgd
1181 1.1 cgd err(msg)
1182 1.1 cgd char *msg;
1183 1.1 cgd {
1184 1.1 cgd (void)fprintf(stderr, "indent: %s: %s\n", msg, strerror(errno));
1185 1.1 cgd exit(1);
1186 1.1 cgd }
1187