| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: indent.c,v 1.232 2021/11/25 18:48:37 rillig Exp $ */ | | 1 | /* $NetBSD: indent.c,v 1.233 2021/11/26 14:17:01 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 |
| @@ -33,27 +33,27 @@ | | | @@ -33,27 +33,27 @@ |
33 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 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 | | 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 | | 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 | | 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 | static char sccsid[] = "@(#)indent.c 5.17 (Berkeley) 6/7/93"; | | 41 | static char sccsid[] = "@(#)indent.c 5.17 (Berkeley) 6/7/93"; |
42 | #endif | | 42 | #endif |
43 | | | 43 | |
44 | #include <sys/cdefs.h> | | 44 | #include <sys/cdefs.h> |
45 | #if defined(__NetBSD__) | | 45 | #if defined(__NetBSD__) |
46 | __RCSID("$NetBSD: indent.c,v 1.232 2021/11/25 18:48:37 rillig Exp $"); | | 46 | __RCSID("$NetBSD: indent.c,v 1.233 2021/11/26 14:17:01 rillig Exp $"); |
47 | #elif defined(__FreeBSD__) | | 47 | #elif defined(__FreeBSD__) |
48 | __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $"); | | 48 | __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $"); |
49 | #endif | | 49 | #endif |
50 | | | 50 | |
51 | #include <sys/param.h> | | 51 | #include <sys/param.h> |
52 | #if HAVE_CAPSICUM | | 52 | #if HAVE_CAPSICUM |
53 | #include <sys/capsicum.h> | | 53 | #include <sys/capsicum.h> |
54 | #include <capsicum_helpers.h> | | 54 | #include <capsicum_helpers.h> |
55 | #endif | | 55 | #endif |
56 | #include <assert.h> | | 56 | #include <assert.h> |
57 | #include <err.h> | | 57 | #include <err.h> |
58 | #include <errno.h> | | 58 | #include <errno.h> |
59 | #include <fcntl.h> | | 59 | #include <fcntl.h> |
| @@ -205,26 +205,46 @@ diag(int level, const char *msg, ...) | | | @@ -205,26 +205,46 @@ diag(int level, const char *msg, ...) |
205 | va_list ap; | | 205 | va_list ap; |
206 | | | 206 | |
207 | if (level != 0) | | 207 | if (level != 0) |
208 | found_err = true; | | 208 | found_err = true; |
209 | | | 209 | |
210 | va_start(ap, msg); | | 210 | va_start(ap, msg); |
211 | fprintf(stderr, "%s: %s:%d: ", | | 211 | fprintf(stderr, "%s: %s:%d: ", |
212 | level == 0 ? "warning" : "error", in_name, line_no); | | 212 | level == 0 ? "warning" : "error", in_name, line_no); |
213 | vfprintf(stderr, msg, ap); | | 213 | vfprintf(stderr, msg, ap); |
214 | fprintf(stderr, "\n"); | | 214 | fprintf(stderr, "\n"); |
215 | va_end(ap); | | 215 | va_end(ap); |
216 | } | | 216 | } |
217 | | | 217 | |
| | | 218 | /* |
| | | 219 | * Compute the indentation from starting at 'ind' and adding the text from |
| | | 220 | * 'start' to 'end'. |
| | | 221 | */ |
| | | 222 | int |
| | | 223 | ind_add(int ind, const char *start, const char *end) |
| | | 224 | { |
| | | 225 | for (const char *p = start; p != end; ++p) { |
| | | 226 | if (*p == '\n' || *p == '\f') |
| | | 227 | ind = 0; |
| | | 228 | else if (*p == '\t') |
| | | 229 | ind = next_tab(ind); |
| | | 230 | else if (*p == '\b') |
| | | 231 | --ind; |
| | | 232 | else |
| | | 233 | ++ind; |
| | | 234 | } |
| | | 235 | return ind; |
| | | 236 | } |
| | | 237 | |
218 | static void | | 238 | static void |
219 | search_stmt_newline(bool *force_nl) | | 239 | search_stmt_newline(bool *force_nl) |
220 | { | | 240 | { |
221 | inp_comment_init_newline(); | | 241 | inp_comment_init_newline(); |
222 | inp_comment_add_char('\n'); | | 242 | inp_comment_add_char('\n'); |
223 | debug_inp(__func__); | | 243 | debug_inp(__func__); |
224 | | | 244 | |
225 | line_no++; | | 245 | line_no++; |
226 | | | 246 | |
227 | /* | | 247 | /* |
228 | * We may have inherited a force_nl == true from the previous token (like | | 248 | * 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 | | 249 | * a semicolon). But once we know that a newline has been scanned in this |
230 | * loop, force_nl should be false. | | 250 | * loop, force_nl should be false. |