summaryrefslogtreecommitdiffstats
path: root/src/util/util.c
blob: b3498df6cd84b9b1834ef599e3aa09a40efbe600 (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
// SPDX-License-Identifier: GPL-2.0

// Parts are copied from the linux kernel

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

// CODE FROM LINUX KERNEL START

#define _U  0x01                /* upper */
#define _L  0x02                /* lower */
#define _D  0x04                /* digit */
#define _C  0x08                /* cntrl */
#define _P  0x10                /* punct */
#define _S  0x20                /* white space (space/lf/tab) */
#define _X  0x40                /* hex digit */
#define _SP 0x80                /* hard space (0x20) */

const unsigned char _ctype[] = {
        _C, _C, _C, _C, _C, _C, _C, _C, /* 0-7 */
        _C, _C | _S, _C | _S, _C | _S, _C | _S, _C | _S, _C, _C,        /* 8-15 */
        _C, _C, _C, _C, _C, _C, _C, _C, /* 16-23 */
        _C, _C, _C, _C, _C, _C, _C, _C, /* 24-31 */
        _S | _SP, _P, _P, _P, _P, _P, _P, _P,   /* 32-39 */
        _P, _P, _P, _P, _P, _P, _P, _P, /* 40-47 */
        _D, _D, _D, _D, _D, _D, _D, _D, /* 48-55 */
        _D, _D, _P, _P, _P, _P, _P, _P, /* 56-63 */
        _P, _U | _X, _U | _X, _U | _X, _U | _X, _U | _X, _U | _X, _U,   /* 64-71 */
        _U, _U, _U, _U, _U, _U, _U, _U, /* 72-79 */
        _U, _U, _U, _U, _U, _U, _U, _U, /* 80-87 */
        _U, _U, _U, _P, _P, _P, _P, _P, /* 88-95 */
        _P, _L | _X, _L | _X, _L | _X, _L | _X, _L | _X, _L | _X, _L,   /* 96-103 */
        _L, _L, _L, _L, _L, _L, _L, _L, /* 104-111 */
        _L, _L, _L, _L, _L, _L, _L, _L, /* 112-119 */
        _L, _L, _L, _P, _P, _P, _P, _C, /* 120-127 */
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 128-143 */
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 144-159 */
        _S | _SP, _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, _P,   /* 160-175 */
        _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, /* 176-191 */
        _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, /* 192-207 */
        _U, _U, _U, _U, _U, _U, _U, _P, _U, _U, _U, _U, _U, _U, _U, _L, /* 208-223 */
        _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, /* 224-239 */
        _L, _L, _L, _L, _L, _L, _L, _P, _L, _L, _L, _L, _L, _L, _L, _L  /* 240-255 */
};

#define __ismask(x) (_ctype[(int)(unsigned char)(x)])

#define kernel_isspace(c)  ((__ismask(c)&(_S)) != 0)

static char *skip_spaces(const char *str)
{
        while (kernel_isspace(*str))
                ++str;
        return (char *)str;
}

/*
 * Parse a string to get a param value pair.
 * You can use " around spaces, but can't escape ".
 * Hyphens and underscores equivalent in parameter names.
 */
static char *next_arg(char *args, char **param, char **val)
{
        unsigned int i, equals = 0;
        int in_quote = 0, quoted = 0;
        char *next;

        if (*args == '"') {
                args++;
                in_quote = 1;
                quoted = 1;
        }

        for (i = 0; args[i]; i++) {
                if (kernel_isspace(args[i]) && !in_quote)
                        break;
                if (equals == 0) {
                        if (args[i] == '=')
                                equals = i;
                }
                if (args[i] == '"')
                        in_quote = !in_quote;
        }

        *param = args;
        if (!equals)
                *val = NULL;
        else {
                args[equals] = '\0';
                *val = args + equals + 1;

                /* Don't include quotes in value. */
                if (**val == '"') {
                        (*val)++;
                        if (args[i - 1] == '"')
                                args[i - 1] = '\0';
                }
        }
        if (quoted && args[i - 1] == '"')
                args[i - 1] = '\0';

        if (args[i]) {
                args[i] = '\0';
                next = args + i + 1;
        } else
                next = args + i;

        /* Chew up trailing spaces. */
        return skip_spaces(next);
}

// CODE FROM LINUX KERNEL STOP

enum EXEC_MODE {
        UNDEFINED,
        GETARG,
        GETARGS,
};

static void usage(enum EXEC_MODE enumExecMode, int ret, char *msg)
{
        switch (enumExecMode) {
        case UNDEFINED:
                fprintf(stderr, "ERROR: 'dracut-util' has to be called via a symlink to the tool name.\n");
                break;
        case GETARG:
                fprintf(stderr, "ERROR: %s\nUsage: dracut-getarg <KEY>[=[<VALUE>]]\n", msg);
                break;
        case GETARGS:
                fprintf(stderr, "ERROR: %s\nUsage: dracut-getargs <KEY>[=]\n", msg);
                break;
        }
        exit(ret);
}

#define ARGV0_GETARG "dracut-getarg"
#define ARGV0_GETARGS "dracut-getargs"

static enum EXEC_MODE get_mode(const char *argv_0)
{
        struct _mode_table {
                enum EXEC_MODE mode;
                const char *arg;
                size_t arg_len;
                const char *s_arg;
        } modeTable[] = {
                {GETARG, ARGV0_GETARG, sizeof(ARGV0_GETARG), "/" ARGV0_GETARG},
                {GETARGS, ARGV0_GETARGS, sizeof(ARGV0_GETARGS), "/" ARGV0_GETARGS},
                {UNDEFINED, NULL, 0, NULL}
        };
        int i;

        size_t argv_0_len = strlen(argv_0);

        if (!argv_0_len)
                return UNDEFINED;

        for (i = 0; modeTable[i].mode != UNDEFINED; i++) {
                if (argv_0_len == (modeTable[i].arg_len - 1)) {
                        if (strncmp(argv_0, modeTable[i].arg, argv_0_len) == 0) {
                                return modeTable[i].mode;
                        }
                }

                if (modeTable[i].arg_len > argv_0_len)
                        continue;

                if (strncmp(argv_0 + argv_0_len - modeTable[i].arg_len, modeTable[i].s_arg, modeTable[i].arg_len) == 0)
                        return modeTable[i].mode;
        }
        return UNDEFINED;
}

static int getarg(int argc, char **argv)
{
        char *search_key;
        char *search_value;
        char *end_value = NULL;
        bool bool_value = false;
        char *cmdline = NULL;

        char *p = getenv("CMDLINE");
        if (p == NULL) {
                usage(GETARG, EXIT_FAILURE, "CMDLINE env not set");
        }
        cmdline = strdup(p);

        if (argc != 2) {
                usage(GETARG, EXIT_FAILURE, "Number of arguments invalid");
        }

        search_key = argv[1];

        search_value = strchr(argv[1], '=');
        if (search_value != NULL) {
                *search_value = 0;
                search_value++;
                if (*search_value == 0)
                        search_value = NULL;
        }

        if (strlen(search_key) == 0)
                usage(GETARG, EXIT_FAILURE, "search key undefined");

        do {
                char *key = NULL, *value = NULL;
                cmdline = next_arg(cmdline, &key, &value);
                if (strcmp(key, search_key) == 0) {
                        if (value) {
                                end_value = value;
                                bool_value = -1;
                        } else {
                                end_value = NULL;
                                bool_value = true;
                        }
                }
        } while (cmdline[0]);

        if (search_value) {
                if (end_value && strcmp(end_value, search_value) == 0) {
                        return EXIT_SUCCESS;
                }
                return EXIT_FAILURE;
        }

        if (end_value) {
                // includes "=0"
                puts(end_value);
                return EXIT_SUCCESS;
        }

        if (bool_value) {
                return EXIT_SUCCESS;
        }

        return EXIT_FAILURE;
}

static int getargs(int argc, char **argv)
{
        char *search_key;
        char *search_value;
        bool found_value = false;
        char *cmdline = NULL;

        char *p = getenv("CMDLINE");
        if (p == NULL) {
                usage(GETARGS, EXIT_FAILURE, "CMDLINE env not set");
        }
        cmdline = strdup(p);

        if (argc != 2) {
                usage(GETARGS, EXIT_FAILURE, "Number of arguments invalid");
        }

        search_key = argv[1];

        search_value = strchr(argv[1], '=');
        if (search_value != NULL) {
                *search_value = 0;
                search_value++;
                if (*search_value == 0)
                        search_value = NULL;
        }

        if (strlen(search_key) == 0)
                usage(GETARGS, EXIT_FAILURE, "search key undefined");

        do {
                char *key = NULL, *value = NULL;
                cmdline = next_arg(cmdline, &key, &value);
                if (strcmp(key, search_key) == 0) {
                        if (search_value) {
                                if (strcmp(value, search_value) == 0) {
                                        printf("%s\n", value);
                                        found_value = true;
                                }
                        } else {
                                if (value) {
                                        printf("%s\n", value);
                                } else {
                                        puts(key);
                                }
                                found_value = true;
                        }
                }
        } while (cmdline[0]);
        return found_value ? EXIT_SUCCESS : EXIT_FAILURE;
}

int main(int argc, char **argv)
{
        switch (get_mode(argv[0])) {
        case UNDEFINED:
                usage(UNDEFINED, EXIT_FAILURE, NULL);
                break;
        case GETARG:
                return getarg(argc, argv);
        case GETARGS:
                return getargs(argc, argv);
        }

        return EXIT_FAILURE;
}