doc: pwd: improve the -P help description
[coreutils.git] / src / nl.c
blobf77a2aa9e85768381a77b960749814be29d85abe
1 /* nl -- number lines of files
2 Copyright (C) 1989-2024 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 3 of the License, or
7 (at your option) any later version.
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, see <https://www.gnu.org/licenses/>. */
17 /* Written by Scott Bartram (nancy!scott@uunet.uu.net)
18 Revised by David MacKenzie (djm@gnu.ai.mit.edu) */
20 #include <config.h>
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <getopt.h>
26 #include "system.h"
28 #include <regex.h>
30 #include "fadvise.h"
31 #include "linebuffer.h"
32 #include "quote.h"
33 #include "xdectoint.h"
35 /* The official name of this program (e.g., no 'g' prefix). */
36 #define PROGRAM_NAME "nl"
38 #define AUTHORS \
39 proper_name ("Scott Bartram"), \
40 proper_name ("David MacKenzie")
42 /* Line-number formats. They are given an int width, an intmax_t
43 value, and a string separator. */
45 /* Right justified, no leading zeroes. */
46 static char const FORMAT_RIGHT_NOLZ[] = "%*jd%s";
48 /* Right justified, leading zeroes. */
49 static char const FORMAT_RIGHT_LZ[] = "%0*jd%s";
51 /* Left justified, no leading zeroes. */
52 static char const FORMAT_LEFT[] = "%-*jd%s";
54 /* Default section delimiter characters. */
55 static char DEFAULT_SECTION_DELIMITERS[] = "\\:";
57 /* Types of input lines: either one of the section delimiters,
58 or text to output. */
59 enum section
61 Header, Body, Footer, Text
64 /* Format of body lines (-b). */
65 static char const *body_type = "t";
67 /* Format of header lines (-h). */
68 static char const *header_type = "n";
70 /* Format of footer lines (-f). */
71 static char const *footer_type = "n";
73 /* Format currently being used (body, header, or footer). */
74 static char const *current_type;
76 /* Regex for body lines to number (-bp). */
77 static struct re_pattern_buffer body_regex;
79 /* Regex for header lines to number (-hp). */
80 static struct re_pattern_buffer header_regex;
82 /* Regex for footer lines to number (-fp). */
83 static struct re_pattern_buffer footer_regex;
85 /* Fastmaps for the above. */
86 static char body_fastmap[UCHAR_MAX + 1];
87 static char header_fastmap[UCHAR_MAX + 1];
88 static char footer_fastmap[UCHAR_MAX + 1];
90 /* Pointer to current regex, if any. */
91 static struct re_pattern_buffer *current_regex = nullptr;
93 /* Separator string to print after line number (-s). */
94 static char const *separator_str = "\t";
96 /* Input section delimiter string (-d). */
97 static char *section_del = DEFAULT_SECTION_DELIMITERS;
99 /* Header delimiter string. */
100 static char *header_del = nullptr;
102 /* Header section delimiter length. */
103 static size_t header_del_len;
105 /* Body delimiter string. */
106 static char *body_del = nullptr;
108 /* Body section delimiter length. */
109 static size_t body_del_len;
111 /* Footer delimiter string. */
112 static char *footer_del = nullptr;
114 /* Footer section delimiter length. */
115 static size_t footer_del_len;
117 /* Input buffer. */
118 static struct linebuffer line_buf;
120 /* printf format string for unnumbered lines. */
121 static char *print_no_line_fmt = nullptr;
123 /* Starting line number on each page (-v). */
124 static intmax_t starting_line_number = 1;
126 /* Line number increment (-i). */
127 static intmax_t page_incr = 1;
129 /* If true, reset line number at start of each page (-p). */
130 static bool reset_numbers = true;
132 /* Number of blank lines to consider to be one line for numbering (-l). */
133 static intmax_t blank_join = 1;
135 /* Width of line numbers (-w). */
136 static int lineno_width = 6;
138 /* Line number format (-n). */
139 static char const *lineno_format = FORMAT_RIGHT_NOLZ;
141 /* Current print line number. */
142 static intmax_t line_no;
144 /* Whether the current line number has incremented past limits. */
145 static bool line_no_overflow;
147 /* True if we have ever read standard input. */
148 static bool have_read_stdin;
150 static struct option const longopts[] =
152 {"header-numbering", required_argument, nullptr, 'h'},
153 {"body-numbering", required_argument, nullptr, 'b'},
154 {"footer-numbering", required_argument, nullptr, 'f'},
155 {"starting-line-number", required_argument, nullptr, 'v'},
156 {"line-increment", required_argument, nullptr, 'i'},
157 {"no-renumber", no_argument, nullptr, 'p'},
158 {"join-blank-lines", required_argument, nullptr, 'l'},
159 {"number-separator", required_argument, nullptr, 's'},
160 {"number-width", required_argument, nullptr, 'w'},
161 {"number-format", required_argument, nullptr, 'n'},
162 {"section-delimiter", required_argument, nullptr, 'd'},
163 {GETOPT_HELP_OPTION_DECL},
164 {GETOPT_VERSION_OPTION_DECL},
165 {nullptr, 0, nullptr, 0}
168 /* Print a usage message and quit. */
170 void
171 usage (int status)
173 if (status != EXIT_SUCCESS)
174 emit_try_help ();
175 else
177 printf (_("\
178 Usage: %s [OPTION]... [FILE]...\n\
180 program_name);
181 fputs (_("\
182 Write each FILE to standard output, with line numbers added.\n\
183 "), stdout);
185 emit_stdin_note ();
186 emit_mandatory_arg_note ();
188 fputs (_("\
189 -b, --body-numbering=STYLE use STYLE for numbering body lines\n\
190 -d, --section-delimiter=CC use CC for logical page delimiters\n\
191 -f, --footer-numbering=STYLE use STYLE for numbering footer lines\n\
192 "), stdout);
193 fputs (_("\
194 -h, --header-numbering=STYLE use STYLE for numbering header lines\n\
195 -i, --line-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 for each section\n\
199 -s, --number-separator=STRING add STRING after (possible) line number\n\
200 "), stdout);
201 fputs (_("\
202 -v, --starting-line-number=NUMBER first line number for each section\n\
203 -w, --number-width=NUMBER use NUMBER columns for line numbers\n\
204 "), stdout);
205 fputs (HELP_OPTION_DESCRIPTION, stdout);
206 fputs (VERSION_OPTION_DESCRIPTION, stdout);
207 fputs (_("\
209 Default options are: -bt -d'\\:' -fn -hn -i1 -l1 -n'rn' -s<TAB> -v1 -w6\n\
211 CC are two delimiter characters used to construct logical page delimiters;\n\
212 a missing second character implies ':'. As a GNU extension one can specify\n\
213 more than two characters, and also specifying the empty string (-d '')\n\
214 disables section matching.\n\
215 "), stdout);
216 fputs (_("\
218 STYLE is one of:\n\
220 a number all lines\n\
221 t number only nonempty lines\n\
222 n number no lines\n\
223 pBRE number only lines that contain a match for the basic regular\n\
224 expression, BRE\n\
225 "), stdout);
226 fputs (_("\
228 FORMAT is one of:\n\
230 ln left justified, no leading zeros\n\
231 rn right justified, no leading zeros\n\
232 rz right justified, leading zeros\n\
234 "), stdout);
235 emit_ancillary_info (PROGRAM_NAME);
237 exit (status);
240 /* Set the command line flag TYPEP and possibly the regex pointer REGEXP,
241 according to 'optarg'. */
243 static bool
244 build_type_arg (char const **typep,
245 struct re_pattern_buffer *regexp, char *fastmap)
247 char const *errmsg;
248 bool rval = true;
250 switch (*optarg)
252 case 'a':
253 case 't':
254 case 'n':
255 *typep = optarg;
256 break;
257 case 'p':
258 *typep = optarg++;
259 regexp->buffer = nullptr;
260 regexp->allocated = 0;
261 regexp->fastmap = fastmap;
262 regexp->translate = nullptr;
263 re_syntax_options =
264 RE_SYNTAX_POSIX_BASIC & ~RE_CONTEXT_INVALID_DUP & ~RE_NO_EMPTY_RANGES;
265 errmsg = re_compile_pattern (optarg, strlen (optarg), regexp);
266 if (errmsg)
267 error (EXIT_FAILURE, 0, "%s", (errmsg));
268 break;
269 default:
270 rval = false;
271 break;
273 return rval;
276 /* Print the line number and separator; increment the line number. */
278 static void
279 print_lineno (void)
281 if (line_no_overflow)
282 error (EXIT_FAILURE, 0, _("line number overflow"));
284 printf (lineno_format, lineno_width, line_no, separator_str);
286 if (ckd_add (&line_no, line_no, page_incr))
287 line_no_overflow = true;
290 static void
291 reset_lineno (void)
293 if (reset_numbers)
295 line_no = starting_line_number;
296 line_no_overflow = false;
300 /* Switch to a header section. */
302 static void
303 proc_header (void)
305 current_type = header_type;
306 current_regex = &header_regex;
307 reset_lineno ();
308 putchar ('\n');
311 /* Switch to a body section. */
313 static void
314 proc_body (void)
316 current_type = body_type;
317 current_regex = &body_regex;
318 reset_lineno ();
319 putchar ('\n');
322 /* Switch to a footer section. */
324 static void
325 proc_footer (void)
327 current_type = footer_type;
328 current_regex = &footer_regex;
329 reset_lineno ();
330 putchar ('\n');
333 /* Process a regular text line in 'line_buf'. */
335 static void
336 proc_text (void)
338 static intmax_t blank_lines = 0; /* Consecutive blank lines so far. */
340 switch (*current_type)
342 case 'a':
343 if (blank_join > 1)
345 if (1 < line_buf.length || ++blank_lines == blank_join)
347 print_lineno ();
348 blank_lines = 0;
350 else
351 fputs (print_no_line_fmt, stdout);
353 else
354 print_lineno ();
355 break;
356 case 't':
357 if (1 < line_buf.length)
358 print_lineno ();
359 else
360 fputs (print_no_line_fmt, stdout);
361 break;
362 case 'n':
363 fputs (print_no_line_fmt, stdout);
364 break;
365 case 'p':
366 switch (re_search (current_regex, line_buf.buffer, line_buf.length - 1,
367 0, line_buf.length - 1, nullptr))
369 case -2:
370 error (EXIT_FAILURE, errno, _("error in regular expression search"));
372 case -1:
373 fputs (print_no_line_fmt, stdout);
374 break;
376 default:
377 print_lineno ();
378 break;
381 fwrite (line_buf.buffer, sizeof (char), line_buf.length, stdout);
384 /* Return the type of line in 'line_buf'. */
386 static enum section
387 check_section (void)
389 size_t len = line_buf.length - 1;
391 if (len < 2 || footer_del_len < 2
392 || memcmp (line_buf.buffer, section_del, 2))
393 return Text;
394 if (len == header_del_len
395 && !memcmp (line_buf.buffer, header_del, header_del_len))
396 return Header;
397 if (len == body_del_len
398 && !memcmp (line_buf.buffer, body_del, body_del_len))
399 return Body;
400 if (len == footer_del_len
401 && !memcmp (line_buf.buffer, footer_del, footer_del_len))
402 return Footer;
403 return Text;
406 /* Read and process the file pointed to by FP. */
408 static void
409 process_file (FILE *fp)
411 while (readlinebuffer (&line_buf, fp))
413 switch (check_section ())
415 case Header:
416 proc_header ();
417 break;
418 case Body:
419 proc_body ();
420 break;
421 case Footer:
422 proc_footer ();
423 break;
424 case Text:
425 proc_text ();
426 break;
431 /* Process file FILE to standard output.
432 Return true if successful. */
434 static bool
435 nl_file (char const *file)
437 FILE *stream;
439 if (STREQ (file, "-"))
441 have_read_stdin = true;
442 stream = stdin;
443 assume (stream); /* Pacify GCC bug#109613. */
445 else
447 stream = fopen (file, "r");
448 if (stream == nullptr)
450 error (0, errno, "%s", quotef (file));
451 return false;
455 fadvise (stream, FADVISE_SEQUENTIAL);
457 process_file (stream);
459 int err = errno;
460 if (!ferror (stream))
461 err = 0;
462 if (STREQ (file, "-"))
463 clearerr (stream); /* Also clear EOF. */
464 else if (fclose (stream) != 0 && !err)
465 err = errno;
466 if (err)
468 error (0, err, "%s", quotef (file));
469 return false;
471 return true;
475 main (int argc, char **argv)
477 int c;
478 size_t len;
479 bool ok = true;
481 initialize_main (&argc, &argv);
482 set_program_name (argv[0]);
483 setlocale (LC_ALL, "");
484 bindtextdomain (PACKAGE, LOCALEDIR);
485 textdomain (PACKAGE);
487 atexit (close_stdout);
489 have_read_stdin = false;
491 while ((c = getopt_long (argc, argv, "h:b:f:v:i:pl:s:w:n:d:", longopts,
492 nullptr))
493 != -1)
495 switch (c)
497 case 'h':
498 if (! build_type_arg (&header_type, &header_regex, header_fastmap))
500 error (0, 0, _("invalid header numbering style: %s"),
501 quote (optarg));
502 ok = false;
504 break;
505 case 'b':
506 if (! build_type_arg (&body_type, &body_regex, body_fastmap))
508 error (0, 0, _("invalid body numbering style: %s"),
509 quote (optarg));
510 ok = false;
512 break;
513 case 'f':
514 if (! build_type_arg (&footer_type, &footer_regex, footer_fastmap))
516 error (0, 0, _("invalid footer numbering style: %s"),
517 quote (optarg));
518 ok = false;
520 break;
521 case 'v':
522 starting_line_number = xdectoimax (optarg, INTMAX_MIN, INTMAX_MAX, "",
523 _("invalid starting line number"),
525 break;
526 case 'i':
527 page_incr = xdectoimax (optarg, INTMAX_MIN, INTMAX_MAX, "",
528 _("invalid line number increment"), 0);
529 break;
530 case 'p':
531 reset_numbers = false;
532 break;
533 case 'l':
534 blank_join = xdectoimax (optarg, 1, INTMAX_MAX, "",
535 _("invalid line number of blank lines"), 0);
536 break;
537 case 's':
538 separator_str = optarg;
539 break;
540 case 'w':
541 lineno_width = xdectoimax (optarg, 1, INT_MAX, "",
542 _("invalid line number field width"), 0);
543 break;
544 case 'n':
545 if (STREQ (optarg, "ln"))
546 lineno_format = FORMAT_LEFT;
547 else if (STREQ (optarg, "rn"))
548 lineno_format = FORMAT_RIGHT_NOLZ;
549 else if (STREQ (optarg, "rz"))
550 lineno_format = FORMAT_RIGHT_LZ;
551 else
553 error (0, 0, _("invalid line numbering format: %s"),
554 quote (optarg));
555 ok = false;
557 break;
558 case 'd':
559 len = strlen (optarg);
560 if (len == 1 || len == 2) /* POSIX. */
562 char *p = section_del;
563 while (*optarg)
564 *p++ = *optarg++;
566 else
567 section_del = optarg; /* GNU extension. */
568 break;
569 case_GETOPT_HELP_CHAR;
570 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
571 default:
572 ok = false;
573 break;
577 if (!ok)
578 usage (EXIT_FAILURE);
580 /* Initialize the section delimiters. */
581 len = strlen (section_del);
583 header_del_len = len * 3;
584 header_del = xmalloc (header_del_len + 1);
585 stpcpy (stpcpy (stpcpy (header_del, section_del), section_del), section_del);
587 body_del_len = len * 2;
588 body_del = header_del + len;
590 footer_del_len = len;
591 footer_del = body_del + len;
593 /* Initialize the input buffer. */
594 initbuffer (&line_buf);
596 /* Initialize the printf format for unnumbered lines. */
597 len = strlen (separator_str);
598 print_no_line_fmt = xmalloc (lineno_width + len + 1);
599 memset (print_no_line_fmt, ' ', lineno_width + len);
600 print_no_line_fmt[lineno_width + len] = '\0';
602 line_no = starting_line_number;
603 current_type = body_type;
604 current_regex = &body_regex;
606 /* Main processing. */
608 if (optind == argc)
609 ok = nl_file ("-");
610 else
611 for (; optind < argc; optind++)
612 ok &= nl_file (argv[optind]);
614 if (have_read_stdin && fclose (stdin) == EOF)
615 error (EXIT_FAILURE, errno, "-");
617 return ok ? EXIT_SUCCESS : EXIT_FAILURE;