nl: UTF-8 support and removed the two characters limit for delimiter

This commit is contained in:
Quentin Rameau 2015-03-20 18:09:23 +01:00 committed by sin
parent b8c9a88371
commit e6c20fe367
3 changed files with 24 additions and 12 deletions

2
README
View File

@ -49,7 +49,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
=*| mktemp non-posix none =*| mktemp non-posix none
=*| mv yes none (-i) =*| mv yes none (-i)
=*| nice yes none =*| nice yes none
=* nl yes none #* nl yes none
=*| nohup yes none =*| nohup yes none
#*| paste yes none #*| paste yes none
=*| printenv non-posix none =*| printenv non-posix none

2
nl.1
View File

@ -47,7 +47,7 @@ a regular expression as defined in
.Xr regex 7 . .Xr regex 7 .
.El .El
.It Fl d Ar delim .It Fl d Ar delim
Specify the two characters delimiter (default is "\\:"). If only one character is specified, the second remains ':'. Specify the delimiter (default is "\\:"). If only one character is specified, the second remains ':'.
.It Fl f Ar type .It Fl f Ar type
Same as Same as
.Fl b .Fl b

32
nl.c
View File

@ -1,11 +1,12 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <limits.h> #include <limits.h>
#include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "text.h" #include "text.h"
#include "utf.h"
#include "util.h" #include "util.h"
/* formats here specify line number and separator (not line content) */ /* formats here specify line number and separator (not line content) */
@ -14,7 +15,7 @@
#define FORMAT_RZ "%0*ld%s" #define FORMAT_RZ "%0*ld%s"
static char type[] = { 'n', 't', 'n' }; /* footer, body, header */ static char type[] = { 'n', 't', 'n' }; /* footer, body, header */
static char delim[] = { '\\', ':' }; static char *delim = "\\:";
static const char *format = FORMAT_RN; static const char *format = FORMAT_RN;
static const char *sep = "\t"; static const char *sep = "\t";
static int width = 6; static int width = 6;
@ -22,6 +23,7 @@ static int pflag = 0;
static size_t startnum = 1; static size_t startnum = 1;
static size_t incr = 1; static size_t incr = 1;
static size_t blines = 1; static size_t blines = 1;
static size_t delimlen = 2;
static regex_t preg[3]; static regex_t preg[3];
static int static int
@ -30,7 +32,7 @@ getsection(char *buf, int *section)
int sectionchanged = 0; int sectionchanged = 0;
int newsection = *section; int newsection = *section;
while (!strncmp(buf, delim, 2)) { for (; !strncmp(buf, delim, delimlen); buf += delimlen) {
if (!sectionchanged) { if (!sectionchanged) {
sectionchanged = 1; sectionchanged = 1;
newsection = 0; newsection = 0;
@ -38,7 +40,6 @@ getsection(char *buf, int *section)
++newsection; ++newsection;
newsection %= 3; newsection %= 3;
} }
buf += 2;
} }
if (buf && buf[0] == '\n') if (buf && buf[0] == '\n')
@ -115,6 +116,7 @@ main(int argc, char *argv[])
{ {
FILE *fp; FILE *fp;
char *d; char *d;
size_t l, s;
ARGBEGIN { ARGBEGIN {
case 'b': case 'b':
@ -122,12 +124,22 @@ main(int argc, char *argv[])
break; break;
case 'd': case 'd':
d = EARGF(usage()); d = EARGF(usage());
if (strlen(d) > 2) { l = utflen(d);
usage();
} else if (d[0] != '\0') { switch (l) {
delim[0] = d[0]; case 0:
if (d[1]) break;
delim[1] = d[1]; case 1:
s = strlen(d);
delim = emalloc(s + 2);
estrlcpy(delim, d, s + 2);
estrlcat(delim, ":", s + 2);
delimlen = s + 1;
break;
default:
delim = d;
delimlen = strlen(delim);
break;
} }
break; break;
case 'f': case 'f':