/* orisort : Program to sort the helpfile used by Origami-Editors tool
 * keybind.
 * Source Code written by Thomas Hadig in January 1993. This program
 * is freeware (read the README.AMIGA file in the src/amiga/docs dir)
 *
 * Compile with SAS C 6.1 : sc link ansi orisort
 *
 * Version 1.1
 */

/*{{{}}}*/
/*{{{  #includes*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*}}}  */
#define STOP_TOKEN '|'
/*{{{  variables*/
static char version_string_os_2_x[] = "$VER: Orisort 1.1 (" __DATE__ ")";

struct Store
{
  struct Store *Next;
  char         *Token;
  char         *String;
} *root=NULL,*zw;
/*}}}  */

/*{{{  read_lines*/
void read_lines (void)
{
  char one_line[200];
  struct Store *p;

  while (!feof(stdin))
  {
    fgets (one_line,200,stdin);
    if (*one_line)
    {
      /*{{{  get and init Store struct*/
      if (!(p=malloc(sizeof(struct Store))))
      {
        fputs ("No memory left !",stdout);
        exit (1);
      }
      if (!(p->String = malloc(strlen (one_line)+1)))
      {
        fputs ("No memory left !",stdout);
        exit (1);
      }
      strcpy (p->String,one_line);
      /*}}}  */
      p->Token=strchr (p->String,STOP_TOKEN);
      /*{{{  is there a root entry ? -> no*/
      if (!root)
      {
        p->Next =NULL;
        root = p;
      }
      /*}}}  */
      /*{{{  yes; is the the token in string ? -> no*/
      else if (!p->Token)
      {
        /*{{{  has root a token ? -> yes*/
        if (root->Token)
        {
          zw=root;
          while ((zw->Next)&&(zw->Next->Token)) zw=zw->Next;
          p->Next = zw->Next;
          zw->Next =p;
        }
        /*}}}  */
        /*{{{  no*/
        else
        {
          p->Next = root;
          root = p;
        }
        /*}}}  */
      }
      /*}}}  */
      /*{{{  yes*/
      else
      {
        if ((root->Token)&&(strcmp (root->Token,p->Token) > 0))
        {
          p->Next = root;
          root = p;
        }
        else
        {
          zw=root;
          while ((zw->Next)&&(zw->Next->Token)&&
            (strcmp (zw->Next->Token,p->Token)<0)) zw=zw->Next;
          p->Next = zw->Next;
          zw->Next =p;
        }
      }
      /*}}}  */
    }
  }
}
/*}}}  */
/*{{{  write_lines*/
void write_lines(void)
{
  while (root)
  {
    fputs (root->String,stdout);
    zw=root;
    root=zw->Next;
    free (zw->String);
    free (zw);
  }
}
/*}}}  */

/*{{{  main*/
int main (void)

{
  read_lines();
  write_lines();
  return 0;
}
/*}}}  */
