/* xconfig.l  , part of xradiotrack
 *
 * renamed and adapted from the original config.l by Gideon le Grange
 * as used for his RadioTrack program. This should make
 * backwards radiotrack compatible with his /etc/radio.conf file I hope.
 * Note that this version allows a few more chars, like '-'
 *
 * 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
 *
 * F.Brinkman Oct.18 1995: changed error handling to X variety 
 *                         calling have_error in init
 *			   Allow '-' in station strings
 */
 
letter  [A-Za-z*-9]
digit   [0-9] 
ws      [ \t]+
name    {letter}+
freq    {digit}+"."{digit}+
port    0x{letter}+

%{ 
/* code to parse comes here */

#include <string.h> 
#include <stdlib.h>
#include "config.h"

extern have_error(int);

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

int     num_lines=1;

void Station(char* text)
{
  char*  name;
  float  freq;

  token=(char*)strtok(text,delims);  /* swallow "station" */
  name=(char*)strtok(NULL,delims);   /* get station name */
  sscanf((char*)strtok(NULL,delims),"%f",&freq);   /* get station frequency */
  if (used<NumStations)
  {
      strncpy(stations[used].name,name,NameLen);
      if ((freq<MinFreq) || (freq>MaxFreq)) { have_error(4); }
      stations[used].freq=freq;
      used++;
  }
}

void DefaultN(char* text)
{
  int i;
  int found=0;
  char* name;
  char *dummy;
 
  token=(char*)strtok(text,delims);
  name=(char*)strtok(NULL,delims);

  for (i=0; i<10; ++i)
  {
     if (strcmp(name,stations[i].name)==0)
     {
      dummy=strcpy(defaultstation.name,stations[i].name);
      defaultstation.freq=stations[i].freq;
      found=1;
      break;
     }
  }
  if (found==0) { have_error(5); }
}

void Port(char* text)
{
  unsigned int port;
  
  token=(char*)strtok(text,delims);  /* swallow "port" */
  sscanf((char*)strtok(NULL,delims),"%x",&port);   /* get port number */
  RadioBase=port;
  if ((RadioBase!=0x30c) && (RadioBase!=0x20c)) { have_error(2); }
}
%}
/* now the rules */
%%

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

default{ws}{name}                  DefaultN(yytext);

port{ws}{port}                     Port(yytext);

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

\n                                 num_lines++;

{ws}

.                                  have_error(6); 

%%

