"Fossies" - the Fresh Open Source Software Archive

Member "xzgv-0.9.2/src/mkopts.awk" (3 Sep 2017, 2868 Bytes) of package /linux/misc/old/xzgv-0.9.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) (G)AWK source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 # mkopts.awk - make getopt_long(), config file tables, and short-option
    2 #   string from options.src.
    3 #
    4 # See options.src for details of the format that file uses.
    5 
    6 # writing to /dev/stderr for errors is dodgy if we're not using gawk,
    7 # so I just use stdout, which should be ok.
    8 
    9 
   10 BEGIN \
   11   {
   12   # thanks to the comp.lang.awk FAQ for this old-awk-detecting trick :-)
   13   if(ARGC==0)
   14     {
   15     print "mkopts: error: must run under a `new' awk, e.g. gawk, mawk, nawk;"
   16     print "  change the AWK setting in config.mk."
   17     exit 1
   18     }
   19   
   20   runend=1
   21   optfile="rcfile_opt.h"
   22   varfile="rcfile_var.h"
   23   shortoptfile="rcfile_short.h"
   24   getopt_string=""
   25   exit_val=0
   26   
   27   edit_warning="/* auto-generated from options.src, edits will be lost! */"
   28   
   29   print edit_warning >optfile
   30   print "struct option long_opts[]="    >>optfile
   31   print "  {"               >>optfile
   32   
   33   print edit_warning >varfile
   34   print "struct cfglookup_tag cfglookup[]=" >>varfile
   35   print "  {"               >>varfile
   36 
   37   print edit_warning >shortoptfile
   38   }
   39 
   40 
   41 END \
   42   {
   43   if(runend)
   44     {
   45     print "  {NULL,0,NULL,0}"       >>optfile
   46     print "  };"            >>optfile
   47     
   48     print "  {\"\",0,NULL,NULL}"    >>varfile
   49     print "  };"            >>varfile
   50   
   51     if(getopt_string=="")
   52       {
   53       print "mkopts:" NR ": NULL short-options string!"
   54       exit_val=1
   55       }
   56     else
   57       {
   58       printf("#define SHORTOPT_STRING\t\"%s\"\n",getopt_string) >>shortoptfile
   59       }
   60     
   61     exit exit_val
   62     }
   63   }
   64 
   65 
   66 # ignore comment lines and blank lines
   67 /^$/ || /^#/    { next }
   68 
   69 
   70 {
   71 if(NF!=7)
   72   {
   73   print "mkopts:" NR ": line must have 7 fields"
   74   exit_val=1
   75   next
   76   }
   77 
   78 # check none of them end in commas
   79 for(f=1;f<=NF;f++)
   80   {
   81   if($f ~ /,$/)
   82     {
   83     print "mkopts:" NR ": field ends with a comma"
   84     exit_val=1
   85     next
   86     }
   87   }
   88 
   89 name=$1
   90 shortopt=$2
   91 allow_option=$3
   92 allow_config=$4
   93 has_arg=$5
   94 funcptr=$6
   95 dataptr=$7
   96 
   97 # convert has_arg to getopt_long stuff
   98 sub(/NO_ARG/,"no_argument",has_arg);
   99 sub(/OPT_ARG/,"optional_argument",has_arg);
  100 sub(/REQ_ARG/,"required_argument",has_arg);
  101 
  102 # the config vars file
  103 # They go here whether wanted as config vars or not, as this is
  104 # also used to lookup funcptr/dataptr for command-line options.
  105 #
  106 printf("  {%s,\t%d,\t%s,\t%s},\n",name,allow_config,funcptr,dataptr) >>varfile
  107 
  108 # the options file
  109 if(allow_option)
  110   {
  111   printf("  {%s,\t%s,\tNULL,\t%s},\n",name,has_arg,shortopt) >>optfile
  112   
  113   # add any short option name on to getopt_string
  114   if(shortopt!="0" && shortopt!="NUL")
  115     {
  116     if(shortopt !~ /^'.'$/)
  117       {
  118       print "mkopts:" NR ": short option field is not of form 'c'"
  119       exit_val=1
  120       next
  121       }
  122     
  123     gsub(/'/,"",shortopt)
  124     getopt_string=getopt_string shortopt
  125     
  126     # only give `:' when arg is *required*; you can't do optional
  127     # args with short options.
  128     #
  129     if(has_arg=="required_argument")
  130       {
  131       getopt_string=getopt_string ":"
  132       }
  133     }
  134   }
  135 }