aboutsummaryrefslogtreecommitdiff
path: root/toys/pending/nl.c
blob: a698c8631546c593281684db536f715d1c9200a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* nl.c - print line numbers
 *
 * Copyright 2013 CE Strake <strake888@gmail.com>
 *
 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/
 * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cmdbehav.html

USE_NL(NEWTOY(nl, "Eb:n:s:w#", TOYFLAG_BIN))

config NL
  bool "nl"
  default n
  help
    usage: nl [-E] [-b mode] [-n (r|l)(n|z)] [-s separator] [-w width]

    modes: give numbers to
      a:   all lines
      t:   non-empty lines
      n:   no lines
      pRE: lines what match regex RE

    flags:
      E:   extended RE syntax
*/

#define FOR_nl
#include "toys.h"
#include <regex.h>

GLOBALS(
  long wArgu;
  char *sArgu, *nArgu, *bArgu, *re_xs;
  char fmt[5];
)

char s = '\t';
long width = 6;
regex_t re; /* fails in GLOBALS */

void do_nl (int fd, char *name) {
  char *x;
  FILE *f;
  long n = 0;

  f = fdopen (fd, "r");
  if (!f) perror_exit ("failed to open %s", name);

  x = 0;
  for (;;) {
    size_t l;
    if (getline (&x, &l, f) < 0) {
      if (feof (f)) break;
      perror_exit ("failed to read");
    }
    if (TT.re_xs && regexec (&re, x, 0, 0, 0) == 0) printf (TT.fmt, width, ++n);
    printf ("%c%s", s, x);
  }

  free (x);
  fclose (f);
}

void nl_main (void) {
  if (toys.optflags & FLAG_w) width = TT.wArgu;

  if (TT.sArgu) s = TT.sArgu[0];

  if (!TT.nArgu) TT.nArgu = "rn";

  if (TT.bArgu) switch (TT.bArgu[0]) {
  case 'a':
    TT.re_xs = "";
    break;
  case 't':
    TT.re_xs = ".\n";
    break;
  case 'n':
    TT.re_xs = 0;
    break;
  case 'p':
    TT.re_xs = TT.bArgu + 1;
    break;
  default:
    error_exit ("bad mode: %c", TT.bArgu[0]);
  }
  else TT.re_xs = ".\n";

  if (TT.re_xs &&
      regcomp (&re, TT.re_xs,
               REG_NOSUB |
               (toys.optflags & FLAG_E ? REG_EXTENDED : 0)) != 0) {
    error_exit ("bad RE");
  }

  strcpy (TT.fmt, "%");
  if (TT.nArgu[0] == 'l') strcat (TT.fmt, "-");
  if (TT.nArgu[1] == 'z') strcat (TT.fmt, "0");
  strcat (TT.fmt, "*d");

  loopfiles (toys.optargs, do_nl);
}