~emersion/mrsh

7386a3a657cdf0d4e8ee9f9221dcc21fb3cdbd5d — emersion 3 years ago ef23123
shell: refactor, split into multiple files
20 files changed, 114 insertions(+), 70 deletions(-)

M include/ast.h
A include/shell/process.h
A include/shell/shell.h
A include/shell/shm.h
R include/{shell.h => shell/task.h}
A include/shell/word.h
M meson.build
M shell/process.c
M shell/shell.c
R shell/{task_assignment.c => task/assignment.c}
R shell/{task_async.c => task/async.c}
R shell/{task_binop.c => task/binop.c}
R shell/{task_command.c => task/command.c}
R shell/{task_if_clause.c => task/if_clause.c}
R shell/{task_list.c => task/list.c}
R shell/{task_pipeline.c => task/pipeline.c}
R shell/{task_subshell.c => task/subshell.c}
R shell/{task.c => task/task.c}
R shell/{task_word.c => task/word.c}
M shell/word.c
M include/ast.h => include/ast.h +5 -0
@@ 1,4 1,9 @@
#ifndef _AST_H
#define _AST_H

#include <mrsh/ast.h>

void command_list_array_finish(struct mrsh_array *cmds);
void case_item_destroy(struct mrsh_case_item *item);

#endif

A include/shell/process.h => include/shell/process.h +21 -0
@@ 0,0 1,21 @@
#ifndef _SHELL_PROCESS_H
#define _SHELL_PROCESS_H

#include <stdbool.h>
#include <sys/types.h>

/**
 * This struct is used to track child processes.
 */
struct process {
	pid_t pid;
	bool finished;
	int stat;
};

void process_init(struct process *process, pid_t pid);
void process_finish(struct process *process);
int process_poll(struct process *process);
void process_notify(pid_t pid, int stat);

#endif

A include/shell/shell.h => include/shell/shell.h +12 -0
@@ 0,0 1,12 @@
#ifndef _SHELL_SHELL_H
#define _SHELL_SHELL_H

#include <mrsh/shell.h>

struct context {
	struct mrsh_state *state;
	int stdin_fileno;
	int stdout_fileno;
};

#endif

A include/shell/shm.h => include/shell/shm.h +6 -0
@@ 0,0 1,6 @@
#ifndef _SHELL_SHM_H
#define _SHELL_SHM_H

int create_anonymous_file(void);

#endif

R include/shell.h => include/shell/task.h +7 -47
@@ 1,19 1,8 @@
#include <mrsh/shell.h>
#ifndef _SHELL_TASK_H
#define _SHELL_TASK_H

struct context {
	struct mrsh_state *state;
	int stdin_fileno;
	int stdout_fileno;
};

/**
 * This struct is used to track child processes.
 */
struct process {
	pid_t pid;
	bool finished;
	int stat;
};
#include "shell/shell.h"
#include "shell/word.h"

#define TASK_STATUS_WAIT -1
#define TASK_STATUS_ERROR -2


@@ 55,22 44,6 @@ struct task_interface {
	void (*destroy)(struct task *task);
};

enum tilde_expansion {
	// Don't perform tilde expansion at all
	TILDE_EXPANSION_NONE,
	// Only expand at the begining of words
	TILDE_EXPANSION_NAME,
	// Expand at the begining of words and after semicolons
	TILDE_EXPANSION_ASSIGNMENT,
};

int create_anonymous_file(void);

void process_init(struct process *process, pid_t pid);
void process_finish(struct process *process);
int process_poll(struct process *process);
void process_notify(pid_t pid, int stat);

void task_init(struct task *task, const struct task_interface *impl);
void task_destroy(struct task *task);
int task_poll(struct task *task, struct context *ctx);


@@ 94,8 67,6 @@ struct task *task_async_create(struct task *async);

struct task *task_assignment_create(struct mrsh_array *assignments);

struct task *task_subshell_create(struct task *subtask);

/**
 * Creates a task that mutates `word_ptr`, executing all substitutions. After
 * the task has finished, the word tree is guaranteed to only contain word


@@ 104,17 75,6 @@ struct task *task_subshell_create(struct task *subtask);
struct task *task_word_create(struct mrsh_word **word_ptr,
	enum tilde_expansion tilde_expansion);

/**
 * Performs tilde expansion. It leaves the string as-is in case of error.
 */
void expand_tilde(struct mrsh_state *state, char **str_ptr);
/**
 * Performs field splitting on `word`, writing fields to `fields`. This should
 * be done after expansions/substitutions.
 */
void split_fields(struct mrsh_array *fields, struct mrsh_word *word,
	const char *ifs);
/**
 * Performs pathname expansion on each item in `fields`.
 */
bool expand_pathnames(struct mrsh_array *expanded, struct mrsh_array *fields);
struct task *task_subshell_create(struct task *subtask);

#endif

A include/shell/word.h => include/shell/word.h +31 -0
@@ 0,0 1,31 @@
#ifndef _SHELL_WORD_H
#define _SHELL_WORD_H

#include <mrsh/shell.h>

enum tilde_expansion {
	// Don't perform tilde expansion at all
	TILDE_EXPANSION_NONE,
	// Only expand at the begining of words
	TILDE_EXPANSION_NAME,
	// Expand at the begining of words and after semicolons
	TILDE_EXPANSION_ASSIGNMENT,
};

/**
 * Performs tilde expansion. It leaves the string as-is in case of error.
 */
void expand_tilde(struct mrsh_state *state, char **str_ptr);
/**
 * Performs field splitting on `word`, writing fields to `fields`. This should
 * be done after expansions/substitutions.
 */
void split_fields(struct mrsh_array *fields, struct mrsh_word *word,
	const char *ifs);
/**
 * Performs pathname expansion on each item in `fields`.
 */
bool expand_pathnames(struct mrsh_array *expanded, struct mrsh_array *fields);


#endif

M meson.build => meson.build +10 -10
@@ 72,16 72,16 @@ executable(
		'shell/process.c',
		'shell/shell.c',
		'shell/shm.c',
		'shell/task_assignment.c',
		'shell/task_async.c',
		'shell/task_binop.c',
		'shell/task_command.c',
		'shell/task_if_clause.c',
		'shell/task_list.c',
		'shell/task_pipeline.c',
		'shell/task_subshell.c',
		'shell/task_word.c',
		'shell/task.c',
		'shell/task/assignment.c',
		'shell/task/async.c',
		'shell/task/binop.c',
		'shell/task/command.c',
		'shell/task/if_clause.c',
		'shell/task/list.c',
		'shell/task/pipeline.c',
		'shell/task/subshell.c',
		'shell/task/task.c',
		'shell/task/word.c',
		'shell/word.c',
	]),
	dependencies: [rt, mrsh_static],

M shell/process.c => shell/process.c +2 -1
@@ 1,7 1,8 @@
#include <mrsh/array.h>
#include <stdbool.h>
#include <string.h>
#include <sys/wait.h>
#include "shell.h"
#include "shell/process.h"

// TODO: don't use a global
// TODO: use a linked list instead

M shell/shell.c => shell/shell.c +2 -1
@@ 1,7 1,8 @@
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include "shell.h"
#include "shell/shell.h"
#include "shell/task.h"

void mrsh_state_init(struct mrsh_state *state) {
	state->exit = -1;

R shell/task_assignment.c => shell/task/assignment.c +1 -1
@@ 1,6 1,6 @@
#include <string.h>
#include <stdlib.h>
#include "shell.h"
#include "shell/task.h"

struct task_assignment {
	struct task task;

R shell/task_async.c => shell/task/async.c +1 -1
@@ 4,7 4,7 @@
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include "shell.h"
#include "shell/task.h"

struct task_async {
	struct task task;

R shell/task_binop.c => shell/task/binop.c +1 -1
@@ 1,5 1,5 @@
#include <stdlib.h>
#include "shell.h"
#include "shell/task.h"

struct task_binop {
	struct task task;

R shell/task_command.c => shell/task/command.c +3 -1
@@ 7,7 7,9 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "shell.h"
#include "shell/process.h"
#include "shell/shm.h"
#include "shell/task.h"

struct task_command {
	struct task task;

R shell/task_if_clause.c => shell/task/if_clause.c +1 -1
@@ 1,5 1,5 @@
#include <stdlib.h>
#include "shell.h"
#include "shell/task.h"

struct task_if_clause {
	struct task task;

R shell/task_list.c => shell/task/list.c +1 -1
@@ 1,5 1,5 @@
#include <stdlib.h>
#include "shell.h"
#include "shell/task.h"

struct task_list {
	struct task task;

R shell/task_pipeline.c => shell/task/pipeline.c +1 -1
@@ 1,6 1,6 @@
#include <unistd.h>
#include <stdlib.h>
#include "shell.h"
#include "shell/task.h"

struct task_pipeline {
	struct task task;

R shell/task_subshell.c => shell/task/subshell.c +2 -1
@@ 2,7 2,8 @@
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "shell.h"
#include "shell/process.h"
#include "shell/task.h"

struct task_subshell {
	struct task task;

R shell/task.c => shell/task/task.c +2 -1
@@ 4,7 4,8 @@
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include "shell.h"
#include "shell/process.h"
#include "shell/task.h"

void task_init(struct task *task, const struct task_interface *impl) {
	assert(impl->poll);

R shell/task_word.c => shell/task/word.c +3 -1
@@ 11,7 11,9 @@

#include "buffer.h"
#include "builtin.h"
#include "shell.h"
#include "shell/process.h"
#include "shell/shm.h"
#include "shell/task.h"

#define TOKEN_COMMAND_READ_SIZE 128


M shell/word.c => shell/word.c +2 -1
@@ 7,7 7,8 @@
#include <stdlib.h>
#include <string.h>
#include "buffer.h"
#include "shell.h"
#include "shell/shell.h"
#include "shell/word.h"

void expand_tilde(struct mrsh_state *state, char **str_ptr) {
	char *str = *str_ptr;