File:  [Public] / rpm2html / config.c
Revision 1.6: download - view: text, annotated - select for diffs
Sat Nov 15 21:48:32 1997 UTC (26 years, 6 months ago) by veillard
Branches: MAIN
CVS tags: VERSION_0_2, HEAD
Begin implementing the dump of installed RPM base too, Daniel.

/*
 * config.c : handle the configuration file.
 *
 * Copyright (c) 1997 Daniel Veillard <veillard@apocalypse.org>
 * See COPYING for the status of this software.
 *
 * $Id: config.c,v 1.6 1997/11/15 21:48:32 veillard Exp $
 */

#include <config.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "rpm2html.h"
#include "rpmdata.h"

/*
 * configuration variables for rpm2html
 */

char *rpm2html_name	= RPM2HTML_NAME;
char *rpm2html_ver	= RPM2HTML_VER;
char *rpm2html_url	= RPM2HTML_URL;
char *rpm2html_maint	= RPM2HTML_MAINT;
char *rpm2html_mail	= RPM2HTML_MAIL;
char *rpm2html_dir	= RPM2HTML_DIR;
char *rpm2html_ftp	= RPM2HTML_FTP;

int  rpm2html_files = 0;
int  rpm2html_size = 0;

int  rpm2html_install_files = 0;
int  rpm2html_install_size = 0;

/*
 * Search a directory in the list. If not found, create a new one.
 */

rpmDirPtr rpmDirSearch(char *dirname) {
    rpmDirPtr cur = dirList;

    while (cur != NULL) {
        if (!strcmp(dirname, cur->dir)) return(cur);
	cur = cur->next;
    }
    cur = (rpmDirPtr) malloc(sizeof(rpmDir));
    cur->dir = strdup(dirname);
    cur->name = "";
    cur->ftp = NULL;
    cur->ftpsrc = NULL;
    cur->color = "#ffffff";
    cur->trust = "1.0";
    cur->nb_urls = 0;
    if (strcmp(dirname, "localbase"))
        cur->installbase = 0;
    else
        cur->installbase = 1;
    cur->next = dirList;
    dirList = cur;
    return(cur);
}

/*
 * addConfigEntry : an entry in the config file has just been read.
 */
void addConfigEntry(char *rpmdir, char *name, char *value) {
    rpmDirPtr cur;

#ifdef DEBUG
    printf("addConfigEntry(\"%s\", \"%s\", \"%s\")\n", rpmdir, name, value);
#endif
    /*
     * case of global option for rpm2html.
     */
    if (!strcasecmp(rpmdir, RPM2HTML_NAME)) {
        if (!strcasecmp(name, "url")) {
	    rpm2html_url = strdup(value);
	} else if (!strcasecmp(name, "maint")) {
	    rpm2html_maint = strdup(value);
	} else if (!strcasecmp(name, "mail")) {
	    rpm2html_mail = strdup(value);
	} else if (!strcasecmp(name, "dir")) {
	    rpm2html_dir = strdup(value);
	} else if (!strcasecmp(name, "ftp")) {
	    rpm2html_ftp = strdup(value);
        } else {
	    printf("Config file : %s global entry ignored\n", name);
	}
	return;
    }

    /*
     * option for a directory.
     */
    cur = rpmDirSearch(rpmdir);
    if (!strcasecmp(name, "name")) {
	cur->name = strdup(value);
    } else if (!strcasecmp(name, "ftp")) {
	cur->ftp = strdup(value);
    } else if (!strcasecmp(name, "ftpsrc")) {
	cur->ftpsrc = strdup(value);
    } else if (!strcasecmp(name, "color")) {
	cur->color = strdup(value);
    } else if (!strcasecmp(name, "trust")) {
	cur->trust = strdup(value);
    } else if (!strcasecmp(name, "url")) {
        /*
	 * all "url" values are collected in the urls array.
	 */
	if (cur->nb_urls >= MAX_URLS) {
	    fprintf(stderr, "increase MAX_URLS %d overflow\n", MAX_URLS);
	} else {
	    cur->urls[cur->nb_urls] = strdup(value);
	    cur->nb_urls++;
	}
    } else {
	printf("Config file : %s entry for %s ignored\n", name, rpmdir);
    }
}

/****************************************************************
 *								*
 *		The configuration file parser			*
 *								*
 ****************************************************************/

/*
 * A few macro needed to help building the parser
 */

#define IS_BLANK(ptr) \
     (((*(ptr)) == ' ') || ((*(ptr)) == '\b') || \
      ((*(ptr)) == '\n') || ((*(ptr)) == '\r'))
#define SKIP_BLANK(ptr) \
     { while (((*(ptr)) == ' ') || ((*(ptr)) == '\b') || \
              ((*(ptr)) == '\n') || ((*(ptr)) == '\r')) ptr++; }
#define GOTO_EQL(ptr) \
     { while (((*(ptr)) != '\0') && ((*(ptr)) != '=') && \
              ((*(ptr)) != '\n') && ((*(ptr)) != '\r')) ptr++; }
#define GOTO_EOL(ptr) \
     { while (((*(ptr)) != '\0') && \
              ((*(ptr)) != '\n') && ((*(ptr)) != '\r')) ptr++; }


/*
 * read config file: parse a configuration file.
 */
int readConfigFile(char *filename)
{
   FILE *input;
   char *str, *base;
   char string[1000];
   char rpmdir[1000] = "rpm2html";
   char *name;
   char *value;
   int errors = 0;

   input = fopen (filename, "r");
   if (input == NULL)
     {
	fprintf (stderr, "Cannot read config from %s :\n", filename);
	perror ("fopen failed");
	return -1;
     }

   while (1)
     {
	/*
	 * read one line in string buffer.
	 */
	if (fgets (&string[0], sizeof (string) - 1, input) == NULL)
	   break;

	str = &string[0];
	SKIP_BLANK (str)
	string[sizeof (string) - 1] = '\0';

	/*
	 * Comment starts with a semicolumn.
	 */
	if (*str == ';')
	   continue;
	if (*str == '\0')
	   continue;

	/*
	 * sections are indicated between brackets, e.g. [amaya]
	 */
	if (*str == '[')
	  {
	     str++;
	     SKIP_BLANK (str)
	     base = str;
	     while ((*str != '\0') && (*str != ']'))
		str++;
	     if (*str == '\0')
	       {
		  fprintf (stderr, "config file %s corrupted :\n\t\"%s\"\n",
			   filename, string);
		  break;
	       }
	     *str = '\0';
	     strcpy (&rpmdir[0], base);
#ifdef DEBUG
	     fprintf (stderr, "readConfigFile section [%s]\n", rpmdir);
#endif
	     continue;
	  }

	/*
	 * entries have the following form :
	 *    name=value
	 */
	name = str;
	GOTO_EQL (str)
	if (*str != '=') {
	   errors++;
	   if (errors >= 30) {
	       fprintf (stderr, "config file %s seems invalid\n", filename);
	       break;
	   }
	   continue;
	}
	*str++ = '\0';
	SKIP_BLANK (str)
	value = str;
	GOTO_EOL (str)
	*str = '\0';
	addConfigEntry(rpmdir, name, value);
     }

   fclose (input);
   return(0);
}


Webmaster