Repository navigation
Expand file tree
/
Copy pathscript.c
More file actions
1067 lines (956 loc) · 20.7 KB
/
Copy pathscript.c
File metadata and controls
1067 lines (956 loc) · 20.7 KB
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
/*
* Runscript Run a login-or-something script.
* A basic like "programming language".
* This program also looks like a basic interpreter :
* a bit messy. (But hey, I'm no compiler writer :-))
*
* Author: Miquel van Smoorenburg, miquels@drinkel.ow.nl
*
* Bugs: The "expect" routine is, unlike gosub, NOT reentrant !
*
* This file is part of the minicom communications package,
* Copyright 1991-1995 Miquel van Smoorenburg,
* 1997-1999 Jukka Lahtinen
*
* 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 of the License, or (at your option) any later version.
*
* 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.
*
* 10.07.98 jl Added the log command
* 05.04.99 jl The logfile name should a
10BC8
lso be passed as a parameter
* 04.03.2002 jl Treat the ^ character between quotes as control code prefix
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdarg.h>
#include "port.h"
#include "minicom.h"
#include "intl.h"
#define OK 0
#define ERR -1
#define RETURN 1
#define BREAK 2
struct line {
char *line;
int labelcount;
int lineno;
struct line *next;
};
struct var {
char *name;
int value;
struct var *next;
};
/*
* Structure describing the script we are currently executing.
*/
struct env {
struct line *lines; /* Start of all lines */
struct var *vars; /* Start of all variables */
const char *scriptname; /* Name of this script */
int verbose; /* Are we verbose? */
jmp_buf ebuf; /* For exit */
int exstat; /* For exit */
};
struct env *curenv; /* Execution environment */
int gtimeout = 120; /* Global Timeout */
int etimeout = 0; /* Timeout in expect routine */
jmp_buf ejmp; /* To jump to if expect times out */
int inexpect = 0; /* Are we in the expect routine */
const char *newline; /* What to print for '\n'. */
const char *s_login = "name"; /* User's login name */
const char *s_pass = "password";/* User's password */
struct line *thisline; /* Line to be executed */
int laststatus = 0; /* Status of last command */
char homedir[256]; /* Home directory */
char logfname[PARS_VAL_LEN]; /* Name of logfile */
static char inbuf[65]; /* Input buffer. */
/* Forward declarations */
int s_exec(char *);
int execscript(const char *);
/*
* Walk through the environment, see if LOGIN and/or PASS are present.
* If so, delete them. (Someone using "ps" might see them!)
*/
void init_env(void)
{
extern char **environ;
char **e;
for (e = environ; *e; e++) {
if (!strncmp(*e, "LOGIN=", 6)) {
s_login = *e + 6;
*e = "LOGIN=";
}
if (!strncmp(*e, "PASS=", 5)) {
s_pass = *e + 5;
*e = "PASS=";
}
}
}
/*
* Return an environment variable.
*/
const char *mygetenv(char *env)
{
if (!strcmp(env, "LOGIN"))
return s_login;
if (!strcmp(env, "PASS"))
return s_pass;
return getenv(env);
}
/*
* Display a syntax error and exit.
*/
void syntaxerr(const char *s)
{
fprintf(stderr, _("script \"%s\": syntax error in line %d %s%s\n"),
curenv->scriptname, thisline->lineno, s, "\r");
exit(1);
}
/*
* Skip all space
*/
void skipspace(char **s)
{
while (**s == ' ' || **s == '\t')
(*s)++;
}
/*
* Our clock. This gets called every second.
*/
void myclock(int dummy)
{
(void)dummy;
signal(SIGALRM, myclock);
alarm(1);
if (--gtimeout == 0) {
fprintf(stderr, _("script \"%s\": global timeout%s\n"),
curenv->scriptname,"\r");
exit(1);
}
if (inexpect && etimeout && --etimeout == 0)
siglongjmp(ejmp, 1);
}
static char *buffer; /* The buffer is only growing and never freed... */
static unsigned buffersize;
static void buf_wr(unsigned idx, char val)
{
if (idx >= buffersize)
{
buffersize += 64;
buffer = realloc(buffer, buffersize);
}
buffer[idx] = val;
}
static inline char buf_rd(unsigned idx)
{
return buffer[idx];
}
static unsigned bufsize()
{
return buffersize;
}
static inline char *buf()
{
return buffer;
}
/*
* Read a word and advance pointer.
* Also processes quoting, variable substituting, and \ escapes.
*/
char *getword(char **s)
{
unsigned int len;
int f;
int idx = 0;
const char *t = *s;
int sawesc = 0;
int sawq = 0;
const char *env;
char envbuf[32];
if (**s == 0)
return NULL;
for (len = 0; ; len++) {
if (sawesc && t[len]) {
sawesc = 0;
if (t[len] <= '7' && t[len] >= '0') {
buf_wr(idx, 0);
for (f = 0; f < 4 && len < bufsize() && t[len] <= '7' &&
t[len] >= '0'; f++)
buf_wr(idx, 8 * buf_rd(idx) + t[len++] - '0');
if (buf_rd(idx) == 0)
buf_wr(idx, '@');
idx++;
len--;
continue;
}
switch (t[len]) {
case 'r':
buf_wr(idx++, '\r');
break;
case 'n':
buf_wr(idx++, '\n');
break;
case 'b':
buf_wr(idx++, '\b');
break;
case 'a':
buf_wr(idx++, '\a');
break;
case 'f':
buf_wr(idx++, '\f');
break;
case 'c':
buf_wr(idx++, 255);
break;
default:
buf_wr(idx++, t[len]);
break;
}
sawesc = 0;
continue;
}
if (t[len] == '\\') {
sawesc = 1;
continue;
}
if (t[len] == '"') {
if (sawq == 1) {
sawq = 0;
len++;
break;
}
sawq = 1;
continue;
}
if (t[len] == '$' && t[len + 1] == '(') {
for(f = len; t[f] && t[f] != ')'; f++)
;
if (t[f] == ')') {
strncpy(envbuf, &t[len + 2], f - len - 2);
envbuf[f - len - 2] = 0;
len = f;
env = mygetenv(envbuf);
if (env == NULL)
env = "";
while (*env)
buf_wr(idx++, *env++);
continue;
}
}
/* ^ prefix for control chars - jl 3.2002 */
if (sawq == 1 && t[len] == '^' && t[len + 1] != 0) {
char c = toupper(t[len + 1]);
if (c >= 'A' && c <= '_') {
len++;
buf_wr(idx++, c - 'A' + 1);
continue;
}
}
if ((!sawq && (t[len] == ' ' || t[len] == '\t')) || t[len] == 0)
break;
buf_wr(idx++, t[len]);
}
buf_wr(idx, 0);
*s += len;
skipspace(s);
if (sawesc || sawq)
syntaxerr(_("(word contains ESC or quote)"));
return buf();
}
/*
* Save a string to memory. Strip trailing '\n'.
*/
char *strsave(char *s)
{
char *t;
int len;
len = strlen(s);
if (len && s[len - 1] == '\n')
s[--len] = 0;
if (!(t = malloc(len + 1
10BC8
)))
return t;
strcpy(t, s);
return t;
}
/*
* Throw away all malloced memory.
*/
void freemem(void)
{
struct line *l, *nextl;
struct var *v, *nextv;
for (l = curenv->lines; l; l = nextl) {
nextl = l->next;
free(l->line);
free(l);
}
for (v = curenv->vars; v; v = nextv) {
nextv = v->next;
free(v->name);
free(v);
}
}
/*
* Read a script into memory.
*/
static int readscript(const char *s)
{
FILE *fp;
struct line *tl, *prev = NULL;
char *t;
char buf[500]; /* max length of a line - this should be dynamically! */
int lineno = 0;
if ((fp = fopen(s, "r")) == NULL) {
fprintf(stderr, _("runscript: couldn't open \"%s\"%s\n"), s, "\r");
exit(1);
}
/* Read all the lines into a linked list in memory. */
while ((t = fgets(buf, sizeof(buf), fp)) != NULL) {
lineno++;
if (strlen(t) == sizeof(buf) - 1) {
/* Wow, this is really braindead, once upon a time buf was 81 chars
* big and triggered nice errors for too long input lines, now
* we just enlarge the buffer and add a sanity check. This code
* needs to allocate memory dynamically... */
fprintf(stderr, "Input line %u too long, aborting (and fix me!)!\n",
lineno);
exit(1);
}
skipspace(&t);
if (*t == '\n' || *t == '#')
continue;
if (((tl = (struct line *)malloc(sizeof (struct line))) == NULL) ||
((tl->line = strsave(t)) == NULL)) {
fprintf(stderr, _("script \"%s\": out of memory%s\n"), s, "\r");
exit(1);
}
if (prev)
prev->next = tl;
else
curenv->lines = tl;
tl->next = NULL;
tl->labelcount = 0;
tl->lineno = lineno;
prev = tl;
}
fclose(fp);
return 0;
}
/* Read one character, and store it in the buffer. */
void readchar(void)
{
char c;
int n;
while ((n = read(0, &c, 1)) != 1)
if (errno != EINTR)
break;
if (n <= 0)
return;
/* Shift character into the buffer. */
#ifdef _SYSV
memcpy(inbuf, inbuf + 1, 63);
#else
# ifdef _BSD43
bcopy(inbuf + 1, inbuf, 63);
# else
/* This is Posix, I believe. */
memmove(inbuf, inbuf + 1, 63);
# endif
#endif
if (curenv->verbose)
fputc(c, stderr);
inbuf[63] = c;
}
/* See if a string just came in. */
int expfound(const char *word)
{
int len;
if (word == NULL) {
fprintf(stderr, "NULL paramenter to %s!", __func__);
exit(1);
}
len = strlen(word);
if (len > 64)
len = 64;
return !strcmp(inbuf + 64 - len, word);
}
/*
* Send text to a file (stdout or stderr).
*/
int output(char *text, FILE *fp)
{
unsigned char *w;
int first = 1;
int donl = 1;
while ((w = (unsigned char *)getword(&text)) != NULL) {
if (!first)
fputc(' ', fp);
first = 0;
for(; *w; w++) {
if (*w == 255) {
donl = 0;
continue;
}
if (*w == '\n')
fputs(newline, fp);
else
fputc(*w, fp);
}
}
if (donl)
fputs(newline, fp);
fflush(fp);
return OK;
}
/*
* Find a variable in the list.
* If it is not there, create it.
*/
struct var *getvar(char *name, int cr)
{
struct var *v, *end = NULL;
for (v = curenv->vars; v; v = v->next) {
end = v;
if (!strcmp(v->name, name))
return v;
}
if (!cr) {
fprintf(stderr, _("script \"%s\" line %d: unknown variable \"%s\"%s\n"),
curenv->scriptname, thisline->lineno, name, "\r");
exit(1);
}
if ((v = (struct var *)malloc(sizeof(struct var))) == NULL ||
(v->name = strsave(name)) == NULL) {
fprintf(stderr, _("script \"%s\": out of memory%s\n"),
curenv->scriptname, "\r");
exit(1);
}
if (end)
end->next = v;
else
curenv->vars = v;
v->next = NULL;
v->value = 0;
return v;
}
/*
* Read a number or variable.
*/
int getnum(char *text)
{
int val;
if (!strcmp(text, "$?"))
return laststatus;
if ((val = atoi(text)) != 0 || *text == '0')
return val;
return getvar(text, 0)->value;
}
/*
* Get the lines following "expect" into memory.
*/
struct line **buildexpect(void)
{
static struct line *seq[17];
int f;
char *w, *t;
for(f = 0; f < 16; f++) {
if (thisline == NULL) {
fprintf(stderr, _("script \"%s\": unexpected end of file%s\n"),
curenv->scriptname, "\r");
exit(1);
}
t = thisline->line;
w = getword(&t);
if (!strcmp(w, "}")) {
if (*t)
syntaxerr(_("(garbage after })"));
seq[f] = NULL;
return seq;
}
seq[f] = thisline;
thisline = thisline->next;
}
if (f == 16)
syntaxerr(_("(too many arguments)"));
return seq;
}
/*
* Our "expect" function.
*/
int expect(char *text)
{
char *s, *w;
struct line **volatile seq;
struct line oneline;
struct line *dflseq[2];
char *volatile toact = "exit 1";
volatile int found = 0;
int f, val, c;
char *action = NULL;
if (inexpect) {
fprintf(stderr, _("script \"%s\" line %d: nested expect%s\n"),
curenv->scriptname, thisline->lineno, "\r");
exit(1);
}
etimeout = 120;
inexpect = 1;
s = getword(&text);
if (!strcmp(s, "{")) {
if (*text)
syntaxerr(_("(garbage after {)"));
thisline = thisline->next;
seq = buildexpect();
} else {
oneline.line = s;
oneline.next = NULL;
dflseq[0] = &oneline;
dflseq[1] = NULL;
seq = dflseq;
}
/* Seek a timeout command */
for (f = 0; seq[f]; f++) {
if (!strncmp(seq[f]->line, "timeout", 7)) {
c = seq[f]->line[7];
if (c == 0 || (c != ' ' && c != '\t'))
continue;
s = seq[f]->line + 7;
/* seq[f] = NULL; */
skipspace(&s);
w = getword(&s);
if (w == NULL)
syntaxerr(_("(argument expected)"));
val = getnum(w);
if (val == 0)
syntaxerr(_("(invalid argument)"));
e
8
timeout
10BC8
= val;
skipspace(&s);
if (*s != 0)
toact = s;
break;
}
}
if (sigsetjmp(ejmp, 1) != 0) {
f = s_exec(toact);
inexpect = 0;
return f;
}
/* Allright. Now do the expect. */
c = OK;
while (!found) {
action = NULL;
readchar();
for (f = 0; seq[f]; f++) {
s = seq[f]->line;
w = getword(&s);
if (expfound(w)) {
action = s;
found = 1;
break;
}
}
if (action != NULL && *action) {
found = 0;
/* Maybe BREAK or RETURN */
if ((c = s_exec(action)) != OK)
found = 1;
}
}
inexpect = 0;
etimeout = 0;
return c;
}
/*
* Jump to a shell and run a command.
*/
int shell(char *text)
{
laststatus = system(text);
return OK;
}
/*
* Send output to stdout ( = modem)
*/
int dosend(char *text)
{
#ifdef HAVE_USLEEP
/* 200 ms delay. */
usleep(200000);
#endif
/* Before we send anything, flush input buffer. */
m_flush(0);
memset(inbuf, 0, sizeof(inbuf));
newline = "\r";
return output(text, stdout);
}
/*
* Exit from the script, possibly with a value.
*/
int doexit(char *text)
{
char *w;
int ret = 0;
w = getword(&text);
if (w != NULL)
ret = getnum(w);
curenv->exstat = ret;
longjmp(curenv->ebuf, 1);
return 0;
}
/*
* Goto a specific label.
*/
int dogoto(char *text)
{
char *w;
struct line *l;
char buf[32];
int len;
w = getword(&text);
if (w == NULL || *text)
syntaxerr(_("(in goto/gosub label)"));
snprintf(buf, sizeof(buf), "%s:", w);
len = strlen(buf);
for (l = curenv->lines; l; l = l->next)
if (!strncmp(l->line, buf, len))
break;
if (l == NULL) {
fprintf(stderr, _("script \"%s\" line %d: label \"%s\" not found%s\n"),
curenv->scriptname, thisline->lineno, w, "\r");
exit(1);
}
thisline = l;
/* We return break, to automatically break out of expect loops. */
return BREAK;
}
/*
* Goto a subroutine.
*/
int dogosub(char *text)
{
struct line *oldline;
int ret = OK;
oldline = thisline;
dogoto(text);
while (ret != ERR) {
if ((thisline = thisline->next) == NULL) {
fprintf(stderr, _("script \"%s\": no return from gosub%s\n"),
curenv->scriptname, "\r");
exit(1);
}
ret = s_exec(thisline->line);
if (ret == RETURN) {
ret = OK;
thisline = oldline;
break;
}
}
return ret;
}
/*
* Return from a subroutine.
*/
int doreturn(char *text)
{
(void)text;
return RETURN;
}
/*
* Print text to stderr.
*/
int print(char *text)
{
newline = "\r\n";
return output(text, stderr);
}
/*
* Declare a variable (integer)
*/
int doset(char *text)
{
char *w;
struct var *v;
w = getword(&text);
if (w == NULL)
syntaxerr(_("(missing var name)"));
v = getvar(w, 1);
if (*text)
v->value = getnum(getword(&text));
return OK;
}
/*
* Lower the value of a variable.
*/
int dodec(char *text)
{
char *w;
struct var *v;
w = getword(&text);
if (w == NULL)
syntaxerr(_("(expected variable)"));
v = getvar(w, 0);
v->value--;
return OK;
}
/*
* Increase the value of a variable.
*/
int doinc(char *text)
{
char *w;
struct var *v;
w = getword(&text);
if (w == NULL)
syntaxerr(_("(expected variable)"));
v = getvar(w, 0);
v->value++;
return OK;
}
/*
* If syntax: if n1 [><=] n2 command.
*/
int doif(char *text)
{
char *w;
int n1;
int n2;
char op;
if ((w = getword(&text)) == NULL)
syntaxerr("(if)");
n1 = getnum(w);
if ((w = getword(&text)) == NULL)
syntaxerr("(if)");
if (strcmp(w, "!=") == 0)
op = '!';
else {
if (*w == 0 || w[1] != 0)
syntaxerr("(if)");
op = *w;
}
if ((w = getword(&text)) == NULL)
syntaxerr("(if)");
n2 = getnum(w);
if (!*text)
syntaxerr(_("(expected command after if)"));
if (op == '=') {
if (n1 != n2)
return OK;
} else if (op == '!') {
if (n1 == n2)
return OK;
} else if (op == '>') {
if (n1 <= n2)
return OK;
} else if (op == '<') {
if (n1 >= n2)
return OK;
} else
syntaxerr(_("(unknown operator)"));
return s_exec(text);
}
/*
* Set the global timeout-time.
*/
int dotimeout(char *text)
{
char *w;
int val;
w = getword(&text);
if (w == NULL)
syntaxerr(_("(argument expected)"));
if ((val = getnum(w)) == 0)
syntaxerr(_("(invalid argument)"));
gtimeout = val;
return OK;
}
/*
* Turn verbose on/off (= echo stdin to stderr)
*/
int doverbose(char *text)
{
char *w;
curenv->verbose = 1;
if ((w = getword(&text)) != NULL) {
if (!strcmp(w, "on"))
return OK;
if (!strcmp(w, "off")) {
curenv->verbose = 0;
return OK;
}
}
syntaxerr(_("(unexpected argument)"));
return ERR;
}
/*
* Sleep for a certain number of seconds.
*/
int dosleep(char *text)
{
int foo, tm;
tm = getnum(text);
foo = gtimeout - tm;
/* The alarm goes off every second.. */
while (gtimeout != foo)
pause();
return OK;
}
/*
* Break out of an expect loop.
*/
int dobreak(char *dummy)
{
(void)dummy;
if (!inexpect) {
fprintf(stderr, _("script \"%s\" line %d: break outside of expect%s\n"),
curenv->scriptname, thisline->lineno, "\r");
exit(1);
}
return BREAK;
}
/*
* Call another script!
*/
int docall(char *text)
{
struct line *oldline;
struct env *oldenv;
int er;
if (*text == 0)
syntaxerr(_("(argument expected)"));
if (inexpect) {
fprintf(stderr, _("script \"%s\" line %d: call inside expect%s\n"),
curenv->scriptname, thisline->lineno, "\r");
exit(1);
}
oldline = thisline;
oldenv = curenv;
if ((er = execscript(text)) != 0)
exit(er);
/* freemem(); */
thisline = oldline;
curenv = oldenv;
return 0;
}
static int do_log_wrapper(char *s)
{
do_log("%s", s);
return 0;
}
/* KEYWORDS */
struct kw {
const char *command;
int (*fn)(char *);
} keywords[] = {
{ "expect", expect },
{ "send", dosend },
{ "!", shell },
{ "goto", dogoto },
{ "gosub", dogosub },
{ "return", doreturn },
{ "exit", doexit },
{ "print", print },
{ "set", doset },
{ "inc", doinc },
{ "dec", dodec },
{ "if", doif },
{ "timeout", dotimeout },
{ "verbose", doverbose },
{ "sleep", dosleep },
{ "break", dobreak },
{ "call", docall },
{ "log", do_log_wrapper },
{ NULL, (int(*)(char *))0 }
};
/*
* Execute one line.
*/
int s_exec(char *text)
{
char *w;
struct kw *k;
w = getword(&text);
/* If it is a label or a comment, skip it. */
if (w == NULL || *w == '#' || w[strlen(w) - 1] == ':')
return OK;
/* See which command it is. */
for (k = keywords; k->command; k++)
if (!strcmp(w, k->command))
break;
/* Command not found? */
if (k->command == NULL) {
fprintf(stderr, _("script \"%s\" line %d: unknown command \"%s\"%s\n"),
curenv->scriptname, thisline->lineno, w, "\r");
exit(1);
}
return (*(k->fn))(text);
}
/*
* Run the script by continously executing "thisline".
*/
int execscript(const char *s)
{
volatile int ret = OK;
curenv = (struct env *)malloc(sizeof(struct env));
curenv->lines = NULL;