2 static char rcsid_module_c[] = "$Header$";
5 /* This is the file utils.c for allmaint, the SMS client that allows
6 * a user to maintaint most important parts of the SMS database.
7 * It Contains: Many utilities used by allmaint.
10 * By: Chris D. Peterson
16 * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
18 * For further information on copyright and distribution
19 * see the file mit-copyright.h
28 #include "mit-copyright.h"
30 #include "allmaint_funcs.h"
34 #include <netdb.h> /* for gethostbyname. */
35 /* #include <ctype.h> */
36 /* #include <varargs.h> */
37 /* #include <sys/types.h> */
39 /* Function Name: FreeInfo
40 * Description: Frees all elements of a NULL terminated arrary of char*'s
41 * Arguments: info - array who's elements we are to free.
49 char *pointer = *info;
50 while (pointer != NULL)
54 /* Function Name: FreeAndClear - I couldn't resist the name.
55 * Description: Clears pointer and optionially frees it.
56 * Arguments: pointer - pointer to work with.
57 * free_it - if TRUE then free pointer.
62 FreeAndClear(pointer, free_it)
73 /* Function Name: QueueTop
74 * Description: returns a qelem pointer that points to the top of
76 * Arguments: elem - any element of a queue.
77 * Returns: top element of a queue.
84 if (elem == NULL) /* NULL returns NULL. */
86 while (elem->q_back != NULL)
91 /* Function Name: FreeQueueElem
92 * Description: Frees one element of the queue.
93 * Arguments: elem - the elem to free.
101 char ** info = (char **) elem->q_data;
103 if (info != (char **) NULL) {
104 FreeInfo( info ); /* free info fields */
105 free(elem->q_data); /* free info array itself. */
107 remque(elem); /* remove this element from the queue */
108 free(elem); /* free its space. */
111 /* Function Name: FreeQueue
112 * Description: Cleans up the queue
113 * Arguments: elem - any element of the queue.
121 struct qelem *temp, *local = QueueTop(elem);
123 while(local != NULL) {
124 temp = local->q_forw;
125 FreeQueueElem(local);
130 /* Function Name: QueueCount
131 * Description: Counts the number of elements in a queue
132 * Arguments: elem - any element in the queue.
141 elem = QueueTop(elem);
142 while (elem != NULL) {
151 StoreInfo(argc, argv, data)
156 char ** info = (char **) malloc( MAX_ARGS_SIZE * sizeof(char *) );
157 struct qelem ** old_elem = (struct qelem **) data;
158 struct qelem * new_elem = (struct qelem *) malloc (sizeof (struct qelem));
161 for (count = 0; count < argc; count++)
162 info[count] = Strsave(argv[count]);
163 info[count] = NULL; /* NULL terminate this sucker. */
165 if (*old_elem == (struct qelem *) NULL) { /* first elem. */
166 new_elem->q_data = (char *) info;
167 new_elem->q_forw = new_elem->q_back = (struct qelem *) NULL;
169 else { /* all other elements. */
170 new_elem->q_data = (char *) info;
171 insque(new_elem, *old_elem);
174 *old_elem = new_elem;
178 /* Function Name: CountArgs
179 * Description: Retrieve the number of args in a null terminated
181 * Arguments: info - the argument list.
182 * Returns: number if args in the list.
191 while (*info != NULL) {
199 /* Function Name: Scream
200 * Description: Bitch Loudly and exit, it is intended as a callback
201 * function for queries that should never return a value.
203 * Returns: doesn't exit.
209 com_err(program_name, 0,
210 "\nAn SMS update returned a value -- programmer botch\n");
215 /* Function Name: PromptWithDefault
216 * Description: allows a user to be prompted for input, and given a
218 * Arguments: prompt - the prompt string.
219 * buf, buflen - buffer to be returned and its MAX size?
220 * default value for the answer.
221 * Returns: the value returned by prompt_input.
225 PromptWithDefault(prompt, buf, buflen, def)
233 (void) sprintf(tmp, "%s [%s]: ", prompt, def ? def : "");
234 ans = Prompt_input(tmp, buf, buflen);
236 (void) strcpy(buf, def);
240 /* Function Name: YesNoQuestion
241 * Description: This prompts the user for the answer to a yes-no or
242 * true-false question.
243 * Arguments: prompt - the prompt for the user.
244 * bool_def - the default value either TRUE or FALSE.
245 * Returns: TRUE or FALSE or -1 on error
249 YesNoQuestion(prompt, bool_def)
256 if (!PromptWithDefault(prompt, &ans, 1, bool_def ? "y" : "n"))
266 Put_message("Please answer 'y' or 'n'.");
271 /* Function Name: YesNoQuitQuestion
272 * Description: This prompts the user for the answer to a yes-no or
273 * true-false question, with a quit option.
274 * Arguments: prompt - the prompt for the user.
275 * bool_def - the default value either TRUE or FALSE.
276 * Returns: TRUE or FALSE or -1 on error or QUIT
277 * NOTE: It is not possible to have quit the default, but then I don't
278 * seem to need this functionality.
282 YesNoQuitQuestion(prompt, bool_def)
289 if (!PromptWithDefault(prompt, &ans, 1, bool_def ? "y" : "n"))
302 Put_message("Please answer 'y', 'n' or 'q'.");
308 /* Function Name: Confirm
309 * Description: This function asks the user to confirm the action
310 * he is about to take, used for things like deleting.
311 * Arguments: prompt - the prompt string.
312 * Returns: TRUE/FALSE - wether or not the confirmation occured.
319 return( ~verbose || YesNoQuestion(prompt,FALSE) );
322 /* Function Name: ValidName
323 * Description: This function checks to see if we have a valid list name.
324 * Arguments: s - the list name.
325 * Returns: TRUE if Valid.
333 Put_message("Please use a non-empty name.");
334 else if (index(s, ' '))
335 Put_message("You cannot use space (' ') in this name.");
341 /* Function Name: ToggleVerboseMode
342 * Description: This function toggles the verbose mode.
344 * Returns: DM_NORMAL.
354 Put_message("Delete functions will first confirm\n");
356 Put_message("Delete functions will be silent\n");
361 /* Function Name: NullFunc
362 * Description: dummy callback routine
373 /* Function Name: SlipInNewName
374 * Description: Slips the new name into the number 2 slot of a list, and
375 * returns a pointer to the new list.
376 * Arguments: info - list that needs this name slipped into it.
377 * name - the name to slip into the list.
378 * Returns: a pointer to the new list.
379 * NOTE: This screws up the numbers if the elements of the array in a
383 SlipInNewName(info, name)
389 /* This also pushes the NULL down. */
390 for (i = CountArgs(info); i > 1; i--) {
393 info[1] = Strsave(name); /* now slip in the name. */
396 /* Function Name: GetValueFromUser
397 * Description: This function gets a value from a user for the field
399 * Arguments: prompt - prompt for user.
400 * pointer - pointer to default value, will be returned
406 GetValueFromUser(prompt, pointer)
407 char * prompt, ** pointer;
411 PromptWithDefault(prompt, buf, BUFSIZ, *pointer);
412 if ( pointer != NULL)
414 *pointer = Strsave(buf);
417 /* Function Name: CanonicalizeHostname
418 * Description: This function takes a machine name and canonicalize's it.
419 * Arguments: machine - name of the machine to work on.
420 * Returns: new name or NULL if nameserver returns error
424 CanonicalizeHostname(machine)
427 struct hostent *hostinfo;
429 hostinfo = gethostbyname(machine);
430 if (hostinfo != (struct hostent *) NULL)
431 machine = hostinfo->h_name;
434 * gethostbyname failed. This should be very rare, since we're
435 * dealing with local hosts, so no fancy error recovery.
437 machine = (char *) NULL;
441 /* Function Name: Strsave
442 * Description: save a string.
443 * Arguments: string - the string to save.
444 * Returns: The malloced string, now safely saved, or NULL.
451 register char *newstr = malloc((unsigned) strlen(str) + 1);
453 if (newstr == (char *) NULL)
454 return ((char *) NULL);
456 return (strcpy(newstr, str));
459 /* Function Name: EncryptMITID
460 * Description: Encrypts an mit ID number.
461 * Arguments: sbuf - the buffer to return the encrypted number in.
462 * idnumber - the id number (string).
463 * first, last - name of the person.
468 EncryptMITID(sbuf, idnumber, first, last)
469 char *sbuf, *idnumber, *first, *last;
472 extern char *crypt();
474 #define _tolower(c) ((c)|0x60)
476 salt[0] = _tolower(last[0]);
477 salt[1] = _tolower(first[0]);
480 (void) strcpy(sbuf, crypt(&idnumber[2], salt));
483 /* Function Name: RemoveHyphens
484 * Description: Removes all hyphens from the string passed to it.
485 * Arguments: str - the string to remove the hyphes from
495 while ((hyphen = index(str, '-')) != NULL)
496 (void) strcpy(hyphen, hyphen + 1);
499 /* Function Name: Print
500 * Description: prints out all the arguments on a single line.
501 * Arguments: argc, argv - the standard SMS arguments.
502 * callback - the callback function - NOT USED.
508 Print(argc, argv, callback)
510 char **argv, *callback;
516 (void) strcpy(buf,argv[0]); /* no newline 'cause Put_message adds one */
517 for (i = 1; i < argc; i++)
518 (void) sprintf(buf,"%s %s",buf,argv[i]);
519 (void) Put_message(buf);
524 /* Function Name: PrintByType
525 * Description: This function prints all members of the type specified
526 * by the callback arg, unless the callback is NULL, in which
527 * case it prints all members.
528 * Arguments: argc, argc - normal arguments for sms_callback function.
529 * callback - either a type of member or NULL.
530 * Returns: SMS_CONT or SMS_QUIT.
535 PrintByType(argc, argv, callback)
537 char **argv, *callback;
539 if (callback == NULL)
540 return( Print(argc, argv, callback) );
541 if (strcmp(argv[0], callback) == 0)
542 return( Print(1, argv + 1, callback) );
546 /* Function Name: PrintHelp
547 * Description: Prints Help Information in a NULL terminated
549 * Arguments: message.
550 * Returns: DM_NORMAL.
557 Print(CountArgs(message), message, (char *) NULL);
565 * c-continued-statement-offset: 4
567 * c-argdecl-indent: 4