File: gbi.c

package info (click to toggle)
gambas3 3.5.4-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 57,504 kB
  • ctags: 24,586
  • sloc: ansic: 123,138; cpp: 89,281; sh: 21,078; makefile: 1,434; cs: 342; perl: 184; xml: 36
file content (845 lines) | stat: -rw-r--r-- 16,303 bytes parent folder | download
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
/***************************************************************************

	gbi.c

	(c) 2000-2013 BenoƮt Minisini <gambas@users.sourceforge.net>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2, or (at your option)
	any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
	MA 02110-1301, USA.

***************************************************************************/

#define __GBI_C

#include "config.h"
#include "trunk_version.h"

#include "gb_limit.h"
#include "gb_common.h"
#include "gb_alloc.h"

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <dirent.h>

#include <dlfcn.h>

#if defined(OS_LINUX) || defined(OS_GNU) || defined(OS_OPENBSD) || defined(OS_FREEBSD) || defined(OS_CYGWIN)
	#define lt_dlinit() (0)
	#define lt_dlhandle void *
	#define lt_dlopenext(_path) dlopen(_path, RTLD_LAZY)
	#define lt_dlsym(_handle, _symbol) dlsym(_handle, _symbol)
	#define lt_dlclose(_handle) dlclose(_handle)
	#define lt_dlerror() dlerror()
#else
	#include <ltdl.h>
#endif

#include <ctype.h>

#include "gb_component.h"
#include "gb_file.h"
#include "gb_str.h"
#include "gb_arch.h"
#include "gb_common_swap.h"
#include "gb_array.h"
#include "gambas.h"

static char _root[PATH_MAX + 1] = { 0 };
static char _lib_path[PATH_MAX + 1];
static char _info_path[PATH_MAX + 1];
static char _buffer[PATH_MAX + 1];
static char _env[PATH_MAX + 16];

static FILE *out_info;
static FILE *out_list;
static bool _verbose = FALSE;
static bool _format = FALSE;
static bool _nopreload = FALSE;
static bool _root_set = FALSE;
static bool _analyze = FALSE;
static bool _no_include_warning = TRUE;

static char **_components = NULL;

static void analyze(const char *comp, bool include);

#if HAVE_GETOPT_LONG
static struct option LongOptions[] =
{
	{ "version", 0, NULL, 'V' },
	{ "verbose", 0, NULL, 'v' },
	{ "help", 0, NULL, 'h' },
	{ "license", 0, NULL, 'L' },
	{ "root", 1, NULL, 'r' },
	{ 0 }
};
#endif


static int compare_components(char **a, char **b)
{
	return strcmp(*a, *b);
}

static void print(const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);

	vfprintf(out_info, fmt, args);
	
	va_end(args);
}

static void warning(const char *fmt, ...)
{
	va_list args;

	fprintf(stderr, "gbi" GAMBAS_VERSION_STRING ": warning: ");
		
	va_start(args, fmt);

	vfprintf(stderr, fmt, args);
	
	putc('\n', stderr);
	
	va_end(args);
}

static void error(bool must_exit, const char *fmt, ...)
{
	va_list args;

	fprintf(stderr, "gbi" GAMBAS_VERSION_STRING ": ERROR: ");
		
	va_start(args, fmt);

	vfprintf(stderr, fmt, args);
	
	putc('\n', stderr);
	
	va_end(args);

	if (must_exit)
		exit(1);
}

// static void error2(const char *msg, const char *msg2, bool must_exit)
// {
//   fprintf(stderr, "gbi" GAMBAS_VERSION_STRING ": ERROR: %s: %s%s%s\n", msg, msg2, (errno != 0) ? ": " : "", (errno != 0) ? strerror(errno) : "");
//   if (must_exit)
//   	exit(1);
// }


static void init(void)
{
	const char *path;
	char *env;

	/* chemin d'installation de Gambas */

	if (!_root[0])
	{
		const char *dir;
		
		path = FILE_find_gambas();
		dir = FILE_get_dir(FILE_get_dir(path));
		if (dir)
			strncpy(_root, dir, PATH_MAX);
	}

	#ifdef OS_64BITS
	strcpy(_lib_path, FILE_cat(_root, GAMBAS_LIB64_PATH, NULL));
	if (access(FILE_cat(_lib_path, "gb.component", NULL), F_OK))
	#endif
		strcpy(_lib_path, FILE_cat(_root, GAMBAS_LIB_PATH, NULL));
		
	strcpy(_info_path, FILE_cat(_root, "share/gambas" GAMBAS_VERSION_STRING "/info", NULL));

	if (lt_dlinit())
		error(TRUE, "Cannot initialize plug-in management: %s", lt_dlerror());

	env = getenv("GBI_DEBUG");
	if (env && *env && atoi(env))
		_no_include_warning = FALSE;
}


static void newline()
{
	print("\n");
}

#if 0
static bool print_type(const char *type)
{
	switch (*type)
	{
		case 'b': print("Boolean"); break;
		case 'i': print("Integer"); break;
		case 's': print("String"); break;
		case 'd': print("Date"); break;
		case 'f': print("Float"); break;
		case 'v': print("Variant"); break;
		case 'o': print("Object"); break;

		default:

			while (*type && *type != ';')
			{
				print("%c", *type);
				type++;
			}
			return TRUE;
	}

	return FALSE;
}
#endif

static void dump_symbol(GB_DESC *desc)
{
	const char *name;
	const char *p;

	name = &desc->name[1];

	print("%s\n", name);
	print("%c\n", *desc->name);
	if (desc->val1)
		print("%s", desc->val1);
	newline();

	if (*desc->name == 'C')
	{
		switch(*(char *)desc->val1)
		{
			case 'i':
				print("%d", desc->val2);
				break;

			case 's':
				p = (const char *)desc->val2;
				while (*p)
				{
					if (*p == '\n')
						print("\\n");
					else if (*p == '\t')
						print("\\t");
					else
						print("%c", *p);
					p++;
				}
				break;
				
			case 'b':
				if (desc->val2)
					print("T");
				break;
				
			case 'f':
				print("%.15g", desc->val4);
				break;

			default:
				print("?");
				break;
		}
	}
	else if (desc->val3)
		print("%s", (char *)desc->val3);

	newline();
}


static GB_DESC *_sort_symbol;

static int sort_symbol(const int *a, const int *b)
{
	return strcmp(_sort_symbol[*a].name, _sort_symbol[*b].name);
}

static void analyze_class(GB_DESC *desc)
{
	const char *name = desc->name;
	char *parent = NULL;
	bool autocreate = FALSE;
	bool nocreate = FALSE;
	uintptr_t hook;
	int nsymbol;
	int *sort;
	GB_DESC *p;
	int i;

	if (out_list) // && name[0] != '.')
		fprintf(out_list, "%s\n", name);

	desc++;

	while (desc->name)
	{
		hook = (uintptr_t)desc->name;

		if (hook == (intptr_t)GB_INHERITS_ID)
			parent = (char *)desc->val1;
		else if ((hook == (intptr_t)GB_NOT_CREATABLE_ID))
			nocreate = TRUE;
		else if (hook == (intptr_t)GB_AUTO_CREATABLE_ID)
			autocreate = TRUE;
		else if (hook > 16)
			break;

		desc++;
	}

	p = desc;
	nsymbol = 0;
	while (p->name)
	{
		nsymbol++;
		p++;
	}

	if (_format)
	{
		print("CLASS %s\n", name);
		if (parent) print("INHERITS %s\n", parent);
		if (!nocreate) print("CREATABLE\n");
		if (autocreate) print("AUTOCREATABLE\n");
	}
	else
	{
		print("#%s\n", name);
		if (parent)
			print("%s", parent);
		newline();
		if (!nocreate)
			print("C");
		if (autocreate)
			print("A");
		newline();
	}

	ALLOC(&sort, sizeof(int) * nsymbol);
	for (i = 0; i < nsymbol; i++)
		sort[i] = i;
	
	_sort_symbol = desc;
	qsort(sort, nsymbol, sizeof(int), (int (*)(const void *, const void *))sort_symbol);

	for (i = 0; i < nsymbol; i++)
		dump_symbol(&desc[sort[i]]);

	FREE(&sort);
	
	if (_format)
		newline();
}


static GB_DESC **_sort_desc;

static int sort_desc(const int *a, const int *b)
{
	return strcmp(_sort_desc[*a]->name, _sort_desc[*b]->name);
}

static void analyze_include(char *include_list)
{
	char *includes[8];
	int nincludes;
	char *include;
	int i;
	
	include_list = STR_copy(include_list);
	
	if (_verbose)
		fprintf(stderr, "Including %s\n", include_list);
	
	nincludes = 0;
	include = strtok(include_list, ",");
	while (include && nincludes < 8)
	{
		includes[nincludes++] = include;
		include = strtok(NULL, ",");
	}
	
	for (i = 0; i < nincludes; i++)
		analyze(includes[i], TRUE);
		
	STR_free(include_list);
}

static bool analyze_native_component(const char *path)
{
	lt_dlhandle lib;
	GB_DESC **desc;
	char **include;
	GB_DESC **p;
	bool ret = FALSE;
	int nclass;
	int *sort;
	int i;

	if (_verbose)
		fprintf(stderr, "Loading native component: %s\n", path);

	//lt_dlopen_flag = RTLD_LAZY; /* | RTLD_GLOBAL;*/

	lib = lt_dlopenext(path);
	if (!lib)
	{
		error(FALSE, "Cannot load shared library: %s", lt_dlerror());
		return TRUE;
	}

	include = lt_dlsym(lib, LIB_INCLUDE);
	if (include)
		analyze_include(*include);

	desc = lt_dlsym(lib, LIB_CLASS);

	if (desc)
	{
		p = desc;
		nclass = 0;
		while (*p)
		{
			nclass++;
			p++;
		}
	
		ALLOC(&sort, sizeof(int) * nclass);
		for (i = 0; i < nclass; i++)
			sort[i] = i;
		
		_sort_desc = desc;
		qsort(sort, nclass, sizeof(int), (int (*)(const void *, const void *))sort_desc);
	
		for (i = 0; i < nclass; i++)
			analyze_class(desc[sort[i]]);
			
		FREE(&sort);
	}
	else
	{
		if (_verbose)
			warning("cannot find '" LIB_CLASS "' symbol in shared library.");
		//ret = TRUE;
	}

	// Do not close shared libraries,except on openbsd that seems to feel better with
	#ifdef OS_OPENBSD
	lt_dlclose(lib);
	#endif

	return ret;
}


static bool analyze_gambas_component(const char *path)
{
	ARCH *arch;
	ARCH_FIND find;
	bool ret = TRUE;

	if (_verbose)
		fprintf(stderr, "Loading gambas component: %s\n", path);

	arch = ARCH_open(path);

	if (ARCH_find(arch, ".info", 0, &find))
	{
		warning(".info file not found in component archive.");
		goto __RETURN;
	}

	fwrite(&arch->addr[find.pos], 1, find.len, out_info);

	if (ARCH_find(arch, ".list", 0, &find))
	{
		warning(".list file not found in component archive.");
		goto __RETURN;
	}

	fwrite(&arch->addr[find.pos], 1, find.len, out_list);
	ret = FALSE;

__RETURN:
	
	ARCH_close(arch);
	return ret;
}

#if 0
static void preload(char **argv, char *lib)
{
#if DO_PRELOADING
	if (_nopreload || getenv("GB_PRELOAD") || !lib || !*lib)
		return;

	snprintf(_env, sizeof(_env), "LD_PRELOAD=%s", lib);
	putenv(_env);
	putenv("GB_PRELOAD=1");

	execvp(argv[0], argv);
#endif
}
#endif

static bool find_native_component(const char *name)
{
	snprintf(_buffer, sizeof(_buffer), LIB_PATTERN, _lib_path, name);
	return (access(_buffer, F_OK) == 0);
}

static void analyze(const char *comp, bool include)
{
	bool native, gambas;
	char *name;
	char *path_list = NULL;
	char *path_info = NULL;
	bool ok;

	name = STR_copy(comp);

	if (_verbose)
		fprintf(stderr, "%s component %s\n", include ? "Including" : "Analyzing", name);
	else if (!include)
		puts(name);
	
	native = find_native_component(name);
		
	snprintf(_buffer, sizeof(_buffer), ARCH_PATTERN, _lib_path, name);
	gambas = (access(_buffer, F_OK) == 0);

	if (!native && !gambas)
	{
		if (!include || !_no_include_warning)
			warning("component %s not found", name);
		STR_free(name);
		return;
	}

	if (!include)
	{
		path_info = STR_cat(FILE_cat(_info_path, name, NULL), ".info", NULL);
		path_list = STR_cat(FILE_cat(_info_path, name, NULL), ".list", NULL);
	
		out_info = fopen(path_info, "w");
		if (!out_info)
		{
			error(FALSE, "Cannot write file: %s", path_info);
			return;
		}
	
		out_list = fopen(path_list, "w");
		if (!out_list)
		{
			error(FALSE, "Cannot write file: %s", path_list);
			return;
		}
	}
	
	fflush(stdout);
	ok = TRUE;

	if (native)
	{
		snprintf(_buffer, sizeof(_buffer), LIB_PATTERN, _lib_path, name);

		if (analyze_native_component(_buffer))
			ok = FALSE;
	}

	if (gambas)
	{
		snprintf(_buffer, sizeof(_buffer), ARCH_PATTERN, _lib_path, name);

		if (analyze_gambas_component(_buffer))
			if (!native)
				ok = FALSE;
	}

	if (!include)
	{
		fclose(out_info);
		fclose(out_list);
	
		if (!ok)
		{
			FILE_unlink(path_info);
			FILE_unlink(path_list);
		}
		else if (_verbose)
		{
			fprintf(stderr, "wrote %s\n", path_info);
			fprintf(stderr, "wrote %s\n", path_list);
		}

		STR_free(path_info);
		STR_free(path_list);
	}
	
	STR_free(name);
}

static void run_myself(const char *path, const char *name)
{
	const char *argv[10];
	int n = 0;
	pid_t pid;
	int status;
	
	if (_verbose)
		fprintf(stderr, "Running myself for component %s\n", name);
	
	argv[n++] = path;
	if (_verbose)
		argv[n++] = "-v";
	if (_nopreload)
		argv[n++] = "-p";
	if (_root_set)
	{
		argv[n++] = "-r";
		argv[n++] = _root;
	}
	argv[n++] = "-a";
	argv[n++] = name;
	argv[n] = NULL;
	
	if (find_native_component(name))
	{
		snprintf(_env, sizeof(_env), "LD_PRELOAD=%s", _buffer);
		putenv(_env);
	}

	pid = fork();
	switch (pid)
	{
		case 0:
			execvp(path, (char **)argv);
			error(FALSE, "Cannot run sub-process: %s", strerror(errno));
			exit(1);
		case -1:
			error(FALSE, "Cannot run sub-process: %s", strerror(errno));
			exit(1);
		default:
			waitpid(pid, &status, 0);
	}
}

static void make_component_list()
{
	DIR *dir;
	struct dirent *dirent;
	const char *name;
			
	dir = opendir(_lib_path);
	if (dir == NULL)
		error(TRUE, "Cannot read directory: %s", _lib_path);

	//save_fd = dup(STDOUT_FILENO);

	ARRAY_create(&_components);
	
	while ((dirent = readdir(dir)) != NULL)
	{
		name = dirent->d_name;
		if (strcmp(FILE_get_ext(name), "component"))
			continue;
		name = FILE_get_basename(name);
		*((char **)ARRAY_add(&_components)) = STR_copy(name);
	}

	closedir(dir);
	
	qsort(_components, ARRAY_count(_components), sizeof(*_components), (int (*)(const void *, const void *))compare_components);
}

int main(int argc, char **argv)
{
	int i;
	char *name;
	int opt;
	int ind = 0;

	/*#ifdef __FreeBSD__
	optind = 1;
	#else
	optind = 0;
	#endif*/

	//dup(STDOUT_FILENO);

	//_verbose = TRUE;

	for(;;)
	{
		#if HAVE_GETOPT_LONG
			opt = getopt_long(argc, argv, "vVhLpar:", LongOptions, &ind);
		#else
			opt = getopt(argc, argv, "vVhLpar:");
		#endif
		if (opt < 0) break;

		switch (opt)
		{
			case 'V':
				#ifdef TRUNK_VERSION
				printf(VERSION " r" TRUNK_VERSION "\n");
				#else
				printf(VERSION "\n");
				#endif
				exit(0);

			case 'v':
				_verbose = TRUE;
				break;
#if DO_PRELOADING
			case 'p':
				_nopreload = TRUE;
				break;
#endif
				
			case 'r':
				strncpy(_root, optarg, PATH_MAX);
				_root_set = TRUE;
				break;
				
			case 'a':
				_analyze = TRUE;
				break;
				
			case 'L':
				printf(
					"\nGAMBAS Component Informer version " VERSION " " __DATE__ " " __TIME__ "\n"
					COPYRIGHT
					);
				exit(0);

			case 'h':
				printf(
					"\nGenerate component description files.\n"
					"\nUsage: gbi" GAMBAS_VERSION_STRING " [options] [components]\n"
					"Options:"
					#if HAVE_GETOPT_LONG
					"\n"
					#if DO_PRELOADING
					"  -p                         disable preloading\n"
					#endif
					"  -r  --root <directory>     gives the gambas installation directory\n"
					"  -V  --version              display version\n"
					"  -L  --license              display license\n"
					"  -h  --help                 display this help\n"
					#else
					" (no long options on this system)\n"
					#if DO_PRELOADING
					"  -p                         disable preloading\n"
					#endif
					"  -r <directory>             gives the gambas installation directory\n"
					"  -V                         display version\n"
					"  -L                         display license\n"
					"  -h                         display this help\n"
					#endif
					"\n"
					);

				exit(0);
		}
	}

	init();
	
	if (_analyze)
	{
		if (_verbose)
			fprintf(stderr, "LD_PRELOAD=%s\n", getenv("LD_PRELOAD"));

		analyze(argv[optind], FALSE);
		/*if (strcmp(argv[optind], "gb.qt4") == 0)
			analyze("gb.gui", FALSE);
		else if (strcmp(argv[optind], "gb.qt4.opengl") == 0)
			analyze("gb.gui.opengl", FALSE);*/
	}
	else
	{
		if (optind == argc
		#ifdef OS_SOLARIS  /* solaris bug ? */
				|| optind == 0
		#endif
				)
		{
			/*preload(argv,
				"libqt-mt.so.3 "
				"libkdecore.so.4 "
				);*/
	
			if (_verbose)
			{
				fprintf(stderr, "component path: %s\n", _lib_path);
				fprintf(stderr, "info path: %s\n", _info_path);
			}
			
			make_component_list();
			
			for (i = 0; i < ARRAY_count(_components); i++)
			{
				name = _components[i];
				run_myself(argv[0], name);
				STR_free(name);
			}
			
			ARRAY_delete(&_components);
		}
		else
		{
			if (!getenv("GB_PRELOAD"))
			{
				for (ind = optind; ind < argc; ind++)
				{
					name = argv[ind];			
					/*if (strncmp(name, "gb.qt.kde", 9) == 0)
						preload(argv, "libqt-mt.so.3 libkdecore.so.4");
					else if (strcmp(name, "gb.qt") == 0 || strncmp(name, "gb.qt.", 6) == 0)
						preload(argv, "libqt-mt.so.3");*/
				}
			}
		
			for (ind = optind; ind < argc; ind++)
			{
				name = argv[ind];
				//analyze(name, FALSE);
				run_myself(argv[0], name);
			}
		}
	}

	exit(0);
}