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
|
implement Script;
include "sys.m";
sys: Sys;
include "draw.m";
include "string.m";
str: String;
include "lock.m";
include "modem.m";
modem: Modem;
include "script.m";
delim: con "-"; # expect-send delimiter
BUFSIZE: con (1024 * 32);
execute( modmod: Modem, m: ref Modem->Device, scriptinfo: ref ScriptInfo )
{
sys= load Sys Sys->PATH;
str= load String String->PATH;
if (str == nil) {
raise "fail: couldn't load string module";
return;
}
modem = modmod;
if (scriptinfo.path != nil) {
sys->print("Executing Script %s\n",scriptinfo.path);
# load the script
scriptinfo.content = scriptload(scriptinfo.path);
} else {
sys->print("Executing Inline Script\n");
}
# Check for timeout variable
if (scriptinfo.timeout == 0)
scriptinfo.timeout = 20;
tend := sys->millisec() + 1000*scriptinfo.timeout;
conv := scriptinfo.content;
while (conv != nil) {
e, s: string = nil;
p := hd conv;
conv = tl conv;
if (len p == 0)
continue;
sys->print("script: %s\n",p);
if (p[0] == '-') { # just send
if (len p == 1)
continue;
s = p[1:];
} else {
(n, esl) := sys->tokenize(p, delim);
if (n > 0) {
e = hd esl;
esl = tl esl;
if (n > 1)
s = hd esl;
}
}
if (e != nil) {
if (match(m, special(e,scriptinfo), tend-sys->millisec()) == 0) {
sys->print("script: match failed\n");
raise "fail: Script Failed";
return;
}
}
if (s != nil)
modem->send(m, special(s, scriptinfo));
}
sys->print("script: done!\n");
}
match(m: ref Modem->Device, s: string, timo: int): int
{
for(;;) {
c := modem->getc(m, timo);
if (c == '\r')
c = '\n';
sys->print("%c",c);
if (c == 0)
return 0;
head:
while(c == s[0]) {
i := 1;
while(i < len s) {
c = modem->getc(m, timo);
if (c == '\r')
c = '\n';
sys->print("%c",c);
if(c == 0)
return 0;
if(c != s[i])
continue head;
i++;
}
return 1;
}
if(c == '~')
return 1; # assume PPP for now
}
}
#
# Expand special script sequences
#
special(s: string, scriptinfo: ref ScriptInfo ): string
{
if (s == "$username") # special variable
s = scriptinfo.username;
else if (s == "$password")
s = scriptinfo.password;
return deparse(s);
}
deparse(s : string) : string
{
r: string = "";
for(i:=0; i < len s; i++) {
c := s[i];
if (c == '\\' && i+1 < len s) {
c = s[++i];
case c {
't' => c = '\t';
'n' => c = '\n';
'r' => c = '\r';
'b' => c = '\b';
'a' => c = '\a';
'v' => c = '\v';
'0' => c = '\0';
'$' => c = '$';
'u' =>
if (i+4 < len s) {
i++;
(c, nil) = str->toint(s[i:i+4], 16);
i+=3;
}
}
}
r[len r] = c;
}
return r;
}
scriptload( path: string) :list of string
{
dfd := sys->open(path, Sys->OREAD);
if (dfd == nil) {
raise "fail: Script file ("+path+") not found";
return nil;
}
scriptbuf := array[BUFSIZE] of byte;
scriptlen := sys->read(dfd, scriptbuf, len scriptbuf);
if(scriptlen < 0)
raise "fail: can't read script: "+sys->sprint("%r");
(nil, scriptlist) := sys->tokenize(string scriptbuf[0:scriptlen], "\n");
return scriptlist;
}
|