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"
34 /* Line-number formats. */
37 FORMAT_RIGHT_NOLZ, /* Right justified, no leading zeroes. */
38 FORMAT_RIGHT_LZ, /* Right justified, leading zeroes. */
39 FORMAT_LEFT /* Left justified, no leading zeroes. */
42 /* Default section delimiter characters. */
43 #define DEFAULT_SECTION_DELIMITERS "\\:"
45 /* Types of input lines: either one of the section delimiters,
49 Header, Body, Footer, Text
56 static enum section check_section ();
57 static int build_type_arg ();
58 static int nl_file ();
60 static void process_file ();
61 static void proc_header ();
62 static void proc_body ();
63 static void proc_footer ();
64 static void proc_text ();
65 static void print_lineno ();
66 static void build_print_fmt ();
68 /* The name this program was run with. */
71 /* Format of body lines (-b). */
72 static char *body_type = "t";
74 /* Format of header lines (-h). */
75 static char *header_type = "n";
77 /* Format of footer lines (-f). */
78 static char *footer_type = "n";
80 /* Format currently being used (body, header, or footer). */
81 static char *current_type;
83 /* Regex for body lines to number (-bp). */
84 static struct re_pattern_buffer body_regex;
86 /* Regex for header lines to number (-hp). */
87 static struct re_pattern_buffer header_regex;
89 /* Regex for footer lines to number (-fp). */
90 static struct re_pattern_buffer footer_regex;
92 /* Pointer to current regex, if any. */
93 static struct re_pattern_buffer *current_regex = NULL;
95 /* Separator string to print after line number (-s). */
96 static char *separator_str = "\t";
98 /* Input section delimiter string (-d). */
99 static char *section_del = DEFAULT_SECTION_DELIMITERS;
101 /* Header delimiter string. */
102 static char *header_del = NULL;
104 /* Header section delimiter length. */
105 static int header_del_len;
107 /* Body delimiter string. */
108 static char *body_del = NULL;
110 /* Body section delimiter length. */
111 static int body_del_len;
113 /* Footer delimiter string. */
114 static char *footer_del = NULL;
116 /* Footer section delimiter length. */
117 static int footer_del_len;
120 static struct linebuffer line_buf;
122 /* printf format string for line number. */
123 static char *print_fmt;
125 /* printf format string for unnumbered lines. */
126 static char *print_no_line_fmt = NULL;
128 /* Starting line number on each page (-v). */
129 static int page_start = 1;
131 /* Line number increment (-i). */
132 static int page_incr = 1;
134 /* If TRUE, reset line number at start of each page (-p). */
135 static int reset_numbers = TRUE;
137 /* Number of blank lines to consider to be one line for numbering (-l). */
138 static int blank_join = 1;
140 /* Width of line numbers (-w). */
141 static int lineno_width = 6;
143 /* Line number format (-n). */
144 static enum number_format lineno_format = FORMAT_RIGHT_NOLZ;
146 /* Current print line number. */
149 /* Nonzero if we have ever read standard input. */
150 static int have_read_stdin;
152 /* If non-zero, display usage information and exit. */
153 static int show_help;
155 /* If non-zero, print the version on standard output then exit. */
156 static int show_version;
158 static struct option const longopts[] =
160 {"header-numbering", required_argument, NULL, 'h'},
161 {"body-numbering", required_argument, NULL, 'b'},
162 {"footer-numbering", required_argument, NULL, 'f'},
163 {"first-page", required_argument, NULL, 'v'},
164 {"page-increment", required_argument, NULL, 'i'},
165 {"no-renumber", no_argument, NULL, 'p'},
166 {"join-blank-lines", required_argument, NULL, 'l'},
167 {"number-separator", required_argument, NULL, 's'},
168 {"number-width", required_argument, NULL, 'w'},
169 {"number-format", required_argument, NULL, 'n'},
170 {"section-delimiter", required_argument, NULL, 'd'},
171 {"help", no_argument, &show_help, 1},
172 {"version", no_argument, &show_version, 1},
181 int c, exit_status = 0;
183 program_name = argv[0];
186 while ((c = getopt_long (argc, argv, "h:b:f:v:i:pl:s:w:n:d:", longopts,
195 if (build_type_arg (&header_type, &header_regex) != TRUE)
199 if (build_type_arg (&body_type, &body_regex) != TRUE)
203 if (build_type_arg (&footer_type, &footer_regex) != TRUE)
207 page_start = atoi (optarg);
210 page_incr = atoi (optarg);
215 reset_numbers = FALSE;
218 blank_join = atoi (optarg);
221 separator_str = optarg;
224 lineno_width = atoi (optarg);
225 if (lineno_width < 1)
232 if (optarg[1] == 'n')
233 lineno_format = FORMAT_LEFT;
241 lineno_format = FORMAT_RIGHT_NOLZ;
244 lineno_format = FORMAT_RIGHT_LZ;
257 section_del = optarg;
267 printf ("%s\n", version_string);
274 /* Initialize the section delimiters. */
275 c = strlen (section_del);
277 header_del_len = c * 3;
278 header_del = xmalloc (header_del_len + 1);
279 strcat (strcat (strcpy (header_del, section_del), section_del), section_del);
281 body_del_len = c * 2;
282 body_del = xmalloc (body_del_len + 1);
283 strcat (strcpy (body_del, section_del), section_del);
286 footer_del = xmalloc (footer_del_len + 1);
287 strcpy (footer_del, section_del);
289 /* Initialize the input buffer. */
290 initbuffer (&line_buf);
292 /* Initialize the printf format for unnumbered lines. */
293 c = strlen (separator_str);
294 print_no_line_fmt = xmalloc (lineno_width + c + 1);
295 memset (print_no_line_fmt, ' ', lineno_width + c);
296 print_no_line_fmt[lineno_width + c] = '\0';
298 line_no = page_start;
299 current_type = body_type;
300 current_regex = &body_regex;
303 /* Main processing. */
306 exit_status |= nl_file ("-");
308 for (; optind < argc; optind++)
309 exit_status |= nl_file (argv[optind]);
311 if (have_read_stdin && fclose (stdin) == EOF)
313 error (0, errno, "-");
316 if (ferror (stdout) || fclose (stdout) == EOF)
317 error (1, errno, "write error");
322 /* Process file FILE to standard output.
323 Return 0 if successful, 1 if not. */
331 if (!strcmp (file, "-"))
338 stream = fopen (file, "r");
341 error (0, errno, "%s", file);
346 process_file (stream);
350 error (0, errno, "%s", file);
353 if (!strcmp (file, "-"))
354 clearerr (stream); /* Also clear EOF. */
355 else if (fclose (stream) == EOF)
357 error (0, errno, "%s", file);
363 /* Read and process the file pointed to by FP. */
369 while (readline (&line_buf, fp))
371 switch ((int) check_section ())
389 /* Return the type of line in `line_buf'. */
394 if (line_buf.length < 2 || memcmp (line_buf.buffer, section_del, 2))
396 if (line_buf.length == header_del_len
397 && !memcmp (line_buf.buffer, header_del, header_del_len))
399 if (line_buf.length == body_del_len
400 && !memcmp (line_buf.buffer, body_del, body_del_len))
402 if (line_buf.length == footer_del_len
403 && !memcmp (line_buf.buffer, footer_del, footer_del_len))
408 /* Switch to a header section. */
413 current_type = header_type;
414 current_regex = &header_regex;
416 line_no = page_start;
420 /* Switch to a body section. */
425 current_type = body_type;
426 current_regex = &body_regex;
430 /* Switch to a footer section. */
435 current_type = footer_type;
436 current_regex = &footer_regex;
440 /* Process a regular text line in `line_buf'. */
445 static int blank_lines = 0; /* Consecutive blank lines so far. */
447 switch (*current_type)
452 if (line_buf.length || ++blank_lines == blank_join)
458 printf (print_no_line_fmt);
467 printf (print_no_line_fmt);
470 printf (print_no_line_fmt);
473 if (re_search (current_regex, line_buf.buffer, line_buf.length,
474 0, line_buf.length, (struct re_registers *) 0) < 0)
475 printf (print_no_line_fmt);
480 fwrite (line_buf.buffer, sizeof (char), line_buf.length, stdout);
484 /* Print and increment the line number. */
489 printf (print_fmt, line_no);
490 line_no += page_incr;
493 /* Build the printf format string, based on `lineno_format'. */
498 /* 12 = 10 chars for lineno_width, 1 for %, 1 for \0. */
499 print_fmt = xmalloc (strlen (separator_str) + 12);
500 switch (lineno_format)
502 case FORMAT_RIGHT_NOLZ:
503 sprintf (print_fmt, "%%%dd%s", lineno_width, separator_str);
505 case FORMAT_RIGHT_LZ:
506 sprintf (print_fmt, "%%0%dd%s", lineno_width, separator_str);
509 sprintf (print_fmt, "%%-%dd%s", lineno_width, separator_str);
514 /* Set the command line flag TYPEP and possibly the regex pointer REGEXP,
515 according to `optarg'. */
518 build_type_arg (typep, regexp)
520 struct re_pattern_buffer *regexp;
535 optlen = strlen (optarg);
536 regexp->allocated = optlen * 2;
537 regexp->buffer = (unsigned char *) xmalloc (regexp->allocated);
538 regexp->translate = NULL;
539 regexp->fastmap = xmalloc (256);
540 regexp->fastmap_accurate = 0;
541 errmsg = re_compile_pattern (optarg, optlen, regexp);
543 error (1, 0, "%s", errmsg);
552 /* Print a usage message and quit. */
558 Usage: %s [-h header-style] [-b body-style] [-f footer-style] [-p] [-d cc]\n\
559 [-v start-number] [-i increment] [-l lines] [-s line-separator]\n\
560 [-w line-no-width] [-n {ln,rn,rz}] [--header-numbering=style]\n\
561 [--body-numbering=style] [--footer-numbering=style]\n\
562 [--first-page=number] [--page-increment=number] [--no-renumber]\n\
563 [--join-blank-lines=number] [--number-separator=string]\n\
564 [--number-width=number] [--number-format={ln,rn,rz}]\n\
565 [--section-delimiter=cc] [--help] [--version] [file...]\n",