Merge branch 'topic/earlyfix'
[s-roff.git] / include / nonposix.h
blob5aadc1ee35047a454f3dd9737fe884ec3658a5ca
1 /*@ This header file compartmentalize all idiosyncrasies of non-Posix
2 *@ systems, such as MS-DOS, MS-Windows, etc. It should be loaded after
3 *@ the system headers like stdio.h to protect against warnings and error
4 *@ messages w.r.t. redefining macros.
6 * Copyright (c) 2014 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
8 * Copyright (C) 2000 - 2005
9 * Free Software Foundation, Inc.
10 * Written by Eli Zaretskii (eliz@is.elta.co.il)
12 * This is free software; you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2, or (at your option) any later
15 * version.
17 * This is distributed in the hope that it will be useful, but WITHOUT ANY
18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with groff; see the file COPYING. If not, write to the Free Software
24 * Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA.
26 #ifndef _NONPOSIX_H
27 #define _NONPOSIX_H
29 #include "config.h"
31 #if defined _MSC_VER
32 # ifndef _WIN32
33 # define _WIN32
34 # endif
35 #endif
37 #if defined(__MSDOS__) || defined(__EMX__) \
38 || (defined(_WIN32) && !defined(_UWIN) && !defined(__CYGWIN__))
40 /* Binary I/O nuisances. */
41 # include <fcntl.h>
42 # include <io.h>
43 # ifdef HAVE_UNISTD_H
44 # include <unistd.h>
45 # endif
46 # ifndef STDIN_FILENO
47 # define STDIN_FILENO 0
48 # define STDOUT_FILENO 1
49 # define STDERR_FILENO 2
50 # endif
51 # ifdef HAVE_DIRECT_H
52 # include <direct.h>
53 # endif
54 # ifdef HAVE_PROCESS_H
55 # include <process.h>
56 # endif
57 # if defined(_MSC_VER) || defined(__MINGW32__)
58 # define POPEN_RT "rt"
59 # define POPEN_WT "wt"
60 # define popen(c,m) _popen(c,m)
61 # define pclose(p) _pclose(p)
62 # define pipe(pfd) _pipe((pfd),0,_O_BINARY|_O_NOINHERIT)
63 # define mkdir(p,m) _mkdir(p)
64 # define setmode(f,m) _setmode(f,m)
65 # define WAIT(s,p,m) _cwait(s,p,m)
66 # define creat(p,m) _creat(p,m)
67 # define read(f,b,s) _read(f,b,s)
68 # define write(f,b,s) _write(f,b,s)
69 # define dup(f) _dup(f)
70 # define dup2(f1,f2) _dup2(f1,f2)
71 # define close(f) _close(f)
72 # define isatty(f) _isatty(f)
73 # define access(p,m) _access(p,m)
74 # endif
75 # define SET_BINARY(f) do {if (!isatty(f)) setmode(f,O_BINARY);} while(0)
76 # define FOPEN_RB "rb"
77 # define FOPEN_WB "wb"
78 # define FOPEN_RWB "wb+"
79 # ifndef O_BINARY
80 # ifdef _O_BINARY
81 # define O_BINARY (_O_BINARY)
82 # endif
83 # endif
85 /* The system shell. Groff assumes a Unixy shell, but non-Posix
86 systems don't have standard places where it lives, and might not
87 have it installed to begin with. We want to give them some leeway. */
88 # ifdef __EMX__
89 # define getcwd(b,s) _getcwd2(b,s)
90 # else
91 # define BSHELL (system_shell_name())
92 # define BSHELL_DASH_C (system_shell_dash_c())
93 # define IS_BSHELL(s) (is_system_shell(s))
94 # endif
96 /* The separator for directories in PATH and other environment
97 variables. */
98 # define PATH_SEP ";"
99 # define PATH_SEP_CHAR ';'
101 /* Characters that separate directories in a path name. */
102 # define DIR_SEPS "/\\:"
104 /* How to tell if the argument is an absolute file name. */
105 # define IS_ABSOLUTE(f) \
106 ((f)[0] == '/' || (f)[0] == '\\' || (f)[0] && (f)[1] == ':')
108 /* The executable extension. */
109 # define EXE_EXT ".exe"
111 /* Possible executable extensions. */
112 # define PATH_EXT ".com;.exe;.bat;.cmd"
114 /* The system null device. */
115 # define NULL_DEV "NUL"
117 /* The default place to create temporary files. */
118 # ifndef P_tmpdir
119 # ifdef _P_tmpdir
120 # define P_tmpdir _P_tmpdir
121 # else
122 # define P_tmpdir "c:/temp"
123 # endif
124 # endif
126 /* Prototypes. */
127 # ifdef __cplusplus
128 extern "C" {
129 # endif
130 char * system_shell_name(void);
131 const char * system_shell_dash_c(void);
132 int is_system_shell(const char *);
133 # ifdef __cplusplus
135 # endif
137 #endif
139 #if defined(_WIN32) && !defined(_UWIN) && !defined(__CYGWIN__)
140 /* Win32 implementations which use the Microsoft runtime library
141 * are prone to hanging when a pipe reader quits with unread data in the pipe.
142 * `gtroff' avoids this, by invoking `FLUSH_INPUT_PIPE()', defined as ... */
143 # define FLUSH_INPUT_PIPE(fd) \
144 do if (!isatty(fd)) \
146 char drain[BUFSIZ]; \
147 while (read(fd, drain, sizeof(drain)) > 0) \
149 } while (0)
151 /* The Microsoft runtime library also has a broken argument passing mechanism,
152 * which may result in improper grouping of arguments passed to a child process
153 * by the `spawn()' family of functions. In `groff', only the `spawnvp()'
154 * function is affected; we work around this defect, by substituting a
155 * wrapper function in place of `spawnvp()' calls. */
157 # ifdef __cplusplus
158 extern "C" {
159 # endif
160 int spawnvp_wrapper(int, char *, char **);
161 # ifdef __cplusplus
163 # endif
164 # ifndef SPAWN_FUNCTION_WRAPPERS
165 # undef spawnvp
166 # define spawnvp spawnvp_wrapper
167 # undef _spawnvp
168 # define _spawnvp spawnvp
169 # endif /* SPAWN_FUNCTION_WRAPPERS */
171 #else
172 /* Other implementations do not suffer from Microsoft runtime bugs,
173 * but `gtroff' requires a dummy definition for FLUSH_INPUT_PIPE() */
174 # define FLUSH_INPUT_PIPE(fd) do {} while(0)
175 #endif
177 /* Defaults, for Posix systems. */
179 #ifndef SET_BINARY
180 # define SET_BINARY(f) do {} while(0)
181 #endif
182 #ifndef FOPEN_RB
183 # define FOPEN_RB "r"
184 #endif
185 #ifndef FOPEN_WB
186 # define FOPEN_WB "w"
187 #endif
188 #ifndef FOPEN_RWB
189 # define FOPEN_RWB "w+"
190 #endif
191 #ifndef POPEN_RT
192 # define POPEN_RT "r"
193 #endif
194 #ifndef POPEN_WT
195 # define POPEN_WT "w"
196 #endif
197 #ifndef O_BINARY
198 # define O_BINARY 0
199 #endif
200 #ifndef BSHELL
201 # define BSHELL "/bin/sh"
202 #endif
203 #ifndef BSHELL_DASH_C
204 # define BSHELL_DASH_C "-c"
205 #endif
206 #ifndef IS_BSHELL
207 # define IS_BSHELL(s) ((s) && strcmp(s,BSHELL) == 0)
208 #endif
209 #ifndef PATH_SEP
210 # define PATH_SEP ":"
211 # define PATH_SEP_CHAR ':'
212 #endif
213 #ifndef DIR_SEPS
214 # define DIR_SEPS "/"
215 #endif
216 #ifndef IS_ABSOLUTE
217 # define IS_ABSOLUTE(f) ((f)[0] == '/')
218 #endif
219 #ifndef EXE_EXT
220 # define EXE_EXT ""
221 #endif
222 #ifndef PATH_EXT
223 # define PATH_EXT ""
224 #endif
225 #ifndef NULL_DEV
226 # define NULL_DEV "/dev/null"
227 #endif
228 #ifndef GS_NAME
229 # define GS_NAME "gs"
230 #endif
231 #ifndef WAIT
232 # define WAIT(s,p,m) wait(s)
233 #endif
234 #ifndef _WAIT_CHILD
235 # define _WAIT_CHILD 0
236 #endif
238 #endif // _NONPOSIX_H
239 // s-it2-mode