/* $Header: /bioy2/biocomputing/doelz/development/jam/RCS/jam.c,v 1.19 94/10/08 13:35:13 doelz Exp Locker: doelz $
/* JAM processor */ 
/*
         ***********************************************************
         *                                                         *
         *                         J A M                           * 
         *        J u s t   A n o t h e r   M e t a f i l e        *
         *                                                         *
         *                                                         *
         * This program package has been written starting from '94 *
         * at the Biocomputing Laboratory, University of Basel, by *
         * Reinhard Doelz. Valuable  suggestions from various col- *
         * leagues, including code contributions (as  documented), *
         * allowed to make this code portable, and  running on the *
         * different platforms.  Though it has been a major effort *
         * to make this language and its  production  tools  work, *
         * no   responsibility  can   be  taken for  errors  which *
         * might occur while running or result from processing the *
         * program's output. The program writing was supported  by *
         * Basel University, and a grant from the  Swiss  National *
         * Science Foundation  (NF). The  software  resembling JAM *
         * may be copied and resdistributed only if this statement *
         * is  preserved.  JAM  may  not,  as  a whole or in part, *
         * sold or used by commercial institutions to gain  profit *
         * from the services accessed on the basis of academic re- *
         * search. Users should acknowledge  the  program  package *
         * (Doelz, R., Just Another Metafile (JAM) (submitted)).   *
         ***********************************************************


If the definition JAMGUI is active, the program will generate a graphical user 
interface which is based on the VIBRANT toolkit as available from the NCBI. The
help and support from the NCBI for the VIBRANT product is very much appreciated. 

Vibrant is a multi-platform user interface development library that runs 
on the Macintosh, Microsoft Windows on the PC, or X11 and OSF/Motif on 
UNIX and VAX computers [separate documentation].  It is used to build 
the graphical interface for the Entrez application (whose source code is 
in the browser directory). The philosophy behind Vibrant is that 
everything in the published user interface guidelines (the generic 
behavior of windows, menus, buttons, etc.), as well as positioning and 
sizing of graphical control objects, is taken care of automatically.  
The program provides callback functions that are notified when the user 
has manipulated an object. Vibrant and Entrez code are not supported, 
but are provided on an as-is basis.
Questions or comments can be directed to toolbox@ncbi.nlm.nih.gov.


=====================================================================================
*/


#ifdef JAMGUI
#include "vibrant.h"
#include "document.h"
static void AboutClose(WindoW about);
FonT headingFont;
#define DEALEXIT ArrowCursor(); 
#else
#define MSG_ERROR stdout
#define MSG_POST stdout
#define MSG_OK stdout
#define DEALEXIT perror("Error in JAM"); 
#define Message fprintf
#endif
#include "jam.h"

static char code_id[] = "$Id: vutils.c,v 1.6 1994/11/23 05:54:37 eggenber Exp $";

/* Global variables */ 

#ifdef DEBUG 
int jam_debug=1;
#else
int jam_debug=0; 
#endif 
char yesno[2][2] ; 

char jam_line[MAXLINE]; 
char jam_link[MAXLINE]; 
char jam_file[MAXINC][MAXLINE]; 
char jam_out[MAXLINE]; 
FILE *jam_fp[MAXINC], *jam_op, *jam_index,*jam_all,*jam_cts; 
int current_jam; /* current open jam file */ 

/* conditionals */ 

int if_tstack;
int if_dstack; 
char if_token[MAXIF][MAXLINE];  
char if_defined[MAXIF][MAXLINE]; 

/* output modus */ 
int modus,sitespecific=TRUE; 
int did_a_print=FALSE; 
int vms_status=FALSE; 
int single_file=FALSE; 

/* chapters */ 
int chapter_number=0; 
int section_number;
int subsection_number; 
int line_number; 
int line_in_file; 
int line_in_input[MAXINC]; 

/* styles */ 
int verbatim = FALSE; 
int special = FALSE;  
int item = FALSE; 
int last_verbatim; 
int special_on=FALSE; 
int behaviour= NONE; 

/* item formatter */
char item_prefix[MAXLINE]; 


/*======================================= V I B R A N T =====================*/
#ifdef JAMGUI
ButtoN  bSRS, bENTREZ, bDECNET, bRCP, bPEARSON, bATLAS, bREADSEQ, bEGCG;
ButtoN bUSR1,bUSR2; 	
GrouP bOS,bSTE,bBEHAVIOUR, bSINGLE; 
PopuP  pop;
void MakeOptionalWindow (WindoW w2);
/*void TurnOnDefines (void);  */

/* Jam_modus sets the output format. */ 

static void Jam_modus (PopuP p)
{
  modus = GetValue(pop); 
  switch (modus) { 
  case HTML: 
    Enable (bBEHAVIOUR); 
    Disable (bSINGLE); 
    break; 
  case RTF: 
    Disable (bBEHAVIOUR); 
    Enable (bSINGLE); 
    break; 
  case LATEX:
    Disable (bBEHAVIOUR); 
    Disable (bSINGLE); 
    break; 
  } 
}

/* Jam_HTML sets either 'active' or 'none' for program links. */ 

static void Jam_HTML (void)
{
  behaviour=GetValue(bBEHAVIOUR); 
}

/* JAM_Single allows to have more than one RTF file if not set. */ 

static void Jam_SINGLE (void)
{
  if (GetValue(bSINGLE) > 1 ){ 
    single_file=TRUE;
  } 
}

static void jamstart(ButtoN dummy)
#else
/*================================= end    V I B R A N T =====================*/
main(int argc,char** argv)
#endif
{ 
  int status; 
  int i,ii; 
  int need_item_prefix; 
  int process;
  int define_process[MAXINC]; 
  int def_flag= TRUE; 
  char currentline[MAXLINE];   
  char token[MAXLINE]; 
  char* headline; 
  char link[MAXLINE], myline[MAXLINE], linkbody[MAXLINE]; 
  int curpointer; 
  int linkpointer; 
  int linktype; 
  int special; 

  need_item_prefix=0; 
  current_jam =0; 
  strcpy (jam_file[0], "MASTER.JAM"); 
  yesno[1][0]= 'Y'; 
  yesno[0][0]= 'N'; 

#ifdef JAMGUI
/*======================================= V I B R A N T =====================*/
  WatchCursor(); 
  TurnOnDefines(); 
  modus = GetValue(pop); 
  Jam_HTML(); 
  Jam_SINGLE(); 
  /*================================== end  V I B R A N T =====================*/
#else
  /* no command line interface here, will just require coding! */ 
  /*                                                           */
  /****                                                     ****/
  /********                                             ********/
  /**************                                  *************/
  /***********************                 *********************/
  /*************************************************************/
  
  /* Default is to produce a single RTF file for international UNIX. */ 
  
  modus        = RTF;      /*options are RTF, HTML, LATEX */ 
  behaviour    = JAMDUMMY; /* options are JAMDUMMY, JAMREFLECTOR, JAMPROCESSOR */ 
  single_file  = TRUE;     /* makes one RTF rather than many */ 
  sitespecific = FALSE;    /* international code here */ 
  
  if (argc > 1) { 
    Message (stdout,"commandline arguments detected - trying to process correctly.\n...\n\n"); 
    modus = 0;
    
    if (strcmp(argv[1],"RTF") == 0){ 
      modus = RTF; 
      Message (stdout,"setting MODUS to be RTF\n"); 
    } 
    if (strcmp(argv[1],"LATEX") == 0){ 
		modus = LATEX;
		Message (stdout,"setting MODUS to be LATEX\n");
	 }
	 if (strcmp(argv[1],"HTML") == 0){
      modus = HTML; 
      Message (MSG_OK,"setting MODUS to be HTML\n"); 
    } 
    if (modus == 0){ 
      modus = RTF; 
      Message (MSG_POST,"possible values for MODUS are  RTF, LATEX,HTML\n"); 
      Message (MSG_POST,"as '%s' was a non-recongized MODUS.\n\n\n",argv[1]);
      Message (MSG_OK,"Syntax: %s MODUS SCOPE [SINGLE|NONE] [... defines]\n", argv[0]); 
      exit(1); 
    } 
    sitespecific = -1;     
    if (argc > 2) { 
      if (strcmp(argv[2],"I") == 0){ 
	sitespecific = FALSE; 
	Message (MSG_POST,"setting SCOPE to be International\n"); 
      } 
      if (strcmp(argv[2],"S") == 0){ 
	sitespecific = TRUE; 
	Message (MSG_POST,"setting SCOPE to be Site-specific\n"); 
      } 
      if (sitespecific == -1){ 
	modus = RTF; 
	Message (MSG_POST,"possible values for SCOPE are  I, S (International/Site-specific).\n"); 
	Message (MSG_POST,"as '%s' was a non-recongized SCOPE.\n\n\n",argv[2]);
	Message (MSG_OK,"Syntax: %s MODUS SCOPE [SINGLE|NONE] [... defines]\n",argv[0]); 
	exit(1); 
      } 
      single_file=FALSE; 
      behaviour= JAMREFLECTOR; 
      if (argc > 3 ) { 
	for (i=3;i<argc;i++) { 
	  if (strcmp(argv[i],"SINGLE")==0) { 
	    single_file = TRUE; 
	    if (modus == RTF) {  
	      Message (MSG_POST,"defining a single file as output.\n"); 
	    } else { 
	      Message (MSG_OK,"SINGLE does not make sense - only in RTF modus\n"); 
	    } 
	  } else { 
	    if (strcmp(argv[i],"NONE")==0) { 
	      behaviour = JAMDUMMY;
	      if (modus == HTML) { 
		Message (MSG_POST,"defining no REFLECTOR in HTML.\n"); 
	      } else { 
		Message (MSG_OK,"NONE does not make sense - only in HTML modus\n"); 
	      } 
	    } else { 
	      strcpy(if_defined[if_dstack], argv[i]);
	      if_dstack++; 
	      Message (MSG_POST,"defining '%s' as logical.\n"); 
	    } 
	  } 
	} 
      } 
    } 
  } else { 
    Message (MSG_OK,"No commandline arguments detected - \ntrying to do a single RTF file for international UNIX.\n");
  } 
  
#endif
  
  /*.............................................................................*/
  /*                                                                             */
  /*            A C T U A L    C O D E    S T A R T   H E R E                    */ 
  /*                                                                             */
  
  /* initialize */ 
  status = jam_init(jam_file[0]); 
  define_process[0] = TRUE;  
  strcpy(if_token[0], "ELEMENTARY"); 
  
  /*.............................................................................*/
  /*                                                                             */
  /*            B I G   P A R S I N G    L O O P   S T A R T                     */ 
  /*                                                                             */
  
  
  while ((status =readline(currentline)) >= 0 ) {
    if (status == 0) { 
      readline(currentline); 
      } 
    if (currentline[0] != '%') {
      process = FALSE; 
    } else { 
      process = TRUE; 
    } 
    status = jam_parse(currentline); 
          i=0; 
      headline = currentline; 
      while (((currentline[i] != ' ') &&(currentline[i] != '\0'))  && (i < MAXLINE))  {
	i++; 
	headline++; 
      } 
      headline++; 


    switch (status) { 

      /*..................................................................*/ 
    case STARTIFDEF: 
      if_tstack++; 
      strcpy(token," "); 
      sscanf(currentline, "%s %s", token,token); 
      strcpy( if_token[if_tstack], token); 
      if (jam_debug) Message (MSG_OK,"start ifdef with %s [%d]\n", if_token[if_tstack],if_tstack);
      def_flag = FALSE; 
      for (i=0; i< if_dstack; i++) { 
	if (strcmp(if_token[if_tstack], if_defined[i]) == 0) { 
	  if (strcmp(if_token[if_tstack], "VMS") == 0) { 
	    vms_status=TRUE; 
	  } 
	  def_flag = TRUE; 
	} 
      }
      if (def_flag) 
	define_process[if_tstack] = TRUE; 
      else
	define_process[if_tstack] = FALSE;
      if (jam_debug) { 
	Message (MSG_OK,"[%4.0d][%2.0d:%s] %10s in %s\n", line_in_input[current_jam],if_tstack, yesno[define_process[if_tstack]], if_token[if_tstack],jam_file[current_jam]);
      } 
      def_flag=TRUE; 
      for (i=0; i<=if_tstack; i++){ 
	def_flag = def_flag*define_process[i];
      } 
      break; 

      /*..................................................................*/ 
    case ELSEIFDEF: 
      if (define_process[if_tstack] == TRUE) { 
	define_process[if_tstack] = FALSE; 
      } else { 
	define_process[if_tstack] = TRUE; 
      } 
      if (jam_debug) { Message (MSG_POST,"else ifdef with %s\n", if_token[if_tstack]); 
		       Message (MSG_POST,"[%4.0d][%2.0d:%s] %10s in %s\n", line_in_input[current_jam],if_tstack, yesno[define_process[if_tstack]], if_token[if_tstack],jam_file[current_jam]);} 
      def_flag=TRUE; 
      for (i=0; i<=if_tstack; i++){ 
	def_flag = def_flag*define_process[i]; 
      } 
      break; 
      
      /*..................................................................*/ 
    case ENDIFDEF:
      if_tstack --; 
      if (jam_debug) { Message (MSG_OK,"end ifdef with %s\n", if_token[if_tstack+1]); 
		       Message (MSG_OK,"[%4.0d][%2.0d:%s] %10s in %s; remaining %s %10s\n", line_in_input[current_jam],if_tstack+1,"E", if_token[if_tstack+1], jam_file[current_jam], yesno[define_process[if_tstack]], if_token[if_tstack]);
		     } 
      def_flag=TRUE; 
      for (i=0; i<=if_tstack; i++){ 
	def_flag = def_flag*define_process[i]; 
      } 
      break; 
      
      /*..................................................................*/ 
    case COMMENT: 
      break; 

      /*..................................................................*/ 
    case CHAPTER:
      if (jam_debug)  Message (MSG_POST,"headline with %s\n", headline);
      if (def_flag) makeachapter(headline); 
      break; 

      /*..................................................................*/ 
    case SECTION:
      if (jam_debug)  Message (MSG_POST,"zection with %s\n", headline);
      if (def_flag) makeasection(headline); 
      break; 

      /*..................................................................*/ 
    case SUBSECTION:
      if (jam_debug)  Message (MSG_POST,"zubsection with %s\n", headline);
      if (def_flag) makeasubsection(headline); 
      break; 
      
      /*..................................................................*/ 
    case INCLUDE:
      if (def_flag) { 
	strcpy(token," "); 
	sscanf(currentline, "%s %s", token,token);
	current_jam++; 
	strcpy( jam_file[current_jam], token);

	/* clean buffer */ 

	switch(modus){ 
	case LATEX: 
	  strcpy(myline,"\\mbox{}"); 
	  break; 
	case HTML: 
	  strcpy(myline,"<p>"); 
	  break; 
	case RTF: 
	  strcpy(myline,"\\par "); 
	  break; 
	default: 
	  break; 
	} 
	pprint(myline); 
	strcpy(myline,"!!!"); 
	pprint(myline); 
	strcpy(myline,"");


	/* incluseion means to open a new file and read from there till end */ 
	/* if we are international replace STE file by INT file */ 
	
	if ((strstr(jam_file[current_jam],".STE") != NULL) && (sitespecific != 1) ) {
	  /* replace by netural file, INT */
	  i=0; 
	  while ((jam_file[current_jam][i] != '.' ) && ( i < strlen(jam_file[current_jam]) ) )
	    i++; i++; 
	  jam_file[current_jam][i] = 'I'; i++; 
	  jam_file[current_jam][i] = 'N'; i++; 
	  jam_file[current_jam][i] = 'T'; i++; 
	}
	
	/* tell the user what you did */ 
	if (jam_debug)  Message (MSG_POST,"start include with %s\n", jam_file[current_jam]);
	Message (MSG_POST,"inclusion of %s\n", jam_file[current_jam]);
	jam_fp[current_jam] = fopen(jam_file[current_jam], "r"); 
	if (jam_fp[current_jam] == NULL) { 
	  Message (MSG_ERROR,"FATAL: Cannot open input JAM file %s\n",jam_file[current_jam] ); 
	  DEALEXIT 
	    exit (-1); 
	}  
	status = readline(jam_line);
	if (strncmp(jam_line,"% JAM 1.",strlen("% JAM 1.")) != 0) { 
	  Message (MSG_ERROR,"FATAL: Not a recognized version or JAM file: %s\n",jam_file[current_jam] ); 
	  Message (MSG_ERROR,"expected: 'JAM 1.x' found: %s", jam_line); 
	  DEALEXIT
	    exit (-1); 
	}   
      }
      
      break; 
      
      
      /*..................................................................*/ 
    case VERBATIM: 
      if (jam_debug)  Message (MSG_POST,"toggle VERBATIM in %s\n", jam_file[current_jam]);
      if (def_flag) process_verbatim(); 
      break; 
      
      /*..................................................................*/ 
    case SPECIAL: 
      if (jam_debug)  Message (MSG_POST,"toggle SPECIAL in %s\n", jam_file[current_jam]);
      if (def_flag) process_special(); 
      break; 
      
      /*..................................................................*/ 
    case ITEM: 
      if (jam_debug)  Message (MSG_POST,"toggle ITEM in %s\n", jam_file[current_jam]);
      if (def_flag) process_item(); 
      break; 
      
      /*..................................................................*/ 
    case ANITEM: 
      if (jam_debug)  Message (MSG_POST,"find ANITEM in %s\n", jam_file[current_jam]);
      if (def_flag) {
      	process_anitem(currentline); 
      	need_item_prefix = TRUE; 
      }
      
      /*..................................................................*/ 
    default:    
      /* scan current line for a link */ 
      
      if (def_flag) { 
	curpointer=0;
	linkpointer=0; 
	i=0; 
	/* token is the processing buffer for links */ 
	/* i is the pointer to token */ 
	
	strcpy(token, currentline); 
	linktype = NONE; 
	
	while ((token[i] != '\n') && (i < MAXLINE)) {
	  
	  special = NONE; 
	  
	  /* - - - - - - - - - - - - preprocessing link - - - -  */ 
	  strcpy (myline,token); 
	  myline[i]='!'; 
	  
	  if (token[i+1] == '|') { 
	    if (token[i] == '+')
	      { 
		linktype = ANCHOR; 
		i++; 
		i++; 
		special = TRUE; 
		linkpointer=0; 
	      } 
	    if (token[i] == '*')
	      { 
		linktype = POINTER; 
		i++; 
		i++; 
		special = TRUE;
		strcpy (linkbody, "NULL"); 
		linkpointer=0;  
	      } 
	    if (token[i] == '#')
	      { 
		linktype = INDEX; 
		i++; 
		i++; 
		special = TRUE;
		linkpointer=0;  
	      } 
	    if (token[i] == '[')
	      { 
		linktype = LINK; 
		i++; 
		i++; 
		special = TRUE;
		strcpy (linkbody, "NULL"); 
		linkpointer=0;  
	      } 
	    ; /* other link types here */ 
	    
	  } 
	  
	  
	  /* - - - - - - - - - - - - postprocessing link - - - -  */ 
	  if (token[i] == '|') { 
	    if (token[i+1] == '+') { 
	      linktype = ANCHOR;  
	      ; 
	      i++; 
	      i++; 
	      /* process finished link here */
	      /* linkpointer++; */
	      link[linkpointer]='\0';
	      currentline[curpointer]='\0';
	      pprint(currentline); 
	      if (def_flag){  
		switch (modus) { 
		case LATEX: 
		  sprintf(myline, "\\label{%s}\\index{%s}", link,link);
		  break; 
		case RTF: 
		  sprintf(myline, "{\\v {\\xe\\pard \\fs20  {\\v \\fs18 %s}}}", link);
		  break; 
		case HTML:
		  sprintf(myline, "<a name=%s>",link);
		  fprintf(jam_index,"%s %s%s#%s\n",link,JAMPATH,jam_out,link);  
		  break;
		default:
		  break; 
		} 
		pprint(myline);
		strcpy(currentline,"");
		curpointer=0; 
	      } 
	      linktype = NONE; 
	      special = TRUE; 
	    } 
	    if (token[i+1] == '*') { 
	      linktype = POINTER;  
	      ; 
	      i++; 
	      i++; 
	      /* process finished link here */
	      /* linkpointer++; */  
	      
	      currentline[curpointer]='\0';
	      link[linkpointer]='\0';
	      if (need_item_prefix) {
		strcat (item_prefix,currentline);
		need_item_prefix = 0 ;
	      }else { 
		strcpy(item_prefix,currentline); 
	      }
	      pprint(item_prefix); 
	      
	      if (def_flag) { 
		if (strcmp(linkbody,"NULL") == 0 ) { 
		  strcpy(linkbody, link) ; 
		  switch (modus) { 
		  case LATEX: 
		  case RTF:
		    strcpy(link,"");
		    break; 
		  case HTML: 
		    strcpy (link, "(*)"); 
		    break; 
		  default: 
		    break; 
		  } 
		}
		/* we have to search it in the index file */ 
		switch (modus) { 
		case LATEX: 
		  sprintf(myline, "%s (see also, page \\pageref{%s}) ", link,linkbody);
		  break;
		case HTML: 
		  status = jam_find(linkbody); 
		  sprintf(myline, "<a href=%s> %s </a> ", jam_link,link);
		  break;
		case RTF:
		  sprintf(myline, "%s (also described under keyword %s) ", link,linkbody);
		  break; 
		default:
		  break; 
		} 
		pprint(myline); 
		strcpy(currentline,"");
		curpointer=0; 
	      } 
	      linktype = NONE; 
	      special = TRUE; 
	    } 
	    if (token[i+1] == '#') { 
	      linktype = INDEX;  
	      ; 
	      i++; 
	      i++; 
	      /* process finished link here */
	      /*linkpointer++; */
	      link[linkpointer]='\0';
	      currentline[curpointer]='\0';
	      if (need_item_prefix) {
		strcat (item_prefix,currentline);
		need_item_prefix = 0 ; 
	      }else { 
		strcpy(item_prefix,currentline);
	      } 
	      pprint(item_prefix);  
	      if (def_flag) { 
		switch (modus) { 
		case LATEX: 
		  sprintf(myline, "\\index{%s} ", link);
		  break;
		case RTF: 
		  sprintf(myline, "{\\v {\\xe\\pard  {\\v \\fs18 %s}}}", link);
		  break; 
		case HTML: 
		  sprintf(myline, "<a name=%s_%d_%d_%d> ", link,
			  chapter_number,section_number,subsection_number);
		  fprintf(jam_index,"%s %s%s#%s_%d_%d_%d\n",link, JAMPATH,jam_out,link,
			  chapter_number,section_number,subsection_number);  
		  break;
		default: 
		  break; 
		} 
		pprint(myline);
		strcpy(currentline,"");
		curpointer=0; 
	      } 
	      linktype = NONE; 
	      special = TRUE; 
	    } 
	    if (token[i+1] == ']') { 
	      linktype = ANCHOR;  
	      ; 
	      i++; 
	      i++; 
	      /* process finished link here */
	      /* linkpointer++; */
	      currentline[curpointer]='\0';
	      if (need_item_prefix) {
		strcat (item_prefix,currentline);
		need_item_prefix = 0 ;
	      }else { 
		strcpy(item_prefix,currentline); 
	      }
	      pprint(item_prefix); 
	      link[linkpointer]='\0';
	      if (def_flag) { 
		if (strcmp(linkbody,"NULL") == 0 ) { 
		  strcpy(linkbody, link) ; 
		  switch (modus) { 
		  case LATEX: 
		  case RTF:
		    strcpy(link,"");
		    break; 
		  case HTML: 
		    strcpy (link, "(*)"); 
		    break; 
		  default: 
		    break;  
		  }
		}
		if (def_flag) { 
		  switch (modus) { 
		  case LATEX: 
		    if (special_on) 
		      sprintf(myline, "}\\index{%s}\\fbox{{\\em %s}}{\\em ", linkbody,link);
		    else
		      sprintf(myline, "\\index{%s}\\fbox{{\\em %s}} ", linkbody,link);
		    break; 
		  case RTF: 
		    if (special || verbatim) { 
		      sprintf(myline, "{\\v {\\xe\\pard \\fs20 {\\v \\fs18 %s}}} {\\fs24 \\b \\i %s }\\fs20 \\ql ", link,link);
		    } else { 
		      sprintf(myline, "{\\v {\\xe\\pard \\fs20 {\\v \\fs18 %s}}} {\\fs24 \\b \\i %s} \\fs20 \\qj ", link,link);
		    } 
		    break; 
		  case HTML: 
		    switch (behaviour) { 
		    case JAMDUMMY: 
		      sprintf(myline,"<strong> %s </strong> ",link);
		      break; 
		    case JAMRECEPTOR: 
		      sprintf(myline,"<a href=http://%s/%s> %s </a>",RECEPTOR,linkbody, link);
		      break; 
		    case JAMREFLECTOR: 
		      sprintf(myline,"<a href=http://%s/%s> %s </a>",REFLECTOR,linkbody, link);
		      break; 
		    default:
		      break;
		    } 
		    fprintf(jam_index,"%s %s%s#%s_%d_%d_%d\n",linkbody, JAMPATH,jam_out,linkbody,
			    chapter_number,section_number,subsection_number);  
		    
		    break; 	    
		  default: 
		    break; 
		  } 
		  
		  pprint (myline);
		  strcpy(currentline,"");
		  curpointer=0; 
		} 
		linktype = NONE; 
		special = TRUE; 
	      } 
	      /* other stop linktypes here */ 
	    }
	    
	  }
	  /* - - - - - - - - - processing link / linkbody  - - - -  */ 
	  
	  if (!special && (linktype != NONE) )   {  
	    if (token[i] == '|') { 
	      link[linkpointer]='\0'; 
	      strcpy(linkbody, link); 
	      linkpointer=0; 
	      i++; 
	      /* we have the link saved in linkbody, and wait for the link now*/ 
	    } 
	  }  
	  
	  
	  /* - - - - - -  make extra characters look OK in certain formats */ 
	  if  (!special)  { 
	    if ((linktype == NONE))   {  
	      if (strcmp(linkbody,"NULL") != 0 ) 
		switch (modus){
		  
		case LATEX:  
		  if (!verbatim)  {   
		    clean_latex (i,token,currentline,&curpointer,linkbody); 
		  }
		  break; 
		case HTML: 
		  if (token[i] == '>') { 
		    currentline[curpointer] = '&';
		    currentline[curpointer+1] = 'g';
		    currentline[curpointer+2] = 't';
		    token[i] = ';';
		    curpointer= curpointer+3;
		  } 
		  if (token[i] == '<') { 
		    currentline[curpointer] = '&';
		    currentline[curpointer+1] = 'l';
		    currentline[curpointer+2] = 't';
		    token[i] = ';';
		    curpointer= curpointer+3;
		  } 
		  break; 
		case RTF:  
		  if (token[i] <  ' ') { 
		    token[i] = ' ';
		  } 
		  if (token[i] == '\\') { 
		    currentline[curpointer] = '\\';
		    curpointer++; 
		  } 
		  if (token[i] == '{') { 
		    currentline[curpointer] = '\\';
		    curpointer++; 
		  } 
		  if (token[i] == '}') { 
		    currentline[curpointer] = '\\';
		    curpointer++; 
		  } 
		  
		  break; 
		default:
		  break; 
		  
		  
		  
		} 
	      currentline[curpointer]=token[i]; 
	      curpointer++; 
	    } else { 
	      switch (modus) { 
	      case LATEX: 
		clean_latex (i,token,link,&linkpointer,linkbody); 
		break; 
	      case RTF: 
		  if (token[i] <  ' ') { 
		    token[i] = ' ';
		  } 
		  if (token[i] == '\\') { 
		    link[linkpointer] = '\\';
		    linkpointer++; 
		  } 
		  if (token[i] == '{') { 
		    link[linkpointer] = '\\';
		    curpointer++; 
		  } 
		  if (token[i] == '}') { 
		    link[linkpointer] = '\\';
		    linkpointer++; 
		  } 

	      case HTML:
		break; 
	      default:
		break; 
	      } 
	      link[linkpointer]=token[i]; 
	      linkpointer++;
	    } 
	  
	  i++; 
	  
	} 
	
      } 
      
    } 
      
      
      if (def_flag) { 
	currentline[curpointer]=' '; 
	currentline[curpointer+1]='\0';  
	
	switch  (modus) { 
	case LATEX:
	case RTF:
	case HTML: 
	  if (need_item_prefix) {
	    sprintf (myline,"%s%s\n",item_prefix,currentline);
	    need_item_prefix = 0 ; 
	  } else {
	    sprintf(myline,"%s\n",currentline); 
	  }
	  break; 
	default: 
	  break; 
	} 
	pprint (myline);
	
      } 
      break; 
      
    }   
 }      
      
#ifdef JAMGUI
      DEALEXIT 
#endif
    
    exit(1);
      


}

#ifdef JAMGUI
/*======================================= V I B R A N T =====================*/

static void QuitProc (IteM i)
     
{
  QuitProgram ();
}

void RunSelect (GrouP h)

{
  ButtoN  b;
  GrouP   g;

  g = HiddenGroup (h, 2, 2, NULL); 
  if_tstack = 0; 
  if_dstack = 0;   
  b =  PushButton(g,"  Optional Parameters ", MakeOptionalWindow); 

  b = DefaultButton (g, "   Run JAM  ", jamstart);
}

void SelectDefines (GrouP h)

{

  GrouP   g;

  g = NormalGroup (h, 5, 0, "Available Extras", NULL, NULL);
  bSRS = CheckBox (g, "SRS", NULL);
  SetStatus (bSRS, TRUE);
  bENTREZ =CheckBox (g, "ENTREZ", NULL);
  bATLAS =CheckBox (g, "ATLAS", NULL);
  bREADSEQ =CheckBox (g, "READSEQ", NULL);
  bRCP =CheckBox (g, "RCP", NULL);
  bDECNET =CheckBox (g, "DECNET", NULL);
  bPEARSON =CheckBox (g, "PEARSON", NULL);
  bEGCG =CheckBox (g, "EGCG", NULL);
  bUSR1 =CheckBox (g, "USR1", NULL);
  bUSR2 =CheckBox (g, "USR2", NULL);
  /* new types: declare global Variable, and add here as well as in 
  TurnOnDefines */
}


static CharPtr popHTML [] = {
  "Reflector", "None", NULL
};

static void CreateSelectBehaviour (WindoW w)

{
  Int2 i; 
  int dummy;
  bBEHAVIOUR = NormalGroup (w, 4, 0, "HTML Layout",NULL,NULL);
  for (i = 0; popHTML [i] != NULL; i++) {
    RadioButton (bBEHAVIOUR, popHTML [i]);
  }
  SetValue (bBEHAVIOUR, 1);
} 
static CharPtr popSINGLE [] = {
  "Multiple", "Single", NULL
};

static void CreateSelectNumber (WindoW w)

{
  Int2 i; 
  int dummy;
  bSINGLE = NormalGroup (w, 4, 0, "RTF Layout",NULL,NULL);
  for (i = 0; popSINGLE [i] != NULL; i++) {
    RadioButton (bSINGLE, popSINGLE [i]);
  }
  SetValue (bSINGLE, 2);
} 
 
static void CreateOutputSelectFormat (WindoW w)

{
  int dummy;
  GrouP  h;
  h = HiddenGroup (w, 2, 20, NULL);
  CreateSelectBehaviour (h);
  CreateSelectNumber(h); 
  StaticPrompt (h, "Document Format Type", 0, popupMenuHeight, systemFont,  'l');
  pop = PopupList (h, TRUE, Jam_modus);
  /*PopupItem (pop, "ASCII");
  Disable (pop);*/
  PopupItem (pop, "RTF");
  PopupItem (pop, "LaTEX");
  PopupItem (pop, "HTML");
  SetValue (pop, 3);
  Disable (bSINGLE); 
}
static void AboutClose(WindoW aboutWindow)
{
        Hide(aboutWindow);
}

static void AboutProc(IteM Dummy)
{
  WindoW aboutWindow;
  DoC aboutDoc;
  FonT displayFont;  
  FILE *afile;        
  char amess[120]; 
  
#ifdef WIN_MSWIN
  displayFont = ParseFont("Times New Roman, 10");
#endif
#ifdef WIN_MAC
  displayFont = ParseFont("Times, 10");
#endif
#ifdef WIN_MOTIF
  displayFont = ParseFont("Courier, 12");
#endif
   
  afile = fopen ("about.txt", "r"); 
  if (afile == NULL) {  /* no about file found */
  	sprintf(amess, "%s",code_id); 
  	Message (MSG_OK,amess);
  	Message (MSG_OK, "SORRY; no documentation file in current directory");  
  	} else {
  	fclose (afile);
  	aboutWindow = FixedWindow(-50, -33, -15, -15, "About JAM", AboutClose);
  	aboutDoc = DocumentPanel(aboutWindow, 600, 300);         
	DisplayFile(aboutDoc, "about.txt", displayFont);
  	Show(aboutWindow);
  	}
}

  /* Jam_OS sets either UNIX or VMS, and causes vms_status to be set TRUE if so. */ 
 
static void Jam_OS (void)
{
  if (GetValue(bOS) == 1 )  { 
    strcpy(if_defined[if_dstack], "VMS"); 
    if_dstack++; 
  }
}

/* Jam_STE allows site-specific (STE) files rather than integral (INT) files. */ 

static void Jam_STE (void)
{
  if (GetValue(bSTE) == 1 )  { 
    sitespecific=TRUE;
    }else{
    sitespecific=FALSE;
      }
}


static void SetupObjects (WindoW w)

{
  GrouP  h;
  int dummy; 
  h = HiddenGroup (w, 2, 20, NULL);
  StaticPrompt (h, "JAM - Just Another Metaformat", 0,0, headingFont,  'c');
  h = NormalGroup (w, 3, 1, "Output definition parameters",systemFont,NULL);
  CreateOutputSelectFormat (h);      
  h =  HiddenGroup (w, 2, 20, NULL);
  RunSelect (h);
  Jam_OS(); 
  Jam_STE(); 

}

static void SetupMenus (WindoW w)

{
  ChoicE  c;
  MenU    m;

#ifdef WIN_MAC
  m = AppleMenu (NULL);
  DeskAccGroup (m);
#endif
  m = PulldownMenu (w, "File");
  CommandItem (m, "Quit/Q", QuitProc);
  CommandItem(m, "About/A", AboutProc);
#ifdef WIN_MSWIN
        headingFont = ParseFont("Arial, 18, b");
#endif
#ifdef WIN_MAC
        headingFont = ParseFont("Geneva, 18, b");
#endif
#ifdef WIN_MOTIF
        headingFont = ParseFont("Helvetica, 24, b");
#endif

}

Int2 Main (void)

{
  WindoW  w;

#ifdef WIN_MAC
  SetupMenus (NULL);
#endif
  w = FixedWindow (-50, -33, -10, -10, "(C) Biocomputing Basel 1994/5", NULL);
#ifndef WIN_MAC
  SetupMenus (w);
#endif

  SetupObjects (w);

  Show (w);
  ProcessEvents ();
  return 0;
}

#endif
/*======================================= V I B R A N T =====================*/


 

