ide_cmds.c 11.8 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
/*
 * ide builtin commands
 *
 * $Revision: 1.2 $
 */

#include <stdio.h>
#include "ide.h"
#include "diag.h"
#include "fcntl.h"

#include <netinet/in.h>
#include <dbg_comm.h>

extern int errNumbers[120];
extern int errIndex;
FILE *logfile, *fopen();
sym_t Initflagsym;
sym_t Entrytestsym;
sym_t Skiptestsym;
sym_t printmasksym;
int printmask = DEFAULT_PRINT_MSGS;
sym_t logmasksym;
int logmask = DEFAULT_LOG_MSGS;
sym_t commtypesym;
int commtype = DG_TINYMON_RDRAM;
sym_t exitmasksym;
int exitmask = DEFAULT_EXIT_MSGS;
sym_t Traplevelsym;
int Traplevel = TRAP_NONE;
sym_t Scopeflagsym;
int Scopeflag = FALSE;
sym_t Traceflagsym;
int Traceflag = FALSE;
struct diagComm myComm;
int	ide_dosource = 0;
static int fd;
static int cc;
static char buf[1024];
static char *bp = buf;
static int skipNumbers[10] = {TEST_NULL};
char *SkipTestString;
static int index, loc, total, vals;

static sym_t *lastpsym = (sym_t *)TEST_NULL;

/*
 * build up symbol table
 *
 */
ide_init()
{

	unsigned int pipe;
	char *p, *getenv(), *str;

	/*
	 * Set the umask so that the log file will be created with permission
	 * 666 (rw by all).  The value passed to umask is subtracted from the
	 * permission mask created for the file, that's why it's set to 111
	 * (no execute permissions).
	 */
	umask(0111);
	if (p = getenv("IDE_LOG")) {
	   if (!(logfile = fopen(p, "a"))) {
		printf("no write permission for logfile (%s)\n", p);
		exit(-1);
	   }
	} else {
	   pipe = (str = getenv("PIPE")) ? atoi(str) : 0;


	   if (pipe == 0) {
	   	if (!(logfile = fopen("/usr/tmp/ide.log0", "a"))) {
			printf("no write permission for logfile (/usr/tmp/ide.log0)\n");
			exit(-1);
	   	}
	   } else if (pipe == 1) {

	   	if (!(logfile = fopen("/usr/tmp/ide.log1", "a"))) {
			printf("no write permission for logfile (/usr/tmp/ide.log1)\n");
			exit(-1);
	   	}
	   } else {

	   	if (!(logfile = fopen("/usr/tmp/ide.log2", "a"))) {
			printf("no write permission for logfile (/usr/tmp/ide.log2)\n");
			exit(-1);
	   	}
	   }
	}
	DIAGINIT();
	diagcmdinit();
	SYMINIT();
	ide_init_consts();
	ide_init_arrays();
	return;
}
ide_init_arrays()
{
	int i;

	errIndex = 0;
/*        for (i=0; i<10; i++)
	    skipNumbers[i] = TEST_NULL; */
	myComm.skipNums = skipNumbers;
	myComm.errorCount = errIndex;
	for (i=0; i<120; i++)
	    errNumbers[i] = TEST_NULL;
        myComm.errNums = errNumbers;
}
ide_init_consts()
{
	myComm.init = 1;
	Initflagsym.sym_basetype = SYM_INT;
	Initflagsym.sym_type     = SYM_GLOBAL;
	Initflagsym.sym_i        = myComm.init;
	Initflagsym.sym_name     = "initflg";
	addsym( &Initflagsym );
	Entrytestsym.sym_basetype = SYM_INT;
	Entrytestsym.sym_type     = SYM_GLOBAL;
	Entrytestsym.sym_i        = myComm.entryNum;
	Entrytestsym.sym_name     = "entrytst";
	addsym( &Entrytestsym );
	Skiptestsym.sym_basetype = SYM_STR;
	Skiptestsym.sym_type     = SYM_GLOBAL;
	Skiptestsym.sym_s        = SkipTestString;
	Skiptestsym.sym_name     = "skiptst";
	addsym( &Skiptestsym );
	printmasksym.sym_basetype = SYM_INT;
	printmasksym.sym_type     = SYM_GLOBAL;
	printmasksym.sym_i        = printmask;
	printmasksym.sym_name     = "printmask";
	addsym( &printmasksym );
	logmasksym.sym_basetype = SYM_INT;
	logmasksym.sym_type     = SYM_GLOBAL;
	logmasksym.sym_i        = logmask;
	logmasksym.sym_name     = "logmask";
	addsym( &logmasksym );
	commtypesym.sym_basetype = SYM_INT;
	commtypesym.sym_type     = SYM_GLOBAL;
	commtypesym.sym_i        = commtype;
	commtypesym.sym_name     = "commtype";
	addsym( &commtypesym );
	exitmasksym.sym_basetype = SYM_INT;
	exitmasksym.sym_type     = SYM_GLOBAL;
	exitmasksym.sym_i        = exitmask;
	exitmasksym.sym_name     = "exitmask";
	addsym( &exitmasksym );
	Traplevelsym.sym_basetype = SYM_INT;
	Traplevelsym.sym_type     = SYM_GLOBAL;
	Traplevelsym.sym_i        = Traplevel;
	Traplevelsym.sym_name     = "traplvl";
	addsym( &Traplevelsym );
	Scopeflagsym.sym_basetype = SYM_INT;
	Scopeflagsym.sym_type     = SYM_GLOBAL;
	Scopeflagsym.sym_i        = Scopeflag;
	Scopeflagsym.sym_name     = "scopeflg";
	addsym( &Scopeflagsym );
	Traceflagsym.sym_basetype = SYM_INT;
	Traceflagsym.sym_type     = SYM_GLOBAL;
	Traceflagsym.sym_i        = Traceflag;
	Traceflagsym.sym_name     = "traceflg";
	addsym( &Traceflagsym );
}

syminit(n, b)
int n;
builtin_t *b;
{
	sym_t *psym;
	int i;

	for ( i = 0; i < n; i++, b++ ) {
		psym = makesym();
		if ( b->type == B_SCMD ) {
			psym->sym_type = SYM_CMD;
			psym->sym_func = (int (*)())b->addr;
			}
		else
		if ( b->type == B_CMD ) {
			psym->sym_type = SYM_CMD;
			psym->sym_func = (int (*)())b->addr;
			psym->sym_flags |= SYM_CHARARGV;
			}
		else
		if ( b->type == B_DCMD ) {
			psym->sym_type = SYM_CMD;
			psym->sym_func = (int (*)())b->addr;
			psym->sym_flags |= SYM_DIAGCOMM;
			}
		else
		if ( b->type == B_STR ) {
			psym->sym_type = SYM_VAR;
			psym->sym_basetype = SYM_STR;
			psym->sym_s    = makestr((char *)b->addr);
			}
		else
		if ( b->type == B_INT ) {
			psym->sym_type = SYM_VAR;
			psym->sym_basetype = SYM_INT;
			psym->sym_i    = (int) b->addr;
			}
		psym->sym_name = b->name;
		addsym(psym);
		}
	return;
}

symhelp(n, b)
int n;
builtin_t *b;
{
	int i, type;
	char *help;

	for (i = 0; i < n; ++i) {
		type = b[i].type;
		help = b[i].help;
		if ((type==B_CMD || type==B_SCMD || type==B_DCMD) && help)
			printf("%-10s - %s\n", b[i].name, b[i].help);
	}
}

/*
 * lsym is a global to be assigned
 *
 */
set_global( lsym, rsym )
sym_t *lsym, *rsym;
{
	sym_t *psym = 0;

	lastpsym = psym;
	if ( lsym == &Initflagsym ) {
		psym = pro_int( rsym );
		myComm.init = Initflagsym.sym_i = psym->sym_i;
		destroysym(psym);
		}
	if ( lsym == &Entrytestsym ) {
		psym = pro_int( rsym );
		myComm.entryNum = Entrytestsym.sym_i = psym->sym_i;
		destroysym(psym);
		}
	if ( lsym == &Skiptestsym ) {
		if (lastpsym)
			destroysym(lastpsym);
		psym = pro_str( rsym );
		SkipTestString = Skiptestsym.sym_s = psym->sym_s;
		index = loc = total= vals = 0;
		while (SkipTestString[index] != '\0') {
		    while (SkipTestString[index] == ' '||
		           SkipTestString[index] == '\t')
		        index++;
		    while (SkipTestString[index] >= '0' &&
			   SkipTestString[index] <= '9') {
		        total = 10 * total + (SkipTestString[index] - '0');
		        index++;
		    }
		    for (loc = vals;loc > 0 && total < skipNumbers[loc-1];loc--)
			skipNumbers[loc] = skipNumbers[loc-1];
                    skipNumbers[loc] = total;
		    total = 0;
/*		    loc++; */
		    vals++;
                }
		skipNumbers[vals] = 0;
		myComm.skipNums = skipNumbers;
		}
	if ( lsym == &printmasksym ) {
		psym = pro_int( rsym );
		printmask = printmasksym.sym_i = psym->sym_i;
		destroysym(psym);
		}
	if ( lsym == &logmasksym ) {
		psym = pro_int( rsym );
		logmask = logmasksym.sym_i = psym->sym_i;
		destroysym(psym);
		}
	if ( lsym == &commtypesym ) {
		psym = pro_int( rsym );
		commtype = commtypesym.sym_i = psym->sym_i;
		destroysym(psym);
		}
	if ( lsym == &exitmasksym ) {
		psym = pro_int( rsym );
		exitmask = exitmasksym.sym_i = psym->sym_i;
		destroysym(psym);
		}
	if ( lsym == &Traplevelsym ) {
		psym = pro_int( rsym );
		Traplevel= Traplevelsym.sym_i = psym->sym_i;
		destroysym(psym);
		}
	if ( lsym == &Scopeflagsym ) {
		psym = pro_int( rsym );
		Scopeflag = Scopeflagsym.sym_i = psym->sym_i;
		destroysym(psym);
		}
	if ( lsym == &Traceflagsym ) {
		psym = pro_int( rsym );
		Traceflag = Traceflagsym.sym_i = psym->sym_i;
		destroysym(psym);
		}
}
	
_dotestcmd(argc, argv )
int argc;
sym_t *argv[];
{
	int i;

	printf("%s:", argv[0]->sym_s);
	for ( i = 1; i < argc; i++ )
		switch(argv[i]->sym_basetype) {
			case SYM_INT:
				printf(" %d <int>", argv[i]->sym_i);
				break;
			case SYM_STR:
				if ( argv[i]->sym_type == SYM_CMDFLAG )
					printf(" %s <flags>",argv[i]->sym_s);
				else
					printf(" %s <str>", argv[i]->sym_s);
				break;
			default:
				printf("unknown type %d",argv[i]->sym_basetype);
				break;
			}
	putchar('\n');
	return 0;
}
/*
 * unformatted print
 *
 */
_doprint(argc, argv)
int argc;
sym_t *argv[];
{
	int i, firstopt, logprint;

	firstopt = 1;
	logprint = 0;

	/* Look for -l option */
	argv[firstopt] = pro_str(argv[firstopt]);
	if (!strncmp(argv[firstopt]->sym_s, "l", 1)) {
	    firstopt += 1;
	    logprint++;
	}

	for ( i = firstopt; i < argc; i++ ) {
		argv[i] = pro_str(argv[i]);
		printf("%s ", argv[i]->sym_s);
		if (logprint) {
			fprintf(logfile, "INFO:\t%s ", argv[i]->sym_s);
		}
	}
	
	putchar('\n');
	if (logprint) {
		fprintf(logfile, "\n");
	}
	return 0;
}
/*
 *
 * formatted print
 *
 */
_doprintf(argc, argv)
int argc;
sym_t *argv[];
{
	char	*fmt;
	int	pargv[MAXARGC+1];
	int	pargc;

	if ( argc < 2 )
		return 1;

	fmt = argv[1]->sym_s;

	for ( pargc = 2; pargc < argc; pargc++ )
		pargv[pargc-2] = argv[pargc]->sym_i;
	
	pargv[pargc-2] = 0;
	
	vprintf(fmt, pargv);

	return 0;
}
/*
 * read - shell style read
 *
 * usage: read x y z
 *
 * Assigns variables x, and y to the first two space seperated words
 * and z to all remaining characters.
 *
 */
_doread(argc, argv)
int argc;
sym_t *argv[];
{
	char readbuffer[MAXSTRLEN+1];
	char wordbuffer[MAXSTRLEN+1];
	char *rp = readbuffer;
	char *wp = wordbuffer;
	sym_t *psym = 0;

#ifdef __sgi__
	if ( ! gets(readbuffer) )
#else
	if ( ! fgets(readbuffer, sizeof readbuffer, stdin) )
#endif
		return;

	if ( ! --argc )
		return 0;

	do	{
		if ( !*rp || ((*rp == ' ' || *rp == '\t') && argc) ) {
			if ( wp != wordbuffer ) {
				*wp = '\0';
				if ( ! psym )
					psym = argv[1];
				if ( psym->sym_type == SYM_UNDEFINED ) {
					psym->sym_type = SYM_VAR;
					psym->sym_basetype= 0;
					psym->sym_name = psym->sym_s;
					psym->sym_s = 0;
					addsym(psym);
					}
				if ( psym->sym_type == SYM_VAR ) {
					if ( psym->sym_basetype == SYM_STR )
						destroystr(psym->sym_s);
					psym->sym_basetype = SYM_STR;
					psym->sym_s = makestr(wordbuffer);
					}
				psym++;
				argc--;
				wp = wordbuffer;
				}
			if ( ! *rp )
				break;
			}
		else
			*wp++ = *rp++;
		} while ( 1 );
	
	return 0;
}
_dohelp(argc, argv)
int argc;
sym_t *argv[];
{
	char *help;

	if ( argc > 1 && argv[1]->sym_basetype == SYM_STR)
		help = argv[1]->sym_s;
	else
		help = 0;
		
	if ( ! help || ! strcmp(help,"commands") )
		doqhelp();

	if ( ! help || ! strcmp(help, "loop") )
		printf("repeat n cmd\n             repeat `cmd' n times\n");

	if ( ! help || ! strcmp(help, "loop") )
	      printf("while ( expr ) cmd\n             repeat `cmd' while expr is true\n");

	if ( ! help || ! strcmp(help, "loop") )
	      printf("for ( expr1; expr2; expr3 ) cmd\n             repeat `cmd' while expr2 is true\n");

	if ( ! help || ! strcmp(help, "ifthen") ) {
		printf("if ( expr ) cmd1 [ else cmd2 ] [fi]\n");
         	printf("             execute cmd1 if expr is true, cmd2 otherwise; `fi' is optional\n");
		}

	if ( ! help || ! strcmp(help, "cmd") )
		printf("{ cmd ; cmd ... }\n             commands may grouped with `{' and `}'\n");

	return 0;
}
doqhelp()
{
	printf("IDE Commands:\n");

	diagcmdhelp();
	SYMHELP();

	return 0;
}

_doexit(argc, argv)
int argc;
sym_t *argv[];
{
	DIAGHALT();
	if ( argc > 1 && argv[1]->sym_basetype == SYM_INT )
		exit(argv[1]->sym_i);
	else
		exit(0);

	/*NOTREACHED*/
}

_dosource(argc,argv)
int argc;
char *argv[];
{
	int i;

	if (argc != 3 || strcmp(argv[1],"-f") ) {
		printf("usage: source -f FILESPEC\n");
		return(1);
	}

	if ( ide_dosource == 1 ) {
		if ( bp != buf && bp != buf+cc )
			printf("WARNING: sourcing a new file before the end of old\n");
		bp = buf;
		close(fd);
	}

	fd = open(argv[2], O_RDONLY);
	if (fd < 0) {
		printf("can't open %s\n", *argv);
		ide_dosource = 0;
		return(1);
	}
	ide_dosource = 1;
	return(0);
}

/*
 * code assumes \n at end of file
 */
getsfromsource(p)
char *p;
{
	char c;
	int i = 0;

	while ( 1 ) {
		/* refill buffer if needed */
		if ( bp == buf ) {
			cc = read(fd, buf, sizeof(buf));
			if ( cc <= 0 ) {
				if ( cc < 0 ) 
					printf("*** error on read\n");
				close(fd);
				ide_dosource = 0;
				return(i);
			}
		}
	
		while ( bp < (buf + cc) ) {
			c = *bp++;
			*p++ = c;
			i++;
			if ( c == '\n' )
				return(i);
		}
		bp = buf;
	}
}