Don't return 0 from rl_probe, because rl(4) is not a best match for
[dragonfly.git] / gnu / lib / libdialog / prgbox.c
1 /*
2  *  prgbox.c -- implements the message box and info box
3  *
4  *  AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5  *
6  *  This program is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU General Public License
8  *  as published by the Free Software Foundation; either version 2
9  *  of the License, or (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  * $FreeBSD: src/gnu/lib/libdialog/prgbox.c,v 1.12.6.1 2000/12/15 05:54:36 jkh Exp $
21  * $DragonFly: src/gnu/lib/libdialog/prgbox.c,v 1.2 2003/06/17 04:25:43 dillon Exp $
22  */
23
24
25 #include <sys/types.h>
26
27 #include <dialog.h>
28 #include <errno.h>
29 #include <sys/wait.h>
30 #include "dialog.priv.h"
31
32 /*
33  * Display a message box. Program will pause and display an "OK" button
34  * if the parameter 'pause' is non-zero.
35  */
36 int dialog_prgbox(unsigned char *title, const unsigned char *line, int height, int width, int pause, int use_shell)
37 {
38   int i, x, y, key = 0;
39   WINDOW *dialog;
40   FILE *f;
41   const unsigned char *name;
42   unsigned char *s, buf[MAX_LEN];
43   int status;
44
45   if (height < 0 || width < 0) {
46     endwin();
47     fprintf(stderr, "\nAutosizing is impossible in dialog_prgbox().\n");
48     exit(-1);
49   }
50   width = MAX(width,10);
51
52   if (width > COLS)
53         width = COLS;
54   if (height > LINES)
55         height = LINES;
56   /* center dialog box on screen */
57   x = DialogX ? DialogX : (COLS - width)/2;
58   y = DialogY ? DialogY : (LINES - height)/2;
59
60 #ifdef HAVE_NCURSES
61   if (use_shadow)
62     draw_shadow(stdscr, y, x, height, width);
63 #endif
64   dialog = newwin(height, width, y, x);
65   if (dialog == NULL) {
66     endwin();
67     fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x);
68     exit(1);
69   }
70   keypad(dialog, TRUE);
71
72   draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
73
74   if (title != NULL) {
75     wattrset(dialog, title_attr);
76     wmove(dialog, 0, (width - strlen(title))/2 - 1);
77     waddch(dialog, ' ');
78     waddstr(dialog, title);
79     waddch(dialog, ' ');
80   }
81   wattrset(dialog, dialog_attr);
82   wmove(dialog, 1, 2);
83
84   if (!use_shell) {
85     char cmdline[MAX_LEN];
86     char *av[51], **ap = av, *val, *p;
87
88     strcpy(cmdline, line);
89     p = cmdline;
90     while ((val = strsep(&p," \t")) != NULL) {
91       if (*val != '\0')
92         *ap++ = val;
93     }
94     *ap = NULL;
95     f = raw_popen(name = av[0], av, "r");
96   } else
97     f = raw_popen(name = line, NULL, "r");
98
99   status = -1;
100   if (f == NULL) {
101   err:
102       sprintf(buf, "%s: %s\n", name, strerror(errno));
103   prr:
104       print_autowrap(dialog, buf, height-(pause?3:1), width-2, width, 1, 2, FALSE, TRUE);
105       wrefresh(dialog);
106   } else {
107     while (fgets(buf, sizeof(buf), f) != NULL) {
108       i = strlen(buf);
109       if (buf[i-1] == '\n')
110         buf[i-1] = '\0';
111       s = buf;
112       while ((s = strchr(s, '\t')) != NULL)
113         *s++ = ' ';
114       print_autowrap(dialog, buf, height-(pause?3:1), width-2, width, 1, 2, FALSE, TRUE);
115       print_autowrap(dialog, "\n", height-(pause?3:1), width-2, width, 1, 2, FALSE, FALSE);
116       wrefresh(dialog);
117     }
118     if ((status = raw_pclose(f)) == -1)
119       goto err;
120     if (WIFEXITED(status) && WEXITSTATUS(status) == 127) {
121       sprintf(buf, "%s: program not found\n", name);
122       goto prr;
123     }
124   }
125
126   if (pause) {
127     wattrset(dialog, border_attr);
128     wmove(dialog, height-3, 0);
129     waddch(dialog, ACS_LTEE);
130     for (i = 0; i < width-2; i++)
131       waddch(dialog, ACS_HLINE);
132     wattrset(dialog, dialog_attr);
133     waddch(dialog, ACS_RTEE);
134     wmove(dialog, height-2, 1);
135     for (i = 0; i < width-2; i++)
136     waddch(dialog, ' ');
137     display_helpline(dialog, height-1, width);
138     print_button(dialog, "  OK  ", height-2, width/2-6, TRUE);
139     wrefresh(dialog);
140     while (key != ESC && key != '\n' && key != ' ' && key != '\r')
141       key = wgetch(dialog);
142     if (key == '\r')
143       key = '\n';
144   }
145   else {
146     key = '\n';
147     wrefresh(dialog);
148   }
149
150   delwin(dialog);
151   return (status);
152 }
153 /* End of dialog_msgbox() */