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

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

Implement zzcat -b and -n. Fixes #74.

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