#include <STDIO.H>
#include <STDLIB.H>
#include <STRING.H>
#include <DOS.H>
#include <CTYPE.H>

#ifdef LSI_C
#include <PATH.H>
#endif

#include "SYMBOL.H"

char guide[GUIDSIZE], line[LIN_MAX], buffer[BUFFSIZE];
char *bufptr;
char path[_MAX_PATH], infile[_MAX_PATH], outfile[_MAX_PATH];
char drive[_MAX_DRIVE], dir[_MAX_DIR], fname[_MAX_FNAME], ext[_MAX_EXT];
FILE *manfile, *hlpfile;

static char *topics(FILE *, char *);
static int   search(FILE *);

void main(int argc, char *argv[])
{
int arg;

	fputs("\n<HELPMAKR> Ver 1.02b Copyright (c) RYO 1993.\n\n", stderr);
	if ((argc>=2)&&(*argv[1]=='?')) {
	   fputs("書式 --- HELPMAKR [-F出力ﾌｧｲﾙ] [-Gｶﾞｲﾄﾞ文字列] [入力ﾌｧｲﾙ]\n", stderr);
	   exit(1);
	}

	manfile=hlpfile=(FILE *)NULL;
	_splitpath(argv[0], drive, dir, fname,    ext);
	_makepath(infile,   drive, dir, FILENAME, MANEXT);
	_makepath(outfile,  drive, dir, FILENAME, HLPEXT);
	strcpy(guide, GUIDE);

	for (arg=1; arg<argc; arg++) {
	   if (*argv[arg] == '-') {
	      switch(toupper(*(argv[arg]+1))) {
		 case'F':strncpy(outfile, argv[arg]+2, sizeof(outfile)-1);
			 outfile[sizeof(outfile)-1]='\0';
			 break;
		 case'G':strncpy(guide, argv[arg]+2, sizeof(guide)-1);
			 guide[sizeof(guide)-1]='\0';
			 break;
		 default:fputs("<", stderr);
			 fputs(argv[arg], stderr);
			 fputs("> は未定議ﾊﾟﾗﾒｰﾀです。\n", stderr);
			 exit(2);
	      }
	   }
	   else {
	      strncpy(infile, argv[arg], sizeof(infile)-1);
	      infile[sizeof(infile)-1]='\0';
	   }
	}

	if ((manfile=fopen(infile, "r")) != (FILE *)NULL) {
	   search(manfile);
	   fclose(manfile);
	   if ((hlpfile=fopen(outfile, "w")) != (FILE *)NULL) {
	      fputs(HELPHEAD, hlpfile);
	      _splitpath(infile, drive, dir, fname, ext);
	      _makepath(path, "", "", fname, ext);
	      fputs(path, hlpfile);
	      putc('\f', hlpfile);
	      for (bufptr=buffer; *bufptr; bufptr++)
	         putc(*bufptr, hlpfile);
	      putc('\0', hlpfile);
	      fclose(hlpfile);
	   }
	   else {
	      fputs("ｲﾝﾃﾞｯｸｽ･ﾌｧｲﾙ <", stderr);
	      fputs(outfile, stderr);
	      fputs("> が作成できません。\n", stderr);
	      exit(1);
	   }
	}
	else {
	   fputs("入力ﾌｧｲﾙ <", stderr);
	   fputs(infile, stderr);
	   fputs("> が見つかりません。\n", stderr);
	   exit(2);
	}
	exit(0);
}

int search(FILE *manfile)
{
int text, i;
char *guideptr;
char posbuf[12];
unsigned long fileptr;

	bufptr=&buffer[0];

	while((text=getc(manfile))!=EOF)
	   {
	   if ((char)text==guide[0])                       /* ガイド文字列？ */
	      {
	      guideptr=&guide[1];
	      fileptr=ftell(manfile)-1L;           /* ファイル位置の読みだし */
	      while(*guideptr != '\0')             /* ガイド文字列確認ループ */
		 {
		 if ((text=getc(manfile)) == EOF) break;           /* 違う！ */
		 if (text != *guideptr) break;                     /* 違う！ */
		 guideptr++;
		 }
	      if (*guideptr == '\0')             /* ガイド文字列を見つけた！ */
		 {
		 bufptr=topics(manfile, bufptr);      /*  トピックを切り出す */
		 *bufptr++ = '\t';                          /* ﾌｨｰﾙﾄﾞ･ｾﾊﾟﾚｰﾀ */
		 ltoa(fileptr, posbuf, 10);          /* ファイル位置の文字化 */
		 for (i=0; (i<sizeof(posbuf)-1)&&(posbuf[i]!='\0'); i++)
		    *bufptr++ = posbuf[i];         /* ファイル位置の書き込み */
		 *bufptr++ = '\n';                          /* End Of Record */
		 }
	      }
	   }
	*bufptr='\0';                                       /* End Of Manual */
	return 0;
}

char *topics(FILE *manfile, char *bufptr)
{
int text;
const char zen_spc[]="　";                   /* 上位バイトは全角カンマと同じ */
const char zen_com[]="，";                   /* 上位バイトは全角空白  と同じ */

	while((text=getc(manfile)) != EOF)
	   {
SEPRTR:	   if ((isspace(text))||(text==',')||(text=='(')||(text==')'))
	      continue;
	   else if (text==zen_spc[0])
	      {
	      text=getc(manfile);
	      if ((text==zen_spc[1])||(text==zen_com[1])) continue;
	      else                                        break;
	      }
	   else if (iscsym(text))
	      {
	      do {
		 *bufptr++ = text;
		 text=getc(manfile);
		 } while(iscsym(text));
	      *bufptr++ = ',';
	      goto SEPRTR;
	      }
	   else break;
	   }
	return bufptr-1;
}
