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