1 /* nl -- number lines of files
2 Copyright (C) 89, 92, 1995-2002 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 Foundation,
16 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>
33 #include "linebuffer.h"
36 /* The official name of this program (e.g., no `g' prefix). */
37 #define PROGRAM_NAME "nl"
39 #define AUTHORS N_ ("Scott Bartram and David MacKenzie")
46 /* Line-number formats. */
49 FORMAT_RIGHT_NOLZ
, /* Right justified, no leading zeroes. */
50 FORMAT_RIGHT_LZ
, /* Right justified, leading zeroes. */
51 FORMAT_LEFT
/* Left justified, no leading zeroes. */
54 /* Default section delimiter characters. */
55 #define DEFAULT_SECTION_DELIMITERS "\\:"
57 /* Types of input lines: either one of the section delimiters,
61 Header
, Body
, Footer
, Text
64 /* The name this program was run with. */
67 /* Format of body lines (-b). */
68 static char *body_type
= "t";
70 /* Format of header lines (-h). */
71 static char *header_type
= "n";
73 /* Format of footer lines (-f). */
74 static char *footer_type
= "n";
76 /* Format currently being used (body, header, or footer). */
77 static char *current_type
;
79 /* Regex for body lines to number (-bp). */
80 static struct re_pattern_buffer body_regex
;
82 /* Regex for header lines to number (-hp). */
83 static struct re_pattern_buffer header_regex
;
85 /* Regex for footer lines to number (-fp). */
86 static struct re_pattern_buffer footer_regex
;
88 /* Pointer to current regex, if any. */
89 static struct re_pattern_buffer
*current_regex
= NULL
;
91 /* Separator string to print after line number (-s). */
92 static char *separator_str
= "\t";
94 /* Input section delimiter string (-d). */
95 static char *section_del
= DEFAULT_SECTION_DELIMITERS
;
97 /* Header delimiter string. */
98 static char *header_del
= NULL
;
100 /* Header section delimiter length. */
101 static size_t header_del_len
;
103 /* Body delimiter string. */
104 static char *body_del
= NULL
;
106 /* Body section delimiter length. */
107 static size_t body_del_len
;
109 /* Footer delimiter string. */
110 static char *footer_del
= NULL
;
112 /* Footer section delimiter length. */
113 static size_t footer_del_len
;
116 static struct linebuffer line_buf
;
118 /* printf format string for line number. */
119 static char *print_fmt
;
121 /* printf format string for unnumbered lines. */
122 static char *print_no_line_fmt
= NULL
;
124 /* Starting line number on each page (-v). */
125 static int starting_line_number
= 1;
127 /* Line number increment (-i). */
128 static int page_incr
= 1;
130 /* If TRUE, reset line number at start of each page (-p). */
131 static int reset_numbers
= TRUE
;
133 /* Number of blank lines to consider to be one line for numbering (-l). */
134 static int blank_join
= 1;
136 /* Width of line numbers (-w). */
137 static int lineno_width
= 6;
139 /* Line number format (-n). */
140 static enum number_format lineno_format
= FORMAT_RIGHT_NOLZ
;
142 /* Current print line number. */
145 /* Nonzero if we have ever read standard input. */
146 static int have_read_stdin
;
148 static struct option
const longopts
[] =
150 {"header-numbering", required_argument
, NULL
, 'h'},
151 {"body-numbering", required_argument
, NULL
, 'b'},
152 {"footer-numbering", required_argument
, NULL
, 'f'},
153 {"starting-line-number", required_argument
, NULL
, 'v'},
154 {"page-increment", required_argument
, NULL
, 'i'},
155 {"no-renumber", no_argument
, NULL
, 'p'},
156 {"join-blank-lines", required_argument
, NULL
, 'l'},
157 {"number-separator", required_argument
, NULL
, 's'},
158 {"number-width", required_argument
, NULL
, 'w'},
159 {"number-format", required_argument
, NULL
, 'n'},
160 {"section-delimiter", required_argument
, NULL
, 'd'},
161 {GETOPT_HELP_OPTION_DECL
},
162 {GETOPT_VERSION_OPTION_DECL
},
166 /* Print a usage message and quit. */
172 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
177 Usage: %s [OPTION]... [FILE]...\n\
181 Write each FILE to standard output, with line numbers added.\n\
182 With no FILE, or when FILE is -, read standard input.\n\
186 Mandatory arguments to long options are mandatory for short options too.\n\
189 -b, --body-numbering=STYLE use STYLE for numbering body lines\n\
190 -d, --section-delimiter=CC use CC for separating logical pages\n\
191 -f, --footer-numbering=STYLE use STYLE for numbering footer lines\n\
194 -h, --header-numbering=STYLE use STYLE for numbering header lines\n\
195 -i, --page-increment=NUMBER line number increment at each line\n\
196 -l, --join-blank-lines=NUMBER group of NUMBER empty lines counted as one\n\
197 -n, --number-format=FORMAT insert line numbers according to FORMAT\n\
198 -p, --no-renumber do not reset line numbers at logical pages\n\
199 -s, --number-separator=STRING add STRING after (possible) line number\n\
202 -v, --first-page=NUMBER first line number on each logical page\n\
203 -w, --number-width=NUMBER use NUMBER columns for line numbers\n\
205 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
206 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
209 By default, selects -v1 -i1 -l1 -sTAB -w6 -nrn -hn -bt -fn. CC are\n\
210 two delimiter characters for separating logical pages, a missing\n\
211 second character implies :. Type \\\\ for \\. STYLE is one of:\n\
215 a number all lines\n\
216 t number only nonempty lines\n\
218 pREGEXP number only lines that contain a match for REGEXP\n\
222 ln left justified, no leading zeros\n\
223 rn right justified, no leading zeros\n\
224 rz right justified, leading zeros\n\
227 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
229 exit (status
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);
232 /* Build the printf format string, based on `lineno_format'. */
235 build_print_fmt (void)
237 print_fmt
= xmalloc ( 1 /* for `%' */
238 + 1 /* for `-' or `0' */
239 + INT_STRLEN_BOUND (lineno_width
)
241 + 1 /* for trailing NUL byte */ );
242 switch (lineno_format
)
244 case FORMAT_RIGHT_NOLZ
:
245 sprintf (print_fmt
, "%%%dd", lineno_width
);
247 case FORMAT_RIGHT_LZ
:
248 sprintf (print_fmt
, "%%0%dd", lineno_width
);
251 sprintf (print_fmt
, "%%-%dd", lineno_width
);
256 /* Set the command line flag TYPEP and possibly the regex pointer REGEXP,
257 according to `optarg'. */
260 build_type_arg (char **typep
, struct re_pattern_buffer
*regexp
)
275 optlen
= strlen (optarg
);
276 regexp
->allocated
= optlen
* 2;
277 regexp
->buffer
= (unsigned char *) xmalloc (regexp
->allocated
);
278 regexp
->translate
= NULL
;
279 regexp
->fastmap
= xmalloc (256);
280 regexp
->fastmap_accurate
= 0;
281 errmsg
= re_compile_pattern (optarg
, optlen
, regexp
);
283 error (EXIT_FAILURE
, 0, "%s", errmsg
);
292 /* Print the line number and separator; increment the line number. */
297 printf (print_fmt
, line_no
);
298 fputs (separator_str
, stdout
);
299 line_no
+= page_incr
;
302 /* Switch to a header section. */
307 current_type
= header_type
;
308 current_regex
= &header_regex
;
310 line_no
= starting_line_number
;
314 /* Switch to a body section. */
319 current_type
= body_type
;
320 current_regex
= &body_regex
;
324 /* Switch to a footer section. */
329 current_type
= footer_type
;
330 current_regex
= &footer_regex
;
334 /* Process a regular text line in `line_buf'. */
339 static int blank_lines
= 0; /* Consecutive blank lines so far. */
341 switch (*current_type
)
346 if (1 < line_buf
.length
|| ++blank_lines
== blank_join
)
352 puts (print_no_line_fmt
);
358 if (1 < line_buf
.length
)
361 puts (print_no_line_fmt
);
364 puts (print_no_line_fmt
);
367 if (re_search (current_regex
, line_buf
.buffer
, line_buf
.length
- 1,
368 0, line_buf
.length
- 1, (struct re_registers
*) 0) < 0)
369 puts (print_no_line_fmt
);
374 fwrite (line_buf
.buffer
, sizeof (char), line_buf
.length
, stdout
);
377 /* Return the type of line in `line_buf'. */
382 size_t len
= line_buf
.length
- 1;
384 if (len
< 2 || memcmp (line_buf
.buffer
, section_del
, 2))
386 if (len
== header_del_len
387 && !memcmp (line_buf
.buffer
, header_del
, header_del_len
))
389 if (len
== body_del_len
390 && !memcmp (line_buf
.buffer
, body_del
, body_del_len
))
392 if (len
== 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 (STREQ (file
, "-"))
438 stream
= fopen (file
, "r");
441 error (0, errno
, "%s", file
);
446 process_file (stream
);
450 error (0, errno
, "%s", file
);
453 if (STREQ (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;
469 program_name
= argv
[0];
470 setlocale (LC_ALL
, "");
471 bindtextdomain (PACKAGE
, LOCALEDIR
);
472 textdomain (PACKAGE
);
474 atexit (close_stdout
);
478 while ((c
= getopt_long (argc
, argv
, "h:b:f:v:i:pl:s:w:n:d:", longopts
,
487 if (build_type_arg (&header_type
, &header_regex
) != TRUE
)
491 if (build_type_arg (&body_type
, &body_regex
) != TRUE
)
495 if (build_type_arg (&footer_type
, &footer_regex
) != TRUE
)
501 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, "") != LONGINT_OK
502 /* Allow it to be negative. */
503 || tmp_long
> INT_MAX
)
504 error (EXIT_FAILURE
, 0, _("invalid starting line number: `%s'"),
506 starting_line_number
= (int) tmp_long
;
512 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, "") != LONGINT_OK
513 || tmp_long
<= 0 || tmp_long
> INT_MAX
)
514 error (EXIT_FAILURE
, 0, _("invalid line number increment: `%s'"),
516 page_incr
= (int) tmp_long
;
520 reset_numbers
= FALSE
;
525 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, "") != LONGINT_OK
526 || tmp_long
<= 0 || tmp_long
> INT_MAX
)
527 error (EXIT_FAILURE
, 0, _("invalid number of blank lines: `%s'"),
529 blank_join
= (int) tmp_long
;
533 separator_str
= optarg
;
538 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, "") != LONGINT_OK
539 || tmp_long
<= 0 || tmp_long
> INT_MAX
)
540 error (EXIT_FAILURE
, 0,
541 _("invalid line number field width: `%s'"),
543 lineno_width
= (int) tmp_long
;
550 if (optarg
[1] == 'n')
551 lineno_format
= FORMAT_LEFT
;
559 lineno_format
= FORMAT_RIGHT_NOLZ
;
562 lineno_format
= FORMAT_RIGHT_LZ
;
575 section_del
= optarg
;
577 case_GETOPT_HELP_CHAR
;
578 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
585 /* Initialize the section delimiters. */
586 len
= strlen (section_del
);
588 header_del_len
= len
* 3;
589 header_del
= xmalloc (header_del_len
+ 1);
590 strcat (strcat (strcpy (header_del
, section_del
), section_del
), section_del
);
592 body_del_len
= len
* 2;
593 body_del
= xmalloc (body_del_len
+ 1);
594 strcat (strcpy (body_del
, section_del
), section_del
);
596 footer_del_len
= len
;
597 footer_del
= xmalloc (footer_del_len
+ 1);
598 strcpy (footer_del
, section_del
);
600 /* Initialize the input buffer. */
601 initbuffer (&line_buf
);
603 /* Initialize the printf format for unnumbered lines. */
604 len
= strlen (separator_str
);
605 print_no_line_fmt
= xmalloc (lineno_width
+ len
+ 1);
606 memset (print_no_line_fmt
, ' ', lineno_width
+ len
);
607 print_no_line_fmt
[lineno_width
+ len
] = '\0';
609 line_no
= starting_line_number
;
610 current_type
= body_type
;
611 current_regex
= &body_regex
;
614 /* Main processing. */
617 exit_status
|= nl_file ("-");
619 for (; optind
< argc
; optind
++)
620 exit_status
|= nl_file (argv
[optind
]);
622 if (have_read_stdin
&& fclose (stdin
) == EOF
)
624 error (0, errno
, "-");
628 exit (exit_status
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);