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