/**********************************************************************
 * luc_section.c -- write a report section
 *
 * $Author: brlewis $
 * $Source: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_section.c,v $
 * $Header: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_section.c,v 1.1 91/09/24 14:37:27 brlewis Exp Locker: brlewis $
 *
 * Copyright 1991 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 **********************************************************************/
#include <mit-copyright.h>

#ifndef lint
static char rcsid_luc_section_c[] = "$Header: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_section.c,v 1.1 91/09/24 14:37:27 brlewis Exp Locker: brlewis $";
#endif /* lint */

#include <stdio.h>
#include <sys/types.h>
#include <strings.h>
#include <lucy/lucy.h>

/**********************************************************************
 * luc_section(LMEETING *mp, int n, FILE *fp)
 *
 * - outputs section for latex report on stream fp
 **********************************************************************/

long
luc_section(mp, n, fp)
     LMEETING *mp;
     int n;
     FILE *fp;
{
  long code;
  int i;
  ltrn *t;
  char *fname;

  for(i=n ; i ; i = t->nref) {
    /* get new info and write subsection heading */
    if (code = luc_trn_info(mp, i, &t)) return(code);
    if (i == n) {
      /* print section heading */
      fprintf(fp, "%s\n%s%s  %s%s\n",
	      "\\newpage",
	      "\\section{", luc_flgstr(t->flags),
	      luc_latex_protect(t->subject), "}");
    } else {
      fprintf(fp, "%s%s  %s%s\n",
	      "\\subsection{", luc_flgstr(t->flags),
	      luc_latex_protect(t->subject), "}");
    }
    /* print top half of information box */
    fprintf(fp, "%s\n[%04d]%s%s%s\n",
	    "\\begin{tabular}{lr}\\hline",
	    i, "& ``",
	    luc_latex_protect(t->signature),"''\\\\");
    /* bottom half of information box */
    fprintf(fp, "%s%s%s%s\n%s\n\n",
	    ctime((time_t *) &(t->date_entered)),"& ",
	    luc_latex_protect(t->author),"\\\\\\hline",
	    "\\end{tabular}");
    /* text of transaction */
    if (t->num_chars) {
      fprintf(fp, "\\begin{verbatim}\n");
      if (code = luc_trn_file(mp, i, &fname)) return(code);
      fflush(fp);
      if (code = luc_insert_file(fp, fname)) {
	strcpy(luv_context, fname);
	luc_free_file(&fname);
	return(code);
      }
      luc_free_file(&fname);
      fprintf(fp, "\\end{verbatim}\n\n");
    }
  }
  return(0L);
}


/************************************************************************
 * char *luc_latex_protect(char *orig)
 *
 * - returns static char *, overwritten with every call
 * - copies orig, protecting special latex characters
 ************************************************************************/

char *
luc_latex_protect(orig)
     register char *orig;
{
  static char new[256];
  register char *s;

  s = new;

  /* handle NULL pointer correctly */
  if (!orig) {
    *s = '\0';
    return(s);
  }

  do {
    /* omit illegal characters */
    if (*orig < ' ' && *orig && !index("\n\t", *orig)) continue;
    /* precede special latex characters with a backslash */
    if (*orig && index("~#$%^&{}_\\", *orig)) *s++ = '\\';
    *s++ = *orig;
  } while(*orig++);
  return(new);
}
