]> andersk Git - moira.git/blob - clients/moira/main.c
af8b7767816c22eca1ab20162b00a2a3d9cdb055
[moira.git] / clients / moira / main.c
1 #if (!defined(lint) && !defined(SABER))
2   static char rcsid_module_c[] = "$Header$";
3 #endif lint
4
5 /*      This is the file main.c for the Moira Client, which allows a nieve
6  *      user to quickly and easily maintain most parts of the Moira database.
7  *      It Contains: The main driver for the Moira Client.
8  *      
9  *      Created:        4/12/88
10  *      By:             Chris D. Peterson
11  *
12  *      $Source$
13  *      $Author$
14  *      $Header$
15  *      
16  *      Copyright 1988 by the Massachusetts Institute of Technology.
17  *
18  *      For further information on copyright and distribution 
19  *      see the file mit-copyright.h
20  */
21
22 #include <pwd.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <moira.h>
28 #include <menu.h>
29 #include <krb_et.h>
30
31 #include "mit-copyright.h"
32 #include "defs.h"
33 #include "f_defs.h"
34 #include "globals.h"
35
36 char * whoami;                  /* used by menu.c ugh!!! */
37 char * moira_server;
38 int interrupt = 0;
39
40 extern Menu moira_top_menu, list_menu, user_menu, dcm_menu;
41
42 #ifndef DEBUG
43 static void SignalHandler(), CatchInterrupt();
44 #endif DEBUG
45
46 static void ErrorExit(), Usage();
47 char *getlogin();
48 uid_t getuid();
49 struct passwd *getpwuid();
50
51 Bool use_menu = TRUE;           /* whether or not we are using a menu. */
52
53 /*      Function Name: main
54  *      Description: The main driver for the Moira Client.
55  *      Arguments: argc, argv - standard command line args.
56  *      Returns: doesn't return.
57  */
58
59 void
60 main(argc, argv)
61     int argc;
62     char ** argv;
63 {
64     int status;
65     Menu *menu;
66     char *motd, **arg;
67 #ifdef POSIX
68     struct sigaction act;
69 #endif
70
71     if ((user = getlogin()) == NULL) 
72         user = getpwuid((int) getuid())->pw_name;
73     user = (user && strlen(user)) ? Strsave(user) : "";
74
75     if ((program_name = strrchr(argv[0], '/')) == NULL)
76       program_name = argv[0];
77     else
78       program_name++;
79     program_name = Strsave(program_name);
80     whoami = Strsave(program_name); /* used by menu.c,  ugh !!! */
81
82     verbose = TRUE;
83     arg = argv;
84     moira_server = NULL;
85
86     while (++arg - argv < argc) {
87         if (**arg == '-') {
88             if (!strcmp(*arg, "-nomenu"))
89               use_menu = FALSE;
90             else if (!strcmp(*arg, "-db"))
91               if (arg - argv < argc - 1) {
92                   ++arg;
93                   moira_server = *arg;
94               } else
95                 Usage(argv);
96             else
97               Usage(argv);
98         }
99     }
100
101     if ( status = mr_connect(moira_server) ) 
102         ErrorExit("\nConnection to Moira server failed", status);
103
104     if ( status = mr_motd(&motd) )
105         ErrorExit("\nUnable to check server status", status);
106     if (motd) {
107         fprintf(stderr, "The Moira server is currently unavailable:\n%s\n", motd);
108         mr_disconnect();
109         exit(1);
110     }
111
112     if ( status = mr_auth(program_name) ) {
113         if (status == MR_USER_AUTH) {
114             char buf[BUFSIZ];
115             com_err(program_name, status, "\nPress [RETURN] to continue");
116             gets(buf);
117         } else {
118           if (status >= ERROR_TABLE_BASE_krb &&
119               status <= ERROR_TABLE_BASE_krb + 256)
120             ErrorExit("\nAuthorization failed -- please run kinit", status);
121           else
122             ErrorExit("\nAuthorization failed.", status);
123         }
124     }
125
126 /*
127  * These signals should not be set until just before we fire up the menu
128  * system. 
129  */
130
131 #ifndef DEBUG
132 #ifdef POSIX
133     sigemptyset(&act.sa_mask);
134     act.sa_flags = 0;
135     act.sa_handler= (void (*)()) SignalHandler;
136     (void) sigaction(SIGHUP, &act, NULL);
137     (void) sigaction(SIGQUIT, &act, NULL);
138     if (use_menu)
139       (void) sigaction(SIGINT, &act, NULL); 
140     else {
141         act.sa_handler= (void (*)()) CatchInterrupt;
142         (void) sigaction(SIGINT, &act, NULL); 
143     }
144 #else
145     (void) signal(SIGHUP, SignalHandler);
146     (void) signal(SIGQUIT, SignalHandler);
147     if (use_menu)
148       (void) signal(SIGINT, SignalHandler); 
149     else
150       (void) signal(SIGINT, CatchInterrupt); 
151 #endif /* POSIX */
152 #endif /* DEBUG */
153
154     initialize_gdss_error_table();
155
156     if (!strcmp(program_name, "listmaint"))
157       menu = &list_menu;
158     else if (!strcmp(program_name, "usermaint"))
159       menu = &user_menu;
160     else if (!strcmp(program_name, "dcmmaint"))
161       menu = &dcm_menu;
162     else
163       menu = &moira_top_menu;
164
165     if (use_menu) {             /* Start menus that execute program */
166         Start_paging();
167         Start_menu(menu);
168         Stop_paging();
169     }
170     else                        /* Start program without menus. */
171         Start_no_menu(menu);
172
173     mr_disconnect();
174     exit(0);
175 }
176
177 /*      Function Name: ErrorExit
178  *      Description: This function does the error handling and exits.
179  *      Arguments: buf - the error message to print.
180  *                 status - the error code.
181  *      Returns: doesn't return.
182  */
183
184 static void
185 ErrorExit(buf,status)
186 int status;
187 char * buf;    
188 {
189     com_err(program_name, status, buf);
190     mr_disconnect();
191     exit(1);
192 }
193
194 /*      Function Name: usage
195  *      Description: Prints usage info and then exits.
196  *      Arguments: none
197  *      Returns: doesn't return.
198  */
199
200 static void
201 Usage()
202 {
203     fprintf(stderr, "Usage: %s [-nomenu]\n", program_name);
204     exit(1);
205 }
206
207 #ifndef DEBUG
208 /*      Function Name: SignalHandler
209  *      Description: This function cleans up from a signal interrupt.
210  *      Arguments: none.
211  *      Returns: doesn't
212  */
213
214 static void
215 SignalHandler()
216 {
217     Put_message("Signal caught - exiting");
218     if (use_menu)
219       Cleanup_menu();
220     mr_disconnect();
221     exit(1);
222 }
223
224
225 static void
226 CatchInterrupt()
227 {
228     Put_message("Interrupt! Press RETURN to continue");
229     interrupt = 1;
230 }
231 #endif DEBUG
This page took 0.080806 seconds and 3 git commands to generate.