prt_dos.c - enscript - GNU Enscript
 (HTM) git clone git://thinkerwim.org/enscript.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       prt_dos.c (3361B)
       ---
            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  * Enscript 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 3 of the License, or
           14  * (at your option) any later version.
           15  *
           16  * Enscript 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 Enscript.  If not, see <http://www.gnu.org/licenses/>.
           23  */
           24 
           25 #include "gsint.h"
           26 
           27 /************************** Types and definitions ***************************/
           28 
           29 struct PrinterCtxRec
           30 {
           31   /* Output stream. */
           32   FILE *fp;
           33 
           34   /* If using temporary file, this is its name. */
           35   char tmpfile[512];
           36 
           37   /* Command to spool the temporary output.  This is NULL if we
           38      already spooled the output to `fp' and there is no
           39      post-processing to do. */
           40   Buffer *command;
           41 };
           42 
           43 typedef struct PrinterCtxRec PrinterCtxStruct;
           44 typedef struct PrinterCtxRec *PrinterCtx;
           45 
           46 
           47 /***************************** Global functions *****************************/
           48 
           49 FILE *
           50 printer_open(char *cmd, char *options, char *queue_param, char *printer_name,
           51              void **context_return)
           52 {
           53   PrinterCtx ctx;
           54 
           55   ctx = xcalloc(1, sizeof(*ctx));
           56 
           57   if (cmd && cmd[0])
           58     {
           59       if (tmpnam(ctx->tmpfile) == NULL)
           60         FATAL((stderr, _("could not create temporary spool file name: %s"),
           61                strerror(errno)));
           62 
           63       /* Spool output to a temporary file and spool with with command
           64          when the printer is closed. */
           65 
           66       ctx->command = buffer_alloc();
           67 
           68       buffer_append(ctx->command, cmd);
           69       buffer_append(ctx->command, " ");
           70 
           71       if (options)
           72         {
           73           buffer_append(ctx->command, options);
           74           buffer_append(ctx->command, " ");
           75         }
           76 
           77       if (printer_name)
           78         {
           79           buffer_append(ctx->command, queue_param);
           80           buffer_append(ctx->command, printer_name);
           81           buffer_append(ctx->command, " ");
           82         }
           83 
           84       buffer_append(ctx->command, ctx->tmpfile);
           85 
           86       /* Open the temporary spool file. */
           87       ctx->fp = fopen(ctx->tmpfile, "wb");
           88       if (ctx->fp == NULL)
           89         FATAL((stderr, _("Could not open temporary spool file `%s': %s"),
           90                ctx->tmpfile, strerror(errno)));
           91     }
           92   else
           93     {
           94       /* Just open file pointer to the printer. */
           95       ctx->fp = fopen(printer_name, "wb");
           96       if (ctx->fp == NULL)
           97         FATAL((stderr, _("Could not open printer `%s': %s"), printer_name,
           98               strerror(errno)));
           99     }
          100 
          101   *context_return = ctx;
          102 
          103   return ctx->fp;
          104 }
          105 
          106 
          107 void
          108 printer_close(void *context)
          109 {
          110   PrinterCtx ctx = (PrinterCtx) context;
          111 
          112   /* Close the output stream. */
          113   fclose(ctx->fp);
          114 
          115   /* Do we need to do post-processing (read spooling). */
          116   if (ctx->command)
          117     {
          118       /* Yes. */
          119       if (system(buffer_ptr(ctx->command)) == -1)
          120         FATAL((stderr, _("Could not spool temporary output `%s': %s"),
          121                ctx->tmpfile, strerror(errno)));
          122 
          123       /* We do not need the spool command anymore. */
          124       buffer_free(ctx->command);
          125 
          126       /* Unlink the temporary output file. */
          127       (void) remove(ctx->tmpfile);
          128     }
          129 
          130   /* Free context. */
          131   xfree(ctx);
          132 }