Ejemplo n.º 1
0
alloc_t *read_symbol(FILE *f) {
    alloc_t *x;
    char c;
    ptrdiff_t position = 0;
    size_t segment = 0;
    char *s = (char *) malloc(sizeof(char) * (segment + 1) * SEGMENT_SIZE);
    bool in_symbol = true;

    if (!s) {
        longjmp(fatal, ERROR_SYMBOL_NAME);
    }
    
    x = new_alloc();
    if (!x) {
        longjmp(fatal, ERROR_SYMBOL_NAME);
    }
    x->type = SYMBOL;
    
    while (true) {
        for (c = fgetc(f); position < SEGMENT_SIZE - 1 && c != EOF; position++, c = fgetc(f)) {
            if (strchr(WHITESPACE DELIMITERS, c)) {
                c = ungetc(c, f);
                if (c == EOF && ferror(f)) {
                    longjmp(fatal, ERROR_PUSHBACK);
                }
                in_symbol = false;
                break;
            }
            s[segment * SEGMENT_SIZE + position] = c;
        }
        
        if (SEGMENT_SIZE - 1 <= position) {
            /* Note that this can happen at EOF as well as before EOF */
            position = -1;
            segment++;
            if (MAX_SEGMENT <= segment) {
                longjmp(fatal, ERROR_SYMBOL_NAME);
            }
            s = (char *) realloc(s, sizeof(char) * (segment + 1) * SEGMENT_SIZE);
            if (!s) {
                longjmp(fatal, ERROR_SYMBOL_NAME);
            }
        }
        
        if (c == EOF || !in_symbol) {
            break;
        }
        s[segment * SEGMENT_SIZE + position] = c;
        position = 0;   
    }
    
    s[segment * SEGMENT_SIZE + position] = '\0';
    
    x->value.symbol.name = s;
    return x;
}
Ejemplo n.º 2
0
alloc_t *cons(alloc_t *a, alloc_t *b) {
    alloc_t *c;

    c = new_alloc();
    if (!c) {
        longjmp(fatal, ERROR_CONS);
    }
    c->type = CELL;
    c->value.cell.car = a;
    c->value.cell.cdr = b;

    return c;
}
Ejemplo n.º 3
0
struct linked_list *
new_item(int size)
{
    struct linked_list  *item;

    if ((item = new_list()) == NULL)
        msg("Ran out of memory for header after %d items.", total);

    if ((item->data.l_data = new_alloc(size)) == NULL)
        msg("Ran out of memory for data after %d items.", total);

    item->l_next = item->l_prev = NULL;

    return(item);
}
Ejemplo n.º 4
0
void
ring_on(void)
{
    struct object   *obj;
    struct linked_list  *item;
    int ring;
    char buf[2 * LINELEN];

    if ((item = get_item("put on", RING)) == NULL)
        return;

    obj = OBJPTR(item);

    if (obj->o_type != RING)
    {
        msg("You can't put that on!");
        return;
    }

    /* find out which hand to put it on */

    if (is_current(obj))
    {
        msg("Already wearing that!");
        return;
    }

    if (cur_ring[LEFT_1] == NULL)
        ring = LEFT_1;
    else if (cur_ring[LEFT_2] == NULL)
        ring = LEFT_2;
    else if (cur_ring[LEFT_3] == NULL)
        ring = LEFT_3;
    else if (cur_ring[LEFT_4] == NULL)
        ring = LEFT_4;
    else if (cur_ring[LEFT_5] == NULL)
        ring = LEFT_5;
    else if (cur_ring[RIGHT_1] == NULL)
        ring = RIGHT_1;
    else if (cur_ring[RIGHT_2] == NULL)
        ring = RIGHT_2;
    else if (cur_ring[RIGHT_3] == NULL)
        ring = RIGHT_3;
    else if (cur_ring[RIGHT_4] == NULL)
        ring = RIGHT_4;
    else if (cur_ring[RIGHT_5] == NULL)
        ring = RIGHT_5;
    else
    {
        msg("You already have on ten rings.");
        return;
    }

    cur_ring[ring] = obj;

    /* Calculate the effect it has on the poor guy. */

    switch (obj->o_which)
    {
        case R_ADDSTR:
            pstats.s_str += obj->o_ac;
            break;
        case R_ADDHIT:
            pstats.s_dext += obj->o_ac;
            break;
        case R_ADDINTEL:
            pstats.s_intel += obj->o_ac;
            break;
        case R_ADDWISDOM:
            pstats.s_wisdom += obj->o_ac;
            break;
        case R_FREEDOM:
            turn_off(player, ISHELD);
            hold_count = 0;
            break;
        case R_TRUESEE:
            if (off(player, PERMBLIND))
            {
                turn_on(player, CANTRUESEE);
                msg("You become more aware of your surroundings.");
                sight(NULL);
                light(&hero);
                mvwaddch(cw, hero.y, hero.x, PLAYER);
            }
            break;

        case R_SEEINVIS:
            if (off(player, PERMBLIND))
            {
                turn_on(player, CANTRUESEE);
                msg("Your eyes begin to tingle.");
                sight(NULL);
                light(&hero);
                mvwaddch(cw, hero.y, hero.x, PLAYER);
            }
            break;

        case R_AGGR:
            aggravate();
            break;

        case R_CARRYING:
            updpack();
            break;

        case R_LEVITATION:
            msg("You begin to float in the air!");
            break;

        case R_LIGHT:
            if (roomin(hero) != NULL)
            {
                light(&hero);
                mvwaddch(cw, hero.y, hero.x, PLAYER);
            }
    }

    status(FALSE);

    if (know_items[TYP_RING][obj->o_which] &&
        guess_items[TYP_RING][obj->o_which])
    {
        mem_free(guess_items[TYP_RING][obj->o_which]);
        guess_items[TYP_RING][obj->o_which] = NULL;
    }
    else if (!know_items[TYP_RING][obj->o_which] &&
         askme &&
         (obj->o_flags & ISKNOW) == 0 &&
         guess_items[TYP_RING][obj->o_which] == NULL)
    {
        mpos = 0;
        msg("What do you want to call it? ");

        if (get_string(buf, cw) == NORM)
        {
            guess_items[TYP_RING][obj->o_which] =
                new_alloc(strlen(buf) + 1);
            strcpy(guess_items[TYP_RING][obj->o_which], buf);
        }
        msg("");
    }
}