summaryrefslogtreecommitdiff
path: root/kern/arg.c
blob: 7bb616a4b4d4dd2f58a2df0a44c811498379038c (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
/*
 * Copyright (c) 2017 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>

#include <kern/arg.h>
#include <kern/init.h>
#include <kern/log.h>
#include <kern/macros.h>
#include <kern/panic.h>

/*
 * Internally, all space characters are turned into null bytes so that
 * values returned to callers directly point in the command line string,
 * with no need for dynamic allocation.
 */
static char arg_cmdline[ARG_CMDLINE_MAX_SIZE] __initdata;
static const char *arg_cmdline_end __initdata;

void __init
arg_set_cmdline(const char *cmdline)
{
    strlcpy(arg_cmdline, cmdline, sizeof(arg_cmdline));
}

static int __init
arg_setup(void)
{
    size_t i, length;

    length = strlen(arg_cmdline);

    for (i = 0; i < length; i++) {
        if (arg_cmdline[i] == ' ') {
            arg_cmdline[i] = '\0';
        }
    }

    arg_cmdline_end = arg_cmdline + length;
    return 0;
}

INIT_OP_DEFINE(arg_setup);

void __init
arg_log_info(void)
{
    char cmdline[sizeof(arg_cmdline)];
    size_t i;

    for (i = 0; &arg_cmdline[i] < arg_cmdline_end; i++) {
        cmdline[i] = (arg_cmdline[i] == '\0') ? ' ' : arg_cmdline[i];
    }

    cmdline[i] = '\0';
    log_info("arg: %s", cmdline);
}

static const char * __init
arg_walk(const char *s)
{
    if (s == NULL) {
        s = arg_cmdline;
    } else {
        for (;;) {
            if (s >= arg_cmdline_end) {
                return NULL;
            }

            if (*s == '\0') {
                break;
            }

            s++;
        }
    }

    for (;;) {
        if (s >= arg_cmdline_end) {
            return NULL;
        }

        if (*s != '\0') {
            return s;
        }

        s++;
    }
}

static const char * __init
arg_find_name_end(const char *arg)
{
    const char *end;

    end = strchr(arg, '=');

    if (end == NULL) {
        end = arg + strlen(arg);
    }

    return end;
}

static const char * __init
arg_find(const char *name)
{
    const char *arg, *arg_name_end;
    size_t arg_name_length, name_length;

    name_length = strlen(name);
    assert(name_length != 0);

    for (arg = arg_walk(NULL); arg != NULL; arg = arg_walk(arg)) {
        arg_name_end = arg_find_name_end(arg);
        arg_name_length = arg_name_end - arg;

        if ((arg_name_length == name_length)
            && (memcmp(arg, name, name_length) == 0)) {
            return arg;
        }
    }

    return NULL;
}

bool __init
arg_present(const char *name)
{
    return (arg_find(name) != NULL);
}

const char * __init
arg_value(const char *name)
{
    const char *arg, *value;

    arg = arg_find(name);

    if (arg == NULL) {
        return NULL;
    }

    value = strchr(arg, '=');

    if (value == NULL) {
        return "";
    }

    return value + 1;
}