indent.c revision 1.226 1 /* $NetBSD: indent.c,v 1.226 2021/11/19 19:55:15 rillig Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-4-Clause
5 *
6 * Copyright (c) 1985 Sun Microsystems, Inc.
7 * Copyright (c) 1976 Board of Trustees of the University of Illinois.
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40 #if 0
41 static char sccsid[] = "@(#)indent.c 5.17 (Berkeley) 6/7/93";
42 #endif
43
44 #include <sys/cdefs.h>
45 #if defined(__NetBSD__)
46 __RCSID("$NetBSD: indent.c,v 1.226 2021/11/19 19:55:15 rillig Exp $");
47 #elif defined(__FreeBSD__)
48 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
49 #endif
50
51 #include <sys/param.h>
52 #if HAVE_CAPSICUM
53 #include <sys/capsicum.h>
54 #include <capsicum_helpers.h>
55 #endif
56 #include <assert.h>
57 #include <ctype.h>
58 #include <err.h>
59 #include <errno.h>
60 #include <fcntl.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
65
66 #include "indent.h"
67
68 struct options opt = {
69 .brace_same_line = true,
70 .comment_delimiter_on_blankline = true,
71 .cuddle_else = true,
72 .comment_column = 33,
73 .decl_indent = 16,
74 .else_if = true,
75 .function_brace_split = true,
76 .format_col1_comments = true,
77 .format_block_comments = true,
78 .indent_parameters = true,
79 .indent_size = 8,
80 .local_decl_indent = -1,
81 .lineup_to_parens = true,
82 .procnames_start_line = true,
83 .star_comment_cont = true,
84 .tabsize = 8,
85 .max_line_length = 78,
86 .use_tabs = true,
87 };
88
89 struct parser_state ps;
90
91 struct buffer token;
92
93 struct buffer lab;
94 struct buffer code;
95 struct buffer com;
96
97 bool found_err;
98 int blank_lines_to_output;
99 bool blank_line_before;
100 bool blank_line_after;
101 bool break_comma;
102 float case_ind;
103 bool had_eof;
104 int line_no = 1;
105 bool inhibit_formatting;
106
107 static int ifdef_level;
108 static struct parser_state state_stack[5];
109
110 FILE *input;
111 FILE *output;
112
113 static const char *in_name = "Standard Input";
114 static const char *out_name = "Standard Output";
115 static const char *backup_suffix = ".BAK";
116 static char bakfile[MAXPATHLEN] = "";
117
118 #if HAVE_CAPSICUM
119 static void
120 init_capsicum(void)
121 {
122 cap_rights_t rights;
123
124 /* Restrict input/output descriptors and enter Capsicum sandbox. */
125 cap_rights_init(&rights, CAP_FSTAT, CAP_WRITE);
126 if (caph_rights_limit(fileno(output), &rights) < 0)
127 err(EXIT_FAILURE, "unable to limit rights for %s", out_name);
128 cap_rights_init(&rights, CAP_FSTAT, CAP_READ);
129 if (caph_rights_limit(fileno(input), &rights) < 0)
130 err(EXIT_FAILURE, "unable to limit rights for %s", in_name);
131 if (caph_enter() < 0)
132 err(EXIT_FAILURE, "unable to enter capability mode");
133 }
134 #endif
135
136 static void
137 buf_init(struct buffer *buf)
138 {
139 size_t size = 200;
140 buf->buf = xmalloc(size);
141 buf->l = buf->buf + size - 5 /* safety margin */;
142 buf->s = buf->buf + 1; /* allow accessing buf->e[-1] */
143 buf->e = buf->s;
144 buf->buf[0] = ' ';
145 buf->buf[1] = '\0';
146 }
147
148 static size_t
149 buf_len(const struct buffer *buf)
150 {
151 return (size_t)(buf->e - buf->s);
152 }
153
154 void
155 buf_expand(struct buffer *buf, size_t add_size)
156 {
157 size_t new_size = (size_t)(buf->l - buf->s) + 400 + add_size;
158 size_t len = buf_len(buf);
159 buf->buf = xrealloc(buf->buf, new_size);
160 buf->l = buf->buf + new_size - 5;
161 buf->s = buf->buf + 1;
162 buf->e = buf->s + len;
163 /* At this point, the buffer may not be null-terminated anymore. */
164 }
165
166 static void
167 buf_reserve(struct buffer *buf, size_t n)
168 {
169 if (n >= (size_t)(buf->l - buf->e))
170 buf_expand(buf, n);
171 }
172
173 static void
174 buf_add_char(struct buffer *buf, char ch)
175 {
176 buf_reserve(buf, 1);
177 *buf->e++ = ch;
178 }
179
180 static void
181 buf_add_buf(struct buffer *buf, const struct buffer *add)
182 {
183 size_t len = buf_len(add);
184 buf_reserve(buf, len);
185 memcpy(buf->e, add->s, len);
186 buf->e += len;
187 }
188
189 static void
190 buf_terminate(struct buffer *buf)
191 {
192 buf_reserve(buf, 1);
193 *buf->e = '\0';
194 }
195
196 static void
197 buf_reset(struct buffer *buf)
198 {
199 buf->e = buf->s;
200 }
201
202 void
203 diag(int level, const char *msg, ...)
204 {
205 va_list ap;
206
207 if (level != 0)
208 found_err = true;
209
210 va_start(ap, msg);
211 fprintf(stderr, "%s: %s:%d: ",
212 level == 0 ? "warning" : "error", in_name, line_no);
213 vfprintf(stderr, msg, ap);
214 fprintf(stderr, "\n");
215 va_end(ap);
216 }
217
218 static void
219 search_stmt_newline(bool *force_nl)
220 {
221 inp_comment_init_newline();
222 inp_comment_add_char('\n');
223 debug_inp(__func__);
224
225 line_no++;
226
227 /*
228 * We may have inherited a force_nl == true from the previous token (like
229 * a semicolon). But once we know that a newline has been scanned in this
230 * loop, force_nl should be false.
231 *
232 * However, the force_nl == true must be preserved if newline is never
233 * scanned in this loop, so this assignment cannot be done earlier.
234 */
235 *force_nl = false;
236 }
237
238 static void
239 search_stmt_comment(void)
240 {
241 inp_comment_init_comment();
242 inp_comment_add_range(token.s, token.e);
243 if (token.e[-1] == '/') {
244 while (inp_peek() != '\n')
245 inp_comment_add_char(inp_next());
246 debug_inp("search_stmt_comment end C99");
247 } else {
248 while (!inp_comment_complete_block())
249 inp_comment_add_char(inp_next());
250 debug_inp("search_stmt_comment end block");
251 }
252 }
253
254 static bool
255 search_stmt_lbrace(void)
256 {
257 /*
258 * Put KNF-style lbraces before the buffered up tokens and jump out of
259 * this loop in order to avoid copying the token again.
260 */
261 if (inp_comment_seen() && opt.brace_same_line) {
262 inp_comment_insert_lbrace();
263 /*
264 * Originally the lbrace may have been alone on its own line, but it
265 * will be moved into "the else's line", so if there was a newline
266 * resulting from the "{" before, it must be scanned now and ignored.
267 */
268 while (isspace((unsigned char)inp_peek())) {
269 inp_skip();
270 if (inp_peek() == '\n')
271 break;
272 }
273 debug_inp(__func__);
274 return true;
275 }
276 return false;
277 }
278
279 static bool
280 search_stmt_other(lexer_symbol lsym, bool *force_nl,
281 bool comment_buffered, bool last_else)
282 {
283 bool remove_newlines;
284
285 remove_newlines =
286 /* "} else" */
287 (lsym == lsym_else && code.e != code.s && code.e[-1] == '}')
288 /* "else if" */
289 || (lsym == lsym_if && last_else && opt.else_if);
290 if (remove_newlines)
291 *force_nl = false;
292
293 if (!inp_comment_seen()) {
294 ps.search_stmt = false;
295 return false;
296 }
297
298 debug_inp(__func__);
299 inp_comment_rtrim();
300
301 if (opt.swallow_optional_blanklines ||
302 (!comment_buffered && remove_newlines)) {
303 *force_nl = !remove_newlines;
304 inp_comment_rtrim_newline();
305 }
306
307 if (*force_nl) { /* if we should insert a nl here, put it into
308 * the buffer */
309 *force_nl = false;
310 --line_no; /* this will be re-increased when the newline
311 * is read from the buffer */
312 inp_comment_add_char('\n');
313 inp_comment_add_char(' ');
314 if (opt.verbose) /* warn if the line was not already broken */
315 diag(0, "Line broken");
316 }
317
318 for (const char *t_ptr = token.s; *t_ptr != '\0'; ++t_ptr)
319 inp_comment_add_char(*t_ptr);
320 debug_inp("search_stmt_other end");
321 return true;
322 }
323
324 static void
325 switch_buffer(void)
326 {
327 ps.search_stmt = false;
328 inp_comment_add_char(' '); /* add trailing blank, just in case */
329 inp_from_comment();
330 }
331
332 static void
333 search_stmt_lookahead(lexer_symbol *lsym)
334 {
335 if (*lsym == lsym_eof)
336 return;
337
338 /*
339 * The only intended purpose of calling lexi() below is to categorize the
340 * next token in order to decide whether to continue buffering forthcoming
341 * tokens. Once the buffering is over, lexi() will be called again
342 * elsewhere on all of the tokens - this time for normal processing.
343 *
344 * Calling it for this purpose is a bug, because lexi() also changes the
345 * parser state and discards leading whitespace, which is needed mostly
346 * for comment-related considerations.
347 *
348 * Work around the former problem by giving lexi() a copy of the current
349 * parser state and discard it if the call turned out to be just a
350 * lookahead.
351 *
352 * Work around the latter problem by copying all whitespace characters
353 * into the buffer so that the later lexi() call will read them.
354 */
355 if (inp_comment_seen()) {
356 while (ch_isblank(inp_peek()))
357 inp_comment_add_char(inp_next());
358 debug_inp(__func__);
359 }
360
361 struct parser_state backup_ps = ps;
362 debug_println("made backup of parser state");
363 *lsym = lexi();
364 if (*lsym == lsym_newline || *lsym == lsym_form_feed ||
365 *lsym == lsym_comment || ps.search_stmt) {
366 ps = backup_ps;
367 debug_println("rolled back parser state");
368 }
369 }
370
371 /*
372 * Move newlines and comments following an 'if (expr)', 'while (expr)',
373 * 'else', etc. up to the start of the following statement to a buffer. This
374 * allows proper handling of both kinds of brace placement (-br, -bl) and
375 * "cuddling else" (-ce).
376 */
377 static void
378 search_stmt(lexer_symbol *lsym, bool *force_nl, bool *last_else)
379 {
380 bool comment_buffered = false;
381
382 while (ps.search_stmt) {
383 switch (*lsym) {
384 case lsym_newline:
385 search_stmt_newline(force_nl);
386 break;
387 case lsym_form_feed:
388 /* XXX: Is simply removed from the source code. */
389 break;
390 case lsym_comment:
391 search_stmt_comment();
392 comment_buffered = true;
393 break;
394 case lsym_lbrace:
395 if (search_stmt_lbrace())
396 goto switch_buffer;
397 /* FALLTHROUGH */
398 default:
399 if (!search_stmt_other(*lsym, force_nl, comment_buffered,
400 *last_else))
401 return;
402 switch_buffer:
403 switch_buffer();
404 }
405 search_stmt_lookahead(lsym);
406 }
407
408 *last_else = false;
409 }
410
411 static void
412 main_init_globals(void)
413 {
414 inp_init();
415
416 buf_init(&token);
417
418 buf_init(&lab);
419 buf_init(&code);
420 buf_init(&com);
421
422 ps.s_sym[0] = psym_stmt_list;
423 ps.prev_token = lsym_semicolon;
424 ps.next_col_1 = true;
425
426 const char *suffix = getenv("SIMPLE_BACKUP_SUFFIX");
427 if (suffix != NULL)
428 backup_suffix = suffix;
429 }
430
431 /*
432 * Copy the input file to the backup file, then make the backup file the input
433 * and the original input file the output.
434 */
435 static void
436 bakcopy(void)
437 {
438 ssize_t n;
439 int bak_fd;
440 char buff[8 * 1024];
441
442 const char *last_slash = strrchr(in_name, '/');
443 snprintf(bakfile, sizeof(bakfile), "%s%s",
444 last_slash != NULL ? last_slash + 1 : in_name, backup_suffix);
445
446 /* copy in_name to backup file */
447 bak_fd = creat(bakfile, 0600);
448 if (bak_fd < 0)
449 err(1, "%s", bakfile);
450
451 while ((n = read(fileno(input), buff, sizeof(buff))) > 0)
452 if (write(bak_fd, buff, (size_t)n) != n)
453 err(1, "%s", bakfile);
454 if (n < 0)
455 err(1, "%s", in_name);
456
457 close(bak_fd);
458 (void)fclose(input);
459
460 /* re-open backup file as the input file */
461 input = fopen(bakfile, "r");
462 if (input == NULL)
463 err(1, "%s", bakfile);
464 /* now the original input file will be the output */
465 output = fopen(in_name, "w");
466 if (output == NULL) {
467 unlink(bakfile);
468 err(1, "%s", in_name);
469 }
470 }
471
472 static void
473 main_load_profiles(int argc, char **argv)
474 {
475 const char *profile_name = NULL;
476
477 for (int i = 1; i < argc; ++i) {
478 const char *arg = argv[i];
479
480 if (strcmp(arg, "-npro") == 0)
481 return;
482 if (arg[0] == '-' && arg[1] == 'P' && arg[2] != '\0')
483 profile_name = arg + 2;
484 }
485 load_profiles(profile_name);
486 }
487
488 static void
489 main_parse_command_line(int argc, char **argv)
490 {
491 for (int i = 1; i < argc; ++i) {
492 const char *arg = argv[i];
493
494 if (arg[0] == '-') {
495 set_option(arg, "Command line");
496
497 } else if (input == NULL) {
498 in_name = arg;
499 if ((input = fopen(in_name, "r")) == NULL)
500 err(1, "%s", in_name);
501
502 } else if (output == NULL) {
503 out_name = arg;
504 if (strcmp(in_name, out_name) == 0)
505 errx(1, "input and output files must be different");
506 if ((output = fopen(out_name, "w")) == NULL)
507 err(1, "%s", out_name);
508
509 } else
510 errx(1, "too many arguments: %s", arg);
511 }
512
513 if (input == NULL) {
514 input = stdin;
515 output = stdout;
516 } else if (output == NULL) {
517 out_name = in_name;
518 bakcopy();
519 }
520
521 if (opt.comment_column <= 1)
522 opt.comment_column = 2; /* don't put normal comments before column 2 */
523 if (opt.block_comment_max_line_length <= 0)
524 opt.block_comment_max_line_length = opt.max_line_length;
525 if (opt.local_decl_indent < 0) /* if not specified by user, set this */
526 opt.local_decl_indent = opt.decl_indent;
527 if (opt.decl_comment_column <= 0) /* if not specified by user, set this */
528 opt.decl_comment_column = opt.ljust_decl
529 ? (opt.comment_column <= 10 ? 2 : opt.comment_column - 8)
530 : opt.comment_column;
531 if (opt.continuation_indent == 0)
532 opt.continuation_indent = opt.indent_size;
533 }
534
535 static void
536 main_prepare_parsing(void)
537 {
538 inp_read_line();
539
540 int ind = 0;
541 for (const char *p = inp_p();; p++) {
542 if (*p == ' ')
543 ind++;
544 else if (*p == '\t')
545 ind = next_tab(ind);
546 else
547 break;
548 }
549
550 if (ind >= opt.indent_size)
551 ps.ind_level = ps.ind_level_follow = ind / opt.indent_size;
552 }
553
554 static void
555 code_add_decl_indent(int decl_ind, bool tabs_to_var)
556 {
557 int base_ind = ps.ind_level * opt.indent_size;
558 int ind = base_ind + (int)buf_len(&code);
559 int target_ind = base_ind + decl_ind;
560 char *orig_code_e = code.e;
561
562 if (tabs_to_var)
563 for (int next; (next = next_tab(ind)) <= target_ind; ind = next)
564 buf_add_char(&code, '\t');
565
566 for (; ind < target_ind; ind++)
567 buf_add_char(&code, ' ');
568
569 if (code.e == orig_code_e && ps.want_blank) {
570 buf_add_char(&code, ' ');
571 ps.want_blank = false;
572 }
573 }
574
575 static void __attribute__((__noreturn__))
576 process_eof(void)
577 {
578 if (lab.s != lab.e || code.s != code.e || com.s != com.e)
579 dump_line();
580
581 if (ps.tos > 1) /* check for balanced braces */
582 diag(1, "Stuff missing from end of file");
583
584 if (opt.verbose) {
585 printf("There were %d output lines and %d comments\n",
586 ps.stats.lines, ps.stats.comments);
587 printf("(Lines with comments)/(Lines with code): %6.3f\n",
588 (1.0 * ps.stats.comment_lines) / ps.stats.code_lines);
589 }
590
591 fflush(output);
592 exit(found_err ? EXIT_FAILURE : EXIT_SUCCESS);
593 }
594
595 static void
596 maybe_break_line(lexer_symbol lsym, bool *force_nl)
597 {
598 if (!*force_nl)
599 return;
600 if (lsym == lsym_semicolon)
601 return;
602 else if (lsym == lsym_lbrace && opt.brace_same_line)
603 return;
604
605 if (opt.verbose)
606 diag(0, "Line broken");
607 dump_line();
608 ps.want_blank = false;
609 *force_nl = false;
610 }
611
612 static void
613 move_com_to_code(void)
614 {
615 buf_add_char(&code, ' ');
616 buf_add_buf(&code, &com);
617 buf_add_char(&code, ' ');
618 buf_terminate(&code);
619 buf_reset(&com);
620 ps.want_blank = false;
621 }
622
623 static void
624 process_form_feed(void)
625 {
626 dump_line_ff();
627 ps.want_blank = false;
628 }
629
630 static void
631 process_newline(void)
632 {
633 if (ps.prev_token == lsym_comma && ps.p_l_follow == 0 && !ps.block_init &&
634 !opt.break_after_comma && break_comma &&
635 com.s == com.e)
636 goto stay_in_line;
637
638 dump_line();
639 ps.want_blank = false;
640
641 stay_in_line:
642 ++line_no;
643 }
644
645 static bool
646 want_blank_before_lparen(void)
647 {
648 if (!ps.want_blank)
649 return false;
650 if (opt.proc_calls_space)
651 return true;
652 if (ps.prev_token == lsym_rparen_or_rbracket)
653 return false;
654 if (ps.prev_token == lsym_offsetof)
655 return false;
656 if (ps.prev_token == lsym_sizeof)
657 return opt.blank_after_sizeof;
658 if (ps.prev_token == lsym_word || ps.prev_token == lsym_funcname)
659 return false;
660 return true;
661 }
662
663 static void
664 process_lparen_or_lbracket(int decl_ind, bool tabs_to_var, bool spaced_expr)
665 {
666 if (++ps.p_l_follow == array_length(ps.paren_indents)) {
667 diag(0, "Reached internal limit of %zu unclosed parentheses",
668 array_length(ps.paren_indents));
669 ps.p_l_follow--;
670 }
671
672 if (token.s[0] == '(' && ps.in_decl
673 && !ps.block_init && !ps.decl_indent_done &&
674 !ps.is_function_definition && ps.paren_level == 0) {
675 /* function pointer declarations */
676 code_add_decl_indent(decl_ind, tabs_to_var);
677 ps.decl_indent_done = true;
678 } else if (want_blank_before_lparen())
679 *code.e++ = ' ';
680 ps.want_blank = false;
681 *code.e++ = token.s[0];
682
683 ps.paren_indents[ps.p_l_follow - 1] = (short)ind_add(0, code.s, code.e);
684 debug_println("paren_indents[%d] is now %d",
685 ps.p_l_follow - 1, ps.paren_indents[ps.p_l_follow - 1]);
686
687 if (spaced_expr && ps.p_l_follow == 1 && opt.extra_expr_indent
688 && ps.paren_indents[0] < 2 * opt.indent_size) {
689 ps.paren_indents[0] = (short)(2 * opt.indent_size);
690 debug_println("paren_indents[0] is now %d", ps.paren_indents[0]);
691 }
692
693 if (ps.init_or_struct && *token.s == '(' && ps.tos <= 2) {
694 /*
695 * this is a kluge to make sure that declarations will be aligned
696 * right if proc decl has an explicit type on it, i.e. "int a(x) {..."
697 */
698 parse(psym_semicolon); /* I said this was a kluge... */
699 ps.init_or_struct = false;
700 }
701
702 /* parenthesized type following sizeof or offsetof is not a cast */
703 if (ps.prev_token == lsym_offsetof || ps.prev_token == lsym_sizeof)
704 ps.not_cast_mask |= 1 << ps.p_l_follow;
705 }
706
707 static void
708 process_rparen_or_rbracket(bool *spaced_expr, bool *force_nl, stmt_head hd)
709 {
710 if ((ps.cast_mask & (1 << ps.p_l_follow) & ~ps.not_cast_mask) != 0) {
711 ps.next_unary = true;
712 ps.cast_mask &= (1 << ps.p_l_follow) - 1;
713 ps.want_blank = opt.space_after_cast;
714 } else
715 ps.want_blank = true;
716 ps.not_cast_mask &= (1 << ps.p_l_follow) - 1;
717
718 if (ps.p_l_follow > 0)
719 ps.p_l_follow--;
720 else
721 diag(0, "Extra '%c'", *token.s);
722
723 if (code.e == code.s) /* if the paren starts the line */
724 ps.paren_level = ps.p_l_follow; /* then indent it */
725
726 *code.e++ = token.s[0];
727
728 if (*spaced_expr && ps.p_l_follow == 0) { /* check for end of 'if
729 * (...)', or some such */
730 *spaced_expr = false;
731 *force_nl = true; /* must force newline after if */
732 ps.next_unary = true;
733 ps.in_stmt = false; /* don't use stmt continuation indentation */
734
735 parse_stmt_head(hd);
736 }
737
738 /*
739 * This should ensure that constructs such as main(){...} and int[]{...}
740 * have their braces put in the right place.
741 */
742 ps.search_stmt = opt.brace_same_line;
743 }
744
745 static bool
746 want_blank_before_unary_op(void)
747 {
748 if (ps.want_blank)
749 return true;
750 if (token.s[0] == '+' || token.s[0] == '-')
751 return code.e > code.s && code.e[-1] == token.s[0];
752 return false;
753 }
754
755 static void
756 process_unary_op(int decl_ind, bool tabs_to_var)
757 {
758 if (!ps.decl_indent_done && ps.in_decl && !ps.block_init &&
759 !ps.is_function_definition && ps.paren_level == 0) {
760 /* pointer declarations */
761 code_add_decl_indent(decl_ind - (int)buf_len(&token), tabs_to_var);
762 ps.decl_indent_done = true;
763 } else if (want_blank_before_unary_op())
764 *code.e++ = ' ';
765
766 buf_add_buf(&code, &token);
767 ps.want_blank = false;
768 }
769
770 static void
771 process_binary_op(void)
772 {
773 if (buf_len(&code) > 0)
774 buf_add_char(&code, ' ');
775 buf_add_buf(&code, &token);
776 ps.want_blank = true;
777 }
778
779 static void
780 process_postfix_op(void)
781 {
782 *code.e++ = token.s[0];
783 *code.e++ = token.s[1];
784 ps.want_blank = true;
785 }
786
787 static void
788 process_question(int *quest_level)
789 {
790 (*quest_level)++;
791 if (ps.want_blank)
792 *code.e++ = ' ';
793 *code.e++ = '?';
794 ps.want_blank = true;
795 }
796
797 static void
798 process_colon(int *quest_level, bool *force_nl, bool *seen_case)
799 {
800 if (*quest_level > 0) { /* part of a '?:' operator */
801 --*quest_level;
802 if (ps.want_blank)
803 *code.e++ = ' ';
804 *code.e++ = ':';
805 ps.want_blank = true;
806 return;
807 }
808
809 if (ps.init_or_struct) { /* bit-field */
810 *code.e++ = ':';
811 ps.want_blank = false;
812 return;
813 }
814
815 buf_add_buf(&lab, &code); /* 'case' or 'default' or named label */
816 buf_add_char(&lab, ':');
817 buf_terminate(&lab);
818 buf_reset(&code);
819
820 ps.in_stmt = false;
821 ps.is_case_label = *seen_case;
822 *force_nl = *seen_case;
823 *seen_case = false;
824 ps.want_blank = false;
825 }
826
827 static void
828 process_semicolon(bool *seen_case, int *quest_level, int decl_ind,
829 bool tabs_to_var, bool *spaced_expr, stmt_head hd, bool *force_nl)
830 {
831 if (ps.decl_level == 0)
832 ps.init_or_struct = false;
833 *seen_case = false; /* these will only need resetting in an error */
834 *quest_level = 0;
835 if (ps.prev_token == lsym_rparen_or_rbracket)
836 ps.in_parameter_declaration = false;
837 ps.cast_mask = 0;
838 ps.not_cast_mask = 0;
839 ps.block_init = false;
840 ps.block_init_level = 0;
841 ps.just_saw_decl--;
842
843 if (ps.in_decl && code.s == code.e && !ps.block_init &&
844 !ps.decl_indent_done && ps.paren_level == 0) {
845 /* indent stray semicolons in declarations */
846 code_add_decl_indent(decl_ind - 1, tabs_to_var);
847 ps.decl_indent_done = true;
848 }
849
850 ps.in_decl = ps.decl_level > 0; /* if we were in a first level
851 * structure declaration before, we
852 * aren't anymore */
853
854 if ((!*spaced_expr || hd != hd_for) && ps.p_l_follow > 0) {
855
856 /*
857 * There were unbalanced parentheses in the statement. It is a bit
858 * complicated, because the semicolon might be in a for statement.
859 */
860 diag(1, "Unbalanced parentheses");
861 ps.p_l_follow = 0;
862 if (*spaced_expr) { /* 'if', 'while', etc. */
863 *spaced_expr = false;
864 parse_stmt_head(hd);
865 }
866 }
867 *code.e++ = ';';
868 ps.want_blank = true;
869 ps.in_stmt = ps.p_l_follow > 0;
870
871 if (!*spaced_expr) { /* if not if for (;;) */
872 parse(psym_semicolon); /* let parser know about end of stmt */
873 *force_nl = true; /* force newline after an end of stmt */
874 }
875 }
876
877 static void
878 process_lbrace(bool *force_nl, bool *spaced_expr, stmt_head hd,
879 int *di_stack, int di_stack_cap, int *decl_ind)
880 {
881 ps.in_stmt = false; /* don't indent the {} */
882
883 if (!ps.block_init)
884 *force_nl = true; /* force other stuff on same line as '{' onto
885 * new line */
886 else if (ps.block_init_level <= 0)
887 ps.block_init_level = 1;
888 else
889 ps.block_init_level++;
890
891 if (code.s != code.e && !ps.block_init) {
892 if (!opt.brace_same_line) {
893 dump_line();
894 ps.want_blank = false;
895 } else if (ps.in_parameter_declaration && !ps.init_or_struct) {
896 ps.ind_level_follow = 0;
897 if (opt.function_brace_split) { /* dump the line prior to the
898 * brace ... */
899 dump_line();
900 ps.want_blank = false;
901 } else /* add a space between the decl and brace */
902 ps.want_blank = true;
903 }
904 }
905
906 if (ps.in_parameter_declaration)
907 blank_line_before = false;
908
909 if (ps.p_l_follow > 0) {
910 diag(1, "Unbalanced parentheses");
911 ps.p_l_follow = 0;
912 if (*spaced_expr) { /* check for unclosed 'if', 'for', etc. */
913 *spaced_expr = false;
914 parse_stmt_head(hd);
915 ps.ind_level = ps.ind_level_follow;
916 }
917 }
918
919 if (code.s == code.e)
920 ps.ind_stmt = false; /* don't indent the '{' itself */
921 if (ps.in_decl && ps.init_or_struct) {
922 di_stack[ps.decl_level] = *decl_ind;
923 if (++ps.decl_level == di_stack_cap) {
924 diag(0, "Reached internal limit of %d struct levels",
925 di_stack_cap);
926 ps.decl_level--;
927 }
928 } else {
929 ps.decl_on_line = false; /* we can't be in the middle of a
930 * declaration, so don't do special
931 * indentation of comments */
932 if (opt.blanklines_after_decl_at_top && ps.in_parameter_declaration)
933 blank_line_after = true;
934 ps.in_parameter_declaration = false;
935 ps.in_decl = false;
936 }
937
938 *decl_ind = 0;
939 parse(psym_lbrace);
940 if (ps.want_blank)
941 *code.e++ = ' ';
942 ps.want_blank = false;
943 *code.e++ = '{';
944 ps.just_saw_decl = 0;
945 }
946
947 static void
948 process_rbrace(bool *spaced_expr, int *decl_ind, const int *di_stack)
949 {
950 if (ps.s_sym[ps.tos] == psym_decl && !ps.block_init) {
951 /* semicolons can be omitted in declarations */
952 parse(psym_semicolon);
953 }
954
955 if (ps.p_l_follow > 0) { /* check for unclosed if, for, else. */
956 diag(1, "Unbalanced parentheses");
957 ps.p_l_follow = 0;
958 *spaced_expr = false;
959 }
960
961 ps.just_saw_decl = 0;
962 ps.block_init_level--;
963
964 if (code.s != code.e && !ps.block_init) { /* '}' must be first on line */
965 if (opt.verbose)
966 diag(0, "Line broken");
967 dump_line();
968 }
969
970 *code.e++ = '}';
971 ps.want_blank = true;
972 ps.in_stmt = ps.ind_stmt = false;
973
974 if (ps.decl_level > 0) { /* we are in multi-level structure declaration */
975 *decl_ind = di_stack[--ps.decl_level];
976 if (ps.decl_level == 0 && !ps.in_parameter_declaration) {
977 ps.just_saw_decl = 2;
978 *decl_ind = ps.ind_level == 0
979 ? opt.decl_indent : opt.local_decl_indent;
980 }
981 ps.in_decl = true;
982 }
983
984 blank_line_before = false;
985 parse(psym_rbrace);
986 ps.search_stmt = opt.cuddle_else
987 && ps.s_sym[ps.tos] == psym_if_expr_stmt
988 && ps.s_ind_level[ps.tos] >= ps.ind_level;
989
990 if (ps.tos <= 1 && opt.blanklines_after_procs && ps.decl_level <= 0)
991 blank_line_after = true;
992 }
993
994 static void
995 process_do(bool *force_nl, bool *last_else)
996 {
997 ps.in_stmt = false;
998
999 if (code.e != code.s) { /* make sure this starts a line */
1000 if (opt.verbose)
1001 diag(0, "Line broken");
1002 dump_line();
1003 ps.want_blank = false;
1004 }
1005
1006 *force_nl = true; /* following stuff must go onto new line */
1007 *last_else = false;
1008 parse(psym_do);
1009 }
1010
1011 static void
1012 process_else(bool *force_nl, bool *last_else)
1013 {
1014 ps.in_stmt = false;
1015
1016 if (code.e > code.s && !(opt.cuddle_else && code.e[-1] == '}')) {
1017 if (opt.verbose)
1018 diag(0, "Line broken");
1019 dump_line(); /* make sure this starts a line */
1020 ps.want_blank = false;
1021 }
1022
1023 *force_nl = true; /* following stuff must go onto new line */
1024 *last_else = true;
1025 parse(psym_else);
1026 }
1027
1028 static void
1029 process_type(int *decl_ind, bool *tabs_to_var)
1030 {
1031 parse(psym_decl); /* let the parser worry about indentation */
1032
1033 if (ps.prev_token == lsym_rparen_or_rbracket && ps.tos <= 1) {
1034 if (code.s != code.e) {
1035 dump_line();
1036 ps.want_blank = false;
1037 }
1038 }
1039
1040 if (ps.in_parameter_declaration && opt.indent_parameters &&
1041 ps.decl_level == 0) {
1042 ps.ind_level = ps.ind_level_follow = 1;
1043 ps.ind_stmt = false;
1044 }
1045
1046 ps.init_or_struct = /* maybe */ true;
1047 ps.in_decl = ps.decl_on_line = ps.prev_token != lsym_typedef;
1048 if (ps.decl_level <= 0)
1049 ps.just_saw_decl = 2;
1050
1051 blank_line_before = false;
1052
1053 int len = (int)buf_len(&token) + 1;
1054 int ind = ps.ind_level == 0 || ps.decl_level > 0
1055 ? opt.decl_indent /* global variable or local member */
1056 : opt.local_decl_indent; /* local variable */
1057 *decl_ind = ind > 0 ? ind : len;
1058 *tabs_to_var = opt.use_tabs && ind > 0;
1059 }
1060
1061 static void
1062 process_ident(lexer_symbol lsym, int decl_ind, bool tabs_to_var,
1063 bool *spaced_expr, bool *force_nl, stmt_head hd)
1064 {
1065 if (ps.in_decl) {
1066 if (lsym == lsym_funcname) {
1067 ps.in_decl = false;
1068 if (opt.procnames_start_line && code.s != code.e) {
1069 *code.e = '\0';
1070 dump_line();
1071 } else if (ps.want_blank) {
1072 *code.e++ = ' ';
1073 }
1074 ps.want_blank = false;
1075
1076 } else if (!ps.block_init && !ps.decl_indent_done &&
1077 ps.paren_level == 0) {
1078 code_add_decl_indent(decl_ind, tabs_to_var);
1079 ps.decl_indent_done = true;
1080 ps.want_blank = false;
1081 }
1082
1083 } else if (*spaced_expr && ps.p_l_follow == 0) {
1084 *spaced_expr = false;
1085 *force_nl = true;
1086 ps.next_unary = true;
1087 ps.in_stmt = false;
1088 parse_stmt_head(hd);
1089 }
1090 }
1091
1092 static void
1093 copy_token(void)
1094 {
1095 if (ps.want_blank)
1096 buf_add_char(&code, ' ');
1097 buf_add_buf(&code, &token);
1098 }
1099
1100 static void
1101 process_string_prefix(void)
1102 {
1103 copy_token();
1104 ps.want_blank = false;
1105 }
1106
1107 static void
1108 process_period(void)
1109 {
1110 if (code.e > code.s && code.e[-1] == ',')
1111 *code.e++ = ' ';
1112 *code.e++ = '.';
1113 ps.want_blank = false;
1114 }
1115
1116 static void
1117 process_comma(int decl_ind, bool tabs_to_var, bool *force_nl)
1118 {
1119 ps.want_blank = code.s != code.e; /* only put blank after comma if comma
1120 * does not start the line */
1121
1122 if (ps.in_decl && !ps.is_function_definition && !ps.block_init &&
1123 !ps.decl_indent_done && ps.paren_level == 0) {
1124 /* indent leading commas and not the actual identifiers */
1125 code_add_decl_indent(decl_ind - 1, tabs_to_var);
1126 ps.decl_indent_done = true;
1127 }
1128
1129 *code.e++ = ',';
1130
1131 if (ps.p_l_follow == 0) {
1132 if (ps.block_init_level <= 0)
1133 ps.block_init = false;
1134 int varname_len = 8; /* rough estimate for the length of a typical
1135 * variable name */
1136 if (break_comma && (opt.break_after_comma ||
1137 ind_add(compute_code_indent(), code.s, code.e)
1138 >= opt.max_line_length - varname_len))
1139 *force_nl = true;
1140 }
1141 }
1142
1143 /* move the whole line to the 'label' buffer */
1144 static void
1145 read_preprocessing_line(void)
1146 {
1147 enum {
1148 PLAIN, STR, CHR, COMM
1149 } state;
1150
1151 buf_add_char(&lab, '#');
1152
1153 state = PLAIN;
1154 int com_start = 0, com_end = 0;
1155
1156 while (ch_isblank(inp_peek()))
1157 inp_skip();
1158
1159 while (inp_peek() != '\n' || (state == COMM && !had_eof)) {
1160 buf_reserve(&lab, 2);
1161 *lab.e++ = inp_next();
1162 switch (lab.e[-1]) {
1163 case '\\':
1164 if (state != COMM)
1165 *lab.e++ = inp_next();
1166 break;
1167 case '/':
1168 if (inp_peek() == '*' && state == PLAIN) {
1169 state = COMM;
1170 *lab.e++ = inp_next();
1171 com_start = (int)buf_len(&lab) - 2;
1172 }
1173 break;
1174 case '"':
1175 if (state == STR)
1176 state = PLAIN;
1177 else if (state == PLAIN)
1178 state = STR;
1179 break;
1180 case '\'':
1181 if (state == CHR)
1182 state = PLAIN;
1183 else if (state == PLAIN)
1184 state = CHR;
1185 break;
1186 case '*':
1187 if (inp_peek() == '/' && state == COMM) {
1188 state = PLAIN;
1189 *lab.e++ = inp_next();
1190 com_end = (int)buf_len(&lab);
1191 }
1192 break;
1193 }
1194 }
1195
1196 while (lab.e > lab.s && ch_isblank(lab.e[-1]))
1197 lab.e--;
1198 if (lab.e - lab.s == com_end && !inp_comment_seen()) {
1199 /* comment on preprocessor line */
1200 inp_comment_init_preproc();
1201 inp_comment_add_range(lab.s + com_start, lab.s + com_end);
1202 lab.e = lab.s + com_start;
1203 while (lab.e > lab.s && ch_isblank(lab.e[-1]))
1204 lab.e--;
1205 inp_comment_add_char(' '); /* add trailing blank, just in case */
1206 inp_from_comment();
1207 }
1208 buf_terminate(&lab);
1209 }
1210
1211 static void
1212 process_preprocessing(void)
1213 {
1214 if (com.s != com.e || lab.s != lab.e || code.s != code.e)
1215 dump_line();
1216
1217 read_preprocessing_line();
1218
1219 ps.is_case_label = false;
1220
1221 if (strncmp(lab.s, "#if", 3) == 0) { /* also ifdef, ifndef */
1222 if ((size_t)ifdef_level < array_length(state_stack))
1223 state_stack[ifdef_level++] = ps;
1224 else
1225 diag(1, "#if stack overflow");
1226
1227 } else if (strncmp(lab.s, "#el", 3) == 0) { /* else, elif */
1228 if (ifdef_level <= 0)
1229 diag(1, lab.s[3] == 'i' ? "Unmatched #elif" : "Unmatched #else");
1230 else
1231 ps = state_stack[ifdef_level - 1];
1232
1233 } else if (strncmp(lab.s, "#endif", 6) == 0) {
1234 if (ifdef_level <= 0)
1235 diag(1, "Unmatched #endif");
1236 else
1237 ifdef_level--;
1238
1239 } else {
1240 if (strncmp(lab.s + 1, "pragma", 6) != 0 &&
1241 strncmp(lab.s + 1, "error", 5) != 0 &&
1242 strncmp(lab.s + 1, "line", 4) != 0 &&
1243 strncmp(lab.s + 1, "undef", 5) != 0 &&
1244 strncmp(lab.s + 1, "define", 6) != 0 &&
1245 strncmp(lab.s + 1, "include", 7) != 0) {
1246 diag(1, "Unrecognized cpp directive");
1247 return;
1248 }
1249 }
1250
1251 if (opt.blanklines_around_conditional_compilation) {
1252 blank_line_after = true;
1253 blank_lines_to_output = 0;
1254 } else {
1255 blank_line_after = false;
1256 blank_line_before = false;
1257 }
1258
1259 /*
1260 * subsequent processing of the newline character will cause the line to
1261 * be printed
1262 */
1263 }
1264
1265 static void __attribute__((__noreturn__))
1266 main_loop(void)
1267 {
1268 bool force_nl = false; /* when true, code must be broken */
1269 bool last_else = false; /* true iff last keyword was an else */
1270 int decl_ind = 0; /* current indentation for declarations */
1271 int di_stack[20]; /* a stack of structure indentation levels */
1272 bool tabs_to_var = false; /* true if using tabs to indent to var name */
1273 bool spaced_expr = false; /* whether we are in the expression of
1274 * if(...), while(...), etc. */
1275 stmt_head hd = hd_0; /* the type of statement for 'if (...)', 'for
1276 * (...)', etc */
1277 int quest_level = 0; /* when this is positive, we have seen a '?'
1278 * without the matching ':' in a '?:'
1279 * expression */
1280 bool seen_case = false; /* set to true when we see a 'case', so we
1281 * know what to do with the following colon */
1282
1283 di_stack[ps.decl_level = 0] = 0;
1284
1285 for (;;) { /* loop until we reach eof */
1286 lexer_symbol lsym = lexi();
1287
1288 search_stmt(&lsym, &force_nl, &last_else);
1289
1290 if (lsym == lsym_eof) {
1291 process_eof();
1292 /* NOTREACHED */
1293 }
1294
1295 if (lsym == lsym_newline || lsym == lsym_form_feed ||
1296 lsym == lsym_preprocessing)
1297 force_nl = false;
1298 else if (lsym != lsym_comment) {
1299 maybe_break_line(lsym, &force_nl);
1300 ps.in_stmt = true; /* add an extra level of indentation; turned
1301 * off again by a ';' or '}' */
1302 if (com.s != com.e)
1303 move_com_to_code();
1304 }
1305
1306 buf_reserve(&code, 3); /* space for 2 characters plus '\0' */
1307
1308 switch (lsym) {
1309
1310 case lsym_form_feed:
1311 process_form_feed();
1312 break;
1313
1314 case lsym_newline:
1315 process_newline();
1316 break;
1317
1318 case lsym_lparen_or_lbracket:
1319 process_lparen_or_lbracket(decl_ind, tabs_to_var, spaced_expr);
1320 break;
1321
1322 case lsym_rparen_or_rbracket:
1323 process_rparen_or_rbracket(&spaced_expr, &force_nl, hd);
1324 break;
1325
1326 case lsym_unary_op:
1327 process_unary_op(decl_ind, tabs_to_var);
1328 break;
1329
1330 case lsym_binary_op:
1331 process_binary_op();
1332 break;
1333
1334 case lsym_postfix_op:
1335 process_postfix_op();
1336 break;
1337
1338 case lsym_question:
1339 process_question(&quest_level);
1340 break;
1341
1342 case lsym_case_label:
1343 seen_case = true;
1344 goto copy_token;
1345
1346 case lsym_colon:
1347 process_colon(&quest_level, &force_nl, &seen_case);
1348 break;
1349
1350 case lsym_semicolon:
1351 process_semicolon(&seen_case, &quest_level, decl_ind, tabs_to_var,
1352 &spaced_expr, hd, &force_nl);
1353 break;
1354
1355 case lsym_lbrace:
1356 process_lbrace(&force_nl, &spaced_expr, hd, di_stack,
1357 (int)array_length(di_stack), &decl_ind);
1358 break;
1359
1360 case lsym_rbrace:
1361 process_rbrace(&spaced_expr, &decl_ind, di_stack);
1362 break;
1363
1364 case lsym_switch:
1365 spaced_expr = true; /* the interesting stuff is done after the
1366 * expressions are scanned */
1367 hd = hd_switch; /* remember the type of header for later use
1368 * by the parser */
1369 goto copy_token;
1370
1371 case lsym_for:
1372 spaced_expr = true;
1373 hd = hd_for;
1374 goto copy_token;
1375
1376 case lsym_if:
1377 spaced_expr = true;
1378 hd = hd_if;
1379 goto copy_token;
1380
1381 case lsym_while:
1382 spaced_expr = true;
1383 hd = hd_while;
1384 goto copy_token;
1385
1386 case lsym_do:
1387 process_do(&force_nl, &last_else);
1388 goto copy_token;
1389
1390 case lsym_else:
1391 process_else(&force_nl, &last_else);
1392 goto copy_token;
1393
1394 case lsym_typedef:
1395 case lsym_storage_class:
1396 blank_line_before = false;
1397 goto copy_token;
1398
1399 case lsym_tag:
1400 if (ps.p_l_follow > 0)
1401 goto copy_token;
1402 /* FALLTHROUGH */
1403 case lsym_type_outside_parentheses:
1404 process_type(&decl_ind, &tabs_to_var);
1405 goto copy_token;
1406
1407 case lsym_type_in_parentheses:
1408 case lsym_offsetof:
1409 case lsym_sizeof:
1410 case lsym_word:
1411 case lsym_funcname:
1412 case lsym_return:
1413 process_ident(lsym, decl_ind, tabs_to_var, &spaced_expr,
1414 &force_nl, hd);
1415 copy_token:
1416 copy_token();
1417 if (lsym != lsym_funcname)
1418 ps.want_blank = true;
1419 break;
1420
1421 case lsym_string_prefix:
1422 process_string_prefix();
1423 break;
1424
1425 case lsym_period:
1426 process_period();
1427 break;
1428
1429 case lsym_comma:
1430 process_comma(decl_ind, tabs_to_var, &force_nl);
1431 break;
1432
1433 case lsym_preprocessing:
1434 process_preprocessing();
1435 break;
1436
1437 case lsym_comment:
1438 process_comment();
1439 break;
1440
1441 default:
1442 break;
1443 }
1444
1445 *code.e = '\0';
1446 if (lsym != lsym_comment && lsym != lsym_newline &&
1447 lsym != lsym_preprocessing)
1448 ps.prev_token = lsym;
1449 }
1450 }
1451
1452 int
1453 main(int argc, char **argv)
1454 {
1455 main_init_globals();
1456 main_load_profiles(argc, argv);
1457 main_parse_command_line(argc, argv);
1458 #if HAVE_CAPSICUM
1459 init_capsicum();
1460 #endif
1461 main_prepare_parsing();
1462 main_loop();
1463 }
1464
1465 #ifdef debug
1466 void
1467 debug_printf(const char *fmt, ...)
1468 {
1469 FILE *f = output == stdout ? stderr : stdout;
1470 va_list ap;
1471
1472 va_start(ap, fmt);
1473 vfprintf(f, fmt, ap);
1474 va_end(ap);
1475 }
1476
1477 void
1478 debug_println(const char *fmt, ...)
1479 {
1480 FILE *f = output == stdout ? stderr : stdout;
1481 va_list ap;
1482
1483 va_start(ap, fmt);
1484 vfprintf(f, fmt, ap);
1485 va_end(ap);
1486 fprintf(f, "\n");
1487 }
1488
1489 void
1490 debug_vis_range(const char *prefix, const char *s, const char *e,
1491 const char *suffix)
1492 {
1493 debug_printf("%s", prefix);
1494 for (const char *p = s; p < e; p++) {
1495 if (*p == '\\' || *p == '"')
1496 debug_printf("\\%c", *p);
1497 else if (isprint((unsigned char)*p))
1498 debug_printf("%c", *p);
1499 else if (*p == '\n')
1500 debug_printf("\\n");
1501 else if (*p == '\t')
1502 debug_printf("\\t");
1503 else
1504 debug_printf("\\x%02x", (unsigned char)*p);
1505 }
1506 debug_printf("%s", suffix);
1507 }
1508 #endif
1509
1510 static void *
1511 nonnull(void *p)
1512 {
1513 if (p == NULL)
1514 err(EXIT_FAILURE, NULL);
1515 return p;
1516 }
1517
1518 void *
1519 xmalloc(size_t size)
1520 {
1521 return nonnull(malloc(size));
1522 }
1523
1524 void *
1525 xrealloc(void *p, size_t new_size)
1526 {
1527 return nonnull(realloc(p, new_size));
1528 }
1529
1530 char *
1531 xstrdup(const char *s)
1532 {
1533 return nonnull(strdup(s));
1534 }
1535