/**********************************************************************
 * util.c  -- misc useful stuff
 * tty lucy client using SS library
 *
 * $Author: brlewis $
 * $Source: /afs/athena.mit.edu/astaff/project/lucydev/src/tty/util.c,v $
 * $Header: /afs/athena.mit.edu/astaff/project/lucydev/src/tty/util.c,v 1.1 91/09/24 14:48:46 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_util_c[] = "$Header: /afs/athena.mit.edu/astaff/project/lucydev/src/tty/util.c,v 1.1 91/09/24 14:48:46 brlewis Exp Locker: brlewis $";
#endif /* lint */

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

#if !defined(TRUE)
#define TRUE (0==0)
#define FALSE (0==1)
#endif

/**********************************************************************
 * say_yes(question, d)
 *    char *question
 *    int d;  [default TRUE or FALSE]
 *
 *   + prints question
 *   + gets yes or no answer
 *
 * Returns:
 *   + TRUE if yes
 *   + FALSE if no
 **********************************************************************/

say_yes(question, d)

     char *question;
     int d;
{
  char answer[512];

  printf("%s (y/n, default: %c) ", question, d ? 'y' : 'n');
  switch(*(fgets(answer, 15, stdin))) {
  case 'y':
  case 'Y':
    return(TRUE);
  case 'n':
  case 'N':
    return(FALSE);
  default:
    return(d);
  }
}

/**********************************************************************
 * promptfor(prompt, s, n)
 *    char *prompt, *s
 *    int n;
 *
 *   + prints "prompt"
 *   + puts answer in s, up to n-1 characters
 **********************************************************************/

void
promptfor(prompt, s, n)

     char *prompt, *s;
     int n;
{
  register char *s1;

  printf("%s", prompt);
  if (*(fgets(s, n, stdin)) == '\0') return;
  for (s1 = s; isspace(*s1); s1++);
  if (s1 != s) strcpy(s, s1); /* remove leading blanks */
  while (*s1 != '\0') s1++;
  while (isspace(*(--s1))) *s1 = '\0'; /* remove trailing blanks */
  return;
}

/************************************************************************
 * list_trn(ltrn *t)
 *	caller puts transaction info in t
 *
 * - returns static char *(summary line of essential information)
 *     nn FAPR Jul 26  subject
 ************************************************************************/

char *
list_trn(t)
     ltrn *t;
{
  static char line[512];

  sprintf(line, "%4d %s %6.6s  %-61.61s", t->current,
	  luc_flgstr(t->flags), (ctime((time_t *) &(t->date_entered))+4),
	  t->subject);
  return(line);
}

/**********************************************************************
 * lower_bound(char *range)
 * upper_bound(char *range)
 *	caller sets range to string like "50" or "50-100"
 *
 * - returns int bound
 **********************************************************************/

#if defined(__GNUC__)
inline
#endif
lower_bound(range)
     char *range;
{
  return(atoi(range));
}

#if defined(__GNUC__)
inline
#endif
upper_bound(range)
     char *range;
{
  register char *s;

  if (s=index(range, '-')) return(atoi(s+1));
  return(atoi(range));
}

/**********************************************************************
 * audit(FILE *fp)
 *	- caller opens fp for reading
 *
 * -    prints each line of input
 * - ==>marks questionable lines with an arrow
 * - ##########################################
 * - # Gives an explanation of why each questionable line is wrong
 * - closes fp
 * - returns number of questionable lines
 **********************************************************************/

audit(fp)
     FILE *fp;
{
#define NPROBS (4)		/* number of problem lines to check */
  static char *probs[] =
    { "From:", "A From: field may give away someone's email address.",
      "Subject:", "The subject line shouldn't be part of the text.",
      LUCY_REPLY, "Take out the line about PERSONAL REPLY.",
      LUCY_POST, "Take out the line about the BROWSER." };
  int nfound[NPROBS];		/* number of problems found */
  int found;
  int total=0;
  int i, j;
  char buf[BUFSIZ+1];
  char afname[512], call[512];
  FILE *afp;

  sprintf(afname, "%s/auditXXXXXX", luv_topdir);
  if (!(afp = fopen(afname, "w"))) return(-1);

  /* initialize array of problems found */
  for(i=0; i<NPROBS; i++) nfound[i] = 0;

  /* print each line, be it problem or otherwise */
  while(fgets(buf, BUFSIZ, fp)) {
    found=0;
    for(i=0; i<8 && buf[i] != '\0'; i++) { /* check first 8 chars */
      for(j=0; j < NPROBS; j++) {
	if (strncmp(&(buf[i]), probs[2*j], strlen(probs[2*j])) == 0) {
	  nfound[j]++;
	  total++;
	  found++;
	  break;
	}
      }
      if (found) break;
    }
    if (found) fprintf(afp, "==>%s", buf);
    else fprintf(afp, "   %s", buf);
  }

  if (total) {
    for(i=0; i<72; i++) putc('#', afp);
    for(i=0; i<NPROBS; i++)
      if (nfound[i]) fprintf(afp, "\n# %s", probs[2*i+1]);
    putc('\n', afp);
    for(i=0; i<72; i++) putc('#', afp);
    putc('\n', afp);
  }
  fclose(afp);
  fclose(fp);
  sprintf(call, "MORE=d more %s", afname);
  system(call);
  return(total);
}
