1 /* nl -- number lines of files
2 Copyright (C) 1989, 1992, 1995 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Scott Bartram (nancy!scott@uunet.uu.net)
19 Revised by David MacKenzie (djm@gnu.ai.mit.edu) */
24 #include <sys/types.h>
38 # define UINT_MAX ((unsigned int) ~(unsigned int) 0)
42 # define INT_MAX ((int) (UINT_MAX >> 1))
45 #include "linebuffer.h"
56 /* Line-number formats. */
59 FORMAT_RIGHT_NOLZ
, /* Right justified, no leading zeroes. */
60 FORMAT_RIGHT_LZ
, /* Right justified, leading zeroes. */
61 FORMAT_LEFT
/* Left justified, no leading zeroes. */
64 /* Default section delimiter characters. */
65 #define DEFAULT_SECTION_DELIMITERS "\\:"
67 /* Types of input lines: either one of the section delimiters,
71 Header
, Body
, Footer
, Text
77 /* The name this program was run with. */
80 /* Format of body lines (-b). */
81 static char *body_type
= "t";
83 /* Format of header lines (-h). */
84 static char *header_type
= "n";
86 /* Format of footer lines (-f). */
87 static char *footer_type
= "n";
89 /* Format currently being used (body, header, or footer). */
90 static char *current_type
;
92 /* Regex for body lines to number (-bp). */
93 static struct re_pattern_buffer body_regex
;
95 /* Regex for header lines to number (-hp). */
96 static struct re_pattern_buffer header_regex
;
98 /* Regex for footer lines to number (-fp). */
99 static struct re_pattern_buffer footer_regex
;
101 /* Pointer to current regex, if any. */
102 static struct re_pattern_buffer
*current_regex
= NULL
;
104 /* Separator string to print after line number (-s). */
105 static char *separator_str
= "\t";
107 /* Input section delimiter string (-d). */
108 static char *section_del
= DEFAULT_SECTION_DELIMITERS
;
110 /* Header delimiter string. */
111 static char *header_del
= NULL
;
113 /* Header section delimiter length. */
114 static int header_del_len
;
116 /* Body delimiter string. */
117 static char *body_del
= NULL
;
119 /* Body section delimiter length. */
120 static int body_del_len
;
122 /* Footer delimiter string. */
123 static char *footer_del
= NULL
;
125 /* Footer section delimiter length. */
126 static int footer_del_len
;
129 static struct linebuffer line_buf
;
131 /* printf format string for line number. */
132 static char *print_fmt
;
134 /* printf format string for unnumbered lines. */
135 static char *print_no_line_fmt
= NULL
;
137 /* Starting line number on each page (-v). */
138 static int starting_line_number
= 1;
140 /* Line number increment (-i). */
141 static int page_incr
= 1;
143 /* If TRUE, reset line number at start of each page (-p). */
144 static int reset_numbers
= TRUE
;
146 /* Number of blank lines to consider to be one line for numbering (-l). */
147 static int blank_join
= 1;
149 /* Width of line numbers (-w). */
150 static int lineno_width
= 6;
152 /* Line number format (-n). */
153 static enum number_format lineno_format
= FORMAT_RIGHT_NOLZ
;
155 /* Current print line number. */
158 /* Nonzero if we have ever read standard input. */
159 static int have_read_stdin
;
161 /* If nonzero, display usage information and exit. */
162 static int show_help
;
164 /* If nonzero, print the version on standard output then exit. */
165 static int show_version
;
167 static struct option
const longopts
[] =
169 {"header-numbering", required_argument
, NULL
, 'h'},
170 {"body-numbering", required_argument
, NULL
, 'b'},
171 {"footer-numbering", required_argument
, NULL
, 'f'},
172 {"starting-line-number", required_argument
, NULL
, 'v'},
173 {"page-increment", required_argument
, NULL
, 'i'},
174 {"no-renumber", no_argument
, NULL
, 'p'},
175 {"join-blank-lines", required_argument
, NULL
, 'l'},
176 {"number-separator", required_argument
, NULL
, 's'},
177 {"number-width", required_argument
, NULL
, 'w'},
178 {"number-format", required_argument
, NULL
, 'n'},
179 {"section-delimiter", required_argument
, NULL
, 'd'},
180 {"help", no_argument
, &show_help
, 1},
181 {"version", no_argument
, &show_version
, 1},
185 /* Print a usage message and quit. */
191 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
196 Usage: %s [OPTION]... [FILE]...\n\
200 Write each FILE to standard output, with line numbers added.\n\
201 With no FILE, or when FILE is -, read standard input.\n\
203 -b, --body-numbering=STYLE use STYLE for numbering body lines\n\
204 -d, --section-delimiter=CC use CC for separating logical pages\n\
205 -f, --footer-numbering=STYLE use STYLE for numbering footer lines\n\
206 -h, --header-numbering=STYLE use STYLE for numbering header lines\n\
207 -i, --page-increment=NUMBER line number increment at each line\n\
208 -l, --join-blank-lines=NUMBER group of NUMBER empty lines counted as one\n\
209 -n, --number-format=FORMAT insert line numbers according to FORMAT\n\
210 -p, --no-renumber do not reset line numbers at logical pages\n\
211 -s, --number-separator=STRING add STRING after (possible) line number\n\
212 -v, --first-page=NUMBER first line number on each logical page\n\
213 -w, --number-width=NUMBER use NUMBER columns for line numbers\n\
214 --help display this help and exit\n\
215 --version output version information and exit\n\
217 By default, selects -v1 -i1 -l1 -sTAB -w6 -nrn -hn -bt -fn. CC are\n\
218 two delimiter characters for separating logical pages, a missing\n\
219 second character implies :. Type \\\\ for \\. STYLE is one of:\n\
221 a number all lines\n\
222 t number only nonempty lines\n\
224 pREGEXP number only lines that contain a match for REGEXP\n\
228 ln left justified, no leading zeros\n\
229 rn right justified, no leading zeros\n\
230 rz right justified, leading zeros\n\
237 /* Build the printf format string, based on `lineno_format'. */
240 build_print_fmt (void)
242 /* 12 = 10 chars for lineno_width, 1 for %, 1 for \0. */
243 print_fmt
= xmalloc (strlen (separator_str
) + 12);
244 switch (lineno_format
)
246 case FORMAT_RIGHT_NOLZ
:
247 sprintf (print_fmt
, "%%%dd%s", lineno_width
, separator_str
);
249 case FORMAT_RIGHT_LZ
:
250 sprintf (print_fmt
, "%%0%dd%s", lineno_width
, separator_str
);
253 sprintf (print_fmt
, "%%-%dd%s", lineno_width
, separator_str
);
258 /* Set the command line flag TYPEP and possibly the regex pointer REGEXP,
259 according to `optarg'. */
262 build_type_arg (char **typep
, struct re_pattern_buffer
*regexp
)
277 optlen
= strlen (optarg
);
278 regexp
->allocated
= optlen
* 2;
279 regexp
->buffer
= (unsigned char *) xmalloc (regexp
->allocated
);
280 regexp
->translate
= NULL
;
281 regexp
->fastmap
= xmalloc (256);
282 regexp
->fastmap_accurate
= 0;
283 errmsg
= re_compile_pattern (optarg
, optlen
, regexp
);
285 error (1, 0, "%s", errmsg
);
294 /* Print and increment the line number. */
299 printf (print_fmt
, line_no
);
300 line_no
+= page_incr
;
303 /* Switch to a header section. */
308 current_type
= header_type
;
309 current_regex
= &header_regex
;
311 line_no
= starting_line_number
;
315 /* Switch to a body section. */
320 current_type
= body_type
;
321 current_regex
= &body_regex
;
325 /* Switch to a footer section. */
330 current_type
= footer_type
;
331 current_regex
= &footer_regex
;
335 /* Process a regular text line in `line_buf'. */
340 static int blank_lines
= 0; /* Consecutive blank lines so far. */
342 switch (*current_type
)
347 if (line_buf
.length
|| ++blank_lines
== blank_join
)
353 printf (print_no_line_fmt
);
362 printf (print_no_line_fmt
);
365 printf (print_no_line_fmt
);
368 if (re_search (current_regex
, line_buf
.buffer
, line_buf
.length
,
369 0, line_buf
.length
, (struct re_registers
*) 0) < 0)
370 printf (print_no_line_fmt
);
375 fwrite (line_buf
.buffer
, sizeof (char), line_buf
.length
, stdout
);
379 /* Return the type of line in `line_buf'. */
384 if (line_buf
.length
< 2 || memcmp (line_buf
.buffer
, section_del
, 2))
386 if (line_buf
.length
== header_del_len
387 && !memcmp (line_buf
.buffer
, header_del
, header_del_len
))
389 if (line_buf
.length
== body_del_len
390 && !memcmp (line_buf
.buffer
, body_del
, body_del_len
))
392 if (line_buf
.length
== footer_del_len
393 && !memcmp (line_buf
.buffer
, footer_del
, footer_del_len
))
398 /* Read and process the file pointed to by FP. */
401 process_file (FILE *fp
)
403 while (readline (&line_buf
, fp
))
405 switch ((int) check_section ())
423 /* Process file FILE to standard output.
424 Return 0 if successful, 1 if not. */
427 nl_file (const char *file
)
431 if (!strcmp (file
, "-"))
438 stream
= fopen (file
, "r");
441 error (0, errno
, "%s", file
);
446 process_file (stream
);
450 error (0, errno
, "%s", file
);
453 if (!strcmp (file
, "-"))
454 clearerr (stream
); /* Also clear EOF. */
455 else if (fclose (stream
) == EOF
)
457 error (0, errno
, "%s", file
);
464 main (int argc
, char **argv
)
466 int c
, exit_status
= 0;
468 program_name
= argv
[0];
469 setlocale (LC_ALL
, "");
470 bindtextdomain (PACKAGE
, LOCALEDIR
);
471 textdomain (PACKAGE
);
475 while ((c
= getopt_long (argc
, argv
, "h:b:f:v:i:pl:s:w:n:d:", longopts
,
484 if (build_type_arg (&header_type
, &header_regex
) != TRUE
)
488 if (build_type_arg (&body_type
, &body_regex
) != TRUE
)
492 if (build_type_arg (&footer_type
, &footer_regex
) != TRUE
)
498 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, NULL
) != LONGINT_OK
499 /* Allow it to be negative. */
500 || tmp_long
> INT_MAX
)
501 error (1, 0, _("invalid starting line number: `%s'"),
503 starting_line_number
= (int) tmp_long
;
509 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, NULL
) != LONGINT_OK
510 || tmp_long
<= 0 || tmp_long
> INT_MAX
)
511 error (1, 0, _("invalid line number increment: `%s'"),
513 page_incr
= (int) tmp_long
;
517 reset_numbers
= FALSE
;
522 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, NULL
) != LONGINT_OK
523 || tmp_long
<= 0 || tmp_long
> INT_MAX
)
524 error (1, 0, _("invalid number of blank lines: `%s'"),
526 blank_join
= (int) tmp_long
;
530 separator_str
= optarg
;
535 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, NULL
) != LONGINT_OK
536 || tmp_long
<= 0 || tmp_long
> INT_MAX
)
537 error (1, 0, _("invalid line number field width: `%s'"),
539 lineno_width
= (int) tmp_long
;
546 if (optarg
[1] == 'n')
547 lineno_format
= FORMAT_LEFT
;
555 lineno_format
= FORMAT_RIGHT_NOLZ
;
558 lineno_format
= FORMAT_RIGHT_LZ
;
571 section_del
= optarg
;
581 printf ("nl - %s\n", version_string
);
588 /* Initialize the section delimiters. */
589 c
= strlen (section_del
);
591 header_del_len
= c
* 3;
592 header_del
= xmalloc (header_del_len
+ 1);
593 strcat (strcat (strcpy (header_del
, section_del
), section_del
), section_del
);
595 body_del_len
= c
* 2;
596 body_del
= xmalloc (body_del_len
+ 1);
597 strcat (strcpy (body_del
, section_del
), section_del
);
600 footer_del
= xmalloc (footer_del_len
+ 1);
601 strcpy (footer_del
, section_del
);
603 /* Initialize the input buffer. */
604 initbuffer (&line_buf
);
606 /* Initialize the printf format for unnumbered lines. */
607 c
= strlen (separator_str
);
608 print_no_line_fmt
= xmalloc (lineno_width
+ c
+ 1);
609 memset (print_no_line_fmt
, ' ', lineno_width
+ c
);
610 print_no_line_fmt
[lineno_width
+ c
] = '\0';
612 line_no
= starting_line_number
;
613 current_type
= body_type
;
614 current_regex
= &body_regex
;
617 /* Main processing. */
620 exit_status
|= nl_file ("-");
622 for (; optind
< argc
; optind
++)
623 exit_status
|= nl_file (argv
[optind
]);
625 if (have_read_stdin
&& fclose (stdin
) == EOF
)
627 error (0, errno
, "-");
630 if (ferror (stdout
) || fclose (stdout
) == EOF
)
631 error (1, errno
, _("write error"));