]> git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/help.c
Merge changes from CUPS 1.3.1.
[thirdparty/cups.git] / cgi-bin / help.c
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 cgiSetVariable("TOPIC", n->section);
155
156 /*
157 * Send a standard page header...
158 */
159
160 if (printable)
161 puts("Content-Type: text/html;charset=utf-8\n");
162 else
163 cgiStartHTML(n->text);
164 }
165 else
166 {
167 /*
168 * Send a standard page header...
169 */
170
171 cgiStartHTML(cgiText(_("Help")));
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
182 fprintf(stderr, "DEBUG: query=\"%s\", topic=\"%s\"\n",
183 query ? query : "(null)", topic ? topic : "(null)");
184
185 if (si)
186 {
187 help_node_t *nn; /* Parent node */
188
189
190 fprintf(stderr,
191 "DEBUG: si=%p, si->sorted=%p, cupsArrayCount(si->sorted)=%d\n", si,
192 si->sorted, cupsArrayCount(si->sorted));
193
194 for (i = 0, n = (help_node_t *)cupsArrayFirst(si->sorted);
195 n;
196 i ++, n = (help_node_t *)cupsArrayNext(si->sorted))
197 {
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);
203 else
204 snprintf(line, sizeof(line), "/help/%s?QUERY=%s", n->filename,
205 query ? query : "");
206
207 cgiSetArray("QTEXT", i, n->text);
208 cgiSetArray("QLINK", i, line);
209
210 if (!helpfile && n->anchor)
211 {
212 nn = helpFindNode(hi, n->filename, NULL);
213
214 snprintf(line, sizeof(line), "/help/%s?QUERY=%s", nn->filename,
215 query ? query : "");
216
217 cgiSetArray("QPTEXT", i, nn->text);
218 cgiSetArray("QPLINK", i, line);
219 }
220 else
221 {
222 cgiSetArray("QPTEXT", i, "");
223 cgiSetArray("QPLINK", i, "");
224 }
225
226 fprintf(stderr, "DEBUG: [%d] = \"%s\" @ \"%s\"\n", i, n->text, line);
227 }
228
229 helpDeleteIndex(si);
230 }
231
232 /*
233 * OK, now list the bookmarks within the index...
234 */
235
236 for (i = 0, section = NULL, n = (help_node_t *)cupsArrayFirst(hi->sorted);
237 n;
238 n = (help_node_t *)cupsArrayNext(hi->sorted))
239 {
240 if (n->anchor)
241 continue;
242
243 /*
244 * Add a section link as needed...
245 */
246
247 if (n->section &&
248 (!section || strcmp(n->section, section)))
249 {
250 /*
251 * Add a link for this node...
252 */
253
254 snprintf(line, sizeof(line), "/help/?TOPIC=%s&QUERY=%s",
255 cgiFormEncode(topic_data, n->section, sizeof(topic_data)),
256 query ? query : "");
257 cgiSetArray("BMLINK", i, line);
258 cgiSetArray("BMTEXT", i, n->section);
259 cgiSetArray("BMINDENT", i, "0");
260
261 i ++;
262 section = n->section;
263 }
264
265 if (!topic || strcmp(n->section, topic))
266 continue;
267
268 /*
269 * Add a link for this node...
270 */
271
272 snprintf(line, sizeof(line), "/help/%s?TOPIC=%s&QUERY=%s", n->filename,
273 cgiFormEncode(topic_data, n->section, sizeof(topic_data)),
274 query ? query : "");
275 cgiSetArray("BMLINK", i, line);
276 cgiSetArray("BMTEXT", i, n->text);
277 cgiSetArray("BMINDENT", i, "1");
278
279 i ++;
280
281 if (helpfile && !strcmp(helpfile, n->filename))
282 {
283 help_node_t *nn; /* Pointer to sub-node */
284
285
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))
292 {
293 /*
294 * Add a link for this node...
295 */
296
297 snprintf(line, sizeof(line), "#%s", nn->anchor);
298 cgiSetArray("BMLINK", i, line);
299 cgiSetArray("BMTEXT", i, nn->text);
300 cgiSetArray("BMINDENT", i, "2");
301
302 i ++;
303 }
304
305 cupsArrayRestore(hi->sorted);
306 }
307 }
308
309 /*
310 * Show the search and bookmark content...
311 */
312
313 if (!helpfile || !printable)
314 cgiCopyTemplateLang("help-header.tmpl");
315 else
316 cgiCopyTemplateLang("help-printable.tmpl");
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
358 if (!printable)
359 cgiEndHTML();
360 else
361 puts("</BODY>\n</HTML>");
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 /*
378 * End of "$Id$".
379 */