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

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

Fix zzcat's -s flag. It must propagate to the next file.

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