#include <jaz.h>		/* define misc macros and types */
#include <jzscreen.h>		/* define window types and colors	*/
#include <gscreen.h>		/* define global window characteristics */
#include <stdio.h>		/* standard c stuff			*/
#include <process.h>		/* ms/c exec call stuff 		*/

#if DEBUG
  #include <stdlib.h>
#endif

#define COPYWRITE "JZMENU   (C) JazSoft 1986 by Jack A. Zucker @ 301-794-5950 |\
 CIS: 75766,1336"


main(argc,argv)
int argc;
char **argv;
{

  int w,w2;			/* work integer 	*/
  int NUMITEMS; 		/* count of menu items	*/
  int wchoice;			/* users program choice */
  int wresult;			/* result of exec call	*/
  char wpgm[65];		/* hold the program name*/
  char wstr[80];		/* work string variable */
  char wname[64];		/* menu file name	*/
  char *wargv[23][9];		/* pointer to command line strings */
  char *malloc();		/* allocate memory space for command args */
  char *realloc();		/* re-allocate memory space for sub parms */
  FILE *fd;
  char wsbuf[256],wdbuf[256],*ch;
  char	*wmenu[23];		/* hold the menu text */
  char	*wprog[23];		/* hold the program names */
  int	 wargc[23];		/* hold argument counts   */

 /****************************************************************************/
 /*			      Main Program				     */
 /****************************************************************************/

  *wstr = 0;			/* initialize work strings  */
  *wpgm = 0;

  if (argc < 2) {
    printf("\nNo menu file specified");
    exit(1);
  }

  strcpy(wname,*++argv);	/* get menu file name */

  /* initialize the entire array of pointers so we can use realloc later */
  for (w = 0 ; w < 23 ; w ++) {
    for (w2 = 0 ; w2 < 9 ; w2 ++)
      wargv[w][w2] = malloc(1);
    wmenu[w] = malloc(1);
    wprog[w] = malloc(1);
  }

  do {

    if (!(fd = fopen(wname,"r"))) {    /* does the file exist */
      printf(**argv ? "\n%s Not found!" : "\nNo menu file specified" ,*argv);
      exit(1);
    }

    NUMITEMS = -1;

    while (fgets(wsbuf,255,fd)) {
      if (*wsbuf) {
	NUMITEMS ++;
	wsbuf[strlen(wsbuf)-1] = 0;    /* get rid of "\r" */
	jzgetpce(wsbuf,wdbuf,',',1);   /* parse out the menu description */
	wmenu[NUMITEMS] = (char *) realloc(wmenu[NUMITEMS],strlen(wdbuf)+1);
	strcpy(wmenu[NUMITEMS],wdbuf);
	jzgetpce(wsbuf,wdbuf,',',2);   /* parse out the program name */
	wprog[NUMITEMS] = realloc(wprog[NUMITEMS],strlen(wdbuf)+1);
	strcpy(wprog[NUMITEMS],wdbuf);
	for (w = 0 ; w < 9 ; w ++) {
	  jzgetpce(wsbuf,wdbuf,',',w+2);
	  if (! *wdbuf || *wdbuf == '\n') {
	    wargv[NUMITEMS][w] = realloc(wargv[NUMITEMS][w],1);
	    wargv[NUMITEMS][w] = 0;
	    break;
	  }
	  else {
	    wargv[NUMITEMS][w] = realloc(wargv[NUMITEMS][w],strlen(wdbuf)+1);
	    strcpy(wargv[NUMITEMS][w],wdbuf);
	  }
	}
      }
    }

    fclose(fd);

    #if DEBUG
      for (w = 0 ; w < 9 ; w ++) {
	for (w2 = 0 ; w2 < 9 ; w2 ++)
	  if (! *(wargv[w][w2])) break;
	  else
	    printf("%s",wargv[w][w2]);
	puts("\n");
      }
    #endif

    if (NUMITEMS < 0) {
      printf("\nNo menu information in file");
      exit(1);
    }
    cls(LIGHTGRAY);		  /* clear the display screen */

    jzscrprn(COPYWRITE,0,0,YELLOW);	  /* print logo information */

    jzscrprn("LAST PROGRAM:",23,0,MAGENTA);
    jzscrprn("ERROR CODE  :",24,0,MAGENTA);

    jzscrprn(wpgm,23,14,RED);		/* print program name */
    jzscrprn(wstr,24,14,RED);		/* print error code */

    if ((wchoice = jzmenu(wmenu,WHITE,BLUE,NUMITEMS,"\001"))== -1)
      exit(1);

    /* look for substitution parameters */

    for (w = 0 ; w < 10 ; w ++) {
      if (strcmpi(wargv[wchoice][w],"?INP?") == 0) {
	getinp(wstr,w);
	wargv[wchoice][w] = realloc(wargv[wchoice][w],strlen(wstr)+1);
	strcpy(wargv[wchoice][w],wstr);
      }
    }

    strcpy(wpgm,wprog[wchoice]);      /* get program name to invoke */

    cls(LIGHTGRAY);
    jzscrprn("Invoking:",0,0,CYAN);
    jzscrprn(wpgm,0,10,LIGHTCYAN);

    puts("\n");

    wresult = spawnv(P_WAIT,wargv[wchoice][0],wargv[wchoice]);

    itoa(wresult,wstr,10);		/* convert error code to string */

    printf("\n\nPress <Enter> to continue...");
    jzbeep();

    while (getch() != 13) ;

  } while (-1);

}

getinp(fstr,fline)
char *fstr;
int fline;
{

  char wnumstr[50];

  *fstr = 0;				    /* create null string */
  sprintf(wnumstr,"Argv[%d]:",fline);
  jzscrprn(wnumstr,fline+1,0,RED);	     /* display Prompt */
  jzinstr(fstr,35,fline+1,strlen(wnumstr)+1,GREEN,60L,"");  /* input string */

}

