]> andersk Git - moira.git/blob - clients/moira/main.c
Heap o' new functionality. Specifically:
[moira.git] / clients / moira / main.c
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
29 RCSID("$Header$");
30
31 static void ErrorExit(char *buf, int status);
32 static void Usage(void);
33 static void Signal_Handler(int sig);
34 static void CatchInterrupt(int sig);
35 static void SetHandlers(void);
36
37 char *whoami;                   /* used by menu.c ugh!!! */
38 char *moira_server;
39 int interrupt = 0;
40
41 extern Menu moira_top_menu, list_menu, user_menu, dcm_menu;
42
43 #ifdef HAVE_CURSES
44 Bool use_menu = TRUE;           /* whether or not we are using a menu. */
45 #endif
46
47 /*      Function Name: main
48  *      Description: The main driver for the Moira Client.
49  *      Arguments: argc, argv - standard command line args.
50  *      Returns: doesn't return.
51  */
52
53 int main(int argc, char **argv)
54 {
55   int status;
56   Menu *menu;
57   char **arg;
58
59   if (!(program_name = strrchr(argv[0], '/')))
60     program_name = argv[0];
61   else
62     program_name++;
63   program_name = strdup(program_name);
64   whoami = strdup(program_name); /* used by menu.c,  ugh !!! */
65
66   user = mrcl_krb_user();
67   if (!user)
68     exit(1);
69
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"))
79             {
80 #ifdef HAVE_CURSES
81               use_menu = FALSE;
82 #else
83               ;
84 #endif
85             }
86           else if (!strcmp(*arg, "-menu"))
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             }
95           else if (!strcmp(*arg, "-db"))
96             {
97               if (arg - argv < argc - 1)
98                 {
99                   ++arg;
100                   moira_server = *arg;
101                 } else
102                   Usage();
103             }
104           else
105             Usage();
106         }
107     }
108
109   if (mrcl_connect(moira_server, program_name, QUERY_VERSION, 0) 
110       != MRCL_SUCCESS)
111     exit(1);
112
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         {
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         }
129     }
130
131   /*
132    * These signals should not be set until just before we fire up the menu
133    * system.
134    */
135   SetHandlers();
136
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
146 #ifdef HAVE_CURSES
147   if (use_menu)         /* Start menus that execute program */
148     {
149       Start_paging();
150       Start_menu(menu);
151       Stop_paging();
152     }
153   else                  /* Start program without menus. */
154 #endif
155     Start_no_menu(menu);
156
157   mr_disconnect();
158   exit(0);
159 }
160
161 /*      Function Name: ErrorExit
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
168 static void ErrorExit(char *buf, int status)
169 {
170   com_err(program_name, status, buf);
171   mr_disconnect();
172   exit(1);
173 }
174
175 /*      Function Name: usage
176  *      Description: Prints usage info and then exits.
177  *      Arguments: none
178  *      Returns: doesn't return.
179  */
180
181 static void Usage(void)
182 {
183   fprintf(stderr, "Usage: %s [-nomenu | -menu] [-db server[:port]]\n",
184           program_name);
185   exit(1);
186 }
187
188 /*      Function Name: Signal_Handler
189  *      Description: This function cleans up from a signal interrupt.
190  *      Arguments: none.
191  *      Returns: doesn't
192  */
193
194 static void Signal_Handler(int sig)
195 {
196   Put_message("Signal caught - exiting");
197 #ifdef HAVE_CURSES
198   if (use_menu)
199     Cleanup_menu();
200 #endif
201   mr_disconnect();
202   exit(1);
203 }
204
205
206 static void CatchInterrupt(int sig)
207 {
208   Put_message("Interrupt! Press RETURN to continue");
209   interrupt = 1;
210 }
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
This page took 0.083482 seconds and 5 git commands to generate.