/* ------------------------------------------------------------------------
@NAME       : post_parse.c
@DESCRIPTION: Operations applied to the AST (or strings in it) after 
              parsing is complete.
@GLOBALS    : 
@CALLS      : 
@CREATED    : 1997/01/12, Greg Ward (from code in bibparse.c, lex_auxiliary.c)
@MODIFIED   : 
@VERSION    : $Id: post_parse.c,v 1.17 1997/09/10 02:02:07 greg Exp $
@COPYRIGHT  : Copyright (c) 1996-97 by Gregory P. Ward.  All rights reserved.

              This file is part of the btparse library.  This library is
              free software; you can redistribute it and/or modify it under
              the terms of the GNU Library General Public License as
              published by the Free Software Foundation; either version 2
              of the License, or (at your option) any later version.
-------------------------------------------------------------------------- */
#include "bt_config.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "btparse.h"
#include "error.h"
#include "parse_auxiliary.h"
#include "prototypes.h"
#include "my_dmalloc.h"

#define DEBUG 1


/* ------------------------------------------------------------------------
@NAME       : postprocess_string ()
@INPUT      : s
              collapse_whitespace
              delete_quotes
              convert_quotes
@OUTPUT     : s (modified in place according to the flags)
@RETURNS    : (void)
@DESCRIPTION: Make a pass over string s (which is modified in-place) to
              do some subset of the following operations:
                 * collapse whitespace according to BibTeX rules
                 * delete quotation characters
                 * convert all quotes to curly braces

              (All are optional, and controlled by the obviously-named flag
              parameters.)

              Rules for collapsing whitespace are:
                 * whitespace at beginning/end of string (just inside
                   quotes if string is quoted) is deleted
                 * within the string, each whitespace sequence is replaced by
                   a single space

              Note that part of the work is done by the lexer proper,
              namely conversion of tabs and newlines to spaces.

              If the string is quoted (starts/ends with "" or {}) we also
              verify that the opening and closing quote characters match.
@GLOBALS    : 
@CALLS      : 
@CREATED    : originally in lex_auxiliary.c; moved here 1997/01/12
@MODIFIED   : 
-------------------------------------------------------------------------- */
void 
postprocess_string (char *  s,
                    boolean collapse_whitespace,
                    boolean delete_quotes,
                    boolean convert_quotes)
{
   char *i, *j;
   boolean   quoted;                    /* is the string already quoted? */
   int   len;

#if DEBUG > 1
   printf ("postprocess_string: looking at >%s<\n", s);
#endif

   /* First, assert the assumptions about s. */

   len = strlen (s);
   quoted = (s[0] == '"' || s[0] == '{') &&       /* quote char at start... */
            (s[len-1] == '"' || s[len-1] == '}'); /* ...and at the end */


   /* Now check that the quote characters match up */

   if (quoted &&
       ! ((s[0] == '"' && s[len-1] == '"') ||
          (s[0] == '{' && s[len-1] == '}')))
   {
      lexical_error ("token mismatch: string started with %c, " 
                     "but ended with %c",
                     s[0], s[len-1]);
   }

   if (quoted && !delete_quotes && convert_quotes && s[0] == '"')
   {
      s[    0] = '{';
      s[len-1] = '}';
   }


   /*
    * N.B. i and j will both point into s; j is always >= i, and
    * we copy characters from j to i.  Whitespace is collapsed/deleted
    * by advancing j without advancing i.
    *
    * Initial conditions for i and j:
    *   - copy from first `interesting' character (after the opening
    *     quote if quoted, else first character)
    *   - copy to after the opening quote if quoted and we're not deleting
    *     quotes, else copy to the first char
    */

   j = (quoted) ? (s+1) : s;
   i = (quoted && !delete_quotes) ? (s+1) : s;


   /*
    * If we're supposed to collapse whitespace, then advance j to the
    * first non-space character (apart from any opening quote).
    */

   if (collapse_whitespace)
   {
      while (*j == ' ' && *j != (char) 0)
         j++;
   }

   while (*j != (char) 0)
   {

      /*
       * If we're in a string of spaces (ie. current and previous char.
       * are both space), and we're supposed to be collapsing whitespace,
       * then skip until we hit a non-space character (or end of string).
       */

      if (collapse_whitespace && *j == ' ' && *(j-1) == ' ') 
      {
         while (*j == ' ') j++;         /* skip spaces */
         if (*j == (char) 0)            /* reached end of string? */
            break;
      }

      /* Copy the current character from j down to i */

      *(i++) = *(j++);
   }
   *i = (char) 0;                       /* ensure string is terminated */


   /* Now, delete the closing quote (if appropriate) */

   len = strlen (s);
   if (quoted && delete_quotes)
   {
      s[--len] = (char) 0;
   }


   /*
    * And mop up whitespace (if any) preceding closing quote -- note that
    * if there was any whitespace there, it has already been collapsed to
    * exactly one space.
    */

   if (collapse_whitespace && s[len-1] == ' ')
   {
      s[--len] = (char) 0;
   }

#if DEBUG > 1
   printf ("                transformed to >%s<\n", s);
#endif

} /* postprocess_string */


/* ------------------------------------------------------------------------
@NAME       : postprocess_value()
@INPUT      : 
@OUTPUT     : 
@RETURNS    : 
@DESCRIPTION: Post-processes a series of strings (compound value),
              frequently found as the value of a "field = value" or "macro
              = value" assignment.  The actions taken here are governed by
              the bits in 'options', but there are two distinct modes of
              operation: pasting or not.  

              We paste strings if and only if the BTO_PASTE bit in options
              is set and there are two or more simple values in the
              compound value.  In this case, the BTO_EXPAND bit must be set
              (it would be very silly to paste together strings with
              unexpanded macro names!), and we make two passes over the
              data: one to postprocess individual strings and accumulate
              the one big string, and a second to postprocess the big
              string.  In the first pass, the caller-supplied 'options'
              variable is largely ignored; we will always delete quotes and
              never collapse whitespace in the individual strings.  The
              caller's wishes are respected when we make a post-processing
              pass over the concatenation of the individual strings,
              though.

              If we're not pasting strings, then the character of the
              individual simple values will be preserved; macros might not
              be expanded (depending on the BTO_EXPAND bit), numbers will
              stay numbers, and strings will be post-processed
              independently according to the 'options' variable.  (Beware
              -- this means you might collapse whitespace in individual
              sub-strings, which would be bad if you intend to concatenate
              them later in the BibTeX sense.)

              The 'replace' parameter is used to govern whether the
              existing strings in the AST should be replaced with their
              post-processed versions.  This can extend as far as
              collapsing a series of simple values into a single BTAST_STRING
              node, if we paste sub-strings together.  If replace is FALSE,
              the returned string is allocated here, and you must free() it
              later.
@GLOBALS    : 
@CALLS      : 
@CREATED    : 1997/01/10, GPW
@MODIFIED   : 1997/08/25, GPW: renamed from postprocess_field(), and changed
                               to take the head of a list of simple values,
                               rather than the parent of that list
-------------------------------------------------------------------------- */
char *postprocess_value (AST *value, ushort options, boolean replace)
{
   AST *   svalue;                      /* current simple value */
   boolean pasting;
   boolean collapse_whitespace;
   boolean delete_quotes;
   int     tot_len;                     /* total length of pasted string */
   char *  new_string;                  /* in case of string pasting */
   char *  tmp_string;
   boolean free_tmp;                    /* should we free() tmp_string? */

   if (value == NULL) return NULL;

   /* 
    * We will paste strings iff the user wants us to, and there are at least
    * two simple values in the list headed by 'value'.
    */

   pasting = (options & BTO_PASTE) && (value->right);

   /* 
    * If we're to concatenate (paste) sub-strings, we need to know the
    * total length of them.  (The total length includes whitespace and 
    * quotes, even though we might delete quotes.  Better safe than sorry.)
    */

   tot_len = 0;                         /* these are out here to keep */
   new_string = NULL;                   /* gcc -Wall happy */
   tmp_string = NULL;

   if (pasting)
   {
      svalue = value;
      while (svalue)
      {
         switch (svalue->nodetype)
         {
            case BTAST_MACRO:
               tot_len += macro_length (svalue->text);
               break;
            case BTAST_STRING:
               tot_len += (svalue->text) ? (strlen (svalue->text)) : 0;
               break;
            case BTAST_NUMBER:
               tot_len += (svalue->text) ? (strlen (svalue->text)) : 0;
               break;
            default:
               internal_error ("simple value has bad nodetype (%d)",
                               (int) svalue->nodetype);
         }
         svalue = svalue->right;
      }

      /* Now allocate the buffer in which we'll accumulate the whole string */

      tot_len += 2;             /* quotes for the concatenated string */
      new_string = (char *) calloc (tot_len+1, sizeof (char));
      if (! (options & BTO_DELQUOTES))
         new_string[0] = '{';
   }


   /* 
    * Before entering the main loop, figure out just what
    * postprocess_string() is supposed to do -- eg. if pasting strings,
    * we should delete quotes but not (yet) collapse whitespace.
    */

   if (pasting)
   {
      collapse_whitespace = FALSE;
      delete_quotes = TRUE;
   }
   else
   {
      collapse_whitespace = options & BTO_COLLAPSE;
      delete_quotes = options & BTO_DELQUOTES;
   }

   if (pasting && ! (options & BTO_EXPAND))
   {
      internal_error ("can't paste strings without expanding macros");
   }

   /*
    * Now the main loop to process each string, and possibly tack it onto
    * new_string.
    */

   svalue = value;
   while (svalue)
   {
      tmp_string = NULL;
      free_tmp = FALSE;

      if (svalue->nodetype == BTAST_MACRO && (options & BTO_EXPAND))
      {
         tmp_string = macro_text (svalue);
         if (tmp_string != NULL)
         {
            tmp_string = strdup (tmp_string);
            free_tmp = TRUE;
            postprocess_string (tmp_string, 
                                collapse_whitespace,
                                delete_quotes,
                                0);
         }

         if (replace)
         {
            svalue->nodetype = BTAST_STRING;
            if (svalue->text)
               free (svalue->text);
            svalue->text = tmp_string;
            free_tmp = FALSE;           /* mustn't free, it's now in the AST */
         }
      }

      else if (svalue->nodetype == BTAST_STRING && svalue->text)
      {
         if (replace)
         {
            tmp_string = svalue->text;
         }
         else
         {
            tmp_string = strdup (svalue->text);
            free_tmp = TRUE;
         }

         postprocess_string (tmp_string, 
                             collapse_whitespace,
                             delete_quotes,
                             0);
      }

      if (svalue->nodetype == BTAST_NUMBER && svalue->text)
      {
         tmp_string = svalue->text;
      }

      if (pasting)
      {
         if (tmp_string)
            strcat (new_string, tmp_string);
         if (free_tmp)
            free (tmp_string);
      }
      else
      {
         /* 
          * N.B. if tmp_string is NULL (eg. from a single undefined macro)
          * we make a strdup() of the empty string -- this is so we can
          * safely free() the string returned from this function
          * at some future point.
          *
          * This strdup() seems to cause a 1-byte memory leak in some
          * circumstances.  I s'pose I should look into that some rainy
          * afternoon...
          */

         new_string = (tmp_string != NULL) ? tmp_string : strdup ("");
      }

      svalue = svalue->right;
   }

   if (pasting)
   {
      int    len;

      len = strlen (new_string);
      if (! (options & BTO_DELQUOTES))
      {
         new_string[len++] = '}';
         new_string[len] = (char) 0;
      }
      assert (len <= tot_len);          /* hope we alloc'd enough! */

      postprocess_string (new_string,
                          options & BTO_COLLAPSE,
                          options & BTO_DELQUOTES,
                          0);

      /* 
       * If replacing data in the AST, delete all but first child of
       * `field', and replace text for first child with new_string.
       */

      if (replace)
      {
         assert (value->right != NULL); /* there has to be > 1 svalue! */
         zzfree_ast (value->right);     /* free from second simple value on */
         value->right = NULL;           /* remind ourselves they're gone */
         if (value->text)               /* free text of first simple value */
            free (value->text);
         value->text = new_string;      /* and replace it with concatenation */
      }
   }

   return new_string;
   
} /* postprocess_value() */


/* ------------------------------------------------------------------------
@NAME       : postprocess_field()
@INPUT      : 
@OUTPUT     : 
@RETURNS    : 
@DESCRIPTION: Postprocesses all the strings in a single "field = value"
              assignment subtree.  Just checks that 'field' does indeed
              point to an BTAST_FIELD node (presumably the parent of a list
              of simple values), downcases the field name, and calls
              postprocess_value() on the value.
@GLOBALS    : 
@CALLS      : 
@CALLERS    : 
@CREATED    : 1997/08/25, GPW
@MODIFIED   : 
-------------------------------------------------------------------------- */
char *postprocess_field (AST *field, ushort options, boolean replace)
{
   if (field == NULL) return NULL;
   if (field->nodetype != BTAST_FIELD)
      internal_error ("postprocess_field: illegal AST node (non-field)");

   strlwr (field->text);                /* downcase field name */
   return postprocess_value (field->down, options, replace);

} /* postprocess_field() */



/* ------------------------------------------------------------------------
@NAME       : postprocess_entry() 
@INPUT      : 
@OUTPUT     : 
@RETURNS    : 
@DESCRIPTION: Postprocesses all the strings in an entry: collapse whitespace,
              concatenate substrings, expands macros, and whatnot.
@GLOBALS    : 
@CALLS      : 
@CREATED    : 1997/01/10, GPW
@MODIFIED   : 
-------------------------------------------------------------------------- */
void postprocess_entry (AST *top, ushort options)
{
   AST   *cur;
   
   if (top == NULL) return;     /* not even an entry at all! */
   if (top->nodetype != BTAST_ENTRY)
      internal_error ("postprocess_entry: illegal node type (not entry root)");
   strlwr (top->text);          /* downcase entry type */

   if (top->down == NULL) return; /* no children at all */
   
   cur = top->down;
   if (cur->nodetype == BTAST_KEY)
      cur = cur->right;

   switch (top->metatype)
   {
      case BTE_REGULAR:
      case BTE_MACRODEF:
      {
         while (cur)
         {
            postprocess_field (cur, options, TRUE);
            if (top->metatype == BTE_MACRODEF && ! (options & BTO_NOSTORE))
               add_macro (cur, options);

            cur = cur->right;
         }
         break;
      }

      case BTE_COMMENT:
      case BTE_PREAMBLE:
         postprocess_value (cur, options, TRUE);
         break;
      default:
         internal_error ("postprocess_entry: unknown entry metatype (%d)",
                         (int) top->metatype);
   }

} /* postprocess_entry() */
