xref: /dragonfly/contrib/dialog/prgbox.c (revision b2dabe2e739bd72461a68ac543307c2dedfb048c)
1 /*
2  *  $Id: prgbox.c,v 1.15 2022/04/03 22:38:16 tom Exp $
3  *
4  *  prgbox.c -- implements the prg box
5  *
6  *  Copyright 2011-2019,2022  Thomas E. Dickey
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU Lesser General Public License, version 2.1
10  *  as published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful, but
13  *  WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this program; if not, write to
19  *        Free Software Foundation, Inc.
20  *        51 Franklin St., Fifth Floor
21  *        Boston, MA 02110, USA.
22  */
23 
24 #include <dlg_internals.h>
25 
26 static void
reapchild(int sig)27 reapchild(int sig)
28 {
29     (void) sig;
30 }
31 
32 /*
33  * Open a pipe which ties stderr and stdout together.
34  */
35 FILE *
dlg_popen(const char * command,const char * type)36 dlg_popen(const char *command, const char *type)
37 {
38     FILE *result = 0;
39     int fd[2];
40 
41     if ((*type == 'r' || *type == 'w') && pipe(fd) == 0) {
42           char *blob;
43 
44           switch (fork()) {
45           case -1:            /* Error. */
46               (void) close(fd[0]);
47               (void) close(fd[1]);
48               break;
49           case 0:             /* child. */
50               if (*type == 'r') {
51                     if (fd[1] != STDOUT_FILENO) {
52                         (void) dup2(fd[1], STDOUT_FILENO);
53                         (void) close(fd[1]);
54                     }
55                     (void) dup2(STDOUT_FILENO, STDERR_FILENO);
56                     (void) close(fd[0]);
57               } else {
58                     if (fd[0] != STDIN_FILENO) {
59                         (void) dup2(fd[0], STDIN_FILENO);
60                         (void) close(fd[0]);
61                     }
62                     (void) close(fd[1]);
63                     (void) close(STDERR_FILENO);
64               }
65               /*
66                * Bourne shell needs "-c" option to force it to use only the
67                * given command.  Also, it needs the command to be parsed into
68                * tokens.
69                */
70               if ((blob = malloc(10 + strlen(command))) != 0) {
71                     char **argv;
72                     sprintf(blob, "sh -c \"%s\"", command);
73                     argv = dlg_string_to_argv(blob);
74                     execvp("sh", argv);
75               }
76               _exit(127);
77               /* NOTREACHED */
78           default:            /* parent */
79               if (*type == 'r') {
80                     result = fdopen(fd[0], type);
81                     (void) close(fd[1]);
82               } else {
83                     result = fdopen(fd[1], type);
84                     (void) close(fd[0]);
85               }
86               break;
87           }
88     }
89 
90     return result;
91 }
92 
93 /*
94  * Display text from a pipe in a scrolling window.
95  */
96 int
dialog_prgbox(const char * title,const char * cprompt,const char * command,int height,int width,int pauseopt)97 dialog_prgbox(const char *title,
98                 const char *cprompt,
99                 const char *command,
100                 int height,
101                 int width,
102                 int pauseopt)
103 {
104     int code;
105     FILE *fp;
106     void (*oldreaper) (int) = signal(SIGCHLD, reapchild);
107 
108     fp = dlg_popen(command, "r");
109     if (fp == NULL)
110           dlg_exiterr("pipe open failed: %s", command);
111 
112     code = dlg_progressbox(title, cprompt, height, width, pauseopt, fp);
113 
114     pclose(fp);
115     signal(SIGCHLD, oldreaper);
116 
117     return code;
118 }
119