]> andersk Git - moira.git/blob - clients/moira/main.c
second code style cleanup: void/void * usage, proper #includes. try to
[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 "defs.h"
18 #include "f_defs.h"
19 #include "globals.h"
20
21 #include <pwd.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 RCSID("$Header$");
28
29 static void ErrorExit(char *buf, int status);
30 static void Usage(void);
31 static void Signal_Handler(void);
32 static void CatchInterrupt(void);
33
34 char *whoami;                   /* used by menu.c ugh!!! */
35 char *moira_server;
36 int interrupt = 0;
37
38 extern Menu moira_top_menu, list_menu, user_menu, dcm_menu;
39
40 Bool use_menu = TRUE;           /* whether or not we are using a menu. */
41
42 /*      Function Name: main
43  *      Description: The main driver for the Moira Client.
44  *      Arguments: argc, argv - standard command line args.
45  *      Returns: doesn't return.
46  */
47
48 void main(int argc, char **argv)
49 {
50   int status;
51   Menu *menu;
52   char *motd, **arg;
53   struct sigaction act;
54
55   if (!(user = getlogin()))
56     user = getpwuid(getuid())->pw_name;
57   user = (user && strlen(user)) ? strdup(user) : "";
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   verbose = TRUE;
67   arg = argv;
68   moira_server = NULL;
69
70   while (++arg - argv < argc)
71     {
72       if (**arg == '-')
73         {
74           if (!strcmp(*arg, "-nomenu"))
75             use_menu = FALSE;
76           else if (!strcmp(*arg, "-menu"))
77             use_menu = TRUE;
78           else if (!strcmp(*arg, "-db"))
79             {
80               if (arg - argv < argc - 1)
81                 {
82                   ++arg;
83                   moira_server = *arg;
84                 } else
85                   Usage();
86             }
87           else
88             Usage();
89         }
90     }
91
92   if ((status = mr_connect(moira_server)))
93     ErrorExit("\nConnection to Moira server failed", status);
94
95   if ((status = mr_motd(&motd)))
96     ErrorExit("\nUnable to check server status", status);
97   if (motd)
98     {
99       fprintf(stderr, "The Moira server is currently unavailable:\n%s\n",
100               motd);
101       mr_disconnect();
102       exit(1);
103     }
104
105   if ((status = mr_auth(program_name)))
106     {
107       if (status == MR_USER_AUTH)
108         {
109           char buf[BUFSIZ];
110           com_err(program_name, status, "\nPress [RETURN] to continue");
111           fgets(buf, BUFSIZ, stdin);
112         }
113       else
114         {
115           if (status >= ERROR_TABLE_BASE_krb &&
116               status <= ERROR_TABLE_BASE_krb + 256)
117             ErrorExit("\nAuthorization failed -- please run kinit", status);
118           else
119             ErrorExit("\nAuthorization failed.", status);
120         }
121     }
122
123   /*
124    * These signals should not be set until just before we fire up the menu
125    * system.
126    */
127
128   sigemptyset(&act.sa_mask);
129   act.sa_flags = 0;
130   act.sa_handler = Signal_Handler;
131   sigaction(SIGHUP, &act, NULL);
132   sigaction(SIGQUIT, &act, NULL);
133   if (use_menu)
134     sigaction(SIGINT, &act, NULL);
135   else
136     {
137       act.sa_handler = CatchInterrupt;
138       sigaction(SIGINT, &act, NULL);
139     }
140
141   initialize_gdss_error_table();
142
143   if (!strcmp(program_name, "listmaint"))
144     menu = &list_menu;
145   else if (!strcmp(program_name, "usermaint"))
146     menu = &user_menu;
147   else if (!strcmp(program_name, "dcmmaint"))
148     menu = &dcm_menu;
149   else
150     menu = &moira_top_menu;
151
152   if (use_menu)         /* Start menus that execute program */
153     {
154       Start_paging();
155       Start_menu(menu);
156       Stop_paging();
157     }
158   else                  /* Start program without menus. */
159     Start_no_menu(menu);
160
161   mr_disconnect();
162   exit(0);
163 }
164
165 /*      Function Name: ErrorExit
166  *      Description: This function does the error handling and exits.
167  *      Arguments: buf - the error message to print.
168  *                 status - the error code.
169  *      Returns: doesn't return.
170  */
171
172 static void ErrorExit(char *buf, int status)
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 Usage(void)
186 {
187   fprintf(stderr, "Usage: %s [-nomenu | -menu] [-db server[:port]]\n",
188           program_name);
189   exit(1);
190 }
191
192 /*      Function Name: Signal_Handler
193  *      Description: This function cleans up from a signal interrupt.
194  *      Arguments: none.
195  *      Returns: doesn't
196  */
197
198 static void Signal_Handler(void)
199 {
200   Put_message("Signal caught - exiting");
201   if (use_menu)
202     Cleanup_menu();
203   mr_disconnect();
204   exit(1);
205 }
206
207
208 static void CatchInterrupt(void)
209 {
210   Put_message("Interrupt! Press RETURN to continue");
211   interrupt = 1;
212 }
This page took 0.052292 seconds and 5 git commands to generate.