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