Simon Glass | 86b9c3e | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
| 2 | # |
| 3 | # Copyright 2021 Google, Inc |
| 4 | # |
| 5 | # SPDX-License-Identifier: GPL-2.0+ |
| 6 | # |
| 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. |
| 9 | |
| 10 | # The resulting output is: |
| 11 | # |
| 12 | # #define CONFIG_EXTRA_ENV_TEXT "<environment here>" |
| 13 | # |
| 14 | # If the input is empty, this script outputs a comment instead. |
| 15 | |
| 16 | BEGIN { |
| 17 | # env holds the env variable we are currently processing |
| 18 | env = ""; |
| 19 | ORS = "" |
| 20 | } |
| 21 | |
| 22 | # Skip empty lines, as these are generated by the clang preprocessor |
| 23 | NF { |
Simon Glass | 7acb322 | 2021-11-24 07:40:14 -0700 | [diff] [blame] | 24 | do_output = 0 |
| 25 | |
Simon Glass | 86b9c3e | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 26 | # Quote quotes |
| 27 | gsub("\"", "\\\"") |
| 28 | |
Simon Glass | 7acb322 | 2021-11-24 07:40:14 -0700 | [diff] [blame] | 29 | # Avoid using the non-POSIX third parameter to match(), by splitting |
| 30 | # the work into several steps. |
| 31 | has_var = match($0, "^([^ \t=][^ =]*)=(.*)$") |
| 32 | |
Simon Glass | 86b9c3e | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 33 | # Is this the start of a new environment variable? |
Simon Glass | 7acb322 | 2021-11-24 07:40:14 -0700 | [diff] [blame] | 34 | if (has_var) { |
Simon Glass | 86b9c3e | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 35 | if (length(env) != 0) { |
| 36 | # Record the value of the variable now completed |
| 37 | vars[var] = env |
Simon Glass | 7acb322 | 2021-11-24 07:40:14 -0700 | [diff] [blame] | 38 | do_output = 1 |
Simon Glass | 86b9c3e | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 39 | } |
Simon Glass | 7acb322 | 2021-11-24 07:40:14 -0700 | [diff] [blame] | 40 | |
| 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) |
Simon Glass | 86b9c3e | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 45 | |
| 46 | # Deal with += which concatenates the new string to the existing |
Simon Glass | 7acb322 | 2021-11-24 07:40:14 -0700 | [diff] [blame] | 47 | # variable. Again we are careful to use POSIX match() |
| 48 | if (length(env) != 0 && match(var, "^(.*)[+]$")) { |
| 49 | plusname = substr(var, RSTART, RLENGTH - 1) |
Simon Glass | 86b9c3e | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 50 | # Allow var\+=val to indicate that the variable name is |
| 51 | # var+ and this is not actually a concatenation |
Simon Glass | 7acb322 | 2021-11-24 07:40:14 -0700 | [diff] [blame] | 52 | if (substr(plusname, length(plusname)) == "\\") { |
Simon Glass | 86b9c3e | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 53 | # Drop the backslash |
| 54 | sub(/\\[+]$/, "+", var) |
| 55 | } else { |
Simon Glass | 7acb322 | 2021-11-24 07:40:14 -0700 | [diff] [blame] | 56 | var = plusname |
Simon Glass | 86b9c3e | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 57 | env = vars[var] env |
| 58 | } |
| 59 | } |
| 60 | } else { |
| 61 | # Change newline to space |
| 62 | gsub(/^[ \t]+/, "") |
| 63 | |
| 64 | # Don't keep leading spaces generated by the previous blank line |
| 65 | if (length(env) == 0) { |
| 66 | env = $0 |
| 67 | } else { |
| 68 | env = env " " $0 |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | END { |
| 74 | # Record the value of the variable now completed. If the variable is |
| 75 | # empty it is not set. |
| 76 | if (length(env) != 0) { |
| 77 | vars[var] = env |
Simon Glass | 7acb322 | 2021-11-24 07:40:14 -0700 | [diff] [blame] | 78 | do_output = 1 |
Simon Glass | 86b9c3e | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 79 | } |
| 80 | |
Simon Glass | 7acb322 | 2021-11-24 07:40:14 -0700 | [diff] [blame] | 81 | if (do_output) { |
Simon Glass | 86b9c3e | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 82 | printf("%s", "#define CONFIG_EXTRA_ENV_TEXT \"") |
| 83 | |
Simon Glass | 6910dbe | 2022-03-12 22:47:49 -0700 | [diff] [blame] | 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" |
Simon Glass | 86b9c3e | 2021-10-21 21:08:46 -0600 | [diff] [blame] | 88 | for (var in vars) { |
| 89 | env = vars[var] |
| 90 | print var "=" vars[var] "\\0" |
| 91 | } |
| 92 | print "\"\n" |
| 93 | } |
| 94 | } |