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 21#include <sys/cdefs.h> 22__FBSDID("$FreeBSD$"); 23 24#include <sys/types.h> 25 26#include <dialog.h> 27#include <errno.h> 28#include <sys/wait.h> 29#include "dialog.priv.h" 30 31/* 32 * Display a message box. Program will pause and display an "OK" button 33 * if the parameter 'pause' is non-zero. 34 */ 35int dialog_prgbox(unsigned char *title, const unsigned char *line, int height, int width, int pause, int use_shell) 36{ 37 int i, x, y, key = 0; 38 WINDOW *dialog; 39 FILE *f; 40 const unsigned char *name; 41 unsigned char *s, buf[MAX_LEN]; 42 int status; 43 44 if (height < 0 || width < 0) { 45 endwin(); 46 fprintf(stderr, "\nAutosizing is impossible in dialog_prgbox().\n"); 47 exit(-1); 48 } 49 width = MAX(width,10); 50 51 if (width > COLS) 52 width = COLS; 53 if (height > LINES) 54 height = LINES; 55 /* center dialog box on screen */ 56 x = DialogX ? DialogX : (COLS - width)/2; 57 y = DialogY ? DialogY : (LINES - height)/2; 58 59#ifdef HAVE_NCURSES 60 if (use_shadow) 61 draw_shadow(stdscr, y, x, height, width); 62#endif 63 dialog = newwin(height, width, y, x); 64 if (dialog == NULL) { 65 endwin(); 66 fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x); 67 exit(1); 68 } 69 keypad(dialog, TRUE); 70 71 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr); 72 73 if (title != NULL) { 74 wattrset(dialog, title_attr); 75 wmove(dialog, 0, (width - strlen(title))/2 - 1); 76 waddch(dialog, ' '); 77 waddstr(dialog, title); 78 waddch(dialog, ' '); 79 } 80 wattrset(dialog, dialog_attr); 81 wmove(dialog, 1, 2); 82 83 if (!use_shell) { 84 char cmdline[MAX_LEN]; 85 char *av[51], **ap = av, *val, *p; 86 87 strcpy(cmdline, line); 88 p = cmdline; 89 while ((val = strsep(&p," \t")) != NULL) { 90 if (*val != '\0') 91 *ap++ = val; 92 } 93 *ap = NULL; 94 f = raw_popen(name = av[0], av, "r"); 95 } else 96 f = raw_popen(name = line, NULL, "r"); 97 98 status = -1; 99 if (f == NULL) { 100 err: 101 sprintf(buf, "%s: %s\n", name, strerror(errno)); 102 prr: 103 print_autowrap(dialog, buf, height-(pause?3:1), width-2, width, 1, 2, FALSE, TRUE); 104 wrefresh(dialog); 105 } else { 106 while (fgets(buf, sizeof(buf), f) != NULL) { 107 i = strlen(buf); 108 if (buf[i-1] == '\n') 109 buf[i-1] = '\0'; 110 s = buf; 111 while ((s = strchr(s, '\t')) != NULL) 112 *s++ = ' '; 113 print_autowrap(dialog, buf, height-(pause?3:1), width-2, width, 1, 2, FALSE, TRUE); 114 print_autowrap(dialog, "\n", height-(pause?3:1), width-2, width, 1, 2, FALSE, FALSE); 115 wrefresh(dialog); 116 } 117 if ((status = raw_pclose(f)) == -1) 118 goto err; 119 if (WIFEXITED(status) && WEXITSTATUS(status) == 127) { 120 sprintf(buf, "%s: program not found\n", name); 121 goto prr; 122 } 123 } 124 125 if (pause) { 126 wattrset(dialog, border_attr); 127 wmove(dialog, height-3, 0); 128 waddch(dialog, ACS_LTEE); 129 for (i = 0; i < width-2; i++) 130 waddch(dialog, ACS_HLINE); 131 wattrset(dialog, dialog_attr); 132 waddch(dialog, ACS_RTEE); 133 wmove(dialog, height-2, 1); 134 for (i = 0; i < width-2; i++) 135 waddch(dialog, ' '); 136 display_helpline(dialog, height-1, width); 137 print_button(dialog, " OK ", height-2, width/2-6, TRUE); 138 wrefresh(dialog); 139 while (key != ESC && key != '\n' && key != ' ' && key != '\r') 140 key = wgetch(dialog); 141 if (key == '\r') 142 key = '\n'; 143 } 144 else { 145 key = '\n'; 146 wrefresh(dialog); 147 } 148 149 delwin(dialog); 150 return (status); 151} 152/* End of dialog_msgbox() */ 153