# This is the lexer's old file-splitting code: # eval "$(printf '%s' "${buf}" | awk -v FS='' -v j=0 \ # -v squote="'" -v esc_squote="'\\\\''" ' # { # for (i = 1; i <= NF; ++i) { # sub(squote, esc_squote, $i); # printf("lbufv_%d=" squote "%s" squote "\n", # j++, $i); # }; # printf("lbufv_%d=" squote "\n" squote "\n", j++); # } # ')" # It relies on behavior specific to GNU awk. # POSIX on awk's FS: "If FS is a null string, the behavior is unspecified." # Cf. regarding other implementations. # So we need to get even more creative. US="$(printf '\037.')"; US="${US%.}" buf='Hello, world! Hi! ' # Use sed to preprocess the buffer and give awk a proper field separator. printf '%s' "${buf}" | sed "s/\\(.\\)/\\1${US}/g" | \ awk -v FS="${US}" -v j=0 -v squote="'" -v esc_squote="'\\\\''" ' { for (i = 1; i <= NF; ++i) { if ($i == "") { continue; } sub(squote, esc_squote, $i); printf("lbufv_%d=" squote "%s" squote "\n", j++, $i); }; printf("lbufv_%d=" squote "\n" squote "\n", j++); } ' # And we might as well get rid of awk and use sed and SCL. i=0 IFS="${US}" for c in $(printf '%s' "${buf}" | sed -n " 1h; # Put the first line in the hold space. 1!H; # Append to the hold space each subsequent line. \${ # Once the hold space contains the whole buffer... g; # Prepare to edit the buffer. s/\\(.\\)/\\1${US}/g; # Put US after each char. p; # Print the result. }; "); do printf "lbufv_%d='%s'\n" ${i} "${c}" i=$((${i} + 1)) done unset IFS