]> andersk Git - moira.git/blob - clients/moira/printer.c
Code style cleanup. (No functional changes)
[moira.git] / clients / moira / printer.c
1 #if (!defined(lint) && !defined(SABER))
2   static char rcsid_module_c[] = "$Header$";
3 #endif
4
5 /*      This is the file printer.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: Functions for handling the printers.
8  *
9  *      Created:        8/16/88
10  *      By:             Theodore Y. Ts'o
11  *
12  *      $Source$
13  *      $Author$
14  *      $Header$
15  *
16  *      Copyright 1988 by the Massachusetts Institute of Technology.
17  *
18  *      For further information on copyright and distribution
19  *      see the file mit-copyright.h
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <moira.h>
26 #include <moira_site.h>
27 #include <menu.h>
28
29 #include "mit-copyright.h"
30 #include "defs.h"
31 #include "f_defs.h"
32 #include "globals.h"
33
34 #define DEFAULT_MACHINE "E40-PRINT-SERVER-1.MIT.EDU"
35
36 /*      Function Name: SetDefaults
37  *      Description: sets the default values for filesystem additions.
38  *      Arguments: info - an array of char pointers to recieve defaults.
39  *      Returns: char ** (this array, now filled).
40  */
41
42 static char **SetDefaults(char **info, char *name)
43 {
44   char spool_dir[256];
45
46   strcpy(spool_dir, "/usr/spool/printer/");
47   strcat(spool_dir, name);
48
49   info[PCAP_NAME] = Strsave(name);
50   info[PCAP_SPOOL_HOST] = Strsave(DEFAULT_MACHINE);
51   info[PCAP_SPOOL_DIR] = Strsave(spool_dir);
52   info[PCAP_RPRINTER] = Strsave(name);
53   info[PCAP_QSERVER] = Strsave("[NONE]");
54   info[PCAP_AUTH] = Strsave("1");
55   info[PCAP_PRICE] = Strsave("10");
56   info[PCAP_COMMENTS] = Strsave("");
57   info[PCAP_MODTIME] = info[PCAP_MODBY] = info[PCAP_MODWITH] = NULL;
58
59   info[PCAP_END] = NULL;
60   return info;
61 }
62
63 /*      Function Name: GetPcapInfo
64  *      Description: Stores the info in a queue.
65  *      Arguments: name - name of the item to get information on.
66  *      Returns: a pointer to the first element in the queue or null
67  *              if printer not found.
68  */
69
70 static struct qelem *GetPcapInfo(char *name)
71 {
72   int stat;
73   struct qelem *elem = NULL;
74
75   if ((stat = do_mr_query("get_printcap_entry", 1, &name,
76                           StoreInfo, (char *)&elem)))
77     {
78       com_err(program_name, stat, " in GetPcapInfo");
79       return NULL;
80     }
81   return QueueTop(elem);
82 }
83
84 /*      Function Name: PrintPcapInfo
85  *      Description: Yet another specialized print function.
86  *      Arguments: info - all info about this Printer.
87  *      Returns: none
88  */
89
90 static void PrintPcapInfo(char **info)
91 {
92   char buf[BUFSIZ];
93
94   if (!info)            /* If no informaion */
95     {
96       Put_message("PrintPcapInfo called with null info!");
97       return;
98     }
99   sprintf(buf, "Printer: %-35s Spool host: %s", info[PCAP_NAME],
100           info[PCAP_SPOOL_HOST]);
101   Put_message(buf);
102   sprintf(buf, "Spool directory: %-27s Remote Printer Name: %s",
103           info[PCAP_SPOOL_DIR], info[PCAP_RPRINTER]);
104   Put_message(buf);
105   sprintf(buf, "Authentication: %-3s Price/page: %-3s  Quota Server: %s",
106           atoi(info[PCAP_AUTH]) ? "yes" : "no",
107           info[PCAP_PRICE], info[PCAP_QSERVER]);
108   Put_message(buf);
109   sprintf(buf, "Comments: %s", info[PCAP_COMMENTS]);
110   Put_message(buf);
111   sprintf(buf, MOD_FORMAT, info[PCAP_MODBY], info[PCAP_MODTIME],
112           info[PCAP_MODWITH]);
113   Put_message(buf);
114 }
115
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
120  *                             info into.
121  *      Returns: none.
122  */
123
124 static char **AskPcapInfo(char **info)
125 {
126   char temp_buf[BUFSIZ];
127
128   Put_message("");
129   sprintf(temp_buf, "Printcap entry for %s.", info[PCAP_NAME]);
130   Put_message(temp_buf);
131   Put_message("");
132
133   if (GetValueFromUser("Printer Server", &info[PCAP_SPOOL_HOST]) == SUB_ERROR)
134     return NULL;
135   info[PCAP_SPOOL_HOST] = canonicalize_hostname(info[PCAP_SPOOL_HOST]);
136   if (GetValueFromUser("Spool Directory", &info[PCAP_SPOOL_DIR]) ==
137       SUB_ERROR)
138     return NULL;
139   if (GetValueFromUser("Remote Printer Name", &info[PCAP_RPRINTER]) ==
140       SUB_ERROR)
141     return NULL;
142   if (GetValueFromUser("Quotaserver for this printer", &info[PCAP_QSERVER]) ==
143       SUB_ERROR)
144     return NULL;
145   info[PCAP_QSERVER] = canonicalize_hostname(info[PCAP_QSERVER]);
146   if (GetYesNoValueFromUser("Authentication required", &info[PCAP_AUTH]) ==
147       SUB_ERROR)
148     return NULL;
149   if (GetValueFromUser("Price/page", &info[PCAP_PRICE]) == SUB_ERROR)
150     return NULL;
151   if (GetValueFromUser("Comments", &info[PCAP_COMMENTS]) == SUB_ERROR)
152     return NULL;
153
154   FreeAndClear(&info[PCAP_MODTIME], TRUE);
155   FreeAndClear(&info[PCAP_MODBY], TRUE);
156   FreeAndClear(&info[PCAP_MODWITH], TRUE);
157
158   return info;
159 }
160
161 /* ---------------- Printer Menu ------------------ */
162
163 /*      Function Name: GetPcap
164  *      Description: Get Printcap information
165  *      Arguments: argc, argv - name of filsys in argv[1].
166  *      Returns: DM_NORMAL.
167  */
168
169 int GetPcap(int argc, char **argv)
170 {
171   struct qelem *top;
172
173   top = GetPcapInfo(argv[1]); /* get info. */
174   Loop(top, (void *) PrintPcapInfo);
175   FreeQueue(top);               /* clean the queue. */
176   return DM_NORMAL;
177 }
178
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.
184  *      Returns: none.
185  */
186
187 void RealDeletePcap(char **info, Bool one_item)
188 {
189   int stat;
190
191   if ((stat = do_mr_query("delete_printcap_entry", 1,
192                           &info[PCAP_NAME], Scream, NULL)))
193     com_err(program_name, stat, " printcap entry not deleted.");
194   else
195     Put_message("Printcap entry deleted.");
196 }
197
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.
201  *      Returns: none.
202  */
203
204 int DeletePcap(int argc, char **argv)
205 {
206   struct qelem *elem = GetPcapInfo(argv[1]);
207   QueryLoop(elem, PrintPcapInfo, RealDeletePcap, "Delete Printer");
208
209   FreeQueue(elem);
210   return DM_NORMAL;
211 }
212
213 /*      Function Name: AddPcap
214  *      Description: Add a printcap entry
215  *      Arguments: arc, argv - name of printer in argv[1].
216  *      Returns: DM_NORMAL.
217  */
218
219 int AddPcap(int argc, char **argv)
220 {
221   char *info[MAX_ARGS_SIZE], **args;
222   int stat;
223
224   if (!ValidName(argv[1]))
225     return DM_NORMAL;
226
227   if (!(stat = do_mr_query("get_printcap_entry", 1, argv + 1,
228                            NullFunc, NULL)))
229     {
230       Put_message ("A Printer by that name already exists.");
231       return DM_NORMAL;
232     }
233   else if (stat != MR_NO_MATCH)
234     {
235       com_err(program_name, stat, " in AddPcap");
236       return DM_NORMAL;
237     }
238
239   args = AskPcapInfo(SetDefaults(info, argv[1]));
240   if (!args)
241     {
242       Put_message("Aborted.");
243       return DM_NORMAL;
244     }
245
246   if ((stat = do_mr_query("add_printcap_entry", CountArgs(args), args,
247                           NullFunc, NULL)))
248     com_err(program_name, stat, " in AddPcap");
249
250   FreeInfo(info);
251   return DM_NORMAL;
252 }
253
254
255 /*      Function Name: ChangePcap
256  *      Description: Do the work of changing a pcap
257  *      Arguments: argc, argv - printcap info
258  *      Returns:
259  */
260
261 int ChangePcap(char **info, Bool one_item)
262 {
263   int stat;
264   char **oldinfo;
265
266   oldinfo = CopyInfo(info);
267   if (!AskPcapInfo(info))
268     return DM_QUIT;
269   if ((stat = do_mr_query("delete_printcap_entry", 1, &info[PCAP_NAME],
270                           Scream, NULL)))
271     {
272       com_err(program_name, stat, " printcap entry not deleted.");
273       return DM_NORMAL;
274     }
275   if ((stat = do_mr_query("add_printcap_entry", CountArgs(info), info,
276                           NullFunc, NULL)))
277     {
278       com_err(program_name, stat, " in ChngPcap");
279       if ((stat = do_mr_query("add_printcap_entry", CountArgs(oldinfo) - 3,
280                               oldinfo, NullFunc, NULL)))
281         com_err(program_name, stat, " while attempting to put old info back");
282     }
283   FreeInfo(oldinfo);
284   return DM_NORMAL;
285 }
286
287
288 /*      Function Name: ChngPcap
289  *      Description:   Update the printcap information
290  *      Arguments:     argc, argv - name of printer in argv[1].
291  *      Returns:       DM_NORMAL.
292  */
293
294 int ChngPcap(int argc, char **argv)
295 {
296   struct qelem *elem = GetPcapInfo(argv[1]);
297   QueryLoop(elem, NullPrint, ChangePcap, "Change the printer");
298   FreeQueue(elem);
299   return DM_NORMAL;
300 }
301
302
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).
307  */
308
309 static char **SetPdDefaults(char **info, char *name)
310 {
311   info[PD_NAME] = Strsave(name);
312   info[PD_IDENT] = Strsave("10000");
313   info[PD_HOST] = Strsave(DEFAULT_MACHINE);
314   info[PD_MODTIME] = info[PD_MODBY] = info[PD_MODWITH] = NULL;
315
316   info[PD_END] = NULL;
317   return info;
318 }
319
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
324  *                             info into.
325  *      Returns: none.
326  */
327
328 static char **AskPalladiumInfo(char **info)
329 {
330   char temp_buf[BUFSIZ];
331
332   Put_message("");
333   sprintf(temp_buf, "Palladium Server/Supervisor entry for %s.",
334           info[PD_NAME]);
335   Put_message(temp_buf);
336   Put_message("");
337
338   if (GetValueFromUser("RPC Program Number", &info[PD_IDENT]) == SUB_ERROR)
339     return NULL;
340   if (GetValueFromUser("Print Server/Supervisor Host", &info[PD_HOST]) ==
341       SUB_ERROR)
342     return NULL;
343   info[PD_HOST] = canonicalize_hostname(info[PD_HOST]);
344
345   FreeAndClear(&info[PD_MODTIME], TRUE);
346   FreeAndClear(&info[PD_MODBY], TRUE);
347   FreeAndClear(&info[PD_MODWITH], TRUE);
348
349   return info;
350 }
351
352
353 /*      Function Name: PrintPalladiumInfo
354  *      Description: Yet another specialized print function.
355  *      Arguments: info - all info about this Printer.
356  *      Returns: none
357  */
358
359 static void PrintPalladiumInfo(char **info)
360 {
361   char buf[BUFSIZ];
362
363   if (!info)            /* If no informaion */
364     {
365       Put_message("PrintPalladiumInfo called with null info!");
366       return;
367     }
368
369   sprintf(buf, "Name: %-24s Program #: %s  Host: %s",
370           info[PD_NAME], info[PD_IDENT], info[PD_HOST]);
371   Put_message(buf);
372   sprintf(buf, MOD_FORMAT, info[PD_MODBY], info[PD_MODTIME],
373           info[PD_MODWITH]);
374   Put_message(buf);
375 }
376
377
378 static struct qelem *GetPalladiumInfo(char *name)
379 {
380   int status;
381   struct qelem *elem = NULL;
382
383   if ((status = do_mr_query("get_palladium", 1, &name, StoreInfo, &elem)))
384     {
385       com_err(program_name, status, " in GetPalladiumInfo");
386       return NULL;
387     }
388   return QueueTop(elem);
389 }
390
391
392 int ChangePalladium(char **info, Bool one_item)
393 {
394   int status;
395
396   if (!AskPalladiumInfo(info))
397     return DM_QUIT;
398   if ((status = do_mr_query("delete_palladium", 1, &info[PD_NAME],
399                             Scream, NULL)))
400     {
401       com_err(program_name, status, " palladium entry not deleted.");
402       return DM_NORMAL;
403     }
404   if ((status = do_mr_query("add_palladium", CountArgs(info), info,
405                             NullFunc, NULL)))
406     com_err(program_name, status, " in ChngPalladium");
407   return DM_NORMAL;
408 }
409
410
411 /*      Function Name: RealDeletePalladium
412  *      Description: Does the real deletion work.
413  *      Arguments: info - array of char *'s containing all useful info.
414  *                 one_item - a Boolean that is true if only one item
415  *                              in queue that dumped us here.
416  *      Returns: none.
417  */
418
419 void RealDeletePalladium(char **info, Bool one_item)
420 {
421   int stat;
422
423   if ((stat = do_mr_query("delete_palladium", 1,
424                           &info[PD_NAME], Scream, NULL)))
425     com_err(program_name, stat, " palladium entry not deleted.");
426   else
427     Put_message("Palladium entry deleted.");
428 }
429
430
431 int GetPalladium(int argc, char **argv)
432 {
433   struct qelem *top;
434
435   top = GetPalladiumInfo(argv[1]);
436   Loop(top, PrintPalladiumInfo);
437   FreeQueue(top);
438   return DM_NORMAL;
439 }
440
441
442 int AddPalladium(int argc, char **argv)
443 {
444   char *info[MAX_ARGS_SIZE], **args;
445   int status;
446
447   if (!ValidName(argv[1]))
448     return DM_NORMAL;
449
450   if (!(status = do_mr_query("get_palladium", 1, &argv[1], NullFunc, NULL)))
451     {
452       Put_message("A server or supervisor by that name already exists.");
453       return DM_NORMAL;
454     }
455   else if (status != MR_NO_MATCH)
456     {
457       com_err(program_name, status, " in AddPalladium");
458       return DM_NORMAL;
459     }
460
461   args = AskPalladiumInfo(SetPdDefaults(info, argv[1]));
462   if (!args)
463     {
464       Put_message("Aborted.");
465       return DM_NORMAL;
466     }
467
468   if ((status = do_mr_query("add_palladium", CountArgs(args), args,
469                             Scream, NULL)))
470     com_err(program_name, status, " in AddPalladium");
471
472   FreeInfo(info);
473   return DM_NORMAL;
474 }
475
476
477 int ChngPalladium(int argc, char **argv)
478 {
479   struct qelem *elem = GetPalladiumInfo(argv[1]);
480   QueryLoop(elem, NullPrint, ChangePalladium, "Change the server/supervisor");
481   FreeQueue(elem);
482   return DM_NORMAL;
483 }
484
485
486 int DeletePalladium(int argc, char **argv)
487 {
488   struct qelem *elem = GetPalladiumInfo(argv[1]);
489   QueryLoop(elem, PrintPalladiumInfo, RealDeletePalladium,
490             "Delete server/supervisor");
491   FreeQueue(elem);
492   return DM_NORMAL;
493 }
494
495 int ShowPalladiumAlias(int argc, char **argv)
496 {
497   struct qelem *elem = NULL;
498   char *qargv[3], buf[BUFSIZ];
499   int status;
500
501   qargv[0] = argv[1];
502   qargv[1] = "PALLADIUM";
503   qargv[2] = argv[2];
504   if ((status = do_mr_query("get_alias", 3, qargv, StoreInfo, &elem)))
505     {
506       com_err(program_name, status, " in ShowPalladiumAlias");
507       return DM_NORMAL;
508     }
509   elem = QueueTop(elem);
510   Put_message("");
511   while (elem)
512     {
513       char **info = (char **) elem->q_data;
514       sprintf(buf, "Printer: %-16s Server/Supervisor: %s", info[0], info[2]);
515       Put_message(buf);
516       elem = elem->q_forw;
517     }
518
519   FreeQueue(QueueTop(elem));
520   return DM_NORMAL;
521 }
522
523 int AddPalladiumAlias(int argc, char **argv)
524 {
525   int status;
526   char *qargv[3];
527
528   qargv[0] = argv[1];
529   qargv[1] = "PALLADIUM";
530   qargv[2] = argv[2];
531   if ((status = do_mr_query("add_alias", 3, qargv, Scream, NULL)))
532     com_err(program_name, status, " in AddPalladiumAlias");
533   return DM_NORMAL;
534 }
535
536 int DeletePalladiumAlias(int argc, char **argv)
537 {
538   int status;
539   char *qargv[3];
540
541   qargv[0] = argv[1];
542   qargv[1] = "PALLADIUM";
543   qargv[2] = argv[2];
544   if ((status = do_mr_query("delete_alias", 3, qargv, Scream, NULL)))
545     com_err(program_name, status, " in DeletePalladiumAlias");
546   return DM_NORMAL;
547 }
This page took 0.080371 seconds and 5 git commands to generate.