/* config.l  
 * grammar for lex scanner
 * gideon le grange, 1994
 * scanner to scan radio.conf config file 
 * some say this is overkill, but i think the extendability
 * makes it worth the trouble 
 * 1994-11-21 : done
 */
 
letter  [A-Za-z]
filename [A-Za-z0-9./]
digit   [0-9] 
ws      [ \t]+
name    {letter}{letter}{letter}{letter}
freq    {digit}+
dev	{filename}+

%{ 
/* code to parse comes here */

#include <string.h> 
#include <stdlib.h>
#include <ctype.h>
#include "astuner.h"

char*   delims=" \t";
char*  token;

int     num_lines=1;

extern int currstation;
extern float currstationf;

void Station(char* text)
{
  char *name;
  int freq;
  int count;

  token=(char*)strtok(text,delims);  /* swallow "station" */
  name=(char*)strtok(NULL,delims);   /* get station name */
  for(count = 0; count < strlen(name); count++)
  {
  	name[count] = toupper(name[count]);
  }
  sscanf((char*)strtok(NULL,delims),"%d",&freq);   /* get station frequency */
  if (used < NUMSTATIONS)
  {
      strncpy(stations[used].name, name, 4);
      if ((freq < 87900) || (freq > 107900))
      {
          printf("%s : Invalid frequency in configuration file line %d\n",myname,num_lines);
	  printf("freq: %f\n", freq);
          exit(-1);
      }
      stations[used].freq=freq;
      used++;
  }
}

void DefaultF(char* text)
{
  int freq;
  
  token=(char*)strtok(text,delims);
  sscanf((char*)strtok(NULL,delims),"%d",&freq);
  currstation=freq;
  currstationf = currstation;
  currstationf = currstationf / 1000;
  if ((currstationf <= RADIO_MINFREQ) || (currstationf >= RADIO_MAXFREQ))
  {
      printf("%s : Invalid default frequency in configuration file line %d\n",myname,num_lines);
      exit(-1);
  }
}

void Device(char* text)
{
  char *dev;
  
  token=(char*)strtok(text,delims);  /* swallow "device" */
  dev=(char*)strtok(NULL,delims);
  strcpy(device, dev);
}
%}
/* now the rules */
%%

station{ws}{name}{ws}{freq}        Station(yytext);

default{ws}{freq}                  DefaultF(yytext);

device{ws}{dev}                   Device(yytext);

[#\;\!][^\n]*$

\n                                 num_lines++;

{ws}

.                                  { 
                                     printf("%s : Error in config file line %d",myname,num_lines);
				   }

%%

