1 /* Copyright (C) 2001-2012 Artifex Software, Inc.
2    All Rights Reserved.
3 
4    This software is provided AS-IS with no warranty, either express or
5    implied.
6 
7    This software is distributed under license and may not be copied,
8    modified or distributed except as expressly authorized under the terms
9    of the license contained in the file LICENSE in this distribution.
10 
11    Refer to licensing information at http://www.artifex.com or contact
12    Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134, San Rafael,
13    CA  94903, U.S.A., +1(415)492-9861, for further information.
14 */
15 
16 
17 /* Standard definitions for Ghostscript code */
18 
19 #ifndef std_INCLUDED
20 #  define std_INCLUDED
21 
22 #include "stdpre.h"
23 
24 /* Include the architecture definitions. */
25 #include "arch.h"
26 
27 /*
28  * Define lower-case versions of the architecture parameters for backward
29  * compatibility.
30  */
31 #define arch_log2_sizeof_short ARCH_LOG2_SIZEOF_SHORT
32 #define arch_log2_sizeof_int ARCH_LOG2_SIZEOF_INT
33 #define arch_log2_sizeof_long ARCH_LOG2_SIZEOF_LONG
34 #define arch_sizeof_ptr ARCH_SIZEOF_PTR
35 #define arch_sizeof_float ARCH_SIZEOF_FLOAT
36 #define arch_sizeof_double ARCH_SIZEOF_DOUBLE
37 #define arch_is_big_endian ARCH_IS_BIG_ENDIAN
38 #define arch_arith_rshift ARCH_ARITH_RSHIFT
39 /*
40  * Define the alignment that the memory manager must preserve.
41  * We assume all alignment moduli are powers of 2.
42  * NOTE: we require that malloc align blocks at least this strictly.
43  */
44 #define ARCH_ALIGN_MEMORY_MOD\
45   (((ARCH_ALIGN_LONG_MOD - 1) | (ARCH_ALIGN_PTR_MOD - 1) |\
46     (ARCH_ALIGN_DOUBLE_MOD - 1)) + 1)
47 
48 /* Define integer data type sizes in terms of log2s. */
49 #define ARCH_SIZEOF_CHAR (1 << ARCH_LOG2_SIZEOF_CHAR)
50 #define ARCH_SIZEOF_SHORT (1 << ARCH_LOG2_SIZEOF_SHORT)
51 #define ARCH_SIZEOF_INT (1 << ARCH_LOG2_SIZEOF_INT)
52 #define ARCH_SIZEOF_LONG (1 << ARCH_LOG2_SIZEOF_LONG)
53 #define ARCH_SIZEOF_LONG_LONG (1 << ARCH_LOG2_SIZEOF_LONG_LONG)
54 #define ARCH_INTS_ARE_SHORT (ARCH_SIZEOF_INT == ARCH_SIZEOF_SHORT)
55 /* Backward compatibility */
56 #define arch_sizeof_short ARCH_SIZEOF_SHORT
57 #define arch_sizeof_int ARCH_SIZEOF_INT
58 #define arch_sizeof_long ARCH_SIZEOF_LONG
59 
60 /* Define whether we are on a large- or small-memory machine. */
61 /* Currently, we assume small memory and 16-bit ints are synonymous. */
62 #define ARCH_SMALL_MEMORY (ARCH_SIZEOF_INT <= 2)
63 /* Backward compatibility */
64 #define arch_small_memory ARCH_SMALL_MEMORY
65 
66 /* Define unsigned 16- and 32-bit types.  These are needed in */
67 /* a surprising number of places that do bit manipulation. */
68 #if ARCH_SIZEOF_SHORT == 2      /* no plausible alternative! */
69 typedef ushort bits16;
70 #endif
71 #if ARCH_SIZEOF_INT == 4
72 typedef uint bits32;
73 #else
74 # if ARCH_SIZEOF_LONG == 4
75 typedef ulong bits32;
76 # endif
77 #endif
78 
79 /* Minimum and maximum values for the signed types. */
80 /* Avoid casts, to make them acceptable to strict ANSI compilers. */
81 #define min_short (-1 << (arch_sizeof_short * 8 - 1))
82 #define max_short (~min_short)
83 #define min_int (-1 << (arch_sizeof_int * 8 - 1))
84 #define max_int (~min_int)
85 #define min_long (-1L << (arch_sizeof_long * 8 - 1))
86 #define max_long (~min_long)
87 
88 /*
89  * The maximum values for the unsigned types are defined in arch.h,
90  * because so many compilers handle unsigned constants wrong.
91  * In particular, most of the DEC VMS compilers incorrectly sign-extend
92  * short unsigned constants (but not short unsigned variables) when
93  * widening them to longs.  We program around this on a case-by-case basis.
94  * Some compilers don't truncate constants when they are cast down.
95  * The UTek compiler does special weird things of its own.
96  * All the rest (including gcc on all platforms) do the right thing.
97  */
98 #define max_uchar ARCH_MAX_UCHAR
99 #define max_ushort ARCH_MAX_USHORT
100 #define max_uint ARCH_MAX_UINT
101 #define max_ulong ARCH_MAX_ULONG
102 
103 /* Minimum and maximum values for pointers. */
104 #if ARCH_PTRS_ARE_SIGNED
105 #  define min_ptr min_long
106 #  define max_ptr max_long
107 #else
108 #  define min_ptr ((ulong)0)
109 #  define max_ptr max_ulong
110 #endif
111 
112 /* Define a reliable arithmetic right shift. */
113 /* Must use arith_rshift_1 for a shift by a literal 1. */
114 #define arith_rshift_slow(x,n) ((x) < 0 ? ~(~(x) >> (n)) : (x) >> (n))
115 #if arch_arith_rshift == 2
116 #  define arith_rshift(x,n) ((x) >> (n))
117 #  define arith_rshift_1(x) ((x) >> 1)
118 #else
119 #if arch_arith_rshift == 1      /* OK except for n=1 */
120 #  define arith_rshift(x,n) ((x) >> (n))
121 #  define arith_rshift_1(x) arith_rshift_slow(x,1)
122 #else
123 #  define arith_rshift(x,n) arith_rshift_slow(x,n)
124 #  define arith_rshift_1(x) arith_rshift_slow(x,1)
125 #endif
126 #endif
127 
128 /*
129  * Standard error printing macros.
130  * Use dprintf for messages that just go to dpf;
131  * dlprintf for messages to dpf with optional with file name (and,
132  * if available, line number);
133  * eprintf or eprintfm for error messages to epf that include the program
134  * name (eprintfm requires a memory pointer and is safe to use in
135  * multithreaded environments - eprint does not, and is not, and should
136  * therefore be avoided where possible);
137  * lprintf for debugging messages that should include line number info.
138  * Since we all stdout/stderr output must go via outprintf/errprintf,
139  * we have to define dputc and dputs in terms of errprintf also.
140  */
141 
142 /*
143  * We would prefer not to include stdio.h here, but we need it for
144  * the FILE * argument of the printing procedures.
145  */
146 #include <stdio.h>
147 
148 /*
149  * Not a very good place to define this, but we can't find a better one.
150  */
151 #ifndef gs_memory_DEFINED
152 #  define gs_memory_DEFINED
153 typedef struct gs_memory_s gs_memory_t;
154 #endif
155 
156 #define init_proc(proc)\
157   int proc(gs_memory_t *)
158 
159 /* dpf and epf may be redefined */
160 #define dpf errprintf_nomem
161 #define epf errprintf_nomem
162 #define epfm errprintf
163 
164 /* To allow stdout and stderr to be redirected, all stdout goes
165  * though outwrite and all stderr goes through errwrite.
166  */
167 
168 int outwrite(const gs_memory_t *mem, const char *str, int len);
169 int errwrite(const gs_memory_t *mem, const char *str, int len);
170 void outflush(const gs_memory_t *mem);
171 void errflush(const gs_memory_t *mem);
172 
173 /* As a temporary measure, we allow forms of errwrite/errflush that do not
174  * need to be given a memory pointer. Any uses of this (largely the debugging
175  * system) will fail with multithreaded usage. */
176 int errwrite_nomem(const char *str, int len);
177 void errflush_nomem(void);
178 
179 /* Formatted output to outwrite and errwrite.
180  * The maximum string length is 1023 characters.
181  */
182 #ifdef __PROTOTYPES__
183 #  ifndef __printflike
184    /* error checking for printf format args gcc only */
185 #    if __GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ >= 7
186 #      define __printflike(fmtarg, firstvararg) \
187              __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
188 #    else
189 #      define __printflike(fmtarg, firstvararg)
190 #    endif
191 #  endif
192 int outprintf(const gs_memory_t *mem, const char *fmt, ...) __printflike(2, 3);
193 int errprintf(const gs_memory_t *mem, const char *fmt, ...) __printflike(2, 3);
194 int errprintf_nomem(const char *fmt, ...) __printflike(1, 2);
195 #else
196 int outprintf();
197 int errprintf();
198 int errprintf_nomem();
199 #endif
200 
201 /* Print the program line # for debugging. */
202 #if __LINE__                    /* compiler provides it */
203 void dprintf_file_and_line(const char *, int);
204 #  define _dpl dprintf_file_and_line(__FILE__, __LINE__),
205 #else
206 void dprintf_file_only(const char *);
207 #  define _dpl dprintf_file_only(__FILE__),
208 #endif
209 
210 void dflush(void);              /* flush stderr */
211 #define dputc(chr) dprintf1("%c", chr)
212 #define dlputc(chr) dlprintf1("%c", chr)
213 #define dputs(str) dprintf1("%s", str)
214 #define dlputs(str) dlprintf1("%s", str)
215 #define dprintf(str)\
216   dpf(str)
217 #define dlprintf(str)\
218   (_dpl dpf(str))
219 #define dprintf1(str,arg1)\
220   dpf(str, arg1)
221 #define dlprintf1(str,arg1)\
222   (_dpl dprintf1(str, arg1))
223 #define dprintf2(str,arg1,arg2)\
224   dpf(str, arg1, arg2)
225 #define dlprintf2(str,arg1,arg2)\
226   (_dpl dprintf2(str, arg1, arg2))
227 #define dprintf3(str,arg1,arg2,arg3)\
228   dpf(str, arg1, arg2, arg3)
229 #define dlprintf3(str,arg1,arg2,arg3)\
230   (_dpl dprintf3(str, arg1, arg2, arg3))
231 #define dprintf4(str,arg1,arg2,arg3,arg4)\
232   dpf(str, arg1, arg2, arg3, arg4)
233 #define dlprintf4(str,arg1,arg2,arg3,arg4)\
234   (_dpl dprintf4(str, arg1, arg2, arg3, arg4))
235 #define dprintf5(str,arg1,arg2,arg3,arg4,arg5)\
236   dpf(str, arg1, arg2, arg3, arg4, arg5)
237 #define dlprintf5(str,arg1,arg2,arg3,arg4,arg5)\
238   (_dpl dprintf5(str, arg1, arg2, arg3, arg4, arg5))
239 #define dprintf6(str,arg1,arg2,arg3,arg4,arg5,arg6)\
240   dpf(str, arg1, arg2, arg3, arg4, arg5, arg6)
241 #define dlprintf6(str,arg1,arg2,arg3,arg4,arg5,arg6)\
242   (_dpl dprintf6(str, arg1, arg2, arg3, arg4, arg5, arg6))
243 #define dprintf7(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7)\
244   dpf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
245 #define dlprintf7(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7)\
246   (_dpl dprintf7(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7))
247 #define dprintf8(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8)\
248   dpf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
249 #define dlprintf8(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8)\
250   (_dpl dprintf8(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8))
251 #define dprintf9(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9)\
252   dpf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
253 #define dlprintf9(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9)\
254   (_dpl dprintf9(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9))
255 #define dprintf10(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10)\
256   dpf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10)
257 #define dlprintf10(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10)\
258   (_dpl dprintf10(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10))
259 #define dprintf11(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,arg11)\
260   dpf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11)
261 #define dlprintf11(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,arg11)\
262   (_dpl dprintf11(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11))
263 #define dprintf12(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,arg11,arg12)\
264   dpf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12)
265 #define dlprintf12(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,arg11,arg12)\
266   (_dpl dprintf12(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12))
267 
268 void printf_program_ident(const gs_memory_t *mem, const char *program_name, long revision_number);
269 void eprintf_program_ident(const char *program_name, long revision_number);
270 void emprintf_program_ident(const gs_memory_t *mem,
271                             const char *program_name,
272                             long revision_number);
273 const char *gs_program_name(void);
274 long gs_revision_number(void);
275 
276 #define _epi eprintf_program_ident(gs_program_name(), gs_revision_number()),
277 
278 #define eprintf(str)\
279   (_epi epf(str))
280 #define eprintf1(str,arg1)\
281   (_epi epf(str, arg1))
282 #define eprintf2(str,arg1,arg2)\
283   (_epi epf(str, arg1, arg2))
284 #define eprintf3(str,arg1,arg2,arg3)\
285   (_epi epf(str, arg1, arg2, arg3))
286 #define eprintf4(str,arg1,arg2,arg3,arg4)\
287   (_epi epf(str, arg1, arg2, arg3, arg4))
288 #define eprintf5(str,arg1,arg2,arg3,arg4,arg5)\
289   (_epi epf(str, arg1, arg2, arg3, arg4, arg5))
290 #define eprintf6(str,arg1,arg2,arg3,arg4,arg5,arg6)\
291   (_epi epf(str, arg1, arg2, arg3, arg4, arg5, arg6))
292 #define eprintf7(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7)\
293   (_epi epf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7))
294 #define eprintf8(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8)\
295   (_epi epf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8))
296 #define eprintf9(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9)\
297   (_epi epf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9))
298 #define eprintf10(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10)\
299   (_epi epf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10))
300 
301 #define _epim(mem) emprintf_program_ident(mem, gs_program_name(), gs_revision_number()),
302 
303 #define emprintf(mem, str)\
304   (_epim(mem) epfm(mem, str))
305 #define emprintf1(mem, str,arg1)\
306   (_epim(mem) epfm(mem, str, arg1))
307 #define emprintf2(mem, str,arg1,arg2)\
308   (_epim(mem) epfm(mem, str, arg1, arg2))
309 #define emprintf3(mem, str,arg1,arg2,arg3)\
310   (_epim(mem) epfm(mem, str, arg1, arg2, arg3))
311 #define emprintf4(mem, str,arg1,arg2,arg3,arg4)\
312   (_epim(mem) epfm(mem, str, arg1, arg2, arg3, arg4))
313 #define emprintf5(mem, str,arg1,arg2,arg3,arg4,arg5)\
314   (_epim(mem) epfm(mem, str, arg1, arg2, arg3, arg4, arg5))
315 #define emprintf6(mem, str,arg1,arg2,arg3,arg4,arg5,arg6)\
316   (_epim(mem) epfm(mem, str, arg1, arg2, arg3, arg4, arg5, arg6))
317 #define emprintf7(mem, str,arg1,arg2,arg3,arg4,arg5,arg6,arg7)\
318   (_epim(mem) epfm(mem, str, arg1, arg2, arg3, arg4, arg5, arg6, arg7))
319 #define emprintf8(mem, str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8)\
320   (_epim(mem) epfm(mem, str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8))
321 #define emprintf9(mem, str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9)\
322   (_epim(mem) epfm(mem, str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9))
323 #define emprintf10(mem, str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10)\
324   (_epim(mem) epfm(mem, str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10))
325 
326 #if __LINE__			/* compiler provides it */
327 void lprintf_file_and_line(const char *, int);
328 #  define _epl _epi lprintf_file_and_line(__FILE__, __LINE__),
329 #else
330 void lprintf_file_only(const char *);
331 #  define _epl _epi lprintf_file_only(__FILE__)
332 #endif
333 
334 #define lprintf(str)\
335   (_epl epf(str))
336 #define lprintf1(str,arg1)\
337   (_epl epf(str, arg1))
338 #define lprintf2(str,arg1,arg2)\
339   (_epl epf(str, arg1, arg2))
340 #define lprintf3(str,arg1,arg2,arg3)\
341   (_epl epf(str, arg1, arg2, arg3))
342 #define lprintf4(str,arg1,arg2,arg3,arg4)\
343   (_epl epf(str, arg1, arg2, arg3, arg4))
344 #define lprintf5(str,arg1,arg2,arg3,arg4,arg5)\
345   (_epl epf(str, arg1, arg2, arg3, arg4, arg5))
346 #define lprintf6(str,arg1,arg2,arg3,arg4,arg5,arg6)\
347   (_epl epf(str, arg1, arg2, arg3, arg4, arg5, arg6))
348 #define lprintf7(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7)\
349   (_epl epf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7))
350 #define lprintf8(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8)\
351   (_epl epf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8))
352 #define lprintf9(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9)\
353   (_epl epf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9))
354 #define lprintf10(str,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10)\
355   (_epl epf(str, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10))
356 
357 /*
358  * Define the prototype for module initialization procedures.  This is not
359  * a very good place to define this, but we can't find a better one.
360  */
361 #ifndef gs_memory_DEFINED
362 #  define gs_memory_DEFINED
363 typedef struct gs_memory_s gs_memory_t;
364 #endif
365 #define init_proc(proc)\
366   int proc(gs_memory_t *)
367 
368 #endif /* std_INCLUDED */
369