1 /** \file
2  * Copyright (c) 2015 Tim Hentenaar. All rights reserved.<br>
3  * Copyright (c) 2013 Łukasz Stelmach.  All rights reserved.<br>
4  * Copyright (c) 1999 Carlo Wood.  All rights reserved.<br>
5  * Copyright (c) 1994 Joseph Arceneaux.  All rights reserved.<br>
6  * Copyright (c) 1992, 2002, 2008, 2014, 2015 Free Software Foundation, Inc.  All rights reserved.<br>
7  *
8  * Copyright (c) 1985 Sun Microsystems, Inc. <br>
9  * Copyright (c) 1980 The Regents of the University of California. <br>
10  * Copyright (c) 1976 Board of Trustees of the University of Illinois. All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * - 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * - 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * - 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.<br>
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * This program is distributed in the hope that it will be useful,
37  * but WITHOUT ANY WARRANTY; without even the implied warranty of
38  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39  * GNU General Public License for more details.
40  *
41  * You should have received a copy of the GNU General Public License
42  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
43  *
44  * History:
45  * - 2002-11-10 Cristalle Azundris Sabon <cristalle@azundris.com>
46  *            Added --preprocessor-indentation (ppi)   if set, will indent nested
47  *            preprocessor-statements with n spaces per level.  overrides -lps.
48  * -2007-11-11 Jean-Christophe Dubois <jcd@tribudubois.net>
49  *            Added --indent-label and --linux-style options.
50  *
51  */
52 
53 #ifndef INDENT_INDENT_H
54 #define INDENT_INDENT_H
55 
56 #include "sys.h"
57 RCSTAG_H (indent, "$Id$");
58 #include <config.h>
59 
60 #include <libgettext.h>
61 #ifdef ENABLE_NLS
62 #define _(X) gettext(X)
63 #else
64 #define _(X) X
65 #endif
66 
67 #include "lexi.h"
68 
69 /** Values that `indent' can return for exit status.
70  *
71  *  `total_success' means no errors or warnings were found during a successful
72  *    invocation of the program.
73  *
74  *  `invocation_error' is returned if an invocation problem (like an incorrect
75  *    option) prevents any formatting to occur.
76  *
77  *  `indent_error' is returned if errors occur during formatting which
78  *    do not prevent completion of the formatting, but which appear to be
79  *    manifested by incorrect code (i.e, code which wouldn't compile).
80  *
81  *  `indent_punt' is returned if formatting of a file is halted because of
82  *    an error with the file which prevents completion of formatting.  If more
83  *    than one input file was specified, indent continues to the next file.
84  *
85  *  `indent_fatal' is returned if a serious internal problem occurs and
86  *    the entire indent process is terminated, even if all specified files
87  *    have not been processed. */
88 
89 typedef enum exit_values
90 {
91   total_success = 0,
92   invocation_error = 1,
93   indent_error = 2,
94   indent_punt = 3,
95   indent_fatal = 4,
96   system_error = 5
97 } exit_values_ty;
98 
99 typedef unsigned char BOOLEAN;
100 
101 #define false 0
102 #define true  1
103 
104 typedef struct file_buffer
105 {
106   char *name;
107   size_t size;
108   char *data;
109 } file_buffer_ty;
110 
111 typedef enum bb_code
112 {
113   bb_none,
114   bb_comma,
115   bb_embedded_comment_start,
116   bb_embedded_comment_end,
117   bb_proc_call,
118   bb_dec_ind,
119   bb_unary_op,
120   bb_binary_op,
121   bb_before_boolean_binary_op,
122   bb_after_boolean_binary_op,
123   bb_after_equal_sign,
124   bb_comparisation,
125   bb_question,
126   bb_colon,
127   bb_label,
128   bb_semicolon,
129   bb_lbrace,
130   bb_rbrace,
131   bb_overloaded,
132   bb_const_qualifier,
133   bb_ident,
134   bb_attribute,
135   bb_struct_delim,
136   bb_operator2,                 /* member selection (bb_struct_delim `.' or `->') */
137   bb_operator4,                 /* member selection (bb_struct_delim `.*' or `->*') */
138   bb_operator5,                 /* multiply, divide or modulo */
139   bb_operator6,                 /* add or subtract */
140   bb_doublecolon,
141   bb_cast
142 } bb_code_ty;
143 
144 #define DEFAULT_RIGHT_MARGIN 79
145 
146 #define DEFAULT_RIGHT_COMMENT_MARGIN 79
147 
148 #define DEFAULT_LABEL_INDENT -2
149 
150 /* Name of input file.  */
151 extern char *in_name;
152 
153 extern char *in_prog;           /*!< pointer to the null-terminated input
154                                    program */
155 
156 /** Point to the position in the input program which we are currently looking
157  *  at.  */
158 
159 extern char *in_prog_pos;
160 
161 /** Point to the start of the current line.  */
162 extern char *cur_line;
163 
164 /** Size of the input program, not including the NEWLINE EOS we add at the end */
165 extern unsigned long in_prog_size;
166 
167 extern char *labbuf;            /*!< buffer for label */
168 extern char *s_lab;             /*!< start of stored label */
169 extern char *e_lab;             /*!< end of stored label */
170 extern char *l_lab;             /*!< limit of label buffer */
171 
172 extern char *codebuf;           /*!< buffer for code section */
173 extern char *s_code;            /*!<  start  of stored code */
174 extern char *s_code_corresponds_to; /*!<  If set, start of corresponding
175                                      *  code in token buffer... */
176 extern char *e_code;            /*!<  end of stored code */
177 extern char *l_code;            /*!<  limit of code section */
178 
179 extern char *combuf;            /*!<  buffer for comments */
180 extern char *s_com;             /*!<  start of stored comments */
181 extern char *e_com;             /*!<  end of stored comments */
182 extern char *l_com;             /*!<  limit of comment buffer */
183 
184 extern char *buf_ptr;           /*!<  ptr to next character to be taken from
185                                  * in_buffer */
186 extern char *buf_end;           /*!<  ptr to first after last char in in_buffer */
187 
188 /** pointer to the token that lexi() has just found */
189 extern char *token;
190 
191 /** points to the first char after the end of token */
192 extern char *token_end;
193 
194 extern int squest;
195 extern file_buffer_ty *current_input;
196 
197 /** Used to keep track of buffers.  */
198 typedef struct buf
199 {
200   char *ptr;                    /*!<  points to the start of the buffer */
201   char *end;                    /*!<  points to the character beyond the last
202                                  *    one (e.g. is equal to ptr if the buffer is
203                                  *    (empty).  */
204   int size;                     /*!<  how many chars are currently allocated.  */
205   int len;                      /*!<  how many chars we're actually using. */
206   int start_column;             /*!<  corresponding column of first character
207                                  *    in the buffer. */
208   int column;                   /*!<  Column we were in when we switched buffers. */
209 } buf_ty;
210 
211 /** Buffer in which to save a comment which occurs between an if(), while(),
212  * etc., and the statement following it.  Note: the fact that we point into
213  * this buffer, and that we might realloc() it (via the need_chars macro) is
214  * a bad thing (since when the buffer is realloc'd its address might change,
215  * making any pointers into it point to garbage), but since the filling of
216  * the buffer (hence the need_chars) and the using of the buffer (where
217  * buf_ptr points into it) occur at different times, we can get away with it
218  * (it would not be trivial to fix).  */
219 
220 extern buf_ty save_com;
221 
222 extern char *bp_save;           /*!<  saved value of buf_ptr when taking input
223                                    from save_com */
224 extern char *be_save;           /*!<  similarly saved value of buf_end */
225 
226 
227 extern int pointer_as_binop;
228 extern int n_real_blanklines;
229 extern int prefix_blankline_requested;
230 extern codes_ty prefix_blankline_requested_code;      /*!<  The code that caused
231                                                            the blank line to be
232                                                            requested */
233 extern int postfix_blankline_requested;
234 extern codes_ty postfix_blankline_requested_code;     /*!<  The code that caused
235                                                          * the blank line to be
236                                                          * requested */
237 extern int break_comma;         /*!<  when true and not in parens, break after a
238                                  * comma */
239 
240 /**
241  * This structure stores all the user options that come from (e.g.)
242  * command line flags.
243  */
244 typedef struct user_options_st
245 {
246     int verbose;     /*!<  when true, non-essential error messages are printed */
247     int use_tabs;    /*!<  does indent convert tabs to spaces or not */
248     int tabsize;     /*!<  The number of columns a tab character generates. */
249     int use_stdout;  /*!<  Where does output go. */
250     int space_sp_semicolon; /*!<  If true, a space is inserted between if, while, or for, and a semicolon
251                              * for example while (*p++ == ' ') ; */
252     int swallow_optional_blanklines;
253     int star_comment_cont;   /*!<  true if comment continuation lines should have stars at the beginning of each line. */
254     int struct_brace_indent; /*!<  Indentation level to be used for a '{' directly following a struct, union or enum */
255     int space_after_while;   /*!<  Put a space after `while' */
256     int space_after_if;      /*!<  Put a space after `if' */
257     int space_after_for;     /*!<  Put a space after `for' */
258     int procnames_start_line; /*!<  if true, the names of procedures being  defined get placed in column 1 (ie. a
259                                * newline is placed between the type of the procedure and its name) */
260     int parentheses_space;   /*!<  If true, parentheses will look like: ( foo ) rather than (foo) */
261     int preserve_mtime;      /*!<  True when the modification time of the files should  be preserved. */
262     int paren_indent;        /*!<  set to the indentation per open parens */
263     int proc_calls_space;    /*!<  If true, procedure calls look like: foo (bar) rather than foo(bar) */
264     int leave_preproc_space; /*!<  if true, leave the spaces between  '#' and preprocessor commands. */
265     int force_preproc_width; /*!<  if this is >0, this becomes the preproc indent-level */
266     int lineup_to_parens;    /*!<  if true, continued code within parens will be lined up to the open paren */
267     int honour_newlines;     /*!<  True when positions at which we read a newline in the input file, should get
268                               * a high priority to break long lines at. */
269     int fix_nested_comments; /*!<  If nested comments are to be fixed */
270     int format_comments; /*!<  If any comments are to be reformatted */
271     int format_col1_comments; /*!<  If comments which start in column 1 are to be magically reformatted */
272     int extra_expression_indent;     /*!<  True if continuation lines from the expression part of "if(e)",
273                                       * "while(e)", "for(e;e;e)" should be indented an extra tab stop so that
274                                       * they don't conflict with the code that follows */
275     int ljust_decl;          /*!<  true if declarations should be left justified */
276     int cast_space;          /*!<  If true, casts look like: (char *) bar rather than (char *)bar */
277     int cuddle_else;         /*!<  true if else should cuddle up to '}' */
278     int cuddle_do_while;     /*!<  true if '}' should cuddle up to while in do loop */
279     int comment_delimiter_on_blankline;
280     int blank_after_sizeof; /*!<  true iff a blank should always be inserted after sizeof */
281     int break_function_decl_args;    /*!<  true if declarations should have args on new lines */
282     int break_function_decl_args_end; /*!<  true if declarations should have
283                                        * ")" after args on new lines */
284     int leave_comma;         /*!<  if true, never break declarations after commas */
285     int break_before_boolean_operator;       /*!<  True when we prefer to break a long line
286                                               * before a '&&' or '||', instead of behind it.
287                                               */
288     int blanklines_before_blockcomments;
289     int blanklines_after_declarations;
290     int blanklines_after_procs;
291     int blanklines_after_declarations_at_proctop;    /*!<  This is vaguely  similar to blanklines_after_declarations except that
292                                                       * it only applies to the first set of declarations in a procedure (just after
293                                                       * the first '{') and it causes a blank line to be generated even
294                                                       * if there are no declarations
295                                                       */
296     int blanklines_around_conditional_compilation;
297     int comment_max_col;
298     int max_col;             /*!<  the maximum allowable line length */
299     int label_offset;        /* offset of labels */
300     int ind_size;            /* !< The size of one indentation level in spaces.  */
301     int indent_parameters;   /*!<  Number of spaces to indent parameters.  */
302     int decl_indent;         /*!< column to indent declared identifiers to */
303     int unindent_displace;   /*!< comments not to the right of code will be
304                               * placed this many indentation levels to the
305                               * left of code */
306     int else_endif_col; /*!< The column in which comments to the right of # else and # endif should start. */
307     int case_indent;         /*!< The distance to indent case labels from the switch statement */
308     int continuation_indent; /*!< set to the indentation between the edge of code and continuation lines in spaces */
309     int decl_com_ind;        /*!< the column in which comments after declarations should be put */
310     int case_brace_indent;   /*!< Indentation level to be used for a '{'
311                               * directly following a case label. */
312     int c_plus_plus; /*!< True if we're handling C++ code. */
313     int com_ind;             /*!< the column in which comments to the right of code should start */
314     int braces_on_struct_decl_line; /*!< when true, brace should be on same line as the struct declaration */
315     int braces_on_func_def_line; /*!< when true, brace should be on same line as the function definition */
316     int btype_2; /* when true, brace should be on same line as if, while, etc */
317     int brace_indent; /*!< number of spaces to indent braces from the suround if, while, etc. in -bl
318                        * (bype_2 == 0) code */
319     int expect_output_file;  /*!< Means "-o" was specified. */
320     int pointer_align_right; /*!< true: "char *a", false: "char* a" */
321     int gettext_strings;     /*!< true: _("...") is a string, false: it's a function */
322     int allow_single_line_conditionals; /*!< Don't indent the body of an unbraced if, else, etc. */
323     int align_with_spaces; /*!< Align with spaces if indenting with tabs. */
324     int spaces_around_initializers; /*!< Place spaces after { and before } in initializers. */
325     int dont_tab_align_comments; /*!< Don't align comments to the nearest tabstop. */
326 } user_options_ty;
327 
328 extern user_options_ty settings;
329 
330 /** True if there is an embedded comment on this code line */
331 extern int embedded_comment_on_line;
332 
333 extern int di_stack_alloc;
334 extern int *di_stack;
335 
336 extern int else_or_endif; /*!< True if a # else or # endif has been encountered.  */
337 
338 extern int     code_lines;          /*!< count of lines with code */
339 
340 extern int     out_lines;           /*!< the number of lines written, set by
341                                  * dump_line */
342 extern int     com_lines;           /*!< the number of lines with comments, set by
343                                  * dump_line */
344 
345 
346 extern BOOLEAN had_eof;             /*!< set to true when input is exhausted */
347 extern int     line_no;             /*!< the current input line number. */
348 
349 extern int     paren_target;
350 
351 
352 extern int use_stdinout; /*!< Nonzero if we should use standard input/output when files are not
353                           * explicitly specified.  */
354 
355 /**
356  * This structure contains information relating to the state of parsing the
357  * code.  The difference is that the state is saved on # if and restored on
358  * # else.  */
359 
360 typedef struct parser_state
361 {
362     struct parser_state * next;
363     codes_ty              last_token;
364 
365   /** This is the parsers stack, and the current allocated size.  */
366     codes_ty            * p_stack;
367     int                   p_stack_size;
368 
369   /** This stack stores indentation levels
370      * Currently allocated size is stored in p_stack_size.  */
371 
372     int                 * il;
373 
374   /** If the last token was an ident and is a reserved word,
375      * remember the type. */
376 
377     rwcodes_ty            last_rw;
378 
379   /** also, remember its depth in parentheses */
380 
381     int                   last_rw_depth;
382 
383   /** Used to store case stmt indentation levels.
384    * Currently allocated size is stored in p_stack_size.  */
385     int                  *cstk;
386 
387   /** Pointer to the top of stack of the p_stack, il and cstk arrays. */
388     int                   tos;
389 
390     int                   box_com; /*!<  set to true when we are in a
391                                     * "boxed" comment. In that case, the
392                                     * first non-blank char should be
393                                     * lined up with the / in the comment
394                                     * closing delimiter */
395 
396     int                   cast_mask; /*!<  indicates which close parens close off
397                                       * casts */
398   /** A bit for each paren level, set if the open paren was in a context which
399        indicates that this pair of parentheses is not a cast.  */
400     int                   noncast_mask;
401 
402     int                   sizeof_mask; /*!<  indicates which close parens close off
403                                         * sizeof''s */
404     int                   block_init;  /*!<  set to 1 if inside a block initialization
405                                         * set to 2 if inside an enum declaration
406                                         * and 3 for C99 compound literals. */
407     int block_init_level;         /*!<  The level of brace nesting in an
408                                    * initialization (0 in an enum decl) */
409     int last_nl;                  /*!<  this is true if the last thing scanned was
410                                    * a newline */
411     int last_saw_nl;              /*!<  this is true if the last non white space
412                                    * scanned was a newline */
413     int saw_double_colon;         /*!<  set when we see a ::, reset at first semi-
414                                    * colon or left brace */
415     int is_func_ptr_decl;         /*!<  set when we see a decl, followed by lparen
416                                    * and '*'. */
417     int broken_at_non_nl;         /*!<  true when a line was broken at a place
418                                    * where there was no newline in the input file */
419     int in_or_st;                 /*!<  Will be true iff there has been a
420                                    * declarator (e.g. int or char) and no left
421                                    * paren since the last semicolon. When true,
422                                    * a '{' is starting a structure definition
423                                    * or an initialization list */
424     int col_1;                    /*!<  set to true if the last token started in
425                                    * column 1 */
426     int com_col;                  /*!<  this is the column in which the current
427                                    * coment should start */
428     int dec_nest;                 /*!<  current nesting level for structure or
429                                    * init */
430     int decl_on_line;             /*!<  set to true if this line of code has part
431                                    * of a declaration on it */
432     int i_l_follow;               /*!<  the level in spaces to which ind_level
433                                    * should be set after the current line is
434                                    * printed */
435     BOOLEAN in_decl;              /*!<  set to true when we are in a declaration
436                                    * statement.  The processing of braces is then
437                                    * slightly different */
438     int in_stmt;                  /*!<  set to 1 while in a stmt */
439     int in_parameter_declaration;
440     int ind_level;                /*!<  the current indentation level in spaces */
441     int ind_stmt;                 /*!<  set to 1 if next line should have an extra
442                                    * indentation level because we are in the
443                                    * middle of a stmt */
444     int last_u_d;                 /*!<  set to true after scanning a token which
445                                    * forces a following operator to be unary */
446     int p_l_follow;               /*!<  used to remember how to indent following
447                                    * statement */
448     int paren_level;              /*!<  parenthesization level. used to indent
449                                    * within stmts */
450     int paren_depth;              /*!<  Depth of paren nesting anywhere. */
451 
452   /** Column positions of paren at each level.  If positive, it contains just
453      * the number of characters of code on the line up to and including the
454      * right parenthesis character.  If negative, it contains the opposite of
455      * the actual level of indentation in characters (that is, the indentation
456      * of the line has been added to the number of characters and the sign has
457      * been reversed to indicate that this has been done).  */
458     short *paren_indents;         /*!<  column positions of each paren */
459     int paren_indents_size;       /*!<  Currently allocated size.  */
460 
461     int pcase;                    /*!<  set to 1 if the current line label is a
462                                    * case.  It is printed differently from a
463                                    * regular label */
464     int search_brace;             /*!<  set to true by parse when it is necessary
465                                    * to buffer up all info up to the start of a
466                                    * stmt after an if, while, etc */
467     int use_ff;                   /*!<  set to one if the current line should be
468                                    * terminated with a form feed */
469     int want_blank;               /*!<  set to true when the following token
470                                    * should be prefixed by a blank. (Said
471                                    * prefixing is ignored in some cases.) */
472     bb_code_ty can_break;         /*!<  set when a break is ok before the following
473                                    * token (is automatically implied by
474                                    * `want_blank'. */
475     int its_a_keyword;
476     int sizeof_keyword;
477     char *procname;               /*!<  The name of the current procedure */
478     char *procname_end;           /*!<  One char past the last one in procname */
479     char *classname;              /*!<  The name of the current C++ class */
480     char *classname_end;          /*!<  One char past the last one in classname */
481     int just_saw_decl;
482     int matching_brace_on_same_line;      /*!<  Set to a value >= 0 if the the
483                                              current '}' has a matching '{'
484                                              on the same input line */
485 } parser_state_ty;
486 
487 /** All manipulations of the parser state occur at the top of stack (tos). A
488  * stack is kept for conditional compilation (unrelated to the p_stack, il, &
489  * cstk stacks)--it is implemented as a linked list via the next field. */
490 
491 extern parser_state_ty *parser_state_tos;
492 
493 #endif /* INDENT_INDENT_H */
494