]>
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 1997-2006 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * main() - Main entry for CGI.
30 * Include necessary headers...
33 #include "cgi-private.h"
37 * 'main()' - Main entry for CGI.
40 int /* O - Exit status */
41 main(int argc
, /* I - Number of command-line arguments */
42 char *argv
[]) /* I - Command-line arguments */
44 help_index_t
*hi
, /* Help index */
45 *si
; /* Search index */
46 help_node_t
*n
; /* Current help node */
47 int i
; /* Looping var */
48 const char *query
; /* Search query */
49 const char *cache_dir
; /* CUPS_CACHEDIR environment variable */
50 const char *docroot
; /* CUPS_DOCROOT environment variable */
51 const char *helpfile
; /* Current help file */
52 const char *topic
; /* Current topic */
53 char topic_data
[1024]; /* Topic form data */
54 const char *section
; /* Current section */
55 char filename
[1024], /* Filename */
56 directory
[1024]; /* Directory */
57 cups_file_t
*fp
; /* Help file */
58 char line
[1024]; /* Line from file */
59 int printable
; /* Show printable version? */
63 * Get any form variables...
68 printable
= cgiGetVariable("PRINTABLE") != NULL
;
71 * Set the web interface section...
74 cgiSetVariable("SECTION", "help");
77 * Load the help index...
80 if ((cache_dir
= getenv("CUPS_CACHEDIR")) == NULL
)
81 cache_dir
= CUPS_CACHEDIR
;
83 snprintf(filename
, sizeof(filename
), "%s/help.index", cache_dir
);
85 if ((docroot
= getenv("CUPS_DOCROOT")) == NULL
)
86 docroot
= CUPS_DOCROOT
;
88 snprintf(directory
, sizeof(directory
), "%s/help", docroot
);
90 fprintf(stderr
, "DEBUG: helpLoadIndex(filename=\"%s\", directory=\"%s\")\n",
93 hi
= helpLoadIndex(filename
, directory
);
98 cgiStartHTML(cgiText(_("Help")));
99 cgiSetVariable("ERROR", "Unable to load help index!");
100 cgiCopyTemplateLang("error.tmpl");
106 fprintf(stderr
, "DEBUG: %d nodes in help index...\n",
107 cupsArrayCount(hi
->nodes
));
110 * See if we are viewing a file...
113 for (i
= 0; i
< argc
; i
++)
114 fprintf(stderr
, "argv[%d]=\"%s\"\n", i
, argv
[i
]);
116 if ((helpfile
= getenv("PATH_INFO")) != NULL
)
127 * Verify that the help file exists and is part of the index...
130 snprintf(filename
, sizeof(filename
), "%s/help/%s", docroot
, helpfile
);
132 fprintf(stderr
, "DEBUG: helpfile=\"%s\", filename=\"%s\"\n",
135 if (access(filename
, R_OK
))
139 cgiStartHTML(cgiText(_("Help")));
140 cgiSetVariable("ERROR", "Unable to access help file!");
141 cgiCopyTemplateLang("error.tmpl");
147 if ((n
= helpFindNode(hi
, helpfile
, NULL
)) == NULL
)
149 cgiStartHTML(cgiText(_("Help")));
150 cgiSetVariable("ERROR", "Help file not in index!");
151 cgiCopyTemplateLang("error.tmpl");
158 * Set the page title and save the help file...
161 cgiSetVariable("HELPFILE", helpfile
);
162 cgiSetVariable("HELPTITLE", n
->text
);
165 * Send a standard page header...
169 puts("Content-Type: text/html;charset=utf-8\n");
171 cgiStartHTML(n
->text
);
176 * Send a standard page header...
179 cgiStartHTML(cgiText(_("Help")));
183 * Do a search as needed...
186 query
= cgiGetVariable("QUERY");
187 topic
= cgiGetVariable("TOPIC");
188 si
= helpSearchIndex(hi
, query
, topic
, helpfile
);
190 fprintf(stderr
, "DEBUG: query=\"%s\", topic=\"%s\"\n",
191 query
? query
: "(null)", topic
? topic
: "(null)");
195 help_node_t
*nn
; /* Parent node */
199 "DEBUG: si=%p, si->sorted=%p, cupsArrayCount(si->sorted)=%d\n", si
,
200 si
->sorted
, cupsArrayCount(si
->sorted
));
202 for (i
= 0, n
= (help_node_t
*)cupsArrayFirst(si
->sorted
);
204 i
++, n
= (help_node_t
*)cupsArrayNext(si
->sorted
))
206 if (helpfile
&& n
->anchor
)
207 snprintf(line
, sizeof(line
), "#%s", n
->anchor
);
209 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s#%s", n
->filename
,
210 query
? query
: "", n
->anchor
);
212 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s", n
->filename
,
215 cgiSetArray("QTEXT", i
, n
->text
);
216 cgiSetArray("QLINK", i
, line
);
218 if (!helpfile
&& n
->anchor
)
220 nn
= helpFindNode(hi
, n
->filename
, NULL
);
222 snprintf(line
, sizeof(line
), "/help/%s?QUERY=%s", nn
->filename
,
225 cgiSetArray("QPTEXT", i
, nn
->text
);
226 cgiSetArray("QPLINK", i
, line
);
230 cgiSetArray("QPTEXT", i
, "");
231 cgiSetArray("QPLINK", i
, "");
234 fprintf(stderr
, "DEBUG: [%d] = \"%s\" @ \"%s\"\n", i
, n
->text
, line
);
241 * OK, now list the bookmarks within the index...
244 for (i
= 0, section
= NULL
, n
= (help_node_t
*)cupsArrayFirst(hi
->sorted
);
246 n
= (help_node_t
*)cupsArrayNext(hi
->sorted
))
252 * Add a section link as needed...
256 (!section
|| strcmp(n
->section
, section
)))
259 * Add a link for this node...
262 snprintf(line
, sizeof(line
), "/help/?TOPIC=%s&QUERY=%s",
263 cgiFormEncode(topic_data
, n
->section
, sizeof(topic_data
)),
265 cgiSetArray("BMLINK", i
, line
);
266 cgiSetArray("BMTEXT", i
, n
->section
);
267 cgiSetArray("BMINDENT", i
, "0");
270 section
= n
->section
;
273 if (!topic
|| strcmp(n
->section
, topic
))
277 * Add a link for this node...
280 snprintf(line
, sizeof(line
), "/help/%s?TOPIC=%s&QUERY=%s", n
->filename
,
281 cgiFormEncode(topic_data
, n
->section
, sizeof(topic_data
)),
283 cgiSetArray("BMLINK", i
, line
);
284 cgiSetArray("BMTEXT", i
, n
->text
);
285 cgiSetArray("BMINDENT", i
, "1");
289 if (helpfile
&& !strcmp(helpfile
, n
->filename
))
291 help_node_t
*nn
; /* Pointer to sub-node */
294 cupsArraySave(hi
->sorted
);
296 for (nn
= (help_node_t
*)cupsArrayFirst(hi
->sorted
);
298 nn
= (help_node_t
*)cupsArrayNext(hi
->sorted
))
299 if (nn
->anchor
&& !strcmp(helpfile
, nn
->filename
))
302 * Add a link for this node...
305 snprintf(line
, sizeof(line
), "#%s", nn
->anchor
);
306 cgiSetArray("BMLINK", i
, line
);
307 cgiSetArray("BMTEXT", i
, nn
->text
);
308 cgiSetArray("BMINDENT", i
, "2");
313 cupsArrayRestore(hi
->sorted
);
318 * Show the search and bookmark content...
321 if (!helpfile
|| !printable
)
322 cgiCopyTemplateLang("help-header.tmpl");
324 cgiCopyTemplateLang("help-printable.tmpl");
327 * If we are viewing a file, copy it in now...
332 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
334 int inbody
; /* Are we inside the body? */
339 while (cupsFileGets(fp
, line
, sizeof(line
)))
343 if (!strncasecmp(line
, "</BODY>", 7))
346 printf("%s\n", line
);
348 else if (!strncasecmp(line
, "<BODY", 5))
357 cgiSetVariable("ERROR", "Unable to open help file.");
358 cgiCopyTemplateLang("error.tmpl");
363 * Send a standard trailer...
369 puts("</BODY>\n</HTML>");
372 * Delete the index...
378 * Return with no errors...