]> andersk Git - moira.git/blame_incremental - clients/moira/main.c
First cut at support for winhomedir and winprofiledir support.
[moira.git] / clients / moira / main.c
... / ...
CommitLineData
1/* $Id$
2 *
3 * This is the file main.c for the Moira Client, which allows users
4 * to quickly and easily maintain most parts of the Moira database.
5 * It Contains: The main driver for the Moira Client.
6 *
7 * Created: 4/12/88
8 * By: Chris D. Peterson
9 *
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>.
13 */
14
15#include <mit-copyright.h>
16#include <moira.h>
17#include <mrclient.h>
18#include "defs.h"
19#include "f_defs.h"
20#include "globals.h"
21
22#include <signal.h>
23#include <stdio.h>
24#include <string.h>
25#ifdef HAVE_UNISTD_H
26#include <unistd.h>
27#endif /* HAVE_UNISTD_H */
28
29RCSID("$Header$");
30
31static void ErrorExit(char *buf, int status);
32static void Usage(void);
33static void Signal_Handler(int sig);
34static void CatchInterrupt(int sig);
35static void SetHandlers(void);
36static char* get_program_name(char *arg);
37
38char *whoami; /* used by menu.c ugh!!! */
39char *moira_server;
40int interrupt = 0;
41
42extern Menu moira_top_menu, list_menu, user_menu, dcm_menu;
43
44#ifdef HAVE_CURSES
45Bool use_menu = TRUE; /* whether or not we are using a menu. */
46#endif
47
48/* Function Name: main
49 * Description: The main driver for the Moira Client.
50 * Arguments: argc, argv - standard command line args.
51 * Returns: doesn't return.
52 */
53
54int main(int argc, char **argv)
55{
56 int status;
57 Menu *menu;
58 char **arg;
59
60 program_name = get_program_name(argv[0]);
61 whoami = strdup(program_name); /* used by menu.c, ugh !!! */
62
63 user = mrcl_krb_user();
64 if (!user)
65 exit(1);
66
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"))
76 {
77#ifdef HAVE_CURSES
78 use_menu = FALSE;
79#else
80 ;
81#endif
82 }
83 else if (!strcmp(*arg, "-menu"))
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 }
92 else if (!strcmp(*arg, "-db"))
93 {
94 if (arg - argv < argc - 1)
95 {
96 ++arg;
97 moira_server = *arg;
98 } else
99 Usage();
100 }
101 else
102 Usage();
103 }
104 }
105
106 if (mrcl_connect(moira_server, program_name, QUERY_VERSION, 0)
107 != MRCL_SUCCESS)
108 exit(1);
109
110 if ((status = mr_auth(program_name)))
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 {
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 }
126 }
127
128 /*
129 * These signals should not be set until just before we fire up the menu
130 * system.
131 */
132 SetHandlers();
133
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
143#ifdef HAVE_CURSES
144 if (use_menu) /* Start menus that execute program */
145 {
146 Start_paging();
147 Start_menu(menu);
148 Stop_paging();
149 }
150 else /* Start program without menus. */
151#endif
152 Start_no_menu(menu);
153
154 mr_disconnect();
155 exit(0);
156}
157
158/* Function Name: ErrorExit
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
165static void ErrorExit(char *buf, int status)
166{
167 com_err(program_name, status, buf);
168 mr_disconnect();
169 exit(1);
170}
171
172/* Function Name: usage
173 * Description: Prints usage info and then exits.
174 * Arguments: none
175 * Returns: doesn't return.
176 */
177
178static void Usage(void)
179{
180 fprintf(stderr, "Usage: %s [-nomenu | -menu] [-db server[:port]]\n",
181 program_name);
182 exit(1);
183}
184
185/* Function Name: Signal_Handler
186 * Description: This function cleans up from a signal interrupt.
187 * Arguments: none.
188 * Returns: doesn't
189 */
190
191static void Signal_Handler(int sig)
192{
193 Put_message("Signal caught - exiting");
194#ifdef HAVE_CURSES
195 if (use_menu)
196 Cleanup_menu();
197#endif
198 mr_disconnect();
199 exit(1);
200}
201
202
203static void CatchInterrupt(int sig)
204{
205 Put_message("Interrupt! Press RETURN to continue");
206 interrupt = 1;
207}
208
209#ifdef HAVE_POSIX_SIGNALS
210static 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
230static 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
241
242#ifdef _WIN32
243static 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
256static 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
This page took 0.034605 seconds and 5 git commands to generate.