]> git.ipfire.org Git - thirdparty/cups.git/blame - cgi-bin/help.c
Merge changes from CUPS 1.5svn-r8916.
[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 *
ef55b745 6 * Copyright 2007-2009 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");
ef55b745 66 cgiSetVariable("REFRESH_PAGE", "");
ef416fc2 67
68 /*
69 * Load the help index...
70 */
71
ecdc0628 72 if ((cache_dir = getenv("CUPS_CACHEDIR")) == NULL)
73 cache_dir = CUPS_CACHEDIR;
ef416fc2 74
ecdc0628 75 snprintf(filename, sizeof(filename), "%s/help.index", cache_dir);
ef416fc2 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
2e4ff8af 90 cgiStartHTML(cgiText(_("Online Help")));
ef416fc2 91 cgiSetVariable("ERROR", "Unable to load help index!");
92 cgiCopyTemplateLang("error.tmpl");
93 cgiEndHTML();
94
95 return (1);
96 }
97
ecdc0628 98 fprintf(stderr, "DEBUG: %d nodes in help index...\n",
99 cupsArrayCount(hi->nodes));
ef416fc2 100
101 /*
102 * See if we are viewing a file...
103 */
104
105 for (i = 0; i < argc; i ++)
ef55b745 106 fprintf(stderr, "DEBUG: argv[%d]=\"%s\"\n", i, argv[i]);
ef416fc2 107
108 if ((helpfile = getenv("PATH_INFO")) != NULL)
b423cd4c 109 {
ef416fc2 110 helpfile ++;
b423cd4c 111
112 if (!*helpfile)
113 helpfile = NULL;
114 }
ef416fc2 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
2e4ff8af 131 cgiStartHTML(cgiText(_("Online Help")));
ef416fc2 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 {
2e4ff8af 141 cgiStartHTML(cgiText(_("Online Help")));
ef416fc2 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);
ecdc0628 154 cgiSetVariable("HELPTITLE", n->text);
db1f069b 155 cgiSetVariable("TOPIC", n->section);
ef416fc2 156
157 /*
158 * Send a standard page header...
159 */
160
4744bd90 161 if (printable)
162 puts("Content-Type: text/html;charset=utf-8\n");
163 else
164 cgiStartHTML(n->text);
ef416fc2 165 }
166 else
167 {
168 /*
169 * Send a standard page header...
170 */
171
2e4ff8af 172 cgiStartHTML(cgiText(_("Online Help")));
ef416fc2 173 }
174
175 /*
176 * Do a search as needed...
177 */
178
2e4ff8af
MS
179 if (cgiGetVariable("CLEAR"))
180 cgiSetVariable("QUERY", "");
181
ef416fc2 182 query = cgiGetVariable("QUERY");
183 topic = cgiGetVariable("TOPIC");
184 si = helpSearchIndex(hi, query, topic, helpfile);
185
ef55b745
MS
186 cgiClearVariables();
187 if (query)
188 cgiSetVariable("QUERY", query);
189 if (topic)
190 cgiSetVariable("TOPIC", topic);
191
f301802f 192 fprintf(stderr, "DEBUG: query=\"%s\", topic=\"%s\"\n",
193 query ? query : "(null)", topic ? topic : "(null)");
b423cd4c 194
ef416fc2 195 if (si)
196 {
ecdc0628 197 help_node_t *nn; /* Parent node */
ef416fc2 198
199
ecdc0628 200 fprintf(stderr,
201 "DEBUG: si=%p, si->sorted=%p, cupsArrayCount(si->sorted)=%d\n", si,
202 si->sorted, cupsArrayCount(si->sorted));
ef416fc2 203
b423cd4c 204 for (i = 0, n = (help_node_t *)cupsArrayFirst(si->sorted);
ecdc0628 205 n;
b423cd4c 206 i ++, n = (help_node_t *)cupsArrayNext(si->sorted))
ef416fc2 207 {
ecdc0628 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);
ef416fc2 213 else
ecdc0628 214 snprintf(line, sizeof(line), "/help/%s?QUERY=%s", n->filename,
ef416fc2 215 query ? query : "");
216
ecdc0628 217 cgiSetArray("QTEXT", i, n->text);
ef416fc2 218 cgiSetArray("QLINK", i, line);
219
ecdc0628 220 if (!helpfile && n->anchor)
ef416fc2 221 {
ecdc0628 222 nn = helpFindNode(hi, n->filename, NULL);
ef416fc2 223
ecdc0628 224 snprintf(line, sizeof(line), "/help/%s?QUERY=%s", nn->filename,
ef416fc2 225 query ? query : "");
226
ecdc0628 227 cgiSetArray("QPTEXT", i, nn->text);
ef416fc2 228 cgiSetArray("QPLINK", i, line);
229 }
230 else
231 {
232 cgiSetArray("QPTEXT", i, "");
233 cgiSetArray("QPLINK", i, "");
234 }
235
ecdc0628 236 fprintf(stderr, "DEBUG: [%d] = \"%s\" @ \"%s\"\n", i, n->text, line);
ef416fc2 237 }
238
239 helpDeleteIndex(si);
240 }
241
242 /*
243 * OK, now list the bookmarks within the index...
244 */
245
ecdc0628 246 for (i = 0, section = NULL, n = (help_node_t *)cupsArrayFirst(hi->sorted);
247 n;
248 n = (help_node_t *)cupsArrayNext(hi->sorted))
ef416fc2 249 {
ecdc0628 250 if (n->anchor)
ef416fc2 251 continue;
252
253 /*
254 * Add a section link as needed...
255 */
256
ecdc0628 257 if (n->section &&
258 (!section || strcmp(n->section, section)))
ef416fc2 259 {
260 /*
261 * Add a link for this node...
262 */
263
264 snprintf(line, sizeof(line), "/help/?TOPIC=%s&QUERY=%s",
ecdc0628 265 cgiFormEncode(topic_data, n->section, sizeof(topic_data)),
ef416fc2 266 query ? query : "");
ecdc0628 267 cgiSetArray("BMLINK", i, line);
268 cgiSetArray("BMTEXT", i, n->section);
269 cgiSetArray("BMINDENT", i, "0");
ef416fc2 270
ecdc0628 271 i ++;
272 section = n->section;
ef416fc2 273 }
274
ecdc0628 275 if (!topic || strcmp(n->section, topic))
ef416fc2 276 continue;
277
278 /*
279 * Add a link for this node...
280 */
281
ecdc0628 282 snprintf(line, sizeof(line), "/help/%s?TOPIC=%s&QUERY=%s", n->filename,
283 cgiFormEncode(topic_data, n->section, sizeof(topic_data)),
ef416fc2 284 query ? query : "");
ecdc0628 285 cgiSetArray("BMLINK", i, line);
286 cgiSetArray("BMTEXT", i, n->text);
287 cgiSetArray("BMINDENT", i, "1");
ef416fc2 288
ecdc0628 289 i ++;
ef416fc2 290
ecdc0628 291 if (helpfile && !strcmp(helpfile, n->filename))
ef416fc2 292 {
ecdc0628 293 help_node_t *nn; /* Pointer to sub-node */
ef416fc2 294
295
ecdc0628 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))
ef416fc2 302 {
303 /*
304 * Add a link for this node...
305 */
306
ecdc0628 307 snprintf(line, sizeof(line), "#%s", nn->anchor);
308 cgiSetArray("BMLINK", i, line);
309 cgiSetArray("BMTEXT", i, nn->text);
310 cgiSetArray("BMINDENT", i, "2");
ef416fc2 311
ecdc0628 312 i ++;
ef416fc2 313 }
ecdc0628 314
315 cupsArrayRestore(hi->sorted);
ef416fc2 316 }
317 }
318
319 /*
320 * Show the search and bookmark content...
321 */
322
4744bd90 323 if (!helpfile || !printable)
324 cgiCopyTemplateLang("help-header.tmpl");
325 else
326 cgiCopyTemplateLang("help-printable.tmpl");
ef416fc2 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
4744bd90 368 if (!printable)
b19ccc9e
MS
369 {
370 cgiCopyTemplateLang("help-trailer.tmpl");
4744bd90 371 cgiEndHTML();
b19ccc9e 372 }
4744bd90 373 else
374 puts("</BODY>\n</HTML>");
ef416fc2 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/*
f7faf1f5 391 * End of "$Id$".
ef416fc2 392 */