source: toilet/trunk/src/main.c @ 4681

Last change on this file since 4681 was 4681, checked in by Sam Hocevar, 10 years ago

Remove the build dependency on the getopt implementation and use libcaca
instead.

  • Property svn:keywords set to Id
File size: 6.9 KB
RevLine 
[1084]1/*
[1087]2 *  TOIlet        The Other Implementation’s letters
[4134]3 *  Copyright (c) 2006 Sam Hocevar <sam@hocevar.net>
[1087]4 *                All Rights Reserved
[1084]5 *
[1461]6 *  This program is free software. It comes without any warranty, to
[1451]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
[1087]10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
[1084]11 */
12
[1102]13/*
14 * This is the main program entry point.
15 */
16
[1084]17#include "config.h"
18
[1441]19#if defined HAVE_INTTYPES_H
[1087]20#   include <inttypes.h>
21#endif
[1441]22#if defined HAVE_SYS_IOCTL_H && defined HAVE_TIOCGWINSZ
[1116]23#   include <sys/ioctl.h>
24#endif
[1087]25#include <stdio.h>
26#include <stdlib.h>
[1319]27#include <string.h>
[2988]28#include <caca.h>
[1084]29
[1143]30#include "toilet.h"
[1100]31#include "render.h"
[1227]32#include "filter.h"
[1321]33#include "export.h"
[1087]34
[1114]35static void version(void);
36static void usage(void);
37
[1087]38int main(int argc, char *argv[])
[1084]39{
[1193]40    context_t struct_cx;
41    context_t *cx = &struct_cx;
42
[1114]43    int infocode = -1;
[1087]44
[1193]45    cx->export = "utf8";
[4135]46    cx->font = "ascii9";
[1326]47    cx->dir = FONTDIR;
[1193]48
49    cx->term_width = 80;
50
[1404]51    cx->hmode = H_DEFAULT;
[1401]52
[1228]53    cx->filters = NULL;
54    cx->nfilters = 0;
55
[1087]56    for(;;)
57    {
[4347]58#define MOREINFO "Try `%s --help' for more information.\n"
[1087]59        int option_index = 0;
[4681]60        static struct caca_option long_options[] =
[1087]61        {
62            /* Long option, needs arg, flag, short option */
[1101]63            { "font", 1, NULL, 'f' },
[1115]64            { "directory", 1, NULL, 'd' },
[1114]65            { "width", 1, NULL, 'w' },
[1116]66            { "termwidth", 0, NULL, 't' },
[1228]67            { "filter", 1, NULL, 'F' },
[1240]68            { "gay", 0, NULL, 130 },
69            { "metal", 0, NULL, 131 },
[1319]70            { "export", 1, NULL, 'E' },
[1240]71            { "irc", 0, NULL, 140 },
72            { "html", 0, NULL, 141 },
[1087]73            { "help", 0, NULL, 'h' },
[1114]74            { "infocode", 1, NULL, 'I' },
[1087]75            { "version", 0, NULL, 'v' },
76            { NULL, 0, NULL, 0 }
77        };
78
[4681]79        int c = caca_getopt(argc, argv, "f:d:w:tsSkWoF:E:hI:v",
80                            long_options, &option_index);
[1087]81        if(c == -1)
82            break;
83
84        switch(c)
85        {
86        case 'h': /* --help */
[1114]87            usage();
[1087]88            return 0;
[1114]89        case 'I': /* --infocode */
[4681]90            infocode = atoi(caca_optarg);
[1114]91            break;
[1101]92        case 'v': /* --version */
[1114]93            version();
[1101]94            return 0;
95        case 'f': /* --font */
[4681]96            cx->font = caca_optarg;
[1099]97            break;
[1115]98        case 'd': /* --directory */
[4681]99            cx->dir = caca_optarg;
[1115]100            break;
[1228]101        case 'F': /* --filter */
[4681]102            if(!strcmp(caca_optarg, "list"))
[1319]103                return filter_list();
[4681]104            if(filter_add(cx, caca_optarg) < 0)
[1228]105                return -1;
106            break;
[1240]107        case 130: /* --gay */
[1228]108            filter_add(cx, "gay");
[1087]109            break;
[1240]110        case 131: /* --metal */
[1228]111            filter_add(cx, "metal");
[1101]112            break;
[1114]113        case 'w': /* --width */
[4681]114            cx->term_width = atoi(caca_optarg);
[1114]115            break;
[1116]116        case 't': /* --termwidth */
117        {
[1441]118#if defined HAVE_SYS_IOCTL_H && defined HAVE_TIOCGWINSZ
[1116]119            struct winsize ws;
120
121            if((ioctl(1, TIOCGWINSZ, &ws) != -1 ||
122                ioctl(2, TIOCGWINSZ, &ws) != -1 ||
123                ioctl(0, TIOCGWINSZ, &ws) != -1) && ws.ws_col != 0)
[1193]124                cx->term_width = ws.ws_col;
[1116]125#endif
126            break;
127        }
[1401]128        case 's':
[1404]129            cx->hmode = H_DEFAULT;
[1401]130            break;
131        case 'S':
[1404]132            cx->hmode = H_SMUSH;
[1401]133            break;
134        case 'k':
[1404]135            cx->hmode = H_KERN;
[1401]136            break;
137        case 'W':
[1404]138            cx->hmode = H_NONE;
[1401]139            break;
140        case 'o':
[1404]141            cx->hmode = H_OVERLAP;
[1401]142            break;
[1319]143        case 'E': /* --export */
[4681]144            if(!strcmp(caca_optarg, "list"))
[1319]145                return export_list();
[4681]146            if(export_set(cx, caca_optarg) < 0)
[1321]147                return -1;
[1319]148            break;
[1240]149        case 140: /* --irc */
[1321]150            export_set(cx, "irc");
[1087]151            break;
[1240]152        case 141: /* --html */
[1321]153            export_set(cx, "html");
[1240]154            break;
[1087]155        case '?':
156            printf(MOREINFO, argv[0]);
157            return 1;
158        default:
159            printf("%s: invalid option -- %i\n", argv[0], c);
160            printf(MOREINFO, argv[0]);
161            return 1;
162        }
163    }
164
[1114]165    switch(infocode)
166    {
167        case -1:
168            break;
169        case 0:
170            version();
171            return 0;
172        case 1:
173            printf("20201\n");
174            return 0;
175        case 2:
[1193]176            printf("%s\n", cx->dir);
[1114]177            return 0;
178        case 3:
[1193]179            printf("%s\n", cx->font);
[1114]180            return 0;
181        case 4:
[1193]182            printf("%u\n", cx->term_width);
[1114]183            return 0;
184        default:
185            return 0;
186    }
187
[1241]188    if(render_init(cx) < 0)
[1199]189        return -1;
190
[4681]191    if(caca_optind >= argc)
[1241]192        render_stdin(cx);
193    else
[4681]194        render_list(cx, argc - caca_optind, argv + caca_optind);
[1087]195
[1241]196    render_end(cx);
197    filter_end(cx);
[1087]198
[1084]199    return 0;
200}
201
[4347]202#define USAGE \
[1403]203    "Usage: toilet [ -hkostvSW ] [ -d fontdirectory ]\n" \
[1240]204    "              [ -f fontfile ] [ -F filter ] [ -w outputwidth ]\n" \
[1319]205    "              [ -I infocode ] [ -E format ] [ message ]\n"
[1193]206
[4347]207#define HELP \
[1441]208    "  -f, --font <name>        select the font\n" \
209    "  -d, --directory <dir>    specify font directory\n" \
210    "  -s, -S, -k, -W, -o       render mode (default, force smushing,\n" \
211    "                           kerning, full width, overlap)\n" \
212    "  -w, --width <width>      set output width\n" \
213    "  -t, --termwidth          adapt to terminal's width\n" \
214    "  -F, --filter <filters>   apply one or several filters to the text\n" \
215    "  -F, --filter list        list available filters\n" \
216    "      --gay                rainbow filter (same as -F gay)\n" \
217    "      --metal              metal filter (same as -F metal)\n" \
218    "  -E, --export <format>    select export format\n" \
219    "  -E, --export list        list available export formats\n" \
220    "      --irc                output IRC colour codes (same as -E irc)\n" \
221    "      --html               output an HTML document (same as -E html)\n" \
222    "  -h, --help               display this help and exit\n" \
223    "  -I, --infocode <code>    print FIGlet-compatible infocode\n" \
224    "  -v, --version            output version information and exit\n"
225
[1114]226static void version(void)
227{
[1193]228    printf(
229    "TOIlet Copyright 2006 Sam Hocevar\n"
[4134]230    "Internet: <sam@hocevar.net> Version: %s, date: %s\n"
[1193]231    "\n"
232    "TOIlet, along with the various TOIlet fonts and documentation, may be\n"
233    "freely copied and distributed.\n"
234    "\n"
[4134]235    "If you use TOIlet, please send an e-mail message to <sam@hocevar.net>.\n"
[1193]236    "\n"
237    "The latest version of TOIlet is available from the web site,\n"
238    "        http://libcaca.zoy.org/toilet.html\n"
239    "\n"
[1441]240    "%s", VERSION, DATE, USAGE);
[1114]241}
242
243static void usage(void)
244{
[1441]245    printf("%s%s", HELP, USAGE);
[1114]246}
247
Note: See TracBrowser for help on using the repository browser.