summaryrefslogtreecommitdiff
path: root/src/misc.c
blob: 6a94aa1f2a0eae2b18568f44d1f09dde31dc2226 (plain)
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * Copyright (C) 1997-2009, Michael Jennings
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

static const char cvs_ident[] = "$Id$";

#include "config.h"
#include "feature.h"

#include <ctype.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include "command.h"
#include "startup.h"
#include "misc.h"
#include "options.h"

const char *
my_basename(const char *str)
{
    const char *base = strrchr(str, '/');

    return (base ? base + 1 : str);
}

/*
 * Compares the first n characters of s1 and s2, where n is strlen(s2)
 * Returns strlen(s2) if they match, 0 if not.
 */
unsigned long
str_leading_match(register const char *s1, register const char *s2)
{
    register unsigned long n;

    if (!s1 || !s2) {
        return (0);
    }
    for (n = 0; *s2; n++, s1++, s2++) {
        if (*s1 != *s2) {
            return 0;
        }
    }

    return n;
}

/* Strip leading and trailing whitespace and quotes from a string */
char *
str_trim(char *str)
{

    register spif_charptr_t tmp = (spif_charptr_t) str;
    size_t n;

    if (str && *str) {

        spiftool_chomp(str);
        n = strlen(str);

        if (!n) {
            *str = 0;
            return str;
        }
        /* strip leading/trailing quotes */
        if (*tmp == '"') {
            tmp++;
            n--;
            if (!n) {
                *str = 0;
                return str;
            } else if (tmp[n - 1] == '"') {
                tmp[--n] = '\0';
            }
        }
        if (tmp != str) {
            memmove(str, tmp, (strlen(tmp)) + 1);
        }
    }
    return str;
}

/*
 * in-place interpretation of string:
 *
 *      backslash-escaped:      "\a\b\E\e\n\r\t", "\octal"
 *      Ctrl chars:     ^@ .. ^_, ^?
 *
 *      Emacs-style:    "M-" prefix
 *
 * Also,
 *      "M-x" prefixed strings, append "\r" if needed
 *      "\E]" prefixed strings (XTerm escape sequence) append "\a" if needed
 *
 * returns the converted string length
 */

int
parse_escaped_string(char *str)
{

    register char *pold = str, *pnew;
    unsigned char i;

    D_STRINGS(("parse_escaped_string(\"%s\")\n", str));

    if (!BEG_STRCASECMP(pold, "m-")) {
        *pold = '\\';
        *(pold + 1) = 'e';
    }
    for (pold = pnew = str; *pold; pold++, pnew++) {
        D_STRINGS(("Looking at \"%s\"\n", pold));
        if (!BEG_STRCASECMP(pold, "m-") && (isspace(*(pold - 1)) || !isprint(*(pold - 1)))) {
            *pold = '\\';
            *(pold + 1) = 'e';
        } else if (!BEG_STRCASECMP(pold, "c-")) {
            *(++pold) = '^';
        }
        D_STRINGS(("Operating on \'%c\'\n", *pold));
        switch (*pold) {
            case '\\':
                D_STRINGS(("Backslash + %c\n", *(pold + 1)));
                switch (tolower(*(++pold))) {
                    case '0':
                    case '1':
                    case '2':
                    case '3':
                    case '4':
                    case '5':
                    case '6':
                    case '7':
                        for (i = 0; *pold >= '0' && *pold <= '7'; pold++) {
                            i = (i * 8) + (*pold - '0');
                        }
                        pold--;
                        D_STRINGS(("Octal number evaluates to %d\n", i));
                        *pnew = i;
                        break;
                    case 'n':
                        *pnew = '\n';
                        break;
                    case 'r':
                        *pnew = '\r';
                        break;
                    case 't':
                        *pnew = '\t';
                        break;
                    case 'b':
                        *pnew = '\b';
                        break;
                    case 'f':
                        *pnew = '\f';
                        break;
                    case 'a':
                        *pnew = '\a';
                        break;
                    case 'v':
                        *pnew = '\v';
                        break;
                    case 'e':
                        *pnew = '\033';
                        break;
                    case 'c':
                        pold++;
                        *pnew = MAKE_CTRL_CHAR(*pold);
                        break;
                    default:
                        *pnew = *pold;
                        break;
                }
                break;
            case '^':
                D_STRINGS(("Caret + %c\n", *(pold + 1)));
                pold++;
                *pnew = MAKE_CTRL_CHAR(*pold);
                break;
            default:
                *pnew = *pold;
        }
    }

    if (!BEG_STRCASECMP(str, "\033x") && *(pnew - 1) != '\r') {
        D_STRINGS(("Adding carriage return\n"));
        *(pnew++) = '\r';
    } else if (!BEG_STRCASECMP(str, "\033]") && *(pnew - 1) != '\a') {
        D_STRINGS(("Adding bell character\n"));
        *(pnew++) = '\a';
    }
    *pnew = 0;

#if DEBUG >= DEBUG_STRINGS
    if (DEBUG_LEVEL >= DEBUG_STRINGS) {
        D_STRINGS(("New value is:\n"));
        spiftool_hex_dump(str, (size_t) (pnew - str));
    }
#endif

    return (pnew - str);
}

spif_charptr_t
escape_string(spif_charptr_t str, spif_char_t quote, spif_int32_t maxlen)
{
    spif_charptr_t buff, s = str, pbuff;

    D_STRINGS(("escape_string(%s %c %ld)\n", (char *) str, quote, maxlen));
    if (!quote) {
        quote = '\"';
    }

    /* The escaped string will be at most twice the length of the original. */
    buff = (spif_charptr_t) MALLOC(strlen((char *) str) * 2 + 1);

    /* Copy and escape the string from str into buff. */
    for (pbuff = buff; (*s); s++, pbuff++) {
        if (*s == quote) {
            D_STRINGS(("Double-escaping \'%c\' at position %d\n", *s, s - str));
            *pbuff = '\\';
            pbuff++;
            *pbuff = '\\';
            pbuff++;
        } else {
            if (quote == '\"') {
                if ((*s == '\\') || (*s == '`')) {
                    D_STRINGS(("Escaping \'%c\' at position %d\n", *s, s - str));
                    *pbuff = '\\';
                    pbuff++;
                }
            }
        }
        D_STRINGS(("Copying \'%c\' at position %d\n", *s, s - str));
        *pbuff = *s;
    }
    *pbuff = 0;

    if (maxlen) {
        /* Given maxlen, we know "str" can hold at least "maxlen" chars. */
        if (!spiftool_safe_strncpy(str, buff, maxlen)) {
            str[maxlen] = 0;
        }
        FREE(buff);
        return str;
    } else {
        return buff;
    }
}

char *
safe_print_string(const char *str, unsigned long len)
{
    static char *ret_buff = NULL;
    static unsigned long rb_size = 0;
    char *p;
    unsigned long n = 0, i;

    if (len == ((unsigned long) -1)) {
        len = strlen(str);
    } else if (len == ((unsigned long) -2)) {
        FREE(ret_buff);
        rb_size = 0;
        return ((char *) NULL);
    }
    if (!ret_buff) {
        rb_size = len;
        ret_buff = (char *) MALLOC(rb_size + 1);
    } else if (len > rb_size) {
        rb_size = len;
        ret_buff = (char *) REALLOC(ret_buff, rb_size + 1);
    }
    for (i = 0, p = ret_buff; i < len; i++, str++, n++) {
        if (n + 2 >= rb_size) {
            rb_size *= 2;
            ret_buff = (char *) REALLOC(ret_buff, rb_size + 1);
            p = ret_buff + n;
        }
        if ((unsigned char) *str < ' ') {
            *p++ = '^';
            *p++ = *str + '@';
            n++;
        } else {
            *p++ = *str;
        }
    }
    *p = 0;
    return ret_buff;
}

unsigned long
add_carriage_returns(unsigned char *buff, unsigned long cnt)
{
    register unsigned char *out, *outp, *in;
    register unsigned long i;

    D_CMD(("buff == %8p \"%s\", cnt == %lu\n", buff, safe_print_string((spif_charptr_t) buff, cnt), cnt));
    outp = out = (unsigned char *) MALLOC(cnt * 2);
    for (i = 0, in = buff; i < cnt; i++) {
        if (*in == '\n') {
            *out++ = '\r';
        }
        *out++ = *in++;
    }
    i = (unsigned long) (out - outp);
    memcpy(buff, outp, i);
    FREE(outp);
    D_CMD(("buff == %8p \"%s\", i == %lu\n", buff, safe_print_string((spif_charptr_t) buff, i), i));
    return i;
}

unsigned char
mkdirhier(const char *path)
{
    spif_charptr_t str, pstr;
    struct stat dst;

    D_CMD(("path == %s\n", path));
    str = STRDUP(path);         /* We need to modify it. */
    pstr = str;
    if (*pstr == '/') {
        pstr++;
    }
    for (; (pstr = strchr(pstr, '/'));) {
        *pstr = 0;
        D_CMD(("Looking at \"%s\"\n", str));
        if (stat(str, &dst)) {
            /* It's not there.  Create it. */
            D_CMD(("stat() failed.  Attempting to create it.\n"));
            if (mkdir(str, 0755)) {
                /* Couldn't create it.  Return failure. */
                D_CMD(("mkdir(%s, 0755) failed -- %s\n", str, strerror(errno)));
                return 0;
            }
        } else if (!S_ISDIR(dst.st_mode)) {
            /* It's there, but it's not a directory.  Fail. */
            D_CMD(("\"%s\" exists, but it's not a directory.\n", str));
            return 0;
        }
        *pstr++ = '/';
    }
    D_CMD(("Looking at \"%s\"\n", str));
    if (stat(str, &dst)) {
        /* It's not there.  Create it. */
        D_CMD(("stat() failed.  Attempting to create it.\n"));
        if (mkdir(str, 0755)) {
            /* Couldn't create it.  Return failure. */
            D_CMD(("mkdir(%s, 0755) failed -- %s\n", str, strerror(errno)));
            return 0;
        }
    } else if (!S_ISDIR(dst.st_mode)) {
        /* It's there, but it's not a directory.  Fail. */
        D_CMD(("\"%s\" exists, but it's not a directory.\n", str));
        return 0;
    }
    D_CMD(("Done!\n"));
    return 1;
}