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

Last change on this file since 2988 was 2988, checked in by Sam Hocevar, 12 years ago

Port toilet to the unified libcaca 0.99.beta15 API.

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