-#if (!defined(lint) && !defined(SABER))
- static char rcsid_module_c[] = "$Header$";
-#endif lint
-
-/* This is the file utils.c for the SMS Client, which allows a nieve
- * user to quickly and easily maintain most parts of the SMS database.
+/* $Id$
+ *
+ * This is the file utils.c for the Moira Client, which allows users
+ * to quickly and easily maintain most parts of the Moira database.
* It Contains: Many useful utility functions.
- *
+ *
* Created: 4/25/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 <stdio.h>
-#include <strings.h>
-#include <sms.h>
-#include <menu.h>
-
-#include "mit-copyright.h"
+#include <mit-copyright.h>
+#include <moira.h>
+#include <moira_site.h>
#include "defs.h"
#include "f_defs.h"
#include "globals.h"
-#include "infodefs.h"
+#include <sys/types.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
#include <netdb.h> /* for gethostbyname. */
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+RCSID("$Header$");
+
/* Function Name: AddQueue
* Description: Adds an element to a queue
* Arguments: elem, pred - element and its predecessor.
* Returns: none.
*/
-static void
-AddQueue(elem, pred)
-struct qelem * elem, *pred;
+static void AddQueue(struct mqelem *elem, struct mqelem *pred)
{
- if (pred == NULL) {
- elem->q_forw = NULL;
- elem->q_back = NULL;
- return;
+ if (!pred)
+ {
+ elem->q_forw = NULL;
+ elem->q_back = NULL;
+ return;
}
- elem->q_back = pred;
- elem->q_forw = pred->q_forw;
- pred->q_forw = elem;
+ elem->q_back = pred;
+ elem->q_forw = pred->q_forw;
+ pred->q_forw = elem;
}
/* Function Name: RemoveQueue
* Returns: none.
*/
-static void
-RemoveQueue(elem)
-struct qelem *elem;
+static void RemoveQueue(struct mqelem *elem)
{
- if (elem->q_forw != NULL)
- (elem->q_forw)->q_back = elem->q_back;
- if (elem->q_back != NULL)
- (elem->q_back)->q_forw = elem->q_forw;
+ if (elem->q_forw)
+ elem->q_forw->q_back = elem->q_back;
+ if (elem->q_back)
+ elem->q_back->q_forw = elem->q_forw;
}
+
+/* CopyInfo: allocates memory for a copy of a NULL terminated array of
+ * strings <and returns a pointer to the copy.
+ */
+
+char **CopyInfo(char **info)
+{
+ char **ret;
+ int i;
+
+ ret = malloc(sizeof(char *) * (CountArgs(info) + 1));
+ if (!ret)
+ return ret;
+ for (i = 0; info[i]; i++)
+ ret[i] = strdup(info[i]);
+ ret[i] = NULL;
+ return ret;
+}
+
+
/* Function Name: FreeInfo
* Description: Frees all elements of a NULL terminated arrary of char*'s
* Arguments: info - array who's elements we are to free.
* Returns: none.
*/
-void
-FreeInfo(info)
-char ** info;
+void FreeInfo(char **info)
{
- while (*info != NULL)
- FreeAndClear(info++, TRUE);
+ while (*info)
+ FreeAndClear(info++, TRUE);
}
/* Function Name: FreeAndClear - I couldn't resist the name.
* Returns: none.
*/
-void
-FreeAndClear(pointer, free_it)
-char ** pointer;
-Bool free_it;
+void FreeAndClear(char **pointer, Bool free_it)
{
- if (*pointer == NULL)
- return;
- else if (free_it)
- free(*pointer);
- *pointer = NULL;
+ if (!*pointer)
+ return;
+ else if (free_it)
+ free(*pointer);
+ *pointer = NULL;
}
/* Function Name: QueueTop
- * Description: returns a qelem pointer that points to the top of
+ * Description: returns a mqelem pointer that points to the top of
* a queue.
* Arguments: elem - any element of a queue.
* Returns: top element of a queue.
*/
-
-struct qelem *
-QueueTop(elem)
-struct qelem * elem;
+
+struct mqelem *QueueTop(struct mqelem *elem)
{
- if (elem == NULL) /* NULL returns NULL. */
- return(NULL);
- while (elem->q_back != NULL)
- elem = elem->q_back;
- return(elem);
+ if (!elem) /* NULL returns NULL. */
+ return NULL;
+ while (elem->q_back)
+ elem = elem->q_back;
+ return elem;
}
/* Function Name: FreeQueueElem
* Returns: none
*/
-static void
-FreeQueueElem(elem)
-struct qelem * elem;
+static void FreeQueueElem(struct mqelem *elem)
{
- char ** info = (char **) elem->q_data;
+ char **info = elem->q_data;
- if (info != (char **) NULL) {
- FreeInfo( info ); /* free info fields */
- free(elem->q_data); /* free info array itself. */
+ if (info)
+ {
+ FreeInfo(info); /* free info fields */
+ free(elem->q_data); /* free info array itself. */
}
- RemoveQueue(elem); /* remove this element from the queue */
- free(elem); /* free its space. */
+ RemoveQueue(elem); /* remove this element from the queue */
+ free(elem); /* free its space. */
}
/* Function Name: FreeQueue
* Returns: none.
*/
-void
-FreeQueue(elem)
-struct qelem * elem;
+void FreeQueue(struct mqelem *elem)
{
- struct qelem *temp, *local = QueueTop(elem);
+ struct mqelem *temp, *local = QueueTop(elem);
- while(local != NULL) {
- temp = local->q_forw;
- FreeQueueElem(local);
- local = temp;
+ while (local)
+ {
+ temp = local->q_forw;
+ FreeQueueElem(local);
+ local = temp;
}
}
* Returns: none.
*/
-int
-QueueCount(elem)
-struct qelem * elem;
+int QueueCount(struct mqelem *elem)
{
- int count = 0;
- elem = QueueTop(elem);
- while (elem != NULL) {
- count ++;
- elem = elem->q_forw;
+ int count = 0;
+ elem = QueueTop(elem);
+ while (elem)
+ {
+ count++;
+ elem = elem->q_forw;
}
- return(count);
+ return count;
}
/* Function Name: StoreInfo
- * Description: Stores information from an sms query into a queue.
+ * Description: Stores information from a moira query into a queue.
* Arguments: argc, argv, - information returned from the query returned
* in argv.
* data - the previous element on the queue, this data will be
- * stored in a qelem struct immediatly after this elem.
+ * stored in a mqelem struct immediatly after this elem.
* If NULL then a new queue will be created. This value
* is updated to the current element at the end off the
* call.
- * Returns: SMS_CONT, or SMS_ABORT if it has problems.
+ * Returns: MR_CONT, or MR_ABORT if it has problems.
*/
-int
-StoreInfo(argc, argv, data)
-int argc;
-char ** argv;
-char * data;
+int StoreInfo(int argc, char **argv, void *data)
{
- char ** info = (char **) malloc( MAX_ARGS_SIZE * sizeof(char *));
- struct qelem ** old_elem = (struct qelem **) data;
- struct qelem * new_elem = (struct qelem *) malloc (sizeof (struct qelem));
- int count;
+ char **info = malloc(MAX_ARGS_SIZE * sizeof(char *));
+ struct mqelem **old_elem = data;
+ struct mqelem *new_elem = malloc(sizeof(struct mqelem));
+ int count;
- if ( (new_elem == (struct qelem *) NULL) || (info == (char **) NULL) ) {
- Put_message("Could Not allocate more memory.");
- FreeQueue(*old_elem);
- *old_elem = (struct qelem *) NULL;
- return(SMS_ABORT);
+ if (!new_elem || !info)
+ {
+ Put_message("Could Not allocate more memory.");
+ FreeQueue(*old_elem);
+ *old_elem = NULL;
+ return MR_ABORT;
}
- for (count = 0; count < argc; count++)
- info[count] = Strsave(argv[count]);
- info[count] = NULL; /* NULL terminate this sucker. */
+ for (count = 0; count < argc; count++)
+ info[count] = strdup(argv[count]);
+ info[count] = NULL; /* NULL terminate this sucker. */
- new_elem->q_data = (char *) info;
- AddQueue(new_elem, *old_elem);
+ new_elem->q_data = info;
+ AddQueue(new_elem, *old_elem);
- *old_elem = new_elem;
- return(SMS_CONT);
+ *old_elem = new_elem;
+ return MR_CONT;
}
/* Function Name: CountArgs
* Returns: number if args in the list.
*/
-int
-CountArgs(info)
-char ** info;
+int CountArgs(char **info)
{
- int number = 0;
-
- while (*info != NULL) {
- number++;
- info++;
- }
-
- return(number);
-}
+ int number = 0;
-/* Function Name: Scream
- * Description: Bitch Loudly and exit, it is intended as a callback
- * function for queries that should never return a value.
- * Arguments: none
- * Returns: doesn't exit.
- */
+ while (*info)
+ {
+ number++;
+ info++;
+ }
-int
-Scream()
-{
- com_err(program_name, 0,
- "\nAn SMS update returned a value -- programmer botch\n");
- sms_disconnect();
- exit(1);
+ return number;
}
/* Function Name: PromptWithDefault
- * Description: allows a user to be prompted for input, and given a
+ * Description: allows a user to be prompted for input, and given a
* default choice.
* Arguments: prompt - the prompt string.
* buf, buflen - buffer to be returned and its MAX size?
* default value for the answer.
- * Returns: the value returned by prompt_input.
+ * Returns: zero on failure
*/
-int
-PromptWithDefault(prompt, buf, buflen, def)
-char *prompt, *buf;
-int buflen;
-char *def;
+int PromptWithDefault(char *prompt, char *buf, int buflen, char *def)
{
- char tmp[BUFSIZ];
- int ans;
+ char tmp[BUFSIZ];
+ int ans;
- (void) sprintf(tmp, "%s [%s]: ", prompt, def ? def : "");
- ans = Prompt_input(tmp, buf, buflen);
- if (IS_EMPTY(buf))
- (void) strcpy(buf, def);
- return(ans);
+ if (parsed_argc > 0)
+ {
+ parsed_argc--;
+ strncpy(buf, parsed_argv[0], buflen);
+ sprintf(tmp, "%s: %s", prompt, buf);
+ Put_message(tmp);
+ parsed_argv++;
+ return 1;
+ }
+
+ sprintf(tmp, "%s [%s]: ", prompt, def ? def : "");
+ ans = Prompt_input(tmp, buf, buflen);
+ if (IS_EMPTY(buf))
+ strcpy(buf, def);
+ else if (!strcmp(buf, "\"\""))
+ *buf = 0;
+ return ans;
}
/* Function Name: YesNoQuestion
* Returns: TRUE or FALSE or -1 on error
*/
-Bool
-YesNoQuestion(prompt, bool_def)
-char *prompt;
-int bool_def;
+Bool YesNoQuestion(char *prompt, int bool_def)
{
- char ans;
+ char ans[2];
- while (TRUE) {
- if (!PromptWithDefault(prompt, &ans, 1, bool_def ? "y" : "n"))
- return(-1);
- switch (ans) {
+ while (TRUE)
+ {
+ if (!PromptWithDefault(prompt, ans, 2, bool_def ? "y" : "n"))
+ return -1;
+ switch (ans[0])
+ {
case 'n':
case 'N':
- return(FALSE);
+ return FALSE;
case 'y':
case 'Y':
- return(TRUE);
+ return TRUE;
default:
- Put_message("Please answer 'y' or 'n'.");
- break;
+ Put_message("Please answer 'y' or 'n'.");
+ break;
}
}
}
+
/* Function Name: YesNoQuitQuestion
* Description: This prompts the user for the answer to a yes-no or
* true-false question, with a quit option.
* seem to need this functionality.
*/
-Bool
-YesNoQuitQuestion(prompt, bool_def)
-char *prompt;
-int bool_def;
+Bool YesNoQuitQuestion(char *prompt, int bool_def)
{
- char ans;
+ char ans[2];
- while (TRUE) {
- if (!PromptWithDefault(prompt, &ans, 1, bool_def ? "y" : "n"))
- return(-1);
- switch (ans) {
+ while (TRUE)
+ {
+ if (!PromptWithDefault(prompt, ans, 2, bool_def ? "y" : "n"))
+ return -1;
+ switch (ans[0])
+ {
case 'n':
case 'N':
- return(FALSE);
+ return FALSE;
case 'y':
case 'Y':
- return(TRUE);
+ return TRUE;
case 'q':
case 'Q':
- return(-1);
+ return -1;
default:
- Put_message("Please answer 'y', 'n' or 'q'.");
- break;
+ Put_message("Please answer 'y', 'n' or 'q'.");
+ break;
}
}
}
* Returns: TRUE/FALSE - wether or not the confirmation occured.
*/
-Bool
-Confirm(prompt)
-char * prompt;
+Bool Confirm(char *prompt)
{
- return( !verbose || (YesNoQuestion(prompt,FALSE) == TRUE) );
+ return !verbose || (YesNoQuestion(prompt, FALSE) == TRUE);
}
/* Function Name: ValidName
* Returns: TRUE if Valid.
*/
-Bool
-ValidName(s)
-char *s;
+Bool ValidName(char *s)
{
- if (IS_EMPTY(s))
- Put_message("Please use a non-empty name.");
- else if (index(s, ' '))
- Put_message("You cannot use space (' ') in this name.");
- else if (index(s, WILDCARD))
- Put_message("Wildcards not accepted here.");
- else
- return TRUE;
- return FALSE;
+ if (IS_EMPTY(s))
+ Put_message("Please use a non-empty name.");
+ else if (strchr(s, ' '))
+ Put_message("You cannot use space (' ') in this name.");
+ else if (strchr(s, '*') || strchr(s, '?'))
+ Put_message("Wildcards not accepted here.");
+ else
+ return TRUE;
+ return FALSE;
}
/* Function Name: ToggleVerboseMode
* Returns: DM_NORMAL.
*/
-int
-ToggleVerboseMode()
+int ToggleVerboseMode(int argc, char **argv)
{
-
verbose = !verbose;
if (verbose)
Put_message("Delete functions will first confirm\n");
else
Put_message("Delete functions will be silent\n");
-
- return(DM_NORMAL);
-}
-/* Function Name: NullFunc
- * Description: dummy callback routine
- * Arguments: none
- * Returns: SMS_CONT
- */
-
-int
-NullFunc()
-{
- return(SMS_CONT);
+ return DM_NORMAL;
}
/* Function Name: SlipInNewName
* big way.
*/
-void
-SlipInNewName(info, name)
-char ** info;
-char * name;
+void SlipInNewName(char **info, char *name)
{
- register int i;
+ int i;
- /* This also pushes the NULL down. */
- for (i = CountArgs(info); i > 1; i--) {
- info[i] = info[i-1];
- }
- info[1] = name; /* now slip in the name. */
+ /* This also pushes the NULL down. */
+ for (i = CountArgs(info); i > 0; i--)
+ info[i + 1] = info[i];
+ info[1] = name; /* now slip in the name. */
}
/* Function Name: GetValueFromUser
* Returns: SUB_ERROR if break hit (^C).
*/
-int
-GetValueFromUser(prompt, pointer)
-char * prompt, ** pointer;
+int GetValueFromUser(char *prompt, char **pointer)
{
- char buf[BUFSIZ];
+ char buf[BUFSIZ];
- if (PromptWithDefault(prompt, buf, BUFSIZ, *pointer) == -1)
- return(SUB_ERROR);
+ if (PromptWithDefault(prompt, buf, BUFSIZ, *pointer) == 0)
+ return SUB_ERROR;
-/*
- * If these are the same then there is no need to allocate a new string.
- *
- * a difference that makes no difference, IS no difference.
- */
+ /*
+ * If these are the same then there is no need to allocate a new string.
+ *
+ * a difference that makes no difference, IS no difference.
+ */
- if (strcmp(buf, *pointer) != 0) {
- if (*pointer != NULL)
- free(*pointer);
- *pointer = Strsave(buf);
+ if (*pointer)
+ {
+ if (strcmp(buf, *pointer))
+ {
+ free(*pointer);
+ *pointer = strdup(buf);
+ }
}
- return(SUB_NORMAL);
+ return SUB_NORMAL;
}
/* Function Name: GetYesNoValueFromUser
* Returns: SUB_ERROR if break hit (^C).
*/
-int
-GetYesNoValueFromUser(prompt, pointer)
-char * prompt, ** pointer;
+int GetYesNoValueFromUser(char *prompt, char **pointer)
{
- char user_prompt[BUFSIZ];
- Bool default_val;
+ char user_prompt[BUFSIZ];
+ Bool default_val;
- if ( strcmp (*pointer, DEFAULT_YES) == 0 )
- default_val = TRUE;
- else
- default_val = FALSE;
-
- sprintf(user_prompt, "%s (y/n)", prompt);
+ if (!strcmp(*pointer, DEFAULT_YES))
+ default_val = TRUE;
+ else
+ default_val = FALSE;
+
+ sprintf(user_prompt, "%s (y/n)", prompt);
- switch (YesNoQuestion(user_prompt, default_val)) {
+ switch (YesNoQuestion(user_prompt, default_val))
+ {
case TRUE:
- if (*pointer != NULL)
- free(*pointer);
- *pointer = Strsave(DEFAULT_YES);
- break;
+ if (*pointer)
+ free(*pointer);
+ *pointer = strdup(DEFAULT_YES);
+ break;
case FALSE:
- if (*pointer != NULL)
- free(*pointer);
- *pointer = Strsave(DEFAULT_NO);
- break;
+ if (*pointer)
+ free(*pointer);
+ *pointer = strdup(DEFAULT_NO);
+ break;
case -1:
default:
- return(SUB_ERROR);
+ return SUB_ERROR;
}
- return(SUB_NORMAL);
+ return SUB_NORMAL;
}
/* Function Name: GetFSVal
* Returns: TRUE if successful.
*/
-static Bool
-GetFSVal(name, mask, current, new)
-char * name;
-int mask, current, *new;
+static Bool GetFSVal(char *name, int mask, int current, int *new)
{
- char temp_buf[BUFSIZ];
- sprintf(temp_buf, "Is this a %s filsystem", name);
- switch (YesNoQuestion(temp_buf, ( (mask & current) == mask) )) {
+ char temp_buf[BUFSIZ];
+ sprintf(temp_buf, "Is this a %s filsystem", name);
+ switch (YesNoQuestion(temp_buf, ((mask & current) == mask)))
+ {
case TRUE:
- *new |= mask;
- break;
+ *new |= mask;
+ break;
case FALSE:
- break; /* zero by default. */
+ break; /* zero by default. */
default:
- return(FALSE);
+ return FALSE;
}
- return(TRUE);
+ return TRUE;
}
/* Function Name: GetFSTypes
* Returns: SUB_ERROR on ^C.
*/
-int
-GetFSTypes(current)
-char ** current;
-{
- int c_value, new_val = 0; /* current value of filesys type (int). */
- char ret_value[BUFSIZ];
-
- if (*current == NULL)
- c_value = 0;
- else
- c_value = atoi(*current);
-
- if (GetFSVal("student", SMS_FS_STUDENT, c_value, &new_val) == FALSE)
- return(SUB_ERROR);
- if (GetFSVal("faculty", SMS_FS_FACULTY, c_value, &new_val) == FALSE)
- return(SUB_ERROR);
- if (GetFSVal("staff", SMS_FS_STAFF, c_value, &new_val) == FALSE)
- return(SUB_ERROR);
- if (GetFSVal("miscellaneous", SMS_FS_MISC, c_value, &new_val) == FALSE)
- return(SUB_ERROR);
-
- FreeAndClear(current, TRUE);
- sprintf(ret_value, "%d", new_val);
- *current = Strsave(ret_value);
- return(SUB_NORMAL);
-}
-
-/* Function Name: CanonicalizeHostname
- * Description: This function takes a machine name and canonicalize's it.
- * Arguments: machine - name of the machine to work on.
- * Returns: new name or NULL if nameserver returns error
- */
-
-char *
-CanonicalizeHostname(machine)
-char *machine;
+int GetFSTypes(char **current, int options)
{
- struct hostent *hostinfo;
-
- hostinfo = gethostbyname(machine);
-/* If this fails then we just return what we were passed. */
- if (hostinfo != (struct hostent *) NULL)
- machine = hostinfo->h_name;
- return (machine);
-}
+ int c_value, new_val = 0; /* current value of filesys type (int). */
+ char ret_value[BUFSIZ];
-/* Function Name: Strsave
- * Description: save a string.
- * Arguments: string - the string to save.
- * Returns: The malloced string, now safely saved, or NULL.
- */
-
-char *
-Strsave(str)
-char *str;
-{
- register char *newstr = malloc((unsigned) strlen(str) + 1);
+ if (!*current)
+ c_value = 0;
+ else
+ c_value = atoi(*current);
+
+ if (GetFSVal("student", MR_FS_STUDENT, c_value, &new_val) == FALSE)
+ return SUB_ERROR;
+ if (GetFSVal("faculty", MR_FS_FACULTY, c_value, &new_val) == FALSE)
+ return SUB_ERROR;
+ if (GetFSVal("staff", MR_FS_STAFF, c_value, &new_val) == FALSE)
+ return SUB_ERROR;
+ if (GetFSVal("miscellaneous", MR_FS_MISC, c_value, &new_val) == FALSE)
+ return SUB_ERROR;
+ if (options)
+ {
+ if (GetFSVal("Group Quotas", MR_FS_GROUPQUOTA, c_value, &new_val) ==
+ FALSE)
+ return SUB_ERROR;
+ }
- if (newstr == (char *) NULL)
- return ((char *) NULL);
- else
- return (strcpy(newstr, str));
+ FreeAndClear(current, TRUE);
+ sprintf(ret_value, "%d", new_val);
+ *current = strdup(ret_value);
+ return SUB_NORMAL;
}
-/* Function Name: EncryptMITID
- * Description: Encrypts an mit ID number.
- * Arguments: sbuf - the buffer to return the encrypted number in.
- * idnumber - the id number (string).
- * first, last - name of the person.
- * Returns: none.
- */
+/* atot: convert ASCII integer unix time into human readable date string */
-void
-EncryptMITID(sbuf, idnumber, first, last)
-char *sbuf, *idnumber, *first, *last;
+char *atot(char *itime)
{
- char salt[3];
- extern char *crypt();
+ time_t time;
+ char *ct;
-#define _tolower(c) ((c)|0x60)
-
- salt[0] = _tolower(last[0]);
- salt[1] = _tolower(first[0]);
- salt[2] = 0;
-
- (void) strcpy(sbuf, crypt(&idnumber[2], salt));
+ time = (time_t) atoi(itime);
+ ct = ctime(&time);
+ ct[24] = 0;
+ return &ct[4];
}
-/* Function Name: RemoveHyphens
- * Description: Removes all hyphens from the string passed to it.
- * Arguments: str - the string to remove the hyphes from
- * Returns: none
- */
-
-void
-RemoveHyphens(str)
-char *str;
-{
- char *hyphen;
-
- while ((hyphen = index(str, '-')) != NULL)
- (void) strcpy(hyphen, hyphen + 1);
-}
/* Function Name: Print
* Description: prints out all the arguments on a single line.
- * Arguments: argc, argv - the standard SMS arguments.
+ * Arguments: argc, argv - the standard Moira arguments.
* callback - the callback function - NOT USED.
- * Returns: SMS_CONT
+ * Returns: MR_CONT
*/
-/* ARGSUSED */
-int
-Print(argc, argv, callback)
-int argc;
-char **argv, *callback;
+int Print(int argc, char **argv, void *callback)
{
- char buf[BUFSIZ];
- register int i;
+ char buf[BUFSIZ];
+ int i;
- found_some = TRUE;
- (void) strcpy(buf,argv[0]); /* no newline 'cause Put_message adds one */
- for (i = 1; i < argc; i++)
- (void) sprintf(buf,"%s %s",buf,argv[i]);
- (void) Put_message(buf);
+ found_some = TRUE;
+ strcpy(buf, argv[0]); /* no newline 'cause Put_message adds one */
+ for (i = 1; i < argc; i++)
+ sprintf(buf, "%s %s", buf, argv[i]);
+ Put_message(buf);
- return (SMS_CONT);
+ return MR_CONT;
}
/* Function Name: PrintByType
* Description: This function prints all members of the type specified
* by the callback arg, unless the callback is NULL, in which
* case it prints all members.
- * Arguments: argc, argc - normal arguments for sms_callback function.
+ * Arguments: argc, argc - normal arguments for mr_callback function.
* callback - either a type of member or NULL.
- * Returns: SMS_CONT or SMS_QUIT.
+ * Returns: MR_CONT or MR_QUIT.
*/
-/*ARGSUSED*/
-int
-PrintByType(argc, argv, callback)
-int argc;
-char **argv, *callback;
+int PrintByType(int argc, char **argv, void *callback)
{
- if (callback == NULL)
- return( Print(argc, argv, callback) );
- if (strcmp(argv[0], callback) == 0)
- return( Print(argc, argv, callback) );
- return(SMS_CONT);
+ if (!callback)
+ return Print(argc, argv, callback);
+ if (!strcmp(argv[0], callback))
+ return Print(argc, argv, callback);
+ return MR_CONT;
}
/* Function Name: PrintHelp
* Returns: DM_NORMAL.
*/
-int
-PrintHelp(message)
-char ** message;
+int PrintHelp(char **message)
{
- Print(CountArgs(message), message, (char *) NULL);
- return(DM_NORMAL);
+ int i;
+
+ for (i = 0; i < CountArgs(message); i++)
+ Put_message(message[i]);
+
+ return DM_NORMAL;
}
/* Function Name: Loop
* Returns: none.
*/
-void
-Loop(elem, func)
-FVoid func;
-struct qelem * elem;
+void Loop(struct mqelem *elem, void (*func)(char **))
{
- while (elem != NULL) {
- char ** info = (char **) elem->q_data;
- (*func) (info);
- elem = elem->q_forw;
+ while (elem)
+ {
+ (*func) (elem->q_data);
+ elem = elem->q_forw;
}
}
* "Delete the list"
*/
-void
-QueryLoop(elem, print_func, op_func, query_string)
-struct qelem *elem;
-FVoid op_func;
-FCharStar print_func;
-char * query_string;
-{
- Bool one_item;
- char temp_buf[BUFSIZ], *name;
-
- one_item = (QueueCount(elem) == 1);
- while (elem != NULL) {
- char **info = (char **) elem->q_data;
-
- if (one_item)
- (*op_func) (info, one_item);
- else {
- name = (*print_func) (info); /* call print function. */
- sprintf(temp_buf,"%s %s (y/n/q)", query_string, name);
- switch(YesNoQuitQuestion(temp_buf, FALSE)) {
+void QueryLoop(struct mqelem *elem, char * (*print_func)(char **),
+ void (*op_func)(char **, Bool), char *query_string)
+{
+ Bool one_item;
+ char temp_buf[BUFSIZ], *name;
+
+ elem = QueueTop(elem);
+ one_item = (QueueCount(elem) == 1);
+ while (elem)
+ {
+ char **info = elem->q_data;
+
+ if (one_item)
+ (*op_func) (info, one_item);
+ else
+ {
+ name = (*print_func) (info); /* call print function. */
+ sprintf(temp_buf, "%s %s (y/n/q)", query_string, name);
+ switch (YesNoQuitQuestion(temp_buf, FALSE))
+ {
case TRUE:
- (*op_func) (info, one_item);
- break;
+ (*op_func) (info, one_item);
+ break;
case FALSE:
- break;
+ break;
default: /* Quit. */
- Put_message("Aborting...");
- return;
+ Put_message("Aborting...");
+ return;
}
}
- elem = elem->q_forw;
+ elem = elem->q_forw;
}
}
* Returns: none.
*/
-char *
-NullPrint(info)
-char ** info;
+char *NullPrint(char **info)
+{
+ return info[NAME];
+}
+
+
+/* Function Name: GetTypeValues
+ * Description: gets legal values for a typed object, keeping a cache
+ * Arguments: type name
+ * Returns: argv of values
+ */
+
+struct mqelem *GetTypeValues(char *tname)
+{
+ int stat;
+ char *argv[3], *p, **pp;
+ struct mqelem *elem, *oelem;
+ static struct mqelem *cache = NULL;
+ struct cache_elem {
+ char *cache_name;
+ struct mqelem *cache_data;
+ } *ce;
+
+ for (elem = cache; elem; elem = elem->q_forw)
+ {
+ ce = elem->q_data;
+ if (!strcmp(ce->cache_name, tname))
+ return ce->cache_data;
+ }
+
+ argv[0] = tname;
+ argv[1] = "TYPE";
+ argv[2] = "*";
+ elem = NULL;
+ if ((stat = do_mr_query("get_alias", 3, argv, StoreInfo, &elem)))
+ {
+ com_err(program_name, stat, " in GetTypeValues");
+ return NULL;
+ }
+ oelem = elem;
+ for (elem = QueueTop(elem); elem; elem = elem->q_forw)
+ {
+ pp = elem->q_data;
+ p = strdup(pp[2]);
+ FreeInfo(pp);
+ elem->q_data = p;
+ }
+ elem = malloc(sizeof(struct mqelem));
+ ce = malloc(sizeof(struct cache_elem));
+ ce->cache_name = strdup(tname);
+ ce->cache_data = QueueTop(oelem);
+ elem->q_data = ce;
+ AddQueue(elem, cache);
+ cache = QueueTop(elem);
+ return ce->cache_data;
+}
+
+
+/* Function Name: GetTypeFromUser
+ * Description: gets a typed value from the user
+ * Arguments: prompt string, type name, buffer pointer
+ * Returns: SUB_ERROR if ^C, SUB_NORMAL otherwise
+ */
+
+int GetTypeFromUser(char *prompt, char *tname, char **pointer)
{
- return(info[NAME]);
+ char def[BUFSIZ], buffer[BUFSIZ], *p, *argv[3];
+ struct mqelem *elem;
+ int stat;
+
+ strcpy(def, *pointer);
+ strcpy(buffer, prompt);
+ strcat(buffer, " (");
+ for (elem = GetTypeValues(tname); elem; elem = elem->q_forw)
+ {
+ strcat(buffer, elem->q_data);
+ if (elem->q_forw)
+ strcat(buffer, ", ");
+ }
+ strcat(buffer, ")");
+ if (strlen(buffer) > 64)
+ sprintf(buffer, "%s (? for help)", prompt);
+ if (GetValueFromUser(buffer, pointer) == SUB_ERROR)
+ return SUB_ERROR;
+ if (**pointer == '?')
+ {
+ sprintf(buffer, "Type %s is one of:", tname);
+ Put_message(buffer);
+ for (elem = GetTypeValues(tname); elem; elem = elem->q_forw)
+ Put_message(elem->q_data);
+ *pointer = strdup(def);
+ return GetTypeFromUser(prompt, tname, pointer);
+ }
+ for (elem = GetTypeValues(tname); elem; elem = elem->q_forw)
+ {
+ if (!strcasecmp(elem->q_data, *pointer))
+ {
+ strcpy(*pointer, elem->q_data);
+ return SUB_NORMAL;
+ }
+ }
+ sprintf(buffer, "\"%s\" is not a legal value for %s. Use one of:",
+ *pointer, tname);
+ Put_message(buffer);
+ for (elem = GetTypeValues(tname); elem; elem = elem->q_forw)
+ Put_message(elem->q_data);
+ sprintf(buffer, "Are you sure you want \"%s\" to be a legal %s",
+ *pointer, tname);
+ if (YesNoQuestion("Do you want this to be a new legal value", 0) == TRUE &&
+ YesNoQuestion(buffer, 0) == TRUE)
+ {
+ argv[0] = tname;
+ argv[1] = "TYPE";
+ argv[2] = *pointer;
+ /* don't uppercase access flags. Do uppercase everything else */
+ if (strncmp(tname, "fs_access", 9))
+ {
+ for (p = argv[2]; *p; p++)
+ {
+ if (islower(*p))
+ *p = toupper(*p);
+ }
+ }
+ if ((stat = do_mr_query("add_alias", 3, argv, NULL, NULL)))
+ com_err(program_name, stat, " in add_alias");
+ else
+ {
+ elem = malloc(sizeof(struct mqelem));
+ elem->q_data = strdup(*pointer);
+ AddQueue(elem, GetTypeValues(tname));
+ Put_message("Done.");
+ }
+ }
+ *pointer = strdup(def);
+ return GetTypeFromUser(prompt, tname, pointer);
}
-/*
- * Local Variables:
- * mode: c
- * c-indent-level: 4
- * c-continued-statement-offset: 4
- * c-brace-offset: -4
- * c-argdecl-indent: 4
- * c-label-offset: -4
- * End:
+
+/* Function Name: GetAddressFromUser
+ * Description: gets an IP address from the user
+ * Arguments: prompt string, buffer pointer
+ * buffer contains default value as long int
+ * Returns: SUB_ERROR if ^C, SUB_NORMAL otherwise
*/
+
+int GetAddressFromUser(char *prompt, char **pointer)
+{
+ char *value, buf[256];
+ struct in_addr addr;
+ int ret;
+
+ addr.s_addr = htonl(atoi(*pointer));
+ value = strdup(inet_ntoa(addr));
+ ret = GetValueFromUser(prompt, &value);
+ if (ret == SUB_ERROR)
+ return SUB_ERROR;
+ addr.s_addr = inet_addr(value);
+ free(pointer);
+ sprintf(buf, "%ld", ntohl(addr.s_addr));
+ *pointer = strdup(buf);
+ return SUB_NORMAL;
+}
+
+
+int do_mr_query(char *name, int argc, char **argv,
+ int (*proc)(int, char **, void *), void *hint)
+{
+ int status;
+ extern char *whoami, *moira_server;
+
+ refresh_screen();
+ status = mr_query(name, argc, argv, proc, hint);
+ if (status != MR_ABORTED && status != MR_NOT_CONNECTED)
+ return status;
+ status = mr_connect(moira_server);
+ if (status)
+ {
+ com_err(whoami, status, " while re-connecting to server %s",
+ moira_server);
+ return MR_ABORTED;
+ }
+ status = mr_auth(whoami);
+ if (status)
+ {
+ com_err(whoami, status, " while re-authenticating to server %s",
+ moira_server);
+ mr_disconnect();
+ return MR_ABORTED;
+ }
+ status = mr_query(name, argc, argv, proc, hint);
+ return status;
+}
+