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