1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4 
5 #include <stdio.h>
6 #include <stdarg.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <assert.h>
10 
11 #include "global.h"
12 #include "data.h"
13 #include "misc.h"
14 
15 #include "hid.h"
16 #include "../hidint.h"
17 #include "../ps/ps.h"
18 #include "hid/common/hidnogui.h"
19 #include "hid/common/hidinit.h"
20 
21 #ifdef HAVE_LIBDMALLOC
22 #include <dmalloc.h>
23 #endif
24 
25 #define CRASH fprintf(stderr, \
26     _("HID error: pcb called unimplemented PS function %s.\n"), \
27     __FUNCTION__); abort()
28 
29 static HID_Attribute base_lpr_options[] = {
30 
31 /* %start-doc options "98 lpr Printing Options"
32 @ftable @code
33 @item --lprcommand <string>
34 Command to use for printing. Defaults to @code{lpr}. This can be used to produce
35 PDF output with a virtual PDF printer. Example: @*
36 @code{--lprcommand "lp -d CUPS-PDF-Printer"}.
37 @end ftable
38 @noindent In addition, all @ref{Postscript Export} options are valid.
39 %end-doc
40 */
41   {N_("lprcommand"), N_("Command to use for printing"),
42    HID_String, 0, 0, {0, 0, 0}, 0, 0},
43 #define HA_lprcommand 0
44 };
45 
46 #define NUM_OPTIONS (sizeof(lpr_options)/sizeof(lpr_options[0]))
47 
48 static HID_Attribute *lpr_options = 0;
49 static int num_lpr_options = 0;
50 static HID_Attr_Val *lpr_values;
51 
52 static HID_Attribute *
lpr_get_export_options(int * n)53 lpr_get_export_options (int *n)
54 {
55   /*
56    * We initialize the default value in this manner because the GUI
57    * HID's may want to free() this string value and replace it with a
58    * new one based on how a user fills out a print dialog.
59    */
60   if (base_lpr_options[HA_lprcommand].default_val.str_value == NULL)
61     {
62       base_lpr_options[HA_lprcommand].default_val.str_value = strdup("lpr");
63     }
64 
65   if (lpr_options == 0)
66     {
67       HID_Attribute *ps_opts = ps_hid.get_export_options (&num_lpr_options);
68       lpr_options =
69 	(HID_Attribute *) calloc (num_lpr_options, sizeof (HID_Attribute));
70       memcpy (lpr_options, ps_opts, num_lpr_options * sizeof (HID_Attribute));
71       memcpy (lpr_options, base_lpr_options, sizeof (base_lpr_options));
72       lpr_values =
73 	(HID_Attr_Val *) calloc (num_lpr_options, sizeof (HID_Attr_Val));
74     }
75   if (n)
76     *n = num_lpr_options;
77   return lpr_options;
78 }
79 
80 static void
lpr_do_export(HID_Attr_Val * options)81 lpr_do_export (HID_Attr_Val * options)
82 {
83   FILE *f;
84   int i;
85   const char *filename;
86 
87   if (!options)
88     {
89       lpr_get_export_options (0);
90       for (i = 0; i < num_lpr_options; i++)
91 	lpr_values[i] = lpr_options[i].default_val;
92       options = lpr_values;
93     }
94 
95   filename = options[HA_lprcommand].str_value;
96 
97   printf (_("LPR: open %s\n"), filename);
98   f = popen (filename, "w");
99   if (!f)
100     {
101       perror (filename);
102       return;
103     }
104 
105   ps_hid_export_to_file (f, options);
106 
107   pclose (f);
108 }
109 
110 static void
lpr_parse_arguments(int * argc,char *** argv)111 lpr_parse_arguments (int *argc, char ***argv)
112 {
113   lpr_get_export_options (0);
114   hid_register_attributes (lpr_options, num_lpr_options);
115   hid_parse_command_line (argc, argv);
116 }
117 
118 static void
lpr_calibrate(double xval,double yval)119 lpr_calibrate (double xval, double yval)
120 {
121   ps_calibrate_1 (xval, yval, 1);
122 }
123 
124 static HID lpr_hid;
125 
126 void
hid_lpr_init()127 hid_lpr_init ()
128 {
129   memset (&lpr_hid, 0, sizeof (HID));
130 
131   common_nogui_init (&lpr_hid);
132   ps_ps_init (&lpr_hid);
133 
134   lpr_hid.struct_size         = sizeof (HID);
135   lpr_hid.name                = "lpr";
136   lpr_hid.description         = N_("Postscript print");
137   lpr_hid.printer             = 1;
138   lpr_hid.poly_before         = 1;
139 
140   lpr_hid.get_export_options  = lpr_get_export_options;
141   lpr_hid.do_export           = lpr_do_export;
142   lpr_hid.parse_arguments     = lpr_parse_arguments;
143   lpr_hid.calibrate           = lpr_calibrate;
144 
145   hid_register_hid (&lpr_hid);
146 }
147