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