Working on multiprocessing support
[tpg/acess2.git] / Modules / Filesystems / InitRD / GenerateInitRD.php
1 <?php
2 $lGenDate = date("Y-m-d H:i");
3 $gOutput = <<<EOF
4 /*
5  * Acess2 InitRD
6  * InitRD Data
7  * Generated $lGenDate
8  */
9 #include "initrd.h"
10
11 EOF;
12
13 $gInputFile = $argv[1];
14 $gOutputFile = $argv[2];
15 $gDepFile = ($argc > 3 ? $argv[3] : false);
16
17 $gDependencies = array();
18
19 $lines = file($argv[1]);
20
21 $lDepth = 0;
22 $lTree = array();
23 $lStack = array( array("",array()) );
24 foreach($lines as $line)
25 {
26         $line = trim($line);
27         if(preg_match('/^Dir\s+"([^"]+)"\s+{$/', $line, $matches))
28         {
29                 $new = array($matches[1], array());
30                 array_push($lStack, $new);
31                 $lDepth ++;
32                 continue;
33         }
34         if($line == "}")
35         {
36                 $lDepth --;
37                 $lStack[$lDepth][1][] = array_pop($lStack);
38                 continue;
39         }
40         if(preg_match('/^File\s+"([^"]+)"\s+"([^"]+)"$/', $line, $matches))
41         {
42                 $lStack[$lDepth][1][] = array($matches[1], $matches[2]);
43                 $gDependencies[] = $matches[2];
44                 continue;
45         }
46         echo "ERROR: $line\n";
47         exit(0);
48 }
49
50 function hd($fp)
51 {
52         //return "0x".str_pad( dechex(ord(fgetc($fp))), 8, "0", STR_PAD_LEFT );
53         $val = unpack("I", fread($fp, 4));
54         //print_r($val);        exit -1;
55         return "0x".dechex($val[1]);
56 }
57
58 function hd8($fp)
59 {
60         return "0x".str_pad( dechex(ord(fgetc($fp))), 2, "0", STR_PAD_LEFT );
61 }
62
63 function ProcessFolder($prefix, $items)
64 {
65         global  $gOutput;
66         foreach($items as $i=>$item)
67         {
68                 if(is_array($item[1]))
69                 {
70                         ProcessFolder("{$prefix}_{$i}", $item[1]);
71                         
72                         $gOutput .= "tInitRD_File {$prefix}_{$i}_entries[] = {\n";
73                         foreach($item[1] as $j=>$child)
74                         {
75                                 if($j)  $gOutput .= ",\n";
76                                 $gOutput .= "\t{\"".addslashes($child[0])."\",&{$prefix}_{$i}_{$j}}";
77                         }
78                         $gOutput .= "\n};\n";
79                         
80                         $size = count($item[1]);
81                         $gOutput .= <<<EOF
82 tVFS_Node {$prefix}_{$i} = {
83         .NumACLs = 1,
84         .ACLs = &gVFS_ACL_EveryoneRX,
85         .Flags = VFS_FFLAG_DIRECTORY,
86         .Size = $size,
87         .ImplPtr = {$prefix}_{$i}_entries,
88         .ReadDir = InitRD_ReadDir,
89         .FindDir = InitRD_FindDir
90 };
91
92 EOF;
93                 }
94                 else {
95                         if(!file_exists($item[1])) {
96                                 echo "ERROR: '{$item[1]}' does not exist\n", 
97                                 exit(1);
98                         }
99                         $size = filesize($item[1]);
100                         
101                         $fp = fopen($item[1], "rb");
102                         if(0)
103                         {
104                                 $gOutput .= "Uint32 {$prefix}_{$i}_data[] = {\n";
105                                 for( $j = 0; $j + 16 < $size; $j += 16 )
106                                 {
107                                         $gOutput .= "\t";
108                                         $gOutput .= hd($fp).",".hd($fp).",";
109                                         $gOutput .= hd($fp).",".hd($fp).",\n";
110                                 }
111                                 $gOutput .= "\t";
112                                 for( ; $j+3 < $size; $j += 4 )
113                                 {
114                                         if( $j & 15 )   $gOutput .= ",";
115                                         $gOutput .= hd($fp);
116                                 }
117                                 if($j < $size) {
118                                         $tmp = "";
119                                         while($j ++ < $size)
120                                                 $tmp .= fgetc($fp);
121                                         $tmp .= "\0\0\0";
122                                                 $tmp = unpack("I", $tmp);
123                                         $gOutput .= "0x".dechex($tmp[1]);
124                                 }
125                         }
126                         else
127                         {
128                                 $gOutput .= "Uint8 {$prefix}_{$i}_data[] = {\n";
129                                 for( $j = 0; $j + 16 < $size; $j += 16 ) {
130                                         $gOutput .= "\t";
131                                         $gOutput .= hd8($fp).",".hd8($fp).",";
132                                         $gOutput .= hd8($fp).",".hd8($fp).",";
133                                         $gOutput .= hd8($fp).",".hd8($fp).",";
134                                         $gOutput .= hd8($fp).",".hd8($fp).",";
135                                         $gOutput .= hd8($fp).",".hd8($fp).",";
136                                         $gOutput .= hd8($fp).",".hd8($fp).",";
137                                         $gOutput .= hd8($fp).",".hd8($fp).",";
138                                         $gOutput .= hd8($fp).",".hd8($fp).",\n";
139                                 }
140                                 $gOutput .= "\t";
141                                 for( ; $j < $size; $j ++ ) {
142                                         if( $j & 15 )   $gOutput .= ",";
143                                         $gOutput .= hd8($fp);
144                                 }
145                         }
146                         fclose($fp);
147                         $gOutput .= "\n};\n";
148                         $gOutput .= <<<EOF
149 tVFS_Node {$prefix}_{$i} = {
150         .NumACLs = 1,
151         .ACLs = &gVFS_ACL_EveryoneRX,
152         .Flags = 0,
153         .Size = $size,
154         .ImplPtr = {$prefix}_{$i}_data,
155         .Read = InitRD_ReadFile
156 };
157
158 EOF;
159                 }
160         }
161 }
162
163 //print_r($lStack);
164 //exit(1);
165
166 ProcessFolder("gInitRD_Files", $lStack[0][1]);
167
168 $gOutput .= "tInitRD_File gInitRD_Root_Files[] = {\n";
169 foreach($lStack[0][1] as $j=>$child)
170 {
171         if($j)  $gOutput .= ",\n";
172         $gOutput .= "\t{\"".addslashes($child[0])."\",&gInitRD_Files_{$j}}";
173 }
174 $gOutput .= "\n};\n";
175 $nRootFiles = count($lStack[0][1]);
176 $gOutput .= <<<EOF
177 tVFS_Node gInitRD_RootNode = {
178         .NumACLs = 1,
179         .ACLs = &gVFS_ACL_EveryoneRX,
180         .Flags = VFS_FFLAG_DIRECTORY,
181         .Size = $nRootFiles,
182         .ImplPtr = gInitRD_Root_Files,
183         .ReadDir = InitRD_ReadDir,
184         .FindDir = InitRD_FindDir
185 };
186 EOF;
187
188 $fp = fopen($gOutputFile, "w");
189 fputs($fp, $gOutput);
190 fclose($fp);
191
192
193 if($gDepFile !== false)
194 {
195         $fp = fopen($gDepFile, "w");
196         $line = $gOutputFile.":\t".implode(" ", $gDependencies);
197         fputs($fp, $line);
198         fclose($fp);
199 }
200
201 ?>

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