Merge branch 'master' of git://git.ucc.asn.au/acess2
[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 $lines = file($argv[1]);
14
15 $lDepth = 0;
16 $lTree = array();
17 $lStack = array( array("",array()) );
18 foreach($lines as $line)
19 {
20         $line = trim($line);
21         if(preg_match('/^Dir\s+"([^"]+)"\s+{$/', $line, $matches))
22         {
23                 $new = array($matches[1], array());
24                 array_push($lStack, $new);
25                 $lDepth ++;
26                 continue;
27         }
28         if($line == "}")
29         {
30                 $lDepth --;
31                 $lStack[$lDepth][1][] = array_pop($lStack);
32                 continue;
33         }
34         if(preg_match('/^File\s+"([^"]+)"\s+"([^"]+)"$/', $line, $matches))
35         {
36                 $lStack[$lDepth][1][] = array($matches[1], $matches[2]);
37                 continue;
38         }
39         echo "ERROR: $line\n";
40         exit(0);
41 }
42
43 function hd($fp)
44 {
45         return "0x".str_pad( dechex(ord(fgetc($fp))), 2, "0", STR_PAD_LEFT );
46 }
47
48 function ProcessFolder($prefix, $items)
49 {
50         global  $gOutput;
51         foreach($items as $i=>$item)
52         {
53                 if(is_array($item[1]))
54                 {
55                         ProcessFolder("{$prefix}_{$i}", $item[1]);
56                         
57                         $gOutput .= "tInitRD_File {$prefix}_{$i}_entries[] = {\n";
58                         foreach($item[1] as $j=>$child)
59                         {
60                                 if($j)  $gOutput .= ",\n";
61                                 $gOutput .= "\t{\"".addslashes($child[0])."\",&{$prefix}_{$i}_{$j}}";
62                         }
63                         $gOutput .= "\n};\n";
64                         
65                         $size = count($item[1]);
66                         $gOutput .= <<<EOF
67 tVFS_Node {$prefix}_{$i} = {
68         .NumACLs = 1,
69         .ACLs = &gVFS_ACL_EveryoneRX,
70         .Flags = VFS_FFLAG_DIRECTORY,
71         .Size = $size,
72         .ImplPtr = {$prefix}_{$i}_entries,
73         .ReadDir = InitRD_ReadDir,
74         .FindDir = InitRD_FindDir
75 };
76
77 EOF;
78                 }
79                 else {
80                         if(!file_exists($item[1])) {
81                                 echo "ERROR: '{$item[1]}' does not exist\n", 
82                                 exit(1);
83                         }
84                         $size = filesize($item[1]);
85                         
86                         $gOutput .= "Uint8 {$prefix}_{$i}_data[] = {\n";
87                         $fp = fopen($item[1], "rb");
88                         for( $j = 0; $j + 16 < $size; $j += 16 )
89                         {
90                                 $gOutput .= "\t";
91                                 $gOutput .= hd($fp).",".hd($fp).",";
92                                 $gOutput .= hd($fp).",".hd($fp).",";
93                                 $gOutput .= hd($fp).",".hd($fp).",";
94                                 $gOutput .= hd($fp).",".hd($fp).",";
95                                 $gOutput .= hd($fp).",".hd($fp).",";
96                                 $gOutput .= hd($fp).",".hd($fp).",";
97                                 $gOutput .= hd($fp).",".hd($fp).",";
98                                 $gOutput .= hd($fp).",".hd($fp).",\n";
99                         }
100                         $gOutput .= "\t";
101                         for( ; $j < $size; $j ++ )
102                         {
103                                 if( $j & 15 )   $gOutput .= ",";
104                                 $gOutput .= hd($fp);
105                         }
106                         fclose($fp);
107                         $gOutput .= "\n};\n";
108                         $gOutput .= <<<EOF
109 tVFS_Node {$prefix}_{$i} = {
110         .NumACLs = 1,
111         .ACLs = &gVFS_ACL_EveryoneRX,
112         .Flags = 0,
113         .Size = $size,
114         .ImplPtr = {$prefix}_{$i}_data,
115         .Read = InitRD_ReadFile
116 };
117
118 EOF;
119                 }
120         }
121 }
122
123 //print_r($lStack);
124 //exit(1);
125
126 ProcessFolder("gInitRD_Files", $lStack[0][1]);
127
128 $gOutput .= "tInitRD_File gInitRD_Root_Files[] = {\n";
129 foreach($lStack[0][1] as $j=>$child)
130 {
131         if($j)  $gOutput .= ",\n";
132         $gOutput .= "\t{\"".addslashes($child[0])."\",&gInitRD_Files_{$j}}";
133 }
134 $gOutput .= "\n};\n";
135 $nRootFiles = count($lStack[0][1]);
136 $gOutput .= <<<EOF
137 tVFS_Node gInitRD_RootNode = {
138         .NumACLs = 1,
139         .ACLs = &gVFS_ACL_EveryoneRX,
140         .Flags = VFS_FFLAG_DIRECTORY,
141         .Size = $nRootFiles,
142         .ImplPtr = gInitRD_Root_Files,
143         .ReadDir = InitRD_ReadDir,
144         .FindDir = InitRD_FindDir
145 };
146 EOF;
147
148 $fp = fopen($argv[2], "w");
149 fputs($fp, $gOutput);
150 fclose($fp);
151 ?>

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