forked from tuvo1106/c_simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.c
106 lines (95 loc) · 2 KB
/
help.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
#include "holberton.h"
/**
* helpFunc - retrieves instruction on how to use builtin
* @build: input build
* Return: Always 1
*/
int helpFunc(config *build)
{
type_b help_arr[] = {
{"exit", helpExit},
{"env", helpEnv},
{"history", helpHistory},
{"alias", helpAlias},
{"cd", helpCd},
{"setenv", helpSetenv},
{"unsetenv", helpUnsetenv},
{"help", helpHelp},
{NULL, NULL}
};
register int i = 0, j = 1, argCount = countArgs(build->args);
_Bool foundCommand = false;
if (argCount == 1)
return (displayHelpMenu());
while (j < argCount)
{
i = 0;
while (help_arr[i].command)
{
if (_strcmp(build->args[j], help_arr[i].command) == 0)
{
foundCommand = true;
help_arr[i].func(build);
break;
}
i++;
}
j++;
}
if (foundCommand == false)
{
errno = ENOBUILTIN;
errorHandler(build);
}
return (1);
}
/**
* displayHelpMenu - displays available help options
* Return: Always 1
*/
int displayHelpMenu(void)
{
char str[81] = "Type help [built-in]\n\nIncluded built-ins:";
char *str2 = "\n\n\texit\n\tenv\n\tcd\n\tsetenv\n\tunsetenv\n\thelp\n";
_strcat(str, str2);
write(STDOUT_FILENO, str, _strlen(str));
return (1);
}
/**
* helpExit - instructions on how to exit
* @build: input build
* Return: Always 1
*/
int helpExit(config *build)
{
char str[82] = "exit: exit [n]\n\tExit the shell.\n\n\t";
char *str2 = "Exit with a status of n, or if n is omitted, 0.\n";
(void)build;
_strcat(str, str2);
write(STDOUT_FILENO, str, _strlen(str));
return (1);
}
/**
* helpEnv - instructions on how to exit
* @build: input build
* Return: Always 1
*/
int helpEnv(config *build)
{
char str[] = "env: env\n\tPrint the environment.\n";
(void)build;
write(STDOUT_FILENO, str, _strlen(str));
return (1);
}
/**
* helpHistory - instructions on how to exit
* @build: input build
* Return: Always 1
*/
int helpHistory(config *build)
{
char str[] = "history: history\n\tNot supported in this version.\n";
(void)build;
write(STDOUT_FILENO, str, _strlen(str));
return (1);
}