-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.c
298 lines (268 loc) · 7.51 KB
/
commands.c
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "main.h"
int shellby_alias(char **args, char __attribute__((__unused__)) **front);
void set_alias(char *var_name, char *value);
void print_alias(alias_t *alias);
void help_env(void);
void help_setenv(void);
void help_unsetenv(void);
void help_history(void);
void help_all(void);
void help_alias(void);
void help_cd(void);
void help_exit(void);
void help_help(void);
/**
* shellby_alias - Builtin command that either prints all aliases, specific
* aliases, or sets an alias.
* @args: An array of arguments.
* @front: A double pointer to the beginning of args.
*
* Return: If an error occurs - -1.
* Otherwise - 0.
*/
int shellby_alias(char **args, char __attribute__((__unused__)) **front)
{
alias_t *temp = aliases;
int i, ret = 0;
char *value;
if (!args[0])
{
while (temp)
{
print_alias(temp);
temp = temp->next;
}
return (ret);
}
for (i = 0; args[i]; i++)
{
temp = aliases;
value = _strchr(args[i], '=');
if (!value)
{
while (temp)
{
if (_strcmp(args[i], temp->name) == 0)
{
print_alias(temp);
break;
}
temp = temp->next;
}
if (!temp)
ret = create_error(args + i, 1);
}
else
set_alias(args[i], value);
}
return (ret);
}
/**
* set_alias - Will either set an existing alias 'name' with a new value,
* 'value' or creates a new alias with 'name' and 'value'.
* @var_name: Name of the alias.
* @value: Value of the alias. First character is a '='.
*/
void set_alias(char *var_name, char *value)
{
alias_t *temp = aliases;
int len, j, k;
char *new_value;
*value = '\0';
value++;
len = _strlen(value) - _strspn(value, "'\"");
new_value = malloc(sizeof(char) * (len + 1));
if (!new_value)
return;
for (j = 0, k = 0; value[j]; j++)
{
if (value[j] != '\'' && value[j] != '"')
new_value[k++] = value[j];
}
new_value[k] = '\0';
while (temp)
{
if (_strcmp(var_name, temp->name) == 0)
{
free(temp->value);
temp->value = new_value;
break;
}
temp = temp->next;
}
if (!temp)
add_alias_end(&aliases, var_name, new_value);
}
/**
* print_alias - Prints the alias in the format name='value'.
* @alias: Pointer to an alias.
*/
void print_alias(alias_t *alias)
{
char *alias_string;
int len = _strlen(alias->name) + _strlen(alias->value) + 4;
alias_string = malloc(sizeof(char) * (len + 1));
if (!alias_string)
return;
_strcpy(alias_string, alias->name);
_strcat(alias_string, "='");
_strcat(alias_string, alias->value);
_strcat(alias_string, "'\n");
write(STDOUT_FILENO, alias_string, len);
free(alias_string);
}
/**
* replace_aliases - Goes through the arguments and replace any matching alias
* with their value.
* @args: 2D pointer to the arguments.
*
* Return: 2D pointer to the arguments.
*/
char **replace_aliases(char **args)
{
alias_t *temp;
int i;
char *new_value;
if (_strcmp(args[0], "alias") == 0)
return (args);
for (i = 0; args[i]; i++)
{
temp = aliases;
while (temp)
{
if (_strcmp(args[i], temp->name) == 0)
{
new_value = malloc(sizeof(char) * (_strlen(temp->value) + 1));
if (!new_value)
{
free_args(args, args);
return (NULL);
}
_strcpy(new_value, temp->value);
free(args[i]);
args[i] = new_value;
i--;
break;
}
temp = temp->next;
}
}
return (args);
}
/**
* help_env - Displays information on the shellby builtin command 'env'.
*/
void help_env(void)
{
char *msg = "env: env\n\tPrints the current environment.\n";
write(STDOUT_FILENO, msg, _strlen(msg));
}
/**
* help_setenv - Displays information on the shellby builtin command 'setenv'.
*/
void help_setenv(void)
{
char *msg = "setenv: setenv [VARIABLE] [VALUE]\n\tInitializes a new";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = "environment variable, or modifies an existing one.\n\n";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = "\tUpon failure, prints a message to stderr.\n";
write(STDOUT_FILENO, msg, _strlen(msg));
}
/**
* help_unsetenv - Displays information on the shellby builtin command
* 'unsetenv'.
*/
void help_unsetenv(void)
{
char *msg = "unsetenv: unsetenv [VARIABLE]\n\tRemoves an ";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = "environmental variable.\n\n\tUpon failure, prints a ";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = "message to stderr.\n";
write(STDOUT_FILENO, msg, _strlen(msg));
}
/**
* help_all - Displays all possible builtin shellby commands.
*/
void help_all(void)
{
char *msg = "Shellby\nThese shell commands are defined internally.\n";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = "Type 'help' to see this list.\nType 'help name' to find ";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = "out more about the function 'name'.\n\n alias \t";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = "alias [NAME[='VALUE'] ...]\n cd \tcd ";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = "[DIRECTORY]\n exit \texit [STATUS]\n env \tenv";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = "\n setenv \tsetenv [VARIABLE] [VALUE]\n unsetenv\t";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = "unsetenv [VARIABLE]\n";
write(STDOUT_FILENO, msg, _strlen(msg));
}
/**
* help_alias - Displays information on the shellby builtin command 'alias'.
*/
void help_alias(void)
{
char *msg = "alias: alias [NAME[='VALUE'] ...]\n\tHandles aliases.\n";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = "\n\talias: Prints a list of all aliases, one per line, in ";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = "the format NAME='VALUE'.\n\talias name [name2 ...]:prints";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = " the aliases name, name2, etc. one per line, in the ";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = "form NAME='VALUE'.\n\talias NAME='VALUE' [...]: Defines";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = " an alias for each NAME whose VALUE is given. If NAME ";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = "is already an alias, replace its value with VALUE.\n";
write(STDOUT_FILENO, msg, _strlen(msg));
}
/**
* help_cd - Displays information on the shellby builtin command 'cd'.
*/
void help_cd(void)
{
char *msg = "cd: cd [DIRECTORY]\n\tChanges the current directory of the";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = " process to DIRECTORY.\n\n\tIf no argument is given, the ";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = "command is interpreted as cd $HOME. If the argument '-' is";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = " given, the command is interpreted as cd $OLDPWD.\n\n";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = "\tThe environment variables PWD and OLDPWD are updated ";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = "after a change of directory.\n";
write(STDOUT_FILENO, msg, _strlen(msg));
}
/**
* help_exit - Displays information on the shellby builtin command 'exit'.
*/
void help_exit(void)
{
char *msg = "exit: exit [STATUS]\n\tExits the shell.\n\n\tThe ";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = "STATUS argument is the integer used to exit the shell.";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = " If no argument is given, the command is interpreted as";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = " exit 0.\n";
write(STDOUT_FILENO, msg, _strlen(msg));
}
/**
* help_help - Displays information on the shellby builtin command 'help'.
*/
void help_help(void)
{
char *msg = "help: help\n\tSee all possible Shellby builtin commands.\n";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = "\n help [BUILTIN NAME]\n\tSee specific information on each ";
write(STDOUT_FILENO, msg, _strlen(msg));
msg = "builtin command.\n";
write(STDOUT_FILENO, msg, _strlen(msg));
}