"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "awklib/eg/lib/getopt.awk" between
gawk-5.0.1.tar.xz and gawk-5.1.0.tar.xz

About: GNU awk - pattern scanning and processing language.

getopt.awk  (gawk-5.0.1.tar.xz):getopt.awk  (gawk-5.1.0.tar.xz)
# getopt.awk --- Do C library getopt(3) function in awk # getopt.awk --- Do C library getopt(3) function in awk
# Also supports long options.
# #
# Arnold Robbins, arnold@skeeve.com, Public Domain # Arnold Robbins, arnold@skeeve.com, Public Domain
# #
# Initial version: March, 1991 # Initial version: March, 1991
# Revised: May, 1993 # Revised: May, 1993
# Long options added by Greg Minshall, January 2020
# External variables: # External variables:
# Optind -- index in ARGV of first nonoption argument # Optind -- index in ARGV of first nonoption argument
# Optarg -- string value of argument to current option # Optarg -- string value of argument to current option
# Opterr -- if nonzero, print our own diagnostic # Opterr -- if nonzero, print our own diagnostic
# Optopt -- current option letter # Optopt -- current option letter
# Returns: # Returns:
# -1 at end of options # -1 at end of options
# "?" for unrecognized option # "?" for unrecognized option
# <c> a character representing the current option # <s> a string representing the current option
# Private Data: # Private Data:
# _opti -- index in multiflag option, e.g., -abc # _opti -- index in multiflag option, e.g., -abc
function getopt(argc, argv, options, thisopt, i) function getopt(argc, argv, options, longopts, thisopt, i, j)
{ {
if (length(options) == 0) # no options given if (length(options) == 0 && length(longopts) == 0)
return -1 return -1 # no options given
if (argv[Optind] == "--") { # all done if (argv[Optind] == "--") { # all done
Optind++ Optind++
_opti = 0 _opti = 0
return -1 return -1
} else if (argv[Optind] !~ /^-[^:[:space:]]/) { } else if (argv[Optind] !~ /^-[^:[:space:]]/) {
_opti = 0 _opti = 0
return -1 return -1
} }
if (_opti == 0) if (argv[Optind] !~ /^--/) { # if this is a short option
_opti = 2 if (_opti == 0)
thisopt = substr(argv[Optind], _opti, 1) _opti = 2
Optopt = thisopt thisopt = substr(argv[Optind], _opti, 1)
i = index(options, thisopt) Optopt = thisopt
if (i == 0) { i = index(options, thisopt)
if (Opterr) if (i == 0) {
printf("%c -- invalid option\n", thisopt) > "/dev/stderr" if (Opterr)
if (_opti >= length(argv[Optind])) { printf("%c -- invalid option\n", thisopt) > "/dev/stderr"
if (_opti >= length(argv[Optind])) {
Optind++
_opti = 0
} else
_opti++
return "?"
}
if (substr(options, i + 1, 1) == ":") {
# get option argument
if (length(substr(argv[Optind], _opti + 1)) > 0)
Optarg = substr(argv[Optind], _opti + 1)
else
Optarg = argv[++Optind]
_opti = 0
} else
Optarg = ""
if (_opti == 0 || _opti >= length(argv[Optind])) {
Optind++ Optind++
_opti = 0 _opti = 0
} else } else
_opti++ _opti++
return "?" return thisopt
} } else {
if (substr(options, i + 1, 1) == ":") { j = index(argv[Optind], "=")
# get option argument if (j > 0)
if (length(substr(argv[Optind], _opti + 1)) > 0) thisopt = substr(argv[Optind], 3, j - 3)
Optarg = substr(argv[Optind], _opti + 1)
else else
Optarg = argv[++Optind] thisopt = substr(argv[Optind], 3)
_opti = 0 Optopt = thisopt
} else i = match(longopts, "(^|,)" thisopt "($|[,:])")
Optarg = "" if (i == 0) {
if (_opti == 0 || _opti >= length(argv[Optind])) { if (Opterr)
printf("%s -- invalid option\n", thisopt) > "/dev/stderr"
Optind++
return "?"
}
if (substr(longopts, i+1+length(thisopt), 1) == ":") {
if (j > 0)
Optarg = substr(argv[Optind], j + 1)
else
Optarg = argv[++Optind]
} else
Optarg = ""
Optind++ Optind++
_opti = 0 return thisopt
} else }
_opti++
return thisopt
} }
BEGIN { BEGIN {
Opterr = 1 # default is to diagnose Opterr = 1 # default is to diagnose
Optind = 1 # skip ARGV[0] Optind = 1 # skip ARGV[0]
# test program # test program
if (_getopt_test) { if (_getopt_test) {
while ((_go_c = getopt(ARGC, ARGV, "ab:cd")) != -1) _myshortopts = "ab:cd"
printf("c = <%c>, Optarg = <%s>\n", _mylongopts = "longa,longb:,otherc,otherd"
_go_c, Optarg)
while ((_go_c = getopt(ARGC, ARGV, _myshortopts, _mylongopts)) != -1)
printf("c = <%s>, Optarg = <%s>\n", _go_c, Optarg)
printf("non-option arguments:\n") printf("non-option arguments:\n")
for (; Optind < ARGC; Optind++) for (; Optind < ARGC; Optind++)
printf("\tARGV[%d] = <%s>\n", printf("\tARGV[%d] = <%s>\n", Optind, ARGV[Optind])
Optind, ARGV[Optind])
} }
} }
 End of changes. 11 change blocks. 
33 lines changed or deleted 61 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)