/*********************************************************************
 *
 *            EZFM---EZWGL based file manager for X11
 *
 *             Copyright (C) 1998  Maorong Zou
 *
 *
 *  This file is part of EZFM.
 *
 *  EZFM is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * EZFM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with EZFM; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *
 *************************************************************************/



#define _RC_C_
/*********************************************************************
 *
 *       Process $EZFMRC or ~/.ezfmrc or resource *ezfmrc
 */
#include "ezfm.h"
/*********************************************************************/
void    ProcessRCFile          MY_ANSIARGS((char *file, char *fname_ret));
/*********************************************************************/
int  nextLine    MY_ANSIARGS((char *buf, int *cnt, FILE *fp));
char *skipSpaces MY_ANSIARGS((char *buf));
int  seekWord    MY_ANSIARGS((char *buf, char **ptr_ret, int *cnt, FILE *fp, char *word));
int  matchWord   MY_ANSIARGS((char *buf, char **ptr_ret, char *word));
int  quoteString MY_ANSIARGS((char *buf, char *word_ret, int *adv_ret));
/*********************************************************************/
static void readRCFile        MY_ANSIARGS((char *fname, FILE *fp));
/*********************************************************************/
EXTERN char *EZ_TildeExpandPath  MY_ANSIARGS((char *path));
/*********************************************************************/
void ProcessRCFile(file, fname_ret) char *file, *fname_ret;
{
  int ret;
  char pathname[1024], tmpname[256], *fname;
  FILE *fp = NULL;
  char *rcf;

  fname=pathname;
  if( (rcf = getenv("EZFMRC")) != NULL)
    {
      if((fp = fopen(rcf, "r")) != NULL)
	strcpy(fname, rcf);
    }
  /*
   * default resource value for file is:
   * ezfmrc:~/.ezfmrc:/etc/ezfmrc:/usr/lib/ezfm/ezfmrc:/usr/local/lib/ezfm/ezfmrc 
   */
  if(file != NULL)
    {
      char *r, *p, *q = strcpy(pathname, file);
      while(q)
	{
	  p = strchr(q, ':');
	  if(p) *p++ = '\0';
	  if( (r = EZ_TildeExpand(q)) != NULL)
	    {
	      strcpy(tmpname, r); fname = tmpname;
	      free(r);
	      if((fp = fopen(fname, "r")) != NULL)
		break;
	    }
	  q = p;
	}    
    }
  
  if(fp == NULL) /* incase ~ expansion failed */
    {
      char *home = getenv("HOME");
      fname=pathname;
      sprintf(fname,  "%s/.ezfmrc", home);
      if((fp = fopen(fname, "r")) == NULL)
	fp = fopen(".ezfmrc", "r");
    }

  if(fp == NULL)
    {
      fprintf(stderr, "No initialization file found !\n");
      return;
    }
  if(fname_ret) strcpy(fname_ret, fname);
  else readRCFile(fname, fp);
  fclose(fp);
}
/*********************************************************************/
int nextLine(buf, cnt, fp)  FILE *fp; char *buf; int *cnt;
{
  int   k, len, set = -1;
  char *ptr;

  for(k=0; fgets(buf, BUFSIZ, fp) != NULL; k++)
    {
      len = strlen(buf);
      if(len <= 1) continue;
      buf[len-1] = '\0'; /* newline */
      ptr = buf;
      while(*ptr && (*ptr == ' ' || *ptr == '\t')) ptr++;
      if(*ptr == '\0' || *ptr == '#') continue;
      else { set = 1;  break; }
    }
  *cnt += k;
  return(set);
}
/*********************************************************************/
char *skipSpaces(buf) char *buf;
{
  char *ptr = buf;
  while(*ptr && (*ptr == ' ' || *ptr == '\t')) ptr++;
  if(*ptr) return(ptr);
  return(NULL);
}
/*********************************************************************/
int seekWord(buf, ptr_ret, cnt, fp, word)
     char *buf, **ptr_ret; int *cnt; FILE *fp; char *word;
{
  char copy[BUFSIZ];
  char *save, *ptr, *dest;
  
  while(1)
    {
      if(nextLine(buf, cnt, fp) > 0)
	{
	  save = ptr = skipSpaces(buf);
	  if(ptr)
	    {
	      dest = copy;
	      while(*ptr && *ptr != ' ' && *ptr != '\t')
		{ *dest++ = tolower(*ptr); ptr++;}
	      *dest = '\0';
	      if(!strncmp(copy, word,strlen(word)))
		{ *ptr_ret = ptr; return(1);}
	    }
	  *ptr_ret = save;
	  return(0);
	}
      else return(-1);
    }
}
/*************************************************************/
int matchWord(buf, ptr_ret, word) 
     char *buf, **ptr_ret; char *word;
{
  char copy[BUFSIZ];
  char *save, *ptr, *dest;

  save = ptr = skipSpaces(buf);
  if(ptr)
    {
      dest = copy;
      while(*ptr && *ptr != ' ' && *ptr != '\t')
	{ *dest++ = tolower(*ptr); ptr++;}
      *dest = '\0';
      if(!strncmp(copy, word, strlen(word)))
	{*ptr_ret = ptr; return(1);}
    }
  *ptr_ret = save;
  return(0);
}
/*************************************************************/
int quoteString(buf, word_ret, adv_ret) 
     char *buf; char *word_ret; int *adv_ret;
{
  char copy[BUFSIZ];
  char *p, *q;
  int cnt = 0;

  p = buf;
  while(*p && (*p == ' ' || *p == '\t')) { cnt++; p++; }
  if(p && *p == '"')
    {
      q = p+1; p++;
      while(*p && *p != '"') p++;
      if(*p)
	{
	  char *r = word_ret;
	  cnt += (p-q) + 2;
	  while(q != p)  *r++ = *q++;
	  *r = '\0';
	  *adv_ret = cnt;
	  return(1);
	}
    }
  *adv_ret = cnt;
  return(0);
}

/**************************************************************************/	  

static void readRCFile(fname, fp) FILE *fp; char *fname;
{
  char line[BUFSIZ], copy[BUFSIZ];
  char *buf, *ptr;
  int  i, status, lineno=1;

  while(1)
    {
      while((status = seekWord(line, &buf, &lineno, fp, "section")) == 0)
	/* null statement */ ; 

      if(status > 0)
	{
	  ptr = skipSpaces(buf);
	  if(!strncmp(ptr, "\"Files\"", 7))
	    {
	      while(1)
		{
		  if( (status = nextLine(line, &lineno, fp)) > 0)
		    {
		      if(matchWord(line,&buf, "magicfile"))
			{
			  char *p = skipSpaces(buf);
			  if(p) ezfmMagicFile = EZ_TildeExpandPath(p);
			}
		      else if(matchWord(line,&buf, "iconpath"))
			{
			  char  *p = skipSpaces(buf);
			  EZ_SetIconPath(p);
			}
		      else if(matchWord(line,&buf, "extensionfile"))
			{
			  char *p = skipSpaces(buf);
			  if(p) ezfmSuffixFile = EZ_TildeExpandPath(p);
			}
		      else if(matchWord(line,&buf, "endsection"))
			break;
		      else fprintf(stderr, "Unknown attributes on line %d: %s\n",lineno, buf);
		    }
		  else break;
		}
	    }
	  else if(!strncmp(ptr, "\"Drives\"",7))
	    {
	      while(1)
		{
		  if( (status = nextLine(line, &lineno, fp)) > 0)
		    {
		      if(matchWord(line,&buf, "endsection"))
			break;
#ifdef REMOVABLE_DRIVES
		      else  addRemovableDevice(buf);
#endif
		    }
		  else break;
		}
	    }
	  else  if(!strncmp(ptr, "\"Tools\"", 7))
	    {
	      char *pageName = NULL, *clr = NULL, pname[128], clrname[128];
	      while(1)
		{
		  if( (status = nextLine(line, &lineno, fp)) > 0)
		    {
		      if(matchWord(line,&buf, "endsection"))
			break;	      
		      else 
			{
			  char *q, *p = skipSpaces(buf);
			  if(p)
			    {
			      if((*p == 'P' || *p == 'p') && !strncmp(p+1, "age", 3) )
				{
				  int adv=0;
				  p += 4;
				  if(quoteString(p, pname, &adv))
				    {
				      pageName = pname;
				      p += adv;
				      if(quoteString(p, clrname, &adv))
					clr = clrname;
				    }
				}
			      else
				{
				  q = p;
				  while(*q && *q != ' ' && *q != '\t') q++;
				  if(*q) { *q = '\0'; q++; }
				  q = skipSpaces(q);
				  if(q) addItemInToolBox(pageName, clr, p, q);
				}
			    }
			}
		    }
		  else break;
		}
	    }
	  else  if(!strncmp(ptr, "\"Commands\"", 7))
	    {
	      while(1)
		{
		  if( (status = nextLine(line, &lineno, fp)) > 0)
		    {
		      if(matchWord(line,&buf, "endsection")) break;	      
		      else
			{
			  char *q, *p = skipSpaces(buf);
			  if(p)
			    {
			      q = p;
			      while(*q && *q != ' ' && *q != '\t') q++;
			      if(*q) { *q = '\0'; q++; }
			      q = skipSpaces(q);
 			      if(q) insertNameToHashTable(p, q);
			    }
			}
		    }
		  else break;
		}
	    }
	  else
	    {
	      fprintf(stderr, "%s: Line %d, unknown section name %s.\n", fname, lineno, ptr);
	      while((status = seekWord(line, &buf, &lineno, fp,"endsection")) == 0)
		/* null statement */ ;
	    }
	}
      if(status == -1) return;
    }
}
/****************************************************************************/

#undef _RC_C_
