baja.c 84.2 KB
Newer Older
1 2 3 4
/* baja.c */

/* Synchronet command shell/module compiler */

5
/* $Id: baja.c,v 1.52 2020/05/14 07:49:59 rswindell Exp $ */
6 7 8 9 10

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
11
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
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
 *																			*
 * 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.					*
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

rswindell's avatar
rswindell committed
38 39 40 41 42 43 44
/* OS-specific */
#ifndef __unix__
	#include <io.h>
	#include <share.h>
#endif

/* ANSI */
45 46 47 48
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
rswindell's avatar
rswindell committed
49
#include <errno.h>
rswindell's avatar
rswindell committed
50 51

/* Synchronet-specific */
52 53 54
#include "cmdshell.h"
#include "ars_defs.h"
#include "crc32.h"
rswindell's avatar
rswindell committed
55
#include "genwrap.h"	/* portability wrappers */
56
#include "dirwrap.h"	/* MAX_PATH */
57 58

#ifdef __BORLANDC__
59
unsigned _stklen=20000;	/* Set stack size in code, not header */
60 61 62 63 64 65 66 67
#endif

char **label_name=NULL
	,**goto_file=NULL
	,**goto_label=NULL
	,**call_file=NULL
	,**call_label=NULL;

deuce's avatar
64-bit  
deuce committed
68
uint32_t *var_name=NULL,vars=0;
69 70 71 72 73 74 75 76 77 78 79 80 81

char **define_str=NULL
	,**define_val=NULL;

char *linestr="%s %d: %s\n";
char tmp[256];

uint *label_indx=NULL
	,*goto_indx=NULL
	,*goto_line=NULL
	,*call_indx=NULL
	,*call_line=NULL;

82 83 84
char bin_file[MAX_PATH+1];
char output_dir[MAX_PATH+1];
char include_dir[MAX_PATH+1];
85

86
uint display=0,line=0,labels=0,gotos=0,calls=0,defines=0,case_sens=0;
87
BOOL pause_on_error=FALSE;
88 89 90

FILE *out=NULL;

91
void bail(int retval)
92
{
93 94 95
	if(out) 
		fclose(out);

96 97 98
	if(retval!=0) {
		if(bin_file[0]!=0)
			remove(bin_file);
99
		if(pause_on_error) {
100 101 102 103
			printf("\nHit enter to contiue...");
			getchar();
		}
	}
104 105

	exit(retval);
106 107 108
}

/****************************************************************************/
rswindell's avatar
rswindell committed
109
/* Converts an ASCII Hex string into an ulong								*/
110
/****************************************************************************/
deuce's avatar
64-bit  
deuce committed
111
uint32_t ahtoul(char *str)
112
{
deuce's avatar
64-bit  
deuce committed
113
	uint32_t l,val=0;
114

rswindell's avatar
rswindell committed
115 116 117
	while((l=(*str++)|0x20)!=0x20)
		val=(l&0xf)+(l>>6&1)*9+val*16;
	return(val);
118 119 120 121 122 123
}

/* C Escape char */

uchar cesc(char ch)
{
rswindell's avatar
rswindell committed
124
	switch(ch) {
125 126
		case 'e':
			return(ESC);
rswindell's avatar
rswindell committed
127 128 129 130 131 132 133 134 135
		case 'r':
			return(CR);
		case 'n':
			return(LF);
		case 't':
			return(TAB);
		case 'b':
			return(BS);
		case 'a':
136
			return(BEL);
rswindell's avatar
rswindell committed
137 138 139 140 141 142 143
		case 'f':
			return(FF);
		case 'v':
			return(11);
		default:
			return(ch); 
	}
144 145
}

deuce's avatar
64-bit  
deuce committed
146
int32_t val(char *src, char *p)
147 148
{
	static int inside;
deuce's avatar
64-bit  
deuce committed
149
	int32_t l;
150

151
	if(IS_DIGIT(*p) || *p=='-')      /* Dec, Hex, or Oct */
rswindell's avatar
rswindell committed
152 153
		l=strtol(p,&p,0);
	else if(*p=='\'') {  /* Char */
154
		p++;
rswindell's avatar
rswindell committed
155 156 157 158 159 160 161 162 163
		if(*p=='\\') {
			p++;
			l=cesc(*p); }
		else
			l=*p;
		p++; }
	else if(*p=='.')    /* Bit */
		l=1L<<strtol(p+1,&p,0);
	else {
164
		printf("!SYNTAX ERROR (expecting integer constant):\n");
rswindell's avatar
rswindell committed
165
		printf(linestr,src,line,*p ? p : "<end of line>");
166
		bail(1);
rswindell's avatar
rswindell committed
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
		return(0); }
	if(inside) {
		return(l); }
	inside=1;
	while(*p)
		switch(*(p++)) {
			case '+':
				l+=val(src,p);
				break;
			case '-':
				l-=val(src,p);
				break;
			case '*':
				l*=val(src,p);
				break;
			case '/':
				l/=val(src,p);
				break;
			case '%':
				l%=val(src,p);
				break;
			case '&':
				l&=val(src,p);
				break;
			case '|':
				l|=val(src,p);
				break;
			case '~':
				l&=~val(src,p);
				break;
			case '^':
				l^=val(src,p);
				break;
			case '>':
				if(*p=='>') {
					p++;
					l>>=val(src,p); }
				break;
			case '<':
				if(*p=='<') {
					p++;
					l<<=val(src,p); }
				break;
210
			case ' ':
rswindell's avatar
rswindell committed
211 212 213 214 215
			case '#':
				inside=0;
				return(l); }
	inside=0;
	return(l);
216 217 218
}


219
void writecstr(char *p)
220 221 222 223
{
	char str[1024];
	int j=0,inquotes=0;

rswindell's avatar
rswindell committed
224 225 226 227 228 229 230 231 232
	while(*p) {
		if(*p=='"') {   /* ignore quotes */
			if(inquotes)
				break;
			inquotes=1;
			p++;
			continue; }
		if(*p=='\\')    { /* escape */
			p++;
233
			if(IS_DIGIT(*p)) {
234 235
				sprintf(tmp,"%.3s",p);
				str[j]=atoi(tmp); 		/* decimal, NOT octal */
236 237
				if(IS_DIGIT(*(++p))) 	/* skip up to 3 digits */
					if(IS_DIGIT(*(++p)))
rswindell's avatar
rswindell committed
238 239 240 241 242 243 244
						p++;
				j++;
				continue; }
			switch(*(p++)) {
				case 'x':
					tmp[0]=*(p++);
					tmp[1]=0;
245
					if(isxdigit((uchar)*p)) {	/* if another hex digit, skip too */
rswindell's avatar
rswindell committed
246 247 248 249
						tmp[1]=*(p++);
						tmp[2]=0; }
					str[j]=(char)ahtoul(tmp);
					break;
250 251 252
				case 'e':
					str[j]=ESC;
					break;
rswindell's avatar
rswindell committed
253 254 255 256 257 258 259 260 261 262 263 264 265
				case 'r':
					str[j]=CR;
					break;
				case 'n':
					str[j]=LF;
					break;
				case 't':
					str[j]=TAB;
					break;
				case 'b':
					str[j]=BS;
					break;
				case 'a':
266
					str[j]=BEL;
rswindell's avatar
rswindell committed
267 268 269 270 271 272 273 274 275 276
					break;
				case 'f':
					str[j]=FF;
					break;
				case 'v':
					str[j]=11;	/* VT */
					break;
				default:
					str[j]=*(p-1);
					break; }
277 278
			j++;
			continue; }
rswindell's avatar
rswindell committed
279 280 281
		str[j++]=*(p++); }
	str[j]=0;
	fwrite(str,1,j+1,out);
282 283
}

284
void writestr(char *p)
285 286 287 288
{
	char str[1024];
	int j=0;

rswindell's avatar
rswindell committed
289 290 291 292 293 294 295 296 297
	while(*p) {
		if(*p=='"') {   /* ignore quotes */
			p++;
			continue; }
		if(*p=='\\' && *(p+1)=='"' && *(p+2))
			p++;
		str[j++]=*(p++); }
	str[j]=0;
	fwrite(str,1,j+1,out);
298 299 300 301 302 303
}

void cvttab(char *str)
{
	int i;

rswindell's avatar
rswindell committed
304 305
	for(i=0;str[i];i++)
		if(str[i]==TAB)
306
			str[i]=' ';
307 308
}

309
void newvar(char* src, char *in)
310
{
311
	char name[128];
deuce's avatar
64-bit  
deuce committed
312
	int32_t i,l;
313

314
	if(IS_DIGIT(*in)) {
315 316 317 318 319
		printf("!SYNTAX ERROR (illegal variable name):\n");
		printf(linestr,src,line,(char*)in);
		bail(1); 
	}

rswindell's avatar
rswindell committed
320
	sprintf(name,"%.80s",in);
321 322 323 324 325 326 327 328 329 330 331 332
	if(strncmp(name,"var_",4)==0)	/* decompiled source? */
		l=strtoul(name+4,NULL,16);
	else {
		if(!case_sens)
			strupr(name);
		l=crc32(name,0);
		for(i=0;i<vars;i++)
			if(var_name[i]==l)
				break;
		if(i<vars)
			return;
	}
deuce's avatar
64-bit  
deuce committed
333
	if((var_name=(uint32_t *)realloc(var_name,sizeof(int32_t)*(vars+1)))==NULL) {
deuce's avatar
deuce committed
334
		printf("Too many (%"PRIu32") variables!\n",vars);
335
		bail(1); }
rswindell's avatar
rswindell committed
336 337
	var_name[vars]=l;
	if(display)
deuce's avatar
deuce committed
338
		printf("newvar(%08"PRIX32")=%s\n",l,in);
rswindell's avatar
rswindell committed
339
	vars++;
340 341
}

342
void writecrc(char *src, char *in)
343
{
344 345
	char	name[128];
	char*	p;
deuce's avatar
64-bit  
deuce committed
346 347
	int32_t	l;
	int		i;
348

rswindell's avatar
rswindell committed
349 350
	/* Automatically terminate variable name Oct-09-2000 rswindell */
	sprintf(name,"%.80s",in);
351
	p=strchr(name,' ');
rswindell's avatar
rswindell committed
352
	if(p) *p=0;
353

rswindell's avatar
rswindell committed
354 355
	if(!stricmp(name,"STR") || !name[0])
		l=0;
356 357
	else if(strncmp(name,"var_",4)==0)	/* decompiled source? */
		l=strtoul(name+4,NULL,16);
rswindell's avatar
rswindell committed
358
	else {
359 360
		if(!case_sens)
			strupr(name);
rswindell's avatar
rswindell committed
361
		l=crc32(name,0);
362

rswindell's avatar
rswindell committed
363 364 365 366
		for(i=0;i<vars;i++)
			if(var_name[i]==l)
				break;
		if(i==vars) {
367
			printf("!SYNTAX ERROR (expecting variable name):\n");
rswindell's avatar
rswindell committed
368
			printf(linestr,src,line,*in ? (char*)in : "<end of line>");
369
			bail(1); 
370
		}
rswindell's avatar
rswindell committed
371 372
	}
	fwrite(&l,4,1,out);
373 374
}

375
int32_t isvar(char *arg)
376
{
377
	char name[128],*p;
deuce's avatar
64-bit  
deuce committed
378
	int32_t i,l;
379

380
	if(!arg || !(*arg) || IS_DIGIT(*arg))
rswindell's avatar
rswindell committed
381 382 383
		return(0);

	sprintf(name,"%.80s",arg);
384
	if((p=strchr(name,' '))!=NULL)	/* Truncate at first space */
rswindell's avatar
rswindell committed
385
		*p=0;
386 387
	if(strncmp(name,"var_",4)==0)	/* decompiled source? */
		return(strtoul(name+4,NULL,16));
rswindell's avatar
rswindell committed
388 389
	if(!case_sens)
		strupr(name);
rswindell's avatar
rswindell committed
390
	l=crc32(name,0);
rswindell's avatar
rswindell committed
391 392 393 394 395 396 397

	for(i=0;i<vars;i++)
		if(var_name[i]==l)
			break;
	if(i==vars)
		return(0);
	return(l);
398 399 400 401
}

int str_cmp(char *s1, char *s2)
{
rswindell's avatar
rswindell committed
402 403 404
	if(case_sens)
		return(strcmp(s1,s2));
	return(stricmp(s1,s2));
405 406
}

407
void expdefs(char *line)
408
{
409
	char str[512],*p,*sp,sav[2]={0};
410 411
	int i;

rswindell's avatar
rswindell committed
412 413
	str[0]=0;
	for(p=line;*p;p++) {
414
		if(*p==' ') {
rswindell's avatar
rswindell committed
415 416
			strcat(str," ");
			continue; }
417

rswindell's avatar
rswindell committed
418 419 420 421 422 423 424 425 426 427 428
		if(*p=='"') {               /* Skip quoted text */
			sp=strchr(p+1,'"');
			if(sp) *sp=0;
			strcat(str,p);
			if(!sp)
				break;
			strcat(str,"\"");
			p+=strlen(p);
			continue; }

		for(sp=p;*sp;sp++)
429
			if(!IS_ALPHANUMERIC(*sp) && *sp!='_')
rswindell's avatar
rswindell committed
430 431 432 433 434 435 436 437 438 439 440 441
				break;
		sav[0]=*sp; 		/* Save delimiter */
		sav[1]=0;
		*sp=0;
		for(i=0;i<defines;i++)
			if(!str_cmp(define_str[i],p))
				break;
		if(i<defines)
			strcat(str,define_val[i]);
		else
			strcat(str,p);
		if(!sav[0]) 		/* Last argument */
442
			break;
rswindell's avatar
rswindell committed
443 444 445 446
		p+=strlen(p);
		strcat(str,sav);	/* Restore delimiter */
		}
	strcpy(line,str);
447 448 449
}


450
#define SKIPCTRLSP(p) while(*(p)<=' ' && *(p)>0) (p)++
451 452 453

void compile(char *src)
{
454 455
	char *str,*save,*p,*sp,*tp,*arg,*arg2,*arg3,*arg4,ch;
	uchar *ar;
456
	char path[MAX_PATH+1];
deuce's avatar
64-bit  
deuce committed
457
	uint16_t i;
deuce's avatar
64-bit  
deuce committed
458
    uint16_t j;
deuce's avatar
64-bit  
deuce committed
459 460
	int32_t l;
	int savline;
461 462
	FILE *in;

rswindell's avatar
rswindell committed
463
	if((in=fopen(src,"rb"))==NULL) {
464
		printf("error %d opening %s for read\n",errno,src);
465
		bail(1); }
rswindell's avatar
rswindell committed
466
	line=0;
467

rswindell's avatar
rswindell committed
468 469
	if((str=malloc(1024))==NULL) {
		printf("malloc error\n");
470
		bail(1);
rswindell's avatar
rswindell committed
471
	}
472

rswindell's avatar
rswindell committed
473 474
	if((save=malloc(1024))==NULL) {
		printf("malloc error\n");
475
		bail(1);
rswindell's avatar
rswindell committed
476
	}
477

rswindell's avatar
rswindell committed
478 479
	while(!feof(in) && !ferror(in)) {
		if(!fgets(str,1000,in))
480
			break;
rswindell's avatar
rswindell committed
481 482 483 484 485
		truncsp(str);
		cvttab(str);
		line++;
		strcpy(save,str);
		p=str;
486
		SKIPCTRLSP(p);   /* look for beginning of command */
rswindell's avatar
rswindell committed
487 488 489 490 491 492 493
		if((*p)==0)
			continue;
		if(*p=='#')             /* remarks start with # */
			continue;
		expdefs(p); 			/* expand defines */
		if(display)
			printf("%s\n",p);
494
		sp=strchr(p,' ');
495
		arg=arg2=arg3=arg4="";
rswindell's avatar
rswindell committed
496
		if(sp) {
497
			*sp=0;
rswindell's avatar
rswindell committed
498
			arg=sp+1;
499
			SKIPCTRLSP(arg);
500
			sp=strchr(arg,' ');
rswindell's avatar
rswindell committed
501 502
			if(sp) {
				arg2=sp+1;
503
				SKIPCTRLSP(arg2);
504
				sp=strchr(arg2,' ');
rswindell's avatar
rswindell committed
505 506
				if(sp) {
					arg3=sp+1;
507
					SKIPCTRLSP(arg3);
508
					sp=strchr(arg3,' ');
509 510
					if(sp) {
						arg4=sp+1;
511 512 513 514 515
						SKIPCTRLSP(arg4);
					}
				}
			}
		}
rswindell's avatar
rswindell committed
516 517 518

		if(!stricmp(p,"!INCLUDE")) {
			savline=line;
519
			sp=strchr(arg,' ');
rswindell's avatar
rswindell committed
520
			if(sp) *sp=0;
521 522
			sprintf(path,"%s%s",include_dir,arg);
			compile(path);
rswindell's avatar
rswindell committed
523
			line=savline;
524 525
			continue; 
		}
526

rswindell's avatar
rswindell committed
527
		if(!stricmp(p,"!DEFINE")) {                     /* define */
528
			sp=strchr(arg,' ');
rswindell's avatar
rswindell committed
529 530
			if(sp)
				*sp=0;
531
			else
rswindell's avatar
rswindell committed
532 533 534 535 536 537 538 539
				break;
			tp=strrchr(arg2,'\"');
			if(!tp)
				tp=arg2;
			sp=strchr(tp,'#');
			if(sp)
				*sp=0;
			truncsp(arg2);
deuce's avatar
deuce committed
540
			if((define_str=(char **)realloc(define_str,sizeof(char *)*(defines+1)))
rswindell's avatar
rswindell committed
541
				==NULL) {
542
				printf("Too many defines.\n");
543
				bail(1); }
deuce's avatar
deuce committed
544
			if((define_str[defines]=(char *)malloc(strlen(arg)+1))==NULL) {
545
				printf("Too many defines.\n");
546
				bail(1); }
deuce's avatar
deuce committed
547
			if((define_val=(char **)realloc(define_val,sizeof(char *)*(defines+1)))
rswindell's avatar
rswindell committed
548
				==NULL) {
549
				printf("Too many defines.\n");
550
				bail(1); }
deuce's avatar
deuce committed
551
			if((define_val[defines]=(char *)malloc(strlen(arg2)+1))==NULL) {
552
				printf("Too many defines.\n");
553
				bail(1); }
rswindell's avatar
rswindell committed
554 555 556 557
			strcpy(define_str[defines],arg);
			strcpy(define_val[defines],arg2);
			defines++;
			continue; }
558

rswindell's avatar
rswindell committed
559 560 561
		if(!stricmp(p,"!GLOBAL")) {             /* declare global variables */
			if(!(*arg)) break;
			for(p=arg;*p && *p!='#';) {
562
				sp=strchr(p,' ');
rswindell's avatar
rswindell committed
563
				if(sp) *sp=0;
564
				newvar(src,p);
rswindell's avatar
rswindell committed
565 566 567
				if(!sp)
					break;
				p=sp+1;
568 569
				SKIPCTRLSP(p);
			}
rswindell's avatar
rswindell committed
570
			continue; }
571

rswindell's avatar
rswindell committed
572 573 574 575
		if(!stricmp(p,"PATCH")) {
			if(!(*arg)) break;
			p=arg;
			while(*p) {
576
				SKIPCTRLSP(p);
rswindell's avatar
rswindell committed
577 578 579 580 581 582 583 584
				tmp[0]=*p++;
				tmp[1]=*p++;
				tmp[2]=0;
				if(!tmp[0])
					break;
				ch=ahtoul(tmp);
				fputc(ch,out); }
			continue; }
585

rswindell's avatar
rswindell committed
586 587 588 589
		if(!stricmp(p,"SHOW_VARS")) {
			fputc(CS_VAR_INSTRUCTION,out);
			fputc(SHOW_VARS,out);
			continue; }
590

rswindell's avatar
rswindell committed
591 592 593
		if(!stricmp(p,"COMPARE_ARS")) {
			if(!(*arg)) break;
			strupr(arg);
594 595 596 597 598 599
			ar=arstr(&i,arg,NULL,NULL);
			if(ar != NULL) {
				fprintf(out,"%c%c",CS_COMPARE_ARS,(uchar)i);
				fwrite(ar,i,1,out);
				free(ar);
			}
rswindell's avatar
rswindell committed
600
			continue; }
601

rswindell's avatar
rswindell committed
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
		if(!stricmp(p,"CHKSYSPASS")) {
			fprintf(out,"%c",CS_CHKSYSPASS);
			continue; }
		if(!stricmp(p,"INFO_SYSTEM")) {
			fprintf(out,"%c",CS_INFO_SYSTEM);
			continue; }
		if(!stricmp(p,"INFO_SUBBOARD")) {
			fprintf(out,"%c",CS_INFO_SUBBOARD);
			continue; }
		if(!stricmp(p,"INFO_DIRECTORY")) {
			fprintf(out,"%c",CS_INFO_DIRECTORY);
			continue; }
		if(!stricmp(p,"INFO_VERSION")) {
			fprintf(out,"%c",CS_INFO_VERSION);
			continue; }
		if(!stricmp(p,"INFO_USER")) {
			fprintf(out,"%c",CS_INFO_USER);
			continue; }
		if(!stricmp(p,"INFO_XFER_POLICY")) {
			fprintf(out,"%c",CS_INFO_XFER_POLICY);
			continue; }
		if(!stricmp(p,"LOGKEY")) {
			fprintf(out,"%c",CS_LOGKEY);
			continue; }
		if(!stricmp(p,"LOGKEY_COMMA")) {
			fprintf(out,"%c",CS_LOGKEY_COMMA);
			continue; }
		if(!stricmp(p,"LOGSTR")) {
			fprintf(out,"%c",CS_LOGSTR);
			continue; }
632

rswindell's avatar
rswindell committed
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
		if(!stricmp(p,"ONLINE")) {
			fprintf(out,"%c%c",CS_ONE_MORE_BYTE,CS_ONLINE);
			continue; }
		if(!stricmp(p,"OFFLINE")) {
			fprintf(out,"%c%c",CS_ONE_MORE_BYTE,CS_OFFLINE);
			continue; }
		if(!stricmp(p,"NEWUSER")) {
			fprintf(out,"%c%c",CS_ONE_MORE_BYTE,CS_NEWUSER);
			continue; }
		if(!stricmp(p,"LOGON")) {
			fprintf(out,"%c%c",CS_ONE_MORE_BYTE,CS_LOGON);
			continue; }
		if(!stricmp(p,"LOGOUT")) {
			fprintf(out,"%c%c",CS_ONE_MORE_BYTE,CS_LOGOUT);
			continue; }
		if(!stricmp(p,"EXIT")) {
			fprintf(out,"%c%c",CS_ONE_MORE_BYTE,CS_EXIT);
			continue; }
651

652 653 654 655 656 657 658 659 660 661 662 663 664
		if(!stricmp(p,"LOOP") || !stricmp(p,"LOOP_BEGIN")) {
			fprintf(out,"%c%c",CS_ONE_MORE_BYTE,CS_LOOP_BEGIN);
			continue; }
		if(!stricmp(p,"CONTINUE") || !stricmp(p,"CONTINUE_LOOP")) {
			fprintf(out,"%c%c",CS_ONE_MORE_BYTE,CS_CONTINUE_LOOP);
			continue; }
		if(!stricmp(p,"BREAK") || !stricmp(p,"BREAK_LOOP")) {
			fprintf(out,"%c%c",CS_ONE_MORE_BYTE,CS_BREAK_LOOP);
			continue; }
		if(!stricmp(p,"END_LOOP")) {
			fprintf(out,"%c%c",CS_ONE_MORE_BYTE,CS_END_LOOP);
			continue; }

rswindell's avatar
rswindell committed
665 666 667 668
		if(!stricmp(p,"USER_EVENT")) {
			if(!(*arg))
				break;
			if((l=isvar(arg))!=0) {
669
				fputc(CS_USE_INT_VAR,out);
670 671 672 673
				fwrite(&l,4,1,out); /* variable */
				fputc(2,out);		/* int offset */
				fputc(1,out);       /* int length */
				ch=0; }             /* place holder */
674
			else
rswindell's avatar
rswindell committed
675 676 677 678
				ch=val(src,arg);
			fprintf(out,"%c%c",CS_TWO_MORE_BYTES,CS_USER_EVENT);
			fwrite(&ch,1,1,out);
			continue; }
679

rswindell's avatar
rswindell committed
680 681 682 683 684 685 686 687 688
		if(!stricmp(p,"PUT_NODE")) {
			fprintf(out,"%c",CS_PUT_NODE);
			continue; }
		if(!stricmp(p,"SYNC")) {
			fprintf(out,"%c",CS_SYNC);
			continue; }
		if(!stricmp(p,"ASYNC")) {
			fprintf(out,"%c",CS_ASYNC);
			continue; }
689 690
		if(!stricmp(p,"RIOSYNC")) {		/* deprecated */
			fprintf(out,"%c",CS_SYNC);
rswindell's avatar
rswindell committed
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
			continue; }
		if(!stricmp(p,"GETTIMELEFT")) {
			fprintf(out,"%c",CS_GETTIMELEFT);
			continue; }
		if(!stricmp(p,"SAVELINE")) {
			fprintf(out,"%c",CS_SAVELINE);
			continue; }
		if(!stricmp(p,"RESTORELINE")) {
			fprintf(out,"%c",CS_RESTORELINE);
			continue; }
		if(!stricmp(p,"IF_TRUE") || !stricmp(p,"IF_EQUAL")) {
			fprintf(out,"%c",CS_IF_TRUE);
			continue; }
		if(!stricmp(p,"IF_FALSE") || !stricmp(p,"IF_NOT_EQUAL")) {
			fprintf(out,"%c",CS_IF_FALSE);
			continue; }
		if(!stricmp(p,"IF_GREATER")) {
			fprintf(out,"%c",CS_IF_GREATER);
			continue; }
		if(!stricmp(p,"IF_GREATER_OR_EQUAL")
			|| !stricmp(p,"IF_EQUAL_OR_GREATER")) {
			fprintf(out,"%c",CS_IF_GREATER_OR_EQUAL);
			continue; }
		if(!stricmp(p,"IF_LESS")) {
			fprintf(out,"%c",CS_IF_LESS);
			continue; }
		if(!stricmp(p,"IF_LESS_OR_EQUAL")
			|| !stricmp(p,"IF_EQUAL_OR_LESS")) {
			fprintf(out,"%c",CS_IF_LESS_OR_EQUAL);
			continue; }
		if(!stricmp(p,"ENDIF") || !stricmp(p,"END_IF")) {
			fprintf(out,"%c",CS_ENDIF);
			continue; }
		if(!stricmp(p,"ELSE")) {
			fprintf(out,"%c",CS_ELSE);
			continue; }
		if(p[0]==':') {                     /* :label */
			p++;
729
			sp=strchr(p,' ');
rswindell's avatar
rswindell committed
730 731 732 733 734 735
			if(sp)
				*sp=0;
			for(i=0;i<labels;i++)
				if(!stricmp(label_name[i],p))
					break;
			if(i<labels) {
736
				printf("!SYNTAX ERROR (duplicate label name):\n");
rswindell's avatar
rswindell committed
737
				printf(linestr,src,line,p);
738
				bail(1); }
deuce's avatar
deuce committed
739
			if((label_name=(char **)realloc(label_name,sizeof(char *)*(labels+1)))
rswindell's avatar
rswindell committed
740 741
				==NULL) {
				printf("Too many labels.\n");
742
				bail(1); }
deuce's avatar
deuce committed
743
			if((label_indx=(uint *)realloc(label_indx,sizeof(int)*(labels+1)))
rswindell's avatar
rswindell committed
744 745
				==NULL) {
				printf("Too many labels.\n");
746
				bail(1); }
deuce's avatar
deuce committed
747
			if((label_name[labels]=(char *)malloc(strlen(p)+1))==NULL) {
748
				printf("Too many labels.\n");
749
				bail(1); }
rswindell's avatar
rswindell committed
750 751 752 753 754 755
			strcpy(label_name[labels],p);
			label_indx[labels]=ftell(out);
			labels++;
			continue; }
		if(!stricmp(p,"GOTO")) {           /* goto */
			if(!(*arg)) break;
756
			sp=strchr(arg,' ');
rswindell's avatar
rswindell committed
757 758
			if(sp)
				*sp=0;
deuce's avatar
deuce committed
759
			if((goto_label=(char **)realloc(goto_label,sizeof(char *)*(gotos+1)))
rswindell's avatar
rswindell committed
760
				==NULL) {
761
				printf("Too many gotos.\n");
762
				bail(1); }
deuce's avatar
deuce committed
763
			if((goto_file=(char **)realloc(goto_file,sizeof(char *)*(gotos+1)))
rswindell's avatar
rswindell committed
764
				==NULL) {
765
				printf("Too many gotos.\n");
766
				bail(1); }
deuce's avatar
deuce committed
767
			if((goto_indx=(uint *)realloc(goto_indx,sizeof(int)*(gotos+1)))
rswindell's avatar
rswindell committed
768
				==NULL) {
769
				printf("Too many gotos.\n");
770
				bail(1); }
deuce's avatar
deuce committed
771
			if((goto_line=(uint *)realloc(goto_line,sizeof(int)*(gotos+1)))
rswindell's avatar
rswindell committed
772
				==NULL) {
773
				printf("Too many gotos.\n");
774
				bail(1); }
deuce's avatar
deuce committed
775
			if((goto_label[gotos]=(char *)malloc(strlen(arg)+1))==NULL) {
776
				printf("Too many gotos.\n");
777
				bail(1); }
deuce's avatar
deuce committed
778
			if((goto_file[gotos]=(char *)malloc(strlen(str)+1))==NULL) {
779
				printf("Too many gotos.\n");
780
				bail(1); }
rswindell's avatar
rswindell committed
781 782 783 784 785 786 787 788 789
			strcpy(goto_label[gotos],arg);
			strcpy(goto_file[gotos],str);
			goto_indx[gotos]=ftell(out);
			goto_line[gotos]=line;
			gotos++;
			fprintf(out,"%c%c%c",CS_GOTO,0xff,0xff);
			continue; }
		if(!stricmp(p,"CALL")) {          /* call */
			if(!(*arg)) break;
790
			sp=strchr(arg,' ');
rswindell's avatar
rswindell committed
791 792
			if(sp)
				*sp=0;
deuce's avatar
deuce committed
793
			if((call_label=(char **)realloc(call_label,sizeof(char *)*(calls+1)))
rswindell's avatar
rswindell committed
794
				==NULL) {
795
				printf("Too many calls.\n");
796
				bail(1); }
deuce's avatar
deuce committed
797
			if((call_file=(char **)realloc(call_file,sizeof(char *)*(calls+1)))
rswindell's avatar
rswindell committed
798
				==NULL) {
799
				printf("Too many calls.\n");
800
				bail(1); }
deuce's avatar
deuce committed
801
			if((call_indx=(uint *)realloc(call_indx,sizeof(int)*(calls+1)))
rswindell's avatar
rswindell committed
802
				==NULL) {
803
				printf("Too many calls.\n");
804
				bail(1); }
deuce's avatar
deuce committed
805
			if((call_line=(uint *)realloc(call_line,sizeof(int)*(calls+1)))
rswindell's avatar
rswindell committed
806
				==NULL) {
807
				printf("Too many calls.\n");
808
				bail(1); }
deuce's avatar
deuce committed
809
			if((call_label[calls]=(char *)malloc(strlen(arg)+1))==NULL) {
810
				printf("Too many calls.\n");
811
				bail(1); }
deuce's avatar
deuce committed
812
			if((call_file[calls]=(char *)malloc(strlen(src)+1))==NULL) {
813
				printf("Too many calls.\n");
814
				bail(1); }
rswindell's avatar
rswindell committed
815 816 817 818 819 820 821 822

			strcpy(call_label[calls],arg);
			strcpy(call_file[calls],src);
			call_indx[calls]=ftell(out);
			call_line[calls]=line;
			calls++;
			fprintf(out,"%c%c%c",CS_CALL,0xff,0xff);
			continue; }
823

rswindell's avatar
rswindell committed
824 825 826 827 828 829 830 831 832 833 834 835
		if(!stricmp(p,"RETURN")) {
			fprintf(out,"%c",CS_RETURN);
			continue; }
		if(!stricmp(p,"CMD_HOME")) {
			fprintf(out,"%c",CS_CMD_HOME);
			continue; }
		if(!stricmp(p,"CMDKEY")) {
			if(!(*arg)) break;
			if(!stricmp(arg,"DIGIT"))
				ch=CS_DIGIT;
			else if(!stricmp(arg,"EDIGIT"))
				ch=CS_EDIGIT;
836
			else
rswindell's avatar
rswindell committed
837 838 839
				ch=toupper(*arg);
			if(ch=='/')
				ch=*(arg+1)|0x80;   /* high bit indicates slash required */
840
			else if(ch=='^' && (*(arg+1)>=0x40))
rswindell's avatar
rswindell committed
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
				ch=*(arg+1)-0x40;   /* ctrl char */
			else if(ch=='\\')
				ch=cesc(*(arg+1));
			else if(ch=='\'')
				ch=*(arg+1);
			fprintf(out,"%c%c",CS_CMDKEY,ch);
			continue; }
		if(!stricmp(p,"CMDCHAR")) {
			if(!(*arg)) break;
			fprintf(out,"%c%c",CS_CMDKEY,*arg);
			continue; }
		if(!stricmp(p,"SETLOGIC") || !stricmp(p,"SET_LOGIC")) {
			if(!(*arg)) break;
			if(!stricmp(arg,"TRUE") || !stricmp(arg,"EQUAL"))
				ch=LOGIC_TRUE;
			else if(!stricmp(arg,"GREATER"))
				ch=LOGIC_GREATER;
			else if(!stricmp(arg,"LESS"))
				ch=LOGIC_LESS;
			else
				ch=LOGIC_FALSE;
			fprintf(out,"%c%c",CS_SETLOGIC,ch);
			continue; }
864

rswindell's avatar
rswindell committed
865 866 867
		if(!stricmp(p,"DEFINE_STR_VAR") || !stricmp(p,"STR")) {
			if(!(*arg)) break;
			for(p=arg;*p && *p!='#';) {
868
				sp=strchr(p,' ');
rswindell's avatar
rswindell committed
869 870 871
				if(sp) *sp=0;
				fputc(CS_VAR_INSTRUCTION,out);
				fputc(DEFINE_STR_VAR,out);
872
				newvar(src,p);
rswindell's avatar
rswindell committed
873 874 875 876
				writecrc(src,p);
				if(!sp)
					break;
				p=sp+1;
877 878
				SKIPCTRLSP(p);
			}
rswindell's avatar
rswindell committed
879 880 881 882
			continue; }
		if(!stricmp(p,"DEFINE_INT_VAR") || !stricmp(p,"INT")) {
			if(!(*arg)) break;
			for(p=arg;*p && *p!='#';) {
883
				sp=strchr(p,' ');
rswindell's avatar
rswindell committed
884 885 886
				if(sp) *sp=0;
				fputc(CS_VAR_INSTRUCTION,out);
				fputc(DEFINE_INT_VAR,out);
887
				newvar(src,p);
rswindell's avatar
rswindell committed
888 889 890 891
				writecrc(src,p);
				if(!sp)
					break;
				p=sp+1;
892 893
				SKIPCTRLSP(p);
			}
rswindell's avatar
rswindell committed
894 895 896 897
			continue; }
		if(!stricmp(p,"DEFINE_GLOBAL_STR_VAR") || !stricmp(p,"GLOBAL_STR")) {
			if(!(*arg)) break;
			for(p=arg;*p && *p!='#';) {
898
				sp=strchr(p,' ');
rswindell's avatar
rswindell committed
899 900 901
				if(sp) *sp=0;
				fputc(CS_VAR_INSTRUCTION,out);
				fputc(DEFINE_GLOBAL_STR_VAR,out);
902
				newvar(src,p);
rswindell's avatar
rswindell committed
903 904 905 906
				writecrc(src,p);
				if(!sp)
					break;
				p=sp+1;
907 908
				SKIPCTRLSP(p);
			}
rswindell's avatar
rswindell committed
909 910 911 912
			continue; }
		if(!stricmp(p,"DEFINE_GLOBAL_INT_VAR") || !stricmp(p,"GLOBAL_INT")) {
			if(!(*arg)) break;
			for(p=arg;*p && *p!='#';) {
913
				sp=strchr(p,' ');
rswindell's avatar
rswindell committed
914 915 916
				if(sp) *sp=0;
				fputc(CS_VAR_INSTRUCTION,out);
				fputc(DEFINE_GLOBAL_INT_VAR,out);
917
				newvar(src,p);
rswindell's avatar
rswindell committed
918 919 920 921
				writecrc(src,p);
				if(!sp)
					break;
				p=sp+1;
922 923
				SKIPCTRLSP(p);
			}
rswindell's avatar
rswindell committed
924
			continue; }
925

rswindell's avatar
rswindell committed
926 927 928 929 930 931
		if(!stricmp(p,"LOGIN")) {
			if(!(*arg)) break;
			fputc(CS_STR_FUNCTION,out);
			fputc(CS_LOGIN,out);
			writecstr(arg);
			continue; }
932

rswindell's avatar
rswindell committed
933 934 935 936 937 938 939 940 941 942 943 944
		if(!stricmp(p,"LOAD_TEXT")) {
			if(!(*arg)) break;
			fputc(CS_STR_FUNCTION,out);
			fputc(CS_LOAD_TEXT,out);
			writestr(arg);
			continue; }

		if(!stricmp(p,"SET_STR_VAR")
			|| (!stricmp(p,"SET") && strchr(arg,'"'))) {
			if(!(*arg)) break;
			fputc(CS_VAR_INSTRUCTION,out);
			fputc(SET_STR_VAR,out);
945
			p=strchr(arg,' ');
rswindell's avatar
rswindell committed
946 947 948 949 950 951 952 953 954 955
			if(!p)
				break;
			*p=0;
			writecrc(src,arg);
			writecstr(arg2);
			continue; }
		if(!stricmp(p,"CAT_STR_VAR")
			|| (!stricmp(p,"STRCAT") && strchr(arg,'"'))) {
			fputc(CS_VAR_INSTRUCTION,out);
			fputc(CAT_STR_VAR,out);
956
			p=strchr(arg,' ');
rswindell's avatar
rswindell committed
957 958 959 960 961 962 963 964 965 966
			if(!p)
				break;
			*p=0;
			writecrc(src,arg);
			writecstr(arg2);
			continue; }
		if((!stricmp(p,"STRSTR") || !stricmp(p,"COMPARE_SUBSTR"))
			&& strchr(arg,'"')) {
			fputc(CS_VAR_INSTRUCTION,out);
			fputc(STRSTR_VAR,out);