msg.c revision 15242
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 * $Id: msg.c,v 1.30 1995/12/07 10:34:09 peter Exp $
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 <machine/console.h>
41
42#define VTY_STATLINE	24
43#define TTY_STATLINE	23
44
45Boolean
46isDebug(void)
47{
48    char *cp;
49
50    return (cp = variable_get(VAR_DEBUG)) && strcmp(cp, "no");
51}
52
53/* Whack up an informational message on the status line, in stand-out */
54void
55msgYap(char *fmt, ...)
56{
57    va_list args;
58    char *errstr;
59    int attrs;
60
61    errstr = (char *)safe_malloc(FILENAME_MAX);
62    va_start(args, fmt);
63    vsnprintf(errstr, FILENAME_MAX, fmt, args);
64    va_end(args);
65    attrs = getattrs(stdscr);
66    attrset(A_REVERSE);
67    mvaddstr(OnVTY ? VTY_STATLINE : TTY_STATLINE, 0, errstr);
68    attrset(attrs);
69    refresh();
70    free(errstr);
71}
72
73/* Whack up an informational message on the status line */
74void
75msgInfo(char *fmt, ...)
76{
77    va_list args;
78    char *errstr;
79    int i, attrs;
80    char line[81];
81
82    attrs = getattrs(stdscr);
83    /* NULL is a special convention meaning "erase the old stuff" */
84    if (!fmt) {
85	move(OnVTY ? VTY_STATLINE : TTY_STATLINE, 0);
86	attrset(A_REVERSE);
87	clrtoeol();
88	attrset(attrs);
89	return;
90    }
91    errstr = (char *)safe_malloc(FILENAME_MAX);
92    va_start(args, fmt);
93    vsnprintf(errstr, FILENAME_MAX, fmt, args);
94    va_end(args);
95    memset(line, ' ', 80);
96    for (i = 0; i < 80; i++) {
97	if (errstr[i])
98	    line[i] = errstr[i];
99	else
100	    break;
101    }
102    line[80] = '\0';
103    attrset(A_REVERSE);
104    mvaddstr(OnVTY ? VTY_STATLINE : TTY_STATLINE, 0, line);
105    attrset(attrs);
106    move(OnVTY ? VTY_STATLINE : TTY_STATLINE, 79);
107    refresh();
108    if (OnVTY) {
109	if (isDebug())
110	    msgDebug("Information: `%s'\n", errstr);
111	msgInfo(NULL);
112    }
113    free(errstr);
114}
115
116/* Whack up a warning on the status line */
117void
118msgWarn(char *fmt, ...)
119{
120    va_list args;
121    char *errstr;
122    int attrs;
123
124    errstr = (char *)safe_malloc(FILENAME_MAX);
125    strcpy(errstr, "Warning: ");
126    va_start(args, fmt);
127    vsnprintf((char *)(errstr + strlen(errstr)), FILENAME_MAX, fmt, args);
128    va_end(args);
129    attrs = getattrs(stdscr);
130    beep();
131    attrset(A_REVERSE);
132    mvaddstr(OnVTY ? VTY_STATLINE : TTY_STATLINE, 0, errstr);
133    attrset(attrs);
134    refresh();
135    if (OnVTY && isDebug())
136	msgDebug("Warning message `%s'\n", errstr);
137    free(errstr);
138}
139
140/* Whack up an error on the status line */
141void
142msgError(char *fmt, ...)
143{
144    va_list args;
145    char *errstr;
146    int attrs;
147
148    errstr = (char *)safe_malloc(FILENAME_MAX);
149    strcpy(errstr, "Error: ");
150    va_start(args, fmt);
151    vsnprintf((char *)(errstr + strlen(errstr)), FILENAME_MAX, fmt, args);
152    va_end(args);
153    beep();
154    attrs = getattrs(stdscr);
155    attrset(A_REVERSE);
156    mvaddstr(OnVTY ? VTY_STATLINE : TTY_STATLINE, 0, errstr);
157    attrset(attrs);
158    refresh();
159    if (OnVTY && isDebug())
160	msgDebug("Error message `%s'\n", errstr);
161    free(errstr);
162}
163
164/* Whack up a fatal error on the status line */
165void
166msgFatal(char *fmt, ...)
167{
168    va_list args;
169    char *errstr;
170    int attrs;
171
172    errstr = (char *)safe_malloc(FILENAME_MAX);
173    strcpy(errstr, "Fatal Error: ");
174    va_start(args, fmt);
175    vsnprintf((char *)(errstr + strlen(errstr)), FILENAME_MAX, fmt, args);
176    va_end(args);
177    beep();
178    attrs = getattrs(stdscr);
179    attrset(A_REVERSE);
180    mvaddstr(OnVTY ? VTY_STATLINE : TTY_STATLINE, 0, errstr);
181    addstr(" - ");
182    addstr("PRESS ANY KEY TO ");
183    if (getpid() == 1)
184	addstr("REBOOT");
185    else
186	addstr("QUIT");
187    attrset(attrs);
188    refresh();
189    if (OnVTY)
190	msgDebug("Fatal error `%s'!\n", errstr);
191    free(errstr);
192    getch();
193    systemShutdown();
194}
195
196/* Put up a message in a popup confirmation box */
197void
198msgConfirm(char *fmt, ...)
199{
200    va_list args;
201    char *errstr;
202    WINDOW *w;
203
204    errstr = (char *)safe_malloc(FILENAME_MAX);
205    va_start(args, fmt);
206    vsnprintf(errstr, FILENAME_MAX, fmt, args);
207    va_end(args);
208    use_helpline(NULL);
209    use_helpfile(NULL);
210    w = savescr();
211    if (OnVTY) {
212	msgDebug("Switching back to VTY1\n");
213	ioctl(0, VT_ACTIVATE, 1);
214	msgInfo(NULL);
215    }
216    dialog_notify(errstr);
217    restorescr(w);
218    free(errstr);
219}
220
221/* Put up a message in a popup information box */
222void
223msgNotify(char *fmt, ...)
224{
225    va_list args;
226    char *errstr;
227
228    errstr = (char *)safe_malloc(FILENAME_MAX);
229    va_start(args, fmt);
230    vsnprintf(errstr, FILENAME_MAX, fmt, args);
231    va_end(args);
232    use_helpline(NULL);
233    use_helpfile(NULL);
234    if (isDebug())
235	msgDebug("Notify: %s\n", errstr);
236    dialog_clear();
237    dialog_msgbox("Information Dialog", errstr, -1, -1, 0);
238    free(errstr);
239}
240
241/* Put up a message in a popup yes/no box and return 1 for YES, 0 for NO */
242int
243msgYesNo(char *fmt, ...)
244{
245    va_list args;
246    char *errstr;
247    int ret;
248    WINDOW *w;
249
250    errstr = (char *)safe_malloc(FILENAME_MAX);
251    va_start(args, fmt);
252    vsnprintf(errstr, FILENAME_MAX, fmt, args);
253    va_end(args);
254    use_helpline(NULL);
255    use_helpfile(NULL);
256    w = savescr();
257    if (OnVTY) {
258	msgDebug("Switching back to VTY1\n");
259	ioctl(0, VT_ACTIVATE, 1);	/* Switch back */
260	msgInfo(NULL);
261    }
262    ret = dialog_yesno("User Confirmation Requested", errstr, -1, -1);
263    restorescr(w);
264    free(errstr);
265    return ret;
266}
267
268/* Put up a message in an input box and return the value */
269char *
270msgGetInput(char *buf, char *fmt, ...)
271{
272    va_list args;
273    char *errstr;
274    static char input_buffer[256];
275    int rval;
276    WINDOW *w;
277
278    errstr = (char *)safe_malloc(FILENAME_MAX);
279    va_start(args, fmt);
280    vsnprintf(errstr, FILENAME_MAX, fmt, args);
281    va_end(args);
282    use_helpline(NULL);
283    use_helpfile(NULL);
284    if (buf)
285	strcpy(input_buffer, buf);
286    else
287	input_buffer[0] = '\0';
288    w = savescr();
289    if (OnVTY) {
290	msgDebug("Switching back to VTY1\n");
291	ioctl(0, VT_ACTIVATE, 1);	/* Switch back */
292	msgInfo(NULL);
293    }
294    rval = dialog_inputbox("Value Required", errstr, -1, -1, input_buffer);
295    restorescr(w);
296    free(errstr);
297    if (!rval)
298	return input_buffer;
299    else
300	return NULL;
301}
302
303/* Write something to the debugging port */
304void
305msgDebug(char *fmt, ...)
306{
307    va_list args;
308    char *dbg;
309
310    if (DebugFD == -1)
311	return;
312    dbg = (char *)safe_malloc(FILENAME_MAX);
313    strcpy(dbg, "DEBUG: ");
314    va_start(args, fmt);
315    vsnprintf((char *)(dbg + strlen(dbg)), FILENAME_MAX, fmt, args);
316    va_end(args);
317    write(DebugFD, dbg, strlen(dbg));
318    free(dbg);
319}
320
321/* Tell the user there's some output to go look at */
322void
323msgWeHaveOutput(char *fmt, ...)
324{
325    va_list args;
326    char *errstr;
327
328    errstr = (char *)safe_malloc(FILENAME_MAX);
329    va_start(args, fmt);
330    vsnprintf(errstr, FILENAME_MAX, fmt, args);
331    va_end(args);
332    use_helpline(NULL);
333    use_helpfile(NULL);
334    msgDebug("Notify: %s\n", errstr);
335    dialog_clear();
336    dialog_msgbox("Information Dialog", errstr, -1, -1, 0);
337    free(errstr);
338    if (OnVTY)
339	msgInfo("Command output is on VTY2 - type ALT-F2 to see it");
340}
341
342/* Simple versions of msgConfirm() and msgNotify() for calling from scripts */
343int
344msgSimpleConfirm(char *str)
345{
346    msgConfirm(str);
347    return DITEM_SUCCESS;
348}
349
350int
351msgSimpleNotify(char *str)
352{
353    msgNotify(str);
354    return DITEM_SUCCESS;
355}
356