
%{

#include <strings.h>

extern char *Command;		// big string defined in the parser.
extern int   Command_Size;	// #chars allocated for Command

extern char *newString(char *old);

extern "C" {
        /**** these may have to be defined depending on the version of flex */
	void *yy_flex_alloc ( int i )  
				{ return (void*) malloc(i); }
	void *yy_flex_realloc ( void *ptr, int size )
				{ return (void*) realloc(ptr, size); }
	void yy_flex_free ( void *ptr )
				{ free(ptr); }
	int yywrap ( void )	
				{ return 1; }
	void yy_strcpy ( char *s1, const char *s2 )
				{ strcpy(s1, s2); }
}


/* parse related stuff */
int line_count;			// initialized in parseGraph.cc

 void count_chars(char *str) 
  {
    while(*str)
      {
        if (*str == '\n') line_count ++;
        str++;
      }
  }

void yyerror(char *str)
{
  fprintf(stderr," line %d : %s\n",line_count,str);
  fflush(stderr);
}


/* allow for bigger lexemes */
#undef  YYLMAX
#define YYLMAX 16*1024

void add_to_command(char *str)
{
	if (!str) return;
	if (strlen(Command) + strlen(str) > Command_Size) {
		yyerror("command too big");
		exit(1);
	}
	strcat(Command, str);
}

#define EMIT(result) { count_chars(yytext); \
		       return(result); }

#define DISCARD      { count_chars(yytext); }


char *special(char c)
{
	char tmp[4];
	strcpy(tmp, "\\");
	tmp[2] = c;
	tmp[3] = '\0';
	return newString(tmp);
}

%}

%%

[&<>_^#%]			EMIT(SPECIAL);
.
