msg.c revision 8837
18097Sjkh/*
28097Sjkh * The new sysinstall program.
38097Sjkh *
48097Sjkh * This is probably the last program in the `sysinstall' line - the next
58097Sjkh * generation being essentially a complete rewrite.
68097Sjkh *
78837Sjkh * $Id: msg.c,v 1.26 1995/05/29 01:43:18 jkh Exp $
88097Sjkh *
98097Sjkh * Copyright (c) 1995
108097Sjkh *	Jordan Hubbard.  All rights reserved.
118097Sjkh *
128097Sjkh * Redistribution and use in source and binary forms, with or without
138097Sjkh * modification, are permitted provided that the following conditions
148097Sjkh * are met:
158097Sjkh * 1. Redistributions of source code must retain the above copyright
168097Sjkh *    notice, this list of conditions and the following disclaimer,
178097Sjkh *    verbatim and that no modifications are made prior to this
188097Sjkh *    point in the file.
198097Sjkh * 2. Redistributions in binary form must reproduce the above copyright
208097Sjkh *    notice, this list of conditions and the following disclaimer in the
218097Sjkh *    documentation and/or other materials provided with the distribution.
228097Sjkh * 3. All advertising materials mentioning features or use of this software
238097Sjkh *    must display the following acknowledgement:
248097Sjkh *	This product includes software developed by Jordan Hubbard
258097Sjkh *	for the FreeBSD Project.
268097Sjkh * 4. The name of Jordan Hubbard or the FreeBSD project may not be used to
278097Sjkh *    endorse or promote products derived from this software without specific
288097Sjkh *    prior written permission.
298097Sjkh *
308097Sjkh * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
318097Sjkh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
328097Sjkh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
338097Sjkh * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
348097Sjkh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
358097Sjkh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
368097Sjkh * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
378097Sjkh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
388097Sjkh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
398097Sjkh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
408097Sjkh * SUCH DAMAGE.
418097Sjkh *
428097Sjkh */
438097Sjkh
448097Sjkh#include "sysinstall.h"
458097Sjkh#include <stdarg.h>
468825Sjkh#include <sys/ioctl.h>
478825Sjkh#include <machine/console.h>
488097Sjkh
498702Sjkh#define VTY_STATLINE	24
508702Sjkh#define TTY_STATLINE	23
518702Sjkh
528837SjkhBoolean
538837SjkhisDebug(void)
548837Sjkh{
558837Sjkh    char *cp;
568837Sjkh
578837Sjkh    cp = getenv("debug");
588837Sjkh    if (cp && !strcmp(cp, "yes"))
598837Sjkh	return TRUE;
608837Sjkh    return FALSE;
618837Sjkh}
628837Sjkh
638262Sjkh/* Whack up an informational message on the status line, in stand-out */
648262Sjkhvoid
658262SjkhmsgYap(char *fmt, ...)
668262Sjkh{
678262Sjkh    va_list args;
688262Sjkh    char *errstr;
698262Sjkh    int attrs;
708262Sjkh
718317Sjkh    errstr = (char *)safe_malloc(FILENAME_MAX);
728262Sjkh    va_start(args, fmt);
738347Sjkh    vsnprintf(errstr, FILENAME_MAX, fmt, args);
748262Sjkh    va_end(args);
758262Sjkh    attrs = getattrs(stdscr);
768314Sjkh    attrset(A_REVERSE);
778702Sjkh    mvaddstr(OnVTY ? VTY_STATLINE : TTY_STATLINE, 0, errstr);
788262Sjkh    attrset(attrs);
798262Sjkh    refresh();
808262Sjkh    free(errstr);
818262Sjkh}
828262Sjkh
838097Sjkh/* Whack up an informational message on the status line */
848097Sjkhvoid
858097SjkhmsgInfo(char *fmt, ...)
868097Sjkh{
878097Sjkh    va_list args;
888097Sjkh    char *errstr;
898705Sjkh    int i, attrs;
908705Sjkh    char line[81];
918097Sjkh
928705Sjkh    attrs = getattrs(stdscr);
938641Sjkh    /* NULL is a special convention meaning "erase the old stuff" */
948641Sjkh    if (!fmt) {
958702Sjkh	move(OnVTY ? VTY_STATLINE : TTY_STATLINE, 0);
968705Sjkh	attrset(A_REVERSE);
978641Sjkh	clrtoeol();
988705Sjkh	attrset(attrs);
998641Sjkh	return;
1008641Sjkh    }
1018317Sjkh    errstr = (char *)safe_malloc(FILENAME_MAX);
1028097Sjkh    va_start(args, fmt);
1038347Sjkh    vsnprintf(errstr, FILENAME_MAX, fmt, args);
1048097Sjkh    va_end(args);
1058705Sjkh    memset(line, ' ', 80);
1068705Sjkh    for (i = 0; i < 80; i++) {
1078705Sjkh	if (errstr[i])
1088705Sjkh	    line[i] = errstr[i];
1098705Sjkh	else
1108705Sjkh	    break;
1118705Sjkh    }
1128705Sjkh    line[80] = '\0';
1138705Sjkh    attrset(A_REVERSE);
1148705Sjkh    mvaddstr(OnVTY ? VTY_STATLINE : TTY_STATLINE, 0, line);
1158262Sjkh    attrset(attrs);
1168722Sjkh    move(OnVTY ? VTY_STATLINE : TTY_STATLINE, 79);
1178262Sjkh    refresh();
1188640Sjkh    if (OnVTY) {
1198837Sjkh	if (isDebug())
1208837Sjkh	    msgDebug("Information: `%s'\n", errstr);
1218641Sjkh	msgInfo(NULL);
1228640Sjkh    }
1238097Sjkh    free(errstr);
1248097Sjkh}
1258097Sjkh
1268097Sjkh/* Whack up a warning on the status line */
1278097Sjkhvoid
1288097SjkhmsgWarn(char *fmt, ...)
1298097Sjkh{
1308097Sjkh    va_list args;
1318097Sjkh    char *errstr;
1328262Sjkh    int attrs;
1338097Sjkh
1348317Sjkh    errstr = (char *)safe_malloc(FILENAME_MAX);
1358097Sjkh    strcpy(errstr, "Warning: ");
1368097Sjkh    va_start(args, fmt);
1378097Sjkh    vsnprintf((char *)(errstr + strlen(errstr)), FILENAME_MAX, fmt, args);
1388097Sjkh    va_end(args);
1398262Sjkh    attrs = getattrs(stdscr);
1408097Sjkh    beep();
1418314Sjkh    attrset(A_REVERSE);
1428702Sjkh    mvaddstr(OnVTY ? VTY_STATLINE : TTY_STATLINE, 0, errstr);
1438262Sjkh    attrset(attrs);
1448262Sjkh    refresh();
1458837Sjkh    if (OnVTY && isDebug())
1468628Sjkh	msgDebug("Warning message `%s'\n", errstr);
1478097Sjkh    free(errstr);
1488097Sjkh}
1498097Sjkh
1508097Sjkh/* Whack up an error on the status line */
1518097Sjkhvoid
1528097SjkhmsgError(char *fmt, ...)
1538097Sjkh{
1548097Sjkh    va_list args;
1558097Sjkh    char *errstr;
1568262Sjkh    int attrs;
1578097Sjkh
1588317Sjkh    errstr = (char *)safe_malloc(FILENAME_MAX);
1598097Sjkh    strcpy(errstr, "Error: ");
1608097Sjkh    va_start(args, fmt);
1618097Sjkh    vsnprintf((char *)(errstr + strlen(errstr)), FILENAME_MAX, fmt, args);
1628097Sjkh    va_end(args);
1638097Sjkh    beep();
1648262Sjkh    attrs = getattrs(stdscr);
1658314Sjkh    attrset(A_REVERSE);
1668702Sjkh    mvaddstr(OnVTY ? VTY_STATLINE : TTY_STATLINE, 0, errstr);
1678262Sjkh    attrset(attrs);
1688262Sjkh    refresh();
1698837Sjkh    if (OnVTY && isDebug())
1708628Sjkh	msgDebug("Error message `%s'\n", errstr);
1718097Sjkh    free(errstr);
1728097Sjkh}
1738097Sjkh
1748097Sjkh/* Whack up a fatal error on the status line */
1758097Sjkhvoid
1768097SjkhmsgFatal(char *fmt, ...)
1778097Sjkh{
1788097Sjkh    va_list args;
1798097Sjkh    char *errstr;
1808262Sjkh    int attrs;
1818097Sjkh
1828317Sjkh    errstr = (char *)safe_malloc(FILENAME_MAX);
1838097Sjkh    strcpy(errstr, "Fatal Error: ");
1848097Sjkh    va_start(args, fmt);
1858097Sjkh    vsnprintf((char *)(errstr + strlen(errstr)), FILENAME_MAX, fmt, args);
1868097Sjkh    va_end(args);
1878097Sjkh    beep();
1888262Sjkh    attrs = getattrs(stdscr);
1898314Sjkh    attrset(A_REVERSE);
1908702Sjkh    mvaddstr(OnVTY ? VTY_STATLINE : TTY_STATLINE, 0, errstr);
1918097Sjkh    addstr(" - ");
1928097Sjkh    addstr("PRESS ANY KEY TO ");
1938097Sjkh    if (getpid() == 1)
1948097Sjkh	addstr("REBOOT");
1958097Sjkh    else
1968097Sjkh	addstr("QUIT");
1978262Sjkh    attrset(attrs);
1988262Sjkh    refresh();
1998628Sjkh    if (OnVTY)
2008628Sjkh	msgDebug("Fatal error `%s'!\n", errstr);
2018097Sjkh    free(errstr);
2028097Sjkh    getch();
2038097Sjkh    systemShutdown();
2048097Sjkh}
2058097Sjkh
2068208Sjkh/* Put up a message in a popup confirmation box */
2078208Sjkhvoid
2088208SjkhmsgConfirm(char *fmt, ...)
2098208Sjkh{
2108208Sjkh    va_list args;
2118208Sjkh    char *errstr;
2128649Sjkh    WINDOW *w;
2138208Sjkh
2148317Sjkh    errstr = (char *)safe_malloc(FILENAME_MAX);
2158208Sjkh    va_start(args, fmt);
2168208Sjkh    vsnprintf(errstr, FILENAME_MAX, fmt, args);
2178208Sjkh    va_end(args);
2188208Sjkh    use_helpline(NULL);
2198208Sjkh    use_helpfile(NULL);
2208649Sjkh    w = dupwin(newscr);
2218640Sjkh    if (OnVTY) {
2228828Sjkh	msgDebug("Switching back to VTY 0\n");
2238828Sjkh	ioctl(0, VT_RELDISP, 1);
2248641Sjkh	msgInfo(NULL);
2258640Sjkh    }
2268556Sjkh    dialog_notify(errstr);
2278649Sjkh    touchwin(w);
2288649Sjkh    wrefresh(w);
2298649Sjkh    delwin(w);
2308208Sjkh    free(errstr);
2318208Sjkh}
2328208Sjkh
2338302Sjkh/* Put up a message in a popup information box */
2348302Sjkhvoid
2358302SjkhmsgNotify(char *fmt, ...)
2368302Sjkh{
2378302Sjkh    va_list args;
2388302Sjkh    char *errstr;
2398302Sjkh
2408317Sjkh    errstr = (char *)safe_malloc(FILENAME_MAX);
2418302Sjkh    va_start(args, fmt);
2428302Sjkh    vsnprintf(errstr, FILENAME_MAX, fmt, args);
2438302Sjkh    va_end(args);
2448302Sjkh    use_helpline(NULL);
2458302Sjkh    use_helpfile(NULL);
2468837Sjkh    if (isDebug())
2478837Sjkh	msgDebug("Notify: %s\n", errstr);
2488649Sjkh    dialog_clear();
2498438Sjkh    dialog_msgbox("Information Dialog", errstr, -1, -1, 0);
2508302Sjkh    free(errstr);
2518302Sjkh}
2528302Sjkh
2538208Sjkh/* Put up a message in a popup yes/no box and return 1 for YES, 0 for NO */
2548208Sjkhint
2558208SjkhmsgYesNo(char *fmt, ...)
2568208Sjkh{
2578208Sjkh    va_list args;
2588208Sjkh    char *errstr;
2598208Sjkh    int ret;
2608556Sjkh    WINDOW *w;
2618208Sjkh
2628317Sjkh    errstr = (char *)safe_malloc(FILENAME_MAX);
2638208Sjkh    va_start(args, fmt);
2648208Sjkh    vsnprintf(errstr, FILENAME_MAX, fmt, args);
2658208Sjkh    va_end(args);
2668208Sjkh    use_helpline(NULL);
2678208Sjkh    use_helpfile(NULL);
2688556Sjkh    w = dupwin(newscr);
2698640Sjkh    if (OnVTY) {
2708837Sjkh	msgDebug("Switching back to VTY 0\n");
2718828Sjkh	ioctl(0, VT_RELDISP, 1);	/* Switch back */
2728641Sjkh	msgInfo(NULL);
2738640Sjkh    }
2748281Sjkh    ret = dialog_yesno("User Confirmation Requested", errstr, -1, -1);
2758556Sjkh    touchwin(w);
2768556Sjkh    wrefresh(w);
2778556Sjkh    delwin(w);
2788208Sjkh    free(errstr);
2798208Sjkh    return ret;
2808208Sjkh}
2818262Sjkh
2828262Sjkh/* Put up a message in an input box and return the value */
2838262Sjkhchar *
2848262SjkhmsgGetInput(char *buf, char *fmt, ...)
2858262Sjkh{
2868262Sjkh    va_list args;
2878262Sjkh    char *errstr;
2888278Sjkh    static char input_buffer[256];
2898262Sjkh    int rval;
2908556Sjkh    WINDOW *w;
2918262Sjkh
2928317Sjkh    errstr = (char *)safe_malloc(FILENAME_MAX);
2938262Sjkh    va_start(args, fmt);
2948262Sjkh    vsnprintf(errstr, FILENAME_MAX, fmt, args);
2958262Sjkh    va_end(args);
2968262Sjkh    use_helpline(NULL);
2978262Sjkh    use_helpfile(NULL);
2988302Sjkh    if (buf)
2998302Sjkh	strcpy(input_buffer, buf);
3008302Sjkh    else
3018302Sjkh	input_buffer[0] = '\0';
3028556Sjkh    w = dupwin(newscr);
3038640Sjkh    if (OnVTY) {
3048828Sjkh	msgDebug("Switching back to VTY 0\n");
3058828Sjkh	ioctl(0, VT_RELDISP, 1);	/* Switch back */
3068641Sjkh	msgInfo(NULL);
3078640Sjkh    }
3088262Sjkh    rval = dialog_inputbox("Value Required", errstr, -1, -1, input_buffer);
3098556Sjkh    touchwin(w);
3108556Sjkh    wrefresh(w);
3118556Sjkh    delwin(w);
3128262Sjkh    free(errstr);
3138262Sjkh    if (!rval)
3148262Sjkh	return input_buffer;
3158262Sjkh    else
3168262Sjkh	return NULL;
3178262Sjkh}
3188262Sjkh
3198347Sjkh/* Write something to the debugging port */
3208347Sjkhvoid
3218347SjkhmsgDebug(char *fmt, ...)
3228347Sjkh{
3238347Sjkh    va_list args;
3248347Sjkh    char *dbg;
3258347Sjkh
3268594Sjkh    if (DebugFD == -1)
3278594Sjkh	return;
3288347Sjkh    dbg = (char *)safe_malloc(FILENAME_MAX);
3298347Sjkh    strcpy(dbg, "DEBUG: ");
3308347Sjkh    va_start(args, fmt);
3318347Sjkh    vsnprintf((char *)(dbg + strlen(dbg)), FILENAME_MAX, fmt, args);
3328347Sjkh    va_end(args);
3338347Sjkh    write(DebugFD, dbg, strlen(dbg));
3348347Sjkh    free(dbg);
3358347Sjkh}
3368705Sjkh
3378705Sjkh/* Tell the user there's some output to go look at */
3388705Sjkhvoid
3398705SjkhmsgWeHaveOutput(char *fmt, ...)
3408705Sjkh{
3418705Sjkh    va_list args;
3428705Sjkh    char *errstr;
3438705Sjkh
3448705Sjkh    errstr = (char *)safe_malloc(FILENAME_MAX);
3458705Sjkh    va_start(args, fmt);
3468705Sjkh    vsnprintf(errstr, FILENAME_MAX, fmt, args);
3478705Sjkh    va_end(args);
3488705Sjkh    use_helpline(NULL);
3498705Sjkh    use_helpfile(NULL);
3508705Sjkh    msgDebug("Notify: %s\n", errstr);
3518705Sjkh    dialog_clear();
3528705Sjkh    dialog_msgbox("Information Dialog", errstr, -1, -1, 0);
3538705Sjkh    free(errstr);
3548705Sjkh    if (OnVTY)
3558705Sjkh	msgInfo("Command output is on debugging screen - type ALT-F2 to see it");
3568705Sjkh}
357