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