2011-05-29 20:30:44 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <regex.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "text.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2011-06-21 04:05:37 +00:00
|
|
|
static void nl(FILE *);
|
2011-05-29 20:30:44 +00:00
|
|
|
|
|
|
|
static char mode = 't';
|
|
|
|
static const char *sep = "\t";
|
|
|
|
static long incr = 1;
|
|
|
|
static regex_t preg;
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2011-06-10 04:41:40 +00:00
|
|
|
char c;
|
2011-05-29 20:30:44 +00:00
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
while((c = getopt(argc, argv, "b:i:s:")) != -1)
|
|
|
|
switch(c) {
|
|
|
|
case 'b':
|
|
|
|
mode = optarg[0];
|
|
|
|
if(optarg[0] == 'p')
|
|
|
|
regcomp(&preg, &optarg[1], REG_NOSUB);
|
|
|
|
else if(!strchr("ant", optarg[0]) || optarg[1] != '\0')
|
2011-05-29 20:34:02 +00:00
|
|
|
eprintf("usage: %s [-b mode] [-i increment] [-s separator] [file...]\n", argv[0]);
|
2011-05-29 20:30:44 +00:00
|
|
|
break;
|
|
|
|
case 'i':
|
2011-06-10 13:55:01 +00:00
|
|
|
incr = estrtol(optarg, 0);
|
2011-05-29 20:30:44 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
sep = optarg;
|
|
|
|
break;
|
|
|
|
default:
|
2012-05-20 12:57:58 +00:00
|
|
|
exit(2);
|
2011-05-29 20:30:44 +00:00
|
|
|
}
|
|
|
|
if(optind == argc)
|
2011-06-21 04:05:37 +00:00
|
|
|
nl(stdin);
|
2011-05-29 20:30:44 +00:00
|
|
|
else for(; optind < argc; optind++) {
|
|
|
|
if(!(fp = fopen(argv[optind], "r")))
|
|
|
|
eprintf("fopen %s:", argv[optind]);
|
2011-06-21 04:05:37 +00:00
|
|
|
nl(fp);
|
2011-05-29 20:30:44 +00:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-06-21 04:05:37 +00:00
|
|
|
nl(FILE *fp)
|
2011-05-29 20:30:44 +00:00
|
|
|
{
|
|
|
|
char *buf = NULL;
|
|
|
|
long n = 0;
|
|
|
|
size_t size = 0;
|
|
|
|
|
|
|
|
while(afgets(&buf, &size, fp))
|
|
|
|
if((mode == 'a')
|
|
|
|
|| (mode == 'p' && !regexec(&preg, buf, 0, NULL, 0))
|
|
|
|
|| (mode == 't' && buf[0] != '\n'))
|
|
|
|
printf("%6ld%s%s", n += incr, sep, buf);
|
|
|
|
else
|
|
|
|
printf(" %s", buf);
|
|
|
|
free(buf);
|
|
|
|
}
|