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

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