-#if (!defined(lint) && !defined(SABER))
- static char rcsid_module_c[] = "$Header$";
-#endif lint
-
-/* This is the file main.c for the Moira Client, which allows a nieve
- * user to quickly and easily maintain most parts of the Moira database.
+/* $Id$
+ *
+ * This is the file main.c for the Moira Client, which allows users
+ * to quickly and easily maintain most parts of the Moira database.
* It Contains: The main driver for the Moira Client.
- *
+ *
* Created: 4/12/88
* By: Chris D. Peterson
*
- * $Source$
- * $Author$
- * $Header$
- *
- * Copyright 1988 by the Massachusetts Institute of Technology.
- *
- * For further information on copyright and distribution
- * see the file mit-copyright.h
+ * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology.
+ * For copying and distribution information, please see the file
+ * <mit-copyright.h>.
*/
-#include <pwd.h>
-#include <signal.h>
-#include <stdio.h>
-#include <strings.h>
-#include <sys/types.h>
+#include <mit-copyright.h>
#include <moira.h>
-#include <menu.h>
-#include <krb_et.h>
-
-#include "mit-copyright.h"
+#include <mrclient.h>
#include "defs.h"
#include "f_defs.h"
#include "globals.h"
-char * whoami; /* used by menu.c ugh!!! */
-char * moira_server;
-int interrupt = 0;
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif /* HAVE_UNISTD_H */
-extern Menu moira_top_menu, list_menu, user_menu, dcm_menu;
+RCSID("$Header$");
-#ifndef DEBUG
-static void SignalHandler(), CatchInterrupt();
-#endif DEBUG
+static void ErrorExit(char *buf, int status);
+static void Usage(void);
+static void Signal_Handler(int sig);
+static void CatchInterrupt(int sig);
+static void SetHandlers(void);
+static char* get_program_name(char *arg);
-static void ErrorExit(), Usage();
-char *getlogin();
-uid_t getuid();
-struct passwd *getpwuid();
+char *whoami; /* used by menu.c ugh!!! */
+char *moira_server;
+int interrupt = 0;
-#ifdef _AIX
-Bool use_menu = FALSE; /* whether or not we are using a menu. */
-#else
+extern Menu moira_top_menu, list_menu, user_menu, dcm_menu;
+
+#ifdef HAVE_CURSES
Bool use_menu = TRUE; /* whether or not we are using a menu. */
#endif
* Returns: doesn't return.
*/
-void
-main(argc, argv)
- int argc;
- char ** argv;
+int main(int argc, char **argv)
{
- int status;
- Menu *menu;
- char *motd, **arg;
-
- if ((user = getlogin()) == NULL)
- user = getpwuid((int) getuid())->pw_name;
- user = (user && strlen(user)) ? Strsave(user) : "";
-
- if ((program_name = rindex(argv[0], '/')) == NULL)
- program_name = argv[0];
- else
- program_name++;
- program_name = Strsave(program_name);
- whoami = Strsave(program_name); /* used by menu.c, ugh !!! */
-
- verbose = TRUE;
- arg = argv;
- moira_server = NULL;
-
- while (++arg - argv < argc) {
- if (**arg == '-') {
- if (!strcmp(*arg, "-nomenu"))
+ int status;
+ Menu *menu;
+ char **arg;
+
+ program_name = get_program_name(argv[0]);
+ whoami = strdup(program_name); /* used by menu.c, ugh !!! */
+
+ user = mrcl_krb_user();
+ if (!user)
+ exit(1);
+
+ verbose = TRUE;
+ arg = argv;
+ moira_server = NULL;
+
+ while (++arg - argv < argc)
+ {
+ if (**arg == '-')
+ {
+ if (!strcmp(*arg, "-nomenu"))
+ {
+#ifdef HAVE_CURSES
use_menu = FALSE;
- else if (!strcmp(*arg, "-db"))
- if (arg - argv < argc - 1) {
+#else
+ ;
+#endif
+ }
+ else if (!strcmp(*arg, "-menu"))
+ {
+#ifdef HAVE_CURSES
+ use_menu = TRUE;
+#else
+ fprintf(stderr, "%s: No curses support. -menu option ignored\n",
+ whoami);
+#endif
+ }
+ else if (!strcmp(*arg, "-db"))
+ {
+ if (arg - argv < argc - 1)
+ {
++arg;
moira_server = *arg;
- } else
- Usage(argv);
- else
- Usage(argv);
+ } else
+ Usage();
+ }
+ else
+ Usage();
}
}
- if ( status = mr_connect(moira_server) )
- ErrorExit("\nConnection to Moira server failed", status);
-
- if ( status = mr_motd(&motd) )
- ErrorExit("\nUnable to check server status", status);
- if (motd) {
- fprintf(stderr, "The Moira server is currently unavailable:\n%s\n", motd);
- mr_disconnect();
- exit(1);
- }
+ if (mrcl_connect(moira_server, program_name, QUERY_VERSION, 0)
+ != MRCL_SUCCESS)
+ exit(1);
- if ( status = mr_auth(program_name) ) {
- if (status == MR_USER_AUTH) {
- char buf[BUFSIZ];
- com_err(program_name, status, "\nPress [RETURN] to continue");
- gets(buf);
- } else {
+ if ((status = mr_auth(program_name)))
+ {
+ if (status == MR_USER_AUTH)
+ {
+ char buf[BUFSIZ];
+ com_err(program_name, status, "\nPress [RETURN] to continue");
+ fgets(buf, BUFSIZ, stdin);
+ }
+ else
+ {
if (status >= ERROR_TABLE_BASE_krb &&
status <= ERROR_TABLE_BASE_krb + 256)
ErrorExit("\nAuthorization failed -- please run kinit", status);
}
}
-/*
- * These signals should not be set until just before we fire up the menu
- * system.
- */
+ /*
+ * These signals should not be set until just before we fire up the menu
+ * system.
+ */
+ SetHandlers();
+
+ if (!strcmp(program_name, "listmaint"))
+ menu = &list_menu;
+ else if (!strcmp(program_name, "usermaint"))
+ menu = &user_menu;
+ else if (!strcmp(program_name, "dcmmaint"))
+ menu = &dcm_menu;
+ else
+ menu = &moira_top_menu;
-#ifndef DEBUG
- (void) signal(SIGHUP, SignalHandler);
- (void) signal(SIGQUIT, SignalHandler);
- if (use_menu)
- (void) signal(SIGINT, SignalHandler);
- else
- (void) signal(SIGINT, CatchInterrupt);
-#endif DEBUG
-
- if (!strcmp(program_name, "listmaint"))
- menu = &list_menu;
- else if (!strcmp(program_name, "usermaint"))
- menu = &user_menu;
- else if (!strcmp(program_name, "dcmmaint"))
- menu = &dcm_menu;
- else
- menu = &moira_top_menu;
-
- if (use_menu) { /* Start menus that execute program */
- Start_paging();
- Start_menu(menu);
- Stop_paging();
+#ifdef HAVE_CURSES
+ if (use_menu) /* Start menus that execute program */
+ {
+ Start_paging();
+ Start_menu(menu);
+ Stop_paging();
}
- else /* Start program without menus. */
- Start_no_menu(menu);
+ else /* Start program without menus. */
+#endif
+ Start_no_menu(menu);
- mr_disconnect();
- exit(0);
+ mr_disconnect();
+ exit(0);
}
/* Function Name: ErrorExit
* Returns: doesn't return.
*/
-static void
-ErrorExit(buf,status)
-int status;
-char * buf;
+static void ErrorExit(char *buf, int status)
{
- com_err(program_name, status, buf);
- mr_disconnect();
- exit(1);
+ com_err(program_name, status, buf);
+ mr_disconnect();
+ exit(1);
}
/* Function Name: usage
* Returns: doesn't return.
*/
-static void
-Usage()
+static void Usage(void)
{
- fprintf(stderr, "Usage: %s [-nomenu]\n", program_name);
- exit(1);
+ fprintf(stderr, "Usage: %s [-nomenu | -menu] [-db server[:port]]\n",
+ program_name);
+ exit(1);
}
-#ifndef DEBUG
-/* Function Name: SignalHandler
+/* Function Name: Signal_Handler
* Description: This function cleans up from a signal interrupt.
* Arguments: none.
* Returns: doesn't
*/
-static void
-SignalHandler()
+static void Signal_Handler(int sig)
{
- Put_message("Signal caught - exiting");
- if (use_menu)
- Cleanup_menu();
- mr_disconnect();
- exit(1);
+ Put_message("Signal caught - exiting");
+#ifdef HAVE_CURSES
+ if (use_menu)
+ Cleanup_menu();
+#endif
+ mr_disconnect();
+ exit(1);
}
-static void
-CatchInterrupt()
+static void CatchInterrupt(int sig)
{
- Put_message("Interrupt! Press RETURN to continue");
- interrupt = 1;
+ Put_message("Interrupt! Press RETURN to continue");
+ interrupt = 1;
}
-#endif DEBUG
+
+#ifdef HAVE_POSIX_SIGNALS
+static void SetHandlers(void)
+{
+ struct sigaction act;
+
+ sigemptyset(&act.sa_mask);
+ act.sa_flags = 0;
+ act.sa_handler = Signal_Handler;
+ sigaction(SIGHUP, &act, NULL);
+ sigaction(SIGQUIT, &act, NULL);
+#ifdef HAVE_CURSES
+ if (use_menu)
+ sigaction(SIGINT, &act, NULL);
+ else
+#endif
+ {
+ act.sa_handler = CatchInterrupt;
+ sigaction(SIGINT, &act, NULL);
+ }
+}
+#else
+static void SetHandlers(void)
+{
+ signal(SIGTERM, Signal_Handler);
+#ifdef HAVE_CURSES
+ if (use_menu)
+ signal(SIGINT, Signal_Handler);
+ else
+#endif
+ signal(SIGINT, CatchInterrupt);
+}
+#endif
+
+#ifdef _WIN32
+static char* get_program_name(char *arg)
+{
+ char* prog;
+ char* ext;
+ prog = max(max(strrchr(arg, '/'), strrchr(arg, '\\')) + 1, arg);
+ prog = _strlwr(prog);
+ prog = strdup(prog);
+ ext = strrchr(prog, '.');
+ if (ext && !strcmp(ext, ".exe"))
+ *ext = 0;
+ return prog;
+}
+#else
+static char* get_program_name(char *arg)
+{
+ char* prog;
+ if (!(prog = strrchr(arg, '/')))
+ prog = arg;
+ else
+ prog++;
+ return strdup(prog);
+}
+#endif