From 46c13717ee5c439fd180f6de2138011914c64780 Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Sun, 26 Aug 2007 11:01:53 +0000 Subject: introduce grep output awareness --- el.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 8 deletions(-) (limited to 'el.c') diff --git a/el.c b/el.c index a1994b6..f615b5c 100644 --- a/el.c +++ b/el.c @@ -1,5 +1,5 @@ /* Extract Lines - Usage: echo -x 10 2 7 100 | el data.txt + Usage: echo -x 10 2 7 100 | el -x data.txt extracts lines 16, 2, 7, 256 from the file data.txt */ @@ -25,12 +25,33 @@ static long indexfilled = 1; // If set, the stream of linenums specified calls // the first line number 0... *shiver* -static int zerobased = 0; +static int g_zerobased = 0; + +// If set, line numbers are prepended to each line +// (the way grep does it, i.e. ^2342:$) +static int g_echolinenr = 0; // This tells us, whether we need to scan for hex // line numbers static char *g_scanfmodifier = "%i"; +// If user specifies some line numbers on command +// line, store pointer here +static char *g_immediatelinenums = (char *)0; + +// If input is guaranteed to come from "grep -n" +// we will spare the user from "| cut -f 1 -d :" +static int g_fromgrep = 0; +static int g_fromgrepverbatim = 0; + +static int nextchar( void ) { + if( !g_immediatelinenums ) + return getchar(); + if( *g_immediatelinenums ) + return *g_immediatelinenums++; + return EOF; +} + // scans the text file for the requested line // returns NULL, if line exceeds file's end // Note: we will not extend the index too early @@ -118,10 +139,24 @@ static int acquire_lineno( int c ) { int inputindex = 0, lineno; while ( (c != EOF) && !isspace(c)) { + if( g_fromgrep && c == ':' ) { + if( g_echolinenr ) + putchar( ':' ); + while( ( ( c = nextchar() ) != EOF ) && ( c != '\n' ) ) + if( g_fromgrepverbatim ) + putchar(c); + if( g_fromgrepverbatim ) + putchar( '\t' ); + break; + } + if( g_echolinenr ) + putchar( c ); if( inputindex < 14 ) input[inputindex++] = (char)c; - c = getchar(); + c = nextchar(); } + if( !g_fromgrep && g_echolinenr ) + putchar( ':' ); if( inputindex == 14 ) return 0; @@ -132,11 +167,11 @@ static int acquire_lineno( int c ) { if( sscanf( input, g_scanfmodifier, &lineno ) != 1 ) return 0; - return lineno + zerobased; + return lineno + g_zerobased; } static void usage() { - fputs( "Usage: el [-0xh] filename < filenums\n", stderr); + fputs( "Usage: el [-0Ggnxh] [-i linenums] filename < linenum_file\n", stderr); } int main( int argc, char **argv ) { @@ -144,14 +179,25 @@ int main( int argc, char **argv ) { MAP textfile = NULL; int c; - while ((c = getopt(argc, argv, ":0x")) != -1) { + while ((c = getopt(argc, argv, ":0i:gGnxh")) != -1) { switch (c) { case '0': - zerobased = 1; + g_zerobased = 1; break; case 'x': g_scanfmodifier = "%x"; break; + case 'i': + g_immediatelinenums = optarg; + break; + case 'n': + g_echolinenr = 1; + break; + case 'G': + g_fromgrepverbatim = 1; + case 'g': + g_fromgrep = 1; + break; case 'h': case '?': default: @@ -178,7 +224,7 @@ int main( int argc, char **argv ) { // First line starts at begin of file. index[0] = textfile->addr; - while( (c = getchar()) != EOF ) { + while( (c = nextchar()) != EOF ) { // get linenumber, pass on eof test char long slen, lineno = acquire_lineno(c); unsigned char *line = scanforline( textfile, lineno, &slen ); -- cgit v1.2.3