75798297f954626437343bf99331514027f4ee80
[enscript.git] / src / prt_dos.c
1 /*
2  * Printer interface for DOS.
3  * Copyright (c) 1996-2001 Markku Rossi.
4  *
5  * Author: Markku Rossi <mtr@iki.fi>
6  */
7
8 /*
9  * This file is part of GNU enscript.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2, or (at your option)
14  * any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; see the file COPYING.  If not, write to
23  * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
24  * Boston, MA 02110-1301, USA.
25  */
26
27 #include "gsint.h"
28
29 /************************** Types and definitions ***************************/
30
31 struct PrinterCtxRec
32 {
33   /* Output stream. */
34   FILE *fp;
35
36   /* If using temporary file, this is its name. */
37   char tmpfile[512];
38
39   /* Command to spool the temporary output.  This is NULL if we
40      already spooled the output to `fp' and there is no
41      post-processing to do. */
42   Buffer *command;
43 };
44
45 typedef struct PrinterCtxRec PrinterCtxStruct;
46 typedef struct PrinterCtxRec *PrinterCtx;
47
48
49 /***************************** Global functions *****************************/
50
51 FILE *
52 printer_open(char *cmd, char *options, char *queue_param, char *printer_name,
53              void **context_return)
54 {
55   PrinterCtx ctx;
56
57   ctx = xcalloc(1, sizeof(*ctx));
58
59   if (cmd && cmd[0])
60     {
61       if (tmpnam(ctx->tmpfile) == NULL)
62         FATAL((stderr, _("could not create temporary spool file name: %s"),
63                strerror(errno)));
64
65       /* Spool output to a temporary file and spool with with command
66          when the printer is closed. */
67
68       ctx->command = buffer_alloc();
69
70       buffer_append(ctx->command, cmd);
71       buffer_append(ctx->command, " ");
72
73       if (options)
74         {
75           buffer_append(ctx->command, options);
76           buffer_append(ctx->command, " ");
77         }
78
79       if (printer_name)
80         {
81           buffer_append(ctx->command, queue_param);
82           buffer_append(ctx->command, printer_name);
83           buffer_append(ctx->command, " ");
84         }
85
86       buffer_append(ctx->command, ctx->tmpfile);
87
88       /* Open the temporary spool file. */
89       ctx->fp = fopen(ctx->tmpfile, "wb");
90       if (ctx->fp == NULL)
91         FATAL((stderr, _("Could not open temporary spool file `%s': %s"),
92                ctx->tmpfile, strerror(errno)));
93     }
94   else
95     {
96       /* Just open file pointer to the printer. */
97       ctx->fp = fopen(printer_name, "wb");
98       if (ctx->fp == NULL)
99         FATAL((stderr, _("Could not open printer `%s': %s"), printer_name,
100               strerror(errno)));
101     }
102
103   *context_return = ctx;
104
105   return ctx->fp;
106 }
107
108
109 void
110 printer_close(void *context)
111 {
112   PrinterCtx ctx = (PrinterCtx) context;
113
114   /* Close the output stream. */
115   fclose(ctx->fp);
116
117   /* Do we need to do post-processing (read spooling). */
118   if (ctx->command)
119     {
120       /* Yes. */
121       if (system(buffer_ptr(ctx->command)) == -1)
122         FATAL((stderr, _("Could not spool temporary output `%s': %s"),
123                ctx->tmpfile, strerror(errno)));
124
125       /* We do not need the spool command anymore. */
126       buffer_free(ctx->command);
127
128       /* Unlink the temporary output file. */
129       (void) remove(ctx->tmpfile);
130     }
131
132   /* Free context. */
133   xfree(ctx);
134 }