15382d832SPeter Avalos /*
2*a8e38dc0SAntonio Huete Jimenez * $Id: prgbox.c,v 1.15 2022/04/03 22:38:16 tom Exp $
35382d832SPeter Avalos *
45382d832SPeter Avalos * prgbox.c -- implements the prg box
55382d832SPeter Avalos *
6*a8e38dc0SAntonio Huete Jimenez * Copyright 2011-2019,2022 Thomas E. Dickey
75382d832SPeter Avalos *
85382d832SPeter Avalos * This program is free software; you can redistribute it and/or modify
95382d832SPeter Avalos * it under the terms of the GNU Lesser General Public License, version 2.1
105382d832SPeter Avalos * as published by the Free Software Foundation.
115382d832SPeter Avalos *
125382d832SPeter Avalos * This program is distributed in the hope that it will be useful, but
135382d832SPeter Avalos * WITHOUT ANY WARRANTY; without even the implied warranty of
145382d832SPeter Avalos * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
155382d832SPeter Avalos * Lesser General Public License for more details.
165382d832SPeter Avalos *
175382d832SPeter Avalos * You should have received a copy of the GNU Lesser General Public
185382d832SPeter Avalos * License along with this program; if not, write to
195382d832SPeter Avalos * Free Software Foundation, Inc.
205382d832SPeter Avalos * 51 Franklin St., Fifth Floor
215382d832SPeter Avalos * Boston, MA 02110, USA.
225382d832SPeter Avalos */
235382d832SPeter Avalos
24*a8e38dc0SAntonio Huete Jimenez #include <dlg_internals.h>
255382d832SPeter Avalos
265382d832SPeter Avalos static void
reapchild(int sig)275382d832SPeter Avalos reapchild(int sig)
285382d832SPeter Avalos {
295382d832SPeter Avalos (void) sig;
305382d832SPeter Avalos }
315382d832SPeter Avalos
325382d832SPeter Avalos /*
335382d832SPeter Avalos * Open a pipe which ties stderr and stdout together.
345382d832SPeter Avalos */
355940c9abSDaniel Fojt FILE *
dlg_popen(const char * command,const char * type)365382d832SPeter Avalos dlg_popen(const char *command, const char *type)
375382d832SPeter Avalos {
385382d832SPeter Avalos FILE *result = 0;
395382d832SPeter Avalos int fd[2];
405382d832SPeter Avalos
415940c9abSDaniel Fojt if ((*type == 'r' || *type == 'w') && pipe(fd) == 0) {
425940c9abSDaniel Fojt char *blob;
435940c9abSDaniel Fojt
445382d832SPeter Avalos switch (fork()) {
455382d832SPeter Avalos case -1: /* Error. */
465382d832SPeter Avalos (void) close(fd[0]);
475382d832SPeter Avalos (void) close(fd[1]);
485382d832SPeter Avalos break;
495382d832SPeter Avalos case 0: /* child. */
505382d832SPeter Avalos if (*type == 'r') {
515382d832SPeter Avalos if (fd[1] != STDOUT_FILENO) {
525382d832SPeter Avalos (void) dup2(fd[1], STDOUT_FILENO);
535382d832SPeter Avalos (void) close(fd[1]);
545382d832SPeter Avalos }
555382d832SPeter Avalos (void) dup2(STDOUT_FILENO, STDERR_FILENO);
565382d832SPeter Avalos (void) close(fd[0]);
575382d832SPeter Avalos } else {
585382d832SPeter Avalos if (fd[0] != STDIN_FILENO) {
595382d832SPeter Avalos (void) dup2(fd[0], STDIN_FILENO);
605382d832SPeter Avalos (void) close(fd[0]);
615382d832SPeter Avalos }
625382d832SPeter Avalos (void) close(fd[1]);
635382d832SPeter Avalos (void) close(STDERR_FILENO);
645382d832SPeter Avalos }
655382d832SPeter Avalos /*
665382d832SPeter Avalos * Bourne shell needs "-c" option to force it to use only the
675382d832SPeter Avalos * given command. Also, it needs the command to be parsed into
685382d832SPeter Avalos * tokens.
695382d832SPeter Avalos */
701ef6786aSJohn Marino if ((blob = malloc(10 + strlen(command))) != 0) {
715940c9abSDaniel Fojt char **argv;
721ef6786aSJohn Marino sprintf(blob, "sh -c \"%s\"", command);
735382d832SPeter Avalos argv = dlg_string_to_argv(blob);
745382d832SPeter Avalos execvp("sh", argv);
755382d832SPeter Avalos }
765382d832SPeter Avalos _exit(127);
775382d832SPeter Avalos /* NOTREACHED */
785382d832SPeter Avalos default: /* parent */
795382d832SPeter Avalos if (*type == 'r') {
805382d832SPeter Avalos result = fdopen(fd[0], type);
815382d832SPeter Avalos (void) close(fd[1]);
825382d832SPeter Avalos } else {
835382d832SPeter Avalos result = fdopen(fd[1], type);
845382d832SPeter Avalos (void) close(fd[0]);
855382d832SPeter Avalos }
865382d832SPeter Avalos break;
875382d832SPeter Avalos }
885382d832SPeter Avalos }
895382d832SPeter Avalos
905382d832SPeter Avalos return result;
915382d832SPeter Avalos }
925382d832SPeter Avalos
935382d832SPeter Avalos /*
945382d832SPeter Avalos * Display text from a pipe in a scrolling window.
955382d832SPeter Avalos */
965382d832SPeter Avalos int
dialog_prgbox(const char * title,const char * cprompt,const char * command,int height,int width,int pauseopt)975382d832SPeter Avalos dialog_prgbox(const char *title,
985382d832SPeter Avalos const char *cprompt,
995382d832SPeter Avalos const char *command,
1005382d832SPeter Avalos int height,
1015382d832SPeter Avalos int width,
1025382d832SPeter Avalos int pauseopt)
1035382d832SPeter Avalos {
1045382d832SPeter Avalos int code;
1055382d832SPeter Avalos FILE *fp;
1065382d832SPeter Avalos void (*oldreaper) (int) = signal(SIGCHLD, reapchild);
1075382d832SPeter Avalos
1085382d832SPeter Avalos fp = dlg_popen(command, "r");
1095382d832SPeter Avalos if (fp == NULL)
1105382d832SPeter Avalos dlg_exiterr("pipe open failed: %s", command);
1115382d832SPeter Avalos
1125382d832SPeter Avalos code = dlg_progressbox(title, cprompt, height, width, pauseopt, fp);
1135382d832SPeter Avalos
1145382d832SPeter Avalos pclose(fp);
1155382d832SPeter Avalos signal(SIGCHLD, oldreaper);
1165382d832SPeter Avalos
1175382d832SPeter Avalos return code;
1185382d832SPeter Avalos }
119