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