]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/help.c
314be87e82ada2af56123393db8b8c37c99d5cf4
2 * Online help CGI for CUPS.
4 * Copyright 2007-2011 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * Include necessary headers...
18 #include "cgi-private.h"
22 * 'main()' - Main entry for CGI.
25 int /* O - Exit status */
26 main(int argc
, /* I - Number of command-line arguments */
27 char *argv
[]) /* I - Command-line arguments */
29 help_index_t
*hi
, /* Help index */
30 *si
; /* Search index */
31 help_node_t
*n
; /* Current help node */
32 int i
; /* Looping var */
33 const char *query
; /* Search query */
34 const char *cache_dir
; /* CUPS_CACHEDIR environment variable */
35 const char *docroot
; /* CUPS_DOCROOT environment variable */
36 const char *helpfile
, /* Current help file */
37 *helptitle
= NULL
; /* Current help title */
38 const char *topic
; /* Current topic */
39 char topic_data
[1024]; /* Topic form data */
40 const char *section
; /* Current section */
41 char filename
[1024], /* Filename */
42 directory
[1024]; /* Directory */
43 cups_file_t
*fp
; /* Help file */
44 char line
[1024]; /* Line from file */
45 int printable
; /* Show printable version? */
49 * Get any form variables...
54 printable
= cgiGetVariable("PRINTABLE") != NULL
;
57 * Set the web interface section...
60 cgiSetVariable("SECTION", "help");
61 cgiSetVariable("REFRESH_PAGE", "");
64 * Load the help index...
67 if ((cache_dir
= getenv("CUPS_CACHEDIR")) == NULL
)
68 cache_dir
= CUPS_CACHEDIR
;
70 snprintf(filename
, sizeof(filename
), "%s/help.index", cache_dir
);
72 if ((docroot
= getenv("CUPS_DOCROOT")) == NULL
)
73 docroot
= CUPS_DOCROOT
;
75 snprintf(directory
, sizeof(directory
), "%s/help", docroot
);
77 fprintf(stderr
, "DEBUG: helpLoadIndex(filename=\"%s\", directory=\"%s\")\n",
80 hi
= helpLoadIndex(filename
, directory
);
85 cgiStartHTML(cgiText(_("Online Help")));
86 cgiSetVariable("ERROR", cgiText(_("Unable to load help index.")));
87 cgiCopyTemplateLang("error.tmpl");
93 fprintf(stderr
, "DEBUG: %d nodes in help index...\n",
94 cupsArrayCount(hi
->nodes
));
97 * See if we are viewing a file...
100 for (i
= 0; i
< argc
; i
++)
101 fprintf(stderr
, "DEBUG: argv[%d]=\"%s\"\n", i
, argv
[i
]);
103 if ((helpfile
= getenv("PATH_INFO")) != NULL
)
114 * Verify that the help file exists and is part of the index...
117 snprintf(filename
, sizeof(filename
), "%s/help/%s", docroot
, helpfile
);
119 fprintf(stderr
, "DEBUG: helpfile=\"%s\", filename=\"%s\"\n",
122 if (access(filename
, R_OK
))
126 cgiStartHTML(cgiText(_("Online Help")));
127 cgiSetVariable("ERROR", cgiText(_("Unable to access help file.")));
128 cgiCopyTemplateLang("error.tmpl");
134 if ((n
= helpFindNode(hi
, helpfile
, NULL
)) == NULL
)
136 cgiStartHTML(cgiText(_("Online Help")));
137 cgiSetVariable("ERROR", cgiText(_("Help file not in index.")));
138 cgiCopyTemplateLang("error.tmpl");
145 * Save the page title and help file...
152 * Send a standard page header...
156 puts("Content-Type: text/html;charset=utf-8\n");
158 cgiStartHTML(n
->text
);
163 * Send a standard page header...
166 cgiStartHTML(cgiText(_("Online Help")));
168 topic
= cgiGetVariable("TOPIC");
172 * Do a search as needed...
175 if (cgiGetVariable("CLEAR"))
176 cgiSetVariable("QUERY", "");
178 query
= cgiGetVariable("QUERY");
179 si
= helpSearchIndex(hi
, query
, topic
, helpfile
);
183 cgiSetVariable("QUERY", query
);
185 cgiSetVariable("TOPIC", topic
);
187 cgiSetVariable("HELPFILE", helpfile
);
189 cgiSetVariable("HELPTITLE", helptitle
);
191 fprintf(stderr
, "DEBUG: query=\"%s\", topic=\"%s\"\n",
192 query
? query
: "(null)", topic
? topic
: "(null)");
196 help_node_t
*nn
; /* Parent node */
200 "DEBUG: si=%p, si->sorted=%p, cupsArrayCount(si->sorted)=%d\n", si
,
201 si
->sorted
, cupsArrayCount(si
->sorted
));
203 for (i
= 0, n
= (help_node_t
*)cupsArrayFirst(si
->sorted
);
205 i
++, n
= (help_node_t
*)cupsArrayNext(si
->sorted
))
207 if (helpfile
&& n
->anchor
)
208 snprintf(line
, sizeof(line
), "#%s", n
->anchor
);
210 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s#%s", n
->filename
,
211 query
? query
: "", n
->anchor
);
213 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s", n
->filename
,
216 cgiSetArray("QTEXT", i
, n
->text
);
217 cgiSetArray("QLINK", i
, line
);
219 if (!helpfile
&& n
->anchor
)
221 nn
= helpFindNode(hi
, n
->filename
, NULL
);
223 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s", nn
->filename
,
226 cgiSetArray("QPTEXT", i
, nn
->text
);
227 cgiSetArray("QPLINK", i
, line
);
231 cgiSetArray("QPTEXT", i
, "");
232 cgiSetArray("QPLINK", i
, "");
235 fprintf(stderr
, "DEBUG: [%d] = \"%s\" @ \"%s\"\n", i
, n
->text
, line
);
242 * OK, now list the bookmarks within the index...
245 for (i
= 0, section
= NULL
, n
= (help_node_t
*)cupsArrayFirst(hi
->sorted
);
247 n
= (help_node_t
*)cupsArrayNext(hi
->sorted
))
253 * Add a section link as needed...
257 (!section
|| strcmp(n
->section
, section
)))
260 * Add a link for this node...
263 snprintf(line
, sizeof(line
), "/help/?TOPIC=%s&QUERY=%s",
264 cgiFormEncode(topic_data
, n
->section
, sizeof(topic_data
)),
266 cgiSetArray("BMLINK", i
, line
);
267 cgiSetArray("BMTEXT", i
, n
->section
);
268 cgiSetArray("BMINDENT", i
, "0");
271 section
= n
->section
;
274 if (!topic
|| strcmp(n
->section
, topic
))
278 * Add a link for this node...
281 snprintf(line
, sizeof(line
), "/help/%s?TOPIC=%s&QUERY=%s", n
->filename
,
282 cgiFormEncode(topic_data
, n
->section
, sizeof(topic_data
)),
284 cgiSetArray("BMLINK", i
, line
);
285 cgiSetArray("BMTEXT", i
, n
->text
);
286 cgiSetArray("BMINDENT", i
, "1");
290 if (helpfile
&& !strcmp(helpfile
, n
->filename
))
292 help_node_t
*nn
; /* Pointer to sub-node */
295 cupsArraySave(hi
->sorted
);
297 for (nn
= (help_node_t
*)cupsArrayFirst(hi
->sorted
);
299 nn
= (help_node_t
*)cupsArrayNext(hi
->sorted
))
300 if (nn
->anchor
&& !strcmp(helpfile
, nn
->filename
))
303 * Add a link for this node...
306 snprintf(line
, sizeof(line
), "#%s", nn
->anchor
);
307 cgiSetArray("BMLINK", i
, line
);
308 cgiSetArray("BMTEXT", i
, nn
->text
);
309 cgiSetArray("BMINDENT", i
, "2");
314 cupsArrayRestore(hi
->sorted
);
319 * Show the search and bookmark content...
322 if (!helpfile
|| !printable
)
323 cgiCopyTemplateLang("help-header.tmpl");
325 cgiCopyTemplateLang("help-printable.tmpl");
328 * If we are viewing a file, copy it in now...
333 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
335 int inbody
; /* Are we inside the body? */
340 while (cupsFileGets(fp
, line
, sizeof(line
)))
344 if (!_cups_strncasecmp(line
, "</BODY>", 7))
347 printf("%s\n", line
);
349 else if (!_cups_strncasecmp(line
, "<BODY", 5))
358 cgiSetVariable("ERROR", cgiText(_("Unable to open help file.")));
359 cgiCopyTemplateLang("error.tmpl");
364 * Send a standard trailer...
369 cgiCopyTemplateLang("help-trailer.tmpl");
373 puts("</BODY>\n</HTML>");
376 * Delete the index...
382 * Return with no errors...