• R/O
  • SSH
  • HTTPS

yash: 提交


Commit MetaInfo

修訂4080 (tree)
時間2020-09-21 12:29:05
作者magicant

Log Message

Extract common code from print_scalar & print_array

Change Summary

差異

--- yash/trunk/variable.c (revision 4079)
+++ yash/trunk/variable.c (revision 4080)
@@ -1570,6 +1570,8 @@
15701570 const wchar_t *name, const function_T *func,
15711571 const wchar_t *argv0, bool readonly)
15721572 __attribute__((nonnull));
1573+static char *vartype_option_string(vartype_T type)
1574+ __attribute__((malloc,warn_unused_result));
15731575 #if YASH_ENABLE_ARRAY
15741576 static int array_dump_all(const wchar_t *argv0);
15751577 static void array_remove_elements(
@@ -1827,7 +1829,7 @@
18271829 {
18281830 wchar_t *quotedvalue;
18291831 const char *format;
1830- xstrbuf_T opts;
1832+ char *opts;
18311833
18321834 if (var->v_value != NULL)
18331835 quotedvalue = quote_as_word(var->v_value);
@@ -1852,16 +1854,10 @@
18521854 case L't':
18531855 assert(wcscmp(argv0, L"typeset") == 0);
18541856 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, " -");
18621857 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);
18651861 break;
18661862 default:
18671863 assert(false);
@@ -1874,8 +1870,6 @@
18741870 void print_array(
18751871 const wchar_t *name, const variable_T *var, const wchar_t *argv0)
18761872 {
1877- xstrbuf_T opts;
1878-
18791873 if (!xprintf("%ls=(", name))
18801874 return;
18811875 if (var->v_valc > 0) {
@@ -1912,16 +1906,10 @@
19121906 goto typeset;
19131907 case L't':
19141908 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);
19251913 break;
19261914 default:
19271915 assert(false);
@@ -1969,6 +1957,21 @@
19691957 free(qname);
19701958 }
19711959
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+
19721975 #if YASH_ENABLE_HELP
19731976 const char typeset_help[] = Ngt(
19741977 "set or print variables"
Show on old repository browser