File: shpp.awk

package info (click to toggle)
dpatch 2.0.38+nmu1~bpo9+1
  • links: PTS, VCS
  • area: main
  • in suites: stretch-backports
  • size: 676 kB
  • sloc: sh: 1,013; makefile: 225; awk: 57; perl: 17
file content (78 lines) | stat: -rwxr-xr-x 2,031 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/awk -f
# shpp.awk -- Simple preprocessor for shell scripts
#
#  Copyright (C) 2003  Miles Bader <miles@gnu.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Written by Miles Bader <miles@gnu.org>

BEGIN {
  if ("srcdir" in ENVIRON)
    srcdir = ENVIRON["srcdir"]
  else
    srcdir = "."
}

function include_file(file, escape_single_quotes  ,hdr_comment_skip_state)
{
  file = srcdir "/" file
  hdr_comment_skip_state = 2
  while ((getline <file) > 0) {
    if (hdr_comment_skip_state) {
      if ($0 ~ /^(#:|[^#]|$)/)
	hdr_comment_skip_state = 0
      else {
	if ($0 ~ /^# *$/)
	  hdr_comment_skip_state = 1
	if (hdr_comment_skip_state == 1)
	  continue
      }
    }
    process_line(escape_single_quotes)
  }
  close (file)
}

# Process the input line in $0; destroys $0!
function process_line(escape_single_quotes,  file,val,var)
{
  if ($1 == "#:include") {
    file = $2
    print "# (---- beginning of " file " ----)"
    include_file(file, escape_single_quotes)
    print "# (---- end of " file " ----)"
  } else if ($1 == "#:var-include") {
    var = $2
    file = $3
    print "# (---- " var " defined from " file " ----)"
    printf ("%s='", var)
    include_file(file, 1)
    print "'"
    print "# (---- end of " var " defined from " file " ----)"
  } else if ($1 == "#:local-var" || $1 == "#:export-var") {
    if ($2 in ENVIRON) {
      val = ENVIRON[$2]
      # Escape any single quotes
      gsub (/'/, "'\\''", val)
      
      if ($1 == "#:export-var")
	printf ("%s='%s'; export %s\n", $2, val, $2)
      else
	printf ("%s='%s'\n", $2, val)
    }
  } else {
    if (escape_single_quotes)
      gsub (/'/, "'\\''")
    # Note we strip arch-tag: lines from the destination
    if ($0 !~ /^#* *arch-tag:/)
      print
  }
}

{ process_line(0) }

# arch-tag: 7f61e98c-a589-4905-a0a1-338a68eb133d