]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/help.c
4 * On-line help CGI for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 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(_("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(_("Help")));
131 cgiSetVariable("ERROR", "Unable to access help file!");
132 cgiCopyTemplateLang("error.tmpl");
138 if ((n
= helpFindNode(hi
, helpfile
, NULL
)) == NULL
)
140 cgiStartHTML(cgiText(_("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
);
156 * Send a standard page header...
160 puts("Content-Type: text/html;charset=utf-8\n");
162 cgiStartHTML(n
->text
);
167 * Send a standard page header...
170 cgiStartHTML(cgiText(_("Help")));
174 * Do a search as needed...
177 query
= cgiGetVariable("QUERY");
178 topic
= cgiGetVariable("TOPIC");
179 si
= helpSearchIndex(hi
, query
, topic
, helpfile
);
181 fprintf(stderr
, "DEBUG: query=\"%s\", topic=\"%s\"\n",
182 query
? query
: "(null)", topic
? topic
: "(null)");
186 help_node_t
*nn
; /* Parent node */
190 "DEBUG: si=%p, si->sorted=%p, cupsArrayCount(si->sorted)=%d\n", si
,
191 si
->sorted
, cupsArrayCount(si
->sorted
));
193 for (i
= 0, n
= (help_node_t
*)cupsArrayFirst(si
->sorted
);
195 i
++, n
= (help_node_t
*)cupsArrayNext(si
->sorted
))
197 if (helpfile
&& n
->anchor
)
198 snprintf(line
, sizeof(line
), "#%s", n
->anchor
);
200 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s#%s", n
->filename
,
201 query
? query
: "", n
->anchor
);
203 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s", n
->filename
,
206 cgiSetArray("QTEXT", i
, n
->text
);
207 cgiSetArray("QLINK", i
, line
);
209 if (!helpfile
&& n
->anchor
)
211 nn
= helpFindNode(hi
, n
->filename
, NULL
);
213 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s", nn
->filename
,
216 cgiSetArray("QPTEXT", i
, nn
->text
);
217 cgiSetArray("QPLINK", i
, line
);
221 cgiSetArray("QPTEXT", i
, "");
222 cgiSetArray("QPLINK", i
, "");
225 fprintf(stderr
, "DEBUG: [%d] = \"%s\" @ \"%s\"\n", i
, n
->text
, line
);
232 * OK, now list the bookmarks within the index...
235 for (i
= 0, section
= NULL
, n
= (help_node_t
*)cupsArrayFirst(hi
->sorted
);
237 n
= (help_node_t
*)cupsArrayNext(hi
->sorted
))
243 * Add a section link as needed...
247 (!section
|| strcmp(n
->section
, section
)))
250 * Add a link for this node...
253 snprintf(line
, sizeof(line
), "/help/?TOPIC=%s&QUERY=%s",
254 cgiFormEncode(topic_data
, n
->section
, sizeof(topic_data
)),
256 cgiSetArray("BMLINK", i
, line
);
257 cgiSetArray("BMTEXT", i
, n
->section
);
258 cgiSetArray("BMINDENT", i
, "0");
261 section
= n
->section
;
264 if (!topic
|| strcmp(n
->section
, topic
))
268 * Add a link for this node...
271 snprintf(line
, sizeof(line
), "/help/%s?TOPIC=%s&QUERY=%s", n
->filename
,
272 cgiFormEncode(topic_data
, n
->section
, sizeof(topic_data
)),
274 cgiSetArray("BMLINK", i
, line
);
275 cgiSetArray("BMTEXT", i
, n
->text
);
276 cgiSetArray("BMINDENT", i
, "1");
280 if (helpfile
&& !strcmp(helpfile
, n
->filename
))
282 help_node_t
*nn
; /* Pointer to sub-node */
285 cupsArraySave(hi
->sorted
);
287 for (nn
= (help_node_t
*)cupsArrayFirst(hi
->sorted
);
289 nn
= (help_node_t
*)cupsArrayNext(hi
->sorted
))
290 if (nn
->anchor
&& !strcmp(helpfile
, nn
->filename
))
293 * Add a link for this node...
296 snprintf(line
, sizeof(line
), "#%s", nn
->anchor
);
297 cgiSetArray("BMLINK", i
, line
);
298 cgiSetArray("BMTEXT", i
, nn
->text
);
299 cgiSetArray("BMINDENT", i
, "2");
304 cupsArrayRestore(hi
->sorted
);
309 * Show the search and bookmark content...
312 if (!helpfile
|| !printable
)
313 cgiCopyTemplateLang("help-header.tmpl");
315 cgiCopyTemplateLang("help-printable.tmpl");
318 * If we are viewing a file, copy it in now...
323 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
325 int inbody
; /* Are we inside the body? */
330 while (cupsFileGets(fp
, line
, sizeof(line
)))
334 if (!strncasecmp(line
, "</BODY>", 7))
337 printf("%s\n", line
);
339 else if (!strncasecmp(line
, "<BODY", 5))
348 cgiSetVariable("ERROR", "Unable to open help file.");
349 cgiCopyTemplateLang("error.tmpl");
354 * Send a standard trailer...
360 puts("</BODY>\n</HTML>");
363 * Delete the index...
369 * Return with no errors...