]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/help.c
2 * Online help CGI for CUPS.
4 * Copyright 2007-2011 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include "cgi-private.h"
18 * 'main()' - Main entry for CGI.
21 int /* O - Exit status */
22 main(int argc
, /* I - Number of command-line arguments */
23 char *argv
[]) /* I - Command-line arguments */
25 help_index_t
*hi
, /* Help index */
26 *si
; /* Search index */
27 help_node_t
*n
; /* Current help node */
28 int i
; /* Looping var */
29 const char *query
; /* Search query */
30 const char *cache_dir
; /* CUPS_CACHEDIR environment variable */
31 const char *docroot
; /* CUPS_DOCROOT environment variable */
32 const char *helpfile
, /* Current help file */
33 *helptitle
= NULL
; /* Current help title */
34 const char *topic
; /* Current topic */
35 char topic_data
[1024]; /* Topic form data */
36 const char *section
; /* Current section */
37 char filename
[1024], /* Filename */
38 directory
[1024]; /* Directory */
39 cups_file_t
*fp
; /* Help file */
40 char line
[1024]; /* Line from file */
41 int printable
; /* Show printable version? */
45 * Get any form variables...
50 printable
= cgiGetVariable("PRINTABLE") != NULL
;
53 * Set the web interface section...
56 cgiSetVariable("SECTION", "help");
57 cgiSetVariable("REFRESH_PAGE", "");
60 * Load the help index...
63 if ((cache_dir
= getenv("CUPS_CACHEDIR")) == NULL
)
64 cache_dir
= CUPS_CACHEDIR
;
66 snprintf(filename
, sizeof(filename
), "%s/help.index", cache_dir
);
68 if ((docroot
= getenv("CUPS_DOCROOT")) == NULL
)
69 docroot
= CUPS_DOCROOT
;
71 snprintf(directory
, sizeof(directory
), "%s/help", docroot
);
73 fprintf(stderr
, "DEBUG: helpLoadIndex(filename=\"%s\", directory=\"%s\")\n",
76 hi
= helpLoadIndex(filename
, directory
);
81 cgiStartHTML(cgiText(_("Online Help")));
82 cgiSetVariable("ERROR", cgiText(_("Unable to load help index.")));
83 cgiCopyTemplateLang("error.tmpl");
89 fprintf(stderr
, "DEBUG: %d nodes in help index...\n",
90 cupsArrayCount(hi
->nodes
));
93 * See if we are viewing a file...
96 for (i
= 0; i
< argc
; i
++)
97 fprintf(stderr
, "DEBUG: argv[%d]=\"%s\"\n", i
, argv
[i
]);
99 if ((helpfile
= getenv("PATH_INFO")) != NULL
)
110 * Verify that the help file exists and is part of the index...
113 snprintf(filename
, sizeof(filename
), "%s/help/%s", docroot
, helpfile
);
115 fprintf(stderr
, "DEBUG: helpfile=\"%s\", filename=\"%s\"\n",
118 if (access(filename
, R_OK
))
122 cgiStartHTML(cgiText(_("Online Help")));
123 cgiSetVariable("ERROR", cgiText(_("Unable to access help file.")));
124 cgiCopyTemplateLang("error.tmpl");
130 if ((n
= helpFindNode(hi
, helpfile
, NULL
)) == NULL
)
132 cgiStartHTML(cgiText(_("Online Help")));
133 cgiSetVariable("ERROR", cgiText(_("Help file not in index.")));
134 cgiCopyTemplateLang("error.tmpl");
141 * Save the page title and help file...
148 * Send a standard page header...
152 puts("Content-Type: text/html;charset=utf-8\n");
154 cgiStartHTML(n
->text
);
159 * Send a standard page header...
162 cgiStartHTML(cgiText(_("Online Help")));
164 topic
= cgiGetVariable("TOPIC");
168 * Do a search as needed...
171 if (cgiGetVariable("CLEAR"))
172 cgiSetVariable("QUERY", "");
174 query
= cgiGetVariable("QUERY");
175 si
= helpSearchIndex(hi
, query
, topic
, helpfile
);
179 cgiSetVariable("QUERY", query
);
181 cgiSetVariable("TOPIC", topic
);
183 cgiSetVariable("HELPFILE", helpfile
);
185 cgiSetVariable("HELPTITLE", helptitle
);
187 fprintf(stderr
, "DEBUG: query=\"%s\", topic=\"%s\"\n",
188 query
? query
: "(null)", topic
? topic
: "(null)");
192 help_node_t
*nn
; /* Parent node */
196 "DEBUG: si=%p, si->sorted=%p, cupsArrayCount(si->sorted)=%d\n", si
,
197 si
->sorted
, cupsArrayCount(si
->sorted
));
199 for (i
= 0, n
= (help_node_t
*)cupsArrayFirst(si
->sorted
);
201 i
++, n
= (help_node_t
*)cupsArrayNext(si
->sorted
))
203 if (helpfile
&& n
->anchor
)
204 snprintf(line
, sizeof(line
), "#%s", n
->anchor
);
206 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s#%s", n
->filename
,
207 query
? query
: "", n
->anchor
);
209 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s", n
->filename
,
212 cgiSetArray("QTEXT", i
, n
->text
);
213 cgiSetArray("QLINK", i
, line
);
215 if (!helpfile
&& n
->anchor
)
217 nn
= helpFindNode(hi
, n
->filename
, NULL
);
219 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s", nn
->filename
,
222 cgiSetArray("QPTEXT", i
, nn
->text
);
223 cgiSetArray("QPLINK", i
, line
);
227 cgiSetArray("QPTEXT", i
, "");
228 cgiSetArray("QPLINK", i
, "");
231 fprintf(stderr
, "DEBUG: [%d] = \"%s\" @ \"%s\"\n", i
, n
->text
, line
);
238 * OK, now list the bookmarks within the index...
241 for (i
= 0, section
= NULL
, n
= (help_node_t
*)cupsArrayFirst(hi
->sorted
);
243 n
= (help_node_t
*)cupsArrayNext(hi
->sorted
))
249 * Add a section link as needed...
253 (!section
|| strcmp(n
->section
, section
)))
256 * Add a link for this node...
259 snprintf(line
, sizeof(line
), "/help/?TOPIC=%s&QUERY=%s",
260 cgiFormEncode(topic_data
, n
->section
, sizeof(topic_data
)),
262 cgiSetArray("BMLINK", i
, line
);
263 cgiSetArray("BMTEXT", i
, n
->section
);
264 cgiSetArray("BMINDENT", i
, "0");
267 section
= n
->section
;
270 if (!topic
|| !n
->section
|| strcmp(n
->section
, topic
))
274 * Add a link for this node...
277 snprintf(line
, sizeof(line
), "/help/%s?TOPIC=%s&QUERY=%s", n
->filename
,
278 cgiFormEncode(topic_data
, n
->section
, sizeof(topic_data
)),
280 cgiSetArray("BMLINK", i
, line
);
281 cgiSetArray("BMTEXT", i
, n
->text
);
282 cgiSetArray("BMINDENT", i
, "1");
286 if (helpfile
&& !strcmp(helpfile
, n
->filename
))
288 help_node_t
*nn
; /* Pointer to sub-node */
291 cupsArraySave(hi
->sorted
);
293 for (nn
= (help_node_t
*)cupsArrayFirst(hi
->sorted
);
295 nn
= (help_node_t
*)cupsArrayNext(hi
->sorted
))
296 if (nn
->anchor
&& !strcmp(helpfile
, nn
->filename
))
299 * Add a link for this node...
302 snprintf(line
, sizeof(line
), "#%s", nn
->anchor
);
303 cgiSetArray("BMLINK", i
, line
);
304 cgiSetArray("BMTEXT", i
, nn
->text
);
305 cgiSetArray("BMINDENT", i
, "2");
310 cupsArrayRestore(hi
->sorted
);
315 * Show the search and bookmark content...
318 if (!helpfile
|| !printable
)
319 cgiCopyTemplateLang("help-header.tmpl");
321 cgiCopyTemplateLang("help-printable.tmpl");
324 * If we are viewing a file, copy it in now...
329 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
331 int inbody
; /* Are we inside the body? */
332 char *lineptr
; /* Pointer into line */
336 while (cupsFileGets(fp
, line
, sizeof(line
)))
338 for (lineptr
= line
; *lineptr
&& isspace(*lineptr
& 255); lineptr
++);
342 if (!_cups_strncasecmp(lineptr
, "</BODY>", 7))
345 printf("%s\n", line
);
347 else if (!_cups_strncasecmp(lineptr
, "<BODY", 5))
356 cgiSetVariable("ERROR", cgiText(_("Unable to open help file.")));
357 cgiCopyTemplateLang("error.tmpl");
362 * Send a standard trailer...
367 cgiCopyTemplateLang("help-trailer.tmpl");
371 puts("</BODY>\n</HTML>");
374 * Delete the index...
380 * Return with no errors...