]>
Commit | Line | Data |
---|---|---|
402461ad | 1 | #if (!defined(lint) && !defined(SABER)) |
08345b74 | 2 | static char rcsid_module_c[] = "$Header$"; |
3 | #endif lint | |
4 | ||
8defc06b | 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. | |
0a2c64cb | 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> | |
8defc06b | 24 | #include <moira.h> |
25 | #include <moira_site.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 | ||
70ea6aa8 | 72 | |
73 | /* CopyInfo: allocates memory for a copy of a NULL terminated array of | |
74 | * strings <and returns a pointer to the copy. | |
75 | */ | |
76 | ||
77 | char **CopyInfo(info) | |
78 | char **info; | |
79 | { | |
80 | char **ret; | |
81 | int i; | |
82 | ||
83 | ret = (char **) malloc(sizeof(char *) * (CountArgs(info) + 1)); | |
84 | if (ret == NULL) | |
85 | return(ret); | |
86 | for (i = 0; info[i]; i++) | |
87 | ret[i] = Strsave(info[i]); | |
88 | ret[i] = NULL; | |
89 | return(ret); | |
90 | } | |
91 | ||
92 | ||
08345b74 | 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. | |
96 | * Returns: none. | |
97 | */ | |
98 | ||
99 | void | |
100 | FreeInfo(info) | |
101 | char ** info; | |
102 | { | |
85ca828a | 103 | while (*info != NULL) |
104 | FreeAndClear(info++, TRUE); | |
08345b74 | 105 | } |
106 | ||
461c03b6 | 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. | |
111 | * Returns: none. | |
112 | */ | |
113 | ||
114 | void | |
115 | FreeAndClear(pointer, free_it) | |
116 | char ** pointer; | |
117 | Bool free_it; | |
118 | { | |
119 | if (*pointer == NULL) | |
120 | return; | |
121 | else if (free_it) | |
122 | free(*pointer); | |
123 | *pointer = NULL; | |
124 | } | |
125 | ||
126 | /* Function Name: QueueTop | |
127 | * Description: returns a qelem pointer that points to the top of | |
128 | * a queue. | |
129 | * Arguments: elem - any element of a queue. | |
130 | * Returns: top element of a queue. | |
131 | */ | |
132 | ||
133 | struct qelem * | |
134 | QueueTop(elem) | |
135 | struct qelem * elem; | |
136 | { | |
137 | if (elem == NULL) /* NULL returns NULL. */ | |
138 | return(NULL); | |
139 | while (elem->q_back != NULL) | |
140 | elem = elem->q_back; | |
141 | return(elem); | |
142 | } | |
143 | ||
08345b74 | 144 | /* Function Name: FreeQueueElem |
145 | * Description: Frees one element of the queue. | |
146 | * Arguments: elem - the elem to free. | |
147 | * Returns: none | |
148 | */ | |
149 | ||
0a2c64cb | 150 | static void |
08345b74 | 151 | FreeQueueElem(elem) |
461c03b6 | 152 | struct qelem * elem; |
08345b74 | 153 | { |
461c03b6 | 154 | char ** info = (char **) elem->q_data; |
155 | ||
156 | if (info != (char **) NULL) { | |
157 | FreeInfo( info ); /* free info fields */ | |
08345b74 | 158 | free(elem->q_data); /* free info array itself. */ |
159 | } | |
85ca828a | 160 | RemoveQueue(elem); /* remove this element from the queue */ |
08345b74 | 161 | free(elem); /* free its space. */ |
162 | } | |
163 | ||
164 | /* Function Name: FreeQueue | |
165 | * Description: Cleans up the queue | |
166 | * Arguments: elem - any element of the queue. | |
167 | * Returns: none. | |
168 | */ | |
169 | ||
170 | void | |
171 | FreeQueue(elem) | |
461c03b6 | 172 | struct qelem * elem; |
08345b74 | 173 | { |
461c03b6 | 174 | struct qelem *temp, *local = QueueTop(elem); |
08345b74 | 175 | |
176 | while(local != NULL) { | |
177 | temp = local->q_forw; | |
178 | FreeQueueElem(local); | |
179 | local = temp; | |
180 | } | |
181 | } | |
182 | ||
183 | /* Function Name: QueueCount | |
184 | * Description: Counts the number of elements in a queue | |
185 | * Arguments: elem - any element in the queue. | |
186 | * Returns: none. | |
187 | */ | |
188 | ||
189 | int | |
190 | QueueCount(elem) | |
191 | struct qelem * elem; | |
192 | { | |
193 | int count = 0; | |
194 | elem = QueueTop(elem); | |
195 | while (elem != NULL) { | |
196 | count ++; | |
197 | elem = elem->q_forw; | |
198 | } | |
199 | return(count); | |
200 | } | |
201 | ||
0a2c64cb | 202 | /* Function Name: StoreInfo |
8defc06b | 203 | * Description: Stores information from an moira query into a queue. |
0a2c64cb | 204 | * Arguments: argc, argv, - information returned from the query returned |
205 | * in argv. | |
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 | |
210 | * call. | |
8defc06b | 211 | * Returns: MR_CONT, or MR_ABORT if it has problems. |
0a2c64cb | 212 | */ |
213 | ||
08345b74 | 214 | int |
215 | StoreInfo(argc, argv, data) | |
216 | int argc; | |
217 | char ** argv; | |
461c03b6 | 218 | char * data; |
08345b74 | 219 | { |
402461ad | 220 | char ** info = (char **) malloc( MAX_ARGS_SIZE * sizeof(char *)); |
08345b74 | 221 | struct qelem ** old_elem = (struct qelem **) data; |
222 | struct qelem * new_elem = (struct qelem *) malloc (sizeof (struct qelem)); | |
223 | int count; | |
224 | ||
402461ad | 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; | |
8defc06b | 229 | return(MR_ABORT); |
402461ad | 230 | } |
231 | ||
08345b74 | 232 | for (count = 0; count < argc; count++) |
233 | info[count] = Strsave(argv[count]); | |
234 | info[count] = NULL; /* NULL terminate this sucker. */ | |
235 | ||
85ca828a | 236 | new_elem->q_data = (char *) info; |
237 | AddQueue(new_elem, *old_elem); | |
08345b74 | 238 | |
239 | *old_elem = new_elem; | |
8defc06b | 240 | return(MR_CONT); |
08345b74 | 241 | } |
242 | ||
243 | /* Function Name: CountArgs | |
244 | * Description: Retrieve the number of args in a null terminated | |
245 | * arglist. | |
246 | * Arguments: info - the argument list. | |
247 | * Returns: number if args in the list. | |
248 | */ | |
249 | ||
250 | int | |
461c03b6 | 251 | CountArgs(info) |
08345b74 | 252 | char ** info; |
253 | { | |
254 | int number = 0; | |
255 | ||
461c03b6 | 256 | while (*info != NULL) { |
08345b74 | 257 | number++; |
461c03b6 | 258 | info++; |
259 | } | |
08345b74 | 260 | |
261 | return(number); | |
262 | } | |
263 | ||
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. | |
267 | * Arguments: none | |
268 | * Returns: doesn't exit. | |
269 | */ | |
270 | ||
461c03b6 | 271 | int |
08345b74 | 272 | Scream() |
273 | { | |
461c03b6 | 274 | com_err(program_name, 0, |
95cd286e | 275 | "\nA Moira update returned a value -- programmer botch\n"); |
8defc06b | 276 | mr_disconnect(); |
08345b74 | 277 | exit(1); |
278 | } | |
279 | ||
08345b74 | 280 | /* Function Name: PromptWithDefault |
281 | * Description: allows a user to be prompted for input, and given a | |
282 | * default choice. | |
283 | * Arguments: prompt - the prompt string. | |
284 | * buf, buflen - buffer to be returned and its MAX size? | |
285 | * default value for the answer. | |
1feef067 | 286 | * Returns: zero on failure |
08345b74 | 287 | */ |
288 | ||
289 | int | |
290 | PromptWithDefault(prompt, buf, buflen, def) | |
291 | char *prompt, *buf; | |
292 | int buflen; | |
293 | char *def; | |
294 | { | |
295 | char tmp[BUFSIZ]; | |
296 | int ans; | |
297 | ||
1feef067 | 298 | if (parsed_argc > 0) { |
299 | parsed_argc--; | |
300 | strncpy(buf, parsed_argv[0], buflen); | |
301 | sprintf(tmp, "%s: %s", prompt, buf); | |
302 | Put_message(tmp); | |
303 | parsed_argv++; | |
304 | return(1); | |
305 | } | |
306 | ||
08345b74 | 307 | (void) sprintf(tmp, "%s [%s]: ", prompt, def ? def : ""); |
308 | ans = Prompt_input(tmp, buf, buflen); | |
309 | if (IS_EMPTY(buf)) | |
310 | (void) strcpy(buf, def); | |
0b6a715f | 311 | else if (!strcmp(buf, "\"\"")) |
312 | *buf = 0; | |
08345b74 | 313 | return(ans); |
314 | } | |
315 | ||
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 | |
322 | */ | |
323 | ||
324 | Bool | |
325 | YesNoQuestion(prompt, bool_def) | |
326 | char *prompt; | |
327 | int bool_def; | |
328 | { | |
3c1a8806 | 329 | char ans[2]; |
08345b74 | 330 | |
331 | while (TRUE) { | |
3c1a8806 | 332 | if (!PromptWithDefault(prompt, ans, 2, bool_def ? "y" : "n")) |
08345b74 | 333 | return(-1); |
3c1a8806 | 334 | switch (ans[0]) { |
08345b74 | 335 | case 'n': |
336 | case 'N': | |
337 | return(FALSE); | |
08345b74 | 338 | case 'y': |
339 | case 'Y': | |
340 | return(TRUE); | |
08345b74 | 341 | default: |
342 | Put_message("Please answer 'y' or 'n'."); | |
343 | break; | |
344 | } | |
345 | } | |
346 | } | |
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. | |
355 | */ | |
356 | ||
357 | Bool | |
461c03b6 | 358 | YesNoQuitQuestion(prompt, bool_def) |
08345b74 | 359 | char *prompt; |
360 | int bool_def; | |
361 | { | |
3c1a8806 | 362 | char ans[2]; |
08345b74 | 363 | |
364 | while (TRUE) { | |
3c1a8806 | 365 | if (!PromptWithDefault(prompt, ans, 2, bool_def ? "y" : "n")) |
08345b74 | 366 | return(-1); |
3c1a8806 | 367 | switch (ans[0]) { |
08345b74 | 368 | case 'n': |
369 | case 'N': | |
370 | return(FALSE); | |
08345b74 | 371 | case 'y': |
372 | case 'Y': | |
373 | return(TRUE); | |
08345b74 | 374 | case 'q': |
375 | case 'Q': | |
376 | return(-1); | |
08345b74 | 377 | default: |
378 | Put_message("Please answer 'y', 'n' or 'q'."); | |
379 | break; | |
380 | } | |
381 | } | |
382 | } | |
383 | ||
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. | |
389 | */ | |
390 | ||
0a2c64cb | 391 | Bool |
08345b74 | 392 | Confirm(prompt) |
393 | char * prompt; | |
394 | { | |
0a2c64cb | 395 | return( !verbose || (YesNoQuestion(prompt,FALSE) == TRUE) ); |
08345b74 | 396 | } |
397 | ||
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. | |
402 | */ | |
403 | ||
404 | Bool | |
405 | ValidName(s) | |
0a2c64cb | 406 | char *s; |
08345b74 | 407 | { |
408 | if (IS_EMPTY(s)) | |
409 | Put_message("Please use a non-empty name."); | |
410 | else if (index(s, ' ')) | |
411 | Put_message("You cannot use space (' ') in this name."); | |
e7296211 | 412 | else if (index(s, '*') || index(s, '?') || index(s, '[')) |
402461ad | 413 | Put_message("Wildcards not accepted here."); |
08345b74 | 414 | else |
415 | return TRUE; | |
416 | return FALSE; | |
417 | } | |
418 | ||
419 | /* Function Name: ToggleVerboseMode | |
420 | * Description: This function toggles the verbose mode. | |
421 | * Arguments: none | |
422 | * Returns: DM_NORMAL. | |
423 | */ | |
424 | ||
425 | int | |
426 | ToggleVerboseMode() | |
427 | { | |
428 | ||
85ca828a | 429 | verbose = !verbose; |
08345b74 | 430 | |
461c03b6 | 431 | if (verbose) |
432 | Put_message("Delete functions will first confirm\n"); | |
433 | else | |
434 | Put_message("Delete functions will be silent\n"); | |
435 | ||
08345b74 | 436 | return(DM_NORMAL); |
437 | } | |
438 | ||
439 | /* Function Name: NullFunc | |
440 | * Description: dummy callback routine | |
441 | * Arguments: none | |
8defc06b | 442 | * Returns: MR_CONT |
08345b74 | 443 | */ |
444 | ||
445 | int | |
446 | NullFunc() | |
447 | { | |
8defc06b | 448 | return(MR_CONT); |
08345b74 | 449 | } |
450 | ||
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. | |
85ca828a | 457 | * NOTE: This screws up the numbers of the elements of the array in a |
08345b74 | 458 | * big way. |
459 | */ | |
460 | ||
0a2c64cb | 461 | void |
461c03b6 | 462 | SlipInNewName(info, name) |
08345b74 | 463 | char ** info; |
08345b74 | 464 | char * name; |
465 | { | |
466 | register int i; | |
467 | ||
468 | /* This also pushes the NULL down. */ | |
af7df632 | 469 | for (i = CountArgs(info); i > 0; i--) { |
470 | info[i+1] = info[i]; | |
08345b74 | 471 | } |
85ca828a | 472 | info[1] = name; /* now slip in the name. */ |
08345b74 | 473 | } |
474 | ||
08345b74 | 475 | /* Function Name: GetValueFromUser |
476 | * Description: This function gets a value from a user for the field | |
477 | * specified. | |
478 | * Arguments: prompt - prompt for user. | |
479 | * pointer - pointer to default value, will be returned | |
480 | * as new value. | |
075fe5bb | 481 | * Returns: SUB_ERROR if break hit (^C). |
08345b74 | 482 | */ |
483 | ||
075fe5bb | 484 | int |
08345b74 | 485 | GetValueFromUser(prompt, pointer) |
461c03b6 | 486 | char * prompt, ** pointer; |
08345b74 | 487 | { |
488 | char buf[BUFSIZ]; | |
489 | ||
1feef067 | 490 | if (PromptWithDefault(prompt, buf, BUFSIZ, *pointer) == 0) |
0a2c64cb | 491 | return(SUB_ERROR); |
075fe5bb | 492 | |
0a2c64cb | 493 | /* |
494 | * If these are the same then there is no need to allocate a new string. | |
495 | * | |
496 | * a difference that makes no difference, IS no difference. | |
497 | */ | |
498 | ||
499 | if (strcmp(buf, *pointer) != 0) { | |
500 | if (*pointer != NULL) | |
501 | free(*pointer); | |
502 | *pointer = Strsave(buf); | |
503 | } | |
504 | return(SUB_NORMAL); | |
075fe5bb | 505 | } |
506 | ||
507 | /* Function Name: GetYesNoValueFromUser | |
508 | * Description: This function gets a value from a user for the field | |
509 | * specified. | |
510 | * Arguments: prompt - prompt for user. | |
511 | * pointer - pointer to default value, will be returned | |
512 | * as new value. | |
513 | * Returns: SUB_ERROR if break hit (^C). | |
514 | */ | |
515 | ||
516 | int | |
517 | GetYesNoValueFromUser(prompt, pointer) | |
518 | char * prompt, ** pointer; | |
519 | { | |
520 | char user_prompt[BUFSIZ]; | |
521 | Bool default_val; | |
522 | ||
523 | if ( strcmp (*pointer, DEFAULT_YES) == 0 ) | |
524 | default_val = TRUE; | |
525 | else | |
526 | default_val = FALSE; | |
527 | ||
528 | sprintf(user_prompt, "%s (y/n)", prompt); | |
529 | ||
530 | switch (YesNoQuestion(user_prompt, default_val)) { | |
531 | case TRUE: | |
532 | if (*pointer != NULL) | |
533 | free(*pointer); | |
0a2c64cb | 534 | *pointer = Strsave(DEFAULT_YES); |
075fe5bb | 535 | break; |
536 | case FALSE: | |
537 | if (*pointer != NULL) | |
538 | free(*pointer); | |
0a2c64cb | 539 | *pointer = Strsave(DEFAULT_NO); |
075fe5bb | 540 | break; |
541 | case -1: | |
542 | default: | |
0a2c64cb | 543 | return(SUB_ERROR); |
075fe5bb | 544 | } |
0a2c64cb | 545 | return(SUB_NORMAL); |
075fe5bb | 546 | } |
547 | ||
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. | |
555 | */ | |
556 | ||
557 | static Bool | |
558 | GetFSVal(name, mask, current, new) | |
559 | char * name; | |
560 | int mask, current, *new; | |
561 | { | |
562 | char temp_buf[BUFSIZ]; | |
563 | sprintf(temp_buf, "Is this a %s filsystem", name); | |
564 | switch (YesNoQuestion(temp_buf, ( (mask & current) == mask) )) { | |
565 | case TRUE: | |
566 | *new |= mask; | |
567 | break; | |
568 | case FALSE: | |
569 | break; /* zero by default. */ | |
570 | default: | |
571 | return(FALSE); | |
572 | } | |
573 | return(TRUE); | |
574 | } | |
575 | ||
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. | |
580 | */ | |
581 | ||
582 | int | |
1feef067 | 583 | GetFSTypes(current, options) |
075fe5bb | 584 | char ** current; |
1feef067 | 585 | int options; |
075fe5bb | 586 | { |
587 | int c_value, new_val = 0; /* current value of filesys type (int). */ | |
588 | char ret_value[BUFSIZ]; | |
589 | ||
590 | if (*current == NULL) | |
591 | c_value = 0; | |
592 | else | |
593 | c_value = atoi(*current); | |
594 | ||
8defc06b | 595 | if (GetFSVal("student", MR_FS_STUDENT, c_value, &new_val) == FALSE) |
075fe5bb | 596 | return(SUB_ERROR); |
8defc06b | 597 | if (GetFSVal("faculty", MR_FS_FACULTY, c_value, &new_val) == FALSE) |
075fe5bb | 598 | return(SUB_ERROR); |
8defc06b | 599 | if (GetFSVal("staff", MR_FS_STAFF, c_value, &new_val) == FALSE) |
075fe5bb | 600 | return(SUB_ERROR); |
8defc06b | 601 | if (GetFSVal("miscellaneous", MR_FS_MISC, c_value, &new_val) == FALSE) |
075fe5bb | 602 | return(SUB_ERROR); |
1feef067 | 603 | if (options) { |
604 | if (GetFSVal("Group Quotas", MR_FS_GROUPQUOTA, c_value, &new_val) == | |
605 | FALSE) | |
606 | return(SUB_ERROR); | |
607 | } | |
075fe5bb | 608 | |
609 | FreeAndClear(current, TRUE); | |
610 | sprintf(ret_value, "%d", new_val); | |
611 | *current = Strsave(ret_value); | |
612 | return(SUB_NORMAL); | |
08345b74 | 613 | } |
614 | ||
08345b74 | 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. | |
619 | */ | |
620 | ||
621 | char * | |
622 | Strsave(str) | |
623 | char *str; | |
624 | { | |
625 | register char *newstr = malloc((unsigned) strlen(str) + 1); | |
626 | ||
627 | if (newstr == (char *) NULL) | |
628 | return ((char *) NULL); | |
629 | else | |
630 | return (strcpy(newstr, str)); | |
631 | } | |
632 | ||
08345b74 | 633 | /* Function Name: Print |
634 | * Description: prints out all the arguments on a single line. | |
8defc06b | 635 | * Arguments: argc, argv - the standard MR arguments. |
08345b74 | 636 | * callback - the callback function - NOT USED. |
8defc06b | 637 | * Returns: MR_CONT |
08345b74 | 638 | */ |
639 | ||
640 | /* ARGSUSED */ | |
641 | int | |
642 | Print(argc, argv, callback) | |
643 | int argc; | |
644 | char **argv, *callback; | |
645 | { | |
646 | char buf[BUFSIZ]; | |
461c03b6 | 647 | register int i; |
08345b74 | 648 | |
649 | found_some = TRUE; | |
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); | |
654 | ||
8defc06b | 655 | return (MR_CONT); |
08345b74 | 656 | } |
657 | ||
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. | |
8defc06b | 662 | * Arguments: argc, argc - normal arguments for mr_callback function. |
08345b74 | 663 | * callback - either a type of member or NULL. |
8defc06b | 664 | * Returns: MR_CONT or MR_QUIT. |
08345b74 | 665 | */ |
666 | ||
667 | /*ARGSUSED*/ | |
668 | int | |
669 | PrintByType(argc, argv, callback) | |
670 | int argc; | |
671 | char **argv, *callback; | |
672 | { | |
673 | if (callback == NULL) | |
674 | return( Print(argc, argv, callback) ); | |
675 | if (strcmp(argv[0], callback) == 0) | |
0a2c64cb | 676 | return( Print(argc, argv, callback) ); |
8defc06b | 677 | return(MR_CONT); |
08345b74 | 678 | } |
679 | ||
680 | /* Function Name: PrintHelp | |
681 | * Description: Prints Help Information in a NULL terminated | |
682 | * char **. | |
683 | * Arguments: message. | |
684 | * Returns: DM_NORMAL. | |
685 | */ | |
686 | ||
687 | int | |
461c03b6 | 688 | PrintHelp(message) |
08345b74 | 689 | char ** message; |
690 | { | |
a9517c80 | 691 | register int i; |
692 | ||
693 | for (i = 0; i < CountArgs(message); i++) | |
694 | Put_message(message[i]); | |
695 | ||
08345b74 | 696 | return(DM_NORMAL); |
697 | } | |
698 | ||
402461ad | 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. | |
704 | * Returns: none. | |
705 | */ | |
706 | ||
707 | void | |
708 | Loop(elem, func) | |
709 | FVoid func; | |
710 | struct qelem * elem; | |
711 | { | |
712 | while (elem != NULL) { | |
713 | char ** info = (char **) elem->q_data; | |
714 | (*func) (info); | |
715 | elem = elem->q_forw; | |
716 | } | |
717 | } | |
718 | ||
719 | ||
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. | |
730 | * Returns: none. | |
731 | * NOTES: | |
732 | * print_opt - should expect one arguent, the info array | |
733 | * of char *'s. | |
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 | |
739 | * confirmation. | |
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 | |
743 | * "Delete the list" | |
744 | */ | |
745 | ||
746 | void | |
747 | QueryLoop(elem, print_func, op_func, query_string) | |
748 | struct qelem *elem; | |
749 | FVoid op_func; | |
750 | FCharStar print_func; | |
751 | char * query_string; | |
752 | { | |
753 | Bool one_item; | |
754 | char temp_buf[BUFSIZ], *name; | |
755 | ||
fd18d8ea | 756 | elem = QueueTop(elem); |
402461ad | 757 | one_item = (QueueCount(elem) == 1); |
758 | while (elem != NULL) { | |
759 | char **info = (char **) elem->q_data; | |
760 | ||
761 | if (one_item) | |
762 | (*op_func) (info, one_item); | |
763 | else { | |
764 | name = (*print_func) (info); /* call print function. */ | |
0a2c64cb | 765 | sprintf(temp_buf,"%s %s (y/n/q)", query_string, name); |
402461ad | 766 | switch(YesNoQuitQuestion(temp_buf, FALSE)) { |
767 | case TRUE: | |
768 | (*op_func) (info, one_item); | |
769 | break; | |
770 | case FALSE: | |
771 | break; | |
772 | default: /* Quit. */ | |
773 | Put_message("Aborting..."); | |
774 | return; | |
775 | } | |
776 | } | |
777 | elem = elem->q_forw; | |
778 | } | |
779 | } | |
780 | ||
781 | /* Function Name: NullPrint | |
782 | * Description: print function that returns nothing. | |
783 | * Arguments: info - a pointer to the info array - Not used. | |
784 | * Returns: none. | |
785 | */ | |
786 | ||
787 | char * | |
788 | NullPrint(info) | |
789 | char ** info; | |
790 | { | |
791 | return(info[NAME]); | |
792 | } | |
793 | ||
6769d5bc | 794 | |
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 | |
799 | */ | |
800 | ||
801 | struct qelem * | |
802 | GetTypeValues(tname) | |
803 | char *tname; | |
804 | { | |
805 | int stat; | |
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; | |
810 | ||
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); | |
815 | } | |
816 | ||
817 | argv[0] = tname; | |
818 | argv[1] = "TYPE"; | |
819 | argv[2] = "*"; | |
820 | elem = NULL; | |
8defc06b | 821 | if (stat = do_mr_query("get_alias", 3, argv, StoreInfo, (char *)&elem)) { |
6769d5bc | 822 | com_err(program_name, stat, " in GetTypeValues"); |
823 | return(NULL); | |
824 | } | |
825 | oelem = elem; | |
826 | for (elem = QueueTop(elem); elem; elem = elem->q_forw) { | |
827 | pp = (char **) elem->q_data; | |
828 | p = strsave(pp[2]); | |
829 | FreeInfo(pp); | |
830 | elem->q_data = p; | |
831 | } | |
832 | elem = (struct qelem *) malloc(sizeof(struct qelem)); | |
833 | ce = (struct cache_elem *) malloc(sizeof(struct cache_elem)); | |
d1641b6c | 834 | ce->cache_name = strsave(tname); |
6769d5bc | 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); | |
840 | } | |
841 | ||
842 | ||
843 | /* Function Name: GetTypeFromUser | |
844 | * Description: gets a typed value from the user | |
845 | * Arguments: prompt string, type name, buffer pointer | |
245b2337 | 846 | * Returns: SUB_ERROR if ^C, SUB_NORMAL otherwise |
6769d5bc | 847 | */ |
848 | ||
245b2337 | 849 | int GetTypeFromUser(prompt, tname, pointer) |
6769d5bc | 850 | char *prompt; |
851 | char *tname; | |
852 | char **pointer; | |
853 | { | |
854 | char def[BUFSIZ], buffer[BUFSIZ], *p, *argv[3]; | |
855 | struct qelem *elem; | |
856 | int stat; | |
857 | ||
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); | |
863 | if (elem->q_forw) | |
864 | strcat(buffer, ", "); | |
865 | } | |
866 | strcat(buffer, ")"); | |
867 | if (strlen(buffer) > 64) | |
868 | sprintf(buffer, "%s (? for help)", prompt); | |
245b2337 | 869 | if (GetValueFromUser(buffer, pointer) == SUB_ERROR) |
870 | return(SUB_ERROR); | |
6769d5bc | 871 | if (**pointer == '?') { |
872 | sprintf(buffer, "Type %s is one of:", tname); | |
873 | Put_message(buffer); | |
874 | for (elem = GetTypeValues(tname); elem; elem = elem->q_forw) { | |
875 | Put_message(elem->q_data); | |
876 | } | |
877 | *pointer = strsave(def); | |
878 | return(GetTypeFromUser(prompt, tname, pointer)); | |
879 | } | |
880 | for (elem = GetTypeValues(tname); elem; elem = elem->q_forw) { | |
7665ed7a | 881 | if (!strcasecmp(elem->q_data, *pointer)) { |
1feef067 | 882 | strcpy(*pointer, elem->q_data); |
6769d5bc | 883 | return(SUB_NORMAL); |
1feef067 | 884 | } |
6769d5bc | 885 | } |
886 | sprintf(buffer, "\"%s\" is not a legal value for %s. Use one of:", | |
887 | *pointer, tname); | |
888 | Put_message(buffer); | |
889 | for (elem = GetTypeValues(tname); elem; elem = elem->q_forw) { | |
890 | Put_message(elem->q_data); | |
891 | } | |
892 | sprintf(buffer, "Are you sure you want \"%s\" to be a legal %s", | |
893 | *pointer, tname); | |
245b2337 | 894 | if (YesNoQuestion("Do you want this to be a new legal value", 0) == TRUE && |
895 | YesNoQuestion(buffer, 0) == TRUE) { | |
6769d5bc | 896 | argv[0] = tname; |
897 | argv[1] = "TYPE"; | |
898 | argv[2] = *pointer; | |
d1641b6c | 899 | /* don't uppercase access flags. Do uppercase everything else */ |
900 | if (strncmp(tname, "fs_access", 9)) | |
901 | for (p = argv[2]; *p; p++) | |
6769d5bc | 902 | if (islower(*p)) |
d1641b6c | 903 | *p = toupper(*p); |
8defc06b | 904 | if (stat = do_mr_query("add_alias", 3, argv, Scream, NULL)) { |
6769d5bc | 905 | com_err(program_name, stat, " in add_alias"); |
906 | } else { | |
907 | elem = (struct qelem *) malloc(sizeof(struct qelem)); | |
908 | elem->q_data = strsave(*pointer); | |
909 | AddQueue(elem, GetTypeValues(tname)); | |
910 | Put_message("Done."); | |
911 | } | |
912 | } | |
913 | *pointer = strsave(def); | |
914 | return(GetTypeFromUser(prompt, tname, pointer)); | |
915 | } | |
916 | ||
917 | ||
8defc06b | 918 | do_mr_query(name, argc, argv, proc, hint) |
198d5016 | 919 | char *name; |
920 | int argc; | |
921 | char **argv; | |
922 | int (*proc)(); | |
923 | char *hint; | |
924 | { | |
925 | int status; | |
25ce6f7d | 926 | extern char *whoami, *moira_server; |
198d5016 | 927 | |
d32f483b | 928 | refresh_screen(); |
8defc06b | 929 | status = mr_query(name, argc, argv, proc, hint); |
930 | if (status != MR_ABORTED && status != MR_NOT_CONNECTED) | |
198d5016 | 931 | return(status); |
8defc06b | 932 | status = mr_connect(moira_server); |
198d5016 | 933 | if (status) { |
95cd286e | 934 | com_err(whoami, status, " while re-connecting to server %s", |
935 | moira_server); | |
8defc06b | 936 | return(MR_ABORTED); |
198d5016 | 937 | } |
8defc06b | 938 | status = mr_auth(whoami); |
198d5016 | 939 | if (status) { |
95cd286e | 940 | com_err(whoami, status, " while re-authenticating to server %s", |
941 | moira_server); | |
8defc06b | 942 | mr_disconnect(); |
943 | return(MR_ABORTED); | |
198d5016 | 944 | } |
8defc06b | 945 | status = mr_query(name, argc, argv, proc, hint); |
198d5016 | 946 | return(status); |
947 | } | |
948 |