summaryrefslogtreecommitdiffstats
path: root/src/args.c
blob: 867215649972fe3730ed4757a451d11c60faf84c (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
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
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
/*
  Copyright (C) 2008, 2009, 2010 Jiri Olsa <olsajiri@gmail.com>

  This file is part of the latrace.

  The latrace 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 3 of the License, or
  (at your option) any later version.

  The latrace 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 the latrace (file COPYING).  If not, see 
  <http://www.gnu.org/licenses/>.
*/


#include <stdlib.h>
#include <string.h>
#include <search.h>
#include <setjmp.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>

#include "config.h"


#define YY_BUF_SIZE        16384
#define MAX_INCLUDE_DEPTH  10
#define LT_EQUAL           " = "


extern int errno;

typedef struct  yy_buffer_state *YY_BUFFER_STATE;
YY_BUFFER_STATE yy_create_buffer(FILE *file, int size);
extern FILE    *yyin;

int  yyparse();
void yy_switch_to_buffer (YY_BUFFER_STATE new_buffer);
void yy_delete_buffer(YY_BUFFER_STATE b);

int lt_args_parse_init(struct lt_config_shared *cfg);
static struct lt_args_include include_stack[MAX_INCLUDE_DEPTH];
static int include_stack_ptr = 0;
static int enum_init = 0;


/* hardcoded POD types */
static struct lt_arg args_def_pod[] = {
	{
		.dtype     = LT_ARGS_DTYPE_POD, 
		.type_id   = LT_ARGS_TYPEID_VOID,
		.type_len  = sizeof(void),
		.type_name = "void",
		.pointer   = 0,
		.name      = "",
		.mmbcnt    = 0,
                .arch      = NULL,
		.en        = NULL,
		.args_head = NULL,
		.args_list = { NULL, NULL }
	},
	{
		.dtype     = LT_ARGS_DTYPE_POD, 
		.type_id   = LT_ARGS_TYPEID_SHORT,
		.type_len  = sizeof(short),
		.type_name = "short",
		.pointer   = 0,
		.name      = "",
		.mmbcnt    = 0,
                .arch      = NULL,
		.en        = NULL,
		.args_head = NULL,
		.args_list = { NULL, NULL }
	},
	{
		.dtype     = LT_ARGS_DTYPE_POD, 
		.type_id   = LT_ARGS_TYPEID_USHORT,
		.type_len  = sizeof(unsigned short),
		.type_name = "u_short",
		.pointer   = 0,
		.name      = "", 
		.mmbcnt    = 0,
                .arch      = NULL,
		.en        = NULL,
		.args_head = NULL,
		.args_list = { NULL, NULL }
	},
	{
		.dtype     = LT_ARGS_DTYPE_POD, 
		.type_id   = LT_ARGS_TYPEID_INT,
		.type_len  = sizeof(int),
		.type_name = "int",
		.pointer   = 0,
		.name      = "",
		.mmbcnt    = 0,
                .arch      = NULL,
		.en        = NULL,
		.args_head = NULL,
		.args_list = { NULL, NULL }
	},
	{
		.dtype     = LT_ARGS_DTYPE_POD, 
		.type_id   = LT_ARGS_TYPEID_UINT,
		.type_len  = sizeof(unsigned int),
		.type_name = "u_int",
		.pointer   = 0,
		.name      = "",
		.mmbcnt    = 0,
                .arch      = NULL,
		.en        = NULL,
		.args_head = NULL,
		.args_list = { NULL, NULL }
	},
	{
		.dtype     = LT_ARGS_DTYPE_POD, 
		.type_id   = LT_ARGS_TYPEID_LONG,
		.type_len  = sizeof(long),
		.type_name = "long",
		.pointer   = 0,
		.name      = "",
		.mmbcnt    = 0,
                .arch      = NULL,
		.en        = NULL,
		.args_head = NULL,
		.args_list = { NULL, NULL }
	},
	{
		.dtype     = LT_ARGS_DTYPE_POD, 
		.type_id   = LT_ARGS_TYPEID_ULONG,
		.type_len  = sizeof(unsigned long),
		.type_name = "u_long",
		.pointer   = 0,
		.name      = "",
		.mmbcnt    = 0,
                .arch      = NULL,
		.en        = NULL,
		.args_head = NULL,
		.args_list = { NULL, NULL }
	},
	{
		.dtype     = LT_ARGS_DTYPE_POD, 
		.type_id   = LT_ARGS_TYPEID_CHAR,
		.type_len  = sizeof(char),
		.type_name = "char",
		.pointer   = 0,
		.name      = "",
		.mmbcnt    = 0,
                .arch      = NULL,
		.en        = NULL,
		.args_head = NULL,
		.args_list = { NULL, NULL }
	},
	{
		.dtype     = LT_ARGS_DTYPE_POD, 
		.type_id   = LT_ARGS_TYPEID_UCHAR,
		.type_len  = sizeof(unsigned char),
		.type_name = "u_char",
		.pointer   = 0,
		.name      = "",
		.mmbcnt    = 0,
                .arch      = NULL,
		.en        = NULL,
		.args_head = NULL,
		.args_list = { NULL, NULL }
	},
	{
		.dtype     = LT_ARGS_DTYPE_POD, 
		.type_id   = LT_ARGS_TYPEID_LLONG,
		.type_len  = sizeof(long long),
		.type_name = "llong",
		.pointer   = 0,
		.name      = "",
		.mmbcnt    = 0,
                .arch      = NULL,
		.en        = NULL,
		.args_head = NULL,
		.args_list = { NULL, NULL }
	},
	{
		.dtype     = LT_ARGS_DTYPE_POD, 
		.type_id   = LT_ARGS_TYPEID_ULLONG,
		.type_len  = sizeof(unsigned long long),
		.type_name = "u_llong",
		.pointer   = 0,
		.name      = "",
		.mmbcnt    = 0,
                .arch      = NULL,
		.en        = NULL,
		.args_head = NULL,
		.args_list = { NULL, NULL }
	},
	{
		.dtype     = LT_ARGS_DTYPE_POD, 
		.type_id   = LT_ARGS_TYPEID_DOUBLE,
		.type_len  = sizeof(double),
		.type_name = "double",
		.pointer   = 0,
		.name      = "",
		.mmbcnt    = 0,
                .arch      = NULL,
		.en        = NULL,
		.args_head = NULL,
		.args_list = { NULL, NULL }
	},
	{
		.dtype     = LT_ARGS_DTYPE_POD, 
		.type_id   = LT_ARGS_TYPEID_FLOAT,
		.type_len  = sizeof(float),
		.type_name = "float",
		.pointer   = 0,
		.name      = "",
		.mmbcnt    = 0,
                .arch      = NULL,
		.en        = NULL,
		.args_head = NULL,
		.args_list = { NULL, NULL }
	},
};

#define LT_ARGS_DEF_POD_NUM     (sizeof(args_def_pod)/sizeof(struct lt_arg))

/* struct, typedef, enum */
static struct lt_arg args_def_struct[LT_ARGS_DEF_STRUCT_NUM];
static struct lt_arg args_def_typedef[LT_ARGS_DEF_TYPEDEF_NUM];
static int args_def_struct_cnt  = 0;
static int args_def_typedef_cnt = 0;
static struct hsearch_data args_enum_tab;

static struct lt_enum* getenum(struct lt_config_shared *cfg, char *name)
{
	struct lt_enum *en;
	ENTRY e, *ep;

	PRINT_VERBOSE(cfg, 1, "request for <%s>\n", name);

	if (!enum_init) {
		PRINT_VERBOSE(cfg, 1, "no enum added so far\n", name);
		return NULL;
	}

	e.key = name;
	hsearch_r(e, FIND, &ep, &args_enum_tab);

	if (!ep) {
		PRINT_VERBOSE(cfg, 1, "failed to find enum <%s>\n", name);
		return NULL;
	}

	en = (struct lt_enum*) ep->data;

	PRINT_VERBOSE(cfg, 1, "found %p <%s>\n", en, en->name);
	return en;
}

static int enum_comp(const void *ep1, const void *ep2)
{
	struct lt_enum_elem *e1 = (struct lt_enum_elem*) ep1;
	struct lt_enum_elem *e2 = (struct lt_enum_elem*) ep2;

	return e1->val - e2->val;
}

static struct lt_enum_elem* get_enumelem(struct lt_config_shared *cfg, 
	long val, struct lt_enum *en)
{
	struct lt_enum_elem key;
	key.val = val;

	PRINT_VERBOSE(cfg, 1, "looking for %p <%s> value %ld\n",
			en, en->name, val);

	return bsearch(&key, en->elems, en->cnt, 
		sizeof(struct lt_enum_elem), enum_comp);
}

static struct lt_enum_elem* find_enumelem(struct lt_config_shared *cfg,
	char *name, struct lt_enum *en)
{
	struct lt_enum_elem *elem;
	int i;

	for(i = 0; en->cnt; i++) {
		elem = &en->elems[i];

		if (!strcmp(elem->name, name))
			return elem;
	}

	return NULL;
}

int lt_args_add_enum(struct lt_config_shared *cfg, char *name, 
			struct lt_list_head *h)
{
	ENTRY e, *ep;
	struct lt_enum_elem *elem, *last = NULL;
	struct lt_enum *en;
	int i = 0;

	if (NULL == (en = malloc(sizeof(*en))))
		return -1;

	memset(en, 0x0, sizeof(*en));
	en->name = name;

	/* Initialize the hash table holding enum names */
	if (!enum_init) {
	        if (!hcreate_r(LT_ARGS_DEF_ENUM_NUM, &args_enum_tab)) {
	                perror("failed to create has table:");
	                return -1;
	        }
		enum_init = 1;
	}

	e.key = en->name;
	e.data = en;

	if (!hsearch_r(e, ENTER, &ep, &args_enum_tab)) {
		perror("hsearch_r failed");
		free(en);
		/* we dont want to exit just because 
		   we ran out of our symbol limit */
		PRINT_VERBOSE(cfg, 3,
			"reach the enum number limit %u\n", 
			LT_ARGS_DEF_ENUM_NUM);
	}

	/* We've got enum inside the hash, let's prepare the enum itself.
	   The 'elems' field is going to be the qsorted list of 
	   'struct enum_elem's */
	lt_list_for_each_entry(elem, h, list)
		en->cnt++;

	if (NULL == (en->elems = malloc(sizeof(struct lt_enum_elem) * en->cnt)))
		return -1;

	PRINT_VERBOSE(cfg, 3, "enum %s (%d elems) not fixed\n",
			en->name, en->cnt);

	/*
	 * The enum element can be:
	 *
	 * 1) defined
	 * 2) undefined
	 * 3) defined via string reference
	 *
	 * ad 1) no work
	 * ad 2) value of previous element is used
	 * ad 3) we look for the string reference in defined elements' names
	 *
	 * This being said, following actions will happen now:
	 *
	 * - copy all the values to the prepared array
	 * - fix the values based on the above
	 * - sort the array
	 */

	lt_list_for_each_entry(elem, h, list) {

		PRINT_VERBOSE(cfg, 3, "\t %s = %d/%s\n",
			elem->name, elem->val, elem->strval);

		en->elems[i++] = *elem;
	}

	PRINT_VERBOSE(cfg, 3, "enum %s (%d elems) fixed\n",
			en->name, en->cnt);

	/* fixup values */
	for(i = 0; i < en->cnt; i++) {
		elem = &en->elems[i];

		if (!elem->undef) {
			last = elem;
			continue;
		}

		if (elem->strval) {
			/* undefined text value, try to find it in
			 * previous enum definitions */

			struct lt_enum_elem *e;

			e = find_enumelem(cfg, elem->strval, en);
			if (!e) {
				printf("failed to find '%s=%s' enum definition\n",
				       elem->name, elem->strval);
				return -1;
			}

			elem->val = e->val;

		} else {
			/* undefined value -> take last defined + 1 */
			if (!last)
				elem->val = 0;
			else
				elem->val = last->val + 1;
		}

		PRINT_VERBOSE(cfg, 3, "\t %s = %d\n",
			elem->name, elem->val);

		last = elem;
	}

	/* finaly sort the array */
	qsort(en->elems, en->cnt, sizeof(struct lt_enum_elem), enum_comp);
	return 0;
}

struct lt_enum_elem* lt_args_get_enum(struct lt_config_shared *cfg, 
	char *name, char *val)
{
	struct lt_enum_elem* elem;

	if (NULL == (elem = malloc(sizeof(*elem))))
		return NULL;

	memset(elem, 0x0, sizeof(*elem));
	elem->undef = 1;

	if (val) {
		long num;
		char *endptr;

		errno = 0;
		num = strtol(val, &endptr, 10);

		/* parse errors */
		if ((errno == ERANGE && (num == LONG_MAX || num == LONG_MIN)) || 
		    (errno != 0 && num == 0))
			return NULL;

		if (endptr != val) {
			elem->val   = num;
			elem->undef = 0;
		} else {
			/* if no digits were found, we assume the
			 * value is set by string reference */
			elem->strval = strdup(val);
		}

	}

	elem->name = strdup(name);

	PRINT_VERBOSE(cfg, 3, "enum elem %s = %d, strval %s, undef = %d\n",
			elem->name, elem->val, elem->strval, elem->undef);
	return elem;
}

int lt_args_add_struct(struct lt_config_shared *cfg, char *type_name, 
			struct lt_list_head *h)
{
	struct lt_arg *arg, sarg;

	if ((args_def_struct_cnt + 1) == LT_ARGS_DEF_STRUCT_NUM)
		return 1;

	/* check if the struct name is already 
	   defined as a type */
	if (lt_args_getarg(cfg, type_name, NULL, 0, 1, NULL))
		return -1;

	memset(&sarg, 0, sizeof(sarg));
	sarg.dtype     = LT_ARGS_DTYPE_STRUCT;
	sarg.type_id   = LT_ARGS_TYPEID_CUSTOM + args_def_struct_cnt;
	sarg.type_name = type_name;
	sarg.args_head = h;

	PRINT_VERBOSE(cfg, 3, "struct [%s] type %d\n",
			sarg.type_name, sarg.type_id);

	lt_list_for_each_entry(arg, sarg.args_head, args_list) {

		PRINT_VERBOSE(cfg, 3, "\t %s %s %u\n",
				arg->type_name, arg->name, arg->type_len);

		/* This is not what sizeof would return on the structure.
		   The sizeof is arch dependent, this is pure sum. */
		sarg.type_len += arg->type_len;
		sarg.mmbcnt++;
	}

	args_def_struct[args_def_struct_cnt++] = sarg;

	PRINT_VERBOSE(cfg, 3, "%d.struct - final len = %u\n",
			args_def_struct_cnt, sarg.type_len);
	return 0;
}

int lt_args_add_sym(struct lt_config_shared *cfg, struct lt_arg *ret, 
			struct lt_list_head *h)
{
	ENTRY e, *ep;
	struct lt_args_sym *sym;
	struct lt_arg *arg;
	int i = 0;

	PRINT_VERBOSE(cfg, 3, "got symbol '%s %s'\n",
			ret->type_name, ret->name);

	if (NULL == (sym = (struct lt_args_sym*) malloc(sizeof(*sym))))
		return -1;

	memset(sym, 0, sizeof(*sym));
	sym->name = ret->name;

	sym->argcnt = 1;
	lt_list_for_each_entry(arg, h, args_list)
		sym->argcnt++;

	sym->args = (struct lt_arg**) malloc(sym->argcnt * sizeof(struct lt_arg**));
	if (!sym->args)
		/* no need to fre sym, since we are going
		   to exit the program anyway */
		return -1;

	PRINT_VERBOSE(cfg, 3, "got return %s, ptr %d\n",
			ret->type_name, ret->pointer);

	sym->args[i++] = ret;
	lt_list_for_each_entry(arg, h, args_list) {
		PRINT_VERBOSE(cfg, 3, "\t '%s %s'\n",
				arg->type_name, arg->name);
		sym->args[i++] = arg;
	}

	e.key = sym->name;
	e.data = sym;

	if (!hsearch_r(e, ENTER, &ep, &cfg->args_tab)) {
		perror("hsearch_r failed");
		free(sym);
		/* we dont want to exit just because 
		   we ran out of our symbol limit */
		PRINT_VERBOSE(cfg, 3, "reach the symbol number limit %u\n",
				LT_ARGS_TAB);
	} else
		PRINT_VERBOSE(cfg, 3, "got symbol %s (%d args)\n",
				sym->name, sym->argcnt);

	return 0;
}

static struct lt_arg* argdup(struct lt_config_shared *cfg, struct lt_arg *asrc)
{
	struct lt_arg *arg, *a;
        struct lt_list_head *h;

	PRINT_VERBOSE(cfg, 2, "got arg '%s %s', dtype %d\n",
			asrc->type_name, asrc->name, asrc->dtype);

	if (NULL == (arg = malloc(sizeof(*arg)))) {
		perror("malloc failed");
		return NULL;
	}

	*arg = *asrc;

	if (arg->dtype != LT_ARGS_DTYPE_STRUCT)
		return arg;

	/* For structures we need also to copy all its arguments. */

        if (NULL == (h = (struct lt_list_head*) malloc(sizeof(*h)))) {
		perror("malloc failed");
		return NULL;
	}
                
        lt_init_list_head(h);

	lt_list_for_each_entry(a, asrc->args_head, args_list) {
		struct lt_arg *aa;

		/* XXX Not sure how safe is this one... 
		   might need some attention in future :) */
		if (NULL == (aa = argdup(cfg, a)))
			return NULL;

		lt_list_add_tail(&aa->args_list, h);
	}
	
	arg->args_head = h;
	return arg;
}

static struct lt_arg* find_arg(struct lt_config_shared *cfg, char *type, 
			struct lt_arg argsdef[], int size, int create)
{
	int i;

	for(i = 0; i < size; i++) {
		struct lt_arg *arg;
		struct lt_arg adef = argsdef[i];

		PRINT_VERBOSE(cfg, 3, "%d. looking for [%s] - [%s]\n",
					i, type, adef.type_name);

		if (strcmp(type, adef.type_name))
			continue;

		if (!create)
			return &argsdef[i];

		arg = argdup(cfg, &adef);

		PRINT_VERBOSE(cfg, 3, "found %d\n", arg->type_id);
		return arg;
	}

	return NULL;
}

struct lt_arg* lt_args_getarg(struct lt_config_shared *cfg, char *type, 
			char *name, int pointer, int create, char *enum_name)
{
	struct lt_arg *arg;

	do {
		if ((arg = find_arg(cfg, type, 
			args_def_pod, LT_ARGS_DEF_POD_NUM, create)))
			break;

		if ((arg = find_arg(cfg, type, 
			args_def_struct, args_def_struct_cnt, create)))
			break;

		if ((arg = find_arg(cfg, type, 
			args_def_typedef, args_def_typedef_cnt, create)))
			break;

		return NULL;

	} while(0);

	if (!create)
		return arg;

	/* Find out the enum definition if the enum 
	   name is provided. */
	if (enum_name)
		arg->en = getenum(cfg, enum_name);

	arg->name    = strdup(name);

	/* If the type is already a pointer (could be for typedef), 
	   give it a chance to show up. There's only one pointer for 
	   the arg, since there's no reason to go dreper. */
	if (!arg->pointer)
		arg->pointer = pointer;

	return arg;
}

int lt_args_add_typedef(struct lt_config_shared *cfg, char *base, 
	char *new, int pointer)
{
	struct lt_arg *arg;
	int i;

	if ((args_def_typedef_cnt + 1) == LT_ARGS_DEF_TYPEDEF_NUM)
		return 2;

	/* check if the typedef name is already 
	   defined as a type */
	if (lt_args_getarg(cfg, new, NULL, 0, 0, NULL))
		return 1;

	do {
		if ((arg = find_arg(cfg, base, 
			args_def_pod, LT_ARGS_DEF_POD_NUM, 0)))
			break;

		if ((arg = find_arg(cfg, base, 
			args_def_typedef, args_def_typedef_cnt, 0)))
			break;

		PRINT_VERBOSE(cfg, 3, "%s not found\n", base);
		return -1;

	} while(0);

	PRINT_VERBOSE(cfg, 3, "got [%s]\n", new);

	args_def_typedef[i = args_def_typedef_cnt++] = *arg;

	arg = &args_def_typedef[i];
	arg->type_name = strdup(new);
	arg->pointer = pointer;

	lt_init_list_head(&arg->args_list);

	PRINT_VERBOSE(cfg, 3, "%d.typedef - got [%s] [%s]\n",
			args_def_typedef_cnt, base, arg->type_name);
	return 0;
}

int lt_args_init(struct lt_config_shared *cfg)
{
	char *file = LT_ARGS_DEF_CONF;
	int ret = 0;

	if (!hcreate_r(LT_ARGS_TAB, &cfg->args_tab)) {
		perror("failed to create has table:");
		return -1;
	}

	lt_args_parse_init(cfg);

	if (*cfg->args_def)
		file = cfg->args_def;

	PRINT_VERBOSE(cfg, 1, "arguments definition file %s\n", file);

	if (lt_args_buf_open(cfg, file))
		return -1;

	if (yyparse()) {
		printf("failed to parse config file %s\n", file);
		ret = -1;
	}

#if defined(LT_ARGS_ARCH_CONF)
	/* Some architectures provides specific
	 * configuration file. */
	if (lt_args_buf_open(cfg, lt_args_arch_conf(cfg)))
		return -1;

	if (yyparse()) {
		printf("failed to parse config file %s\n", file);
		ret = -1;
	}
#endif

	if (fclose(yyin)) {
		perror("failed to close " LT_ARGS_DEF_CONF);
		return -1;
	}

	return ret;
}

static struct lt_args_sym* getsym(struct lt_config_shared *cfg, char *sym)
{
	struct lt_args_sym *a;
	ENTRY e, *ep;

        PRINT_VERBOSE(cfg, 1, "request for <%s>\n", sym);

	e.key = sym;
	hsearch_r(e, FIND, &ep, &cfg->args_tab);

	if (!ep)
		return NULL;

	a = (struct lt_args_sym*) ep->data;

        PRINT_VERBOSE(cfg, 1, "found %p <%s>\n", a, a->name);
	return a;
}

static int getstr_addenum(struct lt_config_shared *cfg, struct lt_arg *arg,
			char *argbuf, int alen, long val)
{
	char *enstr = NULL;
	struct lt_enum_elem *elem;

	if (!arg->en)
		return 0;

	if (NULL != (elem = get_enumelem(cfg, val, arg->en)))
		enstr = elem->name;

	if (enstr)
		return snprintf(argbuf, alen, "%s", enstr);

	return 0;
}

static int getstr_pod(struct lt_config_shared *cfg, int dspname, struct lt_arg *arg, 
				void *pval, char *argbuf, int *arglen)
{
	int len = 0, alen = *arglen;
	int namelen = strlen(arg->name);

	PRINT_VERBOSE(cfg, 1, "\t arg '%s %s', pval %p, len %d, pointer %d, dtype %d, type_id %d\n",
			arg->type_name, arg->name, pval, alen, arg->pointer, arg->dtype, arg->type_id);

	if (alen < 5)
		return 0;
	
	*arglen = 0;

	if ((dspname) && 
	    (namelen < (alen - 5 - sizeof(LT_EQUAL)))) {
		*arglen  = sprintf(argbuf, "%s"LT_EQUAL, arg->name);
		argbuf  += *arglen;
		alen    -= *arglen;
	}

	/* Get enum resolve for pointers now, the rest 
	   POD is done in ARGS_SPRINTF macro. The char 
	   pointers need special handling later. */
	if ((arg->pointer) &&
	    (arg->type_id != LT_ARGS_TYPEID_CHAR)) {

		void *ptr = *((void**) pval);

		/* Try to get enumed value first. */
		len = getstr_addenum(cfg, arg, argbuf, alen, (long)ptr);

		/* If there's no enum resolved, 
		   just display the ptr value */
		if (!len) {
			if (ptr)
				len = snprintf(argbuf, alen, "%p", ptr);
			else
				len = snprintf(argbuf, alen, "NULL");
		}

		goto out;
	}

#define ARGS_SPRINTF(FMT, TYPE)                                      \
do {                                                                 \
	if (!(len = getstr_addenum(cfg, arg, argbuf, alen,           \
				(long) *((TYPE*) pval))))            \
		len = snprintf(argbuf, alen, FMT, *((TYPE*) pval));  \
} while(0)

	switch(arg->type_id) {
	case LT_ARGS_TYPEID_SHORT:  ARGS_SPRINTF("%hd", short); break;
	case LT_ARGS_TYPEID_USHORT: ARGS_SPRINTF("%hu", unsigned short); break;
	case LT_ARGS_TYPEID_INT:    ARGS_SPRINTF("%d", int); break;
	case LT_ARGS_TYPEID_UINT:   ARGS_SPRINTF("%u", unsigned int); break;
	case LT_ARGS_TYPEID_LONG:   ARGS_SPRINTF("%ld", long); break;
	case LT_ARGS_TYPEID_ULONG:  ARGS_SPRINTF("%lu", unsigned long); break;
	case LT_ARGS_TYPEID_LLONG:  ARGS_SPRINTF("%lld", long long); break;
	case LT_ARGS_TYPEID_ULLONG: ARGS_SPRINTF("%llu", unsigned long long); break;
	case LT_ARGS_TYPEID_DOUBLE: ARGS_SPRINTF("%lf", double); break;
	case LT_ARGS_TYPEID_FLOAT:  ARGS_SPRINTF("%f", float); break;
#undef ARGS_SPRINTF
	case LT_ARGS_TYPEID_CHAR:
		if (arg->pointer) {

			void *val = *((void**) pval);

			if (val) {
				char *s = val;
				int slen = strlen(s);
				int left = alen;

				if ((slen + 2) > left) {
					snprintf(argbuf, left, "\"%s", s);
					strncpy(argbuf + left - sizeof("...\"") + 1, "...\"", sizeof("...\""));
				} else {
					strcpy(argbuf, "\"");
					strcat(argbuf, s);
					strcat(argbuf, "\"");
				}
			} else
				len = snprintf(argbuf, alen, "NULL");
		} else {

			if (*((char*) pval) <= ' ')
				len = snprintf(argbuf, alen, "0x%02x",
						*((char*) pval));
			else
				len = snprintf(argbuf, alen, "0x%02x \'%c\'",
						*((char*) pval), *((char*) pval));
		}
		break;

	case LT_ARGS_TYPEID_VOID:
		len = snprintf(argbuf, alen, "void");
		break;
	}

	if (LT_ARGS_DTYPE_STRUCT == arg->dtype) {
		if (pval)
			len = snprintf(argbuf, alen, "v(%p)", pval);
		else
			len = snprintf(argbuf, alen, "v(REG)");
				
	}

out:
	*arglen += strlen(argbuf);

	PRINT_VERBOSE(cfg, 1, "\t arg out len %d - [%s]\n",
			*arglen, argbuf);
	return 0;
}

int lt_args_cb_arg(struct lt_config_shared *cfg, struct lt_arg *arg, void *pval, 
		   struct lt_args_data *data, int last, int dspname)
{
	int len = data->arglen;

	PRINT_VERBOSE(cfg, 1, "arg '%s %s', pval %p, last %d\n",
				arg->type_name, arg->name, pval, last);

	getstr_pod(cfg, dspname, arg, pval, 
			data->args_buf + data->args_totlen, &len);
	data->args_totlen += len;

	if (!last) {
		strcat(data->args_buf, ", ");
		data->args_totlen += 2;
	}

	return 0;
}

int lt_args_cb_struct(struct lt_config_shared *cfg, int type, struct lt_arg *arg, 
		      void *pval, struct lt_args_data *data, int last)
{
	PRINT_VERBOSE(cfg, 1,
		"type %d, arg '%s %s', pval %p, last %d, pointer %d\n",
		type, arg->type_name, arg->name, pval, last, arg->pointer);

	/* initiall call for the structure argument */
	if (type == LT_ARGS_STRUCT_ITSELF) {

		data->argsd_totlen += sprintf(data->argsd_buf + data->argsd_totlen, 
						"struct %s %s = { ", 
						arg->type_name, arg->name);
		return 0;

	/* subsequent calls for all structure arguments */
	} else if (type == LT_ARGS_STRUCT_ARG) {

		int len = cfg->args_detail_maxlen - data->argsd_totlen;

		getstr_pod(cfg, 1, arg, pval, data->argsd_buf + data->argsd_totlen, &len);
		data->argsd_totlen += len;

		if (!last) {
			strcat(data->argsd_buf, ", ");
			data->argsd_totlen += 2;
		} else
			data->argsd_totlen += sprintf(data->argsd_buf + 
						      data->argsd_totlen, " }\n");
	}
	
	return 0;
}

static int getargs(struct lt_config_shared *cfg, struct lt_args_sym *asym, 
		La_regs *regs, char **abuf, char **adbuf)
{
	struct lt_args_data data;
	int arglen;
	char *buf, *bufd;

	if (NULL == (buf = malloc(cfg->args_maxlen)))
		return -1;

	memset(&data, 0, sizeof(data));

	*buf  = 0;
	*abuf = buf;

	if (cfg->args_detailed) {
		if (NULL == (bufd = malloc(cfg->args_detail_maxlen)))
			return -1;

		*bufd  = 0;
		*adbuf = bufd;
		data.argsd_buf = bufd;
		data.argsd_len = cfg->args_detail_maxlen;
	}


	/* makeup the final space for each 
	   argument textual representation  */
	arglen = (cfg->args_maxlen 
		- ((asym->argcnt - 1) * 2)  /* args separating commas */
		 )/ asym->argcnt;


	data.arglen   = arglen;
	data.args_buf = buf;
	data.args_len = cfg->args_maxlen;

	return lt_stack_process(cfg, asym, regs, &data);
}

static FILE* open_include(struct lt_config_shared *cfg, char *file)
{
	FILE *f;
	char fn[LT_MAXFILE];

	/* we got an absolute path */
	if ((NULL != (f = fopen(file, "r")))) {
		PRINT_VERBOSE(cfg, 1, "open ok [%s]\n", file);
		return f;
	}

	PRINT_VERBOSE(cfg, 1, "open failed [%s]: %s\n",
			file, strerror(errno));

	/* give up if there was already the absolute name */
	if (*file == '/') {
		printf("open failed [%s]: %s\n", file, strerror(errno));
		return NULL;
	}

	/* not an absolute name, give it a chance 
	   inside of the /etc config directory */
	if (strlen(file) > (LT_MAXFILE - sizeof(LT_ARGS_DEF_DIR))) {
		printf("file name length crossed the max %u: %s\n", 
			(u_int) (LT_MAXFILE - sizeof(LT_ARGS_DEF_DIR)), file);
		return NULL;
	}

	sprintf(fn, "%s/%s", LT_ARGS_DEF_DIR, file);

	if ((NULL == (f = fopen(fn, "r")))) {
		PRINT_VERBOSE(cfg, 1, "open failed [%s]: %s\n",
				fn, strerror(errno));
		printf("open failed [%s]: %s\n", file, strerror(errno));
		return NULL;
	}

	PRINT_VERBOSE(cfg, 1, "open ok [%s]\n", fn);
	return f;
}

int lt_args_buf_open(struct lt_config_shared *cfg, char *file)
{
	struct lt_args_include *inc;

	PRINT_VERBOSE(cfg, 1, "opening buffer for [%s] depth %d\n",
			file, include_stack_ptr);

	if ((include_stack_ptr + 1) == MAX_INCLUDE_DEPTH) {
		printf("include depth overstep");
		return -1;
	}

	if (NULL == (yyin = open_include(cfg, file)))
		return -1;

	inc = &include_stack[include_stack_ptr++];
	memset(inc, 0, sizeof(*inc));

	inc->yyin   = yyin;
	inc->file   = strdup(file);
	inc->lineno = 1;
	inc->yybuf  = yy_create_buffer(yyin, YY_BUF_SIZE);

	yy_switch_to_buffer(inc->yybuf);

	PRINT_VERBOSE(cfg, 1, "opened buffer for [%s] depth %d\n",
			file, include_stack_ptr);
	return 0;
}

int lt_args_buf_close(struct lt_config_shared *cfg)
{
	struct lt_args_include *inc = &include_stack[--include_stack_ptr];

	PRINT_VERBOSE(cfg, 1, "buffer closed [%s], depth [%d]\n",
			inc->file, include_stack_ptr);

	free(inc->file);

	/* EOF with no other includes on stack */
	if (!include_stack_ptr)
		return -1;

	/* looks like the base buffer is  cleaned up by the 
	   flex itself, so we do the actual cleaning 
	   only for includes */
	yy_delete_buffer(inc->yybuf);
	fclose(inc->yyin);

	inc = &include_stack[include_stack_ptr - 1];
	yy_switch_to_buffer(inc->yybuf);
	return 0;
}

struct lt_args_include* lt_args_buf_get(void)
{
	struct lt_args_include *inc = &include_stack[include_stack_ptr - 1];
	return inc;
}

int lt_args_sym_entry(struct lt_config_shared *cfg, char *sym, La_regs *regs, 
			char **argbuf, char **argdbuf)
{
	struct lt_args_sym *asym;

	if (NULL == (asym = getsym(cfg, sym)))
		return -1;

	return getargs(cfg, asym, regs, argbuf, argdbuf);
}

static int getargs_ret(struct lt_config_shared *cfg, struct lt_args_sym *asym, 
		La_retval *regs, char **abuf, char **adbuf)
{
	struct lt_args_data data;
	int arglen, totlen;
	char *buf, *bufd;

	if (NULL == (buf = malloc(cfg->args_maxlen)))
		return -1;

	memset(&data, 0, sizeof(data));

	*buf  = 0;
	*abuf = buf;

	/* TODO get together with getargs function somehow... */
	if (cfg->args_detailed) {

		if (NULL == (bufd = malloc(cfg->args_detail_maxlen)))
			return -1;

		*bufd  = 0;
		*adbuf = bufd;
		data.argsd_buf = bufd;
		data.argsd_len = cfg->args_detail_maxlen;
	}

	arglen = cfg->args_maxlen - sizeof(LT_EQUAL);
	totlen = sizeof(LT_EQUAL) - 1;
	strcat(buf, LT_EQUAL);

	data.arglen      = arglen;
	data.args_buf    = buf;
	data.args_len    = cfg->args_maxlen;
	data.args_totlen = totlen;

	return lt_stack_process_ret(cfg, asym, regs, &data);
}

int lt_args_sym_exit(struct lt_config_shared *cfg, char *sym, La_regs *inregs, La_retval *outregs, 
			char **argbuf, char **argdbuf)
{
	struct lt_args_sym *asym;

	if (NULL == (asym = getsym(cfg, sym)))
		return -1;

	return getargs_ret(cfg, asym, outregs, argbuf, argdbuf);
}