1 /* Copyright (C) 1991, 1995, 1998, 1999, 2000 artofcode LLC.  All rights reserved.
2 
3   This program is free software; you can redistribute it and/or modify it
4   under the terms of the GNU General Public License as published by the
5   Free Software Foundation; either version 2 of the License, or (at your
6   option) any later version.
7 
8   This program is distributed in the hope that it will be useful, but
9   WITHOUT ANY WARRANTY; without even the implied warranty of
10   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   General Public License for more details.
12 
13   You should have received a copy of the GNU General Public License along
14   with this program; if not, write to the Free Software Foundation, Inc.,
15   59 Temple Place, Suite 330, Boston, MA, 02111-1307.
16 
17 */
18 
19 /*$Id: gp_iwatc.c,v 1.6.2.2.2.1 2003/01/17 00:49:02 giles Exp $ */
20 /* Intel processor, Watcom C-specific routines for Ghostscript */
21 #include "dos_.h"
22 #include <fcntl.h>
23 #include <signal.h>
24 #include <stdlib.h>
25 #include "stat_.h"
26 #include "string_.h"
27 #include "gx.h"
28 #include "gp.h"
29 #include "gpmisc.h"
30 
31 /* Library routines not declared in a standard header */
32 extern char *mktemp(P1(char *));	/* in gp_mktmp.c */
33 
34 /* Define a substitute for stdprn (see below). */
35 private FILE *gs_stdprn;
36 
37 /* Forward declarations */
38 private void handle_FPE(P1(int));
39 
40 /* Do platform-dependent initialization. */
41 void
gp_init(void)42 gp_init(void)
43 {
44     gs_stdprn = 0;
45     /* Set up the handler for numeric exceptions. */
46     signal(SIGFPE, handle_FPE);
47 }
48 
49 /* Trap numeric exceptions.  Someday we will do something */
50 /* more appropriate with these. */
51 private void
handle_FPE(int sig)52 handle_FPE(int sig)
53 {
54     eprintf("Numeric exception:\n");
55     exit(1);
56 }
57 
58 /* Do platform-dependent cleanup. */
59 void
gp_exit(int exit_status,int code)60 gp_exit(int exit_status, int code)
61 {
62 }
63 
64 /* Exit the program. */
65 void
gp_do_exit(int exit_status)66 gp_do_exit(int exit_status)
67 {
68 }
69 
70 /* ------ Printer accessing ------ */
71 
72 /* Open a connection to a printer.  A null file name means use the */
73 /* standard printer connected to the machine, if any. */
74 /* Return NULL if the connection could not be opened. */
75 extern void gp_set_file_binary(P2(int, int));
76 FILE *
gp_open_printer(char fname[gp_file_name_sizeof],int binary_mode)77 gp_open_printer(char fname[gp_file_name_sizeof], int binary_mode)
78 {
79     FILE *pfile;
80 
81     if (strlen(fname) == 0 || !strcmp(fname, "PRN")) {
82 #ifdef stdprn
83 	if (!binary_mode)
84 	    return stdprn;
85 	if (gs_stdprn == 0) {
86 	    /* We have to effectively reopen the printer, */
87 	    /* because the Watcom library does \n -> \r\n */
88 	    /* substitution on the stdprn stream. */
89 	    int fno = dup(fileno(stdprn));
90 
91 	    setmode(fno, O_BINARY);
92 	    gs_stdprn = fdopen(fno, "wb");
93 	}
94 	pfile = gs_stdprn;
95 #else	/* WATCOM doesn't know about stdprn device */
96 	pfile = fopen("PRN", (binary_mode ? "wb" : "w"));
97 	if (pfile == NULL)
98 	    return NULL;
99 #endif	/* defined(stdprn) */
100     } else {
101 	pfile = fopen(fname, (binary_mode ? "wb" : "w"));
102 	if (pfile == NULL)
103 	    return NULL;
104     }
105     gp_set_file_binary(fileno(pfile), binary_mode);
106     return pfile;
107 }
108 
109 /* Close the connection to the printer. */
110 void
gp_close_printer(FILE * pfile,const char * fname)111 gp_close_printer(FILE * pfile, const char *fname)
112 {
113 #ifdef stdprn
114     if (pfile != stdprn)
115 #endif	/* defined(stdprn) */
116 	fclose(pfile);
117     if (pfile == gs_stdprn)
118 	gs_stdprn = 0;
119 }
120 
121 /* ------ File naming and accessing ------ */
122 
123 /* Create and open a scratch file with a given name prefix. */
124 /* Write the actual file name at fname. */
125 FILE *
gp_open_scratch_file(const char * prefix,char * fname,const char * mode)126 gp_open_scratch_file(const char *prefix, char *fname, const char *mode)
127 {	      /* The -7 is for XXXXXXX */
128     int prefix_length = strlen(prefix);
129     int len = gp_file_name_sizeof - prefix_length - 7;
130 
131     if (gp_pathstring_not_bare(prefix, prefix_length) ||
132 	gp_gettmpdir(fname, &len) != 0
133 	)
134 	*fname = 0;
135     else {
136 	char *temp;
137 
138 	/* Prevent X's in path from being converted by mktemp. */
139 	for (temp = fname; *temp; temp++)
140 	    *temp = tolower(*temp);
141 	if (strlen(fname) && (fname[strlen(fname) - 1] != '\\'))
142 	    strcat(fname, "\\");
143     }
144     if (strlen(fname) + prefix_length + 7 >= gp_file_name_sizeof)
145 	return 0;		/* file name too long */
146     strcat(fname, prefix);
147     strcat(fname, "XXXXXX");
148     mktemp(fname);
149     return gp_fopentemp(fname, mode);
150 }
151 
152 
153 /* Open a file with the given name, as a stream of uninterpreted bytes. */
154 FILE *
gp_fopen(const char * fname,const char * mode)155 gp_fopen(const char *fname, const char *mode)
156 {
157     return fopen(fname, mode);
158 }
159