msg.c revision 8641
1105250Srobert/*
2105250Srobert * The new sysinstall program.
3105250Srobert *
4105250Srobert * This is probably the last program in the `sysinstall' line - the next
5105250Srobert * generation being essentially a complete rewrite.
6105250Srobert *
7105250Srobert * $Id: msg.c,v 1.17 1995/05/20 08:31:42 jkh Exp $
8105250Srobert *
9105250Srobert * Copyright (c) 1995
10105250Srobert *	Jordan Hubbard.  All rights reserved.
11105250Srobert *
12105250Srobert * Redistribution and use in source and binary forms, with or without
13105250Srobert * modification, are permitted provided that the following conditions
14105250Srobert * are met:
15105250Srobert * 1. Redistributions of source code must retain the above copyright
16105250Srobert *    notice, this list of conditions and the following disclaimer,
17105250Srobert *    verbatim and that no modifications are made prior to this
18105250Srobert *    point in the file.
19105250Srobert * 2. Redistributions in binary form must reproduce the above copyright
20105250Srobert *    notice, this list of conditions and the following disclaimer in the
21105250Srobert *    documentation and/or other materials provided with the distribution.
22105250Srobert * 3. All advertising materials mentioning features or use of this software
23105250Srobert *    must display the following acknowledgement:
24105250Srobert *	This product includes software developed by Jordan Hubbard
25105250Srobert *	for the FreeBSD Project.
26105250Srobert * 4. The name of Jordan Hubbard or the FreeBSD project may not be used to
27105250Srobert *    endorse or promote products derived from this software without specific
28105250Srobert *    prior written permission.
29105250Srobert *
30105250Srobert * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
31105250Srobert * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32105250Srobert * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33105250Srobert * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
34105250Srobert * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35105250Srobert * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36105250Srobert * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
37105250Srobert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38105250Srobert * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39105250Srobert * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40105250Srobert * SUCH DAMAGE.
41105250Srobert *
42273614Sbrooks */
43105250Srobert
44105250Srobert#include "sysinstall.h"
45105250Srobert#include <stdarg.h>
46105250Srobert
47105250Srobert/* Whack up an informational message on the status line, in stand-out */
48105250Srobertvoid
49105250SrobertmsgYap(char *fmt, ...)
50105250Srobert{
51105250Srobert    va_list args;
52105250Srobert    char *errstr;
53105250Srobert    int attrs;
54105250Srobert
55105250Srobert    errstr = (char *)safe_malloc(FILENAME_MAX);
56105250Srobert    va_start(args, fmt);
57105250Srobert    vsnprintf(errstr, FILENAME_MAX, fmt, args);
58105250Srobert    va_end(args);
59105250Srobert    attrs = getattrs(stdscr);
60105250Srobert    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
184    errstr = (char *)safe_malloc(FILENAME_MAX);
185    va_start(args, fmt);
186    vsnprintf(errstr, FILENAME_MAX, fmt, args);
187    va_end(args);
188    use_helpline(NULL);
189    use_helpfile(NULL);
190    if (OnVTY) {
191	msgDebug("User confirmation requested (type ALT-F1)\n");
192	msgInfo(NULL);
193    }
194    dialog_notify(errstr);
195    free(errstr);
196}
197
198/* Put up a message in a popup information box */
199void
200msgNotify(char *fmt, ...)
201{
202    va_list args;
203    char *errstr;
204
205    errstr = (char *)safe_malloc(FILENAME_MAX);
206    va_start(args, fmt);
207    vsnprintf(errstr, FILENAME_MAX, fmt, args);
208    va_end(args);
209    use_helpline(NULL);
210    use_helpfile(NULL);
211    msgDebug("Notify: %s\n", errstr);
212    dialog_msgbox("Information Dialog", errstr, -1, -1, 0);
213    free(errstr);
214}
215
216/* Put up a message in a popup yes/no box and return 1 for YES, 0 for NO */
217int
218msgYesNo(char *fmt, ...)
219{
220    va_list args;
221    char *errstr;
222    int ret;
223    WINDOW *w;
224
225    errstr = (char *)safe_malloc(FILENAME_MAX);
226    va_start(args, fmt);
227    vsnprintf(errstr, FILENAME_MAX, fmt, args);
228    va_end(args);
229    use_helpline(NULL);
230    use_helpfile(NULL);
231    w = dupwin(newscr);
232    if (OnVTY) {
233	msgDebug("User decision requested (type ALT-F1)\n");
234	msgInfo(NULL);
235    }
236    ret = dialog_yesno("User Confirmation Requested", errstr, -1, -1);
237    touchwin(w);
238    wrefresh(w);
239    delwin(w);
240    free(errstr);
241    return ret;
242}
243
244/* Put up a message in an input box and return the value */
245char *
246msgGetInput(char *buf, char *fmt, ...)
247{
248    va_list args;
249    char *errstr;
250    static char input_buffer[256];
251    int rval;
252    WINDOW *w;
253
254    errstr = (char *)safe_malloc(FILENAME_MAX);
255    va_start(args, fmt);
256    vsnprintf(errstr, FILENAME_MAX, fmt, args);
257    va_end(args);
258    use_helpline(NULL);
259    use_helpfile(NULL);
260    if (buf)
261	strcpy(input_buffer, buf);
262    else
263	input_buffer[0] = '\0';
264    w = dupwin(newscr);
265    if (OnVTY) {
266	msgDebug("User input requested (type ALT-F1)\n");
267	msgInfo(NULL);
268    }
269    rval = dialog_inputbox("Value Required", errstr, -1, -1, input_buffer);
270    touchwin(w);
271    wrefresh(w);
272    delwin(w);
273    free(errstr);
274    if (!rval)
275	return input_buffer;
276    else
277	return NULL;
278}
279
280/* Write something to the debugging port */
281void
282msgDebug(char *fmt, ...)
283{
284    va_list args;
285    char *dbg;
286
287    if (DebugFD == -1)
288	return;
289    dbg = (char *)safe_malloc(FILENAME_MAX);
290    strcpy(dbg, "DEBUG: ");
291    va_start(args, fmt);
292    vsnprintf((char *)(dbg + strlen(dbg)), FILENAME_MAX, fmt, args);
293    va_end(args);
294    write(DebugFD, dbg, strlen(dbg));
295    free(dbg);
296}
297