]> andersk Git - moira.git/blob - clients/moira/main.c
Change `SMS' to `Moira' where possible.
[moira.git] / clients / moira / main.c
1 #if (!defined(lint) && !defined(SABER))
2   static char rcsid_module_c[] = "$Header$";
3 #endif
4
5 /*      This is the file main.c for the Moira Client, which allows users
6  *      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 static void Signal_Handler(), CatchInterrupt();
43
44 static void ErrorExit(), Usage();
45 char *getlogin();
46 uid_t getuid();
47 struct passwd *getpwuid();
48
49 Bool use_menu = TRUE;           /* whether or not we are using a menu. */
50
51 /*      Function Name: main
52  *      Description: The main driver for the Moira Client.
53  *      Arguments: argc, argv - standard command line args.
54  *      Returns: doesn't return.
55  */
56
57 void main(int argc, char **argv)
58 {
59   int status;
60   Menu *menu;
61   char *motd, **arg;
62   struct sigaction act;
63
64   if (!(user = getlogin()))
65     user = getpwuid(getuid())->pw_name;
66   user = (user && strlen(user)) ? Strsave(user) : "";
67
68   if (!(program_name = strrchr(argv[0], '/')))
69     program_name = argv[0];
70   else
71     program_name++;
72   program_name = Strsave(program_name);
73   whoami = Strsave(program_name); /* used by menu.c,  ugh !!! */
74
75   verbose = TRUE;
76   arg = argv;
77   moira_server = NULL;
78
79   while (++arg - argv < argc)
80     {
81       if (**arg == '-')
82         {
83           if (!strcmp(*arg, "-nomenu"))
84             use_menu = FALSE;
85           else if (!strcmp(*arg, "-menu"))
86             use_menu = TRUE;
87           else if (!strcmp(*arg, "-db"))
88             {
89               if (arg - argv < argc - 1)
90                 {
91                   ++arg;
92                   moira_server = *arg;
93                 } else
94                   Usage(argv);
95             }
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     {
108       fprintf(stderr, "The Moira server is currently unavailable:\n%s\n",
109               motd);
110       mr_disconnect();
111       exit(1);
112     }
113
114   if ((status = mr_auth(program_name)))
115     {
116       if (status == MR_USER_AUTH)
117         {
118           char buf[BUFSIZ];
119           com_err(program_name, status, "\nPress [RETURN] to continue");
120           fgets(buf, BUFSIZ, stdin);
121         }
122       else
123         {
124           if (status >= ERROR_TABLE_BASE_krb &&
125               status <= ERROR_TABLE_BASE_krb + 256)
126             ErrorExit("\nAuthorization failed -- please run kinit", status);
127           else
128             ErrorExit("\nAuthorization failed.", status);
129         }
130     }
131
132   /*
133    * These signals should not be set until just before we fire up the menu
134    * system.
135    */
136
137   sigemptyset(&act.sa_mask);
138   act.sa_flags = 0;
139   act.sa_handler = (void (*)()) Signal_Handler;
140   sigaction(SIGHUP, &act, NULL);
141   sigaction(SIGQUIT, &act, NULL);
142   if (use_menu)
143     sigaction(SIGINT, &act, NULL);
144   else
145     {
146       act.sa_handler = (void (*)()) CatchInterrupt;
147       sigaction(SIGINT, &act, NULL);
148     }
149
150   initialize_gdss_error_table();
151
152   if (!strcmp(program_name, "listmaint"))
153     menu = &list_menu;
154   else if (!strcmp(program_name, "usermaint"))
155     menu = &user_menu;
156   else if (!strcmp(program_name, "dcmmaint"))
157     menu = &dcm_menu;
158   else
159     menu = &moira_top_menu;
160
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     Start_no_menu(menu);
169
170   mr_disconnect();
171   exit(0);
172 }
173
174 /*      Function Name: ErrorExit
175  *      Description: This function does the error handling and exits.
176  *      Arguments: buf - the error message to print.
177  *                 status - the error code.
178  *      Returns: doesn't return.
179  */
180
181 static void ErrorExit(char *buf, int status)
182 {
183   com_err(program_name, status, buf);
184   mr_disconnect();
185   exit(1);
186 }
187
188 /*      Function Name: usage
189  *      Description: Prints usage info and then exits.
190  *      Arguments: none
191  *      Returns: doesn't return.
192  */
193
194 static void Usage(void)
195 {
196   fprintf(stderr, "Usage: %s [-nomenu | -menu] [-db server[:port]]\n",
197           program_name);
198   exit(1);
199 }
200
201 /*      Function Name: Signal_Handler
202  *      Description: This function cleans up from a signal interrupt.
203  *      Arguments: none.
204  *      Returns: doesn't
205  */
206
207 static void Signal_Handler(void)
208 {
209   Put_message("Signal caught - exiting");
210   if (use_menu)
211     Cleanup_menu();
212   mr_disconnect();
213   exit(1);
214 }
215
216
217 static void CatchInterrupt(void)
218 {
219   Put_message("Interrupt! Press RETURN to continue");
220   interrupt = 1;
221 }
This page took 0.051815 seconds and 5 git commands to generate.