#!/usr/bin/perl5

#######!/bin/perl5

open (OUT, ">$ARGV[0]") or die "Can't open output file-($ARGV[0])\n";
open (IN, "<$ARGV[1]") or die "Can't open input file-($ARGV[1])\n";
$bison_tokens = $lex_rules = "";
$enum_attrs  = "enum ATTRS {\n";
$enum_objs   = "enum OBJS {\n";
$n = 0;    # this will be the number of known attributes
$m = 0;    # this will be the number of known objects
%map = (); # this hash will map attr name to integer needed by the C-arrays
%obj = ();
@attrs = ();
@obj_order = ();
@country = ();


while (<IN>) {

    next if (/^\#/);
    next if (/^\s*$/);
    
    last if (/^ENDCONF/);
    
    if (/^ATTR\s+(..)\s+(\S+)\s+([^\s\#]+\s)?/) { 
	$ATTR{$1}=$1; $ATTL{$1}=$2;

	$map{$1} = $n;
	
	$MAXATTRIBUTELENGTH=length($2) if (length($2)>$MAXATTRIBUTELENGTH);
	
	($upper = $1) =~ tr/a-z/A-Z/;
	
	# these are the flex rules for each known field
if ($1 eq "ac" || $1 eq "ah" || $1 eq "pn" || $1 eq "ro" || $1 eq "tc" ||
    $1 eq "zc") { 
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"$2:\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) { 
     BEGIN(STRING);   
     rol_count = 10;
     curr_obj.curr_attr = F_${upper};
     canonicalize_key_attr (&curr_obj, &canon_obj);
   }
   incr_tokenpos (tokenpos); 
   return T_${upper}_KEY; 
}\n";
                }
elsif ($1 eq "aa") {
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"as\"-?\"name:\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) {
     BEGIN (ASNAME);
     curr_obj.curr_attr = F_${upper};
     canonicalize_key_attr (&curr_obj, &canon_obj);
   }
   incr_tokenpos (tokenpos);
   return T_${upper}_KEY; 
}\n";
                }
elsif ($1 eq "rm") {
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"remark\"s?\":\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) {
     BEGIN (NON_NULL_LINE);
     curr_obj.curr_attr = F_${upper};
     canonicalize_key_attr (&curr_obj, &canon_obj);
   }
   incr_tokenpos (tokenpos);
   return T_${upper}_KEY; 
}\n";
                }
elsif ($1 eq "ad" || $1 eq "av" || $1 eq "de" || $1 eq "rm" || $1 eq "tb" ||
       $1 eq "tx" || $1 eq "cp" || $1 eq "cs" || $1 eq "ne" || $1 eq "np" ||
       $1 eq "rc" || $1 eq "ri" || $1 eq "rx" || $1 eq "rr" || $1 eq "ry") {
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"$2\:\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) {
     BEGIN (NON_NULL_LINE);
     curr_obj.curr_attr = F_${upper};
     canonicalize_key_attr (&curr_obj, &canon_obj);
   }
   incr_tokenpos (tokenpos);
   return T_${upper}_KEY; 
}\n";
                } # plain jane
elsif ($1 eq "ae" || $1 eq "al" || $1 eq "an" || $1 eq "am" || $1 eq "as" ||
	 $1 eq "au" || $1 eq "bg" || $1 eq "bi" || $1 eq "bl" || $1 eq "cl" ||
	 $1 eq "co" || $1 eq "da" || $1 eq "di" || $1 eq "dm" || $1 eq "do" ||
	 $1 eq "dp" || $1 eq "gw" || $1 eq "ii" || $1 eq "i6" || $1 eq "it" ||
	 $1 eq "io" || $1 eq "la" || $1 eq "li" || $1 eq "lo" || $1 eq "ml" ||
	 $1 eq "ni" || $1 eq "no" || $1 eq "op" || $1 eq "of" || $1 eq "or" ||
	 $1 eq "ph" || $1 eq "rl" || $1 eq "rp" || $1 eq "rt" || $1 eq "st" ||
	 $1 eq "tr" || $1 eq "wd") {
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"$2:\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) {
     curr_obj.curr_attr = F_${upper};
     canonicalize_key_attr (&curr_obj, &canon_obj);
   }
   incr_tokenpos (tokenpos);
   return T_${upper}_KEY; 
}\n";
                }
elsif ($1 eq "ai") {
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"$2:\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) {
     in_auth = 0;
     curr_obj.curr_attr = F_AI;
     canonicalize_key_attr (&curr_obj, &canon_obj);
     if (lc_str1[0] == '\\0')
       lc_lex (lc_str1, cpl);
   }
   incr_tokenpos (tokenpos); 
   return T_AI_KEY;
}\n";
                }
elsif ($1 eq "ao") {
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"$2:\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) {
     in_auth = 0;
     curr_obj.curr_attr = F_AO;
     canonicalize_key_attr (&curr_obj, &canon_obj);
     if (lc_str1[0] == '\\0')
       lc_lex (lc_str1, cpl);
   }
   incr_tokenpos (tokenpos); 
   return T_AO_KEY;
}\n";
                }
elsif ($1 eq "at") {
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"$2:\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) {
     in_auth = 1;
     correct_num_fields = 2;
     curr_obj.curr_attr = F_${upper};
     canonicalize_key_attr (&curr_obj, &canon_obj);
   }
   incr_tokenpos (tokenpos);
   return T_${upper}_KEY; 
}\n";
                }
elsif ($1 eq "ch") {
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"change\"d?\":\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) {
     BEGIN(EMAIL);
     correct_num_fields = 2;
     rol_count = 2;
     curr_obj.curr_attr = F_${upper};
     canonicalize_key_attr (&curr_obj, &canon_obj);
   }
   incr_tokenpos (tokenpos);
   return T_${upper}_KEY; 
}\n";
                }
if ($1 eq "cm" || $1 eq "cy" || $1 eq "ho" || $1 eq "nh" || $1 eq "so") { 
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"$2:\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) { 
     BEGIN(STRING);   
     rol_count = 1;
     curr_obj.curr_attr = F_${upper};
     canonicalize_key_attr (&curr_obj, &canon_obj);
   }
   incr_tokenpos (tokenpos); 
   return T_${upper}_KEY; 
}\n";
                }
elsif ($1 eq "df") {
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"$2:\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) {
     correct_num_fields = 3;
     curr_obj.curr_attr = F_${upper};
     canonicalize_key_attr (&curr_obj, &canon_obj);
   }
   incr_tokenpos (tokenpos);
   return T_${upper}_KEY; 
}\n";
                }
elsif ($1 eq "dn" || $1 eq "ir" || $1 eq "ns" || $1 eq "sd") {
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"$2:\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) {
     BEGIN(DOMAIN);
     curr_obj.curr_attr = F_${upper};
     canonicalize_key_attr (&curr_obj, &canon_obj);
   }
   incr_tokenpos (tokenpos);
   return T_${upper}_KEY; 
}\n";
                }
elsif ($1 eq "rz") {
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"rev-svr:\"[\\t ]* |\n\"rev-srv:\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) {
     BEGIN(DOMAIN);
     curr_obj.curr_attr = F_${upper};
     canonicalize_key_attr (&curr_obj, &canon_obj);
   }
   incr_tokenpos (tokenpos);
   return T_${upper}_KEY; 
}\n";
                }
elsif ($1 eq "dt" || $1 eq "mn" || $1 eq "ny" || $1 eq "om") {
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"$2:\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) {
     BEGIN(EMAIL);
     curr_obj.curr_attr = F_${upper};
     canonicalize_key_attr (&curr_obj, &canon_obj);
   }
   incr_tokenpos (tokenpos);
   return T_${upper}_KEY; 
}\n";
                }
elsif ($1 eq "em") {
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"e\"-?\"mail:\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) {
     BEGIN(EMAIL);
     curr_obj.curr_attr = F_${upper};
     canonicalize_key_attr (&curr_obj, &canon_obj);
   }
   incr_tokenpos (tokenpos);
   return T_${upper}_KEY; 
}\n";
                }
elsif ($1 eq "fx") {
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"fax\"(-no)?\":\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) {
     curr_obj.curr_attr = F_${upper};
     canonicalize_key_attr (&curr_obj, &canon_obj);
   }
   incr_tokenpos (tokenpos);
   return T_${upper}_KEY; 
}\n";
                }
elsif ($1 eq "gd") {
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"$2:\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) {
     BEGIN(EMAIL);
     rol_count = 1;
     curr_obj.curr_attr = F_${upper};
     canonicalize_key_attr (&curr_obj, &canon_obj);
   }
   incr_tokenpos (tokenpos);
   return T_${upper}_KEY; 
}\n";
                }
elsif ($1 eq "if") {
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"$2:\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) {
     correct_num_fields = 2;
     curr_obj.curr_attr = F_${upper};
     canonicalize_key_attr (&curr_obj, &canon_obj);
   }
   incr_tokenpos (tokenpos);
   return T_${upper}_KEY; 
}\n";
                }
elsif ($1 eq "in") {
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"network:\"[\\t ]* |\n\i?\"netnum:\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) {
     correct_num_fields = 3;
     curr_obj.curr_attr = F_${upper};
     canonicalize_key_attr (&curr_obj, &canon_obj);
   }
   incr_tokenpos (tokenpos);
   return T_${upper}_KEY; 
}\n";
                }
elsif ($1 eq "ma" || $1 eq "mb" || $1 eq "mt") {
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"$2:\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) {
     BEGIN(MNTNER);
     curr_obj.curr_attr = F_${upper};
     canonicalize_key_attr (&curr_obj, &canon_obj);
   }
   incr_tokenpos (tokenpos);
   return T_${upper}_KEY; 
}\n";
                }
elsif ($1 eq "na") {
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"$2:\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) {
     BEGIN (ASNAME);
     curr_obj.curr_attr = F_${upper};
     canonicalize_key_attr (&curr_obj, &canon_obj);
   }
   incr_tokenpos (tokenpos);
   return T_${upper}_KEY; 
}\n";
                }
elsif ($1 eq "pe") {
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"$2:\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) {
     correct_num_fields = 5;
     curr_obj.curr_attr = F_${upper};
     canonicalize_key_attr (&curr_obj, &canon_obj);
   }
   incr_tokenpos (tokenpos);
   return T_${upper}_KEY; 
}\n";
                }
elsif ($1 eq "ue" || $1 eq "uw" || $1 eq "uo") {
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"$2:\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) {
     BEGIN(FREETEXT);
     curr_obj.curr_attr = F_${upper};
   }
   incr_tokenpos (tokenpos);
   return T_${upper}_KEY; 
}\n";
                }
elsif ($1 eq "ud") {
$lex_rules .= "\"\\*ud:\"[\\t ]* |\n\"delete\"d?\":\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) {
     BEGIN(FREETEXT);
     curr_obj.curr_attr = F_${upper};
   }
   incr_tokenpos (tokenpos);
   return T_${upper}_KEY; 
}\n";
                }
elsif ($1 eq "ua") {
$lex_rules .= "\"\\*$1:\"[\\t ]* |\n\"$2:\"[\\t ]* {fprintf(dfile, \"%d T_${upper}_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) {
     BEGIN(FREETEXT);
     curr_obj.curr_attr = F_${upper};
     canonicalize_key_attr (&curr_obj, &canon_obj);
   }
   incr_tokenpos (tokenpos);
   return T_${upper}_KEY; 
}\n";
                }

	
	# this is the bison 'enum ATTRS' enumeration over each field
	if ($n == 0) {
	    $enum_attrs .= "    F_${upper} = 0";
	}
	elsif ($n % 8 == 0) {
	    $enum_attrs .= "    F_${upper}";
	}
	elsif ($n % 8 == 7) {
	    $enum_attrs .= ", F_${upper},\n";
	}
	else {
	    $enum_attrs .= ", F_${upper}";
	}
	
	# these are the bison tokens for each known field
	if ($n % 7 == 0) {
	    $bison_tokens .= "%token  T_${upper}_KEY";
	}
	elsif ($n % 7 == 6) {
	    $bison_tokens .= " T_${upper}_KEY\n";
	}
	else {
	    $bison_tokens .= " T_${upper}_KEY";
	}
	$n++;
	push (@attrs, $1);

	# this is the long field char name
	push (@long_attrs, $2);
	$map_long{$1} = $2;
	next;
    }
    
    
    if (s/^OBJ\s+(\S\S)\s+(\S+)\s+//) {
	
	$type=$1;
	$field=$2;
	
        # this is the bison 'enum OBJS' enumeration over each object
	if (!defined ($obj{$1})) {
	    ($upper = $1) =~ tr/a-z/A-Z/;
	    if ($m == 0) {
		$enum_objs .= "    O_${upper} = 0";
	    }
	    elsif ($m % 8 == 0) {
		$enum_objs .= "    O_${upper}";
	    }
	    elsif ($m % 8 == 7) {
		$enum_objs .= ", O_${upper},\n";
	    }
	    else {
		$enum_objs .= ", O_${upper}";
	    }
	    $m++;
	    $obj{$1} = 1;
	    push (@obj_order, $type);
	}
	
	s/\s+/ /g;
	s/ $//;
	
	if ($field eq "ATSQ") {
	    if ($OBJATSQ{$type}) {
		$OBJATSQ{$type}=join(" ", $OBJATSQ{$type}, $_);
	    }
	    else {
		$OBJATSQ{$type}=$_;
	    }
x	}
	elsif ($field eq "MAND") {
	    if ($OBJMAND{$type}) {
		$OBJMAND{$type}=join(" ", $OBJMAND{$type}, $_);
	    }
	    else {
		$OBJMAND{$type}=$_;
	    }
	}
	elsif ($field eq "MULT") {
	    if ($OBJMULT{$type}) {
		$OBJMULT{$type}=join(" ", $OBJMULT{$type}, $_);
	    }
	    else {
		$OBJMULT{$type}=$_;
	    }
	}

	# print STDERR "-",$KEYS{$type}, "-\n";
	
	next;
	
    }

    if (/^COUNTRY\s+(..)\s+(..)/) { 
	$countries++;
	push (@country, $1);
    }
    
}

# add these rules verbatim 

$lex_rules .= "\"*ua:\"[\\t ]* |
\"authorise:\"[\\t ]* {fprintf (dfile, \"%d T_UA_KEY: %s\\n\", lineno, yytext);
   if (++token_count == 1) { /* JW add this attr to the perl script */
     BEGIN(FREETEXT);
/* JW this is a total hack, since authorise should not be part of 
* the sytax outside of merit, I added this extra to avoid defining 
* authoise as a legal attribute.
*/
     curr_obj.curr_attr = F_UO;
   }
   incr_tokenpos (tokenpos); 
   return T_UA_KEY; 
}\n";

$lex_rules .= "\"password:\" {fprintf(dfile, \"%d T_PASSWD:  %s\\n\", lineno, yytext);
   if (++token_count == 1)
     BEGIN(FREETEXT);
   incr_tokenpos (tokenpos);
   return T_PASSWD; 
}\n";

$lex_rules .= ". {fprintf(dfile, \"[%s] T_JUNK line %d\\n\", yytext, lineno);
   BEGIN (ROL); 
   if (tokenpos == 0 ||
	cpl[tokenpos - 1] == '\t' ||
	cpl[tokenpos - 1] == ' '  ||
	(token_count == 1 && cpl[tokenpos - 1] == ':'))
   token_count++;
   return T_JUNK;
}\n";

print OUT "$lex_rules";

print OUT "\n--------Bison enum FIELDS--------\n";
if ($n % 8 != 7) {
    $enum_attrs .= "\n};";
}
else {
    $enum_attrs .= "};\n";
}
print OUT "#define     F_NOATTR      -1\n";
print OUT "#define     MAX_ATTRS $n\n";
print OUT "$enum_attrs";

print OUT "\n--------Bison enum OBJS--------\n";
if ($m % 8 != 7) {
    $enum_objs .= "\n};";
}
else {
    $enum_objs .= "};\n";
}
print OUT "#define     MAX_OBJS $m\n";
print OUT "$enum_objs";

print OUT "\n--------Bison tokens--------\n";
if ($n % 7 != 6) {
    $bison_tokens .= "\n";
}
print OUT "$bison_tokens";


print OUT "\n--------Bison legal attr------\n";
$legal_attrs = "short legal_attrs[MAX_OBJS][MAX_ATTRS] = {\n";
foreach $type (@obj_order) {
    %legalfield = ();
    $wt = 1;
    foreach $i (split(/\s+/, $OBJATSQ{$type})) {
#	$legalfield{$i}=1;
	$legalfield{$i}=$wt++;
    }
    foreach $i (("ud","uo","uw","ue")) {
	$legalfield{$i}= 1024;
    }

    $count = 0;
    $legal_attrs .= "{";
    foreach $field (@attrs) {
	$count++;
	if (defined ($legalfield{$field})) {
	    $wt = $legalfield{$field};
	    if ($count % 20 == 0) {
		$legal_attrs .= " $wt,\n";
	    }
	    else {
		$legal_attrs .= " $wt,";
	    }
	}
	else {
	    if ($count % 20 == 0) {
		$legal_attrs .= " 0,\n";
	    }
	    else {
		$legal_attrs .= " 0,";
	    }
	}
    }
    $legal_attrs .= "},\n";
}
$legal_attrs .= "};\n";
print OUT "$legal_attrs";

print OUT "\n--------Bison multiple_attrs------\n";
$mult_attrs = "short mult_attrs[MAX_OBJS][MAX_ATTRS] = {\n";
foreach $type (@obj_order) {
    %multfield = ();
    foreach $i (split(/\s+/, $OBJMULT{$type})) {
	$multfield{$i}=1;
    }

    $count = 0;
    $mult_attrs .= "{";
    foreach $field (@attrs) {
	$count++;
	if (defined ($multfield{$field})) {
	    if ($count % 20 == 0) {
		$mult_attrs .= " 1,\n";
	    }
	    else {
		$mult_attrs .= " 1,";
	    }
	}
	else {
	    if ($count % 20 == 0) {
		$mult_attrs .= " 0,\n";
	    }
	    else {
		$mult_attrs .= " 0,";
	    }
	}
    }
    $mult_attrs .= "},\n";
}
$mult_attrs .= "};\n";
print OUT "$mult_attrs";

print OUT "\n--------Bison mandatory's------\n";
$mands = "short mand_attrs[MAX_OBJS][MAX_MANDS] = {\n";
$max = 0;
foreach $type (@obj_order) {
    $count = 0;
    $mands .= "{";
    foreach $i (split(/\s+/, $OBJMAND{$type})) {
	$count++;
	$mands .= " $map{$i},";
    }
    
    $mands .= " -1},\n";
    $max = $count if (++$count > $max);
}
$mands .= "};\n";
print OUT "#define    MAX_MANDS $max\n";
print OUT "$mands";

print OUT "\n--------attrs which are keys-----------\n";
$attr_is_key = "short attr_is_key[MAX_ATTRS] = {\n";
for ($i=0;$i < $n;$i++) {
  $attr_is_key[$i] = -1;
}
$i = 0;
foreach $key (@obj_order) {
  $attr_is_key[$map{$key}] = $i++;
}
for ($i = 0;$i < $n;$i++) {
  if ($i == 0) {
    $attr_is_key .= "$attr_is_key[$i]";
  }
  elsif (($i + 1) % 15 == 0) {
    $attr_is_key .= ", $attr_is_key[$i],\n";
  }
  elsif (($i + 1) % 15 == 1) {
    $attr_is_key .= "$attr_is_key[$i]";
  }
  else {
    $attr_is_key .= ", $attr_is_key[$i]";
  }
}
if ($i % 15 == 0) {
    $attr_is_key .= "};\n";
}
else {
    $attr_is_key .= "\n};\n";
}
print OUT "$attr_is_key";

print OUT "\n-------long attributes char array------\n";
$count = 0;
$longs = "char *attr_name[MAX_ATTRS] = {\n";
foreach $field (@long_attrs) {
    $count++;
    if ($count % 5 == 1) {
	$longs .= "    \"$field\"";
    }
    elsif ($count % 5 == 0) {
	$longs .= ", \"$field\",\n";
    }
    else {
	$longs .= ", \"$field\"";
    }
}
if ($count % 5 == 0) {
    $longs .= "};\n";
}
else {
    $longs .= "\n};\n";
}
print OUT "$longs";


print OUT "\n-------short attributes char array------\n";
$count = 0;
$shorts = "char *attr_sname[MAX_ATTRS] = {\n";
foreach $field (@attrs) {
    $count++;
    if ($count % 10 == 1) {
	$shorts .= "    \"*$field\"";
    }
    elsif ($count % 10 == 0) {
	$shorts .= ", \"*$field\",\n";
    }
    else {
	$shorts .= ", \"*$field\"";
    }
}
if ($count % 10 == 0) {
    $shorts .= "};\n";
}
else {
    $shorts .= "\n};\n";
}
print OUT "$shorts";



print OUT "\n-------object types char array------\n";
$count = 0;
$longs = "char *obj_type[MAX_OBJS] = {\n";
foreach $i (@obj_order) {
    $count++;
    $field = $map_long{$i};
    if ($count % 5 == 1) {
	$longs .= "    \"$field\"";
    }
    elsif ($count % 5 == 0) {
	$longs .= ", \"$field\",\n";
    }
    else {
	$longs .= ", \"$field\"";
    }
}
if ($count % 5 == 0) {
    $longs .= "};\n";
}
else {
    $longs .= "\n};\n";
}
print OUT "$longs";

print OUT "\n/*-------syntax checking array------*/\n";
$syntax = "syntax_info_t syntax_info[MAX_ATTRS] = {\n";
foreach $i (@attrs) {
  $syntax .=  
      "  { do_nothing,     \"\",          do_nothing }, /* $i, $map_long{$i} */\n";
}
$syntax .= "};\n";

print OUT "$syntax";

print OUT "\n/* -------Countries char array------ */\n";
$count = 0;
$origin = "#define MAX_COUNTRIES $countries\n";
$origin .= "char *countries[MAX_COUNTRIES] = {\n";
foreach $field (@country) {
    $count++;
    if ($count % 10 == 1) {
        $origin .= "    \"$field\"";
    }
    elsif ($count % 10 == 0) {
        $origin .= ", \"$field\",\n";
    }
    else {
        $origin .= ", \"$field\"";
    }
}
if ($count % 10 == 0) {
    $origin .= "};\n";
}
else {
    $origin .= "\n};\n";
}
print OUT "$origin";


close (OUT);
