-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.c
More file actions
106 lines (91 loc) · 2.47 KB
/
command.c
File metadata and controls
106 lines (91 loc) · 2.47 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "command.h"
void addCommand(int ind, char* name, int id, int argnum, ...)
{
struct command *cmd = &commands[ind];
cmd->name = name;
cmd->id = id;
cmd->argnum = argnum;
va_list args;
va_start(args, argnum);
int i;
for(i = 0; i < argnum; i++){
cmd->args[i] = va_arg(args, char*);
}
va_end(args);
num_commands++;
}
void insertCommand(int ind, char* name, int id, int argnum, ...)
{
//Shift everything to the right
int k;
for(k = MAX_COMMANDS - 1; k > ind; k--)
{
commands[k] = commands[k-1];
}
//Insert
struct command *cmd = &commands[ind];
cmd->name = name;
cmd->id = id;
cmd->argnum = argnum;
va_list args;
va_start(args, argnum);
int i;
for(i = 0; i < argnum; i++){
cmd->args[i] = va_arg(args, char*);
}
va_end(args);
num_commands++;
}
void removeCommand(int ind)
{
struct command blank;
if(ind == (MAX_COMMANDS -1))
{
commands[ind] = blank;
}
int i;
//Shift the elements on the right of element to the left
for(i = ind; i < MAX_COMMANDS; i++)
{
commands[i] = commands[i+1];
}
num_commands--;
}
void addArgToCommand(int cmd_ind, char* arg)
{
struct command *cmd = &commands[cmd_ind];
cmd->args[cmd->argnum] = strdup(arg); //argnum will point to the next empty slot in args array
cmd->argnum += 1;
}
void addInputRedirection(int cmd_index, char* in_fn)
{
struct command *cmd = &commands[cmd_index];
cmd->infile = in_fn;
}
void addOutputRedirection(int cmd_index, char* out_fn, int appendOut)
{
struct command *cmd = &commands[cmd_index];
cmd->outfile = strdup(out_fn);
cmd->appendOut = appendOut;
}
void clearCommandTable(){
int i;
for(i = 0; i < MAX_COMMANDS; i++)
{
struct command *cmd = &commands[i];
clearCommandArgs(cmd);
cmd->name = NULL;
cmd->id = 0;
cmd->infile = NULL;
cmd->outfile = NULL;
cmd->appendOut = 0;
}
}
void clearCommandArgs(struct command * cmd){
int i;
for(i = 0; i < MAX_ARGS; i++)
{
cmd->args[i] = NULL;
}
cmd->argnum = 0;
}