xref: /csrg-svn/old/sdb/decode.c (revision 7773)
1*7773Srrh static	char sccsid[] = "@(#)decode.c 4.2 08/17/82";
21329Sbill #include "head.h"
31329Sbill 
41329Sbill /* decode() - read a line from standard input and decode it */
51329Sbill 
decode(p)61329Sbill decode(p)
71329Sbill char *p; {
81329Sbill 	register char c, *q;
91329Sbill 	register int diff;
101329Sbill 	integ = scallf = reflag = colonflag = ncolonflag = percentflag = 0;
111329Sbill 	proc[0] = cmd = args[0] = var[0] = '\0';
121329Sbill 	argsp = args;
131329Sbill 
141329Sbill 	if (eqany(*p, "/?")) {	/* regular expression */
151329Sbill 		c = *p;
161329Sbill 		redir = (c == '/');
171329Sbill 		reflag = 1;
181329Sbill 		p++;
191329Sbill 		if (*p == '\n' || *p == c) return(0);
201329Sbill 		q = re;
211329Sbill 		while(*p != c && *p != '\n') *q++ = *p++;
221329Sbill 		*q = '\0';
231329Sbill 		return(0);
241329Sbill 	}
251329Sbill 
261329Sbill 	if (*p == '!') { /* shell escape */
271329Sbill 		for (q = p; *q != '\n'; q++) ;
281329Sbill 		*q = '\0';
291329Sbill 		system(p+1);
301329Sbill 		return(0);
311329Sbill 	}
321329Sbill 
331329Sbill 	if (*p == '\n') {
341329Sbill 		cmd = '\n';
351329Sbill 		return(0);
361329Sbill 	}
371329Sbill 
381329Sbill 	if (*p == ':') {
391329Sbill 		colonflag++;
401329Sbill 	}
411329Sbill 
421329Sbill 	while (*p != '\n') {	/* decode item by item */
431329Sbill 
441329Sbill 		if (number(*p)) {	/* decimal number */
451329Sbill 			if (integ) {
461329Sbill 				error("Too many numbers");
471329Sbill 				return(1);
481329Sbill 			}
491329Sbill 			integ = readint(&p);
501329Sbill 			if (*p == ':') {
511329Sbill 				ncolonflag++;
521329Sbill 				p++;
531329Sbill 			}
541329Sbill 			continue;
551329Sbill 		}
561329Sbill 
571329Sbill 		if (varchar(*p) || eqany(*p, COMMANDS)) {
581329Sbill 					/* proc, variable or command */
591329Sbill 			if (cmd != '\0') {
601329Sbill 				p = cpall(args, p);
611329Sbill 				continue;
621329Sbill 			}
631329Sbill 			q = p;
641329Sbill 			while (varchar(*q) || number(*q) || eqany(*q,COMMANDS))
651329Sbill 				q++;
661329Sbill 			if (*q == '(') {	/* procedure call */
671329Sbill 				if (proc[0] != '\0') {
681329Sbill 					error("Too many procedure calls");
691329Sbill 					return(1);
701329Sbill 				}
711329Sbill 				scallf = 1;
721329Sbill 				p = cpname(proc, p);
731329Sbill 				p = cpall(args, p);
741329Sbill 				continue;
751329Sbill 			}
761329Sbill 			if (*q == ':') {	/* procedure name */
771329Sbill 				colonflag++;
781329Sbill 				p = cpname(proc, p);
791329Sbill 				continue;
801329Sbill 			}
811329Sbill 			if (*q == '$') {	/* variable name */
821329Sbill 				p = cpname(var, p);
831329Sbill 				continue;
841329Sbill 			}
851329Sbill 			if (((q-p == 1 && eqany(*p,COMMANDS) &&
861329Sbill 				(proc[0]=='\0' || eqany(*p, "abcd"))) ||
871329Sbill 				(integ && eqany(*p,COMMANDS)) ||
881329Sbill 				 eqany(*p, "+-?"))
891329Sbill 				&& !(*p=='-' && *(p+1) == '>'))
901329Sbill 							{  /* command */
911329Sbill 				cmd = *p++;
921329Sbill 				if (eqany(cmd, "Macers")) {
931329Sbill 					while(*p == ' ')
941329Sbill 						p++;
951329Sbill 					p = cpall(args, p);
961329Sbill 				}
971329Sbill 				continue;
981329Sbill 			}
991329Sbill 			/* otherwise, its a variable */
1001329Sbill 			if (var[0] != '\0') {
1011329Sbill 				error("Too many variable names");
1021329Sbill 				return(1);
1031329Sbill 			}
1041329Sbill 			p = cpname(var, p);
1051329Sbill 			if (*p == '%') {
1061329Sbill 				percentflag++;
1071329Sbill 				p++;
1081329Sbill 			}
1091329Sbill 			if (eqstr(var, ".?")) {
1101329Sbill 				var[1] = '\0';
1111329Sbill 				cmd = '?';
1121329Sbill 			}
1131329Sbill 			if (*p == '\n') {
1141329Sbill 				cmd = '/';
1151329Sbill 				continue;
1161329Sbill 			}
1171329Sbill 			if (cmd == '\0') cmd = *p ? *p : '/';
1181329Sbill 			p++;
1191329Sbill 			p = cpall(args,p);
1201329Sbill 			continue;
1211329Sbill 		}
1221329Sbill 		p++;	/* otherwise ignore p */
1231329Sbill 	}
1241329Sbill 	return(0);
1251329Sbill }
126