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