Пример #1
0
void add_rom(const char *prefix, FILE *file) {
    int i;

    t_rom *rom = malloc(sizeof(t_rom));
    rom->prefix = prefix;

    // Load ROM file
    eat_comment(file);
    fscanf(file, "%d %d\n", &(rom->words_defined), &(rom->word_size));
    rom->data = malloc(rom->words_defined * sizeof(t_value));

    for (i = 0; i < rom->words_defined; i++) {
        eat_comment(file);
        if (fscanf(file, "/%lu", &(rom->data[i]))) {
            // ok, value is read
        } else if (fscanf(file, "x%lx", &(rom->data[i]))) {
            // ok, value is read
        } else {
            rom->data[i] = read_bool(file, NULL);
        }
    }

    rom->next = roms;
    roms = rom;
}
Пример #2
0
void
scan_init(const char *cmdline_program)
{
    if (cmdline_program) {
	program_fd = -1;	/* command line program */
	program_string = new_STRING0(strlen(cmdline_program) + 1);
	strcpy(program_string->str, cmdline_program);
	/* simulate file termination */
	program_string->str[program_string->len - 1] = '\n';
	buffp = (UChar *) program_string->str;
	eof_flag = 1;
    } else {			/* program from file[s] */
	scan_open();
	buffp = buffer = (UChar *) zmalloc((size_t) (BUFFSZ + 1));
	scan_fillbuff();
    }

#ifdef OS2			/* OS/2 "extproc" is similar to #! */
    if (strnicmp(buffp, "extproc ", 8) == 0)
	eat_comment();
#endif
    eat_nl();			/* scan to first token */
    if (next() == 0) {
	/* no program */
	mawk_exit(0);
    }

    un_next();

}
Пример #3
0
/*
 * Scan a section name and remember it in `cursec'.
 */
static void parse_section(FILE *fp)
{
    int c, i, end;

    /* We've already got the '['. Scan past initial white space. */
    c = eat_whitespace(fp);
    i = 0;
    end = 0;
    while (c > 0) {
        if (i > (bsize-2)) {
            bsize += 1024;
            bufr = realloc(bufr, bsize);
            if (! bufr) {
                fprintf(stderr, "%s: malloc failed\n", confname);
                exit(-1);
            }
        }
        switch (c) {
        case ']':       /* found the closing bracked */
            bufr[end] = '\0';
            if (end == 0) {
                fprintf(stderr, "%s: empty section name\n", confname);
                exit(-1);
            }
            /* Register a section. */
            if (cursec)
                free(cursec);
            cursec = strdup(bufr);

            eat_comment(fp);
            return;

        case '\n':
            i = find_continuation(bufr, i);
            if (i < 0) {
                bufr [end] = 0;
                fprintf(stderr, "%s: invalid line: '%s'\n",
                    confname, bufr);
                exit(-1);
            }
            end = ((i > 0) && (bufr[i-1] == ' ')) ? (i-1) : (i);
            c = getc(fp);
            break;

        default:
            if (isspace(c)) {
                bufr[end] = ' ';
                i = end + 1;
                c = eat_whitespace(fp);
            } else {
                bufr[i++] = c;
                end = i;
                c = getc(fp);
            }
            break;
        }
    }
}
Пример #4
0
void
eat_nl(void)			/* eat all space including newlines */
{
    while (1) {
	switch (scan_code[(UChar) next()]) {
	case SC_COMMENT:
	    eat_comment();
	    break;

	case SC_NL:
	    lineno++;
	    /* FALLTHRU  */

	case SC_SPACE:
	    break;

	case SC_ESCAPE:
	    /* bug fix - surprised anyone did this,
	       a csh user with backslash dyslexia.(Not a joke)
	     */
	    {
		int c;

		while (scan_code[NextUChar(c)] == SC_SPACE) {
		    ;		/* empty */
		}
		if (c == '\n')
		    token_lineno = ++lineno;
		else if (c == 0) {
		    un_next();
		    return;
		} else {	/* error */
		    un_next();
		    /* can't un_next() twice so deal with it */
		    yylval.ival = '\\';
		    unexpected_char();
		    if (++compile_error_count == MAX_COMPILE_ERRORS)
			mawk_exit(2);
		    return;
		}
	    }
	    break;

	default:
	    un_next();
	    return;
	}
    }
}
Пример #5
0
void Image::load(const std::string& filename)
{

	std::ifstream ifs(filename.c_str(), std::ios::binary);

	// check if file exists
	if (!ifs.is_open())
	{
		std::cout << "Error: Unable to open file \"" << filename << "\"" << std::endl;
		return;
	}

	// we got file

	std::cout << "Reading file header of \"" << filename << "\"" << std::endl;

	eat_comment(ifs);

	// read and evaluate magic number
	std::string magic_number;
	ifs >> magic_number;

	ANYMAP map = PPM;
	bool ascii = false;

	if (magic_number.compare("P1") == 0)
	{
		// P1 = PBM in ASCII
		map = PBM;
		ascii = true;
	}
	else if (magic_number.compare("P2") == 0)
	{
		// P2 = PGM in ASCII
		map = PGM;
		ascii = true;
	}
	else if (magic_number.compare("P3") == 0)
	{
		// P3 = PPM in ASCII
		map = PPM;
		ascii = true;
	}
	else if (magic_number.compare("P4") == 0)
	{
		// P4 = PBM in binary
		map = PBM;
		ascii = false;
	}
	else if (magic_number.compare("P5") == 0)
	{
		// P5 = PGM in binary
		map = PGM;
		ascii = false;
	}
	else if (magic_number.compare("P6") == 0)
	{
		// P6 = PPM in binary
		map = PPM;
		ascii = false;
	}
	else
	{
		std::cout << "Error, Magic number doesn't fit in anymap format!" << std::endl;
		ifs.close();
		return;
	}

	// read width and height

	int width = 1, height = 1;

	eat_comment(ifs);
	ifs >> width;

	eat_comment(ifs);
	ifs >> height;

	// let's hope size is not smaller than 1
	if (width < 1 || height < 1)
	{
		std::cout << "Error: Unsupported size " << width << "/" << height << std::endl;
		ifs.close();
		return;
	}

	// read color depth if not PBM
	int color_depth = 1;

	if (magic_number.compare("P1") != 0 && magic_number.compare("P4") != 0)
	{
		eat_comment(ifs);
		ifs >> color_depth;

		if (color_depth < 1 || color_depth > 255)
		{
			std::cout << "Error: Unsupported color depth " << color_depth << std::endl;
			ifs.close();
			return;
		}
	}
Пример #6
0
int
yylex(void)
{
    register int c;

    token_lineno = lineno;

#ifdef NO_LEAKS
    memset(&yylval, 0, sizeof(yylval));
#endif

  reswitch:

    switch (scan_code[NextUChar(c)]) {
    case 0:
	ct_ret(EOF);

    case SC_SPACE:
	goto reswitch;

    case SC_COMMENT:
	eat_comment();
	goto reswitch;

    case SC_NL:
	lineno++;
	eat_nl();
	ct_ret(NL);

    case SC_ESCAPE:
	while (scan_code[NextUChar(c)] == SC_SPACE) {
	    ;			/* empty */
	};
	if (c == '\n') {
	    token_lineno = ++lineno;
	    goto reswitch;
	}

	if (c == 0)
	    ct_ret(EOF);
	un_next();
	yylval.ival = '\\';
	ct_ret(UNEXPECTED);

    case SC_SEMI_COLON:
	eat_nl();
	ct_ret(SEMI_COLON);

    case SC_LBRACE:
	eat_nl();
	brace_cnt++;
	ct_ret(LBRACE);

    case SC_PLUS:
	switch (next()) {
	case '+':
	    yylval.ival = '+';
	    string_buff[0] =
		string_buff[1] = '+';
	    string_buff[2] = 0;
	    ct_ret(INC_or_DEC);

	case '=':
	    ct_ret(ADD_ASG);

	default:
	    un_next();
	    ct_ret(PLUS);
	}

    case SC_MINUS:
	switch (next()) {
	case '-':
	    yylval.ival = '-';
	    string_buff[0] =
		string_buff[1] = '-';
	    string_buff[2] = 0;
	    ct_ret(INC_or_DEC);

	case '=':
	    ct_ret(SUB_ASG);

	default:
	    un_next();
	    ct_ret(MINUS);
	}

    case SC_COMMA:
	eat_nl();
	ct_ret(COMMA);

    case SC_MUL:
	test1_ret('=', MUL_ASG, MUL);

    case SC_DIV:
	{
	    static const int can_precede_div[] =
	    {DOUBLE, STRING_, RPAREN, ID, D_ID, RE, RBOX, FIELD,
	     GETLINE, INC_or_DEC, -1};

	    const int *p = can_precede_div;

	    do {
		if (*p == current_token) {
		    if (*p != INC_or_DEC) {
			test1_ret('=', DIV_ASG, DIV);
		    }

		    if (next() == '=') {
			un_next();
			ct_ret(collect_RE());
		    }
		}
	    }
	    while (*++p != -1);

	    ct_ret(collect_RE());
	}

    case SC_MOD:
	test1_ret('=', MOD_ASG, MOD);

    case SC_POW:
	test1_ret('=', POW_ASG, POW);

    case SC_LPAREN:
	paren_cnt++;
	ct_ret(LPAREN);

    case SC_RPAREN:
	if (--paren_cnt < 0) {
	    compile_error("extra ')'");
	    paren_cnt = 0;
	    goto reswitch;
	}

	ct_ret(RPAREN);

    case SC_LBOX:
	ct_ret(LBOX);

    case SC_RBOX:
	ct_ret(RBOX);

    case SC_MATCH:
	string_buff[1] = '~';
	string_buff[0] = 0;
	yylval.ival = 1;
	ct_ret(MATCH);

    case SC_EQUAL:
	test1_ret('=', EQ, ASSIGN);

    case SC_NOT:		/* !  */
	if ((c = next()) == '~') {
	    string_buff[0] = '!';
	    string_buff[1] = '~';
	    string_buff[2] = 0;
	    yylval.ival = 0;
	    ct_ret(MATCH);
	} else if (c == '=')
	    ct_ret(NEQ);

	un_next();
	ct_ret(NOT);

    case SC_LT:		/* '<' */
	if (next() == '=')
	    ct_ret(LTE);
	else
	    un_next();

	if (getline_flag) {
	    getline_flag = 0;
	    ct_ret(IO_IN);
	} else
	    ct_ret(LT);

    case SC_GT:		/* '>' */
	if (print_flag && paren_cnt == 0) {
	    print_flag = 0;
	    /* there are 3 types of IO_OUT
	       -- build the error string in string_buff */
	    string_buff[0] = '>';
	    if (next() == '>') {
		yylval.ival = F_APPEND;
		string_buff[1] = '>';
		string_buff[2] = 0;
	    } else {
		un_next();
		yylval.ival = F_TRUNC;
		string_buff[1] = 0;
	    }
	    return current_token = IO_OUT;
	}

	test1_ret('=', GTE, GT);

    case SC_OR:
	if (next() == '|') {
	    eat_nl();
	    ct_ret(OR);
	} else {
	    un_next();

	    if (print_flag && paren_cnt == 0) {
		print_flag = 0;
		yylval.ival = PIPE_OUT;
		string_buff[0] = '|';
		string_buff[1] = 0;
		ct_ret(IO_OUT);
	    } else
		ct_ret(PIPE);
	}

    case SC_AND:
	if (next() == '&') {
	    eat_nl();
	    ct_ret(AND);
	} else {
	    un_next();
	    yylval.ival = '&';
	    ct_ret(UNEXPECTED);
	}

    case SC_QMARK:
	ct_ret(QMARK);

    case SC_COLON:
	ct_ret(COLON);

    case SC_RBRACE:
	if (--brace_cnt < 0) {
	    compile_error("extra '}'");
	    eat_semi_colon();
	    brace_cnt = 0;
	    goto reswitch;
	}

	if ((c = current_token) == NL || c == SEMI_COLON
	    || c == SC_FAKE_SEMI_COLON || c == RBRACE) {
	    /* if the brace_cnt is zero , we've completed
	       a pattern action block. If the user insists
	       on adding a semi-colon on the same line
	       we will eat it.  Note what we do below:
	       physical law -- conservation of semi-colons */

	    if (brace_cnt == 0)
		eat_semi_colon();
	    eat_nl();
	    ct_ret(RBRACE);
	}

	/* supply missing semi-colon to statement that
	   precedes a '}' */
	brace_cnt++;
	un_next();
	current_token = SC_FAKE_SEMI_COLON;
	return SEMI_COLON;

    case SC_DIGIT:
    case SC_DOT:
	{
	    double d;
	    int flag;

	    if ((d = collect_decimal(c, &flag)) == 0.0) {
		if (flag)
		    ct_ret(flag);
		else
		    yylval.ptr = (PTR) & double_zero;
	    } else if (d == 1.0) {
		yylval.ptr = (PTR) & double_one;
	    } else {
		yylval.ptr = (PTR) ZMALLOC(double);
		*(double *) yylval.ptr = d;
	    }
	    ct_ret(DOUBLE);
	}

    case SC_DOLLAR:		/* '$' */
	{
	    double d;
	    int flag;

	    while (scan_code[NextUChar(c)] == SC_SPACE) {
		;		/* empty */
	    };
	    if (scan_code[c] != SC_DIGIT &&
		scan_code[c] != SC_DOT) {
		un_next();
		ct_ret(DOLLAR);
	    }

	    /* compute field address at compile time */
	    if ((d = collect_decimal(c, &flag)) <= 0.0) {
		if (flag)
		    ct_ret(flag);	/* an error */
		else
		    yylval.cp = &field[0];
	    } else {
		int ival = d_to_I(d);
		double dval = (double) ival;
		if (dval != d) {
		    compile_error("$%g is invalid field index", d);
		}
		yylval.cp = field_ptr(ival);
	    }

	    ct_ret(FIELD);
	}

    case SC_DQUOTE:
	return current_token = collect_string();

    case SC_IDCHAR:		/* collect an identifier */
	{
	    char *p = string_buff + 1;
	    SYMTAB *stp;

	    string_buff[0] = (char) c;

	    while (1) {
		CheckStringSize(p);
		c = scan_code[NextUChar(*p++)];
		if (c != SC_IDCHAR && c != SC_DIGIT)
		    break;
	    }

	    un_next();
	    *--p = 0;

	    switch ((stp = find(string_buff))->type) {
	    case ST_NONE:
		/* check for function call before defined */
		if (next() == '(') {
		    stp->type = ST_FUNCT;
		    stp->stval.fbp = (FBLOCK *)
			zmalloc(sizeof(FBLOCK));
		    stp->stval.fbp->name = stp->name;
		    stp->stval.fbp->code = (INST *) 0;
		    stp->stval.fbp->size = 0;
		    yylval.fbp = stp->stval.fbp;
		    current_token = FUNCT_ID;
		} else {
		    yylval.stp = stp;
		    current_token =
			current_token == DOLLAR ? D_ID : ID;
		}
		un_next();
		break;

	    case ST_NR:
		NR_flag = 1;
		stp->type = ST_VAR;
		/* FALLTHRU */

	    case ST_VAR:
	    case ST_ARRAY:
	    case ST_LOCAL_NONE:
	    case ST_LOCAL_VAR:
	    case ST_LOCAL_ARRAY:

		yylval.stp = stp;
		current_token =
		    current_token == DOLLAR ? D_ID : ID;
		break;

	    case ST_ENV:
		stp->type = ST_ARRAY;
		stp->stval.array = new_ARRAY();
		load_environ(stp->stval.array);
		yylval.stp = stp;
		current_token =
		    current_token == DOLLAR ? D_ID : ID;
		break;

	    case ST_FUNCT:
		yylval.fbp = stp->stval.fbp;
		current_token = FUNCT_ID;
		break;

	    case ST_KEYWORD:
		current_token = stp->stval.kw;
		break;

	    case ST_BUILTIN:
		yylval.bip = stp->stval.bip;
		current_token = BUILTIN;
		break;

	    case ST_LENGTH:

		yylval.bip = stp->stval.bip;

		/* check for length alone, this is an ugly
		   hack */
		while (scan_code[NextUChar(c)] == SC_SPACE) {
		    ;		/* empty */
		};
		un_next();

		current_token = c == '(' ? BUILTIN : LENGTH;
		break;

	    case ST_FIELD:
		yylval.cp = stp->stval.cp;
		current_token = FIELD;
		break;

	    default:
		bozo("find returned bad st type");
	    }
	    return current_token;
	}

    case SC_UNEXPECTED:
	yylval.ival = c & 0xff;
	ct_ret(UNEXPECTED);
    }
    return 0;			/* never get here make lint happy */
}
Пример #7
0
/*
 * Scan a parameter name (or name and value pair) and pass the value (or
 * values) to function pfunc().
 */
static void parse_parameter(FILE *fp, void (*pfunc)(char*, char*, char*), int c)
{
    int i = 0;          /* position withing bufr */
    int end = 0;        /* bufr[end] is current end-of-string */
    int vstart = 0;     /* starting position of the parameter */

    /* Loop until we found the start of the value */
    while (vstart == 0) {
        /* Ensure there's space for next char */
        if (i > (bsize-2)) {
            bsize += 1024;
            bufr = realloc(bufr, bsize);
            if (! bufr) {
                fprintf(stderr, "%s: malloc failed\n", confname);
                exit(-1);
            }
        }
        switch (c) {
        case '=':
            if (end == 0) {
                fprintf(stderr, "%s: invalid parameter name\n", confname);
                exit(-1);
            }
            bufr[end++] = '\0';
            i = end;
            vstart = end;
            bufr[i] = '\0';
            break;

        case ';':           /* comment line */
        case '#':
            c = eat_comment(fp);
        case '\n':
            i = find_continuation(bufr, i);
            if (i < 0) {
                /* End of line, but no assignment symbol. */
                bufr[end]='\0';
                fprintf(stderr, "%s: bad line, ignored: `%s'\n",
                    confname, bufr);
                return;
            }
            end = ((i > 0) && (bufr[i-1] == ' ')) ? (i-1) : (i);
            c = getc(fp);
            break;

        case '\0':
        case EOF:
            bufr[i] = '\0';
            fprintf(stderr, "%s: unexpected end-of-file at %s: func\n",
                confname, bufr);
            exit(-1);

        default:
            if (isspace(c)) {
                bufr[end] = ' ';
                i = end + 1;
                c = eat_whitespace(fp);
            } else {
                bufr[i++] = c;
                end = i;
                c = getc(fp);
            }
            break;
        }
    }

    /* Now parse the value */
    c = eat_whitespace(fp);
    while (c > 0) {
        if (i > (bsize-2)) {
            bsize += 1024;
            bufr = realloc(bufr, bsize);
            if (! bufr) {
                fprintf(stderr, "%s: malloc failed\n", confname);
                exit(-1);
            }
        }
        switch(c) {
        case '\r':
            c = getc(fp);
            break;

        case ';':           /* comment line */
        case '#':
            c = eat_comment(fp);
        case '\n':
            i = find_continuation(bufr, i);
            if (i < 0)
                c = 0;
            else {
                for (end=i; (end >= 0) && isspace(bufr[end]); end--)
                    ;
                c = getc(fp);
            }
            break;

        default:
            bufr[i++] = c;
            if (! isspace(c))
                end = i;
            c = getc(fp);
            break;
        }
    }
    bufr[end] = '\0';
    pfunc(cursec, bufr, &bufr [vstart]);
}
Пример #8
0
/*
 * Read configuration file and setup target parameters.
 */
void target_configure()
{
    FILE *fp;
    int c;

    /*
     * Find the configuration file, if any.
     * (1) First, try a path from PIC32PROG_CONF_FILE environment variable.
     * (2) Otherwise, look for a file in the local directory.
     * (3) Otherwise, use /usr/local/etc/ directory (on Unix)
     *     or a directory where pic32prog.exe resides (on Windows)
     */
    confname = getenv("PIC32PROG_CONF_FILE");
    if (! confname)
        confname = "pic32prog.conf";

    if (access(confname, 0) < 0) {
#if defined(__CYGWIN32__) || defined(MINGW32)
        char *p = strrchr(progname, '\\');
        if (p) {
            char *buf = malloc(p - progname + 16);
            if (! buf) {
                fprintf(stderr, "%s: out of memory\n", progname);
                exit(-1);
            }
            strncpy(buf, progname, p - progname);
            strcpy(buf + (p - progname), "\\pic32prog.conf");
            confname = buf;
        } else
            confname = "c:\\pic32prog.conf";
#else
        confname = "/usr/local/etc/pic32prog.conf";
#endif
    }

    fp = fopen(confname, "r");
    if (! fp) {
        /* No config file available: that's OK. */
        return;
    }
    bsize = 1024;
    bufr = (char*) malloc(bsize);
    if (! bufr) {
        fprintf(stderr, "%s: malloc failed\n", confname);
        fclose(fp);
        exit(-1);
    }

    /* Parse file. */
    c = eat_whitespace(fp);
    while (c > 0) {
        switch (c) {
        case '\n':          /* blank line */
            c = eat_whitespace(fp);
            break;

        case ';':           /* comment line */
        case '#':
            c = eat_comment(fp);
            break;

        case '[':           /* section header */
            parse_section(fp);
            c = eat_whitespace(fp);
            break;

        case '\\':          /* bogus backslash */
            c = eat_whitespace(fp);
            break;

        default:            /* parameter line */
            parse_parameter(fp, configure_parameter, c);
            c = eat_whitespace(fp);
            break;
        }
    }
    configure_parameter("", 0, 0); /* Final call: end of file. */
    fclose(fp);
    if (cursec) {
        free(cursec);
        cursec = 0;
    }
    free(bufr);
    bufr = 0;
    bsize = 0;
}