Extract common code from print_scalar & print_array
@@ -1570,6 +1570,8 @@ | ||
1570 | 1570 | const wchar_t *name, const function_T *func, |
1571 | 1571 | const wchar_t *argv0, bool readonly) |
1572 | 1572 | __attribute__((nonnull)); |
1573 | +static char *vartype_option_string(vartype_T type) | |
1574 | + __attribute__((malloc,warn_unused_result)); | |
1573 | 1575 | #if YASH_ENABLE_ARRAY |
1574 | 1576 | static int array_dump_all(const wchar_t *argv0); |
1575 | 1577 | static void array_remove_elements( |
@@ -1827,7 +1829,7 @@ | ||
1827 | 1829 | { |
1828 | 1830 | wchar_t *quotedvalue; |
1829 | 1831 | const char *format; |
1830 | - xstrbuf_T opts; | |
1832 | + char *opts; | |
1831 | 1833 | |
1832 | 1834 | if (var->v_value != NULL) |
1833 | 1835 | quotedvalue = quote_as_word(var->v_value); |
@@ -1852,16 +1854,10 @@ | ||
1852 | 1854 | case L't': |
1853 | 1855 | assert(wcscmp(argv0, L"typeset") == 0); |
1854 | 1856 | typeset: |
1855 | - sb_initwithmax(&opts, 4); | |
1856 | - if (var->v_type & VF_EXPORT) | |
1857 | - sb_ccat(&opts, 'x'); | |
1858 | - if (var->v_type & VF_READONLY) | |
1859 | - sb_ccat(&opts, 'r'); | |
1860 | - if (opts.length > 0) | |
1861 | - sb_insert(&opts, 0, " -"); | |
1862 | 1857 | format = (quotedvalue != NULL) ? "%ls%s %ls=%ls\n" : "%ls%s %ls\n"; |
1863 | - xprintf(format, argv0, opts.contents, name, quotedvalue); | |
1864 | - sb_destroy(&opts); | |
1858 | + opts = vartype_option_string(var->v_type); | |
1859 | + xprintf(format, argv0, opts, name, quotedvalue); | |
1860 | + free(opts); | |
1865 | 1861 | break; |
1866 | 1862 | default: |
1867 | 1863 | assert(false); |
@@ -1874,8 +1870,6 @@ | ||
1874 | 1870 | void print_array( |
1875 | 1871 | const wchar_t *name, const variable_T *var, const wchar_t *argv0) |
1876 | 1872 | { |
1877 | - xstrbuf_T opts; | |
1878 | - | |
1879 | 1873 | if (!xprintf("%ls=(", name)) |
1880 | 1874 | return; |
1881 | 1875 | if (var->v_valc > 0) { |
@@ -1912,16 +1906,10 @@ | ||
1912 | 1906 | goto typeset; |
1913 | 1907 | case L't': |
1914 | 1908 | assert(wcscmp(argv0, L"typeset") == 0); |
1915 | -typeset: | |
1916 | - sb_initwithmax(&opts, 4); | |
1917 | - if (var->v_type & VF_EXPORT) | |
1918 | - sb_ccat(&opts, 'x'); | |
1919 | - if (var->v_type & VF_READONLY) | |
1920 | - sb_ccat(&opts, 'r'); | |
1921 | - if (opts.length > 0) | |
1922 | - sb_insert(&opts, 0, " -"); | |
1923 | - xprintf("%ls%s %ls\n", argv0, opts.contents, name); | |
1924 | - sb_destroy(&opts); | |
1909 | +typeset:; | |
1910 | + char *opts = vartype_option_string(var->v_type); | |
1911 | + xprintf("%ls%s %ls\n", argv0, opts, name); | |
1912 | + free(opts); | |
1925 | 1913 | break; |
1926 | 1914 | default: |
1927 | 1915 | assert(false); |
@@ -1969,6 +1957,21 @@ | ||
1969 | 1957 | free(qname); |
1970 | 1958 | } |
1971 | 1959 | |
1960 | +/* Returns a newly malloced string that specifies options for the typeset | |
1961 | + * built-in that can be used to restore the given variable type. */ | |
1962 | +char *vartype_option_string(vartype_T type) | |
1963 | +{ | |
1964 | + xstrbuf_T opts; | |
1965 | + sb_initwithmax(&opts, 4); | |
1966 | + if (type & VF_EXPORT) | |
1967 | + sb_ccat(&opts, 'x'); | |
1968 | + if (type & VF_READONLY) | |
1969 | + sb_ccat(&opts, 'r'); | |
1970 | + if (opts.length > 0) | |
1971 | + sb_insert(&opts, 0, " -"); | |
1972 | + return sb_tostr(&opts); | |
1973 | +} | |
1974 | + | |
1972 | 1975 | #if YASH_ENABLE_HELP |
1973 | 1976 | const char typeset_help[] = Ngt( |
1974 | 1977 | "set or print variables" |