]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/help.c
4 * Online help CGI for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 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 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 */
50 int printable
; /* Show printable version? */
54 * Get any form variables...
59 printable
= cgiGetVariable("PRINTABLE") != NULL
;
62 * Set the web interface section...
65 cgiSetVariable("SECTION", "help");
68 * Load the help index...
71 if ((cache_dir
= getenv("CUPS_CACHEDIR")) == NULL
)
72 cache_dir
= CUPS_CACHEDIR
;
74 snprintf(filename
, sizeof(filename
), "%s/help.index", cache_dir
);
76 if ((docroot
= getenv("CUPS_DOCROOT")) == NULL
)
77 docroot
= CUPS_DOCROOT
;
79 snprintf(directory
, sizeof(directory
), "%s/help", docroot
);
81 fprintf(stderr
, "DEBUG: helpLoadIndex(filename=\"%s\", directory=\"%s\")\n",
84 hi
= helpLoadIndex(filename
, directory
);
89 cgiStartHTML(cgiText(_("Online Help")));
90 cgiSetVariable("ERROR", "Unable to load help index!");
91 cgiCopyTemplateLang("error.tmpl");
97 fprintf(stderr
, "DEBUG: %d nodes in help index...\n",
98 cupsArrayCount(hi
->nodes
));
101 * See if we are viewing a file...
104 for (i
= 0; i
< argc
; i
++)
105 fprintf(stderr
, "argv[%d]=\"%s\"\n", i
, argv
[i
]);
107 if ((helpfile
= getenv("PATH_INFO")) != NULL
)
118 * Verify that the help file exists and is part of the index...
121 snprintf(filename
, sizeof(filename
), "%s/help/%s", docroot
, helpfile
);
123 fprintf(stderr
, "DEBUG: helpfile=\"%s\", filename=\"%s\"\n",
126 if (access(filename
, R_OK
))
130 cgiStartHTML(cgiText(_("Online Help")));
131 cgiSetVariable("ERROR", "Unable to access help file!");
132 cgiCopyTemplateLang("error.tmpl");
138 if ((n
= helpFindNode(hi
, helpfile
, NULL
)) == NULL
)
140 cgiStartHTML(cgiText(_("Online Help")));
141 cgiSetVariable("ERROR", "Help file not in index!");
142 cgiCopyTemplateLang("error.tmpl");
149 * Set the page title and save the help file...
152 cgiSetVariable("HELPFILE", helpfile
);
153 cgiSetVariable("HELPTITLE", n
->text
);
154 cgiSetVariable("TOPIC", n
->section
);
157 * Send a standard page header...
161 puts("Content-Type: text/html;charset=utf-8\n");
163 cgiStartHTML(n
->text
);
168 * Send a standard page header...
171 cgiStartHTML(cgiText(_("Online Help")));
175 * Do a search as needed...
178 if (cgiGetVariable("CLEAR"))
179 cgiSetVariable("QUERY", "");
181 query
= cgiGetVariable("QUERY");
182 topic
= cgiGetVariable("TOPIC");
183 si
= helpSearchIndex(hi
, query
, topic
, helpfile
);
185 fprintf(stderr
, "DEBUG: query=\"%s\", topic=\"%s\"\n",
186 query
? query
: "(null)", topic
? topic
: "(null)");
190 help_node_t
*nn
; /* Parent node */
194 "DEBUG: si=%p, si->sorted=%p, cupsArrayCount(si->sorted)=%d\n", si
,
195 si
->sorted
, cupsArrayCount(si
->sorted
));
197 for (i
= 0, n
= (help_node_t
*)cupsArrayFirst(si
->sorted
);
199 i
++, n
= (help_node_t
*)cupsArrayNext(si
->sorted
))
201 if (helpfile
&& n
->anchor
)
202 snprintf(line
, sizeof(line
), "#%s", n
->anchor
);
204 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s#%s", n
->filename
,
205 query
? query
: "", n
->anchor
);
207 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s", n
->filename
,
210 cgiSetArray("QTEXT", i
, n
->text
);
211 cgiSetArray("QLINK", i
, line
);
213 if (!helpfile
&& n
->anchor
)
215 nn
= helpFindNode(hi
, n
->filename
, NULL
);
217 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s", nn
->filename
,
220 cgiSetArray("QPTEXT", i
, nn
->text
);
221 cgiSetArray("QPLINK", i
, line
);
225 cgiSetArray("QPTEXT", i
, "");
226 cgiSetArray("QPLINK", i
, "");
229 fprintf(stderr
, "DEBUG: [%d] = \"%s\" @ \"%s\"\n", i
, n
->text
, line
);
236 * OK, now list the bookmarks within the index...
239 for (i
= 0, section
= NULL
, n
= (help_node_t
*)cupsArrayFirst(hi
->sorted
);
241 n
= (help_node_t
*)cupsArrayNext(hi
->sorted
))
247 * Add a section link as needed...
251 (!section
|| strcmp(n
->section
, section
)))
254 * Add a link for this node...
257 snprintf(line
, sizeof(line
), "/help/?TOPIC=%s&QUERY=%s",
258 cgiFormEncode(topic_data
, n
->section
, sizeof(topic_data
)),
260 cgiSetArray("BMLINK", i
, line
);
261 cgiSetArray("BMTEXT", i
, n
->section
);
262 cgiSetArray("BMINDENT", i
, "0");
265 section
= n
->section
;
268 if (!topic
|| strcmp(n
->section
, topic
))
272 * Add a link for this node...
275 snprintf(line
, sizeof(line
), "/help/%s?TOPIC=%s&QUERY=%s", n
->filename
,
276 cgiFormEncode(topic_data
, n
->section
, sizeof(topic_data
)),
278 cgiSetArray("BMLINK", i
, line
);
279 cgiSetArray("BMTEXT", i
, n
->text
);
280 cgiSetArray("BMINDENT", i
, "1");
284 if (helpfile
&& !strcmp(helpfile
, n
->filename
))
286 help_node_t
*nn
; /* Pointer to sub-node */
289 cupsArraySave(hi
->sorted
);
291 for (nn
= (help_node_t
*)cupsArrayFirst(hi
->sorted
);
293 nn
= (help_node_t
*)cupsArrayNext(hi
->sorted
))
294 if (nn
->anchor
&& !strcmp(helpfile
, nn
->filename
))
297 * Add a link for this node...
300 snprintf(line
, sizeof(line
), "#%s", nn
->anchor
);
301 cgiSetArray("BMLINK", i
, line
);
302 cgiSetArray("BMTEXT", i
, nn
->text
);
303 cgiSetArray("BMINDENT", i
, "2");
308 cupsArrayRestore(hi
->sorted
);
313 * Show the search and bookmark content...
316 if (!helpfile
|| !printable
)
317 cgiCopyTemplateLang("help-header.tmpl");
319 cgiCopyTemplateLang("help-printable.tmpl");
322 * If we are viewing a file, copy it in now...
327 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
329 int inbody
; /* Are we inside the body? */
334 while (cupsFileGets(fp
, line
, sizeof(line
)))
338 if (!strncasecmp(line
, "</BODY>", 7))
341 printf("%s\n", line
);
343 else if (!strncasecmp(line
, "<BODY", 5))
352 cgiSetVariable("ERROR", "Unable to open help file.");
353 cgiCopyTemplateLang("error.tmpl");
358 * Send a standard trailer...
363 cgiCopyTemplateLang("help-trailer.tmpl");
367 puts("</BODY>\n</HTML>");
370 * Delete the index...
376 * Return with no errors...