Namazu-users-ja(旧)


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

__APPLE_ (patch) Re: make error on Mac OS X



>                                            千葉市中央区長洲
>                                                    藤原  誠
To: namazu-users-ja@xxxxxxxxxx
From: Yasuaki KAI <kai@xxxxxxxxxxxxxx>
Date: Sun, 8 Jun 2003 12:15:19 +0900

甲> その他に4変数を使っている箇所がないか調べた結果、該当するのは
甲> ご指摘の src/namazu-cmd.c の中の optind の1箇所のみでしたので、
甲> これを optind_t と変更してみたところ、make check 結果もすべて
甲> PASS となって問題が解決しました。make install も無事済んで、
甲> TerminalはもちろんCGIもちゃんと動いているようです。

という話があったのを思い出し、これが patch の形にはならない
かなと思って、一応作って見ました。かなりいい加減で、もう少し
ましな方法があるだろうと思いながら、
===================
All 46 tests passed
===================
になりましたので、一応ここにお送りしておきます。
これを当てても、他の NetBSD/macppc でも問題ないことは確めてあり
ます。 

__APPLE__ の定義の有無で、
  getopt            __getopt
  opterr            __opterr
  optind OPTIND  -> __optind
  optopt OPTOPT  -> __optopt
のように置換えています。四つが同じ方式にしていないのに強い意味は
ありません。
---
(藤原)
===File /tmp/diff===========================================
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 02:59:13 -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 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,17 @@
 /* Callers store zero here to inhibit the error message
    for unrecognized options.  */
 
-int opterr = 1;
+#ifdef __APPLE__
+  int __opterr = 1;
+#else
+  int opterr = 1;
+#endif
 
 /* 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 OPTOPT = '?';
 
 /* Describe how to deal with options that follow non-option ARGV-elements.
 
@@ -301,7 +305,7 @@
 {
   int bottom = first_nonopt;
   int middle = last_nonopt;
-  int top = optind;
+  int top = OPTIND;
   char *tem;
 
   /* Exchange the shorter segment with the far end of the longer segment.
@@ -370,9 +374,9 @@
     }
 
   /* Update records for the slots the non-options now occupy.  */
+  first_nonopt += (OPTIND - last_nonopt);
+  last_nonopt = OPTIND;
 
-  first_nonopt += (optind - last_nonopt);
-  last_nonopt = optind;
 }
 
 /* Initialize the internal data when the first call is made.  */
@@ -389,8 +393,7 @@
   /* Start processing options with ARGV-element 1 (since ARGV-element 0
      is the program name); the sequence of previously skipped
      non-option ARGV-elements is empty.  */
-
-  first_nonopt = last_nonopt = optind;
+  first_nonopt = last_nonopt = OPTIND;
 
   nextchar = NULL;
 
@@ -511,7 +514,11 @@
      int *longind;
      int long_only;
 {
+#ifdef __APPLE__
+  int print_errors = __opterr;
+#else
   int print_errors = opterr;
+#endif
   if (optstring[0] == ':')
     print_errors = 0;
 
@@ -520,10 +527,10 @@
 
   optarg = NULL;
 
-  if (optind == 0 || !__getopt_initialized)
+  if (OPTIND == 0 || !__getopt_initialized)
     {
-      if (optind == 0)
-	optind = 1;	/* Don't scan ARGV[0], the program name.  */
+      if (OPTIND == 0)
+	OPTIND = 1;	/* Don't scan ARGV[0], the program name.  */
       optstring = _getopt_initialize (argc, argv, optstring);
       __getopt_initialized = 1;
     }
@@ -533,11 +540,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[OPTIND][0] != '-' || argv[OPTIND][1] == '\0'	      \
+		      || (OPTIND < nonoption_flags_len			      \
+			  && __getopt_nonoption_flags[OPTIND] == '1'))
 #else
-# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
+# define NONOPTION_P (argv[OPTIND][0] != '-' || argv[OPTIND][1] == '\0')
 #endif
 
   if (nextchar == NULL || *nextchar == '\0')
@@ -546,27 +553,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 > OPTIND)
+	last_nonopt = OPTIND;
+      if (first_nonopt > OPTIND)
+	first_nonopt = 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 != OPTIND)
 	    exchange ((char **) argv);
-	  else if (last_nonopt != optind)
-	    first_nonopt = optind;
+	  else if (last_nonopt != OPTIND)
+	    first_nonopt = 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 (OPTIND < argc && NONOPTION_P)
+	    OPTIND++;
+	  last_nonopt = OPTIND;
 	}
 
       /* The special ARGV-element `--' means premature end of options.
@@ -574,28 +581,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 (OPTIND != argc && !strcmp (argv[OPTIND], "--"))
 	{
-	  optind++;
+	  OPTIND++;
 
-	  if (first_nonopt != last_nonopt && last_nonopt != optind)
+	  if (first_nonopt != last_nonopt && last_nonopt != OPTIND)
 	    exchange ((char **) argv);
 	  else if (first_nonopt == last_nonopt)
-	    first_nonopt = optind;
+	    first_nonopt = OPTIND;
 	  last_nonopt = argc;
 
-	  optind = argc;
+	  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 (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;
+	    OPTIND = first_nonopt;
 	  return -1;
 	}
 
@@ -606,15 +613,15 @@
 	{
 	  if (ordering == REQUIRE_ORDER)
 	    return -1;
-	  optarg = argv[optind++];
+	  optarg = argv[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[OPTIND] + 1
+		  + (longopts != NULL && argv[OPTIND][1] == '-'));
     }
 
   /* Decode the current option-ARGV-element.  */
@@ -633,8 +640,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[OPTIND][1] == '-'
+	  || (long_only && (argv[OPTIND][2] || !my_index (optstring, argv[OPTIND][1])))))
     {
       char *nameend;
       const struct option *p;
@@ -679,17 +686,17 @@
 	{
 	  if (print_errors)
 	    fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
-		     argv[0], argv[optind]);
+		     argv[0], argv[OPTIND]);
 	  nextchar += strlen (nextchar);
-	  optind++;
-	  optopt = 0;
+	  OPTIND++;
+	  OPTOPT = 0;
 	  return '?';
 	}
 
       if (pfound != NULL)
 	{
 	  option_index = indfound;
-	  optind++;
+	  OPTIND++;
 	  if (*nameend)
 	    {
 	      /* Don't test has_arg with >, because some C compilers don't
@@ -700,7 +707,7 @@
 		{
 		  if (print_errors)
 		    {
-		      if (argv[optind - 1][1] == '-')
+		      if (argv[OPTIND - 1][1] == '-')
 			/* --option */
 			fprintf (stderr,
 				 _("%s: option `--%s' doesn't allow an argument\n"),
@@ -709,27 +716,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[OPTIND - 1][0], pfound->name);
 		    }
 
 		  nextchar += strlen (nextchar);
 
-		  optopt = pfound->val;
+		  OPTOPT = pfound->val;
 		  return '?';
 		}
 	    }
 	  else if (pfound->has_arg == 1)
 	    {
-	      if (optind < argc)
-		optarg = argv[optind++];
+	      if (OPTIND < argc)
+		optarg = argv[OPTIND++];
 	      else
 		{
 		  if (print_errors)
 		    fprintf (stderr,
 			   _("%s: option `%s' requires an argument\n"),
-			   argv[0], argv[optind - 1]);
+			   argv[0], argv[OPTIND - 1]);
 		  nextchar += strlen (nextchar);
-		  optopt = pfound->val;
+		  OPTOPT = pfound->val;
 		  return optstring[0] == ':' ? ':' : '?';
 		}
 	    }
@@ -748,23 +755,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[OPTIND][1] == '-'
 	  || my_index (optstring, *nextchar) == NULL)
 	{
 	  if (print_errors)
 	    {
-	      if (argv[optind][1] == '-')
+	      if (argv[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[OPTIND][0], nextchar);
 	    }
 	  nextchar = (char *) "";
-	  optind++;
-	  optopt = 0;
+	  OPTIND++;
+	  OPTOPT = 0;
 	  return '?';
 	}
     }
@@ -777,7 +784,7 @@
 
     /* Increment `optind' when we start to process its last character.  */
     if (*nextchar == '\0')
-      ++optind;
+      ++OPTIND;
 
     if (temp == NULL || c == ':')
       {
@@ -791,7 +798,7 @@
 	      fprintf (stderr, _("%s: invalid option -- %c\n"),
 		       argv[0], c);
 	  }
-	optopt = c;
+	OPTOPT = c;
 	return '?';
       }
     /* Convenience. Treat POSIX -W foo same as long option --foo */
@@ -811,9 +818,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++;
+	    OPTIND++;
 	  }
-	else if (optind == argc)
+	else if (OPTIND == argc)
 	  {
 	    if (print_errors)
 	      {
@@ -821,7 +828,7 @@
 		fprintf (stderr, _("%s: option requires an argument -- %c\n"),
 			 argv[0], c);
 	      }
-	    optopt = c;
+	    OPTOPT = c;
 	    if (optstring[0] == ':')
 	      c = ':';
 	    else
@@ -831,7 +838,7 @@
 	else
 	  /* We already incremented `optind' once;
 	     increment it again when taking next ARGV-elt as argument.  */
-	  optarg = argv[optind++];
+	  optarg = argv[OPTIND++];
 
 	/* optarg is now the argument, see if it's in the
 	   table of longopts.  */
@@ -866,9 +873,9 @@
 	  {
 	    if (print_errors)
 	      fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
-		       argv[0], argv[optind]);
+		       argv[0], argv[OPTIND]);
 	    nextchar += strlen (nextchar);
-	    optind++;
+	    OPTIND++;
 	    return '?';
 	  }
 	if (pfound != NULL)
@@ -893,14 +900,14 @@
 	      }
 	    else if (pfound->has_arg == 1)
 	      {
-		if (optind < argc)
-		  optarg = argv[optind++];
+		if (OPTIND < argc)
+		  optarg = argv[OPTIND++];
 		else
 		  {
 		    if (print_errors)
 		      fprintf (stderr,
 			       _("%s: option `%s' requires an argument\n"),
-			       argv[0], argv[optind - 1]);
+			       argv[0], argv[OPTIND - 1]);
 		    nextchar += strlen (nextchar);
 		    return optstring[0] == ':' ? ':' : '?';
 		  }
@@ -926,7 +933,7 @@
 	    if (*nextchar != '\0')
 	      {
 		optarg = nextchar;
-		optind++;
+		OPTIND++;
 	      }
 	    else
 	      optarg = NULL;
@@ -940,9 +947,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++;
+		OPTIND++;
 	      }
-	    else if (optind == argc)
+	    else if (OPTIND == argc)
 	      {
 		if (print_errors)
 		  {
@@ -951,7 +958,7 @@
 			     _("%s: option requires an argument -- %c\n"),
 			     argv[0], c);
 		  }
-		optopt = c;
+		OPTOPT = c;
 		if (optstring[0] == ':')
 		  c = ':';
 		else
@@ -960,7 +967,7 @@
 	    else
 	      /* We already incremented `optind' once;
 		 increment it again when taking next ARGV-elt as argument.  */
-	      optarg = argv[optind++];
+	      optarg = argv[OPTIND++];
 	    nextchar = NULL;
 	  }
       }
@@ -969,7 +976,11 @@
 }
 
 int
+#ifdef __APPLE__
+__getopt (argc, argv, optstring)
+#else
 getopt (argc, argv, optstring)
+#endif
      int argc;
      char *const *argv;
      const char *optstring;
@@ -997,9 +1008,12 @@
 
   while (1)
     {
-      int this_option_optind = optind ? optind : 1;
-
+      int this_option_optind = OPTIND ? OPTIND : 1;
+#ifdef __APPLE__
+      c = __getopt (argc, argv, "abc:d:0123456789");
+#else
       c = getopt (argc, argv, "abc:d:0123456789");
+#endif
       if (c == -1)
 	break;
 
@@ -1041,11 +1055,11 @@
 	}
     }
 
-  if (optind < argc)
+  if (OPTIND < argc)
     {
       printf ("non-option ARGV-elements: ");
-      while (optind < argc)
-	printf ("%s ", argv[optind++]);
+      while (OPTIND < argc)
+	printf ("%s ", argv[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 02:59:13 -0000
@@ -17,6 +17,14 @@
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA.  */
 
+#ifdef __APPLE__
+   #define OPTIND __optind
+   #define OPTOPT __optopt
+#else
+   #define OPTIND optind
+   #define OPTOPT optopt
+#endif
+
 #ifndef _GETOPT_H
 
 #ifndef __need_getopt
@@ -58,16 +66,19 @@
    Otherwise, `optind' communicates from one call to the next
    how much of ARGV has been scanned so far.  */
 
-extern int optind;
+extern int OPTIND;
 
 /* Callers store zero here to inhibit the error message `getopt' prints
    for unrecognized options.  */
-
+#ifdef __APPLE__
+extern int __opterr;
+#else
 extern int opterr;
+#endif
 
 /* Set to an option character which was unrecognized.  */
 
-extern int optopt;
+extern int OPTOPT;
 
 #ifndef __need_getopt
 /* Describe the long-named options requested by the application.
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 02:59:13 -0000
@@ -107,7 +107,7 @@
 
   while (1)
     {
-      int this_option_optind = optind ? optind : 1;
+      int this_option_optind = OPTIND ? OPTIND : 1;
       int option_index = 0;
       static struct option long_options[] =
       {
@@ -174,11 +174,11 @@
 	}
     }
 
-  if (optind < argc)
+  if (OPTIND < argc)
     {
       printf ("non-option ARGV-elements: ");
-      while (optind < argc)
-	printf ("%s ", argv[optind++]);
+      while (OPTIND < argc)
+	printf ("%s ", argv[OPTIND++]);
       printf ("\n");
     }
 
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 02:59:13 -0000
@@ -284,7 +284,7 @@
 	exit(EXIT_SUCCESS);
     }
 
-    return optind;
+    return OPTIND;
 }
 
 int 
============================================================