-W is included in CFLAGS already.
[dragonfly.git] / release / sysinstall / msg.c
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: src/release/sysinstall/msg.c,v 1.52.2.4 2001/09/11 20:42:42 jkh Exp $
8  * $DragonFly: src/release/sysinstall/Attic/msg.c,v 1.2 2003/06/17 04:27:21 dillon Exp $
9  *
10  * Copyright (c) 1995
11  *      Jordan Hubbard.  All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer,
18  *    verbatim and that no modifications are made prior to this
19  *    point in the file.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  *
24  * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  */
37
38 #include "sysinstall.h"
39 #include <stdarg.h>
40 #include <sys/ioctl.h>
41 #include <machine/console.h>
42
43 Boolean
44 isDebug(void)
45 {
46     char *cp;
47
48     return (cp = variable_get(VAR_DEBUG)) && strcmp(cp, "no");
49 }
50
51 /* Whack up an informational message on the status line, in stand-out */
52 void
53 msgYap(char *fmt, ...)
54 {
55     va_list args;
56     char *errstr;
57     int attrs;
58
59     errstr = (char *)alloca(FILENAME_MAX);
60     va_start(args, fmt);
61     vsnprintf(errstr, FILENAME_MAX, fmt, args);
62     va_end(args);
63     attrs = getattrs(stdscr);
64     attrset(A_REVERSE);
65     mvaddstr(StatusLine, 0, errstr);
66     attrset(attrs);
67     refresh();
68 }
69
70 /* Whack up an informational message on the status line */
71 void
72 msgInfo(char *fmt, ...)
73 {
74     va_list args;
75     char *errstr;
76     int i, attrs;
77     char line[81];
78
79     attrs = getattrs(stdscr);
80     /* NULL is a special convention meaning "erase the old stuff" */
81     if (!fmt) {
82         move(StatusLine, 0);
83         clrtoeol();
84         return;
85     }
86     errstr = (char *)alloca(FILENAME_MAX);
87     va_start(args, fmt);
88     vsnprintf(errstr, FILENAME_MAX, fmt, args);
89     va_end(args);
90     memset(line, ' ', 80);
91     for (i = 0; i < 80; i++) {
92         if (errstr[i])
93             line[i] = errstr[i];
94         else
95             break;
96     }
97     line[80] = '\0';
98     attrset(ATTR_TITLE);
99     mvaddstr(StatusLine, 0, line);
100     attrset(attrs);
101     move(StatusLine, 79);
102     refresh();
103 }
104
105 /* Whack up a warning on the status line */
106 void
107 msgWarn(char *fmt, ...)
108 {
109     va_list args;
110     char *errstr;
111     int attrs;
112
113     errstr = (char *)alloca(FILENAME_MAX);
114     strcpy(errstr, "Warning: ");
115     va_start(args, fmt);
116     vsnprintf((char *)(errstr + strlen(errstr)), FILENAME_MAX, fmt, args);
117     va_end(args);
118     attrs = getattrs(stdscr);
119     beep();
120     attrset(ATTR_TITLE);
121     mvaddstr(StatusLine, 0, errstr);
122     attrset(attrs);
123     refresh();
124     if (OnVTY && isDebug())
125         msgDebug("Warning message `%s'\n", errstr);
126 }
127
128 /* Whack up an error on the status line */
129 void
130 msgError(char *fmt, ...)
131 {
132     va_list args;
133     char *errstr;
134     int attrs;
135
136     errstr = (char *)alloca(FILENAME_MAX);
137     strcpy(errstr, "Error: ");
138     va_start(args, fmt);
139     vsnprintf((char *)(errstr + strlen(errstr)), FILENAME_MAX, fmt, args);
140     va_end(args);
141     beep();
142     attrs = getattrs(stdscr);
143     attrset(ATTR_TITLE);
144     mvaddstr(StatusLine, 0, errstr);
145     attrset(attrs);
146     refresh();
147     if (OnVTY && isDebug())
148         msgDebug("Error message `%s'\n", errstr);
149 }
150
151 /* Whack up a fatal error on the status line */
152 void
153 msgFatal(char *fmt, ...)
154 {
155     va_list args;
156     char *errstr;
157     int attrs;
158
159     errstr = (char *)alloca(FILENAME_MAX);
160     strcpy(errstr, "Fatal Error: ");
161     va_start(args, fmt);
162     vsnprintf((char *)(errstr + strlen(errstr)), FILENAME_MAX, fmt, args);
163     va_end(args);
164     beep();
165     attrs = getattrs(stdscr);
166     attrset(ATTR_TITLE);
167     mvaddstr(StatusLine, 0, errstr);
168     addstr(" - ");
169     addstr("PRESS ANY KEY TO ");
170     if (getpid() == 1)
171         addstr("REBOOT");
172     else
173         addstr("QUIT");
174     attrset(attrs);
175     refresh();
176     if (OnVTY)
177         msgDebug("Fatal error `%s'!\n", errstr);
178     getch();
179     systemShutdown(1);
180 }
181
182 /* Put up a message in a popup confirmation box */
183 void
184 msgConfirm(char *fmt, ...)
185 {
186     va_list args;
187     char *errstr;
188     WINDOW *w = savescr();
189
190     errstr = (char *)alloca(FILENAME_MAX);
191     va_start(args, fmt);
192     vsnprintf(errstr, FILENAME_MAX, fmt, args);
193     va_end(args);
194     use_helpline(NULL);
195     use_helpfile(NULL);
196     if (OnVTY) {
197         ioctl(0, VT_ACTIVATE, 1);
198         msgInfo(NULL);
199     }
200     dialog_notify(errstr);
201     restorescr(w);
202 }
203
204 /* Put up a message in a popup information box */
205 void
206 msgNotify(char *fmt, ...)
207 {
208     va_list args;
209     char *errstr;
210
211     errstr = (char *)alloca(FILENAME_MAX);
212     va_start(args, fmt);
213     vsnprintf(errstr, FILENAME_MAX, fmt, args);
214     va_end(args);
215     use_helpline(NULL);
216     use_helpfile(NULL);
217     if (isDebug())
218         msgDebug("Notify: %s\n", errstr);
219     dialog_msgbox(NULL, errstr, -1, -1, 0);
220 }
221
222 /* Put up a message in a popup yes/no box and return 0 for YES, 1 for NO */
223 int
224 msgYesNo(char *fmt, ...)
225 {
226     va_list args;
227     char *errstr;
228     int ret;
229     WINDOW *w = savescr();
230     
231     errstr = (char *)alloca(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     if (OnVTY) {
238         ioctl(0, VT_ACTIVATE, 1);       /* Switch back */
239         msgInfo(NULL);
240     }
241     if (variable_get(VAR_NONINTERACTIVE))
242         return 0;       /* If non-interactive, return YES all the time */
243     ret = dialog_yesno("User Confirmation Requested", errstr, -1, -1);
244     restorescr(w);
245     return ret;
246 }
247
248 /* Put up a message in a popup no/yes box and return 0 for YES, 1 for NO */
249 int
250 msgNoYes(char *fmt, ...)
251 {
252     va_list args;
253     char *errstr;
254     int ret;
255     WINDOW *w = savescr();
256     
257     errstr = (char *)alloca(FILENAME_MAX);
258     va_start(args, fmt);
259     vsnprintf(errstr, FILENAME_MAX, fmt, args);
260     va_end(args);
261     use_helpline(NULL);
262     use_helpfile(NULL);
263     if (OnVTY) {
264         ioctl(0, VT_ACTIVATE, 1);       /* Switch back */
265         msgInfo(NULL);
266     }
267     if (variable_get(VAR_NONINTERACTIVE))
268         return 1;       /* If non-interactive, return NO all the time */
269     ret = dialog_noyes("User Confirmation Requested", errstr, -1, -1);
270     restorescr(w);
271     return ret;
272 }
273
274 /* Put up a message in an input box and return the value */
275 char *
276 msgGetInput(char *buf, char *fmt, ...)
277 {
278     va_list args;
279     char *errstr;
280     static char input_buffer[256];
281     int rval;
282     WINDOW *w = savescr();
283
284     errstr = (char *)alloca(FILENAME_MAX);
285     va_start(args, fmt);
286     vsnprintf(errstr, FILENAME_MAX, fmt, args);
287     va_end(args);
288     use_helpline(NULL);
289     use_helpfile(NULL);
290     if (buf)
291         SAFE_STRCPY(input_buffer, buf);
292     else
293         input_buffer[0] = '\0';
294     if (OnVTY) {
295         ioctl(0, VT_ACTIVATE, 1);       /* Switch back */
296         msgInfo(NULL);
297     }
298     rval = dialog_inputbox("Value Required", errstr, -1, -1, input_buffer);
299     restorescr(w);
300     if (!rval)
301         return input_buffer;
302     else
303         return NULL;
304 }
305
306 /* Write something to the debugging port */
307 void
308 msgDebug(char *fmt, ...)
309 {
310     va_list args;
311     char *dbg;
312
313     if (DebugFD == -1)
314         return;
315     dbg = (char *)alloca(FILENAME_MAX);
316     strcpy(dbg, "DEBUG: ");
317     va_start(args, fmt);
318     vsnprintf((char *)(dbg + strlen(dbg)), FILENAME_MAX, fmt, args);
319     va_end(args);
320     write(DebugFD, dbg, strlen(dbg));
321 }
322
323 /* Tell the user there's some output to go look at */
324 void
325 msgWeHaveOutput(char *fmt, ...)
326 {
327     va_list args;
328     char *errstr;
329     WINDOW *w = savescr();
330     
331     errstr = (char *)alloca(FILENAME_MAX);
332     va_start(args, fmt);
333     vsnprintf(errstr, FILENAME_MAX, fmt, args);
334     va_end(args);
335     use_helpline(NULL);
336     use_helpfile(NULL);
337     msgDebug("Notify: %s\n", errstr);
338     dialog_clear_norefresh();
339     sleep(2);
340     dialog_msgbox(NULL, errstr, -1, -1, 0);
341     restorescr(w);
342 }
343
344 /* Simple versions of msgConfirm() and msgNotify() for calling from scripts */
345 int
346 msgSimpleConfirm(char *str)
347 {
348     msgConfirm("%s", str);
349     return DITEM_SUCCESS;
350 }
351
352 int
353 msgSimpleNotify(char *str)
354 {
355     msgNotify("%s", str);
356     return DITEM_SUCCESS;
357 }