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

Last change on this file since 1100 was 1100, checked in by Sam Hocevar, 13 years ago
  • Split main.c into main.c/render.c/filters.c.
  • Property svn:keywords set to Id
File size: 4.4 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 1100 2006-09-23 19:54:20Z 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#include "config.h"
15
16#if defined(HAVE_INTTYPES_H)
17#   include <inttypes.h>
18#endif
19#if defined(HAVE_GETOPT_H)
20#   include <getopt.h>
21#endif
22#include <stdio.h>
23#include <string.h>
24#include <stdlib.h>
25#include <cucul.h>
26
27#include "render.h"
28#include "filters.h"
29
30int main(int argc, char *argv[])
31{
32    cucul_canvas_t *cv;
33    cucul_buffer_t *buffer;
34
35    uint32_t *string = NULL;
36    unsigned int length;
37
38    int i;
39
40    char const *export = "utf8";
41    unsigned flag_gay = 0;
42    unsigned flag_metal = 0;
43
44#if defined(HAVE_GETOPT_H)
45    for(;;)
46    {
47#   ifdef HAVE_GETOPT_LONG
48#       define MOREINFO "Try `%s --help' for more information.\n"
49        int option_index = 0;
50        static struct option long_options[] =
51        {
52            /* Long option, needs arg, flag, short option */
53            { "metal", 0, NULL, 'm' },
54            { "gay", 0, NULL, 'g' },
55            { "irc", 0, NULL, 'i' },
56            { "help", 0, NULL, 'h' },
57            { "version", 0, NULL, 'v' },
58            { NULL, 0, NULL, 0 }
59        };
60
61        int c = getopt_long(argc, argv, "gmihv", long_options, &option_index);
62#   else
63#       define MOREINFO "Try `%s -h' for more information.\n"
64        int c = getopt(argc, argv, "gmihv");
65#   endif
66        if(c == -1)
67            break;
68
69        switch(c)
70        {
71        case 'h': /* --help */
72            printf("Usage: %s [ -gmihv ] [ message ]\n", argv[0]);
73#   ifdef HAVE_GETOPT_LONG
74            printf("  -g, --gay        add a rainbow effect to the text\n");
75            printf("  -m, --metal      add a metal effect to the text\n");
76            printf("  -i, --irc        output IRC colour codes\n");
77            printf("  -h, --help       display this help and exit\n");
78            printf("  -v, --version    output version information and exit\n");
79#   else
80            printf("  -g    add a rainbow effect to the text\n");
81            printf("  -m    add a metal effect to the text\n");
82            printf("  -i    output IRC colour codes\n");
83            printf("  -h    display this help and exit\n");
84            printf("  -v    output version information and exit\n");
85#   endif
86            return 0;
87        case 'm': /* --metal */
88            flag_metal = 1;
89            break;
90        case 'g': /* --gay */
91            flag_gay = 1;
92            break;
93        case 'i': /* --irc */
94            export = "irc";
95            break;
96        case 'v': /* --version */
97            printf("TOIlet Copyright 2006 Sam Hocevar %s\n", VERSION);
98            printf("Internet: <sam@zoy.org> Version: 0, date: 21 Sep 2006\n");
99            printf("\n");
100            return 0;
101        case '?':
102            printf(MOREINFO, argv[0]);
103            return 1;
104        default:
105            printf("%s: invalid option -- %i\n", argv[0], c);
106            printf(MOREINFO, argv[0]);
107            return 1;
108        }
109    }
110#else
111#   define MOREINFO "Usage: %s message...\n"
112    int optind = 1;
113#endif
114
115    if(optind >= argc)
116    {
117        printf("%s: too few arguments\n", argv[0]);
118        printf(MOREINFO, argv[0]);
119        return 1;
120    }
121
122    /* Load rest of commandline into a UTF-32 string */
123    for(i = optind, length = 0; i < argc; i++)
124    {
125        unsigned int k, guessed_len, real_len;
126
127        guessed_len = strlen(argv[i]);
128
129        if(i > optind)
130            string[length++] = (uint32_t)' ';
131
132        string = realloc(string, (length + guessed_len + 1) * 4);
133
134        for(k = 0, real_len = 0; k < guessed_len; real_len++)
135        {
136            unsigned int char_len;
137
138            string[length + real_len] =
139                cucul_utf8_to_utf32(argv[i] + k, &char_len);
140
141            k += char_len;
142        }
143
144        length += real_len;
145    }
146
147    /* Do gay stuff with our string (léopard) */
148    cv = render_big(string, length);
149    filter_autocrop(cv);
150    if(flag_metal)
151        filter_metal(cv);
152    if(flag_gay)
153        filter_gay(cv);
154
155    /* Output char */
156    buffer = cucul_export_canvas(cv, export);
157    fwrite(cucul_get_buffer_data(buffer),
158           cucul_get_buffer_size(buffer), 1, stdout);
159    cucul_free_buffer(buffer);
160
161    cucul_free_canvas(cv);
162
163    return 0;
164}
165
Note: See TracBrowser for help on using the repository browser.