blob: 57d0fc8f3ba6145f89fa5bdac6ce537c56753023 [file] [log] [blame]
Simon Glass7327fe72021-10-21 21:08:46 -06001# 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
16BEGIN {
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
23NF {
24 # Quote quotes
25 gsub("\"", "\\\"")
26
27 # Is this the start of a new environment variable?
28 if (match($0, "^([^ \t=][^ =]*)=(.*)$", arr)) {
29 if (length(env) != 0) {
30 # Record the value of the variable now completed
31 vars[var] = env
32 }
33 var = arr[1]
34 env = arr[2]
35
36 # Deal with += which concatenates the new string to the existing
37 # variable
38 if (length(env) != 0 && match(var, "^(.*)[+]$", var_arr))
39 {
40 # Allow var\+=val to indicate that the variable name is
41 # var+ and this is not actually a concatenation
42 if (substr(var_arr[1], length(var_arr[1])) == "\\") {
43 # Drop the backslash
44 sub(/\\[+]$/, "+", var)
45 } else {
46 var = var_arr[1]
47 env = vars[var] env
48 }
49 }
50 } else {
51 # Change newline to space
52 gsub(/^[ \t]+/, "")
53
54 # Don't keep leading spaces generated by the previous blank line
55 if (length(env) == 0) {
56 env = $0
57 } else {
58 env = env " " $0
59 }
60 }
61}
62
63END {
64 # Record the value of the variable now completed. If the variable is
65 # empty it is not set.
66 if (length(env) != 0) {
67 vars[var] = env
68 }
69
70 if (length(vars) != 0) {
71 printf("%s", "#define CONFIG_EXTRA_ENV_TEXT \"")
72
73 # Print out all the variables
74 for (var in vars) {
75 env = vars[var]
76 print var "=" vars[var] "\\0"
77 }
78 print "\"\n"
79 }
80}