char *xmalloc ();
char *xrealloc ();
-static enum section check_section ();
-static int build_type_arg ();
-static int nl_file ();
-static void usage ();
-static void process_file ();
-static void proc_header ();
-static void proc_body ();
-static void proc_footer ();
-static void proc_text ();
-static void print_lineno ();
-static void build_print_fmt ();
-
/* The name this program was run with. */
char *program_name;
{NULL, 0, NULL, 0}
};
-void
-main (argc, argv)
- int argc;
- char **argv;
-{
- int c, exit_status = 0;
-
- program_name = argv[0];
- have_read_stdin = 0;
+/* Print a usage message and quit. */
- while ((c = getopt_long (argc, argv, "h:b:f:v:i:pl:s:w:n:d:", longopts,
- (int *) 0)) != EOF)
+static void
+usage (status)
+ int status;
+{
+ if (status != 0)
+ fprintf (stderr, _("Try `%s --help' for more information.\n"),
+ program_name);
+ else
{
- switch (c)
- {
- case 0:
- break;
-
- case 'h':
- if (build_type_arg (&header_type, &header_regex) != TRUE)
- usage (2);
- break;
- case 'b':
- if (build_type_arg (&body_type, &body_regex) != TRUE)
- usage (2);
- break;
- case 'f':
- if (build_type_arg (&footer_type, &footer_regex) != TRUE)
- usage (2);
- break;
- case 'v':
- page_start = atoi (optarg);
- break;
- case 'i':
- page_incr = atoi (optarg);
- if (page_incr < 1)
- page_incr = 1;
- break;
- case 'p':
- reset_numbers = FALSE;
- break;
- case 'l':
- blank_join = atoi (optarg);
- break;
- case 's':
- separator_str = optarg;
- break;
- case 'w':
- lineno_width = atoi (optarg);
- if (lineno_width < 1)
- lineno_width = 1;
- break;
- case 'n':
- switch (*optarg)
- {
- case 'l':
- if (optarg[1] == 'n')
- lineno_format = FORMAT_LEFT;
- else
- usage (2);
- break;
- case 'r':
- switch (optarg[1])
- {
- case 'n':
- lineno_format = FORMAT_RIGHT_NOLZ;
- break;
- case 'z':
- lineno_format = FORMAT_RIGHT_LZ;
- break;
- default:
- usage (2);
- break;
- }
- break;
- default:
- usage (2);
- break;
- }
- break;
- case 'd':
- section_del = optarg;
- break;
- default:
- usage (2);
- break;
- }
+ printf (_("\
+Usage: %s [OPTION]... [FILE]...\n\
+"),
+ program_name);
+ printf (_("\
+Write each FILE to standard output, with line numbers added.\n\
+With no FILE, or when FILE is -, read standard input.\n\
+\n\
+ -b, --body-numbering=STYLE use STYLE for numbering body lines\n\
+ -d, --section-delimiter=CC use CC for separating logical pages\n\
+ -f, --footer-numbering=STYLE use STYLE for numbering footer lines\n\
+ -h, --header-numbering=STYLE use STYLE for numbering header lines\n\
+ -i, --page-increment=NUMBER line number increment at each line\n\
+ -l, --join-blank-lines=NUMBER group of NUMBER empty lines counted as one\n\
+ -n, --number-format=FORMAT insert line numbers according to FORMAT\n\
+ -p, --no-renumber do not reset line numbers at logical pages\n\
+ -s, --number-separator=STRING add STRING after (possible) line number\n\
+ -v, --first-page=NUMBER first line number on each logical page\n\
+ -w, --number-width=NUMBER use NUMBER columns for line numbers\n\
+ --help display this help and exit\n\
+ --version output version information and exit\n\
+\n\
+By default, selects -v1 -i1 -l1 -sTAB -w6 -nrn -hn -bt -fn. CC are\n\
+two delimiter characters for separating logical pages, a missing\n\
+second character implies :. Type \\\\ for \\. STYLE is one of:\n\
+\n\
+ a number all lines\n\
+ t number only nonempty lines\n\
+ n number no lines\n\
+ pREGEXP number only lines that contain a match for REGEXP\n\
+\n\
+FORMAT is one of:\n\
+\n\
+ ln left justified, no leading zeros\n\
+ rn right justified, no leading zeros\n\
+ rz right justified, leading zeros\n\
+\n\
+"));
}
+ exit (status);
+}
- if (show_version)
+/* Build the printf format string, based on `lineno_format'. */
+
+static void
+build_print_fmt ()
+{
+ /* 12 = 10 chars for lineno_width, 1 for %, 1 for \0. */
+ print_fmt = xmalloc (strlen (separator_str) + 12);
+ switch (lineno_format)
{
- printf ("nl - %s\n", version_string);
- exit (0);
+ case FORMAT_RIGHT_NOLZ:
+ sprintf (print_fmt, "%%%dd%s", lineno_width, separator_str);
+ break;
+ case FORMAT_RIGHT_LZ:
+ sprintf (print_fmt, "%%0%dd%s", lineno_width, separator_str);
+ break;
+ case FORMAT_LEFT:
+ sprintf (print_fmt, "%%-%dd%s", lineno_width, separator_str);
+ break;
}
+}
- if (show_help)
- usage (0);
+/* Set the command line flag TYPEP and possibly the regex pointer REGEXP,
+ according to `optarg'. */
- /* Initialize the section delimiters. */
- c = strlen (section_del);
+static int
+build_type_arg (typep, regexp)
+ char **typep;
+ struct re_pattern_buffer *regexp;
+{
+ const char *errmsg;
+ int rval = TRUE;
+ int optlen;
- header_del_len = c * 3;
- header_del = xmalloc (header_del_len + 1);
- strcat (strcat (strcpy (header_del, section_del), section_del), section_del);
+ switch (*optarg)
+ {
+ case 'a':
+ case 't':
+ case 'n':
+ *typep = optarg;
+ break;
+ case 'p':
+ *typep = optarg++;
+ optlen = strlen (optarg);
+ regexp->allocated = optlen * 2;
+ regexp->buffer = (unsigned char *) xmalloc (regexp->allocated);
+ regexp->translate = NULL;
+ regexp->fastmap = xmalloc (256);
+ regexp->fastmap_accurate = 0;
+ errmsg = re_compile_pattern (optarg, optlen, regexp);
+ if (errmsg)
+ error (1, 0, "%s", errmsg);
+ break;
+ default:
+ rval = FALSE;
+ break;
+ }
+ return rval;
+}
- body_del_len = c * 2;
- body_del = xmalloc (body_del_len + 1);
- strcat (strcpy (body_del, section_del), section_del);
+/* Print and increment the line number. */
- footer_del_len = c;
- footer_del = xmalloc (footer_del_len + 1);
- strcpy (footer_del, section_del);
+static void
+print_lineno ()
+{
+ printf (print_fmt, line_no);
+ line_no += page_incr;
+}
- /* Initialize the input buffer. */
- initbuffer (&line_buf);
+/* Switch to a header section. */
- /* Initialize the printf format for unnumbered lines. */
- c = strlen (separator_str);
- print_no_line_fmt = xmalloc (lineno_width + c + 1);
- memset (print_no_line_fmt, ' ', lineno_width + c);
- print_no_line_fmt[lineno_width + c] = '\0';
+static void
+proc_header ()
+{
+ current_type = header_type;
+ current_regex = &header_regex;
+ if (reset_numbers)
+ line_no = page_start;
+ putchar ('\n');
+}
- line_no = page_start;
+/* Switch to a body section. */
+
+static void
+proc_body ()
+{
current_type = body_type;
current_regex = &body_regex;
- build_print_fmt ();
+ putchar ('\n');
+}
- /* Main processing. */
+/* Switch to a footer section. */
- if (optind == argc)
- exit_status |= nl_file ("-");
- else
- for (; optind < argc; optind++)
- exit_status |= nl_file (argv[optind]);
+static void
+proc_footer ()
+{
+ current_type = footer_type;
+ current_regex = &footer_regex;
+ putchar ('\n');
+}
- if (have_read_stdin && fclose (stdin) == EOF)
- {
- error (0, errno, "-");
- exit_status = 1;
- }
- if (ferror (stdout) || fclose (stdout) == EOF)
- error (1, errno, _("write error"));
+/* Process a regular text line in `line_buf'. */
- exit (exit_status);
+static void
+proc_text ()
+{
+ static int blank_lines = 0; /* Consecutive blank lines so far. */
+
+ switch (*current_type)
+ {
+ case 'a':
+ if (blank_join > 1)
+ {
+ if (line_buf.length || ++blank_lines == blank_join)
+ {
+ print_lineno ();
+ blank_lines = 0;
+ }
+ else
+ printf (print_no_line_fmt);
+ }
+ else
+ print_lineno ();
+ break;
+ case 't':
+ if (line_buf.length)
+ print_lineno ();
+ else
+ printf (print_no_line_fmt);
+ break;
+ case 'n':
+ printf (print_no_line_fmt);
+ break;
+ case 'p':
+ if (re_search (current_regex, line_buf.buffer, line_buf.length,
+ 0, line_buf.length, (struct re_registers *) 0) < 0)
+ printf (print_no_line_fmt);
+ else
+ print_lineno ();
+ break;
+ }
+ fwrite (line_buf.buffer, sizeof (char), line_buf.length, stdout);
+ putchar ('\n');
}
-\f
+
+/* Return the type of line in `line_buf'. */
+
+static enum section
+check_section ()
+{
+ if (line_buf.length < 2 || memcmp (line_buf.buffer, section_del, 2))
+ return Text;
+ if (line_buf.length == header_del_len
+ && !memcmp (line_buf.buffer, header_del, header_del_len))
+ return Header;
+ if (line_buf.length == body_del_len
+ && !memcmp (line_buf.buffer, body_del, body_del_len))
+ return Body;
+ if (line_buf.length == footer_del_len
+ && !memcmp (line_buf.buffer, footer_del, footer_del_len))
+ return Footer;
+ return Text;
+}
+
+/* Read and process the file pointed to by FP. */
+
+static void
+process_file (fp)
+ FILE *fp;
+{
+ while (readline (&line_buf, fp))
+ {
+ switch ((int) check_section ())
+ {
+ case Header:
+ proc_header ();
+ break;
+ case Body:
+ proc_body ();
+ break;
+ case Footer:
+ proc_footer ();
+ break;
+ case Text:
+ proc_text ();
+ break;
+ }
+ }
+}
+
/* Process file FILE to standard output.
Return 0 if successful, 1 if not. */
return 0;
}
-/* Read and process the file pointed to by FP. */
-
-static void
-process_file (fp)
- FILE *fp;
+void
+main (argc, argv)
+ int argc;
+ char **argv;
{
- while (readline (&line_buf, fp))
+ int c, exit_status = 0;
+
+ program_name = argv[0];
+ have_read_stdin = 0;
+
+ while ((c = getopt_long (argc, argv, "h:b:f:v:i:pl:s:w:n:d:", longopts,
+ (int *) 0)) != EOF)
{
- switch ((int) check_section ())
+ switch (c)
{
- case Header:
- proc_header ();
+ case 0:
break;
- case Body:
- proc_body ();
+
+ case 'h':
+ if (build_type_arg (&header_type, &header_regex) != TRUE)
+ usage (2);
break;
- case Footer:
- proc_footer ();
+ case 'b':
+ if (build_type_arg (&body_type, &body_regex) != TRUE)
+ usage (2);
break;
- case Text:
- proc_text ();
+ case 'f':
+ if (build_type_arg (&footer_type, &footer_regex) != TRUE)
+ usage (2);
+ break;
+ case 'v':
+ page_start = atoi (optarg);
+ break;
+ case 'i':
+ page_incr = atoi (optarg);
+ if (page_incr < 1)
+ page_incr = 1;
+ break;
+ case 'p':
+ reset_numbers = FALSE;
+ break;
+ case 'l':
+ blank_join = atoi (optarg);
+ break;
+ case 's':
+ separator_str = optarg;
+ break;
+ case 'w':
+ lineno_width = atoi (optarg);
+ if (lineno_width < 1)
+ lineno_width = 1;
+ break;
+ case 'n':
+ switch (*optarg)
+ {
+ case 'l':
+ if (optarg[1] == 'n')
+ lineno_format = FORMAT_LEFT;
+ else
+ usage (2);
+ break;
+ case 'r':
+ switch (optarg[1])
+ {
+ case 'n':
+ lineno_format = FORMAT_RIGHT_NOLZ;
+ break;
+ case 'z':
+ lineno_format = FORMAT_RIGHT_LZ;
+ break;
+ default:
+ usage (2);
+ break;
+ }
+ break;
+ default:
+ usage (2);
+ break;
+ }
+ break;
+ case 'd':
+ section_del = optarg;
+ break;
+ default:
+ usage (2);
break;
}
}
-}
-\f
-/* Return the type of line in `line_buf'. */
-static enum section
-check_section ()
-{
- if (line_buf.length < 2 || memcmp (line_buf.buffer, section_del, 2))
- return Text;
- if (line_buf.length == header_del_len
- && !memcmp (line_buf.buffer, header_del, header_del_len))
- return Header;
- if (line_buf.length == body_del_len
- && !memcmp (line_buf.buffer, body_del, body_del_len))
- return Body;
- if (line_buf.length == footer_del_len
- && !memcmp (line_buf.buffer, footer_del, footer_del_len))
- return Footer;
- return Text;
-}
-
-/* Switch to a header section. */
-
-static void
-proc_header ()
-{
- current_type = header_type;
- current_regex = &header_regex;
- if (reset_numbers)
- line_no = page_start;
- putchar ('\n');
-}
-
-/* Switch to a body section. */
-
-static void
-proc_body ()
-{
- current_type = body_type;
- current_regex = &body_regex;
- putchar ('\n');
-}
+ if (show_version)
+ {
+ printf ("nl - %s\n", version_string);
+ exit (0);
+ }
-/* Switch to a footer section. */
+ if (show_help)
+ usage (0);
-static void
-proc_footer ()
-{
- current_type = footer_type;
- current_regex = &footer_regex;
- putchar ('\n');
-}
+ /* Initialize the section delimiters. */
+ c = strlen (section_del);
-/* Process a regular text line in `line_buf'. */
+ header_del_len = c * 3;
+ header_del = xmalloc (header_del_len + 1);
+ strcat (strcat (strcpy (header_del, section_del), section_del), section_del);
-static void
-proc_text ()
-{
- static int blank_lines = 0; /* Consecutive blank lines so far. */
+ body_del_len = c * 2;
+ body_del = xmalloc (body_del_len + 1);
+ strcat (strcpy (body_del, section_del), section_del);
- switch (*current_type)
- {
- case 'a':
- if (blank_join > 1)
- {
- if (line_buf.length || ++blank_lines == blank_join)
- {
- print_lineno ();
- blank_lines = 0;
- }
- else
- printf (print_no_line_fmt);
- }
- else
- print_lineno ();
- break;
- case 't':
- if (line_buf.length)
- print_lineno ();
- else
- printf (print_no_line_fmt);
- break;
- case 'n':
- printf (print_no_line_fmt);
- break;
- case 'p':
- if (re_search (current_regex, line_buf.buffer, line_buf.length,
- 0, line_buf.length, (struct re_registers *) 0) < 0)
- printf (print_no_line_fmt);
- else
- print_lineno ();
- break;
- }
- fwrite (line_buf.buffer, sizeof (char), line_buf.length, stdout);
- putchar ('\n');
-}
-\f
-/* Print and increment the line number. */
+ footer_del_len = c;
+ footer_del = xmalloc (footer_del_len + 1);
+ strcpy (footer_del, section_del);
-static void
-print_lineno ()
-{
- printf (print_fmt, line_no);
- line_no += page_incr;
-}
+ /* Initialize the input buffer. */
+ initbuffer (&line_buf);
-/* Build the printf format string, based on `lineno_format'. */
+ /* Initialize the printf format for unnumbered lines. */
+ c = strlen (separator_str);
+ print_no_line_fmt = xmalloc (lineno_width + c + 1);
+ memset (print_no_line_fmt, ' ', lineno_width + c);
+ print_no_line_fmt[lineno_width + c] = '\0';
-static void
-build_print_fmt ()
-{
- /* 12 = 10 chars for lineno_width, 1 for %, 1 for \0. */
- print_fmt = xmalloc (strlen (separator_str) + 12);
- switch (lineno_format)
- {
- case FORMAT_RIGHT_NOLZ:
- sprintf (print_fmt, "%%%dd%s", lineno_width, separator_str);
- break;
- case FORMAT_RIGHT_LZ:
- sprintf (print_fmt, "%%0%dd%s", lineno_width, separator_str);
- break;
- case FORMAT_LEFT:
- sprintf (print_fmt, "%%-%dd%s", lineno_width, separator_str);
- break;
- }
-}
+ line_no = page_start;
+ current_type = body_type;
+ current_regex = &body_regex;
+ build_print_fmt ();
-/* Set the command line flag TYPEP and possibly the regex pointer REGEXP,
- according to `optarg'. */
+ /* Main processing. */
-static int
-build_type_arg (typep, regexp)
- char **typep;
- struct re_pattern_buffer *regexp;
-{
- const char *errmsg;
- int rval = TRUE;
- int optlen;
+ if (optind == argc)
+ exit_status |= nl_file ("-");
+ else
+ for (; optind < argc; optind++)
+ exit_status |= nl_file (argv[optind]);
- switch (*optarg)
+ if (have_read_stdin && fclose (stdin) == EOF)
{
- case 'a':
- case 't':
- case 'n':
- *typep = optarg;
- break;
- case 'p':
- *typep = optarg++;
- optlen = strlen (optarg);
- regexp->allocated = optlen * 2;
- regexp->buffer = (unsigned char *) xmalloc (regexp->allocated);
- regexp->translate = NULL;
- regexp->fastmap = xmalloc (256);
- regexp->fastmap_accurate = 0;
- errmsg = re_compile_pattern (optarg, optlen, regexp);
- if (errmsg)
- error (1, 0, "%s", errmsg);
- break;
- default:
- rval = FALSE;
- break;
+ error (0, errno, "-");
+ exit_status = 1;
}
- return rval;
-}
-\f
-/* Print a usage message and quit. */
+ if (ferror (stdout) || fclose (stdout) == EOF)
+ error (1, errno, _("write error"));
-static void
-usage (status)
- int status;
-{
- if (status != 0)
- fprintf (stderr, _("Try `%s --help' for more information.\n"),
- program_name);
- else
- {
- printf (_("\
-Usage: %s [OPTION]... [FILE]...\n\
-"),
- program_name);
- printf (_("\
-Write each FILE to standard output, with line numbers added.\n\
-With no FILE, or when FILE is -, read standard input.\n\
-\n\
- -b, --body-numbering=STYLE use STYLE for numbering body lines\n\
- -d, --section-delimiter=CC use CC for separating logical pages\n\
- -f, --footer-numbering=STYLE use STYLE for numbering footer lines\n\
- -h, --header-numbering=STYLE use STYLE for numbering header lines\n\
- -i, --page-increment=NUMBER line number increment at each line\n\
- -l, --join-blank-lines=NUMBER group of NUMBER empty lines counted as one\n\
- -n, --number-format=FORMAT insert line numbers according to FORMAT\n\
- -p, --no-renumber do not reset line numbers at logical pages\n\
- -s, --number-separator=STRING add STRING after (possible) line number\n\
- -v, --first-page=NUMBER first line number on each logical page\n\
- -w, --number-width=NUMBER use NUMBER columns for line numbers\n\
- --help display this help and exit\n\
- --version output version information and exit\n\
-\n\
-By default, selects -v1 -i1 -l1 -sTAB -w6 -nrn -hn -bt -fn. CC are\n\
-two delimiter characters for separating logical pages, a missing\n\
-second character implies :. Type \\\\ for \\. STYLE is one of:\n\
-\n\
- a number all lines\n\
- t number only nonempty lines\n\
- n number no lines\n\
- pREGEXP number only lines that contain a match for REGEXP\n\
-\n\
-FORMAT is one of:\n\
-\n\
- ln left justified, no leading zeros\n\
- rn right justified, no leading zeros\n\
- rz right justified, leading zeros\n\
-\n\
-"));
- }
- exit (status);
+ exit (exit_status);
}