1.0.11.30: restore buildability on Windows after 1.0.11.27.
[sbcl.git] / tools-for-build / grovel-headers.c
1 /*
2  * Rummage through the system header files using the C compiler itself
3  * as a parser, extracting stuff like preprocessor constants and the
4  * sizes and signedness of basic system types, and write it out as
5  * Lisp code.
6  */
7
8 /*
9  * This software is part of the SBCL system. See the README file for
10  * more information.
11  *
12  * While most of SBCL is derived from the CMU CL system, many
13  * utilities for the build process (like this one) were written from
14  * scratch after the fork from CMU CL.
15  *
16  * This software is in the public domain and is provided with
17  * absolutely no warranty. See the COPYING and CREDITS files for
18  * more information.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #ifdef _WIN32
25   #define WIN32_LEAN_AND_MEAN
26   #include <windows.h>
27   #include <shlobj.h>
28 #else
29   #include <sys/times.h>
30   #include <sys/wait.h>
31   #include <sys/ioctl.h>
32   #include <sys/termios.h>
33   #ifdef __APPLE_CC__
34     #include "../src/runtime/darwin-dlshim.h"
35     #include "../src/runtime/darwin-langinfo.h"
36   #else
37     #include <dlfcn.h>
38     #include <langinfo.h>
39   #endif
40 #endif
41
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #include <unistd.h>
45 #include <signal.h>
46 #include <errno.h>
47
48 #include "genesis/config.h"
49
50 #include "wrap.h"
51
52 #define DEFTYPE(lispname,cname) { cname foo; \
53     printf("(define-alien-type " lispname " (%s %d))\n", (((foo=-1)<0) ? "sb!alien:signed" : "unsigned"), (8 * (sizeof foo))); }
54
55 void
56 defconstant(char* lisp_name, unsigned long unix_number)
57 {
58     printf("(defconstant %s %lu) ; #x%lx\n",
59            lisp_name, unix_number, unix_number);
60 }
61
62 void deferrno(char* lisp_name, unsigned long unix_number)
63 {
64     defconstant(lisp_name, unix_number);
65 }
66
67 void defsignal(char* lisp_name, unsigned long unix_number)
68 {
69     defconstant(lisp_name, unix_number);
70 }
71
72 int
73 main(int argc, char *argv[])
74 {
75     /* don't need no steenking command line arguments */
76     if (1 != argc) {
77         fprintf(stderr, "argh! command line argument(s)\n");
78         exit(1);
79     }
80
81     /* don't need no steenking hand-editing */
82     printf(
83 ";;;; This is an automatically generated file, please do not hand-edit it.\n\
84 ;;;; See the program \"grovel-headers.c\".\n\
85 \n\
86 ");
87 #ifdef _WIN32
88     printf("(in-package \"SB!WIN32\")\n\n");
89
90     defconstant ("input-record-size", sizeof (INPUT_RECORD));
91
92     defconstant ("MAX_PATH", MAX_PATH);
93
94     printf(";;; CSIDL\n");
95
96     defconstant ("CSIDL_DESKTOP", CSIDL_DESKTOP);
97     defconstant ("CSIDL_INTERNET", CSIDL_INTERNET);
98     defconstant ("CSIDL_PROGRAMS", CSIDL_PROGRAMS);
99     defconstant ("CSIDL_CONTROLS", CSIDL_CONTROLS);
100     defconstant ("CSIDL_PRINTERS", CSIDL_PRINTERS);
101     defconstant ("CSIDL_PERSONAL", CSIDL_PERSONAL);
102     defconstant ("CSIDL_FAVORITES", CSIDL_FAVORITES);
103     defconstant ("CSIDL_STARTUP", CSIDL_STARTUP);
104     defconstant ("CSIDL_RECENT", CSIDL_RECENT);
105     defconstant ("CSIDL_SENDTO", CSIDL_SENDTO);
106     defconstant ("CSIDL_BITBUCKET", CSIDL_BITBUCKET);
107     defconstant ("CSIDL_STARTMENU", CSIDL_STARTMENU);
108     defconstant ("CSIDL_DESKTOPDIRECTORY", CSIDL_DESKTOPDIRECTORY);
109     defconstant ("CSIDL_DRIVES", CSIDL_DRIVES);
110     defconstant ("CSIDL_NETWORK", CSIDL_NETWORK);
111     defconstant ("CSIDL_NETHOOD", CSIDL_NETHOOD);
112     defconstant ("CSIDL_FONTS", CSIDL_FONTS);
113     defconstant ("CSIDL_TEMPLATES", CSIDL_TEMPLATES);
114     defconstant ("CSIDL_COMMON_STARTMENU", CSIDL_COMMON_STARTMENU);
115     defconstant ("CSIDL_COMMON_PROGRAMS", CSIDL_COMMON_PROGRAMS);
116     defconstant ("CSIDL_COMMON_STARTUP", CSIDL_COMMON_STARTUP);
117     defconstant ("CSIDL_COMMON_DESKTOPDIRECTORY", CSIDL_COMMON_DESKTOPDIRECTORY);
118     defconstant ("CSIDL_APPDATA", CSIDL_APPDATA);
119     defconstant ("CSIDL_PRINTHOOD", CSIDL_PRINTHOOD);
120     defconstant ("CSIDL_LOCAL_APPDATA", CSIDL_LOCAL_APPDATA);
121     defconstant ("CSIDL_ALTSTARTUP", CSIDL_ALTSTARTUP);
122     defconstant ("CSIDL_COMMON_ALTSTARTUP", CSIDL_COMMON_ALTSTARTUP);
123     defconstant ("CSIDL_COMMON_FAVORITES", CSIDL_COMMON_FAVORITES);
124     defconstant ("CSIDL_INTERNET_CACHE", CSIDL_INTERNET_CACHE);
125     defconstant ("CSIDL_COOKIES", CSIDL_COOKIES);
126     defconstant ("CSIDL_HISTORY", CSIDL_HISTORY);
127     defconstant ("CSIDL_COMMON_APPDATA", CSIDL_COMMON_APPDATA);
128     defconstant ("CSIDL_WINDOWS", CSIDL_WINDOWS);
129     defconstant ("CSIDL_SYSTEM", CSIDL_SYSTEM);
130     defconstant ("CSIDL_PROGRAM_FILES", CSIDL_PROGRAM_FILES);
131     defconstant ("CSIDL_MYPICTURES", CSIDL_MYPICTURES);
132     defconstant ("CSIDL_PROFILE", CSIDL_PROFILE);
133     defconstant ("CSIDL_SYSTEMX86", CSIDL_SYSTEMX86);
134     defconstant ("CSIDL_PROGRAM_FILESX86", CSIDL_PROGRAM_FILESX86);
135     defconstant ("CSIDL_PROGRAM_FILES_COMMON", CSIDL_PROGRAM_FILES_COMMON);
136     defconstant ("CSIDL_PROGRAM_FILES_COMMONX86", CSIDL_PROGRAM_FILES_COMMONX86);
137     defconstant ("CSIDL_COMMON_TEMPLATES", CSIDL_COMMON_TEMPLATES);
138     defconstant ("CSIDL_COMMON_DOCUMENTS", CSIDL_COMMON_DOCUMENTS);
139     defconstant ("CSIDL_COMMON_ADMINTOOLS", CSIDL_COMMON_ADMINTOOLS);
140     defconstant ("CSIDL_ADMINTOOLS", CSIDL_ADMINTOOLS);
141     defconstant ("CSIDL_CONNECTIONS", CSIDL_CONNECTIONS);
142     defconstant ("CSIDL_COMMON_MUSIC", CSIDL_COMMON_MUSIC);
143     defconstant ("CSIDL_COMMON_PICTURES", CSIDL_COMMON_PICTURES);
144     defconstant ("CSIDL_COMMON_VIDEO", CSIDL_COMMON_VIDEO);
145     defconstant ("CSIDL_RESOURCES", CSIDL_RESOURCES);
146     defconstant ("CSIDL_RESOURCES_LOCALIZED", CSIDL_RESOURCES_LOCALIZED);
147     defconstant ("CSIDL_COMMON_OEM_LINKS", CSIDL_COMMON_OEM_LINKS);
148     defconstant ("CSIDL_CDBURN_AREA", CSIDL_CDBURN_AREA);
149     defconstant ("CSIDL_COMPUTERSNEARME", CSIDL_COMPUTERSNEARME);
150     defconstant ("CSIDL_FLAG_DONT_VERIFY", CSIDL_FLAG_DONT_VERIFY);
151     defconstant ("CSIDL_FLAG_CREATE", CSIDL_FLAG_CREATE);
152     defconstant ("CSIDL_FLAG_MASK", CSIDL_FLAG_MASK);
153
154     printf(";;; Exception codes\n");
155     defconstant("+exception-access-violation+", EXCEPTION_ACCESS_VIOLATION);
156     defconstant("+exception-array-bounds-exceeded+", EXCEPTION_ARRAY_BOUNDS_EXCEEDED);
157     defconstant("+exception-breakpoint+", EXCEPTION_BREAKPOINT);
158     defconstant("+exception-datatype-misalignment+", EXCEPTION_DATATYPE_MISALIGNMENT);
159     defconstant("+exception-flt-denormal-operand+", EXCEPTION_FLT_DENORMAL_OPERAND);
160     defconstant("+exception-flt-divide-by-zero+", EXCEPTION_FLT_DIVIDE_BY_ZERO);
161     defconstant("+exception-flt-inexact-result+", EXCEPTION_FLT_INEXACT_RESULT);
162     defconstant("+exception-flt-invalid-operation+", EXCEPTION_FLT_INVALID_OPERATION);
163     defconstant("+exception-flt-overflow+", EXCEPTION_FLT_OVERFLOW);
164     defconstant("+exception-flt-stack-check+", EXCEPTION_FLT_STACK_CHECK);
165     defconstant("+exception-flt-underflow+", EXCEPTION_FLT_UNDERFLOW);
166     defconstant("+exception-illegal-instruction+", EXCEPTION_ILLEGAL_INSTRUCTION);
167     defconstant("+exception-in-page-error+", EXCEPTION_IN_PAGE_ERROR);
168     defconstant("+exception-int-divide-by-zero+", EXCEPTION_INT_DIVIDE_BY_ZERO);
169     defconstant("+exception-int-overflow+", EXCEPTION_INT_OVERFLOW);
170     defconstant("+exception-invalid-disposition+", EXCEPTION_INVALID_DISPOSITION);
171     defconstant("+exception-noncontinuable-exception+", EXCEPTION_NONCONTINUABLE_EXCEPTION);
172     defconstant("+exception-priv-instruction+", EXCEPTION_PRIV_INSTRUCTION);
173     defconstant("+exception-single-step+", EXCEPTION_SINGLE_STEP);
174     defconstant("+exception-stack-overflow+", EXCEPTION_STACK_OVERFLOW);
175
176     printf(";;; FormatMessage\n");
177
178     defconstant ("FORMAT_MESSAGE_ALLOCATE_BUFFER", FORMAT_MESSAGE_ALLOCATE_BUFFER);
179     defconstant ("FORMAT_MESSAGE_FROM_SYSTEM", FORMAT_MESSAGE_FROM_SYSTEM);
180
181     printf(";;; Errors\n");
182
183     defconstant ("ERROR_ENVVAR_NOT_FOUND", ERROR_ENVVAR_NOT_FOUND);
184
185     printf(";;; GetComputerName\n");
186
187     defconstant ("MAX_COMPUTERNAME_LENGTH", MAX_COMPUTERNAME_LENGTH);
188     defconstant ("ERROR_BUFFER_OVERFLOW", ERROR_BUFFER_OVERFLOW);
189
190     printf(";;; Windows Types\n");
191     DEFTYPE("int-ptr", INT_PTR);
192     DEFTYPE("dword",   DWORD);
193     DEFTYPE("bool",    BOOL);
194     DEFTYPE("uint",    UINT);
195     DEFTYPE("ulong",   ULONG);
196
197     /* FIXME: SB-UNIX and SB-WIN32 really need to be untangled. */
198     printf("(in-package \"SB!UNIX\")\n\n");
199     printf(";;; Unix-like constants and types on Windows\n");
200     defconstant("o_rdonly", _O_RDONLY);
201     defconstant("o_wronly", _O_WRONLY);
202     defconstant("o_rdwr",   _O_RDWR);
203     defconstant("o_creat",  _O_CREAT);
204     defconstant("o_trunc",  _O_TRUNC);
205     defconstant("o_append", _O_APPEND);
206     defconstant("o_excl",   _O_EXCL);
207     defconstant("o_binary", _O_BINARY);
208
209     defconstant("enoent", ENOENT);
210     defconstant("eexist", EEXIST);
211
212     defconstant("s-ifmt",  S_IFMT);
213     defconstant("s-ifdir", S_IFDIR);
214     defconstant("s-ifreg", S_IFREG);
215
216     DEFTYPE("ino-t",  ino_t);
217     DEFTYPE("time-t", time_t);
218     DEFTYPE("off-t",  off_t);
219     DEFTYPE("size-t", size_t);
220     DEFTYPE("mode-t", mode_t);
221
222     DEFTYPE("wst-dev-t", wst_dev_t);
223     DEFTYPE("wst-off-t", wst_off_t);
224     DEFTYPE("wst-blksize-t", wst_blksize_t);
225     DEFTYPE("wst-blkcnt-t", wst_blkcnt_t);
226     DEFTYPE("wst-nlink-t", wst_nlink_t);
227     DEFTYPE("wst-uid-t", wst_uid_t);
228     DEFTYPE("wst-gid-t", wst_gid_t);
229     printf("\n");
230 #else
231     printf("(in-package \"SB!ALIEN\")\n\n");
232
233     printf (";;;flags for dlopen()\n");
234
235     defconstant ("rtld-lazy", RTLD_LAZY);
236     defconstant ("rtld-now", RTLD_NOW);
237     defconstant ("rtld-global", RTLD_GLOBAL);
238
239     printf("(in-package \"SB!UNIX\")\n\n");
240
241     printf(";;; langinfo\n");
242     defconstant("codeset", CODESET);
243
244     printf(";;; types, types, types\n");
245     DEFTYPE("clock-t", clock_t);
246     DEFTYPE("dev-t",   dev_t);
247     DEFTYPE("gid-t",   gid_t);
248     DEFTYPE("ino-t",   ino_t);
249     DEFTYPE("mode-t",  mode_t);
250     DEFTYPE("nlink-t", nlink_t);
251     DEFTYPE("off-t",   off_t);
252     DEFTYPE("size-t",  size_t);
253     DEFTYPE("time-t",  time_t);
254 #if !defined(LISP_FEATURE_OS_PROVIDES_SUSECONDS_T)
255     /* Similar kludge in sb-posix. */
256     DEFTYPE("suseconds-t", long);
257 #else
258     DEFTYPE("suseconds-t", suseconds_t);
259 #endif
260     DEFTYPE("uid-t",   uid_t);
261     printf(";; Types in src/runtime/wrap.h. See that file for explantion.\n");
262     printf(";; Don't use these types for anything other than the stat wrapper.\n");
263     DEFTYPE("wst-dev-t", wst_dev_t);
264     DEFTYPE("wst-off-t", wst_off_t);
265     DEFTYPE("wst-blksize-t", wst_blksize_t);
266     DEFTYPE("wst-blkcnt-t", wst_blkcnt_t);
267     DEFTYPE("wst-nlink-t", wst_nlink_t);
268     DEFTYPE("wst-uid-t", wst_uid_t);
269     DEFTYPE("wst-gid-t", wst_gid_t);
270     printf("\n");
271
272     printf(";;; fcntl.h (or unistd.h on OpenBSD and NetBSD)\n");
273     defconstant("r_ok", R_OK);
274     defconstant("w_ok", W_OK);
275     defconstant("x_ok", X_OK);
276     defconstant("f_ok", F_OK);
277     printf("\n");
278
279     printf(";;; fcntlbits.h\n");
280     defconstant("o_rdonly",  O_RDONLY);
281     defconstant("o_wronly",  O_WRONLY);
282     defconstant("o_rdwr",    O_RDWR);
283     defconstant("o_accmode", O_ACCMODE);
284     defconstant("o_creat",   O_CREAT);
285     defconstant("o_excl",    O_EXCL);
286     defconstant("o_noctty",  O_NOCTTY);
287     defconstant("o_trunc",   O_TRUNC);
288     defconstant("o_append",  O_APPEND);
289 #ifdef LISP_FEATURE_LARGEFILE
290     defconstant("o_largefile", O_LARGEFILE);
291 #endif
292
293     printf(";;;\n");
294     defconstant("s-ifmt",  S_IFMT);
295     defconstant("s-ififo", S_IFIFO);
296     defconstant("s-ifchr", S_IFCHR);
297     defconstant("s-ifdir", S_IFDIR);
298     defconstant("s-ifblk", S_IFBLK);
299     defconstant("s-ifreg", S_IFREG);
300     printf("\n");
301
302     defconstant("s-iflnk",  S_IFLNK);
303     defconstant("s-ifsock", S_IFSOCK);
304     printf("\n");
305
306     printf(";;; error numbers\n");
307     deferrno("enoent", ENOENT);
308     deferrno("eintr", EINTR);
309     deferrno("eio", EIO);
310     deferrno("eexist", EEXIST);
311     deferrno("espipe", ESPIPE);
312     deferrno("ewouldblock", EWOULDBLOCK);
313     printf("\n");
314
315     printf(";;; for wait3(2) in run-program.lisp\n");
316     defconstant("wnohang", WNOHANG);
317     defconstant("wuntraced", WUNTRACED);
318     printf("\n");
319
320     printf(";;; various ioctl(2) flags\n");
321     defconstant("tiocnotty",  TIOCNOTTY);
322     defconstant("tiocgwinsz", TIOCGWINSZ);
323     defconstant("tiocswinsz", TIOCSWINSZ);
324     defconstant("tiocgpgrp",  TIOCGPGRP);
325     defconstant("tiocspgrp",  TIOCSPGRP);
326     /* KLUDGE: These are referenced by old CMUCL-derived code, but
327      * Linux doesn't define them.
328      *
329      * I think these are the BSD names, but I don't know what the
330      * corresponding SysV/Linux names are. As a point of reference,
331      * CMUCL doesn't have these defined either (although the defining
332      * forms *do* exist in src/code/unix.lisp), so I don't feel nearly
333      * so bad about not hunting them down. Insight into renamed
334      * obscure ioctl(2) flags appreciated. --njf, 2002-08-26
335      *
336      * I note that the first one I grepped for, TIOCSIGSEND, is
337      * referenced in SBCL conditional on #+HPUX. Maybe the porters of
338      * Oxbridge know more about things like that? And even if they
339      * don't, one benefit of the Rhodes crusade to heal the worthy
340      * ports should be that afterwards, if we grep for something like
341      * this in CVS and it's not there, we can lightheartedly nuke it.
342      * -- WHN 2002-08-30 */
343     /*
344       defconstant("tiocsigsend", TIOCSIGSEND);
345       defconstant("tiocflush", TIOCFLUSH);
346       defconstant("tiocgetp", TIOCGETP);
347       defconstant("tiocsetp", TIOCSETP);
348       defconstant("tiocgetc", TIOCGETC);
349       defconstant("tiocsetc", TIOCSETC);
350       defconstant("tiocgltc", TIOCGLTC);
351       defconstant("tiocsltc", TIOCSLTC);
352     */
353     printf("\n");
354
355     printf(";;; signals\n");
356     defconstant("sig-dfl", (unsigned long)SIG_DFL);
357     defconstant("sig-ign", (unsigned long)SIG_IGN);
358
359     defsignal("sigalrm", SIGALRM);
360     defsignal("sigbus", SIGBUS);
361     defsignal("sigchld", SIGCHLD);
362     defsignal("sigcont", SIGCONT);
363 #ifdef SIGEMT
364     defsignal("sigemt", SIGEMT);
365 #endif
366     defsignal("sigfpe", SIGFPE);
367     defsignal("sighup", SIGHUP);
368     defsignal("sigill", SIGILL);
369     defsignal("sigint", SIGINT);
370     defsignal("sigio", SIGIO);
371     defsignal("sigiot", SIGIOT);
372     defsignal("sigkill", SIGKILL);
373     defsignal("sigpipe", SIGPIPE);
374     defsignal("sigprof", SIGPROF);
375     defsignal("sigquit", SIGQUIT);
376     defsignal("sigsegv", SIGSEGV);
377 #ifdef SIGSTKFLT
378     defsignal("sigstkflt", SIGSTKFLT);
379 #endif
380     defsignal("sigstop", SIGSTOP);
381 #ifdef SIGSYS
382     defsignal("sigsys", SIGSYS);
383 #endif
384     defsignal("sigterm", SIGTERM);
385     defsignal("sigtrap", SIGTRAP);
386     defsignal("sigtstp", SIGTSTP);
387     defsignal("sigttin", SIGTTIN);
388     defsignal("sigttou", SIGTTOU);
389     defsignal("sigurg", SIGURG);
390     defsignal("sigusr1", SIGUSR1);
391     defsignal("sigusr2", SIGUSR2);
392     defsignal("sigvtalrm", SIGVTALRM);
393 #ifdef SIGWAITING
394     defsignal("sigwaiting", SIGWAITING);
395 #endif
396     defsignal("sigwinch", SIGWINCH);
397 #ifndef SIGXCPU
398     defsignal("sigxcpu", SIGXCPU);
399 #endif
400 #ifdef SIGXFSZ
401     defsignal("sigxfsz", SIGXFSZ);
402 #endif
403
404    /* Floating point exception codes. Some of these
405     * are missing on Darwin. */
406 #ifdef FPE_INTOVF
407     defconstant("fpe-intovf", FPE_INTOVF);
408 #else
409     defconstant("fpe-intovf", -1);
410 #endif
411 #ifdef FPE_INTDIV
412     defconstant("fpe-intdiv", FPE_INTDIV);
413 #else
414     defconstant("fpe-intdiv", -1);
415 #endif
416     defconstant("fpe-fltdiv", FPE_FLTDIV);
417     defconstant("fpe-fltovf", FPE_FLTOVF);
418     defconstant("fpe-fltund", FPE_FLTUND);
419     defconstant("fpe-fltres", FPE_FLTRES);
420     defconstant("fpe-fltinv", FPE_FLTINV);
421 #ifdef FPE_FLTSUB
422     defconstant("fpe-fltsub", FPE_FLTSUB);
423 #else
424     defconstant("fpe-fltsub", -1);
425 #endif
426
427 #endif // !WIN32
428     return 0;
429 }