1
2
3
4
5
6
7#include <linux/kernel.h>
8#include <linux/string.h>
9#include <linux/ctype.h>
10#include <asm/setup.h>
11
12static inline int myisspace(u8 c)
13{
14 return c <= ' ';
15}
16
17
18
19
20
21
22
23
24
25
26
27
28static int
29__cmdline_find_option_bool(const char *cmdline, int max_cmdline_size,
30 const char *option)
31{
32 char c;
33 int pos = 0, wstart = 0;
34 const char *opptr = NULL;
35 enum {
36 st_wordstart = 0,
37 st_wordcmp,
38 st_wordskip,
39 } state = st_wordstart;
40
41 if (!cmdline)
42 return -1;
43
44
45
46
47
48 while (pos < max_cmdline_size) {
49 c = *(char *)cmdline++;
50 pos++;
51
52 switch (state) {
53 case st_wordstart:
54 if (!c)
55 return 0;
56 else if (myisspace(c))
57 break;
58
59 state = st_wordcmp;
60 opptr = option;
61 wstart = pos;
62
63
64 case st_wordcmp:
65 if (!*opptr) {
66
67
68
69
70
71
72 if (!c || myisspace(c))
73 return wstart;
74
75
76
77
78
79 } else if (!c) {
80
81
82
83
84 return 0;
85 } else if (c == *opptr++) {
86
87
88
89
90 break;
91 }
92 state = st_wordskip;
93
94
95 case st_wordskip:
96 if (!c)
97 return 0;
98 else if (myisspace(c))
99 state = st_wordstart;
100 break;
101 }
102 }
103
104 return 0;
105}
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121static int
122__cmdline_find_option(const char *cmdline, int max_cmdline_size,
123 const char *option, char *buffer, int bufsize)
124{
125 char c;
126 int pos = 0, len = -1;
127 const char *opptr = NULL;
128 char *bufptr = buffer;
129 enum {
130 st_wordstart = 0,
131 st_wordcmp,
132 st_wordskip,
133 st_bufcpy,
134 } state = st_wordstart;
135
136 if (!cmdline)
137 return -1;
138
139
140
141
142
143 while (pos++ < max_cmdline_size) {
144 c = *(char *)cmdline++;
145 if (!c)
146 break;
147
148 switch (state) {
149 case st_wordstart:
150 if (myisspace(c))
151 break;
152
153 state = st_wordcmp;
154 opptr = option;
155
156
157 case st_wordcmp:
158 if ((c == '=') && !*opptr) {
159
160
161
162
163
164 len = 0;
165 bufptr = buffer;
166 state = st_bufcpy;
167 break;
168 } else if (c == *opptr++) {
169
170
171
172
173 break;
174 }
175 state = st_wordskip;
176
177
178 case st_wordskip:
179 if (myisspace(c))
180 state = st_wordstart;
181 break;
182
183 case st_bufcpy:
184 if (myisspace(c)) {
185 state = st_wordstart;
186 } else {
187
188
189
190
191
192 if (++len < bufsize)
193 *bufptr++ = c;
194 }
195 break;
196 }
197 }
198
199 if (bufsize)
200 *bufptr = '\0';
201
202 return len;
203}
204
205int cmdline_find_option_bool(const char *cmdline, const char *option)
206{
207 return __cmdline_find_option_bool(cmdline, COMMAND_LINE_SIZE, option);
208}
209
210int cmdline_find_option(const char *cmdline, const char *option, char *buffer,
211 int bufsize)
212{
213 return __cmdline_find_option(cmdline, COMMAND_LINE_SIZE, option,
214 buffer, bufsize);
215}
216