| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: indent.c,v 1.58 2021/03/13 18:46:39 rillig Exp $ */ | | 1 | /* $NetBSD: indent.c,v 1.59 2021/03/14 00:22:16 rillig Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * SPDX-License-Identifier: BSD-4-Clause | | 4 | * SPDX-License-Identifier: BSD-4-Clause |
5 | * | | 5 | * |
6 | * Copyright (c) 1985 Sun Microsystems, Inc. | | 6 | * Copyright (c) 1985 Sun Microsystems, Inc. |
7 | * Copyright (c) 1976 Board of Trustees of the University of Illinois. | | 7 | * Copyright (c) 1976 Board of Trustees of the University of Illinois. |
8 | * Copyright (c) 1980, 1993 | | 8 | * Copyright (c) 1980, 1993 |
9 | * The Regents of the University of California. All rights reserved. | | 9 | * The Regents of the University of California. All rights reserved. |
10 | * | | 10 | * |
11 | * Redistribution and use in source and binary forms, with or without | | 11 | * Redistribution and use in source and binary forms, with or without |
12 | * modification, are permitted provided that the following conditions | | 12 | * modification, are permitted provided that the following conditions |
13 | * are met: | | 13 | * are met: |
14 | * 1. Redistributions of source code must retain the above copyright | | 14 | * 1. Redistributions of source code must retain the above copyright |
| @@ -36,27 +36,27 @@ | | | @@ -36,27 +36,27 @@ |
36 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | | 36 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
37 | * SUCH DAMAGE. | | 37 | * SUCH DAMAGE. |
38 | */ | | 38 | */ |
39 | | | 39 | |
40 | #if 0 | | 40 | #if 0 |
41 | #ifndef lint | | 41 | #ifndef lint |
42 | static char sccsid[] = "@(#)indent.c 5.17 (Berkeley) 6/7/93"; | | 42 | static char sccsid[] = "@(#)indent.c 5.17 (Berkeley) 6/7/93"; |
43 | #endif /* not lint */ | | 43 | #endif /* not lint */ |
44 | #endif | | 44 | #endif |
45 | | | 45 | |
46 | #include <sys/cdefs.h> | | 46 | #include <sys/cdefs.h> |
47 | #ifndef lint | | 47 | #ifndef lint |
48 | #if defined(__NetBSD__) | | 48 | #if defined(__NetBSD__) |
49 | __RCSID("$NetBSD: indent.c,v 1.58 2021/03/13 18:46:39 rillig Exp $"); | | 49 | __RCSID("$NetBSD: indent.c,v 1.59 2021/03/14 00:22:16 rillig Exp $"); |
50 | #elif defined(__FreeBSD__) | | 50 | #elif defined(__FreeBSD__) |
51 | __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $"); | | 51 | __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $"); |
52 | #endif | | 52 | #endif |
53 | #endif | | 53 | #endif |
54 | | | 54 | |
55 | #include <sys/param.h> | | 55 | #include <sys/param.h> |
56 | #if HAVE_CAPSICUM | | 56 | #if HAVE_CAPSICUM |
57 | #include <sys/capsicum.h> | | 57 | #include <sys/capsicum.h> |
58 | #include <capsicum_helpers.h> | | 58 | #include <capsicum_helpers.h> |
59 | #endif | | 59 | #endif |
60 | #include <err.h> | | 60 | #include <err.h> |
61 | #include <errno.h> | | 61 | #include <errno.h> |
62 | #include <fcntl.h> | | 62 | #include <fcntl.h> |
| @@ -196,36 +196,37 @@ search_brace(token_type *inout_type_code | | | @@ -196,36 +196,37 @@ search_brace(token_type *inout_type_code |
196 | sc_end = &save_com[2]; | | 196 | sc_end = &save_com[2]; |
197 | } | | 197 | } |
198 | *sc_end++ = '\n'; | | 198 | *sc_end++ = '\n'; |
199 | /* | | 199 | /* |
200 | * We may have inherited a force_nl == true from the previous | | 200 | * We may have inherited a force_nl == true from the previous |
201 | * token (like a semicolon). But once we know that a newline | | 201 | * token (like a semicolon). But once we know that a newline |
202 | * has been scanned in this loop, force_nl should be false. | | 202 | * has been scanned in this loop, force_nl should be false. |
203 | * | | 203 | * |
204 | * However, the force_nl == true must be preserved if newline | | 204 | * However, the force_nl == true must be preserved if newline |
205 | * is never scanned in this loop, so this assignment cannot be | | 205 | * is never scanned in this loop, so this assignment cannot be |
206 | * done earlier. | | 206 | * done earlier. |
207 | */ | | 207 | */ |
208 | *inout_force_nl = false; | | 208 | *inout_force_nl = false; |
| | | 209 | break; |
209 | case form_feed: | | 210 | case form_feed: |
210 | break; | | 211 | break; |
211 | case comment: | | 212 | case comment: |
212 | if (sc_end == NULL) { | | 213 | if (sc_end == NULL) { |
213 | /* | | 214 | /* |
214 | * Copy everything from the start of the line, because | | 215 | * Copy everything from the start of the line, because |
215 | * process_comment() will use that to calculate original | | 216 | * process_comment() will use that to calculate original |
216 | * indentation of a boxed comment. | | 217 | * indentation of a boxed comment. |
217 | */ | | 218 | */ |
218 | memcpy(sc_buf, in_buffer, buf_ptr - in_buffer - 4); | | 219 | memcpy(sc_buf, in_buffer, (size_t)(buf_ptr - in_buffer) - 4); |
219 | save_com = sc_buf + (buf_ptr - in_buffer - 4); | | 220 | save_com = sc_buf + (buf_ptr - in_buffer - 4); |
220 | save_com[0] = save_com[1] = ' '; | | 221 | save_com[0] = save_com[1] = ' '; |
221 | sc_end = &save_com[2]; | | 222 | sc_end = &save_com[2]; |
222 | } | | 223 | } |
223 | *inout_comment_buffered = true; | | 224 | *inout_comment_buffered = true; |
224 | *sc_end++ = '/'; /* copy in start of comment */ | | 225 | *sc_end++ = '/'; /* copy in start of comment */ |
225 | *sc_end++ = '*'; | | 226 | *sc_end++ = '*'; |
226 | for (;;) { /* loop until the end of the comment */ | | 227 | for (;;) { /* loop until the end of the comment */ |
227 | *sc_end = *buf_ptr++; | | 228 | *sc_end = *buf_ptr++; |
228 | if (buf_ptr >= buf_end) | | 229 | if (buf_ptr >= buf_end) |
229 | fill_buffer(); | | 230 | fill_buffer(); |
230 | if (*sc_end++ == '*' && *buf_ptr == '/') | | 231 | if (*sc_end++ == '*' && *buf_ptr == '/') |
231 | break; /* we are at end of comment */ | | 232 | break; /* we are at end of comment */ |
| @@ -522,27 +523,27 @@ main_parse_command_line(int argc, char * | | | @@ -522,27 +523,27 @@ main_parse_command_line(int argc, char * |
522 | opt.continuation_indent = opt.indent_size; | | 523 | opt.continuation_indent = opt.indent_size; |
523 | } | | 524 | } |
524 | | | 525 | |
525 | static void | | 526 | static void |
526 | main_prepare_parsing(void) | | 527 | main_prepare_parsing(void) |
527 | { | | 528 | { |
528 | fill_buffer(); /* get first batch of stuff into input buffer */ | | 529 | fill_buffer(); /* get first batch of stuff into input buffer */ |
529 | | | 530 | |
530 | parse(semicolon); | | 531 | parse(semicolon); |
531 | | | 532 | |
532 | char *p = buf_ptr; | | 533 | char *p = buf_ptr; |
533 | int col = 1; | | 534 | int col = 1; |
534 | | | 535 | |
535 | while (1) { | | 536 | for (;;) { |
536 | if (*p == ' ') | | 537 | if (*p == ' ') |
537 | col++; | | 538 | col++; |
538 | else if (*p == '\t') | | 539 | else if (*p == '\t') |
539 | col = opt.tabsize * (1 + (col - 1) / opt.tabsize) + 1; | | 540 | col = opt.tabsize * (1 + (col - 1) / opt.tabsize) + 1; |
540 | else | | 541 | else |
541 | break; | | 542 | break; |
542 | p++; | | 543 | p++; |
543 | } | | 544 | } |
544 | if (col > opt.indent_size) | | 545 | if (col > opt.indent_size) |
545 | ps.ind_level = ps.i_l_follow = col / opt.indent_size; | | 546 | ps.ind_level = ps.i_l_follow = col / opt.indent_size; |
546 | } | | 547 | } |
547 | | | 548 | |
548 | static void | | 549 | static void |
| @@ -575,27 +576,27 @@ process_comment_in_code(token_type type_ | | | @@ -575,27 +576,27 @@ process_comment_in_code(token_type type_ |
575 | /* we should force a broken line here */ | | 576 | /* we should force a broken line here */ |
576 | if (opt.verbose) | | 577 | if (opt.verbose) |
577 | diag(0, "Line broken"); | | 578 | diag(0, "Line broken"); |
578 | dump_line(); | | 579 | dump_line(); |
579 | ps.want_blank = false; /* dont insert blank at line start */ | | 580 | ps.want_blank = false; /* dont insert blank at line start */ |
580 | *inout_force_nl = false; | | 581 | *inout_force_nl = false; |
581 | } | | 582 | } |
582 | | | 583 | |
583 | ps.in_stmt = true; /* turn on flag which causes an extra level of | | 584 | ps.in_stmt = true; /* turn on flag which causes an extra level of |
584 | * indentation. this is turned off by a ; or | | 585 | * indentation. this is turned off by a ; or |
585 | * '}' */ | | 586 | * '}' */ |
586 | if (s_com != e_com) { /* the turkey has embedded a comment | | 587 | if (s_com != e_com) { /* the turkey has embedded a comment |
587 | * in a line. fix it */ | | 588 | * in a line. fix it */ |
588 | int len = e_com - s_com; | | 589 | size_t len = e_com - s_com; |
589 | | | 590 | |
590 | check_size_code(len + 3); | | 591 | check_size_code(len + 3); |
591 | *e_code++ = ' '; | | 592 | *e_code++ = ' '; |
592 | memcpy(e_code, s_com, len); | | 593 | memcpy(e_code, s_com, len); |
593 | e_code += len; | | 594 | e_code += len; |
594 | *e_code++ = ' '; | | 595 | *e_code++ = ' '; |
595 | *e_code = '\0'; /* null terminate code sect */ | | 596 | *e_code = '\0'; /* null terminate code sect */ |
596 | ps.want_blank = false; | | 597 | ps.want_blank = false; |
597 | e_com = s_com; | | 598 | e_com = s_com; |
598 | } | | 599 | } |
599 | } | | 600 | } |
600 | | | 601 | |
601 | static void | | 602 | static void |
| @@ -714,39 +715,39 @@ process_unary_op(int dec_ind, int tabs_t | | | @@ -714,39 +715,39 @@ process_unary_op(int dec_ind, int tabs_t |
714 | /* | | 715 | /* |
715 | * if this is a unary op in a declaration, we should indent | | 716 | * if this is a unary op in a declaration, we should indent |
716 | * this token | | 717 | * this token |
717 | */ | | 718 | */ |
718 | int i; | | 719 | int i; |
719 | for (i = 0; token[i]; ++i) | | 720 | for (i = 0; token[i]; ++i) |
720 | /* find length of token */; | | 721 | /* find length of token */; |
721 | indent_declaration(dec_ind - i, tabs_to_var); | | 722 | indent_declaration(dec_ind - i, tabs_to_var); |
722 | ps.dumped_decl_indent = true; | | 723 | ps.dumped_decl_indent = true; |
723 | } else if (ps.want_blank) | | 724 | } else if (ps.want_blank) |
724 | *e_code++ = ' '; | | 725 | *e_code++ = ' '; |
725 | | | 726 | |
726 | { | | 727 | { |
727 | int len = e_token - s_token; | | 728 | size_t len = e_token - s_token; |
728 | | | 729 | |
729 | check_size_code(len); | | 730 | check_size_code(len); |
730 | memcpy(e_code, token, len); | | 731 | memcpy(e_code, token, len); |
731 | e_code += len; | | 732 | e_code += len; |
732 | } | | 733 | } |
733 | ps.want_blank = false; | | 734 | ps.want_blank = false; |
734 | } | | 735 | } |
735 | | | 736 | |
736 | static void | | 737 | static void |
737 | process_binary_op(void) | | 738 | process_binary_op(void) |
738 | { | | 739 | { |
739 | int len = e_token - s_token; | | 740 | size_t len = e_token - s_token; |
740 | | | 741 | |
741 | check_size_code(len + 1); | | 742 | check_size_code(len + 1); |
742 | if (ps.want_blank) | | 743 | if (ps.want_blank) |
743 | *e_code++ = ' '; | | 744 | *e_code++ = ' '; |
744 | memcpy(e_code, token, len); | | 745 | memcpy(e_code, token, len); |
745 | e_code += len; | | 746 | e_code += len; |
746 | | | 747 | |
747 | ps.want_blank = true; | | 748 | ps.want_blank = true; |
748 | } | | 749 | } |
749 | | | 750 | |
750 | static void | | 751 | static void |
751 | process_postfix_op(void) | | 752 | process_postfix_op(void) |
752 | { | | 753 | { |
| @@ -779,27 +780,27 @@ process_colon(int *inout_squest, int *in | | | @@ -779,27 +780,27 @@ process_colon(int *inout_squest, int *in |
779 | return; | | 780 | return; |
780 | } | | 781 | } |
781 | if (ps.in_or_st) { | | 782 | if (ps.in_or_st) { |
782 | *e_code++ = ':'; | | 783 | *e_code++ = ':'; |
783 | ps.want_blank = false; | | 784 | ps.want_blank = false; |
784 | return; | | 785 | return; |
785 | } | | 786 | } |
786 | ps.in_stmt = false; /* seeing a label does not imply we are in a | | 787 | ps.in_stmt = false; /* seeing a label does not imply we are in a |
787 | * stmt */ | | 788 | * stmt */ |
788 | /* | | 789 | /* |
789 | * turn everything so far into a label | | 790 | * turn everything so far into a label |
790 | */ | | 791 | */ |
791 | { | | 792 | { |
792 | int len = e_code - s_code; | | 793 | size_t len = e_code - s_code; |
793 | | | 794 | |
794 | check_size_label(len + 3); | | 795 | check_size_label(len + 3); |
795 | memcpy(e_lab, s_code, len); | | 796 | memcpy(e_lab, s_code, len); |
796 | e_lab += len; | | 797 | e_lab += len; |
797 | *e_lab++ = ':'; | | 798 | *e_lab++ = ':'; |
798 | *e_lab = '\0'; | | 799 | *e_lab = '\0'; |
799 | e_code = s_code; | | 800 | e_code = s_code; |
800 | } | | 801 | } |
801 | *inout_force_nl = ps.pcase = *inout_scase; /* ps.pcase will be used by | | 802 | *inout_force_nl = ps.pcase = *inout_scase; /* ps.pcase will be used by |
802 | * dump_line to decide how to | | 803 | * dump_line to decide how to |
803 | * indent the label. force_nl | | 804 | * indent the label. force_nl |
804 | * will force a case n: to be | | 805 | * will force a case n: to be |
805 | * on a line by itself */ | | 806 | * on a line by itself */ |
| @@ -1056,39 +1057,39 @@ process_ident(token_type type_code, int | | | @@ -1056,39 +1057,39 @@ process_ident(token_type type_code, int |
1056 | } | | 1057 | } |
1057 | } else if (*inout_sp_sw && ps.p_l_follow == 0) { | | 1058 | } else if (*inout_sp_sw && ps.p_l_follow == 0) { |
1058 | *inout_sp_sw = false; | | 1059 | *inout_sp_sw = false; |
1059 | *inout_force_nl = true; | | 1060 | *inout_force_nl = true; |
1060 | ps.last_u_d = true; | | 1061 | ps.last_u_d = true; |
1061 | ps.in_stmt = false; | | 1062 | ps.in_stmt = false; |
1062 | parse(hd_type); | | 1063 | parse(hd_type); |
1063 | } | | 1064 | } |
1064 | } | | 1065 | } |
1065 | | | 1066 | |
1066 | static void | | 1067 | static void |
1067 | copy_id(void) | | 1068 | copy_id(void) |
1068 | { | | 1069 | { |
1069 | int len = e_token - s_token; | | 1070 | size_t len = e_token - s_token; |
1070 | | | 1071 | |
1071 | check_size_code(len + 1); | | 1072 | check_size_code(len + 1); |
1072 | if (ps.want_blank) | | 1073 | if (ps.want_blank) |
1073 | *e_code++ = ' '; | | 1074 | *e_code++ = ' '; |
1074 | memcpy(e_code, s_token, len); | | 1075 | memcpy(e_code, s_token, len); |
1075 | e_code += len; | | 1076 | e_code += len; |
1076 | } | | 1077 | } |
1077 | | | 1078 | |
1078 | static void | | 1079 | static void |
1079 | process_string_prefix(void) | | 1080 | process_string_prefix(void) |
1080 | { | | 1081 | { |
1081 | int len = e_token - s_token; | | 1082 | size_t len = e_token - s_token; |
1082 | | | 1083 | |
1083 | check_size_code(len + 1); | | 1084 | check_size_code(len + 1); |
1084 | if (ps.want_blank) | | 1085 | if (ps.want_blank) |
1085 | *e_code++ = ' '; | | 1086 | *e_code++ = ' '; |
1086 | memcpy(e_code, token, len); | | 1087 | memcpy(e_code, token, len); |
1087 | e_code += len; | | 1088 | e_code += len; |
1088 | | | 1089 | |
1089 | ps.want_blank = false; | | 1090 | ps.want_blank = false; |
1090 | } | | 1091 | } |
1091 | | | 1092 | |
1092 | static void | | 1093 | static void |
1093 | process_period(void) | | 1094 | process_period(void) |
1094 | { | | 1095 | { |
| @@ -1145,46 +1146,46 @@ process_preprocessing(void) | | | @@ -1145,46 +1146,46 @@ process_preprocessing(void) |
1145 | fill_buffer(); | | 1146 | fill_buffer(); |
1146 | switch (*e_lab++) { | | 1147 | switch (*e_lab++) { |
1147 | case '\\': | | 1148 | case '\\': |
1148 | if (!in_comment) { | | 1149 | if (!in_comment) { |
1149 | *e_lab++ = *buf_ptr++; | | 1150 | *e_lab++ = *buf_ptr++; |
1150 | if (buf_ptr >= buf_end) | | 1151 | if (buf_ptr >= buf_end) |
1151 | fill_buffer(); | | 1152 | fill_buffer(); |
1152 | } | | 1153 | } |
1153 | break; | | 1154 | break; |
1154 | case '/': | | 1155 | case '/': |
1155 | if (*buf_ptr == '*' && !in_comment && quote == '\0') { | | 1156 | if (*buf_ptr == '*' && !in_comment && quote == '\0') { |
1156 | in_comment = 1; | | 1157 | in_comment = 1; |
1157 | *e_lab++ = *buf_ptr++; | | 1158 | *e_lab++ = *buf_ptr++; |
1158 | com_start = e_lab - s_lab - 2; | | 1159 | com_start = (int)(e_lab - s_lab) - 2; |
1159 | } | | 1160 | } |
1160 | break; | | 1161 | break; |
1161 | case '"': | | 1162 | case '"': |
1162 | if (quote == '"') | | 1163 | if (quote == '"') |
1163 | quote = '\0'; | | 1164 | quote = '\0'; |
1164 | else if (quote == '\0') | | 1165 | else if (quote == '\0') |
1165 | quote = '"'; | | 1166 | quote = '"'; |
1166 | break; | | 1167 | break; |
1167 | case '\'': | | 1168 | case '\'': |
1168 | if (quote == '\'') | | 1169 | if (quote == '\'') |
1169 | quote = '\0'; | | 1170 | quote = '\0'; |
1170 | else if (quote == '\0') | | 1171 | else if (quote == '\0') |
1171 | quote = '\''; | | 1172 | quote = '\''; |
1172 | break; | | 1173 | break; |
1173 | case '*': | | 1174 | case '*': |
1174 | if (*buf_ptr == '/' && in_comment) { | | 1175 | if (*buf_ptr == '/' && in_comment) { |
1175 | in_comment = 0; | | 1176 | in_comment = 0; |
1176 | *e_lab++ = *buf_ptr++; | | 1177 | *e_lab++ = *buf_ptr++; |
1177 | com_end = e_lab - s_lab; | | 1178 | com_end = (int)(e_lab - s_lab); |
1178 | } | | 1179 | } |
1179 | break; | | 1180 | break; |
1180 | } | | 1181 | } |
1181 | } | | 1182 | } |
1182 | | | 1183 | |
1183 | while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t')) | | 1184 | while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == '\t')) |
1184 | e_lab--; | | 1185 | e_lab--; |
1185 | if (e_lab - s_lab == com_end && bp_save == NULL) { | | 1186 | if (e_lab - s_lab == com_end && bp_save == NULL) { |
1186 | /* comment on preprocessor line */ | | 1187 | /* comment on preprocessor line */ |
1187 | if (sc_end == NULL) { /* if this is the first comment, | | 1188 | if (sc_end == NULL) { /* if this is the first comment, |
1188 | * we must set up the buffer */ | | 1189 | * we must set up the buffer */ |
1189 | save_com = sc_buf; | | 1190 | save_com = sc_buf; |
1190 | sc_end = &save_com[0]; | | 1191 | sc_end = &save_com[0]; |
| @@ -1284,27 +1285,27 @@ main_loop(void) | | | @@ -1284,27 +1285,27 @@ main_loop(void) |
1284 | int squest; /* when this is positive, we have seen a ? | | 1285 | int squest; /* when this is positive, we have seen a ? |
1285 | * without the matching : in a <c>?<s>:<s> | | 1286 | * without the matching : in a <c>?<s>:<s> |
1286 | * construct */ | | 1287 | * construct */ |
1287 | int scase; /* set to true when we see a case, so we will | | 1288 | int scase; /* set to true when we see a case, so we will |
1288 | * know what to do with the following colon */ | | 1289 | * know what to do with the following colon */ |
1289 | | | 1290 | |
1290 | sp_sw = force_nl = false; | | 1291 | sp_sw = force_nl = false; |
1291 | dec_ind = 0; | | 1292 | dec_ind = 0; |
1292 | di_stack[ps.dec_nest = 0] = 0; | | 1293 | di_stack[ps.dec_nest = 0] = 0; |
1293 | scase = false; | | 1294 | scase = false; |
1294 | squest = 0; | | 1295 | squest = 0; |
1295 | tabs_to_var = 0; | | 1296 | tabs_to_var = 0; |
1296 | | | 1297 | |
1297 | while (1) { /* this is the main loop. it will go until we | | 1298 | for (;;) { /* this is the main loop. it will go until we |
1298 | * reach eof */ | | 1299 | * reach eof */ |
1299 | int comment_buffered = false; | | 1300 | int comment_buffered = false; |
1300 | | | 1301 | |
1301 | type_code = lexi(&ps); /* lexi reads one token. The actual | | 1302 | type_code = lexi(&ps); /* lexi reads one token. The actual |
1302 | * characters read are stored in "token". lexi | | 1303 | * characters read are stored in "token". lexi |
1303 | * returns a code indicating the type of token */ | | 1304 | * returns a code indicating the type of token */ |
1304 | | | 1305 | |
1305 | /* | | 1306 | /* |
1306 | * The following code moves newlines and comments following an if (), | | 1307 | * The following code moves newlines and comments following an if (), |
1307 | * while (), else, etc. up to the start of the following stmt to | | 1308 | * while (), else, etc. up to the start of the following stmt to |
1308 | * a buffer. This allows proper handling of both kinds of brace | | 1309 | * a buffer. This allows proper handling of both kinds of brace |
1309 | * placement (-br, -bl) and cuddling "else" (-ce). | | 1310 | * placement (-br, -bl) and cuddling "else" (-ce). |
1310 | */ | | 1311 | */ |
| @@ -1374,27 +1375,27 @@ main_loop(void) | | | @@ -1374,27 +1375,27 @@ main_loop(void) |
1374 | goto copy_id; | | 1375 | goto copy_id; |
1375 | | | 1376 | |
1376 | case colon: /* got a ':' */ | | 1377 | case colon: /* got a ':' */ |
1377 | process_colon(&squest, &force_nl, &scase); | | 1378 | process_colon(&squest, &force_nl, &scase); |
1378 | break; | | 1379 | break; |
1379 | | | 1380 | |
1380 | case semicolon: /* got a ';' */ | | 1381 | case semicolon: /* got a ';' */ |
1381 | process_semicolon(&scase, &squest, dec_ind, tabs_to_var, &sp_sw, | | 1382 | process_semicolon(&scase, &squest, dec_ind, tabs_to_var, &sp_sw, |
1382 | hd_type, &force_nl); | | 1383 | hd_type, &force_nl); |
1383 | break; | | 1384 | break; |
1384 | | | 1385 | |
1385 | case lbrace: /* got a '{' */ | | 1386 | case lbrace: /* got a '{' */ |
1386 | process_lbrace(&force_nl, &sp_sw, hd_type, di_stack, | | 1387 | process_lbrace(&force_nl, &sp_sw, hd_type, di_stack, |
1387 | nitems(di_stack), &dec_ind); | | 1388 | (int)nitems(di_stack), &dec_ind); |
1388 | break; | | 1389 | break; |
1389 | | | 1390 | |
1390 | case rbrace: /* got a '}' */ | | 1391 | case rbrace: /* got a '}' */ |
1391 | process_rbrace(&sp_sw, &dec_ind, di_stack); | | 1392 | process_rbrace(&sp_sw, &dec_ind, di_stack); |
1392 | break; | | 1393 | break; |
1393 | | | 1394 | |
1394 | case switch_expr: /* got keyword "switch" */ | | 1395 | case switch_expr: /* got keyword "switch" */ |
1395 | sp_sw = true; | | 1396 | sp_sw = true; |
1396 | hd_type = switch_expr; /* keep this for when we have seen the | | 1397 | hd_type = switch_expr; /* keep this for when we have seen the |
1397 | * expression */ | | 1398 | * expression */ |
1398 | goto copy_id; /* go move the token into buffer */ | | 1399 | goto copy_id; /* go move the token into buffer */ |
1399 | | | 1400 | |
1400 | case keyword_for_if_while: | | 1401 | case keyword_for_if_while: |
| @@ -1474,67 +1475,67 @@ main(int argc, char **argv) | | | @@ -1474,67 +1475,67 @@ main(int argc, char **argv) |
1474 | #endif | | 1475 | #endif |
1475 | main_prepare_parsing(); | | 1476 | main_prepare_parsing(); |
1476 | main_loop(); | | 1477 | main_loop(); |
1477 | } | | 1478 | } |
1478 | | | 1479 | |
1479 | /* | | 1480 | /* |
1480 | * copy input file to backup file if in_name is /blah/blah/blah/file, then | | 1481 | * copy input file to backup file if in_name is /blah/blah/blah/file, then |
1481 | * backup file will be ".Bfile" then make the backup file the input and | | 1482 | * backup file will be ".Bfile" then make the backup file the input and |
1482 | * original input file the output | | 1483 | * original input file the output |
1483 | */ | | 1484 | */ |
1484 | static void | | 1485 | static void |
1485 | bakcopy(void) | | 1486 | bakcopy(void) |
1486 | { | | 1487 | { |
1487 | int n, | | 1488 | ssize_t n; |
1488 | bakchn; | | 1489 | int bakchn; |
1489 | char buff[8 * 1024]; | | 1490 | char buff[8 * 1024]; |
1490 | const char *p; | | 1491 | const char *p; |
1491 | | | 1492 | |
1492 | /* construct file name .Bfile */ | | 1493 | /* construct file name .Bfile */ |
1493 | for (p = in_name; *p; p++); /* skip to end of string */ | | 1494 | for (p = in_name; *p; p++); /* skip to end of string */ |
1494 | while (p > in_name && *p != '/') /* find last '/' */ | | 1495 | while (p > in_name && *p != '/') /* find last '/' */ |
1495 | p--; | | 1496 | p--; |
1496 | if (*p == '/') | | 1497 | if (*p == '/') |
1497 | p++; | | 1498 | p++; |
1498 | sprintf(bakfile, "%s%s", p, simple_backup_suffix); | | 1499 | sprintf(bakfile, "%s%s", p, simple_backup_suffix); |
1499 | | | 1500 | |
1500 | /* copy in_name to backup file */ | | 1501 | /* copy in_name to backup file */ |
1501 | bakchn = creat(bakfile, 0600); | | 1502 | bakchn = creat(bakfile, 0600); |
1502 | if (bakchn < 0) | | 1503 | if (bakchn < 0) |
1503 | err(1, "%s", bakfile); | | 1504 | err(1, "%s", bakfile); |
1504 | while ((n = read(fileno(input), buff, sizeof(buff))) > 0) | | 1505 | while ((n = read(fileno(input), buff, sizeof(buff))) > 0) |
1505 | if (write(bakchn, buff, n) != n) | | 1506 | if (write(bakchn, buff, (size_t)n) != n) |
1506 | err(1, "%s", bakfile); | | 1507 | err(1, "%s", bakfile); |
1507 | if (n < 0) | | 1508 | if (n < 0) |
1508 | err(1, "%s", in_name); | | 1509 | err(1, "%s", in_name); |
1509 | close(bakchn); | | 1510 | close(bakchn); |
1510 | fclose(input); | | 1511 | fclose(input); |
1511 | | | 1512 | |
1512 | /* re-open backup file as the input file */ | | 1513 | /* re-open backup file as the input file */ |
1513 | input = fopen(bakfile, "r"); | | 1514 | input = fopen(bakfile, "r"); |
1514 | if (input == NULL) | | 1515 | if (input == NULL) |
1515 | err(1, "%s", bakfile); | | 1516 | err(1, "%s", bakfile); |
1516 | /* now the original input file will be the output */ | | 1517 | /* now the original input file will be the output */ |
1517 | output = fopen(in_name, "w"); | | 1518 | output = fopen(in_name, "w"); |
1518 | if (output == NULL) { | | 1519 | if (output == NULL) { |
1519 | unlink(bakfile); | | 1520 | unlink(bakfile); |
1520 | err(1, "%s", in_name); | | 1521 | err(1, "%s", in_name); |
1521 | } | | 1522 | } |
1522 | } | | 1523 | } |
1523 | | | 1524 | |
1524 | static void | | 1525 | static void |
1525 | indent_declaration(int cur_dec_ind, int tabs_to_var) | | 1526 | indent_declaration(int cur_dec_ind, int tabs_to_var) |
1526 | { | | 1527 | { |
1527 | int pos = e_code - s_code; | | 1528 | int pos = (int)(e_code - s_code); |
1528 | char *startpos = e_code; | | 1529 | char *startpos = e_code; |
1529 | | | 1530 | |
1530 | /* | | 1531 | /* |
1531 | * get the tab math right for indentations that are not multiples of tabsize | | 1532 | * get the tab math right for indentations that are not multiples of tabsize |
1532 | */ | | 1533 | */ |
1533 | if ((ps.ind_level * opt.indent_size) % opt.tabsize != 0) { | | 1534 | if ((ps.ind_level * opt.indent_size) % opt.tabsize != 0) { |
1534 | pos += (ps.ind_level * opt.indent_size) % opt.tabsize; | | 1535 | pos += (ps.ind_level * opt.indent_size) % opt.tabsize; |
1535 | cur_dec_ind += (ps.ind_level * opt.indent_size) % opt.tabsize; | | 1536 | cur_dec_ind += (ps.ind_level * opt.indent_size) % opt.tabsize; |
1536 | } | | 1537 | } |
1537 | if (tabs_to_var) { | | 1538 | if (tabs_to_var) { |
1538 | int tpos; | | 1539 | int tpos; |
1539 | | | 1540 | |
1540 | check_size_code(cur_dec_ind / opt.tabsize); | | 1541 | check_size_code(cur_dec_ind / opt.tabsize); |