xref: /plan9-contrib/sys/src/cmd/db/input.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 /*
2  *
3  *	debugger
4  *
5  */
6 
7 #include "defs.h"
8 #include "fns.h"
9 
10 char	line[LINSIZ];
11 extern	int	infile;
12 char	*lp;
13 char	peekc,lastc = EOR;
14 int	eof;
15 
16 /* input routines */
17 
18 eol(int c)
19 {
20 	return(c==EOR || c==';');
21 }
22 
23 int
24 rdc(void)
25 {
26 	do {
27 		readchar();
28 	} while (lastc==SPC || lastc==TB);
29 	return(lastc);
30 }
31 
32 void
33 reread(void)
34 {
35 	peekc = lastc;
36 }
37 
38 void
39 clrinp(void)
40 {
41 	flush();
42 	lp = 0;
43 	peekc = 0;
44 }
45 
46 int
47 readchar(void)
48 {
49 	char *p;
50 	if (eof)
51 		lastc=0;
52 	else if (peekc) {
53 		lastc = peekc;
54 		peekc = 0;
55 	}
56 	else {
57 		if (lp==0) {
58 			for (p = line; p < &line[LINSIZ-1]; p++) {
59 				eof = read(infile, p, 1) <= 0;
60 				if (mkfault) {
61 					eof = 0;
62 					error(0);
63 				}
64 				if (eof) {
65 					p--;
66 					break;
67 				}
68 				if (*p == EOR) {
69 					if (p <= line)
70 						break;
71 					if (p[-1] != '\\')
72 						break;
73 					p -= 2;
74 				}
75 			}
76 			p[1] = 0;
77 			lp = line;
78 		}
79 		if ((lastc = *lp) != 0)
80 			lp++;
81 	}
82 	return(lastc);
83 }
84 
85 nextchar(void)
86 {
87 	if (eol(rdc())) {
88 		reread();
89 		return(0);
90 	}
91 	return(lastc);
92 }
93 
94 quotchar(void)
95 {
96 	if (readchar()=='\\')
97 		return(readchar());
98 	else if (lastc=='\'')
99 		return(0);
100 	else
101 		return(lastc);
102 }
103 
104 void
105 getformat(char *deformat)
106 {
107 	char *fptr;
108 	BOOL	quote;
109 
110 	fptr=deformat;
111 	quote=FALSE;
112 	while ((quote ? readchar()!=EOR : !eol(readchar())))
113 		if ((*fptr++ = lastc)=='"')
114 			quote = ~quote;
115 	lp--;
116 	if (fptr!=deformat)
117 		*fptr = '\0';
118 }
119 
120 /*
121  *	check if the input line if of the form:
122  *		<filename>:<digits><verb> ...
123  *
124  *	we handle this case specially because we have to look ahead
125  *	at the token after the colon to decide if it is a file reference
126  *	or a colon-command with a symbol name prefix.
127  */
128 
129 int
130 isfileref(void)
131 {
132 	char *cp;
133 
134 	for (cp = lp-1; *cp && !strchr(CMD_VERBS, *cp); cp++)
135 		if (*cp == '\\' && cp[1])	/* escape next char */
136 			cp++;
137 	if (*cp && cp > lp-1) {
138 		while (*cp == ' ' || *cp == '\t')
139 			cp++;
140 		if (*cp++ == ':') {
141 			while (*cp == ' ' || *cp == '\t')
142 				cp++;
143 			if (isdigit(*cp))
144 				return 1;
145 		}
146 	}
147 	return 0;
148 }
149