This commit is contained in:
p-w-rs
2022-09-15 14:40:38 -05:00
parent 59e3d32e38
commit 742bdd74d2

View File

@@ -23,14 +23,16 @@
int read_cmd_string(char dest[INPUT_MAX]) int read_cmd_string(char dest[INPUT_MAX])
{ {
// Read user input // Read user input
if(fgets(dest, INPUT_MAX, stdin) == NULL) { if (fgets(dest, INPUT_MAX, stdin) == NULL)
{
fprintf(stderr, "Unable to read user input\n"); fprintf(stderr, "Unable to read user input\n");
return -1; return -1;
} }
// Remove trailing return character // Remove trailing return character
int len = strlen(dest); int len = strlen(dest);
if(dest[len-1] == '\n') { if (dest[len - 1] == '\n')
{
dest[len - 1] = '\0'; dest[len - 1] = '\0';
} }
@@ -48,9 +50,11 @@ int parse_commands(char input[INPUT_MAX], char cmd_strs[CMD_MAX][INPUT_MAX])
{ {
// Chop the input into command strings // Chop the input into command strings
int cmd_count = 0; int cmd_count = 0;
char* cmd_ptr = strtok(input, ";"); char *cmd_ptr = strtok(input, ";");
while(cmd_ptr) { while (cmd_ptr)
if(cmd_count >= CMD_MAX) { {
if (cmd_count >= CMD_MAX)
{
fprintf(stderr, "Too many commands\n"); fprintf(stderr, "Too many commands\n");
return -1; return -1;
} }
@@ -74,7 +78,8 @@ int main()
printf("$> "); printf("$> ");
// Read user input // Read user input
if(read_cmd_string(user_input) == -1) { if (read_cmd_string(user_input) == -1)
{
return 1; return 1;
} }
@@ -82,12 +87,14 @@ int main()
// Chop the input into command strings // Chop the input into command strings
int cmd_count = parse_commands(user_input, cmd_strs); int cmd_count = parse_commands(user_input, cmd_strs);
if(cmd_count == -1) { if (cmd_count == -1)
{
return 1; return 1;
} }
// Chop the commands into arguments and execute one at a time // Chop the commands into arguments and execute one at a time
for(int i = 0; i < cmd_count; i++) { for (int i = 0; i < cmd_count; i++)
{
// TODO: // TODO:
// 1) Chop the command into command line arguments // 1) Chop the command into command line arguments
@@ -95,7 +102,6 @@ int main()
// NOTE: the command name is always the first argument // NOTE: the command name is always the first argument
// 2) fork a process // 2) fork a process
// 3) execute the command with execvp // 3) execute the command with execvp
} }
return 0; return 0;