Annotation of src/usr.bin/nl/nl.c, Revision 1.4
1.4 ! deraadt 1: /* $OpenBSD: nl.c,v 1.3 2014/05/20 01:25:23 guenther Exp $ */
1.1 jca 2: /* $NetBSD: nl.c,v 1.11 2011/08/16 12:00:46 christos Exp $ */
3:
4: /*-
5: * Copyright (c) 1999 The NetBSD Foundation, Inc.
6: * All rights reserved.
7: *
8: * This code is derived from software contributed to The NetBSD Foundation
9: * by Klaus Klein.
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: *
20: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30: * POSSIBILITY OF SUCH DAMAGE.
31: */
32:
33: #include <err.h>
34: #include <errno.h>
35: #include <limits.h>
36: #include <locale.h>
37: #include <regex.h>
38: #include <stdio.h>
39: #include <stdlib.h>
40: #include <string.h>
1.2 millert 41: #include <unistd.h>
1.1 jca 42: #include <wchar.h>
43:
44: typedef enum {
45: number_all, /* number all lines */
46: number_nonempty, /* number non-empty lines */
47: number_none, /* no line numbering */
48: number_regex /* number lines matching regular expression */
49: } numbering_type;
50:
51: struct numbering_property {
52: const char * const name; /* for diagnostics */
53: numbering_type type; /* numbering type */
54: regex_t expr; /* for type == number_regex */
55: };
56:
57: /* line numbering formats */
58: #define FORMAT_LN "%-*d" /* left justified, leading zeros suppressed */
59: #define FORMAT_RN "%*d" /* right justified, leading zeros suppressed */
60: #define FORMAT_RZ "%0*d" /* right justified, leading zeros kept */
61:
62: #define FOOTER 0
63: #define BODY 1
64: #define HEADER 2
65: #define NP_LAST HEADER
66:
67: static struct numbering_property numbering_properties[NP_LAST + 1] = {
68: { "footer", number_none, { 0, 0, 0, 0 } },
69: { "body", number_nonempty, { 0, 0, 0, 0 } },
70: { "header", number_none, { 0, 0, 0, 0 } },
71: };
72:
73: void filter(void);
74: void parse_numbering(const char *, int);
75: __dead void usage(void);
76:
77: /*
78: * Delimiter characters that indicate the start of a logical page section.
79: */
80: static char delim[2 * MB_LEN_MAX];
81: static int delimlen;
82:
83: /*
84: * Configurable parameters.
85: */
86:
87: /* line numbering format */
88: static const char *format = FORMAT_RN;
89:
90: /* increment value used to number logical page lines */
91: static int incr = 1;
92:
93: /* number of adjacent blank lines to be considered (and numbered) as one */
94: static unsigned int nblank = 1;
95:
96: /* whether to restart numbering at logical page delimiters */
97: static int restart = 1;
98:
99: /* characters used in separating the line number and the corrsp. text line */
100: static const char *sep = "\t";
101:
102: /* initial value used to number logical page lines */
103: static int startnum = 1;
104:
105: /* number of characters to be used for the line number */
106: /* should be unsigned but required signed by `*' precision conversion */
107: static int width = 6;
108:
109:
110: int
111: main(int argc, char *argv[])
112: {
113: int c;
114: size_t clen;
115: char delim1[MB_LEN_MAX] = { '\\' }, delim2[MB_LEN_MAX] = { ':' };
116: size_t delim1len = 1, delim2len = 1;
117: const char *errstr;
118:
119: (void)setlocale(LC_ALL, "");
120:
121: while ((c = getopt(argc, argv, "pb:d:f:h:i:l:n:s:v:w:")) != -1) {
122: switch (c) {
123: case 'p':
124: restart = 0;
125: break;
126: case 'b':
127: parse_numbering(optarg, BODY);
128: break;
129: case 'd':
130: clen = mbrlen(optarg, MB_CUR_MAX, NULL);
1.3 guenther 131: if (clen == (size_t)-1 || clen == (size_t)-2)
132: errc(EXIT_FAILURE, EILSEQ, NULL);
1.1 jca 133: if (clen != 0) {
134: memcpy(delim1, optarg, delim1len = clen);
135: clen = mbrlen(optarg + delim1len,
136: MB_CUR_MAX, NULL);
1.3 guenther 137: if (clen == (size_t)-1 || clen == (size_t)-2)
138: errc(EXIT_FAILURE, EILSEQ, NULL);
1.1 jca 139: if (clen != 0) {
140: memcpy(delim2, optarg + delim1len,
141: delim2len = clen);
142: if (optarg[delim1len + clen] != '\0') {
143: errx(EXIT_FAILURE,
144: "invalid delimiter: %s",
145: optarg);
146: }
147: }
148: }
149: break;
150: case 'f':
151: parse_numbering(optarg, FOOTER);
152: break;
153: case 'h':
154: parse_numbering(optarg, HEADER);
155: break;
156: case 'i':
157: incr = strtonum(optarg, INT_MIN, INT_MAX, &errstr);
158: if (errstr)
159: errx(EXIT_FAILURE, "increment value is %s: %s",
160: errstr, optarg);
161: break;
162: case 'l':
163: nblank = strtonum(optarg, 0, UINT_MAX, &errstr);
164: if (errstr)
165: errx(EXIT_FAILURE,
166: "blank line value is %s: %s",
167: errstr, optarg);
168: break;
169: case 'n':
170: if (strcmp(optarg, "ln") == 0) {
171: format = FORMAT_LN;
172: } else if (strcmp(optarg, "rn") == 0) {
173: format = FORMAT_RN;
174: } else if (strcmp(optarg, "rz") == 0) {
175: format = FORMAT_RZ;
176: } else
177: errx(EXIT_FAILURE,
178: "illegal format -- %s", optarg);
179: break;
180: case 's':
181: sep = optarg;
182: break;
183: case 'v':
184: startnum = strtonum(optarg, INT_MIN, INT_MAX, &errstr);
185: if (errstr)
186: errx(EXIT_FAILURE,
187: "initial logical page value is %s: %s",
188: errstr, optarg);
189: break;
190: case 'w':
191: width = strtonum(optarg, 1, INT_MAX, &errstr);
192: if (errstr)
193: errx(EXIT_FAILURE, "width is %s: %s", errstr,
194: optarg);
195: break;
196: case '?':
197: default:
198: usage();
199: /* NOTREACHED */
200: }
201: }
202: argc -= optind;
203: argv += optind;
204:
205: switch (argc) {
206: case 0:
207: break;
208: case 1:
209: if (strcmp(argv[0], "-") != 0 &&
210: freopen(argv[0], "r", stdin) == NULL)
211: err(EXIT_FAILURE, "%s", argv[0]);
212: break;
213: default:
214: usage();
215: /* NOTREACHED */
216: }
217:
218: /* Generate the delimiter sequence */
219: memcpy(delim, delim1, delim1len);
220: memcpy(delim + delim1len, delim2, delim2len);
221: delimlen = delim1len + delim2len;
222:
223: /* Do the work. */
224: filter();
225:
226: exit(EXIT_SUCCESS);
227: }
228:
229: void
230: filter(void)
231: {
232: char *buffer;
233: size_t buffersize;
234: ssize_t linelen;
235: int line; /* logical line number */
236: int section; /* logical page section */
237: unsigned int adjblank; /* adjacent blank lines */
238: int donumber = 0, idx;
239:
240: adjblank = 0;
241: line = startnum;
242: section = BODY;
243:
244: buffer = NULL;
245: buffersize = 0;
246: while ((linelen = getline(&buffer, &buffersize, stdin)) > 0) {
247: for (idx = FOOTER; idx <= NP_LAST; idx++) {
248: /* Does it look like a delimiter? */
249: if (delimlen * (idx + 1) > linelen)
250: break;
251: if (memcmp(buffer + delimlen * idx, delim,
252: delimlen) != 0)
253: break;
254: /* Was this the whole line? */
255: if (buffer[delimlen * (idx + 1)] == '\n') {
256: section = idx;
257: adjblank = 0;
258: if (restart)
259: line = startnum;
260: goto nextline;
261: }
262: }
263:
264: switch (numbering_properties[section].type) {
265: case number_all:
266: /*
267: * Doing this for number_all only is disputable, but
268: * the standard expresses an explicit dependency on
269: * `-b a' etc.
270: */
271: if (buffer[0] == '\n' && ++adjblank < nblank)
272: donumber = 0;
273: else
274: donumber = 1, adjblank = 0;
275: break;
276: case number_nonempty:
277: donumber = (buffer[0] != '\n');
278: break;
279: case number_none:
280: donumber = 0;
281: break;
282: case number_regex:
283: donumber =
284: (regexec(&numbering_properties[section].expr,
285: buffer, 0, NULL, 0) == 0);
286: break;
287: }
288:
289: if (donumber) {
290: (void)printf(format, width, line);
291: line += incr;
292: (void)fputs(sep, stdout);
293: } else {
294: (void)printf("%*s", width, "");
295: }
296: (void)fwrite(buffer, linelen, 1, stdout);
297:
298: if (ferror(stdout))
299: err(EXIT_FAILURE, "output error");
300: nextline:
301: ;
302: }
303:
304: if (ferror(stdin))
305: err(EXIT_FAILURE, "input error");
306:
307: free(buffer);
308: }
309:
310: /*
311: * Various support functions.
312: */
313:
314: void
315: parse_numbering(const char *argstr, int section)
316: {
317: int error;
318: char errorbuf[NL_TEXTMAX];
319:
320: switch (argstr[0]) {
321: case 'a':
322: numbering_properties[section].type = number_all;
323: break;
324: case 'n':
325: numbering_properties[section].type = number_none;
326: break;
327: case 't':
328: numbering_properties[section].type = number_nonempty;
329: break;
330: case 'p':
331: /* If there was a previous expression, throw it away. */
332: if (numbering_properties[section].type == number_regex)
333: regfree(&numbering_properties[section].expr);
334: else
335: numbering_properties[section].type = number_regex;
336:
337: /* Compile/validate the supplied regular expression. */
338: if ((error = regcomp(&numbering_properties[section].expr,
339: &argstr[1], REG_NEWLINE|REG_NOSUB)) != 0) {
340: (void)regerror(error,
341: &numbering_properties[section].expr,
342: errorbuf, sizeof(errorbuf));
343: errx(EXIT_FAILURE,
344: "%s expr: %s -- %s",
345: numbering_properties[section].name, errorbuf,
346: &argstr[1]);
347: }
348: break;
349: default:
350: errx(EXIT_FAILURE,
351: "illegal %s line numbering type -- %s",
352: numbering_properties[section].name, argstr);
353: }
354: }
355:
356: __dead void
357: usage(void)
358: {
359: (void)fprintf(stderr, "usage: %s [-p] [-b type] [-d delim] [-f type] "
360: "[-h type] [-i incr] [-l num]\n\t[-n format] [-s sep] "
361: "[-v startnum] [-w width] [file]\n", getprogname());
362: exit(EXIT_FAILURE);
363: }