From: <dg...@su...> - 2009-01-22 13:28:04
|
Author: bellmich Date: Thu Jan 22 14:26:49 2009 New Revision: 176 URL: http://libwbxml.opensync.org/changeset/176 Log: - fixed ticket #25 - replaced the getopt implementation because of a potential copyright problem (kannel.org uses a BSD license) Modified: wbxml2/trunk/tools/attgetopt.c wbxml2/trunk/tools/getopt.h wbxml2/trunk/tools/wbxml2xml_tool.c wbxml2/trunk/tools/xml2wbxml_tool.c Modified: wbxml2/trunk/tools/attgetopt.c ============================================================================== --- wbxml2/trunk/tools/attgetopt.c Tue Jan 20 13:18:47 2009 (r175) +++ wbxml2/trunk/tools/attgetopt.c Thu Jan 22 14:26:49 2009 (r176) @@ -7,61 +7,97 @@ * * From the mod.sources newsgroup, volume 3, issue 58, with modifications * to bring it up to 21st century C. + */ + +/* Copied from: http://everything2.com/e2node/getopt%2528%2529 + * getopt renamed to wbxml_getopt + */ + +/* getopt.c: + * a public domain implementation of getopt() + * + * The following source code is an adaptation of the public domain getopt() + * implementation presented at the 1985 UNIFORUM conference in Dallas, + * Texas. Slight edits have been made to improve readability and the result + * is released into the public domain like that from which it was derived. * - * Taken from Kannel Project (http://www.kannel.org/) */ #include <stdio.h> #include <string.h> +int optind = 1; +int optopt; +char *optarg; -#define ERR(s, c) if (opterr) (void) fprintf(stderr, "%s: %s\n", argv[0], s) - -int opterr = 1; -int optind = 1; -int optopt; -char *optarg; - -int getopt(int argc, char **argv, char *opts) +int +wbxml_getopt(int argc, char **argv, char *opts) { - static int sp = 1; - register int c; - register char *cp; - - if(sp == 1) { - if(optind >= argc || - argv[optind][0] != '-' || argv[optind][1] == '\0') - return(EOF); - else if(strcmp(argv[optind], "--") == 0) { - optind++; - return(EOF); - } - } - optopt = c = argv[optind][sp]; - if(c == ':' || (cp=strchr(opts, c)) == NULL) { - ERR(": illegal option -- ", c); - if(argv[optind][++sp] == '\0') { - optind++; - sp = 1; - } - return('?'); - } - if(*++cp == ':') { - if(argv[optind][sp+1] != '\0') - optarg = &argv[optind++][sp+1]; - else if(++optind >= argc) { - ERR(": option requires an argument -- ", c); - sp = 1; - return('?'); - } else - optarg = argv[optind++]; - sp = 1; - } else { - if(argv[optind][++sp] == '\0') { - sp = 1; - optind++; - } - optarg = NULL; - } - return(c); + static int sp = 1; + register int c; + register char *cp; + + if (sp == 1) { + + /* If all args are processed, finish */ + if (optind >= argc) { + return EOF; + } + if (argv[optind][0] != '-' || argv[optind][1] == '\0') { + return EOF; + } + + } else if (!strcmp(argv[optind], "--")) { + + /* No more options to be processed after this one */ + optind++; + return EOF; + + } + + optopt = c = argv[optind][sp]; + + /* Check for invalid option */ + if (c == ':' || (cp = strchr(opts, c)) == NULL) { + + fprintf(stderr, + "%s: illegal option -- %c\n", + argv[0], + c); + if (argv[optind][++sp] == '\0') { + optind++; + sp = 1; + } + + return '?'; + } + + /* Does this option require an argument? */ + if (*++cp == ':') { + + /* If so, get argument; if none provided output error */ + if (argv[optind][sp+1] != '\0') { + optarg = &argv[optind++][sp+1]; + } else if (++optind >= argc) { + fprintf(stderr, + "%s: option requires an argument -- %c\n", + argv[0], + c); + sp = 1; + return '?'; + } else { + optarg = argv[optind++]; + } + sp = 1; + + } else { + if (argv[optind][++sp] == '\0') { + sp = 1; + optind++; + } + optarg = NULL; + } + + return c; } + Modified: wbxml2/trunk/tools/getopt.h ============================================================================== --- wbxml2/trunk/tools/getopt.h Tue Jan 20 13:18:47 2009 (r175) +++ wbxml2/trunk/tools/getopt.h Thu Jan 22 14:26:49 2009 (r176) @@ -3,16 +3,13 @@ * @ingroup wbxml2xml_tool * @ingroup xml2wbxml_tool * - * @author Kannel Team (http://www.kannel.org/) - * - * @brief getopt() implementation + * @brief wbxml_getopt() is just another copy of the getopt implementation of AT&T */ #ifndef WBXML_GETOPT_H #define WBXML_GETOPT_H -int getopt(int argc, char **argv, char *opts); -extern int opterr; +int wbxml_getopt(int argc, char **argv, char *opts); extern int optind; extern int optopt; extern char *optarg; Modified: wbxml2/trunk/tools/wbxml2xml_tool.c ============================================================================== --- wbxml2/trunk/tools/wbxml2xml_tool.c Tue Jan 20 13:18:47 2009 (r175) +++ wbxml2/trunk/tools/wbxml2xml_tool.c Thu Jan 22 14:26:49 2009 (r176) @@ -236,7 +236,7 @@ params.indent = 1; params.keep_ignorable_ws = FALSE; - while ((opt = (WB_TINY) getopt(argc, argv, "kh?o:m:i:l:")) != EOF) + while ((opt = (WB_TINY) wbxml_getopt(argc, argv, "kh?o:m:i:l:")) != EOF) { switch (opt) { case 'k': Modified: wbxml2/trunk/tools/xml2wbxml_tool.c ============================================================================== --- wbxml2/trunk/tools/xml2wbxml_tool.c Tue Jan 20 13:18:47 2009 (r175) +++ wbxml2/trunk/tools/xml2wbxml_tool.c Thu Jan 22 14:26:49 2009 (r176) @@ -105,7 +105,7 @@ params.use_strtbl = TRUE; params.keep_ignorable_ws = FALSE; - while ((opt = (WB_TINY) getopt(argc, argv, "nkh?o:v:")) != EOF) + while ((opt = (WB_TINY) wbxml_getopt(argc, argv, "nkh?o:v:")) != EOF) { switch (opt) { case 'v': |