Move some fields from the HID* structure to HID_DRAW* and HID_DRAW_CLASS*
[geda-pcb/pcjc2.git] / src / hid / lpr / lpr.c
blobda9e20fb3d3e0711312e4cd72aa4190af062a8ca
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
5 #include <stdio.h>
6 #include <stdarg.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <assert.h>
11 #include "global.h"
12 #include "data.h"
13 #include "misc.h"
15 #include "hid.h"
16 #include "hid_draw.h"
17 #include "../hidint.h"
18 #include "../ps/ps.h"
19 #include "hid/common/hidnogui.h"
20 #include "hid/common/hidinit.h"
22 #ifdef HAVE_LIBDMALLOC
23 #include <dmalloc.h>
24 #endif
26 #define CRASH fprintf(stderr, \
27 _("HID error: pcb called unimplemented PS function %s.\n"), \
28 __FUNCTION__); abort()
30 static HID_Attribute base_lpr_options[] = {
32 /* %start-doc options "98 lpr Printing Options"
33 @ftable @code
34 @item --lprcommand <string>
35 Command to use for printing. Defaults to @code{lpr}. This can be used to produce
36 PDF output with a virtual PDF printer. Example: @*
37 @code{--lprcommand "lp -d CUPS-PDF-Printer"}.
38 @end ftable
39 @noindent In addition, all @ref{Postscript Export} options are valid.
40 %end-doc
42 {N_("lprcommand"), N_("Command to use for printing"),
43 HID_String, 0, 0, {0, 0, 0}, 0, 0},
44 #define HA_lprcommand 0
47 #define NUM_OPTIONS (sizeof(lpr_options)/sizeof(lpr_options[0]))
49 static HID_Attribute *lpr_options = 0;
50 static int num_lpr_options = 0;
51 static HID_Attr_Val *lpr_values;
53 static HID_Attribute *
54 lpr_get_export_options (int *n)
57 * We initialize the default value in this manner because the GUI
58 * HID's may want to free() this string value and replace it with a
59 * new one based on how a user fills out a print dialog.
61 if (base_lpr_options[HA_lprcommand].default_val.str_value == NULL)
63 base_lpr_options[HA_lprcommand].default_val.str_value = strdup("lpr");
66 if (lpr_options == 0)
68 HID_Attribute *ps_opts = ps_hid.get_export_options (&num_lpr_options);
69 lpr_options =
70 (HID_Attribute *) calloc (num_lpr_options, sizeof (HID_Attribute));
71 memcpy (lpr_options, ps_opts, num_lpr_options * sizeof (HID_Attribute));
72 memcpy (lpr_options, base_lpr_options, sizeof (base_lpr_options));
73 lpr_values =
74 (HID_Attr_Val *) calloc (num_lpr_options, sizeof (HID_Attr_Val));
76 if (n)
77 *n = num_lpr_options;
78 return lpr_options;
81 static void
82 lpr_do_export (HID_Attr_Val * options)
84 FILE *f;
85 int i;
86 const char *filename;
88 if (!options)
90 lpr_get_export_options (0);
91 for (i = 0; i < num_lpr_options; i++)
92 lpr_values[i] = lpr_options[i].default_val;
93 options = lpr_values;
96 filename = options[HA_lprcommand].str_value;
98 printf (_("LPR: open %s\n"), filename);
99 f = popen (filename, "w");
100 if (!f)
102 perror (filename);
103 return;
106 ps_hid_export_to_file (f, options);
108 pclose (f);
111 static void
112 lpr_parse_arguments (int *argc, char ***argv)
114 lpr_get_export_options (0);
115 hid_register_attributes (lpr_options, num_lpr_options);
116 hid_parse_command_line (argc, argv);
119 static void
120 lpr_calibrate (double xval, double yval)
122 ps_calibrate_1 (xval, yval, 1);
125 static HID lpr_hid;
127 void
128 hid_lpr_init ()
130 memset (&lpr_hid, 0, sizeof (HID));
132 common_nogui_init (&lpr_hid);
133 ps_ps_init (&lpr_hid);
135 lpr_hid.struct_size = sizeof (HID);
136 lpr_hid.name = "lpr";
137 lpr_hid.description = N_("Postscript print");
138 lpr_hid.printer = 1;
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;
145 hid_register_hid (&lpr_hid);