source: zzuf/trunk/src/zzcat.c @ 4259

Last change on this file since 4259 was 4259, checked in by Sam Hocevar, 11 years ago

Minor zzcat refactoring.

  • Property svn:keywords set to Id
File size: 22.7 KB
Line 
1/*
2 *  zzcat - various cat reimplementations for testing purposes
3 *  Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net>
4 *                All Rights Reserved
5 *
6 *  This program is free software. It comes without any warranty, to
7 *  the extent permitted by applicable law. You can redistribute it
8 *  and/or modify it under the terms of the Do What The Fuck You Want
9 *  To Public License, Version 2, as published by Sam Hocevar. See
10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
11 */
12
13/*
14 * TODO: fsetpos64, fgetln
15 */
16
17#include "config.h"
18
19/* Needed for lseek64() */
20#define _LARGEFILE64_SOURCE
21/* Needed for O_RDONLY on HP-UX */
22#define _INCLUDE_POSIX_SOURCE
23/* Needed for fgets_unlocked() */
24#define _GNU_SOURCE
25
26#if defined HAVE_STDINT_H
27#   include <stdint.h>
28#elif defined HAVE_INTTYPES_H
29#   include <inttypes.h>
30#endif
31#include <sys/types.h>
32#include <sys/stat.h>
33#include <fcntl.h>
34#if defined HAVE_UNISTD_H
35#   include <unistd.h>
36#endif
37#if defined HAVE_SYS_MMAN_H
38#   include <sys/mman.h>
39#endif
40#include <stdlib.h>
41#include <stdio.h>
42#include <string.h>
43
44#if !defined HAVE_GETOPT_LONG
45#   include "mygetopt.h"
46#elif defined HAVE_GETOPT_H
47#   include <getopt.h>
48#endif
49
50#if defined HAVE_GETOPT_LONG
51#   define mygetopt getopt_long
52#   define myoptind optind
53#   define myoptarg optarg
54#   define myoption option
55#endif
56
57static int run(char const *sequence, char const *file);
58static void output(char const *buf, size_t len);
59
60static void syntax(void);
61static void version(void);
62static void usage(void);
63
64/* Output parameters */
65static int escape_tabs = 0;
66static int escape_ends = 0;
67static int escape_other = 0;
68static int number_lines = 0;
69static int number_nonblank = 0;
70
71/*
72 * Main program.
73 */
74
75int main(int argc, char *argv[])
76{
77    char const *sequence = "repeat(-1, fread(1,32768), feof(1))";
78    int i;
79
80    for (;;)
81    {
82#define OPTSTR "+AbeEntTvx:lhV"
83#define MOREINFO "Try `%s --help' for more information.\n"
84        int option_index = 0;
85        static struct myoption long_options[] =
86        {
87            { "show-all",         0, NULL, 'A' },
88            { "number-nonblank",  0, NULL, 'b' },
89            { "show-ends",        0, NULL, 'E' },
90            { "number",           0, NULL, 'n' },
91            { "show-tabs",        0, NULL, 'T' },
92            { "show-nonprinting", 0, NULL, 'v' },
93            { "execute",          1, NULL, 'x' },
94            { "list",             0, NULL, 'l' },
95            { "help",             0, NULL, 'h' },
96            { "version",          0, NULL, 'V' },
97            { NULL,               0, NULL,  0  }
98        };
99        int c = mygetopt(argc, argv, OPTSTR, long_options, &option_index);
100
101        if (c == -1)
102            break;
103
104        switch (c)
105        {
106        case 'A': /* --show-all */
107            escape_tabs = escape_ends = escape_other = 1;
108            break;
109        case 'b': /* --number-nonblank */
110            number_lines = 1;
111            break;
112        case 'e':
113            escape_ends = escape_other = 1;
114            break;
115        case 'E': /* --show-ends */
116            escape_ends = 1;
117            break;
118        case 'n': /* --number */
119            number_nonblank = 1;
120            break;
121        case 't':
122            escape_tabs = escape_other = 1;
123            break;
124        case 'T': /* --show-tabs */
125            escape_tabs = 1;
126            break;
127        case 'v': /* --show-nonprinting */
128            escape_tabs = 1;
129            break;
130        case 'x': /* --execute */
131            if (myoptarg[0] == '=')
132                myoptarg++;
133            sequence = myoptarg;
134            break;
135        case 'l': /* --list */
136            syntax();
137            return 0;
138        case 'h': /* --help */
139            usage();
140            return 0;
141        case 'V': /* --version */
142            version();
143            return 0;
144        default:
145            fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
146            printf(MOREINFO, argv[0]);
147            return EXIT_FAILURE;
148        }
149    }
150
151    if (myoptind >= argc)
152    {
153        fprintf(stderr, "E: zzcat: too few arguments\n");
154        return EXIT_FAILURE;
155    }
156
157    for (i = myoptind; i < argc; i++)
158    {
159        int ret = run(sequence, argv[i]);
160        if (ret)
161            return ret;
162    }
163
164    return EXIT_SUCCESS;
165}
166
167/*
168 * File output method.
169 */
170
171static void output(char const *buf, size_t len)
172{
173    size_t i;
174
175    if (!(escape_tabs || escape_ends || escape_other
176           || number_lines || number_nonblank))
177    {
178        fwrite(buf, len, 1, stdout);
179        return;
180    }
181
182    for (i = 0; i < len; i++)
183    {
184        int ch = (unsigned int)(unsigned char)buf[i];
185
186        if (escape_other && ch >= 0x80)
187        {
188            if (ch - 0x80 < 0x20 || ch - 0x80 == 0x7f)
189                fprintf(stdout, "M-^%c", (ch - 0x80) ^ 0x40);
190            else
191                fprintf(stdout, "M-%c", ch - 0x80);
192        }
193        else if (escape_tabs && ch == '\t')
194            fprintf(stdout, "^I");
195        else if (escape_ends && ch == '\n')
196            puts("$");
197        else if (escape_other && (ch < 0x20 || ch == 0x7f))
198            fprintf(stdout, "^%c", ch ^ 0x40);
199        else
200            putchar(ch);
201    }
202}
203
204/*
205 * Command intepreter
206 */
207
208#define MY_FOPEN(cmd) \
209    do { \
210        cmd; \
211        if (!f) \
212        { \
213            fprintf(stderr, "E: zzcat: cannot open `%s'\n", file); \
214            return EXIT_FAILURE; \
215        } \
216        retoff = 0; \
217        sequence = strchr(sequence, ')') + 1; \
218    } while(0)
219
220#define MY_FCLOSE(cmd) \
221    do { \
222        cmd; \
223        f = NULL; \
224        sequence = strchr(sequence, ')') + 1; \
225    } while(0)
226
227#define ROUNDUP(size) (((size) + 0xfff) & ~0xfff)
228
229#define MERGE(address, cnt, off) \
230    do { \
231        size_t _cnt = cnt, _off = off; \
232        if (_cnt && retoff + _cnt > retlen) \
233        { \
234            retlen = retoff + _cnt; \
235            if (ROUNDUP(retlen) != ROUNDUP(retlen - _cnt)) \
236                retbuf = realloc(retbuf, ROUNDUP(retlen)); \
237        } \
238        if (_cnt > 0) \
239            memcpy(retbuf + retoff, address, _cnt); \
240        retoff += _off; \
241    } while(0)
242
243#define MY_FREAD(cmd, buf, cnt) MY_FCALL(cmd, buf, cnt, cnt)
244#define MY_FSEEK(cmd, off) MY_FCALL(cmd, /* unused */ "", 0, off)
245
246#define MY_FCALL(cmd, buf, cnt, off) \
247    do { \
248        if (!f) \
249        { \
250            f = fopen(file, "r"); \
251            if (!f) \
252            { \
253                fprintf(stderr, "E: zzcat: cannot open `%s'\n", file); \
254                return EXIT_FAILURE; \
255            } \
256        } \
257        /* fprintf(stderr, "debug: %s\n", #cmd); */ \
258        cmd; \
259        MERGE(buf, cnt, off); \
260        sequence = strchr(sequence, ')') + 1; \
261    } while(0)
262
263#define MY_FEOF() \
264    do { \
265        if (!f) \
266        { \
267            f = fopen(file, "r"); \
268            if (!f) \
269            { \
270                fprintf(stderr, "E: zzcat: cannot open `%s'\n", file); \
271                return EXIT_FAILURE; \
272            } \
273        } \
274        if (feof(f)) \
275            feofs++; \
276        if (feofs >= l1) \
277            finish = 1; \
278        sequence = strchr(sequence, ')') + 1; \
279    } while(0)
280
281/*
282 * Command parser. We rewrite fmt by replacing the last character with
283 * '%c' and check that the sscanf() call returns the expected number of
284 * matches plus one (for the last character). We use this macro trick to
285 * avoid using vsscanf() which does not exist on all platforms.
286 */
287
288struct parser
289{
290    char tmpfmt[1024], ch, lastch;
291};
292
293static int make_fmt(struct parser *p, char const *fmt)
294{
295    char const *tmp;
296    size_t len;
297    int ret = 0;
298
299    len = strlen(fmt);
300    p->lastch = fmt[len - 1];
301
302    memcpy(p->tmpfmt, fmt, len - 1);
303    p->tmpfmt[len - 1] = '%';
304    p->tmpfmt[len] = 'c';
305    p->tmpfmt[len + 1] = '\0';
306
307    for (tmp = p->tmpfmt; *tmp; tmp++)
308        if (*tmp == '%')
309            tmp++, ret++;
310
311    return ret;
312}
313
314#define PARSECMD(fmt, arg...) \
315    (make_fmt(&parser, fmt) == sscanf(sequence, parser.tmpfmt, \
316                                      ##arg, &parser.ch) \
317         && parser.ch == parser.lastch)
318
319/*
320 * File reader. We parse a command line and perform all the operations it
321 * contains on the specified file.
322 */
323
324static int run(char const *sequence, char const *file)
325{
326    struct { char const *p; int count; } loops[128];
327    char *retbuf = NULL, *tmp;
328    FILE *f = NULL;
329    size_t retlen = 0, retoff = 0;
330    int nloops = 0, fd = -1, feofs = 0, finish = 0;
331
332    /* Allocate 32MB for our temporary buffer. Any larger value will crash. */
333    tmp = malloc(32 * 1024 * 1024);
334
335    while (*sequence)
336    {
337        struct parser parser;
338        long int l1, l2;
339        char *s, *lineptr = NULL;
340        size_t k;
341        ssize_t l;
342        int n;
343        char ch;
344
345        (void)k;
346
347        /* Ignore punctuation */
348        if (strchr(" \t,;\r\n", *sequence))
349            sequence++;
350
351        /* Loop handling */
352        else if (PARSECMD("repeat ( %li ,", &l1))
353        {
354            sequence = strchr(sequence, ',') + 1;
355            loops[nloops].p = sequence;
356            loops[nloops].count = l1;
357            nloops++;
358        }
359        else if (PARSECMD(")"))
360        {
361            if (nloops == 0)
362            {
363                fprintf(stderr, "E: zzcat: ')' outside a loop\n");
364                return EXIT_FAILURE;
365            }
366            if (loops[nloops - 1].count == 1 || finish)
367            {
368                nloops--;
369                sequence = strchr(sequence, ')') + 1;
370            }
371            else
372            {
373                loops[nloops - 1].count--;
374                sequence = loops[nloops - 1].p;
375            }
376
377            finish = 0;
378        }
379
380        /* FILE * opening functions */
381        else if (PARSECMD("fopen ( )"))
382            MY_FOPEN(f = fopen(file, "r"));
383#if defined HAVE_FOPEN64
384        else if (PARSECMD("fopen64 ( )"))
385            MY_FOPEN(f = fopen64(file, "r"));
386#endif
387#if defined HAVE___FOPEN64
388        else if (PARSECMD("__fopen64 ( )"))
389            MY_FOPEN(f = __fopen64(file, "r"));
390#endif
391        else if (PARSECMD("freopen ( )"))
392            MY_FOPEN(f = freopen(file, "r", f));
393#if defined HAVE_FREOPEN64
394        else if (PARSECMD("freopen64 ( )"))
395            MY_FOPEN(f = freopen64(file, "r", f));
396#endif
397#if defined HAVE___FREOPEN64
398        else if (PARSECMD("__freopen64 ( )"))
399            MY_FOPEN(f = __freopen64(file, "r", f));
400#endif
401
402        /* FILE * EOF detection */
403        else if (PARSECMD("feof ( %li )", &l1))
404            MY_FEOF();
405
406        /* FILE * closing functions */
407        else if (PARSECMD("fclose ( )"))
408            MY_FCLOSE(fclose(f));
409
410        /* FILE * reading functions */
411        else if (PARSECMD("fread ( %li , %li )", &l1, &l2))
412            MY_FREAD(l = fread(tmp, l1, l2, f), tmp, l > 0 ? l * l1 : 0);
413        else if (PARSECMD("getc ( )"))
414            MY_FREAD(ch = (n = getc(f)), &ch, (n != EOF));
415        else if (PARSECMD("fgetc ( )"))
416            MY_FREAD(ch = (n = fgetc(f)), &ch, (n != EOF));
417        else if (PARSECMD("fgets ( %li )", &l1))
418            MY_FREAD(s = fgets(tmp, l1, f), tmp, s ? strlen(tmp) : 0);
419#if defined HAVE__IO_GETC
420        else if (PARSECMD("_IO_getc ( )"))
421            MY_FREAD(ch = (n = _IO_getc(f)), &ch, (n != EOF));
422#endif
423#if defined HAVE_FREAD_UNLOCKED
424        else if (PARSECMD("fread_unlocked ( %li , %li )", &l1, &l2))
425            MY_FREAD(l = fread_unlocked(tmp, l1, l2, f), tmp, l > 0 ? l * l1 : 0);
426#endif
427#if defined HAVE_FGETS_UNLOCKED
428        else if (PARSECMD("fgets_unlocked ( %li )", &l1))
429            MY_FREAD(s = fgets_unlocked(tmp, l1, f), tmp, s ? strlen(tmp) : 0);
430#endif
431#if defined HAVE_GETC_UNLOCKED
432        else if (PARSECMD("getc_unlocked ( )"))
433            MY_FREAD(ch = (n = getc_unlocked(f)), &ch, (n != EOF));
434#endif
435#if defined HAVE_FGETC_UNLOCKED
436        else if (PARSECMD("fgetc_unlocked ( )"))
437            MY_FREAD(ch = (n = fgetc_unlocked(f)), &ch, (n != EOF));
438#endif
439
440        /* FILE * getdelim functions */
441#if defined HAVE_GETLINE
442        else if (PARSECMD("getline ( )"))
443            MY_FREAD(l = getline(&lineptr, &k, f), lineptr, l >= 0 ? l : 0);
444#endif
445#if defined HAVE_GETDELIM
446        else if (PARSECMD("getdelim ( '%c' )", &ch))
447            MY_FREAD(l = getdelim(&lineptr, &k, ch, f), lineptr, l >= 0 ? l : 0);
448        else if (PARSECMD("getdelim ( %i )", &n))
449            MY_FREAD(l = getdelim(&lineptr, &k, n, f), lineptr, l >= 0 ? l : 0);
450#endif
451#if defined HAVE___GETDELIM
452        else if (PARSECMD("__getdelim ( '%c' )", &ch))
453            MY_FREAD(l = __getdelim(&lineptr, &k, ch, f), lineptr, l >= 0 ? l : 0);
454        else if (PARSECMD("__getdelim ( %i )", &n))
455            MY_FREAD(l = __getdelim(&lineptr, &k, n, f), lineptr, l >= 0 ? l : 0);
456#endif
457
458        /* FILE * seeking functions */
459        else if (PARSECMD("fseek ( %li , SEEK_CUR )", &l1))
460            MY_FSEEK(l = fseek(f, l1, SEEK_CUR),
461                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
462        else if (PARSECMD("fseek ( %li , SEEK_SET )", &l1))
463            MY_FSEEK(l = fseek(f, l1, SEEK_SET),
464                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
465        else if (PARSECMD("fseek ( %li , SEEK_END )", &l1))
466            MY_FSEEK(l = fseek(f, l1, SEEK_END),
467                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
468#if defined HAVE_FSEEKO
469        else if (PARSECMD("fseeko ( %li , SEEK_CUR )", &l1))
470            MY_FSEEK(l = fseeko(f, l1, SEEK_CUR),
471                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
472        else if (PARSECMD("fseeko ( %li , SEEK_SET )", &l1))
473            MY_FSEEK(l = fseeko(f, l1, SEEK_SET),
474                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
475        else if (PARSECMD("fseeko ( %li , SEEK_END )", &l1))
476            MY_FSEEK(l = fseeko(f, l1, SEEK_END),
477                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
478#endif
479#if defined HAVE_FSEEKO64
480        else if (PARSECMD("fseeko64 ( %li , SEEK_CUR )", &l1))
481            MY_FSEEK(l = fseeko64(f, l1, SEEK_CUR),
482                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
483        else if (PARSECMD("fseeko64 ( %li , SEEK_SET )", &l1))
484            MY_FSEEK(l = fseeko64(f, l1, SEEK_SET),
485                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
486        else if (PARSECMD("fseeko64 ( %li , SEEK_END )", &l1))
487            MY_FSEEK(l = fseeko64(f, l1, SEEK_END),
488                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
489#endif
490#if defined HAVE___FSEEKO64
491        else if (PARSECMD("__fseeko64 ( %li , SEEK_CUR )", &l1))
492            MY_FSEEK(l = __fseeko64(f, l1, SEEK_CUR),
493                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
494        else if (PARSECMD("__fseeko64 ( %li , SEEK_SET )", &l1))
495            MY_FSEEK(l = __fseeko64(f, l1, SEEK_SET),
496                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
497        else if (PARSECMD("__fseeko64 ( %li , SEEK_END )", &l1))
498            MY_FSEEK(l = __fseeko64(f, l1, SEEK_END),
499                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
500#endif
501        else if (PARSECMD("rewind ( )"))
502            MY_FSEEK(rewind(f), -retlen);
503        else if (PARSECMD("ungetc ( )"))
504            MY_FSEEK(if(retoff) ungetc((unsigned char)retbuf[retoff - 1], f),
505                     retoff ? -1 : 0);
506
507        /* Unrecognised sequence */
508        else
509        {
510            char buf[16];
511            snprintf(buf, 16, strlen(sequence) < 16 ? "%s" : "%.12s...",
512                     sequence);
513            fprintf(stderr, "E: zzcat: syntax error near `%s'\n", buf);
514            return EXIT_FAILURE;
515        }
516
517        /* Clean up our mess */
518        if (lineptr)
519            free(lineptr);
520
521        if (finish && !nloops)
522            break;
523    }
524
525    if (f)
526        fclose(f);
527
528    if (fd >= 0)
529        close(fd);
530
531    output(retbuf, retlen);
532    free(retbuf);
533    free(tmp);
534
535    return EXIT_SUCCESS;
536}
537
538#if 0
539/* Only read() calls */
540static int zzcat_read(char const *name, unsigned char *data, int64_t len,
541                      int64_t chunk)
542{
543    int i, fd = open(name, O_RDONLY);
544    if(fd < 0)
545        return EXIT_FAILURE;
546    for(i = 0; i < len; i += chunk)
547        read(fd, data + i, chunk);
548    close(fd);
549    return EXIT_SUCCESS;
550}
551
552/* Socket seeks and reads */
553static int zzcat_random_socket(char const *name, unsigned char *data,
554                               int64_t len)
555{
556    int i, j, fd = open(name, O_RDONLY);
557    if(fd < 0)
558        return EXIT_FAILURE;
559    for(i = 0; i < 128; i++)
560    {
561        lseek(fd, myrand() % len, SEEK_SET);
562        for(j = 0; j < 4; j++)
563            read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096);
564#ifdef HAVE_LSEEK64
565        lseek64(fd, myrand() % len, SEEK_SET);
566        for(j = 0; j < 4; j++)
567            read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096);
568#endif
569    }
570    close(fd);
571    return EXIT_SUCCESS;
572}
573
574/* Standard stream seeks and reads */
575static int zzcat_random_stream(char const *name, unsigned char *data,
576                               int64_t len)
577{
578    FILE *stream = fopen(name, "r");
579    int i, j;
580    if(!stream)
581        return EXIT_FAILURE;
582    for(i = 0; i < 128; i++)
583    {
584        long int now;
585        fseek(stream, myrand() % len, SEEK_SET);
586        for(j = 0; j < 4; j++)
587            fread(data + ftell(stream),
588                  myrand() % (len - ftell(stream)), 1, stream);
589        fseek(stream, myrand() % len, SEEK_SET);
590        now = ftell(stream);
591        for(j = 0; j < 16; j++)
592            data[now + j] = getc(stream);
593        now = ftell(stream);
594        for(j = 0; j < 16; j++)
595            data[now + j] = fgetc(stream);
596    }
597    fclose(stream);
598    return EXIT_SUCCESS;
599}
600
601#ifdef HAVE_MMAP
602/* mmap() followed by random memory reads */
603static int zzcat_random_mmap(char const *name, unsigned char *data,
604                               int64_t len)
605{
606    int i, j, fd = open(name, O_RDONLY);
607    if(fd < 0)
608        return EXIT_FAILURE;
609    for(i = 0; i < 128; i++)
610    {
611        char *map;
612        int moff, mlen, pgsz = len + 1;
613#ifdef HAVE_GETPAGESIZE
614        pgsz = getpagesize();
615#endif
616        moff = len < pgsz ? 0 : (myrand() % (len / pgsz)) * pgsz;
617        mlen = 1 + (myrand() % (len - moff));
618        map = mmap(NULL, mlen, PROT_READ, MAP_PRIVATE, fd, moff);
619        if(map == MAP_FAILED)
620            return EXIT_FAILURE;
621        for(j = 0; j < 128; j++)
622        {
623            int x = myrand() % mlen;
624            data[moff + x] = map[x];
625        }
626        munmap(map, mlen);
627    }
628    close(fd);
629    return EXIT_SUCCESS;
630}
631#endif
632#endif
633
634static char const *keyword_list[] =
635{
636    "repeat", "(<int>,<sequence>)", "loop <int> times through <sequence>",
637    "feof", "(<int>)", "break out of loop or sequence after <int> EOFs",
638    NULL
639};
640
641static char const *function_list[] =
642{
643    "fopen", "()", "open file",
644#if defined HAVE_FOPEN64
645    "fopen64", "()", "same as fopen()",
646#endif
647#if defined HAVE___FOPEN64
648    "__fopen64", "()", "same as fopen()",
649#endif
650    "freopen", "()", "reopen file",
651#if defined HAVE_FREOPEN64
652    "freopen64", "()", "same as reopen()",
653#endif
654#if defined HAVE___FREOPEN64
655    "__freopen64", "()", "same as reopen()",
656#endif
657    "fclose", "()", "close file",
658    "fread", "(<inta>,<intb>)", "read <intb> chunks of <inta> bytes",
659    "getc", "()", "get one character (can be a macro)",
660    "fgetc", "()", "get one character",
661    "fgets", "(<int>)", "read one line no longer than <int> bytes",
662#if defined HAVE__IO_GETC
663    "_IO_getc", "()", "get one character",
664#endif
665#if defined HAVE_FREAD_UNLOCKED
666    "fread_unlocked", "(<inta>,<intb>)", "same as fread(), unlocked I/O version",
667#endif
668#if defined HAVE_FGETS_UNLOCKED
669    "fgets_unlocked", "(<int>)", "same as fgets(), unlocked I/O version",
670#endif
671#if defined HAVE_GETC_UNLOCKED
672    "getc_unlocked", "()", "same as getc(), unlocked I/O version",
673#endif
674#if defined HAVE_FGETC_UNLOCKED
675    "fgetc_unlocked", "()", "same as fgetc(), unlocked I/O version",
676#endif
677#if defined HAVE_GETLINE
678    "getline", "()", "read one complete line of text",
679#endif
680#if defined HAVE_GETDELIM
681    "getdelim", "('<char>')", "read all data until delimiter character <char>",
682    "getdelim", "(<int>)", "read all data until delimiter character <int>",
683#endif
684#if defined HAVE___GETDELIM
685    "__getdelim", "('<char>')", "same as getdelim()",
686    "__getdelim", "(<int>)", "same as getdelim()",
687#endif
688    "fseek", "(<int>,<whence>)", "seek using SEEK_CUR, SEEK_SET or SEEK_END",
689#if defined HAVE_FSEEKO
690    "fseeko", "(<int>,<whence>)", "same as fseek()",
691#endif
692#if defined HAVE_FSEEKO64
693    "fseeko64", "(<int>,<whence>)", "same as fseek()",
694#endif
695#if defined HAVE___FSEEKO64
696    "__fseeko64", "(<int>,<whence>)", "same as fseek()",
697#endif
698    "rewind", "()", "rewind to the beginning of the stream",
699    "ungetc", "()", "put one byte back in the stream",
700    NULL
701};
702
703static void print_list(char const **list)
704{
705    static char const spaces[] = "                                ";
706
707    while (*list)
708    {
709        size_t len = printf("  %s%s", list[0], list[1]);
710        if (len < strlen(spaces))
711            printf("%s", spaces + len);
712        printf("%s\n", list[2]);
713        list += 3;
714    }
715}
716
717static void syntax(void)
718{
719    printf("Available control keywords:\n");
720    print_list(keyword_list);
721    printf("\n");
722    printf("Available functions:\n");
723    print_list(function_list);
724}
725
726static void version(void)
727{
728    printf("zzcat %s\n", PACKAGE_VERSION);
729    printf("Copyright (C) 2002-2010 Sam Hocevar <sam@hocevar.net>\n");
730    printf("This program is free software. It comes without any warranty, to the extent\n");
731    printf("permitted by applicable law. You can redistribute it and/or modify it under\n");
732    printf("the terms of the Do What The Fuck You Want To Public License, Version 2, as\n");
733    printf("published by Sam Hocevar. See <http://sam.zoy.org/wtfpl/> for more details.\n");
734    printf("\n");
735    printf("Written by Sam Hocevar. Report bugs to <sam@hocevar.net>.\n");
736}
737
738static void usage(void)
739{
740    printf("Usage: zzcat [AbeEntTv] [-x sequence] [FILE...]\n");
741    printf("       zzcat -l | --list\n");
742    printf("       zzcat -h | --help\n");
743    printf("       zzcat -V | --version\n");
744    printf("Read FILE using a sequence of various I/O methods.\n");
745    printf("\n");
746    printf("Mandatory arguments to long options are mandatory for short options too.\n");
747    printf("  -A, --show-all            equivalent to -vET\n");
748    printf("  -b, --number-nonblank     number nonempty output lines\n");
749    printf("  -e                        equivalent to -vE\n");
750    printf("  -E, --show-ends           display $ at end of each line\n");
751    printf("  -n, --number              number all output lines\n");
752    printf("  -t                        equivalent to -vT\n");
753    printf("  -T, --show-tabs           display TAB characters as ^I\n");
754    printf("  -v, --show-nonprinting    use ^ and M- notation, except for LFD and TAB\n");
755    printf("  -x, --execute <sequence>  execute commands in <sequence>\n");
756    printf("  -l, --list                list available program functions\n");
757    printf("  -h, --help                display this help and exit\n");
758    printf("  -V, --version             output version information and exit\n");
759    printf("\n");
760    printf("Written by Sam Hocevar. Report bugs to <sam@hocevar.net>.\n");
761}
762
Note: See TracBrowser for help on using the repository browser.