nl.c (5715B)
1 /* see LICENSE file for copyright and license details */ 2 3 #include <limits.h> 4 #include <stdint.h> 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 9 #include "text.h" 10 #include "utf.h" 11 #include "util.h" 12 13 static size_t startnum = 1; 14 static size_t incr = 1; 15 static size_t blines = 1; 16 static size_t delimlen = 2; 17 static size_t seplen = 1; 18 static int width = 6; 19 static int pflag = 0; 20 static char type[] = {'n', 't', 'n'}; /* footer, body, header */ 21 static char *delim = "\\:"; 22 static char format[6] = "%*ld"; 23 static char *sep = "\t"; 24 static regex_t preg[3]; 25 26 static int 27 getsection(struct line *l, int *section) 28 { 29 size_t i; 30 int sectionchanged = 0, newsection = *section; 31 32 for (i = 0; (l->len - i) >= delimlen && !memcmp(l->data + i, delim, delimlen); i += delimlen) { 33 if (!sectionchanged) { 34 sectionchanged = 1; 35 newsection = 0; 36 } else { 37 newsection = (newsection + 1) % 3; 38 } 39 } 40 41 if (!(l->len - i) || l->data[i] == '\n') 42 *section = newsection; 43 else 44 sectionchanged = 0; 45 46 return sectionchanged; 47 } 48 49 static void 50 nl(const char *fname, FILE *fp) 51 { 52 static struct line line; 53 static size_t size; 54 size_t number = startnum, bl = 1; 55 ssize_t len; 56 int donumber, oldsection, section = 1; 57 58 while ((len = getline(&line.data, &size, fp)) > 0) { 59 line.len = len; 60 donumber = 0; 61 oldsection = section; 62 63 if (getsection(&line, §ion)) { 64 if ((section >= oldsection) && !pflag) 65 number = startnum; 66 continue; 67 } 68 69 switch (type[section]) { 70 case 't': 71 if (line.data[0] != '\n') 72 donumber = 1; 73 break; 74 case 'p': 75 if (!regexec(preg + section, line.data, 0, NULL, 0)) 76 donumber = 1; 77 break; 78 case 'a': 79 if (line.data[0] == '\n' && bl < blines) { 80 ++bl; 81 } else { 82 donumber = 1; 83 bl = 1; 84 } 85 } 86 87 if (donumber) { 88 printf(format, width, number); 89 fwrite(sep, 1, seplen, stdout); 90 number += incr; 91 } 92 fwrite(line.data, 1, line.len, stdout); 93 } 94 free(line.data); 95 if (ferror(fp)) 96 eprintf("getline %s:", fname); 97 } 98 99 static void 100 usage(void) 101 { 102 eprintf( 103 "usage: %s [-p] [-b type] [-d delim] [-f type]\n" 104 " [-h type] [-i num] [-l num] [-n format]\n" 105 " [-s sep] [-v num] [-w num] [file]\n", 106 argv0 107 ); 108 } 109 110 static char 111 getlinetype(char *type, regex_t *preg) 112 { 113 if (type[0] == 'p') 114 eregcomp(preg, type + 1, REG_NOSUB); 115 else if (!type[0] || !strchr("ant", type[0])) 116 usage(); 117 118 return type[0]; 119 } 120 121 // ?man nl: number lines 122 // ?man number the lines of files 123 int 124 main(int argc, char *argv[]) 125 { 126 FILE *fp = NULL; 127 size_t s; 128 int ret = 0; 129 char *d, *formattype, *formatblit; 130 131 ARGBEGIN 132 { 133 // ?man -d:str: specify directory 134 case 'd': 135 switch (utflen((d = EARGF(usage())))) { 136 case 0: 137 eprintf("empty logical page delimiter\n"); 138 break; 139 case 1: 140 s = strlen(d); 141 delim = emalloc(s + 1 + 1); 142 estrlcpy(delim, d, s + 1 + 1); 143 estrlcat(delim, ":", s + 1 + 1); 144 delimlen = s + 1; 145 break; 146 default: 147 delim = d; 148 delimlen = strlen(delim); 149 break; 150 } 151 break; 152 // ?man -f:str: force the operation 153 case 'f': 154 type[0] = getlinetype(EARGF(usage()), preg); 155 break; 156 // ?man -b:str: specify block size or base directory 157 case 'b': 158 type[1] = getlinetype(EARGF(usage()), preg + 1); 159 break; 160 // ?man -h:str: suppress headers or print help 161 case 'h': 162 type[2] = getlinetype(EARGF(usage()), preg + 2); 163 break; 164 // ?man -i:num: interactive mode or prompt for confirmation 165 case 'i': 166 incr = estrtonum( 167 EARGF(usage()), 0, MIN((unsigned long long)LLONG_MAX, (unsigned long long)SIZE_MAX) 168 ); 169 break; 170 // ?man -l:num: list in long format 171 case 'l': 172 blines = estrtonum( 173 EARGF(usage()), 0, MIN((unsigned long long)LLONG_MAX, (unsigned long long)SIZE_MAX) 174 ); 175 break; 176 // ?man -n:str: print line numbers or counts 177 case 'n': 178 formattype = EARGF(usage()); 179 estrlcpy(format, "%", sizeof(format)); 180 181 if (!strcmp(formattype, "ln")) { 182 formatblit = "-"; 183 } else if (!strcmp(formattype, "rn")) { 184 formatblit = ""; 185 } else if (!strcmp(formattype, "rz")) { 186 formatblit = "0"; 187 } else { 188 eprintf("%s: bad format\n", formattype); 189 } 190 191 estrlcat(format, formatblit, sizeof(format)); 192 estrlcat(format, "*ld", sizeof(format)); 193 break; 194 // ?man -p: preserve file attributes 195 case 'p': 196 pflag = 1; 197 break; 198 // ?man -s:str: silent mode or print summary 199 case 's': 200 sep = EARGF(usage()); 201 seplen = unescape(sep); 202 break; 203 // ?man -v:num: verbose mode; show progress 204 case 'v': 205 startnum = estrtonum( 206 EARGF(usage()), 0, MIN((unsigned long long)LLONG_MAX, (unsigned long long)SIZE_MAX) 207 ); 208 break; 209 // ?man -w:num: wait for completion 210 case 'w': 211 width = estrtonum(EARGF(usage()), 1, INT_MAX); 212 break; 213 default: 214 usage(); 215 } 216 ARGEND 217 218 if (argc > 1) 219 usage(); 220 221 if (!argc) { 222 nl("<stdin>", stdin); 223 } else { 224 if (!strcmp(argv[0], "-")) { 225 argv[0] = "<stdin>"; 226 fp = stdin; 227 } else if (!(fp = fopen(argv[0], "r"))) { 228 eprintf("fopen %s:", argv[0]); 229 } 230 nl(argv[0], fp); 231 } 232 233 ret |= fp && fp != stdin && fshut(fp, argv[0]); 234 ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); 235 236 return ret; 237 }