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;
72 /* Function Name: FreeInfo
73 * Description: Frees all elements of a NULL terminated arrary of char*'s
74 * Arguments: info - array who's elements we are to free.
83 FreeAndClear(info++, TRUE);
86 /* Function Name: FreeAndClear - I couldn't resist the name.
87 * Description: Clears pointer and optionially frees it.
88 * Arguments: pointer - pointer to work with.
89 * free_it - if TRUE then free pointer.
94 FreeAndClear(pointer, free_it)
105 /* Function Name: QueueTop
106 * Description: returns a qelem pointer that points to the top of
108 * Arguments: elem - any element of a queue.
109 * Returns: top element of a queue.
116 if (elem == NULL) /* NULL returns NULL. */
118 while (elem->q_back != NULL)
123 /* Function Name: FreeQueueElem
124 * Description: Frees one element of the queue.
125 * Arguments: elem - the elem to free.
133 char ** info = (char **) elem->q_data;
135 if (info != (char **) NULL) {
136 FreeInfo( info ); /* free info fields */
137 free(elem->q_data); /* free info array itself. */
139 RemoveQueue(elem); /* remove this element from the queue */
140 free(elem); /* free its space. */
143 /* Function Name: FreeQueue
144 * Description: Cleans up the queue
145 * Arguments: elem - any element of the queue.
153 struct qelem *temp, *local = QueueTop(elem);
155 while(local != NULL) {
156 temp = local->q_forw;
157 FreeQueueElem(local);
162 /* Function Name: QueueCount
163 * Description: Counts the number of elements in a queue
164 * Arguments: elem - any element in the queue.
173 elem = QueueTop(elem);
174 while (elem != NULL) {
181 /* Function Name: StoreInfo
182 * Description: Stores information from an moira query into a queue.
183 * Arguments: argc, argv, - information returned from the query returned
185 * data - the previous element on the queue, this data will be
186 * stored in a qelem struct immediatly after this elem.
187 * If NULL then a new queue will be created. This value
188 * is updated to the current element at the end off the
190 * Returns: MR_CONT, or MR_ABORT if it has problems.
194 StoreInfo(argc, argv, data)
199 char ** info = (char **) malloc( MAX_ARGS_SIZE * sizeof(char *));
200 struct qelem ** old_elem = (struct qelem **) data;
201 struct qelem * new_elem = (struct qelem *) malloc (sizeof (struct qelem));
204 if ( (new_elem == (struct qelem *) NULL) || (info == (char **) NULL) ) {
205 Put_message("Could Not allocate more memory.");
206 FreeQueue(*old_elem);
207 *old_elem = (struct qelem *) NULL;
211 for (count = 0; count < argc; count++)
212 info[count] = Strsave(argv[count]);
213 info[count] = NULL; /* NULL terminate this sucker. */
215 new_elem->q_data = (char *) info;
216 AddQueue(new_elem, *old_elem);
218 *old_elem = new_elem;
222 /* Function Name: CountArgs
223 * Description: Retrieve the number of args in a null terminated
225 * Arguments: info - the argument list.
226 * Returns: number if args in the list.
235 while (*info != NULL) {
243 /* Function Name: Scream
244 * Description: Bitch Loudly and exit, it is intended as a callback
245 * function for queries that should never return a value.
247 * Returns: doesn't exit.
253 com_err(program_name, 0,
254 "\nA Moira update returned a value -- programmer botch\n");
259 /* Function Name: PromptWithDefault
260 * Description: allows a user to be prompted for input, and given a
262 * Arguments: prompt - the prompt string.
263 * buf, buflen - buffer to be returned and its MAX size?
264 * default value for the answer.
265 * Returns: zero on failure
269 PromptWithDefault(prompt, buf, buflen, def)
277 if (parsed_argc > 0) {
279 strncpy(buf, parsed_argv[0], buflen);
280 sprintf(tmp, "%s: %s", prompt, buf);
286 (void) sprintf(tmp, "%s [%s]: ", prompt, def ? def : "");
287 ans = Prompt_input(tmp, buf, buflen);
289 (void) strcpy(buf, def);
290 else if (!strcmp(buf, "\"\""))
295 /* Function Name: YesNoQuestion
296 * Description: This prompts the user for the answer to a yes-no or
297 * true-false question.
298 * Arguments: prompt - the prompt for the user.
299 * bool_def - the default value either TRUE or FALSE.
300 * Returns: TRUE or FALSE or -1 on error
304 YesNoQuestion(prompt, bool_def)
311 if (!PromptWithDefault(prompt, ans, 2, bool_def ? "y" : "n"))
321 Put_message("Please answer 'y' or 'n'.");
326 /* Function Name: YesNoQuitQuestion
327 * Description: This prompts the user for the answer to a yes-no or
328 * true-false question, with a quit option.
329 * Arguments: prompt - the prompt for the user.
330 * bool_def - the default value either TRUE or FALSE.
331 * Returns: TRUE or FALSE or -1 on error or QUIT
332 * NOTE: It is not possible to have quit the default, but then I don't
333 * seem to need this functionality.
337 YesNoQuitQuestion(prompt, bool_def)
344 if (!PromptWithDefault(prompt, ans, 2, bool_def ? "y" : "n"))
357 Put_message("Please answer 'y', 'n' or 'q'.");
363 /* Function Name: Confirm
364 * Description: This function asks the user to confirm the action
365 * he is about to take, used for things like deleting.
366 * Arguments: prompt - the prompt string.
367 * Returns: TRUE/FALSE - wether or not the confirmation occured.
374 return( !verbose || (YesNoQuestion(prompt,FALSE) == TRUE) );
377 /* Function Name: ValidName
378 * Description: This function checks to see if we have a valid list name.
379 * Arguments: s - the list name.
380 * Returns: TRUE if Valid.
388 Put_message("Please use a non-empty name.");
389 else if (index(s, ' '))
390 Put_message("You cannot use space (' ') in this name.");
391 else if (index(s, '*') || index(s, '?') || index(s, '['))
392 Put_message("Wildcards not accepted here.");
398 /* Function Name: ToggleVerboseMode
399 * Description: This function toggles the verbose mode.
401 * Returns: DM_NORMAL.
411 Put_message("Delete functions will first confirm\n");
413 Put_message("Delete functions will be silent\n");
418 /* Function Name: NullFunc
419 * Description: dummy callback routine
430 /* Function Name: SlipInNewName
431 * Description: Slips the new name into the number 2 slot of a list, and
432 * returns a pointer to the new list.
433 * Arguments: info - list that needs this name slipped into it.
434 * name - the name to slip into the list.
435 * Returns: a pointer to the new list.
436 * NOTE: This screws up the numbers of the elements of the array in a
441 SlipInNewName(info, name)
447 /* This also pushes the NULL down. */
448 for (i = CountArgs(info); i > 0; i--) {
451 info[1] = name; /* now slip in the name. */
454 /* Function Name: GetValueFromUser
455 * Description: This function gets a value from a user for the field
457 * Arguments: prompt - prompt for user.
458 * pointer - pointer to default value, will be returned
460 * Returns: SUB_ERROR if break hit (^C).
464 GetValueFromUser(prompt, pointer)
465 char * prompt, ** pointer;
469 if (PromptWithDefault(prompt, buf, BUFSIZ, *pointer) == 0)
473 * If these are the same then there is no need to allocate a new string.
475 * a difference that makes no difference, IS no difference.
478 if (strcmp(buf, *pointer) != 0) {
479 if (*pointer != NULL)
481 *pointer = Strsave(buf);
486 /* Function Name: GetYesNoValueFromUser
487 * Description: This function gets a value from a user for the field
489 * Arguments: prompt - prompt for user.
490 * pointer - pointer to default value, will be returned
492 * Returns: SUB_ERROR if break hit (^C).
496 GetYesNoValueFromUser(prompt, pointer)
497 char * prompt, ** pointer;
499 char user_prompt[BUFSIZ];
502 if ( strcmp (*pointer, DEFAULT_YES) == 0 )
507 sprintf(user_prompt, "%s (y/n)", prompt);
509 switch (YesNoQuestion(user_prompt, default_val)) {
511 if (*pointer != NULL)
513 *pointer = Strsave(DEFAULT_YES);
516 if (*pointer != NULL)
518 *pointer = Strsave(DEFAULT_NO);
527 /* Function Name: GetFSVal
528 * Description: asks about a specific filesystem value.
529 * Arguments: name - string for this type of filesystem.
530 * mask - mask for this type of filesystem.
531 * current - current filesystem state. (for defaults).
532 * new - new filesystem state.
533 * Returns: TRUE if successful.
537 GetFSVal(name, mask, current, new)
539 int mask, current, *new;
541 char temp_buf[BUFSIZ];
542 sprintf(temp_buf, "Is this a %s filsystem", name);
543 switch (YesNoQuestion(temp_buf, ( (mask & current) == mask) )) {
548 break; /* zero by default. */
555 /* Function Name: GetFSTypes
556 * Description: Allows user to specify filsystem types.
557 * Arguments: current - current value of filsystem, freed here.
558 * Returns: SUB_ERROR on ^C.
562 GetFSTypes(current, options)
566 int c_value, new_val = 0; /* current value of filesys type (int). */
567 char ret_value[BUFSIZ];
569 if (*current == NULL)
572 c_value = atoi(*current);
574 if (GetFSVal("student", MR_FS_STUDENT, c_value, &new_val) == FALSE)
576 if (GetFSVal("faculty", MR_FS_FACULTY, c_value, &new_val) == FALSE)
578 if (GetFSVal("staff", MR_FS_STAFF, c_value, &new_val) == FALSE)
580 if (GetFSVal("miscellaneous", MR_FS_MISC, c_value, &new_val) == FALSE)
583 if (GetFSVal("Group Quotas", MR_FS_GROUPQUOTA, c_value, &new_val) ==
588 FreeAndClear(current, TRUE);
589 sprintf(ret_value, "%d", new_val);
590 *current = Strsave(ret_value);
594 /* Function Name: Strsave
595 * Description: save a string.
596 * Arguments: string - the string to save.
597 * Returns: The malloced string, now safely saved, or NULL.
604 register char *newstr = malloc((unsigned) strlen(str) + 1);
606 if (newstr == (char *) NULL)
607 return ((char *) NULL);
609 return (strcpy(newstr, str));
612 /* Function Name: Print
613 * Description: prints out all the arguments on a single line.
614 * Arguments: argc, argv - the standard MR arguments.
615 * callback - the callback function - NOT USED.
621 Print(argc, argv, callback)
623 char **argv, *callback;
629 (void) strcpy(buf,argv[0]); /* no newline 'cause Put_message adds one */
630 for (i = 1; i < argc; i++)
631 (void) sprintf(buf,"%s %s",buf,argv[i]);
632 (void) Put_message(buf);
637 /* Function Name: PrintByType
638 * Description: This function prints all members of the type specified
639 * by the callback arg, unless the callback is NULL, in which
640 * case it prints all members.
641 * Arguments: argc, argc - normal arguments for mr_callback function.
642 * callback - either a type of member or NULL.
643 * Returns: MR_CONT or MR_QUIT.
648 PrintByType(argc, argv, callback)
650 char **argv, *callback;
652 if (callback == NULL)
653 return( Print(argc, argv, callback) );
654 if (strcmp(argv[0], callback) == 0)
655 return( Print(argc, argv, callback) );
659 /* Function Name: PrintHelp
660 * Description: Prints Help Information in a NULL terminated
662 * Arguments: message.
663 * Returns: DM_NORMAL.
672 for (i = 0; i < CountArgs(message); i++)
673 Put_message(message[i]);
678 /* Function Name: Loop
679 * Description: This function goes through the entire queue, and
680 * and executes the given function on each element.
681 * Arguments: elem - top element of the queue.
682 * func - the function to execute.
691 while (elem != NULL) {
692 char ** info = (char **) elem->q_data;
699 /* Function Name: QueryLoop
700 * Description: This functions loops through a queue containing
701 * information about some item that we want to perform
702 * an operation on, and then calls the correct routine
703 * perform that operation.
704 * Arguments: top - top of the queue of information.
705 * print_func - print function.
706 * op_function - operation to be performed.
707 * query_string - string the prompts the user whether or not
708 * to perform this operation.
711 * print_opt - should expect one arguent, the info array
713 * is expected to return the name of the item.
714 * op_func - should expect two arguments.
715 * 1) the info array of char *'s.
716 * 2) a boolean the is true if there only
717 * one item in this queue, used for delete
719 * query_string - this should be of such a form that when the
720 * name of the object and '(y/n/q) ?' are appended
721 * then it should still make sense, an example is
726 QueryLoop(elem, print_func, op_func, query_string)
729 FCharStar print_func;
733 char temp_buf[BUFSIZ], *name;
735 elem = QueueTop(elem);
736 one_item = (QueueCount(elem) == 1);
737 while (elem != NULL) {
738 char **info = (char **) elem->q_data;
741 (*op_func) (info, one_item);
743 name = (*print_func) (info); /* call print function. */
744 sprintf(temp_buf,"%s %s (y/n/q)", query_string, name);
745 switch(YesNoQuitQuestion(temp_buf, FALSE)) {
747 (*op_func) (info, one_item);
752 Put_message("Aborting...");
760 /* Function Name: NullPrint
761 * Description: print function that returns nothing.
762 * Arguments: info - a pointer to the info array - Not used.
774 /* Function Name: GetTypeValues
775 * Description: gets legal values for a typed object, keeping a cache
776 * Arguments: type name
777 * Returns: argv of values
785 char *argv[3], *p, **pp, *strsave();
786 struct qelem *elem, *oelem;
787 static struct qelem *cache = NULL;
788 struct cache_elem { char *cache_name; struct qelem *cache_data; } *ce;
790 for (elem = cache; elem; elem = elem->q_forw) {
791 ce = (struct cache_elem *)elem->q_data;
792 if (!strcmp(ce->cache_name, tname))
793 return(ce->cache_data);
800 if (stat = do_mr_query("get_alias", 3, argv, StoreInfo, (char *)&elem)) {
801 com_err(program_name, stat, " in GetTypeValues");
805 for (elem = QueueTop(elem); elem; elem = elem->q_forw) {
806 pp = (char **) elem->q_data;
811 elem = (struct qelem *) malloc(sizeof(struct qelem));
812 ce = (struct cache_elem *) malloc(sizeof(struct cache_elem));
813 ce->cache_name = strsave(tname);
814 ce->cache_data = QueueTop(oelem);
815 elem->q_data = (char *)ce;
816 AddQueue(elem, cache);
817 cache = QueueTop(elem);
818 return(ce->cache_data);
822 /* Function Name: GetTypeFromUser
823 * Description: gets a typed value from the user
824 * Arguments: prompt string, type name, buffer pointer
825 * Returns: SUB_ERROR if ^C, SUB_NORMAL otherwise
828 int GetTypeFromUser(prompt, tname, pointer)
833 char def[BUFSIZ], buffer[BUFSIZ], *p, *argv[3];
837 strcpy(def, *pointer);
838 strcpy(buffer, prompt);
839 strcat(buffer, " (");
840 for (elem = GetTypeValues(tname); elem; elem = elem->q_forw) {
841 strcat(buffer, elem->q_data);
843 strcat(buffer, ", ");
846 if (strlen(buffer) > 64)
847 sprintf(buffer, "%s (? for help)", prompt);
848 if (GetValueFromUser(buffer, pointer) == SUB_ERROR)
850 if (**pointer == '?') {
851 sprintf(buffer, "Type %s is one of:", tname);
853 for (elem = GetTypeValues(tname); elem; elem = elem->q_forw) {
854 Put_message(elem->q_data);
856 *pointer = strsave(def);
857 return(GetTypeFromUser(prompt, tname, pointer));
859 for (elem = GetTypeValues(tname); elem; elem = elem->q_forw) {
860 if (!strcasecmp(elem->q_data, *pointer)) {
861 strcpy(*pointer, elem->q_data);
865 sprintf(buffer, "\"%s\" is not a legal value for %s. Use one of:",
868 for (elem = GetTypeValues(tname); elem; elem = elem->q_forw) {
869 Put_message(elem->q_data);
871 sprintf(buffer, "Are you sure you want \"%s\" to be a legal %s",
873 if (YesNoQuestion("Do you want this to be a new legal value", 0) == TRUE &&
874 YesNoQuestion(buffer, 0) == TRUE) {
878 /* don't uppercase access flags. Do uppercase everything else */
879 if (strncmp(tname, "fs_access", 9))
880 for (p = argv[2]; *p; p++)
883 if (stat = do_mr_query("add_alias", 3, argv, Scream, NULL)) {
884 com_err(program_name, stat, " in add_alias");
886 elem = (struct qelem *) malloc(sizeof(struct qelem));
887 elem->q_data = strsave(*pointer);
888 AddQueue(elem, GetTypeValues(tname));
889 Put_message("Done.");
892 *pointer = strsave(def);
893 return(GetTypeFromUser(prompt, tname, pointer));
897 do_mr_query(name, argc, argv, proc, hint)
905 extern char *whoami, *moira_server;
908 status = mr_query(name, argc, argv, proc, hint);
909 if (status != MR_ABORTED && status != MR_NOT_CONNECTED)
911 status = mr_connect(moira_server);
913 com_err(whoami, status, " while re-connecting to server %s",
917 status = mr_auth(whoami);
919 com_err(whoami, status, " while re-authenticating to server %s",
924 status = mr_query(name, argc, argv, proc, hint);