/* EMACS_MODES: !lnumb !fill */
#define NAME "prnmenu"
#define VERSION "2.0"

#include <stdio.h>
#include <string.h>
#include <io.h>
#include <fcntl.h>
#include <dos.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>

#define MAXLINE 512
#define MAXPATH 128
#define KEYWAIT 120L  /* number of seconds to wait for no keys pressed */
#define PAUSEWARN 20L /* Warn user if paused more than PAUSEWARN secs */

#define TMOUT -1
#define OK 0
#define ERR -9

#define YES 1
#define NO -1
#define NONE 0

#define BREAKON 0
#define NOBREAK 1

#define nl() printf("\n");
#define BBSLST_LO 1
#define BBSLST_HI 999
#define CONLST_LO 0
#define CONLST_HI 999
#define SUBLST_LO 0
#define SUBLST_HI 9
#define NETLST_LO 0
#define NETLST_HI 2

#define bbslst_bad (lo<BBSLST_LO||lo>BBSLST_HI||hi<BBSLST_LO||hi>BBSLST_HI)
#define conlst_bad (lo<CONLST_LO||lo>CONLST_HI||hi<CONLST_LO||hi>CONLST_HI)
#define sublst_bad (lo<SUBLST_LO||lo>SUBLST_HI||hi<SUBLST_LO||hi>SUBLST_HI)
#define netlst_bad (lo<NETLST_LO||lo>NETLST_HI||hi<NETLST_LO||hi>NETLST_HI)

void purgkey();
void prompt();
void inline();
int charin();

/* Name of WWIV chain.txt file */
char chainfile[MAXPATH];

/* From skeleton.c */
#define v407


int    usernum,             /* user number for the user */
       age,                 /* age of the user */
       screenchars,         /* chars/line user has specified */
       screenlines,         /* lines/screen user has specified */
       sl,                  /* sec lev for user (0-255) */
       so,                  /* non-zero if user is sysop (sl=255) */
       cs,                  /* non-zero if user is co-sysop */
       okansi,              /* non-zero if user can support ANSI */
       incom,               /* non-zero if user is calling remotely */
       comport;             /* com port user is on */
char   name[81],            /* name/alias of user */
       realname[81],        /* real name of user */
       callsign[10],        /* amateur radio callsign of user */
       sex,                 /* sex of user, M or F */
       laston[81],          /* date user was last on */
       gfiles[81],          /* directory for text files, ends in \ */
       data[81],            /* directory for non-text files, ends in \ */
       sysoplog[81],        /* full path & filename for sysop log */
       curspeed[81];        /* speed user is on at, "KB" if local */
double gold,                /* gold user has */
       timeallowed,         /* number of seconds before user logged off */
       version;             /* version of WWIV running under */


#ifdef v407

/* function pointers for BBS functions.  ONLY used for 4.07 or later. */

void far interrupt (*inli)(char *, char *, int, int);
void far interrupt (*checka)(int *, int *);
void far interrupt (*pla)(char *, int *);
void far interrupt (*outchr)(char);
void far interrupt (*outstr)(char *);
void far interrupt (*nl)();
void far interrupt (*pl)(char *);
int  far interrupt (*empty)();
char far interrupt (*inkey)();
unsigned char far interrupt (*getkey)();
void far interrupt (*input)(char *, int);
void far interrupt (*inputl)(char *, int);
int  far interrupt (*yn)();
int  far interrupt (*ny)();
void far interrupt (*ansic)(int);
char far interrupt (*onek)(char *);
void far interrupt (*prt)(int, char *);
void far interrupt (*mpl)(int);

void far **funcs;

#endif

int read_in_data(char *fn)
{
  char buf[1024];
  char *ptr[50],*ss,s[81];
  int i,f,len,i1;
  float fl;

  f=open(fn,O_RDONLY | O_BINARY);
  if (f<0) {
    return(-1);
  }
  i1=1;
  ptr[0]=buf;
  len=read(f,(void *)buf,1024);
  close(f);
  for (i=0; i<len; i++)
    if (buf[i]==13) {
      buf[i]=0;
      ptr[i1++]=&buf[i+2];
    }
  while (*ptr[6]==32)
    ++(ptr[6]);
  while (*ptr[15]==32)
    ++(ptr[15]);
  usernum=atoi(ptr[0]);
  strcpy(name,ptr[1]);
  strcpy(realname,ptr[2]);
  strcpy(callsign,ptr[3]);
  age=atoi(ptr[4]);
  sex=*ptr[5];
  sscanf(ptr[6],"%f",&fl);
  gold=(double)fl;
  strcpy(laston,ptr[7]);
  screenchars=atoi(ptr[8]);
  screenlines=atoi(ptr[9]);
  sl=atoi(ptr[10]);
  so=atoi(ptr[11]);
  cs=atoi(ptr[12]);
  okansi=atoi(ptr[13]);
  incom=atoi(ptr[14]);
  sscanf(ptr[15],"%f",&fl);
  timeallowed=(double)fl;
  strcpy(gfiles,ptr[16]);
  strcpy(data,ptr[17]);
  strcpy(sysoplog,gfiles);
  strcat(sysoplog,ptr[18]);
  strcpy(curspeed,ptr[19]);
  comport=atoi(ptr[20]);

  version=-1.0;

  funcs=(void far *)getvect(0x6a);
  inli=funcs[0];
  checka=funcs[1];
  pla=funcs[2];
  outchr=funcs[3];
  outstr=funcs[4];
  nl=funcs[5];
  pl=funcs[8];
  empty=funcs[9];
  inkey=funcs[10];
  getkey=funcs[11];
  input=funcs[12];
  inputl=funcs[13];
  yn=funcs[14];
  ny=funcs[15];
  ansic=funcs[16];
  onek=funcs[17];
  prt=funcs[18];
  mpl=funcs[19];
  return(0);
}

/* My stuff */

void main(int argc, char *argv[])
{
	char s[MAXLINE];
	char outstr[128];

	if (argc<4){
		printf("By Andrew Gaunt, AKA Quantum, #1@6300 - May be freely shared.\n\n");
		printf("usage: prnmenu chainfile menufile menu\n");
		printf("Chainfile is file created by WWIV\n");
		printf("Menufile is file of menus, located in gfiles section.\n");
		printf("Menu is menu to print in menu file located in gfiles area\n");
		printf("\n[%s v%s]\n",NAME,VERSION);
		exit(0);
	}

	strcpy(chainfile,argv[1]);
	if (read_in_data(chainfile)==-1) {
 		fprintf(stderr,"%s: %s not found\n",NAME,s);
		exit(-1);
	}


printmenu(argv[2],atoi(argv[3]));
}

/* search through def'd menu file and printout appropriate menu */
printmenu(file,menu)
char file[];
int menu;
{
	char line[MAXLINE];
	char s[MAXPATH];
	FILE *fi;
	short flag;
	sprintf(s,"%s%s",gfiles,file);
/* 	printf("s=%s\n",s); */
	if ( (fi=fopen(s,"r")) == NULL){
		fprintf(stderr,"FATAL ERROR: can't read menufile, %s.\n",s);
		exit(ERR);
	}
	flag=0;
	while (fgets(line, MAXLINE,fi) != NULL){
		if (line[0] == '`'){
			sprintf(s,"`%02d",menu);
/* 			printf("line=%s s=%s\n",line,s); */
			if(strncmp(line,s,3) == NULL){
				flag=1;
				continue;
			}
			else{
				if (flag==1){ /* stop searching, found once */
					break;
				}
				flag=0;
			}
		}
		if (flag==1){
			printf("%s",line);
		}
	}
	fclose(fi);
/* 	printf("End\n") */
	return(0);
}
