1 # SPDX-License-Identifier: GPL-2.0+
3 # Copyright 2021 Google, Inc
5 # SPDX-License-Identifier: GPL-2.0+
7 # Awk script to parse a text file containing an environment and convert it
8 # to a C string which can be compiled into U-Boot.
10 # The resulting output is:
12 # #define CONFIG_EXTRA_ENV_TEXT "<environment here>"
14 # If the input is empty, this script outputs a comment instead.
17 # env holds the env variable we are currently processing
22 # Skip empty lines, as these are generated by the clang preprocessor
29 # Avoid using the non-POSIX third parameter to match(), by splitting
30 # the work into several steps.
31 has_var = match($0, "^([^ \t=][^ =]*)=(.*)$")
33 # Is this the start of a new environment variable?
35 if (length(env) != 0) {
36 # Record the value of the variable now completed
41 # Collect the variable name. The value follows the '='
42 match($0, "^([^ \t=][^ =]*)=")
43 var = substr($0, 1, RLENGTH - 1)
44 env = substr($0, RLENGTH + 1)
46 # Deal with += which concatenates the new string to the existing
47 # variable. Again we are careful to use POSIX match()
48 if (length(env) != 0 && match(var, "^(.*)[+]$")) {
49 plusname = substr(var, RSTART, RLENGTH - 1)
50 # Allow var\+=val to indicate that the variable name is
51 # var+ and this is not actually a concatenation
52 if (substr(plusname, length(plusname)) == "\\") {
54 sub(/\\[+]$/, "+", var)
61 # Change newline to space
64 # Don't keep leading spaces generated by the previous blank line
65 if (length(env) == 0) {
74 # Record the value of the variable now completed. If the variable is
75 # empty it is not set.
76 if (length(env) != 0) {
82 printf("%s", "#define CONFIG_EXTRA_ENV_TEXT \"")
84 # Print out all the variables by alphabetic order, if using
85 # gawk. This allows test_env_test.py to work on both awk (where
86 # this next line does nothing)
87 PROCINFO["sorted_in"] = "@ind_str_asc"
90 print var "=" vars[var] "\\0"