Skip to content

Commit 47e1822

Browse files
cmdline: turn parse_cmdline into a constructor
And add another to read directly from a file. Signed-off-by: Michael Olbrich <m.olbrich@pengutronix.de>
1 parent 6a9d0e5 commit 47e1822

File tree

2 files changed

+61
-53
lines changed

2 files changed

+61
-53
lines changed

src/cmdline.rs

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -91,61 +91,70 @@ fn parse_nfsroot(options: &mut CmdlineOptions) -> Result<()> {
9191
Ok(())
9292
}
9393

94-
pub fn parse_cmdline(cmdline: &str) -> Result<CmdlineOptions> {
95-
let mut options = CmdlineOptions::default();
96-
let mut have_value = false;
97-
let mut quoted = false;
98-
let mut key = &cmdline[0..0];
99-
let mut start = 0;
100-
101-
for (i, c) in cmdline.char_indices() {
102-
let mut skip = false;
103-
match c {
104-
'=' => {
105-
if !have_value {
106-
skip = true;
107-
key = &cmdline[start..i];
108-
start = i;
109-
}
110-
have_value = true;
111-
}
112-
'"' => {
113-
quoted = !quoted;
114-
skip = true;
115-
}
116-
' ' | '\n' => {
117-
if !quoted {
94+
impl CmdlineOptions {
95+
pub fn from_string(cmdline: &str) -> Result<Self> {
96+
let mut options = Self::default();
97+
let mut have_value = false;
98+
let mut quoted = false;
99+
let mut key = &cmdline[0..0];
100+
let mut start = 0;
101+
102+
for (i, c) in cmdline.char_indices() {
103+
let mut skip = false;
104+
match c {
105+
'=' => {
118106
if !have_value {
107+
skip = true;
119108
key = &cmdline[start..i];
109+
start = i;
120110
}
121-
if !key.is_empty() {
122-
parse_option(
123-
key,
124-
if have_value {
125-
Some(&cmdline[start..i])
126-
} else {
127-
None
128-
},
129-
&mut options,
130-
)?;
131-
}
132-
key = &cmdline[0..0];
133-
have_value = false;
111+
have_value = true;
112+
}
113+
'"' => {
114+
quoted = !quoted;
134115
skip = true;
135116
}
117+
' ' | '\n' => {
118+
if !quoted {
119+
if !have_value {
120+
key = &cmdline[start..i];
121+
}
122+
if !key.is_empty() {
123+
parse_option(
124+
key,
125+
if have_value {
126+
Some(&cmdline[start..i])
127+
} else {
128+
None
129+
},
130+
&mut options,
131+
)?;
132+
}
133+
key = &cmdline[0..0];
134+
have_value = false;
135+
skip = true;
136+
}
137+
}
138+
_ => {}
139+
}
140+
if skip {
141+
start = i + 1;
136142
}
137-
_ => {}
138143
}
139-
if skip {
140-
start = i + 1;
144+
145+
if options.root.as_deref() == Some("/dev/nfs")
146+
|| options.rootfstype.as_deref() == Some("nfs")
147+
{
148+
parse_nfsroot(&mut options)?;
141149
}
142-
}
143150

144-
if options.root.as_deref() == Some("/dev/nfs") || options.rootfstype.as_deref() == Some("nfs") {
145-
parse_nfsroot(&mut options)?;
151+
Ok(options)
146152
}
147153

148-
Ok(options)
154+
pub fn from_file(filename: &str) -> Result<Self> {
155+
let cmdline = read_file(filename)?;
156+
Self::from_string(&cmdline)
157+
}
149158
}
150159

151160
#[cfg(test)]
@@ -162,7 +171,7 @@ mod tests {
162171
..Default::default()
163172
};
164173

165-
let options = parse_cmdline(cmdline).expect("failed");
174+
let options = CmdlineOptions::from_string(cmdline).expect("failed");
166175

167176
assert_eq!(options, expected);
168177
}
@@ -180,7 +189,7 @@ mod tests {
180189
..Default::default()
181190
};
182191

183-
let options = parse_cmdline(cmdline).expect("failed");
192+
let options = CmdlineOptions::from_string(cmdline).expect("failed");
184193

185194
assert_eq!(options, expected);
186195
}
@@ -197,7 +206,7 @@ mod tests {
197206
..Default::default()
198207
};
199208

200-
let options = parse_cmdline(cmdline).expect("failed");
209+
let options = CmdlineOptions::from_string(cmdline).expect("failed");
201210

202211
assert_eq!(options, expected);
203212
}
@@ -217,7 +226,7 @@ mod tests {
217226
..Default::default()
218227
};
219228

220-
let options = parse_cmdline(cmdline).expect("failed");
229+
let options = CmdlineOptions::from_string(cmdline).expect("failed");
221230

222231
assert_eq!(options, expected);
223232
}
@@ -232,7 +241,7 @@ mod tests {
232241
..Default::default()
233242
};
234243

235-
let options = parse_cmdline(cmdline).expect("failed");
244+
let options = CmdlineOptions::from_string(cmdline).expect("failed");
236245

237246
assert_eq!(options, expected);
238247
}

src/init.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ use nix::sys::reboot::{reboot, RebootMode};
1919
use nix::sys::termios::tcdrain;
2020
use nix::unistd::{chdir, chroot, dup2_stderr, dup2_stdout, execv, unlink};
2121

22-
use crate::cmdline::{parse_cmdline, CmdlineOptions};
22+
use crate::cmdline::CmdlineOptions;
2323
#[cfg(feature = "dmverity")]
2424
use crate::dmverity::prepare_dmverity;
2525
use crate::mount::{mount_move_special, mount_root, mount_special};
2626
#[cfg(feature = "systemd")]
2727
use crate::systemd::mount_systemd;
2828
#[cfg(feature = "usb9pfs")]
2929
use crate::usbg_9pfs::prepare_9pfs_gadget;
30-
use crate::util::{read_file, Result};
30+
use crate::util::Result;
3131

3232
/*
3333
* Setup stdout/stderr. The kernel will create /dev/console in the
@@ -109,8 +109,7 @@ impl InitContext {
109109

110110
setup_log()?;
111111

112-
let cmdline = read_file("/proc/cmdline")?;
113-
self.options = parse_cmdline(&cmdline)?;
112+
self.options = CmdlineOptions::from_file("/proc/cmdline")?;
114113

115114
Ok(())
116115
}

0 commit comments

Comments
 (0)