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 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.
int fork_exec (char **argv)
 Fork and exec a command.

Detailed Description

This module provides functions to execute external commands.

Note
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_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
Note
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 128 of file exec.c.

128 {
129 char tmp_str[MAXLEN];
130 pid_t pid;
131 int status;
132 int rc;
133
134 if (argv[0] == 0) {
135 Perror("fork_exec: missing argument for execvp");
136 return (-1);
137 }
139 curs_set(1);
140 sig_dfl_mode();
141
142 tmp_str[0] = '\0';
143 pid = fork();
144 switch (pid) {
145 case -1: // parent fork failed
147 keypad(stdscr, true);
148 ssnprintf(tmp_str, sizeof(tmp_str), "fork failed: %s, errno: %d",
149 argv[0], errno);
150 Perror(tmp_str);
151 return (-1);
152 case 0: // child
154 werase(stdscr);
155 wrefresh(stdscr);
156 execvp(argv[0], argv);
159 keypad(stdscr, true);
160 ssnprintf(tmp_str, sizeof(tmp_str), "execvp failed: %s, errno: %d",
161 argv[0], errno);
162 Perror(tmp_str);
163 exit(-1);
164 default: // parent
165 rc = 0;
168 waitpid(pid, &status, 0);
169 if (WIFEXITED(status)) {
170 rc = WEXITSTATUS(status);
171 if (rc != 0) {
172 keypad(stdscr, true);
173 ssnprintf(tmp_str, sizeof(tmp_str),
174 "Child %s exited with status %d", argv[0], rc);
175 }
176 } else {
177 if (WIFSIGNALED(status)) {
178 rc = WTERMSIG(status);
179 keypad(stdscr, true);
180 ssnprintf(tmp_str, sizeof(tmp_str),
181 "Child %s terminated by signal %d", argv[0], rc);
182 } else {
183 keypad(stdscr, true);
184 ssnprintf(tmp_str, sizeof(tmp_str),
185 "Child %s terminated abnormally", argv[0]);
186 }
187 }
188 break;
189 }
192 restore_wins();
193 tmp_str[0] = '\0';
194 if (tmp_str[0] != '\0') {
195 Perror(tmp_str);
196 }
197 return (rc);
198}
#define MAXLEN
Definition curskeys.c:15
void restore_wins()
Restore all windows after a screen resize.
Definition dwin.c:938
int Perror(char *)
Display a simple error message window or print to stderr.
Definition dwin.c:1110
size_t ssnprintf(char *, size_t, const char *,...)
ssnprintf was designed to be a safer alternative to snprintf.
Definition futil.c:147
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
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_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
Note
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 44 of file exec.c.

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

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

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

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
Note
Executes the command string using the user's shell.
If the SHELL environment variable is not set, use /bin/sh.

Definition at line 80 of file exec.c.

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