/*
** MAKEHELP.C	Converts text files to C strings.
**
** http://willus.com, 11-14-92
**
*/

#include <stdio.h>
#include <string.h>

#define BHELP	    "bhelp.txt"
#define EHELP	    "ehelp.txt"
#define HELPFILE    "helpvms.c"
#define EOS	    '\0'

static char *intro =
"/*\n"
"**\n"
"** HELP.C      Eval help text.\n"
"**\n"
"** Will Mennninger, ANSI C\n"
"**\n"
"*/\n"
"\n"
"char *bhelp =";

static void fix(char *buf);


int main()

    {
    FILE *out;
    FILE *f;
    char    buf[200];
    int     firstline;

    f=fopen(BHELP,"r");
    if (f==NULL)
	{
	printf("Cannot open file %s for input.\n",BHELP);
	return(1);
	}
    out=fopen(HELPFILE,"w");
    if (out==NULL)
	{
	fclose(f);
	printf("Cannot open file %s for output.\n",HELPFILE);
	return(2);
	}
    fprintf(out,"%s",intro);
    firstline=1;
    while (fgets(buf,99,f)!=NULL)
	{
	fix(buf);
	if (firstline)
	    {
	    firstline=0;
	    fprintf(out,"\n\"%s",&buf[1]);
	    }
	else
	    fprintf(out,"\\%s",buf);
	}
    fclose(f);
    fprintf(out,"\";\n\nchar *ehelp =");
    f=fopen(EHELP,"r");
    if (f==NULL)
	{
	fclose(out);
	printf("Cannot open file %s for input.\n",EHELP);
	return(3);
	}
    firstline=1;
    while (fgets(buf,99,f)!=NULL)
	{
	fix(buf);
	if (firstline)
	    {
	    firstline=0;
	    fprintf(out,"\n\"%s",&buf[1]);
	    }
	else
	    fprintf(out,"\\%s",buf);
	}
    fclose(f);
    fprintf(out,"\";\n");
    fclose(out);
    return(0);
    }


static void fix(char *buf)

    {
    char    s[200];
    int     is,ib;
    int     c;

    is=0;
    c=0;
    s[is++]='\n';
    for (ib=0;buf[ib]!='\n' && buf[ib]!=EOS;ib++)
	{
	if (buf[ib]=='\t')
	    {
	    s[is++]=' ';
	    c++;
	    while ((c&7))
		{
		s[is++]=' ';
		c++;
		}
	    continue;
	    }
	if (buf[ib]=='\f')
	    {
	    s[is++]='\\';
	    s[is++]='f';
	    c=0;
	    continue;
	    }
	if (buf[ib]=='\"' || buf[ib]=='\\')
	    s[is++]='\\';
	s[is++]=buf[ib];
	c++;
	}
    s[is++]='\\';
    s[is++]='n';
    s[is]=EOS;
    strcpy(buf,s);
    }
