]> git.ipfire.org Git - thirdparty/cups.git/blame - cgi-bin/help.c
Merge changes from CUPS 1.4svn-r7961.
[thirdparty/cups.git] / cgi-bin / help.c
CommitLineData
ef416fc2 1/*
f7faf1f5 2 * "$Id$"
ef416fc2 3 *
2e4ff8af 4 * Online help CGI for the Common UNIX Printing System (CUPS).
ef416fc2 5 *
b19ccc9e 6 * Copyright 2007-2008 by Apple Inc.
ef416fc2 7 * Copyright 1997-2006 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 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/".
ef416fc2 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
31int /* O - Exit status */
32main(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 */
ecdc0628 37 help_node_t *n; /* Current help node */
38 int i; /* Looping var */
ef416fc2 39 const char *query; /* Search query */
ecdc0628 40 const char *cache_dir; /* CUPS_CACHEDIR environment variable */
ef416fc2 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 */
4744bd90 50 int printable; /* Show printable version? */
ef416fc2 51
52
53 /*
54 * Get any form variables...
55 */
56
57 cgiInitialize();
58
4744bd90 59 printable = cgiGetVariable("PRINTABLE") != NULL;
60
ef416fc2 61 /*
62 * Set the web interface section...
63 */
64
65 cgiSetVariable("SECTION", "help");
66
67 /*
68 * Load the help index...
69 */
70
ecdc0628 71 if ((cache_dir = getenv("CUPS_CACHEDIR")) == NULL)
72 cache_dir = CUPS_CACHEDIR;
ef416fc2 73
ecdc0628 74 snprintf(filename, sizeof(filename), "%s/help.index", cache_dir);
ef416fc2 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
2e4ff8af 89 cgiStartHTML(cgiText(_("Online Help")));
ef416fc2 90 cgiSetVariable("ERROR", "Unable to load help index!");
91 cgiCopyTemplateLang("error.tmpl");
92 cgiEndHTML();
93
94 return (1);
95 }
96
ecdc0628 97 fprintf(stderr, "DEBUG: %d nodes in help index...\n",
98 cupsArrayCount(hi->nodes));
ef416fc2 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)
b423cd4c 108 {
ef416fc2 109 helpfile ++;
b423cd4c 110
111 if (!*helpfile)
112 helpfile = NULL;
113 }
ef416fc2 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
2e4ff8af 130 cgiStartHTML(cgiText(_("Online Help")));
ef416fc2 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 {
2e4ff8af 140 cgiStartHTML(cgiText(_("Online Help")));
ef416fc2 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);
ecdc0628 153 cgiSetVariable("HELPTITLE", n->text);
db1f069b 154 cgiSetVariable("TOPIC", n->section);
ef416fc2 155
156 /*
157 * Send a standard page header...
158 */
159
4744bd90 160 if (printable)
161 puts("Content-Type: text/html;charset=utf-8\n");
162 else
163 cgiStartHTML(n->text);
ef416fc2 164 }
165 else
166 {
167 /*
168 * Send a standard page header...
169 */
170
2e4ff8af 171 cgiStartHTML(cgiText(_("Online Help")));
ef416fc2 172 }
173
174 /*
175 * Do a search as needed...
176 */
177
2e4ff8af
MS
178 if (cgiGetVariable("CLEAR"))
179 cgiSetVariable("QUERY", "");
180
ef416fc2 181 query = cgiGetVariable("QUERY");
182 topic = cgiGetVariable("TOPIC");
183 si = helpSearchIndex(hi, query, topic, helpfile);
184
f301802f 185 fprintf(stderr, "DEBUG: query=\"%s\", topic=\"%s\"\n",
186 query ? query : "(null)", topic ? topic : "(null)");
b423cd4c 187
ef416fc2 188 if (si)
189 {
ecdc0628 190 help_node_t *nn; /* Parent node */
ef416fc2 191
192
ecdc0628 193 fprintf(stderr,
194 "DEBUG: si=%p, si->sorted=%p, cupsArrayCount(si->sorted)=%d\n", si,
195 si->sorted, cupsArrayCount(si->sorted));
ef416fc2 196
b423cd4c 197 for (i = 0, n = (help_node_t *)cupsArrayFirst(si->sorted);
ecdc0628 198 n;
b423cd4c 199 i ++, n = (help_node_t *)cupsArrayNext(si->sorted))
ef416fc2 200 {
ecdc0628 201 if (helpfile && n->anchor)
202 snprintf(line, sizeof(line), "#%s", n->anchor);
203 else if (n->anchor)
204 snprintf(line, sizeof(line), "/help/%s?QUERY=%s#%s", n->filename,
205 query ? query : "", n->anchor);
ef416fc2 206 else
ecdc0628 207 snprintf(line, sizeof(line), "/help/%s?QUERY=%s", n->filename,
ef416fc2 208 query ? query : "");
209
ecdc0628 210 cgiSetArray("QTEXT", i, n->text);
ef416fc2 211 cgiSetArray("QLINK", i, line);
212
ecdc0628 213 if (!helpfile && n->anchor)
ef416fc2 214 {
ecdc0628 215 nn = helpFindNode(hi, n->filename, NULL);
ef416fc2 216
ecdc0628 217 snprintf(line, sizeof(line), "/help/%s?QUERY=%s", nn->filename,
ef416fc2 218 query ? query : "");
219
ecdc0628 220 cgiSetArray("QPTEXT", i, nn->text);
ef416fc2 221 cgiSetArray("QPLINK", i, line);
222 }
223 else
224 {
225 cgiSetArray("QPTEXT", i, "");
226 cgiSetArray("QPLINK", i, "");
227 }
228
ecdc0628 229 fprintf(stderr, "DEBUG: [%d] = \"%s\" @ \"%s\"\n", i, n->text, line);
ef416fc2 230 }
231
232 helpDeleteIndex(si);
233 }
234
235 /*
236 * OK, now list the bookmarks within the index...
237 */
238
ecdc0628 239 for (i = 0, section = NULL, n = (help_node_t *)cupsArrayFirst(hi->sorted);
240 n;
241 n = (help_node_t *)cupsArrayNext(hi->sorted))
ef416fc2 242 {
ecdc0628 243 if (n->anchor)
ef416fc2 244 continue;
245
246 /*
247 * Add a section link as needed...
248 */
249
ecdc0628 250 if (n->section &&
251 (!section || strcmp(n->section, section)))
ef416fc2 252 {
253 /*
254 * Add a link for this node...
255 */
256
257 snprintf(line, sizeof(line), "/help/?TOPIC=%s&QUERY=%s",
ecdc0628 258 cgiFormEncode(topic_data, n->section, sizeof(topic_data)),
ef416fc2 259 query ? query : "");
ecdc0628 260 cgiSetArray("BMLINK", i, line);
261 cgiSetArray("BMTEXT", i, n->section);
262 cgiSetArray("BMINDENT", i, "0");
ef416fc2 263
ecdc0628 264 i ++;
265 section = n->section;
ef416fc2 266 }
267
ecdc0628 268 if (!topic || strcmp(n->section, topic))
ef416fc2 269 continue;
270
271 /*
272 * Add a link for this node...
273 */
274
ecdc0628 275 snprintf(line, sizeof(line), "/help/%s?TOPIC=%s&QUERY=%s", n->filename,
276 cgiFormEncode(topic_data, n->section, sizeof(topic_data)),
ef416fc2 277 query ? query : "");
ecdc0628 278 cgiSetArray("BMLINK", i, line);
279 cgiSetArray("BMTEXT", i, n->text);
280 cgiSetArray("BMINDENT", i, "1");
ef416fc2 281
ecdc0628 282 i ++;
ef416fc2 283
ecdc0628 284 if (helpfile && !strcmp(helpfile, n->filename))
ef416fc2 285 {
ecdc0628 286 help_node_t *nn; /* Pointer to sub-node */
ef416fc2 287
288
ecdc0628 289 cupsArraySave(hi->sorted);
290
291 for (nn = (help_node_t *)cupsArrayFirst(hi->sorted);
292 nn;
293 nn = (help_node_t *)cupsArrayNext(hi->sorted))
294 if (nn->anchor && !strcmp(helpfile, nn->filename))
ef416fc2 295 {
296 /*
297 * Add a link for this node...
298 */
299
ecdc0628 300 snprintf(line, sizeof(line), "#%s", nn->anchor);
301 cgiSetArray("BMLINK", i, line);
302 cgiSetArray("BMTEXT", i, nn->text);
303 cgiSetArray("BMINDENT", i, "2");
ef416fc2 304
ecdc0628 305 i ++;
ef416fc2 306 }
ecdc0628 307
308 cupsArrayRestore(hi->sorted);
ef416fc2 309 }
310 }
311
312 /*
313 * Show the search and bookmark content...
314 */
315
4744bd90 316 if (!helpfile || !printable)
317 cgiCopyTemplateLang("help-header.tmpl");
318 else
319 cgiCopyTemplateLang("help-printable.tmpl");
ef416fc2 320
321 /*
322 * If we are viewing a file, copy it in now...
323 */
324
325 if (helpfile)
326 {
327 if ((fp = cupsFileOpen(filename, "r")) != NULL)
328 {
329 int inbody; /* Are we inside the body? */
330
331
332 inbody = 0;
333
334 while (cupsFileGets(fp, line, sizeof(line)))
335 {
336 if (inbody)
337 {
338 if (!strncasecmp(line, "</BODY>", 7))
339 break;
340
341 printf("%s\n", line);
342 }
343 else if (!strncasecmp(line, "<BODY", 5))
344 inbody = 1;
345 }
346
347 cupsFileClose(fp);
348 }
349 else
350 {
351 perror(filename);
352 cgiSetVariable("ERROR", "Unable to open help file.");
353 cgiCopyTemplateLang("error.tmpl");
354 }
355 }
356
357 /*
358 * Send a standard trailer...
359 */
360
4744bd90 361 if (!printable)
b19ccc9e
MS
362 {
363 cgiCopyTemplateLang("help-trailer.tmpl");
4744bd90 364 cgiEndHTML();
b19ccc9e 365 }
4744bd90 366 else
367 puts("</BODY>\n</HTML>");
ef416fc2 368
369 /*
370 * Delete the index...
371 */
372
373 helpDeleteIndex(hi);
374
375 /*
376 * Return with no errors...
377 */
378
379 return (0);
380}
381
382
383/*
f7faf1f5 384 * End of "$Id$".
ef416fc2 385 */