/*  -*- C -*- */
/*
 * Assembler for DOCSIS cable modem configuration files
 *
 * $Id: yaccan.y,v 1.2 2000/07/27 16:18:35 petras Exp $
 * 
 * Copyright (C) 2000  Dietmar Petras  <Dietmar.Petras@ELSA.de>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * History:
 * $Log: yaccan.y,v $
 * Revision 1.2  2000/07/27 16:18:35  petras
 * Bug fix with reallocation of OutputBuffer
 *
 * Revision 1.1  2000/03/01 23:19:38  petras
 * Initial revision.
 *
 */

%token DOWN_FREQ UP_CHAN_ID NETWORK_ACCESS COS MODEM_CAP VENDOR_ID
%token UPGRADE_FILENAME SNMP_WRITE_ACCESS SNMP_MIB_OBJECT CPE_MAC_ADDR BPI
%token MAX_NUM_CPE UPGRADE_SERVER VENDOR_SPECIFIC_INFO
%token CLASS_ID MAX_DOWN_RATE MAX_UP_RATE UP_CHAN_PRIO MIN_UP_RATE
%token MAX_BURST_SIZE COS_BPI_ENABLE CONCATENATION
%token AUTHORIZE_WAIT_TIMEOUT REAUTHORIZE_WAIT_TIMEOUT
%token AUTHORIZATION_GRACE_TIME OPERATIONAL_WAIT_TIMEOUT REKEY_WAIT_TIMEOUT
%token REKEY_WAIT_TIMEOUT TEK_GRACE_TIME AUTHORIZE_REJECT_WAIT_TIMEOUT

%token INTEGER OCTET_STRING NULL_OBJ OBJECT_ID IPADDRESS

%token IS OPEN_BRACKET CLOSE_BRACKET SEMICOLON
%token ON OFF ENABLE DISABLE ALLOW DISALLOW

%token <ulong_val>  NUMBER
%token <byte_val>   BYTE
%token <string_val> STRING IP_ADDR_STRING MAC_ADDR_STRING SNMP_OBJ_ID

%type <ulong_val>   number
%type <string_val>  ip_addr mac_addr pre_snmp_obj_id snmp_obj_id snmp_type_value
%type <string_val>  byte_sequence
%type <bool_val>    enabled_disabled allow_disallow

%pure_parser
%union {
   unsigned long	ulong_val;
   unsigned char	byte_val;
   char			*string_val;
   int			bool_val;
}

%{

/*-----------------------------------------------------------------------*
 * $Log: yaccan.y,v $
 * Revision 1.2  2000/07/27 16:18:35  petras
 * Bug fix with reallocation of OutputBuffer
 *
 * Revision 1.1  2000/03/01 23:19:38  petras
 * Initial revision.
 *
 *-----------------------------------------------------------------------*/

int yylex(YYSTYPE *yylval);
#include "lexan.h"
#define YYDEBUG 1
#define YYERROR_VERBOSE

void yyerror (const char*);

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

%}

%%

input:		commands
	;
commands:	/* empty */
	|	commands group
	|	commands command SEMICOLON
	|	commands error
	;
group:		COS OPEN_BRACKET
		{
		  StartGroup(4);
		}
		cos_commands CLOSE_BRACKET
		{
		  EndGroup(eCMTS_MIC_COS);
		}
	|	MODEM_CAP OPEN_BRACKET
		{
		  StartGroup(5);
		}
		modem_cap_commands CLOSE_BRACKET
		{
		  EndGroup(eCMTS_MIC_NIL);
		}
	|	BPI OPEN_BRACKET
		{
		  StartGroup(17);
		}
		bpi_commands CLOSE_BRACKET
		{
		  EndGroup(eCMTS_MIC_BPI);
		}
	|	VENDOR_SPECIFIC_INFO OPEN_BRACKET
		{
		  StartGroup(43);
		}
		vendor_specific_info_commands CLOSE_BRACKET
		{
		  EndGroup(eCMTS_MIC_VendorSpecificInfo);
		}
	;
command:	DOWN_FREQ IS number
		{
		  unsigned long rem = $3 % 62500;
		  if (rem != 0) {
		    yyerror(NULL);
		    fprintf(
		      stderr, 
		      "down_freq = %lu is not a multiple of 62500 Hz, ",
		      $3
		    );
		    $3 -= rem;
		    fprintf(stderr, "using %lu instead.\n", $3);
		    
		  }
		  WriteByte(1); /* type */
		  WriteByte(4); /* length */
		  WriteNumber($3, 4);
		  ReportCMTS_MIC_Data(eCMTS_MIC_DownFreq, NextWritePosition()-6, 6);
		}
	|	UP_CHAN_ID IS number
		{
		  if ($3 > 0xff) {
		    yyerror(NULL);
		    fprintf(
		      stderr, 
		      "UP_CHAN_ID %lu out of range, valid range is [0..255], ",
		      $3
		    );
		    $3 = 0xff;
		    fprintf(stderr, "using %lu instead.\n", $3);
		  }
		  WriteByte(2); /* type */
		  WriteByte(1); /* length */
		  WriteNumber($3, 1);
		  ReportCMTS_MIC_Data(eCMTS_MIC_UpChanId, NextWritePosition()-3, 3);
		}
	|	NETWORK_ACCESS IS enabled_disabled
		{
		  WriteByte(3); /* type */
		  WriteByte(1); /* length */
		  WriteNumber($3? 1:0, 1);
		  ReportCMTS_MIC_Data(eCMTS_MIC_NetworkAccess, NextWritePosition()-3, 3);
		}
	|	VENDOR_ID IS BYTE BYTE BYTE
		{
		  WriteByte(8); /* type */
		  WriteByte(3); /* length */
		  WriteByte($3); /* byte 1 */
		  WriteByte($4); /* byte 2 */
		  WriteByte($5); /* byte 3 */
		}
	|	UPGRADE_FILENAME IS STRING
		{
		  char *name = $3;
		  int len = strlen(name);
		  if (len>0xff) {
		    yyerror(NULL);
		    fprintf(
		      stderr, 
		      "upgrade_filename too long, shortening to 255 characters\n"
		    );
		    len=0xff;
		  }
		  WriteByte(9); /* type */
		  WriteByte(len); /* length */
		  WriteString(name, len);
		  free(name);
		}
	|	SNMP_WRITE_ACCESS IS STRING allow_disallow
		{
		}
	|	SNMP_MIB_OBJECT snmp_obj_id IS snmp_type_value
		{
		  /* the first char in string snmp_obj_id gives the length */
		  int mib_len_pos;
		  int seq_len_pos;
		  int obj_id_len = (int)$2[0];
		  byte *obj_id = (byte *)$2+1;
		  int value_len = (int)$4[0];
		  byte *value = (byte *)$4+1;
		  

		  WriteByte(11); /* type = SNMP MIB Object */
		  mib_len_pos = NextWritePosition();
		  WriteByte(0); /* dummy for SNMP MIB Object length */

		  WriteByte(0x30); /* type = SEQUENCE */
		  seq_len_pos = NextWritePosition();
		  WriteByte(0); /* dummy for SEQUENCE length */

		  WriteByte(0x06); /* type = OBJECT IDENTIFIER */
		  WriteByte(obj_id_len);
		  WriteString(obj_id, obj_id_len);

		  WriteString(value, value_len);

		  WriteByteAtPosition(seq_len_pos, (byte)(NextWritePosition()-seq_len_pos-1));
		  WriteByteAtPosition(mib_len_pos, (byte)(NextWritePosition()-mib_len_pos-1));
 
		  free($2);
		  free($4);
		}
	|	CPE_MAC_ADDR IS mac_addr
		{
		  WriteByte(14); /* type */
		  WriteByte(6); /* length */
		  WriteString($3, 6);
		  free($3);
		}
	|	MAX_NUM_CPE IS number
		{
		  if ($3 > 0xff) {
		    yyerror(NULL);
		    fprintf(
		      stderr, 
		      "max_num_cpe %lu out of range, valid range is [0..255], ",
		      $3
		    );
		    $3 = 0xff;
		    fprintf(stderr, "using %lu instead.\n", $3);
		  }
		  WriteByte(18); /* type */
		  WriteByte(1); /* length */
		  WriteNumber($3, 1);
		  ReportCMTS_MIC_Data(eCMTS_MIC_MaxNumCpe, NextWritePosition()-3, 3);
		}
	|	UPGRADE_SERVER IS ip_addr
		{
		  WriteByte(21); /* type */
		  WriteByte(4); /* length */
		  WriteString($3, 4);
		  free($3);
		}
	;

cos_commands:	cos_command SEMICOLON
	|	cos_commands cos_command SEMICOLON
	|	cos_commands error
	;
cos_command:	
	|	CLASS_ID IS number
		{
		  if ($3<1 || $3>16) {
		    yyerror(NULL);
		    fprintf(
		      stderr, 
		      "class_id %lu out of range, valid range is [1..16], ",
		      $3
		    );
		    $3 = 16;
		    fprintf(stderr, "using %lu instead.\n", $3);
		  }
		  WriteByte(1); /* type */
		  WriteByte(1); /* length */
		  WriteNumber($3, 1);
		}
	|	MAX_DOWN_RATE IS number
		{
		  WriteByte(2); /* type */
		  WriteByte(4); /* length */
		  WriteNumber($3, 4);
		}
	|	MAX_UP_RATE IS number
		{
		  WriteByte(3); /* type */
		  WriteByte(4); /* length */
		  WriteNumber($3, 4);
		}
	|	UP_CHAN_PRIO IS number
		{
		  if ($3>7) {
		    yyerror(NULL);
		    fprintf(
		      stderr, 
		      "up_chan_prio %lu out of range, valid range is [0..7], ",
		      $3
		    );
		    $3 = 7;
		    fprintf(stderr, "using %lu instead.\n", $3);
		  }
		  WriteByte(4); /* type */
		  WriteByte(1); /* length */
		  WriteNumber($3, 1);
		}
	|	MIN_UP_RATE IS number
		{
		  WriteByte(5); /* type */
		  WriteByte(4); /* length */
		  WriteNumber($3, 4);
		}
	|	MAX_BURST_SIZE IS number
		{
		  if ($3 > 1518) {
		    yyerror(NULL);
		    fprintf(
		      stderr, 
		      "max_burst_size %lu out of range, valid range is [0..1518], ",
		      $3
		    );
		    $3 = 1518;
		    fprintf(stderr, "using %lu instead.\n", $3);
		  }
		  WriteByte(6); /* type */
		  WriteByte(2); /* length */
		  WriteNumber($3, 2);
		}
	|	COS_BPI_ENABLE IS enabled_disabled
		{
		  WriteByte(7); /* type */
		  WriteByte(1); /* length */
		  WriteNumber($3? 1:0, 1);
		}
	;

modem_cap_commands:	modem_cap_command SEMICOLON
	|	modem_cap_commands modem_cap_command SEMICOLON
	|	modem_cap_commands error
	;
modem_cap_command:	
		CONCATENATION IS enabled_disabled
		{
		  WriteByte(1); /* type */
		  WriteByte(1); /* length */
		  WriteNumber($3? 1:0, 1);
		}
	;

bpi_commands:	bpi_command SEMICOLON
	|	bpi_commands bpi_command SEMICOLON
	|	bpi_commands error
	;
bpi_command:	AUTHORIZE_WAIT_TIMEOUT IS number
		{
		  if ($3<1 || $3>30) {
		    yyerror(NULL);
		    fprintf(
		      stderr, 
		      "authorize_wait_timeout %lu out of range, valid range is [1..30], ",
		      $3
		    );
		    $3 = 30;
		    fprintf(stderr, "using %lu instead.\n", $3);
		  }
		  WriteByte(1); /* type */
		  WriteByte(4); /* length */
		  WriteNumber($3, 4);
		}
	|	REAUTHORIZE_WAIT_TIMEOUT IS number
		{
		  if ($3<1 || $3>20) {
		    yyerror(NULL);
		    fprintf(
		      stderr, 
		      "reauthorize_wait_timeout %lu out of range, valid range is [1..20], ",
		      $3
		    );
		    $3 = 20;
		    fprintf(stderr, "using %lu instead.\n", $3);
		  }
		  WriteByte(2); /* type */
		  WriteByte(4); /* length */
		  WriteNumber($3, 4);
		}
	|	AUTHORIZATION_GRACE_TIME IS number
		{
		  if ($3<1 || $3>1800) {
		    yyerror(NULL);
		    fprintf(
		      stderr, 
		      "authorization_grace_time %lu out of range, valid range is [1..1800], ",
		      $3
		    );
		    $3 = 1800;
		    fprintf(stderr, "using %lu instead.\n", $3);
		  }
		  WriteByte(3); /* type */
		  WriteByte(4); /* length */
		  WriteNumber($3, 4);
		}
	|	OPERATIONAL_WAIT_TIMEOUT IS number
		{
		  if ($3<1 || $3>10) {
		    yyerror(NULL);
		    fprintf(
		      stderr, 
		      "operational_wait_timeout %lu out of range, valid range is [1..10], ",
		      $3
		    );
		    $3 = 10;
		    fprintf(stderr, "using %lu instead.\n", $3);
		  }
		  WriteByte(4); /* type */
		  WriteByte(4); /* length */
		  WriteNumber($3, 4);
		}
	|	REKEY_WAIT_TIMEOUT IS number
		{
		  if ($3<1 || $3>10) {
		    yyerror(NULL);
		    fprintf(
		      stderr, 
		      "rekey_wait_timeout %lu out of range, valid range is [1..10], ",
		      $3
		    );
		    $3 = 10;
		    fprintf(stderr, "using %lu instead.\n", $3);
		  }
		  WriteByte(5); /* type */
		  WriteByte(4); /* length */
		  WriteNumber($3, 4);
		}
	|	TEK_GRACE_TIME IS number
		{
		  if ($3<1 || $3>1800) {
		    yyerror(NULL);
		    fprintf(
		      stderr, 
		      "tek_grace_time %lu out of range, valid range is [1..1800], ",
		      $3
		    );
		    $3 = 1800;
		    fprintf(stderr, "using %lu instead.\n", $3);
		  }
		  WriteByte(6); /* type */
		  WriteByte(4); /* length */
		  WriteNumber($3, 4);
		}
	|	AUTHORIZE_REJECT_WAIT_TIMEOUT IS number
		{
		  WriteByte(7); /* type */
		  WriteByte(4); /* length */
		  WriteNumber($3, 4);
		}
	;

vendor_specific_info_commands:	vendor_specific_info_command SEMICOLON
	|	vendor_specific_info_commands vendor_specific_info_command SEMICOLON
	|	vendor_specific_info_commands error
	;
vendor_specific_info_command:	
		VENDOR_ID IS BYTE BYTE BYTE
		{
		  WriteByte(8); /* type */
		  WriteByte(3); /* length */
		  WriteByte($3);
		  WriteByte($4);
		  WriteByte($5);
		}
	;

snmp_obj_id:	pre_snmp_obj_id
		{
		  /* Now I can correct the encoding of the first two sub_obj_ids. */
		  /* The first char in string snmp_obj_id gives the length */
		  int obj_id_len = (int)$1[0];
		  byte *obj_id = (byte *)$1+1;
		  if (obj_id[0] > 2) {
		    yyerror(NULL);
		    fprintf(
		      stderr, 
		      "illegal value %d of first sub-identifier in SNMP Object Id, valid range is [1..3], ",
		      (int)obj_id[0]
		    );
		    obj_id[0] = 1;
		  }
		  if (obj_id[1] > 39) {
		    yyerror(NULL);
		    fprintf(
		      stderr, 
		      "illegal value %d of first sub-identifier in SNMP Object Id, valid range is [1..39], ",
		      (int)obj_id[1]
		    );
		    obj_id[1] = 39;
		  }
		  obj_id[1] = obj_id[0]*40 + obj_id[1];
		  obj_id_len--;
		  /* shift sequence one byte to the left */
		  memmove(obj_id, obj_id+1, obj_id_len);
		  /* write back new length */
		  obj_id[-1] = obj_id_len;
		  $$ = $1;
		}
	;

pre_snmp_obj_id:	SNMP_OBJ_ID
		{
		  /* we return a string with its length being encoded in
		     its first char */
		  /* Correct of the encoding of the first two sub_obj_ids
		     is done by snmp_obj_id */
		  int in_cnt = 0;
		  int out_cnt = 1;
		  char *input = $1;
		  byte *buffer = (byte*)malloc(strlen(input)*3+4);

		  while (1) {
		    unsigned long su_obj_id = 0;
		    char c;

		    /* read the next integer */
		    while (1) {
		      c = input[in_cnt++];
		      if (c=='\0' || c=='.') break;
		      su_obj_id = su_obj_id*10 + c-'0';
		    }

		    /* write it to buffer */
		    if (su_obj_id>0x0fffffff) buffer[out_cnt++] =
						(byte)((su_obj_id>>28 & 0x7f) | 0x80);
		    if (su_obj_id>0x001fffff) buffer[out_cnt++] =
						(byte)((su_obj_id>>21 & 0x7f) | 0x80);
		    if (su_obj_id>0x00003fff) buffer[out_cnt++] =
						(byte)((su_obj_id>>14 & 0x7f) | 0x80);
		    if (su_obj_id>0x0000007f) buffer[out_cnt++] =
						(byte)((su_obj_id>>7  & 0x7f) | 0x80);
		    if (1                   ) buffer[out_cnt++] =
						(byte)((su_obj_id     & 0x7f)       );

		    if (c=='\0') break; 
		    if (c!='.') {
		      yyerror(NULL);
		      fprintf(stderr,"syntax SNMP object identifier `%s'", input);
		      break;
		    }
		  }
		  buffer[0] = out_cnt-1;
		  $$ = buffer;

		  free(input);
		}
	;

snmp_type_value: INTEGER number
		{
		  /* return type is a non-null-terminated string with the
		   * first char giving its length.
		   * This item is only for positiv numbers!!!
		   */
		  unsigned int val = $2;
		  int out_cnt = 1;
		  byte *buffer = (byte*)malloc(7);

		  buffer[out_cnt++] = 0x02; /* type = INTEGER */
		  buffer[out_cnt++] = 0; /* value length will be filled in later */
		  if (val>0x007fffff) buffer[out_cnt++] = (byte)(val>>24 & 0xff);
		  if (val>0x00007fff) buffer[out_cnt++] = (byte)(val>>16 & 0xff);
		  if (val>0x0000007f) buffer[out_cnt++] = (byte)(val>> 8 & 0xff);
		  if (1             ) buffer[out_cnt++] = (byte)(val     & 0xff);
		  buffer[2] = out_cnt-3; /* value length */
		  buffer[0] = out_cnt-1; /* string length */
		  $$ = buffer;
		}
	|	OCTET_STRING STRING
		{
		  int len = strlen($2);
		  byte *buffer = (byte*)malloc(len+3);

		  buffer[0] = len+2; /* string length */
		  buffer[1] = 0x04; /* type = OCTET STRING */
		  buffer[2] = len;  /* value length */
		  memcpy(buffer+3, $2, len);
		  free($2);
		  $$ = buffer;
		}
	|	OCTET_STRING byte_sequence
		{
		  int len = $2[0];
		  byte *buffer = (byte*)malloc(len+3);

		  buffer[0] = len+2; /* string length */
		  buffer[1] = 0x04; /* type = OCTET STRING */
		  buffer[2] = len;  /* value length */
		  memcpy(buffer+3, $2+1, len);
		  free($2);
		  $$ = buffer;
		}
	|	NULL_OBJ
		{
		  byte *buffer = (byte*)malloc(3);
		  buffer[0] = 2;    /* string length */
		  buffer[1] = 0x05; /* type = NULL */
		  buffer[2] = 0;    /* value length */
		  $$ = buffer;
		}
	|	OBJECT_ID snmp_obj_id
		{
		  int len = $2[0];
		  byte *buffer = (byte*)malloc(len+3);

		  buffer[0] = len+2; /* string length */
		  buffer[1] = 0x06; /* type = OBJECT ID */
		  buffer[2] = len;  /* value length */
		  memcpy(buffer+3, $2+1, len);
		  free($2);
		  $$ = buffer;
		}
	|	IPADDRESS ip_addr
		{
		  byte *buffer = (byte*)malloc(7);

		  buffer[0] = 6; /* string length */
		  buffer[1] = 0x40; /* type = IpAddress */
		  buffer[2] = 4;  /* value length */
		  memcpy(buffer+3, $2, 4);
		  free($2);
		  $$ = buffer;
		}
 	|	error
		{
		  byte *buffer = (byte*)malloc(1);
		  buffer[0] = 0;
		  $$ = buffer;
		}		  
	;

enabled_disabled: ON
		{
		  $$ = 1;
		}
	|	OFF
		{
		  $$ = 0;
		}
	|	ENABLE
		{
		  $$ = 1;
		}
	|	DISABLE
		{
		  $$ = 0;
		}
	;

allow_disallow:	ALLOW
		{
		  $$ = 1;
		}
	|	DISALLOW
		{
		  $$ = 0;
		}
	;

number:		NUMBER
	|	BYTE
		{
		  unsigned char c = $1;
		  $$ = (unsigned long)c;
		}
	;

byte_sequence:  BYTE
		{
		  /* The first byte of the sequence gives its length.
		   * The length of the malloc block is always n*8+1
		   * Thus, sequence[0]==5 -> malloc(9)
		   *       sequence[0]==8 -> malloc(9)
		   *       sequence[0]==9 -> malloc(17)
		   *       etc.
		   */
		  byte *sequence = (byte *)malloc(9);
		  sequence[0] = 1;
		  sequence[1] = $1;
		  $$ = sequence;
		}

	|	byte_sequence BYTE
		{
		  byte *sequence = (byte *)$1;
		  int len = sequence[0];
		  if (len%8 == 0) {
		    /* no more free bytes in malloc block, */
		    /* have to extend it by 8 byte */
		    int new_len = len+9;
		    byte *new_seq = (byte *)malloc(new_len);
		    memcpy(new_seq, sequence, len+1);
		    free(sequence);
		    sequence = new_seq;
		  }
		  sequence[0] = len+1;
		  sequence[len+1] = $2;
		  $$ = sequence;
		}
	;

ip_addr:	IP_ADDR_STRING
		{
		  int byte_cnt;
		  int char_cnt = 0;
		  char *ip_addr = (char *)malloc(4);

		  for (byte_cnt=0; byte_cnt<4; byte_cnt++) {
		    int b = 0;
		    while (1) {
		      char c = $1[char_cnt];
		      if (c=='\0' || c=='.') break;
		      b = b*10 + c-'0';
		      char_cnt++;
		    }
		    if (b > 0xff) {
		      int i;
		      yyerror(NULL);
		      fprintf(stderr,"syntax error in IP address string `%s'", $1);
		      /* reset ip_addr */
		      for(i=0; i<4;i++) ip_addr[i] = 0;
		      break;
		    }
		    ip_addr[byte_cnt] = (char)b;
		    char_cnt++; /* jump over '.' */
		  }
		  $$ = ip_addr;
		  free($1);
		}
	;

mac_addr:	MAC_ADDR_STRING
		{
		  int byte_cnt;
		  int char_cnt = 0;
		  char *mac_addr = (char *)malloc(6);

		  for (byte_cnt=0; byte_cnt<6; byte_cnt++) {
		    int b = 0;
		    while (1) {
		      char c = $1[char_cnt];
		      if (c=='\0' || c==':') break;
		      if (c>='0' && c<='9') c -= '0';
		      else if (isalpha(c))  c = toupper(c) - 'A' + 10;
		      b = b*16 + c;
		      char_cnt++;
		    }
		    mac_addr[byte_cnt] = (char)b;
		    char_cnt++; /* jump over ':' */
		  }
		  $$ = mac_addr;
		}
	;
		      
%%

  void yyerror (const char* s)  {
    fprintf (stderr, "%s:%d: ", YYSourceName, YYLineNo);
    if (s) fprintf(stderr, "%s\n", s);
  }


/* {{{ Local variables */

/*
  Local variables:
  folded-file: t
  c-basic-offset: 2
  End:
*/

/* }}} */
