File: debconf-escape.c

package info (click to toggle)
cdebconf 0.182
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,668 kB
  • sloc: ansic: 16,289; sh: 547; makefile: 438; sql: 52; perl: 13
file content (126 lines) | stat: -rw-r--r-- 2,864 bytes parent folder | download | duplicates (5)
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
/**
 * @file debconf-escape.c
 * @brief helper when working with debconf's escape capability
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>

static int escape = 0;
static int unescape = 0;

static struct option options[] = {
    { "help", 0, 0, 'h' },
    { "escape", 0, 0, 'e' },
    { "unescape", 0, 0, 'u' },
    { 0, 0, 0, 0 },
};

static void help(FILE *f, const char *exename)
{
    fprintf(f, "Usage: %s -e|-u < input-text\n", exename);
    fputs("  -e, --escape      escape text\n"
        "  -u, --unescape    unescape text\n"
        "\n"
        "Exactly one of -e or -u must be used.\n", f);
}

static void parsecmdline(int argc, char **argv)
{
    int c;

    while ((c = getopt_long(argc, argv, "euh", options, NULL)) >= 0)
    {
        switch (c)
        {
            case 'h':
                help(stdout, argv[0]);
                exit(0);
                break;
            case 'e':
                escape = 1;
                break;
            case 'u':
                unescape = 1;
                break;
            default:
                break;
        }
    }

    if (optind > argc || escape == unescape)
    {
        help(stderr, argv[0]);
        exit(1);
    }
}

int main(int argc, char **argv)
{
    char buf[1024];
    const char *p;
    size_t r;

    parsecmdline(argc, argv);

    if (escape)
    {
        while (!feof(stdin) && !ferror(stdin))
        {
            r = fread(buf, sizeof(*buf), 1024, stdin);
            for (p = buf; p < buf + r; ++p)
            {
                switch (*p)
                {
                    case '\\':
                        fputs("\\\\", stdout);
                        break;
                    case '\n':
                        fputs("\\n", stdout);
                        break;
                    default:
                        fputc(*p, stdout);
                        break;
                }
            }
        }
    }
    else
    {
        int unesc = 0;
        while (!feof(stdin) && !ferror(stdin))
        {
            r = fread(buf, sizeof(*buf), 1024, stdin);
            for (p = buf; p < buf + r; ++p)
            {
                if (unesc)
                {
                    switch (*p)
                    {
                        case 'n':
                            fputc('\n', stdout);
                            break;
                        default:
                            fputc(*p, stdout);
                            break;
                    }
                    unesc = 0;
                }
                else if (*p == '\\')
                    unesc = 1;
                else
                    fputc(*p, stdout);
            }
        }
    }

    if (ferror(stdin))
    {
        fprintf(stderr, "%s: error reading stdin\n", argv[0]);
        return 1;
    }

    return 0;
}