summaryrefslogtreecommitdiff
path: root/src/info2html/main.c
blob: 38fd9fe29f2d05b274c428cc4ae4f22231fb28e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* little test main() to see how we're doing */

/* modifications to support : 
   1. command-line of the form filename?section for yelp support
   2. outputs only "Top" section of document if no node / section is specified
   3. links modified to be of the form "info://filename?section"
   
   - Patanjali 
*/

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <popt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>

#include "data.h"
#include "html.h"
#include "parse.h"
#include "utils.h"
#include "version.h"

/* be quiet or not? */
static int be_quiet=1;

char top_string [] = "Top";

/* line_number we're on */
static int work_line_number;

static char *requested_nodename=NULL;
static char *requested_section=NULL;
static struct poptOption options[] = {
        {NULL, 'a', POPT_ARG_STRING, &requested_nodename},
        {NULL, 'b', POPT_ARG_STRING, &OverrideBaseFilename},
        {NULL, 'g', POPT_ARG_NONE, &galeon_mode},
        {NULL}
};

static int
file_exists(const char *fn)
{
        struct stat sbuf;

	if (g_file_test(fn, G_FILE_TEST_IS_DIR)) {
		return FALSE;
	}

	if (g_file_test(fn, G_FILE_TEST_IS_DIR)) {
		return FALSE;
	}

        return (stat(fn, &sbuf) == 0);
}

int
main(int argc, const char **argv)
{
        ReadBuf *f=NULL;
	char line[250];
	poptContext ctx;
	int result;
	int foundit=0;
	int i, n;
	char *cptr;
        gboolean no_info = FALSE;

	char convanc[1024];
	NODE *node;

	const char **args;
	char *fixup_args[512];
	int curarg;
	
	if (!be_quiet)
		printf("info2html Version %s\n",INFO2HTML_VERSION);

	ctx = poptGetContext("gnome-info2html2", argc, argv, options, 0); 

	while(poptGetNextOpt(ctx) >= 0)
                /**/ ;

	args = poptGetArgs(ctx);
	curarg = 0;
	if(!args)
                return 1;

	for(n = 0; args[n]; n++) /* */;

	/* hack to convert the first argument to the form : 
	   filename?section instead of passing it in with the 
	   -a option */
	for (cptr = args [0]; *cptr != '\0'; cptr++) {
                if (*cptr == '?') {
                        *cptr++ = '\0';
                        requested_section = g_strdup (cptr);
                        break;
                }

	} 
	/* requested_section now contains the requested section, if at all ... */
	if(n == 1 && !file_exists(args[0])) {
                /* As strtok destroys the string it parses and g_getenv returns a pointer to
                   the actually env var, we have to duplicate the var before parsing it. */
                char *ctmp, *infopath = g_strdup(g_getenv("INFOPATH"));
                char *dirs[64], *ext = NULL;
                int ndirs;
                char buf[PATH_MAX];

                /* First, find the directory that the info file is in. */
                dirs[0] = "/usr/info";
                dirs[1] = "/usr/share/info";
                /* We now have at least one directory to look in. This is
                 * necessary because we may not have an 'INFOPATH' set */
                ndirs = 2;
                if(infopath) {
                        for (ndirs = 2, ctmp = strtok(infopath, ":"); ndirs < 64 && ctmp; ndirs++, ctmp = strtok(NULL, ":")) {
                                dirs[ndirs] = strdup(ctmp);
                        }
                }

                for(i = 0; i < ndirs; i++) {
                        ext = "";
                        sprintf(buf, "%s/%s.info", dirs[i], args[0]);
                        if(file_exists(buf))
                                break;
                        sprintf(buf, "%s/%s", dirs[i], args[0]);
                        if(file_exists(buf)) {
                                no_info = TRUE;
                                break;
                        }
                  
                        ext = ".gz";
                        sprintf(buf, "%s/%s.info.gz", dirs[i], args[0]);
                        if(file_exists(buf))
                                break;
                        sprintf(buf, "%s/%s.gz", dirs[i], args[0]);
                        if(file_exists(buf)) {
                                no_info = TRUE;
                                break;
                        }
#ifdef HAVE_LIBBZ2
                        ext = ".bz2";
                        sprintf(buf, "%s/%s.info.bz2", dirs[i], args[0]);
                        if(file_exists(buf))
                                break;

                        sprintf(buf, "%s/%s.bz2", dirs[i], args[0]);
                        if(file_exists(buf)) {
                                no_info = TRUE;
                                break;
                        }
#endif
                }
                if(i >= ndirs) {
                        printf ("<HTML><HEAD><TITLE>Document not found</TITLE>\n"
                                "</HEAD><BODY>The info document \"%s\" could not be found. It may have been removed from your system.\n"
                                "</BODY></HTML>\n", args[0]);
                        return 2;
                }

                n = i;

                for(i = 0; ; i++) {
                        gchar *path;
                        
                        if (no_info) {
                                path = g_strdup_printf ("%s/%s", 
                                                        dirs[n], args[0]);
                        } else {
                                path = g_strdup_printf ("%s/%s.info",
                                                        dirs[n], args[0]);
                        } 

                        if(i) { 
                                sprintf(buf, "%s-%d%s", path, i, ext);
                        } else {
                                sprintf(buf, "%s%s", path, ext);
                        }

                        if(!file_exists(buf)) {
                                fixup_args[i] = NULL;
                                break;
                        }

                        fixup_args[i] = strdup(buf);
                }
                args = (const char **)fixup_args;
        }
	
	if(requested_nodename) {
                char *s, *t;
                int  len;
                /* strip off quotes */
                for (s=requested_nodename; *s == '\"'; ) {
                        len = strlen( s );
                        memmove(s, s+1, len);
                }

                t = s + strlen(s) - 1;
                while (*t == '\"') {
                        t--;
                }

                *(t+1) = '\0';

                /* convert anchor so matching works */
                map_spaces_to_underscores(requested_nodename);
        } else {
                /* since no node has been asked for, it might have been passed in 
                   as filename?section. In that case, set requested_nodename
                   to what is in requested_section, if that is NULL, set requested_nodename
                   to "Top" to display only the "Top" Node */
                if (requested_section) {
                        requested_nodename = requested_section;
                } else {
                        requested_nodename = top_string;
                }
        }
	work_line_number = 0;

	/* hack, just send to stdout for now */
	g_print("<HTML><HEAD><TITLE>Info page for \"%s\"</TITLE></BODY><BODY>\n", argv[1]);
	
	/* big loop to identify sections of info files */
	/* NEW PLAN - format on the fly */
	/* No need to store all nodes, etc since we let web server */
	/* handle resolving tags!                                  */
	for (;1 || !foundit || !requested_nodename;) {
                if(!f) {
                        if(args && args[curarg]) {
                                f = readbuf_open (args[curarg++]);
                                if(!f) {
                                        break;
                                }
                                num_files_left = args[curarg]?1:0;
                                for (work_line_number = 0, readbuf_gets(f,line,sizeof(line)); *line != INFO_COOKIE;
                                    readbuf_gets(f,line,sizeof(line)), work_line_number++)
                                        /**/ ;
                        } else {
                                break;
                        }
                }
                if(!readbuf_gets(f,line,sizeof(line))) {
                        readbuf_close(f);
                        f = NULL;
                        continue;
                }
	
                work_line_number++;

		/* found a node definition line */
                if (!strncmp(line, "File:", 5)) {
                        node = alloc_node();
                        result=read_node( f, line, node );
                        if ( result == READ_ERR ) {
                                fprintf(stderr, "Error reading the node "
                                        "contents\n");
                                fprintf(stderr, "line was |%s|\n",line);
                                continue;
                        }
			
                        /* see if this is the requested node name */
                        strncpy(convanc, node->nodename, sizeof(convanc));
                        map_spaces_to_underscores(convanc);
                        if (requested_nodename && 
                            strcmp(requested_nodename, convanc)) {
#ifdef DEBUG			    
                                fprintf(stderr, "skipping ->%s<-\n",
                                        node->nodename);
#endif				

                                continue;
                        }

                        foundit = 1;
                        strcpy(work_node,node->nodename);

                        BaseFilename = node->filename;
#ifdef DEBUG
                        printf("NEW NODE\n");
                        printf("\tFile:|%s|\n\tNode:|%s|\n\tNext:|%s|\n",
                               node->filename, node->nodename,node->next);
                        printf("\tPrev:|%s|\n\tUp:|%s|\n\n", 
                               node->prev, node->up);
                        printf("-------------------------------------------"
                               "-----------\n");
#endif
                        /* now lets make some html */
                        dump_html_for_node( node );
			
                        if (node) {
                                if ( node->contents )
                                        free(node->contents);
				
                                g_free(node);
                                BaseFilename = NULL;
                        }
                } else {
                        continue;
                }
	}
	
	if (!foundit && requested_nodename) {
                fprintf(stderr, "Requested node <b>%s</b> not found\n",
                        requested_nodename);
                exit(1);
	}

	fprintf(stdout, "</BODY></HTML>\n");
	return 0;
}