add: line parsing via getchar

This commit is contained in:
Guamss 2026-03-25 11:25:02 +01:00
parent f1dc8fe9f4
commit 5c37658bcc
6 changed files with 72 additions and 0 deletions

1
src/const.h Normal file
View File

@ -0,0 +1 @@
#define BUFSIZE 1024

3
src/exec.c Normal file
View File

@ -0,0 +1,3 @@
#include "exec.h"
void execute_cmd(void) {}

4
src/exec.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
int is_builtin_cmd(void);
void execute_cmd(void);

View File

@ -1,8 +1,16 @@
#include "parsing.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[], char *envp[]) {
(void)argc;
(void)argv;
int index = 0;
while (envp[index]) {
printf("%s\n", envp[index++]);
}
shell_loop();
return EXIT_SUCCESS;
}

51
src/parsing.c Normal file
View File

@ -0,0 +1,51 @@
#include "parsing.h"
#include "const.h"
#include "exec.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
char *read_line() {
char c;
int i = 0;
char *buffer = malloc(BUFSIZE * sizeof(char));
if (buffer == NULL) {
dprintf(STDERR_FILENO, "Line read buffer allocation error !");
exit(EXIT_FAILURE);
}
while (1) {
c = getchar();
if (c == EOF || c == '\n') {
buffer[i] = '\0';
return buffer;
} else {
buffer[i] = c;
}
i++;
// TODO: realoc quand le buffer est dépassé
}
}
char **split_line(char *line) {
(void)line;
return NULL;
}
void shell_loop(void) {
char *line;
char **args;
int status = 1;
do {
dprintf(STDOUT_FILENO, "> ");
line = read_line();
dprintf(STDOUT_FILENO, "%s\n", line);
args = split_line(line);
execute_cmd();
// free(line);
free(args);
} while (status);
}

5
src/parsing.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
#include <stdio.h>
#include <unistd.h>
void shell_loop(void);