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 "genesis/config.h"
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <sys/types.h>
26 #ifdef _WIN32
27   /* KLUDGE: From src/runtime/runtime.h, avoid double definition of
28      boolean.  We really should clean up our act on this one. */
29   #define boolean rpcndr_boolean
30   #define WIN32_LEAN_AND_MEAN
31   #include <windows.h>
32   #include <shlobj.h>
33   #include <wincrypt.h>
34   #include <winsock2.h>
35   #undef boolean
36 #else
37   #include <poll.h>
38   #include <sys/select.h>
39   #include <sys/times.h>
40   #include <sys/wait.h>
41   #include <sys/ioctl.h>
42 #ifdef LISP_FEATURE_ANDROID
43   #include <termios.h>
44 #else
45   #include <termios.h>
46   #include <langinfo.h>
47 #endif
48   #include <sys/time.h>
49   #include <dlfcn.h>
50 #endif
51 
52 #include <sys/stat.h>
53 #include <fcntl.h>
54 #include <unistd.h>
55 #include <signal.h>
56 #include <errno.h>
57 #include <time.h>
58 
59 #ifdef LISP_FEATURE_HPUX
60 #include <sys/bsdtty.h> /* for TIOCGPGRP */
61 #endif
62 
63 #ifdef LISP_FEATURE_BSD
64   #include <sys/param.h>
65   #include <sys/sysctl.h>
66 #endif
67 
68 #ifdef _WIN32
69   #include "pthreads_win32.h"
70 #endif
71 
72 #include "wrap.h"
73 #include "gc.h"
74 
75 #define DEFTYPE(lispname,cname) { cname foo; \
76     printf("(define-alien-type " lispname " (%s %d))\n", (((foo=-1)<0) ? "signed" : "unsigned"), (8 * (sizeof foo))); }
77 
78 #define DEFSTRUCT(lispname,cname,body) { cname bar; \
79     printf("(define-alien-type nil\n  (struct %s", #lispname); \
80     body; \
81     printf("))\n"); }
82 #define DEFSLOT(lispname,cname) \
83     printf("\n          (%s (%s %d))", \
84            #lispname, \
85            (((bar.cname=-1)<0) ? "signed" : "unsigned"), \
86            (8 * (sizeof bar.cname)))
87 
88 void
defconstant(char * lisp_name,unsigned long unix_number)89 defconstant(char* lisp_name, unsigned long unix_number)
90 {
91     printf("(defconstant %s %lu) ; #x%lx\n",
92            lisp_name, unix_number, unix_number);
93 }
94 
deferrno(char * lisp_name,unsigned long unix_number)95 void deferrno(char* lisp_name, unsigned long unix_number)
96 {
97     defconstant(lisp_name, unix_number);
98 }
99 
defsignal(char * lisp_name,unsigned long unix_number)100 void defsignal(char* lisp_name, unsigned long unix_number)
101 {
102     defconstant(lisp_name, unix_number);
103 }
104 
105 int
main(int argc,char * argv[])106 main(int argc, char *argv[])
107 {
108     /* don't need no steenking command line arguments */
109     if (1 != argc) {
110         fprintf(stderr, "argh! command line argument(s)\n");
111         exit(1);
112     }
113 
114     /* don't need no steenking hand-editing */
115     printf(
116 ";;;; This is an automatically generated file, please do not hand-edit it.\n\
117 ;;;; See the program \"grovel-headers.c\".\n\
118 \n\
119 ");
120 #ifdef _WIN32
121     #include "grovel-headers-win32.h"
122 #else
123     printf("(in-package \"SB!ALIEN\")\n\n");
124 
125     printf (";;;flags for dlopen()\n");
126 
127     defconstant ("rtld-lazy", RTLD_LAZY);
128     defconstant ("rtld-now", RTLD_NOW);
129     defconstant ("rtld-global", RTLD_GLOBAL);
130 
131     printf("(in-package \"SB!UNIX\")\n\n");
132 
133     printf(";;; select()\n");
134     defconstant("fd-setsize", FD_SETSIZE);
135 
136     printf(";;; poll()\n");
137     defconstant("pollin", POLLIN);
138     defconstant("pollout", POLLOUT);
139     defconstant("pollpri", POLLPRI);
140     defconstant("pollhup", POLLHUP);
141     defconstant("pollnval", POLLNVAL);
142     defconstant("pollerr", POLLERR);
143     DEFTYPE("nfds-t", nfds_t);
144 #ifndef LISP_FEATURE_ANDROID
145     printf(";;; langinfo\n");
146     defconstant("codeset", CODESET);
147 #endif
148     printf(";;; types, types, types\n");
149     DEFTYPE("clock-t", clock_t);
150     DEFTYPE("dev-t",   dev_t);
151     DEFTYPE("gid-t",   gid_t);
152     DEFTYPE("ino-t",   ino_t);
153     DEFTYPE("mode-t",  mode_t);
154     DEFTYPE("nlink-t", nlink_t);
155     DEFTYPE("off-t",   off_t);
156     DEFTYPE("size-t",  size_t);
157     DEFTYPE("time-t",  time_t);
158 #if !defined(LISP_FEATURE_OS_PROVIDES_SUSECONDS_T)
159     /* Similar kludge in sb-posix. */
160     DEFTYPE("suseconds-t", long);
161 #else
162     DEFTYPE("suseconds-t", suseconds_t);
163 #endif
164     DEFTYPE("uid-t",   uid_t);
165     printf(";; Types in src/runtime/wrap.h. See that file for explantion.\n");
166     printf(";; Don't use these types for anything other than the stat wrapper.\n");
167     DEFTYPE("wst-ino-t", wst_ino_t);
168     DEFTYPE("wst-dev-t", wst_dev_t);
169     DEFTYPE("wst-off-t", wst_off_t);
170     DEFTYPE("wst-blksize-t", wst_blksize_t);
171     DEFTYPE("wst-blkcnt-t", wst_blkcnt_t);
172     DEFTYPE("wst-nlink-t", wst_nlink_t);
173     DEFTYPE("wst-uid-t", wst_uid_t);
174     DEFTYPE("wst-gid-t", wst_gid_t);
175     printf("\n");
176 
177     printf(";;; fcntl.h (or unistd.h on OpenBSD and NetBSD)\n");
178     defconstant("r_ok", R_OK);
179     defconstant("w_ok", W_OK);
180     defconstant("x_ok", X_OK);
181     defconstant("f_ok", F_OK);
182     printf("\n");
183 
184     printf(";;; fcntlbits.h\n");
185     defconstant("o_rdonly",  O_RDONLY);
186     defconstant("o_wronly",  O_WRONLY);
187     defconstant("o_rdwr",    O_RDWR);
188     defconstant("o_accmode", O_ACCMODE);
189     defconstant("o_creat",   O_CREAT);
190     defconstant("o_excl",    O_EXCL);
191     defconstant("o_noctty",  O_NOCTTY);
192     defconstant("o_trunc",   O_TRUNC);
193     defconstant("o_append",  O_APPEND);
194 #ifdef LISP_FEATURE_LARGEFILE
195     defconstant("o_largefile", O_LARGEFILE);
196 #endif
197 
198     printf(";;;\n");
199     defconstant("s-ifmt",  S_IFMT);
200     defconstant("s-ififo", S_IFIFO);
201     defconstant("s-ifchr", S_IFCHR);
202     defconstant("s-ifdir", S_IFDIR);
203     defconstant("s-ifblk", S_IFBLK);
204     defconstant("s-ifreg", S_IFREG);
205     printf("\n");
206 
207     defconstant("s-iflnk",  S_IFLNK);
208     defconstant("s-ifsock", S_IFSOCK);
209     printf("\n");
210 
211     printf(";;; error numbers\n");
212     deferrno("ebadf", EBADF);
213     deferrno("enoent", ENOENT);
214     deferrno("eintr", EINTR);
215     deferrno("eagain", EAGAIN);
216     deferrno("eio", EIO);
217     deferrno("eexist", EEXIST);
218     deferrno("eloop", ELOOP);
219     deferrno("espipe", ESPIPE);
220     deferrno("ewouldblock", EWOULDBLOCK);
221     printf("\n");
222 
223     printf(";;; for wait3(2) in run-program.lisp\n");
224     defconstant("wnohang", WNOHANG);
225     defconstant("wuntraced", WUNTRACED);
226     printf("\n");
227 
228     printf(";;; various ioctl(2) flags\n");
229     defconstant("tiocgpgrp",  TIOCGPGRP);
230     defconstant("tiocspgrp",  TIOCSPGRP);
231     defconstant("tiocgwinsz", TIOCGWINSZ);
232     defconstant("tiocswinsz", TIOCSWINSZ);
233     /* KLUDGE: These are referenced by old CMUCL-derived code, but
234      * Linux doesn't define them.
235      *
236      * I think these are the BSD names, but I don't know what the
237      * corresponding SysV/Linux names are. As a point of reference,
238      * CMUCL doesn't have these defined either (although the defining
239      * forms *do* exist in src/code/unix.lisp), so I don't feel nearly
240      * so bad about not hunting them down. Insight into renamed
241      * obscure ioctl(2) flags appreciated. --njf, 2002-08-26
242      *
243      * I note that the first one I grepped for, TIOCSIGSEND, is
244      * referenced in SBCL conditional on #+HPUX. Maybe the porters of
245      * Oxbridge know more about things like that? And even if they
246      * don't, one benefit of the Rhodes crusade to heal the worthy
247      * ports should be that afterwards, if we grep for something like
248      * this in CVS and it's not there, we can lightheartedly nuke it.
249      * -- WHN 2002-08-30 */
250     /*
251       defconstant("tiocsigsend", TIOCSIGSEND);
252       defconstant("tiocflush", TIOCFLUSH);
253       defconstant("tiocgetp", TIOCGETP);
254       defconstant("tiocsetp", TIOCSETP);
255       defconstant("tiocgetc", TIOCGETC);
256       defconstant("tiocsetc", TIOCSETC);
257       defconstant("tiocgltc", TIOCGLTC);
258       defconstant("tiocsltc", TIOCSLTC);
259     */
260     printf("\n");
261 
262     printf(";;; signals\n");
263     defconstant("sig-dfl", (unsigned long)SIG_DFL);
264     defconstant("sig-ign", (unsigned long)SIG_IGN);
265 
266     defsignal("sigalrm", SIGALRM);
267     defsignal("sigbus", SIGBUS);
268     defsignal("sigchld", SIGCHLD);
269     defsignal("sigcont", SIGCONT);
270 #ifdef SIGEMT
271     defsignal("sigemt", SIGEMT);
272 #endif
273     defsignal("sigfpe", SIGFPE);
274     defsignal("sighup", SIGHUP);
275     defsignal("sigill", SIGILL);
276     defsignal("sigint", SIGINT);
277     defsignal("sigio", SIGIO);
278     defsignal("sigiot", SIGIOT);
279     defsignal("sigkill", SIGKILL);
280     defsignal("sigpipe", SIGPIPE);
281     defsignal("sigprof", SIGPROF);
282     defsignal("sigquit", SIGQUIT);
283     defsignal("sigsegv", SIGSEGV);
284 #ifdef SIGSTKFLT
285     defsignal("sigstkflt", SIGSTKFLT);
286 #endif
287     defsignal("sigstop", SIGSTOP);
288 #ifdef SIGSYS
289     defsignal("sigsys", SIGSYS);
290 #endif
291     defsignal("sigterm", SIGTERM);
292     defsignal("sigtrap", SIGTRAP);
293     defsignal("sigtstp", SIGTSTP);
294     defsignal("sigttin", SIGTTIN);
295     defsignal("sigttou", SIGTTOU);
296     defsignal("sigurg", SIGURG);
297     defsignal("sigusr1", SIGUSR1);
298     defsignal("sigusr2", SIGUSR2);
299     defsignal("sigvtalrm", SIGVTALRM);
300 #ifdef SIGWAITING
301     defsignal("sigwaiting", SIGWAITING);
302 #endif
303     defsignal("sigwinch", SIGWINCH);
304 #ifdef SIGXCPU
305     defsignal("sigxcpu", SIGXCPU);
306 #endif
307 #ifdef SIGXFSZ
308     defsignal("sigxfsz", SIGXFSZ);
309 #endif
310 
311    /* Floating point exception codes. Some of these
312     * are missing on Darwin. */
313 #ifdef FPE_INTOVF
314     defconstant("fpe-intovf", FPE_INTOVF);
315 #else
316     defconstant("fpe-intovf", -1);
317 #endif
318 #ifdef FPE_INTDIV
319     defconstant("fpe-intdiv", FPE_INTDIV);
320 #else
321     defconstant("fpe-intdiv", -1);
322 #endif
323     defconstant("fpe-fltdiv", FPE_FLTDIV);
324     defconstant("fpe-fltovf", FPE_FLTOVF);
325     defconstant("fpe-fltund", FPE_FLTUND);
326     defconstant("fpe-fltres", FPE_FLTRES);
327     defconstant("fpe-fltinv", FPE_FLTINV);
328 #ifdef FPE_FLTSUB
329     defconstant("fpe-fltsub", FPE_FLTSUB);
330 #else
331     defconstant("fpe-fltsub", -1);
332 #endif
333 #endif // !WIN32
334     printf("\n");
335 
336     printf(";;; structures\n");
337     DEFSTRUCT(timeval, struct timeval,
338         DEFSLOT(tv-sec, tv_sec);
339         DEFSLOT(tv-usec, tv_usec));
340     DEFSTRUCT(timespec, struct timespec,
341         DEFSLOT(tv-sec, tv_sec);
342         DEFSLOT(tv-nsec, tv_nsec));
343     printf("\n");
344 
345 #ifdef LISP_FEATURE_ANDROID
346     defconstant("path-max", PATH_MAX);
347     printf("\n");
348 #endif
349 
350 #ifdef LISP_FEATURE_BSD
351     printf(";;; sysctl(3) names\n");
352     printf("(in-package \"SB!IMPL\")\n");
353     defconstant("ctl-kern", CTL_KERN);
354     defconstant("ctl-hw", CTL_HW);
355     defconstant("ctl-maxname", CTL_MAXNAME);
356     defconstant("kern-ostype", KERN_OSTYPE);
357     defconstant("kern-osrelease", KERN_OSRELEASE);
358     defconstant("hw-model", HW_MODEL);
359     defconstant("hw-pagesize", HW_PAGESIZE);
360 
361     defconstant("path-max", PATH_MAX);
362     printf("\n");
363 #endif
364 
365     printf("(in-package \"SB!KERNEL\")\n\n");
366 #ifdef LISP_FEATURE_GENCGC
367     printf(";;; GENCGC related\n");
368     DEFTYPE("page-index-t", page_index_t);
369     DEFTYPE("generation-index-t", generation_index_t);
370     printf("\n");
371 #endif
372 
373     printf(";;; Our runtime types\n");
374     DEFTYPE("os-vm-size-t", os_vm_size_t);
375 
376     return 0;
377 }
378