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