0.9.16.35:
[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 <sys/types.h>
23 #ifdef _WIN32
24   #define WIN32_LEAN_AND_MEAN
25   #include <windows.h>
26   #include <stdlib.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 #define DEFTYPE(lispname,cname) { cname foo; \
51     printf("(define-alien-type " lispname " (%s %d))\n", (((foo=-1)<0) ? "sb!alien:signed" : "unsigned"), (8 * (sizeof foo))); }
52
53 void
54 defconstant(char* lisp_name, long unix_number)
55 {
56     printf("(defconstant %s %ld) ; #x%lx\n",
57            lisp_name, unix_number, unix_number);
58 }
59
60 void deferrno(char* lisp_name, long unix_number)
61 {
62     defconstant(lisp_name, unix_number);
63 }
64
65 void defsignal(char* lisp_name, long unix_number)
66 {
67     defconstant(lisp_name, unix_number);
68 }
69
70 int
71 main(int argc, char *argv[])
72 {
73     /* don't need no steenking command line arguments */
74     if (1 != argc) {
75         fprintf(stderr, "argh! command line argument(s)\n");
76         exit(1);
77     }
78
79     /* don't need no steenking hand-editing */
80     printf(
81 ";;;; This is an automatically generated file, please do not hand-edit it.\n\
82 ;;;; See the program \"grovel-headers.c\".\n\
83 \n\
84 ");
85 #ifdef _WIN32
86     printf("(in-package \"SB!WIN32\")\n\n");
87
88     defconstant ("input-record-size", sizeof (INPUT_RECORD));
89
90     defconstant ("MAX_PATH", MAX_PATH);
91
92     printf(";;; CSIDL\n");
93
94     defconstant ("CSIDL_DESKTOP", CSIDL_DESKTOP);
95     defconstant ("CSIDL_INTERNET", CSIDL_INTERNET);
96     defconstant ("CSIDL_PROGRAMS", CSIDL_PROGRAMS);
97     defconstant ("CSIDL_CONTROLS", CSIDL_CONTROLS);
98     defconstant ("CSIDL_PRINTERS", CSIDL_PRINTERS);
99     defconstant ("CSIDL_PERSONAL", CSIDL_PERSONAL);
100     defconstant ("CSIDL_FAVORITES", CSIDL_FAVORITES);
101     defconstant ("CSIDL_STARTUP", CSIDL_STARTUP);
102     defconstant ("CSIDL_RECENT", CSIDL_RECENT);
103     defconstant ("CSIDL_SENDTO", CSIDL_SENDTO);
104     defconstant ("CSIDL_BITBUCKET", CSIDL_BITBUCKET);
105     defconstant ("CSIDL_STARTMENU", CSIDL_STARTMENU);
106     defconstant ("CSIDL_DESKTOPDIRECTORY", CSIDL_DESKTOPDIRECTORY);
107     defconstant ("CSIDL_DRIVES", CSIDL_DRIVES);
108     defconstant ("CSIDL_NETWORK", CSIDL_NETWORK);
109     defconstant ("CSIDL_NETHOOD", CSIDL_NETHOOD);
110     defconstant ("CSIDL_FONTS", CSIDL_FONTS);
111     defconstant ("CSIDL_TEMPLATES", CSIDL_TEMPLATES);
112     defconstant ("CSIDL_COMMON_STARTMENU", CSIDL_COMMON_STARTMENU);
113     defconstant ("CSIDL_COMMON_PROGRAMS", CSIDL_COMMON_PROGRAMS);
114     defconstant ("CSIDL_COMMON_STARTUP", CSIDL_COMMON_STARTUP);
115     defconstant ("CSIDL_COMMON_DESKTOPDIRECTORY", CSIDL_COMMON_DESKTOPDIRECTORY);
116     defconstant ("CSIDL_APPDATA", CSIDL_APPDATA);
117     defconstant ("CSIDL_PRINTHOOD", CSIDL_PRINTHOOD);
118     defconstant ("CSIDL_LOCAL_APPDATA", CSIDL_LOCAL_APPDATA);
119     defconstant ("CSIDL_ALTSTARTUP", CSIDL_ALTSTARTUP);
120     defconstant ("CSIDL_COMMON_ALTSTARTUP", CSIDL_COMMON_ALTSTARTUP);
121     defconstant ("CSIDL_COMMON_FAVORITES", CSIDL_COMMON_FAVORITES);
122     defconstant ("CSIDL_INTERNET_CACHE", CSIDL_INTERNET_CACHE);
123     defconstant ("CSIDL_COOKIES", CSIDL_COOKIES);
124     defconstant ("CSIDL_HISTORY", CSIDL_HISTORY);
125     defconstant ("CSIDL_COMMON_APPDATA", CSIDL_COMMON_APPDATA);
126     defconstant ("CSIDL_WINDOWS", CSIDL_WINDOWS);
127     defconstant ("CSIDL_SYSTEM", CSIDL_SYSTEM);
128     defconstant ("CSIDL_PROGRAM_FILES", CSIDL_PROGRAM_FILES);
129     defconstant ("CSIDL_MYPICTURES", CSIDL_MYPICTURES);
130     defconstant ("CSIDL_PROFILE", CSIDL_PROFILE);
131     defconstant ("CSIDL_SYSTEMX86", CSIDL_SYSTEMX86);
132     defconstant ("CSIDL_PROGRAM_FILESX86", CSIDL_PROGRAM_FILESX86);
133     defconstant ("CSIDL_PROGRAM_FILES_COMMON", CSIDL_PROGRAM_FILES_COMMON);
134     defconstant ("CSIDL_PROGRAM_FILES_COMMONX86", CSIDL_PROGRAM_FILES_COMMONX86);
135     defconstant ("CSIDL_COMMON_TEMPLATES", CSIDL_COMMON_TEMPLATES);
136     defconstant ("CSIDL_COMMON_DOCUMENTS", CSIDL_COMMON_DOCUMENTS);
137     defconstant ("CSIDL_COMMON_ADMINTOOLS", CSIDL_COMMON_ADMINTOOLS);
138     defconstant ("CSIDL_ADMINTOOLS", CSIDL_ADMINTOOLS);
139     defconstant ("CSIDL_CONNECTIONS", CSIDL_CONNECTIONS);
140     defconstant ("CSIDL_COMMON_MUSIC", CSIDL_COMMON_MUSIC);
141     defconstant ("CSIDL_COMMON_PICTURES", CSIDL_COMMON_PICTURES);
142     defconstant ("CSIDL_COMMON_VIDEO", CSIDL_COMMON_VIDEO);
143     defconstant ("CSIDL_RESOURCES", CSIDL_RESOURCES);
144     defconstant ("CSIDL_RESOURCES_LOCALIZED", CSIDL_RESOURCES_LOCALIZED);
145     defconstant ("CSIDL_COMMON_OEM_LINKS", CSIDL_COMMON_OEM_LINKS);
146     defconstant ("CSIDL_CDBURN_AREA", CSIDL_CDBURN_AREA);
147     defconstant ("CSIDL_COMPUTERSNEARME", CSIDL_COMPUTERSNEARME);
148     defconstant ("CSIDL_FLAG_DONT_VERIFY", CSIDL_FLAG_DONT_VERIFY);
149     defconstant ("CSIDL_FLAG_CREATE", CSIDL_FLAG_CREATE);
150     defconstant ("CSIDL_FLAG_MASK", CSIDL_FLAG_MASK);
151
152     printf(";;; FormatMessage\n");
153
154     defconstant ("FORMAT_MESSAGE_ALLOCATE_BUFFER", FORMAT_MESSAGE_ALLOCATE_BUFFER);
155     defconstant ("FORMAT_MESSAGE_FROM_SYSTEM", FORMAT_MESSAGE_FROM_SYSTEM);
156
157     printf(";;; Errors\n");
158
159     defconstant ("ERROR_ENVVAR_NOT_FOUND", ERROR_ENVVAR_NOT_FOUND);
160
161     printf(";;; GetComputerName\n");
162
163     defconstant ("MAX_COMPUTERNAME_LENGTH", MAX_COMPUTERNAME_LENGTH);
164     defconstant ("ERROR_BUFFER_OVERFLOW", ERROR_BUFFER_OVERFLOW);
165
166     printf(";;; Windows Types\n");
167     DEFTYPE("int-ptr", INT_PTR);
168     DEFTYPE("dword",   DWORD);
169     DEFTYPE("bool",    BOOL);
170     DEFTYPE("uint",    UINT);
171
172     /* FIXME: SB-UNIX and SB-WIN32 really need to be untangled. */
173     printf("(in-package \"SB!UNIX\")\n\n");
174     printf(";;; Unix-like constants and types on Windows\n");
175     defconstant("o_rdonly", _O_RDONLY);
176     defconstant("o_wronly", _O_WRONLY);
177     defconstant("o_rdwr",   _O_RDWR);
178     defconstant("o_creat",  _O_CREAT);
179     defconstant("o_trunc",  _O_TRUNC);
180     defconstant("o_append", _O_APPEND);
181     defconstant("o_excl",   _O_EXCL);
182     defconstant("o_binary", _O_BINARY);
183
184     defconstant("enoent", ENOENT);
185     defconstant("eexist", EEXIST);
186
187     defconstant("s-ifmt",  S_IFMT);
188     defconstant("s-ifdir", S_IFDIR);
189     defconstant("s-ifreg", S_IFREG);
190
191     DEFTYPE("ino-t",  ino_t);
192     DEFTYPE("time-t", time_t);
193     DEFTYPE("off-t",  off_t);
194     DEFTYPE("size-t", size_t);
195     DEFTYPE("mode-t", mode_t);
196
197 #else
198     printf("(in-package \"SB!ALIEN\")\n\n");
199
200     printf (";;;flags for dlopen()\n");
201
202     defconstant ("rtld-lazy", RTLD_LAZY);
203     defconstant ("rtld-now", RTLD_NOW);
204     defconstant ("rtld-global", RTLD_GLOBAL);
205
206     printf("(in-package \"SB!UNIX\")\n\n");
207
208     printf(";;; langinfo\n");
209     defconstant("codeset", CODESET);
210
211     printf(";;; types, types, types\n");
212     DEFTYPE("clock-t", clock_t);
213     DEFTYPE("dev-t",   dev_t);
214     DEFTYPE("gid-t",   gid_t);
215     DEFTYPE("ino-t",   ino_t);
216     DEFTYPE("mode-t",  mode_t);
217     DEFTYPE("nlink-t", nlink_t);
218     DEFTYPE("off-t",   off_t);
219     DEFTYPE("size-t",  size_t);
220     DEFTYPE("time-t",  time_t);
221     DEFTYPE("uid-t",   uid_t);
222     printf("\n");
223
224     printf(";;; fcntl.h (or unistd.h on OpenBSD and NetBSD)\n");
225     defconstant("r_ok", R_OK);
226     defconstant("w_ok", W_OK);
227     defconstant("x_ok", X_OK);
228     defconstant("f_ok", F_OK);
229     printf("\n");
230
231     printf(";;; fcntlbits.h\n");
232     defconstant("o_rdonly",  O_RDONLY);
233     defconstant("o_wronly",  O_WRONLY);
234     defconstant("o_rdwr",    O_RDWR);
235     defconstant("o_accmode", O_ACCMODE);
236     defconstant("o_creat",   O_CREAT);
237     defconstant("o_excl",    O_EXCL);
238     defconstant("o_noctty",  O_NOCTTY);
239     defconstant("o_trunc",   O_TRUNC);
240     defconstant("o_append",  O_APPEND);
241     printf(";;;\n");
242     defconstant("s-ifmt",  S_IFMT);
243     defconstant("s-ififo", S_IFIFO);
244     defconstant("s-ifchr", S_IFCHR);
245     defconstant("s-ifdir", S_IFDIR);
246     defconstant("s-ifblk", S_IFBLK);
247     defconstant("s-ifreg", S_IFREG);
248     printf("\n");
249
250     defconstant("s-iflnk",  S_IFLNK);
251     defconstant("s-ifsock", S_IFSOCK);
252     printf("\n");
253
254     printf(";;; error numbers\n");
255     deferrno("enoent", ENOENT);
256     deferrno("eintr", EINTR);
257     deferrno("eio", EIO);
258     deferrno("eexist", EEXIST);
259     deferrno("espipe", ESPIPE);
260     deferrno("ewouldblock", EWOULDBLOCK);
261     printf("\n");
262
263     printf(";;; for wait3(2) in run-program.lisp\n");
264     defconstant("wnohang", WNOHANG);
265     defconstant("wuntraced", WUNTRACED);
266     printf("\n");
267
268     printf(";;; various ioctl(2) flags\n");
269     defconstant("tiocnotty",  TIOCNOTTY);
270     defconstant("tiocgwinsz", TIOCGWINSZ);
271     defconstant("tiocswinsz", TIOCSWINSZ);
272     defconstant("tiocgpgrp",  TIOCGPGRP);
273     defconstant("tiocspgrp",  TIOCSPGRP);
274     /* KLUDGE: These are referenced by old CMUCL-derived code, but
275      * Linux doesn't define them.
276      *
277      * I think these are the BSD names, but I don't know what the
278      * corresponding SysV/Linux names are. As a point of reference,
279      * CMUCL doesn't have these defined either (although the defining
280      * forms *do* exist in src/code/unix.lisp), so I don't feel nearly
281      * so bad about not hunting them down. Insight into renamed
282      * obscure ioctl(2) flags appreciated. --njf, 2002-08-26
283      *
284      * I note that the first one I grepped for, TIOCSIGSEND, is
285      * referenced in SBCL conditional on #+HPUX. Maybe the porters of
286      * Oxbridge know more about things like that? And even if they
287      * don't, one benefit of the Rhodes crusade to heal the worthy
288      * ports should be that afterwards, if we grep for something like
289      * this in CVS and it's not there, we can lightheartedly nuke it.
290      * -- WHN 2002-08-30 */
291     /*
292       defconstant("tiocsigsend", TIOCSIGSEND);
293       defconstant("tiocflush", TIOCFLUSH);
294       defconstant("tiocgetp", TIOCGETP);
295       defconstant("tiocsetp", TIOCSETP);
296       defconstant("tiocgetc", TIOCGETC);
297       defconstant("tiocsetc", TIOCSETC);
298       defconstant("tiocgltc", TIOCGLTC);
299       defconstant("tiocsltc", TIOCSLTC);
300     */
301     printf("\n");
302
303     printf(";;; signals\n");
304     defsignal("sigalrm", SIGALRM);
305     defsignal("sigbus", SIGBUS);
306     defsignal("sigchld", SIGCHLD);
307     defsignal("sigcont", SIGCONT);
308 #ifdef SIGEMT
309     defsignal("sigemt", SIGEMT);
310 #endif
311     defsignal("sigfpe", SIGFPE);
312     defsignal("sighup", SIGHUP);
313     defsignal("sigill", SIGILL);
314     defsignal("sigint", SIGINT);
315     defsignal("sigio", SIGIO);
316     defsignal("sigiot", SIGIOT);
317     defsignal("sigkill", SIGKILL);
318     defsignal("sigpipe", SIGPIPE);
319     defsignal("sigprof", SIGPROF);
320     defsignal("sigquit", SIGQUIT);
321     defsignal("sigsegv", SIGSEGV);
322 #if ((defined LISP_FEATURE_LINUX) && (defined LISP_FEATURE_X86))
323     defsignal("sigstkflt", SIGSTKFLT);
324 #endif
325     defsignal("sigstop", SIGSTOP);
326 #if (!((defined LISP_FEATURE_LINUX) && (defined LISP_FEATURE_X86)))
327     defsignal("sigsys", SIGSYS);
328 #endif
329     defsignal("sigterm", SIGTERM);
330     defsignal("sigtrap", SIGTRAP);
331     defsignal("sigtstp", SIGTSTP);
332     defsignal("sigttin", SIGTTIN);
333     defsignal("sigttou", SIGTTOU);
334     defsignal("sigurg", SIGURG);
335     defsignal("sigusr1", SIGUSR1);
336     defsignal("sigusr2", SIGUSR2);
337     defsignal("sigvtalrm", SIGVTALRM);
338 #ifdef LISP_FEATURE_SUNOS
339     defsignal("sigwaiting", SIGWAITING);
340 #endif
341     defsignal("sigwinch", SIGWINCH);
342 #ifndef LISP_FEATURE_HPUX
343     defsignal("sigxcpu", SIGXCPU);
344     defsignal("sigxfsz", SIGXFSZ);
345 #endif
346 #endif // _WIN32
347     return 0;
348 }