plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

tm.c (1122B)


      1 /* tm.c: split numerical fields */
      2 # include "t.h"
      3 
      4 char	*
      5 maknew(char *str)
      6 {
      7 				/* make two numerical fields */
      8 	int	c;
      9 	char	*p, *q, *ba, *dpoint;
     10 
     11 	p = str;
     12 	for (ba = 0; c = *str; str++)
     13 		if (c == '\\' && *(str + 1) == '&')
     14 			ba = str;
     15 	str = p;
     16 	if (ba == 0) {
     17 		for (dpoint = 0; *str; str++) {
     18 			if (*str == '.' && !ineqn(str, p) &&
     19 			    (str > p && digit(*(str - 1)) ||
     20 			    digit(*(str + 1))))
     21 				dpoint = str;
     22 		}
     23 		if (dpoint == 0)
     24 			for (; str > p; str--) {
     25 				if (digit( *(str - 1) ) && !ineqn(str, p))
     26 					break;
     27 			}
     28 		if (!dpoint && p == str) /* not numerical, don't split */
     29 			return(0);
     30 		if (dpoint)
     31 			str = dpoint;
     32 	} else
     33 		str = ba;
     34 	p = str;
     35 	if (exstore == 0 || exstore > exlim) {
     36 		exstore = exspace = chspace();
     37 		exlim = exstore + MAXCHS;
     38 	}
     39 	q = exstore;
     40 	while (*exstore++ = *str++)
     41 		;
     42 	*p = 0;
     43 	return(q);
     44 }
     45 
     46 
     47 int
     48 ineqn (char *s, char *p)
     49 {
     50 				/* true if s is in a eqn within p */
     51 	int	ineq = 0, c;
     52 
     53 	while (c = *p) {
     54 		if (s == p)
     55 			return(ineq);
     56 		p++;
     57 		if ((ineq == 0) && (c == delim1))
     58 			ineq = 1;
     59 		else if ((ineq == 1) && (c == delim2))
     60 			ineq = 0;
     61 	}
     62 	return(0);
     63 }