· 7 years ago · Feb 19, 2019, 01:18 PM
1diff --git a/Makefile b/Makefile
2index f80f226..0cc9f57 100644
3--- a/Makefile
4+++ b/Makefile
5@@ -1,9 +1,7 @@
6-# @(#)Makefile 8.1 (Berkeley) 6/6/93
7-# $FreeBSD: head/usr.bin/indent/Makefile 249657 2013-04-19 19:45:00Z ed $
8+# $NetBSD: Makefile,v 1.6 2006/10/08 17:52:28 peter Exp $
9+# from: @(#)Makefile 8.1 (Berkeley) 6/6/93
10
11 PROG= indent
12 SRCS= indent.c io.c lexi.c parse.c pr_comment.c args.c
13
14-NO_WMISSING_VARIABLE_DECLARATIONS=
15-
16 .include <bsd.prog.mk>
17diff --git a/README b/README
18index 33d016d..618f1de 100644
19--- a/README
20+++ b/README
21@@ -1,6 +1,3 @@
22-
23- $FreeBSD: head/usr.bin/indent/README 105244 2002-10-16 13:58:39Z charnier $
24-
25 This is the C indenter, it originally came from the University of Illinois
26 via some distribution tape for PDP-11 Unix. It has subsequently been
27 hacked upon by James Gosling @ CMU. It isn't very pretty, and really needs
28@@ -48,7 +45,7 @@ version. David Willcox (the author) states that:
29 | Time passed. Some years later, indent showed up on one of the early
30 | emacs distributions.
31 |
32-| Later still, someone from UC Berkeley called the UofI and asked if
33+| Later still, someone from UC Berlekey called the UofI and asked if
34 | indent was in the public domain. They wanted to include it in their
35 | UNIX distributions, along with the emacs stuff. I was no longer at the
36 | UofI, but Rob Kolstad, who was, asked me about it. I told him I didn't
37@@ -70,7 +67,7 @@ version. David Willcox (the author) states that:
38 | Berkeley's copyright probably should only cover their changes, and I
39 | don't know their feelings about sending it out.
40
41-In any case, there appears to be none at UofI to clarify/and change
42+In any case, there appears to be noone at UofI to clarify/and change
43 that copyright, but I am confident (based on the statements of its
44 author) that the code, as it stands with its copyright, is
45 distributable, and will not cause any legal problems.
46diff --git a/args.c b/args.c
47index 63f1539..c60a939 100644
48--- a/args.c
49+++ b/args.c
50@@ -1,7 +1,37 @@
51-/*-
52- * Copyright (c) 1985 Sun Microsystems, Inc.
53+/* $NetBSD: args.c,v 1.13 2016/02/22 21:20:29 ginsbach Exp $ */
54+
55+/*
56 * Copyright (c) 1980, 1993
57 * The Regents of the University of California. All rights reserved.
58+ *
59+ * Redistribution and use in source and binary forms, with or without
60+ * modification, are permitted provided that the following conditions
61+ * are met:
62+ * 1. Redistributions of source code must retain the above copyright
63+ * notice, this list of conditions and the following disclaimer.
64+ * 2. Redistributions in binary form must reproduce the above copyright
65+ * notice, this list of conditions and the following disclaimer in the
66+ * documentation and/or other materials provided with the distribution.
67+ * 3. Neither the name of the University nor the names of its contributors
68+ * may be used to endorse or promote products derived from this software
69+ * without specific prior written permission.
70+ *
71+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
72+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
75+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
77+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
79+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
80+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
81+ * SUCH DAMAGE.
82+ */
83+
84+/*
85+ * Copyright (c) 1976 Board of Trustees of the University of Illinois.
86+ * Copyright (c) 1985 Sun Microsystems, Inc.
87 * All rights reserved.
88 *
89 * Redistribution and use in source and binary forms, with or without
90@@ -33,14 +63,15 @@
91 * SUCH DAMAGE.
92 */
93
94+
95 #if 0
96 #ifndef lint
97 static char sccsid[] = "@(#)args.c 8.1 (Berkeley) 6/6/93";
98-#endif /* not lint */
99+#endif /* not lint */
100 #endif
101
102 #include <sys/cdefs.h>
103-__FBSDID("$FreeBSD: head/usr.bin/indent/args.c 304650 2016-08-23 01:40:45Z pfg $");
104+__RCSID("$NetBSD: args.c,v 1.13 2016/02/22 21:20:29 ginsbach Exp $");
105
106 /*
107 * Argument scanning and profile reading code. Default parameters are set
108@@ -49,14 +80,12 @@ __FBSDID("$FreeBSD: head/usr.bin/indent/args.c 304650 2016-08-23 01:40:45Z pfg $
109
110 #include <ctype.h>
111 #include <err.h>
112-#include <limits.h>
113 #include <stdio.h>
114 #include <stdlib.h>
115 #include <string.h>
116 #include "indent_globs.h"
117-#include "indent.h"
118
119-#define INDENT_VERSION "2.0"
120+#define INDENT_VERSION "2.0"
121
122 /* profile types */
123 #define PRO_SPECIAL 1 /* special case */
124@@ -73,10 +102,10 @@ __FBSDID("$FreeBSD: head/usr.bin/indent/args.c 304650 2016-08-23 01:40:45Z pfg $
125 #define STDIN 3 /* use stdin */
126 #define KEY 4 /* type (keyword) */
127
128-static void scan_profile(FILE *);
129+void scan_profile(FILE *);
130
131 #define KEY_FILE 5 /* only used for args */
132-#define VERSION 6 /* only used for args */
133+#define VERSION 6 /* only used for args */
134
135 const char *option_source = "?";
136
137@@ -89,87 +118,224 @@ void add_typedefs_from_file(const char *str);
138 * default value is the one actually assigned.
139 */
140 struct pro {
141- const char *p_name; /* name, e.g. -bl, -cli */
142- int p_type; /* type (int, bool, special) */
143- int p_default; /* the default value (if int) */
144- int p_special; /* depends on type */
145- int *p_obj; /* the associated variable */
146-} pro[] = {
147-
148- {"T", PRO_SPECIAL, 0, KEY, 0},
149- {"U", PRO_SPECIAL, 0, KEY_FILE, 0},
150- {"-version", PRO_SPECIAL, 0, VERSION, 0},
151- {"P", PRO_SPECIAL, 0, IGN, 0},
152- {"bacc", PRO_BOOL, false, ON, &blanklines_around_conditional_compilation},
153- {"badp", PRO_BOOL, false, ON, &blanklines_after_declarations_at_proctop},
154- {"bad", PRO_BOOL, false, ON, &blanklines_after_declarations},
155- {"bap", PRO_BOOL, false, ON, &blanklines_after_procs},
156- {"bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments},
157- {"bc", PRO_BOOL, true, OFF, &ps.leave_comma},
158- {"bl", PRO_BOOL, true, OFF, &btype_2},
159- {"br", PRO_BOOL, true, ON, &btype_2},
160- {"bs", PRO_BOOL, false, ON, &Bill_Shannon},
161- {"cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline},
162- {"cd", PRO_INT, 0, 0, &ps.decl_com_ind},
163- {"ce", PRO_BOOL, true, ON, &cuddle_else},
164- {"ci", PRO_INT, 0, 0, &continuation_indent},
165- {"cli", PRO_SPECIAL, 0, CLI, 0},
166- {"c", PRO_INT, 33, 0, &ps.com_ind},
167- {"di", PRO_INT, 16, 0, &ps.decl_indent},
168- {"dj", PRO_BOOL, false, ON, &ps.ljust_decl},
169- {"d", PRO_INT, 0, 0, &ps.unindent_displace},
170- {"eei", PRO_BOOL, false, ON, &extra_expression_indent},
171- {"ei", PRO_BOOL, true, ON, &ps.else_if},
172- {"fbs", PRO_BOOL, true, ON, &function_brace_split},
173- {"fc1", PRO_BOOL, true, ON, &format_col1_comments},
174- {"fcb", PRO_BOOL, true, ON, &format_block_comments},
175- {"ip", PRO_BOOL, true, ON, &ps.indent_parameters},
176- {"i", PRO_INT, 8, 0, &ps.ind_size},
177- {"lc", PRO_INT, 0, 0, &block_comment_max_col},
178- {"ldi", PRO_INT, -1, 0, &ps.local_decl_indent},
179- {"lpl", PRO_BOOL, false, ON, &lineup_to_parens_always},
180- {"lp", PRO_BOOL, true, ON, &lineup_to_parens},
181- {"l", PRO_INT, 78, 0, &max_col},
182- {"nbacc", PRO_BOOL, false, OFF, &blanklines_around_conditional_compilation},
183- {"nbadp", PRO_BOOL, false, OFF, &blanklines_after_declarations_at_proctop},
184- {"nbad", PRO_BOOL, false, OFF, &blanklines_after_declarations},
185- {"nbap", PRO_BOOL, false, OFF, &blanklines_after_procs},
186- {"nbbb", PRO_BOOL, false, OFF, &blanklines_before_blockcomments},
187- {"nbc", PRO_BOOL, true, ON, &ps.leave_comma},
188- {"nbs", PRO_BOOL, false, OFF, &Bill_Shannon},
189- {"ncdb", PRO_BOOL, true, OFF, &comment_delimiter_on_blankline},
190- {"nce", PRO_BOOL, true, OFF, &cuddle_else},
191- {"ndj", PRO_BOOL, false, OFF, &ps.ljust_decl},
192- {"neei", PRO_BOOL, false, OFF, &extra_expression_indent},
193- {"nei", PRO_BOOL, true, OFF, &ps.else_if},
194- {"nfbs", PRO_BOOL, true, OFF, &function_brace_split},
195- {"nfc1", PRO_BOOL, true, OFF, &format_col1_comments},
196- {"nfcb", PRO_BOOL, true, OFF, &format_block_comments},
197- {"nip", PRO_BOOL, true, OFF, &ps.indent_parameters},
198- {"nlpl", PRO_BOOL, false, OFF, &lineup_to_parens_always},
199- {"nlp", PRO_BOOL, true, OFF, &lineup_to_parens},
200- {"npcs", PRO_BOOL, false, OFF, &proc_calls_space},
201- {"npro", PRO_SPECIAL, 0, IGN, 0},
202- {"npsl", PRO_BOOL, true, OFF, &procnames_start_line},
203- {"nsac", PRO_BOOL, false, OFF, &space_after_cast},
204- {"nsc", PRO_BOOL, true, OFF, &star_comment_cont},
205- {"nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines},
206- {"nut", PRO_BOOL, true, OFF, &use_tabs},
207- {"nv", PRO_BOOL, false, OFF, &verbose},
208- {"pcs", PRO_BOOL, false, ON, &proc_calls_space},
209- {"psl", PRO_BOOL, true, ON, &procnames_start_line},
210- {"sac", PRO_BOOL, false, ON, &space_after_cast},
211- {"sc", PRO_BOOL, true, ON, &star_comment_cont},
212- {"sob", PRO_BOOL, false, ON, &swallow_optional_blanklines},
213- {"st", PRO_SPECIAL, 0, STDIN, 0},
214- {"ta", PRO_BOOL, false, ON, &auto_typedefs},
215- {"ts", PRO_INT, 8, 0, &tabsize},
216- {"ut", PRO_BOOL, true, ON, &use_tabs},
217- {"v", PRO_BOOL, false, ON, &verbose},
218- /* whew! */
219- {0, 0, 0, 0, 0}
220+ const char *p_name; /* name, e.g. -bl, -cli */
221+ int p_type; /* type (int, bool, special) */
222+ int p_default; /* the default value (if int) */
223+ int p_special; /* depends on type */
224+ int *p_obj; /* the associated variable */
225+} pro[] = {
226+ {
227+ "T", PRO_SPECIAL, 0, KEY, 0
228+ },
229+ {
230+ "U", PRO_SPECIAL, 0, KEY_FILE, 0
231+ },
232+ {
233+ "-version", PRO_SPECIAL, 0, VERSION, 0
234+ },
235+ {
236+ "P", PRO_SPECIAL, 0, IGN, 0
237+ },
238+ {
239+ "bacc", PRO_BOOL, false, ON, &opt.blanklines_around_conditional_compilation
240+ },
241+ {
242+ "badp", PRO_BOOL, false, ON, &opt.blanklines_after_declarations_at_proctop
243+ },
244+ {
245+ "bad", PRO_BOOL, false, ON, &opt.blanklines_after_declarations
246+ },
247+ {
248+ "bap", PRO_BOOL, false, ON, &opt.blanklines_after_procs
249+ },
250+ {
251+ "bbb", PRO_BOOL, false, ON, &opt.blanklines_before_blockcomments
252+ },
253+ {
254+ "bc", PRO_BOOL, true, OFF, &opt.leave_comma
255+ },
256+ {
257+ "bl", PRO_BOOL, true, OFF, &opt.btype_2
258+ },
259+ {
260+ "br", PRO_BOOL, true, ON, &opt.btype_2
261+ },
262+ {
263+ "bs", PRO_BOOL, false, ON, &opt.Bill_Shannon
264+ },
265+ {
266+ "cdb", PRO_BOOL, true, ON, &opt.comment_delimiter_on_blankline
267+ },
268+ {
269+ "cd", PRO_INT, 0, 0, &opt.decl_com_ind
270+ },
271+ {
272+ "ce", PRO_BOOL, true, ON, &opt.cuddle_else
273+ },
274+ {
275+ "ci", PRO_INT, 0, 0, &opt.continuation_indent
276+ },
277+ {
278+ "cli", PRO_SPECIAL, 0, CLI, 0
279+ },
280+ {
281+ "cs", PRO_BOOL, false, ON, &opt.space_after_cast
282+ },
283+ {
284+ "c", PRO_INT, 33, 0, &opt.com_ind
285+ },
286+ {
287+ "di", PRO_INT, 16, 0, &opt.decl_indent
288+ },
289+ {
290+ "dj", PRO_BOOL, false, ON, &opt.ljust_decl
291+ },
292+ {
293+ "d", PRO_INT, 0, 0, &opt.unindent_displace
294+ },
295+ {
296+ "eei", PRO_BOOL, false, ON, &opt.extra_expression_indent
297+ },
298+ {
299+ "ei", PRO_BOOL, true, ON, &opt.else_if
300+ },
301+ {
302+ "fbs", PRO_BOOL, true, ON, &opt.function_brace_split
303+ },
304+ {
305+ "fc1", PRO_BOOL, true, ON, &opt.format_col1_comments
306+ },
307+ {
308+ "fcb", PRO_BOOL, true, ON, &opt.format_block_comments,
309+ },
310+ {
311+ "ip", PRO_BOOL, true, ON, &opt.indent_parameters
312+ },
313+ {
314+ "i", PRO_INT, 8, 0, &opt.ind_size
315+ },
316+ {
317+ "lc", PRO_INT, 0, 0, &opt.block_comment_max_col
318+ },
319+ {
320+ "ldi", PRO_INT, -1, 0, &opt.local_decl_indent
321+ }
322+ {
323+ "lpl", PRO_BOOL, false, ON, &opt.lineup_to_parens_always
324+ },
325+ {
326+ "lp", PRO_BOOL, true, ON, &opt.lineup_to_parens
327+ },
328+ {
329+ "l", PRO_INT, 78, 0, &opt.max_col
330+ },
331+ {
332+ "nbacc", PRO_BOOL, false, OFF, &opt.blanklines_around_conditional_compilation
333+ },
334+ {
335+ "nbadp", PRO_BOOL, false, OFF, &opt.blanklines_after_declarations_at_proctop
336+ },
337+ {
338+ "nbad", PRO_BOOL, false, OFF, &opt.blanklines_after_declarations
339+ },
340+ {
341+ "nbap", PRO_BOOL, false, OFF, &opt.blanklines_after_procs
342+ },
343+ {
344+ "nbbb", PRO_BOOL, false, OFF, &opt.blanklines_before_blockcomments
345+ },
346+ {
347+ "nbc", PRO_BOOL, true, ON, &opt.leave_comma
348+ },
349+ {
350+ "nbs", PRO_BOOL, false, OFF, opt.&Bill_Shannon
351+ },
352+ {
353+ "ncdb", PRO_BOOL, true, OFF, &opt.comment_delimiter_on_blankline
354+ },
355+ {
356+ "nce", PRO_BOOL, true, OFF, &opt.cuddle_else
357+ },
358+ {
359+ "ncs", PRO_BOOL, false, OFF, &opt.space_after_cast
360+ },
361+ {
362+ "ndj", PRO_BOOL, false, OFF, &opt.ljust_decl
363+ },
364+ {
365+ "neei", PRO_BOOL, false, OFF, &opt.extra_expression_indent
366+ },
367+ {
368+ "nei", PRO_BOOL, true, OFF, &opt.else_if
369+ },
370+ {
371+ "nfbs", PRO_BOOL, true, OFFm &opt.function_brace_split
372+ },
373+ {
374+ "nfc1", PRO_BOOL, true, OFF, &opt.format_col1_comments
375+ },
376+ {
377+ "nfcb", PRO_BOOL, true, OFF, &opt.format_block_comments,
378+ },
379+ {
380+ "nip", PRO_BOOL, true, OFF, &opt.indent_parameters
381+ },
382+ {
383+ "nlpl", PRO_BOOL, false, OFF, &opt.lineup_to_parens_always
384+ },
385+ {
386+ "nlp", PRO_BOOL, true, OFF, &opt.lineup_to_parens
387+ },
388+ {
389+ "npcs", PRO_BOOL, false, OFF, &opt.proc_calls_space
390+ },
391+ {
392+ "npro", PRO_SPECIAL, 0, IGN, 0
393+ },
394+ {
395+ "npsl", PRO_BOOL, true, OFF, &opt.procnames_start_line
396+ },
397+ {
398+ "nsc", PRO_BOOL, true, OFF, &opt.star_comment_cont
399+ },
400+ {
401+ "nut", PRO_BOOL, true, OFF, &opt.use_tabs
402+ },
403+ {
404+ "nsob", PRO_BOOL, false, OFF, &opt.swallow_optional_blanklines
405+ },
406+ {
407+ "nv", PRO_BOOL, false, OFF, &opt.verbose
408+ },
409+ {
410+ "pcs", PRO_BOOL, false, ON, &opt.proc_calls_space
411+ },
412+ {
413+ "psl", PRO_BOOL, true, ON, &opt.procnames_start_line
414+ },
415+ {
416+ "sc", PRO_BOOL, true, ON, &opt.star_comment_cont
417+ },
418+ {
419+ "sob", PRO_BOOL, false, ON, &opt.swallow_optional_blanklines
420+ },
421+ {
422+ "st", PRO_SPECIAL, 0, STDIN, 0
423+ },
424+ {
425+ "ts", PRO_INT, 8, 0, &opt.tabsize
426+ },
427+ {
428+ "ut", PRO_BOOL, true, ON, &opt.use_tabs
429+ },
430+ {
431+ "v", PRO_BOOL, false, ON, &opt.verbose
432+ },
433+ /* whew! */
434+ {
435+ 0, 0, 0, 0, 0
436+ }
437 };
438-
439 /*
440 * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
441 * given in these files.
442@@ -177,160 +343,164 @@ struct pro {
443 void
444 set_profile(const char *profile_name)
445 {
446- FILE *f;
447- char fname[PATH_MAX];
448- static char prof[] = ".indent.pro";
449-
450- if (profile_name == NULL)
451- snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), prof);
452- else
453- snprintf(fname, sizeof(fname), "%s", profile_name + 2);
454- if ((f = fopen(option_source = fname, "r")) != NULL) {
455- scan_profile(f);
456- (void) fclose(f);
457- }
458- if ((f = fopen(option_source = prof, "r")) != NULL) {
459- scan_profile(f);
460- (void) fclose(f);
461- }
462- option_source = "Command line";
463+ FILE *f;
464+ char fname[BUFSIZ];
465+ static char prof[] = ".indent.pro";
466+
467+ if (profile_name == NULL)
468+ snprintf(fname, sizeof(fname), "%s/%s", getenv("HOME"), prof);
469+ else
470+ snprintf(fname, sizeof(fname), "%s", profile_name + 2);
471+ if ((f = fopen(option_source = fname, "r")) != NULL) {
472+ scan_profile(f);
473+ (void) fclose(f);
474+ }
475+ if ((f = fopen(option_source = prof, "r")) != NULL) {
476+ scan_profile(f);
477+ (void) fclose(f);
478+ }
479+ option_source = "Command line";
480 }
481
482-static void
483+void
484 scan_profile(FILE *f)
485 {
486- int comment, i;
487- char *p;
488- char buf[BUFSIZ];
489-
490- while (1) {
491- p = buf;
492- comment = 0;
493- while ((i = getc(f)) != EOF) {
494+ int comment, i;
495+ char *p;
496+ char buf[BUFSIZ];
497+ while (1) {
498+ p = buf;
499+ comment = 0;
500+ while ((i = getc(f)) != EOF) {
501 if (i == '*' && !comment && p > buf && p[-1] == '/') {
502- comment = p - buf;
503- *p++ = i;
504+ comment = p - buf;
505+ *p++ = i;
506 } else if (i == '/' && comment && p > buf && p[-1] == '*') {
507- p = buf + comment - 1;
508- comment = 0;
509+ p = buf + comment - 1;
510+ comment = 0;
511+ } else if (isspace(i)) {
512 } else if (isspace((unsigned char)i)) {
513- if (p > buf && !comment)
514- break;
515+ if (p > buf && !comment)
516+ break;
517 } else {
518- *p++ = i;
519+ *p++ = i;
520 }
521 }
522 if (p != buf) {
523 *p++ = 0;
524- if (verbose)
525- printf("profile: %s\n", buf);
526+ if (opt.verbose)
527+ printf("profile: %s\n", buf);
528 set_option(buf);
529 }
530 else if (i == EOF)
531 return;
532- }
533+ }
534+
535 }
536
537+
538+
539 static const char *
540 eqin(const char *s1, const char *s2)
541 {
542- while (*s1) {
543- if (*s1++ != *s2++)
544- return (NULL);
545- }
546- return (s2);
547+ while (*s1) {
548+ if (*s1++ != *s2++)
549+ return (NULL);
550+ }
551+ return (s2);
552 }
553-
554 /*
555 * Set the defaults.
556 */
557 void
558 set_defaults(void)
559 {
560- struct pro *p;
561-
562- /*
563- * Because ps.case_indent is a float, we can't initialize it from the
564- * table:
565- */
566- ps.case_indent = 0.0; /* -cli0.0 */
567- for (p = pro; p->p_name; p++)
568- if (p->p_type != PRO_SPECIAL)
569- *p->p_obj = p->p_default;
570+ struct pro *p;
571+
572+ /*
573+ * Because ps.case_indent is a float, we can't initialize it from the
574+ * table:
575+ */
576+ ps.case_indent = 0.0; /* -cli0.0 */
577+ for (p = pro; p->p_name; p++)
578+ if (p->p_type != PRO_SPECIAL)
579+ *p->p_obj = p->p_default;
580 }
581
582 void
583 set_option(char *arg)
584 {
585- struct pro *p;
586- const char *param_start;
587-
588- arg++; /* ignore leading "-" */
589- for (p = pro; p->p_name; p++)
590- if (*p->p_name == *arg && (param_start = eqin(p->p_name, arg)) != NULL)
591- goto found;
592- errx(1, "%s: unknown parameter \"%s\"", option_source, arg - 1);
593+ struct pro *p;
594+ const char *param_start;
595+
596+ arg++; /* ignore leading "-" */
597+ for (p = pro; p->p_name; p++)
598+ if (*p->p_name == *arg && (param_start = eqin(p->p_name, arg)) != NULL)
599+ goto found;
600+ errx(1, "%s: unknown parameter \"%s\"", option_source, arg - 1);
601 found:
602- switch (p->p_type) {
603-
604- case PRO_SPECIAL:
605- switch (p->p_special) {
606-
607- case IGN:
608- break;
609-
610- case CLI:
611- if (*param_start == 0)
612- goto need_param;
613- ps.case_indent = atof(param_start);
614- break;
615-
616- case STDIN:
617- if (input == NULL)
618- input = stdin;
619- if (output == NULL)
620- output = stdout;
621- break;
622-
623- case KEY:
624- if (*param_start == 0)
625- goto need_param;
626- add_typename(param_start);
627- break;
628-
629- case KEY_FILE:
630- if (*param_start == 0)
631- goto need_param;
632- add_typedefs_from_file(param_start);
633- break;
634-
635- case VERSION:
636- printf("FreeBSD indent %s\n", INDENT_VERSION);
637+ switch (p->p_type) {
638+
639+ case PRO_SPECIAL:
640+ switch (p->p_special) {
641+
642+ case IGN:
643+ break;
644+
645+ case CLI:
646+ if (*param_start == 0)
647+ goto need_param;
648+ ps.case_indent = atof(param_start);
649+ break;
650+
651+ case STDIN:
652+ if (input == NULL)
653+ input = stdin;
654+ if (output == NULL)
655+ output = stdout;
656+ break;
657+
658+ case KEY:
659+ if (*param_start == 0)
660+ goto need_param;
661+ add_typename(param_start);
662+ break;
663+
664+ case KEY_FILE:
665+ if (*param_start == 0)
666+ goto need_param;
667+ add_typedefs_from_file(param_start);
668+ break;
669+
670+ case VERSION:
671+ printf("NetBSD indent %s\n", INDENT_VERSION);
672 exit(0);
673
674+ default:
675+ errx(1, "set_option: internal error: p_special %d",
676+ p->p_special);
677+ }
678+ break;
679+
680+ case PRO_BOOL:
681+ if (p->p_special == OFF)
682+ *p->p_obj = false;
683+ else
684+ *p->p_obj = true;
685+ break;
686+
687+ case PRO_INT:
688+ if (!isdigit((unsigned char)*param_start)) {
689+ need_param:
690+ errx(1, "%s: ``%s'' requires a parameter",
691+ option_source, p->p_name);
692+ }
693+ *p->p_obj = atoi(param_start);
694+ break;
695+
696 default:
697- errx(1, "set_option: internal error: p_special %d", p->p_special);
698- }
699- break;
700-
701- case PRO_BOOL:
702- if (p->p_special == OFF)
703- *p->p_obj = false;
704- else
705- *p->p_obj = true;
706- break;
707-
708- case PRO_INT:
709- if (!isdigit((unsigned char)*param_start)) {
710- need_param:
711- errx(1, "%s: ``%s'' requires a parameter", option_source, p->p_name);
712+ errx(1, "set_option: internal error: p_type %d", p->p_type);
713 }
714- *p->p_obj = atoi(param_start);
715- break;
716-
717- default:
718- errx(1, "set_option: internal error: p_type %d", p->p_type);
719- }
720 }
721
722 void
723@@ -340,13 +510,13 @@ add_typedefs_from_file(const char *str)
724 char line[BUFSIZ];
725
726 if ((file = fopen(str, "r")) == NULL) {
727- fprintf(stderr, "indent: cannot open file %s\n", str);
728- exit(1);
729+ fprintf(stderr, "indent: cannot open file %s\n", str);
730+ exit(1);
731 }
732 while ((fgets(line, BUFSIZ, file)) != NULL) {
733- /* Remove trailing whitespace */
734- line[strcspn(line, " \t\n\r")] = '\0';
735- add_typename(line);
736+ /* Remove trailing whitespace */
737+ line[strcspn(line, " \t\n\r")] = '\0';
738+ add_typename(line);
739 }
740 fclose(file);
741-}
742+}
743\ No newline at end of file
744diff --git a/indent.1 b/indent.1
745index 585073a..a474eba 100644
746--- a/indent.1
747+++ b/indent.1
748@@ -1,5 +1,33 @@
749+.\" $NetBSD: indent.1,v 1.26 2016/02/25 14:55:56 wiz Exp $
750+.\"
751 .\" Copyright (c) 1980, 1990, 1993
752 .\" The Regents of the University of California. All rights reserved.
753+.\"
754+.\" Redistribution and use in source and binary forms, with or without
755+.\" modification, are permitted provided that the following conditions
756+.\" are met:
757+.\" 1. Redistributions of source code must retain the above copyright
758+.\" notice, this list of conditions and the following disclaimer.
759+.\" 2. Redistributions in binary form must reproduce the above copyright
760+.\" notice, this list of conditions and the following disclaimer in the
761+.\" documentation and/or other materials provided with the distribution.
762+.\" 3. Neither the name of the University nor the names of its contributors
763+.\" may be used to endorse or promote products derived from this software
764+.\" without specific prior written permission.
765+.\"
766+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
767+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
768+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
769+.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
770+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
771+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
772+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
773+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
774+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
775+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
776+.\" SUCH DAMAGE.
777+.\"
778+.\" Copyright (c) 1985 Sun Microsystems, Inc.
779 .\" Copyright (c) 1976 Board of Trustees of the University of Illinois.
780 .\" All rights reserved.
781 .\"
782@@ -11,7 +39,11 @@
783 .\" 2. Redistributions in binary form must reproduce the above copyright
784 .\" notice, this list of conditions and the following disclaimer in the
785 .\" documentation and/or other materials provided with the distribution.
786-.\" 3. Neither the name of the University nor the names of its contributors
787+.\" 3. All advertising materials mentioning features or use of this software
788+.\" must display the following acknowledgement:
789+.\" This product includes software developed by the University of
790+.\" California, Berkeley and its contributors.
791+.\" 4. Neither the name of the University nor the names of its contributors
792 .\" may be used to endorse or promote products derived from this software
793 .\" without specific prior written permission.
794 .\"
795@@ -27,10 +59,9 @@
796 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
797 .\" SUCH DAMAGE.
798 .\"
799-.\" @(#)indent.1 8.1 (Berkeley) 7/1/93
800-.\" $FreeBSD: head/usr.bin/indent/indent.1 309524 2016-12-04 03:10:25Z pfg $
801+.\" from: @(#)indent.1 8.1 (Berkeley) 7/1/93
802 .\"
803-.Dd December 2, 2016
804+.Dd February 16, 2019
805 .Dt INDENT 1
806 .Os
807 .Sh NAME
808@@ -46,15 +77,14 @@
809 .Op Fl bbb | Fl nbbb
810 .Op Fl \&bc | Fl nbc
811 .Op Fl \&bl | Fl \&br
812-.Op Fl bs | Fl nbs
813+.OP Fl bs | Fl nbs
814 .Op Fl c Ns Ar n
815 .Op Fl \&cd Ns Ar n
816-.Bk -words
817 .Op Fl cdb | Fl ncdb
818-.Ek
819 .Op Fl \&ce | Fl nce
820 .Op Fl \&ci Ns Ar n
821 .Op Fl cli Ns Ar n
822+.Op Fl cs | Fl ncs
823 .Op Fl d Ns Ar n
824 .Op Fl \&di Ns Ar n
825 .Op Fl dj | Fl ndj
826@@ -78,11 +108,8 @@
827 .Op Fl P Ns Ar file
828 .Op Fl pcs | Fl npcs
829 .Op Fl psl | Fl npsl
830-.Op Fl sac | Fl nsac
831 .Op Fl \&sc | Fl nsc
832-.Bk -words
833 .Op Fl sob | Fl nsob
834-.Ek
835 .Op Fl \&st
836 .Op Fl \&ta
837 .Op Fl T Ns Ar typename
838@@ -92,20 +119,17 @@
839 .Op Fl v | Fl \&nv
840 .Op Fl -version
841 .Sh DESCRIPTION
842-The
843 .Nm
844-utility is a
845-.Em C
846+is a
847+.Tn C
848 program formatter.
849 It reformats the
850-.Em C
851+.Tn C
852 program in the
853 .Ar input-file
854 according to the switches.
855-The switches which can be
856-specified are described below.
857-They may appear before or after the file
858-names.
859+The switches which can be specified are described below.
860+They may appear before or after the file names.
861 .Pp
862 .Sy NOTE :
863 If you only specify an
864@@ -121,15 +145,20 @@ If
865 is named
866 .Sq Pa /blah/blah/file ,
867 the backup file is named
868-.Sq Pa file.BAK .
869+.Pa file.BAK .
870 .Pp
871 If
872 .Ar output-file
873 is specified,
874 .Nm
875-checks to make sure that it is different from
876+checks to make sure it is different from
877 .Ar input-file .
878 .Pp
879+If no
880+.Ar input-file
881+is specified
882+input is read from stdin and the formatted file is written to stdout.
883+.Pp
884 The options listed below control the formatting style imposed by
885 .Nm .
886 .Bl -tag -width Op
887@@ -141,7 +170,7 @@ compilation block.
888 For example, in front of every #ifdef and after every #endif.
889 Other blank lines surrounding such blocks will be swallowed.
890 Default:
891-.Fl nbacc .
892+.Fl nbacc .
893 .It Fl bad , nbad
894 If
895 .Fl bad
896@@ -176,7 +205,7 @@ is specified, then a newline is forced after each comma in a declaration.
897 .Fl nbc
898 turns off this option.
899 Default:
900-.Fl \&nbc .
901+.Fl \&bc .
902 .It Fl \&bl , \&br
903 Specifying
904 .Fl \&bl
905@@ -196,21 +225,24 @@ if (...) {
906 code
907 }
908 .Ed
909-.It Fl bs , nbs
910-Whether a blank should always be inserted after sizeof.
911-The default is
912-.Fl nbs.
913+.It Fl bs , Fl nbs
914+If
915+.Fl bs
916+is specified, a blank is forced after
917+.Ic sizeof .
918+Default:
919+.Fl nbs .
920 .It Fl c Ns Ar n
921 The column in which comments on code start.
922-The default is 33.
923+Default:
924+.Fl c33 .
925 .It Fl cd Ns Ar n
926 The column in which comments on declarations start.
927 The default
928 is for these comments to start in the same column as those on code.
929 .It Fl cdb , ncdb
930 Enables (disables) the placement of comment delimiters on blank lines.
931-With
932-this option enabled, comments look like this:
933+With this option enabled, comments look like this:
934 .Bd -literal -offset indent
935 /*
936 * this is a comment
937@@ -224,12 +256,12 @@ Rather than like this:
938 .Pp
939 This only affects block comments, not comments to the right of
940 code.
941-The default is
942+Default:
943 .Fl cdb .
944 .It Fl ce , nce
945-Enables (disables) forcing of `else's to cuddle up to the immediately preceding
946+Enables (disables) forcing `else's to cuddle up to the immediately preceding
947 `}'.
948-The default is
949+Default:
950 .Fl \&ce .
951 .It Fl \&ci Ns Ar n
952 Sets the continuation indent to be
953@@ -240,8 +272,7 @@ statement.
954 Parenthesized expressions have extra indentation added to
955 indicate the nesting, unless
956 .Fl \&lp
957-is in effect
958-or the continuation indent is exactly half of the main indent.
959+is in effect.
960 .Fl \&ci
961 defaults to the same value as
962 .Fl i .
963@@ -253,16 +284,18 @@ tab stops to the right of the containing
964 statement.
965 .Fl cli0.5
966 causes case labels to be indented half a tab stop.
967-The
968-default is
969+Default:
970 .Fl cli0 .
971+.It Fl cs , ncs
972+Control whether parenthesized type names in casts are followed by a space or
973+not.
974+The default is
975+.Fl ncs .
976 .It Fl d Ns Ar n
977-Controls the placement of comments which are not to the
978-right of code.
979+Controls the placement of comments which are not to the right of code.
980 For example,
981 .Fl \&d\&1
982-means that such comments are placed one indentation level to the
983-left of code.
984+means that such comments are placed one indentation level to the left of code.
985 Specifying the default
986 .Fl \&d\&0
987 lines up these comments with the code.
988@@ -273,26 +306,27 @@ Specifies the indentation, in character positions,
989 of global variable names and all struct/union member names
990 relative to the beginning of their type declaration.
991 The default is
992+Default:
993 .Fl di16 .
994 .It Fl dj , ndj
995 .Fl \&dj
996 left justifies declarations.
997 .Fl ndj
998 indents declarations the same as code.
999-The default is
1000+Default:
1001 .Fl ndj .
1002 .It Fl \&ei , nei
1003 Enables (disables) special
1004 .Ic else-if
1005 processing.
1006-If it is enabled, an
1007+If it's enabled, an
1008 .Ic if
1009 following an
1010 .Ic else
1011 will have the same indentation as the preceding
1012 .Ic \&if
1013 statement.
1014-The default is
1015+Default:
1016 .Fl ei .
1017 .It Fl eei , neei
1018 Enables (disables) extra indentation on continuation lines of
1019@@ -302,7 +336,7 @@ and
1020 .Ic while
1021 statements.
1022 These continuation lines will be indented one extra level.
1023-The default is
1024+Default:
1025 .Fl neei .
1026 .It Fl fbs , nfbs
1027 Enables (disables) splitting the function declaration and opening brace
1028@@ -315,57 +349,55 @@ Often, comments whose leading `/' is in column 1 have been carefully
1029 hand formatted by the programmer.
1030 In such cases,
1031 .Fl nfc1
1032-should be
1033-used.
1034-The default is
1035+should be used.
1036+Default:
1037 .Fl fc1 .
1038 .It Fl fcb , nfcb
1039 Enables (disables) the formatting of block comments (ones that begin
1040-with `/*\\n').
1041-Often, block comments have been not so carefully hand formatted by the
1042-programmer, but reformatting that would just change the line breaks is not
1043-wanted.
1044-In such cases,
1045+with `/*\\n'). Often, block comments have been not so carefully hand
1046+formatted by the programmer, but reformatting that would just change
1047+the line breaks is not wanted. In such cases,
1048 .Fl nfcb
1049-should be used.
1050-Block comments are then handled like box comments.
1051+should be used. Block comments are then handled like box comments.
1052 The default is
1053-.Fl fcb .
1054+.Fl fcb .
1055 .It Fl i Ns Ar n
1056 The number of columns for one indentation level.
1057-The default is 8.
1058+Default:
1059+.Fl i8 .
1060 .It Fl \&ip , nip
1061 Enables (disables) the indentation of parameter declarations from the left
1062 margin.
1063-The default is
1064+Default:
1065 .Fl \&ip .
1066 .It Fl l Ns Ar n
1067 Maximum length of an output line.
1068-The default is 78.
1069-.It Fl lc Ns Ar n
1070-Maximum length of an output line in a block comment.
1071-The default is 0, which means to limit block comment lines in accordance with
1072-.Fl l.
1073 .It Fl \&ldi Ns Ar n
1074 Specifies the indentation, in character positions,
1075 of local variable names
1076 relative to the beginning of their type declaration.
1077 The default is for local variable names to be indented
1078 by the same amount as global ones.
1079+Default:
1080+.Fl l78 .
1081+.It Fl lc Ns Ar n
1082+Maximum length of an output line in a block comment.
1083+The default is 0, which means to limit block comment lines in accordance with
1084+.Fl l.
1085 .It Fl \&lp , nlp
1086 Lines up code surrounded by parentheses in continuation lines.
1087 With
1088 .Fl \&lp ,
1089 if a line
1090-has a left paren which is not closed on that line, then continuation lines
1091-will be lined up to start at the character position just after the left
1092-paren.
1093+has a left parentheses which is not closed on that line, then
1094+continuation lines will be lined up to start at the character
1095+position just after the left parenthesis.
1096 For example, here is how a piece of continued code looks with
1097 .Fl nlp
1098 in effect:
1099 .Bd -literal -offset indent
1100 p1 = first_procedure(second_procedure(p2, p3),
1101-\ \ third_procedure(p4, p5));
1102+\ \ third_procedure(p4,p5));
1103 .Ed
1104 .Pp
1105 With
1106@@ -373,14 +405,14 @@ With
1107 in effect (the default) the code looks somewhat clearer:
1108 .Bd -literal -offset indent
1109 p1\ =\ first_procedure(second_procedure(p2,\ p3),
1110-\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4,\ p5));
1111+\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4,p5));
1112 .Ed
1113 .Pp
1114 Inserting two more newlines we get:
1115 .Bd -literal -offset indent
1116 p1\ =\ first_procedure(second_procedure(p2,
1117 \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p3),
1118-\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4,
1119+\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ third_procedure(p4
1120 \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ p5));
1121 .Ed
1122 .It Fl \&lpl , nlpl
1123@@ -410,41 +442,31 @@ If true
1124 .Pq Fl pcs
1125 all procedure calls will have a space inserted between
1126 the name and the `('.
1127-The default is
1128+Default:
1129 .Fl npcs .
1130 .It Fl psl , npsl
1131 If true
1132 .Pq Fl psl
1133 the names of procedures being defined are placed in
1134 column 1 \- their types, if any, will be left on the previous lines.
1135-The
1136-default is
1137+Default:
1138 .Fl psl .
1139-.It Fl sac , nsac
1140-Control whether parenthesized type names in casts are followed by a space or
1141-not.
1142-The default is
1143-.Fl nsac .
1144 .It Fl \&sc , nsc
1145 Enables (disables) the placement of asterisks (`*'s) at the left edge of all
1146 comments.
1147-The default is
1148+Default:
1149 .Fl sc .
1150 .It Fl sob , nsob
1151 If
1152 .Fl sob
1153 is specified, indent will swallow optional blank lines.
1154-You can use this to
1155-get rid of blank lines after declarations.
1156+You can use this to get rid of blank lines after declarations.
1157 Default:
1158 .Fl nsob .
1159 .It Fl \&st
1160 Causes
1161 .Nm
1162-to take its input from stdin and put its output to stdout.
1163-.It Fl ta
1164-Automatically add all identifiers ending in "_t" to the list
1165-of type keywords.
1166+to take its input from stdin, and put its output to stdout.
1167 .It Fl T Ns Ar typename
1168 Adds
1169 .Ar typename
1170@@ -456,15 +478,15 @@ You need to specify all the typenames that
1171 appear in your program that are defined by
1172 .Ic typedef
1173 \- nothing will be
1174-harmed if you miss a few, but the program will not be formatted as nicely as
1175+harmed if you miss a few, but the program won't be formatted as nicely as
1176 it should.
1177-This sounds like a painful thing to have to do, but it is really
1178+This sounds like a painful thing to have to do, but it's really
1179 a symptom of a problem in C:
1180 .Ic typedef
1181 causes a syntactic change in the
1182 language and
1183 .Nm
1184-cannot find all
1185+can't find all
1186 instances of
1187 .Ic typedef .
1188 .It Fl ts Ns Ar n
1189@@ -487,7 +509,7 @@ When in verbose mode,
1190 .Nm
1191 reports when it splits one line of input into two or more lines of output,
1192 and gives some size statistics at completion.
1193-The default is
1194+Default:
1195 .Fl \&nv .
1196 .It Fl -version
1197 Causes
1198@@ -507,16 +529,12 @@ If
1199 .Nm
1200 is run and a profile file exists, then it is read to set up the program's
1201 defaults.
1202-Switches on the command line, though, always override profile
1203-switches.
1204+Switches on the command line, though, always override profile switches.
1205 The switches should be separated by spaces, tabs or newlines.
1206-.Pp
1207 .Ss Comments
1208 .Sq Em Box
1209 .Em comments .
1210-The
1211 .Nm
1212-utility
1213 assumes that any comment with a dash or star immediately after the start of
1214 comment (that is, `/*\-' or `/**') is a comment surrounded by a box of stars.
1215 Each line of such a comment is left unchanged, except that its indentation
1216@@ -525,9 +543,8 @@ of the comment.
1217 .Pp
1218 .Em Straight text .
1219 All other comments are treated as straight text.
1220-The
1221 .Nm
1222-utility fits as many words (separated by blanks, tabs, or newlines) on a
1223+fits as many words (separated by blanks, tabs, or newlines) on a
1224 line as possible.
1225 Blank lines break paragraphs.
1226 .Ss Comment indentation
1227@@ -549,10 +566,8 @@ automatically extended in extreme cases.
1228 In general,
1229 .Nm
1230 leaves preprocessor lines alone.
1231-The only
1232-reformatting that it will do is to straighten up trailing comments.
1233-It
1234-leaves embedded comments alone.
1235+The only reformatting that it will do is to straighten up trailing comments.
1236+It leaves embedded comments alone.
1237 Conditional compilation
1238 .Pq Ic #ifdef...#endif
1239 is recognized and
1240@@ -560,21 +575,18 @@ is recognized and
1241 attempts to correctly
1242 compensate for the syntactic peculiarities introduced.
1243 .Ss C syntax
1244-The
1245 .Nm
1246-utility understands a substantial amount about the syntax of C, but it
1247+understands a substantial amount about the syntax of C, but it
1248 has a `forgiving' parser.
1249-It attempts to cope with the usual sorts of
1250-incomplete and malformed syntax.
1251+It attempts to cope with the usual sorts of incomplete and misformed syntax.
1252 In particular, the use of macros like:
1253 .Pp
1254 .Dl #define forever for(;;)
1255 .Pp
1256 is handled properly.
1257 .Sh ENVIRONMENT
1258-The
1259 .Nm
1260-utility uses the
1261+uses the
1262 .Ev HOME
1263 environment variable.
1264 .Sh FILES
1265@@ -590,15 +602,15 @@ The
1266 command appeared in
1267 .Bx 4.2 .
1268 .Sh BUGS
1269-The
1270 .Nm
1271-utility has even more switches than
1272+has even more switches than
1273 .Xr ls 1 .
1274 .Pp
1275-A common mistake is to try to indent all the
1276-.Em C
1277-programs in a directory by typing:
1278+A common mistake that often causes grief is typing:
1279 .Pp
1280 .Dl indent *.c
1281 .Pp
1282+to the shell in an attempt to indent all the
1283+.Tn C
1284+programs in a directory.
1285 This is probably a bug, not a feature.
1286diff --git a/indent.c b/indent.c
1287index f49b6ee..91db504 100644
1288--- a/indent.c
1289+++ b/indent.c
1290@@ -1,6 +1,6 @@
1291-/*-
1292- * Copyright (c) 1985 Sun Microsystems, Inc.
1293- * Copyright (c) 1976 Board of Trustees of the University of Illinois.
1294+/* $NetBSD: indent.c,v 1.23 2016/09/05 00:40:29 sevan Exp $ */
1295+
1296+/*
1297 * Copyright (c) 1980, 1993
1298 * The Regents of the University of California. All rights reserved.
1299 *
1300@@ -12,6 +12,36 @@
1301 * 2. Redistributions in binary form must reproduce the above copyright
1302 * notice, this list of conditions and the following disclaimer in the
1303 * documentation and/or other materials provided with the distribution.
1304+ * 3. Neither the name of the University nor the names of its contributors
1305+ * may be used to endorse or promote products derived from this software
1306+ * without specific prior written permission.
1307+ *
1308+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1309+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1310+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1311+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1312+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1313+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1314+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1315+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1316+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1317+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1318+ * SUCH DAMAGE.
1319+ */
1320+
1321+/*
1322+ * Copyright (c) 1976 Board of Trustees of the University of Illinois.
1323+ * Copyright (c) 1985 Sun Microsystems, Inc.
1324+ * All rights reserved.
1325+ *
1326+ * Redistribution and use in source and binary forms, with or without
1327+ * modification, are permitted provided that the following conditions
1328+ * are met:
1329+ * 1. Redistributions of source code must retain the above copyright
1330+ * notice, this list of conditions and the following disclaimer.
1331+ * 2. Redistributions in binary form must reproduce the above copyright
1332+ * notice, this list of conditions and the following disclaimer in the
1333+ * documentation and/or other materials provided with the distribution.
1334 * 3. All advertising materials mentioning features or use of this software
1335 * must display the following acknowledgement:
1336 * This product includes software developed by the University of
1337@@ -33,1163 +63,1227 @@
1338 * SUCH DAMAGE.
1339 */
1340
1341-#if 0
1342+#include <sys/cdefs.h>
1343+
1344 #ifndef lint
1345+#if 0
1346 static char sccsid[] = "@(#)indent.c 5.17 (Berkeley) 6/7/93";
1347-#endif /* not lint */
1348+#else
1349+__RCSID("$NetBSD: indent.c,v 1.23 2016/09/05 00:40:29 sevan Exp $");
1350 #endif
1351-
1352-#include <sys/cdefs.h>
1353-__FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 309380 2016-12-02 01:25:51Z pfg $");
1354+#endif /* not lint */
1355
1356 #include <sys/param.h>
1357+#ifdef HAVE_CAPSICUM
1358+#include <sys/capsicum.h>
1359+#endif
1360+#include <ctype.h>
1361 #include <err.h>
1362+#include <errno.h>
1363 #include <fcntl.h>
1364-#include <unistd.h>
1365 #include <stdio.h>
1366 #include <stdlib.h>
1367 #include <string.h>
1368-#include <ctype.h>
1369+#include <unistd.h>
1370+#include <locale.h>
1371+#define EXTERN
1372 #include "indent_globs.h"
1373+#undef EXTERN
1374 #include "indent_codes.h"
1375-#include "indent.h"
1376
1377-static void bakcopy(void);
1378-static void indent_declaration(int, int);
1379+static void bakcopy(void);
1380
1381-const char *in_name = "Standard Input"; /* will always point to name of input
1382- * file */
1383-const char *out_name = "Standard Output"; /* will always point to name
1384- * of output file */
1385-char bakfile[MAXPATHLEN] = "";
1386+const char *in_name = "Standard Input"; /* will always point to name of
1387+ * input file */
1388+const char *out_name = "Standard Output"; /* will always point to name of
1389+ * output file */
1390+char bakfile[MAXPATHLEN] = "";
1391
1392 int
1393 main(int argc, char **argv)
1394 {
1395- int dec_ind; /* current indentation for declarations */
1396- int di_stack[20]; /* a stack of structure indentation levels */
1397- int force_nl; /* when true, code must be broken */
1398- int hd_type = 0; /* used to store type of stmt for if (...),
1399+#ifdef HAVE_CAPSICUM
1400+ cap_rights_t rights;
1401+#endif
1402+ int dec_ind; /* current indentation for declarations */
1403+ int di_stack[20]; /* a stack of structure indentation levels */
1404+ int force_nl; /* when true, code must be broken */
1405+ int hd_type; /* used to store type of stmt for if (...),
1406 * for (...), etc */
1407- int i; /* local loop counter */
1408- int scase; /* set to true when we see a case, so we will
1409+ int i; /* local loop counter */
1410+ int scase; /* set to true when we see a case, so we will
1411 * know what to do with the following colon */
1412- int sp_sw; /* when true, we are in the expression of
1413+ int sp_sw; /* when true, we are in the expressin of
1414 * if(...), while(...), etc. */
1415- int squest; /* when this is positive, we have seen a ?
1416+ int squest; /* when this is positive, we have seen a ?
1417 * without the matching : in a <c>?<s>:<s>
1418 * construct */
1419- const char *t_ptr; /* used for copying tokens */
1420- int tabs_to_var; /* true if using tabs to indent to var name */
1421- int type_code; /* the type of token, returned by lexi */
1422-
1423- int last_else = 0; /* true iff last keyword was an else */
1424- const char *profile_name = NULL;
1425- struct parser_state transient_state; /* a copy for lookup */
1426+ const char *t_ptr; /* used for copying tokens */
1427+ int tabs_to_var = 0; /* true if using tabs to indent to var name */
1428+ int type_code; /* the type of token, returned by lexi */
1429
1430+ int last_else = 0; /* true iff last keyword was an else */
1431+ const char *profile_name = NULL;
1432+ struct parser_state transient_state; /* a copy for lookup */
1433
1434- /*-----------------------------------------------*\
1435- | INITIALIZATION |
1436- \*-----------------------------------------------*/
1437+ /*-----------------------------------------------*\
1438+ | INITIALIZATION |
1439+ \*-----------------------------------------------*/
1440
1441- found_err = 0;
1442+ if (!setlocale(LC_ALL, ""))
1443+ warnx("can't set locale.");
1444
1445- ps.p_stack[0] = stmt; /* this is the parser's stack */
1446- ps.last_nl = true; /* this is true if the last thing scanned was
1447+ hd_type = 0;
1448+ ps.p_stack[0] = stmt; /* this is the parser's stack */
1449+ ps.last_nl = true; /* this is true if the last thing scanned was
1450 * a newline */
1451- ps.last_token = semicolon;
1452- combuf = (char *) malloc(bufsize);
1453- if (combuf == NULL)
1454- err(1, NULL);
1455- labbuf = (char *) malloc(bufsize);
1456- if (labbuf == NULL)
1457- err(1, NULL);
1458- codebuf = (char *) malloc(bufsize);
1459- if (codebuf == NULL)
1460- err(1, NULL);
1461- tokenbuf = (char *) malloc(bufsize);
1462- if (tokenbuf == NULL)
1463- err(1, NULL);
1464- alloc_typenames();
1465- l_com = combuf + bufsize - 5;
1466- l_lab = labbuf + bufsize - 5;
1467- l_code = codebuf + bufsize - 5;
1468- l_token = tokenbuf + bufsize - 5;
1469- combuf[0] = codebuf[0] = labbuf[0] = ' '; /* set up code, label, and
1470- * comment buffers */
1471- combuf[1] = codebuf[1] = labbuf[1] = '\0';
1472- ps.else_if = 1; /* Default else-if special processing to on */
1473- s_lab = e_lab = labbuf + 1;
1474- s_code = e_code = codebuf + 1;
1475- s_com = e_com = combuf + 1;
1476- s_token = e_token = tokenbuf + 1;
1477-
1478- in_buffer = (char *) malloc(10);
1479- if (in_buffer == NULL)
1480- err(1, NULL);
1481- in_buffer_limit = in_buffer + 8;
1482- buf_ptr = buf_end = in_buffer;
1483- line_no = 1;
1484- had_eof = ps.in_decl = ps.decl_on_line = break_comma = false;
1485- sp_sw = force_nl = false;
1486- ps.in_or_st = false;
1487- ps.bl_line = true;
1488- dec_ind = 0;
1489- di_stack[ps.dec_nest = 0] = 0;
1490- ps.want_blank = ps.in_stmt = ps.ind_stmt = false;
1491-
1492- scase = ps.pcase = false;
1493- squest = 0;
1494- sc_end = NULL;
1495- bp_save = NULL;
1496- be_save = NULL;
1497-
1498- output = NULL;
1499- tabs_to_var = 0;
1500-
1501- /*--------------------------------------------------*\
1502- | COMMAND LINE SCAN |
1503- \*--------------------------------------------------*/
1504+ ps.last_token = semicolon;
1505+ combuf = (char *) malloc(bufsize);
1506+ if (combuf == NULL)
1507+ err(1, NULL);
1508+ labbuf = (char *) malloc(bufsize);
1509+ if (labbuf == NULL)
1510+ err(1, NULL);
1511+ codebuf = (char *) malloc(bufsize);
1512+ if (codebuf == NULL)
1513+ err(1, NULL);
1514+ tokenbuf = (char *) malloc(bufsize);
1515+ if (tokenbuf == NULL)
1516+ err(1, NULL);
1517+ alloc_typenames();
1518+ init_constant_tt();
1519+ l_com = combuf + bufsize - 5;
1520+ l_lab = labbuf + bufsize - 5;
1521+ l_code = codebuf + bufsize - 5;
1522+ l_token = tokenbuf + bufsize - 5;
1523+ combuf[0] = codebuf[0] = labbuf[0] = ' '; /* set up code, label,
1524+ * and comment buffers */
1525+ combuf[1] = codebuf[1] = labbuf[1] = '\0';
1526+ opt.else_if = 1; /* Default else-if special processing to on */
1527+ s_lab = e_lab = labbuf + 1;
1528+ s_code = e_code = codebuf + 1;
1529+ s_com = e_com = combuf + 1;
1530+ s_token = e_token = tokenbuf + 1;
1531+
1532+ in_buffer = (char *) malloc(10);
1533+ in_buffer_limit = in_buffer + 8;
1534+ if (in_buffer == NULL)
1535+ err(1, NULL);
1536+ buf_ptr = buf_end = in_buffer;
1537+ line_no = 1;
1538+ had_eof = ps.in_decl = ps.decl_on_line = break_comma = false;
1539+ sp_sw = force_nl = false;
1540+ ps.in_or_st = false;
1541+ ps.bl_line = true;
1542+ dec_ind = 0;
1543+ di_stack[ps.dec_nest = 0] = 0;
1544+ ps.want_blank = ps.in_stmt = ps.ind_stmt = false;
1545+
1546+
1547+ scase = ps.pcase = false;
1548+ squest = 0;
1549+ sc_end = NULL;
1550+ bp_save = NULL;
1551+ be_save = NULL;
1552+
1553+ output = NULL;
1554+
1555+
1556+
1557+ /*--------------------------------------------------*\
1558+ | COMMAND LINE SCAN |
1559+ \*--------------------------------------------------*/
1560
1561 #ifdef undef
1562- max_col = 78; /* -l78 */
1563- lineup_to_parens = 1; /* -lp */
1564- lineup_to_parens_always = 0; /* -nlpl */
1565- ps.ljust_decl = 0; /* -ndj */
1566- ps.com_ind = 33; /* -c33 */
1567- star_comment_cont = 1; /* -sc */
1568- ps.ind_size = 8; /* -i8 */
1569- verbose = 0;
1570- ps.decl_indent = 16; /* -di16 */
1571- ps.local_decl_indent = -1; /* if this is not set to some nonnegative value
1572+ max_col = 78; /* -l78 */
1573+ lineup_to_parens = 1; /* -lp */
1574+ lineup_to_parens_always = 0; /* -nlpl */
1575+ ps.ljust_decl = 0; /* -ndj */
1576+ ps.com_ind = 33; /* -c33 */
1577+ star_comment_cont = 1; /* -sc */
1578+ ps.ind_size = 8; /* -i8 */
1579+ verbose = 0;
1580+ ps.decl_indent = 16; /* -di16 */
1581+ ps.local_decl_indent = -1; /* if this is not set to some nonnegative value
1582 * by an arg, we will set this equal to
1583 * ps.decl_ind */
1584- ps.indent_parameters = 1; /* -ip */
1585- ps.decl_com_ind = 0; /* if this is not set to some positive value
1586+ ps.indent_parameters = 1; /* -ip */
1587+ ps.decl_com_ind = 0; /* if this is not set to some positive value
1588 * by an arg, we will set this equal to
1589 * ps.com_ind */
1590- btype_2 = 1; /* -br */
1591- cuddle_else = 1; /* -ce */
1592- ps.unindent_displace = 0; /* -d0 */
1593- ps.case_indent = 0; /* -cli0 */
1594- format_block_comments = 1; /* -fcb */
1595- format_col1_comments = 1; /* -fc1 */
1596- procnames_start_line = 1; /* -psl */
1597- proc_calls_space = 0; /* -npcs */
1598- comment_delimiter_on_blankline = 1; /* -cdb */
1599- ps.leave_comma = 1; /* -nbc */
1600+ btype_2 = 1; /* -br */
1601+ cuddle_else = 1; /* -ce */
1602+ ps.unindent_displace = 0; /* -d0 */
1603+ ps.case_indent = 0; /* -cli0 */
1604+ format_block_comments = 1; /* -fcb */
1605+ format_col1_comments = 1; /* -fc1 */
1606+ procnames_start_line = 1; /* -psl */
1607+ proc_calls_space = 0; /* -npcs */
1608+ comment_delimiter_on_blankline = 1; /* -cdb */
1609+ ps.leave_comma = 1; /* -nbc */
1610 #endif
1611
1612- for (i = 1; i < argc; ++i)
1613- if (strcmp(argv[i], "-npro") == 0)
1614- break;
1615+ for (i = 1; i < argc; ++i)
1616+ if (strcmp(argv[i], "-npro") == 0)
1617+ break;
1618 else if (argv[i][0] == '-' && argv[i][1] == 'P' && argv[i][2] != '\0')
1619 profile_name = argv[i]; /* non-empty -P (set profile) */
1620- set_defaults();
1621- if (i >= argc)
1622- set_profile(profile_name);
1623+ set_defaults();
1624+ if (i >= argc)
1625+ set_profile(profile_name);
1626
1627- for (i = 1; i < argc; ++i) {
1628+ for (i = 1; i < argc; ++i) {
1629
1630- /*
1631- * look thru args (if any) for changes to defaults
1632- */
1633- if (argv[i][0] != '-') {/* no flag on parameter */
1634- if (input == NULL) { /* we must have the input file */
1635- in_name = argv[i]; /* remember name of input file */
1636- input = fopen(in_name, "r");
1637- if (input == NULL) /* check for open error */
1638- err(1, "%s", in_name);
1639- continue;
1640- }
1641- else if (output == NULL) { /* we have the output file */
1642- out_name = argv[i]; /* remember name of output file */
1643- if (strcmp(in_name, out_name) == 0) { /* attempt to overwrite
1644- * the file */
1645- errx(1, "input and output files must be different");
1646- }
1647- output = fopen(out_name, "w");
1648- if (output == NULL) /* check for create error */
1649- err(1, "%s", out_name);
1650- continue;
1651- }
1652- errx(1, "unknown parameter: %s", argv[i]);
1653+ /*
1654+ * look thru args (if any) for changes to defaults
1655+ */
1656+ if (argv[i][0] != '-') { /* no flag on parameter */
1657+ if (input == 0) { /* we must have the input file */
1658+ in_name = argv[i]; /* remember name of
1659+ * input file */
1660+ input = fopen(in_name, "r");
1661+ if (input == 0) /* check for open error */
1662+ err(1, "%s", in_name);
1663+ continue;
1664+ } else
1665+ if (output == 0) { /* we have the output
1666+ * file */
1667+ out_name = argv[i]; /* remember name of
1668+ * output file */
1669+ if (strcmp(in_name, out_name) == 0) { /* attempt to overwrite
1670+ * the file */
1671+ errx(1, "input and output files must be different");
1672+ }
1673+ output = fopen(out_name, "w");
1674+ if (output == 0) /* check for create
1675+ * error */
1676+ err(1, "%s", out_name);
1677+ continue;
1678+ }
1679+ errx(1, "unknown parameter: %s", argv[i]);
1680+ } else
1681+ set_option(argv[i]);
1682+ } /* end of for */
1683+ if (input == 0) {
1684+ input = stdin;
1685 }
1686- else
1687- set_option(argv[i]);
1688- } /* end of for */
1689- if (input == NULL)
1690- input = stdin;
1691- if (output == NULL) {
1692- if (input == stdin)
1693- output = stdout;
1694- else {
1695- out_name = in_name;
1696- bakcopy();
1697+ if (output == 0) {
1698+ if (input == stdin)
1699+ output = stdout;
1700+ else {
1701+ out_name = in_name;
1702+ bakcopy();
1703+ }
1704 }
1705- }
1706
1707- if (ps.com_ind <= 1)
1708- ps.com_ind = 2; /* dont put normal comments before column 2 */
1709- if (block_comment_max_col <= 0)
1710- block_comment_max_col = max_col;
1711- if (ps.local_decl_indent < 0) /* if not specified by user, set this */
1712- ps.local_decl_indent = ps.decl_indent;
1713- if (ps.decl_com_ind <= 0) /* if not specified by user, set this */
1714- ps.decl_com_ind = ps.ljust_decl ? (ps.com_ind <= 10 ? 2 : ps.com_ind - 8) : ps.com_ind;
1715- if (continuation_indent == 0)
1716- continuation_indent = ps.ind_size;
1717- fill_buffer(); /* get first batch of stuff into input buffer */
1718-
1719- parse(semicolon);
1720- {
1721- char *p = buf_ptr;
1722- int col = 1;
1723-
1724- while (1) {
1725- if (*p == ' ')
1726- col++;
1727- else if (*p == '\t')
1728- col = tabsize * (1 + (col - 1) / tabsize) + 1;
1729- else
1730- break;
1731- p++;
1732+#ifdef HAVE_CAPSICUM
1733+ /* Restrict input/output descriptors and enter Capsicum sandbox. */
1734+ cap_rights_init(&rights, CAP_FSTAT, CAP_WRITE);
1735+ if (cap_rights_limit(fileno(output), &rights) < 0 && errno != ENOSYS)
1736+ err(EXIT_FAILURE, "unable to limit rights for %s", out_name);
1737+ cap_rights_init(&rights, CAP_FSTAT, CAP_READ);
1738+ if (cap_rights_limit(fileno(input), &rights) < 0 && errno != ENOSYS)
1739+ err(EXIT_FAILURE, "unable to limit rights for %s", in_name);
1740+ if (cap_enter() < 0 && errno != ENOSYS)
1741+ err(EXIT_FAILURE, "unable to enter capability mode");
1742+#endif
1743+
1744+ if (opt.com_ind <= 1)
1745+ opt.com_ind = 2; /* don't put normal comments before column 2 */
1746+ if (opt.block_comment_max_col <= 0)
1747+ opt.block_comment_max_col = opt.max_col;
1748+ if (ps.local_decl_indent < 0) /* if not specified by user, set this */
1749+ ps.local_decl_indent = ps.decl_indent;s
1750+ if (opt.decl_com_ind <= 0) /* if not specified by user, set this */
1751+ opt.decl_com_ind = opt.ljust_decl ? (opt.com_ind <= 10 ? 2 : opt.com_ind - 8) : opt.com_ind;
1752+ if (opt.continuation_indent == 0)
1753+ opt.continuation_indent = opt.ind_size;
1754+ fill_buffer(); /* get first batch of stuff into input buffer */
1755+
1756+ parse(semicolon);
1757+ {
1758+ char *p = buf_ptr;
1759+ int col = 1;
1760+
1761+ while (1) {
1762+ if (*p == ' ')
1763+ col++;
1764+ else
1765+ if (*p == '\t')
1766+ col = opt.tabsize * (1 + (col - 1) / opt.tabsize) + 1;
1767+ else
1768+ break;
1769+ p++;
1770+ }
1771+ if (col > opt.ind_size)
1772+ ps.ind_level = ps.i_l_follow = col / opt.ind_size;
1773 }
1774- if (col > ps.ind_size)
1775- ps.ind_level = ps.i_l_follow = col / ps.ind_size;
1776- }
1777
1778- /*
1779- * START OF MAIN LOOP
1780- */
1781+ /*
1782+ * START OF MAIN LOOP
1783+ */
1784
1785- while (1) { /* this is the main loop. it will go until we
1786+ while (1) { /* this is the main loop. it will go until we
1787 * reach eof */
1788- int comment_buffered = false;
1789+ int comment_buffered = false;
1790
1791- type_code = lexi(&ps); /* lexi reads one token. The actual
1792- * characters read are stored in "token". lexi
1793- * returns a code indicating the type of token */
1794+ type_code = lexi(&ps); /* lexi reads one token. The actual
1795+ * characters read are stored in
1796+ * "token". lexi returns a code
1797+ * indicating the type of token */
1798
1799- /*
1800- * The following code moves newlines and comments following an if (),
1801- * while (), else, etc. up to the start of the following stmt to
1802- * a buffer. This allows proper handling of both kinds of brace
1803- * placement (-br, -bl) and cuddling "else" (-ce).
1804- */
1805-
1806- while (ps.search_brace) {
1807- switch (type_code) {
1808- case newline:
1809- if (sc_end == NULL) {
1810- save_com = sc_buf;
1811- save_com[0] = save_com[1] = ' ';
1812- sc_end = &save_com[2];
1813- }
1814- *sc_end++ = '\n';
1815 /*
1816- * We may have inherited a force_nl == true from the previous
1817- * token (like a semicolon). But once we know that a newline
1818- * has been scanned in this loop, force_nl should be false.
1819- *
1820- * However, the force_nl == true must be preserved if newline
1821- * is never scanned in this loop, so this assignment cannot be
1822- * done earlier.
1823+ * The following code moves newlines and comments following an if (),
1824+ * while (), else, etc. up to the start of the following stmt to
1825+ * a buffer. This allows proper handling of both kinds of brace
1826+ * placement (-br, -bl) and cuddling "else" (-ce).
1827 */
1828- force_nl = false;
1829- case form_feed:
1830- break;
1831- case comment:
1832- if (sc_end == NULL) {
1833- /*
1834- * Copy everything from the start of the line, because
1835- * pr_comment() will use that to calculate original
1836- * indentation of a boxed comment.
1837- */
1838- memcpy(sc_buf, in_buffer, buf_ptr - in_buffer - 4);
1839- save_com = sc_buf + (buf_ptr - in_buffer - 4);
1840- save_com[0] = save_com[1] = ' ';
1841- sc_end = &save_com[2];
1842- }
1843- comment_buffered = true;
1844- *sc_end++ = '/'; /* copy in start of comment */
1845- *sc_end++ = '*';
1846- for (;;) { /* loop until we get to the end of the comment */
1847- *sc_end = *buf_ptr++;
1848- if (buf_ptr >= buf_end)
1849- fill_buffer();
1850- if (*sc_end++ == '*' && *buf_ptr == '/')
1851- break; /* we are at end of comment */
1852- if (sc_end >= &save_com[sc_size]) { /* check for temp buffer
1853- * overflow */
1854- diag2(1, "Internal buffer overflow - Move big comment from right after if, while, or whatever");
1855- fflush(output);
1856- exit(1);
1857- }
1858- }
1859- *sc_end++ = '/'; /* add ending slash */
1860- if (++buf_ptr >= buf_end) /* get past / in buffer */
1861- fill_buffer();
1862- break;
1863- case lbrace:
1864- /*
1865- * Put KNF-style lbraces before the buffered up tokens and
1866- * jump out of this loop in order to avoid copying the token
1867- * again under the default case of the switch below.
1868- */
1869- if (sc_end != NULL && btype_2) {
1870- save_com[0] = '{';
1871- /*
1872- * Originally the lbrace may have been alone on its own
1873- * line, but it will be moved into "the else's line", so
1874- * if there was a newline resulting from the "{" before,
1875- * it must be scanned now and ignored.
1876- */
1877- while (isspace((unsigned char)*buf_ptr)) {
1878- if (++buf_ptr >= buf_end)
1879- fill_buffer();
1880- if (*buf_ptr == '\n')
1881- break;
1882- }
1883- goto sw_buffer;
1884- }
1885- /* FALLTHROUGH */
1886- default: /* it is the start of a normal statement */
1887- {
1888- int remove_newlines;
1889-
1890- remove_newlines =
1891- /* "} else" */
1892- (type_code == sp_nparen && *token == 'e' &&
1893- e_code != s_code && e_code[-1] == '}')
1894- /* "else if" */
1895- || (type_code == sp_paren && *token == 'i' &&
1896- last_else && ps.else_if);
1897- if (remove_newlines)
1898- force_nl = false;
1899- if (sc_end == NULL) { /* ignore buffering if
1900- * comment wasn't saved up */
1901- ps.search_brace = false;
1902- goto check_type;
1903- }
1904- while (sc_end > save_com && isblank((unsigned char)sc_end[-1])) {
1905- sc_end--;
1906- }
1907- if (swallow_optional_blanklines ||
1908- (!comment_buffered && remove_newlines)) {
1909- force_nl = !remove_newlines;
1910- while (sc_end > save_com && sc_end[-1] == '\n') {
1911- sc_end--;
1912- }
1913+
1914+ while (ps.search_brace) {
1915+ switch (type_code) {
1916+ case newline:
1917+ if (sc_end == NULL) {
1918+ save_com = sc_buf;
1919+ save_com[0] = save_com[1] = ' ';
1920+ sc_end = &save_com[2];
1921+ }
1922+ *sc_end++ = '\n';
1923+ /*
1924+ * We may have inherited a force_nl == true from the previous
1925+ * token (like a semicolon). But once we know that a newline
1926+ * has been scanned in this loop, force_nl should be false.
1927+ *
1928+ * However, the force_nl == true must be preserved if newline
1929+ * is never scanned in this loop, so this assignment cannot be
1930+ * done earlier.
1931+ */
1932+ force_nl = false;
1933+ case form_feed:
1934+ break;
1935+ case comment:
1936+ if (sc_end == NULL) {
1937+ /*
1938+ * Copy everything from the start of the line, because
1939+ * pr_comment() will use that to calculate original
1940+ * indentation of a boxed comment.
1941+ */
1942+ memcpy(sc_buf, in_buffer, buf_ptr - in_buffer - 4);
1943+ save_com = sc_buf + (buf_ptr - in_buffer - 4);
1944+ save_com[0] = save_com[1] = ' ';
1945+ sc_end = &save_com[2];
1946+ }
1947+ comment_buffered = true;
1948+ *sc_end++ = '/'; /* copy in start of comment */
1949+ *sc_end++ = '*';
1950+ for (;;) { /* loop until we get to the end of the comment */
1951+ *sc_end = *buf_ptr++;
1952+ if (buf_ptr >= buf_end)
1953+ fill_buffer();
1954+ if (*sc_end++ == '*' && *buf_ptr == '/')
1955+ break; /* we are at end of comment */
1956+ if (sc_end >= &save_com[sc_size]) { /* check for temp buffer
1957+ * overflow */
1958+ diag2(1, "Internal buffer overflow - Move big comment from right after if, while, or whatever");
1959+ fflush(output);
1960+ exit(1);
1961+ }
1962+ }
1963+
1964+ *sc_end++ = '/'; /* add ending slash */
1965+ if (++buf_ptr >= buf_end) /* get past / in buffer */
1966+ fill_buffer();
1967+ break;
1968+ case lbrace:
1969+ /*
1970+ * Put KNF-style lbraces before the buffered up tokens and
1971+ * jump out of this loop in order to avoid copying the token
1972+ * again under the default case of the switch below.
1973+ */
1974+ if (sc_end != NULL && opt.btype_2) {
1975+ save_com[0] = '{';
1976+ /*
1977+ * Originally the lbrace may have been alone on its own
1978+ * line, but it will be moved into "the else's line", so
1979+ * if there was a newline resulting from the "{" before,
1980+ * it must be scanned now and ignored.
1981+ */
1982+ while (isspace((unsigned char)*buf_ptr)) {
1983+ if (++buf_ptr >= buf_end)
1984+ fill_buffer();
1985+
1986+ if (*buf_ptr == '\n')
1987+ break;
1988+ }
1989+ goto sw_buffer;
1990+ }
1991+ /* FALLTHROUGH */
1992+ default: /* it is the start of a normal
1993+ * statment */
1994+ {
1995+ int remove_newlines;
1996+
1997+ remove_newlines =
1998+ /* "} else" */
1999+ (type_code == sp_nparen && *token == 'e' &&
2000+ e_code != s_code && e_code[-1] == '}')
2001+ /* "else if" */
2002+ || (type_code == sp_paren && *token == 'i' &&
2003+ last_else && opt.else_if);
2004+ if (remove_newlines)
2005+ force_nl = false;
2006+ if (sc_end == NULL) { /* ignore buffering if
2007+ * comment wasn't saved up */
2008+ ps.search_brace = false;
2009+ goto check_type;
2010+ }
2011+ while (sc_end > save_com && isblank((unsigned char)sc_end[-1])) {
2012+ sc_end--;
2013+ }
2014+ if (opt.swallow_optional_blanklines ||
2015+ (!comment_buffered && remove_newlines)) {
2016+ force_nl = !remove_newlines;
2017+ while (sc_end > save_com && sc_end[-1] == '\n') {
2018+ sc_end--;
2019+ }
2020 }
2021 if (force_nl) { /* if we should insert a nl here, put
2022- * it into the buffer */
2023- force_nl = false;
2024- --line_no; /* this will be re-increased when the
2025- * newline is read from the buffer */
2026- *sc_end++ = '\n';
2027- *sc_end++ = ' ';
2028- if (verbose) /* print error msg if the line was
2029- * not already broken */
2030- diag2(0, "Line broken");
2031+ * it into the buffer */
2032+ force_nl = false;
2033+ --line_no; /* this will be re-increased when the
2034+ * newline is read from the buffer */
2035+ *sc_end++ = '\n';
2036+ *sc_end++ = ' ';
2037+ if (opt.verbose) /* print error msg if the line was
2038+ * not already broken */
2039+ diag2(0, "Line broken");
2040 }
2041 for (t_ptr = token; *t_ptr; ++t_ptr)
2042- *sc_end++ = *t_ptr;
2043-
2044- sw_buffer:
2045- ps.search_brace = false; /* stop looking for start of
2046- * stmt */
2047- bp_save = buf_ptr; /* save current input buffer */
2048- be_save = buf_end;
2049- buf_ptr = save_com; /* fix so that subsequent calls to
2050- * lexi will take tokens out of
2051- * save_com */
2052- *sc_end++ = ' ';/* add trailing blank, just in case */
2053- buf_end = sc_end;
2054- sc_end = NULL;
2055- break;
2056- }
2057- } /* end of switch */
2058- /*
2059- * We must make this check, just in case there was an unexpected
2060- * EOF.
2061- */
2062- if (type_code != 0) {
2063- /*
2064- * The only intended purpose of calling lexi() below is to
2065- * categorize the next token in order to decide whether to
2066- * continue buffering forthcoming tokens. Once the buffering
2067- * is over, lexi() will be called again elsewhere on all of
2068- * the tokens - this time for normal processing.
2069- *
2070- * Calling it for this purpose is a bug, because lexi() also
2071- * changes the parser state and discards leading whitespace,
2072- * which is needed mostly for comment-related considerations.
2073- *
2074- * Work around the former problem by giving lexi() a copy of
2075- * the current parser state and discard it if the call turned
2076- * out to be just a look ahead.
2077- *
2078- * Work around the latter problem by copying all whitespace
2079- * characters into the buffer so that the later lexi() call
2080- * will read them.
2081- */
2082- if (sc_end != NULL) {
2083- while (*buf_ptr == ' ' || *buf_ptr == '\t') {
2084- *sc_end++ = *buf_ptr++;
2085- if (sc_end >= &save_com[sc_size]) {
2086- errx(1, "input too long");
2087+ *sc_end++ = *t_ptr;
2088+
2089+ sw_buffer:
2090+ ps.search_brace = false; /* stop looking for
2091+ * start of stmt */
2092+ bp_save = buf_ptr; /* save current input
2093+ * buffer */
2094+ be_save = buf_end;
2095+ buf_ptr = save_com; /* fix so that
2096+ * subsequent calls to
2097+ * lexi will take tokens
2098+ * out of save_com */
2099+ *sc_end++ = ' '; /* add trailing blank,
2100+ * just in case */
2101+ buf_end = sc_end;
2102+ sc_end = NULL;
2103+ break;
2104+ }
2105+ } /* end of switch */
2106+ /*
2107+ * We must make this check, just in case there was an unexpected
2108+ * EOF.
2109+ */
2110+ if (type_code != 0) {
2111+ /*
2112+ * The only intended purpose of calling lexi() below is to
2113+ * categorize the next token in order to decide whether to
2114+ * continue buffering forthcoming tokens. Once the buffering
2115+ * is over, lexi() will be called again elsewhere on all of
2116+ * the tokens - this time for normal processing.
2117+ *
2118+ * Calling it for this purpose is a bug, because lexi() also
2119+ * changes the parser state and discards leading whitespace,
2120+ * which is needed mostly for comment-related considerations.
2121+ *
2122+ * Work around the former problem by giving lexi() a copy of
2123+ * the current parser state and discard it if the call turned
2124+ * out to be just a look ahead.
2125+ *
2126+ * Work around the latter problem by copying all whitespace
2127+ * characters into the buffer so that the later lexi() call
2128+ * will read them.
2129+ */
2130+ if (sc_end != NULL) {
2131+ while (*buf_ptr == ' ' || *buf_ptr == '\t') {
2132+ *sc_end++ = *buf_ptr++;
2133+ if (sc_end >= &save_com[sc_size]) {
2134+ errx(1, "input too long");
2135+ }
2136+ }
2137+ if (buf_ptr >= buf_end) {
2138+ fill_buffer();
2139+ }
2140+ }
2141+ transient_state = ps;
2142+ type_code = lexi(&transient_state); /* read another token */
2143+ if (type_code != newline && type_code != form_feed &&
2144+ type_code != comment && !transient_state.search_brace) {
2145+ ps = transient_state;
2146+ }
2147+ }
2148+ } /* end of while (search_brace) */
2149+ last_else = 0;
2150+check_type:
2151+ if (type_code == 0) { /* we got eof */
2152+ if (s_lab != e_lab || s_code != e_code
2153+ || s_com != e_com) /* must dump end of line */
2154+ dump_line();
2155+ if (ps.tos > 1) /* check for balanced braces */
2156+ diag(1, "Stuff missing from end of file.");
2157+
2158+ if (opt.verbose) {
2159+ printf("There were %d output lines and %d comments\n",
2160+ ps.out_lines, ps.out_coms);
2161+ printf("(Lines with comments)/(Lines with code): %6.3f\n",
2162+ (1.0 * ps.com_lines) / code_lines);
2163 }
2164- }
2165- if (buf_ptr >= buf_end) {
2166- fill_buffer();
2167- }
2168- }
2169- transient_state = ps;
2170- type_code = lexi(&transient_state); /* read another token */
2171- if (type_code != newline && type_code != form_feed &&
2172- type_code != comment && !transient_state.search_brace) {
2173- ps = transient_state;
2174+ fflush(output);
2175+ exit(found_err);
2176 }
2177- }
2178- } /* end of while (search_brace) */
2179- last_else = 0;
2180-check_type:
2181- if (type_code == 0) { /* we got eof */
2182- if (s_lab != e_lab || s_code != e_code
2183- || s_com != e_com) /* must dump end of line */
2184- dump_line();
2185- if (ps.tos > 1) /* check for balanced braces */
2186- diag2(1, "Stuff missing from end of file");
2187-
2188- if (verbose) {
2189- printf("There were %d output lines and %d comments\n",
2190- ps.out_lines, ps.out_coms);
2191- printf("(Lines with comments)/(Lines with code): %6.3f\n",
2192- (1.0 * ps.com_lines) / code_lines);
2193- }
2194- fflush(output);
2195- exit(found_err);
2196- }
2197- if (
2198- (type_code != comment) &&
2199- (type_code != newline) &&
2200- (type_code != preesc) &&
2201- (type_code != form_feed)) {
2202- if (force_nl &&
2203- (type_code != semicolon) &&
2204- (type_code != lbrace || !btype_2)) {
2205- /* we should force a broken line here */
2206- if (verbose)
2207- diag2(0, "Line broken");
2208- dump_line();
2209- ps.want_blank = false; /* dont insert blank at line start */
2210- force_nl = false;
2211- }
2212- ps.in_stmt = true; /* turn on flag which causes an extra level of
2213- * indentation. this is turned off by a ; or
2214- * '}' */
2215- if (s_com != e_com) { /* the turkey has embedded a comment
2216- * in a line. fix it */
2217- int len = e_com - s_com;
2218-
2219- CHECK_SIZE_CODE(len + 3);
2220- *e_code++ = ' ';
2221- memcpy(e_code, s_com, len);
2222- e_code += len;
2223- *e_code++ = ' ';
2224- *e_code = '\0'; /* null terminate code sect */
2225- ps.want_blank = false;
2226- e_com = s_com;
2227- }
2228- }
2229- else if (type_code != comment) /* preserve force_nl thru a comment */
2230- force_nl = false; /* cancel forced newline after newline, form
2231- * feed, etc */
2232-
2233-
2234-
2235- /*-----------------------------------------------------*\
2236- | do switch on type of token scanned |
2237- \*-----------------------------------------------------*/
2238- CHECK_SIZE_CODE(3); /* maximum number of increments of e_code
2239- * before the next CHECK_SIZE_CODE or
2240- * dump_line() is 2. After that there's the
2241- * final increment for the null character. */
2242- switch (type_code) { /* now, decide what to do with the token */
2243-
2244- case form_feed: /* found a form feed in line */
2245- ps.use_ff = true; /* a form feed is treated much like a newline */
2246- dump_line();
2247- ps.want_blank = false;
2248- break;
2249-
2250- case newline:
2251- if (ps.last_token != comma || ps.p_l_follow > 0
2252- || !ps.leave_comma || ps.block_init || !break_comma || s_com != e_com) {
2253- dump_line();
2254- ps.want_blank = false;
2255- }
2256- ++line_no; /* keep track of input line number */
2257- break;
2258-
2259- case lparen: /* got a '(' or '[' */
2260- /* count parens to make Healy happy */
2261- if (++ps.p_l_follow == nitems(ps.paren_indents)) {
2262- diag3(0, "Reached internal limit of %d unclosed parens",
2263- nitems(ps.paren_indents));
2264- ps.p_l_follow--;
2265- }
2266- if (*token == '[')
2267- /* not a function pointer declaration or a function call */;
2268- else if (ps.in_decl && !ps.block_init && !ps.dumped_decl_indent &&
2269- ps.procname[0] == '\0' && ps.paren_level == 0) {
2270- /* function pointer declarations */
2271- indent_declaration(dec_ind, tabs_to_var);
2272- ps.dumped_decl_indent = true;
2273- }
2274- else if (ps.want_blank &&
2275- ((ps.last_token != ident && ps.last_token != funcname) ||
2276- proc_calls_space ||
2277- /* offsetof (1) is never allowed a space; sizeof (2) gets
2278- * one iff -bs; all other keywords (>2) always get a space
2279- * before lparen */
2280- ps.keyword + Bill_Shannon > 2))
2281- *e_code++ = ' ';
2282- ps.want_blank = false;
2283- *e_code++ = token[0];
2284- ps.paren_indents[ps.p_l_follow - 1] = count_spaces_until(1, s_code, e_code) - 1;
2285- if (sp_sw && ps.p_l_follow == 1 && extra_expression_indent
2286- && ps.paren_indents[0] < 2 * ps.ind_size)
2287- ps.paren_indents[0] = 2 * ps.ind_size;
2288- if (ps.in_or_st && *token == '(' && ps.tos <= 2) {
2289- /*
2290- * this is a kluge to make sure that declarations will be
2291- * aligned right if proc decl has an explicit type on it, i.e.
2292- * "int a(x) {..."
2293- */
2294- parse(semicolon); /* I said this was a kluge... */
2295- ps.in_or_st = false; /* turn off flag for structure decl or
2296- * initialization */
2297- }
2298- /* parenthesized type following sizeof or offsetof is not a cast */
2299- if (ps.keyword == 1 || ps.keyword == 2)
2300- ps.not_cast_mask |= 1 << ps.p_l_follow;
2301- break;
2302-
2303- case rparen: /* got a ')' or ']' */
2304- if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.not_cast_mask) {
2305- ps.last_u_d = true;
2306- ps.cast_mask &= (1 << ps.p_l_follow) - 1;
2307- ps.want_blank = space_after_cast;
2308- } else
2309- ps.want_blank = true;
2310- ps.not_cast_mask &= (1 << ps.p_l_follow) - 1;
2311- if (--ps.p_l_follow < 0) {
2312- ps.p_l_follow = 0;
2313- diag3(0, "Extra %c", *token);
2314- }
2315- if (e_code == s_code) /* if the paren starts the line */
2316- ps.paren_level = ps.p_l_follow; /* then indent it */
2317-
2318- *e_code++ = token[0];
2319-
2320- if (sp_sw && (ps.p_l_follow == 0)) { /* check for end of if
2321- * (...), or some such */
2322- sp_sw = false;
2323- force_nl = true;/* must force newline after if */
2324- ps.last_u_d = true; /* inform lexi that a following
2325- * operator is unary */
2326- ps.in_stmt = false; /* dont use stmt continuation
2327- * indentation */
2328-
2329- parse(hd_type); /* let parser worry about if, or whatever */
2330- }
2331- ps.search_brace = btype_2; /* this should insure that constructs
2332- * such as main(){...} and int[]{...}
2333- * have their braces put in the right
2334- * place */
2335- break;
2336-
2337- case unary_op: /* this could be any unary operation */
2338- if (!ps.dumped_decl_indent && ps.in_decl && !ps.block_init &&
2339- ps.procname[0] == '\0' && ps.paren_level == 0) {
2340- /* pointer declarations */
2341+ if (
2342+ (type_code != comment) &&
2343+ (type_code != newline) &&
2344+ (type_code != preesc) &&
2345+ (type_code != form_feed)) {
2346+ if (force_nl &&
2347+ (type_code != semicolon) &&
2348+ (type_code != lbrace || !opt.btype_2)) {
2349+ /* we should force a broken line here */
2350+ if (opt.verbose)
2351+ diag(0, "Line broken");
2352+ dump_line();
2353+ ps.want_blank = false; /* don't insert blank at
2354+ * line start */
2355+ force_nl = false;
2356+ }
2357+ ps.in_stmt = true; /* turn on flag which causes
2358+ * an extra level of
2359+ * indentation. this is turned
2360+ * off by a ; or '}' */
2361+ if (s_com != e_com) { /* the turkey has embedded a
2362+ * comment in a line. fix it */
2363+ int len = e_com - s_com;
2364+
2365+ CHECK_SIZE_CODE(len + 3);
2366+ *e_code++ = ' ';
2367+ memcpy(e_code, s_com, len);
2368+ e_code += len;
2369+ *e_code++ = ' ';
2370+ *e_code = '\0'; /* null terminate code sect */
2371+ ps.want_blank = false;
2372+ e_com = s_com;
2373+ }
2374+ } else
2375+ if (type_code != comment) /* preserve force_nl
2376+ * thru a comment */
2377+ force_nl = false; /* cancel forced newline
2378+ * after newline, form
2379+ * feed, etc */
2380+
2381+
2382+
2383+ /*-----------------------------------------------------*\
2384+ | do switch on type of token scanned |
2385+ \*-----------------------------------------------------*/
2386+ CHECK_SIZE_CODE(3); /* maximum number of increments of e_code
2387+ * before the next CHECK_SIZE_CODE or
2388+ * dump_line() is 2. After that there's the
2389+ * final increment for the null character. */
2390+ switch (type_code) { /* now, decide what to do with the
2391+ * token */
2392+
2393+ case form_feed:/* found a form feed in line */
2394+ ps.use_ff = true; /* a form feed is treated much
2395+ * like a newline */
2396+ dump_line();
2397+ ps.want_blank = false;
2398+ break;
2399
2400- /*
2401- * if this is a unary op in a declaration, we should indent
2402- * this token
2403- */
2404- for (i = 0; token[i]; ++i)
2405- /* find length of token */;
2406- indent_declaration(dec_ind - i, tabs_to_var);
2407- ps.dumped_decl_indent = true;
2408- }
2409- else if (ps.want_blank)
2410- *e_code++ = ' ';
2411-
2412- {
2413- int len = e_token - s_token;
2414-
2415- CHECK_SIZE_CODE(len);
2416- memcpy(e_code, token, len);
2417- e_code += len;
2418- }
2419- ps.want_blank = false;
2420- break;
2421-
2422- case binary_op: /* any binary operation */
2423- {
2424- int len = e_token - s_token;
2425-
2426- CHECK_SIZE_CODE(len + 1);
2427- if (ps.want_blank)
2428- *e_code++ = ' ';
2429- memcpy(e_code, token, len);
2430- e_code += len;
2431- }
2432- ps.want_blank = true;
2433- break;
2434-
2435- case postop: /* got a trailing ++ or -- */
2436- *e_code++ = token[0];
2437- *e_code++ = token[1];
2438- ps.want_blank = true;
2439- break;
2440-
2441- case question: /* got a ? */
2442- squest++; /* this will be used when a later colon
2443- * appears so we can distinguish the
2444- * <c>?<n>:<n> construct */
2445- if (ps.want_blank)
2446- *e_code++ = ' ';
2447- *e_code++ = '?';
2448- ps.want_blank = true;
2449- break;
2450-
2451- case casestmt: /* got word 'case' or 'default' */
2452- scase = true; /* so we can process the later colon properly */
2453- goto copy_id;
2454-
2455- case colon: /* got a ':' */
2456- if (squest > 0) { /* it is part of the <c>?<n>: <n> construct */
2457- --squest;
2458- if (ps.want_blank)
2459- *e_code++ = ' ';
2460- *e_code++ = ':';
2461- ps.want_blank = true;
2462- break;
2463- }
2464- if (ps.in_or_st) {
2465- *e_code++ = ':';
2466- ps.want_blank = false;
2467- break;
2468- }
2469- ps.in_stmt = false; /* seeing a label does not imply we are in a
2470- * stmt */
2471- /*
2472- * turn everything so far into a label
2473- */
2474- {
2475- int len = e_code - s_code;
2476-
2477- CHECK_SIZE_LAB(len + 3);
2478- memcpy(e_lab, s_code, len);
2479- e_lab += len;
2480- *e_lab++ = ':';
2481- *e_lab = '\0';
2482- e_code = s_code;
2483- }
2484- force_nl = ps.pcase = scase; /* ps.pcase will be used by
2485- * dump_line to decide how to
2486- * indent the label. force_nl
2487- * will force a case n: to be
2488- * on a line by itself */
2489- scase = false;
2490- ps.want_blank = false;
2491- break;
2492-
2493- case semicolon: /* got a ';' */
2494- if (ps.dec_nest == 0)
2495- ps.in_or_st = false;/* we are not in an initialization or
2496- * structure declaration */
2497- scase = false; /* these will only need resetting in an error */
2498- squest = 0;
2499- if (ps.last_token == rparen)
2500- ps.in_parameter_declaration = 0;
2501- ps.cast_mask = 0;
2502- ps.not_cast_mask = 0;
2503- ps.block_init = 0;
2504- ps.block_init_level = 0;
2505- ps.just_saw_decl--;
2506-
2507- if (ps.in_decl && s_code == e_code && !ps.block_init &&
2508- !ps.dumped_decl_indent && ps.paren_level == 0) {
2509- /* indent stray semicolons in declarations */
2510- indent_declaration(dec_ind - 1, tabs_to_var);
2511- ps.dumped_decl_indent = true;
2512- }
2513-
2514- ps.in_decl = (ps.dec_nest > 0); /* if we were in a first level
2515- * structure declaration, we
2516- * arent any more */
2517-
2518- if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) {
2519+ case newline:
2520+ if (ps.last_token != comma || ps.p_l_follow > 0
2521+ || !opt.leave_comma || ps.block_init || !break_comma || s_com != e_com) {
2522+ dump_line();
2523+ ps.want_blank = false;
2524+ }
2525+ ++line_no; /* keep track of input line number */
2526+ break;
2527
2528- /*
2529- * This should be true iff there were unbalanced parens in the
2530- * stmt. It is a bit complicated, because the semicolon might
2531- * be in a for stmt
2532- */
2533- diag2(1, "Unbalanced parens");
2534- ps.p_l_follow = 0;
2535- if (sp_sw) { /* this is a check for an if, while, etc. with
2536- * unbalanced parens */
2537- sp_sw = false;
2538- parse(hd_type); /* dont lose the if, or whatever */
2539- }
2540- }
2541- *e_code++ = ';';
2542- ps.want_blank = true;
2543- ps.in_stmt = (ps.p_l_follow > 0); /* we are no longer in the
2544- * middle of a stmt */
2545-
2546- if (!sp_sw) { /* if not if for (;;) */
2547- parse(semicolon); /* let parser know about end of stmt */
2548- force_nl = true;/* force newline after an end of stmt */
2549- }
2550- break;
2551-
2552- case lbrace: /* got a '{' */
2553- ps.in_stmt = false; /* dont indent the {} */
2554- if (!ps.block_init)
2555- force_nl = true;/* force other stuff on same line as '{' onto
2556- * new line */
2557- else if (ps.block_init_level <= 0)
2558- ps.block_init_level = 1;
2559- else
2560- ps.block_init_level++;
2561-
2562- if (s_code != e_code && !ps.block_init) {
2563- if (!btype_2) {
2564- dump_line();
2565- ps.want_blank = false;
2566- }
2567- else if (ps.in_parameter_declaration && !ps.in_or_st) {
2568- ps.i_l_follow = 0;
2569- if (function_brace_split) { /* dump the line prior to the
2570- * brace ... */
2571- dump_line();
2572+ case lparen: /* got a '(' or '[' */
2573+ /* count parens to make Healy happy */
2574+ if (++ps.p_l_follow == nitems(ps.paren_indents)) {
2575+ diag3(0, "Reached internal limit of %d unclosed parens",
2576+ nitems(ps.paren_indents));
2577+ ps.p_l_follow--;
2578+ }
2579+
2580+ if (*token == '[')
2581+ /* not a function pointer declaration or a function call */;
2582+ else if (ps.in_decl && !ps.block_init && !ps.dumped_decl_indent &&
2583+ ps.procname[0] == '\0' && ps.paren_level == 0) {
2584+ /* function pointer declarations. */
2585+ indent_declaration(dec_ind, tabs_to_var);
2586+ ps.dumped_decl_indent = true;
2587+ }
2588+ else if (ps.want_blank &&
2589+ (ps.last_token != ident && ps.last_token != funcname ||
2590+ opt.proc_calls_space ||
2591+ /* offsetof (1) is never allowed a space; sizeof (2) gets
2592+ * one iff -bs; all other keywords (>2) always get a space
2593+ * before lparen */
2594+ ps.keyword + opt.Bill_Shannon > 2))
2595+ *e_code++ = ' ';
2596+
2597 ps.want_blank = false;
2598- } else /* add a space between the decl and brace */
2599- ps.want_blank = true;
2600- }
2601- }
2602- if (ps.in_parameter_declaration)
2603- prefix_blankline_requested = 0;
2604-
2605- if (ps.p_l_follow > 0) { /* check for preceding unbalanced
2606- * parens */
2607- diag2(1, "Unbalanced parens");
2608- ps.p_l_follow = 0;
2609- if (sp_sw) { /* check for unclosed if, for, etc. */
2610- sp_sw = false;
2611- parse(hd_type);
2612- ps.ind_level = ps.i_l_follow;
2613- }
2614- }
2615- if (s_code == e_code)
2616- ps.ind_stmt = false; /* dont put extra indentation on line
2617- * with '{' */
2618- if (ps.in_decl && ps.in_or_st) { /* this is either a structure
2619- * declaration or an init */
2620- di_stack[ps.dec_nest] = dec_ind;
2621- if (++ps.dec_nest == nitems(di_stack)) {
2622- diag3(0, "Reached internal limit of %d struct levels",
2623- nitems(di_stack));
2624- ps.dec_nest--;
2625- }
2626- /* ? dec_ind = 0; */
2627- }
2628- else {
2629- ps.decl_on_line = false; /* we can't be in the middle of
2630- * a declaration, so don't do
2631- * special indentation of
2632- * comments */
2633- if (blanklines_after_declarations_at_proctop
2634- && ps.in_parameter_declaration)
2635- postfix_blankline_requested = 1;
2636- ps.in_parameter_declaration = 0;
2637- ps.in_decl = false;
2638- }
2639- dec_ind = 0;
2640- parse(lbrace); /* let parser know about this */
2641- if (ps.want_blank) /* put a blank before '{' if '{' is not at
2642- * start of line */
2643- *e_code++ = ' ';
2644- ps.want_blank = false;
2645- *e_code++ = '{';
2646- ps.just_saw_decl = 0;
2647- break;
2648-
2649- case rbrace: /* got a '}' */
2650- if (ps.p_stack[ps.tos] == decl && !ps.block_init) /* semicolons can be
2651- * omitted in
2652- * declarations */
2653- parse(semicolon);
2654- if (ps.p_l_follow) {/* check for unclosed if, for, else. */
2655- diag2(1, "Unbalanced parens");
2656- ps.p_l_follow = 0;
2657- sp_sw = false;
2658- }
2659- ps.just_saw_decl = 0;
2660- ps.block_init_level--;
2661- if (s_code != e_code && !ps.block_init) { /* '}' must be first on
2662- * line */
2663- if (verbose)
2664- diag2(0, "Line broken");
2665- dump_line();
2666- }
2667- *e_code++ = '}';
2668- ps.want_blank = true;
2669- ps.in_stmt = ps.ind_stmt = false;
2670- if (ps.dec_nest > 0) { /* we are in multi-level structure
2671- * declaration */
2672- dec_ind = di_stack[--ps.dec_nest];
2673- if (ps.dec_nest == 0 && !ps.in_parameter_declaration)
2674- ps.just_saw_decl = 2;
2675- ps.in_decl = true;
2676- }
2677- prefix_blankline_requested = 0;
2678- parse(rbrace); /* let parser know about this */
2679- ps.search_brace = cuddle_else && ps.p_stack[ps.tos] == ifhead
2680- && ps.il[ps.tos] >= ps.ind_level;
2681- if (ps.tos <= 1 && blanklines_after_procs && ps.dec_nest <= 0)
2682- postfix_blankline_requested = 1;
2683- break;
2684-
2685- case swstmt: /* got keyword "switch" */
2686- sp_sw = true;
2687- hd_type = swstmt; /* keep this for when we have seen the
2688- * expression */
2689- goto copy_id; /* go move the token into buffer */
2690-
2691- case sp_paren: /* token is if, while, for */
2692- sp_sw = true; /* the interesting stuff is done after the
2693- * expression is scanned */
2694- hd_type = (*token == 'i' ? ifstmt :
2695- (*token == 'w' ? whilestmt : forstmt));
2696-
2697- /*
2698- * remember the type of header for later use by parser
2699- */
2700- goto copy_id; /* copy the token into line */
2701-
2702- case sp_nparen: /* got else, do */
2703- ps.in_stmt = false;
2704- if (*token == 'e') {
2705- if (e_code != s_code && (!cuddle_else || e_code[-1] != '}')) {
2706- if (verbose)
2707- diag2(0, "Line broken");
2708- dump_line();/* make sure this starts a line */
2709- ps.want_blank = false;
2710- }
2711- force_nl = true;/* also, following stuff must go onto new line */
2712- last_else = 1;
2713- parse(elselit);
2714- }
2715- else {
2716- if (e_code != s_code) { /* make sure this starts a line */
2717- if (verbose)
2718- diag2(0, "Line broken");
2719- dump_line();
2720- ps.want_blank = false;
2721- }
2722- force_nl = true;/* also, following stuff must go onto new line */
2723- last_else = 0;
2724- parse(dolit);
2725- }
2726- goto copy_id; /* move the token into line */
2727-
2728- case type_def:
2729- case storage:
2730- prefix_blankline_requested = 0;
2731- goto copy_id;
2732-
2733- case structure:
2734- if (ps.p_l_follow > 0)
2735- goto copy_id;
2736- case decl: /* we have a declaration type (int, etc.) */
2737- parse(decl); /* let parser worry about indentation */
2738- if (ps.last_token == rparen && ps.tos <= 1) {
2739- if (s_code != e_code) {
2740- dump_line();
2741- ps.want_blank = 0;
2742- }
2743- }
2744- if (ps.in_parameter_declaration && ps.indent_parameters && ps.dec_nest == 0) {
2745- ps.ind_level = ps.i_l_follow = 1;
2746- ps.ind_stmt = 0;
2747- }
2748- ps.in_or_st = true; /* this might be a structure or initialization
2749- * declaration */
2750- ps.in_decl = ps.decl_on_line = ps.last_token != type_def;
2751- if ( /* !ps.in_or_st && */ ps.dec_nest <= 0)
2752- ps.just_saw_decl = 2;
2753- prefix_blankline_requested = 0;
2754- for (i = 0; token[i++];); /* get length of token */
2755-
2756- if (ps.ind_level == 0 || ps.dec_nest > 0) {
2757- /* global variable or struct member in local variable */
2758- dec_ind = ps.decl_indent > 0 ? ps.decl_indent : i;
2759- tabs_to_var = (use_tabs ? ps.decl_indent > 0 : 0);
2760- } else {
2761- /* local variable */
2762- dec_ind = ps.local_decl_indent > 0 ? ps.local_decl_indent : i;
2763- tabs_to_var = (use_tabs ? ps.local_decl_indent > 0 : 0);
2764- }
2765- goto copy_id;
2766-
2767- case funcname:
2768- case ident: /* got an identifier or constant */
2769- if (ps.in_decl) {
2770- if (type_code == funcname) {
2771- ps.in_decl = false;
2772- if (procnames_start_line && s_code != e_code) {
2773- *e_code = '\0';
2774- dump_line();
2775- }
2776- else if (ps.want_blank) {
2777- *e_code++ = ' ';
2778- }
2779- ps.want_blank = false;
2780- }
2781- else if (!ps.block_init && !ps.dumped_decl_indent &&
2782- ps.paren_level == 0) { /* if we are in a declaration, we
2783- * must indent identifier */
2784- indent_declaration(dec_ind, tabs_to_var);
2785- ps.dumped_decl_indent = true;
2786- ps.want_blank = false;
2787- }
2788- }
2789- else if (sp_sw && ps.p_l_follow == 0) {
2790- sp_sw = false;
2791- force_nl = true;
2792- ps.last_u_d = true;
2793- ps.in_stmt = false;
2794- parse(hd_type);
2795- }
2796- copy_id:
2797- {
2798- int len = e_token - s_token;
2799-
2800- CHECK_SIZE_CODE(len + 1);
2801- if (ps.want_blank)
2802- *e_code++ = ' ';
2803- memcpy(e_code, s_token, len);
2804- e_code += len;
2805- }
2806- if (type_code != funcname)
2807- ps.want_blank = true;
2808- break;
2809-
2810- case strpfx:
2811- {
2812- int len = e_token - s_token;
2813-
2814- CHECK_SIZE_CODE(len + 1);
2815- if (ps.want_blank)
2816- *e_code++ = ' ';
2817- memcpy(e_code, token, len);
2818- e_code += len;
2819- }
2820- ps.want_blank = false;
2821- break;
2822-
2823- case period: /* treat a period kind of like a binary
2824- * operation */
2825- *e_code++ = '.'; /* move the period into line */
2826- ps.want_blank = false; /* dont put a blank after a period */
2827- break;
2828+ *e_code++ = token[0];
2829+ ps.paren_indents[ps.p_l_follow - 1] = count_spaces_until(1, s_code, e_code) - 1;
2830+ if (sp_sw && ps.p_l_follow == 1 && opt.extra_expression_indent
2831+ && ps.paren_indents[0] < 2 * opt.ind_size)
2832+ ps.paren_indents[0] = 2 * opt.ind_size;
2833+ if (ps.in_or_st && *token == '(' && ps.tos <= 2) {
2834+ /*
2835+ * this is a kluge to make sure that declarations will be
2836+ * aligned right if proc decl has an explicit type on it, i.e.
2837+ * "int a(x) {..."
2838+ */
2839+ parse(semicolon); /* I said this was a
2840+ * kluge... */
2841+ ps.in_or_st = false; /* turn off flag for
2842+ * structure decl or
2843+ * initialization */
2844+ }
2845+ /* parenthesized type following sizeof or offsetof is not a cast */
2846+ if (ps.sizeof_keyword == 1 || ps.keyboard == 2)
2847+ ps.not_cast_mask |= 1 << ps.p_l_follow;
2848+ break;
2849
2850- case comma:
2851- ps.want_blank = (s_code != e_code); /* only put blank after comma
2852- * if comma does not start the
2853+ case rparen: /* got a ')' or ']' */
2854+ if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.not_cast_mask) {
2855+ ps.last_u_d = true;
2856+ ps.cast_mask &= (1 << ps.p_l_follow) - 1;
2857+ ps.want_blank = opt.space_after_cast;
2858+ }
2859+ ps.not_cast_mask &= (1 << ps.p_l_follow) - 1;
2860+ if (--ps.p_l_follow < 0) {
2861+ ps.p_l_follow = 0;
2862+ diag(0, "Extra %c", *token);
2863+ }
2864+ if (e_code == s_code) /* if the paren starts the
2865 * line */
2866- if (ps.in_decl && ps.procname[0] == '\0' && !ps.block_init &&
2867- !ps.dumped_decl_indent && ps.paren_level == 0) {
2868- /* indent leading commas and not the actual identifiers */
2869- indent_declaration(dec_ind - 1, tabs_to_var);
2870- ps.dumped_decl_indent = true;
2871- }
2872- *e_code++ = ',';
2873- if (ps.p_l_follow == 0) {
2874- if (ps.block_init_level <= 0)
2875- ps.block_init = 0;
2876- if (break_comma && (!ps.leave_comma ||
2877- count_spaces_until(compute_code_target(), s_code, e_code) >
2878- max_col - tabsize))
2879- force_nl = true;
2880- }
2881- break;
2882-
2883- case preesc: /* got the character '#' */
2884- if ((s_com != e_com) ||
2885- (s_lab != e_lab) ||
2886- (s_code != e_code))
2887- dump_line();
2888- CHECK_SIZE_LAB(1);
2889- *e_lab++ = '#'; /* move whole line to 'label' buffer */
2890- {
2891- int in_comment = 0;
2892- int com_start = 0;
2893- char quote = 0;
2894- int com_end = 0;
2895-
2896- while (*buf_ptr == ' ' || *buf_ptr == '\t') {
2897- buf_ptr++;
2898- if (buf_ptr >= buf_end)
2899- fill_buffer();
2900- }
2901- while (*buf_ptr != '\n' || (in_comment && !had_eof)) {
2902- CHECK_SIZE_LAB(2);
2903- *e_lab = *buf_ptr++;
2904- if (buf_ptr >= buf_end)
2905- fill_buffer();
2906- switch (*e_lab++) {
2907- case BACKSLASH:
2908- if (!in_comment) {
2909- *e_lab++ = *buf_ptr++;
2910- if (buf_ptr >= buf_end)
2911- fill_buffer();
2912+ ps.paren_level = ps.p_l_follow; /* then indent it */
2913+
2914+ *e_code++ = token[0];
2915+ ps.want_blank = true;
2916+
2917+ if (sp_sw && (ps.p_l_follow == 0)) { /* check for end of if
2918+ * (...), or some such */
2919+ sp_sw = false;
2920+ force_nl = true; /* must force newline
2921+ * after if */
2922+ ps.last_u_d = true; /* inform lexi that a
2923+ * following operator is
2924+ * unary */
2925+ ps.in_stmt = false; /* don't use stmt
2926+ * continuation
2927+ * indentation */
2928+
2929+ parse(hd_type); /* let parser worry about if,
2930+ * or whatever */
2931 }
2932+ ps.search_brace = opt.btype_2; /* this should ensure
2933+ * that constructs such
2934+ * as main(){...} and
2935+ * int[]{...} have their
2936+ * braces put in the
2937+ * right place */
2938 break;
2939- case '/':
2940- if (*buf_ptr == '*' && !in_comment && !quote) {
2941- in_comment = 1;
2942- *e_lab++ = *buf_ptr++;
2943- com_start = e_lab - s_lab - 2;
2944+
2945+ case unary_op: /* this could be any unary operation */
2946+ if (!ps.dumped_decl_indent && ps.in_decl && !ps.block_init &&
2947+ !ps.procname[0] == '\0' && ps.paren_level == 0) {
2948+ /* pointer declarations */
2949+ /*
2950+ * if this is a unary op in a declaration, we should indent
2951+ * this token
2952+ */
2953+ for (i = 0; token[i]; ++i)
2954+ /* find length of token */;
2955+ indent_declaration(dec_ind - i, tabs_to_var);
2956+ ps.dumped_decl_indent = true;
2957+ } else if (ps.want_blank)
2958+ *e_code++ = ' ';
2959+ for (t_ptr = token; *t_ptr; ++t_ptr) {
2960+ CHECK_SIZE_CODE;
2961+ *e_code++ = *t_ptr;
2962 }
2963+ ps.want_blank = false;
2964 break;
2965- case '"':
2966- if (quote == '"')
2967- quote = 0;
2968+
2969+ case binary_op:/* any binary operation */
2970+ if (ps.want_blank)
2971+ *e_code++ = ' ';
2972+ for (t_ptr = token; *t_ptr; ++t_ptr) {
2973+ CHECK_SIZE_CODE;
2974+ *e_code++ = *t_ptr; /* move the operator */
2975+ }
2976+ ps.want_blank = true;
2977 break;
2978- case '\'':
2979- if (quote == '\'')
2980- quote = 0;
2981+
2982+ case postop: /* got a trailing ++ or -- */
2983+ *e_code++ = token[0];
2984+ *e_code++ = token[1];
2985+ ps.want_blank = true;
2986+ break;
2987+
2988+ case question: /* got a ? */
2989+ squest++; /* this will be used when a later
2990+ * colon appears so we can distinguish
2991+ * the <c>?<n>:<n> construct */
2992+ if (ps.want_blank)
2993+ *e_code++ = ' ';
2994+ *e_code++ = '?';
2995+ ps.want_blank = true;
2996 break;
2997- case '*':
2998- if (*buf_ptr == '/' && in_comment) {
2999- in_comment = 0;
3000- *e_lab++ = *buf_ptr++;
3001- com_end = e_lab - s_lab;
3002+
3003+ case casestmt: /* got word 'case' or 'default' */
3004+ scase = true; /* so we can process the later colon
3005+ * properly */
3006+ goto copy_id;
3007+
3008+ case colon: /* got a ':' */
3009+ if (squest > 0) { /* it is part of the <c>?<n>:
3010+ * <n> construct */
3011+ --squest;
3012+ if (ps.want_blank)
3013+ *e_code++ = ' ';
3014+ *e_code++ = ':';
3015+ ps.want_blank = true;
3016+ break;
3017+ }
3018+ if (ps.in_or_st) {
3019+ *e_code++ = ':';
3020+ ps.want_blank = false;
3021+ break;
3022 }
3023+ ps.in_stmt = false; /* seeing a label does not
3024+ * imply we are in a stmt */
3025+ for (t_ptr = s_code; *t_ptr; ++t_ptr)
3026+ *e_lab++ = *t_ptr; /* turn everything so
3027+ * far into a label */
3028+ e_code = s_code;
3029+ *e_lab++ = ':';
3030+ *e_lab = '\0';
3031+
3032+ force_nl = ps.pcase = scase; /* ps.pcase will be used
3033+ * by dump_line to
3034+ * decide how to indent
3035+ * the label. force_nl
3036+ * will force a case n:
3037+ * to be on a line by
3038+ * itself */
3039+ scase = false;
3040+ ps.want_blank = false;
3041 break;
3042- }
3043- }
3044
3045- while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
3046- e_lab--;
3047- if (e_lab - s_lab == com_end && bp_save == NULL) {
3048- /* comment on preprocessor line */
3049- if (sc_end == NULL) { /* if this is the first comment,
3050- * we must set up the buffer */
3051- save_com = sc_buf;
3052- sc_end = &save_com[0];
3053- }
3054- else {
3055- *sc_end++ = '\n'; /* add newline between
3056- * comments */
3057- *sc_end++ = ' ';
3058- --line_no;
3059- }
3060- if (sc_end - save_com + com_end - com_start > sc_size)
3061- errx(1, "input too long");
3062- memmove(sc_end, s_lab + com_start, com_end - com_start);
3063- sc_end += com_end - com_start;
3064- e_lab = s_lab + com_start;
3065- while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
3066- e_lab--;
3067- bp_save = buf_ptr; /* save current input buffer */
3068- be_save = buf_end;
3069- buf_ptr = save_com; /* fix so that subsequent calls to
3070- * lexi will take tokens out of
3071- * save_com */
3072- *sc_end++ = ' '; /* add trailing blank, just in case */
3073- buf_end = sc_end;
3074- sc_end = NULL;
3075- }
3076- CHECK_SIZE_LAB(1);
3077- *e_lab = '\0'; /* null terminate line */
3078- ps.pcase = false;
3079- }
3080-
3081- if (strncmp(s_lab, "#if", 3) == 0) { /* also ifdef, ifndef */
3082- if ((size_t)ifdef_level < nitems(state_stack)) {
3083- match_state[ifdef_level].tos = -1;
3084- state_stack[ifdef_level++] = ps;
3085- }
3086- else
3087- diag2(1, "#if stack overflow");
3088- }
3089- else if (strncmp(s_lab, "#el", 3) == 0) { /* else, elif */
3090- if (ifdef_level <= 0)
3091- diag2(1, s_lab[3] == 'i' ? "Unmatched #elif" : "Unmatched #else");
3092- else {
3093- match_state[ifdef_level - 1] = ps;
3094- ps = state_stack[ifdef_level - 1];
3095- }
3096- }
3097- else if (strncmp(s_lab, "#endif", 6) == 0) {
3098- if (ifdef_level <= 0)
3099- diag2(1, "Unmatched #endif");
3100- else
3101- ifdef_level--;
3102- } else {
3103- struct directives {
3104- int size;
3105- const char *string;
3106- }
3107- recognized[] = {
3108- {7, "include"},
3109- {6, "define"},
3110- {5, "undef"},
3111- {4, "line"},
3112- {5, "error"},
3113- {6, "pragma"}
3114- };
3115- int d = nitems(recognized);
3116- while (--d >= 0)
3117- if (strncmp(s_lab + 1, recognized[d].string, recognized[d].size) == 0)
3118+ case semicolon:/* got a ';' */
3119+ if (ps.dec_nest == 0) {
3120+ ps.in_or_st = false; /* we are not in an initialization
3121+ * or structure declaration */
3122+
3123+ scase = false; /* these will only need resetting in a
3124+ * error */
3125+ squest = 0;
3126+ if (ps.last_token == rparen)
3127+ ps.in_parameter_declaration = 0;
3128+ ps.cast_mask = 0;
3129+ ps.not_cast_mask = 0;
3130+ ps.block_init = 0;
3131+ ps.block_init_level = 0;
3132+ ps.just_saw_decl--;
3133+
3134+ if (ps.in_decl && s_code == e_code && !ps.block_init &&
3135+ !ps.dumped_decl_indent && ps.paren_level == 0) {
3136+ /* indent stray semicolons in declarations */
3137+ indent_declaration(dec_ind - 1, tabs_to_var);
3138+ ps.dumped_decl_indent = true;
3139+ }
3140+
3141+ ps.in_decl = (ps.dec_nest > 0); /* if we were in a first
3142+ * level structure
3143+ * declaration, we
3144+ * aren't any more */
3145+
3146+ if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) {
3147+
3148+ /*
3149+ * This should be true iff there were unbalanced parens in the
3150+ * stmt. It is a bit complicated, because the semicolon might
3151+ * be in a for stmt
3152+ */
3153+ diag(1, "Unbalanced parens");
3154+ ps.p_l_follow = 0;
3155+ if (sp_sw) { /* this is a check for a if,
3156+ * while, etc. with unbalanced
3157+ * parens */
3158+ sp_sw = false;
3159+ parse(hd_type); /* don't lose the if,
3160+ * or whatever */
3161+ }
3162+ }
3163+ *e_code++ = ';';
3164+ ps.want_blank = true;
3165+ ps.in_stmt = (ps.p_l_follow > 0); /* we are no longer in
3166+ * the middle of a stmt */
3167+
3168+ if (!sp_sw) { /* if not if for (;;) */
3169+ parse(semicolon); /* let parser know about
3170+ * end of stmt */
3171+ force_nl = true; /* force newline after a
3172+ * end of stmt */
3173+ }
3174 break;
3175- if (d < 0) {
3176- diag2(1, "Unrecognized cpp directive");
3177- break;
3178- }
3179- }
3180- if (blanklines_around_conditional_compilation) {
3181- postfix_blankline_requested++;
3182- n_real_blanklines = 0;
3183- }
3184- else {
3185- postfix_blankline_requested = 0;
3186- prefix_blankline_requested = 0;
3187- }
3188- break; /* subsequent processing of the newline
3189+
3190+ case lbrace: /* got a '{' */
3191+ ps.in_stmt = false; /* don't indent the {} */
3192+ if (!ps.block_init)
3193+ force_nl = true; /* force other stuff on
3194+ * same line as '{' onto
3195+ * new line */
3196+ else
3197+ if (ps.block_init_level <= 0)
3198+ ps.block_init_level = 1;
3199+ else
3200+ ps.block_init_level++;
3201+
3202+ if (s_code != e_code && !ps.block_init) {
3203+ if (!opt.btype_2) {
3204+ dump_line();
3205+ ps.want_blank = false;
3206+ } else
3207+ if (ps.in_parameter_declaration && !ps.in_or_st) {
3208+ ps.i_l_follow = 0;
3209+ if (opt.function_brace_split) { /* dump the line prior
3210+ * to the brace ... */
3211+ dump_line();
3212+ ps.want_blank = false;
3213+ } else /*add a space between the decl and brace */
3214+ ps.want_blank = false;
3215+
3216+ }
3217+ }
3218+ if (ps.in_parameter_declaration)
3219+ prefix_blankline_requested = 0;
3220+
3221+ if (ps.p_l_follow > 0) { /* check for preceding
3222+ * unbalanced parens */
3223+ diag(1, "Unbalanced parens");
3224+ ps.p_l_follow = 0;
3225+ if (sp_sw) { /* check for unclosed if, for,
3226+ * etc. */
3227+ sp_sw = false;
3228+ parse(hd_type);
3229+ ps.ind_level = ps.i_l_follow;
3230+ }
3231+ }
3232+ if (s_code == e_code)
3233+ ps.ind_stmt = false; /* don't put extra
3234+ * indentation on line
3235+ * with '{' */
3236+ if (ps.in_decl && ps.in_or_st) { /* this is either a
3237+ * structure declaration
3238+ * or an init */
3239+ di_stack[ps.dec_nest] = dec_ind;
3240+ if (++ps.dec_nest == nitems(di_stack)) {
3241+ diag3(0, "Reached internal limit of %d struct levels",
3242+ nitems(di_stack));
3243+ ps.dec_nest--;
3244+ }
3245+ /* ? dec_ind = 0; */
3246+ } else {
3247+ ps.decl_on_line = false; /* we can't be in the
3248+ * middle of a
3249+ * declaration, so don't
3250+ * do special
3251+ * indentation of
3252+ * comments */
3253+ if (opt.blanklines_after_declarations_at_proctop
3254+ && ps.in_parameter_declaration)
3255+ postfix_blankline_requested = 1;
3256+ ps.in_parameter_declaration = 0;
3257+ ps.in_decl = false;
3258+ }
3259+ dec_ind = 0;
3260+ parse(lbrace); /* let parser know about this */
3261+ if (ps.want_blank) /* put a blank before '{' if
3262+ * '{' is not at start of line */
3263+ *e_code++ = ' ';
3264+ ps.want_blank = false;
3265+ *e_code++ = '{';
3266+ ps.just_saw_decl = 0;
3267+ break;
3268+
3269+ case rbrace: /* got a '}' */
3270+ if (ps.p_stack[ps.tos] == decl && !ps.block_init) /* semicolons can be
3271+ * omitted in
3272+ * declarations */
3273+ parse(semicolon);
3274+ if (ps.p_l_follow) { /* check for unclosed if, for,
3275+ * else. */
3276+ diag(1, "Unbalanced parens");
3277+ ps.p_l_follow = 0;
3278+ sp_sw = false;
3279+ }
3280+ ps.just_saw_decl = 0;
3281+ ps.block_init_level--;
3282+ if (s_code != e_code && !ps.block_init) { /* '}' must be first on
3283+ * line */
3284+ if (opt.verbose)
3285+ diag(0, "Line broken");
3286+ dump_line();
3287+ }
3288+ *e_code++ = '}';
3289+ ps.want_blank = true;
3290+ ps.in_stmt = ps.ind_stmt = false;
3291+ if (ps.dec_nest > 0) { /* we are in multi-level
3292+ * structure declaration */
3293+ dec_ind = di_stack[--ps.dec_nest];
3294+ if (ps.dec_nest == 0 && !ps.in_parameter_declaration)
3295+ ps.just_saw_decl = 2;
3296+ ps.in_decl = true;
3297+ }
3298+ prefix_blankline_requested = 0;
3299+ parse(rbrace); /* let parser know about this */
3300+ ps.search_brace = opt.cuddle_else && ps.p_stack[ps.tos] == ifhead
3301+ && ps.il[ps.tos] >= ps.ind_level;
3302+ if (ps.tos <= 1 && opt.blanklines_after_procs && ps.dec_nest <= 0)
3303+ postfix_blankline_requested = 1;
3304+ break;
3305+
3306+ case swstmt: /* got keyword "switch" */
3307+ sp_sw = true;
3308+ hd_type = swstmt; /* keep this for when we have
3309+ * seen the expression */
3310+ goto copy_id; /* go move the token into buffer */
3311+
3312+ case sp_paren: /* token is if, while, for */
3313+ sp_sw = true; /* the interesting stuff is done after
3314+ * the expression is scanned */
3315+ hd_type = (*token == 'i' ? ifstmt :
3316+ (*token == 'w' ? whilestmt : forstmt));
3317+
3318+ /*
3319+ * remember the type of header for later use by parser
3320+ */
3321+ goto copy_id; /* copy the token into line */
3322+
3323+ case sp_nparen:/* got else, do */
3324+ ps.in_stmt = false;
3325+ if (*token == 'e') {
3326+ if (e_code != s_code && (!opt.cuddle_else || e_code[-1] != '}')) {
3327+ if (opt.verbose)
3328+ diag(0, "Line broken");
3329+ dump_line(); /* make sure this starts
3330+ * a line */
3331+ ps.want_blank = false;
3332+ }
3333+ force_nl = true; /* also, following stuff
3334+ * must go onto new line */
3335+ last_else = 1;
3336+ parse(elselit);
3337+ } else {
3338+ if (e_code != s_code) { /* make sure this starts
3339+ * a line */
3340+ if (opt.verbose)
3341+ diag(0, "Line broken");
3342+ dump_line();
3343+ ps.want_blank = false;
3344+ }
3345+ force_nl = true; /* also, following stuff
3346+ * must go onto new line */
3347+ last_else = 0;
3348+ parse(dolit);
3349+ }
3350+ goto copy_id; /* move the token into line */
3351+
3352+ case type_def;
3353+ case storage:
3354+ prefix_blankline_requested = 0;
3355+ goto copy_id;
3356+ case structure:
3357+ if (ps.p_l_follow > 0)
3358+ goto copy_id;
3359+
3360+ case decl:
3361+ parse(decl); /* let parser worry about indentation */
3362+ if (ps.last_token == rparen && ps.tos <= 1) {
3363+ if (s_code != e_code) {
3364+ dump_line();
3365+ ps.want_blank = 0;
3366+ }
3367+ }
3368+ if (ps.in_parameter_declaration && opt.indent_parameters && ps.dec_nest == 0) {
3369+ ps.ind_level = ps.i_l_follow = 1;
3370+ ps.ind_stmt = 0;
3371+ }
3372+ ps.in_or_st = true; /* this might be a structure
3373+ * or initialization
3374+ * declaration */
3375+ ps.in_decl = ps.decl_on_line = ps.last_token != type_def;
3376+ if ( /* !ps.in_or_st && */ ps.dec_nest <= 0)
3377+ ps.just_saw_decl = 2;
3378+ prefix_blankline_requested = 0;
3379+ for (i = 0; token[i++];); /* get length of token */
3380+
3381+ /*
3382+ * dec_ind = e_code - s_code + (ps.decl_indent>i ? ps.decl_indent
3383+ * : i);
3384+ */
3385+ if (ps.ind_level == 0 || ps.dec_nest > 0) {
3386+ /* global variable or struct member in local variable */
3387+ dec_ind = opt.decl_indent > 0 ? opt.decl_indent : i;
3388+ tabs_to_var = (opt.use_tabs ? opt.decl_indent > 0 : 0);
3389+ } else {
3390+ /* local variable */
3391+ dec_ind = opt.local_decl_indent > 0 ? opt.local_decl_indent : i;
3392+ tabs_to_var = (opt.use_tabs ? opt.local_decl_indent > 0 : 0);
3393+ }
3394+ goto copy_id;
3395+
3396+ case funcname:
3397+ case ident: /* got an identifier or constant */
3398+ if (ps.in_decl) {
3399+ if (type_code == funcname) {
3400+ ps.in_decl = false;
3401+ if (opt.procnames_start_line && s_code != e_code) {
3402+ *e_code = '\0';
3403+ dump_line();
3404+ }
3405+ else if (ps.want_blank) {
3406+ *e_code++ = ' ';
3407+ }
3408+ ps.want_blank = false;
3409+ }
3410+ else if (!ps.block_init && !ps.dumped_decl_indent &&
3411+ ps.paren_level == 0) { /* if we are in a declaration, we
3412+ * must indent identifier */
3413+
3414+ indent_declaration(dec_ind, tabs_to_var);
3415+ ps.dumped_decl_indent = true;
3416+ ps.want_blank = false;
3417+ }
3418+ } else
3419+ if (sp_sw && ps.p_l_follow == 0) {
3420+ sp_sw = false;
3421+ force_nl = true;
3422+ ps.last_u_d = true;
3423+ ps.in_stmt = false;
3424+ parse(hd_type);
3425+ }
3426+ copy_id:
3427+ if (ps.want_blank)
3428+ *e_code++ = ' ';
3429+ for (t_ptr = token; *t_ptr; ++t_ptr) {
3430+ CHECK_SIZE_CODE;
3431+ *e_code++ = *t_ptr;
3432+ }
3433+ if (type_code != funcname)
3434+ ps.want_blank = true;
3435+ break;
3436+
3437+ case strpfx:
3438+ if (ps.want_blank)
3439+ *e_code++ = ' ';
3440+ for (t_ptr = token; *t_ptr; ++t_ptr) {
3441+ CHECK_SIZE_CODE;
3442+ *e_code++ = *t_ptr;
3443+ }
3444+ ps.want_blank = false;
3445+ break;
3446+
3447+ case period: /* treat a period kind of like a binary
3448+ * operation */
3449+ *e_code++ = '.'; /* move the period into line */
3450+ ps.want_blank = false; /* don't put a blank after a
3451+ * period */
3452+ break;
3453+
3454+ case comma:
3455+ ps.want_blank = (s_code != e_code); /* only put blank after
3456+ * comma if comma does
3457+ * not start the line */
3458+ if (ps.in_decl && ps.procname == '\0' && !ps.block_init &&
3459+ !ps.dumped_decl_indent && ps.paren_level == 0) {
3460+ /* indent leading commas and not the actual identifiers */
3461+ indent_declaration(dec_ind - 1, tabs_to_var);
3462+ ps.dumped_decl_indent = true;
3463+ }
3464+ *e_code++ = ',';
3465+ if (ps.p_l_follow == 0) {
3466+ if (ps.block_init_level <= 0)
3467+ ps.block_init = 0;
3468+ if (break_comma && (!opt.leave_comma ||
3469+ count_spaces_until(compute_code_target(), s_code, e_code) >
3470+ opt.max_col - opt.tabsize))
3471+ force_nl = true;
3472+ }
3473+ break;
3474+
3475+ case preesc: /* got the character '#' */
3476+ if ((s_com != e_com) ||
3477+ (s_lab != e_lab) ||
3478+ (s_code != e_code))
3479+ dump_line();
3480+ *e_lab++ = '#'; /* move whole line to 'label' buffer */
3481+ {
3482+ int in_comment = 0;
3483+ int com_start = 0;
3484+ char quote = 0;
3485+ int com_end = 0;
3486+
3487+ while (*buf_ptr == ' ' || *buf_ptr == '\t') {
3488+ buf_ptr++;
3489+ if (buf_ptr >= buf_end)
3490+ fill_buffer();
3491+ }
3492+ while (*buf_ptr != '\n' || in_comment) {
3493+ CHECK_SIZE_LAB;
3494+ *e_lab = *buf_ptr++;
3495+ if (buf_ptr >= buf_end)
3496+ fill_buffer();
3497+ switch (*e_lab++) {
3498+ case BACKSLASH:
3499+ if (!in_comment) {
3500+ *e_lab++ = *buf_ptr++;
3501+ if (buf_ptr >= buf_end)
3502+ fill_buffer();
3503+ }
3504+ break;
3505+ case '/':
3506+ if (*buf_ptr == '*' && !in_comment && !quote) {
3507+ in_comment = 1;
3508+ *e_lab++ = *buf_ptr++;
3509+ com_start = e_lab - s_lab - 2;
3510+ }
3511+ break;
3512+ case '"':
3513+ if (quote == '"')
3514+ quote = 0;
3515+ break;
3516+ case '\'':
3517+ if (quote == '\'')
3518+ quote = 0;
3519+ break;
3520+ case '*':
3521+ if (*buf_ptr == '/' && in_comment) {
3522+ in_comment = 0;
3523+ *e_lab++ = *buf_ptr++;
3524+ com_end = e_lab - s_lab;
3525+ }
3526+ break;
3527+ }
3528+ }
3529+
3530+ while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
3531+ e_lab--;
3532+ if (e_lab - s_lab == com_end && bp_save == NULL) {
3533+ /* comment on preprocessor line */
3534+ if (sc_end == NULL) { /* if this is the first
3535+ * comment, we must set
3536+ * up the buffer */
3537+ save_com = sc_buf;
3538+ sc_end = &(save_com[0]);
3539+ }
3540+ else {
3541+ *sc_end++ = '\n'; /* add newline between
3542+ * comments */
3543+ *sc_end++ = ' ';
3544+ --line_no;
3545+ }
3546+ if (sc_end - save_com + com_end - com_start > sc_size)
3547+ errx(1, "input too long");
3548+ memmove(sc_end, s_lab + com_start, com_end - com_start);
3549+ sc_end += com_end - com_start;
3550+
3551+ e_lab = s_lab + com_start;
3552+ while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
3553+ e_lab--;
3554+ bp_save = buf_ptr; /* save current input
3555+ * buffer */
3556+ be_save = buf_end;
3557+ buf_ptr = save_com; /* fix so that
3558+ * subsequent calls to
3559+ * lexi will take tokens
3560+ * out of save_com */
3561+ *sc_end++ = ' '; /* add trailing blank,
3562+ * just in case */
3563+ buf_end = sc_end;
3564+ sc_end = NULL;
3565+ }
3566+ *e_lab = '\0'; /* null terminate line */
3567+ ps.pcase = false;
3568+ }
3569+
3570+ if (strncmp(s_lab, "#if", 3) == 0) { /* also ifdef, ifndef */
3571+ if (ifdef_level < (int)(sizeof state_stack / sizeof state_stack[0])) {
3572+ match_state[ifdef_level].tos = -1;
3573+ state_stack[ifdef_level++] = ps;
3574+ } else
3575+ diag(1, "#if stack overflow");
3576+ } else
3577+ if (strncmp(s_lab, "#el", 3) == 0) { /* else, elif */
3578+ if (ifdef_level <= 0)
3579+ diag(1, s_lab[3] == 'i' ? "Unmatched #elif" : "Unmatched #else");
3580+ else {
3581+ match_state[ifdef_level - 1] = ps;
3582+ ps = state_stack[ifdef_level - 1];
3583+ }
3584+ } else
3585+ if (strncmp(s_lab, "#endif", 6) == 0) {
3586+ if (ifdef_level <= 0)
3587+ diag(1, "Unmatched #endif");
3588+ else
3589+ ifdef_level--;
3590+ } else {
3591+ struct directives {
3592+ int size;
3593+ const char *string;
3594+ }
3595+ recognized[] = {
3596+ {7, "include"},
3597+ {6, "define"},
3598+ {5, "undef"},
3599+ {4, "line"},
3600+ {5, "error"},
3601+ {6, "pragma"}
3602+ };
3603+ int d = nitems(recognized);
3604+ while (--d >= 0)
3605+ if (strncmp(s_lab + 1, recognized[d].string, recognized[d].size) == 0)
3606+ break;
3607+ if (d < 0) {
3608+ diag2(1, "Unrecognized cpp directive");
3609+ break;
3610+ }
3611+ }
3612+ if (opt.blanklines_around_conditional_compilation) {
3613+ postfix_blankline_requested++;
3614+ n_real_blanklines = 0;
3615+ }
3616+ else {
3617+ postfix_blankline_requested = 0;
3618+ prefix_blankline_requested = 0;
3619+ }
3620+ break; /* subsequent processing of the newline
3621 * character will cause the line to be printed */
3622
3623- case comment: /* we have gotten a / followed by * this is a biggie */
3624- pr_comment();
3625- break;
3626- } /* end of big switch stmt */
3627+ case comment: /* we have gotten a start comment */
3628+ /* this is a biggie */
3629+ pr_comment();
3630+ break;
3631+ } /* end of big switch stmt */
3632
3633- *e_code = '\0'; /* make sure code section is null terminated */
3634- if (type_code != comment && type_code != newline && type_code != preesc)
3635- ps.last_token = type_code;
3636- } /* end of main while (1) loop */
3637+ *e_code = '\0'; /* make sure code section is null terminated */
3638+ if (type_code != comment && type_code != newline && type_code != preesc)
3639+ ps.last_token = type_code;
3640+ } /* end of main while (1) loop */
3641 }
3642-
3643 /*
3644 * copy input file to backup file if in_name is /blah/blah/blah/file, then
3645 * backup file will be ".Bfile" then make the backup file the input and
3646@@ -1198,41 +1292,40 @@ check_type:
3647 static void
3648 bakcopy(void)
3649 {
3650- int n,
3651- bakchn;
3652- char buff[8 * 1024];
3653- const char *p;
3654-
3655- /* construct file name .Bfile */
3656- for (p = in_name; *p; p++); /* skip to end of string */
3657- while (p > in_name && *p != '/') /* find last '/' */
3658- p--;
3659- if (*p == '/')
3660- p++;
3661- sprintf(bakfile, "%s.BAK", p);
3662-
3663- /* copy in_name to backup file */
3664- bakchn = creat(bakfile, 0600);
3665- if (bakchn < 0)
3666- err(1, "%s", bakfile);
3667- while ((n = read(fileno(input), buff, sizeof(buff))) > 0)
3668- if (write(bakchn, buff, n) != n)
3669- err(1, "%s", bakfile);
3670- if (n < 0)
3671- err(1, "%s", in_name);
3672- close(bakchn);
3673- fclose(input);
3674-
3675- /* re-open backup file as the input file */
3676- input = fopen(bakfile, "r");
3677- if (input == NULL)
3678- err(1, "%s", bakfile);
3679- /* now the original input file will be the output */
3680- output = fopen(in_name, "w");
3681- if (output == NULL) {
3682- unlink(bakfile);
3683- err(1, "%s", in_name);
3684- }
3685+ int n, bakchn;
3686+ char buff[8 * 1024];
3687+ const char *p;
3688+
3689+ /* construct file name .Bfile */
3690+ for (p = in_name; *p; p++); /* skip to end of string */
3691+ while (p > in_name && *p != '/') /* find last '/' */
3692+ p--;
3693+ if (*p == '/')
3694+ p++;
3695+ sprintf(bakfile, "%s.BAK", p);
3696+
3697+ /* copy in_name to backup file */
3698+ bakchn = creat(bakfile, 0600);
3699+ if (bakchn < 0)
3700+ err(1, "%s", bakfile);
3701+ while ((n = read(fileno(input), buff, sizeof buff)) > 0)
3702+ if (write(bakchn, buff, n) != n)
3703+ err(1, "%s", bakfile);
3704+ if (n < 0)
3705+ err(1, "%s", in_name);
3706+ close(bakchn);
3707+ fclose(input);
3708+
3709+ /* re-open backup file as the input file */
3710+ input = fopen(bakfile, "r");
3711+ if (input == 0)
3712+ err(1, "%s", bakfile);
3713+ /* now the original input file will be the output */
3714+ output = fopen(in_name, "w");
3715+ if (output == NULL) {
3716+ unlink(bakfile);
3717+ err(1, "%s", in_name);
3718+ }
3719 }
3720
3721 static void
3722@@ -1244,21 +1337,21 @@ indent_declaration(int cur_dec_ind, int tabs_to_var)
3723 /*
3724 * get the tab math right for indentations that are not multiples of tabsize
3725 */
3726- if ((ps.ind_level * ps.ind_size) % tabsize != 0) {
3727- pos += (ps.ind_level * ps.ind_size) % tabsize;
3728- cur_dec_ind += (ps.ind_level * ps.ind_size) % tabsize;
3729+ if ((ps.ind_level * opt.ind_size) % opt.tabsize != 0) {
3730+ pos += (ps.ind_level * opt.ind_size) % opt.tabsize;
3731+ cur_dec_ind += (ps.ind_level * opt.ind_size) % opt.tabsize;
3732 }
3733 if (tabs_to_var) {
3734- int tpos;
3735-
3736- CHECK_SIZE_CODE(cur_dec_ind / tabsize);
3737- while ((tpos = tabsize * (1 + pos / tabsize)) <= cur_dec_ind) {
3738- *e_code++ = '\t';
3739- pos = tpos;
3740+ int tpos;
3741+
3742+ CHECK_SIZE_CODE(cur_dec_ind / opt.tabsize);
3743+ while ((tpos = opt.tabsize * (1 + pos / opt.tabsize)) <= cur_dec_ind) {
3744+ *e_code++ = '\t';
3745+ pos = tpos;
3746+ }
3747 }
3748- }
3749- CHECK_SIZE_CODE(cur_dec_ind - pos + 1);
3750 while (pos < cur_dec_ind) {
3751+ CHECK_SIZE_CODE;
3752 *e_code++ = ' ';
3753 pos++;
3754 }
3755@@ -1267,3 +1360,4 @@ indent_declaration(int cur_dec_ind, int tabs_to_var)
3756 ps.want_blank = false;
3757 }
3758 }
3759+
3760diff --git a/indent_codes.h b/indent_codes.h
3761index 24c43fa..abf83df 100644
3762--- a/indent_codes.h
3763+++ b/indent_codes.h
3764@@ -1,7 +1,39 @@
3765-/*-
3766- * Copyright (c) 1985 Sun Microsystems, Inc.
3767+/* $NetBSD: indent_codes.h,v 1.5 2003/08/07 11:14:08 agc Exp $ */
3768+
3769+/*
3770 * Copyright (c) 1980, 1993
3771 * The Regents of the University of California. All rights reserved.
3772+ *
3773+ * Redistribution and use in source and binary forms, with or without
3774+ * modification, are permitted provided that the following conditions
3775+ * are met:
3776+ * 1. Redistributions of source code must retain the above copyright
3777+ * notice, this list of conditions and the following disclaimer.
3778+ * 2. Redistributions in binary form must reproduce the above copyright
3779+ * notice, this list of conditions and the following disclaimer in the
3780+ * documentation and/or other materials provided with the distribution.
3781+ * 3. Neither the name of the University nor the names of its contributors
3782+ * may be used to endorse or promote products derived from this software
3783+ * without specific prior written permission.
3784+ *
3785+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
3786+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3787+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3788+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3789+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3790+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3791+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3792+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3793+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3794+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3795+ * SUCH DAMAGE.
3796+ *
3797+ * from: @(#)indent_codes.h 8.1 (Berkeley) 6/6/93
3798+ */
3799+
3800+/*
3801+ * Copyright (c) 1985 Sun Microsystems, Inc.
3802+ * Copyright (c) 1976 Board of Trustees of the University of Illinois.
3803 * All rights reserved.
3804 *
3805 * Redistribution and use in source and binary forms, with or without
3806@@ -32,8 +64,7 @@
3807 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3808 * SUCH DAMAGE.
3809 *
3810- * @(#)indent_codes.h 8.1 (Berkeley) 6/6/93
3811- * $FreeBSD: head/usr.bin/indent/indent_codes.h 309380 2016-12-02 01:25:51Z pfg $
3812+ * from: @(#)indent_codes.h 8.1 (Berkeley) 6/6/93
3813 */
3814
3815 #define newline 1
3816@@ -68,8 +99,8 @@
3817 #define ifhead 30
3818 #define elsehead 31
3819 #define period 32
3820-#define strpfx 33
3821-#define storage 34
3822-#define funcname 35
3823-#define type_def 36
3824-#define structure 37
3825+#define strpfx 33
3826+#define storage 34
3827+#define funcname 35
3828+#define type_def 36
3829+#define structure 37
3830\ No newline at end of file
3831diff --git a/indent_globs.h b/indent_globs.h
3832index 6561ddb..c57736b 100644
3833--- a/indent_globs.h
3834+++ b/indent_globs.h
3835@@ -1,7 +1,39 @@
3836-/*-
3837- * Copyright (c) 1985 Sun Microsystems, Inc.
3838+/* $NetBSD: indent_globs.h,v 1.10 2014/09/04 04:06:07 mrg Exp $ */
3839+
3840+/*
3841 * Copyright (c) 1980, 1993
3842 * The Regents of the University of California. All rights reserved.
3843+ *
3844+ * Redistribution and use in source and binary forms, with or without
3845+ * modification, are permitted provided that the following conditions
3846+ * are met:
3847+ * 1. Redistributions of source code must retain the above copyright
3848+ * notice, this list of conditions and the following disclaimer.
3849+ * 2. Redistributions in binary form must reproduce the above copyright
3850+ * notice, this list of conditions and the following disclaimer in the
3851+ * documentation and/or other materials provided with the distribution.
3852+ * 3. Neither the name of the University nor the names of its contributors
3853+ * may be used to endorse or promote products derived from this software
3854+ * without specific prior written permission.
3855+ *
3856+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
3857+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3858+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3859+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3860+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3861+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3862+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3863+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3864+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3865+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3866+ * SUCH DAMAGE.
3867+ *
3868+ * from: @(#)indent_globs.h 8.1 (Berkeley) 6/6/93
3869+ */
3870+
3871+/*
3872+ * Copyright (c) 1985 Sun Microsystems, Inc.
3873+ * Copyright (c) 1976 Board of Trustees of the University of Illinois.
3874 * All rights reserved.
3875 *
3876 * Redistribution and use in source and binary forms, with or without
3877@@ -32,8 +64,7 @@
3878 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3879 * SUCH DAMAGE.
3880 *
3881- * @(#)indent_globs.h 8.1 (Berkeley) 6/6/93
3882- * $FreeBSD: head/usr.bin/indent/indent_globs.h 303735 2016-08-03 22:08:07Z pfg $
3883+ * from: @(#)indent_globs.h 8.1 (Berkeley) 6/6/93
3884 */
3885
3886 #define BACKSLASH '\\'
3887@@ -43,283 +74,317 @@
3888 * of code */
3889
3890
3891-#ifndef false
3892+
3893 #define false 0
3894-#endif
3895-#ifndef true
3896 #define true 1
3897+
3898+
3899+#ifndef EXTERN
3900+#define EXTERN extern
3901 #endif
3902
3903
3904-FILE *input; /* the fid for the input file */
3905-FILE *output; /* the output file */
3906+EXTERN FILE *input; /* the fid for the input file */
3907+EXTERN FILE *output; /* the output file */
3908
3909-#define CHECK_SIZE_CODE(desired_size) \
3910- if (e_code + (desired_size) >= l_code) { \
3911- int nsize = l_code-s_code + 400 + desired_size; \
3912- int code_len = e_code-s_code; \
3913+#define CHECK_SIZE_CODE \
3914+ if (e_code >= l_code) { \
3915+ int nsize = l_code-s_code+400; \
3916+ int code_len = e_code-s_code; \
3917 codebuf = (char *) realloc(codebuf, nsize); \
3918 if (codebuf == NULL) \
3919- err(1, NULL); \
3920- e_code = codebuf + code_len + 1; \
3921+ err(1, NULL); \
3922+ e_code = codebuf + code_len + 1; \
3923 l_code = codebuf + nsize - 5; \
3924 s_code = codebuf + 1; \
3925 }
3926-#define CHECK_SIZE_COM(desired_size) \
3927- if (e_com + (desired_size) >= l_com) { \
3928- int nsize = l_com-s_com + 400 + desired_size; \
3929- int com_len = e_com - s_com; \
3930+#define CHECK_SIZE_COM \
3931+ if (e_com >= l_com) { \
3932+ int nsize = l_com-s_com+400; \
3933+ int com_len = e_com - s_com; \
3934 int blank_pos; \
3935- if (last_bl != NULL) \
3936+ if (last_bl != NULL) \
3937 blank_pos = last_bl - combuf; \
3938 else \
3939 blank_pos = -1; \
3940 combuf = (char *) realloc(combuf, nsize); \
3941 if (combuf == NULL) \
3942- err(1, NULL); \
3943- e_com = combuf + com_len + 1; \
3944- if (blank_pos > 0) \
3945+ err(1, NULL); \
3946+ e_com = combuf + com_len + 1; \
3947+ if (blank_pos > 0) \
3948 last_bl = combuf + blank_pos; \
3949 l_com = combuf + nsize - 5; \
3950 s_com = combuf + 1; \
3951 }
3952-#define CHECK_SIZE_LAB(desired_size) \
3953- if (e_lab + (desired_size) >= l_lab) { \
3954- int nsize = l_lab-s_lab + 400 + desired_size; \
3955- int label_len = e_lab - s_lab; \
3956+#define CHECK_SIZE_LAB \
3957+ if (e_lab >= l_lab) { \
3958+ int nsize = l_lab-s_lab+400; \
3959+ int label_len = e_lab - s_lab; \
3960 labbuf = (char *) realloc(labbuf, nsize); \
3961- if (labbuf == NULL) \
3962- err(1, NULL); \
3963+ if (labbuf == NULL) \
3964+ err(1, NULL); \
3965 e_lab = labbuf + label_len + 1; \
3966 l_lab = labbuf + nsize - 5; \
3967 s_lab = labbuf + 1; \
3968 }
3969-#define CHECK_SIZE_TOKEN(desired_size) \
3970- if (e_token + (desired_size) >= l_token) { \
3971- int nsize = l_token-s_token + 400 + desired_size; \
3972- int token_len = e_token - s_token; \
3973+#define CHECK_SIZE_TOKEN \
3974+ if (e_token >= l_token) { \
3975+ int nsize = l_token-s_token+400; \
3976+ int token_len = e_token - s_token; \
3977 tokenbuf = (char *) realloc(tokenbuf, nsize); \
3978- if (tokenbuf == NULL) \
3979- err(1, NULL); \
3980+ if (tokenbuf == NULL) \
3981+ err(1, NULL); \
3982 e_token = tokenbuf + token_len + 1; \
3983 l_token = tokenbuf + nsize - 5; \
3984 s_token = tokenbuf + 1; \
3985 }
3986
3987-char *labbuf; /* buffer for label */
3988-char *s_lab; /* start ... */
3989-char *e_lab; /* .. and end of stored label */
3990-char *l_lab; /* limit of label buffer */
3991+EXTERN char *labbuf; /* buffer for label */
3992+EXTERN char *s_lab; /* start ... */
3993+EXTERN char *e_lab; /* .. and end of stored label */
3994+EXTERN char *l_lab; /* limit of label buffer */
3995
3996-char *codebuf; /* buffer for code section */
3997-char *s_code; /* start ... */
3998-char *e_code; /* .. and end of stored code */
3999-char *l_code; /* limit of code section */
4000+EXTERN char *codebuf; /* buffer for code section */
4001+EXTERN char *s_code; /* start ... */
4002+EXTERN char *e_code; /* .. and end of stored code */
4003+EXTERN char *l_code; /* limit of code section */
4004
4005-char *combuf; /* buffer for comments */
4006-char *s_com; /* start ... */
4007-char *e_com; /* ... and end of stored comments */
4008-char *l_com; /* limit of comment buffer */
4009+EXTERN char *combuf; /* buffer for comments */
4010+EXTERN char *s_com; /* start ... */
4011+EXTERN char *e_com; /* ... and end of stored comments */
4012+EXTERN char *l_com; /* limit of comment buffer */
4013
4014 #define token s_token
4015-char *tokenbuf; /* the last token scanned */
4016-char *s_token;
4017-char *e_token;
4018-char *l_token;
4019+EXTERN char *tokenbuf; /* the last token scanned */
4020+EXTERN char *s_token;
4021+EXTERN char *e_token;
4022+EXTERN char *l_token;
4023
4024-char *in_buffer; /* input buffer */
4025-char *in_buffer_limit; /* the end of the input buffer */
4026-char *buf_ptr; /* ptr to next character to be taken from
4027- * in_buffer */
4028-char *buf_end; /* ptr to first after last char in in_buffer */
4029+EXTERN char *in_buffer; /* input buffer */
4030+EXTERN char *in_buffer_limit; /* the end of the input buffer */
4031+EXTERN char *buf_ptr; /* ptr to next character to be taken from
4032+ * in_buffer */
4033+EXTERN char *buf_end; /* ptr to first after last char in in_buffer */
4034
4035-char sc_buf[sc_size]; /* input text is saved here when looking for
4036- * the brace after an if, while, etc */
4037-char *save_com; /* start of the comment stored in sc_buf */
4038-char *sc_end; /* pointer into save_com buffer */
4039+EXTERN char sc_buf[sc_size]; /* input text is saved here when looking for
4040+ * the brace after an if, while, etc */
4041+EXTERN char save_com; /* start of the comment stored in sc_buf */
4042+EXTERN char *sc_end; /* pointer into save_com buffer */
4043
4044-char *bp_save; /* saved value of buf_ptr when taking input
4045- * from save_com */
4046-char *be_save; /* similarly saved value of buf_end */
4047+EXTERN char *bp_save; /* saved value of buf_ptr when taking input
4048+ * from save_com */
4049+EXTERN char *be_save; /* similarly saved value of buf_end */
4050
4051-
4052-int found_err;
4053-int blanklines_after_declarations;
4054-int blanklines_before_blockcomments;
4055-int blanklines_after_procs;
4056-int blanklines_around_conditional_compilation;
4057-int swallow_optional_blanklines;
4058-int n_real_blanklines;
4059-int prefix_blankline_requested;
4060-int postfix_blankline_requested;
4061-int break_comma; /* when true and not in parens, break after a
4062- * comma */
4063-int btype_2; /* when true, brace should be on same line as
4064- * if, while, etc */
4065-float case_ind; /* indentation level to be used for a "case
4066- * n:" */
4067-int code_lines; /* count of lines with code */
4068-int had_eof; /* set to true when input is exhausted */
4069-int line_no; /* the current line number. */
4070-int max_col; /* the maximum allowable line length */
4071-int verbose; /* when true, non-essential error messages are
4072- * printed */
4073-int cuddle_else; /* true if else should cuddle up to '}' */
4074-int star_comment_cont; /* true iff comment continuation lines should
4075- * have stars at the beginning of each line. */
4076-int comment_delimiter_on_blankline;
4077-int troff; /* true iff were generating troff input */
4078-int procnames_start_line; /* if true, the names of procedures
4079- * being defined get placed in column
4080- * 1 (ie. a newline is placed between
4081- * the type of the procedure and its
4082- * name) */
4083-int proc_calls_space; /* If true, procedure calls look like:
4084- * foo(bar) rather than foo (bar) */
4085-int format_block_comments; /* true if comments beginning with
4086- * `/ * \n' are to be reformatted */
4087-int format_col1_comments; /* If comments which start in column 1
4088- * are to be magically reformatted
4089- * (just like comments that begin in
4090- * later columns) */
4091-int inhibit_formatting; /* true if INDENT OFF is in effect */
4092-int suppress_blanklines;/* set iff following blanklines should be
4093- * suppressed */
4094-int continuation_indent;/* set to the indentation between the edge of
4095- * code and continuation lines */
4096-int lineup_to_parens; /* if true, continued code within parens will
4097- * be lined up to the open paren */
4098-int lineup_to_parens_always; /* if true, do not attempt to keep
4099+EXTERN struct options {
4100+ int blanklines_around_conditional_compilation;
4101+ int blanklines_after_declarations_at_proctop; /* This is vaguely
4102+ * similar to
4103+ * blanklines_after_decla
4104+ * rations except that
4105+ * it only applies to
4106+ * the first set of
4107+ * declarations in a
4108+ * procedure (just after
4109+ * the first '{') and it
4110+ * causes a blank line
4111+ * to be generated even
4112+ * if there are no
4113+ * declarations */
4114+ int blanklines_after_declarations;
4115+ int blanklines_after_procs;
4116+ int blanklines_before_blockcomments;
4117+ int leave_comma; /* if true, never break declarations after
4118+ * commas */
4119+ int btype_2; /* when true, brace should be on same line as
4120+ * if, while, etc */
4121+ int Bill_Shannon; /* true iff a blank should always be inserted
4122+ * after sizeof */
4123+ int comment_delimiter_on_blankline;
4124+ int decl_com_ind; /* the column in which comments after
4125+ * declarations should be put */
4126+ int cuddle_else; /* true if else should cuddle up to '}' */
4127+ int continuation_indent; /* set to the indentation between the edge of
4128+ * code and continuation lines */
4129+ int com_ind; /* the column in which comments to the right
4130+ * of code should start */
4131+ int decl_indent; /* column to indent declared identifiers to */
4132+ int local_decl_indent; /* like decl_indent but for locals */
4133+ int ljust_decl; /* true if declarations should be left
4134+ * justified */
4135+ int unindent_displace; /* comments not to the right of code
4136+ * will be placed this many
4137+ * indentation levels to the left of
4138+ * code */
4139+ int extra_expression_indent; /* True if continuation lines from the
4140+ * expression part of "if(e)", "while(e)",
4141+ * "for(e;e;e)" should be indented an extra
4142+ * tab stop so that they don't conflict with
4143+ * the code that follows */
4144+ int else_if; /* True iff else if pairs should be handled
4145+ * specially */
4146+ int function_brace_split; /* split function declaration and
4147+ * brace onto separate lines */
4148+ int format_col1_comments; /* If comments which start in column 1 are to
4149+ * be magically reformatted (just like
4150+ * comments that begin in later columns) */
4151+ int format_block_comments; /* true if comments beginning with
4152+ * `/*\n' are to be reformatted */
4153+ int indent_parameters;
4154+ int ind_size; /* the size of one indentation level */
4155+ int block_comment_max_col;
4156+ int lineup_to_parens_always; /* if true, do not attempt to keep
4157 * lined-up code within the margin */
4158-int Bill_Shannon; /* true iff a blank should always be inserted
4159- * after sizeof */
4160-int blanklines_after_declarations_at_proctop; /* This is vaguely
4161- * similar to
4162- * blanklines_after_decla
4163- * rations except that
4164- * it only applies to
4165- * the first set of
4166- * declarations in a
4167- * procedure (just after
4168- * the first '{') and it
4169- * causes a blank line
4170- * to be generated even
4171- * if there are no
4172- * declarations */
4173-int block_comment_max_col;
4174-int extra_expression_indent; /* true if continuation lines from the
4175- * expression part of "if(e)",
4176- * "while(e)", "for(e;e;e)" should be
4177- * indented an extra tab stop so that
4178- * they don't conflict with the code
4179- * that follows */
4180-int function_brace_split; /* split function declaration and
4181- * brace onto separate lines */
4182-int use_tabs; /* set true to use tabs for spacing,
4183+ int lineup_to_parens; /* if true, continued code within parens will
4184+ * be lined up to the open paren */
4185+ int proc_calls_space; /* If true, procedure calls look like:
4186+ * foo (bar) rather than foo(bar) */
4187+ int procnames_start_line; /* if true, the names of procedures being
4188+ * defined get placed in column 1 (ie. a
4189+ * newline is placed between the type of the
4190+ * procedure and its name) */
4191+ int space_after_cast; /* "b = (int) a" vs "b = (int)a" */
4192+ int star_comment_cont; /* true iff comment continuation lines should
4193+ * have stars at the beginning of each line. */
4194+ int swallow_optional_blanklines;
4195+ int tabsize; /* the size of a tab */
4196+ int max_col; /* the maximum allowable line length */
4197+ int use_tabs; /* set true to use tabs for spacing,
4198 * false uses all spaces */
4199-int auto_typedefs; /* set true to recognize identifiers
4200- * ending in "_t" like typedefs */
4201-int space_after_cast; /* "b = (int) a" vs "b = (int)a" */
4202-int tabsize; /* the size of a tab */
4203+ int verbose; /* when true, non-essential error messages are
4204+ * printed */
4205+
4206+
4207+
4208+} opt;
4209+
4210+EXTERN int n_real_blanklines;
4211+EXTERN int prefix_blankline_requested;
4212+EXTERN int postfix_blankline_requested;
4213+EXTERN int break_comma; /* when true and not in parens, break after a
4214+ * comma */
4215+EXTERN float case_ind; /* indentation level to be used for a "case
4216+ * n:" */
4217+EXTERN int code_lines; /* count of lines with code */
4218+EXTERN int had_eof; /* set to true when input is exhausted */
4219+EXTERN int line_no; /* the current line number. */
4220+EXTERN int found_err;
4221+EXTERN int inhibit_formatting; /* true if INDENT OFF is in effect */
4222+EXTERN int suppress_blanklines; /* set iff following blanklines should be
4223+ * suppressed */
4224+
4225+
4226+#define STACK_SIZE 256
4227
4228-struct parser_state {
4229- int last_token;
4230- int p_stack[256]; /* this is the parsers stack */
4231- int il[64]; /* this stack stores indentation levels */
4232- float cstk[32]; /* used to store case stmt indentation levels */
4233- int box_com; /* set to true when we are in a "boxed"
4234+EXTERN struct parser_state {
4235+ int last_token;
4236+
4237+ int p_stack[STACK_SIZE]; /* this is the parsers stack */
4238+ int il[STACK_SIZE]; /* this stack stores indentation levels */
4239+ float cstk[STACK_SIZE];/* used to store case stmt indentation levels */
4240+ int box_com; /* set to true when we are in a "boxed"
4241 * comment. In that case, the first non-blank
4242- * char should be lined up with the / in / followed by * */
4243- int comment_delta; /* used to set up indentation for all lines
4244- * of a boxed comment after the first one */
4245- int n_comment_delta;/* remembers how many columns there were
4246- * before the start of a box comment so that
4247- * forthcoming lines of the comment are
4248- * indented properly */
4249- int cast_mask; /* indicates which close parens potentially
4250+ * char should be lined up with the comment / */
4251+ int comment_delta, /* used to set up indentation for all lines
4252+ * of a boxed comment after the first one */
4253+
4254+ n_comment_delta; /* remembers how many columns there were
4255+ * before the start of a box comment so that
4256+ * forthcoming lines of the comment are
4257+ * indented properly */
4258+ int cast_mask; /* indicates which close parens potentially
4259 * close off casts */
4260- int not_cast_mask; /* indicates which close parens definitely
4261- * close off something else than casts */
4262- int block_init; /* true iff inside a block initialization */
4263- int block_init_level; /* The level of brace nesting in an
4264+ int not_cast_mask; /* indicates which close parens definitely
4265+ * close off sizeof''s */
4266+ int block_init; /* true iff inside a block initialization */
4267+ int block_init_level; /* The level of brace nesting in an
4268 * initialization */
4269- int last_nl; /* this is true if the last thing scanned was
4270+ int last_nl; /* this is true if the last thing scanned was
4271 * a newline */
4272- int in_or_st; /* Will be true iff there has been a
4273+ int in_or_st; /* Will be true iff there has been a
4274 * declarator (e.g. int or char) and no left
4275 * paren since the last semicolon. When true,
4276 * a '{' is starting a structure definition or
4277 * an initialization list */
4278- int bl_line; /* set to 1 by dump_line if the line is blank */
4279- int col_1; /* set to true if the last token started in
4280+ int bl_line; /* set to 1 by dump_line if the line is blank */
4281+ int col_1; /* set to true if the last token started in
4282 * column 1 */
4283- int com_col; /* this is the column in which the current
4284- * comment should start */
4285- int com_ind; /* the column in which comments to the right
4286- * of code should start */
4287- int com_lines; /* the number of lines with comments, set by
4288+ int com_col; /* this is the column in which the current
4289+ * coment should start */
4290+ int com_lines; /* the number of lines with comments, set by
4291 * dump_line */
4292- int dec_nest; /* current nesting level for structure or init */
4293- int decl_com_ind; /* the column in which comments after
4294- * declarations should be put */
4295- int decl_on_line; /* set to true if this line of code has part
4296+ int dec_nest; /* current nesting level for structure or init */
4297+ int decl_on_line; /* set to true if this line of code has part
4298 * of a declaration on it */
4299- int i_l_follow; /* the level to which ind_level should be set
4300+ int i_l_follow; /* the level to which ind_level should be set
4301 * after the current line is printed */
4302- int in_decl; /* set to true when we are in a declaration
4303+ int in_decl; /* set to true when we are in a declaration
4304 * stmt. The processing of braces is then
4305 * slightly different */
4306- int in_stmt; /* set to 1 while in a stmt */
4307- int ind_level; /* the current indentation level */
4308- int ind_size; /* the size of one indentation level */
4309- int ind_stmt; /* set to 1 if next line should have an extra
4310+ int in_stmt; /* set to 1 while in a stmt */
4311+ int ind_level; /* the current indentation level */
4312+ int ind_stmt; /* set to 1 if next line should have an extra
4313 * indentation level because we are in the
4314 * middle of a stmt */
4315- int last_u_d; /* set to true after scanning a token which
4316+ int last_u_d; /* set to true after scanning a token which
4317 * forces a following operator to be unary */
4318- int leave_comma; /* if true, never break declarations after
4319- * commas */
4320- int ljust_decl; /* true if declarations should be left
4321- * justified */
4322- int out_coms; /* the number of comments processed, set by
4323+ int out_coms; /* the number of comments processed, set by
4324 * pr_comment */
4325- int out_lines; /* the number of lines written, set by
4326+ int out_lines; /* the number of lines written, set by
4327 * dump_line */
4328- int p_l_follow; /* used to remember how to indent following
4329+ int p_l_follow; /* used to remember how to indent following
4330 * statement */
4331- int paren_level; /* parenthesization level. used to indent
4332- * within statements */
4333- short paren_indents[20]; /* column positions of each paren */
4334- int pcase; /* set to 1 if the current line label is a
4335+ int paren_level; /* parenthesization level. used to indent
4336+ * within stmts */
4337+ short paren_indents[20]; /* column positions of each paren */
4338+ int pcase; /* set to 1 if the current line label is a
4339 * case. It is printed differently from a
4340 * regular label */
4341- int search_brace; /* set to true by parse when it is necessary
4342+ int search_brace; /* set to true by parse when it is necessary
4343 * to buffer up all info up to the start of a
4344 * stmt after an if, while, etc */
4345- int unindent_displace; /* comments not to the right of code
4346- * will be placed this many
4347- * indentation levels to the left of
4348- * code */
4349- int use_ff; /* set to one if the current line should be
4350+ int use_ff; /* set to one if the current line should be
4351 * terminated with a form feed */
4352- int want_blank; /* set to true when the following token should
4353+ int want_blank; /* set to true when the following token should
4354 * be prefixed by a blank. (Said prefixing is
4355 * ignored in some cases.) */
4356- int else_if; /* True iff else if pairs should be handled
4357- * specially */
4358- int decl_indent; /* column to indent declared identifiers to */
4359- int local_decl_indent; /* like decl_indent but for locals */
4360- int keyword; /* the type of a keyword or 0 */
4361- int dumped_decl_indent;
4362- float case_indent; /* The distance to indent case labels from the
4363+
4364+ int keyword; /* the type of a keyword or 0 */
4365+ int dumped_decl_indent;
4366+ float case_indent; /* The distance to indent case labels from the
4367 * switch statement */
4368- int in_parameter_declaration;
4369- int indent_parameters;
4370- int tos; /* pointer to top of stack */
4371- char procname[100]; /* The name of the current procedure */
4372- int just_saw_decl;
4373-} ps;
4374+ int in_parameter_declaration;
4375+ int tos; /* pointer to top of stack */
4376+ char procname[100]; /* The name of the current procedure */
4377+ int just_saw_decl;
4378+} ps;
4379+
4380+EXTERN int ifdef_level;
4381+EXTERN struct parser_state state_stack[5];
4382+EXTERN struct parser_state match_state[5];
4383+
4384
4385-int ifdef_level;
4386-struct parser_state state_stack[5];
4387-struct parser_state match_state[5];
4388+int compute_code_target(void);
4389+int compute_label_target(void);
4390+int count_spaces(int, char *);
4391+int count_spaces_until(int, char *, char *);
4392+void diag(int, const char *,...) __attribute__((__format__(__printf__, 2, 3)));
4393+void diag2(int, const char *);
4394+void diag3(int, const char *, int);
4395+void diag4(int, const char *, int, int);
4396+void dump_line(void);
4397+int eqin(const char *, const char *);
4398+void fill_buffer(void);
4399+void init_constant_tt(void);
4400+int pad_output(int, int);
4401+void scan_profile(FILE *);
4402+void set_defaults(void);
4403+void set_option(char *);
4404+void add_typename(const char *);
4405+void alloc_typenames(void);
4406+void set_profile(const char *);
4407+char *chfont(struct fstate *, struct fstate *, char *);
4408+int lexi(struct parser_state *);
4409+void reduce(void);
4410+void parse(int);
4411+void pr_comment(void);
4412+void bakcopy(void);
4413diff --git a/io.c b/io.c
4414index d9af767..dc395c7 100644
4415--- a/io.c
4416+++ b/io.c
4417@@ -1,7 +1,37 @@
4418-/*-
4419- * Copyright (c) 1985 Sun Microsystems, Inc.
4420+/* $NetBSD: io.c,v 1.17 2016/02/25 13:23:27 ginsbach Exp $ */
4421+
4422+/*
4423 * Copyright (c) 1980, 1993
4424 * The Regents of the University of California. All rights reserved.
4425+ *
4426+ * Redistribution and use in source and binary forms, with or without
4427+ * modification, are permitted provided that the following conditions
4428+ * are met:
4429+ * 1. Redistributions of source code must retain the above copyright
4430+ * notice, this list of conditions and the following disclaimer.
4431+ * 2. Redistributions in binary form must reproduce the above copyright
4432+ * notice, this list of conditions and the following disclaimer in the
4433+ * documentation and/or other materials provided with the distribution.
4434+ * 3. Neither the name of the University nor the names of its contributors
4435+ * may be used to endorse or promote products derived from this software
4436+ * without specific prior written permission.
4437+ *
4438+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
4439+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4440+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4441+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
4442+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4443+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4444+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4445+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4446+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4447+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4448+ * SUCH DAMAGE.
4449+ */
4450+
4451+/*
4452+ * Copyright (c) 1976 Board of Trustees of the University of Illinois.
4453+ * Copyright (c) 1985 Sun Microsystems, Inc.
4454 * All rights reserved.
4455 *
4456 * Redistribution and use in source and binary forms, with or without
4457@@ -33,26 +63,27 @@
4458 * SUCH DAMAGE.
4459 */
4460
4461+#include <sys/cdefs.h>
4462+
4463 #if 0
4464 #ifndef lint
4465 static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93";
4466-#endif /* not lint */
4467-#endif
4468+#else
4469+__RCSID("$NetBSD: io.c,v 1.17 2016/02/25 13:23:27 ginsbach Exp $");
4470
4471-#include <sys/cdefs.h>
4472-__FBSDID("$FreeBSD: head/usr.bin/indent/io.c 309418 2016-12-02 16:41:08Z pfg $");
4473+#endif /* not lint */
4474+#endif
4475
4476 #include <ctype.h>
4477 #include <err.h>
4478+#include <stdarg.h>
4479 #include <stdio.h>
4480 #include <stdlib.h>
4481 #include <string.h>
4482 #include "indent_globs.h"
4483-#include "indent.h"
4484
4485-int comment_open;
4486-static int paren_target;
4487-static int pad_output(int current, int target);
4488+int comment_open;
4489+static int paren_target;
4490
4491 void
4492 dump_line(void)
4493@@ -61,196 +92,198 @@ dump_line(void)
4494 * prints the label section, followed by the
4495 * code section with the appropriate nesting
4496 * level, followed by any comments */
4497- int cur_col,
4498- target_col = 1;
4499- static int not_first_line;
4500+ int cur_col, target_col;
4501+ static int not_first_line;
4502
4503- if (ps.procname[0]) {
4504- ps.ind_level = 0;
4505- ps.procname[0] = 0;
4506- }
4507- if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
4508- if (suppress_blanklines > 0)
4509- suppress_blanklines--;
4510- else {
4511- ps.bl_line = true;
4512- n_real_blanklines++;
4513- }
4514- }
4515- else if (!inhibit_formatting) {
4516- suppress_blanklines = 0;
4517- ps.bl_line = false;
4518- if (prefix_blankline_requested && not_first_line) {
4519- if (swallow_optional_blanklines) {
4520- if (n_real_blanklines == 1)
4521- n_real_blanklines = 0;
4522- }
4523- else {
4524- if (n_real_blanklines == 0)
4525- n_real_blanklines = 1;
4526- }
4527- }
4528- while (--n_real_blanklines >= 0)
4529- putc('\n', output);
4530- n_real_blanklines = 0;
4531- if (ps.ind_level == 0)
4532- ps.ind_stmt = 0; /* this is a class A kludge. dont do
4533- * additional statement indentation if we are
4534- * at bracket level 0 */
4535-
4536- if (e_lab != s_lab || e_code != s_code)
4537- ++code_lines; /* keep count of lines with code */
4538-
4539-
4540- if (e_lab != s_lab) { /* print lab, if any */
4541- if (comment_open) {
4542- comment_open = 0;
4543- fprintf(output, ".*/\n");
4544- }
4545- while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
4546- e_lab--;
4547- *e_lab = '\0';
4548- cur_col = pad_output(1, compute_label_target());
4549- if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
4550- || strncmp(s_lab, "#endif", 6) == 0)) {
4551- char *s = s_lab;
4552- if (e_lab[-1] == '\n') e_lab--;
4553- do putc(*s++, output);
4554- while (s < e_lab && 'a' <= *s && *s<='z');
4555- while ((*s == ' ' || *s == '\t') && s < e_lab)
4556- s++;
4557- if (s < e_lab)
4558- fprintf(output, s[0]=='/' && s[1]=='*' ? "\t%.*s" : "\t/* %.*s */",
4559- (int)(e_lab - s), s);
4560- }
4561- else fprintf(output, "%.*s", (int)(e_lab - s_lab), s_lab);
4562- cur_col = count_spaces(cur_col, s_lab);
4563- }
4564- else
4565- cur_col = 1; /* there is no label section */
4566-
4567- ps.pcase = false;
4568-
4569- if (s_code != e_code) { /* print code section, if any */
4570- char *p;
4571-
4572- if (comment_open) {
4573- comment_open = 0;
4574- fprintf(output, ".*/\n");
4575- }
4576- target_col = compute_code_target();
4577- {
4578- int i;
4579-
4580- for (i = 0; i < ps.p_l_follow; i++)
4581- if (ps.paren_indents[i] >= 0)
4582- ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
4583- }
4584- cur_col = pad_output(cur_col, target_col);
4585- for (p = s_code; p < e_code; p++)
4586- if (*p == (char) 0200)
4587- fprintf(output, "%d", target_col * 7);
4588- else
4589- putc(*p, output);
4590- cur_col = count_spaces(cur_col, s_code);
4591+ target_col = 0;
4592+ if (ps.procname[0]) {
4593+ ps.ind_level = 0;
4594+ ps.procname[0] = 0;
4595 }
4596- if (s_com != e_com) { /* print comment, if any */
4597- int target = ps.com_col;
4598- char *com_st = s_com;
4599-
4600- target += ps.comment_delta;
4601- while (*com_st == '\t') /* consider original indentation in
4602- * case this is a box comment */
4603- com_st++, target += tabsize;
4604- while (target <= 0)
4605- if (*com_st == ' ')
4606- target++, com_st++;
4607- else if (*com_st == '\t')
4608- target = tabsize * (1 + (target - 1) / tabsize) + 1, com_st++;
4609- else
4610- target = 1;
4611- if (cur_col > target) { /* if comment can't fit on this line,
4612- * put it on next line */
4613- putc('\n', output);
4614- cur_col = 1;
4615- ++ps.out_lines;
4616- }
4617- while (e_com > com_st && isspace((unsigned char)e_com[-1]))
4618- e_com--;
4619- (void)pad_output(cur_col, target);
4620- fwrite(com_st, e_com - com_st, 1, output);
4621- ps.comment_delta = ps.n_comment_delta;
4622- ++ps.com_lines; /* count lines with comments */
4623- }
4624- if (ps.use_ff)
4625- putc('\014', output);
4626- else
4627- putc('\n', output);
4628- ++ps.out_lines;
4629- if (ps.just_saw_decl == 1 && blanklines_after_declarations) {
4630- prefix_blankline_requested = 1;
4631- ps.just_saw_decl = 0;
4632- }
4633- else
4634- prefix_blankline_requested = postfix_blankline_requested;
4635- postfix_blankline_requested = 0;
4636- }
4637- ps.decl_on_line = ps.in_decl; /* if we are in the middle of a
4638+ if (s_code == e_code && s_lab == e_lab && s_com == e_com) {
4639+ if (suppress_blanklines > 0)
4640+ suppress_blanklines--;
4641+ else {
4642+ ps.bl_line = true;
4643+ n_real_blanklines++;
4644+ }
4645+ } else
4646+ if (!inhibit_formatting) {
4647+ suppress_blanklines = 0;
4648+ ps.bl_line = false;
4649+ if (prefix_blankline_requested && not_first_line) {
4650+ if (opt.swallow_optional_blanklines) {
4651+ if (n_real_blanklines == 1)
4652+ n_real_blanklines = 0;
4653+ } else {
4654+ if (n_real_blanklines == 0)
4655+ n_real_blanklines = 1;
4656+ }
4657+ }
4658+ while (--n_real_blanklines >= 0)
4659+ putc('\n', output);
4660+ n_real_blanklines = 0;
4661+ if (ps.ind_level == 0)
4662+ ps.ind_stmt = 0; /* this is a class A
4663+ * kludge. don't do
4664+ * additional statement
4665+ * indentation if we are
4666+ * at bracket level 0 */
4667+
4668+ if (e_lab != s_lab || e_code != s_code)
4669+ ++code_lines; /* keep count of lines with
4670+ * code */
4671+
4672+
4673+ if (e_lab != s_lab) { /* print lab, if any */
4674+ if (comment_open) {
4675+ comment_open = 0;
4676+ fprintf(output, ".*/\n");
4677+ }
4678+ while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t'))
4679+ e_lab--;
4680+ *e_lab = '\0';
4681+ cur_col = pad_output(1, compute_label_target());
4682+ if (s_lab[0] == '#' && (strncmp(s_lab, "#else", 5) == 0
4683+ || strncmp(s_lab, "#endif", 6) == 0)) {
4684+ char *s = s_lab;
4685+ if (e_lab[-1] == '\n')
4686+ e_lab--;
4687+ do
4688+ putc(*s++, output);
4689+ while (s < e_lab && 'a' <= *s && *s <= 'z');
4690+ while ((*s == ' ' || *s == '\t') && s < e_lab)
4691+ s++;
4692+ if (s < e_lab)
4693+ fprintf(output, s[0] == '/' && s[1] == '*' ? "\t%.*s" : "\t/* %.*s */",
4694+ (int)(e_lab - s), s);
4695+ } else
4696+ fprintf(output, "%.*s", (int)(e_lab - s_lab), s_lab);
4697+ cur_col = count_spaces(cur_col, s_lab);
4698+ } else
4699+ cur_col = 1; /* there is no label section */
4700+
4701+ ps.pcase = false;
4702+
4703+ if (s_code != e_code) { /* print code section, if any */
4704+ char *p;
4705+
4706+ if (comment_open) {
4707+ comment_open = 0;
4708+ fprintf(output, ".*/\n");
4709+ }
4710+ target_col = compute_code_target();
4711+ {
4712+ int i;
4713+
4714+ for (i = 0; i < ps.p_l_follow; i++)
4715+ if (ps.paren_indents[i] >= 0)
4716+ ps.paren_indents[i] = -(ps.paren_indents[i] + target_col);
4717+ }
4718+ cur_col = pad_output(cur_col, target_col);
4719+ for (p = s_code; p < e_code; p++)
4720+ if (*p == (char) 0200)
4721+ fprintf(output, "%d", target_col * 7);
4722+ else
4723+ putc(*p, output);
4724+ cur_col = count_spaces(cur_col, s_code);
4725+ }
4726+ if (s_com != e_com) { /* print comment, if any */
4727+ int target = ps.com_col;
4728+ char *com_st = s_com;
4729+
4730+ target += ps.comment_delta;
4731+ while (*com_st == '\t') /* consider original indentation in
4732+ * case this is a box comment */
4733+ com_st++, target += tabsize;
4734+ while (target <= 0)
4735+ if (*com_st == ' ')
4736+ target++, com_st++;
4737+ else if (*com_st == '\t')
4738+ target = tabsize * (1 + (target - 1) / tabsize) + 1, com_st++;
4739+ else
4740+ target = 1;
4741+ if (cur_col > target) { /* if comment can't fit on this line,
4742+ * put it on next line */
4743+ putc('\n', output);
4744+ cur_col = 1;
4745+ ++ps.out_lines;
4746+ }
4747+ while (e_com > com_st && isspace((unsigned char)e_com[-1]))
4748+ e_com--;
4749+ (void)pad_output(cur_col, target);
4750+ fwrite(com_st, e_com - com_st, 1, output);
4751+ ps.comment_delta = ps.n_comment_delta;
4752+ ++ps.com_lines; /* count lines with comments */
4753+ }
4754+ if (ps.use_ff)
4755+ putc('\014', output);
4756+ else
4757+ putc('\n', output);
4758+ ++ps.out_lines;
4759+ if (ps.just_saw_decl == 1 && opt.blanklines_after_declarations) {
4760+ prefix_blankline_requested = 1;
4761+ ps.just_saw_decl = 0;
4762+ } else
4763+ prefix_blankline_requested = postfix_blankline_requested;
4764+ postfix_blankline_requested = 0;
4765+ }
4766+ ps.decl_on_line = ps.in_decl; /* if we are in the middle of a
4767 * declaration, remember that fact for
4768 * proper comment indentation */
4769- ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be
4770+ ps.ind_stmt = ps.in_stmt & ~ps.in_decl; /* next line should be
4771 * indented if we have not
4772 * completed this stmt and if
4773 * we are not in the middle of
4774 * a declaration */
4775- ps.use_ff = false;
4776- ps.dumped_decl_indent = 0;
4777- *(e_lab = s_lab) = '\0'; /* reset buffers */
4778- *(e_code = s_code) = '\0';
4779- *(e_com = s_com = combuf + 1) = '\0';
4780- ps.ind_level = ps.i_l_follow;
4781- ps.paren_level = ps.p_l_follow;
4782- if (ps.paren_level > 0)
4783- paren_target = -ps.paren_indents[ps.paren_level - 1];
4784- not_first_line = 1;
4785+ ps.use_ff = false;
4786+ ps.dumped_decl_indent = 0;
4787+ *(e_lab = s_lab) = '\0';/* reset buffers */
4788+ *(e_code = s_code) = '\0';
4789+ *(e_com = s_com = combuf+1) = '\0';
4790+ ps.ind_level = ps.i_l_follow;
4791+ ps.paren_level = ps.p_l_follow;
4792+ if (ps.paren_level > 0)
4793+ paren_target = -ps.paren_indents[ps.paren_level - 1];
4794+ not_first_line = 1;
4795 }
4796
4797 int
4798 compute_code_target(void)
4799 {
4800- int target_col = ps.ind_size * ps.ind_level + 1;
4801-
4802- if (ps.paren_level)
4803- if (!lineup_to_parens)
4804- target_col += continuation_indent
4805- * (2 * continuation_indent == ps.ind_size ? 1 : ps.paren_level);
4806- else if (lineup_to_parens_always)
4807- target_col = paren_target;
4808- else {
4809- int w;
4810- int t = paren_target;
4811-
4812- if ((w = count_spaces(t, s_code) - max_col) > 0
4813- && count_spaces(target_col, s_code) <= max_col) {
4814- t -= w + 1;
4815- if (t > target_col)
4816- target_col = t;
4817- }
4818- else
4819- target_col = t;
4820- }
4821- else if (ps.ind_stmt)
4822- target_col += continuation_indent;
4823- return target_col;
4824+ int target_col = opt.ind_size * ps.ind_level + 1;
4825+
4826+ if (ps.paren_level) {
4827+ if (!opt.lineup_to_parens)
4828+ target_col += opt.continuation_indent *
4829+ (2 * opt.continuation_indent == opt.ind_size ? 1 : ps.paren_level);
4830+ else if (opt.lineup_to_parens_always)
4831+ target_col = paren_target;
4832+ else {
4833+ int w;
4834+ int t = paren_target;
4835+
4836+ if ((w = count_spaces(t, s_code) - opt.max_col) > 0
4837+ && count_spaces(target_col, s_code) <= opt.max_col) {
4838+ t -= w + 1;
4839+ if (t > target_col)
4840+ target_col = t;
4841+ } else
4842+ target_col = t;
4843+ }
4844+ } else
4845+ if (ps.ind_stmt)
4846+ target_col += opt.continuation_indent;
4847+ return target_col;
4848 }
4849
4850 int
4851 compute_label_target(void)
4852 {
4853- return
4854- ps.pcase ? (int) (case_ind * ps.ind_size) + 1
4855+ return
4856+ ps.pcase ? (int) (case_ind * opt.ind_size) + 1
4857 : *s_lab == '#' ? 1
4858- : ps.ind_size * (ps.ind_level - label_offset) + 1;
4859+ : opt.ind_size * (ps.ind_level - label_offset) + 1;
4860 }
4861
4862
4863@@ -272,91 +305,96 @@ compute_label_target(void)
4864 void
4865 fill_buffer(void)
4866 { /* this routine reads stuff from the input */
4867- char *p;
4868- int i;
4869- FILE *f = input;
4870-
4871- if (bp_save != NULL) { /* there is a partly filled input buffer left */
4872- buf_ptr = bp_save; /* do not read anything, just switch buffers */
4873- buf_end = be_save;
4874- bp_save = be_save = NULL;
4875- if (buf_ptr < buf_end)
4876- return; /* only return if there is really something in
4877+ char *p;
4878+ int i;
4879+ FILE *f = input;
4880+ char *n;
4881+
4882+ if (bp_save != NULL) { /* there is a partly filled input buffer left */
4883+ buf_ptr = bp_save; /* donot read anything, just switch
4884+ * buffers */
4885+ buf_end = be_save;
4886+ bp_save = be_save = NULL;
4887+ if (buf_ptr < buf_end)
4888+ return; /* only return if there is really something in
4889 * this buffer */
4890- }
4891- for (p = in_buffer;;) {
4892- if (p >= in_buffer_limit) {
4893- int size = (in_buffer_limit - in_buffer) * 2 + 10;
4894- int offset = p - in_buffer;
4895- in_buffer = realloc(in_buffer, size);
4896- if (in_buffer == NULL)
4897- errx(1, "input line too long");
4898- p = in_buffer + offset;
4899- in_buffer_limit = in_buffer + size - 2;
4900 }
4901- if ((i = getc(f)) == EOF) {
4902- *p++ = ' ';
4903- *p++ = '\n';
4904- had_eof = true;
4905- break;
4906+ for (p = in_buffer;;) {
4907+ if (p >= in_buffer_limit) {
4908+ int size = (in_buffer_limit - in_buffer) * 2 + 10;
4909+ int offset = p - in_buffer;
4910+ n = (char *) realloc(in_buffer, size);
4911+ if (n == NULL)
4912+ errx(1, "input line too long");
4913+ in_buffer = n;
4914+ p = in_buffer + offset;
4915+ in_buffer_limit = in_buffer + size - 2;
4916+ }
4917+ if ((i = getc(f)) == EOF) {
4918+ *p++ = ' ';
4919+ *p++ = '\n';
4920+ had_eof = true;
4921+ break;
4922+ }
4923+ if (i != '\0')
4924+ *p++ = i;
4925+ if (i == '\n')
4926+ break;
4927 }
4928- if (i != '\0')
4929- *p++ = i;
4930- if (i == '\n')
4931- break;
4932- }
4933- buf_ptr = in_buffer;
4934- buf_end = p;
4935- if (p - in_buffer > 2 && p[-2] == '/' && p[-3] == '*') {
4936- if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
4937- fill_buffer(); /* flush indent error message */
4938- else {
4939- int com = 0;
4940-
4941- p = in_buffer;
4942- while (*p == ' ' || *p == '\t')
4943- p++;
4944- if (*p == '/' && p[1] == '*') {
4945- p += 2;
4946- while (*p == ' ' || *p == '\t')
4947- p++;
4948- if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
4949- && p[4] == 'N' && p[5] == 'T') {
4950- p += 6;
4951- while (*p == ' ' || *p == '\t')
4952- p++;
4953- if (*p == '*')
4954- com = 1;
4955- else if (*p == 'O') {
4956- if (*++p == 'N')
4957- p++, com = 1;
4958- else if (*p == 'F' && *++p == 'F')
4959- p++, com = 2;
4960- }
4961- while (*p == ' ' || *p == '\t')
4962- p++;
4963- if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
4964- if (s_com != e_com || s_lab != e_lab || s_code != e_code)
4965- dump_line();
4966- if (!(inhibit_formatting = com - 1)) {
4967- n_real_blanklines = 0;
4968- postfix_blankline_requested = 0;
4969- prefix_blankline_requested = 0;
4970- suppress_blanklines = 1;
4971+ buf_ptr = in_buffer;
4972+ buf_end = p;
4973+ if (p - in_buffer > 2 && p[-2] == '/' && p[-3] == '*') {
4974+ if (in_buffer[3] == 'I' && strncmp(in_buffer, "/**INDENT**", 11) == 0)
4975+ fill_buffer(); /* flush indent error message */
4976+ else {
4977+ int com = 0;
4978+
4979+ p = in_buffer;
4980+ while (*p == ' ' || *p == '\t')
4981+ p++;
4982+ if (*p == '/' && p[1] == '*') {
4983+ p += 2;
4984+ while (*p == ' ' || *p == '\t')
4985+ p++;
4986+ if (p[0] == 'I' && p[1] == 'N' && p[2] == 'D' && p[3] == 'E'
4987+ && p[4] == 'N' && p[5] == 'T') {
4988+ p += 6;
4989+ while (*p == ' ' || *p == '\t')
4990+ p++;
4991+ if (*p == '*')
4992+ com = 1;
4993+ else {
4994+ if (*p == 'O') {
4995+ if (*++p == 'N')
4996+ p++, com = 1;
4997+ else
4998+ if (*p == 'F' && *++p == 'F')
4999+ p++, com = 2;
5000+ }
5001+ }
5002+ while (*p == ' ' || *p == '\t')
5003+ p++;
5004+ if (p[0] == '*' && p[1] == '/' && p[2] == '\n' && com) {
5005+ if (s_com != e_com || s_lab != e_lab || s_code != e_code)
5006+ dump_line();
5007+ if (!(inhibit_formatting = com - 1)) {
5008+ n_real_blanklines = 0;
5009+ postfix_blankline_requested = 0;
5010+ prefix_blankline_requested = 0;
5011+ suppress_blanklines = 1;
5012+ }
5013+ }
5014+ }
5015 }
5016- }
5017 }
5018- }
5019 }
5020- }
5021- if (inhibit_formatting) {
5022- p = in_buffer;
5023- do
5024- putc(*p, output);
5025- while (*p++ != '\n');
5026- }
5027+ if (inhibit_formatting) {
5028+ p = in_buffer;
5029+ do
5030+ putc(*p, output);
5031+ while (*p++ != '\n');
5032+ }
5033 }
5034-
5035 /*
5036 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
5037 *
5038@@ -371,7 +409,7 @@ fill_buffer(void)
5039 * ALGORITHM: Put tabs and/or blanks into pobuf, then write pobuf.
5040 *
5041 * PARAMETERS: current integer The current column target
5042- * nteger The desired column
5043+ * target integer The desired column
5044 *
5045 * RETURNS: Integer value of the new column. (If current >= target, no action is
5046 * taken, and current is returned.
5047@@ -385,33 +423,26 @@ fill_buffer(void)
5048 * HISTORY: initial coding November 1976 D A Willcox of CAC
5049 *
5050 */
5051-static int
5052+int
5053 pad_output(int current, int target)
5054- /* writes tabs and blanks (if necessary) to
5055- * get the current output position up to the
5056- * target column */
5057- /* current: the current column value */
5058- /* target: position we want it at */
5059 {
5060- int curr; /* internal column pointer */
5061-
5062- if (current >= target)
5063- return (current); /* line is already long enough */
5064+ int curr; /* internal column pointer */
5065+ if (current >= target)
5066+ return (current); /* line is already long enough */
5067 curr = current;
5068 if (use_tabs) {
5069- int tcur;
5070+ int tcur;
5071
5072- while ((tcur = tabsize * (1 + (curr - 1) / tabsize) + 1) <= target) {
5073- putc('\t', output);
5074- curr = tcur;
5075+ while ((tcur = tabsize * (1 + (curr - 1) / tabsize) + 1) <= target) {
5076+ putc('\t', output);
5077+ curr = tcur;
5078+ }
5079 }
5080- }
5081- while (curr++ < target)
5082- putc(' ', output); /* pad with final blanks */
5083+ while (curr++ < target)
5084+ putc(' ', output); /* pad with final blanks */
5085
5086- return (target);
5087+ return (target);
5088 }
5089-
5090 /*
5091 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
5092 *
5093@@ -439,36 +470,58 @@ count_spaces_until(int cur, char *buffer, char *end)
5094 * printing the text in buffer starting at column "current"
5095 */
5096 {
5097- char *buf; /* used to look thru buffer */
5098-
5099- for (buf = buffer; *buf != '\0' && buf != end; ++buf) {
5100- switch (*buf) {
5101-
5102- case '\n':
5103- case 014: /* form feed */
5104- cur = 1;
5105- break;
5106-
5107- case '\t':
5108- cur = tabsize * (1 + (cur - 1) / tabsize) + 1;
5109- break;
5110-
5111- case 010: /* backspace */
5112- --cur;
5113- break;
5114-
5115- default:
5116- ++cur;
5117- break;
5118- } /* end of switch */
5119- } /* end of for loop */
5120- return (cur);
5121+ char *buf; /* used to look thru buffer */
5122+
5123+ for (buf = buffer; *buf != '\0' && buf != end; ++buf) {
5124+ switch (*buf) {
5125+
5126+ case '\n':
5127+ case 014: /* form feed */
5128+ cur = 1;
5129+ break;
5130+
5131+ case '\t':
5132+ cur = opt.tabsize * (1 + (cur - 1) / opt.tabsize) + 1;;
5133+ break;
5134+
5135+ case 010: /* backspace */
5136+ --cur;
5137+ break;
5138+
5139+ default:
5140+ ++cur;
5141+ break;
5142+ } /* end of switch */
5143+ } /* end of for loop */
5144+ return (cur);
5145 }
5146
5147 int
5148 count_spaces(int cur, char *buffer)
5149 {
5150- return (count_spaces_until(cur, buffer, NULL));
5151+ return (count_spaces_until(cur, buffer, NULL));
5152+}
5153+int found_err;
5154+
5155+void
5156+diag(int level, const char *msg, ...)
5157+{
5158+ va_list ap;
5159+
5160+ va_start(ap, msg);
5161+
5162+ if (level)
5163+ found_err = 1;
5164+ if (output == stdout) {
5165+ fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
5166+ vfprintf(stdout, msg, ap);
5167+ fprintf(stdout, " */\n");
5168+ } else {
5169+ fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
5170+ vfprintf(stdout, msg, ap);
5171+ fprintf(stderr, "\n");
5172+ }
5173+ va_end(ap);
5174 }
5175
5176 void
5177@@ -521,4 +574,3 @@ diag2(int level, const char *msg)
5178 fprintf(stderr, "\n");
5179 }
5180 }
5181-
5182diff --git a/lexi.c b/lexi.c
5183index d1a5fa7..4ec4723 100644
5184--- a/lexi.c
5185+++ b/lexi.c
5186@@ -1,7 +1,37 @@
5187-/*-
5188- * Copyright (c) 1985 Sun Microsystems, Inc.
5189+/* $NetBSD: lexi.c,v 1.14 2016/06/05 18:35:32 dholland Exp $ */
5190+
5191+/*
5192 * Copyright (c) 1980, 1993
5193 * The Regents of the University of California. All rights reserved.
5194+ *
5195+ * Redistribution and use in source and binary forms, with or without
5196+ * modification, are permitted provided that the following conditions
5197+ * are met:
5198+ * 1. Redistributions of source code must retain the above copyright
5199+ * notice, this list of conditions and the following disclaimer.
5200+ * 2. Redistributions in binary form must reproduce the above copyright
5201+ * notice, this list of conditions and the following disclaimer in the
5202+ * documentation and/or other materials provided with the distribution.
5203+ * 3. Neither the name of the University nor the names of its contributors
5204+ * may be used to endorse or promote products derived from this software
5205+ * without specific prior written permission.
5206+ *
5207+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5208+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5209+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5210+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5211+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5212+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5213+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5214+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5215+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5216+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5217+ * SUCH DAMAGE.
5218+ */
5219+
5220+/*
5221+ * Copyright (c) 1976 Board of Trustees of the University of Illinois.
5222+ * Copyright (c) 1985 Sun Microsystems, Inc.
5223 * All rights reserved.
5224 *
5225 * Redistribution and use in source and binary forms, with or without
5226@@ -33,13 +63,14 @@
5227 * SUCH DAMAGE.
5228 */
5229
5230-#if 0
5231+#include <sys/cdefs.h>
5232 #ifndef lint
5233+#if 0
5234 static char sccsid[] = "@(#)lexi.c 8.1 (Berkeley) 6/6/93";
5235-#endif /* not lint */
5236+#else
5237+__RCSID("$NetBSD: lexi.c,v 1.14 2016/06/05 18:35:32 dholland Exp $");
5238 #endif
5239-#include <sys/cdefs.h>
5240-__FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 309382 2016-12-02 01:52:32Z pfg $");
5241+#endif /* not lint */
5242
5243 /*
5244 * Here we have the token scanner for indent. It scans off one token and puts
5245@@ -52,18 +83,14 @@ __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 309382 2016-12-02 01:52:32Z pfg $
5246 #include <ctype.h>
5247 #include <stdlib.h>
5248 #include <string.h>
5249+#include <sys/param.h>
5250+
5251 #include "indent_globs.h"
5252 #include "indent_codes.h"
5253-#include "indent.h"
5254-
5255-#define alphanum 1
5256-#ifdef undef
5257-#define opchar 3
5258-#endif
5259
5260 struct templ {
5261- const char *rwd;
5262- int rwcode;
5263+ const char *rwd;
5264+ int rwcode;
5265 };
5266
5267 /*
5268@@ -72,74 +99,97 @@ struct templ {
5269 */
5270 struct templ specials[] =
5271 {
5272- {"_Bool", 4},
5273- {"_Complex", 4},
5274- {"_Imaginary", 4},
5275- {"auto", 10},
5276- {"bool", 4},
5277- {"break", 9},
5278- {"case", 8},
5279- {"char", 4},
5280- {"complex", 4},
5281- {"const", 4},
5282- {"continue", 12},
5283- {"default", 8},
5284- {"do", 6},
5285- {"double", 4},
5286- {"else", 6},
5287- {"enum", 3},
5288- {"extern", 10},
5289- {"float", 4},
5290- {"for", 5},
5291- {"global", 4},
5292- {"goto", 9},
5293- {"if", 5},
5294- {"imaginary", 4},
5295+ {"_Bool", 4},
5296+ {"_Complex", 4},
5297+ {"_Imaginary", 4},
5298+ {"auto", 10},
5299+ {"bool", 4},
5300+ {"break", 9},
5301+ {"case", 8},
5302+ {"char", 4},
5303+ {"complex", 4},
5304+ {"const", 4},
5305+ {"continue", 12},
5306+ {"default", 8},
5307+ {"do", 6},
5308+ {"double", 4},
5309+ {"else", 6},
5310+ {"enum", 3},
5311+ {"extern", 10},
5312+ {"float", 4},
5313+ {"for", 5},
5314+ {"global", 4},
5315+ {"goto", 9},
5316+ {"if", 5},
5317+ {"imaginary", 4},
5318 {"inline", 12},
5319- {"int", 4},
5320- {"long", 4},
5321- {"offsetof", 1},
5322- {"register", 10},
5323- {"restrict", 12},
5324- {"return", 9},
5325- {"short", 4},
5326- {"signed", 4},
5327- {"sizeof", 2},
5328- {"static", 10},
5329- {"struct", 3},
5330- {"switch", 7},
5331- {"typedef", 11},
5332- {"union", 3},
5333- {"unsigned", 4},
5334- {"void", 4},
5335- {"volatile", 4},
5336- {"while", 5}
5337+ {"int", 4},
5338+ {"long", 4},
5339+ {"offsetof", 1},
5340+ {"register", 10},
5341+ {"restrict", 12},
5342+ {"return", 9},
5343+ {"short", 4},
5344+ {"signed", 4},
5345+ {"sizeof", 2},
5346+ {"static", 10},
5347+ {"struct", 3},
5348+ {"switch", 7},
5349+ {"typedef", 11},
5350+ {"union", 3},
5351+ {"unsigned", 4},
5352+ {"void", 4},
5353+ {"volatile", 4},
5354+ {"while", 5},
5355+ {0, 0}
5356 };
5357
5358 const char **typenames;
5359 int typename_count;
5360 int typename_top = -1;
5361
5362-char chartype[128] =
5363-{ /* this is used to facilitate the decision of
5364- * what type (alphanumeric, operator) each
5365- * character is */
5366- 0, 0, 0, 0, 0, 0, 0, 0,
5367- 0, 0, 0, 0, 0, 0, 0, 0,
5368- 0, 0, 0, 0, 0, 0, 0, 0,
5369- 0, 0, 0, 0, 0, 0, 0, 0,
5370- 0, 3, 0, 0, 1, 3, 3, 0,
5371- 0, 0, 3, 3, 0, 3, 0, 3,
5372- 1, 1, 1, 1, 1, 1, 1, 1,
5373- 1, 1, 0, 0, 3, 3, 3, 3,
5374- 0, 1, 1, 1, 1, 1, 1, 1,
5375- 1, 1, 1, 1, 1, 1, 1, 1,
5376- 1, 1, 1, 1, 1, 1, 1, 1,
5377- 1, 1, 1, 0, 0, 0, 3, 1,
5378- 0, 1, 1, 1, 1, 1, 1, 1,
5379- 1, 1, 1, 1, 1, 1, 1, 1,
5380- 1, 1, 1, 1, 1, 1, 1, 1,
5381- 1, 1, 1, 0, 3, 0, 3, 0
5382+/*
5383+ * The transition table below was rewritten by hand from lx's output, given
5384+ * the following definitions. lx is Katherine Flavel's lexer generator.
5385+ *
5386+ * O = /[0-7]/; D = /[0-9]/; NZ = /[1-9]/;
5387+ * H = /[a-f0-9]/i; B = /[0-1]/; HP = /0x/i;
5388+ * BP = /0b/i; E = /e[+\-]?/i D+; P = /p[+\-]?/i D+;
5389+ * FS = /[fl]/i; IS = /u/i /(l|L|ll|LL)/? | /(l|L|ll|LL)/ /u/i?;
5390+ *
5391+ * D+ E FS? -> $float;
5392+ * D* "." D+ E? FS? -> $float;
5393+ * D+ "." E? FS? -> $float; HP H+ IS? -> $int;
5394+ * HP H+ P FS? -> $float; NZ D* IS? -> $int;
5395+ * HP H* "." H+ P FS? -> $float; "0" O* IS? -> $int;
5396+ * HP H+ "." P FS -> $float; BP B+ IS? -> $int;
5397+ */
5398+static char const *table[] = {
5399+ /* examples:
5400+ 00
5401+ s 0xx
5402+ t 00xaa
5403+ a 11 101100xxa..
5404+ r 11ee0001101lbuuxx.a.pp
5405+ t.01.e+008bLuxll0Ll.aa.p+0
5406+ states: ABCDEFGHIJKLMNOPQRSTUVWXYZ */
5407+ ['0'] = "CEIDEHHHIJQ U Q VUVVZZZ",
5408+ ['1'] = "DEIDEHHHIJQ U Q VUVVZZZ",
5409+ ['7'] = "DEIDEHHHIJ U VUVVZZZ",
5410+ ['9'] = "DEJDEHHHJJ U VUVVZZZ",
5411+ ['a'] = " U VUVV ",
5412+ ['b'] = " K U VUVV ",
5413+ ['e'] = " FFF FF U VUVV ",
5414+ ['f'] = " f f U VUVV f",
5415+ ['u'] = " MM M i iiM M ",
5416+ ['x'] = " N ",
5417+ ['p'] = " FFX ",
5418+ ['L'] = " LLf fL PR Li L f",
5419+ ['l'] = " OOf fO S P O i O f",
5420+ ['+'] = " G Y ",
5421+ ['.'] = "B EE EE T W ",
5422+ /* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
5423+ [0] = "uuiifuufiuuiiuiiiiiuiuuuuu",
5424 };
5425
5426 static int
5427@@ -148,523 +198,491 @@ strcmp_type(const void *e1, const void *e2)
5428 return (strcmp(e1, *(const char * const *)e2));
5429 }
5430
5431+
5432+
5433 int
5434 lexi(struct parser_state *state)
5435 {
5436- int unary_delim; /* this is set to 1 if the current token
5437+ int unary_delim; /* this is set to 1 if the current token
5438 * forces a following operator to be unary */
5439- int code; /* internal code to be returned */
5440- char qchar; /* the delimiter character for a string */
5441-
5442- e_token = s_token; /* point to start of place to save token */
5443- unary_delim = false;
5444- state->col_1 = state->last_nl; /* tell world that this token started
5445- * in column 1 iff the last thing
5446- * scanned was a newline */
5447- state->last_nl = false;
5448+ int code; /* internal code to be returned */
5449+ char qchar; /* the delimiter character for a string */
5450
5451- while (*buf_ptr == ' ' || *buf_ptr == '\t') { /* get rid of blanks */
5452- state->col_1 = false; /* leading blanks imply token is not in column
5453- * 1 */
5454- if (++buf_ptr >= buf_end)
5455- fill_buffer();
5456- }
5457-
5458- /* Scan an alphanumeric token */
5459- if (chartype[*buf_ptr & 127] == alphanum ||
5460- (buf_ptr[0] == '.' && isdigit((unsigned char)buf_ptr[1]))) {
5461- /*
5462- * we have a character or number
5463- */
5464- struct templ *p;
5465+ e_token = s_token; /* point to start of place to save token */
5466+ unary_delim = false;
5467+ state->col_1 = state->last_nl; /* tell world that this token started
5468+ * in column 1 iff the last thing
5469+ * scanned was a newline */
5470+ state->last_nl = false;
5471+
5472+ while (*buf_ptr == ' ' || *buf_ptr == '\t') { /* get rid of blanks */
5473+ state->col_1 = false; /* leading blanks imply token is not
5474+ * in column 1 */
5475+ if (++buf_ptr >= buf_end)
5476+ fill_buffer();
5477+ }
5478
5479- if (isdigit((unsigned char)*buf_ptr) ||
5480+ /* Scan an alphanumeric token */
5481+ if (isalnum((unsigned char)*buf_ptr) ||
5482+ *buf_ptr == '_' || *buf_ptr == '$' ||
5483 (buf_ptr[0] == '.' && isdigit((unsigned char)buf_ptr[1]))) {
5484- int seendot = 0,
5485- seenexp = 0,
5486- seensfx = 0;
5487-
5488- /*
5489- * base 2, base 8, base 16:
5490- */
5491- if (buf_ptr[0] == '0' && buf_ptr[1] != '.') {
5492- int len;
5493-
5494- if (buf_ptr[1] == 'b' || buf_ptr[1] == 'B')
5495- len = strspn(buf_ptr + 2, "01") + 2;
5496- else if (buf_ptr[1] == 'x' || buf_ptr[1] == 'X')
5497- len = strspn(buf_ptr + 2, "0123456789ABCDEFabcdef") + 2;
5498- else
5499- len = strspn(buf_ptr + 1, "012345678") + 1;
5500- if (len > 0) {
5501- CHECK_SIZE_TOKEN(len);
5502- memcpy(e_token, buf_ptr, len);
5503- e_token += len;
5504- buf_ptr += len;
5505- }
5506- else
5507- diag2(1, "Unterminated literal");
5508- }
5509- else /* base 10: */
5510- while (1) {
5511- if (*buf_ptr == '.') {
5512- if (seendot)
5513- break;
5514- else
5515- seendot++;
5516- }
5517- CHECK_SIZE_TOKEN(3);
5518- *e_token++ = *buf_ptr++;
5519- if (!isdigit((unsigned char)*buf_ptr) && *buf_ptr != '.') {
5520- if ((*buf_ptr != 'E' && *buf_ptr != 'e') || seenexp)
5521- break;
5522- else {
5523- seenexp++;
5524- seendot++;
5525- *e_token++ = *buf_ptr++;
5526- if (*buf_ptr == '+' || *buf_ptr == '-')
5527+ /*
5528+ * we have a character or number
5529+ */
5530+
5531+ struct templ *p;
5532+
5533+ if (isdigit((unsigned char)*buf_ptr) ||
5534+ (buf_ptr[0] == '.' && isdigit((unsigned char)buf_ptr[1]))) {
5535+ char s;
5536+ unsigned char i;
5537+
5538+ for (s = 'A'; s != 'f' && s != 'i' && s != 'u'; ) {
5539+ i = (unsigned char)*buf_ptr;
5540+ if (i >= nitems(table) || table[i] == NULL ||
5541+ table[i][s - 'A'] == ' ') {
5542+ s = table[0][s - 'A'];
5543+ break;
5544+ }
5545+ s = table[i][s - 'A'];
5546+ CHECK_SIZE_TOKEN(1);
5547+ *e_token++ = *buf_ptr++;
5548+ if (buf_ptr >= buf_end)
5549+ fill_buffer();
5550+ }
5551+ /* s now indicates the type: f(loating), i(integer), u(nknown) */
5552+ }
5553+ } else
5554+ while ((isalnum((unsigned char)*buf_ptr) || *buf_ptr == BACKSLASH ||
5555+ *buf_ptr == '_' || *buf_ptr == '$') {
5556+ /* fill_buffer() terminates buffer with newline */
5557+ if (*buf_ptr == BACKSLASH) {
5558+ if (*(buf_ptr + 1) == '\n') {
5559+ buf_ptr += 2;
5560+ if (buf_ptr >= buf_end)
5561+ fill_buffer();
5562+ } else
5563+ break;
5564+ }
5565+ CHECK_SIZE_TOKEN;
5566+ /* copy it over */
5567 *e_token++ = *buf_ptr++;
5568+ if (buf_ptr >= buf_end)
5569+ fill_buffer();
5570 }
5571- }
5572- }
5573+ *e_token++ = '\0';
5574
5575- while (1) {
5576- CHECK_SIZE_TOKEN(2);
5577- if (!(seensfx & 1) && (*buf_ptr == 'U' || *buf_ptr == 'u')) {
5578- *e_token++ = *buf_ptr++;
5579- seensfx |= 1;
5580- continue;
5581+ if (s_token[0] == 'L' && s_token[1] == '\0' &&
5582+ (*buf_ptr == '"' || *buf_ptr == '\''))
5583+ return (strpfx);
5584+
5585+ while (*buf_ptr == ' ' || *buf_ptr == '\t') { /* get rid of blanks */
5586+ if (++buf_ptr >= buf_end)
5587+ fill_buffer();
5588 }
5589- if (!(seensfx & 2) && (strchr("fFlL", *buf_ptr) != NULL)) {
5590- if (buf_ptr[1] == buf_ptr[0])
5591- *e_token++ = *buf_ptr++;
5592- *e_token++ = *buf_ptr++;
5593- seensfx |= 2;
5594- continue;
5595+ state->keyword = 0;
5596+ if (state->last_token == structure && !state->p_l_follow) { /* if last token was 'struct', then this token
5597+ * should be treated as a declaration */
5598+ state->last_u_d = true;
5599+ return (decl);
5600 }
5601- break;
5602- }
5603- }
5604- else
5605- while (chartype[*buf_ptr & 127] == alphanum || *buf_ptr == BACKSLASH) {
5606- /* fill_buffer() terminates buffer with newline */
5607- if (*buf_ptr == BACKSLASH) {
5608- if (*(buf_ptr + 1) == '\n') {
5609- buf_ptr += 2;
5610- if (buf_ptr >= buf_end)
5611- fill_buffer();
5612- } else
5613- break;
5614+ /*
5615+ * Operator after identifier is binary unless last token was 'struct'
5616+ */
5617+ state->last_u_d = (state->last_token == structure);
5618+
5619+ p = bsearch(s_token,
5620+ specials,
5621+ sizeof(specials) / sizeof(specials[0]),
5622+ sizeof(specials[0]),
5623+ strcmp_type);
5624+ if (p == NULL) { /* not a special keyword... */
5625+ char *u;
5626+
5627+ /* ... so maybe a type_t or a typedef */
5628+ if ((opt.auto_typedefs && ((u = strrchr(s_token, '_')) != NULL) &&
5629+ strcmp(u, "_t") == 0) || (typename_top >= 0 &&
5630+ bsearch(s_token, typenames, typename_top + 1,
5631+ sizeof(typenames[0]), strcmp_type))) {
5632+ ps.keyword = 4; /* a type name */
5633+ ps.last_u_d = true;
5634+ goto found_auto_typedef;
5635+ goto found_typename;
5636+ }
5637+ } else {
5638+ state->keyword = p->rwcode;
5639+ state->last_u_d = true;
5640+ switch (p->rwcode) {
5641+ case 7:/* it is a switch */
5642+ return (swstmt);
5643+ case 8:/* a case or default */
5644+ return (casestmt);
5645+
5646+ case 3:/* a "struct" */
5647+ if (ps.p_l_follow)
5648+ break; /* inside parens: cast */
5649+
5650+ case 4:/* one of the declaration keywords */
5651+ found_typename:
5652+ if (state->p_l_follow) {
5653+ /* inside parens: cast, param list, offsetof or sizeof */
5654+ state->cast_mask |= (1 << state->p_l_follow) & ~state->not_cast_mask;
5655+ }
5656+ if (state->last_token == period || state->last_token == unary_op) {
5657+ state->keyword = 0;
5658+ break;
5659+ }
5660+ if (p != NULL && p->rwcode == 3)
5661+ return (structure);
5662+ if (state->p_l_follow)
5663+ break;
5664+ return (decl);
5665+
5666+ case 5:/* if, while, for */
5667+ return (sp_paren);
5668+
5669+ case 6:/* do, else */
5670+ return (sp_nparen);
5671+
5672+ case 10:/*storage class specifier */
5673+ return(storage);
5674+
5675+ case 11: /* typedef */
5676+ return (type_def_);
5677+
5678+ default: /* all others are treated like any
5679+ * other identifier */
5680+ return (ident);
5681+ } /* end of switch */
5682+ } /* end of if (found_it) */
5683+ if (*buf_ptr == '(' && state->tos <= 1 && state->ind_level == 0 &&
5684+ state->in_parameter_declaration == 0 && state->block_init == 0) {
5685+ char *tp = buf_ptr;
5686+ while (tp < buf_end)
5687+ if (*tp++ == ')' && (*tp == ';' || *tp == ','))
5688+ goto not_proc;
5689+ strncpy(state->procname, token, sizeof state->procname - 1);
5690+ if (state->in_decl)
5691+ state->in_parameter_declaration = 1;
5692+ return (funcname);
5693+ not_proc: ;
5694+ }
5695+ /*
5696+ * The following hack attempts to guess whether or not the current
5697+ * token is in fact a declaration keyword -- one that has been
5698+ * typedefd
5699+ */
5700+ else if (!state->p_l_follow && !state->block_init &&
5701+ !state->in_stmt &&
5702+ ((*buf_ptr == '*' && buf_ptr[1] != '=') ||
5703+ isalpha((unsigned char)*buf_ptr)) &&
5704+ (state->last_token == semicolon || state->last_token == lbrace ||
5705+ state->last_token == rbrace)) {
5706+ state->keyword = 4; /* a type name */
5707+ state->last_u_d = true;
5708+ return decl;
5709 }
5710- CHECK_SIZE_TOKEN(1);
5711- /* copy it over */
5712- *e_token++ = *buf_ptr++;
5713- if (buf_ptr >= buf_end)
5714- fill_buffer();
5715- }
5716+ if (state->last_token == decl) /* if this is a declared variable,
5717+ * then following sign is unary */
5718+ state->last_u_d = true; /* will make "int a -1" work */
5719+ return (ident); /* the ident is not in the list */
5720+ } /* end of procesing for alpanum character */
5721+ /* Scan a non-alphanumeric token */
5722+ *e_token++ = *buf_ptr; /* if it is only a one-character token, it is
5723+ * moved here */
5724 *e_token = '\0';
5725+ if (++buf_ptr >= buf_end)
5726+ fill_buffer();
5727
5728- if (s_token[0] == 'L' && s_token[1] == '\0' &&
5729- (*buf_ptr == '"' || *buf_ptr == '\''))
5730- return (strpfx);
5731+ switch (*token) {
5732+ case '\n':
5733+ unary_delim = state->last_u_d;
5734+ state->last_nl = true; /* remember that we just had a newline */
5735+ code = (had_eof ? 0 : newline);
5736
5737- while (*buf_ptr == ' ' || *buf_ptr == '\t') { /* get rid of blanks */
5738- if (++buf_ptr >= buf_end)
5739- fill_buffer();
5740- }
5741- state->keyword = 0;
5742- if (state->last_token == structure && !state->p_l_follow) {
5743- /* if last token was 'struct' and we're not
5744- * in parentheses, then this token
5745- * should be treated as a declaration */
5746- state->last_u_d = true;
5747- return (decl);
5748- }
5749- /*
5750- * Operator after identifier is binary unless last token was 'struct'
5751- */
5752- state->last_u_d = (state->last_token == structure);
5753-
5754- p = bsearch(s_token,
5755- specials,
5756- sizeof(specials) / sizeof(specials[0]),
5757- sizeof(specials[0]),
5758- strcmp_type);
5759- if (p == NULL) { /* not a special keyword... */
5760- char *u;
5761-
5762- /* ... so maybe a type_t or a typedef */
5763- if ((auto_typedefs && ((u = strrchr(s_token, '_')) != NULL) &&
5764- strcmp(u, "_t") == 0) || (typename_top >= 0 &&
5765- bsearch(s_token, typenames, typename_top + 1,
5766- sizeof(typenames[0]), strcmp_type))) {
5767- state->keyword = 4; /* a type name */
5768- state->last_u_d = true;
5769- goto found_typename;
5770- }
5771- } else { /* we have a keyword */
5772- state->keyword = p->rwcode;
5773- state->last_u_d = true;
5774- switch (p->rwcode) {
5775- case 7: /* it is a switch */
5776- return (swstmt);
5777- case 8: /* a case or default */
5778- return (casestmt);
5779-
5780- case 3: /* a "struct" */
5781- /* FALLTHROUGH */
5782- case 4: /* one of the declaration keywords */
5783- found_typename:
5784- if (state->p_l_follow) {
5785- /* inside parens: cast, param list, offsetof or sizeof */
5786- state->cast_mask |= (1 << state->p_l_follow) & ~state->not_cast_mask;
5787- }
5788- if (state->last_token == period || state->last_token == unary_op) {
5789- state->keyword = 0;
5790- break;
5791+ /*
5792+ * if data has been exausted, the newline is a dummy, and we should
5793+ * return code to stop
5794+ */
5795+ break;
5796+
5797+ case '\'': /* start of quoted character */
5798+ case '"': /* start of string */
5799+ qchar = *token;
5800+ do { /* copy the string */
5801+ while (1) { /* move one character or
5802+ * [/<char>]<char> */
5803+ if (*buf_ptr == '\n') {
5804+ printf("%d: Unterminated literal\n", line_no);
5805+ goto stop_lit;
5806+ }
5807+ CHECK_SIZE_TOKEN; /* Only have to do this
5808+ * once in this loop,
5809+ * since CHECK_SIZE
5810+ * guarantees that there
5811+ * are at least 5
5812+ * entries left */
5813+ *e_token = *buf_ptr++;
5814+ if (buf_ptr >= buf_end)
5815+ fill_buffer();
5816+ if (*e_token == BACKSLASH) { /* if escape, copy extra
5817+ * char */
5818+ if (*buf_ptr == '\n') /* check for escaped
5819+ * newline */
5820+ ++line_no;
5821+ *++e_token = *buf_ptr++;
5822+ ++e_token; /* we must increment
5823+ * this again because we
5824+ * copied two chars */
5825+ if (buf_ptr >= buf_end)
5826+ fill_buffer();
5827+ } else
5828+ break; /* we copied one character */
5829+ } /* end of while (1) */
5830+ } while (*e_token++ != qchar);
5831+stop_lit:
5832+ code = ident;
5833+ break;
5834+
5835+ case ('('):
5836+ case ('['):
5837+ unary_delim = true;
5838+ code = lparen;
5839+ break;
5840+
5841+ case (')'):
5842+ case (']'):
5843+ code = rparen;
5844+ break;
5845+
5846+ case '#':
5847+ unary_delim = state->last_u_d;
5848+ code = preesc;
5849+ break;
5850+
5851+ case '?':
5852+ unary_delim = true;
5853+ code = question;
5854+ break;
5855+
5856+ case (':'):
5857+ code = colon;
5858+ unary_delim = true;
5859+ break;
5860+
5861+ case (';'):
5862+ unary_delim = true;
5863+ code = semicolon;
5864+ break;
5865+
5866+ case ('{'):
5867+ unary_delim = true;
5868+
5869+ /*
5870+ * if (state->in_or_st) state->block_init = 1;
5871+ */
5872+ /* ? code = state->block_init ? lparen : lbrace; */
5873+ code = lbrace;
5874+ break;
5875+
5876+ case ('}'):
5877+ unary_delim = true;
5878+ /* ? code = state->block_init ? rparen : rbrace; */
5879+ code = rbrace;
5880+ break;
5881+
5882+ case 014: /* a form feed */
5883+ unary_delim = state->last_u_d;
5884+ state->last_nl = true; /* remember this so we can set
5885+ * 'state->col_1' right */
5886+ code = form_feed;
5887+ break;
5888+
5889+ case (','):
5890+ unary_delim = true;
5891+ code = comma;
5892+ break;
5893+
5894+ case '.':
5895+ unary_delim = false;
5896+ code = period;
5897+ break;
5898+
5899+ case '-':
5900+ case '+': /* check for -, +, --, ++ */
5901+ code = (state->last_u_d ? unary_op : binary_op);
5902+ unary_delim = true;
5903+
5904+ if (*buf_ptr == token[0]) {
5905+ /* check for doubled character */
5906+ *e_token++ = *buf_ptr++;
5907+ /* buffer overflow will be checked at end of loop */
5908+ if (state->last_token == ident || state->last_token == rparen) {
5909+ code = (state->last_u_d ? unary_op : postop);
5910+ /* check for following ++ or -- */
5911+ unary_delim = false;
5912+ }
5913+ } else
5914+ if (*buf_ptr == '=')
5915+ /* check for operator += */
5916+ *e_token++ = *buf_ptr++;
5917+ else
5918+ if (*buf_ptr == '>') {
5919+ /* check for operator -> */
5920+ *e_token++ = *buf_ptr++;
5921+ unary_delim = false;
5922+ code = unary_op;
5923+ state->want_blank = false;
5924+ }
5925+ break; /* buffer overflow will be checked at end of
5926+ * switch */
5927+
5928+ case '=':
5929+ if (state->in_or_st)
5930+ state->block_init = 1;
5931+
5932+ if (*buf_ptr == '=') { /* == */
5933+ *e_token++ = '='; /* Flip =+ to += */
5934+ buf_ptr++;
5935+ *e_token = 0;
5936 }
5937- if (p != NULL && p->rwcode == 3)
5938- return (structure);
5939- if (state->p_l_follow)
5940- break;
5941- return (decl);
5942-
5943- case 5: /* if, while, for */
5944- return (sp_paren);
5945-
5946- case 6: /* do, else */
5947- return (sp_nparen);
5948-
5949- case 10: /* storage class specifier */
5950- return (storage);
5951-
5952- case 11: /* typedef */
5953- return (type_def);
5954-
5955- default: /* all others are treated like any other
5956- * identifier */
5957- return (ident);
5958- } /* end of switch */
5959- } /* end of if (found_it) */
5960- if (*buf_ptr == '(' && state->tos <= 1 && state->ind_level == 0 &&
5961- state->in_parameter_declaration == 0 && state->block_init == 0) {
5962- char *tp = buf_ptr;
5963- while (tp < buf_end)
5964- if (*tp++ == ')' && (*tp == ';' || *tp == ','))
5965- goto not_proc;
5966- strncpy(state->procname, token, sizeof state->procname - 1);
5967- if (state->in_decl)
5968- state->in_parameter_declaration = 1;
5969- return (funcname);
5970- not_proc:;
5971- }
5972- /*
5973- * The following hack attempts to guess whether or not the current
5974- * token is in fact a declaration keyword -- one that has been
5975- * typedefd
5976- */
5977- else if (!state->p_l_follow && !state->block_init &&
5978- !state->in_stmt &&
5979- ((*buf_ptr == '*' && buf_ptr[1] != '=') ||
5980- isalpha((unsigned char)*buf_ptr)) &&
5981- (state->last_token == semicolon || state->last_token == lbrace ||
5982- state->last_token == rbrace)) {
5983- state->keyword = 4; /* a type name */
5984- state->last_u_d = true;
5985- return decl;
5986- }
5987- if (state->last_token == decl) /* if this is a declared variable,
5988- * then following sign is unary */
5989- state->last_u_d = true; /* will make "int a -1" work */
5990- return (ident); /* the ident is not in the list */
5991- } /* end of procesing for alpanum character */
5992
5993- /* Scan a non-alphanumeric token */
5994+ code = binary_op;
5995+ unary_delim = true;
5996+ break;
5997+ /* can drop thru!!! */
5998+
5999+ case '>':
6000+ case '<':
6001+ case '!': /* ops like <, <<, <=, !=, etc */
6002+ if (*buf_ptr == '>' || *buf_ptr == '<' || *buf_ptr == '=') {
6003+ *e_token++ = *buf_ptr;
6004+ if (++buf_ptr >= buf_end)
6005+ fill_buffer();
6006+ }
6007+ if (*buf_ptr == '=')
6008+ *e_token++ = *buf_ptr++;
6009+ code = (state->last_u_d ? unary_op : binary_op);
6010+ unary_delim = true;
6011+ break;
6012
6013- CHECK_SIZE_TOKEN(3); /* things like "<<=" */
6014- *e_token++ = *buf_ptr; /* if it is only a one-character token, it is
6015- * moved here */
6016- *e_token = '\0';
6017- if (++buf_ptr >= buf_end)
6018- fill_buffer();
6019-
6020- switch (*token) {
6021- case '\n':
6022- unary_delim = state->last_u_d;
6023- state->last_nl = true; /* remember that we just had a newline */
6024- code = (had_eof ? 0 : newline);
6025-
6026- /*
6027- * if data has been exhausted, the newline is a dummy, and we should
6028- * return code to stop
6029- */
6030- break;
6031-
6032- case '\'': /* start of quoted character */
6033- case '"': /* start of string */
6034- qchar = *token;
6035- do { /* copy the string */
6036- while (1) { /* move one character or [/<char>]<char> */
6037- if (*buf_ptr == '\n') {
6038- diag2(1, "Unterminated literal");
6039- goto stop_lit;
6040+ case '*':
6041+ unary_delim = true;
6042+ if (!state->last_u_d) {
6043+ if (*buf_ptr == '=')
6044+ *e_token++ = *buf_ptr++;
6045+ code = binary_op;
6046+ break;
6047 }
6048- CHECK_SIZE_TOKEN(2);
6049- *e_token = *buf_ptr++;
6050- if (buf_ptr >= buf_end)
6051- fill_buffer();
6052- if (*e_token == BACKSLASH) { /* if escape, copy extra char */
6053- if (*buf_ptr == '\n') /* check for escaped newline */
6054- ++line_no;
6055- *++e_token = *buf_ptr++;
6056- ++e_token; /* we must increment this again because we
6057- * copied two chars */
6058- if (buf_ptr >= buf_end)
6059- fill_buffer();
6060+ while (*buf_ptr == '*' || isspace((unsigned char)*buf_ptr)) {
6061+ if (*buf_ptr == '*')
6062+ *e_token++ = *buf_ptr;
6063+ if (++buf_ptr >= buf_end)
6064+ fill_buffer();
6065 }
6066- else
6067- break; /* we copied one character */
6068- } /* end of while (1) */
6069- } while (*e_token++ != qchar);
6070-stop_lit:
6071- code = ident;
6072- break;
6073-
6074- case ('('):
6075- case ('['):
6076- unary_delim = true;
6077- code = lparen;
6078- break;
6079-
6080- case (')'):
6081- case (']'):
6082- code = rparen;
6083- break;
6084-
6085- case '#':
6086- unary_delim = state->last_u_d;
6087- code = preesc;
6088- break;
6089-
6090- case '?':
6091- unary_delim = true;
6092- code = question;
6093- break;
6094-
6095- case (':'):
6096- code = colon;
6097- unary_delim = true;
6098- break;
6099-
6100- case (';'):
6101- unary_delim = true;
6102- code = semicolon;
6103- break;
6104-
6105- case ('{'):
6106- unary_delim = true;
6107-
6108- /*
6109- * if (state->in_or_st) state->block_init = 1;
6110- */
6111- /* ? code = state->block_init ? lparen : lbrace; */
6112- code = lbrace;
6113- break;
6114-
6115- case ('}'):
6116- unary_delim = true;
6117- /* ? code = state->block_init ? rparen : rbrace; */
6118- code = rbrace;
6119- break;
6120-
6121- case 014: /* a form feed */
6122- unary_delim = state->last_u_d;
6123- state->last_nl = true; /* remember this so we can set 'state->col_1'
6124- * right */
6125- code = form_feed;
6126- break;
6127-
6128- case (','):
6129- unary_delim = true;
6130- code = comma;
6131- break;
6132-
6133- case '.':
6134- unary_delim = false;
6135- code = period;
6136- break;
6137-
6138- case '-':
6139- case '+': /* check for -, +, --, ++ */
6140- code = (state->last_u_d ? unary_op : binary_op);
6141- unary_delim = true;
6142-
6143- if (*buf_ptr == token[0]) {
6144- /* check for doubled character */
6145- *e_token++ = *buf_ptr++;
6146- /* buffer overflow will be checked at end of loop */
6147- if (state->last_token == ident || state->last_token == rparen) {
6148- code = (state->last_u_d ? unary_op : postop);
6149- /* check for following ++ or -- */
6150- unary_delim = false;
6151- }
6152- }
6153- else if (*buf_ptr == '=')
6154- /* check for operator += */
6155- *e_token++ = *buf_ptr++;
6156- else if (*buf_ptr == '>') {
6157- /* check for operator -> */
6158- *e_token++ = *buf_ptr++;
6159- unary_delim = false;
6160- code = unary_op;
6161- state->want_blank = false;
6162- }
6163- break; /* buffer overflow will be checked at end of
6164- * switch */
6165+ if (ps.in_decl) {
6166+ char *tp = buf_ptr;
6167+
6168+ while (isalpha((unsigned char)*tp) ||
6169+ isspace((unsigned char)*tp)) {
6170+ if (++tp >= buf_end)
6171+ fill_buffer();
6172+ }
6173+ if (*tp == '(')
6174+ ps.procname[0] = ' ';
6175+ }
6176+ code = unary_op;
6177+ break;
6178
6179- case '=':
6180- if (state->in_or_st)
6181- state->block_init = 1;
6182-#ifdef undef
6183- if (chartype[*buf_ptr & 127] == opchar) { /* we have two char assignment */
6184- e_token[-1] = *buf_ptr++;
6185- if ((e_token[-1] == '<' || e_token[-1] == '>') && e_token[-1] == *buf_ptr)
6186- *e_token++ = *buf_ptr++;
6187- *e_token++ = '='; /* Flip =+ to += */
6188- *e_token = 0;
6189- }
6190-#else
6191- if (*buf_ptr == '=') {/* == */
6192- *e_token++ = '='; /* Flip =+ to += */
6193- buf_ptr++;
6194- *e_token = 0;
6195- }
6196-#endif
6197- code = binary_op;
6198- unary_delim = true;
6199- break;
6200- /* can drop thru!!! */
6201-
6202- case '>':
6203- case '<':
6204- case '!': /* ops like <, <<, <=, !=, etc */
6205- if (*buf_ptr == '>' || *buf_ptr == '<' || *buf_ptr == '=') {
6206- *e_token++ = *buf_ptr;
6207- if (++buf_ptr >= buf_end)
6208- fill_buffer();
6209- }
6210- if (*buf_ptr == '=')
6211- *e_token++ = *buf_ptr++;
6212- code = (state->last_u_d ? unary_op : binary_op);
6213- unary_delim = true;
6214- break;
6215-
6216- case '*':
6217- unary_delim = true;
6218- if (!state->last_u_d) {
6219- if (*buf_ptr == '=')
6220- *e_token++ = *buf_ptr++;
6221- code = binary_op;
6222- break;
6223- }
6224- while (*buf_ptr == '*' || isspace((unsigned char)*buf_ptr)) {
6225- if (*buf_ptr == '*') {
6226- CHECK_SIZE_TOKEN(1);
6227- *e_token++ = *buf_ptr;
6228- }
6229- if (++buf_ptr >= buf_end)
6230- fill_buffer();
6231- }
6232- if (ps.in_decl) {
6233- char *tp = buf_ptr;
6234-
6235- while (isalpha((unsigned char)*tp) ||
6236- isspace((unsigned char)*tp)) {
6237- if (++tp >= buf_end)
6238- fill_buffer();
6239- }
6240- if (*tp == '(')
6241- ps.procname[0] = ' ';
6242- }
6243- code = unary_op;
6244- break;
6245+ default:
6246+ if (token[0] == '/' && *buf_ptr == '*') {
6247+ /* it is start of comment */
6248+ *e_token++ = '*';
6249
6250- default:
6251- if (token[0] == '/' && *buf_ptr == '*') {
6252- /* it is start of comment */
6253- *e_token++ = '*';
6254+ if (++buf_ptr >= buf_end)
6255+ fill_buffer();
6256
6257- if (++buf_ptr >= buf_end)
6258- fill_buffer();
6259+ code = comment;
6260+ unary_delim = state->last_u_d;
6261+ break;
6262+ }
6263+ while (*(e_token - 1) == *buf_ptr || *buf_ptr == '=') {
6264+ /*
6265+ * handle ||, &&, etc, and also things as in int *****i
6266+ */
6267+ *e_token++ = *buf_ptr;
6268+ if (++buf_ptr >= buf_end)
6269+ fill_buffer();
6270+ }
6271+ code = (state->last_u_d ? unary_op : binary_op);
6272+ unary_delim = true;
6273
6274- code = comment;
6275- unary_delim = state->last_u_d;
6276- break;
6277- }
6278- while (*(e_token - 1) == *buf_ptr || *buf_ptr == '=') {
6279- /*
6280- * handle ||, &&, etc, and also things as in int *****i
6281- */
6282- CHECK_SIZE_TOKEN(1);
6283- *e_token++ = *buf_ptr;
6284- if (++buf_ptr >= buf_end)
6285- fill_buffer();
6286- }
6287- code = (state->last_u_d ? unary_op : binary_op);
6288- unary_delim = true;
6289
6290+ } /* end of switch */
6291+ if (buf_ptr >= buf_end) /* check for input buffer empty */
6292+ fill_buffer();
6293+ state->last_u_d = unary_delim;
6294+ *e_token = '\0'; /* null terminate the token */
6295+ return (code);
6296+}
6297
6298- } /* end of switch */
6299- if (buf_ptr >= buf_end) /* check for input buffer empty */
6300- fill_buffer();
6301- state->last_u_d = unary_delim;
6302- CHECK_SIZE_TOKEN(1);
6303- *e_token = '\0'; /* null terminate the token */
6304- return (code);
6305+/* Initialize constant transition table */
6306+void
6307+init_constant_tt(void)
6308+{
6309+ table['-'] = table['+'];
6310+ table['8'] = table['9'];
6311+ table['2'] = table['3'] = table['4'] = table['5'] = table['6'] = table['7'];
6312+ table['A'] = table['C'] = table['D'] = table['c'] = table['d'] = table['a'];
6313+ table['B'] = table['b'];
6314+ table['E'] = table['e'];
6315+ table['U'] = table['u'];
6316+ table['X'] = table['x'];
6317+ table['P'] = table['p'];
6318+ table['F'] = table['f'];
6319 }
6320
6321 void
6322 alloc_typenames(void)
6323 {
6324-
6325- typenames = (const char **)malloc(sizeof(typenames[0]) *
6326+ typenames = (const char **)mallroc(sizeof(typenames[0]) *
6327 (typename_count = 16));
6328 if (typenames == NULL)
6329- err(1, NULL);
6330+ err(1, NULL);
6331 }
6332
6333 void
6334 add_typename(const char *key)
6335 {
6336 int comparison;
6337- const char *copy;
6338+ const char *copy;
6339
6340 if (typename_top + 1 >= typename_count) {
6341- typenames = realloc((void *)typenames,
6342- sizeof(typenames[0]) * (typename_count *= 2));
6343- if (typenames == NULL)
6344- err(1, NULL);
6345+ typenames = realloc((void *)typenames,
6346+ sizeof(typenames[0]) * (typename_count *= 2));
6347+ if (typenames == NULL)
6348+ err(1, NULL);
6349 }
6350 if (typename_top == -1)
6351- typenames[++typename_top] = copy = strdup(key);
6352+ typenames[++typename_top] = copy = strdup(key);
6353 else if ((comparison = strcmp(key, typenames[typename_top])) >= 0) {
6354- /* take advantage of sorted input */
6355- if (comparison == 0) /* remove duplicates */
6356- return;
6357- typenames[++typename_top] = copy = strdup(key);
6358+ /* take advantage of sorted input */
6359+ if (comparison == 0) /* remove duplicates */
6360+ return;
6361+ typenames[++typename_top] = copy = strdup(key);
6362 }
6363 else {
6364- int p;
6365-
6366- for (p = 0; (comparison = strcmp(key, typenames[p])) > 0; p++)
6367- /* find place for the new key */;
6368- if (comparison == 0) /* remove duplicates */
6369- return;
6370- memmove(&typenames[p + 1], &typenames[p],
6371- sizeof(typenames[0]) * (++typename_top - p));
6372- typenames[p] = copy = strdup(key);
6373- }
6374+ int p;
6375+
6376+ for (p = 0; (comparison = strcmp(key, typenames[p])) > 0; p++)
6377+ /* find place for the new key */;
6378+ if (comparison == 0) /* remove duplicates */
6379+ return;
6380+ memmove(&typenames[p + 1], &typenames[p],
6381+ sizeof(typenames[0]) * (++typename_top - p));
6382+ typenames[p] = copy = strdup(key);
6383+ }
6384
6385- if (copy == NULL)
6386- err(1, NULL);
6387+ if (copy == NULL)
6388+ err(1, NULL);
6389 }
6390diff --git a/parse.c b/parse.c
6391index e95d8d8..08c5d48 100644
6392--- a/parse.c
6393+++ b/parse.c
6394@@ -1,7 +1,37 @@
6395-/*-
6396- * Copyright (c) 1985 Sun Microsystems, Inc.
6397+/* $NetBSD: parse.c,v 1.7 2003/08/07 11:14:09 agc Exp $ */
6398+
6399+/*
6400 * Copyright (c) 1980, 1993
6401 * The Regents of the University of California. All rights reserved.
6402+ *
6403+ * Redistribution and use in source and binary forms, with or without
6404+ * modification, are permitted provided that the following conditions
6405+ * are met:
6406+ * 1. Redistributions of source code must retain the above copyright
6407+ * notice, this list of conditions and the following disclaimer.
6408+ * 2. Redistributions in binary form must reproduce the above copyright
6409+ * notice, this list of conditions and the following disclaimer in the
6410+ * documentation and/or other materials provided with the distribution.
6411+ * 3. Neither the name of the University nor the names of its contributors
6412+ * may be used to endorse or promote products derived from this software
6413+ * without specific prior written permission.
6414+ *
6415+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
6416+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6417+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6418+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
6419+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6420+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6421+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6422+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6423+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6424+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6425+ * SUCH DAMAGE.
6426+ */
6427+
6428+/*
6429+ * Copyright (c) 1976 Board of Trustees of the University of Illinois.
6430+ * Copyright (c) 1985 Sun Microsystems, Inc.
6431 * All rights reserved.
6432 *
6433 * Redistribution and use in source and binary forms, with or without
6434@@ -33,194 +63,195 @@
6435 * SUCH DAMAGE.
6436 */
6437
6438+#include <sys/cdefs.h>
6439+
6440 #if 0
6441 #ifndef lint
6442 static char sccsid[] = "@(#)parse.c 8.1 (Berkeley) 6/6/93";
6443-#endif /* not lint */
6444-#endif
6445+#else
6446+__RCSID("$NetBSD: parse.c,v 1.7 2003/08/07 11:14:09 agc Exp $");
6447
6448-#include <sys/cdefs.h>
6449-__FBSDID("$FreeBSD: head/usr.bin/indent/parse.c 309419 2016-12-02 16:42:45Z pfg $");
6450+#endif /* not lint */
6451+#endif
6452
6453 #include <err.h>
6454 #include <stdio.h>
6455 #include "indent_globs.h"
6456 #include "indent_codes.h"
6457-#include "indent.h"
6458-
6459-static void reduce(void);
6460
6461+/* tk: the code for the construct scanned */
6462 void
6463-parse(int tk) /* tk: the code for the construct scanned */
6464+parse(int tk)
6465 {
6466- int i;
6467+ int i;
6468
6469 #ifdef debug
6470- printf("%2d - %s\n", tk, token);
6471+ printf("%2d - %s\n", tk, token);
6472 #endif
6473
6474- while (ps.p_stack[ps.tos] == ifhead && tk != elselit) {
6475- /* true if we have an if without an else */
6476- ps.p_stack[ps.tos] = stmt; /* apply the if(..) stmt ::= stmt
6477- * reduction */
6478- reduce(); /* see if this allows any reduction */
6479- }
6480+ while (ps.p_stack[ps.tos] == ifhead && tk != elselit) {
6481+ /* true if we have an if without an else */
6482+ ps.p_stack[ps.tos] = stmt; /* apply the if(..) stmt ::=
6483+ * stmt reduction */
6484+ reduce(); /* see if this allows any reduction */
6485+ }
6486
6487
6488- switch (tk) { /* go on and figure out what to do with the
6489+ switch (tk) { /* go on and figure out what to do with the
6490 * input */
6491
6492- case decl: /* scanned a declaration word */
6493- ps.search_brace = btype_2;
6494- /* indicate that following brace should be on same line */
6495- if (ps.p_stack[ps.tos] != decl) { /* only put one declaration
6496- * onto stack */
6497- break_comma = true; /* while in declaration, newline should be
6498- * forced after comma */
6499- ps.p_stack[++ps.tos] = decl;
6500- ps.il[ps.tos] = ps.i_l_follow;
6501-
6502- if (ps.ljust_decl) {/* only do if we want left justified
6503- * declarations */
6504- ps.ind_level = 0;
6505- for (i = ps.tos - 1; i > 0; --i)
6506- if (ps.p_stack[i] == decl)
6507- ++ps.ind_level; /* indentation is number of
6508- * declaration levels deep we are */
6509- ps.i_l_follow = ps.ind_level;
6510- }
6511- }
6512- break;
6513-
6514- case ifstmt: /* scanned if (...) */
6515- if (ps.p_stack[ps.tos] == elsehead && ps.else_if) /* "else if ..." */
6516- /*
6517- * Note that the stack pointer here is decremented, effectively
6518- * reducing "else if" to "if". This saves a lot of stack space
6519- * in case of a long "if-else-if ... else-if" sequence.
6520- */
6521- ps.i_l_follow = ps.il[ps.tos--];
6522- /* the rest is the same as for dolit and forstmt */
6523- case dolit: /* 'do' */
6524- case forstmt: /* for (...) */
6525- ps.p_stack[++ps.tos] = tk;
6526- ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
6527- ++ps.i_l_follow; /* subsequent statements should be indented 1 */
6528- ps.search_brace = btype_2;
6529- break;
6530-
6531- case lbrace: /* scanned { */
6532- break_comma = false; /* don't break comma in an initial list */
6533- if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl
6534- || ps.p_stack[ps.tos] == stmtl)
6535- ++ps.i_l_follow; /* it is a random, isolated stmt group or a
6536- * declaration */
6537- else {
6538- if (s_code == e_code) {
6539- /*
6540- * only do this if there is nothing on the line
6541- */
6542- --ps.ind_level;
6543- /*
6544- * it is a group as part of a while, for, etc.
6545- */
6546- if (ps.p_stack[ps.tos] == swstmt && ps.case_indent >= 1)
6547- --ps.ind_level;
6548- /*
6549- * for a switch, brace should be two levels out from the code
6550- */
6551- }
6552- }
6553+ case decl: /* scanned a declaration word */
6554+ ps.search_brace = opt.btype_2;
6555+ /* indicate that following brace should be on same line */
6556+ if (ps.p_stack[ps.tos] != decl) { /* only put one
6557+ * declaration onto
6558+ * stack */
6559+ break_comma = true; /* while in declaration,
6560+ * newline should be forced
6561+ * after comma */
6562+ ps.p_stack[++ps.tos] = decl;
6563+ ps.il[ps.tos] = ps.i_l_follow;
6564+
6565+ if (opt.ljust_decl) { /* only do if we want left
6566+ * justified declarations */
6567+ ps.ind_level = 0;
6568+ for (i = ps.tos - 1; i > 0; --i)
6569+ if (ps.p_stack[i] == decl)
6570+ ++ps.ind_level; /* indentation is number
6571+ * of declaration levels
6572+ * deep we are */
6573+ ps.i_l_follow = ps.ind_level;
6574+ }
6575+ }
6576+ break;
6577
6578- ps.p_stack[++ps.tos] = lbrace;
6579- ps.il[ps.tos] = ps.ind_level;
6580- ps.p_stack[++ps.tos] = stmt;
6581- /* allow null stmt between braces */
6582- ps.il[ps.tos] = ps.i_l_follow;
6583- break;
6584-
6585- case whilestmt: /* scanned while (...) */
6586- if (ps.p_stack[ps.tos] == dohead) {
6587- /* it is matched with do stmt */
6588- ps.ind_level = ps.i_l_follow = ps.il[ps.tos];
6589- ps.p_stack[++ps.tos] = whilestmt;
6590- ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
6591- }
6592- else { /* it is a while loop */
6593- ps.p_stack[++ps.tos] = whilestmt;
6594- ps.il[ps.tos] = ps.i_l_follow;
6595- ++ps.i_l_follow;
6596- ps.search_brace = btype_2;
6597- }
6598+ case ifstmt: /* scanned if (...) */
6599+ if (ps.p_stack[ps.tos] == elsehead && opt.else_if) /* "else if ..." */
6600+ /*
6601+ * Note that the stack pointer here is decremented, effectively
6602+ * reducing "else if" to "if". This saves a lot of stack space
6603+ * in case of a long "if-else-if ... else-if" sequence.
6604+ */
6605+ ps.i_l_follow = ps.il[ps.tos--];
6606+ /* the rest is the same as for dolit and forstmt */
6607+
6608+ case dolit: /* 'do' */
6609+ case forstmt: /* for (...) */
6610+ ps.p_stack[++ps.tos] = tk;
6611+ ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
6612+ ++ps.i_l_follow;/* subsequent statements should be indented 1 */
6613+ ps.search_brace = opt.btype_2;
6614+ break;
6615
6616- break;
6617+ case lbrace: /* scanned { */
6618+ break_comma = false; /* don't break comma in an initial
6619+ * list */
6620+ if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl
6621+ || ps.p_stack[ps.tos] == stmtl)
6622+ ++ps.i_l_follow; /* it is a random, isolated
6623+ * stmt group or a declaration */
6624+ else {
6625+ if (s_code == e_code) {
6626+ /*
6627+ * only do this if there is nothing on the line
6628+ */
6629+ --ps.ind_level;
6630+ /*
6631+ * it is a group as part of a while, for, etc.
6632+ */
6633+ if (ps.p_stack[ps.tos] == swstmt && ps.case_indent >= 1)
6634+ --ps.ind_level;
6635+ /*
6636+ * for a switch, brace should be two levels out from the code
6637+ */
6638+ }
6639+ }
6640+
6641+ ps.p_stack[++ps.tos] = lbrace;
6642+ ps.il[ps.tos] = ps.ind_level;
6643+ ps.p_stack[++ps.tos] = stmt;
6644+ /* allow null stmt between braces */
6645+ ps.il[ps.tos] = ps.i_l_follow;
6646+ break;
6647
6648- case elselit: /* scanned an else */
6649+ case whilestmt: /* scanned while (...) */
6650+ if (ps.p_stack[ps.tos] == dohead) {
6651+ /* it is matched with do stmt */
6652+ ps.ind_level = ps.i_l_follow = ps.il[ps.tos];
6653+ ps.p_stack[++ps.tos] = whilestmt;
6654+ ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
6655+ } else { /* it is a while loop */
6656+ ps.p_stack[++ps.tos] = whilestmt;
6657+ ps.il[ps.tos] = ps.i_l_follow;
6658+ ++ps.i_l_follow;
6659+ ps.search_brace = opt.btype_2;
6660+ }
6661
6662- if (ps.p_stack[ps.tos] != ifhead)
6663- diag2(1, "Unmatched 'else'");
6664- else {
6665- ps.ind_level = ps.il[ps.tos]; /* indentation for else should
6666- * be same as for if */
6667- ps.i_l_follow = ps.ind_level + 1; /* everything following should
6668- * be in 1 level */
6669- ps.p_stack[ps.tos] = elsehead;
6670- /* remember if with else */
6671- ps.search_brace = btype_2 | ps.else_if;
6672- }
6673- break;
6674+ break;
6675
6676- case rbrace: /* scanned a } */
6677- /* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
6678- if (ps.tos > 0 && ps.p_stack[ps.tos - 1] == lbrace) {
6679- ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];
6680- ps.p_stack[ps.tos] = stmt;
6681- }
6682- else
6683- diag2(1, "Statement nesting error");
6684- break;
6685-
6686- case swstmt: /* had switch (...) */
6687- ps.p_stack[++ps.tos] = swstmt;
6688- ps.cstk[ps.tos] = case_ind;
6689- /* save current case indent level */
6690- ps.il[ps.tos] = ps.i_l_follow;
6691- case_ind = ps.i_l_follow + ps.case_indent; /* cases should be one
6692- * level down from
6693- * switch */
6694- ps.i_l_follow += ps.case_indent + 1; /* statements should be two
6695- * levels in */
6696- ps.search_brace = btype_2;
6697- break;
6698-
6699- case semicolon: /* this indicates a simple stmt */
6700- break_comma = false; /* turn off flag to break after commas in a
6701- * declaration */
6702- ps.p_stack[++ps.tos] = stmt;
6703- ps.il[ps.tos] = ps.ind_level;
6704- break;
6705-
6706- default: /* this is an error */
6707- diag2(1, "Unknown code to parser");
6708- return;
6709-
6710-
6711- } /* end of switch */
6712-
6713- if (ps.tos >= nitems(ps.p_stack) - 1)
6714- errx(1, "Parser stack overflow");
6715-
6716- reduce(); /* see if any reduction can be done */
6717+ case elselit: /* scanned an else */
6718+
6719+ if (ps.p_stack[ps.tos] != ifhead)
6720+ diag(1, "Unmatched 'else'");
6721+ else {
6722+ ps.ind_level = ps.il[ps.tos]; /* indentation for else
6723+ * should be same as for
6724+ * if */
6725+ ps.i_l_follow = ps.ind_level + 1; /* everything following
6726+ * should be in 1 level */
6727+ ps.p_stack[ps.tos] = elsehead;
6728+ /* remember if with else */
6729+ ps.search_brace = opt.btype_2 | opt.else_if;
6730+ }
6731+ break;
6732+
6733+ case rbrace: /* scanned a } */
6734+ /* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
6735+ if (ps.tos > 0 && ps.p_stack[ps.tos - 1] == lbrace) {
6736+ ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];
6737+ ps.p_stack[ps.tos] = stmt;
6738+ } else
6739+ diag(1, "Stmt nesting error.");
6740+ break;
6741+
6742+ case swstmt: /* had switch (...) */
6743+ ps.p_stack[++ps.tos] = swstmt;
6744+ ps.cstk[ps.tos] = case_ind;
6745+ /* save current case indent level */
6746+ ps.il[ps.tos] = ps.i_l_follow;
6747+ case_ind = ps.i_l_follow + ps.case_indent; /* cases should be one
6748+ * level down from
6749+ * switch */
6750+ ps.i_l_follow += ps.case_indent + 1; /* statements should be
6751+ * two levels in */
6752+ ps.search_brace = opt.btype_2;
6753+ break;
6754+
6755+ case semicolon: /* this indicates a simple stmt */
6756+ break_comma = false; /* turn off flag to break after commas
6757+ * in a declaration */
6758+ ps.p_stack[++ps.tos] = stmt;
6759+ ps.il[ps.tos] = ps.ind_level;
6760+ break;
6761+
6762+ default: /* this is an error */
6763+ diag(1, "Unknown code to parser");
6764+ return;
6765+
6766+
6767+ } /* end of switch */
6768+
6769+ if (ps.tos >= STACKSIZE - 1)
6770+ errx(1, "Parser stack overflow.")
6771+
6772+ reduce(); /* see if any reduction can be done */
6773
6774 #ifdef debug
6775- for (i = 1; i <= ps.tos; ++i)
6776- printf("(%d %d)", ps.p_stack[i], ps.il[i]);
6777- printf("\n");
6778+ for (i = 1; i <= ps.tos; ++i)
6779+ printf("(%d %d)", ps.p_stack[i], ps.il[i]);
6780+ printf("\n");
6781 #endif
6782-
6783- return;
6784 }
6785-
6786 /*
6787 * NAME: reduce
6788 *
6789@@ -260,83 +291,84 @@ parse(int tk) /* tk: the code for the construct scanned */
6790 /*----------------------------------------------*\
6791 | REDUCTION PHASE |
6792 \*----------------------------------------------*/
6793-static void
6794+void
6795 reduce(void)
6796 {
6797- int i;
6798-
6799- for (;;) { /* keep looping until there is nothing left to
6800- * reduce */
6801-
6802- switch (ps.p_stack[ps.tos]) {
6803-
6804- case stmt:
6805- switch (ps.p_stack[ps.tos - 1]) {
6806-
6807- case stmt:
6808- case stmtl:
6809- /* stmtl stmt or stmt stmt */
6810- ps.p_stack[--ps.tos] = stmtl;
6811- break;
6812-
6813- case dolit: /* <do> <stmt> */
6814- ps.p_stack[--ps.tos] = dohead;
6815- ps.i_l_follow = ps.il[ps.tos];
6816- break;
6817
6818- case ifstmt:
6819- /* <if> <stmt> */
6820- ps.p_stack[--ps.tos] = ifhead;
6821- for (i = ps.tos - 1;
6822- (
6823- ps.p_stack[i] != stmt
6824- &&
6825- ps.p_stack[i] != stmtl
6826- &&
6827- ps.p_stack[i] != lbrace
6828- );
6829- --i);
6830- ps.i_l_follow = ps.il[i];
6831- /*
6832- * for the time being, we will assume that there is no else on
6833- * this if, and set the indentation level accordingly. If an
6834- * else is scanned, it will be fixed up later
6835- */
6836- break;
6837-
6838- case swstmt:
6839- /* <switch> <stmt> */
6840- case_ind = ps.cstk[ps.tos - 1];
6841- /* FALLTHROUGH */
6842- case decl: /* finish of a declaration */
6843- case elsehead:
6844- /* <<if> <stmt> else> <stmt> */
6845- case forstmt:
6846- /* <for> <stmt> */
6847- case whilestmt:
6848- /* <while> <stmt> */
6849- ps.p_stack[--ps.tos] = stmt;
6850- ps.i_l_follow = ps.il[ps.tos];
6851- break;
6852-
6853- default: /* <anything else> <stmt> */
6854- return;
6855+ int i;
6856
6857- } /* end of section for <stmt> on top of stack */
6858- break;
6859-
6860- case whilestmt: /* while (...) on top */
6861- if (ps.p_stack[ps.tos - 1] == dohead) {
6862- /* it is termination of a do while */
6863- ps.tos -= 2;
6864- break;
6865- }
6866- else
6867- return;
6868-
6869- default: /* anything else on top */
6870- return;
6871+ for (;;) { /* keep looping until there is nothing left to
6872+ * reduce */
6873
6874+ switch (ps.p_stack[ps.tos]) {
6875+
6876+ case stmt:
6877+ switch (ps.p_stack[ps.tos - 1]) {
6878+
6879+ case stmt:
6880+ case stmtl:
6881+ /* stmtl stmt or stmt stmt */
6882+ ps.p_stack[--ps.tos] = stmtl;
6883+ break;
6884+
6885+ case dolit: /* <do> <stmt> */
6886+ ps.p_stack[--ps.tos] = dohead;
6887+ ps.i_l_follow = ps.il[ps.tos];
6888+ break;
6889+
6890+ case ifstmt:
6891+ /* <if> <stmt> */
6892+ ps.p_stack[--ps.tos] = ifhead;
6893+ for (i = ps.tos - 1;
6894+ (
6895+ ps.p_stack[i] != stmt
6896+ &&
6897+ ps.p_stack[i] != stmtl
6898+ &&
6899+ ps.p_stack[i] != lbrace
6900+ );
6901+ --i);
6902+ ps.i_l_follow = ps.il[i];
6903+ /*
6904+ * for the time being, we will assume that there is no else on
6905+ * this if, and set the indentation level accordingly. If an
6906+ * else is scanned, it will be fixed up later
6907+ */
6908+ break;
6909+
6910+ case swstmt:
6911+ /* <switch> <stmt> */
6912+ case_ind = ps.cstk[ps.tos - 1];
6913+
6914+ /* FALLTHROUGH */
6915+ case decl: /* finish of a declaration */
6916+ case elsehead:
6917+ /* <<if> <stmt> else> <stmt> */
6918+ case forstmt:
6919+ /* <for> <stmt> */
6920+ case whilestmt:
6921+ /* <while> <stmt> */
6922+ ps.p_stack[--ps.tos] = stmt;
6923+ ps.i_l_follow = ps.il[ps.tos];
6924+ break;
6925+
6926+ default: /* <anything else> <stmt> */
6927+ return;
6928+
6929+ } /* end of section for <stmt> on top of stack */
6930+ break;
6931+
6932+ case whilestmt:/* while (...) on top */
6933+ if (ps.p_stack[ps.tos - 1] == dohead) {
6934+ /* it is termination of a do while */
6935+ ps.p_stack[--ps.tos] = stmt;
6936+ break;
6937+ } else
6938+ return;
6939+
6940+ default: /* anything else on top */
6941+ return;
6942+
6943+ }
6944 }
6945- }
6946 }
6947diff --git a/pr_comment.c b/pr_comment.c
6948index b79182e..48e087c 100644
6949--- a/pr_comment.c
6950+++ b/pr_comment.c
6951@@ -1,7 +1,37 @@
6952-/*-
6953- * Copyright (c) 1985 Sun Microsystems, Inc.
6954+/* $NetBSD: pr_comment.c,v 1.10 2016/02/25 13:23:27 ginsbach Exp $ */
6955+
6956+/*
6957 * Copyright (c) 1980, 1993
6958 * The Regents of the University of California. All rights reserved.
6959+ *
6960+ * Redistribution and use in source and binary forms, with or without
6961+ * modification, are permitted provided that the following conditions
6962+ * are met:
6963+ * 1. Redistributions of source code must retain the above copyright
6964+ * notice, this list of conditions and the following disclaimer.
6965+ * 2. Redistributions in binary form must reproduce the above copyright
6966+ * notice, this list of conditions and the following disclaimer in the
6967+ * documentation and/or other materials provided with the distribution.
6968+ * 3. Neither the name of the University nor the names of its contributors
6969+ * may be used to endorse or promote products derived from this software
6970+ * without specific prior written permission.
6971+ *
6972+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
6973+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6974+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6975+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
6976+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6977+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6978+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6979+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6980+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6981+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6982+ * SUCH DAMAGE.
6983+ */
6984+
6985+/*
6986+ * Copyright (c) 1976 Board of Trustees of the University of Illinois.
6987+ * Copyright (c) 1985 Sun Microsystems, Inc.
6988 * All rights reserved.
6989 *
6990 * Redistribution and use in source and binary forms, with or without
6991@@ -33,22 +63,25 @@
6992 * SUCH DAMAGE.
6993 */
6994
6995+#include <sys/cdefs.h>
6996+
6997 #if 0
6998 #ifndef lint
6999 static char sccsid[] = "@(#)pr_comment.c 8.1 (Berkeley) 6/6/93";
7000-#endif /* not lint */
7001-#endif
7002+#else
7003+__RCSID("$NetBSD: pr_comment.c,v 1.10 2016/02/25 13:23:27 ginsbach Exp $");
7004
7005-#include <sys/cdefs.h>
7006-__FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 309343 2016-12-01 01:56:34Z pfg $");
7007+#endif /* not lint */
7008+#endif
7009
7010 #include <err.h>
7011 #include <stdio.h>
7012 #include <stdlib.h>
7013 #include <string.h>
7014+#include <ctype.h>
7015 #include "indent_globs.h"
7016 #include "indent_codes.h"
7017-#include "indent.h"
7018+
7019 /*
7020 * NAME:
7021 * pr_comment
7022@@ -80,273 +113,267 @@ __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 309343 2016-12-01 01:56:34Z
7023 * beginning of the input line are removed
7024 */
7025
7026+
7027 void
7028 pr_comment(void)
7029 {
7030- int now_col; /* column we are in now */
7031- int adj_max_col; /* Adjusted max_col for when we decide to
7032+ int now_col; /* column we are in now */
7033+ int adj_max_col; /* Adjusted max_col for when we decide to
7034 * spill comments over the right margin */
7035- char *last_bl; /* points to the last blank in the output
7036+ char *last_bl; /* points to the last blank in the output
7037 * buffer */
7038- char *t_ptr; /* used for moving string */
7039- int break_delim = comment_delimiter_on_blankline;
7040- int l_just_saw_decl = ps.just_saw_decl;
7041- adj_max_col = max_col;
7042- ps.just_saw_decl = 0;
7043- last_bl = NULL; /* no blanks found so far */
7044- ps.box_com = false; /* at first, assume that we are not in
7045- * a boxed comment or some other
7046- * comment that should not be touched */
7047- ++ps.out_coms; /* keep track of number of comments */
7048-
7049- /* Figure where to align and how to treat the comment */
7050+ char *t_ptr; /* used for moving string */
7051+ int break_delim = opt.comment_delimiter_on_blankline;
7052+ int l_just_saw_decl = ps.just_saw_decl;
7053+
7054+ adj_max_col = opt.max_col;
7055+ ps.just_saw_decl = 0;
7056+ last_bl = NULL; /* no blanks found so far */
7057+ ps.box_com = false; /* at first, assume that we are not in a boxed
7058+ * comment or some other comment that should
7059+ * not be touched */
7060+ ++ps.out_coms; /* keep track of number of comments */
7061+
7062+ /* Figure where to align and how to treat the comment */
7063
7064- if (ps.col_1 && !format_col1_comments) { /* if comment starts in column
7065- * 1 it should not be touched */
7066- ps.box_com = true;
7067- break_delim = false;
7068- ps.com_col = 1;
7069- }
7070- else {
7071- if (*buf_ptr == '-' || *buf_ptr == '*' ||
7072- (*buf_ptr == '\n' && !format_block_comments)) {
7073- ps.box_com = true; /* A comment with a '-' or '*' immediately
7074- * after the /+* is assumed to be a boxed
7075- * comment. A comment with a newline
7076- * immediately after the /+* is assumed to
7077- * be a block comment and is treated as a
7078- * box comment unless format_block_comments
7079- * is nonzero (the default). */
7080- break_delim = false;
7081- }
7082- if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
7083- /* klg: check only if this line is blank */
7084- /*
7085- * If this (*and previous lines are*) blank, dont put comment way
7086- * out at left
7087- */
7088- ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
7089- adj_max_col = block_comment_max_col;
7090- if (ps.com_col <= 1)
7091- ps.com_col = 1 + !format_col1_comments;
7092+ if (ps.col_1 && !opt.format_col1_comments) { /* if comment starts in
7093+ * column 1 it should
7094+ * not be touched */
7095+ ps.box_com = true;
7096+ break_delim = false;
7097+ ps.com_col = 1;
7098+ } else {
7099+ if (*buf_ptr == '-' || *buf_ptr == '*' ||
7100+ (*buf_ptr == '\n' && !opt.format_block_comments)) {
7101+ ps.box_com = true; /* a comment with a '-', '*'
7102+ * or newline immediately
7103+ * after the start comment is
7104+ * assumed to be a boxed
7105+ * comment */
7106+ break_delim = false;
7107+ }
7108+ if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
7109+ /* klg: check only if this line is blank */
7110+ /*
7111+ * If this (*and previous lines are*) blank, don't
7112+ * put comment way out at left
7113+ */
7114+ ps.com_col = (ps.ind_level - opt.unindent_displace) * opt.ind_size + 1;
7115+ adj_max_col = opt.block_comment_max_col;
7116+ if (ps.com_col <= 1)
7117+ ps.com_col = 1 + !opt.format_col1_comments;
7118+ } else {
7119+ int target_col;
7120+ break_delim = false;
7121+ if (s_code != e_code)
7122+ target_col = count_spaces(compute_code_target(), s_code);
7123+ else {
7124+ target_col = 1;
7125+ if (s_lab != e_lab)
7126+ target_col = count_spaces(compute_label_target(), s_lab);
7127+ }
7128+ ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? opt.decl_com_ind : opt.com_ind;
7129+ if (ps.com_col <= target_col)
7130+ ps.com_col = ((target_col + 7) & ~7) + 1;
7131+ if (ps.com_col + 24 > adj_max_col)
7132+ adj_max_col = ps.com_col + 24;
7133+ }
7134 }
7135- else {
7136- int target_col;
7137- break_delim = false;
7138- if (s_code != e_code)
7139- target_col = count_spaces(compute_code_target(), s_code);
7140- else {
7141- target_col = 1;
7142- if (s_lab != e_lab)
7143- target_col = count_spaces(compute_label_target(), s_lab);
7144- }
7145- ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? ps.decl_com_ind : ps.com_ind;
7146- if (ps.com_col <= target_col)
7147- ps.com_col = tabsize * (1 + (target_col - 1) / tabsize) + 1;
7148- if (ps.com_col + 24 > adj_max_col)
7149- adj_max_col = ps.com_col + 24;
7150+ if (ps.box_com) {
7151+ /*
7152+ * Find out how much indentation there was originally, because that
7153+ * much will have to be ignored by pad_output() in dump_line(). This
7154+ * is a box comment, so nothing changes -- not even indentation.
7155+ *
7156+ * The comment we're about to read usually comes from in_buffer,
7157+ * unless it has been copied into save_com.
7158+ */
7159+ char *start;
7160+
7161+ start = buf_ptr >= save_com && buf_ptr < save_com + sc_size ? sc_buf : in_buffer;
7162+ ps.n_comment_delta = 1 - count_spaces_until(1, start, buf_ptr - 2);
7163+ } else {
7164+ ps.n_comment_delta = 0;
7165+ while (*buf_ptr == ' ' || *buf_ptr == '\t')
7166+ buf_ptr++;
7167 }
7168- }
7169- if (ps.box_com) {
7170- /*
7171- * Find out how much indentation there was originally, because that
7172- * much will have to be ignored by pad_output() in dump_line(). This
7173- * is a box comment, so nothing changes -- not even indentation.
7174- *
7175- * The comment we're about to read usually comes from in_buffer,
7176- * unless it has been copied into save_com.
7177- */
7178- char *start;
7179-
7180- start = buf_ptr >= save_com && buf_ptr < save_com + sc_size ?
7181- sc_buf : in_buffer;
7182- ps.n_comment_delta = 1 - count_spaces_until(1, start, buf_ptr - 2);
7183- }
7184- else {
7185- ps.n_comment_delta = 0;
7186- while (*buf_ptr == ' ' || *buf_ptr == '\t')
7187- buf_ptr++;
7188- }
7189- ps.comment_delta = 0;
7190- *e_com++ = '/'; /* put '/' followed by '*' into buffer */
7191- *e_com++ = '*';
7192- if (*buf_ptr != ' ' && !ps.box_com)
7193- *e_com++ = ' ';
7194+ ps.comment_delta = 0;
7195+ *e_com++ = '/'; /* put '/' + '*' into buffer */
7196+ *e_com++ = '*';
7197+ if (*buf_ptr != ' ' && !ps.box_com)
7198+ *e_com++ = ' ';
7199
7200 /*
7201- * Don't put a break delimiter if this is a one-liner that won't wrap.
7202- */
7203+ * Don't put a break delimiter if this comment is a one-liner
7204+ */
7205 if (break_delim)
7206- for (t_ptr = buf_ptr; *t_ptr != '\0' && *t_ptr != '\n'; t_ptr++) {
7207- if (t_ptr >= buf_end)
7208- fill_buffer();
7209- if (t_ptr[0] == '*' && t_ptr[1] == '/') {
7210- if (adj_max_col >= count_spaces_until(ps.com_col, buf_ptr, t_ptr + 2))
7211- break_delim = false;
7212- break;
7213- }
7214- }
7215+ for (t_ptr = buf_ptr; *t_ptr != '\0' && *t_ptr != '\n'; t_ptr++) {
7216+ if (t_ptr >= buf_end)
7217+ fill_buffer();
7218+ if (t_ptr[0] == '*' && t_ptr[1] == '/') {
7219+ break_delim = false;
7220+ break;
7221+ }
7222+ }
7223
7224 if (break_delim) {
7225- char *t = e_com;
7226- e_com = s_com + 2;
7227- *e_com = 0;
7228- if (blanklines_before_blockcomments && ps.last_token != lbrace)
7229- prefix_blankline_requested = 1;
7230- dump_line();
7231- e_com = s_com = t;
7232- if (!ps.box_com && star_comment_cont)
7233- *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
7234+ char *t = e_com;
7235+ e_com = s_com + 2;
7236+ *e_com = 0;
7237+ if (opt.blanklines_before_blockcomments && ps.last_token != lbrace)
7238+ prefix_blankline_requested = 1;
7239+ dump_line();
7240+ e_com = s_com = t;
7241+ if (!ps.box_com && opt.star_comment_cont)
7242+ *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
7243 }
7244
7245- /* Start to copy the comment */
7246
7247- while (1) { /* this loop will go until the comment is
7248+ /* Start to copy the comment */
7249+
7250+ while (1) { /* this loop will go until the comment is
7251 * copied */
7252- switch (*buf_ptr) { /* this checks for various spcl cases */
7253- case 014: /* check for a form feed */
7254- CHECK_SIZE_COM(3);
7255- if (!ps.box_com) { /* in a text comment, break the line here */
7256- ps.use_ff = true;
7257- /* fix so dump_line uses a form feed */
7258- dump_line();
7259- last_bl = NULL;
7260- if (!ps.box_com && star_comment_cont)
7261- *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
7262- while (*++buf_ptr == ' ' || *buf_ptr == '\t')
7263- ;
7264- }
7265- else {
7266- if (++buf_ptr >= buf_end)
7267- fill_buffer();
7268- *e_com++ = 014;
7269- }
7270- break;
7271+ CHECK_SIZE_COM;
7272+ switch (*buf_ptr) { /* this checks for various spcl cases */
7273+ case 014: /* check for a form feed */
7274+ if (!ps.box_com) { /* in a text comment, break
7275+ * the line here */
7276+ ps.use_ff = true;
7277+ /* fix so dump_line uses a form feed */
7278+ dump_line();
7279+ last_bl = NULL;
7280+ if (!ps.box_com && opt.star_comment_cont)
7281+ *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
7282+
7283+ while (*++buf_ptr == ' ' || *buf_ptr == '\t')
7284+ ;
7285+ } else {
7286+ if (++buf_ptr >= buf_end)
7287+ fill_buffer();
7288+ *e_com++ = 014;
7289+ }
7290+ break;
7291
7292- case '\n':
7293- if (had_eof) { /* check for unexpected eof */
7294- printf("Unterminated comment\n");
7295- dump_line();
7296- return;
7297- }
7298- last_bl = NULL;
7299- CHECK_SIZE_COM(4);
7300- if (ps.box_com || ps.last_nl) { /* if this is a boxed comment,
7301- * we dont ignore the newline */
7302- if (s_com == e_com)
7303- *e_com++ = ' ';
7304- if (!ps.box_com && e_com - s_com > 3) {
7305- dump_line();
7306- if (star_comment_cont)
7307- *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
7308- }
7309- dump_line();
7310- if (!ps.box_com && star_comment_cont)
7311- *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
7312- }
7313- else {
7314- ps.last_nl = 1;
7315- if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
7316- last_bl = e_com - 1;
7317- /*
7318- * if there was a space at the end of the last line, remember
7319- * where it was
7320- */
7321- else { /* otherwise, insert one */
7322- last_bl = e_com;
7323- *e_com++ = ' ';
7324- }
7325- }
7326- ++line_no; /* keep track of input line number */
7327- if (!ps.box_com) {
7328- int nstar = 1;
7329- do { /* flush any blanks and/or tabs at start of
7330- * next line */
7331- if (++buf_ptr >= buf_end)
7332- fill_buffer();
7333- if (*buf_ptr == '*' && --nstar >= 0) {
7334- if (++buf_ptr >= buf_end)
7335- fill_buffer();
7336- if (*buf_ptr == '/')
7337- goto end_of_comment;
7338- }
7339- } while (*buf_ptr == ' ' || *buf_ptr == '\t');
7340- }
7341- else if (++buf_ptr >= buf_end)
7342- fill_buffer();
7343- break; /* end of case for newline */
7344+ case '\n':
7345+ if (had_eof) { /* check for unexpected eof */
7346+ printf("Unterminated comment\n");
7347+ dump_line();
7348+ return;
7349+ }
7350+ last_bl = NULL;
7351+ if (ps.box_com || ps.last_nl) { /* if this is a boxed
7352+ * comment, we don't
7353+ * ignore the newline */
7354+ if (s_com == e_com)
7355+ *e_com++ = ' ';
7356+
7357+ if (!ps.box_com && e_com - s_com > 3) {
7358+ dump_line();
7359+ if (opt.star_comment_cont)
7360+ *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
7361+ }
7362+ dump_line();
7363+ if (!ps.box_com && opt.star_comment_cont)
7364+ *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
7365+ } else {
7366+ ps.last_nl = 1;
7367+ if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
7368+ last_bl = e_com - 1;
7369+ /*
7370+ * if there was a space at the end of the last line, remember
7371+ * where it was
7372+ */
7373+ else { /* otherwise, insert one */
7374+ last_bl = e_com;
7375+ CHECK_SIZE_COM;
7376+ *e_com++ = ' ';
7377+ }
7378+ }
7379+ ++line_no; /* keep track of input line number */
7380+ if (!ps.box_com) {
7381+ int nstar = 1;
7382+ do { /* flush any blanks and/or tabs at
7383+ * start of next line */
7384+ if (++buf_ptr >= buf_end)
7385+ fill_buffer();
7386+ if (*buf_ptr == '*' && --nstar >= 0) {
7387+ if (++buf_ptr >= buf_end)
7388+ fill_buffer();
7389+ if (*buf_ptr == '/')
7390+ goto end_of_comment;
7391+ }
7392+ } while (*buf_ptr == ' ' || *buf_ptr == '\t');
7393+ } else
7394+ if (++buf_ptr >= buf_end)
7395+ fill_buffer();
7396+ break; /* end of case for newline */
7397
7398- case '*': /* must check for possibility of being at end
7399+ case '*': /* must check for possibility of being at end
7400 * of comment */
7401- if (++buf_ptr >= buf_end) /* get to next char after * */
7402- fill_buffer();
7403- CHECK_SIZE_COM(4);
7404- if (*buf_ptr == '/') { /* it is the end!!! */
7405- end_of_comment:
7406- if (++buf_ptr >= buf_end)
7407- fill_buffer();
7408- if (break_delim) {
7409- if (e_com > s_com + 3) {
7410- dump_line();
7411- }
7412- else
7413- s_com = e_com;
7414- *e_com++ = ' ';
7415- }
7416- if (e_com[-1] != ' ' && e_com[-1] != '\t' && !ps.box_com)
7417- *e_com++ = ' '; /* ensure blank before end */
7418- *e_com++ = '*', *e_com++ = '/', *e_com = '\0';
7419- ps.just_saw_decl = l_just_saw_decl;
7420- return;
7421- }
7422- else /* handle isolated '*' */
7423- *e_com++ = '*';
7424- break;
7425- default: /* we have a random char */
7426- now_col = count_spaces_until(ps.com_col, s_com, e_com);
7427- do {
7428- CHECK_SIZE_COM(1);
7429- *e_com = *buf_ptr++;
7430- if (buf_ptr >= buf_end)
7431- fill_buffer();
7432- if (*e_com == ' ' || *e_com == '\t')
7433- last_bl = e_com; /* remember we saw a blank */
7434- ++e_com;
7435- now_col++;
7436- } while (!memchr("*\n\r\b\t", *buf_ptr, 6) &&
7437- (now_col <= adj_max_col || !last_bl));
7438- ps.last_nl = false;
7439- if (now_col > adj_max_col && !ps.box_com && e_com[-1] > ' ') {
7440- /*
7441- * the comment is too long, it must be broken up
7442- */
7443- if (last_bl == NULL) {
7444- dump_line();
7445- if (!ps.box_com && star_comment_cont)
7446- *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
7447- break;
7448- }
7449- *e_com = '\0';
7450- e_com = last_bl;
7451- dump_line();
7452- if (!ps.box_com && star_comment_cont)
7453- *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
7454- for (t_ptr = last_bl + 1; *t_ptr == ' ' || *t_ptr == '\t';
7455- t_ptr++)
7456- ;
7457- last_bl = NULL;
7458- /*
7459- * t_ptr will be somewhere between e_com (dump_line() reset)
7460- * and l_com. So it's safe to copy byte by byte from t_ptr
7461- * to e_com without any CHECK_SIZE_COM().
7462- */
7463- while (*t_ptr != '\0') {
7464- if (*t_ptr == ' ' || *t_ptr == '\t')
7465- last_bl = e_com;
7466+ if (++buf_ptr >= buf_end) /* get to next char
7467+ * after * */
7468+ fill_buffer();
7469+
7470+ if (*buf_ptr == '/') { /* it is the end!!! */
7471+ end_of_comment:
7472+ if (++buf_ptr >= buf_end)
7473+ fill_buffer();
7474+
7475+
7476+ CHECK_SIZE_COM;
7477+ if (break_delim) {
7478+ if (e_com > s_com + 3) {
7479+ dump_line();
7480+ }
7481+ }
7482+ if (e_com[-1] != ' ' && e_com[-1] != '\t' && !ps.box_com)
7483+ *e_com++ = ' '; /* ensure blank before end */
7484+ *e_com++ = '*', *e_com++ = '/', *e_com = '\0';
7485+ ps.just_saw_decl = l_just_saw_decl;
7486+ return;
7487+ } else /* handle isolated '*' */
7488+ *e_com++ = '*';
7489+ break;
7490+ default: /* we have a random char */
7491+ now_col = count_spaces_until(ps.com_col, s_com, e_com);
7492+ do {
7493+ *e_com = *buf_ptr++;
7494+ if (buf_ptr >= buf_end)
7495+ fill_buffer();
7496+ if (*e_com == ' ' || *e_com == '\t')
7497+ last_bl = e_com; /* remember we saw a blank */
7498+ ++e_com;
7499+ now_col++;
7500+ } while (!memchr("*\n\r\b\t", *buf_ptr, 6) &&
7501+ (now_col <= adj_max_col || !last_bl));
7502+ ps.last_nl = false;
7503+ if (now_col > adj_max_col && !ps.box_com && !iscntrl((unsigned char)e_com[-1])
7504+ && !isblank((unsigned char)e_com[-1])) {
7505+ /*
7506+ * the comment is too long, it must be broken up
7507+ */
7508+ if (last_bl == NULL) { /* we have seen no blanks */
7509+ dump_line();
7510+ if (!ps.box_com && opt.star_comment_cont)
7511+ *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
7512+ break;
7513+ }
7514+ *e_com = '\0';
7515+ e_com = last_bl;
7516+ dump_line();
7517+ if (!ps.box_com && opt.star_comment_cont)
7518+ *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
7519+ for (t_ptr = last_bl + 1; *t_ptr == ' ' || *t_ptr == '\t';
7520+ t_ptr++)
7521+ ;
7522+ last_bl = NULL;
7523+ while (*t_ptr != '\0') {
7524+ if (*t_ptr == ' ' || *t_ptr == '\t')
7525+ last_bl = e_com;
7526 *e_com++ = *t_ptr++;
7527+ }
7528+ }
7529+ break;
7530 }
7531- }
7532- break;
7533 }
7534- }
7535 }
7536diff --git a/tests/comments.0 b/tests/comments.0
7537index 7b65c2e..77ccb10 100644
7538--- a/tests/comments.0
7539+++ b/tests/comments.0
7540@@ -1,4 +1,4 @@
7541-/* $FreeBSD$ */
7542+/* $NetBSD$ */
7543 typedef enum x {
7544 aaaaaaaaaaaaaaaaaaaaaa = 1 << 0, /* test a */
7545 bbbbbbbbbbbbbbbbb = 1 << 1, /* test b */
7546@@ -11,7 +11,7 @@ void t(void) {
7547 /*
7548 * Old indent wrapped the URL near where this sentence ends.
7549 *
7550- * https://www.freebsd.org/cgi/man.cgi?query=indent&apropos=0&sektion=0&manpath=FreeBSD+12-current&arch=default&format=html
7551+ * http://netbsd.gw.com/cgi-bin/man-cgi?indent++NetBSD-current
7552 */
7553
7554 /*
7555diff --git a/tests/comments.0.stdout b/tests/comments.0.stdout
7556index 8ca5aa5..80e3972 100644
7557--- a/tests/comments.0.stdout
7558+++ b/tests/comments.0.stdout
7559@@ -1,4 +1,4 @@
7560-/* $FreeBSD$ */
7561+/* $NetBSD$ */
7562 typedef enum x {
7563 aaaaaaaaaaaaaaaaaaaaaa = 1 << 0, /* test a */
7564 bbbbbbbbbbbbbbbbb = 1 << 1, /* test b */
7565@@ -13,7 +13,7 @@ t(void)
7566 /*
7567 * Old indent wrapped the URL near where this sentence ends.
7568 *
7569- * https://www.freebsd.org/cgi/man.cgi?query=indent&apropos=0&sektion=0&manpath=FreeBSD+12-current&arch=default&format=html
7570+ * http://netbsd.gw.com/cgi-bin/man-cgi?indent++NetBSD-current
7571 */
7572
7573 /*
7574diff --git a/tests/declarations.0 b/tests/declarations.0
7575index 6d668b1..53212f5 100644
7576--- a/tests/declarations.0
7577+++ b/tests/declarations.0
7578@@ -1,4 +1,4 @@
7579-/* $FreeBSD$ */
7580+/* $NetBSD$ */
7581 /* See r303570 */
7582
7583 typedef void (*voidptr) (int *);
7584diff --git a/tests/declarations.0.stdout b/tests/declarations.0.stdout
7585index a164ec2..0b80394 100644
7586--- a/tests/declarations.0.stdout
7587+++ b/tests/declarations.0.stdout
7588@@ -1,4 +1,4 @@
7589-/* $FreeBSD$ */
7590+/* $NetBSD$ */
7591 /* See r303570 */
7592
7593 typedef void (*voidptr) (int *);
7594diff --git a/tests/elsecomment.0 b/tests/elsecomment.0
7595index 61066c2..ccefa0b 100644
7596--- a/tests/elsecomment.0
7597+++ b/tests/elsecomment.0
7598@@ -1,4 +1,4 @@
7599-/* $FreeBSD$ */
7600+/* $NetBSD$ */
7601 /* See r303484 and r309342 */
7602 void t(void) {
7603 /* The two if statements below excercise two different code paths. */
7604diff --git a/tests/elsecomment.0.stdout b/tests/elsecomment.0.stdout
7605index 7de23be..3186538 100644
7606--- a/tests/elsecomment.0.stdout
7607+++ b/tests/elsecomment.0.stdout
7608@@ -1,4 +1,4 @@
7609-/* $FreeBSD$ */
7610+/* $NetBSD$ */
7611 /* See r303484 and r309342 */
7612 void
7613 t(void)
7614diff --git a/tests/float.0 b/tests/float.0
7615index 91f017f..5ca790e 100644
7616--- a/tests/float.0
7617+++ b/tests/float.0
7618@@ -1,4 +1,4 @@
7619-/* $FreeBSD$ */
7620+/* $NetBSD$ */
7621 /* See r303499 */
7622 void t(void) {
7623 unsigned long x = 314UL;
7624diff --git a/tests/float.0.stdout b/tests/float.0.stdout
7625index 0f21318..eff2c85 100644
7626--- a/tests/float.0.stdout
7627+++ b/tests/float.0.stdout
7628@@ -1,4 +1,4 @@
7629-/* $FreeBSD$ */
7630+/* $NetBSD$ */
7631 /* See r303499 */
7632 void
7633 t(void)
7634diff --git a/tests/label.0 b/tests/label.0
7635index 7798a4d..6f2c415 100644
7636--- a/tests/label.0
7637+++ b/tests/label.0
7638@@ -1,4 +1,4 @@
7639-/* $FreeBSD$ */
7640+/* $NetBSD$ */
7641 /* See r303489 */
7642 void t(void) {
7643 switch (1)
7644diff --git a/tests/label.0.stdout b/tests/label.0.stdout
7645index 22ec122..40e129c 100644
7646--- a/tests/label.0.stdout
7647+++ b/tests/label.0.stdout
7648@@ -1,4 +1,4 @@
7649-/* $FreeBSD$ */
7650+/* $NetBSD$ */
7651 /* See r303489 */
7652 void
7653 t(void)
7654diff --git a/tests/list_head.0 b/tests/list_head.0
7655index 3a186ca..beeb561 100644
7656--- a/tests/list_head.0
7657+++ b/tests/list_head.0
7658@@ -1,4 +1,4 @@
7659-/* $FreeBSD$ */
7660+/* $NetBSD$ */
7661 /* See r309380 */
7662 static int
7663 do_execve(td, args, mac_p)
7664diff --git a/tests/list_head.0.stdout b/tests/list_head.0.stdout
7665index b6f0762..b9c2528 100644
7666--- a/tests/list_head.0.stdout
7667+++ b/tests/list_head.0.stdout
7668@@ -1,4 +1,4 @@
7669-/* $FreeBSD$ */
7670+/* $NetBSD$ */
7671 /* See r309380 */
7672 static int
7673 do_execve(td, args, mac_p)
7674diff --git a/tests/nsac.0 b/tests/nsac.0
7675index 449eadf..25769be 100644
7676--- a/tests/nsac.0
7677+++ b/tests/nsac.0
7678@@ -1,4 +1,4 @@
7679-/* $FreeBSD$ */
7680+/* $NetBSD$ */
7681 void t(void) {
7682 int a = (double) 8;
7683 }
7684diff --git a/tests/nsac.0.stdout b/tests/nsac.0.stdout
7685index 78f3b28..cd10ed4 100644
7686--- a/tests/nsac.0.stdout
7687+++ b/tests/nsac.0.stdout
7688@@ -1,4 +1,4 @@
7689-/* $FreeBSD$ */
7690+/* $NetBSD$ */
7691 void
7692 t(void)
7693 {
7694diff --git a/tests/offsetof.0 b/tests/offsetof.0
7695index 078db19..98ffe2d 100644
7696--- a/tests/offsetof.0
7697+++ b/tests/offsetof.0
7698@@ -1,4 +1,4 @@
7699-/* $FreeBSD$ */
7700+/* $NetBSD$ */
7701 /* See r303718 */
7702 void t(void) {
7703 int n = malloc(offsetof(struct s, f) + 1);
7704diff --git a/tests/offsetof.0.stdout b/tests/offsetof.0.stdout
7705index 199bf0f..25877fe 100644
7706--- a/tests/offsetof.0.stdout
7707+++ b/tests/offsetof.0.stdout
7708@@ -1,4 +1,4 @@
7709-/* $FreeBSD$ */
7710+/* $NetBSD$ */
7711 /* See r303718 */
7712 void
7713 t(void)
7714diff --git a/tests/sac.0 b/tests/sac.0
7715index 449eadf..25769be 100644
7716--- a/tests/sac.0
7717+++ b/tests/sac.0
7718@@ -1,4 +1,4 @@
7719-/* $FreeBSD$ */
7720+/* $NetBSD$ */
7721 void t(void) {
7722 int a = (double) 8;
7723 }
7724diff --git a/tests/sac.0.stdout b/tests/sac.0.stdout
7725index 1849b28..6f303d3 100644
7726--- a/tests/sac.0.stdout
7727+++ b/tests/sac.0.stdout
7728@@ -1,4 +1,4 @@
7729-/* $FreeBSD$ */
7730+/* $NetBSD$ */
7731 void
7732 t(void)
7733 {
7734diff --git a/tests/struct.0 b/tests/struct.0
7735index 83142bf..dd21d03 100644
7736--- a/tests/struct.0
7737+++ b/tests/struct.0
7738@@ -1,4 +1,4 @@
7739-/* $FreeBSD$ */
7740+/* $NetBSD$ */
7741
7742 int f(struct x *a);
7743
7744diff --git a/tests/struct.0.stdout b/tests/struct.0.stdout
7745index 3861312..d2ab5e6 100644
7746--- a/tests/struct.0.stdout
7747+++ b/tests/struct.0.stdout
7748@@ -1,4 +1,4 @@
7749-/* $FreeBSD$ */
7750+/* $NetBSD$ */
7751
7752 int f(struct x *a);
7753
7754diff --git a/tests/surplusbad.0 b/tests/surplusbad.0
7755index 07d0702..55cf585 100644
7756--- a/tests/surplusbad.0
7757+++ b/tests/surplusbad.0
7758@@ -1,4 +1,4 @@
7759-/* $FreeBSD$ */
7760+/* $NetBSD$ */
7761 /* See r303599 */
7762 #if defined(__i386__)
7763 int a;
7764diff --git a/tests/surplusbad.0.stdout b/tests/surplusbad.0.stdout
7765index b288970..8b8182c 100644
7766--- a/tests/surplusbad.0.stdout
7767+++ b/tests/surplusbad.0.stdout
7768@@ -1,4 +1,4 @@
7769-/* $FreeBSD$ */
7770+/* $NetBSD$ */
7771 /* See r303599 */
7772 #if defined(__i386__)
7773 int a;
7774diff --git a/tests/types_from_file.0 b/tests/types_from_file.0
7775index 6efca24..e6b182f 100644
7776--- a/tests/types_from_file.0
7777+++ b/tests/types_from_file.0
7778@@ -1,3 +1,3 @@
7779-/* $FreeBSD$ */
7780+/* $NetBSD$ */
7781 /* See r303735 */
7782 void t(a *x, b *y, c *z);
7783diff --git a/tests/types_from_file.0.stdout b/tests/types_from_file.0.stdout
7784index 8776ca6..83f9c62 100644
7785--- a/tests/types_from_file.0.stdout
7786+++ b/tests/types_from_file.0.stdout
7787@@ -1,3 +1,3 @@
7788-/* $FreeBSD$ */
7789+/* $NetBSD$ */
7790 /* See r303735 */
7791 void t(a *x, b *y, c * z);
7792diff --git a/tests/wchar.0 b/tests/wchar.0
7793index 9910e77..2d237ee 100644
7794--- a/tests/wchar.0
7795+++ b/tests/wchar.0
7796@@ -1,4 +1,4 @@
7797-/* $FreeBSD$ */
7798+/* $NetBSD$ */
7799 /* See r309220 */
7800 #include <wchar.h>
7801
7802diff --git a/tests/wchar.0.stdout b/tests/wchar.0.stdout
7803index 92774ab..0367a20 100644
7804--- a/tests/wchar.0.stdout
7805+++ b/tests/wchar.0.stdout
7806@@ -1,4 +1,4 @@
7807-/* $FreeBSD$ */
7808+/* $NetBSD$ */
7809 /* See r309220 */
7810 #include <wchar.h>