/* Time-stamp: <96/12/20 15:10:10 john> */

/*
# Purpose: This implements substitutions, as described in <a href="../doc/tech/getplan-args.html#extra">getplan argument documentation</a>.
   It is used for the getplan arguments:
   extra
   special
   special_page_start
   special_page_end
 */

#include <stdio.h>
#include "attr.h"
#include "style.h"
#include "substitution.h"

#define MAX_NAME 1024

void output_with_substitutions(char *text,
			       struct entry *this,
			       struct entry *top,
			       char *attr_file_type,
			       char* attr_file_name,
			       struct style *style,
			       char *controls)
{
  char *s;
  char c;
  int out = 0;

  char sub_start = controls[0];
  char sub_end = controls[1];

  for (s = text;
       (!out) && ((c = *s) != '\0');
       s++)
    {
      if (c == '\\')
	{
	  continue;
	} else if (c == sub_end)
	  {
	    out = 1;		/* go out a level of nesting */
	  } else if (c == sub_start)
	    {
	      int keyi = 0;
	      int keyc;
	      int done = 0;
	      int has_default = 0;
	      char key[MAX_NAME];
	      char *value;
	    
	      for (;!done;)
		{
		  keyc = *++s;
		  switch (keyc)
		    {
		    case '/':
		    case '\0':
		    case '}':
		      done = 1;
		      break;
		    default:
		      key[keyi++] = keyc;
		      if (keyi >= MAX_NAME) done = 1;
		      break;
		    }
		}
	      key[keyi] = '\0';
	      has_default = (keyc == '/');

	      if (key[0] == '!')
		{
		  if (strcmp(key+1, "date")==0)
		    {
		      print_time();
		    } else if (strcmp(key+1, "pagefile")==0)
		      {
			printf("%s", attr_file_name);
		      } else if (strcmp(key+1, "pagetype")==0)
			{
			  printf("%s", attr_file_type);
			} else if (strcmp(key+1, "pagename")==0)
			  {
			    printf("%s", top->name);
			  } else if (strcmp(key+1, "browser")==0)
			    {
			      char *browser = (char*)getenv("HTTP_USER_AGENT");
			      if (browser == NULL)
				{
				  printf("unknown browser");
				} else {
				  printf("%s", browser);
				}
			    } else if (strcmp(key+1, "browserfeatures")==0)
			      {
				int browser_features = style->browser_features;
				if (browser_features & BROWSER_HAS_ALIGN) printf("align ");
				if (browser_features & BROWSER_JUMP_IN_TABLES) printf("jump_in_tables ");
				if (browser_features & BROWSER_ANTI_ALIAS) printf("anti_alias ");
				if (browser_features & BROWSER_GRAPHICAL) printf("graphics ");
				if (browser_features & BROWSER_CELL_COLOURS) printf("cell_colours ");
			      } else {
				printf("Unknown built-in substitution %s", key+1);
			      }
		} else {

		  value = get_attr(this, key);

		  if (value == NULL)
		    {
		      value = get_attr(top, key);
		    }

		  if (value != NULL)
		    {
		      output_with_substitutions(value,
						this, top,
						attr_file_type, attr_file_name,
						style, "{}");
		    } else {
		      if (has_default)
			{
			  output_with_substitutions(s,
						    this, top,
						    attr_file_type, attr_file_name,
						    style, "{}");
			}
		    }
		}
	    } else {
	      putchar(c);
	    }
    }
}


/* end of substitution.c */
