]>
Commit | Line | Data |
---|---|---|
402461ad | 1 | #if (!defined(lint) && !defined(SABER)) |
08345b74 | 2 | static char rcsid_module_c[] = "$Header$"; |
3 | #endif lint | |
4 | ||
0a2c64cb | 5 | /* This is the file utils.c for the SMS Client, which allows a nieve |
6 | * user to quickly and easily maintain most parts of the SMS database. | |
7 | * It Contains: Many useful utility functions. | |
08345b74 | 8 | * |
9 | * Created: 4/25/88 | |
10 | * By: Chris D. Peterson | |
11 | * | |
12 | * $Source$ | |
13 | * $Author$ | |
14 | * $Header$ | |
15 | * | |
0a2c64cb | 16 | * Copyright 1988 by the Massachusetts Institute of Technology. |
08345b74 | 17 | * |
18 | * For further information on copyright and distribution | |
19 | * see the file mit-copyright.h | |
20 | */ | |
21 | ||
08345b74 | 22 | #include <stdio.h> |
23 | #include <strings.h> | |
24 | #include <sms.h> | |
cc3056a6 | 25 | #include <sms_app.h> |
08345b74 | 26 | #include <menu.h> |
6769d5bc | 27 | #include <ctype.h> |
08345b74 | 28 | |
29 | #include "mit-copyright.h" | |
0a2c64cb | 30 | #include "defs.h" |
31 | #include "f_defs.h" | |
08345b74 | 32 | #include "globals.h" |
08345b74 | 33 | |
34 | #include <netdb.h> /* for gethostbyname. */ | |
08345b74 | 35 | |
0a2c64cb | 36 | /* Function Name: AddQueue |
37 | * Description: Adds an element to a queue | |
38 | * Arguments: elem, pred - element and its predecessor. | |
39 | * Returns: none. | |
40 | */ | |
41 | ||
42 | static void | |
43 | AddQueue(elem, pred) | |
44 | struct qelem * elem, *pred; | |
45 | { | |
46 | if (pred == NULL) { | |
47 | elem->q_forw = NULL; | |
48 | elem->q_back = NULL; | |
49 | return; | |
50 | } | |
51 | elem->q_back = pred; | |
52 | elem->q_forw = pred->q_forw; | |
53 | pred->q_forw = elem; | |
54 | } | |
55 | ||
56 | /* Function Name: RemoveQueue | |
57 | * Description: removes an element from a queue. | |
58 | * Arguments: elem. | |
59 | * Returns: none. | |
60 | */ | |
61 | ||
62 | static void | |
63 | RemoveQueue(elem) | |
64 | struct qelem *elem; | |
65 | { | |
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; | |
70 | } | |
71 | ||
08345b74 | 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. | |
75 | * Returns: none. | |
76 | */ | |
77 | ||
78 | void | |
79 | FreeInfo(info) | |
80 | char ** info; | |
81 | { | |
85ca828a | 82 | while (*info != NULL) |
83 | FreeAndClear(info++, TRUE); | |
08345b74 | 84 | } |
85 | ||
461c03b6 | 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. | |
90 | * Returns: none. | |
91 | */ | |
92 | ||
93 | void | |
94 | FreeAndClear(pointer, free_it) | |
95 | char ** pointer; | |
96 | Bool free_it; | |
97 | { | |
98 | if (*pointer == NULL) | |
99 | return; | |
100 | else if (free_it) | |
101 | free(*pointer); | |
102 | *pointer = NULL; | |
103 | } | |
104 | ||
105 | /* Function Name: QueueTop | |
106 | * Description: returns a qelem pointer that points to the top of | |
107 | * a queue. | |
108 | * Arguments: elem - any element of a queue. | |
109 | * Returns: top element of a queue. | |
110 | */ | |
111 | ||
112 | struct qelem * | |
113 | QueueTop(elem) | |
114 | struct qelem * elem; | |
115 | { | |
116 | if (elem == NULL) /* NULL returns NULL. */ | |
117 | return(NULL); | |
118 | while (elem->q_back != NULL) | |
119 | elem = elem->q_back; | |
120 | return(elem); | |
121 | } | |
122 | ||
08345b74 | 123 | /* Function Name: FreeQueueElem |
124 | * Description: Frees one element of the queue. | |
125 | * Arguments: elem - the elem to free. | |
126 | * Returns: none | |
127 | */ | |
128 | ||
0a2c64cb | 129 | static void |
08345b74 | 130 | FreeQueueElem(elem) |
461c03b6 | 131 | struct qelem * elem; |
08345b74 | 132 | { |
461c03b6 | 133 | char ** info = (char **) elem->q_data; |
134 | ||
135 | if (info != (char **) NULL) { | |
136 | FreeInfo( info ); /* free info fields */ | |
08345b74 | 137 | free(elem->q_data); /* free info array itself. */ |
138 | } | |
85ca828a | 139 | RemoveQueue(elem); /* remove this element from the queue */ |
08345b74 | 140 | free(elem); /* free its space. */ |
141 | } | |
142 | ||
143 | /* Function Name: FreeQueue | |
144 | * Description: Cleans up the queue | |
145 | * Arguments: elem - any element of the queue. | |
146 | * Returns: none. | |
147 | */ | |
148 | ||
149 | void | |
150 | FreeQueue(elem) | |
461c03b6 | 151 | struct qelem * elem; |
08345b74 | 152 | { |
461c03b6 | 153 | struct qelem *temp, *local = QueueTop(elem); |
08345b74 | 154 | |
155 | while(local != NULL) { | |
156 | temp = local->q_forw; | |
157 | FreeQueueElem(local); | |
158 | local = temp; | |
159 | } | |
160 | } | |
161 | ||
162 | /* Function Name: QueueCount | |
163 | * Description: Counts the number of elements in a queue | |
164 | * Arguments: elem - any element in the queue. | |
165 | * Returns: none. | |
166 | */ | |
167 | ||
168 | int | |
169 | QueueCount(elem) | |
170 | struct qelem * elem; | |
171 | { | |
172 | int count = 0; | |
173 | elem = QueueTop(elem); | |
174 | while (elem != NULL) { | |
175 | count ++; | |
176 | elem = elem->q_forw; | |
177 | } | |
178 | return(count); | |
179 | } | |
180 | ||
0a2c64cb | 181 | /* Function Name: StoreInfo |
182 | * Description: Stores information from an sms query into a queue. | |
183 | * Arguments: argc, argv, - information returned from the query returned | |
184 | * in argv. | |
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 | |
189 | * call. | |
190 | * Returns: SMS_CONT, or SMS_ABORT if it has problems. | |
191 | */ | |
192 | ||
08345b74 | 193 | int |
194 | StoreInfo(argc, argv, data) | |
195 | int argc; | |
196 | char ** argv; | |
461c03b6 | 197 | char * data; |
08345b74 | 198 | { |
402461ad | 199 | char ** info = (char **) malloc( MAX_ARGS_SIZE * sizeof(char *)); |
08345b74 | 200 | struct qelem ** old_elem = (struct qelem **) data; |
201 | struct qelem * new_elem = (struct qelem *) malloc (sizeof (struct qelem)); | |
202 | int count; | |
203 | ||
402461ad | 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; | |
208 | return(SMS_ABORT); | |
209 | } | |
210 | ||
08345b74 | 211 | for (count = 0; count < argc; count++) |
212 | info[count] = Strsave(argv[count]); | |
213 | info[count] = NULL; /* NULL terminate this sucker. */ | |
214 | ||
85ca828a | 215 | new_elem->q_data = (char *) info; |
216 | AddQueue(new_elem, *old_elem); | |
08345b74 | 217 | |
218 | *old_elem = new_elem; | |
219 | return(SMS_CONT); | |
220 | } | |
221 | ||
222 | /* Function Name: CountArgs | |
223 | * Description: Retrieve the number of args in a null terminated | |
224 | * arglist. | |
225 | * Arguments: info - the argument list. | |
226 | * Returns: number if args in the list. | |
227 | */ | |
228 | ||
229 | int | |
461c03b6 | 230 | CountArgs(info) |
08345b74 | 231 | char ** info; |
232 | { | |
233 | int number = 0; | |
234 | ||
461c03b6 | 235 | while (*info != NULL) { |
08345b74 | 236 | number++; |
461c03b6 | 237 | info++; |
238 | } | |
08345b74 | 239 | |
240 | return(number); | |
241 | } | |
242 | ||
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. | |
246 | * Arguments: none | |
247 | * Returns: doesn't exit. | |
248 | */ | |
249 | ||
461c03b6 | 250 | int |
08345b74 | 251 | Scream() |
252 | { | |
461c03b6 | 253 | com_err(program_name, 0, |
08345b74 | 254 | "\nAn SMS update returned a value -- programmer botch\n"); |
255 | sms_disconnect(); | |
256 | exit(1); | |
257 | } | |
258 | ||
08345b74 | 259 | /* Function Name: PromptWithDefault |
260 | * Description: allows a user to be prompted for input, and given a | |
261 | * default choice. | |
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: the value returned by prompt_input. | |
266 | */ | |
267 | ||
268 | int | |
269 | PromptWithDefault(prompt, buf, buflen, def) | |
270 | char *prompt, *buf; | |
271 | int buflen; | |
272 | char *def; | |
273 | { | |
274 | char tmp[BUFSIZ]; | |
275 | int ans; | |
276 | ||
277 | (void) sprintf(tmp, "%s [%s]: ", prompt, def ? def : ""); | |
278 | ans = Prompt_input(tmp, buf, buflen); | |
0b6a715f | 279 | if (ans == 0) { |
6769d5bc | 280 | if (YesNoQuestion("Are you sure you want to exit", 1)) { |
281 | Cleanup_menu(); | |
0b6a715f | 282 | exit(0); |
6769d5bc | 283 | } |
0b6a715f | 284 | Put_message("Continuing input..."); |
285 | return(PromptWithDefault(prompt, buf, buflen, def)); | |
286 | } | |
08345b74 | 287 | if (IS_EMPTY(buf)) |
288 | (void) strcpy(buf, def); | |
0b6a715f | 289 | else if (!strcmp(buf, "\"\"")) |
290 | *buf = 0; | |
08345b74 | 291 | return(ans); |
292 | } | |
293 | ||
294 | /* Function Name: YesNoQuestion | |
295 | * Description: This prompts the user for the answer to a yes-no or | |
296 | * true-false question. | |
297 | * Arguments: prompt - the prompt for the user. | |
298 | * bool_def - the default value either TRUE or FALSE. | |
299 | * Returns: TRUE or FALSE or -1 on error | |
300 | */ | |
301 | ||
302 | Bool | |
303 | YesNoQuestion(prompt, bool_def) | |
304 | char *prompt; | |
305 | int bool_def; | |
306 | { | |
3c1a8806 | 307 | char ans[2]; |
08345b74 | 308 | |
309 | while (TRUE) { | |
3c1a8806 | 310 | if (!PromptWithDefault(prompt, ans, 2, bool_def ? "y" : "n")) |
08345b74 | 311 | return(-1); |
3c1a8806 | 312 | switch (ans[0]) { |
08345b74 | 313 | case 'n': |
314 | case 'N': | |
315 | return(FALSE); | |
08345b74 | 316 | case 'y': |
317 | case 'Y': | |
318 | return(TRUE); | |
08345b74 | 319 | default: |
320 | Put_message("Please answer 'y' or 'n'."); | |
321 | break; | |
322 | } | |
323 | } | |
324 | } | |
325 | /* Function Name: YesNoQuitQuestion | |
326 | * Description: This prompts the user for the answer to a yes-no or | |
327 | * true-false question, with a quit option. | |
328 | * Arguments: prompt - the prompt for the user. | |
329 | * bool_def - the default value either TRUE or FALSE. | |
330 | * Returns: TRUE or FALSE or -1 on error or QUIT | |
331 | * NOTE: It is not possible to have quit the default, but then I don't | |
332 | * seem to need this functionality. | |
333 | */ | |
334 | ||
335 | Bool | |
461c03b6 | 336 | YesNoQuitQuestion(prompt, bool_def) |
08345b74 | 337 | char *prompt; |
338 | int bool_def; | |
339 | { | |
3c1a8806 | 340 | char ans[2]; |
08345b74 | 341 | |
342 | while (TRUE) { | |
3c1a8806 | 343 | if (!PromptWithDefault(prompt, ans, 2, bool_def ? "y" : "n")) |
08345b74 | 344 | return(-1); |
3c1a8806 | 345 | switch (ans[0]) { |
08345b74 | 346 | case 'n': |
347 | case 'N': | |
348 | return(FALSE); | |
08345b74 | 349 | case 'y': |
350 | case 'Y': | |
351 | return(TRUE); | |
08345b74 | 352 | case 'q': |
353 | case 'Q': | |
354 | return(-1); | |
08345b74 | 355 | default: |
356 | Put_message("Please answer 'y', 'n' or 'q'."); | |
357 | break; | |
358 | } | |
359 | } | |
360 | } | |
361 | ||
362 | /* Function Name: Confirm | |
363 | * Description: This function asks the user to confirm the action | |
364 | * he is about to take, used for things like deleting. | |
365 | * Arguments: prompt - the prompt string. | |
366 | * Returns: TRUE/FALSE - wether or not the confirmation occured. | |
367 | */ | |
368 | ||
0a2c64cb | 369 | Bool |
08345b74 | 370 | Confirm(prompt) |
371 | char * prompt; | |
372 | { | |
0a2c64cb | 373 | return( !verbose || (YesNoQuestion(prompt,FALSE) == TRUE) ); |
08345b74 | 374 | } |
375 | ||
376 | /* Function Name: ValidName | |
377 | * Description: This function checks to see if we have a valid list name. | |
378 | * Arguments: s - the list name. | |
379 | * Returns: TRUE if Valid. | |
380 | */ | |
381 | ||
382 | Bool | |
383 | ValidName(s) | |
0a2c64cb | 384 | char *s; |
08345b74 | 385 | { |
386 | if (IS_EMPTY(s)) | |
387 | Put_message("Please use a non-empty name."); | |
388 | else if (index(s, ' ')) | |
389 | Put_message("You cannot use space (' ') in this name."); | |
402461ad | 390 | else if (index(s, WILDCARD)) |
391 | Put_message("Wildcards not accepted here."); | |
08345b74 | 392 | else |
393 | return TRUE; | |
394 | return FALSE; | |
395 | } | |
396 | ||
397 | /* Function Name: ToggleVerboseMode | |
398 | * Description: This function toggles the verbose mode. | |
399 | * Arguments: none | |
400 | * Returns: DM_NORMAL. | |
401 | */ | |
402 | ||
403 | int | |
404 | ToggleVerboseMode() | |
405 | { | |
406 | ||
85ca828a | 407 | verbose = !verbose; |
08345b74 | 408 | |
461c03b6 | 409 | if (verbose) |
410 | Put_message("Delete functions will first confirm\n"); | |
411 | else | |
412 | Put_message("Delete functions will be silent\n"); | |
413 | ||
08345b74 | 414 | return(DM_NORMAL); |
415 | } | |
416 | ||
417 | /* Function Name: NullFunc | |
418 | * Description: dummy callback routine | |
419 | * Arguments: none | |
420 | * Returns: SMS_CONT | |
421 | */ | |
422 | ||
423 | int | |
424 | NullFunc() | |
425 | { | |
426 | return(SMS_CONT); | |
427 | } | |
428 | ||
429 | /* Function Name: SlipInNewName | |
430 | * Description: Slips the new name into the number 2 slot of a list, and | |
431 | * returns a pointer to the new list. | |
432 | * Arguments: info - list that needs this name slipped into it. | |
433 | * name - the name to slip into the list. | |
434 | * Returns: a pointer to the new list. | |
85ca828a | 435 | * NOTE: This screws up the numbers of the elements of the array in a |
08345b74 | 436 | * big way. |
437 | */ | |
438 | ||
0a2c64cb | 439 | void |
461c03b6 | 440 | SlipInNewName(info, name) |
08345b74 | 441 | char ** info; |
08345b74 | 442 | char * name; |
443 | { | |
444 | register int i; | |
445 | ||
446 | /* This also pushes the NULL down. */ | |
447 | for (i = CountArgs(info); i > 1; i--) { | |
85ca828a | 448 | info[i] = info[i-1]; |
08345b74 | 449 | } |
85ca828a | 450 | info[1] = name; /* now slip in the name. */ |
08345b74 | 451 | } |
452 | ||
08345b74 | 453 | /* Function Name: GetValueFromUser |
454 | * Description: This function gets a value from a user for the field | |
455 | * specified. | |
456 | * Arguments: prompt - prompt for user. | |
457 | * pointer - pointer to default value, will be returned | |
458 | * as new value. | |
075fe5bb | 459 | * Returns: SUB_ERROR if break hit (^C). |
08345b74 | 460 | */ |
461 | ||
075fe5bb | 462 | int |
08345b74 | 463 | GetValueFromUser(prompt, pointer) |
461c03b6 | 464 | char * prompt, ** pointer; |
08345b74 | 465 | { |
466 | char buf[BUFSIZ]; | |
467 | ||
075fe5bb | 468 | if (PromptWithDefault(prompt, buf, BUFSIZ, *pointer) == -1) |
0a2c64cb | 469 | return(SUB_ERROR); |
075fe5bb | 470 | |
0a2c64cb | 471 | /* |
472 | * If these are the same then there is no need to allocate a new string. | |
473 | * | |
474 | * a difference that makes no difference, IS no difference. | |
475 | */ | |
476 | ||
477 | if (strcmp(buf, *pointer) != 0) { | |
478 | if (*pointer != NULL) | |
479 | free(*pointer); | |
480 | *pointer = Strsave(buf); | |
481 | } | |
482 | return(SUB_NORMAL); | |
075fe5bb | 483 | } |
484 | ||
485 | /* Function Name: GetYesNoValueFromUser | |
486 | * Description: This function gets a value from a user for the field | |
487 | * specified. | |
488 | * Arguments: prompt - prompt for user. | |
489 | * pointer - pointer to default value, will be returned | |
490 | * as new value. | |
491 | * Returns: SUB_ERROR if break hit (^C). | |
492 | */ | |
493 | ||
494 | int | |
495 | GetYesNoValueFromUser(prompt, pointer) | |
496 | char * prompt, ** pointer; | |
497 | { | |
498 | char user_prompt[BUFSIZ]; | |
499 | Bool default_val; | |
500 | ||
501 | if ( strcmp (*pointer, DEFAULT_YES) == 0 ) | |
502 | default_val = TRUE; | |
503 | else | |
504 | default_val = FALSE; | |
505 | ||
506 | sprintf(user_prompt, "%s (y/n)", prompt); | |
507 | ||
508 | switch (YesNoQuestion(user_prompt, default_val)) { | |
509 | case TRUE: | |
510 | if (*pointer != NULL) | |
511 | free(*pointer); | |
0a2c64cb | 512 | *pointer = Strsave(DEFAULT_YES); |
075fe5bb | 513 | break; |
514 | case FALSE: | |
515 | if (*pointer != NULL) | |
516 | free(*pointer); | |
0a2c64cb | 517 | *pointer = Strsave(DEFAULT_NO); |
075fe5bb | 518 | break; |
519 | case -1: | |
520 | default: | |
0a2c64cb | 521 | return(SUB_ERROR); |
075fe5bb | 522 | } |
0a2c64cb | 523 | return(SUB_NORMAL); |
075fe5bb | 524 | } |
525 | ||
526 | /* Function Name: GetFSVal | |
527 | * Description: asks about a specific filesystem value. | |
528 | * Arguments: name - string for this type of filesystem. | |
529 | * mask - mask for this type of filesystem. | |
530 | * current - current filesystem state. (for defaults). | |
531 | * new - new filesystem state. | |
532 | * Returns: TRUE if successful. | |
533 | */ | |
534 | ||
535 | static Bool | |
536 | GetFSVal(name, mask, current, new) | |
537 | char * name; | |
538 | int mask, current, *new; | |
539 | { | |
540 | char temp_buf[BUFSIZ]; | |
541 | sprintf(temp_buf, "Is this a %s filsystem", name); | |
542 | switch (YesNoQuestion(temp_buf, ( (mask & current) == mask) )) { | |
543 | case TRUE: | |
544 | *new |= mask; | |
545 | break; | |
546 | case FALSE: | |
547 | break; /* zero by default. */ | |
548 | default: | |
549 | return(FALSE); | |
550 | } | |
551 | return(TRUE); | |
552 | } | |
553 | ||
554 | /* Function Name: GetFSTypes | |
555 | * Description: Allows user to specify filsystem types. | |
556 | * Arguments: current - current value of filsystem, freed here. | |
557 | * Returns: SUB_ERROR on ^C. | |
558 | */ | |
559 | ||
560 | int | |
561 | GetFSTypes(current) | |
562 | char ** current; | |
563 | { | |
564 | int c_value, new_val = 0; /* current value of filesys type (int). */ | |
565 | char ret_value[BUFSIZ]; | |
566 | ||
567 | if (*current == NULL) | |
568 | c_value = 0; | |
569 | else | |
570 | c_value = atoi(*current); | |
571 | ||
572 | if (GetFSVal("student", SMS_FS_STUDENT, c_value, &new_val) == FALSE) | |
573 | return(SUB_ERROR); | |
574 | if (GetFSVal("faculty", SMS_FS_FACULTY, c_value, &new_val) == FALSE) | |
575 | return(SUB_ERROR); | |
576 | if (GetFSVal("staff", SMS_FS_STAFF, c_value, &new_val) == FALSE) | |
577 | return(SUB_ERROR); | |
578 | if (GetFSVal("miscellaneous", SMS_FS_MISC, c_value, &new_val) == FALSE) | |
579 | return(SUB_ERROR); | |
580 | ||
581 | FreeAndClear(current, TRUE); | |
582 | sprintf(ret_value, "%d", new_val); | |
583 | *current = Strsave(ret_value); | |
584 | return(SUB_NORMAL); | |
08345b74 | 585 | } |
586 | ||
08345b74 | 587 | /* Function Name: Strsave |
588 | * Description: save a string. | |
589 | * Arguments: string - the string to save. | |
590 | * Returns: The malloced string, now safely saved, or NULL. | |
591 | */ | |
592 | ||
593 | char * | |
594 | Strsave(str) | |
595 | char *str; | |
596 | { | |
597 | register char *newstr = malloc((unsigned) strlen(str) + 1); | |
598 | ||
599 | if (newstr == (char *) NULL) | |
600 | return ((char *) NULL); | |
601 | else | |
602 | return (strcpy(newstr, str)); | |
603 | } | |
604 | ||
08345b74 | 605 | /* Function Name: Print |
606 | * Description: prints out all the arguments on a single line. | |
607 | * Arguments: argc, argv - the standard SMS arguments. | |
608 | * callback - the callback function - NOT USED. | |
609 | * Returns: SMS_CONT | |
610 | */ | |
611 | ||
612 | /* ARGSUSED */ | |
613 | int | |
614 | Print(argc, argv, callback) | |
615 | int argc; | |
616 | char **argv, *callback; | |
617 | { | |
618 | char buf[BUFSIZ]; | |
461c03b6 | 619 | register int i; |
08345b74 | 620 | |
621 | found_some = TRUE; | |
622 | (void) strcpy(buf,argv[0]); /* no newline 'cause Put_message adds one */ | |
623 | for (i = 1; i < argc; i++) | |
624 | (void) sprintf(buf,"%s %s",buf,argv[i]); | |
625 | (void) Put_message(buf); | |
626 | ||
627 | return (SMS_CONT); | |
628 | } | |
629 | ||
630 | /* Function Name: PrintByType | |
631 | * Description: This function prints all members of the type specified | |
632 | * by the callback arg, unless the callback is NULL, in which | |
633 | * case it prints all members. | |
634 | * Arguments: argc, argc - normal arguments for sms_callback function. | |
635 | * callback - either a type of member or NULL. | |
636 | * Returns: SMS_CONT or SMS_QUIT. | |
637 | */ | |
638 | ||
639 | /*ARGSUSED*/ | |
640 | int | |
641 | PrintByType(argc, argv, callback) | |
642 | int argc; | |
643 | char **argv, *callback; | |
644 | { | |
645 | if (callback == NULL) | |
646 | return( Print(argc, argv, callback) ); | |
647 | if (strcmp(argv[0], callback) == 0) | |
0a2c64cb | 648 | return( Print(argc, argv, callback) ); |
08345b74 | 649 | return(SMS_CONT); |
650 | } | |
651 | ||
652 | /* Function Name: PrintHelp | |
653 | * Description: Prints Help Information in a NULL terminated | |
654 | * char **. | |
655 | * Arguments: message. | |
656 | * Returns: DM_NORMAL. | |
657 | */ | |
658 | ||
659 | int | |
461c03b6 | 660 | PrintHelp(message) |
08345b74 | 661 | char ** message; |
662 | { | |
a9517c80 | 663 | register int i; |
664 | ||
665 | for (i = 0; i < CountArgs(message); i++) | |
666 | Put_message(message[i]); | |
667 | ||
08345b74 | 668 | return(DM_NORMAL); |
669 | } | |
670 | ||
402461ad | 671 | /* Function Name: Loop |
672 | * Description: This function goes through the entire queue, and | |
673 | * and executes the given function on each element. | |
674 | * Arguments: elem - top element of the queue. | |
675 | * func - the function to execute. | |
676 | * Returns: none. | |
677 | */ | |
678 | ||
679 | void | |
680 | Loop(elem, func) | |
681 | FVoid func; | |
682 | struct qelem * elem; | |
683 | { | |
684 | while (elem != NULL) { | |
685 | char ** info = (char **) elem->q_data; | |
686 | (*func) (info); | |
687 | elem = elem->q_forw; | |
688 | } | |
689 | } | |
690 | ||
691 | ||
692 | /* Function Name: QueryLoop | |
693 | * Description: This functions loops through a queue containing | |
694 | * information about some item that we want to perform | |
695 | * an operation on, and then calls the correct routine | |
696 | * perform that operation. | |
697 | * Arguments: top - top of the queue of information. | |
698 | * print_func - print function. | |
699 | * op_function - operation to be performed. | |
700 | * query_string - string the prompts the user whether or not | |
701 | * to perform this operation. | |
702 | * Returns: none. | |
703 | * NOTES: | |
704 | * print_opt - should expect one arguent, the info array | |
705 | * of char *'s. | |
706 | * is expected to return the name of the item. | |
707 | * op_func - should expect two arguments. | |
708 | * 1) the info array of char *'s. | |
709 | * 2) a boolean the is true if there only | |
710 | * one item in this queue, used for delete | |
711 | * confirmation. | |
712 | * query_string - this should be of such a form that when the | |
713 | * name of the object and '(y/n/q) ?' are appended | |
714 | * then it should still make sense, an example is | |
715 | * "Delete the list" | |
716 | */ | |
717 | ||
718 | void | |
719 | QueryLoop(elem, print_func, op_func, query_string) | |
720 | struct qelem *elem; | |
721 | FVoid op_func; | |
722 | FCharStar print_func; | |
723 | char * query_string; | |
724 | { | |
725 | Bool one_item; | |
726 | char temp_buf[BUFSIZ], *name; | |
727 | ||
728 | one_item = (QueueCount(elem) == 1); | |
729 | while (elem != NULL) { | |
730 | char **info = (char **) elem->q_data; | |
731 | ||
732 | if (one_item) | |
733 | (*op_func) (info, one_item); | |
734 | else { | |
735 | name = (*print_func) (info); /* call print function. */ | |
0a2c64cb | 736 | sprintf(temp_buf,"%s %s (y/n/q)", query_string, name); |
402461ad | 737 | switch(YesNoQuitQuestion(temp_buf, FALSE)) { |
738 | case TRUE: | |
739 | (*op_func) (info, one_item); | |
740 | break; | |
741 | case FALSE: | |
742 | break; | |
743 | default: /* Quit. */ | |
744 | Put_message("Aborting..."); | |
745 | return; | |
746 | } | |
747 | } | |
748 | elem = elem->q_forw; | |
749 | } | |
750 | } | |
751 | ||
752 | /* Function Name: NullPrint | |
753 | * Description: print function that returns nothing. | |
754 | * Arguments: info - a pointer to the info array - Not used. | |
755 | * Returns: none. | |
756 | */ | |
757 | ||
758 | char * | |
759 | NullPrint(info) | |
760 | char ** info; | |
761 | { | |
762 | return(info[NAME]); | |
763 | } | |
764 | ||
6769d5bc | 765 | |
766 | /* Function Name: GetTypeValues | |
767 | * Description: gets legal values for a typed object, keeping a cache | |
768 | * Arguments: type name | |
769 | * Returns: argv of values | |
770 | */ | |
771 | ||
772 | struct qelem * | |
773 | GetTypeValues(tname) | |
774 | char *tname; | |
775 | { | |
776 | int stat; | |
777 | char *argv[3], *p, **pp, *strsave(); | |
778 | struct qelem *elem, *oelem; | |
779 | static struct qelem *cache = NULL; | |
780 | struct cache_elem { char *cache_name; struct qelem *cache_data; } *ce; | |
781 | ||
782 | for (elem = cache; elem; elem = elem->q_forw) { | |
783 | ce = (struct cache_elem *)elem->q_data; | |
784 | if (!strcmp(ce->cache_name, tname)) | |
785 | return(ce->cache_data); | |
786 | } | |
787 | ||
788 | argv[0] = tname; | |
789 | argv[1] = "TYPE"; | |
790 | argv[2] = "*"; | |
791 | elem = NULL; | |
a47906f2 | 792 | if (stat = do_sms_query("get_alias", 3, argv, StoreInfo, (char *)&elem)) { |
6769d5bc | 793 | com_err(program_name, stat, " in GetTypeValues"); |
794 | return(NULL); | |
795 | } | |
796 | oelem = elem; | |
797 | for (elem = QueueTop(elem); elem; elem = elem->q_forw) { | |
798 | pp = (char **) elem->q_data; | |
799 | p = strsave(pp[2]); | |
800 | FreeInfo(pp); | |
801 | elem->q_data = p; | |
802 | } | |
803 | elem = (struct qelem *) malloc(sizeof(struct qelem)); | |
804 | ce = (struct cache_elem *) malloc(sizeof(struct cache_elem)); | |
805 | ce->cache_name = tname; | |
806 | ce->cache_data = QueueTop(oelem); | |
807 | elem->q_data = (char *)ce; | |
808 | AddQueue(elem, cache); | |
809 | cache = QueueTop(elem); | |
810 | return(ce->cache_data); | |
811 | } | |
812 | ||
813 | ||
814 | /* Function Name: GetTypeFromUser | |
815 | * Description: gets a typed value from the user | |
816 | * Arguments: prompt string, type name, buffer pointer | |
817 | * Returns: | |
818 | */ | |
819 | ||
820 | GetTypeFromUser(prompt, tname, pointer) | |
821 | char *prompt; | |
822 | char *tname; | |
823 | char **pointer; | |
824 | { | |
825 | char def[BUFSIZ], buffer[BUFSIZ], *p, *argv[3]; | |
826 | struct qelem *elem; | |
827 | int stat; | |
828 | ||
829 | strcpy(def, *pointer); | |
830 | strcpy(buffer, prompt); | |
831 | strcat(buffer, " ("); | |
832 | for (elem = GetTypeValues(tname); elem; elem = elem->q_forw) { | |
833 | strcat(buffer, elem->q_data); | |
834 | if (elem->q_forw) | |
835 | strcat(buffer, ", "); | |
836 | } | |
837 | strcat(buffer, ")"); | |
838 | if (strlen(buffer) > 64) | |
839 | sprintf(buffer, "%s (? for help)", prompt); | |
840 | GetValueFromUser(buffer, pointer); | |
841 | if (**pointer == '?') { | |
842 | sprintf(buffer, "Type %s is one of:", tname); | |
843 | Put_message(buffer); | |
844 | for (elem = GetTypeValues(tname); elem; elem = elem->q_forw) { | |
845 | Put_message(elem->q_data); | |
846 | } | |
847 | *pointer = strsave(def); | |
848 | return(GetTypeFromUser(prompt, tname, pointer)); | |
849 | } | |
850 | for (elem = GetTypeValues(tname); elem; elem = elem->q_forw) { | |
851 | if (!cistrcmp(elem->q_data, *pointer)) | |
852 | return(SUB_NORMAL); | |
853 | } | |
854 | sprintf(buffer, "\"%s\" is not a legal value for %s. Use one of:", | |
855 | *pointer, tname); | |
856 | Put_message(buffer); | |
857 | for (elem = GetTypeValues(tname); elem; elem = elem->q_forw) { | |
858 | Put_message(elem->q_data); | |
859 | } | |
860 | sprintf(buffer, "Are you sure you want \"%s\" to be a legal %s", | |
861 | *pointer, tname); | |
862 | if (YesNoQuestion("Do you want this to be a new legal value", 0) && | |
863 | YesNoQuestion(buffer, 0)) { | |
864 | argv[0] = tname; | |
865 | argv[1] = "TYPE"; | |
866 | argv[2] = *pointer; | |
867 | for (p = argv[2]; *p; p++) | |
868 | if (islower(*p)) | |
869 | *p = toupper(*p); | |
a47906f2 | 870 | if (stat = do_sms_query("add_alias", 3, argv, Scream, NULL)) { |
6769d5bc | 871 | com_err(program_name, stat, " in add_alias"); |
872 | } else { | |
873 | elem = (struct qelem *) malloc(sizeof(struct qelem)); | |
874 | elem->q_data = strsave(*pointer); | |
875 | AddQueue(elem, GetTypeValues(tname)); | |
876 | Put_message("Done."); | |
877 | } | |
878 | } | |
879 | *pointer = strsave(def); | |
880 | return(GetTypeFromUser(prompt, tname, pointer)); | |
881 | } | |
882 | ||
883 | ||
198d5016 | 884 | do_sms_query(name, argc, argv, proc, hint) |
885 | char *name; | |
886 | int argc; | |
887 | char **argv; | |
888 | int (*proc)(); | |
889 | char *hint; | |
890 | { | |
891 | int status; | |
892 | extern char *whoami; | |
893 | ||
894 | status = sms_query(name, argc, argv, proc, hint); | |
a47906f2 | 895 | if (status != SMS_ABORTED && status != SMS_NOT_CONNECTED) |
198d5016 | 896 | return(status); |
9a2d61b0 | 897 | status = sms_connect(SMS_SERVER); |
198d5016 | 898 | if (status) { |
899 | com_err(whoami, status, " while re-connecting to server"); | |
900 | return(SMS_ABORTED); | |
901 | } | |
902 | status = sms_auth(whoami); | |
903 | if (status) { | |
904 | com_err(whoami, status, " while re-authenticating to server"); | |
a47906f2 | 905 | sms_disconnect(); |
198d5016 | 906 | return(SMS_ABORTED); |
907 | } | |
908 | status = sms_query(name, argc, argv, proc, hint); | |
909 | return(status); | |
910 | } | |
911 |