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

Last change on this file since 1110 was 1110, checked in by Sam Hocevar, 15 years ago
  • Don't autocrop final canvas if it was rendered with the figlet engine.
  • Property svn:keywords set to Id
File size: 5.1 KB
Line 
1/*
2 *  TOIlet        The Other Implementation’s letters
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: main.c 1110 2006-09-26 21:40:35Z sam $
7 *
8 *  This program is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14/*
15 * This is the main program entry point.
16 */
17
18#include "config.h"
19
20#if defined(HAVE_INTTYPES_H)
21#   include <inttypes.h>
22#endif
23#if defined(HAVE_GETOPT_H)
24#   include <getopt.h>
25#endif
26#include <stdio.h>
27#include <string.h>
28#include <stdlib.h>
29#include <cucul.h>
30
31#include "render.h"
32#include "figlet.h"
33#include "filters.h"
34
35int main(int argc, char *argv[])
36{
37    cucul_canvas_t *cv;
38    cucul_buffer_t *buffer;
39
40    uint32_t *string = NULL;
41    unsigned int length;
42
43    int i;
44
45    char const *export = "utf8";
46    char const *font = "mono9";
47    unsigned flag_gay = 0;
48    unsigned flag_metal = 0;
49
50#if defined(HAVE_GETOPT_H)
51    for(;;)
52    {
53#   ifdef HAVE_GETOPT_LONG
54#       define MOREINFO "Try `%s --help' for more information.\n"
55        int option_index = 0;
56        static struct option long_options[] =
57        {
58            /* Long option, needs arg, flag, short option */
59            { "font", 1, NULL, 'f' },
60            { "gay", 0, NULL, 'g' },
61            { "metal", 0, NULL, 'm' },
62            { "irc", 0, NULL, 'i' },
63            { "help", 0, NULL, 'h' },
64            { "version", 0, NULL, 'v' },
65            { NULL, 0, NULL, 0 }
66        };
67
68        int c = getopt_long(argc, argv, "f:gmihv", long_options, &option_index);
69#   else
70#       define MOREINFO "Try `%s -h' for more information.\n"
71        int c = getopt(argc, argv, "f:gmihv");
72#   endif
73        if(c == -1)
74            break;
75
76        switch(c)
77        {
78        case 'h': /* --help */
79            printf("Usage: %s [ -f:gmihv ] [ message ]\n", argv[0]);
80#   ifdef HAVE_GETOPT_LONG
81            printf("  -f, --font <fontfile>\n");
82            printf("                   select the font\n");
83            printf("  -g, --gay        add a rainbow effect to the text\n");
84            printf("  -m, --metal      add a metal effect to the text\n");
85            printf("  -i, --irc        output IRC colour codes\n");
86            printf("  -h, --help       display this help and exit\n");
87            printf("  -v, --version    output version information and exit\n");
88#   else
89            printf("  -f <fontfile>\n");
90            printf("        select the font\n");
91            printf("  -g    add a rainbow effect to the text\n");
92            printf("  -m    add a metal effect to the text\n");
93            printf("  -i    output IRC colour codes\n");
94            printf("  -h    display this help and exit\n");
95            printf("  -v    output version information and exit\n");
96#   endif
97            return 0;
98        case 'v': /* --version */
99            printf("TOIlet Copyright 2006 Sam Hocevar %s\n", VERSION);
100            printf("Internet: <sam@zoy.org> Version: 0, date: 21 Sep 2006\n");
101            printf("\n");
102            return 0;
103        case 'f': /* --font */
104            font = optarg;
105            break;
106        case 'g': /* --gay */
107            flag_gay = 1;
108            break;
109        case 'm': /* --metal */
110            flag_metal = 1;
111            break;
112        case 'i': /* --irc */
113            export = "irc";
114            break;
115        case '?':
116            printf(MOREINFO, argv[0]);
117            return 1;
118        default:
119            printf("%s: invalid option -- %i\n", argv[0], c);
120            printf(MOREINFO, argv[0]);
121            return 1;
122        }
123    }
124#else
125#   define MOREINFO "Usage: %s message...\n"
126    int optind = 1;
127#endif
128
129    if(optind >= argc)
130    {
131        printf("%s: too few arguments\n", argv[0]);
132        printf(MOREINFO, argv[0]);
133        return 1;
134    }
135
136    /* Load rest of commandline into a UTF-32 string */
137    for(i = optind, length = 0; i < argc; i++)
138    {
139        unsigned int k, guessed_len, real_len;
140
141        guessed_len = strlen(argv[i]);
142
143        if(i > optind)
144            string[length++] = (uint32_t)' ';
145
146        string = realloc(string, (length + guessed_len + 1) * 4);
147
148        for(k = 0, real_len = 0; k < guessed_len; real_len++)
149        {
150            unsigned int char_len;
151
152            string[length + real_len] =
153                cucul_utf8_to_utf32(argv[i] + k, &char_len);
154
155            k += char_len;
156        }
157
158        length += real_len;
159    }
160
161    /* Render string to canvas */
162    if(!strcasecmp(font, "mono9"))
163    {
164        cv = render_big(string, length);
165        filter_autocrop(cv);
166    }
167    else if(!strcasecmp(font, "term"))
168        cv = render_tiny(string, length);
169    else
170        cv = render_figlet(string, length, font);
171
172    /* Do gay stuff with our string (léopard) */
173    if(flag_metal)
174        filter_metal(cv);
175    if(flag_gay)
176        filter_gay(cv);
177
178    /* Output char */
179    buffer = cucul_export_canvas(cv, export);
180    fwrite(cucul_get_buffer_data(buffer),
181           cucul_get_buffer_size(buffer), 1, stdout);
182    cucul_free_buffer(buffer);
183
184    cucul_free_canvas(cv);
185
186    return 0;
187}
188
Note: See TracBrowser for help on using the repository browser.