@@ -170,31 +170,15 @@ static int
170170tok_nextc (tok )
171171 register struct tok_state * tok ;
172172{
173- if (tok -> done != E_OK )
174- return EOF ;
175-
176173 for (;;) {
177- if (tok -> cur < tok -> inp )
178- return * tok -> cur ++ ;
174+ if (tok -> cur != tok -> inp )
175+ return * tok -> cur ++ ; /* Fast path */
176+ if (tok -> done != E_OK )
177+ return EOF ;
179178 if (tok -> fp == NULL ) {
180179 tok -> done = E_EOF ;
181180 return EOF ;
182181 }
183- if (tok -> inp > tok -> buf && tok -> inp [-1 ] == '\n' )
184- tok -> inp = tok -> buf ;
185- if (tok -> inp == tok -> end ) {
186- int n = tok -> end - tok -> buf ;
187- char * new = tok -> buf ;
188- RESIZE (new , char , n + n );
189- if (new == NULL ) {
190- fprintf (stderr , "tokenizer out of mem\n" );
191- tok -> done = E_NOMEM ;
192- return EOF ;
193- }
194- tok -> buf = new ;
195- tok -> inp = tok -> buf + n ;
196- tok -> end = tok -> inp + n ;
197- }
198182#ifdef USE_READLINE
199183 if (tok -> prompt != NULL ) {
200184 extern char * readline PROTO ((char * prompt ));
@@ -211,46 +195,71 @@ tok_nextc(tok)
211195 (void ) intrcheck (); /* Clear pending interrupt */
212196 if (tok -> nextprompt != NULL )
213197 tok -> prompt = tok -> nextprompt ;
214- /* XXX different semantics w/o readline()! */
215198 if (tok -> buf == NULL ) {
216199 tok -> done = E_EOF ;
217200 }
218201 else {
219- unsigned int n = strlen (tok -> buf );
220- if (n > 0 )
202+ tok -> end = strchr (tok -> buf , '\0' );
203+ if (tok -> end > tok -> buf )
221204 add_history (tok -> buf );
222- /* Append the '\n' that readline()
223- doesn't give us, for the tokenizer... */
224- tok -> buf = realloc (tok -> buf , n + 2 );
225- if (tok -> buf == NULL )
226- tok -> done = E_NOMEM ;
227- else {
228- tok -> end = tok -> buf + n ;
229- * tok -> end ++ = '\n' ;
230- * tok -> end = '\0' ;
231- tok -> inp = tok -> end ;
232- tok -> cur = tok -> buf ;
233- }
205+ /* Replace trailing '\n' by '\0'
206+ (we don't need a '\0', but the
207+ tokenizer wants a '\n'...) */
208+ * tok -> end ++ = '\n' ;
209+ tok -> inp = tok -> end ;
210+ tok -> cur = tok -> buf ;
234211 }
235212 }
236213 else
237214#endif
238215 {
239- tok -> cur = tok -> inp ;
240- if (tok -> prompt != NULL && tok -> inp == tok -> buf ) {
216+ if (tok -> prompt != NULL ) {
241217 fprintf (stderr , "%s" , tok -> prompt );
242- tok -> prompt = tok -> nextprompt ;
218+ if (tok -> nextprompt != NULL )
219+ tok -> prompt = tok -> nextprompt ;
243220 }
244- tok -> done = fgets_intr (tok -> inp ,
245- (int )(tok -> end - tok -> inp ), tok -> fp );
221+ if (tok -> buf == NULL ) {
222+ tok -> buf = NEW (char , BUFSIZ );
223+ if (tok -> buf == NULL ) {
224+ tok -> done = E_NOMEM ;
225+ return EOF ;
226+ }
227+ tok -> end = tok -> buf + BUFSIZ ;
228+ }
229+ tok -> done = fgets_intr (tok -> buf ,
230+ (int )(tok -> end - tok -> buf ), tok -> fp );
231+ tok -> inp = strchr (tok -> buf , '\0' );
232+ /* Read until '\n' or EOF */
233+ while (tok -> inp + 1 == tok -> end && tok -> inp [-1 ]!= '\n' ) {
234+ int curvalid = tok -> inp - tok -> buf ;
235+ int cursize = tok -> end - tok -> buf ;
236+ int newsize = cursize + BUFSIZ ;
237+ char * newbuf = tok -> buf ;
238+ RESIZE (newbuf , char , newsize );
239+ if (newbuf == NULL ) {
240+ tok -> done = E_NOMEM ;
241+ tok -> cur = tok -> inp ;
242+ return EOF ;
243+ }
244+ tok -> buf = newbuf ;
245+ tok -> inp = tok -> buf + curvalid ;
246+ tok -> end = tok -> buf + newsize ;
247+ if (fgets_intr (tok -> inp ,
248+ (int )(tok -> end - tok -> inp ),
249+ tok -> fp ) != E_OK )
250+ break ;
251+ tok -> inp = strchr (tok -> inp , '\0' );
252+ }
253+ tok -> cur = tok -> buf ;
246254 }
247255 if (tok -> done != E_OK ) {
248256 if (tok -> prompt != NULL )
249257 fprintf (stderr , "\n" );
258+ tok -> cur = tok -> inp ;
250259 return EOF ;
251260 }
252- tok -> inp = strchr (tok -> inp , '\0' );
253261 }
262+ /*NOTREACHED*/
254263}
255264
256265
@@ -390,6 +399,7 @@ tok_get(tok, p_start, p_end)
390399 if (tok -> indent + 1 >= MAXINDENT ) {
391400 fprintf (stderr , "excessive indent\n" );
392401 tok -> done = E_TOKEN ;
402+ tok -> cur = tok -> inp ;
393403 return ERRORTOKEN ;
394404 }
395405 tok -> pendin ++ ;
@@ -405,6 +415,7 @@ tok_get(tok, p_start, p_end)
405415 if (col != tok -> indstack [tok -> indent ]) {
406416 fprintf (stderr , "inconsistent dedent\n" );
407417 tok -> done = E_TOKEN ;
418+ tok -> cur = tok -> inp ;
408419 return ERRORTOKEN ;
409420 }
410421 }
@@ -558,13 +569,15 @@ tok_get(tok, p_start, p_end)
558569 c = tok_nextc (tok );
559570 if (c == '\n' || c == EOF ) {
560571 tok -> done = E_TOKEN ;
572+ tok -> cur = tok -> inp ;
561573 return ERRORTOKEN ;
562574 }
563575 if (c == '\\' ) {
564576 c = tok_nextc (tok );
565577 * p_end = tok -> cur ;
566578 if (c == '\n' || c == EOF ) {
567579 tok -> done = E_TOKEN ;
580+ tok -> cur = tok -> inp ;
568581 return ERRORTOKEN ;
569582 }
570583 continue ;
@@ -581,6 +594,7 @@ tok_get(tok, p_start, p_end)
581594 c = tok_nextc (tok );
582595 if (c != '\n' ) {
583596 tok -> done = E_TOKEN ;
597+ tok -> cur = tok -> inp ;
584598 return ERRORTOKEN ;
585599 }
586600 tok -> lineno ++ ;
0 commit comments