Compare commits
No commits in common. "e5aa953c043f0d0714a794453b7d485d60444382" and "ec3fc81544008de4efe7189b297e204bb7d53b38" have entirely different histories.
e5aa953c04
...
ec3fc81544
6 changed files with 33 additions and 100 deletions
|
@ -1,17 +0,0 @@
|
|||
#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;
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
#ifndef COMMAND_H
|
||||
#define COMMAND_H
|
||||
|
||||
#include "parse.h"
|
||||
|
||||
unsigned char invoke(struct Args args);
|
||||
|
||||
#endif /* COMMAND_H */
|
38
src/main.c
38
src/main.c
|
@ -1,11 +1,16 @@
|
|||
#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) {
|
||||
|
@ -15,9 +20,32 @@ int main() {
|
|||
return 0;
|
||||
}
|
||||
|
||||
struct Args args = parse(input_line);
|
||||
free(input_line);
|
||||
printf("command: '%s'\n", input_line);
|
||||
|
||||
invoke(args);
|
||||
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);
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
|
53
src/parse.c
53
src/parse.c
|
@ -1,53 +0,0 @@
|
|||
#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
16
src/parse.h
|
@ -1,16 +0,0 @@
|
|||
#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,7 +28,6 @@ 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