xref: /aosp_15_r20/external/libcups/cgi-bin/help.c (revision 5e7646d21f1134fb0638875d812ef646c12ab91e)
1*5e7646d2SAndroid Build Coastguard Worker /*
2*5e7646d2SAndroid Build Coastguard Worker  * Online help CGI for CUPS.
3*5e7646d2SAndroid Build Coastguard Worker  *
4*5e7646d2SAndroid Build Coastguard Worker  * Copyright 2007-2011 by Apple Inc.
5*5e7646d2SAndroid Build Coastguard Worker  * Copyright 1997-2006 by Easy Software Products.
6*5e7646d2SAndroid Build Coastguard Worker  *
7*5e7646d2SAndroid Build Coastguard Worker  * Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
8*5e7646d2SAndroid Build Coastguard Worker  */
9*5e7646d2SAndroid Build Coastguard Worker 
10*5e7646d2SAndroid Build Coastguard Worker /*
11*5e7646d2SAndroid Build Coastguard Worker  * Include necessary headers...
12*5e7646d2SAndroid Build Coastguard Worker  */
13*5e7646d2SAndroid Build Coastguard Worker 
14*5e7646d2SAndroid Build Coastguard Worker #include "cgi-private.h"
15*5e7646d2SAndroid Build Coastguard Worker 
16*5e7646d2SAndroid Build Coastguard Worker 
17*5e7646d2SAndroid Build Coastguard Worker /*
18*5e7646d2SAndroid Build Coastguard Worker  * 'main()' - Main entry for CGI.
19*5e7646d2SAndroid Build Coastguard Worker  */
20*5e7646d2SAndroid Build Coastguard Worker 
21*5e7646d2SAndroid Build Coastguard Worker int					/* O - Exit status */
main(int argc,char * argv[])22*5e7646d2SAndroid Build Coastguard Worker main(int  argc,				/* I - Number of command-line arguments */
23*5e7646d2SAndroid Build Coastguard Worker      char *argv[])			/* I - Command-line arguments */
24*5e7646d2SAndroid Build Coastguard Worker {
25*5e7646d2SAndroid Build Coastguard Worker   help_index_t	*hi,			/* Help index */
26*5e7646d2SAndroid Build Coastguard Worker 		*si;			/* Search index */
27*5e7646d2SAndroid Build Coastguard Worker   help_node_t	*n;			/* Current help node */
28*5e7646d2SAndroid Build Coastguard Worker   int		i;			/* Looping var */
29*5e7646d2SAndroid Build Coastguard Worker   const char	*query;			/* Search query */
30*5e7646d2SAndroid Build Coastguard Worker   const char	*cache_dir;		/* CUPS_CACHEDIR environment variable */
31*5e7646d2SAndroid Build Coastguard Worker   const char	*docroot;		/* CUPS_DOCROOT environment variable */
32*5e7646d2SAndroid Build Coastguard Worker   const char	*helpfile,		/* Current help file */
33*5e7646d2SAndroid Build Coastguard Worker 		*helptitle = NULL;	/* Current help title */
34*5e7646d2SAndroid Build Coastguard Worker   const char	*topic;			/* Current topic */
35*5e7646d2SAndroid Build Coastguard Worker   char		topic_data[1024];	/* Topic form data */
36*5e7646d2SAndroid Build Coastguard Worker   const char	*section;		/* Current section */
37*5e7646d2SAndroid Build Coastguard Worker   char		filename[1024],		/* Filename */
38*5e7646d2SAndroid Build Coastguard Worker 		directory[1024];	/* Directory */
39*5e7646d2SAndroid Build Coastguard Worker   cups_file_t	*fp;			/* Help file */
40*5e7646d2SAndroid Build Coastguard Worker   char		line[1024];		/* Line from file */
41*5e7646d2SAndroid Build Coastguard Worker   int		printable;		/* Show printable version? */
42*5e7646d2SAndroid Build Coastguard Worker 
43*5e7646d2SAndroid Build Coastguard Worker 
44*5e7646d2SAndroid Build Coastguard Worker  /*
45*5e7646d2SAndroid Build Coastguard Worker   * Get any form variables...
46*5e7646d2SAndroid Build Coastguard Worker   */
47*5e7646d2SAndroid Build Coastguard Worker 
48*5e7646d2SAndroid Build Coastguard Worker   cgiInitialize();
49*5e7646d2SAndroid Build Coastguard Worker 
50*5e7646d2SAndroid Build Coastguard Worker   printable = cgiGetVariable("PRINTABLE") != NULL;
51*5e7646d2SAndroid Build Coastguard Worker 
52*5e7646d2SAndroid Build Coastguard Worker  /*
53*5e7646d2SAndroid Build Coastguard Worker   * Set the web interface section...
54*5e7646d2SAndroid Build Coastguard Worker   */
55*5e7646d2SAndroid Build Coastguard Worker 
56*5e7646d2SAndroid Build Coastguard Worker   cgiSetVariable("SECTION", "help");
57*5e7646d2SAndroid Build Coastguard Worker   cgiSetVariable("REFRESH_PAGE", "");
58*5e7646d2SAndroid Build Coastguard Worker 
59*5e7646d2SAndroid Build Coastguard Worker  /*
60*5e7646d2SAndroid Build Coastguard Worker   * Load the help index...
61*5e7646d2SAndroid Build Coastguard Worker   */
62*5e7646d2SAndroid Build Coastguard Worker 
63*5e7646d2SAndroid Build Coastguard Worker   if ((cache_dir = getenv("CUPS_CACHEDIR")) == NULL)
64*5e7646d2SAndroid Build Coastguard Worker     cache_dir = CUPS_CACHEDIR;
65*5e7646d2SAndroid Build Coastguard Worker 
66*5e7646d2SAndroid Build Coastguard Worker   snprintf(filename, sizeof(filename), "%s/help.index", cache_dir);
67*5e7646d2SAndroid Build Coastguard Worker 
68*5e7646d2SAndroid Build Coastguard Worker   if ((docroot = getenv("CUPS_DOCROOT")) == NULL)
69*5e7646d2SAndroid Build Coastguard Worker     docroot = CUPS_DOCROOT;
70*5e7646d2SAndroid Build Coastguard Worker 
71*5e7646d2SAndroid Build Coastguard Worker   snprintf(directory, sizeof(directory), "%s/help", docroot);
72*5e7646d2SAndroid Build Coastguard Worker 
73*5e7646d2SAndroid Build Coastguard Worker   fprintf(stderr, "DEBUG: helpLoadIndex(filename=\"%s\", directory=\"%s\")\n",
74*5e7646d2SAndroid Build Coastguard Worker           filename, directory);
75*5e7646d2SAndroid Build Coastguard Worker 
76*5e7646d2SAndroid Build Coastguard Worker   hi = helpLoadIndex(filename, directory);
77*5e7646d2SAndroid Build Coastguard Worker   if (!hi)
78*5e7646d2SAndroid Build Coastguard Worker   {
79*5e7646d2SAndroid Build Coastguard Worker     perror(filename);
80*5e7646d2SAndroid Build Coastguard Worker 
81*5e7646d2SAndroid Build Coastguard Worker     cgiStartHTML(cgiText(_("Online Help")));
82*5e7646d2SAndroid Build Coastguard Worker     cgiSetVariable("ERROR", cgiText(_("Unable to load help index.")));
83*5e7646d2SAndroid Build Coastguard Worker     cgiCopyTemplateLang("error.tmpl");
84*5e7646d2SAndroid Build Coastguard Worker     cgiEndHTML();
85*5e7646d2SAndroid Build Coastguard Worker 
86*5e7646d2SAndroid Build Coastguard Worker     return (1);
87*5e7646d2SAndroid Build Coastguard Worker   }
88*5e7646d2SAndroid Build Coastguard Worker 
89*5e7646d2SAndroid Build Coastguard Worker   fprintf(stderr, "DEBUG: %d nodes in help index...\n",
90*5e7646d2SAndroid Build Coastguard Worker           cupsArrayCount(hi->nodes));
91*5e7646d2SAndroid Build Coastguard Worker 
92*5e7646d2SAndroid Build Coastguard Worker  /*
93*5e7646d2SAndroid Build Coastguard Worker   * See if we are viewing a file...
94*5e7646d2SAndroid Build Coastguard Worker   */
95*5e7646d2SAndroid Build Coastguard Worker 
96*5e7646d2SAndroid Build Coastguard Worker   for (i = 0; i < argc; i ++)
97*5e7646d2SAndroid Build Coastguard Worker     fprintf(stderr, "DEBUG: argv[%d]=\"%s\"\n", i, argv[i]);
98*5e7646d2SAndroid Build Coastguard Worker 
99*5e7646d2SAndroid Build Coastguard Worker   if ((helpfile = getenv("PATH_INFO")) != NULL)
100*5e7646d2SAndroid Build Coastguard Worker   {
101*5e7646d2SAndroid Build Coastguard Worker     helpfile ++;
102*5e7646d2SAndroid Build Coastguard Worker 
103*5e7646d2SAndroid Build Coastguard Worker     if (!*helpfile)
104*5e7646d2SAndroid Build Coastguard Worker       helpfile = NULL;
105*5e7646d2SAndroid Build Coastguard Worker   }
106*5e7646d2SAndroid Build Coastguard Worker 
107*5e7646d2SAndroid Build Coastguard Worker   if (helpfile)
108*5e7646d2SAndroid Build Coastguard Worker   {
109*5e7646d2SAndroid Build Coastguard Worker    /*
110*5e7646d2SAndroid Build Coastguard Worker     * Verify that the help file exists and is part of the index...
111*5e7646d2SAndroid Build Coastguard Worker     */
112*5e7646d2SAndroid Build Coastguard Worker 
113*5e7646d2SAndroid Build Coastguard Worker     snprintf(filename, sizeof(filename), "%s/help/%s", docroot, helpfile);
114*5e7646d2SAndroid Build Coastguard Worker 
115*5e7646d2SAndroid Build Coastguard Worker     fprintf(stderr, "DEBUG: helpfile=\"%s\", filename=\"%s\"\n",
116*5e7646d2SAndroid Build Coastguard Worker             helpfile, filename);
117*5e7646d2SAndroid Build Coastguard Worker 
118*5e7646d2SAndroid Build Coastguard Worker     if (access(filename, R_OK))
119*5e7646d2SAndroid Build Coastguard Worker     {
120*5e7646d2SAndroid Build Coastguard Worker       perror(filename);
121*5e7646d2SAndroid Build Coastguard Worker 
122*5e7646d2SAndroid Build Coastguard Worker       cgiStartHTML(cgiText(_("Online Help")));
123*5e7646d2SAndroid Build Coastguard Worker       cgiSetVariable("ERROR", cgiText(_("Unable to access help file.")));
124*5e7646d2SAndroid Build Coastguard Worker       cgiCopyTemplateLang("error.tmpl");
125*5e7646d2SAndroid Build Coastguard Worker       cgiEndHTML();
126*5e7646d2SAndroid Build Coastguard Worker 
127*5e7646d2SAndroid Build Coastguard Worker       return (1);
128*5e7646d2SAndroid Build Coastguard Worker     }
129*5e7646d2SAndroid Build Coastguard Worker 
130*5e7646d2SAndroid Build Coastguard Worker     if ((n = helpFindNode(hi, helpfile, NULL)) == NULL)
131*5e7646d2SAndroid Build Coastguard Worker     {
132*5e7646d2SAndroid Build Coastguard Worker       cgiStartHTML(cgiText(_("Online Help")));
133*5e7646d2SAndroid Build Coastguard Worker       cgiSetVariable("ERROR", cgiText(_("Help file not in index.")));
134*5e7646d2SAndroid Build Coastguard Worker       cgiCopyTemplateLang("error.tmpl");
135*5e7646d2SAndroid Build Coastguard Worker       cgiEndHTML();
136*5e7646d2SAndroid Build Coastguard Worker 
137*5e7646d2SAndroid Build Coastguard Worker       return (1);
138*5e7646d2SAndroid Build Coastguard Worker     }
139*5e7646d2SAndroid Build Coastguard Worker 
140*5e7646d2SAndroid Build Coastguard Worker    /*
141*5e7646d2SAndroid Build Coastguard Worker     * Save the page title and help file...
142*5e7646d2SAndroid Build Coastguard Worker     */
143*5e7646d2SAndroid Build Coastguard Worker 
144*5e7646d2SAndroid Build Coastguard Worker     helptitle = n->text;
145*5e7646d2SAndroid Build Coastguard Worker     topic     = n->section;
146*5e7646d2SAndroid Build Coastguard Worker 
147*5e7646d2SAndroid Build Coastguard Worker    /*
148*5e7646d2SAndroid Build Coastguard Worker     * Send a standard page header...
149*5e7646d2SAndroid Build Coastguard Worker     */
150*5e7646d2SAndroid Build Coastguard Worker 
151*5e7646d2SAndroid Build Coastguard Worker     if (printable)
152*5e7646d2SAndroid Build Coastguard Worker       puts("Content-Type: text/html;charset=utf-8\n");
153*5e7646d2SAndroid Build Coastguard Worker     else
154*5e7646d2SAndroid Build Coastguard Worker       cgiStartHTML(n->text);
155*5e7646d2SAndroid Build Coastguard Worker   }
156*5e7646d2SAndroid Build Coastguard Worker   else
157*5e7646d2SAndroid Build Coastguard Worker   {
158*5e7646d2SAndroid Build Coastguard Worker    /*
159*5e7646d2SAndroid Build Coastguard Worker     * Send a standard page header...
160*5e7646d2SAndroid Build Coastguard Worker     */
161*5e7646d2SAndroid Build Coastguard Worker 
162*5e7646d2SAndroid Build Coastguard Worker     cgiStartHTML(cgiText(_("Online Help")));
163*5e7646d2SAndroid Build Coastguard Worker 
164*5e7646d2SAndroid Build Coastguard Worker     topic = cgiGetVariable("TOPIC");
165*5e7646d2SAndroid Build Coastguard Worker   }
166*5e7646d2SAndroid Build Coastguard Worker 
167*5e7646d2SAndroid Build Coastguard Worker  /*
168*5e7646d2SAndroid Build Coastguard Worker   * Do a search as needed...
169*5e7646d2SAndroid Build Coastguard Worker   */
170*5e7646d2SAndroid Build Coastguard Worker 
171*5e7646d2SAndroid Build Coastguard Worker   if (cgiGetVariable("CLEAR"))
172*5e7646d2SAndroid Build Coastguard Worker     cgiSetVariable("QUERY", "");
173*5e7646d2SAndroid Build Coastguard Worker 
174*5e7646d2SAndroid Build Coastguard Worker   query = cgiGetVariable("QUERY");
175*5e7646d2SAndroid Build Coastguard Worker   si    = helpSearchIndex(hi, query, topic, helpfile);
176*5e7646d2SAndroid Build Coastguard Worker 
177*5e7646d2SAndroid Build Coastguard Worker   cgiClearVariables();
178*5e7646d2SAndroid Build Coastguard Worker   if (query)
179*5e7646d2SAndroid Build Coastguard Worker     cgiSetVariable("QUERY", query);
180*5e7646d2SAndroid Build Coastguard Worker   if (topic)
181*5e7646d2SAndroid Build Coastguard Worker     cgiSetVariable("TOPIC", topic);
182*5e7646d2SAndroid Build Coastguard Worker   if (helpfile)
183*5e7646d2SAndroid Build Coastguard Worker     cgiSetVariable("HELPFILE", helpfile);
184*5e7646d2SAndroid Build Coastguard Worker   if (helptitle)
185*5e7646d2SAndroid Build Coastguard Worker     cgiSetVariable("HELPTITLE", helptitle);
186*5e7646d2SAndroid Build Coastguard Worker 
187*5e7646d2SAndroid Build Coastguard Worker   fprintf(stderr, "DEBUG: query=\"%s\", topic=\"%s\"\n",
188*5e7646d2SAndroid Build Coastguard Worker           query ? query : "(null)", topic ? topic : "(null)");
189*5e7646d2SAndroid Build Coastguard Worker 
190*5e7646d2SAndroid Build Coastguard Worker   if (si)
191*5e7646d2SAndroid Build Coastguard Worker   {
192*5e7646d2SAndroid Build Coastguard Worker     help_node_t	*nn;			/* Parent node */
193*5e7646d2SAndroid Build Coastguard Worker 
194*5e7646d2SAndroid Build Coastguard Worker 
195*5e7646d2SAndroid Build Coastguard Worker     fprintf(stderr,
196*5e7646d2SAndroid Build Coastguard Worker             "DEBUG: si=%p, si->sorted=%p, cupsArrayCount(si->sorted)=%d\n", si,
197*5e7646d2SAndroid Build Coastguard Worker             si->sorted, cupsArrayCount(si->sorted));
198*5e7646d2SAndroid Build Coastguard Worker 
199*5e7646d2SAndroid Build Coastguard Worker     for (i = 0, n = (help_node_t *)cupsArrayFirst(si->sorted);
200*5e7646d2SAndroid Build Coastguard Worker          n;
201*5e7646d2SAndroid Build Coastguard Worker 	 i ++, n = (help_node_t *)cupsArrayNext(si->sorted))
202*5e7646d2SAndroid Build Coastguard Worker     {
203*5e7646d2SAndroid Build Coastguard Worker       if (helpfile && n->anchor)
204*5e7646d2SAndroid Build Coastguard Worker         snprintf(line, sizeof(line), "#%s", n->anchor);
205*5e7646d2SAndroid Build Coastguard Worker       else if (n->anchor)
206*5e7646d2SAndroid Build Coastguard Worker         snprintf(line, sizeof(line), "/help/%s?QUERY=%s#%s", n->filename,
207*5e7646d2SAndroid Build Coastguard Worker 	         query ? query : "", n->anchor);
208*5e7646d2SAndroid Build Coastguard Worker       else
209*5e7646d2SAndroid Build Coastguard Worker         snprintf(line, sizeof(line), "/help/%s?QUERY=%s", n->filename,
210*5e7646d2SAndroid Build Coastguard Worker 	         query ? query : "");
211*5e7646d2SAndroid Build Coastguard Worker 
212*5e7646d2SAndroid Build Coastguard Worker       cgiSetArray("QTEXT", i, n->text);
213*5e7646d2SAndroid Build Coastguard Worker       cgiSetArray("QLINK", i, line);
214*5e7646d2SAndroid Build Coastguard Worker 
215*5e7646d2SAndroid Build Coastguard Worker       if (!helpfile && n->anchor)
216*5e7646d2SAndroid Build Coastguard Worker       {
217*5e7646d2SAndroid Build Coastguard Worker         nn = helpFindNode(hi, n->filename, NULL);
218*5e7646d2SAndroid Build Coastguard Worker 
219*5e7646d2SAndroid Build Coastguard Worker         snprintf(line, sizeof(line), "/help/%s?QUERY=%s", nn->filename,
220*5e7646d2SAndroid Build Coastguard Worker 	         query ? query : "");
221*5e7646d2SAndroid Build Coastguard Worker 
222*5e7646d2SAndroid Build Coastguard Worker         cgiSetArray("QPTEXT", i, nn->text);
223*5e7646d2SAndroid Build Coastguard Worker 	cgiSetArray("QPLINK", i, line);
224*5e7646d2SAndroid Build Coastguard Worker       }
225*5e7646d2SAndroid Build Coastguard Worker       else
226*5e7646d2SAndroid Build Coastguard Worker       {
227*5e7646d2SAndroid Build Coastguard Worker         cgiSetArray("QPTEXT", i, "");
228*5e7646d2SAndroid Build Coastguard Worker 	cgiSetArray("QPLINK", i, "");
229*5e7646d2SAndroid Build Coastguard Worker       }
230*5e7646d2SAndroid Build Coastguard Worker 
231*5e7646d2SAndroid Build Coastguard Worker       fprintf(stderr, "DEBUG: [%d] = \"%s\" @ \"%s\"\n", i, n->text, line);
232*5e7646d2SAndroid Build Coastguard Worker     }
233*5e7646d2SAndroid Build Coastguard Worker 
234*5e7646d2SAndroid Build Coastguard Worker     helpDeleteIndex(si);
235*5e7646d2SAndroid Build Coastguard Worker   }
236*5e7646d2SAndroid Build Coastguard Worker 
237*5e7646d2SAndroid Build Coastguard Worker  /*
238*5e7646d2SAndroid Build Coastguard Worker   * OK, now list the bookmarks within the index...
239*5e7646d2SAndroid Build Coastguard Worker   */
240*5e7646d2SAndroid Build Coastguard Worker 
241*5e7646d2SAndroid Build Coastguard Worker   for (i = 0, section = NULL, n = (help_node_t *)cupsArrayFirst(hi->sorted);
242*5e7646d2SAndroid Build Coastguard Worker        n;
243*5e7646d2SAndroid Build Coastguard Worker        n = (help_node_t *)cupsArrayNext(hi->sorted))
244*5e7646d2SAndroid Build Coastguard Worker   {
245*5e7646d2SAndroid Build Coastguard Worker     if (n->anchor)
246*5e7646d2SAndroid Build Coastguard Worker       continue;
247*5e7646d2SAndroid Build Coastguard Worker 
248*5e7646d2SAndroid Build Coastguard Worker    /*
249*5e7646d2SAndroid Build Coastguard Worker     * Add a section link as needed...
250*5e7646d2SAndroid Build Coastguard Worker     */
251*5e7646d2SAndroid Build Coastguard Worker 
252*5e7646d2SAndroid Build Coastguard Worker     if (n->section &&
253*5e7646d2SAndroid Build Coastguard Worker         (!section || strcmp(n->section, section)))
254*5e7646d2SAndroid Build Coastguard Worker     {
255*5e7646d2SAndroid Build Coastguard Worker      /*
256*5e7646d2SAndroid Build Coastguard Worker       * Add a link for this node...
257*5e7646d2SAndroid Build Coastguard Worker       */
258*5e7646d2SAndroid Build Coastguard Worker 
259*5e7646d2SAndroid Build Coastguard Worker       snprintf(line, sizeof(line), "/help/?TOPIC=%s&QUERY=%s",
260*5e7646d2SAndroid Build Coastguard Worker                cgiFormEncode(topic_data, n->section, sizeof(topic_data)),
261*5e7646d2SAndroid Build Coastguard Worker 	       query ? query : "");
262*5e7646d2SAndroid Build Coastguard Worker       cgiSetArray("BMLINK", i, line);
263*5e7646d2SAndroid Build Coastguard Worker       cgiSetArray("BMTEXT", i, n->section);
264*5e7646d2SAndroid Build Coastguard Worker       cgiSetArray("BMINDENT", i, "0");
265*5e7646d2SAndroid Build Coastguard Worker 
266*5e7646d2SAndroid Build Coastguard Worker       i ++;
267*5e7646d2SAndroid Build Coastguard Worker       section = n->section;
268*5e7646d2SAndroid Build Coastguard Worker     }
269*5e7646d2SAndroid Build Coastguard Worker 
270*5e7646d2SAndroid Build Coastguard Worker     if (!topic || !n->section || strcmp(n->section, topic))
271*5e7646d2SAndroid Build Coastguard Worker       continue;
272*5e7646d2SAndroid Build Coastguard Worker 
273*5e7646d2SAndroid Build Coastguard Worker    /*
274*5e7646d2SAndroid Build Coastguard Worker     * Add a link for this node...
275*5e7646d2SAndroid Build Coastguard Worker     */
276*5e7646d2SAndroid Build Coastguard Worker 
277*5e7646d2SAndroid Build Coastguard Worker     snprintf(line, sizeof(line), "/help/%s?TOPIC=%s&QUERY=%s", n->filename,
278*5e7646d2SAndroid Build Coastguard Worker              cgiFormEncode(topic_data, n->section, sizeof(topic_data)),
279*5e7646d2SAndroid Build Coastguard Worker 	     query ? query : "");
280*5e7646d2SAndroid Build Coastguard Worker     cgiSetArray("BMLINK", i, line);
281*5e7646d2SAndroid Build Coastguard Worker     cgiSetArray("BMTEXT", i, n->text);
282*5e7646d2SAndroid Build Coastguard Worker     cgiSetArray("BMINDENT", i, "1");
283*5e7646d2SAndroid Build Coastguard Worker 
284*5e7646d2SAndroid Build Coastguard Worker     i ++;
285*5e7646d2SAndroid Build Coastguard Worker 
286*5e7646d2SAndroid Build Coastguard Worker     if (helpfile && !strcmp(helpfile, n->filename))
287*5e7646d2SAndroid Build Coastguard Worker     {
288*5e7646d2SAndroid Build Coastguard Worker       help_node_t	*nn;		/* Pointer to sub-node */
289*5e7646d2SAndroid Build Coastguard Worker 
290*5e7646d2SAndroid Build Coastguard Worker 
291*5e7646d2SAndroid Build Coastguard Worker       cupsArraySave(hi->sorted);
292*5e7646d2SAndroid Build Coastguard Worker 
293*5e7646d2SAndroid Build Coastguard Worker       for (nn = (help_node_t *)cupsArrayFirst(hi->sorted);
294*5e7646d2SAndroid Build Coastguard Worker            nn;
295*5e7646d2SAndroid Build Coastguard Worker 	   nn = (help_node_t *)cupsArrayNext(hi->sorted))
296*5e7646d2SAndroid Build Coastguard Worker         if (nn->anchor && !strcmp(helpfile, nn->filename))
297*5e7646d2SAndroid Build Coastguard Worker 	{
298*5e7646d2SAndroid Build Coastguard Worker 	 /*
299*5e7646d2SAndroid Build Coastguard Worker 	  * Add a link for this node...
300*5e7646d2SAndroid Build Coastguard Worker 	  */
301*5e7646d2SAndroid Build Coastguard Worker 
302*5e7646d2SAndroid Build Coastguard Worker 	  snprintf(line, sizeof(line), "#%s", nn->anchor);
303*5e7646d2SAndroid Build Coastguard Worker 	  cgiSetArray("BMLINK", i, line);
304*5e7646d2SAndroid Build Coastguard Worker 	  cgiSetArray("BMTEXT", i, nn->text);
305*5e7646d2SAndroid Build Coastguard Worker 	  cgiSetArray("BMINDENT", i, "2");
306*5e7646d2SAndroid Build Coastguard Worker 
307*5e7646d2SAndroid Build Coastguard Worker 	  i ++;
308*5e7646d2SAndroid Build Coastguard Worker 	}
309*5e7646d2SAndroid Build Coastguard Worker 
310*5e7646d2SAndroid Build Coastguard Worker       cupsArrayRestore(hi->sorted);
311*5e7646d2SAndroid Build Coastguard Worker     }
312*5e7646d2SAndroid Build Coastguard Worker   }
313*5e7646d2SAndroid Build Coastguard Worker 
314*5e7646d2SAndroid Build Coastguard Worker  /*
315*5e7646d2SAndroid Build Coastguard Worker   * Show the search and bookmark content...
316*5e7646d2SAndroid Build Coastguard Worker   */
317*5e7646d2SAndroid Build Coastguard Worker 
318*5e7646d2SAndroid Build Coastguard Worker   if (!helpfile || !printable)
319*5e7646d2SAndroid Build Coastguard Worker     cgiCopyTemplateLang("help-header.tmpl");
320*5e7646d2SAndroid Build Coastguard Worker   else
321*5e7646d2SAndroid Build Coastguard Worker     cgiCopyTemplateLang("help-printable.tmpl");
322*5e7646d2SAndroid Build Coastguard Worker 
323*5e7646d2SAndroid Build Coastguard Worker  /*
324*5e7646d2SAndroid Build Coastguard Worker   * If we are viewing a file, copy it in now...
325*5e7646d2SAndroid Build Coastguard Worker   */
326*5e7646d2SAndroid Build Coastguard Worker 
327*5e7646d2SAndroid Build Coastguard Worker   if (helpfile)
328*5e7646d2SAndroid Build Coastguard Worker   {
329*5e7646d2SAndroid Build Coastguard Worker     if ((fp = cupsFileOpen(filename, "r")) != NULL)
330*5e7646d2SAndroid Build Coastguard Worker     {
331*5e7646d2SAndroid Build Coastguard Worker       int	inbody;			/* Are we inside the body? */
332*5e7646d2SAndroid Build Coastguard Worker       char	*lineptr;		/* Pointer into line */
333*5e7646d2SAndroid Build Coastguard Worker 
334*5e7646d2SAndroid Build Coastguard Worker       inbody = 0;
335*5e7646d2SAndroid Build Coastguard Worker 
336*5e7646d2SAndroid Build Coastguard Worker       while (cupsFileGets(fp, line, sizeof(line)))
337*5e7646d2SAndroid Build Coastguard Worker       {
338*5e7646d2SAndroid Build Coastguard Worker         for (lineptr = line; *lineptr && isspace(*lineptr & 255); lineptr ++);
339*5e7646d2SAndroid Build Coastguard Worker 
340*5e7646d2SAndroid Build Coastguard Worker         if (inbody)
341*5e7646d2SAndroid Build Coastguard Worker 	{
342*5e7646d2SAndroid Build Coastguard Worker 	  if (!_cups_strncasecmp(lineptr, "</BODY>", 7))
343*5e7646d2SAndroid Build Coastguard Worker 	    break;
344*5e7646d2SAndroid Build Coastguard Worker 
345*5e7646d2SAndroid Build Coastguard Worker 	  printf("%s\n", line);
346*5e7646d2SAndroid Build Coastguard Worker         }
347*5e7646d2SAndroid Build Coastguard Worker 	else if (!_cups_strncasecmp(lineptr, "<BODY", 5))
348*5e7646d2SAndroid Build Coastguard Worker 	  inbody = 1;
349*5e7646d2SAndroid Build Coastguard Worker       }
350*5e7646d2SAndroid Build Coastguard Worker 
351*5e7646d2SAndroid Build Coastguard Worker       cupsFileClose(fp);
352*5e7646d2SAndroid Build Coastguard Worker     }
353*5e7646d2SAndroid Build Coastguard Worker     else
354*5e7646d2SAndroid Build Coastguard Worker     {
355*5e7646d2SAndroid Build Coastguard Worker       perror(filename);
356*5e7646d2SAndroid Build Coastguard Worker       cgiSetVariable("ERROR", cgiText(_("Unable to open help file.")));
357*5e7646d2SAndroid Build Coastguard Worker       cgiCopyTemplateLang("error.tmpl");
358*5e7646d2SAndroid Build Coastguard Worker     }
359*5e7646d2SAndroid Build Coastguard Worker   }
360*5e7646d2SAndroid Build Coastguard Worker 
361*5e7646d2SAndroid Build Coastguard Worker  /*
362*5e7646d2SAndroid Build Coastguard Worker   * Send a standard trailer...
363*5e7646d2SAndroid Build Coastguard Worker   */
364*5e7646d2SAndroid Build Coastguard Worker 
365*5e7646d2SAndroid Build Coastguard Worker   if (!printable)
366*5e7646d2SAndroid Build Coastguard Worker   {
367*5e7646d2SAndroid Build Coastguard Worker     cgiCopyTemplateLang("help-trailer.tmpl");
368*5e7646d2SAndroid Build Coastguard Worker     cgiEndHTML();
369*5e7646d2SAndroid Build Coastguard Worker   }
370*5e7646d2SAndroid Build Coastguard Worker   else
371*5e7646d2SAndroid Build Coastguard Worker     puts("</BODY>\n</HTML>");
372*5e7646d2SAndroid Build Coastguard Worker 
373*5e7646d2SAndroid Build Coastguard Worker  /*
374*5e7646d2SAndroid Build Coastguard Worker   * Delete the index...
375*5e7646d2SAndroid Build Coastguard Worker   */
376*5e7646d2SAndroid Build Coastguard Worker 
377*5e7646d2SAndroid Build Coastguard Worker   helpDeleteIndex(hi);
378*5e7646d2SAndroid Build Coastguard Worker 
379*5e7646d2SAndroid Build Coastguard Worker  /*
380*5e7646d2SAndroid Build Coastguard Worker   * Return with no errors...
381*5e7646d2SAndroid Build Coastguard Worker   */
382*5e7646d2SAndroid Build Coastguard Worker 
383*5e7646d2SAndroid Build Coastguard Worker   return (0);
384*5e7646d2SAndroid Build Coastguard Worker }
385