adamdunkels | 9fcf9d6 | 2003-09-04 19:46:32 +0000 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | |
| 3 | open(OUTPUT, "> httpd-fsdata.c"); |
| 4 | |
| 5 | chdir("httpd-fs"); |
| 6 | open(FILES, "find . -type f |"); |
| 7 | |
| 8 | |
| 9 | while($file = <FILES>) { |
| 10 | |
| 11 | # Do not include files in CVS directories nor backup files. |
| 12 | if($file =~ /(CVS|~)/) { |
| 13 | next; |
| 14 | } |
| 15 | |
| 16 | chop($file); |
| 17 | |
| 18 | open(HEADER, "> /tmp/header") || die $!; |
adamdunkels | f57466f | 2003-09-05 21:24:08 +0000 | [diff] [blame] | 19 | if($file =~ /404.html/) { |
| 20 | print(HEADER "HTTP/1.0 404 File not found\r\n"); |
| 21 | } else { |
| 22 | print(HEADER "HTTP/1.0 200 OK\r\n"); |
| 23 | } |
adamdunkels | 9fcf9d6 | 2003-09-04 19:46:32 +0000 | [diff] [blame] | 24 | print(HEADER "Server: Contiki/pre-1.1 (http://dunkels.com/adam/contiki/)\r\n"); |
| 25 | if($file =~ /\.html$/) { |
| 26 | print(HEADER "Content-type: text/html\r\n"); |
| 27 | } elsif($file =~ /\.gif$/) { |
| 28 | print(HEADER "Content-type: image/gif\r\n"); |
| 29 | } elsif($file =~ /\.png$/) { |
| 30 | print(HEADER "Content-type: image/png\r\n"); |
| 31 | } elsif($file =~ /\.jpg$/) { |
| 32 | print(HEADER "Content-type: image/jpeg\r\n"); |
| 33 | } elsif($file =~ /\.css$/) { |
| 34 | print(HEADER "Content-type: text/css\r\n"); |
| 35 | } else { |
| 36 | print(HEADER "Content-type: text/plain\r\n"); |
| 37 | } |
| 38 | print(HEADER "\r\n"); |
| 39 | close(HEADER); |
| 40 | |
| 41 | unless($file =~ /\.plain$/ || $file =~ /cgi/) { |
| 42 | system("cat /tmp/header $file > /tmp/file"); |
| 43 | } else { |
| 44 | system("cp $file /tmp/file"); |
| 45 | } |
| 46 | |
| 47 | open(FILE, "/tmp/file"); |
| 48 | unlink("/tmp/file"); |
| 49 | unlink("/tmp/header"); |
| 50 | |
| 51 | $file =~ s/\.//; |
| 52 | $fvar = $file; |
| 53 | $fvar =~ s-/-_-g; |
| 54 | $fvar =~ s-\.-_-g; |
| 55 | print(OUTPUT "static const unsigned char data".$fvar."[] __attribute__ ((aligned (2))) = {\n"); |
| 56 | print(OUTPUT "\t/* $file */\n\t"); |
| 57 | for($j = 0; $j < length($file); $j++) { |
| 58 | printf(OUTPUT "%#02x, ", unpack("C", substr($file, $j, 1))); |
| 59 | } |
| 60 | printf(OUTPUT "0,\n"); |
| 61 | |
| 62 | if((length($file) & 1) == 0) { |
| 63 | printf(OUTPUT "0,\n"); |
| 64 | } |
| 65 | |
| 66 | $i = 0; |
| 67 | while(read(FILE, $data, 1)) { |
| 68 | if($i == 0) { |
| 69 | print(OUTPUT "\t"); |
| 70 | } |
| 71 | printf(OUTPUT "%#02x, ", unpack("C", $data)); |
| 72 | $i++; |
| 73 | if($i == 10) { |
| 74 | printf(OUTPUT "\n"); |
| 75 | $i = 0; |
| 76 | } |
| 77 | |
| 78 | } |
| 79 | print(OUTPUT "};\n\n"); |
| 80 | close(FILE); |
| 81 | push(@fvars, $fvar); |
| 82 | push(@files, $file); |
| 83 | } |
| 84 | |
| 85 | for($i = 0; $i < @fvars; $i++) { |
| 86 | $file = $files[$i]; |
| 87 | $fvar = $fvars[$i]; |
| 88 | |
| 89 | if($i == 0) { |
| 90 | $prevfile = "NULL"; |
| 91 | } else { |
| 92 | $prevfile = "file" . $fvars[$i - 1]; |
| 93 | } |
| 94 | $len = length($file) + 1; |
| 95 | if(($len & 1) == 1) { |
| 96 | $len++; |
| 97 | } |
| 98 | print(OUTPUT "const struct httpd_fsdata_file file".$fvar."[] = {{$prevfile, data$fvar, "); |
| 99 | print(OUTPUT "data$fvar + ". $len .", "); |
| 100 | print(OUTPUT "sizeof(data$fvar) - ". $len ."}};\n\n"); |
| 101 | } |
| 102 | |
| 103 | print(OUTPUT "#define HTTPD_FS_ROOT file$fvars[$i - 1]\n\n"); |
| 104 | print(OUTPUT "#define HTTPD_FS_NUMFILES $i"); |