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