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