4 * Online help CGI for CUPS.
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
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/".
17 * main() - Main entry for CGI.
21 * Include necessary headers...
24 #include "cgi-private.h"
28 * 'main()' - Main entry for CGI.
31 int /* O - Exit status */
32 main(int argc, /* I - Number of command-line arguments */
33 char *argv[]) /* I - Command-line arguments */
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 *helptitle = NULL; /* Current help title */
44 const char *topic; /* Current topic */
45 char topic_data[1024]; /* Topic form data */
46 const char *section; /* Current section */
47 char filename[1024], /* Filename */
48 directory[1024]; /* Directory */
49 cups_file_t *fp; /* Help file */
50 char line[1024]; /* Line from file */
51 int printable; /* Show printable version? */
55 * Get any form variables...
60 printable = cgiGetVariable("PRINTABLE") != NULL;
63 * Set the web interface section...
66 cgiSetVariable("SECTION", "help");
67 cgiSetVariable("REFRESH_PAGE", "");
70 * Load the help index...
73 if ((cache_dir = getenv("CUPS_CACHEDIR")) == NULL)
74 cache_dir = CUPS_CACHEDIR;
76 snprintf(filename, sizeof(filename), "%s/help.index", cache_dir);
78 if ((docroot = getenv("CUPS_DOCROOT")) == NULL)
79 docroot = CUPS_DOCROOT;
81 snprintf(directory, sizeof(directory), "%s/help", docroot);
83 fprintf(stderr, "DEBUG: helpLoadIndex(filename=\"%s\", directory=\"%s\")\n",
86 hi = helpLoadIndex(filename, directory);
91 cgiStartHTML(cgiText(_("Online Help")));
92 cgiSetVariable("ERROR", cgiText(_("Unable to load help index.")));
93 cgiCopyTemplateLang("error.tmpl");
99 fprintf(stderr, "DEBUG: %d nodes in help index...\n",
100 cupsArrayCount(hi->nodes));
103 * See if we are viewing a file...
106 for (i = 0; i < argc; i ++)
107 fprintf(stderr, "DEBUG: argv[%d]=\"%s\"\n", i, argv[i]);
109 if ((helpfile = getenv("PATH_INFO")) != NULL)
120 * Verify that the help file exists and is part of the index...
123 snprintf(filename, sizeof(filename), "%s/help/%s", docroot, helpfile);
125 fprintf(stderr, "DEBUG: helpfile=\"%s\", filename=\"%s\"\n",
128 if (access(filename, R_OK))
132 cgiStartHTML(cgiText(_("Online Help")));
133 cgiSetVariable("ERROR", cgiText(_("Unable to access help file.")));
134 cgiCopyTemplateLang("error.tmpl");
140 if ((n = helpFindNode(hi, helpfile, NULL)) == NULL)
142 cgiStartHTML(cgiText(_("Online Help")));
143 cgiSetVariable("ERROR", cgiText(_("Help file not in index.")));
144 cgiCopyTemplateLang("error.tmpl");
151 * Save the page title and help file...
158 * Send a standard page header...
162 puts("Content-Type: text/html;charset=utf-8\n");
164 cgiStartHTML(n->text);
169 * Send a standard page header...
172 cgiStartHTML(cgiText(_("Online Help")));
174 topic = cgiGetVariable("TOPIC");
178 * Do a search as needed...
181 if (cgiGetVariable("CLEAR"))
182 cgiSetVariable("QUERY", "");
184 query = cgiGetVariable("QUERY");
185 si = helpSearchIndex(hi, query, topic, helpfile);
189 cgiSetVariable("QUERY", query);
191 cgiSetVariable("TOPIC", topic);
193 cgiSetVariable("HELPFILE", helpfile);
195 cgiSetVariable("HELPTITLE", helptitle);
197 fprintf(stderr, "DEBUG: query=\"%s\", topic=\"%s\"\n",
198 query ? query : "(null)", topic ? topic : "(null)");
202 help_node_t *nn; /* Parent node */
206 "DEBUG: si=%p, si->sorted=%p, cupsArrayCount(si->sorted)=%d\n", si,
207 si->sorted, cupsArrayCount(si->sorted));
209 for (i = 0, n = (help_node_t *)cupsArrayFirst(si->sorted);
211 i ++, n = (help_node_t *)cupsArrayNext(si->sorted))
213 if (helpfile && n->anchor)
214 snprintf(line, sizeof(line), "#%s", n->anchor);
216 snprintf(line, sizeof(line), "/help/%s?QUERY=%s#%s", n->filename,
217 query ? query : "", n->anchor);
219 snprintf(line, sizeof(line), "/help/%s?QUERY=%s", n->filename,
222 cgiSetArray("QTEXT", i, n->text);
223 cgiSetArray("QLINK", i, line);
225 if (!helpfile && n->anchor)
227 nn = helpFindNode(hi, n->filename, NULL);
229 snprintf(line, sizeof(line), "/help/%s?QUERY=%s", nn->filename,
232 cgiSetArray("QPTEXT", i, nn->text);
233 cgiSetArray("QPLINK", i, line);
237 cgiSetArray("QPTEXT", i, "");
238 cgiSetArray("QPLINK", i, "");
241 fprintf(stderr, "DEBUG: [%d] = \"%s\" @ \"%s\"\n", i, n->text, line);
248 * OK, now list the bookmarks within the index...
251 for (i = 0, section = NULL, n = (help_node_t *)cupsArrayFirst(hi->sorted);
253 n = (help_node_t *)cupsArrayNext(hi->sorted))
259 * Add a section link as needed...
263 (!section || strcmp(n->section, section)))
266 * Add a link for this node...
269 snprintf(line, sizeof(line), "/help/?TOPIC=%s&QUERY=%s",
270 cgiFormEncode(topic_data, n->section, sizeof(topic_data)),
272 cgiSetArray("BMLINK", i, line);
273 cgiSetArray("BMTEXT", i, n->section);
274 cgiSetArray("BMINDENT", i, "0");
277 section = n->section;
280 if (!topic || strcmp(n->section, topic))
284 * Add a link for this node...
287 snprintf(line, sizeof(line), "/help/%s?TOPIC=%s&QUERY=%s", n->filename,
288 cgiFormEncode(topic_data, n->section, sizeof(topic_data)),
290 cgiSetArray("BMLINK", i, line);
291 cgiSetArray("BMTEXT", i, n->text);
292 cgiSetArray("BMINDENT", i, "1");
296 if (helpfile && !strcmp(helpfile, n->filename))
298 help_node_t *nn; /* Pointer to sub-node */
301 cupsArraySave(hi->sorted);
303 for (nn = (help_node_t *)cupsArrayFirst(hi->sorted);
305 nn = (help_node_t *)cupsArrayNext(hi->sorted))
306 if (nn->anchor && !strcmp(helpfile, nn->filename))
309 * Add a link for this node...
312 snprintf(line, sizeof(line), "#%s", nn->anchor);
313 cgiSetArray("BMLINK", i, line);
314 cgiSetArray("BMTEXT", i, nn->text);
315 cgiSetArray("BMINDENT", i, "2");
320 cupsArrayRestore(hi->sorted);
325 * Show the search and bookmark content...
328 if (!helpfile || !printable)
329 cgiCopyTemplateLang("help-header.tmpl");
331 cgiCopyTemplateLang("help-printable.tmpl");
334 * If we are viewing a file, copy it in now...
339 if ((fp = cupsFileOpen(filename, "r")) != NULL)
341 int inbody; /* Are we inside the body? */
346 while (cupsFileGets(fp, line, sizeof(line)))
350 if (!_cups_strncasecmp(line, "</BODY>", 7))
353 printf("%s\n", line);
355 else if (!_cups_strncasecmp(line, "<BODY", 5))
364 cgiSetVariable("ERROR", cgiText(_("Unable to open help file.")));
365 cgiCopyTemplateLang("error.tmpl");
370 * Send a standard trailer...
375 cgiCopyTemplateLang("help-trailer.tmpl");
379 puts("</BODY>\n</HTML>");
382 * Delete the index...
388 * Return with no errors...