mtree(8): Fix crc() prototype.
[dragonfly.git] / contrib / dialog / prgbox.c
blob5c8b13f7bde5e12019980ce3d5b04c36abfcfe9c
1 /*
2 * $Id: prgbox.c,v 1.11 2014/09/11 07:56:41 tom Exp $
4 * prgbox.c -- implements the prg box
6 * Copyright 2011-2012,2014 Thomas E. Dickey
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.
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.
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.
24 #include <dialog.h>
26 static void
27 reapchild(int sig)
29 (void) sig;
33 * Open a pipe which ties stderr and stdout together.
35 static FILE *
36 dlg_popen(const char *command, const char *type)
38 FILE *result = 0;
39 int fd[2];
40 char *blob;
41 char **argv;
43 if ((*type == 'r' || *type != 'w') && pipe(fd) == 0) {
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]);
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]);
62 (void) close(fd[1]);
63 (void) close(STDERR_FILENO);
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.
70 if ((blob = malloc(10 + strlen(command))) != 0) {
71 sprintf(blob, "sh -c \"%s\"", command);
72 argv = dlg_string_to_argv(blob);
73 execvp("sh", argv);
75 _exit(127);
76 /* NOTREACHED */
77 default: /* parent */
78 if (*type == 'r') {
79 result = fdopen(fd[0], type);
80 (void) close(fd[1]);
81 } else {
82 result = fdopen(fd[1], type);
83 (void) close(fd[0]);
85 break;
89 return result;
93 * Display text from a pipe in a scrolling window.
95 int
96 dialog_prgbox(const char *title,
97 const char *cprompt,
98 const char *command,
99 int height,
100 int width,
101 int pauseopt)
103 int code;
104 FILE *fp;
105 void (*oldreaper) (int) = signal(SIGCHLD, reapchild);
107 fp = dlg_popen(command, "r");
108 if (fp == NULL)
109 dlg_exiterr("pipe open failed: %s", command);
111 code = dlg_progressbox(title, cprompt, height, width, pauseopt, fp);
113 pclose(fp);
114 signal(SIGCHLD, oldreaper);
116 return code;