Back to index

courier  0.68.2
comcargs.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 1999 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #include      "courier.h"
00007 #include      "comcargs.h"
00008 #include      <string.h>
00009 #include      <stdlib.h>
00010 
00011 /*
00012        Parse command line arguments.
00013 */
00014 
00015 const char *progname;
00016 
00017 int cargs(int argc, char **argv, const struct courier_args *argp)
00018 {
00019 int    argn;
00020 int    i,l=0;
00021 const char *argval;
00022 
00023        if ((progname=strrchr(argv[0], '/')) != 0)
00024               ++progname;
00025        else
00026               progname=argv[0];
00027 
00028        for (argn=1; argn<argc; argn++)
00029        {
00030               if (argv[argn][0] != '-')   break;
00031               if (argv[argn][1] == 0)
00032               {
00033                      ++argn;
00034                      break;
00035               }
00036               if (argv[argn][0] != '-')   break;
00037               for (i=0; argp[i].argname; i++)
00038               {
00039                      l=strlen(argp[i].argname);
00040                      if (strncmp(argv[argn]+1, argp[i].argname, l) == 0 &&
00041                             (argv[argn][l+1] == '=' ||
00042                                    argv[argn][l+1] == '\0'))
00043                             break;
00044               }
00045               if (!argp[i].argname)
00046               {
00047                      clog_open_stderr(progname);
00048                      clog_msg_start_err();
00049                      clog_msg_str("Invalid argument: ");
00050                      clog_msg_str(argv[argn]);
00051                      clog_msg_send();
00052                      exit(1);
00053               }
00054 
00055               if (*(argval=argv[argn]+1+l))
00056                      ++argval;
00057 
00058               if (argp[i].argval)
00059                      *argp[i].argval=argval;
00060 
00061               if (argp[i].argfunc)
00062                      (*argp[i].argfunc)(argval);
00063        }
00064        return (argn);
00065 }