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; }
unsigned char *my_gettext(unsigned char *s) { if (gettext_ht) { unsigned char *r = htfind(gettext_ht, s); if (r) s = r; } if (s[0] == '|') return ignore_prefix(s); else return s; }
const char *my_gettext(const char *s) { if (gettext_ht) { const char *r = (const char *)htfind(gettext_ht, s); if (r) s = r; } if (s[0] == '|') return ignore_prefix(s); else return s; }
/* count all 'buf' substrings of length 'len', return count for buf[0..len-1] */ static void do_cnt(const struct buf *seq, unsigned len, char *buf) { const char *Match = "GGTATTTTAATTTATAGT"; unsigned long cnt = 0; struct ht t; htinit(&t, HT_BINS, MIN(dna_combo(len), MAX_ENTRIES)); freq_build(&t, seq, len); struct htentry *e = htfind(&t, Match, len, index(Match, len)); if (e) cnt = e->val.cnt; htfree(&t); sprintf(buf, "%lu\t%.*s\n", cnt, len, Match); }
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; }
CMD *findcmd(unsigned char *s) { if (!cmdhash) izcmds(); return (CMD *) htfind(cmdhash, s); }