File: glob.awk

package info (click to toggle)
runawk 1.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 712 kB
  • sloc: awk: 1,127; ansic: 736; sh: 420; makefile: 103
file content (44 lines) | stat: -rw-r--r-- 1,165 bytes parent folder | download | duplicates (4)
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
# Written by Aleksey Cheusov <vle@gmx.net>, public domain
#
# This awk module is a part of RunAWK distribution,
#        http://sourceforge.net/projects/runawk
#
############################################################

# =head2 glob.awk
#
# =over 2
#
# =item I<glob2ere (PATTERN)>
#
# convert glob PATTERN
# (http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_13)
# to equivalent extended regular expression
# (http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap09.html#tag_09_04)
#
# =back
#

#use "multisub.awk"

BEGIN {
	__g2e="^:\\^   $:[$]   (:[(]   ):[)]   {:[{]   }:[}]"
	__g2e=__g2e "   \\[([^\\[\\]]|\\\\\\[|\\\\\\])*\\]:&   .:[.]   *:.*   +:[+]"
	__g2e=__g2e "   ?:.   |:[|]   \\\\:\\\\"
	__g2e=__g2e "   \\^:\\^   \\$:[$]   \\(:[(]   \\):[)]   \\{:[{]   \\}:[}]"
	__g2e=__g2e "   \\[:\\[   \\]:\\]   \\.:[.]   \\*:[*]   \\+:[+]   \\?:[?]"
	__g2e=__g2e "   \\|:[|]   \\:"
}

function glob2ere (p){
	return multisub(p, __g2e, "&")
}

function glob (s, p,                 re){
	if (p in __runawk_glob2ere)
		re = __runawk_glob2ere [p]
	else
		re = __runawk_glob2ere [p] = ("^" glob2ere(p) "$")

	return s ~ re
}