1 /* nl -- number lines of files
2 Copyright (C) 1989, 1992 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by Scott Bartram (nancy!scott@uunet.uu.net)
19 Revised by David MacKenzie (djm@gnu.ai.mit.edu) */
22 #include <sys/types.h>
25 #include "linebuffer.h"
33 /* Line-number formats. */
36 FORMAT_RIGHT_NOLZ, /* Right justified, no leading zeroes. */
37 FORMAT_RIGHT_LZ, /* Right justified, leading zeroes. */
38 FORMAT_LEFT /* Left justified, no leading zeroes. */
41 /* Default section delimiter characters. */
42 #define DEFAULT_SECTION_DELIMITERS "\\:"
44 /* Types of input lines: either one of the section delimiters,
48 Header, Body, Footer, Text
51 /* Format of body lines (-b). */
52 static char *body_type = "t";
54 /* Format of header lines (-h). */
55 static char *header_type = "n";
57 /* Format of footer lines (-f). */
58 static char *footer_type = "n";
60 /* Format currently being used (body, header, or footer). */
61 static char *current_type;
63 /* Regex for body lines to number (-bp). */
64 static struct re_pattern_buffer body_regex;
66 /* Regex for header lines to number (-hp). */
67 static struct re_pattern_buffer header_regex;
69 /* Regex for footer lines to number (-fp). */
70 static struct re_pattern_buffer footer_regex;
72 /* Pointer to current regex, if any. */
73 static struct re_pattern_buffer *current_regex = NULL;
75 /* Separator string to print after line number (-s). */
76 static char *separator_str = "\t";
78 /* Input section delimiter string (-d). */
79 static char *section_del = DEFAULT_SECTION_DELIMITERS;
81 /* Header delimiter string. */
82 static char *header_del = NULL;
84 /* Header section delimiter length. */
85 static int header_del_len;
87 /* Body delimiter string. */
88 static char *body_del = NULL;
90 /* Body section delimiter length. */
91 static int body_del_len;
93 /* Footer delimiter string. */
94 static char *footer_del = NULL;
96 /* Footer section delimiter length. */
97 static int footer_del_len;
100 static struct linebuffer line_buf;
102 /* printf format string for line number. */
103 static char *print_fmt;
105 /* printf format string for unnumbered lines. */
106 static char *print_no_line_fmt = NULL;
108 /* Starting line number on each page (-v). */
109 static int page_start = 1;
111 /* Line number increment (-i). */
112 static int page_incr = 1;
114 /* If TRUE, reset line number at start of each page (-p). */
115 static int reset_numbers = TRUE;
117 /* Number of blank lines to consider to be one line for numbering (-l). */
118 static int blank_join = 1;
120 /* Width of line numbers (-w). */
121 static int lineno_width = 6;
123 /* Line number format (-n). */
124 static enum number_format lineno_format = FORMAT_RIGHT_NOLZ;
126 /* Current print line number. */
129 /* The name this program was run with. */
132 /* Nonzero if we have ever read standard input. */
133 static int have_read_stdin;
139 static enum section check_section ();
140 static int build_type_arg ();
141 static int nl_file ();
142 static void usage ();
143 static void process_file ();
144 static void proc_header ();
145 static void proc_body ();
146 static void proc_footer ();
147 static void proc_text ();
148 static void print_lineno ();
149 static void build_print_fmt ();
151 static struct option const longopts[] =
153 {"header-numbering", required_argument, NULL, 'h'},
154 {"body-numbering", required_argument, NULL, 'b'},
155 {"footer-numbering", required_argument, NULL, 'f'},
156 {"first-page", required_argument, NULL, 'v'},
157 {"page-increment", required_argument, NULL, 'i'},
158 {"no-renumber", no_argument, NULL, 'p'},
159 {"join-blank-lines", required_argument, NULL, 'l'},
160 {"number-separator", required_argument, NULL, 's'},
161 {"number-width", required_argument, NULL, 'w'},
162 {"number-format", required_argument, NULL, 'n'},
163 {"section-delimiter", required_argument, NULL, 'd'},
172 int c, exit_status = 0;
174 program_name = argv[0];
177 while ((c = getopt_long (argc, argv, "h:b:f:v:i:pl:s:w:n:d:", longopts,
183 if (build_type_arg (&header_type, &header_regex) != TRUE)
187 if (build_type_arg (&body_type, &body_regex) != TRUE)
191 if (build_type_arg (&footer_type, &footer_regex) != TRUE)
195 page_start = atoi (optarg);
198 page_incr = atoi (optarg);
203 reset_numbers = FALSE;
206 blank_join = atoi (optarg);
209 separator_str = optarg;
212 lineno_width = atoi (optarg);
213 if (lineno_width < 1)
220 if (optarg[1] == 'n')
221 lineno_format = FORMAT_LEFT;
229 lineno_format = FORMAT_RIGHT_NOLZ;
232 lineno_format = FORMAT_RIGHT_LZ;
245 section_del = optarg;
253 /* Initialize the section delimiters. */
254 c = strlen (section_del);
256 header_del_len = c * 3;
257 header_del = xmalloc (header_del_len + 1);
258 strcat (strcat (strcpy (header_del, section_del), section_del), section_del);
260 body_del_len = c * 2;
261 body_del = xmalloc (body_del_len + 1);
262 strcat (strcpy (body_del, section_del), section_del);
265 footer_del = xmalloc (footer_del_len + 1);
266 strcpy (footer_del, section_del);
268 /* Initialize the input buffer. */
269 initbuffer (&line_buf);
271 /* Initialize the printf format for unnumbered lines. */
272 c = strlen (separator_str);
273 print_no_line_fmt = xmalloc (lineno_width + c + 1);
274 memset (print_no_line_fmt, ' ', lineno_width + c);
275 print_no_line_fmt[lineno_width + c] = '\0';
277 line_no = page_start;
278 current_type = body_type;
279 current_regex = &body_regex;
282 /* Main processing. */
285 exit_status |= nl_file ("-");
287 for (; optind < argc; optind++)
288 exit_status |= nl_file (argv[optind]);
290 if (have_read_stdin && fclose (stdin) == EOF)
292 error (0, errno, "-");
295 if (ferror (stdout) || fclose (stdout) == EOF)
296 error (1, 0, "write error");
301 /* Process file FILE to standard output.
302 Return 0 if successful, 1 if not. */
310 if (!strcmp (file, "-"))
317 stream = fopen (file, "r");
320 error (0, errno, "%s", file);
325 process_file (stream);
329 error (0, errno, "%s", file);
332 if (!strcmp (file, "-"))
333 clearerr (stream); /* Also clear EOF. */
334 else if (fclose (stream) == EOF)
336 error (0, errno, "%s", file);
342 /* Read and process the file pointed to by FP. */
348 while (readline (&line_buf, fp))
350 switch ((int) check_section ())
368 /* Return the type of line in `line_buf'. */
373 if (line_buf.length < 2 || memcmp (line_buf.buffer, section_del, 2))
375 if (line_buf.length == header_del_len
376 && !memcmp (line_buf.buffer, header_del, header_del_len))
378 if (line_buf.length == body_del_len
379 && !memcmp (line_buf.buffer, body_del, body_del_len))
381 if (line_buf.length == footer_del_len
382 && !memcmp (line_buf.buffer, footer_del, footer_del_len))
387 /* Switch to a header section. */
392 current_type = header_type;
393 current_regex = &header_regex;
395 line_no = page_start;
399 /* Switch to a body section. */
404 current_type = body_type;
405 current_regex = &body_regex;
409 /* Switch to a footer section. */
414 current_type = footer_type;
415 current_regex = &footer_regex;
419 /* Process a regular text line in `line_buf'. */
424 static int blank_lines = 0; /* Consecutive blank lines so far. */
426 switch (*current_type)
431 if (line_buf.length || ++blank_lines == blank_join)
437 printf (print_no_line_fmt);
446 printf (print_no_line_fmt);
449 printf (print_no_line_fmt);
452 if (re_search (current_regex, line_buf.buffer, line_buf.length,
453 0, line_buf.length, (struct re_registers *) 0) < 0)
454 printf (print_no_line_fmt);
459 fwrite (line_buf.buffer, sizeof (char), line_buf.length, stdout);
463 /* Print and increment the line number. */
468 printf (print_fmt, line_no);
469 line_no += page_incr;
472 /* Build the printf format string, based on `lineno_format'. */
477 /* 12 = 10 chars for lineno_width, 1 for %, 1 for \0. */
478 print_fmt = xmalloc (strlen (separator_str) + 12);
479 switch (lineno_format)
481 case FORMAT_RIGHT_NOLZ:
482 sprintf (print_fmt, "%%%dd%s", lineno_width, separator_str);
484 case FORMAT_RIGHT_LZ:
485 sprintf (print_fmt, "%%0%dd%s", lineno_width, separator_str);
488 sprintf (print_fmt, "%%-%dd%s", lineno_width, separator_str);
493 /* Set the command line flag TYPEP and possibly the regex pointer REGEXP,
494 according to `optarg'. */
497 build_type_arg (typep, regexp)
499 struct re_pattern_buffer *regexp;
514 optlen = strlen (optarg);
515 regexp->allocated = optlen * 2;
516 regexp->buffer = (unsigned char *) xmalloc (regexp->allocated);
517 regexp->translate = NULL;
518 regexp->fastmap = xmalloc (256);
519 regexp->fastmap_accurate = 0;
520 errmsg = re_compile_pattern (optarg, optlen, regexp);
522 error (1, 0, "%s", errmsg);
531 /* Print a usage message and quit. */
537 Usage: %s [-h header-style] [-b body-style] [-f footer-style] [-p] [-d cc]\n\
538 [-v start-number] [-i increment] [-l lines] [-s line-separator]\n\
539 [-w line-no-width] [-n {ln,rn,rz}] [--header-numbering=style]\n\
540 [--body-numbering=style] [--footer-numbering=style]\n\
541 [--first-page=number] [--page-increment=number] [--no-renumber]\n\
542 [--join-blank-lines=number] [--number-separator=string]\n\
543 [--number-width=number] [--number-format={ln,rn,rz}]\n\
544 [--section-delimiter=cc] [file...]\n",