]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f7faf1f5 | 2 | * "$Id$" |
ef416fc2 | 3 | * |
4 | * On-line help CGI for the Common UNIX Printing System (CUPS). | |
5 | * | |
bc44d920 | 6 | * Copyright 2007 by Apple Inc. |
ef416fc2 | 7 | * Copyright 1997-2006 by Easy Software Products. |
8 | * | |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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/". | |
ef416fc2 | 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 */ | |
ecdc0628 | 37 | help_node_t *n; /* Current help node */ |
38 | int i; /* Looping var */ | |
ef416fc2 | 39 | const char *query; /* Search query */ |
ecdc0628 | 40 | const char *cache_dir; /* CUPS_CACHEDIR environment variable */ |
ef416fc2 | 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 */ | |
4744bd90 | 50 | int printable; /* Show printable version? */ |
ef416fc2 | 51 | |
52 | ||
53 | /* | |
54 | * Get any form variables... | |
55 | */ | |
56 | ||
57 | cgiInitialize(); | |
58 | ||
4744bd90 | 59 | printable = cgiGetVariable("PRINTABLE") != NULL; |
60 | ||
ef416fc2 | 61 | /* |
62 | * Set the web interface section... | |
63 | */ | |
64 | ||
65 | cgiSetVariable("SECTION", "help"); | |
66 | ||
67 | /* | |
68 | * Load the help index... | |
69 | */ | |
70 | ||
ecdc0628 | 71 | if ((cache_dir = getenv("CUPS_CACHEDIR")) == NULL) |
72 | cache_dir = CUPS_CACHEDIR; | |
ef416fc2 | 73 | |
ecdc0628 | 74 | snprintf(filename, sizeof(filename), "%s/help.index", cache_dir); |
ef416fc2 | 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 | ||
4744bd90 | 89 | cgiStartHTML(cgiText(_("Help"))); |
ef416fc2 | 90 | cgiSetVariable("ERROR", "Unable to load help index!"); |
91 | cgiCopyTemplateLang("error.tmpl"); | |
92 | cgiEndHTML(); | |
93 | ||
94 | return (1); | |
95 | } | |
96 | ||
ecdc0628 | 97 | fprintf(stderr, "DEBUG: %d nodes in help index...\n", |
98 | cupsArrayCount(hi->nodes)); | |
ef416fc2 | 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) | |
b423cd4c | 108 | { |
ef416fc2 | 109 | helpfile ++; |
b423cd4c | 110 | |
111 | if (!*helpfile) | |
112 | helpfile = NULL; | |
113 | } | |
ef416fc2 | 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 | ||
4744bd90 | 130 | cgiStartHTML(cgiText(_("Help"))); |
ef416fc2 | 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 | { | |
4744bd90 | 140 | cgiStartHTML(cgiText(_("Help"))); |
ef416fc2 | 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); | |
ecdc0628 | 153 | cgiSetVariable("HELPTITLE", n->text); |
db1f069b | 154 | cgiSetVariable("TOPIC", n->section); |
ef416fc2 | 155 | |
156 | /* | |
157 | * Send a standard page header... | |
158 | */ | |
159 | ||
4744bd90 | 160 | if (printable) |
161 | puts("Content-Type: text/html;charset=utf-8\n"); | |
162 | else | |
163 | cgiStartHTML(n->text); | |
ef416fc2 | 164 | } |
165 | else | |
166 | { | |
167 | /* | |
168 | * Send a standard page header... | |
169 | */ | |
170 | ||
4744bd90 | 171 | cgiStartHTML(cgiText(_("Help"))); |
ef416fc2 | 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 | ||
f301802f | 182 | fprintf(stderr, "DEBUG: query=\"%s\", topic=\"%s\"\n", |
183 | query ? query : "(null)", topic ? topic : "(null)"); | |
b423cd4c | 184 | |
ef416fc2 | 185 | if (si) |
186 | { | |
ecdc0628 | 187 | help_node_t *nn; /* Parent node */ |
ef416fc2 | 188 | |
189 | ||
ecdc0628 | 190 | fprintf(stderr, |
191 | "DEBUG: si=%p, si->sorted=%p, cupsArrayCount(si->sorted)=%d\n", si, | |
192 | si->sorted, cupsArrayCount(si->sorted)); | |
ef416fc2 | 193 | |
b423cd4c | 194 | for (i = 0, n = (help_node_t *)cupsArrayFirst(si->sorted); |
ecdc0628 | 195 | n; |
b423cd4c | 196 | i ++, n = (help_node_t *)cupsArrayNext(si->sorted)) |
ef416fc2 | 197 | { |
ecdc0628 | 198 | if (helpfile && n->anchor) |
199 | snprintf(line, sizeof(line), "#%s", n->anchor); | |
200 | else if (n->anchor) | |
201 | snprintf(line, sizeof(line), "/help/%s?QUERY=%s#%s", n->filename, | |
202 | query ? query : "", n->anchor); | |
ef416fc2 | 203 | else |
ecdc0628 | 204 | snprintf(line, sizeof(line), "/help/%s?QUERY=%s", n->filename, |
ef416fc2 | 205 | query ? query : ""); |
206 | ||
ecdc0628 | 207 | cgiSetArray("QTEXT", i, n->text); |
ef416fc2 | 208 | cgiSetArray("QLINK", i, line); |
209 | ||
ecdc0628 | 210 | if (!helpfile && n->anchor) |
ef416fc2 | 211 | { |
ecdc0628 | 212 | nn = helpFindNode(hi, n->filename, NULL); |
ef416fc2 | 213 | |
ecdc0628 | 214 | snprintf(line, sizeof(line), "/help/%s?QUERY=%s", nn->filename, |
ef416fc2 | 215 | query ? query : ""); |
216 | ||
ecdc0628 | 217 | cgiSetArray("QPTEXT", i, nn->text); |
ef416fc2 | 218 | cgiSetArray("QPLINK", i, line); |
219 | } | |
220 | else | |
221 | { | |
222 | cgiSetArray("QPTEXT", i, ""); | |
223 | cgiSetArray("QPLINK", i, ""); | |
224 | } | |
225 | ||
ecdc0628 | 226 | fprintf(stderr, "DEBUG: [%d] = \"%s\" @ \"%s\"\n", i, n->text, line); |
ef416fc2 | 227 | } |
228 | ||
229 | helpDeleteIndex(si); | |
230 | } | |
231 | ||
232 | /* | |
233 | * OK, now list the bookmarks within the index... | |
234 | */ | |
235 | ||
ecdc0628 | 236 | for (i = 0, section = NULL, n = (help_node_t *)cupsArrayFirst(hi->sorted); |
237 | n; | |
238 | n = (help_node_t *)cupsArrayNext(hi->sorted)) | |
ef416fc2 | 239 | { |
ecdc0628 | 240 | if (n->anchor) |
ef416fc2 | 241 | continue; |
242 | ||
243 | /* | |
244 | * Add a section link as needed... | |
245 | */ | |
246 | ||
ecdc0628 | 247 | if (n->section && |
248 | (!section || strcmp(n->section, section))) | |
ef416fc2 | 249 | { |
250 | /* | |
251 | * Add a link for this node... | |
252 | */ | |
253 | ||
254 | snprintf(line, sizeof(line), "/help/?TOPIC=%s&QUERY=%s", | |
ecdc0628 | 255 | cgiFormEncode(topic_data, n->section, sizeof(topic_data)), |
ef416fc2 | 256 | query ? query : ""); |
ecdc0628 | 257 | cgiSetArray("BMLINK", i, line); |
258 | cgiSetArray("BMTEXT", i, n->section); | |
259 | cgiSetArray("BMINDENT", i, "0"); | |
ef416fc2 | 260 | |
ecdc0628 | 261 | i ++; |
262 | section = n->section; | |
ef416fc2 | 263 | } |
264 | ||
ecdc0628 | 265 | if (!topic || strcmp(n->section, topic)) |
ef416fc2 | 266 | continue; |
267 | ||
268 | /* | |
269 | * Add a link for this node... | |
270 | */ | |
271 | ||
ecdc0628 | 272 | snprintf(line, sizeof(line), "/help/%s?TOPIC=%s&QUERY=%s", n->filename, |
273 | cgiFormEncode(topic_data, n->section, sizeof(topic_data)), | |
ef416fc2 | 274 | query ? query : ""); |
ecdc0628 | 275 | cgiSetArray("BMLINK", i, line); |
276 | cgiSetArray("BMTEXT", i, n->text); | |
277 | cgiSetArray("BMINDENT", i, "1"); | |
ef416fc2 | 278 | |
ecdc0628 | 279 | i ++; |
ef416fc2 | 280 | |
ecdc0628 | 281 | if (helpfile && !strcmp(helpfile, n->filename)) |
ef416fc2 | 282 | { |
ecdc0628 | 283 | help_node_t *nn; /* Pointer to sub-node */ |
ef416fc2 | 284 | |
285 | ||
ecdc0628 | 286 | cupsArraySave(hi->sorted); |
287 | ||
288 | for (nn = (help_node_t *)cupsArrayFirst(hi->sorted); | |
289 | nn; | |
290 | nn = (help_node_t *)cupsArrayNext(hi->sorted)) | |
291 | if (nn->anchor && !strcmp(helpfile, nn->filename)) | |
ef416fc2 | 292 | { |
293 | /* | |
294 | * Add a link for this node... | |
295 | */ | |
296 | ||
ecdc0628 | 297 | snprintf(line, sizeof(line), "#%s", nn->anchor); |
298 | cgiSetArray("BMLINK", i, line); | |
299 | cgiSetArray("BMTEXT", i, nn->text); | |
300 | cgiSetArray("BMINDENT", i, "2"); | |
ef416fc2 | 301 | |
ecdc0628 | 302 | i ++; |
ef416fc2 | 303 | } |
ecdc0628 | 304 | |
305 | cupsArrayRestore(hi->sorted); | |
ef416fc2 | 306 | } |
307 | } | |
308 | ||
309 | /* | |
310 | * Show the search and bookmark content... | |
311 | */ | |
312 | ||
4744bd90 | 313 | if (!helpfile || !printable) |
314 | cgiCopyTemplateLang("help-header.tmpl"); | |
315 | else | |
316 | cgiCopyTemplateLang("help-printable.tmpl"); | |
ef416fc2 | 317 | |
318 | /* | |
319 | * If we are viewing a file, copy it in now... | |
320 | */ | |
321 | ||
322 | if (helpfile) | |
323 | { | |
324 | if ((fp = cupsFileOpen(filename, "r")) != NULL) | |
325 | { | |
326 | int inbody; /* Are we inside the body? */ | |
327 | ||
328 | ||
329 | inbody = 0; | |
330 | ||
331 | while (cupsFileGets(fp, line, sizeof(line))) | |
332 | { | |
333 | if (inbody) | |
334 | { | |
335 | if (!strncasecmp(line, "</BODY>", 7)) | |
336 | break; | |
337 | ||
338 | printf("%s\n", line); | |
339 | } | |
340 | else if (!strncasecmp(line, "<BODY", 5)) | |
341 | inbody = 1; | |
342 | } | |
343 | ||
344 | cupsFileClose(fp); | |
345 | } | |
346 | else | |
347 | { | |
348 | perror(filename); | |
349 | cgiSetVariable("ERROR", "Unable to open help file."); | |
350 | cgiCopyTemplateLang("error.tmpl"); | |
351 | } | |
352 | } | |
353 | ||
354 | /* | |
355 | * Send a standard trailer... | |
356 | */ | |
357 | ||
4744bd90 | 358 | if (!printable) |
359 | cgiEndHTML(); | |
360 | else | |
361 | puts("</BODY>\n</HTML>"); | |
ef416fc2 | 362 | |
363 | /* | |
364 | * Delete the index... | |
365 | */ | |
366 | ||
367 | helpDeleteIndex(hi); | |
368 | ||
369 | /* | |
370 | * Return with no errors... | |
371 | */ | |
372 | ||
373 | return (0); | |
374 | } | |
375 | ||
376 | ||
377 | /* | |
f7faf1f5 | 378 | * End of "$Id$". |
ef416fc2 | 379 | */ |