main.c revision 161060
1/*
2 * $FreeBSD: head/usr.sbin/sade/main.c 161060 2006-08-07 23:35:49Z netchild $
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 <sys/signal.h>
34#include <sys/fcntl.h>
35
36const char *StartName;		/* Initial contents of argv[0] */
37
38static void
39screech(int sig)
40{
41    msgDebug("\007Signal %d caught!  That's bad!\n", sig);
42    longjmp(BailOut, sig);
43}
44
45int
46main(int argc, char **argv)
47{
48    int choice, scroll, curr, max, status;
49
50    /* Record name to be able to restart */
51    StartName = argv[0];
52
53    signal(SIGPIPE, SIG_IGN);
54
55    /* We don't work too well when running as non-root anymore */
56    if (geteuid() != 0) {
57	fprintf(stderr, "Error: This utility should only be run as root.\n");
58	return 1;
59    }
60
61#ifdef PC98
62    {
63	/* XXX */
64	char *p = getenv("TERM");
65	if (p && strcmp(p, "cons25") == 0)
66	    putenv("TERM=cons25w");
67    }
68#endif
69
70    /* Set up whatever things need setting up */
71    systemInitialize(argc, argv);
72
73    /* Set default flag and variable values */
74    installVarDefaults(NULL);
75
76    if (argc > 1 && !strcmp(argv[1], "-fake")) {
77	variable_set2(VAR_DEBUG, "YES", 0);
78	Fake = TRUE;
79	msgConfirm("I'll be just faking it from here on out, OK?");
80    }
81    if (argc > 1 && !strcmp(argv[1], "-restart"))
82	Restarting = TRUE;
83
84    /* Try to preserve our scroll-back buffer */
85    if (OnVTY) {
86	for (curr = 0; curr < 25; curr++)
87	    putchar('\n');
88    }
89    /* Move stderr aside */
90    if (DebugFD)
91	dup2(DebugFD, 2);
92
93    /* Initialize driver modules, if we haven't already done so (ie,
94       the user hit Ctrl-C -> Restart. */
95    if (!pvariable_get("modulesInitialize")) {
96	pvariable_set("modulesInitialize=1");
97    }
98
99    /* Probe for all relevant devices on the system */
100    deviceGetAll();
101
102    /* First, see if we have any arguments to process (and argv[0] counts if it's not "sysinstall") */
103
104    status = setjmp(BailOut);
105    if (status) {
106	msgConfirm("A signal %d was caught - I'm saving what I can and shutting\n"
107		   "down.  If you can reproduce the problem, please turn Debug on\n"
108		   "in the Options menu for the extra information it provides\n"
109		   "in debugging problems like this.", status);
110  ;
111    }
112
113    /* Begin user dialog at outer menu */
114    dialog_clear();
115    while (1) {
116	choice = scroll = curr = max = 0;
117	dmenuOpen(&MenuMain, &choice, &scroll, &curr, &max, FALSE);
118	if (getpid() != 1
119	    || !msgNoYes("Are you sure you wish to exit?")
120	    )
121	    break;
122    }
123
124    return 0; /* We should never get here */
125}
126