Exemple #1
0
JFILE *jfopen(unsigned char *name, char *mode)
{
	if (name[0] == '*') {
		int x;
		for (x = 0; builtins[x]; x += 2) {
			if (!zcmp(builtins[x], name + 1)) {
				JFILE *j = (JFILE *)joe_malloc(sizeof(JFILE));
				j->f = 0;
				j->p = builtins[x + 1];
				return j;
			}
		}
		return 0;
	} else {
		FILE *f = fopen((char *)name, (char *)mode);
		if (f) {
			JFILE *j = (JFILE *)joe_malloc(sizeof(JFILE));
			j->f = f;
			j->p = 0;
			return j;
		} else {
			return 0;
		}
	}
}
Exemple #2
0
struct high_syntax *load_syntax_subr(unsigned char *name, unsigned char *subr, struct high_param *params)
{
    struct high_syntax *syntax; /* New syntax table */

    /* Find syntax table */

    /* Already loaded? */
    for (syntax = syntax_list; syntax; syntax = syntax->next)
    {
        if (syntax_match(syntax, name, subr, params))
        {
            return syntax;
        }
    }

    /* Create new one */
    syntax = joe_malloc(sizeof(struct high_syntax));
    syntax->name = zdup(name);
    syntax->subr = subr ? zdup(subr) : 0;
    syntax->params = params;
    syntax->next = syntax_list;
    syntax->nstates = 0;
    syntax->color = 0;
    syntax->states = joe_malloc(sizeof(struct high_state *) * (syntax->szstates = 64));
    syntax->ht_states = htmk(syntax->szstates);
    iz_cmd(&syntax->default_cmd);
    syntax->default_cmd.reset = 1;
    syntax->stack_base = 0;
    syntax_list = syntax;

    if (load_dfa(syntax))
    {
        /* dump_syntax(syntax); */
        return syntax;
    }
    else
    {
        if (syntax_list == syntax)
        {
            syntax_list = syntax_list->next;
        }
        else
        {
            struct high_syntax *syn;
            for (syn = syntax_list; syn->next != syntax; syn = syn->next);
            syn->next = syntax->next;
        }
        htrm(syntax->ht_states);
        joe_free(syntax->name);
        joe_free(syntax->states);
        joe_free(syntax);
        return 0;
    }
}
Exemple #3
0
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;
}
Exemple #4
0
unsigned char *zdup(unsigned char *bf)
{
	int size = zlen(bf);
	unsigned char *p = (unsigned char *)joe_malloc(size+1);
	memcpy(p,bf,size+1);
	return p;
}
Exemple #5
0
void load_yank(FILE *f)
{
	UNDOREC *rec;
	unsigned char buf[SMALL*4+80];
	unsigned char bf[SMALL+1];
	while(fgets((char *)buf,sizeof(buf)-1,f) && strcmp(buf,"done\n")) {
		unsigned char *p = buf;
		int len;
		parse_ws(&p,'#');
		len = parse_string(&p,bf,sizeof(bf));
		if (len>0 && len<=SMALL) {
			if (++nyanked == MAX_YANK) {
				frrec(deque_f(UNDOREC, link, yanked.link.next));
				--nyanked;
			}
			rec = alrec();
			rec->small = (unsigned char *) joe_malloc(len);
			memcpy(rec->small,bf,len);
			rec->where = -1;
			rec->len = len;
			rec->del = 1;
			enqueb(UNDOREC, link, &yanked, rec);
		}
	}
}
Exemple #6
0
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;
}
Exemple #7
0
ZS raw_mk_zs(GC **gc,unsigned char *s,int len)
{
	ZS zs;
	zs.s = (unsigned char *)joe_malloc(len+1);
	if (len)
		memcpy(zs.s,s,len);
	zs.s[len] = 0;
	return zs;
}
Exemple #8
0
KBD *mkkbd(KMAP *kmap)
{
    KBD *kbd = (KBD *) joe_malloc(sizeof(KBD));

    kbd->topmap = kmap;
    kbd->curmap = kmap;
    kbd->x = 0;
    return kbd;
}
Exemple #9
0
void *joe_calloc(size_t nmemb, size_t size)
{
	size_t sz = nmemb*size;
	int x;
	unsigned char *ptr;
	ptr = joe_malloc(sz);
	for(x=0;x!=sz;++x)
		ptr[x] = 0;
	return ptr;
}
Exemple #10
0
void *replenish(void **list,int size)
{
	unsigned char *i = joe_malloc(size*16);
	int x;
	for (x=0; x!=15; ++x) {
		fr_single(list, i);
		i += size;
	}
	return i;
}
Exemple #11
0
KMAP *kmap_getcontext(unsigned char *name)
{
    struct context *c;

    for (c = contexts; c; c = c->next)
        if (!zcmp(c->name, name))
            return c->kmap;
    c = (struct context *) joe_malloc(sizeof(struct context));

    c->next = contexts;
    c->name = zdup(name);
    contexts = c;
    return c->kmap = mkkmap();
}
Exemple #12
0
void addcmd(unsigned char *s, MACRO *m)
{
	CMD *cmd = (CMD *) joe_malloc(sizeof(CMD));

	if (!cmdhash)
		izcmds();
	cmd->name = zdup(s);
	cmd->flag = 0;
	cmd->func = NULL;
	cmd->m = m;
	cmd->arg = 1;
	cmd->negarg = NULL;
	htadd(cmdhash, cmd->name, cmd);
}
Exemple #13
0
void *alitem(void *list, ptrdiff_t itemsize)
{
	STDITEM	*freelist = (STDITEM *)list;

	if (qempty(STDITEM, link, freelist)) {
		STDITEM *i = (STDITEM *) joe_malloc(itemsize * 16);
		STDITEM *z = (STDITEM *) ((char *) i + itemsize * 16);

		while (i != z) {
			enquef(STDITEM, link, freelist, i);
			i = (STDITEM *) ((char *) i + itemsize);
		}
	}
	return (void *) deque_f(STDITEM, link, freelist->link.prev);
}
Exemple #14
0
struct lattr_db *mk_lattr_db(B *new_b, struct high_syntax *new_syn)
{
	struct lattr_db *db = (struct lattr_db *)joe_malloc(SIZEOF(struct lattr_db));
	db->next = 0;
	db->syn = new_syn;
	db->b = new_b;
	db->end = 512;
	db->hole = 1;
	db->ehole = db->end;
	db->buffer = (HIGHLIGHT_STATE *)joe_malloc(db->end * SIZEOF(HIGHLIGHT_STATE));
	db->first_invalid = 1;
	db->invalid_window = -1;
	/* State of first line is idle */
	clear_state(db->buffer+0);
	return db;
}
Exemple #15
0
void parse_color_def(struct high_color **color_list, unsigned char *p, unsigned char *name, int line)
{
    unsigned char bf[256];
    if (!parse_tows(&p, bf))
    {
        struct high_color *color, *gcolor;

        /* Find color */
        color = find_color(*color_list, bf, name);

        /* If it doesn't exist, create it */
        if (!color)
        {
            color = joe_malloc(sizeof(struct high_color));
            color->name = zdup(bf);
            color->color = 0;
            color->next = *color_list;
            *color_list = color;
        }
        else
        {
            i_printf_2((char *)joe_gettext(_("%s %d: Class already defined\n")), name, line);
        }

        /* Find it in global list */
        if (color_list != &global_colors && (gcolor = find_color(global_colors, bf, name)))
        {
            color->color = gcolor->color;
        }
        else
        {
            /* Parse color definition */
            while (parse_ws(&p, '#'), !parse_ident(&p, bf, sizeof(bf)))
            {
                color->color |= meta_color(bf);
            }
        }
    }
    else
    {
        i_printf_2((char *)joe_gettext(_("%s %d: Missing class name\n")), name, line);
    }
}
Exemple #16
0
void *joe_realloc(void *ptr, size_t size)
{
	struct mcheck *n;
	unsigned char *np;
	struct mcheck *m = (struct mcheck *)((char *)ptr-sizeof(struct mcheck));

	if(!size) {
		printf("0 passed to realloc\n");
		*(int *)0=0;
	}

	np = joe_malloc(size);

	n = (struct mcheck *)(np-sizeof(struct mcheck));

	if(m->size>size)
		memcpy(np,ptr,size);
	else
		memcpy(np,ptr,m->size);

	joe_free(ptr);

	return np;
}
Exemple #17
0
int uformat(BW *bw)
{
	long indent;
	unsigned char *indents;
	B *buf;
	P *b;
	long curoff;
	int c;
	P *p, *q;

	p = pdup(bw->cursor, USTR "uformat");
	p_goto_bol(p);

	/* Do nothing if we're not on a paragraph line */
	if (pisnpara(bw, p)) {
		prm(p);
		return 0;
	}

	/* Move p to beginning of paragraph, bw->cursor to end of paragraph and
	 * set curoff to original cursor offset within the paragraph */
	pbop(bw, p);
	curoff = bw->cursor->byte - p->byte;
	pset(bw->cursor, p);
	peop(bw, bw->cursor);

	/* Ensure that paragraph ends on a beginning of a line */
	if (!pisbol(bw->cursor))
		binsc(bw->cursor, '\n'), pgetc(bw->cursor);

	/* Record indentation of second line of paragraph, of first line if there
	 * is only one line */
	q = pdup(p, USTR "uformat");
	pnextl(q);
	if (q->line != bw->cursor->line) {
		P *r = pdup(q, USTR "uformat");

		indent = nindent(bw, q, 0);
		pcol(r, indent);
		indents = brs(q, r->byte - q->byte);
		prm(r);
	} else {
		P *r = pdup(p, USTR "uformat");
		int x, y;
		indent = nindent(bw, p, 1); /* allowing * and - here */
		pcol(r, indent);
		indents = brs(p, r->byte - p->byte);
		prm(r);
		if (!bw->o.autoindent) {
			/* Don't indent second line of single-line paragraphs if autoindent is off */
			int x = zlen(indents);
			while (x && (indents[x - 1] == ' ' || indents[x - 1] == '\t'))
				indents[--x] = 0;
			if (x) {
				indents[x++] = ' ';
				indents[x] = 0;
			}
			indent = txtwidth1(bw->o.charmap, bw->o.tab, indents, x);
		}
		for (x = 0; indents[x] && (indents[x] == ' ' || indents[x] == '\t'); ++x);
		y = zlen(indents);
		while (y && (indents[y - 1] == ' ' || indents[y - 1] == '\t'))
			--y;
		/* Don't duplicate if it looks like a bullet */
/*		if (y && (indents[y - 1] == '*' || indents[y - 1] == '-') &&
		    (y == 1 || indents[y - 2] == ' ' || indents[y - 2] == '\t'))
			indents[y - 1] = ' '; */
		/* Fix C comment */
		if (indents[x] == '/' && indents[x + 1] == '*')
			indents[x] = ' ';
	}
	prm(q);

	/* But if the left margin is greater, we use that instead */
	if (bw->o.lmargin > indent) {
		int x;
		for (x = 0; indents[x] == ' ' || indents[x] == '\t'; ++x);
		if (!indents[x]) {
			joe_free(indents);
			indent = bw->o.lmargin;
			indents = joe_malloc(indent+1);
			for (x = 0; x != indent; ++x)
				indents[x] = ' ';
			indents[x] = 0;
		}
	}

	/* Cut paragraph into new buffer */
	
	/* New buffer needs to inherit UTF-8 and CR-LF options */
	buf = bcpy(p, bw->cursor);
	buf->o.crlf = p->b->o.crlf;
	buf->o.charmap = p->b->o.charmap;
	bdel(p, bw->cursor);

	/* text is in buffer.  insert it at cursor */

	/* Do first line */
	b = pdup(buf->bof, USTR "uformat");

	while (!piseof(b)) {
		/* Set cursor position if we're at original offset */
		if (b->byte == curoff)
			pset(bw->cursor, p);

		/* Get character from buffer */
		c = pgetc(b);

		/* Stop if we found end of line */
		if (c == '\n') {
			prgetc(b);
			break;
		}

		/* Stop if we found white-space followed by end of line */
		if (joe_isblank(b->b->o.charmap, c) && piseolblank(b)) {
			prgetc(b);
			break;
		}

		/* Insert character, advance pointer */
		binsc(p, c);
		pgetc(p);

		/* Do word wrap if we reach right margin */
		if (piscol(p) > bw->o.rmargin && !joe_isblank(p->b->o.charmap,c)) {
			wrapword(bw, p, indent, bw->o.french, 1, indents);
			break;
		}
	}

	/* Do rest */

	while (!piseof(b)) {
		c = brch(b);
		if (joe_isblank(b->b->o.charmap,c) || c == '\n') {
			int f = 0;
			P *d;
			int g;

			/* Set f if there are two spaces after . ? or ! instead of one */
			/* (What is c was '\n'?) */
			d=pdup(b, USTR "uformat");
			g=prgetc(d);
			if (g=='.' || g=='?' || g=='!') {
				f = 1;
/*				pset(d,b);
				pgetc(d);
				if (c == '\n' || piseol(d) || joe_isspace(bw->b->o.charmap,brch(d))) {
					f = 1;
				}
*/
			}
			prm(d);
			
			/* Skip past the whitespace.  Skip over indentations */
		      loop:
			
			c = brch(b);
			if (c == '\n') {
				if (b->byte == curoff)
					pset(bw->cursor, p);

				pgetc(b);
				while (cpara(bw, (c=brch(b)))) {
					if (b->byte == curoff)
						pset(bw->cursor, p);
					pgetc(b);
				}
			}

			if (joe_isblank(b->b->o.charmap,c)) {
				if(b->byte == curoff)
					pset(bw->cursor, p);
				pgetc(b);
				goto loop;
			}

			/* Insert proper amount of whitespace */
			if (!piseof(b)) {
				if (f && !bw->o.french)
					binsc(p, ' '), pgetc(p);
				binsc(p, ' ');
				pgetc(p);
			}
		} else {
			/* Insert characters of word and wrap if necessary */
			if (b->byte == curoff)
				pset(bw->cursor, p);

			binsc(p, pgetc(b));
			pgetc(p);
			if (piscol(p) > bw->o.rmargin)
				wrapword(bw, p, indent, bw->o.french, 1, indents);
		}
	}

	binsc(p, '\n');
	prm(p);
	brm(buf);
	joe_free(indents);
	return 0;
}
Exemple #18
0
void ttopnn(void)
{
	int x, bbaud;

#ifdef HAVE_POSIX_TERMIOS
	struct termios newterm;
#else
#ifdef HAVE_SYSV_TERMIO
	struct termio newterm;
#else
	struct sgttyb arg;
	struct tchars targ;
	struct ltchars ltarg;
#endif
#endif

	if (!termin) {
		if (idleout ? (!(termin = stdin) || !(termout = stdout)) : (!(termin = fopen("/dev/tty", "r")) || !(termout = fopen("/dev/tty", "w")))) {
			fprintf(stderr, (char *)joe_gettext(_("Couldn\'t open /dev/tty\n")));
			exit(1);
		} else {
#ifdef SIGWINCH
			joe_set_signal(SIGWINCH, winchd);
#endif
		}
	}

	if (ttymode)
		return;
	ttymode = 1;
	fflush(termout);

#ifdef HAVE_POSIX_TERMIOS
	tcgetattr(fileno(termin), &oldterm);
	newterm = oldterm;
	newterm.c_lflag = 0;
	if (noxon)
		newterm.c_iflag &= ~(ICRNL | IGNCR | INLCR | IXON | IXOFF);
	else
		newterm.c_iflag &= ~(ICRNL | IGNCR | INLCR);
	newterm.c_oflag = 0;
	newterm.c_cc[VMIN] = 1;
	newterm.c_cc[VTIME] = 0;
	tcsetattr(fileno(termin), TCSADRAIN, &newterm);
	bbaud = cfgetospeed(&newterm);
#else
#ifdef HAVE_SYSV_TERMIO
	joe_ioctl(fileno(termin), TCGETA, &oldterm);
	newterm = oldterm;
	newterm.c_lflag = 0;
	if (noxon)
		newterm.c_iflag &= ~(ICRNL | IGNCR | INLCR | IXON | IXOFF);
	else
		newterm.c_iflag &= ~(ICRNL | IGNCR | INLCR);
	newterm.c_oflag = 0;
	newterm.c_cc[VMIN] = 1;
	newterm.c_cc[VTIME] = 0;
	joe_ioctl(fileno(termin), TCSETAW, &newterm);
	bbaud = (newterm.c_cflag & CBAUD);
#else
	joe_ioctl(fileno(termin), TIOCGETP, &arg);
	joe_ioctl(fileno(termin), TIOCGETC, &targ);
	joe_ioctl(fileno(termin), TIOCGLTC, &ltarg);
	oarg = arg;
	otarg = targ;
	oltarg = ltarg;
	arg.sg_flags = ((arg.sg_flags & ~(ECHO | CRMOD | XTABS | ALLDELAY | TILDE)) | CBREAK);
	if (noxon) {
		targ.t_startc = -1;
		targ.t_stopc = -1;
	}
	targ.t_intrc = -1;
	targ.t_quitc = -1;
	targ.t_eofc = -1;
	targ.t_brkc = -1;
	ltarg.t_suspc = -1;
	ltarg.t_dsuspc = -1;
	ltarg.t_rprntc = -1;
	ltarg.t_flushc = -1;
	ltarg.t_werasc = -1;
	ltarg.t_lnextc = -1;
	joe_ioctl(fileno(termin), TIOCSETN, &arg);
	joe_ioctl(fileno(termin), TIOCSETC, &targ);
	joe_ioctl(fileno(termin), TIOCSLTC, &ltarg);
	bbaud = arg.sg_ospeed;
#endif
#endif

	baud = 9600;
	upc = 0;
	for (x = 0; x != 30; x += 2)
		if (bbaud == speeds[x]) {
			baud = speeds[x + 1];
			break;
		}
	if (Baud)
		baud = Baud;
	upc = DIVIDEND / baud;
	if (obuf)
		joe_free(obuf);
	if (!(TIMES * upc))
		obufsiz = 4096;
	else {
		obufsiz = 1000000 / (TIMES * upc);
		if (obufsiz > 4096)
			obufsiz = 4096;
	}
	if (!obufsiz)
		obufsiz = 1;
	obuf = (unsigned char *) joe_malloc(obufsiz);
}
Exemple #19
0
struct high_state *load_dfa(struct high_syntax *syntax)
{
    unsigned char name[1024];
    unsigned char buf[1024];
    unsigned char bf[256];
    int clist[256];
    unsigned char *p;
    int c;
    FILE *f = NULL;
    struct ifstack *stack = 0;
    struct high_state *state = 0; /* Current state */
    struct high_state *first = 0; /* First state */
    int line = 0;
    int this_one = 0;
    int inside_subr = 0;

    /* Load it */

    if ((p = (unsigned char *)exists_prefs_dir()) && strlen((const char *)p) + 2 + strlen(SYNTAX_DIR) + strlen(SYNTAX_EXT) + strlen((const char *)syntax->name) < sizeof name)
    {
        strcat(strcat(strcat(strcat(strcpy((char *)name, (const char *)p), SYNTAX_DIR), "/"), (const char *)syntax->name), SYNTAX_EXT);
        f = fopen((char *)name, "r");
    }

    if (!f && (p = (unsigned char *)exists_gprefs_dir()) && strlen((const char *)p) + 2 + strlen(SYNTAX_DIR) + strlen(SYNTAX_EXT) + strlen((const char *)syntax->name) < sizeof name)
    {
        strcat(strcat(strcat(strcat(strcpy((char *)name, (const char *)p), SYNTAX_DIR), "/"), (const char *)syntax->name), SYNTAX_EXT);
        f = fopen((char *)name, "r");
    }

    if (!f)
    {
        return 0;
    }

    /* Parse file */
    while (fgets((char *)buf, 1023, f))
    {
        ++line;
        p = buf;
        c = parse_ws(&p, '#');
        if (!parse_char(&p, '.'))
        {
            if (!parse_ident(&p, bf, sizeof(bf)))
            {
                if (!zcmp(bf, USTR "ifdef"))
                {
                    struct ifstack *st = joe_malloc(sizeof(struct ifstack));
                    st->next = stack;
                    st->else_part = 0;
                    st->ignore = 1;
                    st->skip = 1;
                    st->line = line;
                    if (!stack || !stack->ignore)
                    {
                        parse_ws(&p, '#');
                        if (!parse_ident(&p, bf, sizeof(bf)))
                        {
                            struct high_param *param;
                            for (param = syntax->params; param; param = param->next)
                                if (!zcmp(param->name, bf))
                                {
                                    st->ignore = 0;
                                    break;
                                }
                            st->skip = 0;
                        }
                        else
                        {
                            i_printf_2((char *)joe_gettext(_("%s %d: missing parameter for ifdef\n")), name, line);
                        }
                    }
                    stack = st;
                }
                else if (!zcmp(bf, USTR "else"))
                {
                    if (stack && !stack->else_part)
                    {
                        stack->else_part = 1;
                        if (!stack->skip)
                        {
                            stack->ignore = !stack->ignore;
                        }
                    }
                    else
                    {
                        i_printf_2((char *)joe_gettext(_("%s %d: else with no matching if\n")), name, line);
                    }
                }
                else if (!zcmp(bf, USTR "endif"))
                {
                    if (stack)
                    {
                        struct ifstack *st = stack;
                        stack = st->next;
                        joe_free(st);
                    }
                    else
                    {
                        i_printf_2((char *)joe_gettext(_("%s %d: endif with no matching if\n")), name, line);
                    }
                }
                else if (!zcmp(bf, USTR "subr"))
                {
                    parse_ws(&p, '#');
                    if (parse_ident(&p, bf, sizeof(bf)))
                    {
                        i_printf_2((char *)joe_gettext(_("%s %d: Missing subroutine name\n")), name, line);
                    }
                    else
                    {
                        if (!stack || !stack->ignore)
                        {
                            inside_subr = 1;
                            this_one = 0;
                            if (syntax->subr && !zcmp(bf, syntax->subr))
                            {
                                this_one = 1;
                            }
                        }
                    }
                }
                else if (!zcmp(bf, USTR "end"))
                {
                    if (!stack || !stack->ignore)
                    {
                        this_one = 0;
                        inside_subr = 0;
                    }
                }
                else
                {
                    i_printf_2((char *)joe_gettext(_("%s %d: Unknown control statement\n")), name, line);
                }
            }
            else
            {
                i_printf_2((char *)joe_gettext(_("%s %d: Missing control statement name\n")), name, line);
            }
        }
        else if (stack && stack->ignore)
        {
            /* Ignore this line because of ifdef */
        }
        else if (!parse_char(&p, '='))
        {
            /* Parse color */
            parse_color_def(&syntax->color, p, name, line);
        }
        else if ((syntax->subr && !this_one) || (!syntax->subr && inside_subr))
        {
            /* Ignore this line because it's not the code we want */
        }
        else if (!parse_char(&p, ':'))
        {
            if (!parse_ident(&p, bf, sizeof(bf)))
            {

                state = find_state(syntax, bf);

                if (!first)
                {
                    first = state;
                }

                parse_ws(&p, '#');
                if (!parse_tows(&p, bf))
                {
                    struct high_color *color;
                    for (color = syntax->color; color; color = color->next)
                        if (!zcmp(color->name, bf))
                        {
                            break;
                        }
                    if (color)
                    {
                        state->color = color->color;
                    }
                    else
                    {
                        state->color = 0;
                        i_printf_2((char *)joe_gettext(_("%s %d: Unknown class\n")), name, line);
                    }
                }
                else
                {
                    i_printf_2((char *)joe_gettext(_("%s %d: Missing color for state definition\n")), name, line);
                }
            }
            else
            {
                i_printf_2((char *)joe_gettext(_("%s %d: Missing state name\n")), name, line);
            }
        }
        else if (!parse_char(&p, '-'))
        {
            /* No. sync lines ignored */
        }
        else
        {
            c = parse_ws(&p, '#');

            if (!c)
            {
            }
            else if (c == '"' || c == '*' || c == '&')
            {
                if (state)
                {
                    struct high_cmd *cmd;
                    int delim = 0;
                    if (!parse_field(&p, USTR "*"))
                    {
                        int z;
                        for (z = 0; z != 256; ++z)
                        {
                            clist[z] = 1;
                        }
                    }
                    else if (!parse_field(&p, USTR "&"))
                    {
                        delim = 1;
                    }
                    else
                    {
                        c = parse_string(&p, bf, sizeof(bf));
                        if (c < 0)
                        {
                            i_printf_2((char *)joe_gettext(_("%s %d: Bad string\n")), name, line);
                        }
                        else
                        {
                            int z;
                            int first, second;
                            unsigned char *t = bf;
                            for (z = 0; z != 256; ++z)
                            {
                                clist[z] = 0;
                            }
                            while (!parse_range(&t, &first, &second))
                            {
                                if (first > second)
                                {
                                    second = first;
                                }
                                while (first <= second)
                                {
                                    clist[first++] = 1;
                                }
                            }
                        }
                    }
                    /* Create command */
                    cmd = mkcmd();
                    parse_ws(&p, '#');
                    if (!parse_ident(&p, bf, sizeof(bf)))
                    {
                        int z;
                        cmd->new_state = find_state(syntax, bf);
                        parse_options(syntax, cmd, f, p, 0, name, line);

                        /* Install command */
                        if (delim)
                        {
                            state->delim = cmd;
                        }
                        else for (z = 0; z != 256; ++z)
                                if (clist[z])
                                {
                                    state->cmd[z] = cmd;
                                }
                    }
                    else
                    {
                        i_printf_2((char *)joe_gettext(_("%s %d: Missing jump\n")), name, line);
                    }
                }
                else
                {
                    i_printf_2((char *)joe_gettext(_("%s %d: No state\n")), name, line);
                }
            }
            else
            {
                i_printf_2((char *)joe_gettext(_("%s %d: Unknown character\n")), name, line);
            }
        }
    }

    while (stack)
    {
        struct ifstack *st = stack;
        stack = st->next;
        i_printf_2((char *)joe_gettext(_("%s %d: ifdef with no matching endif\n")), name, st->line);
        joe_free(st);
    }

    fclose(f);

    return first;
}
Exemple #20
0
struct high_param *parse_params(struct high_param *current_params, unsigned char **ptr, unsigned char *name, int line)
{
    unsigned char *p = *ptr;
    unsigned char bf[256];
    struct high_param *params;
    struct high_param **param_ptr;

    /* Propagate currently defined parameters */
    param_ptr = &params;
    while (current_params)
    {
        *param_ptr = joe_malloc(sizeof(struct high_param));
        (*param_ptr)->name = zdup(current_params->name);
        param_ptr = &(*param_ptr)->next;
        current_params = current_params->next;
    }
    *param_ptr = 0;

    parse_ws(&p, '#');
    if (!parse_char(&p, '('))
    {
        for (;;)
        {
            parse_ws(&p, '#');
            if (!parse_char(&p, ')'))
            {
                break;
            }
            else if (!parse_char(&p, '-'))
            {
                if (!parse_ident(&p, bf, sizeof(bf)))
                {
                    int cmp = 0;
                    param_ptr = &params;
                    /* Parameters are sorted */
                    while (*param_ptr && (cmp = zcmp(bf, (*param_ptr)->name)) > 0)
                    {
                        param_ptr = &(*param_ptr)->next;
                    }
                    if (*param_ptr && !cmp)
                    {
                        /* Remove this parameter */
                        struct high_param *param = *param_ptr;
                        *param_ptr = param->next;
                        joe_free(param);
                    }
                }
                else
                {
                    i_printf_2((char *)joe_gettext(_("%s %d: Missing parameter name\n")), name, line);
                }
            }
            else if (!parse_ident(&p, bf, sizeof(bf)))
            {
                int cmp = 0;
                param_ptr = &params;
                /* Keep parameters sorted */
                while (*param_ptr && (cmp = zcmp(bf, (*param_ptr)->name)) > 0)
                {
                    param_ptr = &(*param_ptr)->next;
                }
                /* Discard duplicates */
                if (!*param_ptr || cmp)
                {
                    struct high_param *param = joe_malloc(sizeof(struct high_param));
                    param->name = zdup(bf);
                    param->next = *param_ptr;
                    *param_ptr = param;
                }
            }
            else
            {
                i_printf_2((char *)joe_gettext(_("%s %d: Missing )\n")), name, line);
                break;
            }
        }
    }

    *ptr = p;
    return params;
}
Exemple #21
0
static struct high_cmd *mkcmd()
{
    struct high_cmd *cmd = joe_malloc(sizeof(struct high_cmd));
    iz_cmd(cmd);
    return cmd;
}
Exemple #22
0
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;
}
Exemple #23
0
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);
		}
	}
}
Exemple #24
0
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);
	}
}
Exemple #25
0
void wrapword(BW *bw, P *p, long int indent, int french, int no_over, unsigned char *indents)
{
	P *q;
	P *r;
	P *s;
	int rmf = 0;
	int c;
	long to = p->byte;
	int my_indents = 0;
	
	/* autoindent when called by utype */
	if (!indents) {
		/* Get indentation prefix from beginning of line */
		s = pdup(p, USTR "wrapword");
		p_goto_bol(s);
		pbop(bw, s);
		/* Record indentation of second line of paragraph, of first
		 * line if there is only one line */
		q = pdup(s, USTR "wrapword");
		pnextl(q);
		if (q->line < p->line) {
			/* Second line */
			P *r = pdup(q, USTR "wrapword");

			indent = nindent(bw, q, 0);
			pcol(r, indent);
			indents = brs(q, r->byte - q->byte);
			prm(r);
		} else {
			/* First line */
			P *r = pdup(s, USTR "uformat");
			int x, y;

			indent = nindent(bw, s, 1);
			pcol(r, indent);
			indents = brs(s, r->byte - s->byte);
			prm(r);
			if (!bw->o.autoindent) {
				/* Don't indent second line of single-line paragraphs if autoindent is off */
				int x = zlen(indents);
				while (x && (indents[x - 1] == ' ' || indents[x - 1] == '\t'))
					indents[--x] = 0;
				if (x) {
					indents[x++] = ' ';
					indents[x] = 0;
				}
				indent = txtwidth1(bw->o.charmap, bw->o.tab, indents, x);
			}
			for (x = 0; indents[x] && (indents[x] == ' ' || indents[x] == '\t'); ++x);
			y = zlen(indents);
			while (y && (indents[y - 1] == ' ' || indents[y - 1] == '\t'))
				--y;
			/* Don't duplicate bullet */
/*			if (y && (indents[y - 1] == '*' || indents[y - 1] == '-') &&
			    (y == 1 || indents[y - 2] == ' ' || indents[y - 2] == '\t'))
			    	indents[y - 1] = ' '; */
			/* Fix C comment */
			if (indents[x] == '/' && indents[x + 1] == '*')
				indents[x] = ' ';
		}
		if (bw->o.lmargin > indent) {
			int x;
			for (x = 0; indents[x] == ' ' || indents[x] == '\t'; ++x);
			if (!indents[x]) {
				joe_free(indents);
				indent = bw->o.lmargin;
				indents = joe_malloc(indent+1);
				for (x = 0; x != indent; ++x)
					indents[x] = ' ';
				indents[x] = 0;
			}
		}
		my_indents = 1;
		prm(q);
		prm(s);
	}


/*
	if(!indents) {
		int f = 0;
		P *r = pdup(p);

		p_goto_bol(r);
		q = pdup(r);
		while(cpara(c = brc(q))) {
			if(!joe_isblank(c))
				f = 1;
			pgetc(q);
		}
		if(f) {
			indents = brs(r, q->byte-r->byte);
			rmf = 1;
			if(indents[0] == '/' && indents[1] == '*')
				indents[0] = ' ';
		}
		prm(r);
		prm(q);
	}
*/

	/* Get to beginning of word */
	while (!pisbol(p) && piscol(p) > indent && !joe_isblank(p->b->o.charmap, prgetc(p)))
		/* do nothing */;

	/* If we found the beginning of a word... */
	if (!pisbol(p) && piscol(p) > indent) {
		/* Move q to two (or one if 'french' is set) spaces after end of previous
		   word */
		q = pdup(p, USTR "wrapword");
		while (!pisbol(q))
			if (!joe_isblank(p->b->o.charmap, (c = prgetc(q)))) {
				pgetc(q);
				if ((c == '.' || c == '?' || c == '!')
				    && q->byte != p->byte && !french)
					pgetc(q);
				break;
			}
		pgetc(p);

		/* Delete space between start of word and end of previous word */
		to -= p->byte - q->byte;
		bdel(q, p);
		prm(q);

		if (bw->o.flowed) {
			binsc(p, ' ');
			pgetc(p);
			++to;
		}

		/* Move word to beginning of next line */
		binsc(p, '\n');
		
		/* When overtype is on, do not insert lines */
		if (!no_over && p->b->o.overtype){
			/* delete the next line break which is unnecessary */
			r = pdup(p, USTR "wrapword");
			/* p_goto_eol(r); */
			pgetc(r);
			p_goto_eol(r);
			s = pdup(r, USTR "wrapword");
			pgetc(r);
			bdel(s,r);
			binsc(r, ' ');
			
			/* Now we got to take care that all subsequent lines are not longer than the right margin */
			/* Move cursor to right margin */
			pfwrd(r, r->b->o.rmargin - r->col);
			
			/* Make a copy of the cursor and move the copied cursor to the end of the line */
			prm(s);
			s = pdup(r, USTR "wrapword");
			p_goto_eol(s);
			
			/* If s is located behind r then the line goes beyond the right margin and we need to call wordwrap() for that line. */
/*
			if (r->byte < s->byte){
				wrapword(bw, r, indent, french, 1, indents);
			}
*/			
			prm(r);
			prm(s);
		}
		
		++to;
		if (p->b->o.crlf)
			++to;
		pgetc(p);

		/* Indent to left margin */
		if (indents) {
			binss(p, indents);
			to += zlen(indents);
		} else
			while (indent--) {
				binsc(p, ' ');
				++to;
			}

		if (rmf)
			joe_free(indents);
	}

	/* Move cursor back to original position */
	pfwrd(p, to - p->byte);
	if (my_indents)
		joe_free(indents);
}