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