%{
/*---------McGill-Computer-Vision-and-Robotics-Laboratory--C-Module---------
 *		
 * NAME:	fxsort
 *
 * USAGE:	fxsort < input_stream > output_stream
 *
 * DESCRIPTION: This function takes the outputs from either fxref or invxref,
 *		and sorts them into alphabetical order.
 * 
 * GLOBAL VARS:
 *
 * FILES:	fxsort.lex -- lexical analyzer part
 * 
 * AUTHOR/DATE: Mike Parker, summer, 1984
 *
 * UPDATES:
 *
 *-------------McGill-Computer-Vision-and-Robotics-Laboratory--------------*/


#define YYLMAX 5000
struct node {
	     struct node *next;
	     char *key;
	     char *block;
	     char *name;
	    } *head_node, *cur_node, *test_node;
char key[256];
char *malloc(), *strcpy();
static int start_flag = 0;

%}
%START  GROUP LABEL
ident	[a-zA-Z$_][a-zA-Z$_0-9]*
fileid  [^ \t\n]+
w	[ \t]*
ww	[ \t\n]+
%%
{ident}{w}(":"|"@"){w}{fileid}{w}\n  {
				  set_key_two ();
				  start_flag = 1;
				}
{ident}{w}\n			{ 
				  set_key_one ();
				  start_flag = 1;
				}
((" "+.*\n)|\n)+                {
				  if (start_flag)
				   { 
				     cur_node->block = (char *)
				       strcpy (malloc(strlen(yytext)+1),yytext);
				   }
				}
%%

char *case_fold_upper (string)
char *string;
{
 int index;
 for (index = 0; string[index] != 0; index++)
    if ((string[index] >= 97) && (string[index] <= 122))
       string[index] = string[index] - 32;
 return (string);
}

main ()
{
  head_node = NULL;
  yylex ();
  for (test_node = head_node;
       test_node != NULL;
       test_node = test_node->next)
   {
     printf ("%s", test_node->name);
     if (test_node->block)
	printf ("%s", test_node->block);
   }
}

yywrap () /* what lex does when it reads an end of file */
{
 return (1);
}

set_key_one ()
{
 char *last;
 strcpy (key, yytext);
 last = (char *) index (key, '\n');
 if (last == 0)
  { 
    printf ("Error in fxsort; index 0 in function set_key_one\n");
    exit (1);
  }
 *last = 0;
 set_node (case_fold_upper (key));
}

set_key_two ()
{
 char *last;
 strcpy (key, yytext);
 last = (char *) index (key, ' ');
 if (last == 0)
  { 
    printf ("Error in fxsort; index 0 in function set_key_two\n");
    exit (1);
  }
 *last = 0;
 set_node (case_fold_upper (key));
}

set_node (key)
char *key;
{
 int i;
 cur_node = (struct node *) malloc (sizeof(struct node));
 cur_node->key = strcpy (malloc (strlen (key)+1), key);
 cur_node->name = strcpy (malloc (strlen (yytext)+1), yytext);
 cur_node->next = NULL;
 cur_node->block = NULL;
 if (head_node == NULL)
  { 
    head_node = cur_node;
  }
 else
  { 
    if (strcmp (head_node->key, cur_node->key) >= 0)
     { 
       cur_node->next = head_node;
       head_node = cur_node;
     }
    else
     {
       for (test_node=head_node;
	    test_node->next != NULL;
	    test_node = test_node->next)
	  if (strcmp (test_node->next->key, cur_node->key) >= 0)
           { 
	     cur_node->next = test_node->next;
	     test_node->next = cur_node;
	     break;
	   }
       if (test_node->next == NULL)
	{
	  test_node->next = cur_node;
	}
     }
  }
}
