File: taskfunc.c

package info (click to toggle)
gman 0.9.3-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 304 kB
  • ctags: 356
  • sloc: ansic: 3,313; makefile: 63; perl: 43
file content (411 lines) | stat: -rw-r--r-- 10,498 bytes parent folder | download | duplicates (5)
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
410
411
#include <stdlib.h>
#include "taskfunc.h"
#include "menu.h"
#include "mandata.h"
#include "context.h"
#include "util.h"
#include "gman.h"
#include <string.h>
#include "window2.h"
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <errno.h>
#include <stdio.h>

//#include <gtk/gtk.h>

/********************* init_man_data *************************/
int init_man_data()
{
	char * path_name,*p1,*p2;
	int end = 0;
	man_paths = new Dictionary;
	path_name = my_strdup((char*)context->get_value("man_paths"));
	p1 = path_name;
	while(!end) {
		p2 = strchr(p1,(int)':');
		if(p2 == NULL) end++;
		else *p2 = '\0';
		man_paths->add_item(my_strdup(p1),(void*)( new ManPath(p1)));
		p1 = p2+1;
	}
	if(path_name) free (path_name);
	return 0;
}

int extract_man_data()
{
	ManItem ** buffer;
	int i,j,count;
	int display_section_ID;

	pthread_mutex_lock(&context_lock);
	switch ((long)context->get_value("display_section_policy")) {
	case 0: display_section_ID = ~0;break;
	case 1: display_section_ID = ~(long)(context->get_value("display_section"));break;
	case 2: display_section_ID = (long)context->get_value("display_section"); break;
	default: fprintf(stderr,"warning: init_man_data: \"display_section_policy\" "
					 "have invalid value %d",context->get_value("display_section_policy"));
	display_section_ID = ~0;
	}
	pthread_mutex_unlock(&context_lock);

	if(man_items_count) free(man_items_buffer);
	j = man_paths->get_size();
	for(i = man_items_count = 0;i<j;i++) 
		man_items_count += ((ManPath*)(man_paths->get_value(i)))->GetSize(display_section_ID);
	buffer = (ManItem**)malloc(sizeof(ManItem*)*man_items_count);
	man_items_buffer = (char**) buffer;
	for(i = count = 0;i<j;i++)
		count += ((ManPath*)(man_paths->get_value(i)))->GetItems(display_section_ID,buffer+count);
	if( count != man_items_count) exit(1);

	if(context->get_value("debuging")) printf("count = %d\n",count);
	qsort((void *)buffer,count,sizeof(ManItem*),(int(*)(const void*,const void*))&man_item_compare);
	if(context->get_value("debuging")) printf("count = %d, qsort end.\n",count);

	return 0;
}
	
int add_data_to_clist(int flag)
{
	gchar *text[2];
	static int i;
	int j;
	ManItem ** buffer;
	static char ** pointer;
	char a[100],b[20];

	text[0] = a; text[1] = b;
	buffer = (ManItem**) man_items_buffer;

	pthread_mutex_lock(&gtk_lock);

	if(flag & TASK_START) {
		i = 0;
		pointer = man_items_buffer;
		gtk_clist_clear(GTK_CLIST(clist));
	}

	gtk_clist_freeze(GTK_CLIST(clist));
	for (j = 0;j<100 & i< man_items_count;i++,j++) {
		buffer[i]->get_display_name(a);
		buffer[i]->get_section_name(b);
		gtk_clist_append(GTK_CLIST(clist),text);
		gtk_clist_set_row_data(GTK_CLIST(clist),i,(gpointer)buffer[i]);
		gtk_clist_get_text(GTK_CLIST(clist),i,0,pointer++);
	}
	gtk_clist_thaw(GTK_CLIST(clist));
	pthread_mutex_unlock(&gtk_lock);

	//pointer = man_items_display_name;
	//for(int i = 0;i<man_items_count;i++) printf(*(pointer++));
	return (i < man_items_count);
}

int loading_man_data()
{
	ManPath * p;
	char * c;
	int i;
	char buffer[100];
	if(!man_paths_to_be_load->get_size()) return 0;
	pthread_mutex_lock(&loading_man_path_lock);
	c = (char*)man_paths_to_be_load->get_item(0);
	man_paths_to_be_load->delete_item(0);
	pthread_mutex_unlock(&loading_man_path_lock);
	p = new ManPath(c);
	i = man_paths->search_item(c);
	if (i == -1) {
		delete (p);
		g_warning("man path %s does not exist\n",c);
		return man_paths_to_be_load->get_size();
	}
	//	printf("loading... %d end\n",i);
	man_paths->set_value(i,(void*)p);
	if(clist2) {
		sprintf(buffer,"%d",p->GetSize(-1));
		pthread_mutex_lock(&gtk_lock);
		gtk_clist_set_text(GTK_CLIST(clist2),i,2,buffer);
		pthread_mutex_unlock(&gtk_lock);
	}
	return man_paths_to_be_load->get_size();
}

int is_blank(char c) {return (c==0x20||c==0x09)?1:0;}

typedef enum
{
  S_START,
  S_NAME,
  S_NAME_END1,
  S_NAME_END2,
  S_SECTION,
  S_SECTION_END1,
  S_SECTION_END2,
  S_MINUS,
  S_COMMENT_START,
  S_COMMENT
} STATES;

#define BUF_SIZE 400

static List * names;	//the names returned are strdup() , need to free out side
static char * comment;
static char section[BUF_SIZE/4];

static int parser_whatis(int fd) 
{
	static int init = 0;
	int i,k,end;
	static char buffer[BUF_SIZE];
	char c;

	if(!init) {
		names = new List;
		init ++;
	}

	names->delete_all();
	i = 0;
	end = 0;
	STATES s = S_START;
	while(!end) {
		k = read(fd,&c,1);
		//g_print("%c",c,k);
		if(k == -1) g_error("key word task:reading from pipe:%s",g_strerror(errno));
		if (k == 0 || c == 0x0a) {
			if (s == S_COMMENT) {
				buffer[i] = 0;
				// add items to clist
				//for(j = 0;j<names->get_size();j++) g_print("%s , ",(char*)names->get_item(j));
				//g_print("section = %s, \tcomment = %s\n",section,buffer);
				comment = buffer;
			}
			else if( s != S_START) {
				if(context->get_value("show_warning")) 
					g_warning("parser error: unexpected end of %s",k?"line":"file");
				comment = NULL;
			}
			end++;
			continue;
		} 
		switch (s) {
		case S_START:
			if(!is_blank(c)) {
				s = S_NAME;
				i = 0;
				buffer[i++] = c;
			}
			break;
		case S_NAME:
			if( c == ',') {
				s = S_NAME_END1;
				buffer[i] = 0;
				names->add_item(my_strdup(buffer));
			} else if(is_blank(c)) {
				s = S_NAME_END2;
			}else {
				buffer[i++] = c;
			}
			break;
		case S_NAME_END1:
			if(is_blank(c)) break;
			else if(c == '(') {
				s = S_SECTION;
				i = 0;
			}
			else {
				s = S_NAME;
				i = 0;
				buffer[i++] = c;
			}
			break;
		case S_NAME_END2:
			if(is_blank(c)) break;
			else if(c == '(') {
				buffer[i] = 0;
				names->add_item(my_strdup(buffer));
				s = S_SECTION;
				i = 0;
			} else if(c == ',') {
				s = S_NAME_END1;
				buffer[i] = 0;
				names->add_item(my_strdup(buffer));
			}
			else {
				s = S_NAME;
				buffer[i++] = c;
			}
			break;
		case S_SECTION:
			if(is_blank(c)) break;
			else if(c == ')') {
				s = S_SECTION_END1;
				if(i == 0) {
					g_warning("parser error: unknown format");
				}
				else {
					buffer[i] = 0;
					strcpy(section,buffer);
				}
			}
			else buffer[i++] = c;
			break;
		case S_SECTION_END1:
			if(is_blank(c)) break;
			else if(c == '-') s = S_SECTION_END2;
			break;
		case S_SECTION_END2:
			if(is_blank(c)) s = S_COMMENT_START;
			else g_warning("parser error: unknwn format");
			break;
		case S_COMMENT_START:
			if(!is_blank(c)) {
				s = S_COMMENT;
				i = 0;
				buffer[i++] = c;
			}
			break;
		case S_COMMENT:
			buffer[i++] = c;
			break;
		default: g_warning("parser error: unknow state");
		}
	}
	//g_print("k = %d\n",k);
	return k;
}

List * keyword_search_list;

static int key_word_search_taskfunc(int flag)
{
	static int init = 0;
	static int process_ID;
	static int pipes[2];
	static List * paths;
	static char * path;
	static int pipe_active; // pipe is in active
	static int running; // pipe is in active
	static int counter;

	ManPath * man_path;
	ManItem * man_item;
	int i,j,k;
	char file_name[BUF_SIZE], buffer[BUF_SIZE];
	char * text[3];
	char * s;

	if(!init) {
		pipe_active = 0;
		running = 0;
		paths = new List();
		keyword_search_list = new List();
		init ++;
	}

	//g_print("task key-word search ...\n");
	if((flag & (TASK_START|TASK_STOP)) && pipe_active) {
		//g_print("reset ...\n");
		kill(process_ID,3);
		close(pipes[0]);
		pipe_active = 0;
		running = 0;
		gtk_widget_set_sensitive(stop_button,0);
		//paths->delete_all();
	}
	if(flag & TASK_START) {
		//g_print("starting ... key = %s\n",keyword);
		running = 1;
		gtk_widget_set_sensitive(stop_button,1);
		paths->delete_all();
		gtk_clist_clear(GTK_CLIST(clist3));
		counter = 0;
		j = man_paths->get_size();
		for(i = 0;i<j;i++)	paths->add_item(man_paths->get_name(i));
	} else if( !pipe_active ) {
		if(paths->get_size() == 0) {
			running = 0;
			gtk_widget_set_sensitive(stop_button,0);
		} else {
			path = (char*) paths->get_item(0);
			paths->delete_item(0);
			strcat(buffer,"whatis -w \"*");
			strcat(buffer,keyword);
			strcat(buffer,"*\" ");
			fprintf (stderr, "%s\n", file_name);

			g_return_val_if_fail(!pipe(pipes),0);
			process_ID = fork();
			if(process_ID == -1) {g_warning("can not fork a new process");return 0;}
			if(process_ID == 0) {
				g_return_val_if_fail(close(pipes[0]) != -1,0);;
				close(1);
				g_return_val_if_fail(dup(pipes[1]) == 1,0);
				g_return_val_if_fail(close(pipes[1]) != -1,0);
				//write(1,"1234567890safdsfd%srfwfwefw",10);
				//fprintf(stderr,"%s\n",buffer);
				if(do_system_command(buffer,0));// g_warning("my_system returned a non-zero value!");
				_exit(0);
			}
			close(pipes[1]);
			pipe_active ++;
		}
	} else {	//running, pipe in active
		k = parser_whatis(pipes[0]);
		if((j = names->get_size())) {
			/*
			for(i = 0;i<j;i++) 
				g_print(i?", %s":"%s",names->get_item(i));
			g_print("(%s)",section);
			g_print(" - %s\n",comment);
			*/
			man_path = (ManPath*)man_paths->get_value(man_paths->search_item(path));
			text[1] = section;
			text[2] = "";
			for(i = 0;i<j;i++) {
				//g_print("point 0, man_item = %x\n",man_path);
				s = (char*)names->get_item(i);
				if(s[0] == '/') s = strrchr(s, '/') + 1;
				man_item = (ManItem*)man_path->search_man_item(s,section);
				//g_print("point 1\n");
				if(context->get_value("show_warning")) 
					if (!man_item)
						g_warning("man item: %s (%s) could not be found in %s",names->get_item(i),section,path);
				text[0] = (char*)names->get_item(i);
				//g_print("point 2\n");
				if(i == j-1) text[2] = comment;
				pthread_mutex_lock(&gtk_lock);
				gtk_clist_append(GTK_CLIST(clist3),text);
				gtk_clist_set_row_data(GTK_CLIST(clist3),counter++,(gpointer)man_item);
				pthread_mutex_unlock(&gtk_lock);
			}
		}
		if(k == 0) {
			close(pipes[0]);
			pipe_active = 0;
			//g_print("pipe closeing \n");
		} 
	}
	return running;
}
	
/******************* init thread **********************/
Task *task_init_man_data;
Task *task_extract_man_data;
Task *task_add_data_to_clist;
Task *task_loading_man_data;
Task *task_key_word_search;
void init_thread(TaskGroup * thread)
{
	task_init_man_data = task_new(thread,0.3,(TaskRunFunc)init_man_data,NULL);
	task_set_active(task_init_man_data);
	task_extract_man_data = task_new(thread,0.5,(TaskRunFunc)extract_man_data,NULL);
	task_set_active(task_extract_man_data);
	task_add_data_to_clist = task_new(thread,0.7,(TaskRunFunc)add_data_to_clist,NULL);
	task_set_active(task_add_data_to_clist);
	task_loading_man_data = task_new(thread,0.31,(TaskRunFunc)loading_man_data,NULL);
	task_key_word_search = task_new(thread,0.5,(TaskRunFunc)key_word_search_taskfunc,NULL);
}