Fri Nov 26 14:17:01 2021 UTC ()
indent: move ind_add from io.c to indent.c

It's a general-purpose function that is not directly related to input or
output.


(rillig)
diff -r1.232 -r1.233 src/usr.bin/indent/indent.c
diff -r1.133 -r1.134 src/usr.bin/indent/io.c

cvs diff -r1.232 -r1.233 src/usr.bin/indent/indent.c (expand / switch to unified diff)

--- src/usr.bin/indent/indent.c 2021/11/25 18:48:37 1.232
+++ src/usr.bin/indent/indent.c 2021/11/26 14:17:01 1.233
@@ -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
41static char sccsid[] = "@(#)indent.c 5.17 (Berkeley) 6/7/93"; 41static 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 */
 222int
 223ind_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
218static void 238static void
219search_stmt_newline(bool *force_nl) 239search_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.

cvs diff -r1.133 -r1.134 src/usr.bin/indent/io.c (expand / switch to unified diff)

--- src/usr.bin/indent/io.c 2021/11/25 21:59:40 1.133
+++ src/usr.bin/indent/io.c 2021/11/26 14:17:01 1.134
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: io.c,v 1.133 2021/11/25 21:59:40 rillig Exp $ */ 1/* $NetBSD: io.c,v 1.134 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) 1980, 1993 7 * Copyright (c) 1980, 1993
8 * The Regents of the University of California. All rights reserved. 8 * The Regents of the University of California. All rights reserved.
9 * All rights reserved. 9 * 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
41static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/93"; 41static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 6/6/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: io.c,v 1.133 2021/11/25 21:59:40 rillig Exp $"); 46__RCSID("$NetBSD: io.c,v 1.134 2021/11/26 14:17:01 rillig Exp $");
47#elif defined(__FreeBSD__) 47#elif defined(__FreeBSD__)
48__FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $"); 48__FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
49#endif 49#endif
50 50
51#include <assert.h> 51#include <assert.h>
52#include <stdio.h> 52#include <stdio.h>
53#include <stdlib.h> 53#include <stdlib.h>
54#include <string.h> 54#include <string.h>
55 55
56#include "indent.h" 56#include "indent.h"
57 57
58static struct { 58static struct {
59 struct buffer inp; /* one line of input, ready to be split into 59 struct buffer inp; /* one line of input, ready to be split into
@@ -674,29 +674,13 @@ inp_read_line(void) @@ -674,29 +674,13 @@ inp_read_line(void)
674 if (ch == '\n') 674 if (ch == '\n')
675 break; 675 break;
676 } 676 }
677 677
678 inbuf.inp.s = inbuf.inp.buf; 678 inbuf.inp.s = inbuf.inp.buf;
679 inbuf.inp.e = p; 679 inbuf.inp.e = p;
680 680
681 if (p - inbuf.inp.s >= 3 && p[-3] == '*' && p[-2] == '/') 681 if (p - inbuf.inp.s >= 3 && p[-3] == '*' && p[-2] == '/')
682 parse_indent_comment(); 682 parse_indent_comment();
683 683
684 if (inhibit_formatting) 684 if (inhibit_formatting)
685 output_range(inbuf.inp.s, inbuf.inp.e); 685 output_range(inbuf.inp.s, inbuf.inp.e);
686} 686}
687 
688int 
689ind_add(int ind, const char *start, const char *end) 
690{ 
691 for (const char *p = start; p != end; ++p) { 
692 if (*p == '\n' || *p == '\f') 
693 ind = 0; 
694 else if (*p == '\t') 
695 ind = next_tab(ind); 
696 else if (*p == '\b') 
697 --ind; 
698 else 
699 ++ind; 
700 } 
701 return ind; 
702}