#include <stdio.h>
#include <ctype.h>
char start='#';
#define prodef(x) #x,
#include "getlabel.h"
char *procnames[]={
#include "procedures.h"
};
int procnamesnr=sizeof(procnames)	/sizeof(procnames[0]);
int main(void) {
	char buf[4096],uit[5000],*ptr;
	FILE *names,*indexs,*help;
	do {
		if(!gets(buf)) {
			fprintf(stderr,"no %c\n",start);
			return -1;
			}
		} while(buf[0]!=start);
	names=fopen("helptext.h","w");	
	indexs=fopen("helpindex.h","w");	
	help=fopen("helpdata.h","w");	
	do {
		char *name=buf+1;
		int ind[100],nr=0;
		int startpos,i,iline,procedure;
		if(getlabel(procnames,procnamesnr,name)<0)
			procedure=0;
		else
			procedure=1;
		if(procedure) {
			fprintf(help,"{helpname(%s),",name);
			 fprintf(names,"char %sname[]=\"",name);
			 fprintf(indexs,"short %sind[]={",name);
			}
		if(!gets(buf)) {
			fprintf(stderr,"no text");
			return -1;
			}
		iline=0;
		do  {
		if(procedure) {
		  int escps=0,u=0;
	   	   startpos=ftell(names);
		   for(int j=0;buf[j];j++) {
			   if(buf[j]=='\\'||buf[j]=='\"'){
				   uit[u++]='\\';
				   escps++;
				  }
			   uit[u++]=buf[j];
		   	}
			uit[u]='\0';
		   fprintf(names,"%s\\000",uit);
		  // fputc(0,names);
		   iline=ind[nr]=iline+u+1-escps;
		   nr++;
		   	}
		   	} while((ptr=gets(buf))&&buf[0]!=start);
		if(procedure) {
		 fprintf(help,"%d},\n",nr);
		 fprintf(names,"\";\n");
		 for(i=0;i<(nr-1);i++) {
			 fprintf(indexs,"%d,",ind[i]);
		 	}
		 fprintf(indexs,"};\n");
		 	}
		} while(ptr); 
	fclose(names);
	fclose(help);
	fclose(indexs);
	return 0;
	}



