File: getlong.awk

package info (click to toggle)
dpkg-awk 1.2+nmu2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 56 kB
  • sloc: awk: 364; sh: 3; makefile: 2
file content (129 lines) | stat: -rw-r--r-- 3,311 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
BEGIN{ oflags = ":;#" }

function option_split_helper(ndx, s_o, l_o, val)
{
	if (length(ndx) < 3)
		s_o[ndx] = val
	else
		l_o[ndx] = val
}
function option_split(opt, s_o, s_o_f, l_o, l_o_f,
	a, opts, itm, flags)
{
	split(opt, opts, " ")
	for (a in opts) {
#print "opts[" a "]='" opts[a] "'\n"
		itm = opts[a]
#print itm " "
		flags = ""
		while (match(itm, "^[" oflags "]")) {
			flags = flags substr(itm, 1, 1)
			itm = substr(itm, 2)
#print itm " "
		}
#print "\n"
		option_split_helper(itm, s_o, l_o, "")
		if (length(flags)) option_split_helper(itm, s_o_f, l_o_f, flags)
		delete opts[a]
	}
}
function option_match(header, text, opts, opts_flags, argv, argc, help_text,
	ret_val, fchar, flags, value)
{
	itm = argv[c]
	gsub(/=.*$/, "", itm)
#print "argv[" c "]='" argv[c] "' itm='" itm "'\n"
	ret_val = 0
#for (a in opts) print "o_m opts[" a "]='" opts[a] "'\n"
	if (match(itm, "^" header )){
#print "match!\n"
		sub("^" header, "", itm)
		if (itm in opts){
			if (itm in opts_flags){
				flags = opts_flags[itm]
#print "o_m flags=" flags "\n"
				for (a = 1; a <= length(oflags); a++) {
					fchar = substr(oflags, a, 1)
					if (match(flags, "[" fchar "]")) {
						if ((fchar == ":") || (fchar == ";")) {
							if (match(argv[c], /=.*$/)){
								sub(/^.*=/, "", argv[c])
								value = argv[c]
								delete argv[c]
							} else {
								delete argv[c]
								if ((c++) <= argc) {
									if (((fchar == ";") && !(match(argv[c], /^-/))) || (fchar == ":")) {
										value = argv[c]
										delete argv[c]
									}
								} else
									value = ""
							}
						}
						if (fchar == "#") {
#print "o_m number\n"
							value = options[itm]
							if (match(argv[c], /=.*$/)){
								sub(/^.*=/, "", argv[c])
								value = argv[c]
								delete argv[c]
							} else {
								delete argv[c]
								value++
							}
						}
					}
				}
			} else
				value = ""
			options[itm] = value
		} else
			if (itm) {
				print "illegal " text " option! '" itm "'\n"
				if(help_text)
					print script_exedir "/" script_exebase " [args]\n\n" help_text "\n"
				exit
			}
		ret_val=1
	}
	return ret_val
}
function option_parse(opt_list, relation, help_text, argc, argv,
	short_opts, short_opts_value,
	long_opts, long_opts_value,
	m, rels, short, long)
{
	option_split(opt_list " :exebase :exedir help h", short_opts, short_opts_value, long_opts, long_opts_value)
	for (c = 1; c <= argc; c++) {
		if (option_match("--", "long", long_opts, long_opts_value, argv, argc, help_text)) continue
		if (option_match("-", "short", short_opts, short_opts_value, argv, argc, help_text)) continue
	}
	split(relation " h=help", rels, " ")
	for ( c in rels) {
		equal = index(rels[c], "=")
		first = substr(rels[c], 1, equal-1)
		if (first in options) {
			second = substr(rels[c], equal+1)
			if (!(second in options))
				# Only move the 'first' option into the
				# 'second' slot if the 'second' one
				# hasn't been defined yet.
				options[second] = options[first]
			delete options[first]
		}
	}
	if ("exebase" in options) {
		script_exebase = options["exebase"]
		delete options["exebase"]
	}
	if ("exedir" in options) {
		script_exedir = options["exedir"]
		delete options["exedir"]
	}
	if ("help" in options) {
		print script_exedir "/" script_exebase " [args]\n\n" help_text "\n"
		exit
	}
}