msg.c revision 8594
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.11 1995/05/16 11:37:23 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    errstr = (char *)safe_malloc(FILENAME_MAX);
76    va_start(args, fmt);
77    vsnprintf(errstr, FILENAME_MAX, fmt, args);
78    va_end(args);
79    attrs = getattrs(stdscr);
80    attrset(A_NORMAL);
81    mvaddstr(23, 0, errstr);
82    attrset(attrs);
83    refresh();
84    free(errstr);
85}
86
87/* Whack up a warning on the status line */
88void
89msgWarn(char *fmt, ...)
90{
91    va_list args;
92    char *errstr;
93    int attrs;
94
95    errstr = (char *)safe_malloc(FILENAME_MAX);
96    strcpy(errstr, "Warning: ");
97    va_start(args, fmt);
98    vsnprintf((char *)(errstr + strlen(errstr)), FILENAME_MAX, fmt, args);
99    va_end(args);
100    attrs = getattrs(stdscr);
101    beep();
102    attrset(A_REVERSE);
103    mvaddstr(23, 0, errstr);
104    attrset(attrs);
105    refresh();
106    free(errstr);
107}
108
109/* Whack up an error on the status line */
110void
111msgError(char *fmt, ...)
112{
113    va_list args;
114    char *errstr;
115    int attrs;
116
117    errstr = (char *)safe_malloc(FILENAME_MAX);
118    strcpy(errstr, "Error: ");
119    va_start(args, fmt);
120    vsnprintf((char *)(errstr + strlen(errstr)), FILENAME_MAX, fmt, args);
121    va_end(args);
122    beep();
123    attrs = getattrs(stdscr);
124    attrset(A_REVERSE);
125    mvaddstr(23, 0, errstr);
126    attrset(attrs);
127    refresh();
128    free(errstr);
129}
130
131/* Whack up a fatal error on the status line */
132void
133msgFatal(char *fmt, ...)
134{
135    va_list args;
136    char *errstr;
137    int attrs;
138
139    errstr = (char *)safe_malloc(FILENAME_MAX);
140    strcpy(errstr, "Fatal Error: ");
141    va_start(args, fmt);
142    vsnprintf((char *)(errstr + strlen(errstr)), FILENAME_MAX, fmt, args);
143    va_end(args);
144    beep();
145    attrs = getattrs(stdscr);
146    attrset(A_REVERSE);
147    mvaddstr(23, 0, errstr);
148    addstr(" - ");
149    addstr("PRESS ANY KEY TO ");
150    if (getpid() == 1)
151	addstr("REBOOT");
152    else
153	addstr("QUIT");
154    attrset(attrs);
155    refresh();
156    free(errstr);
157    getch();
158    systemShutdown();
159}
160
161/* Put up a message in a popup confirmation box */
162void
163msgConfirm(char *fmt, ...)
164{
165    va_list args;
166    char *errstr;
167
168    errstr = (char *)safe_malloc(FILENAME_MAX);
169    va_start(args, fmt);
170    vsnprintf(errstr, FILENAME_MAX, fmt, args);
171    va_end(args);
172    use_helpline(NULL);
173    use_helpfile(NULL);
174    dialog_notify(errstr);
175    free(errstr);
176}
177
178/* Put up a message in a popup information box */
179void
180msgNotify(char *fmt, ...)
181{
182    va_list args;
183    char *errstr;
184    WINDOW *w;
185
186    errstr = (char *)safe_malloc(FILENAME_MAX);
187    va_start(args, fmt);
188    vsnprintf(errstr, FILENAME_MAX, fmt, args);
189    va_end(args);
190    use_helpline(NULL);
191    use_helpfile(NULL);
192    msgDebug("%s\n", errstr);
193    w = dupwin(newscr);
194    dialog_msgbox("Information Dialog", errstr, -1, -1, 0);
195    touchwin(w);
196    wrefresh(w);
197    delwin(w);
198    free(errstr);
199}
200
201/* Put up a message in a popup yes/no box and return 1 for YES, 0 for NO */
202int
203msgYesNo(char *fmt, ...)
204{
205    va_list args;
206    char *errstr;
207    int ret;
208    WINDOW *w;
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    w = dupwin(newscr);
217    ret = dialog_yesno("User Confirmation Requested", errstr, -1, -1);
218    touchwin(w);
219    wrefresh(w);
220    delwin(w);
221    msgDebug("User answers %s to \"%s\"\n", ret ? "no" : "yes", errstr);
222    free(errstr);
223    return ret;
224}
225
226/* Put up a message in an input box and return the value */
227char *
228msgGetInput(char *buf, char *fmt, ...)
229{
230    va_list args;
231    char *errstr;
232    static char input_buffer[256];
233    int rval;
234    WINDOW *w;
235
236    errstr = (char *)safe_malloc(FILENAME_MAX);
237    va_start(args, fmt);
238    vsnprintf(errstr, FILENAME_MAX, fmt, args);
239    va_end(args);
240    use_helpline(NULL);
241    use_helpfile(NULL);
242    if (buf)
243	strcpy(input_buffer, buf);
244    else
245	input_buffer[0] = '\0';
246    w = dupwin(newscr);
247    rval = dialog_inputbox("Value Required", errstr, -1, -1, input_buffer);
248    touchwin(w);
249    wrefresh(w);
250    delwin(w);
251    msgDebug("input request \"%s\" returns %d status", errstr, rval);
252    free(errstr);
253    if (!rval)
254	return input_buffer;
255    else
256	return NULL;
257}
258
259/* Write something to the debugging port */
260void
261msgDebug(char *fmt, ...)
262{
263    va_list args;
264    char *dbg;
265
266    if (DebugFD == -1)
267	return;
268    dbg = (char *)safe_malloc(FILENAME_MAX);
269    strcpy(dbg, "DEBUG: ");
270    va_start(args, fmt);
271    vsnprintf((char *)(dbg + strlen(dbg)), FILENAME_MAX, fmt, args);
272    va_end(args);
273    write(DebugFD, dbg, strlen(dbg));
274    free(dbg);
275}
276