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