POSIX compliance

According to the POSIX standard[0], the length() function only supports strings.
Although most common awk implementations support array length() as an
extension[1], it is better to make the program portable.

GNU Awk has a --posix flag that ensures that the script is POSIX compliant.
Running markdown.awk with this flag results in an error. This can be fixed by
getting the length of the array returned by split().

[0]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/awk.html
[1]: https://www.gnu.org/software/gawk/manual/html_node/Common-Extensions.html
This commit is contained in:
arslee07 2024-08-19 00:20:55 +09:00 committed by Konstantin Nazarov
parent dde0ebe599
commit 030c80da6a
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22

View file

@ -100,10 +100,10 @@ function parse_list(str, buf, result, i, ind, line, lines, indent, is_bullet)
result = "";
buf = "";
split(str, lines, "\n");
num_lines = split(str, lines, "\n");
str = ""
for (i=1; i<=length(lines); i++) {
for (i=1; i<=num_lines; i++) {
line = lines[i];
if (match(line, /^[[:space:]]*[-+*][[:space:]]/) ||
@ -113,7 +113,7 @@ function parse_list(str, buf, result, i, ind, line, lines, indent, is_bullet)
str = join_lines(rstrip(str), lstrip(line), " ");
}
split(str, lines, "\n")
num_lines = split(str, lines, "\n")
indent = match(str, /[^ ]/);
is_bullet = match(str, /^[[:space:]]*[-+*][[:space:]]/)
@ -123,7 +123,7 @@ function parse_list(str, buf, result, i, ind, line, lines, indent, is_bullet)
else
result = "<ol>\n"
for (i=1; i<=length(lines); i++) {
for (i=1; i<=num_lines; i++) {
line = lines[i];
if (match(line, "[^ ]") > indent) {
@ -367,10 +367,10 @@ function parse_line(str, result, end, i, c) {
}
function parse_blockquote(str, i, lines, line, buf, result) {
split(str, lines, "\n");
num_lines = split(str, lines, "\n");
str = ""
for (i=1; i<=length(lines); i++) {
for (i=1; i<=num_lines; i++) {
line = lines[i];
if (match(line, /^>/))
@ -379,11 +379,11 @@ function parse_blockquote(str, i, lines, line, buf, result) {
str = join_lines(rstrip(str), lstrip(line), " ");
}
split(str, lines, "\n");
num_lines = split(str, lines, "\n");
result = "<blockquote>";
buf = "";
for (i=1; i<=length(lines); i++) {
for (i=1; i<=num_lines; i++) {
line = lines[i];
gsub(/^> ?/, "", line);
@ -409,9 +409,9 @@ function parse_code(str, i, lines, result) {
}
if (match(str, /^ /)) {
result = "";
split(str, lines, "\n");
num_lines = split(str, lines, "\n");
for (i=1; i<=length(lines); i++) {
for (i=1; i<=num_lines; i++) {
line = lines[i];
gsub(/^ /, "", line);
result = result "\n" line;
@ -424,8 +424,8 @@ function parse_code(str, i, lines, result) {
}
function is_metadata(str, i, lines, line) {
split(str, lines, "\n");
for (i=1; i<=length(lines); i++) {
num_lines = split(str, lines, "\n");
for (i=1; i<=num_lines; i++) {
line = lines[i];
if (! match(line, /^[^ ]+: .+$/))
@ -444,13 +444,13 @@ function col_count(str, i, count) {
}
function is_table(str, i, lines, line) {
split(str, lines, "\n");
num_lines = split(str, lines, "\n");
num_cols = col_count(lines[1]);
if (!match(lines[2], /^([[:space:]]*\|[[:space:]]*-{3,}[[:space:]])*\|[[:space:]]*/)) {
return 0;
}
for (i=2; i<=length(lines); i++) {
for (i=2; i<=num_lines; i++) {
line = lines[i];
if (col_count(line) != num_cols)
@ -461,7 +461,7 @@ function is_table(str, i, lines, line) {
}
function parse_table(str, num_cols, lines, line, cols, i, j) {
split(str, lines, "\n");
num_lines = split(str, lines, "\n");
num_cols = col_count(lines[1]);
split(lines[1], cols, "|");
@ -471,7 +471,7 @@ function parse_table(str, num_cols, lines, line, cols, i, j) {
}
res = res "</tr>\n"
for (i=3; i<=length(lines); i++) {
for (i=3; i<=num_lines; i++) {
line = lines[i];
split(line, cols, "|");
res=res "<tr>\n";
@ -515,11 +515,11 @@ function parse_block(str) {
}
function parse_body(str, body, line, lines, result, i) {
split(str, lines, "\n");
num_lines = split(str, lines, "\n");
result = "";
body = "";
for (i=1; i<=length(lines); i++) {
for (i=1; i<=num_lines; i++) {
line = lines[i];
if (line_continues(body, line)) {
if (body != "")