[comp.unix.questions] Lex and redefining the i/o macros

lampshir@airgun.wg.waii.com (gregory b. lampshir) (07/11/90)

To someone who knows lex.....,

I need to rewrite the input(), output(), and unput() macros that lex
normally supplies so I can accept a binary file.  I will strip out
the funny control characters found in the binary file so I am not worried
about any recognition problems.  I am, however, having trouble rewriting the
macros into functions.

What I have know is this ....

int filePointerIn;

main()
{
	filePointerIn=open(argv[1],O_RDONLY,0);
	while(1)
		yylex();
} 

char input()
{
	int pos;
	char *ch;  
	
	pos=read(filePointerIn,ch,1);
     	/* Some code to strip the funny characters you get from reading
	   binary files a character at a time.  Yes there are ints	
	   and other misc. chars in the file, but the text is o.k.	
	   somewhere inside there.
	*/	
 	if(pos==0) return(0); /* needed by lex to signal eof */
	else return(*ch);
}

void unput(c)
char c;
{
	lseek(filePointerIn,-1,L_INCR);
}

Can any of you lex wizards help me with this one?  What am I doing wrong?



gregory lampshire    | Searching for the computer which can
western geophysical  | do it all.....

ronald@atcmp.nl (Ronald Pikkert) (07/16/90)

From article <859@airgun.wg.waii.com>, by lampshir@airgun.wg.waii.com (gregory b. lampshir):


<> char input()
<> {
<> 	int pos;
<> 	char *ch;  
<> 	
<> 	pos=read(filePointerIn,ch,1);
<>  	if(pos==0) return(0); /* needed by lex to signal eof */
<> 	else return(*ch);
<> }
<> 
<> Can any of you lex wizards help me with this one?  What am I doing wrong?

How about initialising the pointer ch? Or just write something like:

 	char ch;  
 	if (read(filePointerIn,&ch,1)!=1) return(0);
  	return(ch);

By the way, how about not using a system call for every single character 
that is to be read?

-
Ronald Pikkert                 E-mail: ronald@atcmp.nl
@ AT Computing b.v.            Tel:    080 - 566880
Toernooiveld
6525 ED  Nijmegen