]>
Commit | Line | Data |
---|---|---|
c441a31a | 1 | /* $Id$ |
7ac48069 | 2 | * |
3 | * This is the file main.c for the Moira Client, which allows users | |
59ec8dae | 4 | * to quickly and easily maintain most parts of the Moira database. |
1931dc22 | 5 | * It Contains: The main driver for the Moira Client. |
5eaef520 | 6 | * |
08345b74 | 7 | * Created: 4/12/88 |
8 | * By: Chris D. Peterson | |
9 | * | |
7ac48069 | 10 | * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology. |
11 | * For copying and distribution information, please see the file | |
12 | * <mit-copyright.h>. | |
08345b74 | 13 | */ |
14 | ||
7ac48069 | 15 | #include <mit-copyright.h> |
16 | #include <moira.h> | |
acde26f0 | 17 | #include <mrclient.h> |
7ac48069 | 18 | #include "defs.h" |
19 | #include "f_defs.h" | |
20 | #include "globals.h" | |
21 | ||
08345b74 | 22 | #include <signal.h> |
23 | #include <stdio.h> | |
f071d8a7 | 24 | #include <string.h> |
533bacb3 | 25 | #ifdef HAVE_UNISTD_H |
7ac48069 | 26 | #include <unistd.h> |
533bacb3 | 27 | #endif /* HAVE_UNISTD_H */ |
7a40dcd4 | 28 | |
7ac48069 | 29 | RCSID("$Header$"); |
30 | ||
31 | static void ErrorExit(char *buf, int status); | |
32 | static void Usage(void); | |
533bacb3 | 33 | static void Signal_Handler(int sig); |
34 | static void CatchInterrupt(int sig); | |
35 | static void SetHandlers(void); | |
08345b74 | 36 | |
5eaef520 | 37 | char *whoami; /* used by menu.c ugh!!! */ |
38 | char *moira_server; | |
facd6784 | 39 | int interrupt = 0; |
85ca828a | 40 | |
8defc06b | 41 | extern Menu moira_top_menu, list_menu, user_menu, dcm_menu; |
85ca828a | 42 | |
ea0caf4a | 43 | #ifdef HAVE_CURSES |
08345b74 | 44 | Bool use_menu = TRUE; /* whether or not we are using a menu. */ |
ea0caf4a | 45 | #endif |
08345b74 | 46 | |
47 | /* Function Name: main | |
1931dc22 | 48 | * Description: The main driver for the Moira Client. |
08345b74 | 49 | * Arguments: argc, argv - standard command line args. |
50 | * Returns: doesn't return. | |
51 | */ | |
52 | ||
3bad18e8 | 53 | int main(int argc, char **argv) |
08345b74 | 54 | { |
5eaef520 | 55 | int status; |
56 | Menu *menu; | |
533bacb3 | 57 | char **arg; |
5eaef520 | 58 | |
5eaef520 | 59 | if (!(program_name = strrchr(argv[0], '/'))) |
60 | program_name = argv[0]; | |
61 | else | |
62 | program_name++; | |
7ac48069 | 63 | program_name = strdup(program_name); |
64 | whoami = strdup(program_name); /* used by menu.c, ugh !!! */ | |
5eaef520 | 65 | |
acde26f0 | 66 | user = mrcl_krb_user(); |
67 | if (!user) | |
68 | exit(1); | |
69 | ||
5eaef520 | 70 | verbose = TRUE; |
71 | arg = argv; | |
72 | moira_server = NULL; | |
73 | ||
74 | while (++arg - argv < argc) | |
75 | { | |
76 | if (**arg == '-') | |
77 | { | |
78 | if (!strcmp(*arg, "-nomenu")) | |
ea0caf4a | 79 | { |
80 | #ifdef HAVE_CURSES | |
81 | use_menu = FALSE; | |
82 | #else | |
83 | ; | |
84 | #endif | |
85 | } | |
5eaef520 | 86 | else if (!strcmp(*arg, "-menu")) |
ea0caf4a | 87 | { |
88 | #ifdef HAVE_CURSES | |
89 | use_menu = TRUE; | |
90 | #else | |
91 | fprintf(stderr, "%s: No curses support. -menu option ignored\n", | |
92 | whoami); | |
93 | #endif | |
94 | } | |
5eaef520 | 95 | else if (!strcmp(*arg, "-db")) |
96 | { | |
97 | if (arg - argv < argc - 1) | |
98 | { | |
576becad | 99 | ++arg; |
1931dc22 | 100 | moira_server = *arg; |
5eaef520 | 101 | } else |
7ac48069 | 102 | Usage(); |
5eaef520 | 103 | } |
104 | else | |
7ac48069 | 105 | Usage(); |
576becad | 106 | } |
08345b74 | 107 | } |
85ca828a | 108 | |
acde26f0 | 109 | if (mrcl_connect(moira_server, program_name, 2, 0) != MRCL_SUCCESS) |
110 | exit(1); | |
262ca740 | 111 | |
5eaef520 | 112 | if ((status = mr_auth(program_name))) |
113 | { | |
114 | if (status == MR_USER_AUTH) | |
115 | { | |
116 | char buf[BUFSIZ]; | |
117 | com_err(program_name, status, "\nPress [RETURN] to continue"); | |
118 | fgets(buf, BUFSIZ, stdin); | |
119 | } | |
120 | else | |
121 | { | |
71ca689a | 122 | if (status >= ERROR_TABLE_BASE_krb && |
123 | status <= ERROR_TABLE_BASE_krb + 256) | |
124 | ErrorExit("\nAuthorization failed -- please run kinit", status); | |
125 | else | |
126 | ErrorExit("\nAuthorization failed.", status); | |
127 | } | |
1931dc22 | 128 | } |
08345b74 | 129 | |
5eaef520 | 130 | /* |
131 | * These signals should not be set until just before we fire up the menu | |
132 | * system. | |
133 | */ | |
533bacb3 | 134 | SetHandlers(); |
5eaef520 | 135 | |
5eaef520 | 136 | if (!strcmp(program_name, "listmaint")) |
137 | menu = &list_menu; | |
138 | else if (!strcmp(program_name, "usermaint")) | |
139 | menu = &user_menu; | |
140 | else if (!strcmp(program_name, "dcmmaint")) | |
141 | menu = &dcm_menu; | |
142 | else | |
143 | menu = &moira_top_menu; | |
144 | ||
ea0caf4a | 145 | #ifdef HAVE_CURSES |
5eaef520 | 146 | if (use_menu) /* Start menus that execute program */ |
147 | { | |
148 | Start_paging(); | |
149 | Start_menu(menu); | |
150 | Stop_paging(); | |
08345b74 | 151 | } |
5eaef520 | 152 | else /* Start program without menus. */ |
ea0caf4a | 153 | #endif |
5eaef520 | 154 | Start_no_menu(menu); |
08345b74 | 155 | |
5eaef520 | 156 | mr_disconnect(); |
157 | exit(0); | |
08345b74 | 158 | } |
159 | ||
461c03b6 | 160 | /* Function Name: ErrorExit |
08345b74 | 161 | * Description: This function does the error handling and exits. |
162 | * Arguments: buf - the error message to print. | |
163 | * status - the error code. | |
164 | * Returns: doesn't return. | |
165 | */ | |
166 | ||
5eaef520 | 167 | static void ErrorExit(char *buf, int status) |
08345b74 | 168 | { |
5eaef520 | 169 | com_err(program_name, status, buf); |
170 | mr_disconnect(); | |
171 | exit(1); | |
08345b74 | 172 | } |
173 | ||
174 | /* Function Name: usage | |
175 | * Description: Prints usage info and then exits. | |
176 | * Arguments: none | |
177 | * Returns: doesn't return. | |
178 | */ | |
179 | ||
5eaef520 | 180 | static void Usage(void) |
08345b74 | 181 | { |
5eaef520 | 182 | fprintf(stderr, "Usage: %s [-nomenu | -menu] [-db server[:port]]\n", |
183 | program_name); | |
184 | exit(1); | |
08345b74 | 185 | } |
186 | ||
7798ebc3 | 187 | /* Function Name: Signal_Handler |
08345b74 | 188 | * Description: This function cleans up from a signal interrupt. |
189 | * Arguments: none. | |
190 | * Returns: doesn't | |
191 | */ | |
192 | ||
533bacb3 | 193 | static void Signal_Handler(int sig) |
08345b74 | 194 | { |
5eaef520 | 195 | Put_message("Signal caught - exiting"); |
ea0caf4a | 196 | #ifdef HAVE_CURSES |
5eaef520 | 197 | if (use_menu) |
198 | Cleanup_menu(); | |
ea0caf4a | 199 | #endif |
5eaef520 | 200 | mr_disconnect(); |
201 | exit(1); | |
08345b74 | 202 | } |
facd6784 | 203 | |
204 | ||
533bacb3 | 205 | static void CatchInterrupt(int sig) |
facd6784 | 206 | { |
5eaef520 | 207 | Put_message("Interrupt! Press RETURN to continue"); |
208 | interrupt = 1; | |
facd6784 | 209 | } |
533bacb3 | 210 | |
211 | #ifdef HAVE_POSIX_SIGNALS | |
212 | static void SetHandlers(void) | |
213 | { | |
214 | struct sigaction act; | |
215 | ||
216 | sigemptyset(&act.sa_mask); | |
217 | act.sa_flags = 0; | |
218 | act.sa_handler = Signal_Handler; | |
219 | sigaction(SIGHUP, &act, NULL); | |
220 | sigaction(SIGQUIT, &act, NULL); | |
221 | #ifdef HAVE_CURSES | |
222 | if (use_menu) | |
223 | sigaction(SIGINT, &act, NULL); | |
224 | else | |
225 | #endif | |
226 | { | |
227 | act.sa_handler = CatchInterrupt; | |
228 | sigaction(SIGINT, &act, NULL); | |
229 | } | |
230 | } | |
231 | #else | |
232 | static void SetHandlers(void) | |
233 | { | |
234 | signal(SIGTERM, Signal_Handler); | |
235 | #ifdef HAVE_CURSES | |
236 | if (use_menu) | |
237 | signal(SIGINT, Signal_Handler); | |
238 | else | |
239 | #endif | |
240 | signal(SIGINT, CatchInterrupt); | |
241 | } | |
242 | #endif |