/* comment.c */
/*
Copyright (C) 1986 Rahul Dhesi -- All rights reserved
*/

#include "options.h"
#include "portable.h"
/* comment() */
/* Updates comments */

/* buffer size for any one comment line */
#define  COMMENT_LINE_SIZE 77

#define  MAX_COMMENT_SIZE  65535
#include <stdio.h>
#include "various.h"

#ifndef NOSIGNAL
#include <signal.h>
#endif

#include "zoo.h"
#include "zoofns.h"
#include "errors.i"

#ifdef LINT_ARGS
void show_comment (struct direntry *, FILE *, int, char *);
get_comment (struct direntry *, FILE *, char *);
#else
void show_comment ();
get_comment ();
#endif

void comment(zoo_path, option)
char *zoo_path, *option;
{
#ifndef NOSIGNAL  
int (*oldsignal)();
#endif
FILE *zoo_file;                           /* stream for open archive */
long next_ptr;                            /* pointers to within archive */
long this_dir_offset;                     /* pointers to within archive */
struct direntry direntry;                 /* directory entry */
struct zoo_header zoo_header;
int matched = 0;                          /* any files matched? */
unsigned int zoo_date, zoo_time;          /* for restoring archive timestamp */

/* on entry option points to first letter */
++option;
if (*option != '\0')
   prterror ('f', inv_option, *option);

if ((zoo_file = fopen (zoo_path, FRWSTR)) == NULL)
   prterror ('f', could_not_open, zoo_path);

/* save archive timestamp */
#ifdef GETUTIME
getutime (zoo_path, &zoo_date, &zoo_time);
#else
gettime (fileno(zoo_file), &zoo_date, &zoo_time);
#endif

/* read header and rewrite with updated version numbers */
rwheader (&zoo_header, zoo_file);

/* Loop through and add comments for matching files */
while (1) {
   this_dir_offset = ftell (zoo_file);    /* save pos'n of this dir entry */
   readdir (&direntry, zoo_file, 1);      /* read directory entry */
   next_ptr = direntry.next;              /* ptr to next dir entry */

   /* exit on end of directory chain or end of file */
   if (next_ptr == 0L || feof(stdin))
      break;

   /* add comments for matching non-deleted files */
   if (!direntry.deleted && needed (direntry.fname)) {
      matched++;
      show_comment (&direntry, zoo_file, 1, direntry.fname);
      get_comment (&direntry, zoo_file, direntry.fname);
      fseek (zoo_file, this_dir_offset, 0);
#ifndef NOSIGNAL
      oldsignal = signal (SIGINT, SIG_IGN);
#endif
      fwr_dir (&direntry, zoo_file);
      /* fwrite ((char *) &direntry, sizeof(direntry), 1, zoo_file); */
#ifndef NOSIGNAL
      signal (SIGINT, oldsignal);
#endif
   }
   fseek (zoo_file, next_ptr, 0);   /* ..seek to next dir entry */
} /* end while */

#ifdef NIXTIME
fclose (zoo_file);
setutime (zoo_path, zoo_date, zoo_time); /* restore timestamp */
#else
settime (fileno(zoo_file), zoo_date, zoo_time); /* restore timestamp */
fclose (zoo_file);
#endif

if (!matched)
   printf ("Zoo:  %s", no_match);
} /* comment */

/* show_comment() */
/* shows comment on screen.  If show=1, says "Current comment is..." */

void show_comment (direntry, zoo_file, show, name)
struct direntry *direntry;
FILE *zoo_file;
int show;
char *name;       /* name of file for which comment is being added */
{
   if (direntry->cmt_size != 0) {
      unsigned int i;
      char ch;
      int newline = 1;
      fseek (zoo_file, direntry->comment, 0);   
      if (show)
         printf ("Current comment for %s is:\n", name);
      for (i = 0; i < direntry->cmt_size; i++) {/* show it */
         ch = fgetc (zoo_file) & 0x7f;          /* 7 bits only */
         if (newline)
            printf (" |");    /* indent and mark comment lines thus */
         fputchar (ch);
         if (ch == '\n')
            newline = 1;
         else
            newline = 0;
      }
      if (!newline)              /* always terminate with newline */
         fputchar ('\n');
   }
} /* show_comment() */


/* get_comment() */
/* Shows user old comment and updates it */

/* INPUT:
   direntry points to current directory entry.
   zoo_file is archive file.
   this_path is full pathname of file being updated/added.

   OUTPUT:
   Comment is added to file and supplied directory entry is updated
   with comment size and seek position but directory entry is
   not written to file.  Exceptions:  If RETURN is hit as first line,
   previous comment is left unchanged.  If /END is hit, previous
   comment is superseded, even if new comment is null.
*/

get_comment (direntry, zoo_file, this_path)  /* update comment */
register struct direntry *direntry;
FILE *zoo_file;
char *this_path;
{
   unsigned int line_count = 0;        /* count of new comment lines */

   fseek (zoo_file, 0L, 2);            /* ready to append new comment */
   fprintf (stderr, "[Enter comment for %s then type /END]\n", this_path);
   while (1) {
      char cmt_line[COMMENT_LINE_SIZE];
      int cmt_size;
      if (fgets (cmt_line, sizeof(cmt_line), stdin) == NULL)
         break;
      line_count++;
      if (line_count == 1) {                 /* first line typed */
         if (!strcmp (cmt_line, "\n"))   /* exit if first line blank */
            break;
         direntry->comment = ftell (zoo_file);
         direntry->cmt_size = 0;
      }
      if (!strcmpi (cmt_line, "/end\n"))
         break;
      cmt_size = strlen (cmt_line);
      if (MAX_COMMENT_SIZE - direntry->cmt_size > cmt_size) {
         direntry->cmt_size += (unsigned int) cmt_size;
         if (fwrite (cmt_line, cmt_size, 1, zoo_file) < 1)
            prterror ('f', disk_full);
      }
   } /* end while */
} /* get_comment() */

