Compare commits

...

3 commits

Author SHA1 Message Date
e5aa953c04 feat(command): commands are executed 2024-10-05 20:22:53 +02:00
6672cb3bf8 feat: parser 2024-10-01 22:00:40 +02:00
a3d156eb1f fix: delete commented code 2024-09-30 10:28:21 +02:00
6 changed files with 99 additions and 32 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,16 +1,11 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "command.h"
#include "parse.h"
#include "reader.h" #include "reader.h"
int main() { int main() {
/*
size_t buffer_size;
size_t initial_buffer_size = 64;
char *input_buffer;
char *initial_buffer_position;
unsigned int how_many_characters_to_read;
*/
char *input_line; char *input_line;
while (1) { while (1) {
@ -20,32 +15,9 @@ int main() {
return 0; return 0;
} }
printf("command: '%s'\n", input_line); struct Args args = parse(input_line);
free(input_line); free(input_line);
/*
buffer_size = initial_buffer_size;
how_many_characters_to_read = buffer_size + 1;
input_buffer = malloc(how_many_characters_to_read);
initial_buffer_position = input_buffer;
printf("shell> "); invoke(args);
while (fgets(input_buffer, how_many_characters_to_read, stdin) != NULL &&
input_buffer[strlen(input_buffer) - 1] != '\n') {
size_t new_buffer_size = buffer_size * 2;
if (buffer_size != initial_buffer_size) {
how_many_characters_to_read = buffer_size + 1;
}
input_buffer = realloc(initial_buffer_position, new_buffer_size + 1);
initial_buffer_position = input_buffer;
input_buffer += buffer_size;
buffer_size = new_buffer_size;
}
input_buffer = initial_buffer_position;
printf("%s", input_buffer);
free(input_buffer);
*/
} }
} }

53
src/parse.c Normal file
View file

@ -0,0 +1,53 @@
#include "parse.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define ARGV_INIT_SIZE 4
void add_arg(struct Args *args, char *arg_ptr, unsigned int arg_length) {
if (args->argv_size == 0) {
args->argv_size = ARGV_INIT_SIZE;
args->argv = malloc(args->argv_size * sizeof(char *));
}
if (args->argc == args->argv_size) {
args->argv_size *= 2;
args->argv = realloc(args->argv, sizeof(char *) * args->argv_size);
}
char *arg = malloc(arg_length + 1);
strncpy(arg, arg_ptr, arg_length);
args->argv[args->argc] = arg;
args->argc++;
}
void parse_worker(struct Args *args, char *arg_position,
unsigned int arg_length, char *line) {
if (line[0] == '\n') {
if (arg_length != 0) {
add_arg(args, arg_position, arg_length);
}
return;
} else if (isspace(line[0])) {
add_arg(args, arg_position, arg_length);
while (line[0] != '\n' && isspace(line[0])) {
line++;
}
parse_worker(args, line, 0, line);
} else {
parse_worker(args, arg_position, arg_length + 1, line + 1);
}
}
struct Args parse(char *line) {
struct Args args;
args.argc = 0;
args.argv_size = 0;
parse_worker(&args, line, 0, line);
return args;
}

16
src/parse.h Normal file
View file

@ -0,0 +1,16 @@
#ifndef PARSE_H
#define PARSE_H
#include <sys/types.h>
struct Args {
unsigned int argc;
size_t argv_size;
char **argv;
};
void add_arg(struct Args *args, char *arg_ptr, unsigned int arg_length);
void parse_worker(struct Args *args, char *arg_position, unsigned int arg_length, char *line);
struct Args parse(char *line);
#endif /* PARSE_H */

View file

@ -28,6 +28,7 @@ char *read_a_line(FILE *file_to_read) {
if (!feof(file_to_read)) { if (!feof(file_to_read)) {
return input_buffer; return input_buffer;
} else { } else {
free(input_buffer);
return NULL; return NULL;
} }
} }