xref: /csrg-svn/old/sdb/decode.c (revision 1329)
1*1329Sbill static	char sccsid[] = "@(#)decode.c 4.1 10/09/80";
2*1329Sbill #include "head.h"
3*1329Sbill 
4*1329Sbill /* decode() - read a line from standard input and decode it */
5*1329Sbill 
6*1329Sbill decode(p)
7*1329Sbill char *p; {
8*1329Sbill 	register char c, *q;
9*1329Sbill 	register int diff;
10*1329Sbill 	integ = scallf = reflag = colonflag = ncolonflag = percentflag = 0;
11*1329Sbill 	proc[0] = cmd = args[0] = var[0] = '\0';
12*1329Sbill 	argsp = args;
13*1329Sbill 
14*1329Sbill 	if (eqany(*p, "/?")) {	/* regular expression */
15*1329Sbill 		c = *p;
16*1329Sbill 		redir = (c == '/');
17*1329Sbill 		reflag = 1;
18*1329Sbill 		p++;
19*1329Sbill 		if (*p == '\n' || *p == c) return(0);
20*1329Sbill 		q = re;
21*1329Sbill 		while(*p != c && *p != '\n') *q++ = *p++;
22*1329Sbill 		*q = '\0';
23*1329Sbill 		return(0);
24*1329Sbill 	}
25*1329Sbill 
26*1329Sbill 	if (*p == '!') { /* shell escape */
27*1329Sbill 		for (q = p; *q != '\n'; q++) ;
28*1329Sbill 		*q = '\0';
29*1329Sbill 		system(p+1);
30*1329Sbill 		return(0);
31*1329Sbill 	}
32*1329Sbill 
33*1329Sbill 	if (*p == '\n') {
34*1329Sbill 		cmd = '\n';
35*1329Sbill 		return(0);
36*1329Sbill 	}
37*1329Sbill 
38*1329Sbill 	if (*p == ':') {
39*1329Sbill 		colonflag++;
40*1329Sbill 	}
41*1329Sbill 
42*1329Sbill 	while (*p != '\n') {	/* decode item by item */
43*1329Sbill 
44*1329Sbill 		if (number(*p)) {	/* decimal number */
45*1329Sbill 			if (integ) {
46*1329Sbill 				error("Too many numbers");
47*1329Sbill 				return(1);
48*1329Sbill 			}
49*1329Sbill 			integ = readint(&p);
50*1329Sbill 			if (*p == ':') {
51*1329Sbill 				ncolonflag++;
52*1329Sbill 				p++;
53*1329Sbill 			}
54*1329Sbill 			continue;
55*1329Sbill 		}
56*1329Sbill 
57*1329Sbill 		if (varchar(*p) || eqany(*p, COMMANDS)) {
58*1329Sbill 					/* proc, variable or command */
59*1329Sbill 			if (cmd != '\0') {
60*1329Sbill 				p = cpall(args, p);
61*1329Sbill 				continue;
62*1329Sbill 			}
63*1329Sbill 			q = p;
64*1329Sbill 			while (varchar(*q) || number(*q) || eqany(*q,COMMANDS))
65*1329Sbill 				q++;
66*1329Sbill 			if (*q == '(') {	/* procedure call */
67*1329Sbill 				if (proc[0] != '\0') {
68*1329Sbill 					error("Too many procedure calls");
69*1329Sbill 					return(1);
70*1329Sbill 				}
71*1329Sbill 				scallf = 1;
72*1329Sbill 				p = cpname(proc, p);
73*1329Sbill 				p = cpall(args, p);
74*1329Sbill 				continue;
75*1329Sbill 			}
76*1329Sbill 			if (*q == ':') {	/* procedure name */
77*1329Sbill 				colonflag++;
78*1329Sbill 				p = cpname(proc, p);
79*1329Sbill 				continue;
80*1329Sbill 			}
81*1329Sbill 			if (*q == '$') {	/* variable name */
82*1329Sbill 				p = cpname(var, p);
83*1329Sbill 				continue;
84*1329Sbill 			}
85*1329Sbill 			if (((q-p == 1 && eqany(*p,COMMANDS) &&
86*1329Sbill 				(proc[0]=='\0' || eqany(*p, "abcd"))) ||
87*1329Sbill 				(integ && eqany(*p,COMMANDS)) ||
88*1329Sbill 				 eqany(*p, "+-?"))
89*1329Sbill 				&& !(*p=='-' && *(p+1) == '>'))
90*1329Sbill 							{  /* command */
91*1329Sbill 				cmd = *p++;
92*1329Sbill 				if (eqany(cmd, "Macers")) {
93*1329Sbill 					while(*p == ' ')
94*1329Sbill 						p++;
95*1329Sbill 					p = cpall(args, p);
96*1329Sbill 				}
97*1329Sbill 				continue;
98*1329Sbill 			}
99*1329Sbill 			/* otherwise, its a variable */
100*1329Sbill 			if (var[0] != '\0') {
101*1329Sbill 				error("Too many variable names");
102*1329Sbill 				return(1);
103*1329Sbill 			}
104*1329Sbill 			p = cpname(var, p);
105*1329Sbill 			if (*p == '%') {
106*1329Sbill 				percentflag++;
107*1329Sbill 				p++;
108*1329Sbill 			}
109*1329Sbill 			if (eqstr(var, ".?")) {
110*1329Sbill 				var[1] = '\0';
111*1329Sbill 				cmd = '?';
112*1329Sbill 			}
113*1329Sbill 			if (*p == '\n') {
114*1329Sbill 				cmd = '/';
115*1329Sbill 				continue;
116*1329Sbill 			}
117*1329Sbill 			if (cmd == '\0') cmd = *p ? *p : '/';
118*1329Sbill 			p++;
119*1329Sbill 			p = cpall(args,p);
120*1329Sbill 			continue;
121*1329Sbill 		}
122*1329Sbill 		p++;	/* otherwise ignore p */
123*1329Sbill 	}
124*1329Sbill 	return(0);
125*1329Sbill }
126