#define max_key 30
#define max_size 100
#define max_items 300 \

#define abort(c,m) {fprintf(stderr,"%s!\n%s",m,buf) ;return c;}
/*2:*/
#line 42 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.w"

#line 19 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.ch"
#include <stdio.h> 
#include <string.h> 
#include <ctype.h> 
#line 46 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.w"

typedef struct{
char key[max_key];
char entry[max_size];
}item;
item items[max_items];
item*sorted[max_items];
char cur_page[10];
char buf[max_size];
char*input_status;

#line 27 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.ch"
int main()
#line 58 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.w"
{
#line 35 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.ch"
char*p,*q;
int n;
item*x,**y;
#line 62 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.w"
input_status= fgets(buf,max_size,stdin);
while(input_status){
/*3:*/
#line 71 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.w"

if(*buf!='!')abort(-1,"missing '!'");
if(strlen(buf+1)> 11)abort(-2,"page number too long");
for(p= buf+1,q= cur_page;*p!='\n';p++)*q++= *p;
*q= '\0';

/*:3*/
#line 64 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.w"
;
/*4:*/
#line 77 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.w"

n= 0;
while(1){
input_status= fgets(buf,max_size,stdin);
if(input_status==NULL||*buf!='+')break;
x= &items[n];
/*6:*/
#line 99 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.w"

if(*(buf+1)!=' ')abort(-3,"missing blank after +");
/*9:*/
#line 148 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.w"

#line 49 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.ch"
{int toggle= 0;
#line 150 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.w"
for(p= buf+2;(*p!=' '||toggle)&&*p;p++)
if(*p=='"')toggle^= 1;
}

/*:9*/
#line 101 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.w"
;
if(*p!=' ')abort(-4,"missing blank after alpha");
if(*(p+1)=='$')/*7:*/
#line 123 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.w"

{
if(*(p+2)!='\\')abort(-11,"missing custom backlash");
for(p+= 3,q= x->key;*p!=' '&&*p;p++){
if(isupper(*p))*q++= *p+('a'-'A');
else*q++= *p;
}
if(*p!=' ')abort(-12,"missing custom space");
if(*(p+1)!='$')abort(-13,"missing custom dollarsign");
}

/*:7*/
#line 103 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.w"

else{
if(*(p+1)!='\\')abort(-5,"missing backslash");
if(!*(p+2))abort(-6,"missing control code");
if(*(p+3)!='{')abort(-7,"missing left brace");
for(p+= 4,q= x->key;*p!='}'&&*p;p++){
if(*p!='\\'){
if(isupper(*p))*q++= *p+('a'-'A');
else if(*p=='_')*q++= ' ';
else*q++= *p;
}
}
if(*p!='}')abort(-8,"missing right brace");
}
if(q>=&x->key[max_key])abort(-9,"key too long");
*q= '\0';
/*10:*/
#line 156 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.w"

#line 55 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.ch"
{int toggle= 0;
#line 158 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.w"
for(p= buf+2,q= x->entry;(*p!=' '||toggle)&&*p;p++){
if(*p=='"')toggle^= 1;
if(*p!=' ')*q++= *p;
}
#line 62 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.ch"
for(;*p;p++)*q++= *p;
}

/*:10*/
#line 119 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.w"
;
if(p==buf+max_size-1)abort(-10,"entry too long");
*(q-1)= '\0';

/*:6*/
#line 83 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.w"
;
/*8:*/
#line 134 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.w"

for(y= &sorted[n];
y> &sorted[0]&&strcmp((*(y-1))->key,x->key)> 0;
y--)
*y= *(y-1);
*y= x;

/*:8*/
#line 84 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.w"
;
if(++n> max_items)abort(-11,"too many lines in group");
}

/*:4*/
#line 65 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.w"
;
/*5:*/
#line 88 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.w"

#line 43 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.ch"
{
#line 90 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.w"
for(y= sorted;y<sorted+n;y++)printf("%s\n",(*y)->entry);
printf("\\donewithpage%s\n",cur_page);
}

/*:5*/
#line 66 "/home/kbrown/src/cygpackages/texlive/texlive-20230313-2.x86_64/src/texlive-20230313-source/texk/web2c/cwebdir/refsort.w"
;
}
return 0;
}

/*:2*/
