0.9.8.27:
[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   #include <stdlib.h>
25 #else
26   #include <sys/times.h>
27   #include <sys/wait.h>
28   #include <sys/ioctl.h>
29   #include <sys/termios.h>
30   #ifdef __APPLE_CC__
31     #include "../src/runtime/ppc-darwin-dlshim.h"
32     #include "../src/runtime/ppc-darwin-langinfo.h"
33   #else
34     #include <dlfcn.h>
35     #include <langinfo.h>
36   #endif
37 #endif
38
39 #include <sys/stat.h>
40 #include <fcntl.h>
41 #include <unistd.h>
42 #include <signal.h>
43 #include <errno.h>
44
45 #include "genesis/config.h"
46
47 #define DEFTYPE(lispname,cname) { cname foo; \
48     printf("(define-alien-type " lispname " (%s %d))\n", (((foo=-1)<0) ? "sb!alien:signed" : "unsigned"), (8 * (sizeof foo))); }
49
50 void
51 defconstant(char* lisp_name, long unix_number)
52 {
53     printf("(defconstant %s %ld) ; #x%lx\n",
54            lisp_name, unix_number, unix_number);
55 }
56
57 void deferrno(char* lisp_name, long unix_number)
58 {
59     defconstant(lisp_name, unix_number);
60 }
61
62 void defsignal(char* lisp_name, long unix_number)
63 {
64     defconstant(lisp_name, unix_number);
65 }
66
67 int
68 main(int argc, char *argv[])
69 {
70     /* don't need no steenking command line arguments */
71     if (1 != argc) {
72         fprintf(stderr, "argh! command line argument(s)\n");
73         exit(1);
74     }
75
76     /* don't need no steenking hand-editing */
77     printf(
78 ";;;; This is an automatically generated file, please do not hand-edit it.\n\
79 ;;;; See the program \"grovel-headers.c\".\n\
80 \n\
81 ");
82 #ifdef _WIN32
83     printf (";;; This file is presently unused for the Windows version of sbcl.\n");
84 #else
85     printf("(in-package \"SB!ALIEN\")\n\n");
86
87     printf (";;;flags for dlopen()\n");
88
89     defconstant ("rtld-lazy", RTLD_LAZY);
90     defconstant ("rtld-now", RTLD_NOW);
91     defconstant ("rtld-global", RTLD_GLOBAL);
92
93     printf("(in-package \"SB!UNIX\")\n\n");
94
95     printf(";;; langinfo\n");
96     defconstant("codeset", CODESET);
97
98     printf(";;; types, types, types\n");
99     DEFTYPE("clock-t", clock_t);
100     DEFTYPE("dev-t",   dev_t);
101     DEFTYPE("gid-t",   gid_t);
102     DEFTYPE("ino-t",   ino_t);
103     DEFTYPE("mode-t",  mode_t);
104     DEFTYPE("nlink-t", nlink_t);
105     DEFTYPE("off-t",   off_t);
106     DEFTYPE("size-t",  size_t);
107     DEFTYPE("time-t",  time_t);
108     DEFTYPE("uid-t",   uid_t);
109     printf("\n");
110
111     printf(";;; fcntl.h (or unistd.h on OpenBSD and NetBSD)\n");
112     defconstant("r_ok", R_OK);
113     defconstant("w_ok", W_OK);
114     defconstant("x_ok", X_OK);
115     defconstant("f_ok", F_OK);
116     printf("\n");
117
118     printf(";;; fcntlbits.h\n");
119     defconstant("o_rdonly",  O_RDONLY);
120     defconstant("o_wronly",  O_WRONLY);
121     defconstant("o_rdwr",    O_RDWR);
122     defconstant("o_accmode", O_ACCMODE);
123     defconstant("o_creat",   O_CREAT);
124     defconstant("o_excl",    O_EXCL);
125     defconstant("o_noctty",  O_NOCTTY);
126     defconstant("o_trunc",   O_TRUNC);
127     defconstant("o_append",  O_APPEND);
128     printf(";;;\n");
129     defconstant("s-ifmt",  S_IFMT);
130     defconstant("s-ififo", S_IFIFO);
131     defconstant("s-ifchr", S_IFCHR);
132     defconstant("s-ifdir", S_IFDIR);
133     defconstant("s-ifblk", S_IFBLK);
134     defconstant("s-ifreg", S_IFREG);
135     printf("\n");
136
137     defconstant("s-iflnk",  S_IFLNK);
138     defconstant("s-ifsock", S_IFSOCK);
139     printf("\n");
140
141     printf(";;; error numbers\n");
142     deferrno("enoent", ENOENT);
143     deferrno("eintr", EINTR);
144     deferrno("eio", EIO);
145     deferrno("eexist", EEXIST);
146     deferrno("espipe", ESPIPE);
147     deferrno("ewouldblock", EWOULDBLOCK);
148     printf("\n");
149
150     printf(";;; for wait3(2) in run-program.lisp\n");
151     defconstant("wnohang", WNOHANG);
152     defconstant("wuntraced", WUNTRACED);
153     printf("\n");
154
155     printf(";;; various ioctl(2) flags\n");
156     defconstant("tiocnotty",  TIOCNOTTY);
157     defconstant("tiocgwinsz", TIOCGWINSZ);
158     defconstant("tiocswinsz", TIOCSWINSZ);
159     defconstant("tiocgpgrp",  TIOCGPGRP);
160     defconstant("tiocspgrp",  TIOCSPGRP);
161     /* KLUDGE: These are referenced by old CMUCL-derived code, but
162      * Linux doesn't define them.
163      *
164      * I think these are the BSD names, but I don't know what the
165      * corresponding SysV/Linux names are. As a point of reference,
166      * CMUCL doesn't have these defined either (although the defining
167      * forms *do* exist in src/code/unix.lisp), so I don't feel nearly
168      * so bad about not hunting them down. Insight into renamed
169      * obscure ioctl(2) flags appreciated. --njf, 2002-08-26
170      *
171      * I note that the first one I grepped for, TIOCSIGSEND, is
172      * referenced in SBCL conditional on #+HPUX. Maybe the porters of
173      * Oxbridge know more about things like that? And even if they
174      * don't, one benefit of the Rhodes crusade to heal the worthy
175      * ports should be that afterwards, if we grep for something like
176      * this in CVS and it's not there, we can lightheartedly nuke it.
177      * -- WHN 2002-08-30 */
178     /*
179       defconstant("tiocsigsend", TIOCSIGSEND);
180       defconstant("tiocflush", TIOCFLUSH);
181       defconstant("tiocgetp", TIOCGETP);
182       defconstant("tiocsetp", TIOCSETP);
183       defconstant("tiocgetc", TIOCGETC);
184       defconstant("tiocsetc", TIOCSETC);
185       defconstant("tiocgltc", TIOCGLTC);
186       defconstant("tiocsltc", TIOCSLTC);
187     */
188     printf("\n");
189
190     printf(";;; signals\n");
191     defsignal("sigalrm", SIGALRM);
192     defsignal("sigbus", SIGBUS);
193     defsignal("sigchld", SIGCHLD);
194     defsignal("sigcont", SIGCONT);
195 #ifdef SIGEMT
196     defsignal("sigemt", SIGEMT);
197 #endif
198     defsignal("sigfpe", SIGFPE);
199     defsignal("sighup", SIGHUP);
200     defsignal("sigill", SIGILL);
201     defsignal("sigint", SIGINT);
202     defsignal("sigio", SIGIO);
203     defsignal("sigiot", SIGIOT);
204     defsignal("sigkill", SIGKILL);
205     defsignal("sigpipe", SIGPIPE);
206     defsignal("sigprof", SIGPROF);
207     defsignal("sigquit", SIGQUIT);
208     defsignal("sigsegv", SIGSEGV);
209 #if ((defined LISP_FEATURE_LINUX) && (defined LISP_FEATURE_X86))
210     defsignal("sigstkflt", SIGSTKFLT);
211 #endif
212     defsignal("sigstop", SIGSTOP);
213 #if (!((defined LISP_FEATURE_LINUX) && (defined LISP_FEATURE_X86)))
214     defsignal("sigsys", SIGSYS);
215 #endif
216     defsignal("sigterm", SIGTERM);
217     defsignal("sigtrap", SIGTRAP);
218     defsignal("sigtstp", SIGTSTP);
219     defsignal("sigttin", SIGTTIN);
220     defsignal("sigttou", SIGTTOU);
221     defsignal("sigurg", SIGURG);
222     defsignal("sigusr1", SIGUSR1);
223     defsignal("sigusr2", SIGUSR2);
224     defsignal("sigvtalrm", SIGVTALRM);
225 #ifdef LISP_FEATURE_SUNOS
226     defsignal("sigwaiting", SIGWAITING);
227 #endif
228     defsignal("sigwinch", SIGWINCH);
229 #ifndef LISP_FEATURE_HPUX
230     defsignal("sigxcpu", SIGXCPU);
231     defsignal("sigxfsz", SIGXFSZ);
232 #endif
233 #endif // _WIN32
234     return 0;
235 }