3
0

nl.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2017 Denys Vlasenko <vda.linux@googlemail.com>
  4. *
  5. * Licensed under GPLv2, see file LICENSE in this source tree.
  6. */
  7. //config:config NL
  8. //config: bool "nl (4.6 kb)"
  9. //config: default y
  10. //config: help
  11. //config: nl is used to number lines of files.
  12. //applet:IF_NL(APPLET(nl, BB_DIR_USR_BIN, BB_SUID_DROP))
  13. //kbuild:lib-$(CONFIG_NL) += nl.o
  14. //usage:#define nl_trivial_usage
  15. //usage: "[OPTIONS] [FILE]..."
  16. //usage:#define nl_full_usage "\n\n"
  17. //usage: "Write FILEs to standard output with line numbers added\n"
  18. //usage: "\n -b STYLE Which lines to number - a: all, t: nonempty, n: none"
  19. //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^TODO: support "pBRE": number only lines thatmatch regexp BRE"
  20. ////usage: "\n -f STYLE footer lines"
  21. ////usage: "\n -h STYLE header lines"
  22. ////usage: "\n -d CC use CC for separating logical pages"
  23. //usage: "\n -i N Line number increment"
  24. ////usage: "\n -l NUMBER group of NUMBER empty lines counted as one"
  25. ////usage: "\n -n FORMAT lneft justified, no leading zeros; rn or rz"
  26. ////usage: "\n -p do not reset line numbers at logical pages (huh?)"
  27. //usage: "\n -s STRING Use STRING as line number separator"
  28. //usage: "\n -v N Start from N"
  29. //usage: "\n -w N Width of line numbers"
  30. /* By default, selects -v1 -i1 -l1 -sTAB -w6 -nrn -hn -bt -fn */
  31. #include "libbb.h"
  32. int nl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  33. int nl_main(int argc UNUSED_PARAM, char **argv)
  34. {
  35. struct number_state ns;
  36. const char *opt_b = "t";
  37. enum {
  38. OPT_p = (1 << 0),
  39. };
  40. #if ENABLE_LONG_OPTS
  41. static const char nl_longopts[] ALIGN1 =
  42. "body-numbering\0" Required_argument "b"
  43. // "footer-numbering\0" Required_argument "f" - not implemented yet
  44. // "header-numbering\0" Required_argument "h" - not implemented yet
  45. // "section-delimiter\0" Required_argument "d" - not implemented yet
  46. "line-increment\0" Required_argument "i"
  47. // "join-blank-lines\0" Required_argument "l" - not implemented yet
  48. // "number-format\0" Required_argument "n" - not implemented yet
  49. "no-renumber\0" No_argument "p" // no-op so far
  50. "number-separator\0" Required_argument "s"
  51. "starting-line-number\0"Required_argument "v"
  52. "number-width\0" Required_argument "w"
  53. ;
  54. #endif
  55. int exitcode;
  56. ns.width = 6;
  57. ns.start = 1;
  58. ns.inc = 1;
  59. ns.sep = "\t";
  60. getopt32long(argv, "pw:+s:v:+i:+b:", nl_longopts,
  61. &ns.width, &ns.sep, &ns.start, &ns.inc, &opt_b);
  62. ns.all = (opt_b[0] == 'a');
  63. ns.nonempty = (opt_b[0] == 't');
  64. ns.empty_str = xasprintf("%*s\n", ns.width + (int)strlen(ns.sep), "");
  65. argv += optind;
  66. if (!*argv)
  67. *--argv = (char*)"-";
  68. exitcode = EXIT_SUCCESS;
  69. do {
  70. exitcode |= print_numbered_lines(&ns, *argv);
  71. } while (*++argv);
  72. fflush_stdout_and_exit(exitcode);
  73. }