msg.c revision 8649
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.19 1995/05/20 13:24:34 jkh 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 * 3. All advertising materials mentioning features or use of this software
23 *    must display the following acknowledgement:
24 *	This product includes software developed by Jordan Hubbard
25 *	for the FreeBSD Project.
26 * 4. The name of Jordan Hubbard or the FreeBSD project may not be used to
27 *    endorse or promote products derived from this software without specific
28 *    prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 */
43
44#include "sysinstall.h"
45#include <stdarg.h>
46
47/* Whack up an informational message on the status line, in stand-out */
48void
49msgYap(char *fmt, ...)
50{
51    va_list args;
52    char *errstr;
53    int attrs;
54
55    errstr = (char *)safe_malloc(FILENAME_MAX);
56    va_start(args, fmt);
57    vsnprintf(errstr, FILENAME_MAX, fmt, args);
58    va_end(args);
59    attrs = getattrs(stdscr);
60    attrset(A_REVERSE);
61    mvaddstr(23, 0, errstr);
62    attrset(attrs);
63    refresh();
64    free(errstr);
65}
66
67/* Whack up an informational message on the status line */
68void
69msgInfo(char *fmt, ...)
70{
71    va_list args;
72    char *errstr;
73    int attrs;
74
75    /* NULL is a special convention meaning "erase the old stuff" */
76    if (!fmt) {
77	move(23, 0);
78	clrtoeol();
79	return;
80    }
81    errstr = (char *)safe_malloc(FILENAME_MAX);
82    va_start(args, fmt);
83    vsnprintf(errstr, FILENAME_MAX, fmt, args);
84    va_end(args);
85    attrs = getattrs(stdscr);
86    attrset(A_NORMAL);
87    mvaddstr(23, 0, errstr);
88    attrset(attrs);
89    refresh();
90    if (OnVTY) {
91	msgDebug("Informational message `%s'\n", errstr);
92	msgInfo(NULL);
93    }
94    free(errstr);
95}
96
97/* Whack up a warning on the status line */
98void
99msgWarn(char *fmt, ...)
100{
101    va_list args;
102    char *errstr;
103    int attrs;
104
105    errstr = (char *)safe_malloc(FILENAME_MAX);
106    strcpy(errstr, "Warning: ");
107    va_start(args, fmt);
108    vsnprintf((char *)(errstr + strlen(errstr)), FILENAME_MAX, fmt, args);
109    va_end(args);
110    attrs = getattrs(stdscr);
111    beep();
112    attrset(A_REVERSE);
113    mvaddstr(23, 0, errstr);
114    attrset(attrs);
115    refresh();
116    if (OnVTY)
117	msgDebug("Warning message `%s'\n", errstr);
118    free(errstr);
119}
120
121/* Whack up an error on the status line */
122void
123msgError(char *fmt, ...)
124{
125    va_list args;
126    char *errstr;
127    int attrs;
128
129    errstr = (char *)safe_malloc(FILENAME_MAX);
130    strcpy(errstr, "Error: ");
131    va_start(args, fmt);
132    vsnprintf((char *)(errstr + strlen(errstr)), FILENAME_MAX, fmt, args);
133    va_end(args);
134    beep();
135    attrs = getattrs(stdscr);
136    attrset(A_REVERSE);
137    mvaddstr(23, 0, errstr);
138    attrset(attrs);
139    refresh();
140    if (OnVTY)
141	msgDebug("Error message `%s'\n", errstr);
142    free(errstr);
143}
144
145/* Whack up a fatal error on the status line */
146void
147msgFatal(char *fmt, ...)
148{
149    va_list args;
150    char *errstr;
151    int attrs;
152
153    errstr = (char *)safe_malloc(FILENAME_MAX);
154    strcpy(errstr, "Fatal Error: ");
155    va_start(args, fmt);
156    vsnprintf((char *)(errstr + strlen(errstr)), FILENAME_MAX, fmt, args);
157    va_end(args);
158    beep();
159    attrs = getattrs(stdscr);
160    attrset(A_REVERSE);
161    mvaddstr(23, 0, errstr);
162    addstr(" - ");
163    addstr("PRESS ANY KEY TO ");
164    if (getpid() == 1)
165	addstr("REBOOT");
166    else
167	addstr("QUIT");
168    attrset(attrs);
169    refresh();
170    if (OnVTY)
171	msgDebug("Fatal error `%s'!\n", errstr);
172    free(errstr);
173    getch();
174    systemShutdown();
175}
176
177/* Put up a message in a popup confirmation box */
178void
179msgConfirm(char *fmt, ...)
180{
181    va_list args;
182    char *errstr;
183    WINDOW *w;
184
185    errstr = (char *)safe_malloc(FILENAME_MAX);
186    va_start(args, fmt);
187    vsnprintf(errstr, FILENAME_MAX, fmt, args);
188    va_end(args);
189    use_helpline(NULL);
190    use_helpfile(NULL);
191    w = dupwin(newscr);
192    if (OnVTY) {
193	msgDebug("User confirmation requested (type ALT-F1)\n");
194	msgInfo(NULL);
195    }
196    dialog_notify(errstr);
197    touchwin(w);
198    wrefresh(w);
199    delwin(w);
200    free(errstr);
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 *)safe_malloc(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    msgDebug("Notify: %s\n", errstr);
217    dialog_clear();
218    dialog_msgbox("Information Dialog", errstr, -1, -1, 0);
219    free(errstr);
220}
221
222/* Put up a message in a popup yes/no box and return 1 for YES, 0 for NO */
223int
224msgYesNo(char *fmt, ...)
225{
226    va_list args;
227    char *errstr;
228    int ret;
229    WINDOW *w;
230
231    errstr = (char *)safe_malloc(FILENAME_MAX);
232    va_start(args, fmt);
233    vsnprintf(errstr, FILENAME_MAX, fmt, args);
234    va_end(args);
235    use_helpline(NULL);
236    use_helpfile(NULL);
237    w = dupwin(newscr);
238    if (OnVTY) {
239	msgDebug("User decision requested (type ALT-F1)\n");
240	msgInfo(NULL);
241    }
242    ret = dialog_yesno("User Confirmation Requested", errstr, -1, -1);
243    touchwin(w);
244    wrefresh(w);
245    delwin(w);
246    free(errstr);
247    return ret;
248}
249
250/* Put up a message in an input box and return the value */
251char *
252msgGetInput(char *buf, char *fmt, ...)
253{
254    va_list args;
255    char *errstr;
256    static char input_buffer[256];
257    int rval;
258    WINDOW *w;
259
260    errstr = (char *)safe_malloc(FILENAME_MAX);
261    va_start(args, fmt);
262    vsnprintf(errstr, FILENAME_MAX, fmt, args);
263    va_end(args);
264    use_helpline(NULL);
265    use_helpfile(NULL);
266    if (buf)
267	strcpy(input_buffer, buf);
268    else
269	input_buffer[0] = '\0';
270    w = dupwin(newscr);
271    if (OnVTY) {
272	msgDebug("User input requested (type ALT-F1)\n");
273	msgInfo(NULL);
274    }
275    rval = dialog_inputbox("Value Required", errstr, -1, -1, input_buffer);
276    touchwin(w);
277    wrefresh(w);
278    delwin(w);
279    free(errstr);
280    if (!rval)
281	return input_buffer;
282    else
283	return NULL;
284}
285
286/* Write something to the debugging port */
287void
288msgDebug(char *fmt, ...)
289{
290    va_list args;
291    char *dbg;
292
293    if (DebugFD == -1)
294	return;
295    dbg = (char *)safe_malloc(FILENAME_MAX);
296    strcpy(dbg, "DEBUG: ");
297    va_start(args, fmt);
298    vsnprintf((char *)(dbg + strlen(dbg)), FILENAME_MAX, fmt, args);
299    va_end(args);
300    write(DebugFD, dbg, strlen(dbg));
301    free(dbg);
302}
303