GCC Code Coverage Report


Directory: ./
File: src/builtins/help.c
Date: 2024-06-05 00:36:48
Exec Total Coverage
Lines: 0 63 0.0%
Functions: 0 7 0.0%
Branches: 0 28 0.0%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** 42sh
4 ** File description:
5 ** The file containing the help builtins
6 */
7 /**
8 * @file help.c
9 * @brief The file containing the help builtins
10 */
11
12 #include "../../include/myshell.h"
13
14 /**
15 * @brief Print the help for the shell
16 * @param mysh The shell structure
17 * @return <b>int</b> <u>0</u> if the command succeed, <u>1</u> otherwise
18 */
19 static int print_help(mysh_t *mysh)
20 {
21 if (mysh->args[1] == NULL || my_strcmp(mysh->args[1], "help") == 0) {
22 my_printf("USAGE:\n\thelp [BUILTIN]\n");
23 my_printf("\tDisplay information about builtin commands.\n");
24 my_printf("BUILTIN:\n");
25 my_printf("\tabout\n");
26 my_printf("\tcd\n");
27 my_printf("\tenv\n");
28 my_printf("\texit\n");
29 my_printf("\thelp\n");
30 my_printf("\tsetenv\n");
31 my_printf("\tsource\n");
32 my_printf("\tunsetenv\n");
33 return 1;
34 }
35 return 0;
36 }
37
38 /**
39 * @brief Show the help for the cd builtin
40 * @param mysh The shell structure
41 * @return <b>int</b> <u>0</u> if the command succeed, <u>1</u> otherwise
42 */
43 static int cd_help(mysh_t *mysh)
44 {
45 if (my_strcmp(mysh->args[1], "cd") == 0) {
46 my_printf("cd [OPTION]\n\tChange the shell working directory.\n");
47 my_printf("OPTIONS:\n\t- : "
48 "Change to the previous working directory.\n");
49 my_printf("\t~ : Change to the home directory.\n");
50 return 1;
51 }
52 return 0;
53 }
54
55 /**
56 * @brief Show the help for the env builtin
57 * @param mysh The shell structure
58 * @return <b>int</b> <u>0</u> if the command succeed, <u>1</u> otherwise
59 */
60 static int env_help(mysh_t *mysh)
61 {
62 if (my_strcmp(mysh->args[1], "env") == 0) {
63 my_printf("env\n\tPrint the environment variables.\n");
64 return 1;
65 }
66 return 0;
67 }
68
69 /**
70 * @brief Show the help for the setenv builtin
71 * @param mysh The shell structure
72 * @return <b>int</b> <u>0</u> if the command succeed, <u>1</u> otherwise
73 */
74 static int setenv_help(mysh_t *mysh)
75 {
76 if (my_strcmp(mysh->args[1], "setenv") == 0) {
77 my_printf("setenv [VARIABLE] [VALUE]\n");
78 my_printf("\tSet an environment variable.\n");
79 return 1;
80 }
81 return 0;
82 }
83
84 /**
85 * @brief Show the help for the unsetenv builtin
86 * @param mysh The shell structure
87 * @return <b>int</b> <u>0</u> if the command succeed, <u>1</u> otherwise
88 */
89 static int unsetenv_help(mysh_t *mysh)
90 {
91 if (my_strcmp(mysh->args[1], "unsetenv") == 0) {
92 my_printf("unsetenv [VARIABLE]\n");
93 my_printf("\tUnset an environment variable.\n");
94 return 1;
95 }
96 return 0;
97 }
98
99 /**
100 * @brief Show the help for the next builtin
101 * @param mysh The shell structure
102 * @return <b>int</b> <u>0</u> if the command succeed, <u>1</u> otherwise
103 */
104 static int help_next(mysh_t *mysh)
105 {
106 if (my_strcmp(mysh->args[1], "exit") == 0) {
107 my_printf("exit\n\tExit the shell.\n");
108 return 0;
109 }
110 if (my_strcmp(mysh->args[1], "source") == 0) {
111 my_printf("source [FILE]\n\tExecute commands from a file.\n");
112 return 0;
113 }
114 if (my_strcmp(mysh->args[1], "about") == 0) {
115 my_printf("about\n\tDisplay information about the shell.\n");
116 return 0;
117 }
118 my_fprintf(2, "help: %s: No such builtin.\n", mysh->args[1]);
119 return 1;
120 }
121
122 /**
123 * @brief The help builtin
124 * @param mysh The shell structure
125 * @return <b>int</b> <u>0</u> if the command succeed, <u>1</u> otherwise
126 */
127 int exec_help(mysh_t *mysh)
128 {
129 if (print_help(mysh))
130 return 0;
131 if (cd_help(mysh))
132 return 0;
133 if (env_help(mysh))
134 return 0;
135 if (setenv_help(mysh))
136 return 0;
137 if (unsetenv_help(mysh))
138 return 0;
139 return help_next(mysh);
140 }
141