]>
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); | |
80ea9aef | 36 | static char* get_program_name(char *arg); |
08345b74 | 37 | |
5eaef520 | 38 | char *whoami; /* used by menu.c ugh!!! */ |
39 | char *moira_server; | |
facd6784 | 40 | int interrupt = 0; |
85ca828a | 41 | |
8defc06b | 42 | extern Menu moira_top_menu, list_menu, user_menu, dcm_menu; |
85ca828a | 43 | |
ea0caf4a | 44 | #ifdef HAVE_CURSES |
08345b74 | 45 | Bool use_menu = TRUE; /* whether or not we are using a menu. */ |
ea0caf4a | 46 | #endif |
08345b74 | 47 | |
48 | /* Function Name: main | |
1931dc22 | 49 | * Description: The main driver for the Moira Client. |
08345b74 | 50 | * Arguments: argc, argv - standard command line args. |
51 | * Returns: doesn't return. | |
52 | */ | |
53 | ||
3bad18e8 | 54 | int main(int argc, char **argv) |
08345b74 | 55 | { |
5eaef520 | 56 | int status; |
57 | Menu *menu; | |
533bacb3 | 58 | char **arg; |
5eaef520 | 59 | |
80ea9aef | 60 | program_name = get_program_name(argv[0]); |
7ac48069 | 61 | whoami = strdup(program_name); /* used by menu.c, ugh !!! */ |
5eaef520 | 62 | |
acde26f0 | 63 | user = mrcl_krb_user(); |
64 | if (!user) | |
65 | exit(1); | |
66 | ||
5eaef520 | 67 | verbose = TRUE; |
68 | arg = argv; | |
69 | moira_server = NULL; | |
70 | ||
71 | while (++arg - argv < argc) | |
72 | { | |
73 | if (**arg == '-') | |
74 | { | |
75 | if (!strcmp(*arg, "-nomenu")) | |
ea0caf4a | 76 | { |
77 | #ifdef HAVE_CURSES | |
78 | use_menu = FALSE; | |
79 | #else | |
80 | ; | |
81 | #endif | |
82 | } | |
5eaef520 | 83 | else if (!strcmp(*arg, "-menu")) |
ea0caf4a | 84 | { |
85 | #ifdef HAVE_CURSES | |
86 | use_menu = TRUE; | |
87 | #else | |
88 | fprintf(stderr, "%s: No curses support. -menu option ignored\n", | |
89 | whoami); | |
90 | #endif | |
91 | } | |
5eaef520 | 92 | else if (!strcmp(*arg, "-db")) |
93 | { | |
94 | if (arg - argv < argc - 1) | |
95 | { | |
576becad | 96 | ++arg; |
1931dc22 | 97 | moira_server = *arg; |
5eaef520 | 98 | } else |
7ac48069 | 99 | Usage(); |
5eaef520 | 100 | } |
101 | else | |
7ac48069 | 102 | Usage(); |
576becad | 103 | } |
08345b74 | 104 | } |
85ca828a | 105 | |
f91b70c1 | 106 | if (mrcl_connect(moira_server, program_name, QUERY_VERSION, 0) |
107 | != MRCL_SUCCESS) | |
acde26f0 | 108 | exit(1); |
262ca740 | 109 | |
0bb1ca53 | 110 | if ((status = mr_krb5_auth(program_name))) |
5eaef520 | 111 | { |
112 | if (status == MR_USER_AUTH) | |
113 | { | |
114 | char buf[BUFSIZ]; | |
115 | com_err(program_name, status, "\nPress [RETURN] to continue"); | |
116 | fgets(buf, BUFSIZ, stdin); | |
117 | } | |
118 | else | |
119 | { | |
71ca689a | 120 | if (status >= ERROR_TABLE_BASE_krb && |
121 | status <= ERROR_TABLE_BASE_krb + 256) | |
122 | ErrorExit("\nAuthorization failed -- please run kinit", status); | |
123 | else | |
124 | ErrorExit("\nAuthorization failed.", status); | |
125 | } | |
1931dc22 | 126 | } |
08345b74 | 127 | |
5eaef520 | 128 | /* |
129 | * These signals should not be set until just before we fire up the menu | |
130 | * system. | |
131 | */ | |
533bacb3 | 132 | SetHandlers(); |
5eaef520 | 133 | |
5eaef520 | 134 | if (!strcmp(program_name, "listmaint")) |
135 | menu = &list_menu; | |
136 | else if (!strcmp(program_name, "usermaint")) | |
137 | menu = &user_menu; | |
138 | else if (!strcmp(program_name, "dcmmaint")) | |
139 | menu = &dcm_menu; | |
140 | else | |
141 | menu = &moira_top_menu; | |
142 | ||
ea0caf4a | 143 | #ifdef HAVE_CURSES |
5eaef520 | 144 | if (use_menu) /* Start menus that execute program */ |
145 | { | |
146 | Start_paging(); | |
147 | Start_menu(menu); | |
148 | Stop_paging(); | |
08345b74 | 149 | } |
5eaef520 | 150 | else /* Start program without menus. */ |
ea0caf4a | 151 | #endif |
5eaef520 | 152 | Start_no_menu(menu); |
08345b74 | 153 | |
5eaef520 | 154 | mr_disconnect(); |
155 | exit(0); | |
08345b74 | 156 | } |
157 | ||
461c03b6 | 158 | /* Function Name: ErrorExit |
08345b74 | 159 | * Description: This function does the error handling and exits. |
160 | * Arguments: buf - the error message to print. | |
161 | * status - the error code. | |
162 | * Returns: doesn't return. | |
163 | */ | |
164 | ||
5eaef520 | 165 | static void ErrorExit(char *buf, int status) |
08345b74 | 166 | { |
5eaef520 | 167 | com_err(program_name, status, buf); |
168 | mr_disconnect(); | |
169 | exit(1); | |
08345b74 | 170 | } |
171 | ||
172 | /* Function Name: usage | |
173 | * Description: Prints usage info and then exits. | |
174 | * Arguments: none | |
175 | * Returns: doesn't return. | |
176 | */ | |
177 | ||
5eaef520 | 178 | static void Usage(void) |
08345b74 | 179 | { |
5eaef520 | 180 | fprintf(stderr, "Usage: %s [-nomenu | -menu] [-db server[:port]]\n", |
181 | program_name); | |
182 | exit(1); | |
08345b74 | 183 | } |
184 | ||
7798ebc3 | 185 | /* Function Name: Signal_Handler |
08345b74 | 186 | * Description: This function cleans up from a signal interrupt. |
187 | * Arguments: none. | |
188 | * Returns: doesn't | |
189 | */ | |
190 | ||
533bacb3 | 191 | static void Signal_Handler(int sig) |
08345b74 | 192 | { |
5eaef520 | 193 | Put_message("Signal caught - exiting"); |
ea0caf4a | 194 | #ifdef HAVE_CURSES |
5eaef520 | 195 | if (use_menu) |
196 | Cleanup_menu(); | |
ea0caf4a | 197 | #endif |
5eaef520 | 198 | mr_disconnect(); |
199 | exit(1); | |
08345b74 | 200 | } |
facd6784 | 201 | |
202 | ||
533bacb3 | 203 | static void CatchInterrupt(int sig) |
facd6784 | 204 | { |
5eaef520 | 205 | Put_message("Interrupt! Press RETURN to continue"); |
206 | interrupt = 1; | |
facd6784 | 207 | } |
533bacb3 | 208 | |
209 | #ifdef HAVE_POSIX_SIGNALS | |
210 | static void SetHandlers(void) | |
211 | { | |
212 | struct sigaction act; | |
213 | ||
214 | sigemptyset(&act.sa_mask); | |
215 | act.sa_flags = 0; | |
216 | act.sa_handler = Signal_Handler; | |
217 | sigaction(SIGHUP, &act, NULL); | |
218 | sigaction(SIGQUIT, &act, NULL); | |
219 | #ifdef HAVE_CURSES | |
220 | if (use_menu) | |
221 | sigaction(SIGINT, &act, NULL); | |
222 | else | |
223 | #endif | |
224 | { | |
225 | act.sa_handler = CatchInterrupt; | |
226 | sigaction(SIGINT, &act, NULL); | |
227 | } | |
228 | } | |
229 | #else | |
230 | static void SetHandlers(void) | |
231 | { | |
232 | signal(SIGTERM, Signal_Handler); | |
233 | #ifdef HAVE_CURSES | |
234 | if (use_menu) | |
235 | signal(SIGINT, Signal_Handler); | |
236 | else | |
237 | #endif | |
238 | signal(SIGINT, CatchInterrupt); | |
239 | } | |
240 | #endif | |
80ea9aef | 241 | |
242 | #ifdef _WIN32 | |
243 | static char* get_program_name(char *arg) | |
244 | { | |
245 | char* prog; | |
246 | char* ext; | |
247 | prog = max(max(strrchr(arg, '/'), strrchr(arg, '\\')) + 1, arg); | |
248 | prog = _strlwr(prog); | |
249 | prog = strdup(prog); | |
250 | ext = strrchr(prog, '.'); | |
251 | if (ext && !strcmp(ext, ".exe")) | |
252 | *ext = 0; | |
253 | return prog; | |
254 | } | |
255 | #else | |
256 | static char* get_program_name(char *arg) | |
257 | { | |
258 | char* prog; | |
259 | if (!(prog = strrchr(arg, '/'))) | |
260 | prog = arg; | |
261 | else | |
262 | prog++; | |
263 | return strdup(prog); | |
264 | } | |
265 | #endif |