Use maybe_line_continuations instead of ensure_buffer
The ensure_buffer function can read more than needed. The
maybe_line_continuations is preferred.
@@ -823,6 +823,8 @@ | ||
823 | 823 | void maybe_line_continuations(parsestate_T *ps, size_t index) |
824 | 824 | { |
825 | 825 | assert(index <= ps->src.length); |
826 | + if (index == ps->src.length) | |
827 | + read_more_input(ps); | |
826 | 828 | while (ps->src.contents[index] == L'\\' && |
827 | 829 | ps->src.contents[index + 1] == L'\n') |
828 | 830 | line_continuation(ps, index); |
@@ -890,7 +892,8 @@ | ||
890 | 892 | size_t count_name_length(parsestate_T *ps, bool isnamechar(wchar_t c)) |
891 | 893 | { |
892 | 894 | size_t saveindex = ps->index; |
893 | - while (ensure_buffer(ps, 1), isnamechar(ps->src.contents[ps->index])) | |
895 | + while (maybe_line_continuations(ps, ps->index), | |
896 | + isnamechar(ps->src.contents[ps->index])) | |
894 | 897 | ps->index++; |
895 | 898 | |
896 | 899 | size_t result = ps->index - saveindex; |
@@ -1761,7 +1764,7 @@ | ||
1761 | 1764 | } \ |
1762 | 1765 | } while (0) |
1763 | 1766 | |
1764 | - while (ensure_buffer(ps, 1), | |
1767 | + while (maybe_line_continuations(ps, ps->index), | |
1765 | 1768 | indq || !testfunc(ps->src.contents[ps->index])) { |
1766 | 1769 | |
1767 | 1770 | switch (ps->src.contents[ps->index]) { |
@@ -1854,11 +1857,12 @@ | ||
1854 | 1857 | { |
1855 | 1858 | switch (ps->src.contents[ps->index++]) { |
1856 | 1859 | case L'$': |
1857 | - ensure_buffer(ps, 2); | |
1860 | + maybe_line_continuations(ps, ps->index); | |
1858 | 1861 | switch (ps->src.contents[ps->index]) { |
1859 | 1862 | case L'{': |
1860 | 1863 | return parse_paramexp_in_brace(ps); |
1861 | 1864 | case L'(': |
1865 | + maybe_line_continuations(ps, ps->index + 1); | |
1862 | 1866 | if (ps->src.contents[ps->index + 1] == L'(') { |
1863 | 1867 | wordunit_T *wu = tryparse_arith(ps); |
1864 | 1868 | if (wu != NULL) |
@@ -1885,7 +1889,7 @@ | ||
1885 | 1889 | paramexp_T *pe; |
1886 | 1890 | size_t namelen; /* parameter name length */ |
1887 | 1891 | |
1888 | - ensure_buffer(ps, 1); | |
1892 | + maybe_line_continuations(ps, ps->index); | |
1889 | 1893 | switch (ps->src.contents[ps->index]) { |
1890 | 1894 | case L'@': case L'*': case L'#': case L'?': |
1891 | 1895 | case L'-': case L'$': case L'!': |
@@ -1932,13 +1936,15 @@ | ||
1932 | 1936 | ps->index++; |
1933 | 1937 | |
1934 | 1938 | /* parse PT_NUMBER */ |
1935 | - ensure_buffer(ps, 3); | |
1939 | + maybe_line_continuations(ps, ps->index); | |
1936 | 1940 | if (ps->src.contents[ps->index] == L'#') { |
1941 | + maybe_line_continuations(ps, ps->index + 1); | |
1937 | 1942 | switch (ps->src.contents[ps->index + 1]) { |
1938 | 1943 | case L'\0': case L'}': |
1939 | 1944 | case L'+': case L'=': case L':': case L'/': case L'%': |
1940 | 1945 | break; |
1941 | 1946 | case L'-': case L'?': case L'#': |
1947 | + maybe_line_continuations(ps, ps->index + 2); | |
1942 | 1948 | if (ps->src.contents[ps->index + 2] != L'}') |
1943 | 1949 | break; |
1944 | 1950 | /* falls thru! */ |
@@ -1950,7 +1956,7 @@ | ||
1950 | 1956 | } |
1951 | 1957 | |
1952 | 1958 | /* parse nested expansion */ |
1953 | - // ensure_buffer(2); // we've already called `ensure_buffer' | |
1959 | + // maybe_line_continuations(ps, ps->index); // already called above | |
1954 | 1960 | if (!posixly_correct && ps->src.contents[ps->index] == L'{') { |
1955 | 1961 | pe->pe_type |= PT_NEST; |
1956 | 1962 | pe->pe_nest = parse_paramexp_in_brace(ps); |
@@ -1957,14 +1963,15 @@ | ||
1957 | 1963 | } else if (!posixly_correct |
1958 | 1964 | && (ps->src.contents[ps->index] == L'`' |
1959 | 1965 | || (ps->src.contents[ps->index] == L'$' |
1960 | - && (ps->src.contents[ps->index + 1] == L'{' | |
1966 | + && (maybe_line_continuations(ps, ps->index + 1), | |
1967 | + ps->src.contents[ps->index + 1] == L'{' | |
1961 | 1968 | || ps->src.contents[ps->index + 1] == L'(')))) { |
1962 | 1969 | size_t neststartindex = ps->index; |
1963 | 1970 | pe->pe_nest = parse_special_word_unit(ps, false); |
1964 | - if (ps->index != neststartindex) | |
1965 | - pe->pe_type |= PT_NEST; | |
1966 | - else | |
1971 | + if (ps->index == neststartindex) | |
1967 | 1972 | goto parse_name; |
1973 | + pe->pe_type |= PT_NEST; | |
1974 | + maybe_line_continuations(ps, ps->index); | |
1968 | 1975 | } else { |
1969 | 1976 | parse_name:; |
1970 | 1977 | /* no nesting: parse parameter name normally */ |
@@ -1975,7 +1982,7 @@ | ||
1975 | 1982 | ps->index++; |
1976 | 1983 | break; |
1977 | 1984 | default: |
1978 | - while (ensure_buffer(ps, 1), | |
1985 | + while (maybe_line_continuations(ps, ps->index), | |
1979 | 1986 | is_name_char(ps->src.contents[ps->index])) |
1980 | 1987 | ps->index++; |
1981 | 1988 | break; |
@@ -1989,7 +1996,7 @@ | ||
1989 | 1996 | } |
1990 | 1997 | |
1991 | 1998 | /* parse indices */ |
1992 | - ensure_buffer(ps, 3); | |
1999 | + // maybe_line_continuations(ps, ps->index); // already called above | |
1993 | 2000 | if (!posixly_correct && ps->src.contents[ps->index] == L'[') { |
1994 | 2001 | ps->index++; |
1995 | 2002 | pe->pe_start = parse_word_to(ps, is_comma_or_closing_bracket); |
@@ -2001,20 +2008,22 @@ | ||
2001 | 2008 | if (pe->pe_end == NULL) |
2002 | 2009 | serror(ps, Ngt("the index is missing")); |
2003 | 2010 | } |
2004 | - if (ps->src.contents[ps->index] == L']') | |
2005 | - ps->index++; | |
2006 | - else | |
2011 | + if (ps->src.contents[ps->index] == L']') { | |
2012 | + maybe_line_continuations(ps, ++ps->index); | |
2013 | + } else { | |
2007 | 2014 | serror(ps, Ngt("`%ls' is missing"), L"]"); |
2008 | - ensure_buffer(ps, 3); | |
2015 | + } | |
2009 | 2016 | } |
2010 | 2017 | |
2011 | 2018 | /* parse PT_COLON */ |
2019 | + // maybe_line_continuations(ps, ps->index); // already called above | |
2012 | 2020 | if (ps->src.contents[ps->index] == L':') { |
2013 | 2021 | pe->pe_type |= PT_COLON; |
2014 | - ps->index++; | |
2022 | + maybe_line_continuations(ps, ++ps->index); | |
2015 | 2023 | } |
2016 | 2024 | |
2017 | 2025 | /* parse '-', '+', '#', etc. */ |
2026 | + // maybe_line_continuations(ps, ps->index); // already called above | |
2018 | 2027 | switch (ps->src.contents[ps->index]) { |
2019 | 2028 | case L'-': pe->pe_type |= PT_MINUS; goto parse_subst; |
2020 | 2029 | case L'+': pe->pe_type |= PT_PLUS; goto parse_subst; |
@@ -2043,6 +2052,7 @@ | ||
2043 | 2052 | } |
2044 | 2053 | |
2045 | 2054 | parse_match: |
2055 | + maybe_line_continuations(ps, ps->index + 1); | |
2046 | 2056 | if (pe->pe_type & PT_COLON) { |
2047 | 2057 | if ((pe->pe_type & PT_MASK) == PT_SUBST) |
2048 | 2058 | pe->pe_type |= PT_MATCHHEAD | PT_MATCHTAIL; |
@@ -2049,7 +2059,7 @@ | ||
2049 | 2059 | else |
2050 | 2060 | serror(ps, Ngt("invalid use of `%lc' in parameter expansion"), |
2051 | 2061 | (wint_t) L':'); |
2052 | - ps->index += 1; | |
2062 | + maybe_line_continuations(ps, ++ps->index); | |
2053 | 2063 | } else if (ps->src.contents[ps->index] == |
2054 | 2064 | ps->src.contents[ps->index + 1]) { |
2055 | 2065 | if ((pe->pe_type & PT_MASK) == PT_MATCH) |
@@ -2075,7 +2085,7 @@ | ||
2075 | 2085 | goto check_closing_brace; |
2076 | 2086 | } else { |
2077 | 2087 | pe->pe_match = parse_word_to(ps, is_slash_or_closing_brace); |
2078 | - ensure_buffer(ps, 1); | |
2088 | + // maybe_line_continuations(ps, ps->index); // called in parse_word_to | |
2079 | 2089 | if (ps->src.contents[ps->index] != L'/') |
2080 | 2090 | goto check_closing_brace; |
2081 | 2091 | } |
@@ -2085,7 +2095,7 @@ | ||
2085 | 2095 | pe->pe_subst = parse_word_to(ps, is_closing_brace); |
2086 | 2096 | |
2087 | 2097 | check_closing_brace: |
2088 | - ensure_buffer(ps, 1); | |
2098 | + // maybe_line_continuations(ps, ps->index); // already called above | |
2089 | 2099 | if (ps->src.contents[ps->index] == L'}') |
2090 | 2100 | ps->index++; |
2091 | 2101 | else |
@@ -2113,7 +2123,7 @@ | ||
2113 | 2123 | result->wu_type = WT_CMDSUB; |
2114 | 2124 | result->wu_cmdsub = extract_command_in_paren(ps); |
2115 | 2125 | |
2116 | - ensure_buffer(ps, 1); | |
2126 | + maybe_line_continuations(ps, ps->index); | |
2117 | 2127 | if (ps->src.contents[ps->index] == L')') |
2118 | 2128 | ps->index++; |
2119 | 2129 | else |
@@ -2188,7 +2198,7 @@ | ||
2188 | 2198 | assert(ps->src.contents[ps->index - 1] == L'`'); |
2189 | 2199 | wb_init(&buf); |
2190 | 2200 | for (;;) { |
2191 | - ensure_buffer(ps, 1); | |
2201 | + maybe_line_continuations(ps, ps->index); | |
2192 | 2202 | switch (ps->src.contents[ps->index]) { |
2193 | 2203 | case L'\0': |
2194 | 2204 | serror(ps, |
@@ -2241,7 +2251,7 @@ | ||
2241 | 2251 | int nestparen = 0; |
2242 | 2252 | |
2243 | 2253 | for (;;) { |
2244 | - ensure_buffer(ps, 1); | |
2254 | + maybe_line_continuations(ps, ps->index); | |
2245 | 2255 | switch (ps->src.contents[ps->index]) { |
2246 | 2256 | case L'\0': |
2247 | 2257 | serror(ps, Ngt("`%ls' is missing"), L"))"); |
@@ -2276,7 +2286,7 @@ | ||
2276 | 2286 | nestparen--; |
2277 | 2287 | if (nestparen >= 0) |
2278 | 2288 | break; |
2279 | - ensure_buffer(ps, 2); | |
2289 | + maybe_line_continuations(ps, ps->index + 1); | |
2280 | 2290 | switch (ps->src.contents[ps->index + 1]) { |
2281 | 2291 | case L')': |
2282 | 2292 | MAKE_WORDUNIT_STRING; |
@@ -2934,7 +2944,7 @@ | ||
2934 | 2944 | size_t startindex = ps->index; |
2935 | 2945 | |
2936 | 2946 | for (;;) { |
2937 | - ensure_buffer(ps, 1); | |
2947 | + maybe_line_continuations(ps, ps->index); | |
2938 | 2948 | switch (ps->src.contents[ps->index]) { |
2939 | 2949 | case L'\0': |
2940 | 2950 | goto done; |