Codebase list gman / 19c4ffe3-3c6e-4556-9607-da4c2fbc66eb/main mandata.c
19c4ffe3-3c6e-4556-9607-da4c2fbc66eb/main

Tree @19c4ffe3-3c6e-4556-9607-da4c2fbc66eb/main (Download .tar.gz)

mandata.c @19c4ffe3-3c6e-4556-9607-da4c2fbc66eb/mainraw · history · blame

  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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/********************** modified from t2.c *******************/
/********************* kman.c ********************************/

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#ifdef SYS_DIR
#include <sys_dir.h>
#else
#ifdef NDIR

#include <ndir.h>
#else
#include <dirent.h>
#endif
#endif

#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>

#include "mandata.h"
#include "mandatadef.h"
#include "util.h"
#include "context.h"
#include "gman.h"

#include <stdlib.h>
#include <glib.h>

void attach (char *dest, const char *dirname, const char *name);
static int section_translate_c_to_n(const char* c);
static int ManItemComp(ManItem ** a,ManItem ** b);
static int is_zip_suffix(char* c);

char buffer1[BUFFER_SIZE];
char buffer2[BUFFER_SIZE];

/************************* ManPath ************************/
ManPath::ManPath(char* _path_name)
{
	int i;
	int len;
	active = 1;
	path_name = my_strdup(_path_name);
	for (i=0;i<MAX_MAN_SECTION;i++) section[i] = new List();
	LoadManPath(path_name);
}

ManPath::~ManPath()
{
	int i,j,k;
	for (i = 0;i<MAX_MAN_SECTION;i++) {
		k = section[i]->get_size();
		for(j = 0;j<k;j++) delete ((ManItem*)(section[i]->get_item(j)));
		delete(section[i]);
	}
	if(path_name) free(path_name);
}

int ManPath::GetSize(int section_ID)
{
	int count;
	int i;
	for (count = i = 0;i<MAX_MAN_SECTION;i++)
		if (1<<i & section_ID) count += section[i]->get_size();
	return count;
}

int ManPath::GetItems(int section_ID, ManItem * buffer[])
{
	int count;
	int i;
	for (count = i = 0;i<MAX_MAN_SECTION;i++)
		if (1<<i & section_ID) count += section[i]->get_items((void*)(buffer + count));
	return count;
}

const char * ManPath::GetPath() {return (const char*) path_name;}

struct stat state;

int ManPath::LoadManPath(char * path_name)
{
	DIR *dirp;
	#if defined(SYS_DIR)||defined(NDIR)
		struct direct *item;
	#else
		struct dirent *item;
	#endif

	ManItem * man_item;
	int val,i;
	
	//	printf("LoadManPath: %s\n",path_name);

	dirp = opendir(path_name);
	if (!dirp)
	{
		fprintf(stderr,"Can not open man path %s\n",path_name);
		return 1;  
	}
	for (item = readdir(dirp);item != NULL;item = readdir(dirp))
	{
		if(item->d_name[0] == '.' && (item->d_name[1] == 0 ||(item->d_name[1] == '.'&&item->d_name[2]==0)))
			continue;
		attach(buffer1,path_name,item->d_name);
		//printf("LoadManPath: %s\n",name);
		val = stat (buffer1, &state);
		if (val < 0) fprintf(stderr,"error number %d, in get state %s",errno,buffer1);
		else if (S_ISDIR(state.st_mode) && !strncmp(item->d_name,"man",3)) LoadManSubPath(item->d_name);
		else
		{
			man_item = new ManItem(this,item -> d_name);
			val = man_item->get_section_ID();
			if(!val) delete(man_item);
			else{
				for(i = 0;!(1<<i & val);i++);
				section[i]->add_item(man_item);
			}
			//printf("%d\n",n);
		}
	}
	closedir(dirp);
	return 0;
}

int ManPath::LoadManSubPath(char * sub_name)
{
	DIR *dirp;
	#if defined(SYS_DIR)||defined(NDIR)
		struct direct *item;
	#else
		struct dirent *item;
	#endif

	int val,i;
	ManItem * man_item;

	attach(buffer2,path_name,sub_name);
	//printf("LoadManSubPath: %s\n",buffer2);
	dirp = opendir(buffer2);
	if (!dirp)
	{
		fprintf(stderr,"Can not open man path %s\n",buffer2);
	}
	for (item = readdir(dirp);item != NULL;item = readdir(dirp))
	{
		if(item->d_name[0] == '.' && (item->d_name[1] == 0 ||(item->d_name[1] == '.'&&item->d_name[2]==0)))
			continue;
		attach(buffer1,buffer2,item->d_name);
		//printf("LoadManSubPath: %s\n",full_name);
		val = stat (buffer1, &state);
		if (val < 0) fprintf(stderr,"error number %d, in get state %s\n",errno,buffer1);
		else if (S_ISDIR(state.st_mode)) continue;
		else
		{
			attach(buffer1,sub_name,item->d_name);
			man_item = new ManItem(this,buffer1);
			val = man_item->get_section_ID();
			if(!val) delete(man_item);
			else{
				for(i = 0;!(1<<i & val);i++);
				section[i]->add_item(man_item);
			}
		}
	}
	closedir(dirp);
}

ManItem * ManPath::search_man_item(char * name, char * sect)
{
	int i,j,k;
	int len;
	ManItem * a;
	char buffer[100];

	len = strlen(name);
	if(section) {
		j = section_translate_c_to_n(sect);
		//printf("point0.1\n");
		if(!j) return NULL;
		for(i = 0;!(1<<i & j);i++);
		//printf("point0.2, section = %x,%d: section[i] = %x\n", j,i,section[i]);
		k = section[i]->get_size();
		//printf("point0.25, k = %d\n",k);
		for(j = 0;j<k;j++) {
			a=(ManItem*)(section[i]->get_item(j));
			if(!strcmp(name,a->get_display_name(buffer))) return a;
		}
		a = NULL;
	}
	return NULL;
}

/************************* ManItem ************************/
inline ManItem::ManItem(ManPath* _man_path, char* _file_name)
{
	file_name = my_strdup(_file_name);
	man_path = _man_path;
	display_name = strrchr(file_name,'/');
	display_name = display_name? display_name + sizeof('/'):file_name;
	section_name = strrchr(display_name,'.');
	if (!section_name) { section_ID = 0; return;}
	for (;is_zip_suffix(section_name) && section_name != display_name;) 
		for(section_name--;section_name != display_name && *section_name != '.';section_name --);
	if (section_name == display_name) {section_ID = 0; return;}
	section_ID = section_translate_c_to_n(++section_name);
	//if(!section_ID) printf("%s\n",display_name);
}

inline ManItem::~ManItem()
{
	if(file_name) free(file_name);
}

char * ManItem::get_display_name(char * buffer)
{
	char * p2 = buffer;
	for(char * p = display_name;p+1 != section_name && *p != '\0';) *buffer++ = *p++;
	*buffer = '\0';
	return p2;
}

char * ManItem::get_section_name(char * buffer)
{
	char * p2 = buffer;
	for(char * p = section_name;*p != '.' && *p != '\0';) *buffer++ = *p++;
	*buffer = '\0';
	return p2;
}

int ManItem::get_section_ID() {return section_ID;}

void * ManItem::get_man_path() {return (void *)man_path;}

void ManItem::active_man_page(void)
{
	int i;
	int len;
	char buffer[2*BUFFER_SIZE];
	char loc_name[BUFFER_SIZE];
	char buffer2[BUFFER_SIZE];
	switch ((int)context->get_value("show_mode")) {
	case 0:
		sprintf(buffer,"%s -T '%s manual page' -n GMan -e man ",
				(char*)context->get_value("xterm_command"),
				get_display_name(buffer1));
		len = strlen(buffer);
		attach(buffer+len,man_path->GetPath(),file_name);
		//g_warning(buffer);
		if(!fork())
		{
			//printf(buffer);
			system(buffer);
			_exit(0);
		}
		break;
	case 1:
		sprintf(loc_name," ~/.gman.%s.ps ",get_display_name(buffer1));
		sprintf(buffer,"man -t ");
		len = strlen(buffer);
		attach(buffer+len,man_path->GetPath(),file_name);
		strcat(buffer," >> ");
		strcat(buffer,loc_name);
		strcat(buffer," ; ");
		strcat(buffer,(char*)context->get_value("gv_command"));
		strcat(buffer,loc_name);
		strcat(buffer," ; rm ");
		strcat(buffer,loc_name);
		//g_warning(buffer);
		if(!fork())
		{
			//printf(buffer);
			system(buffer);
			_exit(0);
		}
		break;
		/*
	case 2:
		sprintf(loc_name," ~/.gman.%s.html ",get_display_name(buffer1));
		if(strstr(file_name,".gz")) sprintf(buffer,"gunzip -c ");
		else sprintf(buffer,"cat ");
		len = strlen(buffer);
		attach(buffer+len,man_path->GetPath(),file_name);
		strcat(buffer," > ~/.gman.tmp ;");
		strcat(buffer," man2html ~/.gman.tmp -M ");
		strcat(buffer,(char*)context->get_value("cgi_location"));
		strcat(buffer," -H ");
		strcat(buffer,(char*)context->get_value("cgi_host"));
		strcat(buffer," >> ");
		strcat(buffer,loc_name);
		strcat(buffer," ; ");
		strcat(buffer,(char*)context->get_value("browser_command"));
		strcat(buffer,loc_name);
		strcat(buffer," ; rm ~/.gman.tmp ; sleep 120; rm ");
		strcat(buffer,loc_name);
		g_warning(buffer);
		if(!fork())
		{
			//printf(buffer);
			system(buffer);
			_exit(0);
		}
		break;
		*/
	case 2:
		sprintf(loc_name," ~/.gman.%s.html ",get_display_name(buffer1));
		//g_warning(loc_name);
		strcpy(buffer,"gman.cgi ");
		strcat(buffer," ");
		strcat(buffer,get_section_name(buffer2));
		strcat(buffer," ");
		strcat(buffer,get_display_name(buffer2));
		strcat(buffer," >> ");
		strcat(buffer,loc_name);
		strcat(buffer," ; ");
		strcat(buffer,(char*)context->get_value("browser_command"));
		//if (strstr((char*)context->get_value("browser_command"),"mozilla")) strcat(buffer," -chrome ");
		strcat(buffer,loc_name);
		strcat(buffer," ; sleep 120; rm ");
		strcat(buffer,loc_name);
		//g_warning(buffer);
		if(!fork())
		{
			//printf(buffer);
			system(buffer);
			_exit(0);
		}
		break;
	case 3:
		sprintf(buffer,(char*)context->get_value("browser_command"));
		len = strlen(buffer);
		strcat(buffer," \"http://");
		strcat(buffer,(char*)context->get_value("cgi_host"));
		strcat(buffer,(char*)context->get_value("cgi_location"));
		strcat(buffer,"?");
		strcat(buffer,get_section_name(loc_name));
		strcat(buffer,"+");
		strcat(buffer,get_display_name(loc_name));
		strcat(buffer,"\"");
		//g_warning(buffer);
		if(!fork())
		{
			//printf(buffer);
			system(buffer);
			_exit(0);
		}
		break;
	default:
		if(context->get_value("show_warning"))
			g_print("unexpected \'show_mode\' : %d ",(int)context->get_value("show_mode"));
		return;
	}
	//g_warning(buffer);
	
	
	return;
}
	
/************************* init global data ************************/

/* Put DIRNAME/NAME into DEST, handling `.' and `/' properly. */

void attach (char *dest, const char *dirname, const char *name)
{
	const char *dirnamep = dirname;

    /* Copy dirname if it is not ".". */
	if (dirname[0] != '.' || dirname[1] != 0)
	{
		while (*dirnamep) *dest++ = *dirnamep++;
		/* Add '/' if `dirname' doesn't already end with it. */
		if (dirnamep > dirname && dirnamep[-1] != '/') *dest++ = '/';
	}
	while (*name) *dest++ = *name++;
	*dest = 0;
}

int section_translate_c_to_n(const char *c)
{
	int i;
	for(i=0;sections[i].name && strncmp(sections[i].name,c,sections[i].n);i++);
	if(sections[i].name == NULL) return 0;  // no match
	return 1<<i;
}

int is_zip_suffix(char * s)
{
	int i;
	for (i=0;suffix[i].name && strncmp(suffix[i].name,s,suffix[i].n);i++);
	return suffix[i].n;
}

int man_item_compare(ManItem ** a,ManItem ** b)
{
	return (strcmp((*a)->get_display_name(buffer1),(*b)->get_display_name(buffer2)));
}

/*
int ItemMatch(char * name)
{
	int i;
	for (i=0;i<item_list_count && strcmp(name,item_list[i]->display_name)>0 ;i++);
	return (i >= item_list_count)? i-1:i;
}
*/