3 * This is the file printer.c for the Moira Client, which allows users
4 * to quickly and easily maintain most parts of the Moira database.
5 * It Contains: Functions for handling the printers.
10 * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology.
11 * For copying and distribution information, please see the file
15 #include <mit-copyright.h>
17 #include <moira_site.h>
28 void RealDeletePcap(char **info, Bool one_item);
29 void ChangePcap(char **info, Bool one_item);
30 void ChangePalladium(char **info, Bool one_item);
31 void RealDeletePalladium(char **info, Bool one_item);
33 #define DEFAULT_MACHINE "E40-PRINT-SERVER-1.MIT.EDU"
35 /* Function Name: SetDefaults
36 * Description: sets the default values for filesystem additions.
37 * Arguments: info - an array of char pointers to recieve defaults.
38 * Returns: char ** (this array, now filled).
41 static char **SetDefaults(char **info, char *name)
45 strcpy(spool_dir, "/usr/spool/printer/");
46 strcat(spool_dir, name);
48 info[PCAP_NAME] = strdup(name);
49 info[PCAP_SPOOL_HOST] = strdup(DEFAULT_MACHINE);
50 info[PCAP_SPOOL_DIR] = strdup(spool_dir);
51 info[PCAP_RPRINTER] = strdup(name);
52 info[PCAP_QSERVER] = strdup("[NONE]");
53 info[PCAP_AUTH] = strdup("1");
54 info[PCAP_PRICE] = strdup("10");
55 info[PCAP_COMMENTS] = strdup("");
56 info[PCAP_MODTIME] = info[PCAP_MODBY] = info[PCAP_MODWITH] = NULL;
58 info[PCAP_END] = NULL;
62 /* Function Name: GetPcapInfo
63 * Description: Stores the info in a queue.
64 * Arguments: name - name of the item to get information on.
65 * Returns: a pointer to the first element in the queue or null
66 * if printer not found.
69 static struct mqelem *GetPcapInfo(char *name)
72 struct mqelem *elem = NULL;
74 if ((stat = do_mr_query("get_printcap_entry", 1, &name, StoreInfo, &elem)))
76 com_err(program_name, stat, " in GetPcapInfo");
79 return QueueTop(elem);
82 /* Function Name: PrintPcapInfo
83 * Description: Yet another specialized print function.
84 * Arguments: info - all info about this Printer.
85 * Returns: printer name
88 static char *PrintPcapInfo(char **info)
92 if (!info) /* If no informaion */
94 Put_message("PrintPcapInfo called with null info!");
97 sprintf(buf, "Printer: %-35s Spool host: %s", info[PCAP_NAME],
98 info[PCAP_SPOOL_HOST]);
100 sprintf(buf, "Spool directory: %-27s Remote Printer Name: %s",
101 info[PCAP_SPOOL_DIR], info[PCAP_RPRINTER]);
103 sprintf(buf, "Authentication: %-3s Price/page: %-3s Quota Server: %s",
104 atoi(info[PCAP_AUTH]) ? "yes" : "no",
105 info[PCAP_PRICE], info[PCAP_QSERVER]);
107 sprintf(buf, "Comments: %s", info[PCAP_COMMENTS]);
109 sprintf(buf, MOD_FORMAT, info[PCAP_MODBY], info[PCAP_MODTIME],
113 return info[PCAP_NAME];
116 /* Function Name: AskPcapInfo.
117 * Description: This function askes the user for information about a
118 * printer and saves it into a structure.
119 * Arguments: info - a pointer the the structure to put the
124 static char **AskPcapInfo(char **info)
126 char temp_buf[BUFSIZ];
129 sprintf(temp_buf, "Printcap entry for %s.", info[PCAP_NAME]);
130 Put_message(temp_buf);
133 if (GetValueFromUser("Printer Server", &info[PCAP_SPOOL_HOST]) == SUB_ERROR)
135 info[PCAP_SPOOL_HOST] = canonicalize_hostname(info[PCAP_SPOOL_HOST]);
136 if (GetValueFromUser("Spool Directory", &info[PCAP_SPOOL_DIR]) ==
139 if (GetValueFromUser("Remote Printer Name", &info[PCAP_RPRINTER]) ==
142 if (GetValueFromUser("Quotaserver for this printer", &info[PCAP_QSERVER]) ==
145 info[PCAP_QSERVER] = canonicalize_hostname(info[PCAP_QSERVER]);
146 if (GetYesNoValueFromUser("Authentication required", &info[PCAP_AUTH]) ==
149 if (GetValueFromUser("Price/page", &info[PCAP_PRICE]) == SUB_ERROR)
151 if (GetValueFromUser("Comments", &info[PCAP_COMMENTS]) == SUB_ERROR)
154 FreeAndClear(&info[PCAP_MODTIME], TRUE);
155 FreeAndClear(&info[PCAP_MODBY], TRUE);
156 FreeAndClear(&info[PCAP_MODWITH], TRUE);
161 /* ---------------- Printer Menu ------------------ */
163 /* Function Name: GetPcap
164 * Description: Get Printcap information
165 * Arguments: argc, argv - name of filsys in argv[1].
166 * Returns: DM_NORMAL.
169 int GetPcap(int argc, char **argv)
173 top = GetPcapInfo(argv[1]); /* get info. */
174 Loop(top, (void *) PrintPcapInfo);
175 FreeQueue(top); /* clean the queue. */
179 /* Function Name: RealDeletePcap
180 * Description: Does the real deletion work.
181 * Arguments: info - array of char *'s containing all useful info.
182 * one_item - a Boolean that is true if only one item
183 * in queue that dumped us here.
187 void RealDeletePcap(char **info, Bool one_item)
191 if ((stat = do_mr_query("delete_printcap_entry", 1,
192 &info[PCAP_NAME], NULL, NULL)))
193 com_err(program_name, stat, " printcap entry not deleted.");
195 Put_message("Printcap entry deleted.");
198 /* Function Name: DeletePcap
199 * Description: Delete a printcap entry given its name.
200 * Arguments: argc, argv - argv[1] is the name of the printer.
204 int DeletePcap(int argc, char **argv)
206 struct mqelem *elem = GetPcapInfo(argv[1]);
207 QueryLoop(elem, PrintPcapInfo, RealDeletePcap, "Delete Printer");
213 /* Function Name: AddPcap
214 * Description: Add a printcap entry
215 * Arguments: arc, argv - name of printer in argv[1].
216 * Returns: DM_NORMAL.
219 int AddPcap(int argc, char **argv)
221 char *info[MAX_ARGS_SIZE], **args;
224 if (!ValidName(argv[1]))
227 if (!(stat = do_mr_query("get_printcap_entry", 1, argv + 1,
230 Put_message ("A Printer by that name already exists.");
233 else if (stat != MR_NO_MATCH)
235 com_err(program_name, stat, " in AddPcap");
239 args = AskPcapInfo(SetDefaults(info, argv[1]));
242 Put_message("Aborted.");
246 if ((stat = do_mr_query("add_printcap_entry", CountArgs(args), args,
248 com_err(program_name, stat, " in AddPcap");
255 /* Function Name: ChangePcap
256 * Description: Do the work of changing a pcap
257 * Arguments: argc, argv - printcap info
261 void ChangePcap(char **info, Bool one_item)
266 oldinfo = CopyInfo(info);
267 if (!AskPcapInfo(info))
269 if ((stat = do_mr_query("delete_printcap_entry", 1, &info[PCAP_NAME],
272 com_err(program_name, stat, " printcap entry not deleted.");
275 if ((stat = do_mr_query("add_printcap_entry", CountArgs(info), info,
278 com_err(program_name, stat, " in ChngPcap");
279 if ((stat = do_mr_query("add_printcap_entry", CountArgs(oldinfo) - 3,
280 oldinfo, NULL, NULL)))
281 com_err(program_name, stat, " while attempting to put old info back");
288 /* Function Name: ChngPcap
289 * Description: Update the printcap information
290 * Arguments: argc, argv - name of printer in argv[1].
291 * Returns: DM_NORMAL.
294 int ChngPcap(int argc, char **argv)
296 struct mqelem *elem = GetPcapInfo(argv[1]);
297 QueryLoop(elem, NullPrint, ChangePcap, "Change the printer");
303 /* Function Name: SetPdDefaults
304 * Description: sets the default values for palladium additions.
305 * Arguments: info - an array of char pointers to recieve defaults.
306 * Returns: char ** (this array, now filled).
309 static char **SetPdDefaults(char **info, char *name)
311 info[PD_NAME] = strdup(name);
312 info[PD_IDENT] = strdup("10000");
313 info[PD_HOST] = strdup(DEFAULT_MACHINE);
314 info[PD_MODTIME] = info[PD_MODBY] = info[PD_MODWITH] = NULL;
320 /* Function Name: AskPalladiumInfo.
321 * Description: This function askes the user for information about a
322 * printer and saves it into a structure.
323 * Arguments: info - a pointer the the structure to put the
328 static char **AskPalladiumInfo(char **info)
330 char temp_buf[BUFSIZ];
333 sprintf(temp_buf, "Palladium Server/Supervisor entry for %s.",
335 Put_message(temp_buf);
338 if (GetValueFromUser("RPC Program Number", &info[PD_IDENT]) == SUB_ERROR)
340 if (GetValueFromUser("Print Server/Supervisor Host", &info[PD_HOST]) ==
343 info[PD_HOST] = canonicalize_hostname(info[PD_HOST]);
345 FreeAndClear(&info[PD_MODTIME], TRUE);
346 FreeAndClear(&info[PD_MODBY], TRUE);
347 FreeAndClear(&info[PD_MODWITH], TRUE);
353 /* Function Name: PrintPalladiumInfo
354 * Description: Yet another specialized print function.
355 * Arguments: info - all info about this Printer.
356 * Returns: printer name
359 static char *PrintPalladiumInfo(char **info)
363 if (!info) /* If no informaion */
365 Put_message("PrintPalladiumInfo called with null info!");
369 sprintf(buf, "Name: %-24s Program #: %s Host: %s",
370 info[PD_NAME], info[PD_IDENT], info[PD_HOST]);
372 sprintf(buf, MOD_FORMAT, info[PD_MODBY], info[PD_MODTIME],
375 return info[PD_NAME];
379 static struct mqelem *GetPalladiumInfo(char *name)
382 struct mqelem *elem = NULL;
384 if ((status = do_mr_query("get_palladium", 1, &name, StoreInfo, &elem)))
386 com_err(program_name, status, " in GetPalladiumInfo");
389 return QueueTop(elem);
393 void ChangePalladium(char **info, Bool one_item)
397 if (!AskPalladiumInfo(info))
399 if ((status = do_mr_query("delete_palladium", 1, &info[PD_NAME],
402 com_err(program_name, status, " palladium entry not deleted.");
405 if ((status = do_mr_query("add_palladium", CountArgs(info), info,
407 com_err(program_name, status, " in ChngPalladium");
412 /* Function Name: RealDeletePalladium
413 * Description: Does the real deletion work.
414 * Arguments: info - array of char *'s containing all useful info.
415 * one_item - a Boolean that is true if only one item
416 * in queue that dumped us here.
420 void RealDeletePalladium(char **info, Bool one_item)
424 if ((stat = do_mr_query("delete_palladium", 1,
425 &info[PD_NAME], NULL, NULL)))
426 com_err(program_name, stat, " palladium entry not deleted.");
428 Put_message("Palladium entry deleted.");
432 int GetPalladium(int argc, char **argv)
436 top = GetPalladiumInfo(argv[1]);
437 Loop(top, (void (*)(char **))PrintPalladiumInfo);
443 int AddPalladium(int argc, char **argv)
445 char *info[MAX_ARGS_SIZE], **args;
448 if (!ValidName(argv[1]))
451 if (!(status = do_mr_query("get_palladium", 1, &argv[1], NULL, NULL)))
453 Put_message("A server or supervisor by that name already exists.");
456 else if (status != MR_NO_MATCH)
458 com_err(program_name, status, " in AddPalladium");
462 args = AskPalladiumInfo(SetPdDefaults(info, argv[1]));
465 Put_message("Aborted.");
469 if ((status = do_mr_query("add_palladium", CountArgs(args), args,
471 com_err(program_name, status, " in AddPalladium");
478 int ChngPalladium(int argc, char **argv)
480 struct mqelem *elem = GetPalladiumInfo(argv[1]);
481 QueryLoop(elem, NullPrint, ChangePalladium, "Change the server/supervisor");
487 int DeletePalladium(int argc, char **argv)
489 struct mqelem *elem = GetPalladiumInfo(argv[1]);
490 QueryLoop(elem, PrintPalladiumInfo, RealDeletePalladium,
491 "Delete server/supervisor");
496 int ShowPalladiumAlias(int argc, char **argv)
498 struct mqelem *elem = NULL;
499 char *qargv[3], buf[BUFSIZ];
503 qargv[1] = "PALLADIUM";
505 if ((status = do_mr_query("get_alias", 3, qargv, StoreInfo, &elem)))
507 com_err(program_name, status, " in ShowPalladiumAlias");
510 elem = QueueTop(elem);
514 char **info = elem->q_data;
515 sprintf(buf, "Printer: %-16s Server/Supervisor: %s", info[0], info[2]);
520 FreeQueue(QueueTop(elem));
524 int AddPalladiumAlias(int argc, char **argv)
530 qargv[1] = "PALLADIUM";
532 if ((status = do_mr_query("add_alias", 3, qargv, NULL, NULL)))
533 com_err(program_name, status, " in AddPalladiumAlias");
537 int DeletePalladiumAlias(int argc, char **argv)
543 qargv[1] = "PALLADIUM";
545 if ((status = do_mr_query("delete_alias", 3, qargv, NULL, NULL)))
546 com_err(program_name, status, " in DeletePalladiumAlias");