-
Notifications
You must be signed in to change notification settings - Fork 1
/
psh.h
75 lines (61 loc) · 1.94 KB
/
psh.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#ifndef __PSH_H
#define __PSH_H
#ifdef PSH_DEBUG
#define ASSERT(cond, fmt, args...) do { \
if (!(cond)) { \
fprintf(stderr, "%s:%d -- " fmt "\n", __func__, __LINE__, ##args); \
exit(1); \
} \
} while (0)
#else
#define ASSERT(cond, fmt, args...)
#endif
#ifdef PSH_DEBUG
#define DPRINTF(fmt, args...) printf("%s: " fmt, __func__, ##args)
#else
#define DPRINTF(fmt, args...)
#endif
/* The maximum possible length of a shell command. */
#define ARG_MAX 1024
/* The .pshrc file is stored in ~/.pshrc */
#define PSHRC_PATH ".pshrc"
#define HOME_SYMBOL '~'
struct psh_alias {
char *name;
char *value;
};
/* An array of current aliases. */
extern struct psh_alias *aliases;
extern int num_aliases;
/*
* Declarations related to shell builtins.
*
* A shell builtin command is a command that can not be executed in a forked
* shell instance. They need to be executed in the current shell instance. For
* this reason, binaries of these commands can't exist.
*
* For example, cd is a shell builtin command. If cd was a binary in the path,
* and someone cd'ed to a directory, the shell would fork(), and then change
* the working directory of the child. It won't change the current working
* directory of the shell from which the command was executed. So, cd has to be
* implemented by the shell.
*/
/* Prototype of a built-in command handler. */
typedef int builtin_handler_t(char **args);
/* TODO: Try to find a way to avoid these forward declarations. */
builtin_handler_t psh_cd;
builtin_handler_t psh_exit;
builtin_handler_t psh_add_alias;
struct psh_builtin {
char *name;
builtin_handler_t *handler;
};
/* The built-in commands of the shell. */
struct psh_builtin psh_builtins[] = {
{"alias", psh_add_alias},
{"cd", psh_cd},
{"exit", psh_exit}
};
/* TODO: Feels hackish... Maybe there is a better way. */
#define PSH_NUM_BUILTINS (sizeof(psh_builtins) / sizeof(struct psh_builtin))
#endif /* __PSH_H */