summaryrefslogtreecommitdiffstats
path: root/bin/pbfields
blob: 052c6439dc0d74a08e7f50170191bf2fa0740ac8 (plain)
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
130
131
132
133
134
135
#!/usr/bin/awk -f

BEGIN {
	fieldstr = "pkgname _ver pkgver pkgrel pkgdesc epoch" \
		" *arch *license *options" \
		" install changelog" \
		" *depends *makedepends *checkdepends *optdepends" \
		" *conflicts *provides" \
		" url *source *noextract *md5sums *sha512sums"
	fcount = split(fieldstr, fields)
	for(i = 1; i <= fcount; i++){
		f = fields[i]
		if(sub(/^[*]/, "", f)){
			fields[i] = f
			arrfield[f] = 1
		}else{
			strfield[f] = 1
		}
	}

	COLS = 78; FS = "\n"; RS = ""
}

NF < 2 { next }

$1 == "packager" { packager = $2; next }

$1 == "maintainer" { maintainer = $2; next }

# pkgdesc has the only value where parameters do not expand.
$1 == "pkgdesc" { values[$1] = qnoexpand($2); next }

{
	for(i = 2; i <= NF; i++) $i = qexpand($i)
	if($1 in strfield){
		values[$1] = $2
	}else if($1 in arrfield){
		values[$1] = wraparray(length($1) + 2)
	}else if($1 ~ /^_/){
		vars[++vcount] = $1
		if(NF > 2){
			values[$1] = wraparray(length($1) + 2)
		}else{
			values[$1] = $2
		}
	}
}

END {
	if(maintainer){
		print "# Maintainer: " maintainer "\n"
	}else if(packager){
		print "# Packager: " packager "\n"
	}else{
		print "# Packager: Anonymous\n"
	}

	ORS = "\n";

	# Loop through our list of PKGBUILD field names so they are always
	# printed in the same order, matching the PKGBUILD manpage.
	for(i = 1; i <= fcount; i++){
		f = fields[i]
		if(!(f in values)){
			continue
		}
		v = values[f]
		print f "=" v
	}

	# Print our custom variables in the order they appeared.
	for(i = 1; i <= vcount; i++){
		v = vars[i]
		print v "=" values[v]
	}
}

function wraparray (indent)
{
	if(NF == 1) return "()" # this shouldn't happen but just in case.

	line = ""
	delete lines
	linecount = 0

	i = 2
	while(i <= NF){
		linelen = length(line)

		if((indent + linelen + 1 + length($i) > COLS) && linelen > 0){
			lines[++linecount] = line
			line = ""
		}else{
			if(linelen == 0) line = $(i++)
			else line = line " " $(i++)
		}
	}

	if(length(line) > 0) lines[++linecount] = line

	indtxt = sprintf("%" indent "s", "")
	txt = "(" lines[1]
	for(i=2; i<=linecount; i++) txt = txt "\n" indtxt lines[i]
	txt = txt ")"

	return txt
}

# Quote field value so that parameters ARE expanded.
# NEVER expand command substition (ie backtick or $(...))
function qexpand (v)
{
	if(v ~ /[$']/){
		gsub(/["`]/, "\\\\&", v)
		gsub(/[$][(]/, "\\$(", v)
		return sprintf("\"%s\"", v)
	}else if(v ~ /[ <>`"]/){
		return sprintf("'%s'", v)
	}else{
		return v
	}
}

# Quote field value so that parameters ARE NOT expanded.
function qnoexpand (v)
{
	if(v ~ /'/){
		gsub(/[$"`]/, "\\\\&", v)
		return sprintf("\"%s\"", v)
	}else if(v ~ /[ $"`<>]/){
		return sprintf("'%s'", v)
	}else{
		return v
	}
}