-
Notifications
You must be signed in to change notification settings - Fork 0
/
comgt.c
executable file
·1740 lines (1676 loc) · 44.1 KB
/
comgt.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
/*
* comgt version 0.31 - 3G/GPRS datacard management utility
*
* Copyright (C) 2003 Paul Hardwick <[email protected]>
*
* 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.
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* See comgt.doc for more configuration and usage information.
*
*/
/***************************************************************************
* $Id: comgt.c,v 1.4 2006/10/20 14:30:19 pharscape Exp $
****************************************************************************/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <termio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include "comgt.h"
#define MAXLABELS 3000 /* Maximum number of labels */
#define MAXGOSUBS 128 /* Max depth */
#define STRINGL 1024 /* String lengths. Also, max script line length */
#define MAXPATH 1024 /* Max filename length (less or equal to STRINGL) */
#define MAXTOKEN 20 /* Maximum token or label length */
#define GTDEVICE "/dev/modem"
#define BOOL unsigned char
#define NVARS 286 /* a-z, a0-z9 == 26*11 */
extern char *optarg;
extern int optind, opterr;
FILE *filep;
char *script;
int scriptspace;
BOOL ifres;
int lastpc,pc; /* program "counters" */
long resultcode=0; /* result code */
int ignorecase=1; /* no case sensitivity */
BOOL comecho=0; /* echo what's comin' in */
BOOL high_speed=0;
long senddelay=0; /* 0/100th second character delay for sending */
long number; /* For getonearg() returning an long */
char **label; /* Index of labels for goto and gosub */
int *labelpc; /* Positions of said labels in script */
int labels; /* Number of labels found in script */
long intvars[NVARS]; /* [a-z][0-9] integer variables */
char string[STRINGL]; /* For getstring() returns and misc. use (misuse) */
char *stringvars[NVARS]; /* $[a-z][0-9] string variables */
char cspeed[10]; /* Ascii representation of baudrate */
int speed=B0; /* Set to B110, B150, B300,..., B38400 */
char device[MAXPATH]; /* Comm device. May be "-" */
char token[MAXTOKEN]; /* For gettoken() returns */
char scriptfile[MAXPATH]; /* Script file name */
char scriptfilepath[MAXPATH]; /* temp storage for full path */
BOOL verbose=0; /* Log actions */
struct termio cons, stbuf, svbuf; /* termios: svbuf=before, stbuf=while */
int comfd=0; /* Communication file descriptor. Defaults to stdin. */
char msg[STRINGL]; /* Massage messages here */
int preturn,returns[MAXGOSUBS];
int clocal=0;
int parity=0, bits=CS8, stopbits=0;
unsigned long hstart,hset;
char NullString[]={ "" };
BOOL lastcharnl=1; /* Indicate that last char printed from getonebyte
was a nl, so no new one is needed */
//"open com \"/dev/modem\"\r\nset com 38400n81\r\nset senddelay 0.05\r\nsend \"ATi^m\"\r\nget 2 \" ^m\" $s\r\nprint \"Response : \",$s,\"\\r\n\"\r\nget 2 \" ^m\" $s\r\nprint \"Response :\",$s,\"\\r\n\"\r\nget 2 \" ^m\" $s\r\nprint \"Response : \",$s,\"\\r\n\"\r\n\r\n";
/* Prototypes. */
unsigned long htime(void);
void dormir(unsigned long microsecs);
void dotestkey(void);
void ext(long xtc);
void vmsg(char *text);
void skipline(void);
void printwhere(void);
void writecom(char *text);
int getonebyte(void);
void dodump(void);
void serror(char *text, int exitcode);
void skipspaces(void);
void getopen(void);
void getclose(void);
int gettoken(void);
void skiptoken(void);
long getvalue(void);
void getcomma(void);
void gethardstring(void);
void getstring(void);
unsigned long getdvalue(void);
void dolet(void);
int dowaitquiet(void);
int dowaitfor(void);
BOOL getonoroff(void);
void setcom(void);
void doset(void);
void dogoto(void);
void dogosub(void);
unsigned char getonearg(void);
void doif(void);
int getindex(void);
int getintindex(void);
int getstringindex(void);
void doget(void);
void doprint(int channel);
void doclose(void);
void opendevice(void);
void doopen(void);
int doscript(void);
char GTdevice[4][20] = {"/dev/noz2",
"/dev/ttyUSB2",
"/dev/modem",""}; /* default device names to search for */
/* Returns hundreds of seconds */
unsigned long htime(void) {
struct timeval timenow;
gettimeofday(&timenow,NULL);
return(100L*(timenow.tv_sec-hstart)+(timenow.tv_usec)/10000L-hset);
}
/* I use select() 'cause CX/UX 6.2 doesn't have usleep().
On Linux, usleep() uses select() anyway.
*/
void dormir(unsigned long microsecs) {
struct timeval timeout;
timeout.tv_sec=microsecs/1000000L;
timeout.tv_usec=microsecs-(timeout.tv_sec*1000000L);
select(1,0,0,0,&timeout);
}
/* Tests for ENTER key */
void dotestkey(void) {
fd_set fds;
struct timeval timeout;
timeout.tv_sec=0L;
timeout.tv_usec=10000L;
FD_ZERO(&fds);
FD_SET(0,&fds); /* Prepare to select() from stdin */
resultcode=select(1,&fds,0,0,&timeout);
if(resultcode) getchar();
}
/* Exit after resetting terminal settings */
void ext(long xtc) {
ioctl(1, TCSETA, &cons);
exit(xtc);
}
/* Log message if verbose is on */
void vmsg(char *text) {
time_t t;
char *ct;
if(verbose) {
if(lastcharnl==0) {
fprintf(stderr,"\r\n");
lastcharnl=1;
}
t=time(0);
ct=ctime(&t);
fprintf(stderr,"comgt %c%c:%c%c:%c%c -> %s\r\n",
ct[11],ct[12],ct[14],ct[15],ct[17],ct[18],
text);
}
}
/* Skip to next statement */
void skipline(void) {
while(script[pc]!='\n' && script[pc]!=0) pc++;
if(script[pc]) pc++;
}
void printwhere(void) {
int a,b,c;
sprintf(msg,"@%04d ",pc);
a=pc;
skipline();
b=pc-1;
pc=a;
c=strlen(msg);
for(;a<b;a++) msg[c++]=script[a];
msg[c]=0;
vmsg(msg);
}
/* Write a null-terminated string to communication device */
void writecom(char *text) {
int res;
unsigned int a;
char ch;
for(a=0;a<strlen(text);a++) {
ch=text[a];
res=write(comfd,&ch,1);
if(senddelay) dormir(senddelay);
if(res!=1) {
serror("Could not write to COM device",1);
}
}
}
/* Gets a single byte from comm. device. Return -1 if none avail. */
int getonebyte(void) {
fd_set rfds;
int res;
char ch;
comecho = 1;
struct timeval timeout;
timeout.tv_sec=0L;
timeout.tv_usec=10000;
FD_ZERO(&rfds);
FD_SET(comfd, &rfds);
res=select(comfd+1,&rfds,NULL,NULL,&timeout);
if(res) {
res=read(comfd,&ch,1);
if(res==1) {
if(comecho) {
if(ch=='\n') lastcharnl=1;
else {
if(ch!='\r') lastcharnl=0;
}
/*fputc(ch,stderr);*/
}
return(ch);
}
}
else {
return(-1); /* Nada. */
}
return(0);
}
void dodump(void) {
char lmsg[STRINGL];
int a,b,c;
c=verbose;
verbose=1;
sprintf(lmsg,"-- Integer variables --"); vmsg(lmsg);
for(a=0;a<26;a++) {
for(b=0;b<11;b++) {
if(intvars[b*26+a]) {
sprintf(lmsg," = %ld",intvars[b*26+a]);
lmsg[1]='a'+a;
lmsg[2]=' ';
if(b) lmsg[2]='0'+b-1;
vmsg(lmsg);
}
}
}
sprintf(lmsg,"-- String variables --"); vmsg(lmsg);
for(a=0;a<26;a++) {
for(b=0;b<11;b++) {
if(stringvars[b*26+a]!=NullString) {
sprintf(lmsg,"$ = \"%s\"",stringvars[b*26+a]);
lmsg[1]='a'+a;
lmsg[2]=' ';
if(b) lmsg[2]='0'+b-1;
vmsg(lmsg);
}
}
}
verbose=c;
}
/* Report script errors and quit */
void serror(char *text, int exitcode) {
int a,line;
char lmsg[STRINGL];
verbose=1;
//dodump();
vmsg("-- Error Report --");
a=pc;
pc=lastpc;
//printwhere();
pc=a;
while(pc!=0 && (script[pc]==' ' || script[pc]=='\t')) pc--;
strcpy(lmsg,"----> ");
for(a=6;a<STRINGL;a++) lmsg[a]=' ';
for(a=0;a<pc-lastpc;a++) {
if(script[a+lastpc]=='\t') lmsg[a+8]='\t';
}
a+=6;
lmsg[a++]='^';
lmsg[a]=0;
vmsg(lmsg);
a=0; line=1;
while(a<pc) {
if(script[a++]=='\n') {
if(a<pc) line++;
}
}
sprintf(lmsg,"Error @%d, line %d, %s. (%d)\r\n",pc,line,text,exitcode);
vmsg(lmsg);
ext(exitcode);
}
void skipspaces(void) {
while(script[pc]==' ' || script[pc]=='\t' ) pc++;
}
void getopen(void) {
int a;
a=pc;
skipspaces();
if(script[pc++]!='(') {
pc=a;
serror("Function requires open parenthesis",5);
}
}
void getclose(void) {
int a;
a=pc;
skipspaces();
if(script[pc++]!=')') {
pc=a;
serror("Function requires close parenthesis",5);
}
}
void skiptoken(void) {
skipspaces();
while(script[pc]!=' ' && script[pc]!='\t' && script[pc]!='\n' ) pc++;
skipspaces();
}
/* Parse script[pc] to get next statement. Resolve comments and labels... */
int gettoken(void) {
int tokenp=0;
skipspaces();
if(script[pc]=='=' || script[pc]=='<' || script[pc]=='>' || script[pc]=='!') {
token[0]=0;
while(script[pc]=='=' || script[pc]=='<' || script[pc]=='>' || script[pc]=='!') {
token[tokenp++]=script[pc++];
}
token[tokenp]=0;
return(0);
}
if(script[pc]=='#') {
strcpy(token,"rem");
pc++;
return(0);
}
if(script[pc]=='/' && script[pc+1]=='/') {
strcpy(token,"rem");
pc+=2;
return(0);
}
if(script[pc]==':') {
strcpy(token,"label");
pc++;
return(0);
}
while(script[pc]!=' ' && script[pc]!='\n' &&
script[pc]!='\t' && script[pc]!='(') {
token[tokenp]=script[pc++];
if(token[tokenp]>='A' && token[tokenp]<='Z') {
token[tokenp]=token[tokenp]-'A'+'a';
}
if(tokenp++==MAXTOKEN-1) serror("Token too long",5);
}
token[tokenp]=0;
if(strcmp(token,"then")==0) return(gettoken()); /* Ignore then for if */
return(0);
}
/* shitfaced recursive value parser. must write better one */
long getvalue(void) {
unsigned long p=0;
int goone=1;
unsigned int a;
int base,amode;
skipspaces();
if(script[pc]=='(') {
pc++;
p=getvalue();
getclose();
}
else if(script[pc]==')') {
return(p);
}
else if(script[pc]=='"' || script[pc]=='\'' || script[pc]=='$' ) {
serror("Did not expect a string",7);
}
else if( (script[pc]>='a' && script[pc]<='z' &&
script[pc+1]>='a' && script[pc+1]<='z') ||
(script[pc]>='A' && script[pc]<='Z' &&
script[pc+1]>='A' && script[pc+1]<='Z') ) {
gettoken();
getopen();
if(strcmp(token,"len")==0) {
char toto[STRINGL];
strcpy(toto,string);
getstring();
p=strlen(string);
strcpy(string,toto);
}
else if(strcmp(token,"htime")==0) {
p=htime();
}
else if(strcmp(token,"time")==0) {
p=time(0);
}
else if(strcmp(token,"pid")==0) {
p=getpid();
}
else if(strcmp(token,"ppid")==0) {
p=getppid();
}
else if(strcmp(token,"verbose")==0) {
p=verbose;
}
else if(strcmp(token,"isatty")==0) {
p=isatty(getvalue());
}
else if(strcmp(token,"baud")==0) {
p=atol(cspeed);
}
else if(strcmp(token,"access")==0) {
char toto[STRINGL];
char afile[STRINGL];
strcpy(toto,string);
getstring();
strcpy(afile,string);
getcomma();
getstring();
if(string[0]==0) serror("Missing access mode[s]",5);
amode=0;
for(a=0;a<strlen(string);a++) {
switch(string[a]) {
case 'R' :
case 'r' : amode|=R_OK; break;
case 'W' :
case 'w' : amode|=W_OK; break;
case 'X' :
case 'x' : amode|=X_OK; break;
case 'F' :
case 'f' : amode|=F_OK; break;
default : serror("Access modes are r,w,x, and f",5);
}
}
p=access(afile,amode);
strcpy(string,toto);
}
else if(strcmp(token,"val")==0 || strcmp(token,"atol")==0) {
char toto[STRINGL];
strcpy(toto,string);
getstring();
p=atol(string);
strcpy(string,toto);
}
else serror("Unknown Integer function",5);
getclose();
}
if(script[pc]=='%') {
pc++;
skipspaces();
p=resultcode;
}
while(goone) {
if(script[pc]=='+') {
pc++;
p+=getvalue();
}
else if(script[pc]=='-') {
pc++;
p-=getvalue();
}
else if(script[pc]=='^') {
pc++;
p^=getvalue();
}
else if(script[pc]=='&') {
pc++;
p&=getvalue();
}
else if(script[pc]=='|') {
pc++;
p|=getvalue();
}
else if(script[pc]=='*') {
pc++;
p*=getvalue();
}
else if(script[pc]=='/') {
pc++;
a=getvalue();
if(a==0) serror("Division by zero",6);
p/=a;
}
else if((script[pc]>='a' && script[pc]<='z') ||
(script[pc]>='A' && script[pc]<='Z') ) {
p=intvars[getintindex()];
}
else if(script[pc]>='0' && script[pc]<='9') {
base=10;
if(script[pc]=='0') {
base=8;
pc++;
if(script[pc]=='x' || script[pc]=='X') {
base=16;
pc++;
}
if(script[pc]=='%') {
base=2;
pc++;
}
}
while((script[pc]>='0' && script[pc]<='9') ||
(script[pc]>='a' && script[pc]<='f') ||
(script[pc]>='A' && script[pc]<='F')) {
if(script[pc]>='a' && script[pc]<='f') {
p=p*base+script[pc++]-'a'+10;
}
else if(script[pc]>='A' && script[pc]<='F') {
p=p*base+script[pc++]-'A'+10;
}
else {
p=p*base+script[pc++]-'0';
}
}
}
else {
goone=0;
}
}
return(p);
}
void getcomma(void) {
skipspaces();
if(script[pc++]!=',') serror("Comma expected",5);
}
void gethardstring(void) {
int a=0;
skipspaces();
while(script[pc]!=0 && script[pc]!=' ' && script[pc]!='\t' &&
script[pc]!='\n') {
string[a++]=script[pc++];
}
string[a]=0;
}
/* Parse a string from script[pc] */
void getstring(void) {
FILE *fp;
time_t t;
unsigned int a,b;
int c,p=0;
unsigned char ch,match;
string[0]=0;
skipspaces();
if(script[pc]!='"' && script[pc]!='\'' && script[pc]!='$' ) {
serror("Expected a string",7);
}
while(script[pc]!=' ' && script[pc]!='\t' && script[pc]!='\n' && script[pc]!=',' && script[pc]!=')' && script[pc]!='=' && script[pc]!='<' && script[pc]!='>' && script[pc]!='!' ) {
if(script[pc]=='+') pc++;
skipspaces();
if( (script[pc]=='$' && script[pc+1]>='a' && script[pc+1]<='z' &&
script[pc+2]>='a' && script[pc+2]<='z') ||
(script[pc]=='$' && script[pc+1]>='A' && script[pc+1]<='Z' &&
script[pc+2]>='A' && script[pc+2]<='Z') ) {
pc++;
gettoken();
getopen();
if(strcmp(token,"time")==0) {
t=time(0);
strcat(string,ctime(&t));
string[strlen(string)-1]=0;
}
else if(strcmp(token,"rpipe")==0) {
char toto[STRINGL];
strcpy(toto,string);
getstring();
if((fp=popen(string,"r"))==NULL) serror("Could not popen!",4);
fgets(string,STRINGL-1,fp);
string[strlen(string)-1]=0;
pclose(fp);
strcat(toto,string);
strcpy(string,toto);
}
else if(strcmp(token,"env")==0) {
char toto[STRINGL];
strcpy(toto,string);
getstring();
if(getenv(string)) strcat(toto,(char *)getenv(string));
strcpy(string,toto);
}
else if(strcmp(token,"hms")==0) {
long sec,min,hour;
sec=getvalue();
min=sec/60L;
sec-=min*60L;
hour=min/60L;
min-=hour*60L;
sprintf(string,"%s%02ld:%02ld:%02ld",string,hour,min,sec);
}
else if(strcmp(token,"dev")==0) {
strcat(string,device);
}
else if(strcmp(token,"cwd")==0) {
getcwd(string,STRINGL);
}
else if(strcmp(token,"baud")==0) {
strcat(string,cspeed);
}
else if(strcmp(token,"str")==0 || strcmp(token,"ltoa")==0) {
sprintf(string,"%s%ld",string,getvalue());
}
else if(strcmp(token,"hexu")==0) {
sprintf(string,"%s%lX",string,getvalue());
}
else if(strcmp(token,"hex")==0) {
sprintf(string,"%s%lx",string,getvalue());
}
else if(strcmp(token,"oct")==0) {
sprintf(string,"%s%lo",string,getvalue());
}
else if(strcmp(token,"dirname")==0) {
char toto[STRINGL];
strcpy(toto,string);
getstring();
b=0;
for(a=0;a<strlen(string);a++) {
if(string[a]=='/' || string[a]=='\\') b=a;
}
string[b]=0;
strcat(toto,string);
strcpy(string,toto);
}
else if(strcmp(token,"tolower")==0) {
char toto[STRINGL];
strcpy(toto,string);
getstring();
for(a=0;a<strlen(string);a++) {
if(string[a]>='A' && string[a]<='Z' ) string[a]=string[a]-'A'+'a';
}
strcat(toto,string);
strcpy(string,toto);
}
else if(strcmp(token,"toupper")==0) {
char toto[STRINGL];
strcpy(toto,string);
getstring();
for(a=0;a<strlen(string);a++) {
if(string[a]>='a' && string[a]<='z' ) string[a]=string[a]-'a'+'A';
}
strcat(toto,string);
strcpy(string,toto);
}
else if(strcmp(token,"basename")==0) {
char toto[STRINGL];
strcpy(toto,string);
getstring();
b=0;
for(a=0;a<strlen(string);a++) {
if(string[a]=='/' || string[a]=='\\') b=a+1;
}
a=strlen(toto);
while(string[b]) toto[a++]=string[b++];
toto[a]=0;
strcpy(string,toto);
}
else if(strcmp(token,"script")==0) {
strcat(string,scriptfile);
}
else if(strcmp(token,"right")==0) {
char toto[STRINGL];
strcpy(toto,string);
getstring();
getcomma();
b=getvalue();
if(b>strlen(string)) serror("String is shorter than second argument",7);
c=strlen(toto);
a=strlen(string)-b;
while(b!=0 && string[a]!=0) {
toto[c++]=string[a++];
b--;
}
toto[c]=0;
strcpy(string,toto);
}
else if(strcmp(token,"left")==0) {
char toto[STRINGL];
strcpy(toto,string);
getstring();
getcomma();
b=getvalue();
if(b>strlen(string)) serror("String is shorter than second argument",7);
c=strlen(toto);
a=0;
while(b!=0 && string[a]!=0) {
toto[c++]=string[a++];
b--;
}
toto[c]=0;
strcpy(string,toto);
}
else if(strcmp(token,"mid")==0) {
char toto[STRINGL];
strcpy(toto,string);
getstring();
getcomma();
a=getvalue();
getcomma();
b=getvalue();
if(a>strlen(string)) serror("String is shorter than second argument",7);
c=strlen(toto);
while(b!=0 && string[a]!=0) {
toto[c++]=string[a++];
b--;
}
toto[c]=0;
strcpy(string,toto);
}
else if(strcmp(token,"sex")==0) {
strcat(string,"You're a naughty boy, you!");
}
else serror("Invalid string funtion",5);
getclose();
}
else if(script[pc]=='$') {
strcat(string,stringvars[getstringindex()]);
}
else if(script[pc]=='"' || script[pc]=='\'') {
match=script[pc++];
while(script[pc]!=match) {
ch=script[pc++];
if(ch==0) serror("Umatched quote.",5);
if(ch=='\\') {
if(script[pc]<='7' && script[pc]>='0' &&
script[pc+1]<='7' && script[pc+1]>='0' ) {
ch=0;
while(script[pc]>='0' && script[pc]<='7') {
ch=8*ch+script[pc++]-'0';
}
}
else {
switch(script[pc]) {
case 'T' :
case 't' : ch=9; break;
case 'R' :
case 'r' : ch=13; break;
case 'N' :
case 'n' : ch=10; break;
case 'B' :
case 'b' : ch=8; break;
case 'F' :
case 'f' : ch=12; break;
case '"' :
case '^' :
case '\'' :
case '\\' : ch=script[pc]; break;
default : serror("Malformed escaped character",5);
}
pc++;
}
}
else if(ch=='^') {
ch=script[pc];
if(ch!='^' && ch!='"' && ch!='\'' && ch!='\\' ) {
ch=ch&31; /* Control char */
}
pc++;
}
p=strlen(string);
string[p++]=ch;
string[p]=0;
}
pc++; /* Space over quote */
}
else {
p=strlen(string);
string[p++]=script[pc++];
string[p]=0;
}
}
}
/* Get a value, multiply by a hundred (for time values) */
unsigned long getdvalue(void) {
float f;
gettoken();
skipspaces();
sscanf(token,"%f",&f);
f+=0.00001; /* Rounding errors */
return(100.0*f);
}
void dolet(void) {
int index;
BOOL svar=0;
skipspaces();
if(script[pc]=='$') {
svar=1;
index=getstringindex();
}
else index=getintindex();
skipspaces();
gettoken();
if(strcmp(token,"=")!=0) serror("Bad LET assignment, '=' missing",5);
skipspaces();
if(svar) {
getstring();
strcpy(stringvars[index],string);
}
else {
intvars[index]=getvalue();
}
}
/* See documentation for doXXX() functions */
int dowaitquiet(void) {
unsigned long timeout,timequiet,quiet,now;
int c,quit;
timeout=htime()+getdvalue();
quiet=getdvalue();
timequiet=htime()+quiet;
quit=1;
while(quit==1) {
now=htime();
c=getonebyte();
if(c!= -1) timequiet=now+quiet;
if(now>=timequiet) quit=0;
if(now>=timeout) quit=255;
}
return(quit);
}
int dowaitfor(void) {
char strings[20][80];
char buffer[128];
unsigned long timeout;
unsigned int a;
int b,c;
b=0;
buffer[127]=0;
skipspaces();
timeout=htime()+getdvalue();
while(script[pc]==',' || script[pc]=='$' || script[pc]=='"' ||
script[pc]=='\'' ) {
if(script[pc]==',') pc++;
getstring();
skipspaces();
strcpy(strings[b],string);
if(ignorecase) {
for(a=0;a<strlen(strings[b]);a++) {
if(strings[b][a]>='A' && strings[b][a]<='Z') {
strings[b][a]=strings[b][a]-'A'+'a';
}
}
}
b++;
}
strings[b][0]=0;
while(htime()<timeout) {
c=getonebyte();
//printf("Byte \"%c\" ",c);
if(c!= -1) {
if(ignorecase) {
if(c>='A' && c<='Z') c=c-'A'+'a';
}
for(a=0;a<127;a++) buffer[a]=buffer[a+1]; //shuffle down
buffer[126]=c;
b=0;
while(strings[b][0]) {
c=strlen(strings[b]);
if (strcmp(strings[b],&buffer[127-c]) == 0){
return(b);
}
b++;
}
}
}
return(-1);
}
/* Parse script for "on" or "off" wich are tokens, not strings */
BOOL getonoroff(void) {
int a,b;
b=pc;
gettoken();
if(strcmp(token,"on")==0) return(1);
if(strcmp(token,"off")==0) return(0);
pc=b;
a=getvalue();
if(a!=0 && a!=1) serror("Bad value (should be on or off, 1 or 0.)",5);
return(a);
}
void setcom(void) {
stbuf.c_cflag &= ~(CBAUD | CSIZE | CSTOPB | CLOCAL | PARENB);
stbuf.c_cflag |= (speed | bits | CREAD | clocal | parity | stopbits );
if (ioctl(comfd, TCSETA, &stbuf) < 0) {
serror("Can't ioctl set device",1);
}
}
void doset(void) {
struct termio console;
int a,b;
gettoken();
if(strcmp(token,"echo")==0) {
a=0;
if(getonoroff()) a=ECHO|ECHOE;
if(ioctl(0, TCGETA, &console)<0) {
serror("Can't ioctl FD zero!\r\n",2);
}
console.c_lflag &= ~(ECHO | ECHOE);
console.c_lflag |= a;
ioctl(0, TCSETA, &console);
}
else if(strcmp(token,"senddelay")==0) {
senddelay=10000L*getdvalue();
}
else if(strcmp(token,"clocal")==0) {
clocal=0;
if(getonoroff()) clocal=CLOCAL;
setcom();
}
else if(strcmp(token,"umask")==0) {
umask(getvalue()&0777);
}
else if(strcmp(token,"verbose")==0) {
verbose=getonoroff();
}
else if(strcmp(token,"comecho")==0) {
comecho=getonoroff();
}
else if(strcmp(token,"ignorecase")==0) {
ignorecase=getonoroff();
}
else if(strcmp(token,"com")==0) {
skipspaces();
if(script[pc]=='$' || script[pc]=='\'' || script[pc]=='"') {
getstring();
strcpy(token,string);
}
else gettoken();
a=0;
b=0;
while(token[b]>='0' && token[b]<='9') {
a=10*a+token[b++]-'0';
}
if(token[b]) {
switch(token[b]) {
case 'n': parity=0; break;
case 'e': parity=PARENB; break;
case 'o': parity=PARENB|PARODD; break;
default : serror("Parity can only ben E, N, or O",5);
}
b++;
if(token[b]) {
switch(token[b]) {
case '5' : bits=CS5; break;
case '6' : bits=CS6; break;
case '7' : bits=CS7; break;
case '8' : bits=CS8; break;
default : serror("Bits can only be 5, 6, 7, or 8",5);
}
b++;
if(token[b]) {
switch(token[b]) {
case '1': stopbits=0; break;
case '2': stopbits=CSTOPB; break;
default : serror("Stop bits can only be 1 or 2",5);
}
}
}
}
sprintf(cspeed,"%d",a);