xref: /plan9/sys/src/cmd/db/input.c (revision ff8c3af2f44d95267f67219afa20ba82ff6cf7e4)
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 
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 readrune(int fd, Rune *r)
48 {
49 	char buf[UTFmax];
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 	chartorune(r, buf);
56 	return 1;
57 }
58 
59 int
60 readchar(void)
61 {
62 	Rune *p;
63 
64 	if (eof)
65 		lastc=0;
66 	else if (peekc) {
67 		lastc = peekc;
68 		peekc = 0;
69 	}
70 	else {
71 		if (lp==0) {
72 			for (p = line; p < &line[LINSIZ-1]; p++) {
73 				eof = readrune(infile, p) <= 0;
74 				if (mkfault) {
75 					eof = 0;
76 					error(0);
77 				}
78 				if (eof) {
79 					p--;
80 					break;
81 				}
82 				if (*p == EOR) {
83 					if (p <= line)
84 						break;
85 					if (p[-1] != '\\')
86 						break;
87 					p -= 2;
88 				}
89 			}
90 			p[1] = 0;
91 			lp = line;
92 		}
93 		if ((lastc = *lp) != 0)
94 			lp++;
95 	}
96 	return(lastc);
97 }
98 
99 nextchar(void)
100 {
101 	if (eol(rdc())) {
102 		reread();
103 		return(0);
104 	}
105 	return(lastc);
106 }
107 
108 quotchar(void)
109 {
110 	if (readchar()=='\\')
111 		return(readchar());
112 	else if (lastc=='\'')
113 		return(0);
114 	else
115 		return(lastc);
116 }
117 
118 void
119 getformat(char *deformat)
120 {
121 	char *fptr;
122 	BOOL	quote;
123 	Rune r;
124 
125 	fptr=deformat;
126 	quote=FALSE;
127 	while ((quote ? readchar()!=EOR : !eol(readchar()))){
128 		r = lastc;
129 		fptr += runetochar(fptr, &r);
130 		if (lastc == '"')
131 			quote = ~quote;
132 	}
133 	lp--;
134 	if (fptr!=deformat)
135 		*fptr = '\0';
136 }
137 
138 /*
139  *	check if the input line if of the form:
140  *		<filename>:<digits><verb> ...
141  *
142  *	we handle this case specially because we have to look ahead
143  *	at the token after the colon to decide if it is a file reference
144  *	or a colon-command with a symbol name prefix.
145  */
146 
147 int
148 isfileref(void)
149 {
150 	Rune *cp;
151 
152 	for (cp = lp-1; *cp && !strchr(CMD_VERBS, *cp); cp++)
153 		if (*cp == '\\' && cp[1])	/* escape next char */
154 			cp++;
155 	if (*cp && cp > lp-1) {
156 		while (*cp == ' ' || *cp == '\t')
157 			cp++;
158 		if (*cp++ == ':') {
159 			while (*cp == ' ' || *cp == '\t')
160 				cp++;
161 			if (isdigit(*cp))
162 				return 1;
163 		}
164 	}
165 	return 0;
166 }
167