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 fork_detach_execvp (char **eargv)
 Fork, set new session ID, close files, and execute detached command.
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

◆ fork_detach_execvp()

int fork_detach_execvp ( char ** eargv)

Fork, set new session ID, close files, and execute detached command.

Fork, detach, and exec a command.

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

Sets the new session ID. 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
Tested 2026-06-05 on Linux - appears to be functioning properly
Parameters
eargv- array of arguments for the command to execute
Returns
0 on success, or exits on failure

Forks a new process, detaches it from the terminal, and executes the specified command using execvp. Closes standard input, output, and error file descriptors in the child process. Redirects standard input, output, and error to /dev/null in the child process. Closes all other file descriptors in the child process. Sets the session ID for the child process to detach it from the terminal. Restores curses mode and signal handling in the parent process after forking. Restores window states in the parent process after forking.

Definition at line 29 of file detach.c.

29 {
30 pid_t pid = fork();
31
32 if (pid < 0) {
33 fprintf(stderr, "First fork failed: %s\n", strerror(errno));
34 exit(EXIT_FAILURE);
35 }
36 if (pid == 0) {
37 if (setsid() < 0) {
38 fprintf(stderr, "Set session ID failed: %s\n", strerror(errno));
39 exit(EXIT_FAILURE);
40 }
41 close(STDIN_FILENO);
42 close(STDOUT_FILENO);
43 close(STDERR_FILENO);
44 int dev_null = open("/dev/null", O_RDWR);
45 if (dev_null != -1) {
46 dup2(dev_null, STDIN_FILENO);
47 dup2(dev_null, STDOUT_FILENO);
48 dup2(dev_null, STDERR_FILENO);
49 if (dev_null > 2) {
50 close(dev_null);
51 }
52 }
53 long max_fd = sysconf(_SC_OPEN_MAX);
54 for (long fd = 3; fd < max_fd; fd++)
55 close(fd);
56 execvp(eargv[0], eargv);
57 perror("execvp failed");
58 exit(EXIT_FAILURE);
59 }
60 return 0;
61}
char * eargv[MAXARGS]
Definition futil.c:58

Referenced by main(), and menu_cmd_processor().

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.

Definition at line 126 of file exec.c.

126 {
127 char tmp_str[MAXLEN];
128 pid_t pid;
129 int status;
130 int rc;
131
132 if (argv[0] == 0) {
133 Perror("fork_exec: missing argument for execvp");
134 return (-1);
135 }
137 curs_set(1);
138 sig_dfl_mode();
139 stdio_names(stdio_names_str, "exec.c:139");
140 endwin();
141 stdio_names(stdio_names_str, "exec.c:141");
142 tmp_str[0] = '\0';
143 pid = fork();
144 if (pid < 0) {
146 keypad(stdscr, true);
147 ssnprintf(tmp_str, sizeof(tmp_str), "fork failed: %s, errno: %d",
148 argv[0], errno);
149 Perror(tmp_str);
150 return (-1);
151 } else if (pid == 0) {
153 sig_dfl_mode();
154 execvp(argv[0], argv);
155 fprintf(stderr, "execvp failed: %s, errno: %d\n", argv[0], errno);
156 exit(EXIT_FAILURE);
157 }
158 waitpid(pid, &status, 0);
159 if (WIFEXITED(status)) {
160 rc = WEXITSTATUS(status);
161 } else if (WIFSIGNALED(status)) {
162 ssnprintf(tmp_str, sizeof(tmp_str), "Child process terminated by signal: %d",
163 WTERMSIG(status));
164 Perror(tmp_str);
165 rc = -1;
166 } else {
167 ssnprintf(tmp_str, sizeof(tmp_str), "Child process terminated abnormally");
168 Perror(tmp_str);
169 rc = -1;
170 }
171 reset_prog_mode();
172 restore_wins();
173 return (rc);
174}
char * stdio_names(char *, char *)
Definition futil.c:1064
char stdio_names_str[MAXLEN]
Definition futil.c:134
#define MAXLEN
Definition curskeys.c:15
void restore_wins()
Restore all windows after a screen resize.
Definition dwin.c:1225
int Perror(char *)
Display a simple error message window or print to stderr.
Definition dwin.c:1526
size_t ssnprintf(char *, size_t, const char *,...)
ssnprintf was designed to be a safer alternative to snprintf.
Definition futil.c:420
bool capture_curses_tioctl()
capture_curses_tioctl() - capture curses terminal settings
Definition scriou.c:68
bool restore_shell_tioctl()
restore_shell_tioctl() - restore shell terminal settings
Definition scriou.c:56
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(), Perror(), restore_shell_tioctl(), restore_wins(), sig_dfl_mode(), sig_prog_mode(), ssnprintf(), stdio_names(), and stdio_names_str.

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 update the screen before executing the command. After the command completes, clear the screen, move the cursor to the top, update 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:126

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 update 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 rc = shell(shellCmdPtr);
70 return (rc);
71}
int shell(char *)
Execute a shell command.
Definition exec.c:78

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 78 of file exec.c.

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

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: