-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtranslate.c
2647 lines (2438 loc) · 70.1 KB
/
translate.c
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
/* This file is part of "xtrace"
* Copyright (C) 2009,2010,2011 Bernhard R. Link
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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 St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <config.h>
#include <assert.h>
#include <errno.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/time.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/select.h>
#include <unistd.h>
#include <search.h>
#include "xtrace.h"
#include "parse.h"
#include "stringlist.h"
#include "translate.h"
/* This parses an file to generate the description how packets look like.
*/
enum variable_type { vt_namespace = 0, vt_request, vt_response, vt_event, vt_setup, vt_type, vt_constants, vt_values, vt_struct, vt_COUNT };
static const char * const typename[vt_COUNT] = {
"namespace", "request", "response", "event", "type", "constants", "values", "struct"};
static const struct base_type {
const char *name;
enum fieldtype type;
unsigned int flags;
int size;
#define NEEDS_CONSTANTS 1
#define NEEDS_BITMASK 3
#define ALLOWS_CONSTANTS 4
#define USES_STORE 8
#define SETS_STORE 0x10
#define USES_FORMAT 0x20
#define SETS_FORMAT 0x40
#define ELEMENTARY 0x80
#define PUSHES 0x100
#define SETS_NEXT 0x200
#define NEEDS_STORE 0x400
} base_types [] = {
{ "BITMASK8", ft_BITMASK8, NEEDS_BITMASK|ELEMENTARY, 1},
{ "BITMASK16", ft_BITMASK16, NEEDS_BITMASK|ELEMENTARY, 2},
{ "BITMASK32", ft_BITMASK32, NEEDS_BITMASK|ELEMENTARY, 4},
{ "ENUM8", ft_ENUM8, NEEDS_CONSTANTS|ELEMENTARY, 1},
{ "ENUM16", ft_ENUM16, NEEDS_CONSTANTS|ELEMENTARY, 2},
{ "ENUM32", ft_ENUM32, NEEDS_CONSTANTS|ELEMENTARY, 4},
{ "CARD8", ft_CARD8, ALLOWS_CONSTANTS|ELEMENTARY, 1},
{ "CARD16", ft_CARD16, ALLOWS_CONSTANTS|ELEMENTARY, 2},
{ "CARD32", ft_CARD32, ALLOWS_CONSTANTS|ELEMENTARY, 4},
{ "INT8", ft_INT8, ALLOWS_CONSTANTS|ELEMENTARY, 1},
{ "INT16", ft_INT16, ALLOWS_CONSTANTS|ELEMENTARY, 2},
{ "INT32", ft_INT32, ALLOWS_CONSTANTS|ELEMENTARY, 4},
{ "UINT8", ft_UINT8, ALLOWS_CONSTANTS|ELEMENTARY, 1},
{ "UINT16", ft_UINT16, ALLOWS_CONSTANTS|ELEMENTARY, 2},
{ "UINT32", ft_UINT32, ALLOWS_CONSTANTS|ELEMENTARY, 4},
{ "STRING8", ft_STRING8, USES_STORE|SETS_NEXT, 0},
{ "LISTofCARD8", ft_LISTofCARD8, ALLOWS_CONSTANTS|USES_STORE|SETS_NEXT, 0},
{ "LISTofCARD16", ft_LISTofCARD16, ALLOWS_CONSTANTS|USES_STORE|SETS_NEXT, 0},
{ "LISTofCARD32", ft_LISTofCARD32, ALLOWS_CONSTANTS|USES_STORE|SETS_NEXT, 0},
{ "LISTofUINT8", ft_LISTofUINT8, ALLOWS_CONSTANTS|USES_STORE|SETS_NEXT, 0},
{ "LISTofUINT16", ft_LISTofUINT16, ALLOWS_CONSTANTS|USES_STORE|SETS_NEXT, 0},
{ "LISTofUINT32", ft_LISTofUINT32, ALLOWS_CONSTANTS|USES_STORE|SETS_NEXT, 0},
{ "LISTofINT8", ft_LISTofINT8, ALLOWS_CONSTANTS|USES_STORE|SETS_NEXT, 0},
{ "LISTofINT16", ft_LISTofINT16, ALLOWS_CONSTANTS|USES_STORE|SETS_NEXT, 0},
{ "LISTofINT32", ft_LISTofINT32, ALLOWS_CONSTANTS|USES_STORE|SETS_NEXT, 0},
{ "EVENT", ft_EVENT, 0, 0},
{ "ATOM", ft_ATOM, ALLOWS_CONSTANTS|ELEMENTARY, 4},
{ "LISTofFormat", ft_LISTofFormat, USES_FORMAT|USES_STORE|SETS_NEXT, 0},
{ "LISTofATOM", ft_LISTofATOM, ALLOWS_CONSTANTS|USES_STORE|SETS_NEXT, 0},
{ "FORMAT8", ft_FORMAT8, SETS_FORMAT, 1},
{ "BE32", ft_BE32, ALLOWS_CONSTANTS, 4},
{ "FRACTION16_16", ft_FRACTION16_16, 0, 4},
{ "FRACTION32_32", ft_FRACTION32_32, 0, 8},
{ "UFRACTION32_32", ft_UFRACTION32_32, 0, 8},
{ "INT32_32", ft_INT32_32, ELEMENTARY, 8},
{ "FIXED", ft_FIXED, 0, 4},
{ "FIXED1616", ft_FIXED, 0, 4},
{ "FIXED3232", ft_FIXED3232, 0, 8},
{ "LISTofFIXED", ft_LISTofFIXED, USES_STORE|SETS_NEXT, 0},
{ "LISTofFIXED1616", ft_LISTofFIXED, USES_STORE|SETS_NEXT, 0},
{ "LISTofFIXED3232", ft_LISTofFIXED3232, USES_STORE|SETS_NEXT, 0},
{ "FLOAT32", ft_FLOAT32, 0, 4},
{ "LISTofFLOAT32", ft_LISTofFLOAT32, USES_STORE|SETS_NEXT, 0},
{ "PUSH8", ft_PUSH8, PUSHES, 1},
{ "PUSH16", ft_PUSH16, PUSHES, 2},
{ "PUSH32", ft_PUSH32, PUSHES, 4},
{ "STORE8", ft_STORE8, SETS_STORE, 1},
{ "STORE16", ft_STORE16, SETS_STORE, 2},
{ "STORE32", ft_STORE32, SETS_STORE, 4},
{ NULL, 0, 0, 0}
};
/* some types are only implicitable buildable: */
static const struct base_type base_type_list_of_value =
{ "LISTofVALUE", ft_LISTofVALUE, NEEDS_STORE, 0};
static const struct base_type base_type_list_of_struct =
{ "LISTofStruct", ft_LISTofStruct, USES_STORE|SETS_NEXT, 0};
static const struct base_type base_type_list_of_varstruct =
{ "LISTofVarStruct", ft_LISTofVarStruct, USES_STORE|SETS_NEXT, 0};
static const struct base_type base_type_struct =
{ "Struct", ft_Struct, 0, -1};
//static const struct base_type base_type_varstruct =
// { "VarStruct", ft_VarStruct, SETS_NEXT, 0};
#define C(td, f) ((td->flags & f) == f)
struct typespec {
const struct base_type *base_type;
/* constants for most, unless values for LISTofVALUE,
or struct for Struct and List */
struct variable *data;
};
struct variable {
enum variable_type type;
int refcount;
union {
struct unfinished_parameter {
struct unfinished_parameter *next;
bool isspecial;
union {
struct {
size_t offse;
const char *name;
struct typespec type;
} regular;
struct {
enum fieldtype type;
size_t offse;
const char *condition;
bool isjunction;
struct unfinished_parameter *iftrue;
const struct parameter *finalized;
} special;
};
} *parameter;
struct {
struct constant *constants;
size_t size;
bool bitmask;
} c;
struct unfinished_value {
struct unfinished_value *next;
unsigned long flag;
const char *name;
struct typespec type;
} *values;
struct typespec t;
};
union parameter_option finalized;
};
struct namespace {
struct namespace *next;
char *name;
int refcount;
char *extension;
int num_requests;
struct request_data {
const char *name;
int number;
bool has_response;
bool unsupported;
bool special;
int record_variables;
struct variable *request, *response;
} *requests;
int num_events[event_COUNT];
struct event_data {
const char *name;
int number;
bool unsupported;
bool special;
struct variable *event;
} *events[event_COUNT];
int num_errors;
const char **errors;
struct variable *setup;
void *variables[vt_COUNT];
/* namespaces that can be used without prefix: */
int used_count;
struct namespace **used;
};
struct varname {
struct variable *variable;
char name[];
};
static int compare_variables(const void *a, const void *b) {
const char *v1 = a, *v2 = b;
return strcmp(v1 + sizeof(struct varname),
v2 + sizeof(struct varname));
}
static void variable_unref(struct variable *v);
static inline void typespec_done(struct typespec *t) {
variable_unref(t->data);
}
static void typespec_copy(struct typespec *dst, const struct typespec *src) {
*dst = *src;
if( dst->data != NULL )
dst->data->refcount++;
}
static void parameter_free(struct unfinished_parameter *parameter) {
while( parameter != NULL ) {
struct unfinished_parameter *p = parameter;
parameter = p->next;
if( p->isspecial ) {
parameter_free(p->special.iftrue);
} else {
typespec_done(&p->regular.type);
}
free(p);
}
}
static void variable_unref(struct variable *v) {
if( v == NULL )
return;
assert( v->refcount > 0 );
if( -- (v->refcount) > 0 )
return;
if( v->type == vt_values ) {
while( v->values != NULL ) {
struct unfinished_value *n = v->values->next;
typespec_done(&v->values->type);
free(v->values);
v->values = n;
}
} else if( v->type == vt_constants ) {
free(v->c.constants);
} else if( v->type == vt_type ) {
typespec_done(&v->t);
} else if( v->type == vt_struct || v->type == vt_request
|| v->type == vt_setup
|| v->type == vt_response || v->type == vt_event ) {
parameter_free(v->parameter);
} else
assert( v->type != v->type );
free(v);
}
#define namespace_unlock(n) do {if(n != NULL ){(n)->refcount--;}} while(0)
struct parser {
char buffer[300], *position, *last;
struct namespace *namespaces;
struct source_file {
struct source_file *next;
char *name;
char *filename;
FILE *file;
long lineno;
struct namespace *namespace;
} *current;
struct searchpath_entry {
struct searchpath_entry *next;
const char *dir;
size_t len;
} *searchpath;
bool error;
};
static void file_free(struct source_file *current) {
while( current != NULL ) {
struct source_file *n = current->next;
free(current->name);
free(current->filename);
if( current->file != NULL )
(void)fclose(current->file);
namespace_unlock(current->namespace);
free(current);
current = n;
}
}
static void error(struct parser *parser, const char *fmt, ...) FORMAT(printf,2,3);
static bool get_next_line(struct parser *parser, long firstline) {
char *p;
do {
if( parser->error )
return false;
parser->position = parser->buffer;
parser->last = parser->buffer;
if( fgets(parser->buffer, sizeof(parser->buffer),
parser->current->file) == NULL ) {
int e = ferror(parser->current->file);
if( e != 0 ) {
fprintf(stderr,
"Error %d reading from file '%s': %s\n",
e, parser->current->filename,
strerror(e));
} else {
if( firstline > 0 ) {
error(parser,
"Unexpected end of file (forgot END (awaited since %ld) and EOF?)", firstline+1);
}
error(parser,
"Unexpected end of file (forgot EOF?)");
}
parser->error = true;
return false;
}
parser->current->lineno++;
p = strchr(parser->buffer, '\0');
while( p-- > parser->buffer &&
( *p == '\n' || *p == '\r' ||
*p == '\t' || *p == ' ' ) )
*p = '\0';
} while ( parser->buffer[0] == '#' || parser->buffer[0] == '\0' );
return true;
}
static bool file_done(struct parser *parser) {
struct source_file *last_done, *first_unfinished;
int i, e;
if( parser->error )
return false;
e = ferror(parser->current->file);
if( e != 0 ) {
fprintf(stderr,
"Error %d reading from file '%s': %s\n",
e, parser->current->filename, strerror(e));
parser->error = true;
return false;
}
i = fclose(parser->current->file);
parser->current->file = NULL;
if( i != 0 ) {
e = errno;
fprintf(stderr,
"Error %d reading from file '%s': %s\n",
e, parser->current->filename, strerror(e));
parser->error = true;
return false;
}
/* check if there is more to do: */
last_done = parser->current;
while( last_done->next != NULL && last_done->next->file == NULL )
last_done = last_done->next;
first_unfinished = last_done->next;
if( first_unfinished == NULL )
return true;
/* move the first not yet processed file to here: */
last_done->next = first_unfinished->next;
first_unfinished->next = parser->current;
parser->current = first_unfinished;
return false;
}
static const char *get_const_token(struct parser *parser, bool optional) {
char *p, *q;
if( parser->error )
return NULL;
p = parser->position;
while( *p != '\0' && (*p == ' ' || *p == '\t') )
p++;
parser->last = p;
if( *p == '\0' ) {
if( !optional )
error(parser, "unexpected end of line");
return NULL;
}
if( *p == '"' ) {
q = p;
p++;
while( *p != '\0' && *p != '"' ) {
if( *p == '\\' && p[1] != '\0' ) {
p++;
if( *p < '0' || *p > '7' )
*q++ = *p++;
else {
*q = *(p++) - '0';
if( *p >= '0' && *p <= '7' )
*q = *q * 8 + *(p++) - '0';
if( *p >= '0' && *p <= '7' )
*q = *q * 8 + *(p++) - '0';
q++;
}
} else {
*q++ = *p++;
}
}
if( *p != '"' ) {
error(parser, "Unterminated string!");
return NULL;
}
*q = '\0';
p++;
} else {
while( *p != '\0' && *p != ' ' && *p != '\t' ) {
if( *p == '#' ) {
error(parser, "Unescaped '#'");
return NULL;
}
p++;
}
}
while( *p != '\0' && (*p == ' ' || *p == '\t') )
*(p++) = '\0';
parser->position = p;
return parser->last;
}
static void oom(struct parser *parser) {
if( parser->error )
return;
fputs("Out of memory!", stderr);
parser->error = true;
}
static char *get_token(struct parser *parser, bool optional) {
const char *v;
char *p;
v = get_const_token(parser, optional);
if( v == NULL )
return NULL;
p = strdup(v);
if( p == NULL ) {
oom(parser);
}
return p;
}
static char *get_token_with_len(struct parser *parser, size_t *l) {
char *p;
if( parser->error )
return NULL;
p = parser->position;
parser->last = p;
if( *p == '\0' ) {
error(parser, "unespected end of line");
return NULL;
}
while( *p != '\0' && *p != ' ' && *p != '\t' )
p++;
*l = p - parser->last;
while( *p != '\0' && (*p == ' ' || *p == '\t') )
*(p++) = '\0';
parser->position = p;
return parser->last;
}
static void error(struct parser *parser, const char *fmt, ...) {
va_list ap;
if( parser->error )
return;
parser->error = true;
if( parser->last != NULL )
fprintf(stderr, "%s:%ld:%d: ", parser->current->filename,
parser->current->lineno,
(int)(1 + (parser->last - parser->buffer)));
else
fprintf(stderr, "%s:%ld: ", parser->current->filename,
parser->current->lineno);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
}
static void no_more_arguments(struct parser *parser) {
if( parser->position[0] != '\0' ) {
parser->last = parser->position;
error(parser, "End of line expected!");
}
}
void add_searchpath(struct parser *parser, const char *dir) {
struct searchpath_entry **last;
last = &parser->searchpath;
while( *last != NULL )
last = &(*last)->next;
*last = malloc(sizeof(struct searchpath_entry));
if( *last == NULL ) {
oom(parser);
return;
}
(*last)->next = NULL;
(*last)->dir = dir;
(*last)->len = strlen(dir);
}
static FILE *find_file(struct parser *parser, const char *name, char **filename_p) {
size_t l = strlen(name);
struct searchpath_entry *r;
assert( parser->searchpath != NULL );
for( r = parser->searchpath ; r != NULL ; r = r->next ) {
char *filename = NULL;
FILE *f;
filename = malloc(l + r->len + 2);
if( filename == NULL ) {
oom(parser);
return NULL;
}
memcpy(filename, r->dir, r->len);
filename[r->len] = '/';
memcpy(filename + r->len + 1, name, l + 1);
f = fopen(filename, "r");
if( f != NULL ) {
*filename_p = filename;
return f;
}
free(filename);
}
fprintf(stderr, "Unable to find '%s' in search path!\n", name);
parser->error = true;
*filename_p = NULL;
return NULL;
}
static void open_next_file(struct parser *parser, char *name) {
struct source_file *current;
if( name == NULL )
oom(parser);
if( parser->error ) {
free(name);
return;
}
current = parser->current;
while( current != NULL && strcmp(current->name, name) != 0 )
current = current->next;
if( current != NULL ) {
if( current->file != NULL )
error(parser,
"Circular dependency! '%s' requested while parsing it!", name);
free(name);
return;
}
current = calloc(1, sizeof(*current));
if( current == NULL ) {
oom(parser);
free(name);
return;
}
current->name = name;
current->file = find_file(parser, name, ¤t->filename);
if( current->file == NULL ) {
int e = errno;
error(parser, "Error %d opening '%s': %s",
e, current->filename, strerror(e));
file_free(current);
return;
}
current->lineno = 0;
current->namespace = NULL;
parser->position = NULL;
parser->last = NULL;
current->next = parser->current;
parser->current = current;
}
static bool add_variable(struct parser *parser, const char *prefix, const char *name, const char **varname, struct variable *variable) {
struct varname *v, **vv;
size_t pl = strlen(prefix), l = strlen(name);
struct namespace *namespace = parser->current->namespace;
v = malloc(sizeof(struct varname) + l + pl + 1);
if( v == NULL ) {
oom(parser);
return false;
}
memcpy(v->name, prefix, pl);
memcpy(v->name + pl, name, l + 1);
v->variable = NULL;
vv = tsearch(v, &namespace->variables[variable->type], compare_variables);
if( vv == NULL ) {
free(v);
oom(parser);
return false;
}
if( *vv != v ) {
// free(v);
/* already defined */
return false;
}
if( varname != NULL )
*varname = v->name;
v->variable = variable;
variable->refcount ++;
return true;
}
static struct variable *add_var(struct parser *parser, const char *prefix, const char *name, const char **varname, enum variable_type vt) {
struct variable *variable;
if( name == NULL )
return NULL;
variable = calloc(1, sizeof(struct variable));
if( variable == NULL ) {
oom(parser);
return NULL;
}
variable->type = vt;
if( add_variable(parser, prefix, name, varname, variable) )
return variable;
error(parser, "%s '%s%s' already defined!", typename[vt], prefix, name);
free(variable);
return NULL;
}
static struct variable *find_variable(struct parser *parser, enum variable_type vt, const char *name) {
struct varname **v;
const char *e;
int i;
if( name == NULL )
return NULL;
e = strchr(name, ':');
if( e != NULL ) {
struct namespace *n;
if( e[1] != ':' ) {
error(parser, "Unexpected colon (':') in '%s'!", name);
return NULL;
}
for( n = parser->namespaces ; n != NULL ; n = n->next ) {
if( strncmp(n->name, name, e - name) != 0 )
continue;
if( n->name[e-name] != '\0' )
continue;
v = tfind(e + 2 - sizeof(struct varname),
&n->variables[vt],
compare_variables);
if( v == NULL || *v == NULL ) {
error(parser, "Unknown %s '%s' ('%s')!", typename[vt], name, e+2);
return NULL;
} else
return (*v)->variable;
}
error(parser, "Unknown namespace '%.*s'", (int)(e-name), name);
return NULL;
}
v = tfind(name - sizeof(struct varname), &parser->current->namespace->variables[vt],
compare_variables);
if( v != NULL && *v != NULL )
return (*v)->variable;
// check imported namespaces
for( i = 0 ; i < parser->current->namespace->used_count ; i++ ) {
struct namespace *n = parser->current->namespace->used[i];
v = tfind(name - sizeof(struct varname), &n->variables[vt],
compare_variables);
if( v != NULL && *v != NULL )
return (*v)->variable;
}
error(parser, "Unknown %s %s!", typename[vt], name);
return NULL;
}
#define command_is(name) l == sizeof(name)-1 && memcmp(command, name, sizeof(name)-1) == 0
static void parse_errors(struct parser *parser) {
long first_line = parser->current->lineno;
struct namespace *ns = parser->current->namespace;
if( ns->num_errors != 0 || ns->errors != NULL ) {
error(parser, "second ERRORS for namespace '%s'!",
ns->name);
return;
}
while( get_next_line(parser, first_line) ) {
const char *name = get_const_token(parser, false);
const char **n;
if( strcmp(name, "END") == 0 ) {
no_more_arguments(parser);
return;
} else if( strcmp(name, "EOF") == 0 ) {
no_more_arguments(parser);
error(parser,
"Missing END (beginning at line %ld)", first_line);
}
no_more_arguments(parser);
name = string_add(name);
if( name == NULL ) {
parser->error = true;
return;
}
ns->num_errors++;
n = realloc(ns->errors, ns->num_errors * sizeof(const char*));
if( n == NULL ) {
oom(parser);
return;
}
ns->errors = n;
ns->errors[ns->num_errors - 1] = name;
}
error(parser, "missing END!");
}
static unsigned long parse_number(struct parser *parser, const char *value) {
char *e;
unsigned long number = 0;
if( parser->error )
return 0;
assert( value != NULL );
if( value[0] == '$' ) {
char *v;
struct variable *var;
const struct constant *c;
e = strrchr(value, ':');
if( e == NULL || ( e > value && *(e-1) == ':' ) ) {
error(parser, "Constants name and member must be separated with a single colon!");
return 0;
}
v = strndup(value + 1, e - (value+1));
if( v == NULL ) {
oom(parser);
return 0;
}
var = find_variable(parser, vt_constants, v);
if( var == NULL ) {
free(v);
return 0;
}
for( c = var->c.constants ; c->name != NULL ; c++ ) {
if( strcmp(c->name, e+1) == 0 ) {
free(v);
return c->value;
}
}
error(parser, "Unable to find '%s' in constants %s!", e+1, v);
free(v);
return 0;
}
if( value[0] == '0' && value[1] == '\0' ) {
e = (char*)value + 1;
} else if( value[0] == '0' && value[1] == 'x' )
number = strtoll(value+2, &e, 16);
else if( value[0] == '0' && value[1] == 'o' )
number = strtoll(value+2, &e, 8);
else if( value[0] == '0' && value[1] == 'b' )
number = strtoll(value+2, &e, 2);
else if( value[0] != '0' )
number = strtoll(value, &e, 10);
else {
error(parser, "Leading zeros in numbers are forbidden to avoid confusion!");
e = (char*)value;
}
if( e[0] == '<' && e[1] == '<' ) {
char *ee;
long long shift = strtoll(e + 2, &ee, 10);
if( ee > e + 2 && shift >= 0 && shift < 32 ) {
number = number << (unsigned long)shift;
e = ee;
}
}
if( *e != '\0' ) {
error(parser, "Error parsing number!");
return 0;
}
return number;
}
static bool parse_typespec(struct parser *parser, struct typespec *t) {
const char *type, *attribute;
struct variable *tv;
const struct base_type *td;
memset(t, 0, sizeof(*t));
type = get_const_token(parser, false);
if( parser->error ) {
return false;
}
td = base_types;
while( td->name != NULL && strcmp(td->name, type) != 0 )
td++;
if( td->name != NULL ) {
t->base_type = td;
if( C(td, NEEDS_CONSTANTS) ) {
struct variable *cv;
attribute = get_const_token(parser, false);
cv = find_variable(parser, vt_constants, attribute);
if( cv == NULL )
return false;
if( C(td, NEEDS_BITMASK) && ! cv->c.bitmask ) {
error(parser,
"Not-BITMASK constants %s used for bitmask!",
attribute);
}
t->data = cv;
cv->refcount++;
}
} else {
tv = find_variable(parser, vt_type, type);
if( tv == NULL )
return false;
assert( tv->type == vt_type );
typespec_copy(t, &tv->t);
}
attribute = get_const_token(parser, true);
if( attribute != NULL ) {
if( strcmp(attribute, "constants") == 0 ) {
if( !C(t->base_type, ALLOWS_CONSTANTS) ) {
error(parser, "constants not allowed here!");
} else if( t->data != NULL ) {
error(parser, "multiple constants not allowed!");
} else {
struct variable *cv;
attribute = get_const_token(parser, false);
cv = find_variable(parser, vt_constants, attribute);
if( cv == NULL )
return false;
t->data = cv;
cv->refcount++;
}
} else
error(parser, "unknown type attribute!");
}
no_more_arguments(parser);
return !parser->error;
}
static struct unfinished_parameter *new_parameter_special(struct parser *parser, struct unfinished_parameter ***last_pp, enum fieldtype ft, size_t offse, const char *condition) {
struct unfinished_parameter *n;
if( parser->error )
return NULL;
n = calloc(1, sizeof(struct unfinished_parameter));
if( n == NULL ) {
oom(parser);
return NULL;
}
n->isspecial = true;
n->special.offse = offse;
n->special.type = ft;
/* NULL is termination condition */
if( condition == NULL )
n->special.condition = "";
else
n->special.condition = condition;
**last_pp = n;
*last_pp = &n->next;
return n;
}
/* here many things can still be checked:
if nothing is accessed past the length in a struct,
if things overlap, ...
*/
struct parameter_state {
struct parameter_state *parent;
struct unfinished_parameter *junction;
size_t maxsize, minsize;
bool store_set,
store_used,
format_set,
sizemarker_set,
nextmarker_set,
nextmarker_at_end_of_packet;
};
static void field_accessed(struct parser *parser, struct parameter_state *state, size_t ofs, size_t fieldlen) {
if( ofs == (size_t)-1 )
return;
if( ofs + fieldlen > state->minsize ) {
if( ofs + fieldlen > state->maxsize ) {
error(parser, "Accessing field past specified SIZE of %lu!", (unsigned long)state->maxsize);
}
state->minsize = ofs + fieldlen;
}
/* TODO: record here what areas are used to avoid overlaps and
* gaps? */
}
static bool parse_parameters(struct parser *parser, struct variable *variable, bool needsnextmarker) {
long first_line = parser->current->lineno;
struct parameter_state *state;
struct unfinished_parameter *parameters = NULL, **last = ¶meters;
assert( variable->parameter == NULL );
state = calloc(1, sizeof(struct parameter_state));
if( state == NULL ) {
oom(parser);
return false;
}
state->maxsize = SIZE_MAX;
while( get_next_line(parser, first_line) ) {
const char *position, *name;
unsigned long number;
struct typespec type;
position = get_const_token(parser, false);
if( strcmp(position, "EOF") == 0 ) {
error(parser,
"Missing 'END' (closing block opened at line %ld)!",
first_line + 1);
break;
}
if( strcmp(position, "END") == 0 ) {
no_more_arguments(parser);
if( state->parent != NULL )
error(parser, "missing ELSE");
if( needsnextmarker && !state->nextmarker_set )
error(parser, "Missing NEXT (or LISTof...)!");
free(state);
variable->parameter = parameters;
return true;
}
if( strcmp(position, "ALIAS") == 0 ) {
error(parser, "ALIAS is no longer a valid keyword!");
break;
}
if( strcmp(position, "ELSE") == 0 ) {
struct parameter_state *s;
if( state->parent == NULL )
error(parser, "ELSE without IF");
no_more_arguments(parser);
if( needsnextmarker && !state->nextmarker_set )
error(parser, "Missing NEXT (or LISTof...)!");
last = &state->junction->next;
s = state->parent;
free(state);
state = s;
continue;
}
if( strcmp(position, "IF") == 0 ||
strcmp(position, "ELSEIF") == 0 ) {
struct parameter_state *s;
struct unfinished_parameter *i;
const char *v, *condition;
enum fieldtype ft;
size_t fieldlen;
bool inelseif = strcmp(position, "ELSEIF") == 0;
if( inelseif ) {
/* this is like an END here */
if( state->parent == NULL )
error(parser, "ELSEIF without IF");
if( needsnextmarker && !state->nextmarker_set )
error(parser, "Missing NEXT (or LISTof...)!");
}
v = get_const_token(parser, false);
if( strcmp(v, "STORED") == 0 )
number = (size_t)-1; // compare store value
else