~ubuntu-branches/ubuntu/trusty/postfix/trusty-updates

« back to all changes in this revision

Viewing changes to src/util/unescape.c

Tags: upstream-2.3.1
ImportĀ upstreamĀ versionĀ 2.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
/*      VSTRING *unescape(result, input)
10
10
/*      VSTRING *result;
11
11
/*      const char *input;
 
12
/*
 
13
/*      VSTRING *escape(result, input, len)
 
14
/*      VSTRING *result;
 
15
/*      const char *input;
 
16
/*      ssize_t len;
12
17
/* DESCRIPTION
13
18
/*      unescape() translates C-like escape sequences in the null-terminated
14
19
/*      string \fIinput\fR and places the result in \fIresult\fR. The result
15
20
/*      is null-terminated, and is the function result value.
16
21
/*
 
22
/*      escape() does the reverse transformation.
 
23
/*
17
24
/*      Escape sequences and their translations:
18
25
/* .IP \ea
19
26
/*      Bell character.
121
128
    return (result);
122
129
}
123
130
 
 
131
/* escape - reverse transformation */
 
132
 
 
133
VSTRING *escape(VSTRING *result, const char *data, ssize_t len)
 
134
{
 
135
    int     ch;
 
136
 
 
137
    VSTRING_RESET(result);
 
138
    while (len-- > 0) {
 
139
        ch = *UCHAR(data++);
 
140
        if (ISASCII(ch)) {
 
141
            if (ISPRINT(ch)) {
 
142
                if (ch == '\\')
 
143
                    VSTRING_ADDCH(result, ch);
 
144
                VSTRING_ADDCH(result, ch);
 
145
                continue;
 
146
            } else if (ch == '\a') {            /* \a -> audible bell */
 
147
                vstring_strcat(result, "\\a");
 
148
                continue;
 
149
            } else if (ch == '\b') {            /* \b -> backspace */
 
150
                vstring_strcat(result, "\\b");
 
151
                continue;
 
152
            } else if (ch == '\f') {            /* \f -> formfeed */
 
153
                vstring_strcat(result, "\\f");
 
154
                continue;
 
155
            } else if (ch == '\n') {            /* \n -> newline */
 
156
                vstring_strcat(result, "\\n");
 
157
                continue;
 
158
            } else if (ch == '\r') {            /* \r -> carriagereturn */
 
159
                vstring_strcat(result, "\\r");
 
160
                continue;
 
161
            } else if (ch == '\t') {            /* \t -> horizontal tab */
 
162
                vstring_strcat(result, "\\t");
 
163
                continue;
 
164
            } else if (ch == '\v') {            /* \v -> vertical tab */
 
165
                vstring_strcat(result, "\\v");
 
166
                continue;
 
167
            }
 
168
        }
 
169
        if (ISDIGIT(*UCHAR(data)))
 
170
            vstring_sprintf_append(result, "\\%03d", ch);
 
171
        else
 
172
            vstring_sprintf_append(result, "\\%d", ch);
 
173
    }
 
174
    VSTRING_TERMINATE(result);
 
175
    return (result);
 
176
}
 
177
 
124
178
#ifdef TEST
125
179
 
 
180
#include <stdlib.h>
 
181
#include <string.h>
 
182
#include <msg.h>
126
183
#include <vstring_vstream.h>
127
184
 
128
 
int     main(int unused_argc, char **unused_argv)
 
185
int     main(int argc, char **argv)
129
186
{
130
187
    VSTRING *in = vstring_alloc(10);
131
188
    VSTRING *out = vstring_alloc(10);
132
 
 
133
 
    while (vstring_fgets_nonl(in, VSTREAM_IN)) {
134
 
        unescape(out, vstring_str(in));
135
 
        vstream_fwrite(VSTREAM_OUT, vstring_str(out), VSTRING_LEN(out));
 
189
    int     un_escape = 1;
 
190
 
 
191
    if (argc > 2 || (argc > 1 && (un_escape = strcmp(argv[1], "-e"))) != 0)
 
192
        msg_fatal("usage: %s [-e (escape)]", argv[0]);
 
193
 
 
194
    if (un_escape) {
 
195
        while (vstring_fgets_nonl(in, VSTREAM_IN)) {
 
196
            unescape(out, vstring_str(in));
 
197
            vstream_fwrite(VSTREAM_OUT, vstring_str(out), VSTRING_LEN(out));
 
198
        }
 
199
    } else {
 
200
        while (vstring_fgets(in, VSTREAM_IN)) {
 
201
            escape(out, vstring_str(in), VSTRING_LEN(in));
 
202
            vstream_fwrite(VSTREAM_OUT, vstring_str(out), VSTRING_LEN(out));
 
203
        }
136
204
    }
137
205
    vstream_fflush(VSTREAM_OUT);
138
206
    exit(0);