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