C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
External Commands

This module provides functions to execute external commands. More...

Functions

int dmon (char **eargv)
 Fork and execute a command as a daemon.
int fork_exec (char **argv)
 Fork and exec a command.
int full_screen_fork_exec (char **argv)
 Execute a command in full screen mode.
int full_screen_shell (char *shellCmdPtr)
 Execute a shell command in full screen mode.
int shell (char *shellCmdPtr)
 Execute a shell command.

Detailed Description

This module provides functions to execute external commands.

Handles terminal settings, signal handling, and error reporting to ensure a smooth user experience when executing commands from within the application. The main functions include full_screen_fork_exec, full_screen_shell, and fork_exec, which manage the execution of commands while maintaining the integrity of the application's user interface.

Function Documentation

◆ dmon()

int dmon ( char ** eargv)

Fork and execute a command as a daemon.

Parameters
eargv- array of arguments for the command to execute
Returns
EXIT_SUCCESS on success, EXIT_FAILURE on failure

Forks the process twice to create a daemon. Sets the session ID and working directory. Redirects standard file descriptors to /dev/null. Closes all open file descriptors. Executes the command using execvp. Exits with failure if any step fails.

Note
Don't use this code. It is not finished.

Definition at line 226 of file exec.c.

226 {
227
228 pid_t pid = fork();
230 curs_set(1);
231 sig_dfl_mode();
232
233 if (pid < 0) {
234 fprintf(stderr, "Failed to fork: %s\n", strerror(errno));
235 return EXIT_FAILURE;
236 }
237 if (pid > 0) {
240 touchwin(stdscr);
241 wnoutrefresh(stdscr);
242 wrefresh(stdscr);
243 restore_wins();
244 return 0;
245 }
246 if (setsid() < 0) {
247 fprintf(stderr, "Failed to set session ID: %s\n", strerror(errno));
248 exit(EXIT_FAILURE);
249 }
250 pid = fork();
251 if (pid < 0) {
252 fprintf(stderr, "Second fork failed: %s\n", strerror(errno));
253 exit(EXIT_FAILURE);
254 }
255 if (pid > 0) {
256 exit(EXIT_SUCCESS);
257 }
258 char *e = getenv("HOME");
259 if (e != NULL) {
260 if (chdir(e) < 0)
261 exit(EXIT_FAILURE);
262 }
263 umask(0);
264 close(STDIN_FILENO);
265 close(STDOUT_FILENO);
266 close(STDERR_FILENO);
267 int dev_null = open("/dev/null", O_RDWR);
268 if (dev_null != -1) {
269 dup2(dev_null, STDIN_FILENO);
270 dup2(dev_null, STDOUT_FILENO);
271 dup2(dev_null, STDERR_FILENO);
272 if (dev_null > 2) {
273 close(dev_null);
274 }
275 }
276 long max_fd = sysconf(_SC_OPEN_MAX);
277 for (long fd = 3; fd < max_fd; fd++) {
278 close(fd);
279 }
280 execvp(eargv[0], eargv);
281 exit(EXIT_FAILURE);
282}
char * eargv[MAXARGS]
Definition futil.c:52
void restore_wins()
Restore all windows after a screen resize.
Definition dwin.c:933
bool restore_curses_tioctl()
restore_curses_tioctl() - restore curses terminal settings
Definition scriou.c:81
bool capture_curses_tioctl()
capture_curses_tioctl() - capture curses terminal settings
Definition scriou.c:68
void sig_dfl_mode()
Set signal handlers to default behavior.
Definition sig.c:42
void sig_prog_mode()
Set up signal handlers for interrupt signals.
Definition sig.c:62

References capture_curses_tioctl(), restore_curses_tioctl(), restore_wins(), sig_dfl_mode(), and sig_prog_mode().

Referenced by menu_cmd_processor().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fork_exec()

int fork_exec ( char ** argv)

Fork and exec a command.

Parameters
argv- array of arguments for the command to execute
Returns
the return code from the executed command, or -1 on error

Captures and restores terminal settings around the fork and exec. Sets signal handlers to default in the child process. Waits for the child process to complete in the parent process. Handles errors from fork and execvp, and reports child exit status. Restores curses mode and keypad settings after execution. Restores window states after execution. Uses a temporary string buffer tmp_str for error messages. Uses Perror for error reporting. Uses sig_dfl_mode and sig_prog_mode for signal handling. Uses capture_curses_tioctl and restore_curses_tioctl for terminal settings. Uses restore_shell_tioctl for shell terminal settings. Uses waitpid to wait for the child process. Uses WIFEXITED, WEXITSTATUS, WIFSIGNALED, and WTERMSIG to interpret child status. Uses keypad to manage keypad mode in curses. Uses restore_wins to restore window states. Uses errno for error codes. Uses pid_t for process IDs. Uses standard file descriptors STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO. Uses execvp for executing the command. Uses fork for creating a new process. Uses ssnprintf for formatting error messages. Uses switch-case for handling fork results. Uses default shell if SHELL environment variable is not set.

Prevent child process from writing to terminal

Definition at line 130 of file exec.c.

130 {
131 char tmp_str[MAXLEN];
132 pid_t pid;
133 int status;
134 int rc;
135
136 if (argv[0] == 0) {
137 Perror("fork_exec: missing argument for execvp");
138 return (-1);
139 }
141 curs_set(1);
142 sig_dfl_mode();
143
144 tmp_str[0] = '\0';
145 pid = fork();
146 switch (pid) {
147 case -1: // parent fork failed
149 keypad(stdscr, true);
150 ssnprintf(tmp_str, sizeof(tmp_str), "fork failed: %s, errno: %d",
151 argv[0], errno);
152 Perror(tmp_str);
153 return (-1);
154 case 0: // child
156 // if (flags & F_NO_STDERR) {
157 // int dev_null = open("/dev/null", O_WRONLY);
158 // if (dev_null == -1) {
159 // Perror("open(/dev/null) failed in init_pick child process");
160 // exit(EXIT_FAILURE);
161 // }
162 // dup2(dev_null, STDOUT_FILENO);
163 // dup2(dev_null, STDERR_FILENO);
164 // close(dev_null);
165 // }
167 werase(stdscr);
168 wrefresh(stdscr);
169 execvp(argv[0], argv);
172 keypad(stdscr, true);
173 ssnprintf(tmp_str, sizeof(tmp_str), "execvp failed: %s, errno: %d",
174 argv[0], errno);
175 Perror(tmp_str);
176 exit(-1);
177 default: // parent
178 rc = 0;
181 waitpid(pid, &status, 0);
182 if (WIFEXITED(status)) {
183 rc = WEXITSTATUS(status);
184 if (rc != 0) {
185 keypad(stdscr, true);
186 ssnprintf(tmp_str, sizeof(tmp_str),
187 "Child %s exited with status %d", argv[0], rc);
188 }
189 } else {
190 if (WIFSIGNALED(status)) {
191 rc = WTERMSIG(status);
192 keypad(stdscr, true);
193 ssnprintf(tmp_str, sizeof(tmp_str),
194 "Child %s terminated by signal %d", argv[0], rc);
195 } else {
196 keypad(stdscr, true);
197 ssnprintf(tmp_str, sizeof(tmp_str),
198 "Child %s terminated abnormally", argv[0]);
199 }
200 }
201 break;
202 }
205 touchwin(stdscr);
206 wnoutrefresh(stdscr);
207 wrefresh(stdscr);
208 restore_wins();
209 tmp_str[0] = '\0';
210 if (tmp_str[0] != '\0') {
211 Perror(tmp_str);
212 }
213 return (rc);
214}
#define MAXLEN
Definition curskeys.c:15
int Perror(char *)
Display a simple error message window or print to stderr.
Definition dwin.c:1162
size_t ssnprintf(char *, size_t, const char *,...)
ssnprintf was designed to be a safer alternative to snprintf.
Definition futil.c:311
bool restore_shell_tioctl()
restore_shell_tioctl() - restore shell terminal settings
Definition scriou.c:56

References capture_curses_tioctl(), Perror(), restore_curses_tioctl(), restore_shell_tioctl(), restore_wins(), sig_dfl_mode(), sig_prog_mode(), and ssnprintf().

Referenced by full_screen_fork_exec(), and shell().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ full_screen_fork_exec()

int full_screen_fork_exec ( char ** argv)

Execute a command in full screen mode.

Parameters
argv- array of arguments for the command to execute
Returns
the return code from the executed command

Clear the screen, move the cursor to the bottom, and refresh the screen before executing the command. After the command completes, clear the screen, move the cursor to the top, refresh the screen, and restore the windows.

Definition at line 46 of file exec.c.

46 {
47 int rc;
48
49 fflush(stderr);
50 wmove(stdscr, LINES - 1, 0);
51 rc = fork_exec(argv);
52 return (rc);
53}
int fork_exec(char **)
Fork and exec a command.
Definition exec.c:130

References fork_exec().

Referenced by menu_cmd_processor().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ full_screen_shell()

int full_screen_shell ( char * shellCmdPtr)

Execute a shell command in full screen mode.

Parameters
shellCmdPtr- pointer to the shell command string
Returns
the return code from the executed shell command

Clear the screen, move the cursor to the top, and refresh the screen before executing the shell command. After the command completes, restore the windows.

Definition at line 62 of file exec.c.

62 {
63 int rc;
64
65 fflush(stderr);
66 werase(stdscr);
67 wmove(stdscr, 0, 0);
68 wrefresh(stdscr);
69 rc = shell(shellCmdPtr);
70 touchwin(stdscr);
71 wnoutrefresh(stdscr);
73 wrefresh(stdscr);
74 return (rc);
75}
int shell(char *)
Execute a shell command.
Definition exec.c:82

References restore_wins(), and shell().

Referenced by menu_cmd_processor(), and view_cmd_processor().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ shell()

int shell ( char * shellCmdPtr)

Execute a shell command.

Parameters
shellCmdPtr- pointer to the shell command string
Returns
the return code from the executed shell command

Executes the command string using the user's shell. If the SHELL environment variable is not set, use /bin/sh.

Definition at line 82 of file exec.c.

82 {
83 int Eargc;
84 char *Eargv[MAXARGS];
85 char *shellPtr;
86 int rc;
87
88 Eargc = 0;
89 shellPtr = getenv("SHELL");
90 if (shellPtr == nullptr || *shellPtr == '\0')
91 shellPtr = DEFAULTSHELL;
92 Eargv[Eargc++] = strdup(shellPtr);
93 Eargv[Eargc++] = "-c";
94 Eargv[Eargc++] = shellCmdPtr;
95 Eargv[Eargc++] = nullptr;
96 rc = fork_exec(Eargv);
97 free(Eargv[0]);
98 return (rc);
99}
#define DEFAULTSHELL
Definition cm.h:217
#define MAXARGS
Definition cm.h:30

References fork_exec().

Referenced by form_exec_cmd(), full_screen_shell(), lp(), and view_cmd_processor().

Here is the call graph for this function:
Here is the caller graph for this function: