forked from oe5hpm/openBCM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macro.cpp
2030 lines (1882 loc) · 55.7 KB
/
macro.cpp
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
/***************************************************************
BayCom(R) Packet-Radio fuer IBM PC
OpenBCM-Mailbox
-----------------
Makro-Interpreter
-----------------
Copyright (c) Florian Radlherr
Taubenbergstr. 32
D-83627 Warngau
Alle Rechte vorbehalten / All Rights Reserved
***************************************************************/
//19880118 OE3DZW sec in timestr
//19980709 DH3MB changed BCM_MACRO to MACRO (definable within baycom.h)
//20121226 DG4MFN changes to make the Linux GCC 4.x.x Compilers happy :-)
#include "baycom.h"
/*---------------------------------------------------------------------------*/
#ifdef MACRO
#define TOK_MIN 1
#define SYMBOL_LEN 20
#define MAX_CTL 10
#define LINE_LEN 80
#define NUM_LEN 12
#define script ((mk_script_t *)(b->script_p))
/*---------------------------------------------------------------------------*/
/*
example "macro.bcm":
#<event> [<call/command/wildcard>]: <macro>
login *: loginout
login dg3rbu: rbu
accept db0aab-3: aab3
cmd a*ktuell: akt
cmd pu*rge: disabled
scmd fwdadd: fwdadd
beforecmd v*ersion: b
beforecmd *:
aftercmd *:
newmail *:
unknown:
*/
/*---------------------------------------------------------------------------*/
/*
example "marco/aktuell":
begin
ftime=filetime("msg/aktuell.dl");
put("Aktuell vom " & datestr(ftime,12) & "\n");
cmd("rt msg/aktuell.dl");
cmd("o ls -l");
end;
*/
/*---------------------------------------------------------------------------*/
static char *mk_job_tab[]=
{ "LOGIN",
"ACCEPT",
"CMD",
"SCMD",
"BEFORECMD",
"AFTERCMD",
"MAILFROM",
"MAILTO",
"UNKNOWN",
NULL
};
/*---------------------------------------------------------------------------*/
typedef struct mk_scripts_t
{ mk_job_t job;
char para[21];
unsigned abbrev;
char scriptname[21];
} mk_scripts_t;
/*---------------------------------------------------------------------------*/
static mk_scripts_t *mk_jobs;
/*---------------------------------------------------------------------------*/
typedef enum mk_token_t
{ TOK_NOTHING, // Example
TOK_NE=TOK_MIN, // Operator: !=
TOK_CONCAT, // Operator: &
TOK_BRACK_L, // Operator: )
TOK_BRACK_R, // Operator: (
TOK_MUL, // Operator: *
TOK_ADD, // Operator: +
TOK_COMMA, // Operator: ,
TOK_SUB, // Operator: -
TOK_DOT, // Operator: .
TOK_DIV, // Operator: /
TOK_SEMI, // Operator: ;
TOK_LESS, // Operator: <
TOK_LE, // Operator: <=
TOK_ASSIGN, // Operator: =
TOK_EQU, // Operator: ==
TOK_GREA, // Operator: >
TOK_GE, // Operator: >=
TOK_SUBSCR_L, // Operator: [
TOK_SUBSCR_R, // Operator: ]
TOK_ABORT, // abort("reason");
TOK_AND, // Operator: logical AND
TOK_BEGIN, // begin * Block, no parameter
TOK_CALL, // call("subprogram",var_a,...);
TOK_CHR, // x=chr(65); * one ASCII-character
TOK_CMD, // x=cmd("v"); or cmd("e dl8mbt 1-");
TOK_CONNECT, // handle=connect("dl8mbt","db0aab");
TOK_DATESTR, // x=datestr(gettime,3);
TOK_DISCONNECT, // disconnect(handle);
TOK_ELSE, // if(condition) .. else .. endif;
TOK_END, // begin ... end;
TOK_ENDIF, // if(condition) .. else .. endif;
TOK_ENDLOOP, // loop .. exit_if(condition); ... endloop:
TOK_EXIT, // loop .. exit; ... endloop:
TOK_EXIT_IF, // loop .. exit_if(condition); ... endloop:
TOK_FILELINE, // line=fileline("filename",5);
TOK_FILEREAD, // string=fileread("filename");
TOK_FILESIZE, // size=filesize("filename");
TOK_FILETIME, // t=filetime("filename");
TOK_FILEWRITE, // filewrite("filename","string\n");
TOK_GETFNAME, // s=getfname("dl8mbt",25);
TOK_GETLINE, // s=getline;
TOK_GETLIST, // s=getlist("dl8mbt");
TOK_GETMACRO, // s=getmacro("%o");
TOK_GETTIME, // t=gettime; put("time=" & time(t));
TOK_GETUSER, // s=getuser("dl8mbt","forward");
TOK_GETVALID, // if(getvalid) s=getline; endif;
TOK_GETVAR, // call=getvar("logincall");
TOK_IF, // if(condition) .. else .. endif;
TOK_LEN, // i=len(string);
TOK_LINES, // a=lines(x);
TOK_LOOP, // loop .. exit_if(condition); ... endloop:
TOK_MOD, // Operator: modulo
TOK_OR, // Operator: logical OR
TOK_OSHELL, // x=oshell("ls -l"); or oshell("ls -l");
TOK_PUT, // put("hallo\n");
TOK_SLEEP, // sleep(7000); * milliseconds
TOK_STRLINE, // s=strline(x,5);
TOK_STRPOS, // i=strpos(s,"test");
TOK_TIMESTR, // put("Zeit: " & datestr(gettime,4) & "\n");
TOK_TIMEOUT, // loop .. exit_if(timeout) .. endloop;
TOK_TOKEN, // s1=token(command,1);
TOK_TRACE, // trace(report,"test");
TOK_VAL, // x=val("A");
TOK_XOR, // Operator: logical EXOR
TOK_EOF // file end.
} mk_token_t;
/*---------------------------------------------------------------------------*/
typedef struct mk_keyword_t
{ char *word;
mk_token_t token;
} mk_keyword_t;
/*---------------------------------------------------------------------------*/
typedef enum
{ MK_NUM_CONST, MK_STR_CONST, MK_SYMBOL, MK_OPERATOR } mk_symbol_type_t;
/*---------------------------------------------------------------------------*/
typedef enum
{ MK_NO_CTL, MK_LOOP, MK_REMOTE, MK_IF } ctl_struc_t;
/*---------------------------------------------------------------------------*/
typedef enum
{ MK_OK,
MK_NEW,
MK_BEGIN,
MK_BLOCK,
MK_ERROR,
MK_RETURN,
MK_END,
MK_IDLE
} parse_state_t;
/*---------------------------------------------------------------------------*/
typedef struct mk_mem_t
{ struct mk_mem_t *prev;
struct mk_mem_t *next;
char data[1];
} mk_mem_t;
/*---------------------------------------------------------------------------*/
typedef struct var_t
{ mk_mem_t *mem_block;
struct var_t *next;
char name[SYMBOL_LEN];
unsigned len;
char *s;
struct var_t *inherited;
} var_t;
/*---------------------------------------------------------------------------*/
typedef struct mk_script_t
{ char *buffer;
char *pos;
unsigned line;
parse_state_t state;
int syntaxcheck;
char *linebegin;
char *error;
int ctl_count;
char *ctl_pos[MAX_CTL];
unsigned ctl_line[MAX_CTL];
mk_token_t ctl_type[MAX_CTL];
time_t ctl_time[MAX_CTL];
char word[LINE_LEN];
char name[SYMBOL_LEN];
int remote;
mk_token_t token;
mk_symbol_type_t type;
var_t *var_root;
struct mk_script_t *return_ptr;
int nesting_level;
char ermessage[80];
int erline;
jmp_buf jmpbuf;
} mk_script_t;
/*---------------------------------------------------------------------------*/
// Prototypes
//
static int mk_parse_script(void);
static int mk_expect(mk_token_t token);
static int mk_parse_error(void);
static int mk_parse_body(void);
static int mk_prepare_script(void);
static int mk_getbegin(void);
static int mk_next_word(void);
static int mk_get_token(void);
static int mk_op_concat(var_t *a,var_t *b,var_t *result);
static int mk_get_mem(var_t *var);
static int mk_free_mem(var_t *var);
static int mk_put_var(char *name, var_t *var, int inherited, mk_script_t *scr);
static int mk_modify_var(char *name, char *value);
static var_t *mk_get_var( char *name);
static int mk_free_all( void);
static int mk_and_or(var_t *result);
static void mk_error(char *msg);
static int mk_abort;
//static void mk_dump_vars( void);
//static mk_script_t *script=NULL;
/*---------------------------------------------------------------------------*/
#define issymbol(c) (isalnum(c) || (c=='_'))
#define issymbegin(c) (isalpha(c) || (c=='_'))
/*---------------------------------------------------------------------------*/
static mk_mem_t *mem_root = NULL;
static mk_keyword_t mk_wordtab[] =
{
{ "!=", TOK_NE },
{ "&", TOK_CONCAT },
{ "(", TOK_BRACK_L },
{ ")", TOK_BRACK_R },
{ "*", TOK_MUL },
{ "+", TOK_ADD },
{ ",", TOK_COMMA },
{ "-", TOK_SUB },
{ ".", TOK_DOT },
{ "/", TOK_DIV },
{ ";", TOK_SEMI },
{ "<", TOK_LESS },
{ "<=", TOK_LE },
{ "=", TOK_ASSIGN },
{ "==", TOK_EQU },
{ ">", TOK_GREA },
{ ">=", TOK_GE },
{ "[", TOK_SUBSCR_L },
{ "]", TOK_SUBSCR_R },
{ "abort", TOK_ABORT },
{ "and", TOK_AND },
{ "begin", TOK_BEGIN },
{ "call", TOK_CALL },
{ "chr", TOK_CHR },
{ "cmd", TOK_CMD },
{ "connect", TOK_CONNECT },
{ "datestr", TOK_DATESTR },
{ "disconnect", TOK_DISCONNECT},
{ "else", TOK_ELSE },
{ "end", TOK_END },
{ "endif", TOK_ENDIF },
{ "endloop", TOK_ENDLOOP },
{ "exit", TOK_EXIT },
{ "exit_if", TOK_EXIT_IF },
{ "fileline", TOK_FILELINE },
{ "fileread", TOK_FILEREAD },
{ "filesize", TOK_FILESIZE },
{ "filetime", TOK_FILETIME },
{ "filewrite",TOK_FILEWRITE },
{ "getfname", TOK_GETFNAME },
{ "getline", TOK_GETLINE },
{ "getlist", TOK_GETLIST },
{ "getmacro", TOK_GETMACRO },
{ "gettime", TOK_GETTIME },
{ "getuser", TOK_GETUSER },
{ "getvalid", TOK_GETVALID },
{ "getvar", TOK_GETVAR },
{ "if", TOK_IF },
{ "len", TOK_LEN },
{ "lines", TOK_LINES },
{ "loop", TOK_LOOP },
{ "mod", TOK_MOD },
{ "or", TOK_OR },
{ "oshell", TOK_OSHELL },
{ "put", TOK_PUT },
{ "sleep", TOK_SLEEP },
{ "strline", TOK_STRLINE },
{ "strpos", TOK_STRPOS },
{ "timestr", TOK_TIMESTR },
{ "timeout", TOK_TIMEOUT },
{ "token", TOK_TOKEN },
{ "trace", TOK_TRACE },
{ "val", TOK_VAL },
{ "xor", TOK_XOR },
{ NULL, TOK_EOF },
};
/*---------------------------------------------------------------------------*/
int mk_perform (mk_job_t job, char *parameter)
{
lastfunc("mk_perform");
unsigned int i = 0;
int ret = 0;
char word[20];
if (! mk_jobs) return 0;
parameter += blkill(parameter);
while (parameter[i] > ' ' && i < 19)
{
word[i] = parameter[i];
i++;
}
word[i] = 0;
for (i = 0; mk_jobs[i].job != MK_NOJOB; i++)
{
if (job == mk_jobs[i].job)
{ //putf("job=%d, para=%s, abbr=%d, script=%s\n", mk_jobs[i].job,
// mk_jobs[i].para, mk_jobs[i].abbrev, mk_jobs[i].scriptname);
if (mk_jobs[i].para[0])
{ // Konstrukte wie "accept dl*"
if (mk_jobs[i].abbrev == strlen(mk_jobs[i].para))
{
if (stristr(word, mk_jobs[i].para) != word) continue;
}
// konstrukte wie "cmd ne*wcommand"
else if (mk_jobs[i].abbrev)
{
if (stristr(mk_jobs[i].para, word) != mk_jobs[i].para ||
strlen(word) < mk_jobs[i].abbrev) continue;
}
// exakte Uebereinstimmung
else if (stricmp(mk_jobs[i].para, word)) continue;
}
ret += mk_start(mk_jobs[i].scriptname);
}
}
return ret;
}
/*---------------------------------------------------------------------------*/
int strsplit (char *s, char *sc[], unsigned int max, char *sep)
{
unsigned int i;
if (! sc) return 0;
if (! s || ! *s)
{
sc[0] = NULL;
return 0;
}
sc[0] = s;
for (i = 1; i < max; i++)
{
while (strpbrk(sc[i - 1], sep) == sc[i - 1]) sc[i - 1]++;
sc[i] = strpbrk(sc[i - 1], sep);
if (! sc[i]) return i;
sc[i][0] = 0;
sc[i]++;
}
return 0;
}
/*---------------------------------------------------------------------------*/
void mk_read_jobs (void)
{
char s[101];
int number = 0;
char *sc[10];
FILE *f = s_fopen(EVENTNAME, "srt");
if (mk_jobs)
{
t_free(mk_jobs);
mk_jobs = NULL;
}
if (f)
{
while (fgets(s, sizeof(s) - 1, f)) number++;
rewind(f);
mk_jobs = (mk_scripts_t *) t_malloc(sizeof(mk_scripts_t) *(number + 1),
"*evt");
number = 0;
m.macro_mycalls = 0;
while (fgets(s, sizeof(s) - 1, f))
{
s[strlen(s) - 1] = 0;
int l = strsplit(s, sc, 10, " :");
if (l < 2 || l > 3 || *s == '#') continue;
mk_jobs[number].job = (mk_job_t) readcmd(mk_job_tab, &(sc[0]), 0);
mk_jobs[number].para[0] = 0;
mk_jobs[number].abbrev = 0;
if (l == 2) strcpy(mk_jobs[number].scriptname, sc[1]);
else
{
char *wc = strchr(sc[1], '*');
if (wc)
{
*wc = 0;
mk_jobs[number].abbrev = strlen(sc[1]);
strcpy(mk_jobs[number].para, sc[1]);
strcat(mk_jobs[number].para, wc + 1);
}
else
strcpy(mk_jobs[number].para, sc[1]);
strcpy(mk_jobs[number].scriptname, sc[2]);
}
if (mk_jobs[number].job == MK_ACCEPT)
{
if (m.macro_mycalls < MK_MYCALLS)
strcpy(m.macro_mycall[m.macro_mycalls++], mk_jobs[number].para);
else
trace(serious, "mk_read_jobs", "to many accepts");
}
number++;
}
mk_jobs[number].job = MK_NOJOB;
trace(report, "mk_read_jobs", "%d macros activated", number);
if (*b->logincall && number) putf("%d macros.\n", number);
s_fclose(f);
align_mycalls();
}
}
/*---------------------------------------------------------------------------*/
int mk_getuser (char *bef, char *result, char *call)
{
static char *beftab[] =
{ "COMMAND", "SPEECH", "FORWARD", "HELPLEVEL", "LINES",
"NAME", "PROMPT", "REJECT", "TTYPW", "PW", "LF",
"IDIR", "UDIR", "ILIST", "ULIST", "IREAD", "UREAD", "CHECK", "PS",
"STATUS", "QUOTA", "NOPURGE", "PWLINE", "READLOCK", "ECHO", "MYBBS",
"FDELAY", "FHOLD", "RLIMIT",
NULL
};
enum befnum
{ unsinn,
command, speech, forward, helplevel, lines,
name, prompt, reject, ttypw, pw, lf,
idir, udir, ilist, ulist, iread, uread, check, ps,
status, quota, nopurge, pwline, readlock, echo, mybbs_,
fdelay, fhold, rlimit
};
int optgroup = 0;
if (! loaduser(call, u, 1)) return 0;
switch ((befnum) readcmd(beftab, &bef, 0))
{
case unsinn: return 0;
case forward:
case mybbs_: sprintf(result, "%s %lu", u->mybbs, u->mybbstime); break;
case command: strcpy(result, u->firstcmd); break;
case prompt: strcpy(result, u->prompt); break;
case pw: sprintf(result, "%u", strlen(u->password)); break;
case pwline: sprintf(result, "%u", u->pwline); break;
case echo: sprintf(result, "%u", u->echo); break;
case fdelay: sprintf(result, "%u", u->fdelay); break;
case fhold: sprintf(result, "%u", u->fhold); break;
case rlimit: sprintf(result, "%u", u->rlimit); break;
case readlock: sprintf(result, "%u", u->readlock); break;
case helplevel:sprintf(result, "%u", u->helplevel); break;
case lf: sprintf(result, "%u", u->lf); break;
case lines: sprintf(result, "%u", u->zeilen); break;
case name: strcpy(result, u->name); break;
case reject: strcpy(result, u->notvisible); break;
case speech: strcpy(result, u->sprache); break;
case ps: optgroup++;
case check: optgroup++;
case uread: optgroup++;
case iread: optgroup++;
case ulist: optgroup++;
case ilist: optgroup++;
case udir: optgroup++;
case idir: strcpy(result, optstr(optgroup)); break;
case status: sprintf(result, "%u", u->status); break;
case nopurge: sprintf(result, "%u", u->nopurge); break;
case quota: sprintf(result, "%u", (unsigned) (u->daybytes >> 10)); break;
case ttypw: sprintf(result, "%u", strlen(u->ttypw)); break;
}
return 1;
}
/*---------------------------------------------------------------------------*/
int mk_getvar (char *bef, char *result)
{
static char *beftab[] =
{ "LOGINCALL", "UPLINK", "TXBYTES", "RXBYTES", "SYSOP", "LOGINTIME", "BOARD",
"DEST", "SRC", "FROMBOX", "AT", "BID", "BOXLIST", "USERMAIL", "LIFETIME",
"LINES", "BYTES", "SUBJECT", "REPLYBOARD", "REPLYNUM",
"TASKID", "TASKNAME", "CREATED", "LASTINPUT", "LASTCMD", NULL
};
enum befnum
{ unsinn,
logincall, uplink, txbytes, rxbytes, sysop, logintime, board,
dest, src, frombox, at, bid, boxlist, usermail, lifetime,
lines, bytes, subject, replyboard, replynum,
taskid, taskname, created, lastinput, lastcmd_
};
switch ((befnum) readcmd(beftab, &bef, 0))
{
case unsinn: return 0;
case logincall:strcpy(result, b->logincall); break;
case uplink: strcpy(result, b->uplink); break;
case board: strcpy(result, b->prompt); break;
case dest: strcpy(result, b->ziel); break;
case src: strcpy(result, b->herkunft); break;
case at: strcpy(result, b->at); break;
case bid: strcpy(result, b->bid); break;
case boxlist: strcpy(result, b->destboxlist); break;
case subject: strcpy(result, b->betreff); break;
case replyboard:strcpy(result, b->replyboard); break;
case taskname: strcpy(result, t->name); break;
case lastcmd_: strcpy(result, t->lastcmd); break;
case txbytes: sprintf(result, "%lu", b->txbytes); break;
case rxbytes: sprintf(result, "%lu", b->rxbytes); break;
case sysop: sprintf(result, "%u", b->sysop); break;
case taskid: sprintf(result, "%u", b->taskid); break;
case replynum: sprintf(result, "%u", b->replynum); break;
case logintime:sprintf(result, "%lu", b->logintime); break;
case created: sprintf(result, "%lu", b->created); break;
case lastinput:sprintf(result, "%lu", b->last_input); break;
default: break;
}
return 1;
}
/*---------------------------------------------------------------------------*/
static int mk_get_mem (var_t *var)
{
mk_mem_t *mem = (mk_mem_t *) t_malloc(sizeof(mk_mem_t) + var->len, "mkva");
mem->next = mem_root;
mem->prev = NULL;
if (mem->next)
mem->next->prev = mem;
mem_root = mem;
var->s = mem->data;
var->mem_block = mem;
return OK;
}
/*---------------------------------------------------------------------------*/
static int mk_free_mem (var_t *var)
{
if (! var->s || ! var->mem_block)
trace(fatal, "mk_free_mem", "no block allocated");
mk_mem_t *mem = var->mem_block;
if (mem->next) mem->next->prev = mem->prev;
if (mem->prev) mem->prev->next = mem->next;
if (mem_root == mem) mem_root = mem->next;
t_free(mem);
var->len = 0;
var->s = NULL;
var->mem_block = NULL;
return OK;
}
/*---------------------------------------------------------------------------*/
static int mk_put_var (char *name, var_t *var, int inherited, mk_script_t *scr)
{
var_t *var_temp;
var_temp = scr->var_root;
while (var_temp)
{
if (! strcmp(var_temp->name, name)) break;
var_temp = var_temp->next;
}
if (! var_temp)
{
var_temp = (var_t *) t_malloc(sizeof(var_t), "mkpu");
memset(var_temp, 0, sizeof(var_t));
if (inherited == TRUE) var_temp->inherited = var;
var_temp->next = scr->var_root;
scr->var_root = var_temp;
strcpy(var_temp->name, name);
}
if (var_temp->s) mk_free_mem(var_temp);
var_temp->len = var->len;
var_temp->s = var->s;
var_temp->mem_block = var->mem_block;
var->s = NULL;
var->mem_block = NULL;
var->len = 0;
return OK;
}
/*---------------------------------------------------------------------------*/
static int mk_modify_var (char *name, char *value)
{
var_t var_temp;
var_temp.len = strlen(value);
mk_get_mem(&var_temp);
strcpy(var_temp.s, value);
return mk_put_var(name, &var_temp, 0, script);
}
/*---------------------------------------------------------------------------*/
static var_t *mk_get_var (char *name)
{
var_t *var_temp = script->var_root;
while (var_temp)
{
if (! strcmp(var_temp->name, name)) return var_temp;
var_temp = var_temp->next;
}
mk_error("undefined symbol");
return NULL;
}
/*---------------------------------------------------------------------------*/
/*
static void mk_dump_vars(void)
{
var_t *var_temp = script->var_root;
int len=0;
while (var_temp)
{
putf("%s=\"", var_temp->name);
for (len=0; len<var_temp->len; len++)
{
if (var_temp->s[len]>=' ')
putchar(var_temp->s[len]);
else
{
putchar('^');
putchar(var_temp->s[len]+'@');
}
if ((len+strlen(var_temp->name))>70)
{
putf("...");
break;
}
}
putf("\";\n");
var_temp = var_temp->next;
}
}
*/
/*---------------------------------------------------------------------------*/
static int mk_free_all (void)
{
if (script->buffer != NULL)
{
t_free(script->buffer);
script->buffer = NULL;
}
if (script->var_root)
{
int count = 0;
int inh_count = 0;
var_t *var = script->var_root;
do
{
var_t *next = var->next;
if (! var->inherited)
{
if (var->s) mk_free_mem(var);
count++;
}
else
{
var_t *prev = var->inherited;
prev->len = var->len;
prev->s = var->s;
prev->mem_block = var->mem_block;
inh_count++;
}
t_free(var);
var = next;
} while (var != NULL);
//trace(report, "mk_free_all", "%d inherited vars", inh_count);
//trace(report, "mk_free_all", "%d var blocks released", count);
}
if (! script->return_ptr)
{
int count = 0;
mk_mem_t *mem = mem_root;
while (mem)
{
mk_mem_t *next = mem->next;
t_free(mem);
count++;
mem = next;
}
mem_root = NULL;
//trace(report, "mk_free_all", "%d mem blocks released", count);
}
return OK;
}
/*---------------------------------------------------------------------------*/
static int mk_parse_error (void)
{
char line[LINE_LEN];
strcpy(line, "");
if (script->error)
{
sprintf(script->ermessage, "%s, word '%s'", script->error, script->word);
script->erline = script->line;
if (*script->linebegin != LF)
{
int linelen = 0;
*line = 0;
while (*script->linebegin == ' ') script->linebegin++;
while (*script->linebegin != LF && *script->linebegin)
{
int idx = strlen(line);
line[idx] = *script->linebegin;
line[idx + 1] = 0;
script->linebegin++;
linelen++;
if (linelen > 70) break;
if (idx >= (LINE_LEN - 11)) break;
}
}
putf("macro: %s(%u): %s\n", script->name, script->line, script->ermessage);
if (*line) putf("line: %s\n", line);
return ERROR;
}
else return OK;
}
/*---------------------------------------------------------------------------*/
/*
static int mk_read_hexdump(char *hexdump, char *binary, int *length, int maxlen)
{
int incount=0;
while (maxlen && hexdump[0])
{
unsigned hex;
while (hexdump[incount] && hexdump[incount]<=' ') incount++;
if (sscanf(hexdump+incount, "%2x", &hex) != 1)
break;
binary[0] = (char) hex;
binary++;
(*length)++;
maxlen--;
incount++;
if (isalnum(hexdump[incount])) incount++;
}
return incount;
}
*/
/*---------------------------------------------------------------------------*/
static int mk_lex (void)
{
mk_next_word();
return mk_get_token();
}
/*---------------------------------------------------------------------------*/
static int mk_get_token (void)
{
int indext = 0;
char firstchar = *script->word;
while (mk_wordtab[indext].word)
{
if ( firstchar == mk_wordtab[indext].word[0]
&& ! strcmp(mk_wordtab[indext].word, script->word))
{
script->token = mk_wordtab[indext].token;
return OK;
}
indext++;
}
script->token = TOK_NOTHING;
return OK;
}
/*---------------------------------------------------------------------------*/
static int mk_next_word (void)
{
enum { NO_COMM, C_COMM, CPP_COMM } comment = NO_COMM;
while (*script->pos)
{
if (*script->pos == LF)
{
script->line++;
if (comment == CPP_COMM) comment = NO_COMM;
script->linebegin = script->pos + 1;
}
if (comment == C_COMM)
{
if (*script->pos == '*' && script->pos[1] == '/')
{
comment = NO_COMM;
script->pos += 2;
continue;
}
}
else if (comment == NO_COMM)
{
if (*script->pos == '/')
{
if (script->pos[1] == '/')
{
comment = CPP_COMM;
script->pos++;
continue;
}
else if (script->pos[1] == '*')
{
comment = C_COMM;
script->pos++;
continue;
}
}
if (*script->pos > ' ')
{
*script->word = *script->pos;
if (*script->pos == '"')
{
unsigned int i = 0;
script->pos++;
while (*script->pos != '"')
{
if (*script->pos == LF) mk_error("'\"' expected");
if (*script->pos == '\\')
{
script->pos++;
switch (*script->pos)
{
case 'n': script->word[i++] = LF; break;
case 'a': script->word[i++] = '\a'; break;
default: script->word[i++] = *script->pos;
}
}
else script->word[i++] = *script->pos;
script->pos++;
if (i >= LINE_LEN)
{
script->word[20] = 0;
mk_error("constant too long");
}
}
script->word[i] = 0;
script->pos++;
script->type = MK_STR_CONST;
return OK;
}
else if (isdigit(*script->pos))
{
unsigned int i = 0;
while (isdigit(*script->pos))
{
script->word[i++] = *script->pos;
script->pos++;
if (i >= NUM_LEN)
{
script->word[NUM_LEN - 1] = 0;
mk_error("constant range");
}
}
script->word[i] = 0;
script->type = MK_NUM_CONST;
return OK;
}
else if (issymbegin(*script->pos))
{
unsigned int i = 0;
while (issymbol(*script->pos))
{
script->word[i++] = tolower(*script->pos);
script->pos++;
if (i >= SYMBOL_LEN)
{
script->word[20] = 0;
mk_error("symbol too long");
}
}
script->word[i] = 0;
script->type = MK_SYMBOL;
return OK;
}
else
{
script->word[0] = *script->pos;
script->word[1] = 0;
switch (*script->pos)
{
case '=':
case '!':
case '<':
case '>':
if (script->pos[1] == '=')
{
script->word[1] = script->pos[1];
script->word[2] = 0;
script->pos++;
} break;
}
script->pos++;
script->type = MK_OPERATOR;
return OK;
}
}
}
script->pos++;
}
*script->word = 0;
script->token = TOK_EOF;
mk_error("unexpected end of file");
return ERROR;
}
/*---------------------------------------------------------------------------*/
int mk_expression (var_t *result)
{
var_t left, right;
mk_and_or(&left);
if (script->token == TOK_CONCAT)
{
mk_lex();
mk_expression(&right);
mk_op_concat(&left, &right, result);
}
else *result = left;
return OK;
}
/*---------------------------------------------------------------------------*/
int mk_get_para (int number, var_t par[])
{
int indext = 0;
mk_lex();