Logo Search packages:      
Sourcecode: icecast-client version File versions  Download package

configfile.c

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
/* #include <winsock2.h> */
#include <winsock.h>
#include <mmsystem.h>
#include <fcntl.h>
#include <io.h>
#include <direct.h>
#include <process.h>
#include <winbase.h>
#else /*  *NIX  */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <stdarg.h>
#include <signal.h>
#include <ctype.h>
#include <stdarg.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/time.h>
#endif /* !win32 */

#ifdef _WIN32
#include <winbase.h>
#endif

#include "shout.h"

#define LINESIZE 1024

int on_off (char *ap);

extern set_t set;

int
parse_config_file (char *path, char *file)
{
      int ret=0;
      char line[LINESIZE], arg[LINESIZE];
      FILE *fp;
      struct stat st;
      char filename[BUFSIZE]="";

      strcpy (filename, path);

#ifndef _WIN32
    strcat (filename, "/");
      if ((stat (path, &st) == -1) || (!S_ISDIR (st.st_mode))) {
            scream (TOERROR, "[%s] directory does not exist, trying to create\n", path);
        if (mkdir (path, 00755) == -1) {
#else
    strcat (filename, "\\");
      if ((stat (path, &st) == -1) || (!(st.st_mode & _S_IFDIR))) {
            scream (TOERROR, "[%s] directory does not exist, trying to create\n", path);
            
            if (!CreateDirectory(path, NULL)) {
#endif
            scream (TOERROR, "Could not create directory [%s], exiting\n",
                        path);
            }
      }
    strcat (filename, file);

    if ((fp = fopen (filename, "r")) == NULL){
        scream (TOERROR, "No such filename [%s]\n", filename);
            return -1;
    }

    scream (VERBOSE, "Parsing configuration file [%s]\n", file);
      
      while (fgets (arg, LINESIZE, fp))
      {
            if (arg[0] == '#')
                  continue;
            if (strlen (arg) < 2)
                  continue;
            
            if (arg[strlen (arg) - 1] == '\n')
                  arg[strlen (arg) - 1] = '\0';
            
            if (splitc (line, arg, ' ') == NULL)
            {
                  scream (TOERROR, "option %s requires an argument\n", line);
                  continue;
            }

            /* This is just how I do things like this, live with it */
            switch (tolower (line[0]))
            {
            case 'a':
                  if (strncmp (line, "autodetect", 10) == 0)
                        set.autodetection = on_off (arg);
                  else if (strncmp (line, "autocorrect", 11) == 0)
                        set.autocorrection = on_off (arg);
                  else if (strncmp (line, "autodump", 8) == 0)
                        strncpy (set.autodump, arg, BUFSIZE);
                  break;
/*  -- base dir is current dir, use etc for conf files
        case 'b':
                  if (strncmp (line, "base_dir", 8) == 0)
                        strncpy (set.base, arg, BUFSIZE);
                  break;
*/
            case 'd':
                  if (strncmp (line, "djprogram", 9) == 0)
                        strncpy (set.djfile, arg, BUFSIZE);
                  else if (strncmp (line, "desc", 4) == 0)
                        strncpy (set.description, arg, BUFSIZE);
            else if (strncmp (line, "default_bitrate", 15) == 0)
                set.current_bitrate = atoi (arg);
                  else if (strncmp (line, "daemon", 6) == 0)
                        set.daemon = on_off (arg);
                  break;
            case 'f':
                  if (strncmp (line, "force", 5) == 0)
                        set.skip = on_off (arg);
                  break;
            case 'g':
                  if (strncmp (line, "genre", 5) == 0)
                        strncpy (set.genre, arg, BUFSIZE);
                  break;
            case 'i':
                  if (strncmp (line, "id3", 3) == 0)
                        set.use_id3 = on_off (arg);
                  break;
            case 'l':
                  if (strncmp (line, "loop", 4) == 0)
                        set.loop = on_off (arg);
                  break;
            case 'm':
                  if (strncmp (line, "mount", 5) == 0)
                        strncpy (set.mount_name, arg, BUFSIZE);
                  /* set the playlist, etc according to mount name */
#ifndef _WIN32                
                        my_snprintf5 (set.playlist, BUFSIZE, "%s/%s.playlist", set.logdir, set.mount_name);
                        my_snprintf5 (set.cuefile, BUFSIZE, "%s/%s.cue", set.logdir, set.mount_name);
                        my_snprintf5 (set.logfilename, BUFSIZE, "%s/%s.log", set.logdir, set.mount_name);
                        my_snprintf5 (set.pidfile, BUFSIZE, "%s/%s.pid", set.logdir, set.mount_name);
#else
                        my_snprintf5 (set.playlist, BUFSIZE, "%s\\%s.playlist", set.logdir, set.mount_name);
                        my_snprintf5 (set.cuefile, BUFSIZE, "%s\\%s.cue", set.logdir, set.mount_name);
                        my_snprintf5 (set.logfilename, BUFSIZE, "%s\\%s.log", set.logdir, set.mount_name);
                        my_snprintf5 (set.pidfile, BUFSIZE, "%s\\%s.pid", set.logdir, set.mount_name);
#endif
                  break;
            case 'n':
                  if (strncmp (line, "name", 4) == 0)
                        strncpy (set.name, arg, BUFSIZE);
                  break;
            case 'p':
                  if (strncmp (line, "password", 8) == 0)
                        strncpy (set.password, arg, BUFSIZE);
            else if (strncmp (line, "port", 4) == 0){
                        set.port = atoi (arg);
                ret++;
            }
                  else if (strncmp (line, "public", 6) == 0)
                        set.public = on_off (arg);
                  else if (strncmp (line, "playlist", 8) == 0)
                  {
                        strcpy (filename, set.etcdir);
#ifndef _WIN32
                        strcat (filename, "/");
#else
                        strcat (filename, "\\");
#endif
                        strcat (filename, arg);
                        if (!set.setup_playlist)
                              setup_playlist ();
                        add_list_to_playlist (filename);
                  }
            case 's':
            if (strncmp (line, "server_name", 11) == 0){
                 strncpy (set.servername, arg, BUFSIZE);
                 ret++;
            }
                  if (strncmp (line, "shuffle", 7) == 0)
                        set.shuffle_playlist = on_off (arg);
                  else if (strncmp (line, "short_titles", 10) == 0)
                        set.shortfilenames = on_off (arg);
                  break;
            case 't':
                  if (strncmp (line, "truncate", 8) == 0)
                        set.truncate = on_off (arg);
                  else if (strncmp (line, "title_streaming", 14) == 0)
                        set.titlestreaming = on_off (arg);
                  break;
            case 'u':
            if (strncmp (line, "update", 6) == 0)
                        set.update_cue_file = on_off (arg);
                  else if (strncmp (line, "url", 3) == 0)
                        strncpy (set.url, arg, BUFSIZE);
                  else if (strncmp (line, "usecuefile", 10) == 0)
                        set.use_cue_file = on_off (arg);
                  else if (strncmp (line, "usedj", 5) == 0)
                        set.use_dj = on_off (arg);
                  break;
            case 'v':
                  if (strncmp (line, "verbose", 7) == 0)
                        set.verbose = on_off (arg);
                  break;
            }
      }
      scream (VERBOSE, "Done parsing configuration file\n");
    if (ret >= 2)
          return ret;
    else 
        return 0;
}

int
on_off (char *ap)
{
      if (!ap)
            return 1;
      if (ap[0] == '1' || tolower (ap[0]) == 'y')
            return 1;
      if ((strncmp (ap, "on", 2) == 0) || (strncmp (ap, "ON", 2) == 0))
            return 1;

    if (ap[0] == '2') /* debug verbose mode */
        return 2;
    
    return 0;
}

Generated by  Doxygen 1.6.0   Back to index