]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
2 | * "$Id$" | |
3 | * | |
4 | * On-line help CGI for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 1997-2006 by Easy Software Products. | |
7 | * | |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
26 | * main() - Main entry for CGI. | |
27 | */ | |
28 | ||
29 | /* | |
30 | * Include necessary headers... | |
31 | */ | |
32 | ||
33 | #include "cgi-private.h" | |
34 | ||
35 | ||
36 | /* | |
37 | * 'main()' - Main entry for CGI. | |
38 | */ | |
39 | ||
40 | int /* O - Exit status */ | |
41 | main(int argc, /* I - Number of command-line arguments */ | |
42 | char *argv[]) /* I - Command-line arguments */ | |
43 | { | |
44 | help_index_t *hi, /* Help index */ | |
45 | *si; /* Search index */ | |
46 | help_node_t **n; /* Current help node */ | |
47 | int i, j; /* Looping vars */ | |
48 | const char *query; /* Search query */ | |
49 | const char *server_root; /* CUPS_SERVERROOT environment variable */ | |
50 | const char *docroot; /* CUPS_DOCROOT environment variable */ | |
51 | const char *helpfile; /* Current help file */ | |
52 | const char *topic; /* Current topic */ | |
53 | char topic_data[1024]; /* Topic form data */ | |
54 | const char *section; /* Current section */ | |
55 | char filename[1024], /* Filename */ | |
56 | directory[1024]; /* Directory */ | |
57 | cups_file_t *fp; /* Help file */ | |
58 | char line[1024]; /* Line from file */ | |
59 | ||
60 | ||
61 | /* | |
62 | * Get any form variables... | |
63 | */ | |
64 | ||
65 | cgiInitialize(); | |
66 | ||
67 | /* | |
68 | * Set the web interface section... | |
69 | */ | |
70 | ||
71 | cgiSetVariable("SECTION", "help"); | |
72 | ||
73 | /* | |
74 | * Load the help index... | |
75 | */ | |
76 | ||
77 | if ((server_root = getenv("CUPS_SERVERROOT")) == NULL) | |
78 | server_root = CUPS_SERVERROOT; | |
79 | ||
80 | snprintf(filename, sizeof(filename), "%s/help.index", server_root); | |
81 | ||
82 | if ((docroot = getenv("CUPS_DOCROOT")) == NULL) | |
83 | docroot = CUPS_DOCROOT; | |
84 | ||
85 | snprintf(directory, sizeof(directory), "%s/help", docroot); | |
86 | ||
87 | fprintf(stderr, "DEBUG: helpLoadIndex(filename=\"%s\", directory=\"%s\")\n", | |
88 | filename, directory); | |
89 | ||
90 | hi = helpLoadIndex(filename, directory); | |
91 | if (!hi) | |
92 | { | |
93 | perror(filename); | |
94 | ||
95 | cgiStartHTML("Help"); | |
96 | cgiSetVariable("ERROR", "Unable to load help index!"); | |
97 | cgiCopyTemplateLang("error.tmpl"); | |
98 | cgiEndHTML(); | |
99 | ||
100 | return (1); | |
101 | } | |
102 | ||
103 | fprintf(stderr, "hi->num_nodes=%d\n", hi->num_nodes); | |
104 | ||
105 | /* | |
106 | * See if we are viewing a file... | |
107 | */ | |
108 | ||
109 | for (i = 0; i < argc; i ++) | |
110 | fprintf(stderr, "argv[%d]=\"%s\"\n", i, argv[i]); | |
111 | ||
112 | if ((helpfile = getenv("PATH_INFO")) != NULL) | |
113 | helpfile ++; | |
114 | else if (strstr(argv[0], "help.cgi")) | |
115 | helpfile = NULL; | |
116 | else | |
117 | helpfile = argv[0]; | |
118 | ||
119 | if (helpfile) | |
120 | { | |
121 | /* | |
122 | * Verify that the help file exists and is part of the index... | |
123 | */ | |
124 | ||
125 | snprintf(filename, sizeof(filename), "%s/help/%s", docroot, helpfile); | |
126 | ||
127 | fprintf(stderr, "DEBUG: helpfile=\"%s\", filename=\"%s\"\n", | |
128 | helpfile, filename); | |
129 | ||
130 | if (access(filename, R_OK)) | |
131 | { | |
132 | perror(filename); | |
133 | ||
134 | cgiStartHTML("Help"); | |
135 | cgiSetVariable("ERROR", "Unable to access help file!"); | |
136 | cgiCopyTemplateLang("error.tmpl"); | |
137 | cgiEndHTML(); | |
138 | ||
139 | return (1); | |
140 | } | |
141 | ||
142 | if ((n = helpFindNode(hi, helpfile, NULL)) == NULL) | |
143 | { | |
144 | cgiStartHTML("Help"); | |
145 | cgiSetVariable("ERROR", "Help file not in index!"); | |
146 | cgiCopyTemplateLang("error.tmpl"); | |
147 | cgiEndHTML(); | |
148 | ||
149 | return (1); | |
150 | } | |
151 | ||
152 | /* | |
153 | * Set the page title and save the help file... | |
154 | */ | |
155 | ||
156 | cgiSetVariable("HELPFILE", helpfile); | |
157 | cgiSetVariable("HELPTITLE", n[0]->text); | |
158 | ||
159 | /* | |
160 | * Send a standard page header... | |
161 | */ | |
162 | ||
163 | cgiStartHTML(n[0]->text); | |
164 | } | |
165 | else | |
166 | { | |
167 | /* | |
168 | * Send a standard page header... | |
169 | */ | |
170 | ||
171 | cgiStartHTML("Help"); | |
172 | } | |
173 | ||
174 | /* | |
175 | * Do a search as needed... | |
176 | */ | |
177 | ||
178 | query = cgiGetVariable("QUERY"); | |
179 | topic = cgiGetVariable("TOPIC"); | |
180 | si = helpSearchIndex(hi, query, topic, helpfile); | |
181 | ||
182 | if (si) | |
183 | { | |
184 | help_node_t **nn; /* Parent node */ | |
185 | ||
186 | ||
187 | fprintf(stderr, "si=%p, si->num_nodes=%d, si->sorted=%p\n", si, | |
188 | si->num_nodes, si->sorted); | |
189 | ||
190 | for (i = 0, n = si->sorted; i < si->num_nodes; i ++, n ++) | |
191 | { | |
192 | if (helpfile && n[0]->anchor) | |
193 | snprintf(line, sizeof(line), "#%s", n[0]->anchor); | |
194 | else if (n[0]->anchor) | |
195 | snprintf(line, sizeof(line), "/help/%s?QUERY=%s#%s", n[0]->filename, | |
196 | query ? query : "", n[0]->anchor); | |
197 | else | |
198 | snprintf(line, sizeof(line), "/help/%s?QUERY=%s", n[0]->filename, | |
199 | query ? query : ""); | |
200 | ||
201 | cgiSetArray("QTEXT", i, n[0]->text); | |
202 | cgiSetArray("QLINK", i, line); | |
203 | ||
204 | if (!helpfile && n[0]->anchor) | |
205 | { | |
206 | nn = helpFindNode(hi, n[0]->filename, NULL); | |
207 | ||
208 | snprintf(line, sizeof(line), "/help/%s?QUERY=%s", nn[0]->filename, | |
209 | query ? query : ""); | |
210 | ||
211 | cgiSetArray("QPTEXT", i, nn[0]->text); | |
212 | cgiSetArray("QPLINK", i, line); | |
213 | } | |
214 | else | |
215 | { | |
216 | cgiSetArray("QPTEXT", i, ""); | |
217 | cgiSetArray("QPLINK", i, ""); | |
218 | } | |
219 | ||
220 | fprintf(stderr, "DEBUG: [%d] = \"%s\" @ \"%s\"\n", i, n[0]->text, line); | |
221 | } | |
222 | ||
223 | helpDeleteIndex(si); | |
224 | } | |
225 | ||
226 | /* | |
227 | * OK, now list the bookmarks within the index... | |
228 | */ | |
229 | ||
230 | for (i = hi->num_nodes, j = 0, n = hi->sorted, section = NULL; | |
231 | i > 0; | |
232 | i --, n ++) | |
233 | { | |
234 | if (n[0]->anchor) | |
235 | continue; | |
236 | ||
237 | /* | |
238 | * Add a section link as needed... | |
239 | */ | |
240 | ||
241 | if (n[0]->section && | |
242 | (!section || strcmp(n[0]->section, section))) | |
243 | { | |
244 | /* | |
245 | * Add a link for this node... | |
246 | */ | |
247 | ||
248 | snprintf(line, sizeof(line), "/help/?TOPIC=%s&QUERY=%s", | |
249 | cgiFormEncode(topic_data, n[0]->section, sizeof(topic_data)), | |
250 | query ? query : ""); | |
251 | cgiSetArray("BMLINK", j, line); | |
252 | cgiSetArray("BMTEXT", j, n[0]->section); | |
253 | cgiSetArray("BMINDENT", j, "0"); | |
254 | ||
255 | j ++; | |
256 | section = n[0]->section; | |
257 | } | |
258 | ||
259 | if (!topic || strcmp(n[0]->section, topic)) | |
260 | continue; | |
261 | ||
262 | /* | |
263 | * Add a link for this node... | |
264 | */ | |
265 | ||
266 | snprintf(line, sizeof(line), "/help/%s?TOPIC=%s&QUERY=%s", n[0]->filename, | |
267 | cgiFormEncode(topic_data, n[0]->section, sizeof(topic_data)), | |
268 | query ? query : ""); | |
269 | cgiSetArray("BMLINK", j, line); | |
270 | cgiSetArray("BMTEXT", j, n[0]->text); | |
271 | cgiSetArray("BMINDENT", j, "1"); | |
272 | ||
273 | j ++; | |
274 | ||
275 | if (helpfile && !strcmp(helpfile, n[0]->filename)) | |
276 | { | |
277 | int ii; /* Looping var */ | |
278 | help_node_t **nn; /* Pointer to sub-node */ | |
279 | ||
280 | ||
281 | for (ii = hi->num_nodes, nn = hi->sorted; ii > 0; ii --, nn ++) | |
282 | if (nn[0]->anchor && !strcmp(helpfile, nn[0]->filename)) | |
283 | { | |
284 | /* | |
285 | * Add a link for this node... | |
286 | */ | |
287 | ||
288 | snprintf(line, sizeof(line), "#%s", nn[0]->anchor); | |
289 | cgiSetArray("BMLINK", j, line); | |
290 | cgiSetArray("BMTEXT", j, nn[0]->text); | |
291 | cgiSetArray("BMINDENT", j, "2"); | |
292 | ||
293 | j ++; | |
294 | } | |
295 | } | |
296 | } | |
297 | ||
298 | /* | |
299 | * Show the search and bookmark content... | |
300 | */ | |
301 | ||
302 | cgiCopyTemplateLang("help-header.tmpl"); | |
303 | ||
304 | /* | |
305 | * If we are viewing a file, copy it in now... | |
306 | */ | |
307 | ||
308 | if (helpfile) | |
309 | { | |
310 | if ((fp = cupsFileOpen(filename, "r")) != NULL) | |
311 | { | |
312 | int inbody; /* Are we inside the body? */ | |
313 | ||
314 | ||
315 | inbody = 0; | |
316 | ||
317 | while (cupsFileGets(fp, line, sizeof(line))) | |
318 | { | |
319 | if (inbody) | |
320 | { | |
321 | if (!strncasecmp(line, "</BODY>", 7)) | |
322 | break; | |
323 | ||
324 | printf("%s\n", line); | |
325 | } | |
326 | else if (!strncasecmp(line, "<BODY", 5)) | |
327 | inbody = 1; | |
328 | } | |
329 | ||
330 | cupsFileClose(fp); | |
331 | } | |
332 | else | |
333 | { | |
334 | perror(filename); | |
335 | cgiSetVariable("ERROR", "Unable to open help file."); | |
336 | cgiCopyTemplateLang("error.tmpl"); | |
337 | } | |
338 | } | |
339 | ||
340 | /* | |
341 | * Send a standard trailer... | |
342 | */ | |
343 | ||
344 | cgiEndHTML(); | |
345 | ||
346 | /* | |
347 | * Delete the index... | |
348 | */ | |
349 | ||
350 | helpDeleteIndex(hi); | |
351 | ||
352 | /* | |
353 | * Return with no errors... | |
354 | */ | |
355 | ||
356 | return (0); | |
357 | } | |
358 | ||
359 | ||
360 | /* | |
361 | * End of "$Id$". | |
362 | */ |