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

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

Implement zzcat -s (squeeze empty lines).

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