summaryrefslogtreecommitdiff
path: root/src/info2html/main.c
blob: 7f5a1d7a8d0fcad7cdc4af8dbc8a06c9f76336e4 (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
/* little test main() to see how we're doing */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gnome.h>
#include <zlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.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;

/* line_number we're on */
static int work_line_number;
static char *requested_nodename=NULL;
static struct poptOption options[] = {
  {NULL, 'a', POPT_ARG_STRING, &requested_nodename},
  {NULL, 'b', POPT_ARG_STRING, &OverrideBaseFilename},
  {NULL}
};

static int
file_exists(const char *fn)
{
  struct stat sbuf;
  return (stat(fn, &sbuf) == 0);
}

int
main(int argc, char **argv)
{
	gzFile f = NULL;
	char line[250];
	poptContext ctx;
	int result;
	int foundit=0;
	int i, n;

	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++) /* */;
	if(n == 1 && !file_exists(args[0]))
	  {
	    char *ctmp, *infopath = 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((char *)args[0], ":"); 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;
		ext = ".gz";
		sprintf(buf, "%s/%s.info.gz", dirs[i], args[0]);
		if(file_exists(buf))
		  break;
	      }
	    if(i >= ndirs)
	      return 2;

	    n = i;

	    for(i = 0; ; i++)
	      {
		if(i)
		  sprintf(buf, "%s/%s.info-%d%s", dirs[n], args[0], i, ext);
		else
		  sprintf(buf, "%s/%s.info%s", dirs[n], args[0], 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);
	  }

	work_line_number = 0;

	/* hack, just send to stdout for now */
	fprintf(stdout, "<BODY><HTML>\n");
	
	/* 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 = gzopen(args[curarg++], "r");
		if(!f)
		  break;
		num_files_left = args[curarg]?1:0;
		for(work_line_number = 0, gzgets(f, line, sizeof(line)); *line != INFO_COOKIE;
		    gzgets(f, line, sizeof(line)), work_line_number++)
		  /**/ ;
	      }
	    else
	      break;
	  }
	  if(!gzgets(f, line, sizeof(line)))
	    {
	      gzclose(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;
}