help-gengetopt
[Top][All Lists]
Advanced

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

[help-gengetopt] Patch for gengetopt_strdup()


From: Brian Minard
Subject: [help-gengetopt] Patch for gengetopt_strdup()
Date: Fri, 16 Nov 2001 21:31:30 -0500

Hi,

Had a quick look at gengetopt_strdup().  You can munge strdup() to
create gengetopt_strdup() using txtc.sh and some sed commands.  

Relying on pattern matching makes the solution a little brittle. A
pattern match failure should be harmless or cause a link or
compilation failure.

Brian
---
*** src/Makefile.am     Mon May 14 15:41:34 2001
--- src/Makefile.am     Thu Nov 15 21:40:59 2001
*************** SUFFIXES = .text
*** 21,30 ****
  .text.o:
        $(TXTC) $<
  
  bin_PROGRAMS = gengetopt
  
  gengetopt_SOURCES = copyright.text reportbugs.text parser.y scanner.l \
!                     argsdef.c cmdline.c gengetopt.c gm.c yyerror.c
  
  gengetopt_LDADD = @LIBOBJS@ @LEXLIB@ @ALLOCA@
  
--- 21,39 ----
  .text.o:
        $(TXTC) $<
  
+ # automatically generate gengetopt_strdup() from the replacement function
+ # strdup().
+ gengetopt_strdup.text: strdup.c
+       echo "/* gengetopt_strdup(): automatically generated from strdup.c. */" 
> $@
+       cat $< | sed -e 's/^strdup (/gengetopt_&/' \
+               -e 's/^char \* *$$/static &/' \
+               -e '/^#include.*$$/D' -e '/^$$/D' >> $@
+ 
  bin_PROGRAMS = gengetopt
  
  gengetopt_SOURCES = copyright.text reportbugs.text parser.y scanner.l \
!                     argsdef.c cmdline.c gengetopt.c gm.c yyerror.c \
!                     gengetopt_strdup.text
  
  gengetopt_LDADD = @LIBOBJS@ @LEXLIB@ @ALLOCA@
  
*************** pkgdata_DATA = getopt.h getopt.c getopt1
*** 56,59 ****
  
  # automake (maybe due to a bug) doesn't added these files well,
  # so we explicitely declare them
! MAINTAINERCLEANFILES = scanner.c parser.h parser.c 
--- 65,68 ----
  
  # automake (maybe due to a bug) doesn't added these files well,
  # so we explicitely declare them
! MAINTAINERCLEANFILES = scanner.c parser.h parser.c gengetopt_strdup.text
*** src/gm.c    Sun Jun 24 08:27:16 2001
--- src/gm.c    Thu Nov 15 22:23:02 2001
*************** extern struct gengetopt_option * gengeto
*** 32,37 ****
--- 32,39 ----
  extern char * gengetopt_package;
  extern char * gengetopt_version;
  extern char * gengetopt_purpose;
+ extern int gengetopt_strdup_text_length;
+ extern char *gengetopt_strdup_text[];
  
  static int tab_indentation ; /* tab indentation level */
  static int handle_error; /* should I handle errors, by exit(1) */
*************** header_filename);
*** 543,549 ****
                        printf (" ");
                printf ("  %s\\n\\\n", opt->desc);
        }
!   printf ("\", PACKAGE);\n}\n\n\n");
  
    if (! generate_strdup)
      {
--- 545,551 ----
                        printf (" ");
                printf ("  %s\\n\\\n", opt->desc);
        }
!   printf ("\", PACKAGE);\n}\n\n");
  
    if (! generate_strdup)
      {
*************** header_filename);
*** 554,575 ****
        }
      }
  
!   if (generate_strdup)
!     printf (
!             "static char *\n"
!             "gengetopt_strdup (char * s)\n"
!             "{\n"
!             "  char * n, * pn, * ps = s;\n"
!             "  while (*ps) ps++;\n"
!             "  n = (char *) malloc (1 + ps - s);\n"
!             "  if (n != NULL)\n"
!             "    {\n"
!             "      for (ps=s,pn=n; *ps; ps++,pn++)\n"
!             "        *pn = *ps;\n"
!             "      *pn = 0;\n"
!             "    }\n"
!             "  return n;\n"
!             "}\n\n\n");
    
    printf (
  "int\n"
--- 556,567 ----
        }
      }
  
!   if (generate_strdup) 
!   {
!     for (i = 1; i <= gengetopt_strdup_text_length; ++i)
!       printf ("%s\n", gengetopt_strdup_text[i]);
!     printf ("\n");
!   }
    
    printf (
  "int\n"
*** src/strdup.c        Sat May 19 10:49:51 2001
--- src/strdup.c        Wed Nov 14 19:51:21 2001
***************
*** 4,10 ****
  #include <string.h>
  
  char *
! strdup( const char *s )
  {
    char *result = (char*)malloc(strlen(s) + 1);
    if (result == (char*)0)
--- 4,10 ----
  #include <string.h>
  
  char *
! strdup (const char *s)
  {
    char *result = (char*)malloc(strlen(s) + 1);
    if (result == (char*)0)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]