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