#define NAME "wrtinfo"
#define VERSION "2.0"

#include <stdio.h>
#include <string.h>
#include <io.h>
#include <fcntl.h>
#include <dos.h>
#include <stdlib.h>


#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);
}


void main(int argc, char *argv[])
{
  FILE *f;
  char s[81];
  char	*cp;
  int	i, wd;
  int	j;


  if(argc <= 2) {
    printf("By Andrew Gaunt, AKA Quantum, #1@6300 - May be freely shared.\n\n");
    printf("usage: wrtinfo chainfile text ... outfile\n\n");
    printf("ex: wrtinfo chain.txt username=~n\\nusernum=~u\\n user.dat\n");
    printf("Wrtinfo writes the text given as it's arguments\n");
    printf("to the output file. The following escape sequences are\n");
    printf("interpreted before writing to the output file.\n");
    printf("       General Escape Sequences\n");
    printf("\\n newline              \\b backspace\n");
    printf("\\f formfeed             \\t tab\n");
    printf("\\v vertical tab         \\\\ backslash\n");
    printf("\\nnn Octal number, nnn  \\c supress newline\n");
    printf("\\s space\n");
    printf("       Extended escape sequences\n");
    printf("\~0  Colour 0    ....   ~6 Colour 6\n");
    printf("~u usernum      ~n name        ~r realname     ~h callsign\n");
    printf("~a age          ~s sex         ~$ gold         ~d laston\n");
    printf("~R screen rows  ~C screen columns)\n");
    printf("~l sec. level   ~Y sysop flag  ~y cosyop flag ~o okansi\n");
    printf("~i incom        ~t timeallowed\n");
    printf("~G gfiles dir   ~D data dir    ~S sysoplog filename\n");
    printf("~b curspeed     ~p comport     ~~ tilde char.\n");
    printf("[%s v%s]\n",NAME,VERSION);
    exit(0);
  }

  strcpy(s,argv[1]);	

  if (read_in_data(s)==-1) {
    fprintf(stderr,"wrtinfo: %s not found\n",s);
    exit(-1);
  }

  if ( (f=fopen(argv[argc-1],"a")) == NULL ) {
    fprintf(stderr, "wrtinfo: can't write to %s\n",argv[argc-1]);
    exit(-1);
  }


	for(i = 2; i <= argc-2; i++) {
		for(cp = argv[i]; *cp; cp++) {
			if(*cp == '\\')
			switch(*++cp) {
				case 'b':
					fputc('\b',f);
					continue;

				case 'c':
					exit(0);

				case 'f':
					fputc('\f',f);
					continue;

				case 's':
					fputc(' ',f);
					continue;

				case 'n':
					fputc('\n',f);
					continue;

				case 'r':
					fputc('\r',f);
					continue;

				case 't':
					fputc('\t',f);
					continue;

				case 'v':
					fputc('\v',f);
					continue;

				case '\\':
					fputc('\\',f);
					continue;
				case '0':
					j = wd = 0;
					while ((*++cp >= '0' && *cp <= '7') && j++ < 3) {
						wd <<= 3;
						wd |= (*cp - '0');
					}
					fputc(wd,f);
					--cp;
					continue;

				default:
					cp--;
			}

			if(*cp == '~')
			switch(*++cp) {

				case '0':
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
					fputc('\003',f);
					fputc(*cp,f);
					continue;


				case 'u':
					fprintf(f,"%d",usernum);
					continue;

				case 'n':
					fprintf(f,"%s",name);
					continue;

				case 'r':
					fprintf(f,"%s",realname);
					continue;

				case 'h':
					fprintf(f,"%s",callsign);
					continue;

				case 'a':
					fprintf(f,"%d",age);
					continue;

				case 's':
					fprintf(f,"%c",sex);
					continue;

				case '$':
					fprintf(f,"%f",gold);
					continue;

				case 'd':
					fprintf(f,"%s",laston);
					continue;

				case 'C':
					fprintf(f,"%d",screenchars);
					continue;

				case 'R':
					fprintf(f,"%d",screenlines);
					continue;

				case 'l':
					fprintf(f,"%d",sl);
					continue;

				case 'Y':
					fprintf(f,"%d",so);
					continue;

				case 'y':
					fprintf(f,"%d",cs);
					continue;

				case 'o':
					fprintf(f,"%d",okansi);
					continue;

				case 'i':
					fprintf(f,"%d",incom);
					continue;

				case 't':
					fprintf(f,"%f",timeallowed);
					continue;

				case 'G':
					fprintf(f,"%s",gfiles);
					continue;

				case 'D':
					fprintf(f,"%s",data);
					continue;

				case 'S':
					fprintf(f,"%s",sysoplog);
					continue;

				case 'b':
					fprintf(f,"%s",curspeed);
					continue;

				case 'p':
					fprintf(f,"%d",comport);
					continue;

				case '~':
					fputc('~',f);
					continue;

				default:
					cp--;
			}
			fputc(*cp,f);
		}
/* 		fputc(i == argc? '\n': ' ',f); */
	}
	exit(0);
}

