static struct high_state *find_state(struct high_syntax *syntax, unsigned char *name) { struct high_state *state; /* Find state */ state = htfind(syntax->ht_states, name); /* It doesn't exist, so create it */ if (!state) { int y; state = joe_malloc(sizeof(struct high_state)); state->name = zdup(name); state->no = syntax->nstates; state->color = FG_WHITE; /* Expand the state table if necessary */ if (syntax->nstates == syntax->szstates) { syntax->states = joe_realloc(syntax->states, sizeof(struct high_state *) * (syntax->szstates *= 2)); } syntax->states[syntax->nstates++] = state; for (y = 0; y != 256; ++y) { state->cmd[y] = &syntax->default_cmd; } state->delim = 0; htadd(syntax->ht_states, state->name, state); } return state; }
void lattr_check(struct lattr_db *db, ptrdiff_t amnt) { if (amnt > db->ehole - db->hole) { /* Not enough space */ /* Amount of additional space needed */ amnt = amnt - (db->ehole - db->hole) + 16; db->buffer = (HIGHLIGHT_STATE *)joe_realloc(db->buffer, (db->end + amnt) * SIZEOF(HIGHLIGHT_STATE)); mmove(db->buffer + db->ehole + amnt, db->buffer + db->ehole, (db->end - db->ehole) * SIZEOF(HIGHLIGHT_STATE)); db->ehole += amnt; db->end += amnt; } }
int help_init(JFILE *fd,unsigned char *bf,int line) { unsigned char buf[1024]; /* input buffer */ struct help *tmp; unsigned int bfl; /* buffer length */ unsigned int hlpsiz, hlpbsz; /* number of used/allocated bytes for tmp->text */ unsigned char *tempbuf; if (bf[0] == '{') { /* start of help screen */ tmp = (struct help *) joe_malloc(sizeof(struct help)); tmp->text = NULL; tmp->lines = 0; hlpsiz = 0; hlpbsz = 0; tmp->name = vsncpy(NULL, 0, sz(bf + 1) - 1); /* -1 kill the \n */ while ((jfgets(buf, sizeof(buf), fd)) && (buf[0] != '}')) { ++line; bfl = strlen(buf); if (hlpsiz + bfl > hlpbsz) { if (tmp->text) { tempbuf = (unsigned char *) joe_realloc(tmp->text, hlpbsz + bfl + 1024); tmp->text = tempbuf; } else { tmp->text = (unsigned char *) joe_malloc(bfl + 1024); tmp->text[0] = 0; } hlpbsz += bfl + 1024; } if (tmp->text) strcpy(tmp->text + hlpsiz, buf); hlpsiz += bfl; ++tmp->lines; } tmp->prev = help_ptr; tmp->next = NULL; if (help_ptr) { help_ptr->next = tmp; } else { help_actual = tmp; } help_ptr = tmp; if (buf[0] == '}') { /* set new help screen as actual one */ ++line; } else { fprintf(stderr, (char *)joe_gettext(_("\n%d: EOF before end of help text\n")),line); } } return line; }
HIGHLIGHT_STATE parse(struct high_syntax *const syntax, line_desc *const ld, HIGHLIGHT_STATE h_state, const bool utf8) { struct high_frame *stack = h_state.stack; struct high_state *h = (stack ? stack->syntax : syntax)->states[h_state.state]; /* Current state */ unsigned char buf[24]; /* Name buffer (trunc after 23 characters) */ unsigned char lbuf[24]; /* Lower case version of name buffer */ unsigned char lsaved_s[24]; /* Lower case version of delimiter match buffer */ int buf_idx = 0; /* Index into buffer */ int c; /* Current character */ int c_len; /* Character length in bytes */ uint32_t *attr = attr_buf; uint32_t *attr_end = attr_buf + attr_size; int buf_en = 0; /* Set for name buffering */ int ofst = 0; /* record offset after we've stopped buffering */ int mark1 = 0; /* offset to mark start from current pos */ int mark2 = 0; /* offset to mark end from current pos */ int mark_en = 0; /* set if marking */ int recolor_delimiter_or_keyword; const unsigned char *p = (const unsigned char *)ld->line; unsigned char *q = (unsigned char *)(ld->line + ld->line_len); buf[0] = 0; /* Forgot this originally... took 5 months to fix! */ /* Get next character */ /* Una iterazione in più: aggiungo '\n' come ultimo carattere. */ while (p <= q) /* On the last itteration, process the virtual '\n' character. */ { struct high_cmd *cmd; struct high_cmd *kw_cmd; int x; if (p == q) { c = '\n'; } else { c = utf8 ? get_char((const char *)p, ENC_UTF8) : *p; } c_len = utf8 ? utf8seqlen(c) : 1; p += c_len; /* Hack so we can have UTF-8 characters without crashing */ if (c < 0 || c > 255) { c = 0x1F; } /* Create or expand attribute array if necessary */ if (attr == attr_end) { if (!attr_buf) { attr_size = 1024; attr_buf = joe_malloc(sizeof(int) * attr_size); attr = attr_buf; } else { attr_buf = joe_realloc(attr_buf, sizeof(int) * (attr_size * 2)); attr = attr_buf + attr_size; attr_size *= 2; } attr_end = attr_buf + attr_size; } /* Advance to next attribute position (note attr[-1] below) */ attr++; /* Loop while noeat */ do { /* Color with current state */ attr[-1] = h->color; /* Get command for this character */ if (h->delim && c == h_state.saved_s[0] && h_state.saved_s[1] == 0) { cmd = h->delim; } else { cmd = h->cmd[c]; } /* Lowerize strings for case-insensitive matching */ if (cmd->ignore) { zcpy(lbuf, buf); lowerize(lbuf); if (cmd->delim) { zcpy(lsaved_s, h_state.saved_s); lowerize(lsaved_s); } } /* Check for delimiter or keyword matches */ recolor_delimiter_or_keyword = 0; if (cmd->delim && (cmd->ignore ? !zcmp(lsaved_s, lbuf) : !zcmp(h_state.saved_s, buf))) { cmd = cmd->delim; recolor_delimiter_or_keyword = 1; } else if (cmd->keywords && (cmd->ignore ? (kw_cmd = htfind(cmd->keywords, lbuf)) : (kw_cmd = htfind(cmd->keywords, buf)))) { cmd = kw_cmd; recolor_delimiter_or_keyword = 1; } /* Determine new state */ if (cmd->call) { /* Call */ struct high_frame **frame_ptr = stack ? &stack->child : &syntax->stack_base; /* Search for an existing stack frame for this call */ while (*frame_ptr && !((*frame_ptr)->syntax == cmd->call && (*frame_ptr)->return_state == cmd->new_state)) { frame_ptr = &(*frame_ptr)->sibling; } if (*frame_ptr) { stack = *frame_ptr; } else { struct high_frame *frame = joe_malloc(sizeof(struct high_frame)); frame->parent = stack; frame->child = 0; frame->sibling = 0; frame->syntax = cmd->call; frame->return_state = cmd->new_state; *frame_ptr = frame; stack = frame; ++stack_count; } h = stack->syntax->states[0]; } else if (cmd->rtn) { /* Return */ if (stack) { h = stack->return_state; stack = stack->parent; } else /* Not in a subroutine, so ignore the return */ { h = cmd->new_state; } } else if (cmd->reset) { /* Reset the state and call stack */ h = syntax->states[0]; stack = syntax->stack_base; } else { /* Normal edge */ h = cmd->new_state; } /* Recolor if necessary */ if (recolor_delimiter_or_keyword) for (x = -(buf_idx + 1); x < -1; ++x) { attr[x - ofst] = h->color; } for (x = cmd->recolor; x < 0; ++x) if (attr + x >= attr_buf) { attr[x] = h->color; } /* Mark recoloring */ if (cmd->recolor_mark) for (x = -mark1; x < -mark2; ++x) { attr[x] = h->color; } /* Save string? */ if (cmd->save_s) { zcpy(h_state.saved_s, buf); } /* Save character? */ if (cmd->save_c) { h_state.saved_s[1] = 0; if (c == '<') { h_state.saved_s[0] = '>'; } else if (c == '(') { h_state.saved_s[0] = ')'; } else if (c == '[') { h_state.saved_s[0] = ']'; } else if (c == '{') { h_state.saved_s[0] = '}'; } else if (c == '`') { h_state.saved_s[0] = '\''; } else { h_state.saved_s[0] = c; } } /* Start buffering? */ if (cmd->start_buffering) { buf_idx = 0; buf_en = 1; ofst = 0; } /* Stop buffering? */ if (cmd->stop_buffering) { buf_en = 0; } /* Set mark begin? */ if (cmd->start_mark) { mark2 = 1; mark1 = 1; mark_en = 1; } /* Set mark end? */ if (cmd->stop_mark) { mark_en = 0; mark2 = 1; } } while (cmd->noeat); /* Save character in buffer */ if (buf_idx < 23 && buf_en) { buf[buf_idx++] = c; } if (!buf_en) { ++ofst; } buf[buf_idx] = 0; /* Update mark pointers */ ++mark1; if (!mark_en) { ++mark2; } /* if(c=='\n') break;*/ } /* Return new state */ h_state.stack = stack; h_state.state = h->no; attr_len = attr - attr_buf - 1; /* -1 because of the fake newline. */ return h_state; }
void undodel(UNDO *undo, long where, B *b) { UNDOREC *rec; long size = b->eof->byte; if (inredo) { brm(b); return; } if (!inundo) if (undo->ptr && undo->ptr != &undo->recs) undoover(undo); yankdel(where, b); /* Store in undo buffer */ rec = undo->recs.link.prev; if (rec != &undo->recs && rec->min && rec->del && where == rec->where) { if (rec->len + size >= SMALL) { if (rec->len < SMALL) { rec->big = bmk(NULL); binsm(rec->big->bof, rec->small, (int) rec->len); boffline(rec->big); free(rec->small); } bonline(rec->big); binsb(rec->big->eof, b); boffline(rec->big); } else { rec->small = (unsigned char *) joe_realloc(rec->small, rec->len + size); brmem(b->bof, rec->small + rec->len, (int) size); brm(b); } rec->len += size; } else if (rec != &undo->recs && rec->min && rec->del && where + size == rec->where) { if (rec->len + size >= SMALL) { if (rec->len < SMALL) { rec->big = bmk(NULL); binsm(rec->big->bof, rec->small, (int) rec->len); boffline(rec->big); free(rec->small); } bonline(rec->big); binsb(rec->big->bof, b); boffline(rec->big); } else { rec->small = (unsigned char *) joe_realloc(rec->small, rec->len + size); memmove(rec->small + size, rec->small, (int) rec->len); brmem(b->bof, rec->small, (int) size); brm(b); } rec->len += size; rec->where = where; } else { rec = alrec(); if (size < SMALL) { rec->small = (unsigned char *) joe_malloc(size); brmem(b->bof, rec->small, (int) b->eof->byte); brm(b); } else { rec->big = b; boffline(b); } if (!undo->first) undo->first = rec; undo->last = rec; rec->where = where; rec->min = 1; rec->unit = NULL; rec->len = size; rec->del = 1; rec->changed = undo->b->changed; enqueb(UNDOREC, link, &undo->recs, rec); } }
static void yankdel(long where, B *b) { UNDOREC *rec; long size = b->eof->byte; /* Store in yank buffer */ rec = yanked.link.prev; if (!inyank) { if (rec != &yanked && where == rec->where && justkilled) { if (rec->len + size >= SMALL) { if (rec->len < SMALL) { rec->big = bmk(NULL); binsm(rec->big->bof, rec->small, (int) rec->len); boffline(rec->big); free(rec->small); } bonline(rec->big); binsb(rec->big->eof, bcpy(b->bof, b->eof)); boffline(rec->big); } else { rec->small = (unsigned char *) joe_realloc(rec->small, rec->len + size); brmem(b->bof, rec->small + rec->len, (int) size); } rec->len += size; } else if (rec != &yanked && where + size == rec->where && justkilled) { if (rec->len + size >= SMALL) { if (rec->len < SMALL) { rec->big = bmk(NULL); binsm(rec->big->bof, rec->small, (int) rec->len); boffline(rec->big); free(rec->small); } bonline(rec->big); binsb(rec->big->bof, bcpy(b->bof, b->eof)); boffline(rec->big); } else { rec->small = (unsigned char *) joe_realloc(rec->small, rec->len + size); memmove(rec->small + size, rec->small, (int) rec->len); brmem(b->bof, rec->small, (int) size); } rec->len += size; rec->where = where; } else { if (++nyanked == MAX_YANK) { frrec(deque_f(UNDOREC, link, yanked.link.next)); --nyanked; } rec = alrec(); if (size < SMALL) { rec->small = (unsigned char *) joe_malloc(size); brmem(b->bof, rec->small, (int) b->eof->byte); } else { rec->big = bcpy(b->bof, b->eof); boffline(rec->big); } rec->where = where; rec->len = size; rec->del = 1; enqueb(UNDOREC, link, &yanked, rec); } } }