
/**********************************************************************
*
*  projet   : DilibSgml
*  module   : SgmlPath
*  commande : SgmlSetAtt
*  fichier  : SgmlSetAtt.c
*  Auteur   : Guy Achard
*  Date     : Avril 99
*
***********************************************************************
*
* Copyright (c) 1999  LORIA
* 
***********************************************************************/
#include "SgmlPath.h"

extern char *optarg;   
extern int   optind;
void usage()
{
  fprintf (stderr, "usage: SgmlSetAtt [-t tag -p path] -a attribute  -v value \n");
    exit(1);
}
  
SgmlNode *treeSetAtt(n,attrib,tag,value)
     SgmlNode *n;
     char *attrib;
     char *tag;
     char *value;     
{
  SgmlNode *son;
  if(!n) return NULL;
  if(SgmlIsMark(n))
    {
      if(strcmp(SgmlTag(n),tag)==0)SgmlSetAtt(n,attrib,value);
      if((son=SgmlFirst(n)))
	{
	  do
	    {
	      treeSetAtt(son ,attrib,tag,value);
	    }
	  while ((son=SgmlNext(son))); 
	} 
    }
  return son;
}   


int main(argc,argv)
     int argc;
     char **argv;
{
  int cod_arg;
  SgmlPathList *listPath;
  SgmlNode *doc;
  char *attrib=NULL;
  char *tag=NULL;
  char *value=NULL;

  listPath=NULL;
  
  while((cod_arg=getopt(argc,argv,"a:p:t:v:"))!=EOF)
    {
      switch(cod_arg)
	{
	case 'a':
	  attrib=optarg;
	  break;
	case 'p':
	  if(!listPath)listPath=SgmlPathList_Create();
	  SgmlPathList_GetOpt(listPath,argc,argv);
	  break;
	case 't':
	  tag=optarg;
	  break;
	case 'v':
	  value=optarg; 
	  break;
	  
	default: usage();
	}
    }
  while((doc=SgmlInputNextRecord()))
    {
      if (tag)
	{
	  treeSetAtt(doc, attrib,tag,value);
	}
      else
	{
	  SgmlNode *n1;
	  SgmlPathList_Init(listPath,doc);
	  while((n1=SgmlPathList_Next(listPath)))
	    {
	       if(SgmlIsMark(n1))SgmlSetAtt(n1,attrib, value);
	    }
	}
      SgmlPrint(doc);
    }    
  exit(0);
  return 0;
}
