perl 3.0 patch #13 (combined patch)
[p5sagit/p5-mst-13.2.git] / makelib.SH
1 case $CONFIG in
2 '')
3     if test ! -f config.sh; then
4         ln ../config.sh . || \
5         ln ../../config.sh . || \
6         ln ../../../config.sh . || \
7         (echo "Can't find config.sh."; exit 1)
8     fi
9     . config.sh
10     ;;
11 esac
12 : This forces SH files to create target in same directory as SH file.
13 : This is so that make depend always knows where to find SH derivatives.
14 case "$0" in
15 */*) cd `expr X$0 : 'X\(.*\)/'` ;;
16 esac
17 echo "Extracting makelib (with variable substitutions)"
18 : This section of the file will have variable substitutions done on it.
19 : Move anything that needs config subs from !NO!SUBS! section to !GROK!THIS!.
20 : Protect any dollar signs and backticks that you do not want interpreted
21 : by putting a backslash in front.  You may delete these comments.
22 $spitshell >makelib <<!GROK!THIS!
23 #!/usr/bin/perl
24
25 \$perlincl = '$privlib';
26 !GROK!THIS!
27
28 : In the following dollars and backticks do not need the extra backslash.
29 $spitshell >>makelib <<'!NO!SUBS!'
30
31 chdir '/usr/include' || die "Can't cd /usr/include";
32
33 %isatype = ('char',1,'short',1,'int',1,'long',1);
34
35 foreach $file (@ARGV) {
36     print $file,"\n";
37     if ($file =~ m|^(.*)/|) {
38         $dir = $1;
39         if (!-d "$perlincl/$dir") {
40             mkdir("$perlincl/$dir",0777);
41         }
42     }
43     open(IN,"$file") || ((warn "Can't open $file: $!\n"),next);
44     open(OUT,">$perlincl/$file") || die "Can't create $file: $!\n";
45     while (<IN>) {
46         chop;
47         while (/\\$/) {
48             chop;
49             $_ .= <IN>;
50             chop;
51         }
52         if (s:/\*:\200:g) {
53             s:\*/:\201:g;
54             s/\200[^\201]*\201//g;      # delete single line comments
55             if (s/\200.*//) {           # begin multi-line comment?
56                 $_ .= '/*';
57                 $_ .= <IN>;
58                 redo;
59             }
60         }
61         if (s/^#\s*//) {
62             if (s/^define\s+(\w+)//) {
63                 $name = $1;
64                 $new = '';
65                 s/\s+$//;
66                 if (s/^\(([\w,\s]*)\)//) {
67                     $args = $1;
68                     if ($args ne '') {
69                         foreach $arg (split(/,\s*/,$args)) {
70                             $curargs{$arg} = 1;
71                         }
72                         $args =~ s/\b(\w)/\$$1/g;
73                         $args = "local($args) = \@_;\n$t    ";
74                     }
75                     s/^\s+//;
76                     do expr();
77                     $new =~ s/(["\\])/\\$1/g;
78                     if ($t ne '') {
79                         $new =~ s/(['\\])/\\$1/g;
80                         print OUT $t,
81                           "eval 'sub $name {\n$t    ${args}eval \"$new\";\n$t}';\n";
82                     }
83                     else {
84                         print OUT "sub $name {\n    ${args}eval \"$new\";\n}\n";
85                     }
86                     %curargs = ();
87                 }
88                 else {
89                     s/^\s+//;
90                     do expr();
91                     $new = 1 if $new eq '';
92                     if ($t ne '') {
93                         $new =~ s/(['\\])/\\$1/g;
94                         print OUT $t,"eval 'sub $name {",$new,";}';\n";
95                     }
96                     else {
97                         print OUT $t,"sub $name {",$new,";}\n";
98                     }
99                 }
100             }
101             elsif (/^include <(.*)>/) {
102                 print OUT $t,"do '$1' || die \"Can't include $1: \$!\";\n";
103             }
104             elsif (/^ifdef\s+(\w+)/) {
105                 print OUT $t,"if (defined &$1) {\n";
106                 $tab += 4;
107                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
108             }
109             elsif (/^ifndef\s+(\w+)/) {
110                 print OUT $t,"if (!defined &$1) {\n";
111                 $tab += 4;
112                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
113             }
114             elsif (s/^if\s+//) {
115                 $new = '';
116                 do expr();
117                 print OUT $t,"if ($new) {\n";
118                 $tab += 4;
119                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
120             }
121             elsif (s/^elif\s+//) {
122                 $new = '';
123                 do expr();
124                 $tab -= 4;
125                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
126                 print OUT $t,"}\n${t}elsif ($new) {\n";
127                 $tab += 4;
128                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
129             }
130             elsif (/^else/) {
131                 $tab -= 4;
132                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
133                 print OUT $t,"}\n${t}else {\n";
134                 $tab += 4;
135                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
136             }
137             elsif (/^endif/) {
138                 $tab -= 4;
139                 $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
140                 print OUT $t,"}\n";
141             }
142         }
143     }
144     print OUT "1;\n";
145 }
146
147 sub expr {
148     while ($_ ne '') {
149         s/^(\s+)//              && do {$new .= ' '; next;};
150         s/^(0x[0-9a-fA-F]+)//   && do {$new .= $1; next;};
151         s/^(\d+)//              && do {$new .= $1; next;};
152         s/^("(\\"|[^"])*")//    && do {$new .= $1; next;};
153         s/^'((\\"|[^"])*)'//    && do {
154             if ($curargs{$1}) {
155                 $new .= "ord('\$$1')";
156             }
157             else {
158                 $new .= "ord('$1')";
159             }
160             next;
161         };
162         s/^(struct\s+\w+)//     && do {$new .= "'$1'"; next;};
163         s/^sizeof\s*\(([^)]+)\)/{$1}/ && do {
164             $new .= '$sizeof';
165             next;
166         };
167         s/^([_a-zA-Z]\w*)//     && do {
168             $id = $1;
169             if ($curargs{$id}) {
170                 $new .= '$' . $id;
171             }
172             elsif ($id eq 'defined') {
173                 $new .= 'defined';
174             }
175             elsif (/^\(/) {
176                 s/^\((\w),/("$1",/ if $id =~ /^_IO[WR]*$/;      # cheat
177                 $new .= "&$id";
178             }
179             elsif ($isatype{$id}) {
180                 $new .= "'$id'";
181             }
182             else {
183                 $new .= '&' . $id;
184             }
185             next;
186         };
187         s/^(.)//                        && do {$new .= $1; next;};
188     }
189 }
190 !NO!SUBS!
191 chmod 755 makelib
192 $eunicefix makelib