]> andersk Git - moira.git/blob - clients/moira/main.c
Fix typo.
[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 <pwd.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include <krb.h>
29
30 RCSID("$Header$");
31
32 static void ErrorExit(char *buf, int status);
33 static void Usage(void);
34 static void Signal_Handler(void);
35 static void CatchInterrupt(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, pname[ANAME_SZ];
58   struct sigaction act;
59
60   if (!(program_name = strrchr(argv[0], '/')))
61     program_name = argv[0];
62   else
63     program_name++;
64   program_name = strdup(program_name);
65   whoami = strdup(program_name); /* used by menu.c,  ugh !!! */
66
67   user = mrcl_krb_user();
68   if (!user)
69     exit(1);
70
71   verbose = TRUE;
72   arg = argv;
73   moira_server = NULL;
74
75   while (++arg - argv < argc)
76     {
77       if (**arg == '-')
78         {
79           if (!strcmp(*arg, "-nomenu"))
80             {
81 #ifdef HAVE_CURSES
82               use_menu = FALSE;
83 #else
84               ;
85 #endif
86             }
87           else if (!strcmp(*arg, "-menu"))
88             {
89 #ifdef HAVE_CURSES
90               use_menu = TRUE;
91 #else
92               fprintf(stderr, "%s: No curses support. -menu option ignored\n",
93                       whoami);
94 #endif
95             }
96           else if (!strcmp(*arg, "-db"))
97             {
98               if (arg - argv < argc - 1)
99                 {
100                   ++arg;
101                   moira_server = *arg;
102                 } else
103                   Usage();
104             }
105           else
106             Usage();
107         }
108     }
109
110   if (mrcl_connect(moira_server, program_name, 2, 0) != 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
136   sigemptyset(&act.sa_mask);
137   act.sa_flags = 0;
138   act.sa_handler = Signal_Handler;
139   sigaction(SIGHUP, &act, NULL);
140   sigaction(SIGQUIT, &act, NULL);
141 #ifdef HAVE_CURSES
142   if (use_menu)
143     sigaction(SIGINT, &act, NULL);
144   else
145 #endif
146     {
147       act.sa_handler = CatchInterrupt;
148       sigaction(SIGINT, &act, NULL);
149     }
150
151   if (!strcmp(program_name, "listmaint"))
152     menu = &list_menu;
153   else if (!strcmp(program_name, "usermaint"))
154     menu = &user_menu;
155   else if (!strcmp(program_name, "dcmmaint"))
156     menu = &dcm_menu;
157   else
158     menu = &moira_top_menu;
159
160 #ifdef HAVE_CURSES
161   if (use_menu)         /* Start menus that execute program */
162     {
163       Start_paging();
164       Start_menu(menu);
165       Stop_paging();
166     }
167   else                  /* Start program without menus. */
168 #endif
169     Start_no_menu(menu);
170
171   mr_disconnect();
172   exit(0);
173 }
174
175 /*      Function Name: ErrorExit
176  *      Description: This function does the error handling and exits.
177  *      Arguments: buf - the error message to print.
178  *                 status - the error code.
179  *      Returns: doesn't return.
180  */
181
182 static void ErrorExit(char *buf, int status)
183 {
184   com_err(program_name, status, buf);
185   mr_disconnect();
186   exit(1);
187 }
188
189 /*      Function Name: usage
190  *      Description: Prints usage info and then exits.
191  *      Arguments: none
192  *      Returns: doesn't return.
193  */
194
195 static void Usage(void)
196 {
197   fprintf(stderr, "Usage: %s [-nomenu | -menu] [-db server[:port]]\n",
198           program_name);
199   exit(1);
200 }
201
202 /*      Function Name: Signal_Handler
203  *      Description: This function cleans up from a signal interrupt.
204  *      Arguments: none.
205  *      Returns: doesn't
206  */
207
208 static void Signal_Handler(void)
209 {
210   Put_message("Signal caught - exiting");
211 #ifdef HAVE_CURSES
212   if (use_menu)
213     Cleanup_menu();
214 #endif
215   mr_disconnect();
216   exit(1);
217 }
218
219
220 static void CatchInterrupt(void)
221 {
222   Put_message("Interrupt! Press RETURN to continue");
223   interrupt = 1;
224 }
This page took 0.060191 seconds and 5 git commands to generate.