Usermode/libc - Fix strchr and strrchr behavior
[tpg/acess2.git] / KernelLand / Modules / Filesystems / InitRD / GenerateInitRD.php
1 <?php
2 date_default_timezone_set("UTC");
3
4 $lGenDate = date("Y-m-d H:i");
5 $gOutput = <<<EOF
6 /*
7  * Acess2 InitRD
8  * InitRD Data
9  * Generated $lGenDate
10  */
11 #include "initrd.h"
12
13 EOF;
14
15 define("DEBUG_ENABLED", false);
16
17 $ACESSDIR = getenv("ACESSDIR");
18 $ARCH = getenv("ARCH");
19
20 $gInputFile = $argv[1];
21 $gOutputFile = $argv[2];
22 $gOutputLDOptsFile = $argv[3];
23 $gDepFile = ($argc > 4 ? $argv[4] : false);
24
25 $gDependencies = array();
26
27 $lines = file($argv[1]);
28
29 $lDepth = 0;
30 $lTree = array();
31 $lStack = array( array("",array()) );
32 foreach($lines as $line)
33 {
34         $line = trim($line);
35         if($line == "" || $line[0] == "#")      continue;
36         // Directory
37         if(preg_match('/^Dir\s+"([^"]+)"\s+{$/', $line, $matches))
38         {
39                 $new = array($matches[1], array());
40                 array_push($lStack, $new);
41                 $lDepth ++;
42         }
43         // End of a block
44         elseif($line == "}")
45         {
46                 $lDepth --;
47                 $lStack[$lDepth][1][] = array_pop($lStack);
48         }
49         // File
50         elseif(preg_match('/^((?:Opt)?)File\s+"([^"]+)"(?:\s+"([^"]+)")?$/', $line, $matches))
51         {
52                 $isOptional = $matches[1];
53                 $dstfile = $matches[2];
54                 $path = isset($matches[3]) ? $matches[3] : "";
55                 
56                 if( $path == "" ) {
57                         $path = $dstfile;
58                         $dstfile = basename($dstfile);
59                 }
60                 
61                 // Parse path components
62                 $path = str_replace("__EXT__", "$ACESSDIR/Externals/Output/$ARCH", $path);
63                 $path = str_replace("__BIN__", "$ACESSDIR/Usermode/Output/$ARCH", $path);
64                 $path = str_replace("__FS__", "$ACESSDIR/Usermode/Filesystem", $path);
65                 $path = str_replace("__SRC__", "$ACESSDIR", $path);
66
67                 $gDependencies[] = $path;
68                 
69                 if( !file_exists($path) )
70                 {
71                         if( $isOptional == "" )
72                         {
73                                 // Oops
74                                 echo "ERROR: '{$path}' does not exist\n", 
75                                 exit(1);
76                         }
77                         else
78                         {
79                                 // optional file
80                         }
81                 }
82                 else
83                 {
84                         $lStack[$lDepth][1][] = array($dstfile, $path, $isOptional);
85                 }
86         }
87         else
88         {
89                 echo "ERROR: $line\n";
90                 exit(0);
91         }
92 }
93
94 function hd($fp)
95 {
96         //return "0x".str_pad( dechex(ord(fgetc($fp))), 8, "0", STR_PAD_LEFT );
97         $val = unpack("I", fread($fp, 4));
98         //print_r($val);        exit -1;
99         return "0x".dechex($val[1]);
100 }
101
102 function hd8($fp)
103 {
104         return "0x".str_pad( dechex(ord(fgetc($fp))), 2, "0", STR_PAD_LEFT );
105 }
106
107 $inode = 0;
108 $gSymFiles = array();
109 function ProcessFolder($prefix, $items)
110 {
111         global  $gOutput, $gDependencies;
112         global  $ACESSDIR, $ARCH;
113         global  $inode;
114         global  $gSymFiles;
115         foreach($items as $i=>$item)
116         {
117                 $inode ++;
118                 if(is_array($item[1]))
119                 {
120                         ProcessFolder("{$prefix}_{$i}", $item[1]);
121                         
122                         $gOutput .= "tInitRD_File {$prefix}_{$i}_entries[] = {\n";
123                         foreach($item[1] as $j=>$child)
124                         {
125                                 if($j)  $gOutput .= ",\n";
126                                 $gOutput .= "\t{\"".addslashes($child[0])."\",&{$prefix}_{$i}_{$j}}";
127                         }
128                         $gOutput .= "\n};\n";
129                         
130                         $size = count($item[1]);
131                         $gOutput .= <<<EOF
132 tVFS_Node {$prefix}_{$i} = {
133         .NumACLs = 1,
134         .ACLs = &gVFS_ACL_EveryoneRX,
135         .Flags = VFS_FFLAG_DIRECTORY,
136         .Size = $size,
137         .Inode = {$inode},
138         .ImplPtr = {$prefix}_{$i}_entries,
139         .Type = &gInitRD_DirType
140 };
141
142 EOF;
143                 }
144                 else
145                 {
146                         $path = $item[1];
147                         
148                         if( DEBUG_ENABLED )
149                                 echo $path,"\n";
150                         $size = filesize($path);
151         
152                         $_sym = "_binary_".str_replace(array("/","-",".","+"), "_", $path)."_start";
153                         $gOutput .= "extern Uint8 {$_sym}[];";
154                         $gSymFiles[] = $path;
155                         $gOutput .= <<<EOF
156 tVFS_Node {$prefix}_{$i} = {
157         .NumACLs = 1,
158         .ACLs = &gVFS_ACL_EveryoneRX,
159         .Flags = 0,
160         .Size = $size,
161         .Inode = {$inode},
162         .ImplPtr = $_sym,
163         .Type = &gInitRD_FileType
164 };
165
166 EOF;
167                 }
168         }
169 }
170
171 //print_r($lStack);
172 //exit(1);
173
174 ProcessFolder("gInitRD_Files", $lStack[0][1]);
175
176 $gOutput .= "tInitRD_File gInitRD_Root_Files[] = {\n";
177 foreach($lStack[0][1] as $j=>$child)
178 {
179         if($j)  $gOutput .= ",\n";
180         $gOutput .= "\t{\"".addslashes($child[0])."\",&gInitRD_Files_{$j}}";
181 }
182 $gOutput .= "\n};\n";
183 $nRootFiles = count($lStack[0][1]);
184 $gOutput .= <<<EOF
185 tVFS_Node gInitRD_RootNode = {
186         .NumACLs = 1,
187         .ACLs = &gVFS_ACL_EveryoneRX,
188         .Flags = VFS_FFLAG_DIRECTORY,
189         .Size = $nRootFiles,
190         .ImplPtr = gInitRD_Root_Files,
191         .Type = &gInitRD_DirType
192 };
193 EOF;
194
195 $gOutput .= <<<EOF
196
197 tVFS_Node * const gInitRD_FileList[] = {
198 &gInitRD_RootNode
199 EOF;
200
201 function PutNodePointers($prefix, $items)
202 {
203         global $gOutput;
204         foreach($items as $i=>$item)
205         {
206                 $gOutput .= ",&{$prefix}_{$i}";
207                 if(is_array($item[1]))
208                 {
209                         PutNodePointers("{$prefix}_{$i}", $item[1]);
210                 }
211         }
212 }
213
214 PutNodePointers("gInitRD_Files", $lStack[0][1]);
215
216 $gOutput .= <<<EOF
217 };
218 const int giInitRD_NumFiles = sizeof(gInitRD_FileList)/sizeof(gInitRD_FileList[0]);
219
220 EOF;
221
222
223 $fp = fopen($gOutputFile, "w");
224 fputs($fp, $gOutput);
225 fclose($fp);
226
227 // - Create options call
228 $fp = fopen($gOutputLDOptsFile, "w");
229 fputs($fp, "--format binary\n");
230 foreach($gSymFiles as $sym=>$file)
231 {
232         fputs($fp, "$file\n");
233 //      fputs($fp, "--defsym $sym=_binary_".$sym_filename."_start\n");
234 }
235 fclose($fp);
236
237 if($gDepFile !== false)
238 {
239         $fp = fopen($gDepFile, "w");
240         $line = $gOutputFile.":\t".implode(" ", $gDependencies)."\n";
241         fputs($fp, $line);
242         foreach( $gDependencies as $dep )
243                 fputs($fp, "$dep: \n");
244         fclose($fp);
245 }
246
247 ?>

UCC git Repository :: git.ucc.asn.au