Namazu-devel-ja(旧)


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

getopt.c is moving from lib/ to nmz/ (Re: compilation on MacOSX)



>                                            千葉市中央区長洲
>                                                    藤原  誠
namazu-users-ja の方で話題になっていた
MacOS X での compile patch ですが、寺西さんの提案のようにしよ
うとして、
○ まずは移動しないで lib の下で
  getopt   _nmz_getopt
  optopt   _nmz_optopt
  opterr   _nmz_opterr
  optind   _nmz_optind
と改名した上で、(但し注釈の中などは変更していません)

○ nmz/support.h の中に、次のように書いています。

#ifndef __APPLE__
# define optopt _nmz_optopt
# define getopt _nmz_getopt
# define opterr _nmz_opterr
# define optind _nmz_optind
#endif

○ src/ の下の変更は
 namazu-cmd.c
の一行だけです。

これを commit した後に、getopt.c getopt1.c getopt.h を nmz
の下に移動しようかと思っています。

src の下とのやりとりは一行だけなので lib に残っていてもいいので
はという気もしますし、nmz/strcasecmp.c 等があるので、移動した
方がいいかなとも思います。
ちなみに確認は NetBSD/macppc 1.6T, MacOS 10.2.6 の二つの場合
だけで make check でやっています。
---
(藤原)
Index: lib/getopt.c
===================================================================
RCS file: /storage/cvsroot/namazu/lib/getopt.c,v
retrieving revision 1.2.8.2
diff -u -r1.2.8.2 getopt.c
--- lib/getopt.c	18 Jan 2002 05:11:33 -0000	1.2.8.2
+++ lib/getopt.c	13 Jul 2003 11:40:03 -0000
@@ -125,7 +125,7 @@
    how much of ARGV has been scanned so far.  */
 
 /* 1003.2 says this must be 1 before any call.  */
-int optind = 1;
+int _nmz_optind = 1;
 
 /* Formerly, initialization of getopt depended on optind==0, which
    causes problems with re-calling getopt as programs generally don't
@@ -145,13 +145,13 @@
 /* Callers store zero here to inhibit the error message
    for unrecognized options.  */
 
-int opterr = 1;
+int _nmz_opterr = 1;
 
 /* Set to an option character which was unrecognized.
    This must be initialized on some systems to avoid linking in the
    system's own getopt implementation.  */
 
-int optopt = '?';
+int _nmz_optopt = '?';
 
 /* Describe how to deal with options that follow non-option ARGV-elements.
 
@@ -301,7 +301,7 @@
 {
   int bottom = first_nonopt;
   int middle = last_nonopt;
-  int top = optind;
+  int top = _nmz_optind;
   char *tem;
 
   /* Exchange the shorter segment with the far end of the longer segment.
@@ -371,8 +371,8 @@
 
   /* Update records for the slots the non-options now occupy.  */
 
-  first_nonopt += (optind - last_nonopt);
-  last_nonopt = optind;
+  first_nonopt += (_nmz_optind - last_nonopt);
+  last_nonopt = _nmz_optind;
 }
 
 /* Initialize the internal data when the first call is made.  */
@@ -390,7 +390,7 @@
      is the program name); the sequence of previously skipped
      non-option ARGV-elements is empty.  */
 
-  first_nonopt = last_nonopt = optind;
+  first_nonopt = last_nonopt = _nmz_optind;
 
   nextchar = NULL;
 
@@ -511,7 +511,7 @@
      int *longind;
      int long_only;
 {
-  int print_errors = opterr;
+  int print_errors = _nmz_opterr;
   if (optstring[0] == ':')
     print_errors = 0;
 
@@ -520,10 +520,10 @@
 
   optarg = NULL;
 
-  if (optind == 0 || !__getopt_initialized)
+  if (_nmz_optind == 0 || !__getopt_initialized)
     {
-      if (optind == 0)
-	optind = 1;	/* Don't scan ARGV[0], the program name.  */
+      if (_nmz_optind == 0)
+	_nmz_optind = 1;	/* Don't scan ARGV[0], the program name.  */
       optstring = _getopt_initialize (argc, argv, optstring);
       __getopt_initialized = 1;
     }
@@ -533,11 +533,11 @@
      from the shell indicating it is not an option.  The later information
      is only used when the used in the GNU libc.  */
 #if defined _LIBC && defined USE_NONOPTION_FLAGS
-# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0'	      \
-		      || (optind < nonoption_flags_len			      \
-			  && __getopt_nonoption_flags[optind] == '1'))
+# define NONOPTION_P (argv[_nmz_optind][0] != '-' || argv[_nmz_optind][1] == '\0'	      \
+		      || (_nmz_optind < nonoption_flags_len			      \
+			  && __getopt_nonoption_flags[_nmz_optind] == '1'))
 #else
-# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
+# define NONOPTION_P (argv[_nmz_optind][0] != '-' || argv[_nmz_optind][1] == '\0')
 #endif
 
   if (nextchar == NULL || *nextchar == '\0')
@@ -546,27 +546,27 @@
 
       /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
 	 moved back by the user (who may also have changed the arguments).  */
-      if (last_nonopt > optind)
-	last_nonopt = optind;
-      if (first_nonopt > optind)
-	first_nonopt = optind;
+      if (last_nonopt > _nmz_optind)
+	last_nonopt = _nmz_optind;
+      if (first_nonopt > _nmz_optind)
+	first_nonopt = _nmz_optind;
 
       if (ordering == PERMUTE)
 	{
 	  /* If we have just processed some options following some non-options,
 	     exchange them so that the options come first.  */
 
-	  if (first_nonopt != last_nonopt && last_nonopt != optind)
+	  if (first_nonopt != last_nonopt && last_nonopt != _nmz_optind)
 	    exchange ((char **) argv);
-	  else if (last_nonopt != optind)
-	    first_nonopt = optind;
+	  else if (last_nonopt != _nmz_optind)
+	    first_nonopt = _nmz_optind;
 
 	  /* Skip any additional non-options
 	     and extend the range of non-options previously skipped.  */
 
-	  while (optind < argc && NONOPTION_P)
-	    optind++;
-	  last_nonopt = optind;
+	  while (_nmz_optind < argc && NONOPTION_P)
+	    _nmz_optind++;
+	  last_nonopt = _nmz_optind;
 	}
 
       /* The special ARGV-element `--' means premature end of options.
@@ -574,28 +574,28 @@
 	 then exchange with previous non-options as if it were an option,
 	 then skip everything else like a non-option.  */
 
-      if (optind != argc && !strcmp (argv[optind], "--"))
+      if (_nmz_optind != argc && !strcmp (argv[_nmz_optind], "--"))
 	{
-	  optind++;
+	  _nmz_optind++;
 
-	  if (first_nonopt != last_nonopt && last_nonopt != optind)
+	  if (first_nonopt != last_nonopt && last_nonopt != _nmz_optind)
 	    exchange ((char **) argv);
 	  else if (first_nonopt == last_nonopt)
-	    first_nonopt = optind;
+	    first_nonopt = _nmz_optind;
 	  last_nonopt = argc;
 
-	  optind = argc;
+	  _nmz_optind = argc;
 	}
 
       /* If we have done all the ARGV-elements, stop the scan
 	 and back over any non-options that we skipped and permuted.  */
 
-      if (optind == argc)
+      if (_nmz_optind == argc)
 	{
 	  /* Set the next-arg-index to point at the non-options
 	     that we previously skipped, so the caller will digest them.  */
 	  if (first_nonopt != last_nonopt)
-	    optind = first_nonopt;
+	    _nmz_optind = first_nonopt;
 	  return -1;
 	}
 
@@ -606,15 +606,15 @@
 	{
 	  if (ordering == REQUIRE_ORDER)
 	    return -1;
-	  optarg = argv[optind++];
+	  optarg = argv[_nmz_optind++];
 	  return 1;
 	}
 
       /* We have found another option-ARGV-element.
 	 Skip the initial punctuation.  */
 
-      nextchar = (argv[optind] + 1
-		  + (longopts != NULL && argv[optind][1] == '-'));
+      nextchar = (argv[_nmz_optind] + 1
+		  + (longopts != NULL && argv[_nmz_optind][1] == '-'));
     }
 
   /* Decode the current option-ARGV-element.  */
@@ -633,8 +633,8 @@
      This distinction seems to be the most useful approach.  */
 
   if (longopts != NULL
-      && (argv[optind][1] == '-'
-	  || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
+      && (argv[_nmz_optind][1] == '-'
+	  || (long_only && (argv[_nmz_optind][2] || !my_index (optstring, argv[_nmz_optind][1])))))
     {
       char *nameend;
       const struct option *p;
@@ -679,17 +679,17 @@
 	{
 	  if (print_errors)
 	    fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
-		     argv[0], argv[optind]);
+		     argv[0], argv[_nmz_optind]);
 	  nextchar += strlen (nextchar);
-	  optind++;
-	  optopt = 0;
+	  _nmz_optind++;
+	  _nmz_optopt = 0;
 	  return '?';
 	}
 
       if (pfound != NULL)
 	{
 	  option_index = indfound;
-	  optind++;
+	  _nmz_optind++;
 	  if (*nameend)
 	    {
 	      /* Don't test has_arg with >, because some C compilers don't
@@ -700,7 +700,7 @@
 		{
 		  if (print_errors)
 		    {
-		      if (argv[optind - 1][1] == '-')
+		      if (argv[_nmz_optind - 1][1] == '-')
 			/* --option */
 			fprintf (stderr,
 				 _("%s: option `--%s' doesn't allow an argument\n"),
@@ -709,27 +709,27 @@
 			/* +option or -option */
 			fprintf (stderr,
 				 _("%s: option `%c%s' doesn't allow an argument\n"),
-				 argv[0], argv[optind - 1][0], pfound->name);
+				 argv[0], argv[_nmz_optind - 1][0], pfound->name);
 		    }
 
 		  nextchar += strlen (nextchar);
 
-		  optopt = pfound->val;
+		  _nmz_optopt = pfound->val;
 		  return '?';
 		}
 	    }
 	  else if (pfound->has_arg == 1)
 	    {
-	      if (optind < argc)
-		optarg = argv[optind++];
+	      if (_nmz_optind < argc)
+		optarg = argv[_nmz_optind++];
 	      else
 		{
 		  if (print_errors)
 		    fprintf (stderr,
 			   _("%s: option `%s' requires an argument\n"),
-			   argv[0], argv[optind - 1]);
+			   argv[0], argv[_nmz_optind - 1]);
 		  nextchar += strlen (nextchar);
-		  optopt = pfound->val;
+		  _nmz_optopt = pfound->val;
 		  return optstring[0] == ':' ? ':' : '?';
 		}
 	    }
@@ -748,23 +748,23 @@
 	 or the option starts with '--' or is not a valid short
 	 option, then it's an error.
 	 Otherwise interpret it as a short option.  */
-      if (!long_only || argv[optind][1] == '-'
+      if (!long_only || argv[_nmz_optind][1] == '-'
 	  || my_index (optstring, *nextchar) == NULL)
 	{
 	  if (print_errors)
 	    {
-	      if (argv[optind][1] == '-')
+	      if (argv[_nmz_optind][1] == '-')
 		/* --option */
 		fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
 			 argv[0], nextchar);
 	      else
 		/* +option or -option */
 		fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
-			 argv[0], argv[optind][0], nextchar);
+			 argv[0], argv[_nmz_optind][0], nextchar);
 	    }
 	  nextchar = (char *) "";
-	  optind++;
-	  optopt = 0;
+	  _nmz_optind++;
+	  _nmz_optopt = 0;
 	  return '?';
 	}
     }
@@ -777,7 +777,7 @@
 
     /* Increment `optind' when we start to process its last character.  */
     if (*nextchar == '\0')
-      ++optind;
+      ++_nmz_optind;
 
     if (temp == NULL || c == ':')
       {
@@ -791,7 +791,7 @@
 	      fprintf (stderr, _("%s: invalid option -- %c\n"),
 		       argv[0], c);
 	  }
-	optopt = c;
+	_nmz_optopt = c;
 	return '?';
       }
     /* Convenience. Treat POSIX -W foo same as long option --foo */
@@ -811,9 +811,9 @@
 	    optarg = nextchar;
 	    /* If we end this ARGV-element by taking the rest as an arg,
 	       we must advance to the next element now.  */
-	    optind++;
+	    _nmz_optind++;
 	  }
-	else if (optind == argc)
+	else if (_nmz_optind == argc)
 	  {
 	    if (print_errors)
 	      {
@@ -821,7 +821,7 @@
 		fprintf (stderr, _("%s: option requires an argument -- %c\n"),
 			 argv[0], c);
 	      }
-	    optopt = c;
+	    _nmz_optopt = c;
 	    if (optstring[0] == ':')
 	      c = ':';
 	    else
@@ -831,7 +831,7 @@
 	else
 	  /* We already incremented `optind' once;
 	     increment it again when taking next ARGV-elt as argument.  */
-	  optarg = argv[optind++];
+	  optarg = argv[_nmz_optind++];
 
 	/* optarg is now the argument, see if it's in the
 	   table of longopts.  */
@@ -866,9 +866,9 @@
 	  {
 	    if (print_errors)
 	      fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
-		       argv[0], argv[optind]);
+		       argv[0], argv[_nmz_optind]);
 	    nextchar += strlen (nextchar);
-	    optind++;
+	    _nmz_optind++;
 	    return '?';
 	  }
 	if (pfound != NULL)
@@ -893,14 +893,14 @@
 	      }
 	    else if (pfound->has_arg == 1)
 	      {
-		if (optind < argc)
-		  optarg = argv[optind++];
+		if (_nmz_optind < argc)
+		  optarg = argv[_nmz_optind++];
 		else
 		  {
 		    if (print_errors)
 		      fprintf (stderr,
 			       _("%s: option `%s' requires an argument\n"),
-			       argv[0], argv[optind - 1]);
+			       argv[0], argv[_nmz_optind - 1]);
 		    nextchar += strlen (nextchar);
 		    return optstring[0] == ':' ? ':' : '?';
 		  }
@@ -926,7 +926,7 @@
 	    if (*nextchar != '\0')
 	      {
 		optarg = nextchar;
-		optind++;
+		_nmz_optind++;
 	      }
 	    else
 	      optarg = NULL;
@@ -940,9 +940,9 @@
 		optarg = nextchar;
 		/* If we end this ARGV-element by taking the rest as an arg,
 		   we must advance to the next element now.  */
-		optind++;
+		_nmz_optind++;
 	      }
-	    else if (optind == argc)
+	    else if (_nmz_optind == argc)
 	      {
 		if (print_errors)
 		  {
@@ -951,7 +951,7 @@
 			     _("%s: option requires an argument -- %c\n"),
 			     argv[0], c);
 		  }
-		optopt = c;
+		_nmz_optopt = c;
 		if (optstring[0] == ':')
 		  c = ':';
 		else
@@ -960,7 +960,7 @@
 	    else
 	      /* We already incremented `optind' once;
 		 increment it again when taking next ARGV-elt as argument.  */
-	      optarg = argv[optind++];
+	      optarg = argv[_nmz_optind++];
 	    nextchar = NULL;
 	  }
       }
@@ -969,7 +969,7 @@
 }
 
 int
-getopt (argc, argv, optstring)
+_nmz_getopt (argc, argv, optstring)
      int argc;
      char *const *argv;
      const char *optstring;
@@ -999,7 +999,7 @@
     {
       int this_option_optind = optind ? optind : 1;
 
-      c = getopt (argc, argv, "abc:d:0123456789");
+      c = _nmz_getopt (argc, argv, "abc:d:0123456789");
       if (c == -1)
 	break;
 
@@ -1037,15 +1037,15 @@
 	  break;
 
 	default:
-	  printf ("?? getopt returned character code 0%o ??\n", c);
+	  printf ("?? _nmz_getopt returned character code 0%o ??\n", c);
 	}
     }
 
-  if (optind < argc)
+  if (_nmz_optind < argc)
     {
       printf ("non-option ARGV-elements: ");
-      while (optind < argc)
-	printf ("%s ", argv[optind++]);
+      while (_nmz_optind < argc)
+	printf ("%s ", argv[_nmz_optind++]);
       printf ("\n");
     }
 
Index: lib/getopt.h
===================================================================
RCS file: /storage/cvsroot/namazu/lib/getopt.h,v
retrieving revision 1.2.8.1
diff -u -r1.2.8.1 getopt.h
--- lib/getopt.h	18 Jan 2002 05:11:33 -0000	1.2.8.1
+++ lib/getopt.h	13 Jul 2003 11:40:05 -0000
@@ -58,16 +58,16 @@
    Otherwise, `optind' communicates from one call to the next
    how much of ARGV has been scanned so far.  */
 
-extern int optind;
+extern int _nmz_optind;
 
 /* Callers store zero here to inhibit the error message `getopt' prints
    for unrecognized options.  */
 
-extern int opterr;
+extern int _nmz_opterr;
 
 /* Set to an option character which was unrecognized.  */
 
-extern int optopt;
+extern int _nmz_optopt;
 
 #ifndef __need_getopt
 /* Describe the long-named options requested by the application.
@@ -142,9 +142,9 @@
 /* Many other libraries have conflicting prototypes for getopt, with
    differences in the consts, in stdlib.h.  To avoid compilation
    errors, only prototype getopt for the GNU C library.  */
-extern int getopt (int __argc, char *const *__argv, const char *__shortopts);
+extern int _nmz_getopt (int __argc, char *const *__argv, const char *__shortopts);
 # else /* not __GNU_LIBRARY__ */
-extern int getopt ();
+extern int _nmz_getopt ();
 # endif /* __GNU_LIBRARY__ */
 
 # ifndef __need_getopt
Index: lib/getopt1.c
===================================================================
RCS file: /storage/cvsroot/namazu/lib/getopt1.c,v
retrieving revision 1.2.8.1
diff -u -r1.2.8.1 getopt1.c
--- lib/getopt1.c	18 Jan 2002 05:11:33 -0000	1.2.8.1
+++ lib/getopt1.c	13 Jul 2003 11:40:05 -0000
@@ -107,7 +107,7 @@
 
   while (1)
     {
-      int this_option_optind = optind ? optind : 1;
+      int this_option_optind = _nmz_optind ? _nmz_optind : 1;
       int option_index = 0;
       static struct option long_options[] =
       {
@@ -174,11 +174,11 @@
 	}
     }
 
-  if (optind < argc)
+  if (_nmz_optind < argc)
     {
       printf ("non-option ARGV-elements: ");
-      while (optind < argc)
-	printf ("%s ", argv[optind++]);
+      while (_nmz_optind < argc)
+	printf ("%s ", argv[_nmz_optind++]);
       printf ("\n");
     }
 
Index: nmz/support.h
===================================================================
RCS file: /storage/cvsroot/namazu/nmz/support.h,v
retrieving revision 1.2.8.1
diff -u -r1.2.8.1 support.h
--- nmz/support.h	12 Jan 2002 10:54:16 -0000	1.2.8.1
+++ nmz/support.h	13 Jul 2003 11:40:05 -0000
@@ -41,4 +41,11 @@
 # endif 
 #endif
 
+#ifndef __APPLE__
+# define optopt _nmz_optopt
+# define getopt _nmz_getopt
+# define opterr _nmz_opterr
+# define optind _nmz_optind
+#endif
+
 #endif /* _SUPPORT_H */
Index: src/namazu-cmd.c
===================================================================
RCS file: /storage/cvsroot/namazu/src/namazu-cmd.c,v
retrieving revision 1.17.4.3
diff -u -r1.17.4.3 namazu-cmd.c
--- src/namazu-cmd.c	11 Jan 2002 05:16:47 -0000	1.17.4.3
+++ src/namazu-cmd.c	13 Jul 2003 11:40:05 -0000
@@ -284,7 +284,7 @@
 	exit(EXIT_SUCCESS);
     }
 
-    return optind;
+    return _nmz_optind;
 }
 
 int