TODO: add an item for a chmod optimization
[platform/upstream/coreutils.git] / src / nl.c
1 /* nl -- number lines of files
2    Copyright (C) 89, 92, 1995-2008 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16 \f
17 /* Written by Scott Bartram (nancy!scott@uunet.uu.net)
18    Revised by David MacKenzie (djm@gnu.ai.mit.edu) */
19
20 #include <config.h>
21
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <getopt.h>
25
26 #include "system.h"
27
28 #include <regex.h>
29
30 #include "error.h"
31 #include "linebuffer.h"
32 #include "quote.h"
33 #include "xstrtol.h"
34
35 /* The official name of this program (e.g., no `g' prefix).  */
36 #define PROGRAM_NAME "nl"
37
38 #define AUTHORS \
39   proper_name ("Scott Bartram"), \
40   proper_name ("David MacKenzie")
41
42 /* Line-number formats.  They are given an int width, an intmax_t
43    value, and a string separator.  */
44
45 /* Right justified, no leading zeroes.  */
46 static char const FORMAT_RIGHT_NOLZ[] = "%*" PRIdMAX "%s";
47
48 /* Right justified, leading zeroes.  */
49 static char const FORMAT_RIGHT_LZ[] = "%0*" PRIdMAX "%s";
50
51 /* Left justified, no leading zeroes.  */
52 static char const FORMAT_LEFT[] = "%-*" PRIdMAX "%s";
53
54 /* Default section delimiter characters.  */
55 static char const DEFAULT_SECTION_DELIMITERS[] = "\\:";
56
57 /* Types of input lines: either one of the section delimiters,
58    or text to output. */
59 enum section
60 {
61   Header, Body, Footer, Text
62 };
63
64 /* Format of body lines (-b).  */
65 static char const *body_type = "t";
66
67 /* Format of header lines (-h).  */
68 static char const *header_type = "n";
69
70 /* Format of footer lines (-f).  */
71 static char const *footer_type = "n";
72
73 /* Format currently being used (body, header, or footer).  */
74 static char const *current_type;
75
76 /* Regex for body lines to number (-bp).  */
77 static struct re_pattern_buffer body_regex;
78
79 /* Regex for header lines to number (-hp).  */
80 static struct re_pattern_buffer header_regex;
81
82 /* Regex for footer lines to number (-fp).  */
83 static struct re_pattern_buffer footer_regex;
84
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];
89
90 /* Pointer to current regex, if any.  */
91 static struct re_pattern_buffer *current_regex = NULL;
92
93 /* Separator string to print after line number (-s).  */
94 static char const *separator_str = "\t";
95
96 /* Input section delimiter string (-d).  */
97 static char const *section_del = DEFAULT_SECTION_DELIMITERS;
98
99 /* Header delimiter string.  */
100 static char *header_del = NULL;
101
102 /* Header section delimiter length.  */
103 static size_t header_del_len;
104
105 /* Body delimiter string.  */
106 static char *body_del = NULL;
107
108 /* Body section delimiter length.  */
109 static size_t body_del_len;
110
111 /* Footer delimiter string.  */
112 static char *footer_del = NULL;
113
114 /* Footer section delimiter length.  */
115 static size_t footer_del_len;
116
117 /* Input buffer.  */
118 static struct linebuffer line_buf;
119
120 /* printf format string for unnumbered lines.  */
121 static char *print_no_line_fmt = NULL;
122
123 /* Starting line number on each page (-v).  */
124 static intmax_t starting_line_number = 1;
125
126 /* Line number increment (-i).  */
127 static intmax_t page_incr = 1;
128
129 /* If true, reset line number at start of each page (-p).  */
130 static bool reset_numbers = true;
131
132 /* Number of blank lines to consider to be one line for numbering (-l).  */
133 static intmax_t blank_join = 1;
134
135 /* Width of line numbers (-w).  */
136 static int lineno_width = 6;
137
138 /* Line number format (-n).  */
139 static char const *lineno_format = FORMAT_RIGHT_NOLZ;
140
141 /* Current print line number.  */
142 static intmax_t line_no;
143
144 /* True if we have ever read standard input.  */
145 static bool have_read_stdin;
146
147 static struct option const longopts[] =
148 {
149   {"header-numbering", required_argument, NULL, 'h'},
150   {"body-numbering", required_argument, NULL, 'b'},
151   {"footer-numbering", required_argument, NULL, 'f'},
152   {"starting-line-number", required_argument, NULL, 'v'},
153   {"page-increment", required_argument, NULL, 'i'},
154   {"no-renumber", no_argument, NULL, 'p'},
155   {"join-blank-lines", required_argument, NULL, 'l'},
156   {"number-separator", required_argument, NULL, 's'},
157   {"number-width", required_argument, NULL, 'w'},
158   {"number-format", required_argument, NULL, 'n'},
159   {"section-delimiter", required_argument, NULL, 'd'},
160   {GETOPT_HELP_OPTION_DECL},
161   {GETOPT_VERSION_OPTION_DECL},
162   {NULL, 0, NULL, 0}
163 };
164
165 /* Print a usage message and quit. */
166
167 void
168 usage (int status)
169 {
170   if (status != EXIT_SUCCESS)
171     fprintf (stderr, _("Try `%s --help' for more information.\n"),
172              program_name);
173   else
174     {
175       printf (_("\
176 Usage: %s [OPTION]... [FILE]...\n\
177 "),
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 \n\
183 "), stdout);
184       fputs (_("\
185 Mandatory arguments to long options are mandatory for short options too.\n\
186 "), stdout);
187       fputs (_("\
188   -b, --body-numbering=STYLE      use STYLE for numbering body lines\n\
189   -d, --section-delimiter=CC      use CC for separating logical pages\n\
190   -f, --footer-numbering=STYLE    use STYLE for numbering footer lines\n\
191 "), stdout);
192       fputs (_("\
193   -h, --header-numbering=STYLE    use STYLE for numbering header lines\n\
194   -i, --page-increment=NUMBER     line number increment at each line\n\
195   -l, --join-blank-lines=NUMBER   group of NUMBER empty lines counted as one\n\
196   -n, --number-format=FORMAT      insert line numbers according to FORMAT\n\
197   -p, --no-renumber               do not reset line numbers at logical pages\n\
198   -s, --number-separator=STRING   add STRING after (possible) line number\n\
199 "), stdout);
200       fputs (_("\
201   -v, --first-page=NUMBER         first line number on each logical page\n\
202   -w, --number-width=NUMBER       use NUMBER columns for line numbers\n\
203 "), stdout);
204       fputs (HELP_OPTION_DESCRIPTION, stdout);
205       fputs (VERSION_OPTION_DESCRIPTION, stdout);
206       fputs (_("\
207 \n\
208 By default, selects -v1 -i1 -l1 -sTAB -w6 -nrn -hn -bt -fn.  CC are\n\
209 two delimiter characters for separating logical pages, a missing\n\
210 second character implies :.  Type \\\\ for \\.  STYLE is one of:\n\
211 "), stdout);
212       fputs (_("\
213 \n\
214   a         number all lines\n\
215   t         number only nonempty lines\n\
216   n         number no lines\n\
217   pBRE      number only lines that contain a match for the basic regular\n\
218             expression, BRE\n\
219 \n\
220 FORMAT is one of:\n\
221 \n\
222   ln   left justified, no leading zeros\n\
223   rn   right justified, no leading zeros\n\
224   rz   right justified, leading zeros\n\
225 \n\
226 "), stdout);
227       emit_bug_reporting_address ();
228     }
229   exit (status);
230 }
231
232 /* Set the command line flag TYPEP and possibly the regex pointer REGEXP,
233    according to `optarg'.  */
234
235 static bool
236 build_type_arg (char const **typep,
237                 struct re_pattern_buffer *regexp, char *fastmap)
238 {
239   char const *errmsg;
240   bool rval = true;
241
242   switch (*optarg)
243     {
244     case 'a':
245     case 't':
246     case 'n':
247       *typep = optarg;
248       break;
249     case 'p':
250       *typep = optarg++;
251       regexp->buffer = NULL;
252       regexp->allocated = 0;
253       regexp->fastmap = fastmap;
254       regexp->translate = NULL;
255       re_syntax_options =
256         RE_SYNTAX_POSIX_BASIC & ~RE_CONTEXT_INVALID_DUP & ~RE_NO_EMPTY_RANGES;
257       errmsg = re_compile_pattern (optarg, strlen (optarg), regexp);
258       if (errmsg)
259         error (EXIT_FAILURE, 0, "%s", errmsg);
260       break;
261     default:
262       rval = false;
263       break;
264     }
265   return rval;
266 }
267
268 /* Print the line number and separator; increment the line number. */
269
270 static void
271 print_lineno (void)
272 {
273   intmax_t next_line_no;
274
275   printf (lineno_format, lineno_width, line_no, separator_str);
276
277   next_line_no = line_no + page_incr;
278   if (next_line_no < line_no)
279     error (EXIT_FAILURE, 0, _("line number overflow"));
280   line_no = next_line_no;
281 }
282
283 /* Switch to a header section. */
284
285 static void
286 proc_header (void)
287 {
288   current_type = header_type;
289   current_regex = &header_regex;
290   if (reset_numbers)
291     line_no = starting_line_number;
292   putchar ('\n');
293 }
294
295 /* Switch to a body section. */
296
297 static void
298 proc_body (void)
299 {
300   current_type = body_type;
301   current_regex = &body_regex;
302   putchar ('\n');
303 }
304
305 /* Switch to a footer section. */
306
307 static void
308 proc_footer (void)
309 {
310   current_type = footer_type;
311   current_regex = &footer_regex;
312   putchar ('\n');
313 }
314
315 /* Process a regular text line in `line_buf'. */
316
317 static void
318 proc_text (void)
319 {
320   static intmax_t blank_lines = 0;      /* Consecutive blank lines so far. */
321
322   switch (*current_type)
323     {
324     case 'a':
325       if (blank_join > 1)
326         {
327           if (1 < line_buf.length || ++blank_lines == blank_join)
328             {
329               print_lineno ();
330               blank_lines = 0;
331             }
332           else
333             fputs (print_no_line_fmt, stdout);
334         }
335       else
336         print_lineno ();
337       break;
338     case 't':
339       if (1 < line_buf.length)
340         print_lineno ();
341       else
342         fputs (print_no_line_fmt, stdout);
343       break;
344     case 'n':
345       fputs (print_no_line_fmt, stdout);
346       break;
347     case 'p':
348       switch (re_search (current_regex, line_buf.buffer, line_buf.length - 1,
349                          0, line_buf.length - 1, NULL))
350         {
351         case -2:
352           error (EXIT_FAILURE, errno, _("error in regular expression search"));
353
354         case -1:
355           fputs (print_no_line_fmt, stdout);
356           break;
357
358         default:
359           print_lineno ();
360           break;
361         }
362     }
363   fwrite (line_buf.buffer, sizeof (char), line_buf.length, stdout);
364 }
365
366 /* Return the type of line in `line_buf'. */
367
368 static enum section
369 check_section (void)
370 {
371   size_t len = line_buf.length - 1;
372
373   if (len < 2 || memcmp (line_buf.buffer, section_del, 2))
374     return Text;
375   if (len == header_del_len
376       && !memcmp (line_buf.buffer, header_del, header_del_len))
377     return Header;
378   if (len == body_del_len
379       && !memcmp (line_buf.buffer, body_del, body_del_len))
380     return Body;
381   if (len == footer_del_len
382       && !memcmp (line_buf.buffer, footer_del, footer_del_len))
383     return Footer;
384   return Text;
385 }
386
387 /* Read and process the file pointed to by FP. */
388
389 static void
390 process_file (FILE *fp)
391 {
392   while (readlinebuffer (&line_buf, fp))
393     {
394       switch (check_section ())
395         {
396         case Header:
397           proc_header ();
398           break;
399         case Body:
400           proc_body ();
401           break;
402         case Footer:
403           proc_footer ();
404           break;
405         case Text:
406           proc_text ();
407           break;
408         }
409     }
410 }
411
412 /* Process file FILE to standard output.
413    Return true if successful.  */
414
415 static bool
416 nl_file (char const *file)
417 {
418   FILE *stream;
419
420   if (STREQ (file, "-"))
421     {
422       have_read_stdin = true;
423       stream = stdin;
424     }
425   else
426     {
427       stream = fopen (file, "r");
428       if (stream == NULL)
429         {
430           error (0, errno, "%s", file);
431           return false;
432         }
433     }
434
435   process_file (stream);
436
437   if (ferror (stream))
438     {
439       error (0, errno, "%s", file);
440       return false;
441     }
442   if (STREQ (file, "-"))
443     clearerr (stream);          /* Also clear EOF. */
444   else if (fclose (stream) == EOF)
445     {
446       error (0, errno, "%s", file);
447       return false;
448     }
449   return true;
450 }
451
452 int
453 main (int argc, char **argv)
454 {
455   int c;
456   size_t len;
457   bool ok = true;
458
459   initialize_main (&argc, &argv);
460   set_program_name (argv[0]);
461   setlocale (LC_ALL, "");
462   bindtextdomain (PACKAGE, LOCALEDIR);
463   textdomain (PACKAGE);
464
465   atexit (close_stdout);
466
467   have_read_stdin = false;
468
469   while ((c = getopt_long (argc, argv, "h:b:f:v:i:pl:s:w:n:d:", longopts,
470                            NULL)) != -1)
471     {
472       switch (c)
473         {
474         case 'h':
475           if (! build_type_arg (&header_type, &header_regex, header_fastmap))
476             {
477               error (0, 0, _("invalid header numbering style: %s"),
478                      quote (optarg));
479               ok = false;
480             }
481           break;
482         case 'b':
483           if (! build_type_arg (&body_type, &body_regex, body_fastmap))
484             {
485               error (0, 0, _("invalid body numbering style: %s"),
486                      quote (optarg));
487               ok = false;
488             }
489           break;
490         case 'f':
491           if (! build_type_arg (&footer_type, &footer_regex, footer_fastmap))
492             {
493               error (0, 0, _("invalid footer numbering style: %s"),
494                      quote (optarg));
495               ok = false;
496             }
497           break;
498         case 'v':
499           if (xstrtoimax (optarg, NULL, 10, &starting_line_number, "")
500               != LONGINT_OK)
501             {
502               error (0, 0, _("invalid starting line number: %s"),
503                      quote (optarg));
504               ok = false;
505             }
506           break;
507         case 'i':
508           if (! (xstrtoimax (optarg, NULL, 10, &page_incr, "") == LONGINT_OK
509                  && 0 < page_incr))
510             {
511               error (0, 0, _("invalid line number increment: %s"),
512                      quote (optarg));
513               ok = false;
514             }
515           break;
516         case 'p':
517           reset_numbers = false;
518           break;
519         case 'l':
520           if (! (xstrtoimax (optarg, NULL, 10, &blank_join, "") == LONGINT_OK
521                  && 0 < blank_join))
522             {
523               error (0, 0, _("invalid number of blank lines: %s"),
524                      quote (optarg));
525               ok = false;
526             }
527           break;
528         case 's':
529           separator_str = optarg;
530           break;
531         case 'w':
532           {
533             long int tmp_long;
534             if (xstrtol (optarg, NULL, 10, &tmp_long, "") != LONGINT_OK
535                 || tmp_long <= 0 || tmp_long > INT_MAX)
536               {
537                 error (0, 0, _("invalid line number field width: %s"),
538                        quote (optarg));
539                 ok = false;
540               }
541             else
542               {
543                 lineno_width = tmp_long;
544               }
545           }
546           break;
547         case 'n':
548           if (STREQ (optarg, "ln"))
549             lineno_format = FORMAT_LEFT;
550           else if (STREQ (optarg, "rn"))
551             lineno_format = FORMAT_RIGHT_NOLZ;
552           else if (STREQ (optarg, "rz"))
553             lineno_format = FORMAT_RIGHT_LZ;
554           else
555             {
556               error (0, 0, _("invalid line numbering format: %s"),
557                      quote (optarg));
558               ok = false;
559             }
560           break;
561         case 'd':
562           section_del = optarg;
563           break;
564         case_GETOPT_HELP_CHAR;
565         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
566         default:
567           ok = false;
568           break;
569         }
570     }
571
572   if (!ok)
573     usage (EXIT_FAILURE);
574
575   /* Initialize the section delimiters.  */
576   len = strlen (section_del);
577
578   header_del_len = len * 3;
579   header_del = xmalloc (header_del_len + 1);
580   strcat (strcat (strcpy (header_del, section_del), section_del), section_del);
581
582   body_del_len = len * 2;
583   body_del = xmalloc (body_del_len + 1);
584   strcat (strcpy (body_del, section_del), section_del);
585
586   footer_del_len = len;
587   footer_del = xmalloc (footer_del_len + 1);
588   strcpy (footer_del, section_del);
589
590   /* Initialize the input buffer.  */
591   initbuffer (&line_buf);
592
593   /* Initialize the printf format for unnumbered lines. */
594   len = strlen (separator_str);
595   print_no_line_fmt = xmalloc (lineno_width + len + 1);
596   memset (print_no_line_fmt, ' ', lineno_width + len);
597   print_no_line_fmt[lineno_width + len] = '\0';
598
599   line_no = starting_line_number;
600   current_type = body_type;
601   current_regex = &body_regex;
602
603   /* Main processing. */
604
605   if (optind == argc)
606     ok = nl_file ("-");
607   else
608     for (; optind < argc; optind++)
609       ok &= nl_file (argv[optind]);
610
611   if (have_read_stdin && fclose (stdin) == EOF)
612     error (EXIT_FAILURE, errno, "-");
613
614   exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
615 }