]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
c07d5b2d | 2 | * "$Id: help.c 177 2006-06-21 00:20:03Z jlovell $" |
ef416fc2 | 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 */ | |
ecdc0628 | 46 | help_node_t *n; /* Current help node */ |
47 | int i; /* Looping var */ | |
ef416fc2 | 48 | const char *query; /* Search query */ |
ecdc0628 | 49 | const char *cache_dir; /* CUPS_CACHEDIR environment variable */ |
ef416fc2 | 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 */ | |
4744bd90 | 59 | int printable; /* Show printable version? */ |
ef416fc2 | 60 | |
61 | ||
62 | /* | |
63 | * Get any form variables... | |
64 | */ | |
65 | ||
66 | cgiInitialize(); | |
67 | ||
4744bd90 | 68 | printable = cgiGetVariable("PRINTABLE") != NULL; |
69 | ||
ef416fc2 | 70 | /* |
71 | * Set the web interface section... | |
72 | */ | |
73 | ||
74 | cgiSetVariable("SECTION", "help"); | |
75 | ||
76 | /* | |
77 | * Load the help index... | |
78 | */ | |
79 | ||
ecdc0628 | 80 | if ((cache_dir = getenv("CUPS_CACHEDIR")) == NULL) |
81 | cache_dir = CUPS_CACHEDIR; | |
ef416fc2 | 82 | |
ecdc0628 | 83 | snprintf(filename, sizeof(filename), "%s/help.index", cache_dir); |
ef416fc2 | 84 | |
85 | if ((docroot = getenv("CUPS_DOCROOT")) == NULL) | |
86 | docroot = CUPS_DOCROOT; | |
87 | ||
88 | snprintf(directory, sizeof(directory), "%s/help", docroot); | |
89 | ||
90 | fprintf(stderr, "DEBUG: helpLoadIndex(filename=\"%s\", directory=\"%s\")\n", | |
91 | filename, directory); | |
92 | ||
93 | hi = helpLoadIndex(filename, directory); | |
94 | if (!hi) | |
95 | { | |
96 | perror(filename); | |
97 | ||
4744bd90 | 98 | cgiStartHTML(cgiText(_("Help"))); |
ef416fc2 | 99 | cgiSetVariable("ERROR", "Unable to load help index!"); |
100 | cgiCopyTemplateLang("error.tmpl"); | |
101 | cgiEndHTML(); | |
102 | ||
103 | return (1); | |
104 | } | |
105 | ||
ecdc0628 | 106 | fprintf(stderr, "DEBUG: %d nodes in help index...\n", |
107 | cupsArrayCount(hi->nodes)); | |
ef416fc2 | 108 | |
109 | /* | |
110 | * See if we are viewing a file... | |
111 | */ | |
112 | ||
113 | for (i = 0; i < argc; i ++) | |
114 | fprintf(stderr, "argv[%d]=\"%s\"\n", i, argv[i]); | |
115 | ||
116 | if ((helpfile = getenv("PATH_INFO")) != NULL) | |
b423cd4c | 117 | { |
ef416fc2 | 118 | helpfile ++; |
b423cd4c | 119 | |
120 | if (!*helpfile) | |
121 | helpfile = NULL; | |
122 | } | |
ef416fc2 | 123 | |
124 | if (helpfile) | |
125 | { | |
126 | /* | |
127 | * Verify that the help file exists and is part of the index... | |
128 | */ | |
129 | ||
130 | snprintf(filename, sizeof(filename), "%s/help/%s", docroot, helpfile); | |
131 | ||
132 | fprintf(stderr, "DEBUG: helpfile=\"%s\", filename=\"%s\"\n", | |
133 | helpfile, filename); | |
134 | ||
135 | if (access(filename, R_OK)) | |
136 | { | |
137 | perror(filename); | |
138 | ||
4744bd90 | 139 | cgiStartHTML(cgiText(_("Help"))); |
ef416fc2 | 140 | cgiSetVariable("ERROR", "Unable to access help file!"); |
141 | cgiCopyTemplateLang("error.tmpl"); | |
142 | cgiEndHTML(); | |
143 | ||
144 | return (1); | |
145 | } | |
146 | ||
147 | if ((n = helpFindNode(hi, helpfile, NULL)) == NULL) | |
148 | { | |
4744bd90 | 149 | cgiStartHTML(cgiText(_("Help"))); |
ef416fc2 | 150 | cgiSetVariable("ERROR", "Help file not in index!"); |
151 | cgiCopyTemplateLang("error.tmpl"); | |
152 | cgiEndHTML(); | |
153 | ||
154 | return (1); | |
155 | } | |
156 | ||
157 | /* | |
158 | * Set the page title and save the help file... | |
159 | */ | |
160 | ||
161 | cgiSetVariable("HELPFILE", helpfile); | |
ecdc0628 | 162 | cgiSetVariable("HELPTITLE", n->text); |
ef416fc2 | 163 | |
164 | /* | |
165 | * Send a standard page header... | |
166 | */ | |
167 | ||
4744bd90 | 168 | if (printable) |
169 | puts("Content-Type: text/html;charset=utf-8\n"); | |
170 | else | |
171 | cgiStartHTML(n->text); | |
ef416fc2 | 172 | } |
173 | else | |
174 | { | |
175 | /* | |
176 | * Send a standard page header... | |
177 | */ | |
178 | ||
4744bd90 | 179 | cgiStartHTML(cgiText(_("Help"))); |
ef416fc2 | 180 | } |
181 | ||
182 | /* | |
183 | * Do a search as needed... | |
184 | */ | |
185 | ||
186 | query = cgiGetVariable("QUERY"); | |
187 | topic = cgiGetVariable("TOPIC"); | |
188 | si = helpSearchIndex(hi, query, topic, helpfile); | |
189 | ||
f301802f | 190 | fprintf(stderr, "DEBUG: query=\"%s\", topic=\"%s\"\n", |
191 | query ? query : "(null)", topic ? topic : "(null)"); | |
b423cd4c | 192 | |
ef416fc2 | 193 | if (si) |
194 | { | |
ecdc0628 | 195 | help_node_t *nn; /* Parent node */ |
ef416fc2 | 196 | |
197 | ||
ecdc0628 | 198 | fprintf(stderr, |
199 | "DEBUG: si=%p, si->sorted=%p, cupsArrayCount(si->sorted)=%d\n", si, | |
200 | si->sorted, cupsArrayCount(si->sorted)); | |
ef416fc2 | 201 | |
b423cd4c | 202 | for (i = 0, n = (help_node_t *)cupsArrayFirst(si->sorted); |
ecdc0628 | 203 | n; |
b423cd4c | 204 | i ++, n = (help_node_t *)cupsArrayNext(si->sorted)) |
ef416fc2 | 205 | { |
ecdc0628 | 206 | if (helpfile && n->anchor) |
207 | snprintf(line, sizeof(line), "#%s", n->anchor); | |
208 | else if (n->anchor) | |
209 | snprintf(line, sizeof(line), "/help/%s?QUERY=%s#%s", n->filename, | |
210 | query ? query : "", n->anchor); | |
ef416fc2 | 211 | else |
ecdc0628 | 212 | snprintf(line, sizeof(line), "/help/%s?QUERY=%s", n->filename, |
ef416fc2 | 213 | query ? query : ""); |
214 | ||
ecdc0628 | 215 | cgiSetArray("QTEXT", i, n->text); |
ef416fc2 | 216 | cgiSetArray("QLINK", i, line); |
217 | ||
ecdc0628 | 218 | if (!helpfile && n->anchor) |
ef416fc2 | 219 | { |
ecdc0628 | 220 | nn = helpFindNode(hi, n->filename, NULL); |
ef416fc2 | 221 | |
ecdc0628 | 222 | snprintf(line, sizeof(line), "/help/%s?QUERY=%s", nn->filename, |
ef416fc2 | 223 | query ? query : ""); |
224 | ||
ecdc0628 | 225 | cgiSetArray("QPTEXT", i, nn->text); |
ef416fc2 | 226 | cgiSetArray("QPLINK", i, line); |
227 | } | |
228 | else | |
229 | { | |
230 | cgiSetArray("QPTEXT", i, ""); | |
231 | cgiSetArray("QPLINK", i, ""); | |
232 | } | |
233 | ||
ecdc0628 | 234 | fprintf(stderr, "DEBUG: [%d] = \"%s\" @ \"%s\"\n", i, n->text, line); |
ef416fc2 | 235 | } |
236 | ||
237 | helpDeleteIndex(si); | |
238 | } | |
239 | ||
240 | /* | |
241 | * OK, now list the bookmarks within the index... | |
242 | */ | |
243 | ||
ecdc0628 | 244 | for (i = 0, section = NULL, n = (help_node_t *)cupsArrayFirst(hi->sorted); |
245 | n; | |
246 | n = (help_node_t *)cupsArrayNext(hi->sorted)) | |
ef416fc2 | 247 | { |
ecdc0628 | 248 | if (n->anchor) |
ef416fc2 | 249 | continue; |
250 | ||
251 | /* | |
252 | * Add a section link as needed... | |
253 | */ | |
254 | ||
ecdc0628 | 255 | if (n->section && |
256 | (!section || strcmp(n->section, section))) | |
ef416fc2 | 257 | { |
258 | /* | |
259 | * Add a link for this node... | |
260 | */ | |
261 | ||
262 | snprintf(line, sizeof(line), "/help/?TOPIC=%s&QUERY=%s", | |
ecdc0628 | 263 | cgiFormEncode(topic_data, n->section, sizeof(topic_data)), |
ef416fc2 | 264 | query ? query : ""); |
ecdc0628 | 265 | cgiSetArray("BMLINK", i, line); |
266 | cgiSetArray("BMTEXT", i, n->section); | |
267 | cgiSetArray("BMINDENT", i, "0"); | |
ef416fc2 | 268 | |
ecdc0628 | 269 | i ++; |
270 | section = n->section; | |
ef416fc2 | 271 | } |
272 | ||
ecdc0628 | 273 | if (!topic || strcmp(n->section, topic)) |
ef416fc2 | 274 | continue; |
275 | ||
276 | /* | |
277 | * Add a link for this node... | |
278 | */ | |
279 | ||
ecdc0628 | 280 | snprintf(line, sizeof(line), "/help/%s?TOPIC=%s&QUERY=%s", n->filename, |
281 | cgiFormEncode(topic_data, n->section, sizeof(topic_data)), | |
ef416fc2 | 282 | query ? query : ""); |
ecdc0628 | 283 | cgiSetArray("BMLINK", i, line); |
284 | cgiSetArray("BMTEXT", i, n->text); | |
285 | cgiSetArray("BMINDENT", i, "1"); | |
ef416fc2 | 286 | |
ecdc0628 | 287 | i ++; |
ef416fc2 | 288 | |
ecdc0628 | 289 | if (helpfile && !strcmp(helpfile, n->filename)) |
ef416fc2 | 290 | { |
ecdc0628 | 291 | help_node_t *nn; /* Pointer to sub-node */ |
ef416fc2 | 292 | |
293 | ||
ecdc0628 | 294 | cupsArraySave(hi->sorted); |
295 | ||
296 | for (nn = (help_node_t *)cupsArrayFirst(hi->sorted); | |
297 | nn; | |
298 | nn = (help_node_t *)cupsArrayNext(hi->sorted)) | |
299 | if (nn->anchor && !strcmp(helpfile, nn->filename)) | |
ef416fc2 | 300 | { |
301 | /* | |
302 | * Add a link for this node... | |
303 | */ | |
304 | ||
ecdc0628 | 305 | snprintf(line, sizeof(line), "#%s", nn->anchor); |
306 | cgiSetArray("BMLINK", i, line); | |
307 | cgiSetArray("BMTEXT", i, nn->text); | |
308 | cgiSetArray("BMINDENT", i, "2"); | |
ef416fc2 | 309 | |
ecdc0628 | 310 | i ++; |
ef416fc2 | 311 | } |
ecdc0628 | 312 | |
313 | cupsArrayRestore(hi->sorted); | |
ef416fc2 | 314 | } |
315 | } | |
316 | ||
317 | /* | |
318 | * Show the search and bookmark content... | |
319 | */ | |
320 | ||
4744bd90 | 321 | if (!helpfile || !printable) |
322 | cgiCopyTemplateLang("help-header.tmpl"); | |
323 | else | |
324 | cgiCopyTemplateLang("help-printable.tmpl"); | |
ef416fc2 | 325 | |
326 | /* | |
327 | * If we are viewing a file, copy it in now... | |
328 | */ | |
329 | ||
330 | if (helpfile) | |
331 | { | |
332 | if ((fp = cupsFileOpen(filename, "r")) != NULL) | |
333 | { | |
334 | int inbody; /* Are we inside the body? */ | |
335 | ||
336 | ||
337 | inbody = 0; | |
338 | ||
339 | while (cupsFileGets(fp, line, sizeof(line))) | |
340 | { | |
341 | if (inbody) | |
342 | { | |
343 | if (!strncasecmp(line, "</BODY>", 7)) | |
344 | break; | |
345 | ||
346 | printf("%s\n", line); | |
347 | } | |
348 | else if (!strncasecmp(line, "<BODY", 5)) | |
349 | inbody = 1; | |
350 | } | |
351 | ||
352 | cupsFileClose(fp); | |
353 | } | |
354 | else | |
355 | { | |
356 | perror(filename); | |
357 | cgiSetVariable("ERROR", "Unable to open help file."); | |
358 | cgiCopyTemplateLang("error.tmpl"); | |
359 | } | |
360 | } | |
361 | ||
362 | /* | |
363 | * Send a standard trailer... | |
364 | */ | |
365 | ||
4744bd90 | 366 | if (!printable) |
367 | cgiEndHTML(); | |
368 | else | |
369 | puts("</BODY>\n</HTML>"); | |
ef416fc2 | 370 | |
371 | /* | |
372 | * Delete the index... | |
373 | */ | |
374 | ||
375 | helpDeleteIndex(hi); | |
376 | ||
377 | /* | |
378 | * Return with no errors... | |
379 | */ | |
380 | ||
381 | return (0); | |
382 | } | |
383 | ||
384 | ||
385 | /* | |
c07d5b2d | 386 | * End of "$Id: help.c 177 2006-06-21 00:20:03Z jlovell $". |
ef416fc2 | 387 | */ |