Changeset 4254


Ignore:
Timestamp:
01/13/10 00:54:09 (3 years ago)
Author:
sam
Message:

Support cat's -AeEtTv? flags in zzcat.

Location:
zzuf/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • zzuf/trunk/doc/zzcat.1.in

    r4246 r4254  
    33zzcat \- debugging tool for zzuf 
    44.SH SYNOPSIS 
    5 \fBzzcat\fR [\fB\-x\fR \fIsequence\fR] [\fIFILE\fR]... 
     5\fBzzcat\fR [\fB\-AbeEntTv\fR] [\fB\-x\fR \fIsequence\fR] [\fIFILE\fR]... 
    66.br 
    77\fBzzcat \-l\fR | \fB\-\-list\fR 
     
    2121\fB    zzcat -x "fread(1,10000)" /dev/zero\fR 
    2222.SH OPTIONS 
     23.TP 
     24\fB\-A\fR, \fB\-\-show\-all\fR 
     25Equivalent to \fB\-vET\fR. 
     26.TP 
     27\fB\-b\fR, \fB\-\-number\-nonblank\fR 
     28Number nonempty output lines. 
     29.TP 
     30\fB\-e\fR 
     31Equivalent to \fB\-vET\fR. 
     32.TP 
     33\fB\-E\fR, \fB\-\-show\-ends\fR 
     34Display $ at end of each line. 
     35.TP 
     36\fB\-n\fR, \fB\-\-number\fR 
     37Number all output lines. 
     38.TP 
     39\fB\-t\fR 
     40Equivalent to \fB\-vT\fR. 
     41.TP 
     42\fB\-T\fR, \fB\-\-show\-tabs\fR 
     43Display TAB characters as ^I. 
     44.TP 
     45\fB\-v\fR, \fB\-\-show\-nonprinting\fR 
     46Use ^ and M- notation, except for LFD and TAB. 
    2347.TP 
    2448\fB\-x\fR, \fB\-\-execute\fR=\fIsequence\fR 
  • zzuf/trunk/src/zzcat.c

    r4253 r4254  
    6161static void usage(void); 
    6262 
     63/* Output parameters */ 
     64static int escape_tabs = 0; 
     65static int escape_ends = 0; 
     66static int escape_other = 0; 
     67static int number_lines = 0; 
     68static int number_nonblank = 0; 
     69 
    6370/* 
    6471 * Main program. 
     
    7279    for (;;) 
    7380    { 
    74 #define OPTSTR "+x:lhV" 
     81#define OPTSTR "+AbeEntTvx:lhV" 
    7582#define MOREINFO "Try `%s --help' for more information.\n" 
    7683        int option_index = 0; 
    7784        static struct myoption long_options[] = 
    7885        { 
    79             { "execute",     1, NULL, 'x' }, 
    80             { "list",        0, NULL, 'l' }, 
    81             { "help",        0, NULL, 'h' }, 
    82             { "version",     0, NULL, 'V' }, 
    83             { NULL,          0, NULL,  0  } 
     86            { "show-all",         0, NULL, 'A' }, 
     87            { "number-nonblank",  0, NULL, 'b' }, 
     88            { "show-ends",        0, NULL, 'E' }, 
     89            { "number",           0, NULL, 'n' }, 
     90            { "show-tabs",        0, NULL, 'T' }, 
     91            { "show-nonprinting", 0, NULL, 'v' }, 
     92            { "execute",          1, NULL, 'x' }, 
     93            { "list",             0, NULL, 'l' }, 
     94            { "help",             0, NULL, 'h' }, 
     95            { "version",          0, NULL, 'V' }, 
     96            { NULL,               0, NULL,  0  } 
    8497        }; 
    8598        int c = mygetopt(argc, argv, OPTSTR, long_options, &option_index); 
     
    90103        switch (c) 
    91104        { 
     105        case 'A': /* --show-all */ 
     106            escape_tabs = escape_ends = escape_other = 1; 
     107            break; 
     108        case 'b': /* --number-nonblank */ 
     109            number_lines = 1; 
     110            break; 
     111        case 'e': 
     112            escape_ends = escape_other = 1; 
     113            break; 
     114        case 'E': /* --show-ends */ 
     115            escape_ends = 1; 
     116            break; 
     117        case 'n': /* --number */ 
     118            number_nonblank = 1; 
     119            break; 
     120        case 't': 
     121            escape_tabs = escape_other = 1; 
     122            break; 
     123        case 'T': /* --show-tabs */ 
     124            escape_tabs = 1; 
     125            break; 
     126        case 'v': /* --show-nonprinting */ 
     127            escape_tabs = 1; 
     128            break; 
    92129        case 'x': /* --execute */ 
    93130            if (myoptarg[0] == '=') 
     
    451488        close(fd); 
    452489 
    453     fwrite(retbuf, retlen, 1, stdout); 
     490    if (escape_tabs || escape_ends || escape_other || number_lines) 
     491    { 
     492        size_t i; 
     493 
     494        for (i = 0; i < retlen; i++) 
     495        { 
     496            int ch = (unsigned int)(unsigned char)retbuf[i]; 
     497 
     498            if (escape_other && ch >= 0x80) 
     499            { 
     500                if (ch - 0x80 < 0x20 || ch - 0x80 == 0x7f) 
     501                    fprintf(stdout, "M-^%c", (ch - 0x80) ^ 0x40); 
     502                else 
     503                    fprintf(stdout, "M-%c", ch - 0x80); 
     504            } 
     505            else if (escape_tabs && ch == '\t') 
     506                fprintf(stdout, "^I"); 
     507            else if (escape_ends && ch == '\n') 
     508                puts("$"); 
     509            else if (escape_other && (ch < 0x20 || ch == 0x7f)) 
     510                fprintf(stdout, "^%c", ch ^ 0x40); 
     511            else 
     512                putchar(ch); 
     513        } 
     514    } 
     515    else 
     516    { 
     517        fwrite(retbuf, retlen, 1, stdout); 
     518    } 
    454519 
    455520    free(retbuf); 
     
    661726static void usage(void) 
    662727{ 
    663     printf("Usage: zzcat [-x sequence] [FILE...]\n"); 
     728    printf("Usage: zzcat [AbeEntTv] [-x sequence] [FILE...]\n"); 
    664729    printf("       zzcat -l | --list\n"); 
    665730    printf("       zzcat -h | --help\n"); 
     
    668733    printf("\n"); 
    669734    printf("Mandatory arguments to long options are mandatory for short options too.\n"); 
     735    printf("  -A, --show-all            equivalent to -vET\n"); 
     736    printf("  -b, --number-nonblank     number nonempty output lines\n"); 
     737    printf("  -e                        equivalent to -vE\n"); 
     738    printf("  -E, --show-ends           display $ at end of each line\n"); 
     739    printf("  -n, --number              number all output lines\n"); 
     740    printf("  -t                        equivalent to -vT\n"); 
     741    printf("  -T, --show-tabs           display TAB characters as ^I\n"); 
     742    printf("  -v, --show-nonprinting    use ^ and M- notation, except for LFD and TAB\n"); 
    670743    printf("  -x, --execute <sequence>  execute commands in <sequence>\n"); 
    671744    printf("  -l, --list                list available program functions\n"); 
Note: See TracChangeset for help on using the changeset viewer.