msg.c revision 66834
1/* 2 * The new sysinstall program. 3 * 4 * This is probably the last program in the `sysinstall' line - the next 5 * generation being essentially a complete rewrite. 6 * 7 * $FreeBSD: head/usr.sbin/sade/msg.c 66834 2000-10-08 21:34:00Z phk $ 8 * 9 * Copyright (c) 1995 10 * Jordan Hubbard. All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer, 17 * verbatim and that no modifications are made prior to this 18 * point in the file. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 23 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 */ 36 37#include "sysinstall.h" 38#include <stdarg.h> 39#include <sys/ioctl.h> 40#include <sys/consio.h> 41 42Boolean 43isDebug(void) 44{ 45 char *cp; 46 47 return (cp = variable_get(VAR_DEBUG)) && strcmp(cp, "no"); 48} 49 50/* Whack up an informational message on the status line, in stand-out */ 51void 52msgYap(char *fmt, ...) 53{ 54 va_list args; 55 char *errstr; 56 int attrs; 57 58 errstr = (char *)alloca(FILENAME_MAX); 59 va_start(args, fmt); 60 vsnprintf(errstr, FILENAME_MAX, fmt, args); 61 va_end(args); 62 attrs = getattrs(stdscr); 63 attrset(A_REVERSE); 64 mvaddstr(StatusLine, 0, errstr); 65 attrset(attrs); 66 refresh(); 67} 68 69/* Whack up an informational message on the status line */ 70void 71msgInfo(char *fmt, ...) 72{ 73 va_list args; 74 char *errstr; 75 int i, attrs; 76 char line[81]; 77 78 attrs = getattrs(stdscr); 79 /* NULL is a special convention meaning "erase the old stuff" */ 80 if (!fmt) { 81 move(StatusLine, 0); 82 clrtoeol(); 83 return; 84 } 85 errstr = (char *)alloca(FILENAME_MAX); 86 va_start(args, fmt); 87 vsnprintf(errstr, FILENAME_MAX, fmt, args); 88 va_end(args); 89 memset(line, ' ', 80); 90 for (i = 0; i < 80; i++) { 91 if (errstr[i]) 92 line[i] = errstr[i]; 93 else 94 break; 95 } 96 line[80] = '\0'; 97 attrset(ATTR_TITLE); 98 mvaddstr(StatusLine, 0, line); 99 attrset(attrs); 100 move(StatusLine, 79); 101 refresh(); 102} 103 104/* Whack up a warning on the status line */ 105void 106msgWarn(char *fmt, ...) 107{ 108 va_list args; 109 char *errstr; 110 int attrs; 111 112 errstr = (char *)alloca(FILENAME_MAX); 113 strcpy(errstr, "Warning: "); 114 va_start(args, fmt); 115 vsnprintf((char *)(errstr + strlen(errstr)), FILENAME_MAX, fmt, args); 116 va_end(args); 117 attrs = getattrs(stdscr); 118 beep(); 119 attrset(ATTR_TITLE); 120 mvaddstr(StatusLine, 0, errstr); 121 attrset(attrs); 122 refresh(); 123 if (OnVTY && isDebug()) 124 msgDebug("Warning message `%s'\n", errstr); 125} 126 127/* Whack up an error on the status line */ 128void 129msgError(char *fmt, ...) 130{ 131 va_list args; 132 char *errstr; 133 int attrs; 134 135 errstr = (char *)alloca(FILENAME_MAX); 136 strcpy(errstr, "Error: "); 137 va_start(args, fmt); 138 vsnprintf((char *)(errstr + strlen(errstr)), FILENAME_MAX, fmt, args); 139 va_end(args); 140 beep(); 141 attrs = getattrs(stdscr); 142 attrset(ATTR_TITLE); 143 mvaddstr(StatusLine, 0, errstr); 144 attrset(attrs); 145 refresh(); 146 if (OnVTY && isDebug()) 147 msgDebug("Error message `%s'\n", errstr); 148} 149 150/* Whack up a fatal error on the status line */ 151void 152msgFatal(char *fmt, ...) 153{ 154 va_list args; 155 char *errstr; 156 int attrs; 157 158 errstr = (char *)alloca(FILENAME_MAX); 159 strcpy(errstr, "Fatal Error: "); 160 va_start(args, fmt); 161 vsnprintf((char *)(errstr + strlen(errstr)), FILENAME_MAX, fmt, args); 162 va_end(args); 163 beep(); 164 attrs = getattrs(stdscr); 165 attrset(ATTR_TITLE); 166 mvaddstr(StatusLine, 0, errstr); 167 addstr(" - "); 168 addstr("PRESS ANY KEY TO "); 169 if (getpid() == 1) 170 addstr("REBOOT"); 171 else 172 addstr("QUIT"); 173 attrset(attrs); 174 refresh(); 175 if (OnVTY) 176 msgDebug("Fatal error `%s'!\n", errstr); 177 getch(); 178 systemShutdown(1); 179} 180 181/* Put up a message in a popup confirmation box */ 182void 183msgConfirm(char *fmt, ...) 184{ 185 va_list args; 186 char *errstr; 187 WINDOW *w = savescr(); 188 189 errstr = (char *)alloca(FILENAME_MAX); 190 va_start(args, fmt); 191 vsnprintf(errstr, FILENAME_MAX, fmt, args); 192 va_end(args); 193 use_helpline(NULL); 194 use_helpfile(NULL); 195 if (OnVTY) { 196 ioctl(0, VT_ACTIVATE, 1); 197 msgInfo(NULL); 198 } 199 dialog_notify(errstr); 200 restorescr(w); 201} 202 203/* Put up a message in a popup information box */ 204void 205msgNotify(char *fmt, ...) 206{ 207 va_list args; 208 char *errstr; 209 210 errstr = (char *)alloca(FILENAME_MAX); 211 va_start(args, fmt); 212 vsnprintf(errstr, FILENAME_MAX, fmt, args); 213 va_end(args); 214 use_helpline(NULL); 215 use_helpfile(NULL); 216 if (isDebug()) 217 msgDebug("Notify: %s\n", errstr); 218 dialog_msgbox(NULL, errstr, -1, -1, 0); 219} 220 221/* Put up a message in a popup yes/no box and return 1 for YES, 0 for NO */ 222int 223msgYesNo(char *fmt, ...) 224{ 225 va_list args; 226 char *errstr; 227 int ret; 228 WINDOW *w = savescr(); 229 230 errstr = (char *)alloca(FILENAME_MAX); 231 va_start(args, fmt); 232 vsnprintf(errstr, FILENAME_MAX, fmt, args); 233 va_end(args); 234 use_helpline(NULL); 235 use_helpfile(NULL); 236 if (OnVTY) { 237 ioctl(0, VT_ACTIVATE, 1); /* Switch back */ 238 msgInfo(NULL); 239 } 240 ret = dialog_yesno("User Confirmation Requested", errstr, -1, -1); 241 restorescr(w); 242 return ret; 243} 244 245/* Put up a message in an input box and return the value */ 246char * 247msgGetInput(char *buf, char *fmt, ...) 248{ 249 va_list args; 250 char *errstr; 251 static char input_buffer[256]; 252 int rval; 253 WINDOW *w = savescr(); 254 255 errstr = (char *)alloca(FILENAME_MAX); 256 va_start(args, fmt); 257 vsnprintf(errstr, FILENAME_MAX, fmt, args); 258 va_end(args); 259 use_helpline(NULL); 260 use_helpfile(NULL); 261 if (buf) 262 SAFE_STRCPY(input_buffer, buf); 263 else 264 input_buffer[0] = '\0'; 265 if (OnVTY) { 266 ioctl(0, VT_ACTIVATE, 1); /* Switch back */ 267 msgInfo(NULL); 268 } 269 rval = dialog_inputbox("Value Required", errstr, -1, -1, input_buffer); 270 restorescr(w); 271 if (!rval) 272 return input_buffer; 273 else 274 return NULL; 275} 276 277/* Write something to the debugging port */ 278void 279msgDebug(char *fmt, ...) 280{ 281 va_list args; 282 char *dbg; 283 284 if (DebugFD == -1) 285 return; 286 dbg = (char *)alloca(FILENAME_MAX); 287 strcpy(dbg, "DEBUG: "); 288 va_start(args, fmt); 289 vsnprintf((char *)(dbg + strlen(dbg)), FILENAME_MAX, fmt, args); 290 va_end(args); 291 write(DebugFD, dbg, strlen(dbg)); 292} 293 294/* Tell the user there's some output to go look at */ 295void 296msgWeHaveOutput(char *fmt, ...) 297{ 298 va_list args; 299 char *errstr; 300 WINDOW *w = savescr(); 301 302 errstr = (char *)alloca(FILENAME_MAX); 303 va_start(args, fmt); 304 vsnprintf(errstr, FILENAME_MAX, fmt, args); 305 va_end(args); 306 use_helpline(NULL); 307 use_helpfile(NULL); 308 msgDebug("Notify: %s\n", errstr); 309 dialog_clear_norefresh(); 310 sleep(2); 311 dialog_msgbox(NULL, errstr, -1, -1, 0); 312 restorescr(w); 313} 314 315/* Simple versions of msgConfirm() and msgNotify() for calling from scripts */ 316int 317msgSimpleConfirm(char *str) 318{ 319 msgConfirm(str); 320 return DITEM_SUCCESS; 321} 322 323int 324msgSimpleNotify(char *str) 325{ 326 msgNotify(str); 327 return DITEM_SUCCESS; 328} 329