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 41 of file detach.c.

41 {
42 pid_t pid = fork();
43
44 if (pid < 0) {
45 fprintf(stderr, _("First fork failed: %s\n"), strerror(errno));
46 exit(EXIT_FAILURE);
47 }
48 if (pid == 0) {
49 if (setsid() < 0) {
50 fprintf(stderr, _("Set session ID failed: %s\n"), strerror(errno));
51 exit(EXIT_FAILURE);
52 }
53 close(STDIN_FILENO);
54 close(STDOUT_FILENO);
55 close(STDERR_FILENO);
56 int dev_null = open("/dev/null", O_RDWR);
57 if (dev_null != -1) {
58 dup2(dev_null, STDIN_FILENO);
59 dup2(dev_null, STDOUT_FILENO);
60 dup2(dev_null, STDERR_FILENO);
61 if (dev_null > 2) {
62 close(dev_null);
63 }
64 }
65 long max_fd = sysconf(_SC_OPEN_MAX);
66 for (long fd = 3; fd < max_fd; fd++)
67 close(fd);
68 execvp(eargv[0], eargv);
69 perror(_("execvp failed"));
70 exit(EXIT_FAILURE);
71 }
72 return 0;
73}
#define _(String)
Definition detach.c:25
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 ui_perror for error reporting. Uses sig_dfl_mode and sig_prog_mode for signal handling. Uses capture_program_tioctl and restore_program_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 ui_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");
137 ui_endwin();
138 stdio_names(stdio_names_str, "exec.c:141");
139 tmp_str[0] = '\0';
140 pid = fork();
141 if (pid < 0) {
143 ui_keypad(stdsfc, WIN, true);
144 ssnprintf(tmp_str, sizeof(tmp_str), _("fork failed: %s, errno: %d"),
145 argv[0], errno);
146 ui_perror(tmp_str);
147 return (-1);
148 } else if (pid == 0) {
150 sig_dfl_mode();
151 execvp(argv[0], argv);
152 fprintf(stderr, _("execvp failed: %s, errno: %d\n"), argv[0], errno);
153 exit(EXIT_FAILURE);
154 }
155 waitpid(pid, &status, 0);
156 if (WIFEXITED(status)) {
157 rc = WEXITSTATUS(status);
158 } else if (WIFSIGNALED(status)) {
159 ssnprintf(tmp_str, sizeof(tmp_str), _("Child process terminated by signal: %d"),
160 WTERMSIG(status));
161 ui_perror(tmp_str);
162 rc = -1;
163 } else {
164 ssnprintf(tmp_str, sizeof(tmp_str), _("Child process terminated abnormally"));
165 ui_perror(tmp_str);
166 rc = -1;
167 }
168 ui_resume();
170 return (rc);
171}
char stdio_names_str[4096]
Definition futil.c:122
char * stdio_names(char *, char *)
Definition futil.c:1083
void ui_restore_wins()
Restore the state of all windows in the UI. This function refreshes the standard screen and all surfa...
UiSurface * stdsfc
Definition ui_ncurses.c:46
int ui_curs_set(int visibility)
Set the visibility of the cursor.
Definition ui_ncurses.c:840
int ui_resume()
Resume the UI, restoring the previous program state and updating the display.
Definition ui_ncurses.c:503
void ui_endwin()
End the UI application, cleaning up resources and shutting down the UI runtime.
Definition ui_ncurses.c:412
int ui_keypad(UiSurface *s, ss_t w, bool enable)
Enable or disable the keypad for a UiSurface.
Definition ui_ncurses.c:771
void ui_def_prog_mode()
Definition ui_ncurses.c:826
#define MAXLEN
Definition curskeys.c:15
#define _(String)
Definition amort.c:23
size_t ssnprintf(char *, size_t, const char *,...)
ssnprintf was designed to be a safer alternative to snprintf.
Definition futil.c:390
bool restore_shell_tioctl()
restore_shell_tioctl() - restore shell terminal settings
Definition scriou.c:57
bool capture_program_tioctl()
capture_program_tioctl() - capture program terminal settings
Definition scriou.c:70
void sig_dfl_mode()
Set signal handlers to default behavior.
Definition sig.c:41
void sig_prog_mode()
Set up signal handlers for interrupt signals.
Definition sig.c:61
int ui_perror(char *emsg_str)
Display a simple error message window or print to stderr.
Definition ui_common.c:1257

References capture_program_tioctl(), restore_shell_tioctl(), sig_dfl_mode(), sig_prog_mode(), ssnprintf(), stdio_names(), stdio_names_str, stdsfc, ui_curs_set(), ui_def_prog_mode(), ui_endwin(), ui_keypad(), ui_perror(), ui_restore_wins(), and ui_resume().

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);
62 ui_werase(stdsfc, WIN);
63 ui_wmove(stdsfc, WIN, 0, 0);
64 rc = shell(shellCmdPtr);
66 return (rc);
67}
int ui_wmove(UiSurface *s, ss_t w, uint y, uint x)
Move the cursor to a specified position within a UiSurface.
Definition ui_ncurses.c:653
int ui_werase(UiSurface *s, ss_t w)
Erase the contents of a UiSurface or all surfaces.
Definition ui_ncurses.c:551
int shell(char *)
Execute a shell command.
Definition exec.c:74

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

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:262
#define MAXARGS
Definition cm.h:64

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: