msg.c revision 80674
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 80674 2001-07-30 20:45:54Z jkh $
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    if (variable_get(VAR_NONINTERACTIVE))
241	return 1;	/* If non-interactive, return YES all the time */
242    ret = dialog_yesno("User Confirmation Requested", errstr, -1, -1);
243    restorescr(w);
244    return ret;
245}
246
247/* Put up a message in a popup no/yes box and return 1 for YES, 0 for NO */
248int
249msgNoYes(char *fmt, ...)
250{
251    va_list args;
252    char *errstr;
253    int ret;
254    WINDOW *w = savescr();
255
256    errstr = (char *)alloca(FILENAME_MAX);
257    va_start(args, fmt);
258    vsnprintf(errstr, FILENAME_MAX, fmt, args);
259    va_end(args);
260    use_helpline(NULL);
261    use_helpfile(NULL);
262    if (OnVTY) {
263	ioctl(0, VT_ACTIVATE, 1);	/* Switch back */
264	msgInfo(NULL);
265    }
266    if (variable_get(VAR_NONINTERACTIVE))
267	return 0;	/* If non-interactive, return NO all the time */
268    ret = dialog_noyes("User Confirmation Requested", errstr, -1, -1);
269    restorescr(w);
270    return ret;
271}
272
273/* Put up a message in an input box and return the value */
274char *
275msgGetInput(char *buf, char *fmt, ...)
276{
277    va_list args;
278    char *errstr;
279    static char input_buffer[256];
280    int rval;
281    WINDOW *w = savescr();
282
283    errstr = (char *)alloca(FILENAME_MAX);
284    va_start(args, fmt);
285    vsnprintf(errstr, FILENAME_MAX, fmt, args);
286    va_end(args);
287    use_helpline(NULL);
288    use_helpfile(NULL);
289    if (buf)
290	SAFE_STRCPY(input_buffer, buf);
291    else
292	input_buffer[0] = '\0';
293    if (OnVTY) {
294	ioctl(0, VT_ACTIVATE, 1);	/* Switch back */
295	msgInfo(NULL);
296    }
297    rval = dialog_inputbox("Value Required", errstr, -1, -1, input_buffer);
298    restorescr(w);
299    if (!rval)
300	return input_buffer;
301    else
302	return NULL;
303}
304
305/* Write something to the debugging port */
306void
307msgDebug(char *fmt, ...)
308{
309    va_list args;
310    char *dbg;
311
312    if (DebugFD == -1)
313	return;
314    dbg = (char *)alloca(FILENAME_MAX);
315    strcpy(dbg, "DEBUG: ");
316    va_start(args, fmt);
317    vsnprintf((char *)(dbg + strlen(dbg)), FILENAME_MAX, fmt, args);
318    va_end(args);
319    write(DebugFD, dbg, strlen(dbg));
320}
321
322/* Tell the user there's some output to go look at */
323void
324msgWeHaveOutput(char *fmt, ...)
325{
326    va_list args;
327    char *errstr;
328    WINDOW *w = savescr();
329
330    errstr = (char *)alloca(FILENAME_MAX);
331    va_start(args, fmt);
332    vsnprintf(errstr, FILENAME_MAX, fmt, args);
333    va_end(args);
334    use_helpline(NULL);
335    use_helpfile(NULL);
336    msgDebug("Notify: %s\n", errstr);
337    dialog_clear_norefresh();
338    sleep(2);
339    dialog_msgbox(NULL, errstr, -1, -1, 0);
340    restorescr(w);
341}
342
343/* Simple versions of msgConfirm() and msgNotify() for calling from scripts */
344int
345msgSimpleConfirm(char *str)
346{
347    msgConfirm("%s", str);
348    return DITEM_SUCCESS;
349}
350
351int
352msgSimpleNotify(char *str)
353{
354    msgNotify("%s", str);
355    return DITEM_SUCCESS;
356}
357