xref: /plan9/sys/src/cmd/gs/src/gp_iwatc.c (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1991, 1995, 1998, 1999, 2000 Aladdin Enterprises.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: gp_iwatc.c,v 1.17 2004/01/15 09:27:10 giles Exp $ */
18 /* Intel processor, Watcom C-specific routines for Ghostscript */
19 #include "dos_.h"
20 #include <fcntl.h>
21 #include <signal.h>
22 #include <stdlib.h>
23 #include "stat_.h"
24 #include "string_.h"
25 #include "gx.h"
26 #include "gp.h"
27 #include "gpmisc.h"
28 
29 /* Library routines not declared in a standard header */
30 extern char *mktemp(char *);	/* in gp_mktmp.c */
31 
32 /* Define a substitute for stdprn (see below). */
33 private FILE *gs_stdprn;
34 
35 /* Forward declarations */
36 private void handle_FPE(int);
37 
38 /* Do platform-dependent initialization. */
39 void
gp_init(void)40 gp_init(void)
41 {
42     gs_stdprn = 0;
43     /* Set up the handler for numeric exceptions. */
44     signal(SIGFPE, handle_FPE);
45 }
46 
47 /* Trap numeric exceptions.  Someday we will do something */
48 /* more appropriate with these. */
49 private void
handle_FPE(int sig)50 handle_FPE(int sig)
51 {
52     eprintf("Numeric exception:\n");
53     exit(1);
54 }
55 
56 /* Do platform-dependent cleanup. */
57 void
gp_exit(int exit_status,int code)58 gp_exit(int exit_status, int code)
59 {
60 }
61 
62 /* Exit the program. */
63 void
gp_do_exit(int exit_status)64 gp_do_exit(int exit_status)
65 {
66     exit(exit_status);
67 }
68 
69 /* ------ Persistent data cache ------*/
70 
71 /* insert a buffer under a (type, key) pair */
gp_cache_insert(int type,byte * key,int keylen,void * buffer,int buflen)72 int gp_cache_insert(int type, byte *key, int keylen, void *buffer, int buflen)
73 {
74     /* not yet implemented */
75     return 0;
76 }
77 
78 /* look up a (type, key) in the cache */
gp_cache_query(int type,byte * key,int keylen,void ** buffer,gp_cache_alloc alloc,void * userdata)79 int gp_cache_query(int type, byte* key, int keylen, void **buffer,
80     gp_cache_alloc alloc, void *userdata)
81 {
82     /* not yet implemented */
83     return -1;
84 }
85 
86 /* ------ Printer accessing ------ */
87 
88 /* Open a connection to a printer.  A null file name means use the */
89 /* standard printer connected to the machine, if any. */
90 /* Return NULL if the connection could not be opened. */
91 extern void gp_set_file_binary(int, int);
92 FILE *
gp_open_printer(char fname[gp_file_name_sizeof],int binary_mode)93 gp_open_printer(char fname[gp_file_name_sizeof], int binary_mode)
94 {
95     FILE *pfile;
96 
97     if (strlen(fname) == 0 || !strcmp(fname, "PRN")) {
98 #ifdef stdprn
99 	if (!binary_mode)
100 	    return stdprn;
101 	if (gs_stdprn == 0) {
102 	    /* We have to effectively reopen the printer, */
103 	    /* because the Watcom library does \n -> \r\n */
104 	    /* substitution on the stdprn stream. */
105 	    int fno = dup(fileno(stdprn));
106 
107 	    setmode(fno, O_BINARY);
108 	    gs_stdprn = fdopen(fno, "wb");
109 	}
110 	pfile = gs_stdprn;
111 #else	/* WATCOM doesn't know about stdprn device */
112 	pfile = fopen("PRN", (binary_mode ? "wb" : "w"));
113 	if (pfile == NULL)
114 	    return NULL;
115 #endif	/* defined(stdprn) */
116     } else {
117 	pfile = fopen(fname, (binary_mode ? "wb" : "w"));
118 	if (pfile == NULL)
119 	    return NULL;
120     }
121     gp_set_file_binary(fileno(pfile), binary_mode);
122     return pfile;
123 }
124 
125 /* Close the connection to the printer. */
126 void
gp_close_printer(FILE * pfile,const char * fname)127 gp_close_printer(FILE * pfile, const char *fname)
128 {
129 #ifdef stdprn
130     if (pfile != stdprn)
131 #endif	/* defined(stdprn) */
132 	fclose(pfile);
133     if (pfile == gs_stdprn)
134 	gs_stdprn = 0;
135 }
136 
137 /* ------ File naming and accessing ------ */
138 
139 /* Create and open a scratch file with a given name prefix. */
140 /* Write the actual file name at fname. */
141 FILE *
gp_open_scratch_file(const char * prefix,char * fname,const char * mode)142 gp_open_scratch_file(const char *prefix, char *fname, const char *mode)
143 {	      /* The -7 is for XXXXXXX */
144     int prefix_length = strlen(prefix);
145     int len = gp_file_name_sizeof - prefix_length - 7;
146     FILE *f;
147 
148     if (gp_file_name_is_absolute(prefix, prefix_length) ||
149 	gp_gettmpdir(fname, &len) != 0
150 	)
151 	*fname = 0;
152     else {
153 	char *temp;
154 
155 	/* Prevent X's in path from being converted by mktemp. */
156 	for (temp = fname; *temp; temp++)
157 	    *temp = tolower(*temp);
158 	if (strlen(fname) && (fname[strlen(fname) - 1] != '\\'))
159 	    strcat(fname, "\\");
160     }
161     if (strlen(fname) + prefix_length + 7 >= gp_file_name_sizeof)
162 	return 0;		/* file name too long */
163     strcat(fname, prefix);
164     strcat(fname, "XXXXXX");
165     mktemp(fname);
166     f = gp_fopentemp(fname, mode);
167     if (f == NULL)
168 	eprintf1("**** Could not open temporary file %s\n", fname);
169     return f;
170 }
171 
172 
173 /* Open a file with the given name, as a stream of uninterpreted bytes. */
174 FILE *
gp_fopen(const char * fname,const char * mode)175 gp_fopen(const char *fname, const char *mode)
176 {
177     return fopen(fname, mode);
178 }
179 
180 /* ------ Font enumeration ------ */
181 
182  /* This is used to query the native os for a list of font names and
183   * corresponding paths. The general idea is to save the hassle of
184   * building a custom fontmap file.
185   */
186 
gp_enumerate_fonts_init(gs_memory_t * mem)187 void *gp_enumerate_fonts_init(gs_memory_t *mem)
188 {
189     return NULL;
190 }
191 
gp_enumerate_fonts_next(void * enum_state,char ** fontname,char ** path)192 int gp_enumerate_fonts_next(void *enum_state, char **fontname, char **path)
193 {
194     return 0;
195 }
196 
gp_enumerate_fonts_free(void * enum_state)197 void gp_enumerate_fonts_free(void *enum_state)
198 {
199 }
200