@@ -13,7 +13,6 @@ fn shellLoop(stdin: std.fs.File.Reader, stdout: std.fs.File.Writer) !void {
1313 while (true ) {
1414 const max_input = 1024 ;
1515 const max_args = 10 ;
16- const max_arg_size = 255 ;
1716
1817 // Prompt
1918 try stdout .print ("> " , .{});
@@ -31,22 +30,21 @@ fn shellLoop(stdin: std.fs.File.Reader, stdout: std.fs.File.Writer) !void {
3130
3231 // The command and arguments are null-terminated strings. These arrays are
3332 // storage for the strings and pointers to those strings.
34- var args : [max_args ][max_arg_size :0 ]u8 = undefined ;
3533 var args_ptrs : [max_args :null ]? [* :0 ]u8 = undefined ;
3634
37- // Split by a single space. The returned SplitIterator must be var because
38- // it has mutable internal state.
39- var tokens = std .mem .split (u8 , input_str , " " );
40-
41- // Copy each string "token" into the storage array and save a pointer to it.
35+ // Split by a single space. Turn spaces and the final LF into null bytes
4236 var i : usize = 0 ;
43- while (tokens .next ()) | tok | {
44- std .mem .copy (u8 , & args [i ], tok );
45- args [i ][tok .len ] = 0 ; // add sentinel 0
46- args_ptrs [i ] = & args [i ];
47- i += 1 ;
37+ var n : usize = 0 ;
38+ var ofs : usize = 0 ;
39+ while (i <= input_str .len ) : (i += 1 ) {
40+ if (input_buffer [i ] == 0x20 or input_buffer [i ] == 0xa ) {
41+ input_buffer [i ] = 0 ; // turn space or line feed into null byte as sentinel
42+ args_ptrs [n ] = @ptrCast (* align (1 ) const [* :0 ]u8 , & input_buffer [ofs .. i :0 ]).* ;
43+ n += 1 ;
44+ ofs = i + 1 ;
45+ }
4846 }
49- args_ptrs [i ] = null ; // add sentinel null
47+ args_ptrs [n ] = null ; // add sentinel null
5048
5149 // After calling fork(), TWO processes will continue running this
5250 // code! One is the parent, and the other is the new child.
0 commit comments