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