1.0.22.19: Canonicalize whitespace in .asd files.
[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     defconstant("eintr", EINTR);
212     defconstant("eagain", EAGAIN);
213
214     defconstant("s-ifmt",  S_IFMT);
215     defconstant("s-ifdir", S_IFDIR);
216     defconstant("s-ifreg", S_IFREG);
217
218     DEFTYPE("ino-t",  ino_t);
219     DEFTYPE("time-t", time_t);
220     DEFTYPE("off-t",  off_t);
221     DEFTYPE("size-t", size_t);
222     DEFTYPE("mode-t", mode_t);
223
224     DEFTYPE("wst-dev-t", wst_dev_t);
225     DEFTYPE("wst-off-t", wst_off_t);
226     DEFTYPE("wst-blksize-t", wst_blksize_t);
227     DEFTYPE("wst-blkcnt-t", wst_blkcnt_t);
228     DEFTYPE("wst-nlink-t", wst_nlink_t);
229     DEFTYPE("wst-uid-t", wst_uid_t);
230     DEFTYPE("wst-gid-t", wst_gid_t);
231     printf("\n");
232 #else
233     printf("(in-package \"SB!ALIEN\")\n\n");
234
235     printf (";;;flags for dlopen()\n");
236
237     defconstant ("rtld-lazy", RTLD_LAZY);
238     defconstant ("rtld-now", RTLD_NOW);
239     defconstant ("rtld-global", RTLD_GLOBAL);
240
241     printf("(in-package \"SB!UNIX\")\n\n");
242
243     printf(";;; langinfo\n");
244     defconstant("codeset", CODESET);
245
246     printf(";;; types, types, types\n");
247     DEFTYPE("clock-t", clock_t);
248     DEFTYPE("dev-t",   dev_t);
249     DEFTYPE("gid-t",   gid_t);
250     DEFTYPE("ino-t",   ino_t);
251     DEFTYPE("mode-t",  mode_t);
252     DEFTYPE("nlink-t", nlink_t);
253     DEFTYPE("off-t",   off_t);
254     DEFTYPE("size-t",  size_t);
255     DEFTYPE("time-t",  time_t);
256 #if !defined(LISP_FEATURE_OS_PROVIDES_SUSECONDS_T)
257     /* Similar kludge in sb-posix. */
258     DEFTYPE("suseconds-t", long);
259 #else
260     DEFTYPE("suseconds-t", suseconds_t);
261 #endif
262     DEFTYPE("uid-t",   uid_t);
263     printf(";; Types in src/runtime/wrap.h. See that file for explantion.\n");
264     printf(";; Don't use these types for anything other than the stat wrapper.\n");
265     DEFTYPE("wst-dev-t", wst_dev_t);
266     DEFTYPE("wst-off-t", wst_off_t);
267     DEFTYPE("wst-blksize-t", wst_blksize_t);
268     DEFTYPE("wst-blkcnt-t", wst_blkcnt_t);
269     DEFTYPE("wst-nlink-t", wst_nlink_t);
270     DEFTYPE("wst-uid-t", wst_uid_t);
271     DEFTYPE("wst-gid-t", wst_gid_t);
272     printf("\n");
273
274     printf(";;; fcntl.h (or unistd.h on OpenBSD and NetBSD)\n");
275     defconstant("r_ok", R_OK);
276     defconstant("w_ok", W_OK);
277     defconstant("x_ok", X_OK);
278     defconstant("f_ok", F_OK);
279     printf("\n");
280
281     printf(";;; fcntlbits.h\n");
282     defconstant("o_rdonly",  O_RDONLY);
283     defconstant("o_wronly",  O_WRONLY);
284     defconstant("o_rdwr",    O_RDWR);
285     defconstant("o_accmode", O_ACCMODE);
286     defconstant("o_creat",   O_CREAT);
287     defconstant("o_excl",    O_EXCL);
288     defconstant("o_noctty",  O_NOCTTY);
289     defconstant("o_trunc",   O_TRUNC);
290     defconstant("o_append",  O_APPEND);
291 #ifdef LISP_FEATURE_LARGEFILE
292     defconstant("o_largefile", O_LARGEFILE);
293 #endif
294
295     printf(";;;\n");
296     defconstant("s-ifmt",  S_IFMT);
297     defconstant("s-ififo", S_IFIFO);
298     defconstant("s-ifchr", S_IFCHR);
299     defconstant("s-ifdir", S_IFDIR);
300     defconstant("s-ifblk", S_IFBLK);
301     defconstant("s-ifreg", S_IFREG);
302     printf("\n");
303
304     defconstant("s-iflnk",  S_IFLNK);
305     defconstant("s-ifsock", S_IFSOCK);
306     printf("\n");
307
308     printf(";;; error numbers\n");
309     deferrno("ebadf", EBADF);
310     deferrno("enoent", ENOENT);
311     deferrno("eintr", EINTR);
312     deferrno("eagain", EAGAIN);
313     deferrno("eio", EIO);
314     deferrno("eexist", EEXIST);
315     deferrno("eloop", ELOOP);
316     deferrno("espipe", ESPIPE);
317     deferrno("ewouldblock", EWOULDBLOCK);
318     printf("\n");
319
320     printf(";;; for wait3(2) in run-program.lisp\n");
321     defconstant("wnohang", WNOHANG);
322     defconstant("wuntraced", WUNTRACED);
323     printf("\n");
324
325     printf(";;; various ioctl(2) flags\n");
326     defconstant("tiocnotty",  TIOCNOTTY);
327     defconstant("tiocgwinsz", TIOCGWINSZ);
328     defconstant("tiocswinsz", TIOCSWINSZ);
329     defconstant("tiocgpgrp",  TIOCGPGRP);
330     defconstant("tiocspgrp",  TIOCSPGRP);
331     /* KLUDGE: These are referenced by old CMUCL-derived code, but
332      * Linux doesn't define them.
333      *
334      * I think these are the BSD names, but I don't know what the
335      * corresponding SysV/Linux names are. As a point of reference,
336      * CMUCL doesn't have these defined either (although the defining
337      * forms *do* exist in src/code/unix.lisp), so I don't feel nearly
338      * so bad about not hunting them down. Insight into renamed
339      * obscure ioctl(2) flags appreciated. --njf, 2002-08-26
340      *
341      * I note that the first one I grepped for, TIOCSIGSEND, is
342      * referenced in SBCL conditional on #+HPUX. Maybe the porters of
343      * Oxbridge know more about things like that? And even if they
344      * don't, one benefit of the Rhodes crusade to heal the worthy
345      * ports should be that afterwards, if we grep for something like
346      * this in CVS and it's not there, we can lightheartedly nuke it.
347      * -- WHN 2002-08-30 */
348     /*
349       defconstant("tiocsigsend", TIOCSIGSEND);
350       defconstant("tiocflush", TIOCFLUSH);
351       defconstant("tiocgetp", TIOCGETP);
352       defconstant("tiocsetp", TIOCSETP);
353       defconstant("tiocgetc", TIOCGETC);
354       defconstant("tiocsetc", TIOCSETC);
355       defconstant("tiocgltc", TIOCGLTC);
356       defconstant("tiocsltc", TIOCSLTC);
357     */
358     printf("\n");
359
360     printf(";;; signals\n");
361     defconstant("sig-dfl", (unsigned long)SIG_DFL);
362     defconstant("sig-ign", (unsigned long)SIG_IGN);
363
364     defsignal("sigalrm", SIGALRM);
365     defsignal("sigbus", SIGBUS);
366     defsignal("sigchld", SIGCHLD);
367     defsignal("sigcont", SIGCONT);
368 #ifdef SIGEMT
369     defsignal("sigemt", SIGEMT);
370 #endif
371     defsignal("sigfpe", SIGFPE);
372     defsignal("sighup", SIGHUP);
373     defsignal("sigill", SIGILL);
374     defsignal("sigint", SIGINT);
375     defsignal("sigio", SIGIO);
376     defsignal("sigiot", SIGIOT);
377     defsignal("sigkill", SIGKILL);
378     defsignal("sigpipe", SIGPIPE);
379     defsignal("sigprof", SIGPROF);
380     defsignal("sigquit", SIGQUIT);
381     defsignal("sigsegv", SIGSEGV);
382 #ifdef SIGSTKFLT
383     defsignal("sigstkflt", SIGSTKFLT);
384 #endif
385     defsignal("sigstop", SIGSTOP);
386 #ifdef SIGSYS
387     defsignal("sigsys", SIGSYS);
388 #endif
389     defsignal("sigterm", SIGTERM);
390     defsignal("sigtrap", SIGTRAP);
391     defsignal("sigtstp", SIGTSTP);
392     defsignal("sigttin", SIGTTIN);
393     defsignal("sigttou", SIGTTOU);
394     defsignal("sigurg", SIGURG);
395     defsignal("sigusr1", SIGUSR1);
396     defsignal("sigusr2", SIGUSR2);
397     defsignal("sigvtalrm", SIGVTALRM);
398 #ifdef SIGWAITING
399     defsignal("sigwaiting", SIGWAITING);
400 #endif
401     defsignal("sigwinch", SIGWINCH);
402 #ifndef SIGXCPU
403     defsignal("sigxcpu", SIGXCPU);
404 #endif
405 #ifdef SIGXFSZ
406     defsignal("sigxfsz", SIGXFSZ);
407 #endif
408
409    /* Floating point exception codes. Some of these
410     * are missing on Darwin. */
411 #ifdef FPE_INTOVF
412     defconstant("fpe-intovf", FPE_INTOVF);
413 #else
414     defconstant("fpe-intovf", -1);
415 #endif
416 #ifdef FPE_INTDIV
417     defconstant("fpe-intdiv", FPE_INTDIV);
418 #else
419     defconstant("fpe-intdiv", -1);
420 #endif
421     defconstant("fpe-fltdiv", FPE_FLTDIV);
422     defconstant("fpe-fltovf", FPE_FLTOVF);
423     defconstant("fpe-fltund", FPE_FLTUND);
424     defconstant("fpe-fltres", FPE_FLTRES);
425     defconstant("fpe-fltinv", FPE_FLTINV);
426 #ifdef FPE_FLTSUB
427     defconstant("fpe-fltsub", FPE_FLTSUB);
428 #else
429     defconstant("fpe-fltsub", -1);
430 #endif
431
432 #endif // !WIN32
433     return 0;
434 }