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