Getopt-Gen

 view release on metacpan or  search on metacpan

Gen/cmdline_c.pm  view on Meta::CPAN

          abort ();
        } /* switch */
  return 0;
}


/* Initialize options not yet given from environmental defaults */
void
[@$og{funcname}@]_envdefaults(struct [@$og{structname}@] *args_info)
{
  [@
   foreach my $optid (@{$og{optl}}) {
     my $opt = $og{opth}{$optid};
     next if (!$opt->{edefault});
     my ($type,$cname,$cgiven,$edefault) =
       @$opt{qw(type cname cgiven edefault)};
     $OUT .= ('if (!args_info->'.$cgiven.") {\n  "
	      .'  char *value = getenv("'.$edefault.'");'
	      ."\n  "
	      ."  if (value != NULL) {\n  ");
     if ($type eq 'string') {
       $OUT .= ('      if (args_info->'.$cname.') free(args_info->'.$cname.");\n  "
		.'      args_info->'.$cname." = gog_strdup(value);\n  "
		."  }\n  "
		."}\n  ");
     }
     elsif ($type eq 'funct' || $type eq 'flag' || $type eq 'flag2'
	 || $type eq 'int' || $type eq 'short' || $type eq 'long')
       {
	 $OUT .= ('      args_info->'.$cname." = ($opt->{ctype})atoi(value);\n  "
		  ."  }\n  "
		  ."}\n  ");
       }
     elsif ($type eq 'float' || $type eq 'double' || $type eq 'longdouble')
       {
	 $OUT .= ('      args_info->'.$cname." = ($opt->{ctype})strtod(value,NULL);\n  "
		  ."  }\n  "
		  ."}\n  ");
       }
   }
  @]

  return;
}


/* Load option values from an .rc file */
void
[@$og{funcname}@]_read_rcfile(const char *filename,
			      struct [@$og{structname}@] *args_info,
			      int user_specified)
{
  char *fullname;
  FILE *rcfile;

  if (!filename) return; /* ignore NULL filenames */

#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID)
  if (*filename == '~') {
    /* tilde-expansion hack */
    struct passwd *pwent = getpwuid(getuid());
    if (!pwent) {
      fprintf(stderr, "%s: user-id %d not found!\n", PROGRAM, getuid());
      return;
    }
    if (!pwent->pw_dir) {
      fprintf(stderr, "%s: home directory for user-id %d not found!\n", PROGRAM, getuid());
      return;
    }
    fullname = (char *)malloc(strlen(pwent->pw_dir)+strlen(filename));
    strcpy(fullname, pwent->pw_dir);
    strcat(fullname, filename+1);
  } else {
    fullname = gog_strdup(filename);
  }
#else /* !(defined(HAVE_GETUID) && defined(HAVE_GETPWUID)) */
  fullname = gog_strdup(filename);
#endif /* defined(HAVE_GETUID) && defined(HAVE_GETPWUID) */

  /* try to open */
  rcfile = fopen(fullname,"r");
  if (!rcfile) {
    if (user_specified) {
      fprintf(stderr, "%s: warning: open failed for rc-file '%s': %s\n",
	      PROGRAM, fullname, strerror(errno));
    }
  }
  else {
   [@$og{funcname}@]_read_rc_stream(rcfile, fullname, args_info);
  }

  /* cleanup */
  if (fullname != filename) free(fullname);
  if (rcfile) fclose(rcfile);

  return;
}


/* Parse option values from an .rc file : guts */
#define OPTPARSE_GET 32
void
[@$og{funcname}@]_read_rc_stream(FILE *rcfile,
				 const char *filename,
				 struct [@$og{structname}@] *args_info)
{
  char *optname  = (char *)malloc(OPTPARSE_GET);
  char *optval   = (char *)malloc(OPTPARSE_GET);
  size_t onsize  = OPTPARSE_GET;
  size_t ovsize  = OPTPARSE_GET;
  size_t onlen   = 0;
  size_t ovlen   = 0;
  int    lineno  = 0;
  char c;

#ifdef [@$og{funcname}@]_DEBUG
  fprintf(stderr, "[@$og{funcname}@]_read_rc_stream('%s'):\n", filename);
#endif

  while ((c = fgetc(rcfile)) != EOF) {
    onlen = 0;



( run in 2.063 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )