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