1 | /* |
---|
2 | * rom.c -- Support for ROMed page retrieval. |
---|
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 | * $Id$ |
---|
9 | */ |
---|
10 | |
---|
11 | /******************************** Description *********************************/ |
---|
12 | |
---|
13 | /* |
---|
14 | * This module provides web page retrieval from compiled web pages. Use the |
---|
15 | * webcomp program to compile web pages and link into the GoAhead WebServer. |
---|
16 | * This module uses a hashed symbol table for fast page lookup. |
---|
17 | * |
---|
18 | * Usage: webcomp -f webPageFileList -p Prefix >webrom.c |
---|
19 | */ |
---|
20 | |
---|
21 | /********************************* Includes ***********************************/ |
---|
22 | |
---|
23 | #include <stdlib.h> |
---|
24 | |
---|
25 | #include "wsIntrn.h" |
---|
26 | |
---|
27 | /******************************** Local Data **********************************/ |
---|
28 | |
---|
29 | #ifdef WEBS_PAGE_ROM |
---|
30 | |
---|
31 | sym_fd_t romTab; /* Symbol table for web pages */ |
---|
32 | |
---|
33 | /*********************************** Code *************************************/ |
---|
34 | /* |
---|
35 | * Open the ROM module |
---|
36 | */ |
---|
37 | |
---|
38 | int websRomOpen() |
---|
39 | { |
---|
40 | websRomPageIndexType *wip; |
---|
41 | int nchars; |
---|
42 | char_t name[SYM_MAX]; |
---|
43 | |
---|
44 | romTab = symOpen(WEBS_SYM_INIT); |
---|
45 | |
---|
46 | for (wip = websRomPageIndex; wip->path; wip++) { |
---|
47 | gstrncpy(name, wip->path, SYM_MAX); |
---|
48 | nchars = gstrlen(name) - 1; |
---|
49 | if (nchars > 0 && |
---|
50 | (name[nchars] == '/' || name[nchars] == '\\')) { |
---|
51 | name[nchars] = '\0'; |
---|
52 | } |
---|
53 | symEnter(romTab, name, valueInteger((int) wip), 0); |
---|
54 | } |
---|
55 | return 0; |
---|
56 | } |
---|
57 | |
---|
58 | /******************************************************************************/ |
---|
59 | /* |
---|
60 | * Close the ROM module |
---|
61 | */ |
---|
62 | |
---|
63 | void websRomClose() |
---|
64 | { |
---|
65 | symClose(romTab); |
---|
66 | } |
---|
67 | |
---|
68 | /******************************************************************************/ |
---|
69 | /* |
---|
70 | * Open a web page |
---|
71 | */ |
---|
72 | |
---|
73 | int websRomPageOpen(webs_t wp, char_t *path, int mode, int perm) |
---|
74 | { |
---|
75 | websRomPageIndexType *wip; |
---|
76 | sym_t *sp; |
---|
77 | |
---|
78 | a_assert(websValid(wp)); |
---|
79 | a_assert(path && *path); |
---|
80 | |
---|
81 | if ((sp = symLookup(romTab, path)) == NULL) { |
---|
82 | return -1; |
---|
83 | } |
---|
84 | wip = (websRomPageIndexType*) sp->content.value.integer; |
---|
85 | wip->pos = 0; |
---|
86 | return (wp->docfd = wip - websRomPageIndex); |
---|
87 | } |
---|
88 | |
---|
89 | /******************************************************************************/ |
---|
90 | /* |
---|
91 | * Close a web page |
---|
92 | */ |
---|
93 | |
---|
94 | void websRomPageClose(int fd) |
---|
95 | { |
---|
96 | } |
---|
97 | |
---|
98 | /******************************************************************************/ |
---|
99 | /* |
---|
100 | * Stat a web page |
---|
101 | */ |
---|
102 | |
---|
103 | int websRomPageStat(char_t *path, websStatType *sbuf) |
---|
104 | { |
---|
105 | websRomPageIndexType *wip; |
---|
106 | sym_t *sp; |
---|
107 | |
---|
108 | a_assert(path && *path); |
---|
109 | |
---|
110 | if ((sp = symLookup(romTab, path)) == NULL) { |
---|
111 | return -1; |
---|
112 | } |
---|
113 | wip = (websRomPageIndexType*) sp->content.value.integer; |
---|
114 | |
---|
115 | memset(sbuf, 0, sizeof(websStatType)); |
---|
116 | sbuf->size = wip->size; |
---|
117 | if (wip->page == NULL) { |
---|
118 | sbuf->isDir = 1; |
---|
119 | } |
---|
120 | return 0; |
---|
121 | } |
---|
122 | |
---|
123 | /******************************************************************************/ |
---|
124 | /* |
---|
125 | * Read a web page |
---|
126 | */ |
---|
127 | |
---|
128 | int websRomPageReadData(webs_t wp, char *buf, int nBytes) |
---|
129 | { |
---|
130 | websRomPageIndexType *wip; |
---|
131 | int len; |
---|
132 | |
---|
133 | a_assert(websValid(wp)); |
---|
134 | a_assert(buf); |
---|
135 | a_assert(wp->docfd >= 0); |
---|
136 | |
---|
137 | wip = &websRomPageIndex[wp->docfd]; |
---|
138 | |
---|
139 | len = min(wip->size - wip->pos, nBytes); |
---|
140 | memcpy(buf, &wip->page[wip->pos], len); |
---|
141 | wip->pos += len; |
---|
142 | return len; |
---|
143 | } |
---|
144 | |
---|
145 | /******************************************************************************/ |
---|
146 | /* |
---|
147 | * Position a web page |
---|
148 | */ |
---|
149 | |
---|
150 | long websRomPageSeek(webs_t wp, long offset, int origin) |
---|
151 | { |
---|
152 | websRomPageIndexType *wip; |
---|
153 | long pos; |
---|
154 | |
---|
155 | a_assert(websValid(wp)); |
---|
156 | a_assert(origin == SEEK_SET || origin == SEEK_CUR || origin == SEEK_END); |
---|
157 | a_assert(wp->docfd >= 0); |
---|
158 | |
---|
159 | wip = &websRomPageIndex[wp->docfd]; |
---|
160 | |
---|
161 | if (origin != SEEK_SET && origin != SEEK_CUR && origin != SEEK_END) { |
---|
162 | errno = EINVAL; |
---|
163 | return -1; |
---|
164 | } |
---|
165 | |
---|
166 | if (wp->docfd < 0) { |
---|
167 | errno = EBADF; |
---|
168 | return -1; |
---|
169 | } |
---|
170 | |
---|
171 | pos = offset; |
---|
172 | switch (origin) { |
---|
173 | case SEEK_CUR: |
---|
174 | pos = wip->pos + offset; |
---|
175 | break; |
---|
176 | case SEEK_END: |
---|
177 | pos = wip->size + offset; |
---|
178 | break; |
---|
179 | default: |
---|
180 | break; |
---|
181 | } |
---|
182 | |
---|
183 | if (pos < 0) { |
---|
184 | errno = EBADF; |
---|
185 | return -1; |
---|
186 | } |
---|
187 | |
---|
188 | return (wip->pos = pos); |
---|
189 | } |
---|
190 | |
---|
191 | #endif /* WEBS_PAGE_ROM */ |
---|
192 | |
---|
193 | /******************************************************************************/ |
---|