add: line parsing via getchar
This commit is contained in:
parent
f1dc8fe9f4
commit
5c37658bcc
1
src/const.h
Normal file
1
src/const.h
Normal file
@ -0,0 +1 @@
|
|||||||
|
#define BUFSIZE 1024
|
||||||
3
src/exec.c
Normal file
3
src/exec.c
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
#include "exec.h"
|
||||||
|
|
||||||
|
void execute_cmd(void) {}
|
||||||
4
src/exec.h
Normal file
4
src/exec.h
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
int is_builtin_cmd(void);
|
||||||
|
void execute_cmd(void);
|
||||||
@ -1,8 +1,16 @@
|
|||||||
|
#include "parsing.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
int main(int argc, char *argv[], char *envp[]) {
|
int main(int argc, char *argv[], char *envp[]) {
|
||||||
|
(void)argc;
|
||||||
|
(void)argv;
|
||||||
|
|
||||||
int index = 0;
|
int index = 0;
|
||||||
while (envp[index]) {
|
while (envp[index]) {
|
||||||
printf("%s\n", envp[index++]);
|
printf("%s\n", envp[index++]);
|
||||||
}
|
}
|
||||||
|
shell_loop();
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
51
src/parsing.c
Normal file
51
src/parsing.c
Normal 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
5
src/parsing.h
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
void shell_loop(void);
|
||||||
Loading…
Reference in New Issue
Block a user