]>
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 | |
f91b70c1 | 109 | if (mrcl_connect(moira_server, program_name, QUERY_VERSION, 0) |
110 | != MRCL_SUCCESS) | |
acde26f0 | 111 | exit(1); |
262ca740 | 112 | |
5eaef520 | 113 | if ((status = mr_auth(program_name))) |
114 | { | |
115 | if (status == MR_USER_AUTH) | |
116 | { | |
117 | char buf[BUFSIZ]; | |
118 | com_err(program_name, status, "\nPress [RETURN] to continue"); | |
119 | fgets(buf, BUFSIZ, stdin); | |
120 | } | |
121 | else | |
122 | { | |
71ca689a | 123 | if (status >= ERROR_TABLE_BASE_krb && |
124 | status <= ERROR_TABLE_BASE_krb + 256) | |
125 | ErrorExit("\nAuthorization failed -- please run kinit", status); | |
126 | else | |
127 | ErrorExit("\nAuthorization failed.", status); | |
128 | } | |
1931dc22 | 129 | } |
08345b74 | 130 | |
5eaef520 | 131 | /* |
132 | * These signals should not be set until just before we fire up the menu | |
133 | * system. | |
134 | */ | |
533bacb3 | 135 | SetHandlers(); |
5eaef520 | 136 | |
5eaef520 | 137 | if (!strcmp(program_name, "listmaint")) |
138 | menu = &list_menu; | |
139 | else if (!strcmp(program_name, "usermaint")) | |
140 | menu = &user_menu; | |
141 | else if (!strcmp(program_name, "dcmmaint")) | |
142 | menu = &dcm_menu; | |
143 | else | |
144 | menu = &moira_top_menu; | |
145 | ||
ea0caf4a | 146 | #ifdef HAVE_CURSES |
5eaef520 | 147 | if (use_menu) /* Start menus that execute program */ |
148 | { | |
149 | Start_paging(); | |
150 | Start_menu(menu); | |
151 | Stop_paging(); | |
08345b74 | 152 | } |
5eaef520 | 153 | else /* Start program without menus. */ |
ea0caf4a | 154 | #endif |
5eaef520 | 155 | Start_no_menu(menu); |
08345b74 | 156 | |
5eaef520 | 157 | mr_disconnect(); |
158 | exit(0); | |
08345b74 | 159 | } |
160 | ||
461c03b6 | 161 | /* Function Name: ErrorExit |
08345b74 | 162 | * Description: This function does the error handling and exits. |
163 | * Arguments: buf - the error message to print. | |
164 | * status - the error code. | |
165 | * Returns: doesn't return. | |
166 | */ | |
167 | ||
5eaef520 | 168 | static void ErrorExit(char *buf, int status) |
08345b74 | 169 | { |
5eaef520 | 170 | com_err(program_name, status, buf); |
171 | mr_disconnect(); | |
172 | exit(1); | |
08345b74 | 173 | } |
174 | ||
175 | /* Function Name: usage | |
176 | * Description: Prints usage info and then exits. | |
177 | * Arguments: none | |
178 | * Returns: doesn't return. | |
179 | */ | |
180 | ||
5eaef520 | 181 | static void Usage(void) |
08345b74 | 182 | { |
5eaef520 | 183 | fprintf(stderr, "Usage: %s [-nomenu | -menu] [-db server[:port]]\n", |
184 | program_name); | |
185 | exit(1); | |
08345b74 | 186 | } |
187 | ||
7798ebc3 | 188 | /* Function Name: Signal_Handler |
08345b74 | 189 | * Description: This function cleans up from a signal interrupt. |
190 | * Arguments: none. | |
191 | * Returns: doesn't | |
192 | */ | |
193 | ||
533bacb3 | 194 | static void Signal_Handler(int sig) |
08345b74 | 195 | { |
5eaef520 | 196 | Put_message("Signal caught - exiting"); |
ea0caf4a | 197 | #ifdef HAVE_CURSES |
5eaef520 | 198 | if (use_menu) |
199 | Cleanup_menu(); | |
ea0caf4a | 200 | #endif |
5eaef520 | 201 | mr_disconnect(); |
202 | exit(1); | |
08345b74 | 203 | } |
facd6784 | 204 | |
205 | ||
533bacb3 | 206 | static void CatchInterrupt(int sig) |
facd6784 | 207 | { |
5eaef520 | 208 | Put_message("Interrupt! Press RETURN to continue"); |
209 | interrupt = 1; | |
facd6784 | 210 | } |
533bacb3 | 211 | |
212 | #ifdef HAVE_POSIX_SIGNALS | |
213 | static void SetHandlers(void) | |
214 | { | |
215 | struct sigaction act; | |
216 | ||
217 | sigemptyset(&act.sa_mask); | |
218 | act.sa_flags = 0; | |
219 | act.sa_handler = Signal_Handler; | |
220 | sigaction(SIGHUP, &act, NULL); | |
221 | sigaction(SIGQUIT, &act, NULL); | |
222 | #ifdef HAVE_CURSES | |
223 | if (use_menu) | |
224 | sigaction(SIGINT, &act, NULL); | |
225 | else | |
226 | #endif | |
227 | { | |
228 | act.sa_handler = CatchInterrupt; | |
229 | sigaction(SIGINT, &act, NULL); | |
230 | } | |
231 | } | |
232 | #else | |
233 | static void SetHandlers(void) | |
234 | { | |
235 | signal(SIGTERM, Signal_Handler); | |
236 | #ifdef HAVE_CURSES | |
237 | if (use_menu) | |
238 | signal(SIGINT, Signal_Handler); | |
239 | else | |
240 | #endif | |
241 | signal(SIGINT, CatchInterrupt); | |
242 | } | |
243 | #endif |