xref: /freebsd/contrib/dialog/prgbox.c (revision 197186496661d8f63639b5b27b33851f727d4da7)
1 /*
2  *  $Id: prgbox.c,v 1.7 2011/06/30 20:44:13 tom Exp $
3  *
4  *  prgbox.c -- implements the prg box
5  *
6  *  Copyright 2011	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 <dialog.h>
25 
26 /*
27  * Open a pipe which ties stderr and stdout together.
28  */
29 static FILE *
30 dlg_popen(const char *command, const char *type)
31 {
32     FILE *result = 0;
33     int fd[2];
34     int pid;
35     const char *argv[4];
36 
37     if ((*type == 'r' || *type != 'w') && pipe(fd) == 0) {
38 	switch (pid = fork()) {
39 	case -1:		/* Error. */
40 	    (void) close(fd[0]);
41 	    (void) close(fd[1]);
42 	    break;
43 	case 0:		/* child. */
44 	    if (*type == 'r') {
45 		if (fd[1] != STDOUT_FILENO) {
46 		    (void) dup2(fd[1], STDOUT_FILENO);
47 		    (void) close(fd[1]);
48 		}
49 		(void) dup2(STDOUT_FILENO, STDERR_FILENO);
50 		(void) close(fd[0]);
51 	    } else {
52 		if (fd[0] != STDIN_FILENO) {
53 		    (void) dup2(fd[0], STDIN_FILENO);
54 		    (void) close(fd[0]);
55 		}
56 		(void) close(fd[1]);
57 		(void) close(STDERR_FILENO);
58 	    }
59 	    /*
60 	     * Bourne shell needs "-c" option to force it to use only the
61 	     * given command.  Also, it needs the command to be parsed into
62 	     * tokens.
63 	     */
64 	    argv[0] = "sh";
65 	    argv[1] = "-c";
66 	    argv[2] = command;
67 	    argv[3] = NULL;
68 	    execvp("sh", (char **)argv);
69 	    _exit(127);
70 	    /* NOTREACHED */
71 	default:		/* parent */
72 	    if (*type == 'r') {
73 		result = fdopen(fd[0], type);
74 		(void) close(fd[1]);
75 	    } else {
76 		result = fdopen(fd[1], type);
77 		(void) close(fd[0]);
78 	    }
79 	    break;
80 	}
81     }
82 
83     return result;
84 }
85 
86 /*
87  * Display text from a pipe in a scrolling window.
88  */
89 int
90 dialog_prgbox(const char *title,
91 	      const char *cprompt,
92 	      const char *command,
93 	      int height,
94 	      int width,
95 	      int pauseopt)
96 {
97     int code;
98     FILE *fp;
99 
100     fp = dlg_popen(command, "r");
101     if (fp == NULL)
102 	dlg_exiterr("pipe open failed: %s", command);
103 
104     code = dlg_progressbox(title, cprompt, height, width, pauseopt, fp);
105 
106     pclose(fp);
107 
108     return code;
109 }
110