]> andersk Git - moira.git/blob - clients/moira/main.c
c3fe52982d403b035f2319103cc882217b927b02
[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 <strings.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 #ifdef _AIX
52 Bool use_menu = FALSE;          /* whether or not we are using a menu. */
53 #else
54 Bool use_menu = TRUE;           /* whether or not we are using a menu. */
55 #endif
56
57 /*      Function Name: main
58  *      Description: The main driver for the Moira Client.
59  *      Arguments: argc, argv - standard command line args.
60  *      Returns: doesn't return.
61  */
62
63 void
64 main(argc, argv)
65     int argc;
66     char ** argv;
67 {
68     int status;
69     Menu *menu;
70     char *motd, **arg;
71
72     if ((user = getlogin()) == NULL) 
73         user = getpwuid((int) getuid())->pw_name;
74     user = (user && strlen(user)) ? Strsave(user) : "";
75
76     if ((program_name = rindex(argv[0], '/')) == NULL)
77       program_name = argv[0];
78     else
79       program_name++;
80     program_name = Strsave(program_name);
81     whoami = Strsave(program_name); /* used by menu.c,  ugh !!! */
82
83     verbose = TRUE;
84     arg = argv;
85     moira_server = NULL;
86
87     while (++arg - argv < argc) {
88         if (**arg == '-') {
89             if (!strcmp(*arg, "-nomenu"))
90               use_menu = FALSE;
91             else if (!strcmp(*arg, "-db"))
92               if (arg - argv < argc - 1) {
93                   ++arg;
94                   moira_server = *arg;
95               } else
96                 Usage(argv);
97             else
98               Usage(argv);
99         }
100     }
101
102     if ( status = mr_connect(moira_server) ) 
103         ErrorExit("\nConnection to Moira server failed", status);
104
105     if ( status = mr_motd(&motd) )
106         ErrorExit("\nUnable to check server status", status);
107     if (motd) {
108         fprintf(stderr, "The Moira server is currently unavailable:\n%s\n", motd);
109         mr_disconnect();
110         exit(1);
111     }
112
113     if ( status = mr_auth(program_name) ) {
114         if (status == MR_USER_AUTH) {
115             char buf[BUFSIZ];
116             com_err(program_name, status, "\nPress [RETURN] to continue");
117             gets(buf);
118         } else {
119           if (status >= ERROR_TABLE_BASE_krb &&
120               status <= ERROR_TABLE_BASE_krb + 256)
121             ErrorExit("\nAuthorization failed -- please run kinit", status);
122           else
123             ErrorExit("\nAuthorization failed.", status);
124         }
125     }
126
127 /*
128  * These signals should not be set until just before we fire up the menu
129  * system. 
130  */
131
132 #ifndef DEBUG
133     (void) signal(SIGHUP, SignalHandler);
134     (void) signal(SIGQUIT, SignalHandler);
135     if (use_menu)
136       (void) signal(SIGINT, SignalHandler); 
137     else
138       (void) signal(SIGINT, CatchInterrupt); 
139 #endif DEBUG
140
141     if (!strcmp(program_name, "listmaint"))
142       menu = &list_menu;
143     else if (!strcmp(program_name, "usermaint"))
144       menu = &user_menu;
145     else if (!strcmp(program_name, "dcmmaint"))
146       menu = &dcm_menu;
147     else
148       menu = &moira_top_menu;
149
150     if (use_menu) {             /* Start menus that execute program */
151         Start_paging();
152         Start_menu(menu);
153         Stop_paging();
154     }
155     else                        /* Start program without menus. */
156         Start_no_menu(menu);
157
158     mr_disconnect();
159     exit(0);
160 }
161
162 /*      Function Name: ErrorExit
163  *      Description: This function does the error handling and exits.
164  *      Arguments: buf - the error message to print.
165  *                 status - the error code.
166  *      Returns: doesn't return.
167  */
168
169 static void
170 ErrorExit(buf,status)
171 int status;
172 char * buf;    
173 {
174     com_err(program_name, status, buf);
175     mr_disconnect();
176     exit(1);
177 }
178
179 /*      Function Name: usage
180  *      Description: Prints usage info and then exits.
181  *      Arguments: none
182  *      Returns: doesn't return.
183  */
184
185 static void
186 Usage()
187 {
188     fprintf(stderr, "Usage: %s [-nomenu]\n", program_name);
189     exit(1);
190 }
191
192 #ifndef DEBUG
193 /*      Function Name: SignalHandler
194  *      Description: This function cleans up from a signal interrupt.
195  *      Arguments: none.
196  *      Returns: doesn't
197  */
198
199 static void
200 SignalHandler()
201 {
202     Put_message("Signal caught - exiting");
203     if (use_menu)
204       Cleanup_menu();
205     mr_disconnect();
206     exit(1);
207 }
208
209
210 static void
211 CatchInterrupt()
212 {
213     Put_message("Interrupt! Press RETURN to continue");
214     interrupt = 1;
215 }
216 #endif DEBUG
This page took 0.047536 seconds and 3 git commands to generate.