When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
25 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
Just noticed discord messed with the asterisks here is a text file.
!f
How would I fix the parsing? I think the problem lies in handle_command
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#define MAX_LINE_LENGTH 512
#define MAX_ARGS 64
#define MAX_COMMANDS 64
void parse_command(char* line, char** argv) {
static char delimiter[] = " \n\t";
char* token = strtok(line, delimiter);
while (token != NULL) {
*argv++ = token;
token = strtok(NULL, delimiter);
}
*argv = NULL;
}
void execute_command(char** argv) {
pid_t pid = fork();
int status;
if (pid == -1) {
perror("forking child process failed");
exit(1);
} else if (pid == 0) {
if (execvp(*argv, argv) == -1) {
perror("exec failed");
exit(1);
}
} else {
waitpid(pid, &status, 0);
}
}
void handle_command(char* line) {
char* argv[MAX_ARGS];
int i = 0;
while (isspace(line[i]))
i++;
if (strcmp(line + i, "\n") == 0)
return;
parse_command(line + i, argv);
if (argv[0] == NULL)
return;
if (strcmp(argv[0], "quit") == 0) {
exit(0);
}
execute_command(argv);
}
void handle_batch_file(FILE* fp) {
char line[MAX_LINE_LENGTH];
while (fgets(line, MAX_LINE_LENGTH, fp) != NULL) {
handle_command(line);
}
}
int main(int argc, char* argv[]) {
if (argc == 1) {
char line[MAX_LINE_LENGTH];
printf("myshell> ");
while (fgets(line, MAX_LINE_LENGTH, stdin) != NULL) {
handle_command(line);
printf("myshell> ");
}
} else if (argc == 2) {
FILE* fp = fopen(argv[1], "r");
if (fp == NULL) {
perror("error opening file");
exit(1);
}
handle_batch_file(fp);
fclose(fp);
} else {
printf("Usage: myshell [batch_file_name]\n");
exit(1);
}
return 0;
}
!code
```cpp
int main() {}
```
int main() {}
@torpid saddle Where in your code do you try to handle ;?
That was my mistake in the image, I have to handle that later I still get the same error when just using space
; would later be in parse command
What do you want cat file cat file to do?
Run cat file twice
The typical expected behavior of syntax like that is to pass the arguments file, cat, and file to an invocation of cat
anything else is highly weird
In our class, my professor just invokes it from his ubuntu automatically, thats why I got so confused in not having a library in which I would already have the cat function
hence the error where its trying to take the second cat as an argument
What do you want a command to look like? Do you only ever allow a single argument?
Yeah, I would only ever use a single argument case, the example tests that he wants us to run are as follows prompt>
prompt> ls
prompt>/bin/ls
prompt> ls -l
prompt> ls -l ; cat file
prompt> ls -l ; cat file ; grep foo file2
Well I guess grep would take 2 arguments
Yeah
There are other test cases: prompt> ; cat file ;grep foo file2
prompt> cat file ; ; grep foo file2
prompt> cat file ; ls -l ;
prompt> cat file ;;;; ls -l
prompt> ;; ls -l prompt> ;
Right now your parse_command function is specifically designed to parse out multiple arguments
I'm guessing you didn't write that function
He wrote it, for his example program but I don't really know how I would go about separating the commands with ";" and handle multiple white spaces
This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.
This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.