nl.c revision 1.7 1 1.7 mrg /* $NetBSD: nl.c,v 1.7 2006/05/10 21:53:48 mrg Exp $ */
2 1.1 kleink
3 1.1 kleink /*-
4 1.1 kleink * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.1 kleink * All rights reserved.
6 1.1 kleink *
7 1.1 kleink * This code is derived from software contributed to The NetBSD Foundation
8 1.1 kleink * by Klaus Klein.
9 1.1 kleink *
10 1.1 kleink * Redistribution and use in source and binary forms, with or without
11 1.1 kleink * modification, are permitted provided that the following conditions
12 1.1 kleink * are met:
13 1.1 kleink * 1. Redistributions of source code must retain the above copyright
14 1.1 kleink * notice, this list of conditions and the following disclaimer.
15 1.1 kleink * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 kleink * notice, this list of conditions and the following disclaimer in the
17 1.1 kleink * documentation and/or other materials provided with the distribution.
18 1.1 kleink * 3. All advertising materials mentioning features or use of this software
19 1.1 kleink * must display the following acknowledgement:
20 1.1 kleink * This product includes software developed by the NetBSD
21 1.1 kleink * Foundation, Inc. and its contributors.
22 1.1 kleink * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 kleink * contributors may be used to endorse or promote products derived
24 1.1 kleink * from this software without specific prior written permission.
25 1.1 kleink *
26 1.1 kleink * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 kleink * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 kleink * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 kleink * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 kleink * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 kleink * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 kleink * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 kleink * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 kleink * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 kleink * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 kleink * POSSIBILITY OF SUCH DAMAGE.
37 1.1 kleink */
38 1.1 kleink
39 1.1 kleink #include <sys/cdefs.h>
40 1.1 kleink #ifndef lint
41 1.1 kleink __COPYRIGHT(
42 1.1 kleink "@(#) Copyright (c) 1999\
43 1.1 kleink The NetBSD Foundation, Inc. All rights reserved.");
44 1.7 mrg __RCSID("$NetBSD: nl.c,v 1.7 2006/05/10 21:53:48 mrg Exp $");
45 1.1 kleink #endif
46 1.1 kleink
47 1.1 kleink #include <errno.h>
48 1.1 kleink #include <limits.h>
49 1.1 kleink #include <locale.h>
50 1.1 kleink #include <regex.h>
51 1.1 kleink #include <stdio.h>
52 1.1 kleink #include <stdlib.h>
53 1.6 matt #include <string.h>
54 1.1 kleink #include <unistd.h>
55 1.1 kleink
56 1.1 kleink typedef enum {
57 1.1 kleink number_all, /* number all lines */
58 1.1 kleink number_nonempty, /* number non-empty lines */
59 1.1 kleink number_none, /* no line numbering */
60 1.1 kleink number_regex /* number lines matching regular expression */
61 1.1 kleink } numbering_type;
62 1.1 kleink
63 1.1 kleink struct numbering_property {
64 1.1 kleink const char * const name; /* for diagnostics */
65 1.1 kleink numbering_type type; /* numbering type */
66 1.1 kleink regex_t expr; /* for type == number_regex */
67 1.1 kleink };
68 1.1 kleink
69 1.1 kleink /* line numbering formats */
70 1.1 kleink #define FORMAT_LN "%-*d" /* left justified, leading zeros suppressed */
71 1.1 kleink #define FORMAT_RN "%*d" /* right justified, leading zeros suppressed */
72 1.1 kleink #define FORMAT_RZ "%0*d" /* right justified, leading zeros kept */
73 1.1 kleink
74 1.1 kleink #define FOOTER 0
75 1.1 kleink #define BODY 1
76 1.1 kleink #define HEADER 2
77 1.1 kleink #define NP_LAST HEADER
78 1.1 kleink
79 1.1 kleink static struct numbering_property numbering_properties[NP_LAST + 1] = {
80 1.1 kleink { "footer", number_none },
81 1.1 kleink { "body", number_nonempty },
82 1.1 kleink { "header", number_none }
83 1.1 kleink };
84 1.1 kleink
85 1.1 kleink #define max(a, b) ((a) > (b) ? (a) : (b))
86 1.1 kleink
87 1.1 kleink /*
88 1.1 kleink * Maximum number of characters required for a decimal representation of a
89 1.1 kleink * (signed) int; courtesy of tzcode.
90 1.1 kleink */
91 1.1 kleink #define INT_STRLEN_MAXIMUM \
92 1.1 kleink ((sizeof (int) * CHAR_BIT - 1) * 302 / 1000 + 2)
93 1.1 kleink
94 1.1 kleink static void filter __P((void));
95 1.1 kleink int main __P((int, char *[]));
96 1.1 kleink static void parse_numbering __P((const char *, int));
97 1.1 kleink static void usage __P((void));
98 1.1 kleink
99 1.1 kleink /*
100 1.1 kleink * Pointer to dynamically allocated input line buffer, and its size.
101 1.1 kleink */
102 1.1 kleink static char *buffer;
103 1.1 kleink static size_t buffersize;
104 1.1 kleink
105 1.1 kleink /*
106 1.1 kleink * Dynamically allocated buffer suitable for string representation of ints.
107 1.1 kleink */
108 1.1 kleink static char *intbuffer;
109 1.1 kleink
110 1.1 kleink /*
111 1.1 kleink * Configurable parameters.
112 1.1 kleink */
113 1.1 kleink /* delimiter characters that indicate the start of a logical page section */
114 1.1 kleink static char delim[2] = { '\\', ':' };
115 1.1 kleink
116 1.1 kleink /* line numbering format */
117 1.1 kleink static const char *format = FORMAT_RN;
118 1.1 kleink
119 1.1 kleink /* increment value used to number logical page lines */
120 1.1 kleink static int incr = 1;
121 1.1 kleink
122 1.1 kleink /* number of adjacent blank lines to be considered (and numbered) as one */
123 1.1 kleink static unsigned int nblank = 1;
124 1.1 kleink
125 1.1 kleink /* whether to restart numbering at logical page delimiters */
126 1.1 kleink static int restart = 1;
127 1.1 kleink
128 1.1 kleink /* characters used in separating the line number and the corrsp. text line */
129 1.1 kleink static const char *sep = "\t";
130 1.1 kleink
131 1.1 kleink /* initial value used to number logical page lines */
132 1.1 kleink static int startnum = 1;
133 1.1 kleink
134 1.1 kleink /* number of characters to be used for the line number */
135 1.1 kleink /* should be unsigned but required signed by `*' precision conversion */
136 1.1 kleink static int width = 6;
137 1.1 kleink
138 1.1 kleink
139 1.1 kleink int
140 1.1 kleink main(argc, argv)
141 1.1 kleink int argc;
142 1.1 kleink char *argv[];
143 1.1 kleink {
144 1.1 kleink int c;
145 1.3 kleink long val;
146 1.3 kleink unsigned long uval;
147 1.1 kleink char *ep;
148 1.1 kleink size_t intbuffersize;
149 1.1 kleink
150 1.1 kleink (void)setlocale(LC_ALL, "");
151 1.1 kleink
152 1.1 kleink /*
153 1.1 kleink * Note: this implementation strictly conforms to the XBD Utility
154 1.1 kleink * Syntax Guidelines and does not permit the optional `file' operand
155 1.1 kleink * to be intermingled with the options, which is defined in the
156 1.1 kleink * XCU specification (Issue 5) but declared an obsolescent feature that
157 1.1 kleink * will be removed from a future issue. It shouldn't matter, though.
158 1.1 kleink */
159 1.1 kleink while ((c = getopt(argc, argv, "pb:d:f:h:i:l:n:s:v:w:")) != -1) {
160 1.1 kleink switch (c) {
161 1.1 kleink case 'p':
162 1.1 kleink restart = 0;
163 1.1 kleink break;
164 1.1 kleink case 'b':
165 1.1 kleink parse_numbering(optarg, BODY);
166 1.1 kleink break;
167 1.1 kleink case 'd':
168 1.1 kleink if (optarg[0] != '\0')
169 1.1 kleink delim[0] = optarg[0];
170 1.1 kleink if (optarg[1] != '\0')
171 1.1 kleink delim[1] = optarg[1];
172 1.1 kleink /* at most two delimiter characters */
173 1.1 kleink if (optarg[2] != '\0') {
174 1.1 kleink (void)fprintf(stderr,
175 1.1 kleink "nl: invalid delim argument -- %s\n",
176 1.1 kleink optarg);
177 1.1 kleink exit(EXIT_FAILURE);
178 1.1 kleink /* NOTREACHED */
179 1.1 kleink }
180 1.1 kleink break;
181 1.1 kleink case 'f':
182 1.1 kleink parse_numbering(optarg, FOOTER);
183 1.1 kleink break;
184 1.1 kleink case 'h':
185 1.1 kleink parse_numbering(optarg, HEADER);
186 1.1 kleink break;
187 1.1 kleink case 'i':
188 1.1 kleink errno = 0;
189 1.1 kleink val = strtol(optarg, &ep, 10);
190 1.1 kleink if ((ep != NULL && *ep != '\0') ||
191 1.1 kleink ((val == LONG_MIN || val == LONG_MAX) && errno != 0)) {
192 1.1 kleink (void)fprintf(stderr,
193 1.1 kleink "invalid incr argument -- %s\n", optarg);
194 1.1 kleink exit(EXIT_FAILURE);
195 1.1 kleink }
196 1.1 kleink incr = (int)val;
197 1.1 kleink break;
198 1.1 kleink case 'l':
199 1.1 kleink errno = 0;
200 1.1 kleink uval = strtoul(optarg, &ep, 10);
201 1.1 kleink if ((ep != NULL && *ep != '\0') ||
202 1.1 kleink (uval == ULONG_MAX && errno != 0)) {
203 1.1 kleink (void)fprintf(stderr,
204 1.1 kleink "invalid num argument -- %s\n", optarg);
205 1.1 kleink exit(EXIT_FAILURE);
206 1.1 kleink }
207 1.1 kleink nblank = (unsigned int)uval;
208 1.1 kleink break;
209 1.1 kleink case 'n':
210 1.1 kleink if (strcmp(optarg, "ln") == 0) {
211 1.1 kleink format = FORMAT_LN;
212 1.1 kleink } else if (strcmp(optarg, "rn") == 0) {
213 1.1 kleink format = FORMAT_RN;
214 1.1 kleink } else if (strcmp(optarg, "rz") == 0) {
215 1.1 kleink format = FORMAT_RZ;
216 1.1 kleink } else {
217 1.1 kleink (void)fprintf(stderr,
218 1.1 kleink "nl: illegal format -- %s\n", optarg);
219 1.1 kleink exit(EXIT_FAILURE);
220 1.1 kleink }
221 1.1 kleink break;
222 1.1 kleink case 's':
223 1.1 kleink sep = optarg;
224 1.1 kleink break;
225 1.1 kleink case 'v':
226 1.1 kleink errno = 0;
227 1.1 kleink val = strtol(optarg, &ep, 10);
228 1.1 kleink if ((ep != NULL && *ep != '\0') ||
229 1.1 kleink ((val == LONG_MIN || val == LONG_MAX) && errno != 0)) {
230 1.1 kleink (void)fprintf(stderr,
231 1.1 kleink "invalid startnum value -- %s\n", optarg);
232 1.1 kleink exit(EXIT_FAILURE);
233 1.1 kleink }
234 1.1 kleink startnum = (int)val;
235 1.1 kleink break;
236 1.1 kleink case 'w':
237 1.1 kleink errno = 0;
238 1.1 kleink val = strtol(optarg, &ep, 10);
239 1.1 kleink if ((ep != NULL && *ep != '\0') ||
240 1.1 kleink ((val == LONG_MIN || val == LONG_MAX) && errno != 0)) {
241 1.1 kleink (void)fprintf(stderr,
242 1.1 kleink "invalid width value -- %s\n", optarg);
243 1.1 kleink exit(EXIT_FAILURE);
244 1.1 kleink }
245 1.1 kleink width = (int)val;
246 1.1 kleink if (!(width > 0)) {
247 1.1 kleink (void)fprintf(stderr,
248 1.1 kleink "nl: width argument must be > 0 -- %d\n",
249 1.1 kleink width);
250 1.1 kleink exit(EXIT_FAILURE);
251 1.1 kleink }
252 1.1 kleink break;
253 1.1 kleink case '?':
254 1.1 kleink default:
255 1.1 kleink usage();
256 1.1 kleink /* NOTREACHED */
257 1.1 kleink }
258 1.1 kleink }
259 1.1 kleink argc -= optind;
260 1.1 kleink argv += optind;
261 1.1 kleink
262 1.1 kleink switch (argc) {
263 1.1 kleink case 0:
264 1.1 kleink break;
265 1.1 kleink case 1:
266 1.1 kleink if (freopen(argv[0], "r", stdin) == NULL) {
267 1.1 kleink perror(argv[0]);
268 1.1 kleink exit(EXIT_FAILURE);
269 1.1 kleink }
270 1.1 kleink break;
271 1.1 kleink default:
272 1.1 kleink usage();
273 1.1 kleink /* NOTREACHED */
274 1.1 kleink }
275 1.1 kleink
276 1.1 kleink /* Determine the maximum input line length to operate on. */
277 1.1 kleink if ((val = sysconf(_SC_LINE_MAX)) == -1) /* ignore errno */
278 1.1 kleink val = LINE_MAX;
279 1.1 kleink /* Allocate sufficient buffer space (including the terminating NUL). */
280 1.1 kleink buffersize = (size_t)val + 1;
281 1.1 kleink if ((buffer = malloc(buffersize)) == NULL) {
282 1.1 kleink perror("cannot allocate input line buffer");
283 1.1 kleink exit(EXIT_FAILURE);
284 1.1 kleink }
285 1.1 kleink
286 1.1 kleink /* Allocate a buffer suitable for preformatting line number. */
287 1.1 kleink intbuffersize = max(INT_STRLEN_MAXIMUM, width) + 1; /* NUL */
288 1.1 kleink if ((intbuffer = malloc(intbuffersize)) == NULL) {
289 1.1 kleink perror("cannot allocate preformatting buffer");
290 1.1 kleink exit(EXIT_FAILURE);
291 1.1 kleink }
292 1.1 kleink
293 1.1 kleink /* Do the work. */
294 1.1 kleink filter();
295 1.1 kleink
296 1.1 kleink exit(EXIT_SUCCESS);
297 1.1 kleink /* NOTREACHED */
298 1.1 kleink }
299 1.1 kleink
300 1.1 kleink static void
301 1.1 kleink filter()
302 1.1 kleink {
303 1.1 kleink int line; /* logical line number */
304 1.1 kleink int section; /* logical page section */
305 1.1 kleink unsigned int adjblank; /* adjacent blank lines */
306 1.1 kleink int consumed; /* intbuffer measurement */
307 1.1 kleink int donumber, idx;
308 1.1 kleink
309 1.1 kleink adjblank = 0;
310 1.1 kleink line = startnum;
311 1.1 kleink section = BODY;
312 1.2 kleink #ifdef __GNUC__
313 1.7 mrg donumber = 0; /* avoid bogus `uninitialized' warning */
314 1.2 kleink #endif
315 1.1 kleink
316 1.1 kleink while (fgets(buffer, (int)buffersize, stdin) != NULL) {
317 1.1 kleink for (idx = FOOTER; idx <= NP_LAST; idx++) {
318 1.1 kleink /* Does it look like a delimiter? */
319 1.1 kleink if (buffer[2 * idx + 0] == delim[0] &&
320 1.1 kleink buffer[2 * idx + 1] == delim[1]) {
321 1.1 kleink /* Was this the whole line? */
322 1.1 kleink if (buffer[2 * idx + 2] == '\n') {
323 1.1 kleink section = idx;
324 1.1 kleink adjblank = 0;
325 1.1 kleink if (restart)
326 1.1 kleink line = startnum;
327 1.1 kleink goto nextline;
328 1.1 kleink }
329 1.1 kleink } else {
330 1.1 kleink break;
331 1.1 kleink }
332 1.1 kleink }
333 1.1 kleink
334 1.1 kleink switch (numbering_properties[section].type) {
335 1.1 kleink case number_all:
336 1.1 kleink /*
337 1.1 kleink * Doing this for number_all only is disputable, but
338 1.1 kleink * the standard expresses an explicit dependency on
339 1.1 kleink * `-b a' etc.
340 1.1 kleink */
341 1.1 kleink if (buffer[0] == '\n' && ++adjblank < nblank)
342 1.1 kleink donumber = 0;
343 1.1 kleink else
344 1.1 kleink donumber = 1, adjblank = 0;
345 1.1 kleink break;
346 1.1 kleink case number_nonempty:
347 1.1 kleink donumber = (buffer[0] != '\n');
348 1.1 kleink break;
349 1.1 kleink case number_none:
350 1.1 kleink donumber = 0;
351 1.1 kleink break;
352 1.1 kleink case number_regex:
353 1.1 kleink donumber =
354 1.1 kleink (regexec(&numbering_properties[section].expr,
355 1.1 kleink buffer, 0, NULL, 0) == 0);
356 1.1 kleink break;
357 1.1 kleink }
358 1.1 kleink
359 1.1 kleink if (donumber) {
360 1.1 kleink /* Note: sprintf() is safe here. */
361 1.1 kleink consumed = sprintf(intbuffer, format, width, line);
362 1.1 kleink (void)printf("%s",
363 1.1 kleink intbuffer + max(0, consumed - width));
364 1.1 kleink line += incr;
365 1.1 kleink } else {
366 1.1 kleink (void)printf("%*s", width, "");
367 1.1 kleink }
368 1.1 kleink (void)printf("%s%s", sep, buffer);
369 1.1 kleink
370 1.1 kleink if (ferror(stdout)) {
371 1.1 kleink perror("output error");
372 1.1 kleink exit(EXIT_FAILURE);
373 1.1 kleink }
374 1.1 kleink nextline:
375 1.1 kleink ;
376 1.1 kleink }
377 1.1 kleink
378 1.1 kleink if (ferror(stdin)) {
379 1.1 kleink perror("input error");
380 1.1 kleink exit(EXIT_FAILURE);
381 1.1 kleink }
382 1.1 kleink }
383 1.1 kleink
384 1.1 kleink /*
385 1.1 kleink * Various support functions.
386 1.1 kleink */
387 1.1 kleink
388 1.1 kleink static void
389 1.1 kleink parse_numbering(argstr, section)
390 1.1 kleink const char *argstr;
391 1.1 kleink int section;
392 1.1 kleink {
393 1.1 kleink int error;
394 1.5 kleink char errorbuf[NL_TEXTMAX];
395 1.1 kleink
396 1.1 kleink switch (argstr[0]) {
397 1.1 kleink case 'a':
398 1.1 kleink numbering_properties[section].type = number_all;
399 1.1 kleink break;
400 1.1 kleink case 'n':
401 1.1 kleink numbering_properties[section].type = number_none;
402 1.1 kleink break;
403 1.1 kleink case 't':
404 1.1 kleink numbering_properties[section].type = number_nonempty;
405 1.1 kleink break;
406 1.1 kleink case 'p':
407 1.1 kleink /* If there was a previous expression, throw it away. */
408 1.1 kleink if (numbering_properties[section].type == number_regex)
409 1.1 kleink regfree(&numbering_properties[section].expr);
410 1.1 kleink else
411 1.1 kleink numbering_properties[section].type = number_regex;
412 1.1 kleink
413 1.1 kleink /* Compile/validate the supplied regular expression. */
414 1.1 kleink if ((error = regcomp(&numbering_properties[section].expr,
415 1.1 kleink &argstr[1], REG_NEWLINE|REG_NOSUB)) != 0) {
416 1.1 kleink (void)regerror(error,
417 1.1 kleink &numbering_properties[section].expr,
418 1.1 kleink errorbuf, sizeof (errorbuf));
419 1.1 kleink (void)fprintf(stderr,
420 1.1 kleink "nl: %s expr: %s -- %s\n",
421 1.1 kleink numbering_properties[section].name, errorbuf,
422 1.1 kleink &argstr[1]);
423 1.1 kleink exit(EXIT_FAILURE);
424 1.1 kleink }
425 1.1 kleink break;
426 1.1 kleink default:
427 1.1 kleink (void)fprintf(stderr,
428 1.1 kleink "nl: illegal %s line numbering type -- %s\n",
429 1.1 kleink numbering_properties[section].name, argstr);
430 1.1 kleink exit(EXIT_FAILURE);
431 1.1 kleink }
432 1.1 kleink }
433 1.1 kleink
434 1.1 kleink static void
435 1.1 kleink usage()
436 1.1 kleink {
437 1.1 kleink
438 1.1 kleink (void)fprintf(stderr, "usage: nl [-p] [-b type] [-d delim] [-f type] \
439 1.1 kleink [-h type] [-i incr] [-l num]\n\t[-n format] [-s sep] [-v startnum] [-w width] \
440 1.1 kleink [file]\n");
441 1.1 kleink exit(EXIT_FAILURE);
442 1.1 kleink }
443