freshclam/options.c
b151ef55
 /*
  *  Copyright (C) 2001-2002 Tomasz Kojm <zolw@konarski.edu.pl>
  *
  *  This program 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 of the License, or
  *  (at your option) any later version.
  *
  *  This program 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 this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <clamav.h>
 #define _GNU_SOURCE
 #include "getopt.h"
 
 #include "options.h"
36f2038b
 #include "output.h"
8b243778
 #include "memory.h"
b151ef55
 
ca6008cc
 int freshclam(struct optstruct *opt);
b151ef55
 
129b034f
 static void register_char_opt(struct optstruct *opt, char ch, struct option* longopts);
 static void register_long_opt(struct optstruct *opt, const char *optname, struct option* longopts);
 
 
b151ef55
 int main(int argc, char **argv)
 {
 	int ret, opt_index, i, len;
 	struct optstruct *opt;
 
0539b2a8
 	const char *getopt_parameters = "hvdp:Vl:c:u:a:";
b151ef55
 
 	static struct option long_options[] = {
521b19b4
 	    /* 
 	     * WARNING: For compatibility reasons options marked as "not used"
 	     *		must still be accepted !
 	     */
b151ef55
 	    {"help", 0, 0, 'h'},
 	    {"quiet", 0, 0, 0},
 	    {"verbose", 0, 0, 'v'},
442d8407
 	    {"debug", 0, 0, 0},
b151ef55
 	    {"version", 0, 0, 'V'},
 	    {"datadir", 1, 0, 0},
 	    {"log", 1, 0, 'l'},
521b19b4
 	    {"log-verbose", 0, 0, 0}, /* not used */
b151ef55
 	    {"stdout", 0, 0, 0},
 	    {"daemon", 0, 0, 'd'},
d09e8c7c
 	    {"pid", 1, 0, 'p'},
0d98d74c
 	    {"user", 1, 0, 'u'}, /* not used */
8b1cee14
 	    {"config-file", 1, 0, 0},
34f1f7dd
 	    {"no-dns", 0, 0, 0},
b151ef55
 	    {"checks", 1, 0, 'c'},
 	    {"http-proxy", 1, 0, 0},
0539b2a8
 	    {"client-address", 1, 0, 'a'},
b151ef55
 	    {"proxy-user", 1, 0, 0},
 	    {"daemon-notify", 2, 0, 0},
 	    {"on-update-execute", 1, 0, 0},
 	    {"on-error-execute", 1, 0, 0},
 	    {0, 0, 0, 0}
     	};
 
 
35795bca
     opt=(struct optstruct*)mcalloc(1, sizeof(struct optstruct));
b151ef55
     opt->optlist = NULL;
 
     while(1) {
 
 	opt_index=0;
 	ret=getopt_long(argc, argv, getopt_parameters, long_options, &opt_index);
 
 	if (ret == -1)
 	    break;
 
 	switch (ret) {
 	    case 0:
129b034f
 		register_long_opt(opt, long_options[opt_index].name, long_options);
b151ef55
 		break;
 
     	    default:
 		if(strchr(getopt_parameters, ret))
129b034f
 		    register_char_opt(opt, ret, long_options);
b151ef55
 		else {
 		    mprintf("!Unknown option passed.\n");
 		    free_opt(opt);
 		    exit(40);
 		}
         }
     }
 
     if (optind < argc) {
 
         len=0;
 
 	/* count length of non-option arguments */
 
 	for(i=optind; i<argc; i++)
 	    len+=strlen(argv[i]);
 
 	len=len+argc-optind-1; /* add spaces between arguments */
 	opt->filename=(char*)mcalloc(len + 256, sizeof(char));
 
         for(i=optind; i<argc; i++) {
7cc3891c
 	    strncat(opt->filename, argv[i], strlen(argv[i]));
b151ef55
 	    if(i != argc-1)
7cc3891c
 		strncat(opt->filename, " ", 1);
b151ef55
 	}
 
e8217f5a
     }
b151ef55
 
ca6008cc
     ret = freshclam(opt);
b151ef55
 
e8217f5a
     free_opt(opt);
 
ca6008cc
     return ret;
b151ef55
 }
 
129b034f
 static struct option* find_char_opt(char optchar, struct option* longopts)
 {
 	int i;
 
     for (i=0; longopts[i].name; i++) {
 	if ((char) longopts[i].val == optchar) {
 	    return (&longopts[i]);
 	}
     }
     return NULL;
 }
 
 static void register_char_opt(struct optstruct *opt, char ch, struct option* longopts)
b151ef55
 {
 	struct optnode *newnode;
129b034f
 	struct option  *longopt = find_char_opt(ch, longopts);
b151ef55
 
     newnode = (struct optnode *) mmalloc(sizeof(struct optnode));
129b034f
     
b151ef55
     newnode->optchar = ch;
     if(optarg != NULL) {
 	newnode->optarg = (char *) mcalloc(strlen(optarg) + 1, sizeof(char));
 	strcpy(newnode->optarg, optarg);
     } else newnode->optarg = NULL;
 
129b034f
     if (longopt) {
 	newnode->optname = strdup(longopt->name);
     } else {
 	newnode->optname = NULL;
     }
b151ef55
     newnode->next = opt->optlist;
     opt->optlist = newnode;
 }
 
129b034f
 static struct option* find_long_opt(const char *optname, struct option* longopts)
 {
 	int i;
 
     for (i=0; longopts[i].name; i++) {
 	if (strcmp(longopts[i].name, optname) == 0) {
 	    return (&longopts[i]);
 	}
     }
     return NULL;
 }
 
 static void register_long_opt(struct optstruct *opt, const char *optname, struct option* longopts)
b151ef55
 {
 	struct optnode *newnode;
129b034f
 	struct option  *longopt = find_long_opt(optname, longopts);
b151ef55
 
     newnode = (struct optnode *) mmalloc(sizeof(struct optnode));
129b034f
     if (longopt) {
 	newnode->optchar = longopt->val;
     } else {
 	newnode->optchar = 0;
     }
b151ef55
     if(optarg != NULL) {
 	newnode->optarg = (char *) mcalloc(strlen(optarg) + 1, sizeof(char));
 	strcpy(newnode->optarg, optarg);
     } else newnode->optarg = NULL;
 
     newnode->optname = (char *) mcalloc(strlen(optname) + 1, sizeof(char));
     strcpy(newnode->optname, optname);
     newnode->next = opt->optlist;
     opt->optlist = newnode;
 }
 
 int optc(const struct optstruct *opt, char ch)
 {
 	struct optnode *handler;
 
     handler = opt->optlist;
 
     while(1) {
 	if(handler) {
 	    if(handler->optchar == ch) return 1;
 	} else break;
 	handler = handler->next;
     }
 
     return(0);
 }
 
 int optl(const struct optstruct *opt, const char *optname)
 {
 	struct optnode *handler;
 
     handler = opt->optlist;
 
     while(1) {
 	if(handler) {
 	    if(handler->optname)
 		if(!strcmp(handler->optname, optname)) return 1;
 	} else break;
 	handler = handler->next;
     }
 
     return(0);
 }
 
 char *getargc(const struct optstruct *opt, char ch)
 {
 	struct optnode *handler;
 
     handler = opt->optlist;
 
     while(1) {
 	if(handler) {
 	    if(handler->optchar == ch) return handler->optarg;
 	} else break;
 	handler = handler->next;
     }
 
     return(NULL);
 }
 
 char *getargl(const struct optstruct *opt, const char *optname)
 {
 	struct optnode *handler;
 
     handler = opt->optlist;
 
     while(1) {
 	if(handler) {
 	    if(handler->optname)
 		if(!strcmp(handler->optname, optname)) return handler->optarg;
 	} else break;
 	handler = handler->next;
     }
 
     return(NULL);
 }
 
 void free_opt(struct optstruct *opt)
 {
 	struct optnode *handler, *prev;
 
e8217f5a
     if(!opt)
b151ef55
 	return;
 
e8217f5a
     mprintf("*Freeing option list...");
b151ef55
     handler = opt->optlist;
 
     while(handler != NULL) {
 	handler->optchar = 0;
 	if(handler->optarg) free(handler->optarg);
 	if(handler->optname) free(handler->optname);
 	prev = handler;
 	handler = handler->next;
 	free(prev);
     }
 
e8217f5a
     if (opt->filename)
 	free(opt->filename);
b151ef55
     free(opt);
     mprintf("*done\n");
 }