shred: increase I/O block size for periodic pattern case
[coreutils.git] / src / nl.c
blob96716aa1060df032173343ca39c28ce4f5f5de09
1 /* nl -- number lines of files
2 Copyright (C) 1989-2013 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 <http://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 "error.h"
31 #include "fadvise.h"
32 #include "linebuffer.h"
33 #include "quote.h"
34 #include "xstrtol.h"
36 /* The official name of this program (e.g., no 'g' prefix). */
37 #define PROGRAM_NAME "nl"
39 #define AUTHORS \
40 proper_name ("Scott Bartram"), \
41 proper_name ("David MacKenzie")
43 /* Line-number formats. They are given an int width, an intmax_t
44 value, and a string separator. */
46 /* Right justified, no leading zeroes. */
47 static char const FORMAT_RIGHT_NOLZ[] = "%*" PRIdMAX "%s";
49 /* Right justified, leading zeroes. */
50 static char const FORMAT_RIGHT_LZ[] = "%0*" PRIdMAX "%s";
52 /* Left justified, no leading zeroes. */
53 static char const FORMAT_LEFT[] = "%-*" PRIdMAX "%s";
55 /* Default section delimiter characters. */
56 static char const DEFAULT_SECTION_DELIMITERS[] = "\\:";
58 /* Types of input lines: either one of the section delimiters,
59 or text to output. */
60 enum section
62 Header, Body, Footer, Text
65 /* Format of body lines (-b). */
66 static char const *body_type = "t";
68 /* Format of header lines (-h). */
69 static char const *header_type = "n";
71 /* Format of footer lines (-f). */
72 static char const *footer_type = "n";
74 /* Format currently being used (body, header, or footer). */
75 static char const *current_type;
77 /* Regex for body lines to number (-bp). */
78 static struct re_pattern_buffer body_regex;
80 /* Regex for header lines to number (-hp). */
81 static struct re_pattern_buffer header_regex;
83 /* Regex for footer lines to number (-fp). */
84 static struct re_pattern_buffer footer_regex;
86 /* Fastmaps for the above. */
87 static char body_fastmap[UCHAR_MAX + 1];
88 static char header_fastmap[UCHAR_MAX + 1];
89 static char footer_fastmap[UCHAR_MAX + 1];
91 /* Pointer to current regex, if any. */
92 static struct re_pattern_buffer *current_regex = NULL;
94 /* Separator string to print after line number (-s). */
95 static char const *separator_str = "\t";
97 /* Input section delimiter string (-d). */
98 static char const *section_del = DEFAULT_SECTION_DELIMITERS;
100 /* Header delimiter string. */
101 static char *header_del = NULL;
103 /* Header section delimiter length. */
104 static size_t header_del_len;
106 /* Body delimiter string. */
107 static char *body_del = NULL;
109 /* Body section delimiter length. */
110 static size_t body_del_len;
112 /* Footer delimiter string. */
113 static char *footer_del = NULL;
115 /* Footer section delimiter length. */
116 static size_t footer_del_len;
118 /* Input buffer. */
119 static struct linebuffer line_buf;
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 intmax_t starting_line_number = 1;
127 /* Line number increment (-i). */
128 static intmax_t page_incr = 1;
130 /* If true, reset line number at start of each page (-p). */
131 static bool reset_numbers = true;
133 /* Number of blank lines to consider to be one line for numbering (-l). */
134 static intmax_t blank_join = 1;
136 /* Width of line numbers (-w). */
137 static int lineno_width = 6;
139 /* Line number format (-n). */
140 static char const *lineno_format = FORMAT_RIGHT_NOLZ;
142 /* Current print line number. */
143 static intmax_t line_no;
145 /* True if we have ever read standard input. */
146 static bool 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 {"line-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},
163 {NULL, 0, NULL, 0}
166 /* Print a usage message and quit. */
168 void
169 usage (int status)
171 if (status != EXIT_SUCCESS)
172 emit_try_help ();
173 else
175 printf (_("\
176 Usage: %s [OPTION]... [FILE]...\n\
178 program_name);
179 fputs (_("\
180 Write each FILE to standard output, with line numbers added.\n\
181 With no FILE, or when FILE is -, read standard input.\n\
182 "), stdout);
184 emit_mandatory_arg_note ();
186 fputs (_("\
187 -b, --body-numbering=STYLE use STYLE for numbering body lines\n\
188 -d, --section-delimiter=CC use CC for separating logical pages\n\
189 -f, --footer-numbering=STYLE use STYLE for numbering footer lines\n\
190 "), stdout);
191 fputs (_("\
192 -h, --header-numbering=STYLE use STYLE for numbering header lines\n\
193 -i, --line-increment=NUMBER line number increment at each line\n\
194 -l, --join-blank-lines=NUMBER group of NUMBER empty lines counted as one\n\
195 -n, --number-format=FORMAT insert line numbers according to FORMAT\n\
196 -p, --no-renumber do not reset line numbers at logical pages\n\
197 -s, --number-separator=STRING add STRING after (possible) line number\n\
198 "), stdout);
199 fputs (_("\
200 -v, --starting-line-number=NUMBER first line number on each logical page\n\
201 -w, --number-width=NUMBER use NUMBER columns for line numbers\n\
202 "), stdout);
203 fputs (HELP_OPTION_DESCRIPTION, stdout);
204 fputs (VERSION_OPTION_DESCRIPTION, stdout);
205 fputs (_("\
207 By default, selects -v1 -i1 -l1 -sTAB -w6 -nrn -hn -bt -fn. CC are\n\
208 two delimiter characters for separating logical pages, a missing\n\
209 second character implies :. Type \\\\ for \\. STYLE is one of:\n\
210 "), stdout);
211 fputs (_("\
213 a number all lines\n\
214 t number only nonempty lines\n\
215 n number no lines\n\
216 pBRE number only lines that contain a match for the basic regular\n\
217 expression, BRE\n\
219 FORMAT is one of:\n\
221 ln left justified, no leading zeros\n\
222 rn right justified, no leading zeros\n\
223 rz right justified, leading zeros\n\
225 "), stdout);
226 emit_ancillary_info ();
228 exit (status);
231 /* Set the command line flag TYPEP and possibly the regex pointer REGEXP,
232 according to 'optarg'. */
234 static bool
235 build_type_arg (char const **typep,
236 struct re_pattern_buffer *regexp, char *fastmap)
238 char const *errmsg;
239 bool rval = true;
241 switch (*optarg)
243 case 'a':
244 case 't':
245 case 'n':
246 *typep = optarg;
247 break;
248 case 'p':
249 *typep = optarg++;
250 regexp->buffer = NULL;
251 regexp->allocated = 0;
252 regexp->fastmap = fastmap;
253 regexp->translate = NULL;
254 re_syntax_options =
255 RE_SYNTAX_POSIX_BASIC & ~RE_CONTEXT_INVALID_DUP & ~RE_NO_EMPTY_RANGES;
256 errmsg = re_compile_pattern (optarg, strlen (optarg), regexp);
257 if (errmsg)
258 error (EXIT_FAILURE, 0, "%s", errmsg);
259 break;
260 default:
261 rval = false;
262 break;
264 return rval;
267 /* Print the line number and separator; increment the line number. */
269 static void
270 print_lineno (void)
272 intmax_t next_line_no;
274 printf (lineno_format, lineno_width, line_no, separator_str);
276 next_line_no = line_no + page_incr;
277 if (next_line_no < line_no)
278 error (EXIT_FAILURE, 0, _("line number overflow"));
279 line_no = next_line_no;
282 /* Switch to a header section. */
284 static void
285 proc_header (void)
287 current_type = header_type;
288 current_regex = &header_regex;
289 if (reset_numbers)
290 line_no = starting_line_number;
291 putchar ('\n');
294 /* Switch to a body section. */
296 static void
297 proc_body (void)
299 current_type = body_type;
300 current_regex = &body_regex;
301 putchar ('\n');
304 /* Switch to a footer section. */
306 static void
307 proc_footer (void)
309 current_type = footer_type;
310 current_regex = &footer_regex;
311 putchar ('\n');
314 /* Process a regular text line in 'line_buf'. */
316 static void
317 proc_text (void)
319 static intmax_t blank_lines = 0; /* Consecutive blank lines so far. */
321 switch (*current_type)
323 case 'a':
324 if (blank_join > 1)
326 if (1 < line_buf.length || ++blank_lines == blank_join)
328 print_lineno ();
329 blank_lines = 0;
331 else
332 fputs (print_no_line_fmt, stdout);
334 else
335 print_lineno ();
336 break;
337 case 't':
338 if (1 < line_buf.length)
339 print_lineno ();
340 else
341 fputs (print_no_line_fmt, stdout);
342 break;
343 case 'n':
344 fputs (print_no_line_fmt, stdout);
345 break;
346 case 'p':
347 switch (re_search (current_regex, line_buf.buffer, line_buf.length - 1,
348 0, line_buf.length - 1, NULL))
350 case -2:
351 error (EXIT_FAILURE, errno, _("error in regular expression search"));
353 case -1:
354 fputs (print_no_line_fmt, stdout);
355 break;
357 default:
358 print_lineno ();
359 break;
362 fwrite (line_buf.buffer, sizeof (char), line_buf.length, stdout);
365 /* Return the type of line in 'line_buf'. */
367 static enum section
368 check_section (void)
370 size_t len = line_buf.length - 1;
372 if (len < 2 || memcmp (line_buf.buffer, section_del, 2))
373 return Text;
374 if (len == header_del_len
375 && !memcmp (line_buf.buffer, header_del, header_del_len))
376 return Header;
377 if (len == body_del_len
378 && !memcmp (line_buf.buffer, body_del, body_del_len))
379 return Body;
380 if (len == footer_del_len
381 && !memcmp (line_buf.buffer, footer_del, footer_del_len))
382 return Footer;
383 return Text;
386 /* Read and process the file pointed to by FP. */
388 static void
389 process_file (FILE *fp)
391 while (readlinebuffer (&line_buf, fp))
393 switch (check_section ())
395 case Header:
396 proc_header ();
397 break;
398 case Body:
399 proc_body ();
400 break;
401 case Footer:
402 proc_footer ();
403 break;
404 case Text:
405 proc_text ();
406 break;
411 /* Process file FILE to standard output.
412 Return true if successful. */
414 static bool
415 nl_file (char const *file)
417 FILE *stream;
419 if (STREQ (file, "-"))
421 have_read_stdin = true;
422 stream = stdin;
424 else
426 stream = fopen (file, "r");
427 if (stream == NULL)
429 error (0, errno, "%s", file);
430 return false;
434 fadvise (stream, FADVISE_SEQUENTIAL);
436 process_file (stream);
438 if (ferror (stream))
440 error (0, errno, "%s", file);
441 return false;
443 if (STREQ (file, "-"))
444 clearerr (stream); /* Also clear EOF. */
445 else if (fclose (stream) == EOF)
447 error (0, errno, "%s", file);
448 return false;
450 return true;
454 main (int argc, char **argv)
456 int c;
457 size_t len;
458 bool ok = true;
460 initialize_main (&argc, &argv);
461 set_program_name (argv[0]);
462 setlocale (LC_ALL, "");
463 bindtextdomain (PACKAGE, LOCALEDIR);
464 textdomain (PACKAGE);
466 atexit (close_stdout);
468 have_read_stdin = false;
470 while ((c = getopt_long (argc, argv, "h:b:f:v:i:pl:s:w:n:d:", longopts,
471 NULL)) != -1)
473 switch (c)
475 case 'h':
476 if (! build_type_arg (&header_type, &header_regex, header_fastmap))
478 error (0, 0, _("invalid header numbering style: %s"),
479 quote (optarg));
480 ok = false;
482 break;
483 case 'b':
484 if (! build_type_arg (&body_type, &body_regex, body_fastmap))
486 error (0, 0, _("invalid body numbering style: %s"),
487 quote (optarg));
488 ok = false;
490 break;
491 case 'f':
492 if (! build_type_arg (&footer_type, &footer_regex, footer_fastmap))
494 error (0, 0, _("invalid footer numbering style: %s"),
495 quote (optarg));
496 ok = false;
498 break;
499 case 'v':
500 if (xstrtoimax (optarg, NULL, 10, &starting_line_number, "")
501 != LONGINT_OK)
503 error (0, 0, _("invalid starting line number: %s"),
504 quote (optarg));
505 ok = false;
507 break;
508 case 'i':
509 if (! (xstrtoimax (optarg, NULL, 10, &page_incr, "") == LONGINT_OK
510 && 0 < page_incr))
512 error (0, 0, _("invalid line number increment: %s"),
513 quote (optarg));
514 ok = false;
516 break;
517 case 'p':
518 reset_numbers = false;
519 break;
520 case 'l':
521 if (! (xstrtoimax (optarg, NULL, 10, &blank_join, "") == LONGINT_OK
522 && 0 < blank_join))
524 error (0, 0, _("invalid number of blank lines: %s"),
525 quote (optarg));
526 ok = false;
528 break;
529 case 's':
530 separator_str = optarg;
531 break;
532 case 'w':
534 long int tmp_long;
535 if (xstrtol (optarg, NULL, 10, &tmp_long, "") != LONGINT_OK
536 || tmp_long <= 0 || tmp_long > INT_MAX)
538 error (0, 0, _("invalid line number field width: %s"),
539 quote (optarg));
540 ok = false;
542 else
544 lineno_width = tmp_long;
547 break;
548 case 'n':
549 if (STREQ (optarg, "ln"))
550 lineno_format = FORMAT_LEFT;
551 else if (STREQ (optarg, "rn"))
552 lineno_format = FORMAT_RIGHT_NOLZ;
553 else if (STREQ (optarg, "rz"))
554 lineno_format = FORMAT_RIGHT_LZ;
555 else
557 error (0, 0, _("invalid line numbering format: %s"),
558 quote (optarg));
559 ok = false;
561 break;
562 case 'd':
563 section_del = optarg;
564 break;
565 case_GETOPT_HELP_CHAR;
566 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
567 default:
568 ok = false;
569 break;
573 if (!ok)
574 usage (EXIT_FAILURE);
576 /* Initialize the section delimiters. */
577 len = strlen (section_del);
579 header_del_len = len * 3;
580 header_del = xmalloc (header_del_len + 1);
581 stpcpy (stpcpy (stpcpy (header_del, section_del), section_del), section_del);
583 body_del_len = len * 2;
584 body_del = xmalloc (body_del_len + 1);
585 stpcpy (stpcpy (body_del, section_del), section_del);
587 footer_del_len = len;
588 footer_del = xmalloc (footer_del_len + 1);
589 stpcpy (footer_del, section_del);
591 /* Initialize the input buffer. */
592 initbuffer (&line_buf);
594 /* Initialize the printf format for unnumbered lines. */
595 len = strlen (separator_str);
596 print_no_line_fmt = xmalloc (lineno_width + len + 1);
597 memset (print_no_line_fmt, ' ', lineno_width + len);
598 print_no_line_fmt[lineno_width + len] = '\0';
600 line_no = starting_line_number;
601 current_type = body_type;
602 current_regex = &body_regex;
604 /* Main processing. */
606 if (optind == argc)
607 ok = nl_file ("-");
608 else
609 for (; optind < argc; optind++)
610 ok &= nl_file (argv[optind]);
612 if (have_read_stdin && fclose (stdin) == EOF)
613 error (EXIT_FAILURE, errno, "-");
615 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);