]> git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/help.c
Load cups into easysw/current.
[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 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 */
46 help_node_t *n; /* Current help node */
47 int i; /* Looping var */
48 const char *query; /* Search query */
49 const char *cache_dir; /* CUPS_CACHEDIR environment variable */
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 */
59 int printable; /* Show printable version? */
60
61
62 /*
63 * Get any form variables...
64 */
65
66 cgiInitialize();
67
68 printable = cgiGetVariable("PRINTABLE") != NULL;
69
70 /*
71 * Set the web interface section...
72 */
73
74 cgiSetVariable("SECTION", "help");
75
76 /*
77 * Load the help index...
78 */
79
80 if ((cache_dir = getenv("CUPS_CACHEDIR")) == NULL)
81 cache_dir = CUPS_CACHEDIR;
82
83 snprintf(filename, sizeof(filename), "%s/help.index", cache_dir);
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
98 cgiStartHTML(cgiText(_("Help")));
99 cgiSetVariable("ERROR", "Unable to load help index!");
100 cgiCopyTemplateLang("error.tmpl");
101 cgiEndHTML();
102
103 return (1);
104 }
105
106 fprintf(stderr, "DEBUG: %d nodes in help index...\n",
107 cupsArrayCount(hi->nodes));
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)
117 {
118 helpfile ++;
119
120 if (!*helpfile)
121 helpfile = NULL;
122 }
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
139 cgiStartHTML(cgiText(_("Help")));
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 {
149 cgiStartHTML(cgiText(_("Help")));
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);
162 cgiSetVariable("HELPTITLE", n->text);
163
164 /*
165 * Send a standard page header...
166 */
167
168 if (printable)
169 puts("Content-Type: text/html;charset=utf-8\n");
170 else
171 cgiStartHTML(n->text);
172 }
173 else
174 {
175 /*
176 * Send a standard page header...
177 */
178
179 cgiStartHTML(cgiText(_("Help")));
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
190 fprintf(stderr, "DEBUG: query=\"%s\", topic=\"%s\"\n",
191 query ? query : "(null)", topic ? topic : "(null)");
192
193 if (si)
194 {
195 help_node_t *nn; /* Parent node */
196
197
198 fprintf(stderr,
199 "DEBUG: si=%p, si->sorted=%p, cupsArrayCount(si->sorted)=%d\n", si,
200 si->sorted, cupsArrayCount(si->sorted));
201
202 for (i = 0, n = (help_node_t *)cupsArrayFirst(si->sorted);
203 n;
204 i ++, n = (help_node_t *)cupsArrayNext(si->sorted))
205 {
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);
211 else
212 snprintf(line, sizeof(line), "/help/%s?QUERY=%s", n->filename,
213 query ? query : "");
214
215 cgiSetArray("QTEXT", i, n->text);
216 cgiSetArray("QLINK", i, line);
217
218 if (!helpfile && n->anchor)
219 {
220 nn = helpFindNode(hi, n->filename, NULL);
221
222 snprintf(line, sizeof(line), "/help/%s?QUERY=%s", nn->filename,
223 query ? query : "");
224
225 cgiSetArray("QPTEXT", i, nn->text);
226 cgiSetArray("QPLINK", i, line);
227 }
228 else
229 {
230 cgiSetArray("QPTEXT", i, "");
231 cgiSetArray("QPLINK", i, "");
232 }
233
234 fprintf(stderr, "DEBUG: [%d] = \"%s\" @ \"%s\"\n", i, n->text, line);
235 }
236
237 helpDeleteIndex(si);
238 }
239
240 /*
241 * OK, now list the bookmarks within the index...
242 */
243
244 for (i = 0, section = NULL, n = (help_node_t *)cupsArrayFirst(hi->sorted);
245 n;
246 n = (help_node_t *)cupsArrayNext(hi->sorted))
247 {
248 if (n->anchor)
249 continue;
250
251 /*
252 * Add a section link as needed...
253 */
254
255 if (n->section &&
256 (!section || strcmp(n->section, section)))
257 {
258 /*
259 * Add a link for this node...
260 */
261
262 snprintf(line, sizeof(line), "/help/?TOPIC=%s&QUERY=%s",
263 cgiFormEncode(topic_data, n->section, sizeof(topic_data)),
264 query ? query : "");
265 cgiSetArray("BMLINK", i, line);
266 cgiSetArray("BMTEXT", i, n->section);
267 cgiSetArray("BMINDENT", i, "0");
268
269 i ++;
270 section = n->section;
271 }
272
273 if (!topic || strcmp(n->section, topic))
274 continue;
275
276 /*
277 * Add a link for this node...
278 */
279
280 snprintf(line, sizeof(line), "/help/%s?TOPIC=%s&QUERY=%s", n->filename,
281 cgiFormEncode(topic_data, n->section, sizeof(topic_data)),
282 query ? query : "");
283 cgiSetArray("BMLINK", i, line);
284 cgiSetArray("BMTEXT", i, n->text);
285 cgiSetArray("BMINDENT", i, "1");
286
287 i ++;
288
289 if (helpfile && !strcmp(helpfile, n->filename))
290 {
291 help_node_t *nn; /* Pointer to sub-node */
292
293
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))
300 {
301 /*
302 * Add a link for this node...
303 */
304
305 snprintf(line, sizeof(line), "#%s", nn->anchor);
306 cgiSetArray("BMLINK", i, line);
307 cgiSetArray("BMTEXT", i, nn->text);
308 cgiSetArray("BMINDENT", i, "2");
309
310 i ++;
311 }
312
313 cupsArrayRestore(hi->sorted);
314 }
315 }
316
317 /*
318 * Show the search and bookmark content...
319 */
320
321 if (!helpfile || !printable)
322 cgiCopyTemplateLang("help-header.tmpl");
323 else
324 cgiCopyTemplateLang("help-printable.tmpl");
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
366 if (!printable)
367 cgiEndHTML();
368 else
369 puts("</BODY>\n</HTML>");
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 /*
386 * End of "$Id$".
387 */