1 // basic headers
2 #define _GNU_SOURCE /* really just for strndup */
3 
4 #include "config.h"
5 
6 #ifdef HAVE_STDBOOL_H
7 # include <stdbool.h>
8 #else
9 # ifndef HAVE__BOOL
10 #  ifdef __cplusplus
11 typedef bool _Bool;
12 #  else
13 #   define _Bool signed char
14 #  endif
15 # endif
16 # define bool _Bool
17 # define false 0
18 # define true 1
19 # define __bool_true_false_are_defined 1
20 #endif
21 
22 #include <stdio.h>
23 
24 #ifdef HAVE_STDLIB_H
25 #include <stdlib.h>
26 #endif
27 
28 #ifdef HAVE_STDINT_H
29 #include <stdint.h>
30 #endif
31 
32 #ifdef HAVE_STRINGS_H
33 #include <strings.h>
34 #endif
35 
36 #ifdef HAVE_STRING_H
37 #include <string.h>
38 #endif
39 
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43 
44 #ifdef HAVE_INTTYPES_H
45 #include <inttypes.h>
46 #endif
47 
48 #ifdef HAVE_MEMORY_H
49 #include <memory.h>
50 #endif
51 
52 #ifdef HAVE_SYS_STAT_H
53 #include <sys/stat.h>
54 #endif
55 
56 #ifdef HAVE_SYS_TYPES_H
57 #include <sys/types.h>
58 #endif
59 
60 #ifdef HAVE_GETOPT_H
61 #include <getopt.h>
62 #endif
63 
64 #ifdef HAVE_IO_H
65 #include <io.h>
66 #endif
67 
68 #ifdef HAVE_ICONV
69 #include <iconv.h>
70 #endif
71 
72 // this doesn't really belong here, but it was easiest
73 #ifdef HAVE_MAGICK
74 #define BUILDSPEC_MAGICK " imagemagick"
75 #else
76 #ifdef HAVE_GMAGICK
77 #define BUILDSPEC_MAGICK " graphicsmagick"
78 #else
79 #define BUILDSPEC_MAGICK ""
80 #endif
81 #endif
82 
83 #ifdef HAVE_GETOPT_LONG
84 #define BUILDSPEC_GETOPT " gnugetopt"
85 #else
86 #define BUILDSPEC_GETOPT ""
87 #endif
88 
89 #ifdef HAVE_ICONV
90 #define BUILDSPEC_ICONV " iconv"
91 #else
92 #define BUILDSPEC_ICONV ""
93 #endif
94 
95 #ifdef HAVE_FREETYPE
96 #define BUILDSPEC_FREETYPE " freetype"
97 #else
98 #define BUILDSPEC_FREETYPE ""
99 #endif
100 
101 #ifdef HAVE_FRIBIDI
102 #define BUILDSPEC_FRIBIDI " fribidi"
103 #else
104 #define BUILDSPEC_FRIBIDI ""
105 #endif
106 
107 #ifdef HAVE_FONTCONFIG
108 #define BUILDSPEC_FONTCONFIG " fontconfig"
109 #else
110 #define BUILDSPEC_FONTCONFIG ""
111 #endif
112 
113 #define BUILDSPEC BUILDSPEC_GETOPT BUILDSPEC_MAGICK BUILDSPEC_ICONV BUILDSPEC_FREETYPE BUILDSPEC_FRIBIDI BUILDSPEC_FONTCONFIG
114 
115 #ifdef HAVE_ICONV
116 
117 #define ICONV_NULL ((iconv_t)-1)
118 extern const char * default_charset;
119   /* the name of the default character set to use, depending on user's locale settings */
120 
121 #endif /*HAVE_ICONV*/
122 
123 void strconcat
124   (
125     char * dest,
126     size_t maxdestlen,
127     const char * src
128   );
129   /* appends null-terminated src onto dest, ensuring length of contents
130     of latter (including terminating null) do not exceed maxdestlen. */
131 
132 unsigned int strtounsigned
133   (
134     const char * s,
135     const char * what /* description of what I'm trying to convert, for error message */
136   );
137   /* parses s as an unsigned decimal integer, returning its value. Aborts the
138     program on error. */
139 
140 int strtosigned
141   (
142     const char * s,
143     const char * what /* description of what I'm trying to convert, for error message */
144   );
145   /* parses s as a signed decimal integer, returning its value. Aborts the
146     program on error. */
147 
148 #ifndef HAVE_STRNDUP
149 char * strndup
150   (
151     const char * s,
152     size_t n
153   );
154 #endif
155 
156 char * sprintf_alloc
157   (
158     const char * format,
159     ...
160   );
161   /* does the equivalent of sprintf(3) on the args, except the output string buffer
162     is dynamically allocated to be exactly big enough to hold the formatted data.
163     The result is the allocated and filled-in string buffer.
164     On failure, the result will be NULL and errno will contain the error. */
165 
166 char * str_extract_until
167   (
168     const char ** src,
169     const char * delim
170   );
171   /* scans *src, looking for the first occurrence of a character in delim. Returns
172     a copy of the prior part of *src if found, and updates *src to point after the
173     delimiter character; else returns a copy of the whole of *src, and sets *src
174     to NULL. Returns NULL iff *src is NULL. */
175 
176 void init_locale();
177   /* does locale initialization and initializes default_charset. */
178 
179 char * locale_decode
180   (
181     const char * localestr
182   );
183   /* allocates and returns a string containing the UTF-8 representation of
184     localestr interpreted according to the user's locale settings. */
185 
186 #if !HAVE_DECL_O_BINARY
187 #define O_BINARY 0
188 #endif
189 
190 #if defined(HAVE_SETMODE) && HAVE_DECL_O_BINARY
191 #define win32_setmode setmode
192 #else
193 #define win32_setmode(x,y)
194 #endif
195 
196 #define PACKAGE_HEADER(x) PACKAGE_NAME "::" x ", version " PACKAGE_VERSION ".\nBuild options:" BUILDSPEC "\nSend bug reports to <" PACKAGE_BUGREPORT ">\n\n"
197 
198 #ifndef HAVE_FT2BUILD_H
199 #define FT_FREETYPE_H <freetype/freetype.h>
200 #define FT_GLYPH_H <freetype/ftglyph.h>
201 #endif
202 
203 enum {VF_NONE=0,VF_NTSC=1,VF_PAL=2}; /* values for videodesc.vformat in da-internal.h as well as other uses */
204 
205 typedef struct
206   {
207     unsigned char r, g, b, a;
208   } colorspec;
209 
210 #if HAVE_ICONV && LOCALIZE_FILENAMES
211 
212 char * localize_filename(const char * pathname);
213   /* converts a filename from UTF-8 to localized encoding. */
214 
215 #else
216 #    define localize_filename(pathname) (strdup(pathname))
217 #endif
218 
219 /* values for vfile.ftype */
220 #define VFTYPE_FILE 0 /* an actual file I opened */
221 #define VFTYPE_PIPE 1 /* an actual pipe I opened to/from a child process */
222 #define VFTYPE_REDIR 2 /* a redirection to/from another already-opened file */
223 struct vfile /* for keeping track of files opened by varied_open */
224   {
225     FILE * h; /* do your I/O to/from this */
226     int ftype, mode; /* for use by varied_close */
227   } /*vfile*/;
228 
229 struct vfile varied_open
230   (
231     const char * fname,
232     int mode, /* either O_RDONLY or O_WRONLY, nothing more */
233     const char * what /* description of what I'm trying to open, for error message */
234   );
235   /* opens the file fname, which can be an ordinary file name or take one of the
236     following special forms:
237         "-" -- refers to standard input (if mode is O_RDONLY) or output (if O_WRONLY)
238         "&n" -- (n integer) refers to the already-opened file handle n
239         "cmd|" -- spawns cmd as a subprocess and reads from its standard output
240         "|cmd" -- spawns cmd as a subprocess and writes to its standard input.
241 
242     Will abort the process on any errors.
243   */
244 
245 void varied_close(struct vfile vf);
246   /* closes a file previously opened by varied_open. */
247 
248 colorspec parse_color
249   (
250     const char * colorstr,
251     const char * what /* additional explanatory text for error message */
252   );
253   /* parses colorstr and returns the resulting colour. Will abort the process
254     on any errors. */
255