| 1 | #! /bin/sh |
| 2 | |
| 3 | # Output the contents of a shell script with sourced files inlined. |
| 4 | # Written by Gary V. Vaughan, 2012 |
| 5 | |
| 6 | # This is free software. There is NO warranty; not even for |
| 7 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 8 | # |
| 9 | # Copyright (C) 2012-2019 Bootstrap Authors |
| 10 | # |
| 11 | # This file is dual licensed under the terms of the MIT license |
| 12 | # <https://opensource.org/license/MIT>, and GPL version 2 or later |
| 13 | # <http://www.gnu.org/licenses/gpl-2.0.html>. You must apply one of |
| 14 | # these licenses when using or redistributing this software or any of |
| 15 | # the files within it. See the URLs above, or the file `LICENSE` |
| 16 | # included in the Bootstrap distribution for the full license texts. |
| 17 | |
| 18 | # Please report bugs or propose patches to: |
| 19 | # <https://github.com/gnulib-modules/bootstrap/issues> |
| 20 | |
| 21 | # Source required external libraries: |
| 22 | . `echo "$0" |${SED-sed} 's|[^/]*$||'`"funclib.sh" |
| 23 | . `echo "$0" |${SED-sed} 's|[^/]*$||'`"options-parser" |
| 24 | |
| 25 | # Set a version string for *this* script. |
| 26 | scriptversion=2019-02-19.15; # UTC |
| 27 | |
| 28 | |
| 29 | ## ------ ## |
| 30 | ## Usage. ## |
| 31 | ## ------ ## |
| 32 | |
| 33 | # Run 'build-aux/inline-source --help' for help with using this script |
| 34 | # from the command line. |
| 35 | |
| 36 | # Recursively scan through a FILE passed on the command line, replacing |
| 37 | # either of the following: |
| 38 | # . "relative/file" |
| 39 | # . `echo "$0" |edit`"relative/file" |
| 40 | # with the contents of the referenced files. |
| 41 | |
| 42 | |
| 43 | ## ---------------- ## |
| 44 | ## Options parsing. ## |
| 45 | ## ---------------- ## |
| 46 | |
| 47 | usage='$progpath [OPTION]... FILE' |
| 48 | |
| 49 | # Short help message in response to '-h'. |
| 50 | usage_message='Options: |
| 51 | --debug enable verbose shell tracing |
| 52 | --version print version information and exit |
| 53 | -h, --help print help message and exit |
| 54 | ' |
| 55 | |
| 56 | long_help_message="\ |
| 57 | Report bugs to <[email protected]> |
| 58 | General help using GNU software: <http://www.gnu.org/gethelp/>." |
| 59 | |
| 60 | func_options ${1+"$@"} |
| 61 | eval set dummy "$func_options_result"; shift |
| 62 | |
| 63 | |
| 64 | ## -------------------- ## |
| 65 | ## Resource management. ## |
| 66 | ## -------------------- ## |
| 67 | |
| 68 | # require_AWK |
| 69 | # ----------- |
| 70 | # Search for a "not hopeless" awk. |
| 71 | require_AWK=func_require_AWK |
| 72 | func_require_AWK () |
| 73 | { |
| 74 | $debug_cmd |
| 75 | |
| 76 | test -n "$AWK" || { |
| 77 | # Find the first executable in the list. |
| 78 | for _G_prog in gawk mawk nawk awk |
| 79 | do |
| 80 | require_AWK_IFS=$IFS |
| 81 | IFS=${PATH_SEPARATOR-:} |
| 82 | for _G_dir in $PATH |
| 83 | do |
| 84 | IFS=$require_AWK_IFS |
| 85 | if test -f "$_G_dir/$_G_prog" && test -x "$_G_dir/$_G_prog" |
| 86 | then |
| 87 | AWK=$_G_dir/$_G_prog |
| 88 | break 2 |
| 89 | fi |
| 90 | done |
| 91 | IFS=$require_AWK_IFS |
| 92 | done |
| 93 | } |
| 94 | |
| 95 | test -n "$AWK" || func_fatal_error "\ |
| 96 | Please install GNU Awk, or 'export AWK=/path/to/gnu/awk'." |
| 97 | |
| 98 | func_verbose "found '$AWK'." |
| 99 | |
| 100 | require_AWK=: |
| 101 | } |
| 102 | |
| 103 | |
| 104 | ## --------------- ## |
| 105 | ## Core functions. ## |
| 106 | ## --------------- ## |
| 107 | |
| 108 | # func_include LINE |
| 109 | # ----------------- |
| 110 | # Output the contents of file included by LINE. |
| 111 | func_include () |
| 112 | { |
| 113 | $require_AWK |
| 114 | |
| 115 | test -f "$1" \ |
| 116 | || func_fatal_error "file '$1' not found" |
| 117 | |
| 118 | _G_scriptdir=`echo "$1" |$SED 's|[^/]*$||'` |
| 119 | test -n "$_G_scriptdir" || _G_scriptdir="./" |
| 120 | |
| 121 | $AWK ' |
| 122 | BEGIN { magic = '${_RECURSE_MAGIC-0}'; } |
| 123 | |
| 124 | /^#!/ && magic == 0 { |
| 125 | print $0; |
| 126 | print "## DO NOT EDIT - This file generated from '$1'"; |
| 127 | print "## by '$progname' v'$scriptversion'"; |
| 128 | magic++; |
| 129 | next; |
| 130 | } |
| 131 | |
| 132 | /^\. ['\''"].*['\''"]$/ { |
| 133 | file = substr ($2, 2, length ($2) -2); |
| 134 | system (sprintf ("env _RECURSE_MAGIC=%d '$progpath' %s", magic, file)); |
| 135 | next; |
| 136 | } |
| 137 | |
| 138 | /^\. `echo [^`]*`['\''"][^'\''"]*['\''"]$/ { |
| 139 | tail = substr ($0, match ($0, /`['\''"]/)); |
| 140 | file = substr (tail, 3, length (tail) -3); |
| 141 | system (sprintf ("env _RECURSE_MAGIC=%d '$progpath' '"$_G_scriptdir"'%s", magic, file)); |
| 142 | next; |
| 143 | } |
| 144 | |
| 145 | { print; } |
| 146 | ' < "$1" |
| 147 | } |
| 148 | |
| 149 | func_include "$1" |
| 150 | |
| 151 | exit 0 |
| 152 | |
| 153 | # Local variables: |
| 154 | # mode: shell-script |
| 155 | # sh-indentation: 2 |
| 156 | # eval: (add-hook 'before-save-hook 'time-stamp) |
| 157 | # time-stamp-pattern: "30/scriptversion=%:y-%02m-%02d.%02H; # UTC" |
| 158 | # time-stamp-time-zone: "UTC" |
| 159 | # End: |