summaryrefslogtreecommitdiffstats
path: root/sys/boot/common/interp_backslash.c
blob: 3cbdd5b105ea5769590f4130e11f5bdaa772b435 (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
/*-
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * Jordan K. Hubbard
 * 29 August 1998
 *
 * Routine for doing backslash elimination.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <stand.h>
#include <string.h>
#include "bootstrap.h"

#define DIGIT(x) (isdigit(x) ? (x) - '0' : islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')

/*
 * backslash: Return malloc'd copy of str with all standard "backslash
 * processing" done on it.  Original can be free'd if desired.
 */
char *
backslash(char *str)
{
    /*
     * Remove backslashes from the strings. Turn \040 etc. into a single
     * character (we allow eight bit values). Currently NUL is not
     * allowed.
     *
     * Turn "\n" and "\t" into '\n' and '\t' characters. Etc.
     *
     */
    char *new_str;
    int seenbs = 0;
    int i = 0;

    if ((new_str = strdup(str)) == NULL)
	return NULL;

    while (*str) {
	if (seenbs) {
	    seenbs = 0;
	    switch (*str) {
	    case '\\':
		new_str[i++] = '\\';
		str++;
		break;

	    /* preserve backslashed quotes, dollar signs */
	    case '\'':
	    case '"':
	    case '$':
		new_str[i++] = '\\';
		new_str[i++] = *str++;
		break;

	    case 'b':
		new_str[i++] = '\b';
		str++;
		break;

	    case 'f':
		new_str[i++] = '\f';
		str++;
		break;

	    case 'r':
		new_str[i++] = '\r';
		str++;
		break;

	    case 'n':
		new_str[i++] = '\n';
		str++;
		break;

	    case 's':
		new_str[i++] = ' ';
		str++;
		break;

	    case 't':
		new_str[i++] = '\t';
		str++;
		break;

	    case 'v':
		new_str[i++] = '\13';
		str++;
		break;

	    case 'z':
		str++;
		break;

	    case '0': case '1': case '2': case '3': case '4':
	    case '5': case '6': case '7': case '8': case '9': {
		char val;

		/* Three digit octal constant? */
		if (*str >= '0' && *str <= '3' && 
		    *(str + 1) >= '0' && *(str + 1) <= '7' &&
		    *(str + 2) >= '0' && *(str + 2) <= '7') {

		    val = (DIGIT(*str) << 6) + (DIGIT(*(str + 1)) << 3) + 
			DIGIT(*(str + 2));

		    /* Allow null value if user really wants to shoot
                       at feet, but beware! */
		    new_str[i++] = val;
		    str += 3;
		    break;
		}

		/* One or two digit hex constant?
		 * If two are there they will both be taken.
		 * Use \z to split them up if this is not wanted.
		 */
		if (*str == '0' &&
		    (*(str + 1) == 'x' || *(str + 1) == 'X') &&
		    isxdigit(*(str + 2))) {
		    val = DIGIT(*(str + 2));
		    if (isxdigit(*(str + 3))) {
			val = (val << 4) + DIGIT(*(str + 3));
			str += 4;
		    }
		    else
			str += 3;
		    /* Yep, allow null value here too */
		    new_str[i++] = val;
		    break;
		}
	    }
	    break;

	    default:
		new_str[i++] = *str++;
		break;
	    }
	}
        else {
            if (*str == '\\') {
                seenbs = 1;
                str++;
            }
	    else
		new_str[i++] = *str++;
        }
    }

    if (seenbs) {
        /*
         * The final character was a '\'. Put it in as a single backslash.
         */
	new_str[i++] = '\\';
    }
    new_str[i] = '\0';
    return new_str;
}
OpenPOWER on IntegriCloud