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:51

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

122 {
123 char tmp_str[MAXLEN];
124 pid_t pid;
125 int status;
126 int rc;
127
128 if (argv[0] == 0) {
129 Perror("fork_exec: missing argument for execvp");
130 return (-1);
131 }
133 ui_curs_set(1);
134 sig_dfl_mode();
135 stdio_names(stdio_names_str, "exec.c:139");
136 ui_endwin();
137 stdio_names(stdio_names_str, "exec.c:141");
138 tmp_str[0] = '\0';
139 pid = fork();
140 if (pid < 0) {
142 ui_keypad(stdsfc, WIN, true);
143 ssnprintf(tmp_str, sizeof(tmp_str), "fork failed: %s, errno: %d",
144 argv[0], errno);
145 Perror(tmp_str);
146 return (-1);
147 } else if (pid == 0) {
149 sig_dfl_mode();
150 execvp(argv[0], argv);
151 fprintf(stderr, "execvp failed: %s, errno: %d\n", argv[0], errno);
152 exit(EXIT_FAILURE);
153 }
154 waitpid(pid, &status, 0);
155 if (WIFEXITED(status)) {
156 rc = WEXITSTATUS(status);
157 } else if (WIFSIGNALED(status)) {
158 ssnprintf(tmp_str, sizeof(tmp_str), "Child process terminated by signal: %d",
159 WTERMSIG(status));
160 Perror(tmp_str);
161 rc = -1;
162 } else {
163 ssnprintf(tmp_str, sizeof(tmp_str), "Child process terminated abnormally");
164 Perror(tmp_str);
165 rc = -1;
166 }
167 ui_resume();
169 return (rc);
170}
char stdio_names_str[4096]
Definition futil.c:127
char * stdio_names(char *, char *)
Definition futil.c:1057
void ui_restore_wins()
UiSurface * stdsfc
Definition ui_ncurses.c:36
int ui_curs_set(int visibility)
Definition ui_ncurses.c:737
int ui_resume()
Definition ui_ncurses.c:403
void ui_endwin()
Definition ui_ncurses.c:341
int ui_keypad(UiSurface *s, uint w, bool enable)
Definition ui_ncurses.c:635
#define MAXLEN
Definition curskeys.c:15
int Perror(char *emsg_str)
Display a simple error message window or print to stderr.
Definition dwin.c:841
size_t ssnprintf(char *, size_t, const char *,...)
ssnprintf was designed to be a safer alternative to snprintf.
Definition futil.c:413
bool capture_curses_tioctl()
capture_curses_tioctl() - capture curses terminal settings
Definition scriou.c:70
bool restore_shell_tioctl()
restore_shell_tioctl() - restore shell terminal settings
Definition scriou.c:57
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(), sig_dfl_mode(), sig_prog_mode(), ssnprintf(), stdio_names(), stdio_names_str, stdsfc, ui_curs_set(), ui_endwin(), ui_keypad(), ui_restore_wins(), ui_resume(), and WIN.

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

45 {
46 int rc;
47 rc = fork_exec(argv);
48 return (rc);
49}
int fork_exec(char **)
Fork and exec a command.
Definition exec.c:122

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

58 {
59 int rc;
60
61 fflush(stderr);
63 ui_wmove(stdsfc, WIN, 0, 0);
64 rc = shell(shellCmdPtr);
66 return (rc);
67}
int ui_wmove(UiSurface *s, uint w, uint y, uint x)
Definition ui_ncurses.c:732
int ui_werase(UiSurface *s, uint w)
Definition ui_ncurses.c:560
int shell(char *)
Execute a shell command.
Definition exec.c:74

References shell(), stdsfc, ui_restore_wins(), ui_werase(), ui_wmove(), and WIN.

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

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

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: