feat(command): commands are executed

This commit is contained in:
Ahurac 2024-10-05 20:22:53 +02:00
parent 6672cb3bf8
commit e5aa953c04
4 changed files with 31 additions and 13 deletions

17
src/command.c Normal file
View file

@ -0,0 +1,17 @@
#include "command.h"
#include <sys/wait.h>
#include <unistd.h>
#include "parse.h"
unsigned char invoke(struct Args args) {
pid_t child = fork();
if (child == 0 && args.argc > 0) {
execvp(args.argv[0], args.argv);
return 1;
}
waitpid(child, NULL, 0);
return 0;
}

8
src/command.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef COMMAND_H
#define COMMAND_H
#include "parse.h"
unsigned char invoke(struct Args args);
#endif /* COMMAND_H */

View file

@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "command.h"
#include "parse.h" #include "parse.h"
#include "reader.h" #include "reader.h"
@ -14,19 +15,9 @@ int main() {
return 0; return 0;
} }
printf("command: '%s'\n", input_line);
struct Args args = parse(input_line); struct Args args = parse(input_line);
unsigned int i = 0;
while (i < args.argc) {
if (i != 0) {
printf(" ");
}
printf("%s", args.argv[i]);
i++;
}
printf("\n");
free(input_line); free(input_line);
invoke(args);
} }
} }

View file

@ -27,7 +27,9 @@ void add_arg(struct Args *args, char *arg_ptr, unsigned int arg_length) {
void parse_worker(struct Args *args, char *arg_position, void parse_worker(struct Args *args, char *arg_position,
unsigned int arg_length, char *line) { unsigned int arg_length, char *line) {
if (line[0] == '\n') { if (line[0] == '\n') {
add_arg(args, arg_position, arg_length); if (arg_length != 0) {
add_arg(args, arg_position, arg_length);
}
return; return;
} else if (isspace(line[0])) { } else if (isspace(line[0])) {
add_arg(args, arg_position, arg_length); add_arg(args, arg_position, arg_length);