Beispiel #1
0
int
include(const char *filename)
{
    struct includeline	*script, *se, *sp;
    char		input[256];			/* big enough? */
#ifdef BOOT_FORTH
    int			res;
    char		*cp;
    int			prevsrcid, fd, line;
#else
    int			argc,res;
    char		**argv, *cp;
    int			fd, flags, line;
#endif

    if (((fd = open(filename, O_RDONLY)) == -1)) {
	sprintf(command_errbuf,"can't open '%s': %s\n", filename, strerror(errno));
	return(CMD_ERROR);
    }

    /*
     * Read the script into memory.
     */
    script = se = NULL;
    line = 0;
	
    while (fgetstr(input, sizeof(input), fd) >= 0) {
	line++;
#ifdef BOOT_FORTH
	cp = input;
#else
	flags = 0;
	/* Discard comments */
	if (strncmp(input+strspn(input, " "), "\\ ", 2) == 0)
	    continue;
	cp = input;
	/* Echo? */
	if (input[0] == '@') {
	    cp++;
	    flags |= SL_QUIET;
	}
	/* Error OK? */
	if (input[0] == '-') {
	    cp++;
	    flags |= SL_IGNOREERR;
	}
#endif
	/* Allocate script line structure and copy line, flags */
	sp = alloc(sizeof(struct includeline) + strlen(cp) + 1);
	sp->text = (char *)sp + sizeof(struct includeline);
	strcpy(sp->text, cp);
#ifndef BOOT_FORTH
	sp->flags = flags;
#endif
	sp->line = line;
	sp->next = NULL;
	    
	if (script == NULL) {
	    script = sp;
	} else {
	    se->next = sp;
	}
	se = sp;
    }
    close(fd);
    
    /*
     * Execute the script
     */
#ifndef BOOT_FORTH
    argv = NULL;
#else
    prevsrcid = bf_vm->sourceID.i;
    bf_vm->sourceID.i = fd;
#endif
    res = CMD_OK;
    for (sp = script; sp != NULL; sp = sp->next) {
	
#ifdef BOOT_FORTH
	res = bf_run(sp->text);
	if (res != VM_OUTOFTEXT) {
		sprintf(command_errbuf, "Error while including %s, in the line:\n%s", filename, sp->text);
		res = CMD_ERROR;
		break;
	} else
		res = CMD_OK;
#else
	/* print if not being quiet */
	if (!(sp->flags & SL_QUIET)) {
	    prompt();
	    printf("%s\n", sp->text);
	}

	/* Parse the command */
	if (!parse(&argc, &argv, sp->text)) {
	    if ((argc > 0) && (perform(argc, argv) != 0)) {
		/* normal command */
		printf("%s: %s\n", argv[0], command_errmsg);
		if (!(sp->flags & SL_IGNOREERR)) {
		    res=CMD_ERROR;
		    break;
		}
	    }
	    free(argv);
	    argv = NULL;
	} else {
	    printf("%s line %d: parse error\n", filename, sp->line);
	    res=CMD_ERROR;
	    break;
	}
#endif
    }
#ifndef BOOT_FORTH
    if (argv != NULL)
	free(argv);
#else
    bf_vm->sourceID.i = prevsrcid;
#endif
    while(script != NULL) {
	se = script;
	script = script->next;
	free(se);
    }
    return(res);
}
Beispiel #2
0
int main( int argc, char *argv[] ){
	struct stat sb;
	bf_code_t *bf;
	int d_memsize = 30000;
	int d_loops = 0x1000;
	int d_csize = 1;
	FILE *fp;

	char *filename = NULL;
	char *p_in = NULL;
	char *p_out = NULL;
	char ch;
	char debug = 0;
	int i = 0;

	while (( ch = getopt( argc, argv, "f:i:o:m:dh" )) != -1 && i++ < argc ){
		switch( ch ){
			case 'f':
				filename = argv[++i];
				break;
			case 'd':
				debug = 1;
				break;
			case 'i':
				p_in = argv[++i];
				break;
			case 'o':
				p_out = argv[++i];
				break;
			case 'h':
				bf_help( );
				exit( 0 );
				break;
			case 'm':
				d_memsize = atoi( argv[++i] );
				break;
		}
	}

	if ( !filename ){
		die( 1, "Need filename. (see help with -h)\n" );
		exit( 0 );
	}

	if ( stat( filename, &sb ) < -1 )
		die( 2, "Could not stat \"%s\"\n", filename );

	if (( fp = fopen( filename, "r" )) == NULL )
		die( 3, "Could not open program file \"%s\"\n", filename );

	bf = new( bf_code_t );
	bf->codesize = sb.st_size;
	bf->code = new( char[ sb.st_size ]);
	fread( bf->code, bf->codesize, 1, fp );
	fclose( fp );

	bf->in = stdin;
	bf->out = stdout;

	if ( p_in && !( bf->in = fopen( p_in, "r" )))
		die( 4, "Could not open input file \"%s\"\n", p_in );

	if ( p_out && !( bf->out = fopen( p_out, "w" )))
		die( 4, "Could not open output file \"%s\"\n", p_out );

	bf->memsize = d_memsize;
	bf->mem = new( char[ d_memsize + 1 ]);

	bf->maxloops = d_loops;
	bf->lp = new( int[ d_loops ]);

	if ( !debug )
		bf_run( bf );
	else
		bf_debugger( bf );
	
	return 0;
}