1 | /* |
---|
2 | * webcomp -- Compile web pages into C source |
---|
3 | * |
---|
4 | * Copyright (c) GoAhead Software Inc., 1995-2000. All Rights Reserved. |
---|
5 | * |
---|
6 | * See the file "license.txt" for usage and redistribution license requirements |
---|
7 | */ |
---|
8 | |
---|
9 | /******************************** Description *********************************/ |
---|
10 | |
---|
11 | /* |
---|
12 | * Usage: webcomp prefix filelist >webrom.c |
---|
13 | * |
---|
14 | * filelist is a file containing the pathnames of all web pages |
---|
15 | * prefix is a path prefix to remove from all the web page pathnames |
---|
16 | * webrom.c is the resulting C source file to compile and link. |
---|
17 | */ |
---|
18 | |
---|
19 | /********************************* Includes ***********************************/ |
---|
20 | |
---|
21 | #include "wsIntrn.h" |
---|
22 | |
---|
23 | /**************************** Forward Declarations ****************************/ |
---|
24 | |
---|
25 | static int compile(char_t *fileList, char_t *prefix); |
---|
26 | static void usage(); |
---|
27 | |
---|
28 | /*********************************** Code *************************************/ |
---|
29 | /* |
---|
30 | * Main program for webpack test harness |
---|
31 | */ |
---|
32 | |
---|
33 | int gmain(int argc, char_t* argv[]) |
---|
34 | { |
---|
35 | char_t *fileList, *prefix; |
---|
36 | |
---|
37 | fileList = NULL; |
---|
38 | |
---|
39 | if (argc != 3) { |
---|
40 | usage(); |
---|
41 | } |
---|
42 | |
---|
43 | prefix = argv[1]; |
---|
44 | fileList = argv[2]; |
---|
45 | |
---|
46 | if (compile(fileList, prefix) < 0) { |
---|
47 | return -1; |
---|
48 | } |
---|
49 | return 0; |
---|
50 | } |
---|
51 | |
---|
52 | /******************************************************************************/ |
---|
53 | /* |
---|
54 | * Output usage message |
---|
55 | */ |
---|
56 | |
---|
57 | static void usage() |
---|
58 | { |
---|
59 | fprintf(stderr, "usage: webcomp prefix filelist >output.c\n"); |
---|
60 | exit(2); |
---|
61 | } |
---|
62 | |
---|
63 | /******************************************************************************/ |
---|
64 | /* |
---|
65 | * Compile the web pages |
---|
66 | */ |
---|
67 | |
---|
68 | static int compile(char_t *fileList, char_t *prefix) |
---|
69 | { |
---|
70 | gstat_t sbuf; |
---|
71 | FILE *lp; |
---|
72 | time_t now; |
---|
73 | char_t file[FNAMESIZE]; |
---|
74 | char_t *cp, *sl; |
---|
75 | char buf[512]; |
---|
76 | unsigned char *p; |
---|
77 | int j, i, len, fd, nFile; |
---|
78 | |
---|
79 | /* |
---|
80 | * Open list of files |
---|
81 | */ |
---|
82 | if ((lp = fopen(fileList, "r")) == NULL) { |
---|
83 | fprintf(stderr, "Can't open file list %s\n", fileList); |
---|
84 | return -1; |
---|
85 | } |
---|
86 | |
---|
87 | time(&now); |
---|
88 | fprintf(stdout, "/*\n * webrom.c -- Compiled Web Pages\n *\n"); |
---|
89 | fprintf(stdout, " * Compiled by GoAhead WebCompile: %s */\n\n", |
---|
90 | gctime(&now)); |
---|
91 | fprintf(stdout, "#include \"wsIntrn.h\"\n\n"); |
---|
92 | fprintf(stdout, "#ifndef WEBS_PAGE_ROM\n"); |
---|
93 | fprintf(stdout, "websRomPageIndexType websRomPageIndex[] = {\n"); |
---|
94 | fprintf(stdout, " { 0, 0, 0 },\n};\n"); |
---|
95 | fprintf(stdout, "#else\n"); |
---|
96 | |
---|
97 | /* |
---|
98 | * Open each input file and compile each web page |
---|
99 | */ |
---|
100 | nFile = 0; |
---|
101 | while (fgets(file, sizeof(file), lp) != NULL) { |
---|
102 | if ((p = strchr(file, '\n')) || (p = strchr(file, '\r'))) { |
---|
103 | *p = '\0'; |
---|
104 | } |
---|
105 | if (*file == '\0') { |
---|
106 | continue; |
---|
107 | } |
---|
108 | if (gstat(file, &sbuf) == 0 && sbuf.st_mode & S_IFDIR) { |
---|
109 | continue; |
---|
110 | } |
---|
111 | if ((fd = gopen(file, O_RDONLY | O_BINARY)) < 0) { |
---|
112 | fprintf(stderr, "Can't open file %s\n", file); |
---|
113 | return -1; |
---|
114 | } |
---|
115 | fprintf(stdout, "static const unsigned char page_%d[] = {\n", nFile); |
---|
116 | |
---|
117 | while ((len = read(fd, buf, sizeof(buf))) > 0) { |
---|
118 | p = buf; |
---|
119 | for (i = 0; i < len; ) { |
---|
120 | fprintf(stdout, " "); |
---|
121 | for (j = 0; p < &buf[len] && j < 16; j++, p++) { |
---|
122 | fprintf(stdout, "%3d,", *p); |
---|
123 | } |
---|
124 | i += j; |
---|
125 | fprintf(stdout, "\n"); |
---|
126 | } |
---|
127 | } |
---|
128 | fprintf(stdout, " 0 };\n\n"); |
---|
129 | |
---|
130 | close(fd); |
---|
131 | nFile++; |
---|
132 | } |
---|
133 | fclose(lp); |
---|
134 | |
---|
135 | /* |
---|
136 | * Now output the page index |
---|
137 | */ |
---|
138 | fprintf(stdout, "websRomPageIndexType websRomPageIndex[] = {\n"); |
---|
139 | |
---|
140 | if ((lp = fopen(fileList, "r")) == NULL) { |
---|
141 | fprintf(stderr, "Can't open file list %s\n", fileList); |
---|
142 | return -1; |
---|
143 | } |
---|
144 | nFile = 0; |
---|
145 | while (fgets(file, sizeof(file), lp) != NULL) { |
---|
146 | if ((p = strchr(file, '\n')) || (p = strchr(file, '\r'))) { |
---|
147 | *p = '\0'; |
---|
148 | } |
---|
149 | if (*file == '\0') { |
---|
150 | continue; |
---|
151 | } |
---|
152 | /* |
---|
153 | * Remove the prefix and add a leading "/" when we print the path |
---|
154 | */ |
---|
155 | if (strncmp(file, prefix, gstrlen(prefix)) == 0) { |
---|
156 | cp = &file[gstrlen(prefix)]; |
---|
157 | } else { |
---|
158 | cp = file; |
---|
159 | } |
---|
160 | while((sl = strchr(file, '\\')) != NULL) { |
---|
161 | *sl = '/'; |
---|
162 | } |
---|
163 | if (*cp == '/') { |
---|
164 | cp++; |
---|
165 | } |
---|
166 | |
---|
167 | if (gstat(file, &sbuf) == 0 && sbuf.st_mode & S_IFDIR) { |
---|
168 | fprintf(stdout, " { T(\"/%s\"), 0, 0 },\n", cp); |
---|
169 | continue; |
---|
170 | } |
---|
171 | fprintf(stdout, " { T(\"/%s\"), page_%d, %d },\n", cp, nFile, |
---|
172 | sbuf.st_size); |
---|
173 | nFile++; |
---|
174 | } |
---|
175 | fclose(lp); |
---|
176 | |
---|
177 | fprintf(stdout, " { 0, 0, 0 },\n"); |
---|
178 | fprintf(stdout, "};\n"); |
---|
179 | fprintf(stdout, "#endif /* WEBS_PAGE_ROM */\n"); |
---|
180 | |
---|
181 | fclose(lp); |
---|
182 | fflush(stdout); |
---|
183 | return 0; |
---|
184 | } |
---|
185 | |
---|
186 | /******************************************************************************/ |
---|