FreeBSD/contrib/mdocml/eqn.c
/*
 * Get the next token from the input stream using the given quote
 * character.
 * Optionally make any replacements.
 */
Get the next token from the input stream using the given quote
character.
Optionally make any replacements.
Get the next token from the input stream using the given quote character . Optionally make any replacements .
static const char *
eqn_next(struct eqn_node *ep, char quote, size_t *sz, int repl)
{
	char		*start, *next;
	int		 q, diff, lim;
	size_t		 ssz, dummy;
	struct eqn_def	*def;
	if (NULL == sz)
		sz = &dummy;
	lim = 0;
	ep->rew = ep->cur;
again:
	if (lim >= EQN_NEST_MAX) {
		mandoc_msg(MANDOCERR_ROFFLOOP, ep->parse,
		    ep->eqn.ln, ep->eqn.pos, NULL);
		return NULL;
	}
	ep->cur = ep->rew;
	start = &ep->data[(int)ep->cur];
	q = 0;
	if ('\0' == *start)
		return NULL;
	if (quote == *start) {
		ep->cur++;
		q = 1;
	}
	start = &ep->data[(int)ep->cur];
	if ( ! q) {
		if ('{' == *start || '}' == *start)
			ssz = 1;
		else
			ssz = strcspn(start + 1, " ^~\"{}\t") + 1;
		next = start + (int)ssz;
		if ('\0' == *next)
			next = NULL;
	} else
		next = strchr(start, quote);
	if (NULL != next) {
		*sz = (size_t)(next - start);
		ep->cur += *sz;
		if (q)
			ep->cur++;
		while (' ' == ep->data[(int)ep->cur] ||
		    '\t' == ep->data[(int)ep->cur] ||
		    '^' == ep->data[(int)ep->cur] ||
		    '~' == ep->data[(int)ep->cur])
			ep->cur++;
	} else {
		if (q)
			mandoc_msg(MANDOCERR_ARG_QUOTE, ep->parse,
			    ep->eqn.ln, ep->eqn.pos, NULL);
		next = strchr(start, '\0');
		*sz = (size_t)(next - start);
		ep->cur += *sz;
	}
	if (q || ! repl)
		return start;
	if (NULL != (def = eqn_def_find(ep, start, *sz))) {
		diff = def->valsz - *sz;
		if (def->valsz > *sz) {
			ep->sz += diff;
			ep->data = mandoc_realloc(ep->data, ep->sz + 1);
			ep->data[ep->sz] = '\0';
			start = &ep->data[(int)ep->rew];
		}
		diff = def->valsz - *sz;
		memmove(start + *sz + diff, start + *sz,
		    (strlen(start) - *sz) + 1);
		memcpy(start, def->val, def->valsz);
		lim++;
		goto again;
	}
	return start;
}
static const char * eqn_next ( struct eqn_node * ep , char quote , size_t * sz , int repl ) { char * start , * next ; int q , diff , lim ; size_t ssz , dummy ; struct eqn_def * def ; if ( NULL == sz ) sz = & dummy ; lim = 0 ; ep -> rew = ep -> cur ; again : if ( lim >= EQN_NEST_MAX ) { mandoc_msg ( MANDOCERR_ROFFLOOP , ep -> parse , ep -> eqn . ln , ep -> eqn . pos , NULL ) ; return NULL ; } ep -> cur = ep -> rew ; start = & ep -> data [ ( int ) ep -> cur ] ; q = 0 ; if ( ' \0 ' == * start ) return NULL ; if ( quote == * start ) { ep -> cur ++ ; q = 1 ; } start = & ep -> data [ ( int ) ep -> cur ] ; if ( ! q ) { if ( ' ' == * start || ' ' == * start ) ssz = 1 ; else ssz = strcspn ( start + 1 , " \" \t " ) + 1 ; next = start + ( int ) ssz ; if ( ' \0 ' == * next ) next = NULL ; } else next = strchr ( start , quote ) ; if ( NULL != next ) { * sz = ( size_t ) ( next - start ) ; ep -> cur += * sz ; if ( q ) ep -> cur ++ ; while ( ' ' == ep -> data [ ( int ) ep -> cur ] || ' \t ' == ep -> data [ ( int ) ep -> cur ] || ' ' == ep -> data [ ( int ) ep -> cur ] || ' ' == ep -> data [ ( int ) ep -> cur ] ) ep -> cur ++ ; } else { if ( q ) mandoc_msg ( MANDOCERR_ARG_QUOTE , ep -> parse , ep -> eqn . ln , ep -> eqn . pos , NULL ) ; next = strchr ( start , ' \0 ' ) ; * sz = ( size_t ) ( next - start ) ; ep -> cur += * sz ; } if ( q || ! repl ) return start ; if ( NULL != ( def = eqn_def_find ( ep , start , * sz ) ) ) { diff = def -> valsz - * sz ; if ( def -> valsz > * sz ) { ep -> sz += diff ; ep -> data = mandoc_realloc ( ep -> data , ep -> sz + 1 ) ; ep -> data [ ep -> sz ] = ' \0 ' ; start = & ep -> data [ ( int ) ep -> rew ] ; } diff = def -> valsz - * sz ; memmove ( start + * sz + diff , start + * sz , ( strlen ( start ) - * sz ) + 1 ) ; memcpy ( start , def -> val , def -> valsz ) ; lim ++ ; goto again ; } return start ; }
Get the next token from the input stream using the given quote
 character.
Get the next token from the input stream using the given quote character .
/* Prevent self-definitions. */ /* Quotes aren't expanded for values. */