File: main.c

package info (click to toggle)
libexplain 1.4.D001-17
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,248 kB
  • sloc: ansic: 156,043; makefile: 47,892; sh: 16,304; yacc: 1,898; awk: 245
file content (305 lines) | stat: -rw-r--r-- 8,424 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
 * libexplain - Explain errno values returned by libc functions
 * Copyright (C) 2013 Peter Miller
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <libexplain/ac/assert.h>
#include <libexplain/ac/errno.h>
#include <libexplain/ac/getopt.h>
#include <libexplain/ac/iconv.h>
#include <libexplain/ac/libintl.h> /* textdomain */
#include <libexplain/ac/locale.h> /* setlocale */
#include <libexplain/ac/stdio.h>
#include <libexplain/ac/stdlib.h>
#include <libexplain/ac/string.h>
#include <libexplain/ac/unistd.h>

#include <libexplain/fread.h>
#include <libexplain/freopen.h>
#include <libexplain/fwrite.h>
#include <libexplain/iconv.h>
#include <libexplain/iconv_close.h>
#include <libexplain/iconv_open.h>
#include <libexplain/output.h>
#include <libexplain/program_name.h>
#include <libexplain/version_print.h>


static void
usage(void)
{
    const char *prog;

    prog = explain_program_name_get();
    fprintf(stderr, "Usage: %s <to-code> <from-code>\n", prog);
    fprintf(stderr, "       %s -V\n", prog);
    exit(EXIT_FAILURE);
}


#define ICONV_FAIL ((size_t)(-1))


int
main(int argc, char **argv)
{
    const char      *to_code;
    const char      *from_code;
    iconv_t         cd;
    char            *ip;
    size_t          isize;
    char            *op;
    size_t          osize;
    size_t          n;
    int             omit_invalid;
    char            ibuf[1 << 12];
    char            obuf[sizeof(ibuf)];

    /*
     * Code sets to convert from and to respectively.  An empty string as the
     * default causes the 'iconv_open' function to look up the charset of the
     * currently selected locale and use it.
     */
    from_code = "";
    to_code = "";

    omit_invalid = 0;
    for (;;)
    {
        static const struct option options[] =
        {
            { "from-code", 1, 0, 'f' },
            { "to-code", 1, 0, 't' },
            { "output", 1, 0, 'o' },
            { "version", 0, 0, 'V' },
            { 0, 0, 0, 0 }
        };

        int c = getopt_long(argc, argv, "cf:o:t:V", options, 0);
        if (c == EOF)
            break;
        switch (c)
        {
        case 'c':
            /* omit invalid characters from input */
            omit_invalid = 1;
            break;

        case 'f':
            from_code = optarg;
            break;

        case 'o':
            explain_freopen_or_die(optarg, "w", stdout);
            break;

        case 't':
            to_code = optarg;
            break;

        case 'V':
            explain_version_print();
            return EXIT_SUCCESS;

        default:
            usage();
        }
    }
    switch (argc - optind)
    {
    case 0:
        break;

    case 1:
        explain_freopen_or_die(argv[optind], "r", stdin);
        break;

    default:
        usage();
    }

    /* Set locale via LC_ALL.  */
    setlocale(LC_ALL, "");

    /* Set the text message domain.  */
    textdomain("libexplain");

    cd = explain_iconv_open_or_die(to_code, from_code);

    ip = ibuf;
    isize = 0;
    op = obuf;
    osize = sizeof(obuf);
    for (;;)
    {
        if (isize == 0)
        {
            isize = explain_fread_or_die(ibuf, 1, sizeof(ibuf), stdin);
            if (isize == 0)
            {
                /* end of input */
                break;
            }
            ip = ibuf;
        }

        n = iconv(cd, &ip, &isize, &op, &osize);
        if (n == ICONV_FAIL)
        {
            switch (errno)
            {
            case EILSEQ:
                /*
                 * 1. if an invalid multibyte sequence is encountered in
                 * the input.  It sets errno to EILSEQ and
                 * returns ICONV_FAIL.  *inbuf is left pointing to the
                 * beginning of the invalid multibyte sequence.
                 */
                {
                    unsigned char c;
                    assert(isize > 0);
                    c = *ip;
                    if (omit_invalid)
                    {
                        ++ip;
                        --isize;
                    }
                    else
                    {
                        if (op > obuf)
                        {
                            explain_fwrite_or_die(obuf, 1, op - obuf, stdout);
                            op = obuf;
                            osize = sizeof(obuf);
                        }
                        *op++ = c;
                        --osize;
                    }
                }
                break;

            case EINVAL:
                /*
                 * 3. An incomplete multibyte sequence is encountered
                 * in the input, and the input byte sequence terminates
                 * after it.  In this case it sets errno to EINVAL and
                 * returns ICONV_FAIL. *inbuf is left pointing to the
                 * beginning of the incomplete multibyte sequence.
                 */
                {
                    size_t          nbytes;

                    if (op > obuf)
                    {
                        explain_fwrite_or_die(obuf, 1, op - obuf, stdout);
                        op = obuf;
                        osize = sizeof(obuf);
                    }

                    /* shift it all down to the start of the buffer */
                    nbytes = isize;
                    memmove(ibuf, ip, nbytes);
                    ip = ibuf;
                    isize = nbytes;

                    /* now read more input */
                    isize +=
                        explain_fread_or_die
                        (
                            ibuf + nbytes,
                            1,
                            sizeof(ibuf) - nbytes,
                            stdin
                        );
                }
                break;

            case E2BIG:
                /*
                 * 4. The output buffer has no more room for the next
                 * converted character.
                 */
                explain_fwrite_or_die(obuf, 1, op - obuf, stdout);
                op = obuf;
                osize = sizeof(obuf);
                break;

            default:
                /* something not mentioned in the man page */
                explain_output_error_and_die
                (
                    "%s",
                    explain_iconv(cd, &ip, &isize, &op, &osize)
                );
                break;
            }
        }
        else
        {
            if (op > obuf)
            {
                explain_fwrite_or_die(obuf, 1, op - obuf, stdout);
                op = obuf;
                osize = sizeof(obuf);
            }
        }
    }

    /* make sure we have enough room to reset the shift state */
    if (op > obuf)
    {
        explain_fwrite_or_die(obuf, 1, op - obuf, stdout);
        op = obuf;
        osize = sizeof(obuf);
    }

    /* set the conversion state to the initial state */
    n = iconv(cd, NULL, NULL, &op, &osize);
    if (n == ICONV_FAIL)
    {
        switch (errno)
        {
        case EILSEQ:
            /*
             * Invalid multi-byte sequence in input.
             * But there was no input.  Ke?
             */
            assert(!"invalid multi-byte sequence in input, "
                "but no input supplied");
            break;

        case EINVAL:
            assert(!"multi-byte sequence spans end-of-input, "
                "but no input supplied");
            break;

        case E2BIG:
            assert(!"this should not happen, output buffer is huge");
            break;

        default:
            assert(!"no idea");
            break;
        }
    }
    explain_fwrite_or_die(obuf, 1, sizeof(obuf) - osize, stdout);

    explain_iconv_close_or_die(cd);
    return EXIT_SUCCESS;
}


/* vim: set ts=8 sw=4 et : */