]> andersk Git - moira.git/blame - clients/moira/main.c
second code style cleanup: void/void * usage, proper #includes. try to
[moira.git] / clients / moira / main.c
CommitLineData
7ac48069 1/* $Id $
2 *
3 * This is the file main.c for the Moira Client, which allows users
59ec8dae 4 * to quickly and easily maintain most parts of the Moira database.
1931dc22 5 * It Contains: The main driver for the Moira Client.
5eaef520 6 *
08345b74 7 * Created: 4/12/88
8 * By: Chris D. Peterson
9 *
7ac48069 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>.
08345b74 13 */
14
7ac48069 15#include <mit-copyright.h>
16#include <moira.h>
17#include "defs.h"
18#include "f_defs.h"
19#include "globals.h"
20
08345b74 21#include <pwd.h>
22#include <signal.h>
23#include <stdio.h>
f071d8a7 24#include <string.h>
7ac48069 25#include <unistd.h>
08345b74 26
7ac48069 27RCSID("$Header$");
28
29static void ErrorExit(char *buf, int status);
30static void Usage(void);
31static void Signal_Handler(void);
32static void CatchInterrupt(void);
08345b74 33
5eaef520 34char *whoami; /* used by menu.c ugh!!! */
35char *moira_server;
facd6784 36int interrupt = 0;
85ca828a 37
8defc06b 38extern Menu moira_top_menu, list_menu, user_menu, dcm_menu;
85ca828a 39
08345b74 40Bool use_menu = TRUE; /* whether or not we are using a menu. */
41
42/* Function Name: main
1931dc22 43 * Description: The main driver for the Moira Client.
08345b74 44 * Arguments: argc, argv - standard command line args.
45 * Returns: doesn't return.
46 */
47
5eaef520 48void main(int argc, char **argv)
08345b74 49{
5eaef520 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;
7ac48069 57 user = (user && strlen(user)) ? strdup(user) : "";
5eaef520 58
59 if (!(program_name = strrchr(argv[0], '/')))
60 program_name = argv[0];
61 else
62 program_name++;
7ac48069 63 program_name = strdup(program_name);
64 whoami = strdup(program_name); /* used by menu.c, ugh !!! */
5eaef520 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 {
576becad 82 ++arg;
1931dc22 83 moira_server = *arg;
5eaef520 84 } else
7ac48069 85 Usage();
5eaef520 86 }
87 else
7ac48069 88 Usage();
576becad 89 }
08345b74 90 }
85ca828a 91
5eaef520 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);
262ca740 103 }
104
5eaef520 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 {
71ca689a 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 }
1931dc22 121 }
08345b74 122
5eaef520 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;
7ac48069 130 act.sa_handler = Signal_Handler;
5eaef520 131 sigaction(SIGHUP, &act, NULL);
132 sigaction(SIGQUIT, &act, NULL);
133 if (use_menu)
134 sigaction(SIGINT, &act, NULL);
135 else
136 {
7ac48069 137 act.sa_handler = CatchInterrupt;
5eaef520 138 sigaction(SIGINT, &act, NULL);
f071d8a7 139 }
5eaef520 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();
08345b74 157 }
5eaef520 158 else /* Start program without menus. */
159 Start_no_menu(menu);
08345b74 160
5eaef520 161 mr_disconnect();
162 exit(0);
08345b74 163}
164
461c03b6 165/* Function Name: ErrorExit
08345b74 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
5eaef520 172static void ErrorExit(char *buf, int status)
08345b74 173{
5eaef520 174 com_err(program_name, status, buf);
175 mr_disconnect();
176 exit(1);
08345b74 177}
178
179/* Function Name: usage
180 * Description: Prints usage info and then exits.
181 * Arguments: none
182 * Returns: doesn't return.
183 */
184
5eaef520 185static void Usage(void)
08345b74 186{
5eaef520 187 fprintf(stderr, "Usage: %s [-nomenu | -menu] [-db server[:port]]\n",
188 program_name);
189 exit(1);
08345b74 190}
191
7798ebc3 192/* Function Name: Signal_Handler
08345b74 193 * Description: This function cleans up from a signal interrupt.
194 * Arguments: none.
195 * Returns: doesn't
196 */
197
5eaef520 198static void Signal_Handler(void)
08345b74 199{
5eaef520 200 Put_message("Signal caught - exiting");
201 if (use_menu)
202 Cleanup_menu();
203 mr_disconnect();
204 exit(1);
08345b74 205}
facd6784 206
207
5eaef520 208static void CatchInterrupt(void)
facd6784 209{
5eaef520 210 Put_message("Interrupt! Press RETURN to continue");
211 interrupt = 1;
facd6784 212}
This page took 0.155397 seconds and 5 git commands to generate.