Compare commits
3 commits
ec3fc81544
...
e5aa953c04
Author | SHA1 | Date | |
---|---|---|---|
e5aa953c04 | |||
6672cb3bf8 | |||
a3d156eb1f |
6 changed files with 99 additions and 32 deletions
17
src/command.c
Normal file
17
src/command.c
Normal 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
8
src/command.h
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef COMMAND_H
|
||||
#define COMMAND_H
|
||||
|
||||
#include "parse.h"
|
||||
|
||||
unsigned char invoke(struct Args args);
|
||||
|
||||
#endif /* COMMAND_H */
|
36
src/main.c
36
src/main.c
|
@ -1,16 +1,11 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "command.h"
|
||||
#include "parse.h"
|
||||
#include "reader.h"
|
||||
|
||||
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;
|
||||
|
||||
while (1) {
|
||||
|
@ -20,32 +15,9 @@ int main() {
|
|||
return 0;
|
||||
}
|
||||
|
||||
printf("command: '%s'\n", input_line);
|
||||
|
||||
struct Args args = parse(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> ");
|
||||
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);
|
||||
*/
|
||||
invoke(args);
|
||||
}
|
||||
}
|
||||
|
|
53
src/parse.c
Normal file
53
src/parse.c
Normal 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
16
src/parse.h
Normal 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 */
|
|
@ -28,6 +28,7 @@ char *read_a_line(FILE *file_to_read) {
|
|||
if (!feof(file_to_read)) {
|
||||
return input_buffer;
|
||||
} else {
|
||||
free(input_buffer);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue