1 #if (!defined(lint) && !defined(SABER))
2 static char rcsid_module_c[] = "$Header$";
5 /* This is the file utils.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: Many useful utility functions.
10 * By: Chris D. Peterson
16 * Copyright 1988 by the Massachusetts Institute of Technology.
18 * For further information on copyright and distribution
19 * see the file mit-copyright.h
25 #include <moira_site.h>
29 #include "mit-copyright.h"
34 #include <netdb.h> /* for gethostbyname. */
36 /* Function Name: AddQueue
37 * Description: Adds an element to a queue
38 * Arguments: elem, pred - element and its predecessor.
44 struct qelem * elem, *pred;
52 elem->q_forw = pred->q_forw;
56 /* Function Name: RemoveQueue
57 * Description: removes an element from a queue.
66 if (elem->q_forw != NULL)
67 (elem->q_forw)->q_back = elem->q_back;
68 if (elem->q_back != NULL)
69 (elem->q_back)->q_forw = elem->q_forw;
73 /* CopyInfo: allocates memory for a copy of a NULL terminated array of
74 * strings <and returns a pointer to the copy.
83 ret = (char **) malloc(sizeof(char *) * (CountArgs(info) + 1));
86 for (i = 0; info[i]; i++)
87 ret[i] = Strsave(info[i]);
93 /* Function Name: FreeInfo
94 * Description: Frees all elements of a NULL terminated arrary of char*'s
95 * Arguments: info - array who's elements we are to free.
103 while (*info != NULL)
104 FreeAndClear(info++, TRUE);
107 /* Function Name: FreeAndClear - I couldn't resist the name.
108 * Description: Clears pointer and optionially frees it.
109 * Arguments: pointer - pointer to work with.
110 * free_it - if TRUE then free pointer.
115 FreeAndClear(pointer, free_it)
119 if (*pointer == NULL)
126 /* Function Name: QueueTop
127 * Description: returns a qelem pointer that points to the top of
129 * Arguments: elem - any element of a queue.
130 * Returns: top element of a queue.
137 if (elem == NULL) /* NULL returns NULL. */
139 while (elem->q_back != NULL)
144 /* Function Name: FreeQueueElem
145 * Description: Frees one element of the queue.
146 * Arguments: elem - the elem to free.
154 char ** info = (char **) elem->q_data;
156 if (info != (char **) NULL) {
157 FreeInfo( info ); /* free info fields */
158 free(elem->q_data); /* free info array itself. */
160 RemoveQueue(elem); /* remove this element from the queue */
161 free(elem); /* free its space. */
164 /* Function Name: FreeQueue
165 * Description: Cleans up the queue
166 * Arguments: elem - any element of the queue.
174 struct qelem *temp, *local = QueueTop(elem);
176 while(local != NULL) {
177 temp = local->q_forw;
178 FreeQueueElem(local);
183 /* Function Name: QueueCount
184 * Description: Counts the number of elements in a queue
185 * Arguments: elem - any element in the queue.
194 elem = QueueTop(elem);
195 while (elem != NULL) {
202 /* Function Name: StoreInfo
203 * Description: Stores information from an moira query into a queue.
204 * Arguments: argc, argv, - information returned from the query returned
206 * data - the previous element on the queue, this data will be
207 * stored in a qelem struct immediatly after this elem.
208 * If NULL then a new queue will be created. This value
209 * is updated to the current element at the end off the
211 * Returns: MR_CONT, or MR_ABORT if it has problems.
215 StoreInfo(argc, argv, data)
220 char ** info = (char **) malloc( MAX_ARGS_SIZE * sizeof(char *));
221 struct qelem ** old_elem = (struct qelem **) data;
222 struct qelem * new_elem = (struct qelem *) malloc (sizeof (struct qelem));
225 if ( (new_elem == (struct qelem *) NULL) || (info == (char **) NULL) ) {
226 Put_message("Could Not allocate more memory.");
227 FreeQueue(*old_elem);
228 *old_elem = (struct qelem *) NULL;
232 for (count = 0; count < argc; count++)
233 info[count] = Strsave(argv[count]);
234 info[count] = NULL; /* NULL terminate this sucker. */
236 new_elem->q_data = (char *) info;
237 AddQueue(new_elem, *old_elem);
239 *old_elem = new_elem;
243 /* Function Name: CountArgs
244 * Description: Retrieve the number of args in a null terminated
246 * Arguments: info - the argument list.
247 * Returns: number if args in the list.
256 while (*info != NULL) {
264 /* Function Name: Scream
265 * Description: Bitch Loudly and exit, it is intended as a callback
266 * function for queries that should never return a value.
268 * Returns: doesn't exit.
274 com_err(program_name, 0,
275 "\nA Moira update returned a value -- programmer botch\n");
280 /* Function Name: PromptWithDefault
281 * Description: allows a user to be prompted for input, and given a
283 * Arguments: prompt - the prompt string.
284 * buf, buflen - buffer to be returned and its MAX size?
285 * default value for the answer.
286 * Returns: zero on failure
290 PromptWithDefault(prompt, buf, buflen, def)
298 if (parsed_argc > 0) {
300 strncpy(buf, parsed_argv[0], buflen);
301 sprintf(tmp, "%s: %s", prompt, buf);
307 (void) sprintf(tmp, "%s [%s]: ", prompt, def ? def : "");
308 ans = Prompt_input(tmp, buf, buflen);
310 (void) strcpy(buf, def);
311 else if (!strcmp(buf, "\"\""))
316 /* Function Name: YesNoQuestion
317 * Description: This prompts the user for the answer to a yes-no or
318 * true-false question.
319 * Arguments: prompt - the prompt for the user.
320 * bool_def - the default value either TRUE or FALSE.
321 * Returns: TRUE or FALSE or -1 on error
325 YesNoQuestion(prompt, bool_def)
332 if (!PromptWithDefault(prompt, ans, 2, bool_def ? "y" : "n"))
342 Put_message("Please answer 'y' or 'n'.");
347 /* Function Name: YesNoQuitQuestion
348 * Description: This prompts the user for the answer to a yes-no or
349 * true-false question, with a quit option.
350 * Arguments: prompt - the prompt for the user.
351 * bool_def - the default value either TRUE or FALSE.
352 * Returns: TRUE or FALSE or -1 on error or QUIT
353 * NOTE: It is not possible to have quit the default, but then I don't
354 * seem to need this functionality.
358 YesNoQuitQuestion(prompt, bool_def)
365 if (!PromptWithDefault(prompt, ans, 2, bool_def ? "y" : "n"))
378 Put_message("Please answer 'y', 'n' or 'q'.");
384 /* Function Name: Confirm
385 * Description: This function asks the user to confirm the action
386 * he is about to take, used for things like deleting.
387 * Arguments: prompt - the prompt string.
388 * Returns: TRUE/FALSE - wether or not the confirmation occured.
395 return( !verbose || (YesNoQuestion(prompt,FALSE) == TRUE) );
398 /* Function Name: ValidName
399 * Description: This function checks to see if we have a valid list name.
400 * Arguments: s - the list name.
401 * Returns: TRUE if Valid.
409 Put_message("Please use a non-empty name.");
410 else if (index(s, ' '))
411 Put_message("You cannot use space (' ') in this name.");
412 else if (index(s, '*') || index(s, '?') || index(s, '['))
413 Put_message("Wildcards not accepted here.");
419 /* Function Name: ToggleVerboseMode
420 * Description: This function toggles the verbose mode.
422 * Returns: DM_NORMAL.
432 Put_message("Delete functions will first confirm\n");
434 Put_message("Delete functions will be silent\n");
439 /* Function Name: NullFunc
440 * Description: dummy callback routine
451 /* Function Name: SlipInNewName
452 * Description: Slips the new name into the number 2 slot of a list, and
453 * returns a pointer to the new list.
454 * Arguments: info - list that needs this name slipped into it.
455 * name - the name to slip into the list.
456 * Returns: a pointer to the new list.
457 * NOTE: This screws up the numbers of the elements of the array in a
462 SlipInNewName(info, name)
468 /* This also pushes the NULL down. */
469 for (i = CountArgs(info); i > 0; i--) {
472 info[1] = name; /* now slip in the name. */
475 /* Function Name: GetValueFromUser
476 * Description: This function gets a value from a user for the field
478 * Arguments: prompt - prompt for user.
479 * pointer - pointer to default value, will be returned
481 * Returns: SUB_ERROR if break hit (^C).
485 GetValueFromUser(prompt, pointer)
486 char * prompt, ** pointer;
490 if (PromptWithDefault(prompt, buf, BUFSIZ, *pointer) == 0)
494 * If these are the same then there is no need to allocate a new string.
496 * a difference that makes no difference, IS no difference.
499 if (strcmp(buf, *pointer) != 0) {
500 if (*pointer != NULL)
502 *pointer = Strsave(buf);
507 /* Function Name: GetYesNoValueFromUser
508 * Description: This function gets a value from a user for the field
510 * Arguments: prompt - prompt for user.
511 * pointer - pointer to default value, will be returned
513 * Returns: SUB_ERROR if break hit (^C).
517 GetYesNoValueFromUser(prompt, pointer)
518 char * prompt, ** pointer;
520 char user_prompt[BUFSIZ];
523 if ( strcmp (*pointer, DEFAULT_YES) == 0 )
528 sprintf(user_prompt, "%s (y/n)", prompt);
530 switch (YesNoQuestion(user_prompt, default_val)) {
532 if (*pointer != NULL)
534 *pointer = Strsave(DEFAULT_YES);
537 if (*pointer != NULL)
539 *pointer = Strsave(DEFAULT_NO);
548 /* Function Name: GetFSVal
549 * Description: asks about a specific filesystem value.
550 * Arguments: name - string for this type of filesystem.
551 * mask - mask for this type of filesystem.
552 * current - current filesystem state. (for defaults).
553 * new - new filesystem state.
554 * Returns: TRUE if successful.
558 GetFSVal(name, mask, current, new)
560 int mask, current, *new;
562 char temp_buf[BUFSIZ];
563 sprintf(temp_buf, "Is this a %s filsystem", name);
564 switch (YesNoQuestion(temp_buf, ( (mask & current) == mask) )) {
569 break; /* zero by default. */
576 /* Function Name: GetFSTypes
577 * Description: Allows user to specify filsystem types.
578 * Arguments: current - current value of filsystem, freed here.
579 * Returns: SUB_ERROR on ^C.
583 GetFSTypes(current, options)
587 int c_value, new_val = 0; /* current value of filesys type (int). */
588 char ret_value[BUFSIZ];
590 if (*current == NULL)
593 c_value = atoi(*current);
595 if (GetFSVal("student", MR_FS_STUDENT, c_value, &new_val) == FALSE)
597 if (GetFSVal("faculty", MR_FS_FACULTY, c_value, &new_val) == FALSE)
599 if (GetFSVal("staff", MR_FS_STAFF, c_value, &new_val) == FALSE)
601 if (GetFSVal("miscellaneous", MR_FS_MISC, c_value, &new_val) == FALSE)
604 if (GetFSVal("Group Quotas", MR_FS_GROUPQUOTA, c_value, &new_val) ==
609 FreeAndClear(current, TRUE);
610 sprintf(ret_value, "%d", new_val);
611 *current = Strsave(ret_value);
615 /* Function Name: Strsave
616 * Description: save a string.
617 * Arguments: string - the string to save.
618 * Returns: The malloced string, now safely saved, or NULL.
625 register char *newstr = malloc((unsigned) strlen(str) + 1);
627 if (newstr == (char *) NULL)
628 return ((char *) NULL);
630 return (strcpy(newstr, str));
633 /* Function Name: Print
634 * Description: prints out all the arguments on a single line.
635 * Arguments: argc, argv - the standard MR arguments.
636 * callback - the callback function - NOT USED.
642 Print(argc, argv, callback)
644 char **argv, *callback;
650 (void) strcpy(buf,argv[0]); /* no newline 'cause Put_message adds one */
651 for (i = 1; i < argc; i++)
652 (void) sprintf(buf,"%s %s",buf,argv[i]);
653 (void) Put_message(buf);
658 /* Function Name: PrintByType
659 * Description: This function prints all members of the type specified
660 * by the callback arg, unless the callback is NULL, in which
661 * case it prints all members.
662 * Arguments: argc, argc - normal arguments for mr_callback function.
663 * callback - either a type of member or NULL.
664 * Returns: MR_CONT or MR_QUIT.
669 PrintByType(argc, argv, callback)
671 char **argv, *callback;
673 if (callback == NULL)
674 return( Print(argc, argv, callback) );
675 if (strcmp(argv[0], callback) == 0)
676 return( Print(argc, argv, callback) );
680 /* Function Name: PrintHelp
681 * Description: Prints Help Information in a NULL terminated
683 * Arguments: message.
684 * Returns: DM_NORMAL.
693 for (i = 0; i < CountArgs(message); i++)
694 Put_message(message[i]);
699 /* Function Name: Loop
700 * Description: This function goes through the entire queue, and
701 * and executes the given function on each element.
702 * Arguments: elem - top element of the queue.
703 * func - the function to execute.
712 while (elem != NULL) {
713 char ** info = (char **) elem->q_data;
720 /* Function Name: QueryLoop
721 * Description: This functions loops through a queue containing
722 * information about some item that we want to perform
723 * an operation on, and then calls the correct routine
724 * perform that operation.
725 * Arguments: top - top of the queue of information.
726 * print_func - print function.
727 * op_function - operation to be performed.
728 * query_string - string the prompts the user whether or not
729 * to perform this operation.
732 * print_opt - should expect one arguent, the info array
734 * is expected to return the name of the item.
735 * op_func - should expect two arguments.
736 * 1) the info array of char *'s.
737 * 2) a boolean the is true if there only
738 * one item in this queue, used for delete
740 * query_string - this should be of such a form that when the
741 * name of the object and '(y/n/q) ?' are appended
742 * then it should still make sense, an example is
747 QueryLoop(elem, print_func, op_func, query_string)
750 FCharStar print_func;
754 char temp_buf[BUFSIZ], *name;
756 elem = QueueTop(elem);
757 one_item = (QueueCount(elem) == 1);
758 while (elem != NULL) {
759 char **info = (char **) elem->q_data;
762 (*op_func) (info, one_item);
764 name = (*print_func) (info); /* call print function. */
765 sprintf(temp_buf,"%s %s (y/n/q)", query_string, name);
766 switch(YesNoQuitQuestion(temp_buf, FALSE)) {
768 (*op_func) (info, one_item);
773 Put_message("Aborting...");
781 /* Function Name: NullPrint
782 * Description: print function that returns nothing.
783 * Arguments: info - a pointer to the info array - Not used.
795 /* Function Name: GetTypeValues
796 * Description: gets legal values for a typed object, keeping a cache
797 * Arguments: type name
798 * Returns: argv of values
806 char *argv[3], *p, **pp, *strsave();
807 struct qelem *elem, *oelem;
808 static struct qelem *cache = NULL;
809 struct cache_elem { char *cache_name; struct qelem *cache_data; } *ce;
811 for (elem = cache; elem; elem = elem->q_forw) {
812 ce = (struct cache_elem *)elem->q_data;
813 if (!strcmp(ce->cache_name, tname))
814 return(ce->cache_data);
821 if (stat = do_mr_query("get_alias", 3, argv, StoreInfo, (char *)&elem)) {
822 com_err(program_name, stat, " in GetTypeValues");
826 for (elem = QueueTop(elem); elem; elem = elem->q_forw) {
827 pp = (char **) elem->q_data;
832 elem = (struct qelem *) malloc(sizeof(struct qelem));
833 ce = (struct cache_elem *) malloc(sizeof(struct cache_elem));
834 ce->cache_name = strsave(tname);
835 ce->cache_data = QueueTop(oelem);
836 elem->q_data = (char *)ce;
837 AddQueue(elem, cache);
838 cache = QueueTop(elem);
839 return(ce->cache_data);
843 /* Function Name: GetTypeFromUser
844 * Description: gets a typed value from the user
845 * Arguments: prompt string, type name, buffer pointer
846 * Returns: SUB_ERROR if ^C, SUB_NORMAL otherwise
849 int GetTypeFromUser(prompt, tname, pointer)
854 char def[BUFSIZ], buffer[BUFSIZ], *p, *argv[3];
858 strcpy(def, *pointer);
859 strcpy(buffer, prompt);
860 strcat(buffer, " (");
861 for (elem = GetTypeValues(tname); elem; elem = elem->q_forw) {
862 strcat(buffer, elem->q_data);
864 strcat(buffer, ", ");
867 if (strlen(buffer) > 64)
868 sprintf(buffer, "%s (? for help)", prompt);
869 if (GetValueFromUser(buffer, pointer) == SUB_ERROR)
871 if (**pointer == '?') {
872 sprintf(buffer, "Type %s is one of:", tname);
874 for (elem = GetTypeValues(tname); elem; elem = elem->q_forw) {
875 Put_message(elem->q_data);
877 *pointer = strsave(def);
878 return(GetTypeFromUser(prompt, tname, pointer));
880 for (elem = GetTypeValues(tname); elem; elem = elem->q_forw) {
881 if (!strcasecmp(elem->q_data, *pointer)) {
882 strcpy(*pointer, elem->q_data);
886 sprintf(buffer, "\"%s\" is not a legal value for %s. Use one of:",
889 for (elem = GetTypeValues(tname); elem; elem = elem->q_forw) {
890 Put_message(elem->q_data);
892 sprintf(buffer, "Are you sure you want \"%s\" to be a legal %s",
894 if (YesNoQuestion("Do you want this to be a new legal value", 0) == TRUE &&
895 YesNoQuestion(buffer, 0) == TRUE) {
899 /* don't uppercase access flags. Do uppercase everything else */
900 if (strncmp(tname, "fs_access", 9))
901 for (p = argv[2]; *p; p++)
904 if (stat = do_mr_query("add_alias", 3, argv, Scream, NULL)) {
905 com_err(program_name, stat, " in add_alias");
907 elem = (struct qelem *) malloc(sizeof(struct qelem));
908 elem->q_data = strsave(*pointer);
909 AddQueue(elem, GetTypeValues(tname));
910 Put_message("Done.");
913 *pointer = strsave(def);
914 return(GetTypeFromUser(prompt, tname, pointer));
918 do_mr_query(name, argc, argv, proc, hint)
926 extern char *whoami, *moira_server;
929 status = mr_query(name, argc, argv, proc, hint);
930 if (status != MR_ABORTED && status != MR_NOT_CONNECTED)
932 status = mr_connect(moira_server);
934 com_err(whoami, status, " while re-connecting to server %s",
938 status = mr_auth(whoami);
940 com_err(whoami, status, " while re-authenticating to server %s",
945 status = mr_query(name, argc, argv, proc, hint);