/* $Header: /bioy2/biocomputing/doelz/development/jam/RCS/jamparse.c,v 1.14 94/10/08 13:35:17 doelz Exp Locker: doelz $ */ 
#include "jam.h"
#ifdef JAMGUI
#include "vibrant.h"
#define DEALEXIT ArrowCursor(); 
#else                  
gaga ?
#define Message fprintf
#define MSG_ERROR stdout
#define MSG_OK stdout
#define MSG_POST stdout
#define DEALEXIT perror("Error in JAM"); 
#endif

static char code_id[]="$Id: jamparse.c,v 1.14 94/10/08 13:35:17 doelz Exp Locker: doelz $"; 
                 

/* Global variables */ 

#ifdef DEBUG 
extern int jam_debug;
#else
extern int jam_debug; 
#endif 
/* extern const char yesyno[2]; */ 


extern char jam_line[MAXLINE]; 
extern char jam_link[MAXLINE]; 
extern char jam_file[MAXINC][MAXLINE]; 
extern char jam_out[MAXLINE]; 
extern FILE *jam_fp[MAXINC], *jam_op, *jam_index,*jam_all,*jam_cts; 
extern int current_jam; /* current open jam file */ 

/* conditionals */ 

extern int if_tstack;
extern int if_dstack; 
extern char if_token[MAXIF][MAXLINE];  
extern char if_defined[MAXIF][MAXLINE]; 

/* output modus */ 
extern int modus,sitespecific; 
extern int did_a_print; 
extern int vms_status; 
extern int single_file; 

/* chapters */ 
extern  int chapter_number; 
extern  int section_number;
extern  int subsection_number; 
extern  int line_number; 
extern  int line_in_file; 
extern  int line_in_input[MAXINC]; 

/* styles */ 
extern  int verbatim; 
extern  int special;  
extern  int special_on; 
extern  int item; 
extern  int last_verbatim; 
extern int behaviour; 

/* item formatter */
extern char item_prefix[MAXLINE]; 

 

/*---------------------------------------------------------------------*/
int jam_parse(char* line)
{ 
  int status; 
  
  status =0; 
  
  /* # statements first */ 
  
  if ( strncmp(line,"#ifdef", strlen("#ifdef")) == 0) { 
    if (!verbatim) status = STARTIFDEF; 
  } 
  if ( strncmp(line,"#else", strlen("#else")) == 0) { 
    if (!verbatim) status = ELSEIFDEF; 
  } 
  if ( strncmp(line,"#endif", strlen("#endif")) == 0) { 
    if (!verbatim) status = ENDIFDEF; 
  } 
  
  if ( strncmp(line,"#include", strlen("#include")) == 0) { 
    if (!verbatim) status = INCLUDE; 
  } 
  
  /* comments */ 

  if ( strncmp(line,"%", strlen("%")) == 0) { 
    if  (!verbatim)  status = COMMENT; 
  } 
  
  
  /* formats */ 
  
  if ( strncmp(line,"-V-", strlen("-V-")) == 0) { 
    status = VERBATIM; 
  } 
  
  if ( strncmp(line,"-v-", strlen("-v-")) == 0) { 
    status = VERBATIM; 
  } 
  
  if ( strncmp(line,"-S-", strlen("-S-")) == 0) { 
    if (!verbatim) status = SPECIAL; 
  } 
  
  if ( strncmp(line,"-s-", strlen("-s-")) == 0) { 
    if (!verbatim) status = SPECIAL; 
  } 
  
  if ( strncmp(line,"-I-", strlen("-I-")) == 0) { 
    if (!verbatim) status = ITEM; 
  } 
  
  if ( strncmp(line,"-i-", strlen("-i-")) == 0) {
    if (!verbatim) status = ITEM; 
  } 
  
  if ( strncmp(line,"o ", strlen("o ")) == 0) { 
    if ((item) && (!verbatim))  status = ANITEM; 
  } 
  
  
  
  /* chapters and sections */ 
  
  
  if ( strncmp(line,"* ", strlen("* ")) == 0) { 
    if (!verbatim) status = CHAPTER; 
  } 
  
  if ( strncmp(line,"-*- ", strlen("-*- ")) == 0) { 
    if (!verbatim) status = SECTION; 
  } 
  
  if ( strncmp(line,"-**- ", strlen("-**- ")) == 0) { 
    if (!verbatim) status = SUBSECTION; 
  } 
  
  return(status); 
} 
/*---------------------------------------------------------------------*/
int jam_init(char* filename) 
{ 
  int status; 
  
  int i, ii; 
/* chapters */ 
  chapter_number=0; 
  section_number=0;
  subsection_number=0; 

/* styles */ 
  verbatim = 0; 
  special = 0;  
  item = 0; 
  
  
  /* initialization */ 
  
#ifndef JAMGUI
  if_tstack = 0; 
  if_dstack = 2; 

  strcpy(if_defined[0], "HULLA"); 
  strcpy(if_defined[1], "SRS"); 
#endif
  switch (modus) { 
  case LATEX: 
    jam_all= fopen("JAMINX.TEX","w"); 
    break; 
  case RTF:
    jam_all= fopen("JAMINX.RTF","w"); 

    if (jam_all == NULL) { 
      Message ("FATAL: Cannot open index file %s\n", "JAMINX.RTF"); 
      DEALEXIT
	exit(-1); 
    }
    break; 
  case HTML: 
    jam_all= fopen("JAMINX.HTML","w");
    if (jam_all == NULL) { 
      Message ("FATAL: Cannot open index file %s\n", "JAMINX.HTML"); 
          DEALEXIT
	exit (-1); 
    }else { 
      Message (MSG_OK,"will create file JAMINX.HTML (index page)\n"); 
    }
    jam_cts= fopen("JAMCTS.HTML","w");
    if (jam_cts == NULL) { 
      Message ("FATAL: Cannot open contents file %s\n", "JAMCTS.HTML"); 
           DEALEXIT
	exit (-1); 
    } else { 
      Message (MSG_OK,"will create file JAMCTS.HTML (contents page)\n"); 
      fprintf(jam_cts,"<TITLE> JAM TABLE OF CONTENTS Page </TITLE>\n");
      fprintf(jam_cts,"<H1> Table of Contents </H1>\n");    
    } 
    fprintf(jam_all,"<TITLE> JAM Master Page </TITLE>\n");
    fprintf(jam_all,"<H1>Contents</H1><HR>\n"); 
    fprintf(jam_all,"<UL>\n");    
    jam_index = fopen("JAM.INX", "w");
    if (jam_index == NULL) { 
      Message ("FATAL: Cannot open index file %s\n", "JAM.INX"); 
          DEALEXIT
	exit (-1); 
    } break; 
  default: 
    break; 
  } 

  /* open main 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++; 
    }
  
  jam_fp[current_jam] = fopen(jam_file[current_jam], "r"); 
  if (jam_fp[current_jam] == NULL) { 
    Message ("FATAL: Cannot open input JAM file %s\n",jam_file[current_jam] ); 
        DEALEXIT

	exit (-1); 
  } 
  
  status = readline(jam_line);
  status = strlen("% JAM 1.");
  if (strncmp(jam_line,"% JAM 1.",status) != 0) { 
    Message ("FATAL: Not a recognized version or JAM file: %s\n",jam_file[current_jam] ); 
    Message ("expected: 'JAM 1.x' found: %s", jam_line); 
        DEALEXIT
	exit (-1); 
  } 
} 


/*---------------------------------------------------------------------*/

int readline(char *line)
{ 
  int status; 
  char myline[MAXLINE]; 
  
  /* Message ("%d %d\n", current_jam, jam_fp[current_jam]) ;*/ 
  
  if (fgets(line,MAXLINE-5, jam_fp[current_jam]) != NULL ) { 
    /*   Message ("%d %s\n", jam_fp[0],line); */ 
    line_in_input[current_jam]++; 
    status = TRUE; 
  } else { 
    status = FALSE; 
  } 
  ; 
  
  if (status == TRUE) return (status);
  strcpy(myline,"!!!"); 
  pprint(myline); 
  Message(MSG_POST,"done with %s (level %d)\n", jam_file[current_jam],current_jam);  
  current_jam--; 
  if ( current_jam >= 0) { 
    fclose(jam_fp[current_jam+1]);  
    status =0; 
    return(status); 
  } else  { 
    /* close the index file */ 
    switch(modus) { 
    case RTF: 
      if (single_file)  {  
	strcpy(myline,"}"); 
	pprint(myline); 
	strcpy(myline,"!!!"); 
	pprint(myline); 
      }
      break; 
    case LATEX:
      fprintf (jam_all,"\\clearpage"); 
      fprintf (jam_all,"{\\bf This Document was written in the JAM language, \n"); 
      fprintf (jam_all,"which is (C) Biocomputing Basel, 1994. For information on this facility or\n"); 
      fprintf (jam_all,"{\\em EMBnet Switzerland} please send an electronic mail message \n"); 
      fprintf (jam_all, "to {\\tt info@ch.embnet.org} or FAX a clearly addressed letter \n"); 
      fprintf (jam_all, "to \\mbox{Biocomputing Basel}, x41 61 267 20 78}\n\n");
      break; 
    case HTML:
      fprintf (jam_all,"</UL><HR><a href=%sJAMCTS.HTML> Extended Table of Contents</a>\n <p><em>%s</em><p>\n",JAMPATH,code_id); 
      fprintf (jam_all,"<strong>JAM is (C)</strong> <a href=http://beta.embnet.unibas.ch/>EMBnet Switzerland, Biocomputing Basel </a>"); 
      break; 
    default: 
      break; 
    } 
    fclose(jam_fp[current_jam+1]); 
    fclose (jam_all); 
    /* close the link index */ 
    fclose (jam_index); 
    switch(modus) {
      /* tell the reader that you do a postprocessing with the link index */ 
    case LATEX: 
      Message (MSG_OK, "Use 'makeindex' to create the index files."); 
      break; 
    case RTF:
      break; 
    case HTML:
      Message (MSG_POST, "JAM: rewriting index file."); 
      jam_write(); 
      break; 
    default: 
      break; 
    } 
#ifdef JAMGUI
    DEALEXIT
#endif
      switch(modus) { 
      case HTML: 
	Message (MSG_OK,"SUCCESS: JAM done.\nYou might need to rerun JAM for correct references/links.\n");
	break; 
      case LATEX:
      case RTF: 
	Message (MSG_OK,"SUCCESS: JAM done.\n");
	break; 
      default:
	break; 
      } 
    exit (1); 
  } 
  
}  
/*---------------------------------------------------------------------*/

int jam_write(void)
{ 
  char line[MAXLINE], test[MAXLINE]; 
  FILE *indexfile,*newfile;
  int i,status; 
  
  /* this is to copy file into indexfile. Needed for HTML and others. */ 

  newfile = fopen("JAMKEY.INX", "w");
  if (newfile == NULL) { 
    Message ("FATAL: Cannot open output destination %s\n", "JAMKEY.INX"); 
        DEALEXIT
	exit (-1); 
  } else { 
    Message(MSG_OK,"opened %s for cross-references\n", "JAMKEY.INX"); 
  } 
  indexfile = fopen("JAM.INX", "r");
  if (indexfile == NULL) { 
    Message ("FATAL: Cannot open output destination %s\n", "JAM.INX"); 
        DEALEXIT
	exit (-1); 
  } else { 
    Message (MSG_POST,"opened %s for input\n", "JAM.INX"); 
  } 
    
  i=0; 

  while (fgets(line,MAXLINE,indexfile)) { 
    fprintf (newfile,"%s",line); 
    i++; 
  } 
	
  fclose (indexfile); 
  fclose(newfile); 
  Message (MSG_POST,"JAM: rewrote %d index entries \n", i); 
} 
/*---------------------------------------------------------------------*/

int jam_find(char* token)
{ 
  char line[MAXLINE], test[MAXLINE], link[MAXLINE]; 
  FILE *indexfile;
  int i,status; 
  
  /* this is to copy file into indexfile. Needed for HTML and others. */ 

  indexfile = fopen("JAMKEY.INX", "r");
  if (indexfile == NULL) { 
    Message ("FATAL: Cannot open input destination %s\n", "JAMKEY.INX"); 
         DEALEXIT
	exit (-1); 
  }  
    
  status =0; 
  i=0; 

  
  while (fgets(line,MAXLINE,indexfile)) { 
    if (status ==0) { 
      sscanf (line,"%s %s", test, jam_link); 
      if (strncmp(test, token,strlen(test) ) == 0 ) status = TRUE;
    } 
  } 
  if (status == FALSE) strcpy(jam_link, "VOID"); 
  fclose(indexfile); 
  return(status); 
} 


 


/*---------------------------------------------------------------------*/

int makeachapter(char *line)
{ 
  int status; 
  int i,ii,maxdos; 
  char* pos; 
  char myline[MAXLINE];
  char tmpstring[MAXLINE]; 
  char extension[MAXLINE]; 
  char title[MAXLINE]; 

  i=0; 
  pos = line; 
  while (line[i] != '\n') { 
    i++; 
    pos++; 
  } 
  *pos = '\0'; 
  
  chapter_number++;
  section_number=0; 
  subsection_number=0; 
  line_in_input[current_jam]=0; 

  if (chapter_number > 1)  { 
    strcpy(myline,"!!!"); 
    pprint(myline); 
    strcpy(myline,""); 
    
    switch (modus) { 
    case LATEX:
      i=0; 
      ii=0; 
      strcpy(title, jam_out); 
      while ((title[i] != '\n') && (i < MAXLINE)) {
	clean_latex (i,title,tmpstring,&ii,myline); 
	tmpstring[ii]=title[i]; 
	ii++; 
	i++; 
      } 
      sprintf(myline,"\\mbox{}\\\\\\vspace{60pt}{\\tiny JAM produced file: %s}\n", jam_out); 
      pprint(myline); 
      strcpy(myline,"!!!"); 
      pprint(myline); 
      strcpy(myline,""); 
      
      break; 
    case RTF: 
       if (!single_file) 
	 { sprintf (myline, "\\par \\par  \n{ \\pard \\fs20{\\v {\\i\\fs18 (JAM produced file: %s)}}\\pard \\fs20 \\qj  \\par  ", jam_out);
	   strcpy(myline,"}");
	 } 
      break; 
    case HTML:
      sprintf(myline,"<HR><em>JAM produced file:%s</em>\n", jam_out); 
      pprint(myline); 
      strcpy(myline,"!!!"); 
      pprint(myline); 
      strcpy(myline,""); 

      break; 
    default:
      break; 
    }
    pprint(myline); 
    
    strcpy(myline,"!!!"); 
    pprint(myline); 
	 if ((!single_file) && (modus == RTF) ) fclose (jam_op);
	 if (( modus == HTML) ||(modus == LATEX) ) fclose (jam_op);
  } else {
    if ( (single_file) && ( modus == RTF) ) {
      jam_op = fopen("JAM.RTF", "w");
      if (jam_op == NULL) { 
	Message ("FATAL: Cannot open output destination %s\n", jam_out); 
	DEALEXIT
      } 
    } 
  } 
  switch (modus) { 
  case LATEX: 
    strcpy(extension,LATEXEXTENSION); 
    break; 
  case RTF: 
    strcpy(extension,RTFEXTENSION); 
    break; 
  case HTML:
    strcpy(extension,HTMLEXTENSION); 
    break; 
  default:
    break; 
  } 
  strcpy(jam_out,line);
  /* find first blank in tmpstring */ 
  i=0; 
  while ( (i <= strlen(jam_out))  && ( 
				      /* is ascii - not supported on some systems */ 
				      ( (jam_out[i]<='Z') && (jam_out[i]>='A') ) ||  
				      (jam_out[i]<='z') && (jam_out[i]>='a') ) )  
    i++; 
  for (ii = 0; ii<=i; ii++) { 
    if ((jam_out[ii]<='z') && (jam_out[ii]>='a') ) { 
      jam_out[ii]=jam_out[ii]-32; 
    } 
  } /* uppercasing */ 

  strncpy(title,jam_out,i);
  title[i]=0; 
    
  if (chapter_number < 10) maxdos=1; 
  if ((chapter_number > 9) && (chapter_number< 100)) maxdos=2; 
  if (chapter_number > 99) maxdos=3; 
  if (i >= 8-maxdos) i=8-maxdos; /* DOS file name convention, allow for three-digit number*/ 
  strncpy(tmpstring,jam_out,i); 
  tmpstring[i]=0; 

  sprintf (jam_out,"%s%d.%s\0",tmpstring,chapter_number,extension); 
		     
  if ( (!single_file) || ( modus != RTF) )  { 
    jam_op = fopen(jam_out, "w");
    if (jam_op == NULL) { 
      Message ("FATAL: Cannot open output destination %s\n", jam_out); 
      DEALEXIT
	
	exit (-1); 
    } else { 
      Message (MSG_POST,"opened %s for output\n", jam_out); 
    } 
  } 
  switch (modus){ 
  case LATEX: 
    fprintf (jam_all,"\\input{%s}\n", jam_out); 
    sprintf (myline, "\n\\chapter{%s}\n", line);
    pprint(myline);
    strcpy(myline,"!!!"); 
    pprint(myline);
        sprintf (myline,"%this file was generated by %s>", code_id);
    strcpy(myline,"!!!"); 
    pprint(myline);
    strcpy(myline, ""); 
    break; 
  case RTF: 
    if ((!single_file) || ( (single_file) &&  (chapter_number <= 1)) )  { 
      strcpy(myline, "{\\rtf1 \\deff25 \\deffs20{\\fonttbl{\\f25\\fswiss Helvetica;}{\\f26\\fmodern Courier;}}"); 
      pprint(myline);
      strcpy(myline, "\\sectd \\sbknone\\linemod0\\linex0\\cols1\\endnhere \\pard \\fs20\\plain \\qj"); 
      pprint(myline);
      sprintf (myline,"{\\info{\\title %s}}", code_id); 
      strcpy(myline,"!!!"); 
      pprint(myline);
    } else { 
      sprintf (myline," \\sect \\page \n", code_id); 
      strcpy(myline,"!!!"); 
      pprint(myline);      
    } 
    if (chapter_number > 1) {  
      sprintf (myline,"\\sect \\page\n"); 
      pprint(myline); 
    } 
    sprintf (myline, "{\\header {\\scaps \\i %d.%s }} \\par  ",chapter_number, line);
    pprint(myline);
    strcpy(myline,"!!!"); 
    sprintf (myline, "\\par \\par  \n{ {\\v {\\tc\\tcl1\\pard \\fs20\\plain \\qj \\b\\fs36 %d %s}}}\\par \\fs20 \\par \\qj ",chapter_number, line);
    pprint(myline);
    strcpy(myline,"!!!"); 
    pprint(myline);
    strcpy(myline,""); 
    break; 
  case HTML: 
    fprintf (jam_all,"<LI> <a href=%s%s>%s</a>\n",JAMPATH,jam_out,line); 
    sprintf (myline, "<title>JAM: %s</title><H1>%s</H1><p>\n",title, line);
    fprintf (jam_cts,"<HR><a href=%s%s><H2>%d. %s</H2></a>\n",
	     JAMPATH,jam_out,chapter_number,line); 
    pprint(myline);
    strcpy(myline,"!!!"); 
    pprint(myline);
    sprintf (myline,"<!--this file was generated by %s-->", code_id); 
    pprint(myline);
    strcpy(myline, ""); 
    break; 
  default:
    break; 
  } 
    
  pprint(myline); 
  status =1; 
  return(status); 
} 
/*---------------------------------------------------------------------*/

int makeasection(char *line)
{ 
  int status; 
  int i; 
  char* pos; 
  char myline[MAXLINE]; 

  i=0; 
  pos = line; 
  while (line[i] != '\n') { 
    i++; 
    pos++; 
  } 
  *pos = '\0'; 
  
  section_number++; 
  subsection_number=0; 
  
  switch (modus){ 
  case LATEX: 
    sprintf (myline, "\n\\section{%s}\n", line);
    break; 
  case RTF: 
    sprintf (myline, "\n\\par  \\par  \n{\\v {\\tc\\tcl2\\pard \\fs20\\plain \\qj \\fs28 \\b %d.%d %s}}\\par \\fs20 \\par \\qj \n",chapter_number,section_number, line);
    break; 
  case HTML: 
    sprintf (myline, "\n<p><HR><H2>%s</H2><p>\n", line);
    fprintf (jam_cts,"<H4>%d.%d. %s</H4>\n",chapter_number,section_number,line); 
    break; 
  default:
    break; 
  } 
  pprint(myline); 
  status =1; 
  return(status); 
} 
/*---------------------------------------------------------------------*/

int makeasubsection(char *line)
{ 
  int status; 
  int i; 
  char* pos; 
  char myline[MAXLINE]; 

  i=0; 
  pos = line; 
  while (line[i] != '\n') { 
    i++; 
    pos++; 
  } 
  *pos = '\0'; 
  
  subsection_number++; 
  
  switch (modus){ 
  case LATEX: 
    sprintf (myline, "\n\\subsection{%s}\n", line);
    break; 
  case RTF: 
    sprintf (myline, "\n\\par  \\par  \n{\\v {\\tc\\tcl3\\pard \\fs20\\plain \\qj \\ul \\fs24  %d.%d.%d %s }}\n \\fs20 \\par \\par \\par \\qj\n",chapter_number,section_number,subsection_number, line);
    break; 
  case HTML: 
    sprintf (myline, "\n <H3>%s</H3><p>\n", line);
    fprintf (jam_cts,"<H5>%d.%d.%d. %s</H5>\n",
	     chapter_number,section_number,subsection_number,line); 
    break; 
  default:
    break; 
  } 
  pprint(myline); 
  status =1; 
  return(status); 
} 

/*---------------------------------------------------------------------*/

int process_verbatim(void)
{ 
  int status; 
  int i; 
  char myline[MAXLINE]; 
  
  switch (modus){ 
  case LATEX: 
    if (!verbatim){ 
      sprintf (myline,"!!!"); 
      pprint(myline); 
      sprintf (myline, "\n\\begin{verbatim}\n");
      verbatim = TRUE; 
      did_a_print = FALSE; 
    } else { 
      if (modus== LATEX) { 
	if (did_a_print < 2) { 
	  sprintf(myline,"%80s\n",".");
	  pprint(myline); 
	} 
      } 
      /* sprintf (myline,"!!!");
      pprint(myline); */ 
      sprintf (myline, "\\end{verbatim}");
      pprint(myline); 
      sprintf (myline,"!!!");
      did_a_print=FALSE; 
      last_verbatim=line_in_input[current_jam]; 
      verbatim = FALSE; 
    } 
    break; 
  case HTML: 
    if (!verbatim){ 
      sprintf (myline, "\n <PRE>\n");
      verbatim = TRUE; 
    } else { 
      sprintf (myline, "\n </PRE>\n");
      verbatim = FALSE; 
    } 
    break; 
 case RTF: 
    if (!verbatim){ 
      sprintf (myline, "{\\par  \\plain  \\f26 \\fs20\n \\ql " );
      verbatim = TRUE; 
    } else { 
      sprintf (myline, "\\par \\pard }\\fs20 \\qj \n");
      verbatim = FALSE; 
    } 
    break; 

  default:
    break; 
  } 
  pprint (myline); 
  status =1; 
  return(status); 
} 
/*---------------------------------------------------------------------*/

int process_special(void)
{ 
  int status; 
  int i; 
  char myline[MAXLINE]; 

  if (special_on == TRUE) special_on=FALSE; 
  	else 
  		special_on=TRUE; 
  switch (modus){ 
  case LATEX: 
    sprintf (myline,"!!!"); 
    pprint (myline); 
    if (!special){ 
      if (last_verbatim+1 == line_in_input[current_jam]) { 
	sprintf (myline, " \\mbox{}\\\\{\\em \n");
      } else { 
	sprintf (myline, "\n\n \\mbox{}\\\\ { \\em \n");
      } 
      special = TRUE; 
    } else { 
      sprintf (myline, "\n\n\\mbox{}\\\\}\n");
      special = FALSE; 
      last_verbatim=line_in_input[current_jam]; 
    } 
    pprint (myline); 
    sprintf (myline,"!!!");   
    break; 
  case HTML: 
    if (!special){ 
      sprintf (myline, "<p><em>\n");
      special = TRUE; 
    } else { 
      sprintf (myline, "</em><p>\n");
      special = FALSE; 
    } 
    break; 
  case RTF: 
    if (!special){ 
      sprintf (myline, "{\\par \\par \\fs24 \\i \n");
      special = TRUE; 
    } else { 
      sprintf (myline, "} \\par \\par \\fs20 \n");
      special = FALSE; 
    } 
    break; 
  default:
    break; 
  } 
  pprint(myline); 
  status =1; 
  return(status); 
} 
/*---------------------------------------------------------------------*/

int process_item(void)
{ 
  int status; 
  int i; 
  char myline[MAXLINE]; 
  
  switch (modus){ 
  case LATEX: 
      sprintf (myline,"!!!");  
      pprint(myline); 
    if (!item){ 
      sprintf (myline, "\n\\begin{itemize}\n");
      item = TRUE; 
    } else { 
      sprintf (myline, "\n\\end{itemize}\n");
      item = FALSE; 
    } 
      pprint(myline);
      sprintf (myline,"!!!");  
    break; 
  case HTML: 
    if (!item){ 
      sprintf (myline, "\n<UL>");
      item = TRUE; 
    } else { 
      sprintf (myline, "\n</UL><p>");
      item = FALSE; 
    } 
    break; 
 case RTF: 
    if (!item){ 
      sprintf (myline, "\n\\par \\par \\par \n \\pard \\fs20 \\ql \\fi-300 \\li300\n");
      item = TRUE; 
    } else { 
      sprintf (myline, "\n\\pard \\qj \\fs20 \n\\par \\par \\par ");
      item = FALSE; 
    } 
    break; 
  default:
    break; 
  } 
  pprint(myline); 
  status =1; 
  return(status); 
} 
/*---------------------------------------------------------------------*/

int process_anitem(char* current)
{ 
  int status; 
  int i; 
  char *pos; 
  char myline[MAXLINE]; 

  
  switch (modus){ 
  case LATEX: 
     current[0] = ' ';
     sprintf (myline,"!!!"); 
     pprint(myline); 
     sprintf (item_prefix, "\n \\item");
    break; 
  case HTML: 
    current[0] = ' '; 
    sprintf (item_prefix, "<p><LI>");
    break; 
 case RTF: 
    current[0] = ' '; 
    sprintf(item_prefix, "\\par  \\pard \\fs20 \\ql \\fi-300 \\li300 \\bullet");
    break; 
  default:
    break; 
  } 
  status =1; 
  return(status); 
} 

 
/*---------------------------------------------------------------------*/
int clean_latex(const i, char* interim_string, char* interim_rentline, 
		int *interim_pointer,char* linkbody)

{  
  int ii; 
  ii = *interim_pointer; 
  if (strcmp(linkbody,"NULL") == 0)  { 
    if (interim_string[i] == '%') { 
      interim_string[i] = ' ';
      /* VMS_STATUS link type -> index */ 
      if (interim_string[i+1] == '-') { 
	if (vms_status) interim_string[i+1] = '/';
      }
    } 
  } 
  if (interim_string[i] == '$') { 
    interim_rentline[ii] = '\\';
    ii++; 
  } 
  if (interim_string[i] == '_') { 
    interim_rentline[ii] = '\\';
    ii++; 
  } 
  if (interim_string[i] == '{') { 
    interim_rentline[ii] = '\\';
    ii++; 
  } 
  if (interim_string[i] == '}') { 
    interim_rentline[ii] = '\\';
    ii++; 
  } 
  
  if (interim_string[i] == '#') { 
    interim_rentline[ii] = '\\';
    ii++; 
  } 
  if (interim_string[i] == '%') { 
    interim_rentline[ii] = '\\';
    ii++; 
  } 
  if (interim_string[i] == '&') { 
    interim_rentline[ii] = '\\';
    ii++; 
  } 
  if (interim_string[i] == '^') { 
    interim_rentline[ii] = '\\';
    ii++; 
  } 
  if (interim_string[i] == '>') { 
    interim_rentline[ii] = '{';
    interim_rentline[ii+1] = '\\';
    interim_rentline[ii+2] = 't';
    interim_rentline[ii+3] = 't';
    interim_rentline[ii+4] = '>';
    
    interim_string[i] = '}';
    ii= ii+5;
  } 
  if (interim_string[i] == '~') { 
    interim_rentline[ii] = '\\';
    interim_rentline[ii+1] = '~';
    interim_rentline[ii+2] = '{';
    interim_rentline[ii+3] = ' ' ;
    
    interim_string[i] = '}';
    ii= ii+4;
  } 
  if (interim_string[i] == '<') { 
    interim_rentline[ii] = '{';
    interim_rentline[ii+1] = '\\';
    interim_rentline[ii+2] = 't';
    interim_rentline[ii+3] = 't';
    interim_rentline[ii+4] = '<';
    interim_string[i] = '}';
    ii= ii+5;
  } 
  *interim_pointer = ii; 
} 
    
    

/*---------------------------------------------------------------------*/

pprint(char*  myline)
{ 
  static int done,printlinepos; 
  int counter,i,needtoflag;
  static char printline[MAXLINE]; 
  int already_done = FALSE; 

  /* done is a static variable to hold the last position of a \n */ 
  counter=0; 
  i=0; 
  /* remove duplicate line feeds */ 
  while ( i < strlen(myline)) 
  { 
    if ((myline[i] != '\n') && (myline[i] != ' ')) counter++; 
    if (myline[i] == '\n'){
      if (counter == 0 ) { 
        myline[i] =' '; 
      }else{  
        /* nothing, yet */  
      } 
    } 
    
    /* add mode-specific line feeds */ 
    
    
    /* myline[0],[1],[2] are '!!!' to 'flush' the line */ 
    
    if ( (printlinepos >= (MAXWIDTH-MARGIN) ) || (strncmp(myline,"!!!",3) ==0)  ) { 
      if (strncmp(myline,"!!!",3) ==0){ 
        strcpy(myline,"");
        needtoflag = FALSE; 
        printline[printlinepos]=0; 
        if (strlen(printline) > 0 ) { 
          if (jam_op != NULL ) { 
            fprintf (jam_op, "%s \n", printline);
            if (modus == LATEX) did_a_print++; 
	    if ( (modus == RTF) && (verbatim))   fprintf (jam_op, "\\par  "); 
          } 
        } 
        printlinepos=0; 
        counter=0; 
        
      } else 
        needtoflag=TRUE;
    } 
    else
      needtoflag=FALSE; 

    if (!verbatim && ( (myline[i] == ' ')  && ( needtoflag == TRUE) ) ) { 
      /* need to print */ 
      needtoflag = FALSE; 
      printline[printlinepos]=0; 
      if (jam_op != NULL ) { 
        fprintf (jam_op, "%s \n", printline);    
        
      } else { 
        Message (MSG_ERROR,"ERROR: JAM needs 'chapters' to work with \n"); 
        DEALEXIT
          exit (-1); 
      } 
      printlinepos=0; 
      counter=0; 
    } 
    if (counter >0 ) {
      if ( verbatim && (myline[i] == '\n')){ 
        printline[printlinepos]=0; 
        fprintf (jam_op, "%s ", printline);      
	if ( (modus == RTF) && (verbatim))   fprintf (jam_op, "\\par  "); 
        printlinepos=0;
      } 
      switch (modus) { 
      case LATEX:
        if (already_done == 0 ) { 
          did_a_print++; 
          already_done = TRUE; 
        } /* empty verbatim lines hurt, therefore we count that we printed */ 
        break; 
      case HTML:
        if (!verbatim) 
          if ( ( (myline[i] == '<' ) && 
              (myline[i+1] == 'p' ) ) && 
            (myline[i+2] == '>' )  ) { 
            printline[printlinepos]=0;
            if (strlen(printline) > 0)  { 
              fprintf (jam_op, "%s \n", printline);
            } else { 
              if (jam_op != NULL ) fprintf (jam_op, "<p>\n"); 
            } 
            printlinepos=0; 
          } 
      default: 
        break; 
      } 
      
      
      if (!verbatim) 
      if ((myline[i] == ' ') && (printline[printlinepos-1] == ' '))  { 
        /* do nothing - don't accumulate spaces  */
      } else { 
        if (myline[i] == '\n')  { 
          /* don't do line feeds either */ 
        } else { 
          if ( i < strlen(myline) ) { /* we added up i in case of \n */ 
            printline[printlinepos] = myline[i]; 
            printlinepos++;
          } 
        } 
      } 
    else { 
      printline[printlinepos] = myline[i]; 
      printlinepos++; 
    }
    } 
    i++;
  } 
  
  
  
} 




