0.7.2.6:
[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 <fcntl.h>
27 #include <unistd.h>
28
29 #define DEFTYPE(lispname,cname) { cname foo; \
30     printf("(define-alien-type " lispname " (%s %d))\n", (((foo=-1)<0) ? "sb!alien:signed" : "unsigned"), (8 * (sizeof foo))); }
31
32 void
33 defconstant(char* lisp_name, long unix_number)
34 {
35     printf("(defconstant %s %ld) ; #x%lx\n",
36            lisp_name, unix_number, unix_number);
37 }
38
39 int
40 main(int argc, char *argv[])
41 {
42     /* don't need no steenking command line arguments */
43     if (1 != argc) {
44         fprintf(stderr, "argh! command line argument(s)\n");
45         exit(1);
46     }
47
48     /* don't need no steenking hand-editing */
49     printf(
50 ";;;; This is an automatically generated file, please do not hand-edit it.\n\
51 ;;;; See the program \"grovel_headers.c\".\n\
52 \n\
53 ");
54
55     printf("(in-package \"SB!UNIX\")\n\n");
56
57     printf(";;; types, types, types\n");
58     DEFTYPE("clock-t", clock_t);
59     DEFTYPE("dev-t",   dev_t);
60     DEFTYPE("gid-t",   gid_t);
61     DEFTYPE("ino-t",   ino_t);
62     DEFTYPE("mode-t",  mode_t);
63     DEFTYPE("nlink-t", nlink_t);
64     DEFTYPE("off-t",   off_t);
65     DEFTYPE("size-t",  size_t);
66     DEFTYPE("time-t",  time_t);
67     DEFTYPE("uid-t",   uid_t);
68     printf("\n");
69
70     printf(";;; fcntl.h (or unistd.h on OpenBSD)\n");
71     defconstant("r_ok", R_OK);
72     defconstant("w_ok", W_OK);
73     defconstant("x_ok", X_OK);
74     defconstant("f_ok", F_OK);
75     printf("\n");
76
77     printf(";;; fcntlbits.h\n");
78     defconstant("o_rdonly",  O_RDONLY);
79     defconstant("o_wronly",  O_WRONLY);
80     defconstant("o_rdwr",    O_RDWR);
81     defconstant("o_accmode", O_ACCMODE);
82     defconstant("o_creat",   O_CREAT);
83     defconstant("o_excl",    O_EXCL);
84     defconstant("o_noctty",  O_NOCTTY);
85     defconstant("o_trunc",   O_TRUNC);
86     defconstant("o_append",  O_APPEND);
87     printf(";;;\n");
88     defconstant("s-ifmt",  S_IFMT);
89     defconstant("s-ififo", S_IFIFO);
90     defconstant("s-ifchr", S_IFCHR);
91     defconstant("s-ifdir", S_IFDIR);
92     defconstant("s-ifblk", S_IFBLK);
93     defconstant("s-ifreg", S_IFREG);
94     printf("\n");
95   
96     defconstant("s-iflnk",  S_IFLNK);
97     defconstant("s-ifsock", S_IFSOCK);
98     printf("\n");
99
100     printf(";;; for wait3(2) in run-program.lisp\n");
101     defconstant("wnohang", WNOHANG);
102     defconstant("wuntraced", WUNTRACED);
103     printf("\n");
104
105     return 0;
106 }