Make stripctrl_string take an existing StripCtrlChars.

Now instead of making a StripCtrlChars just for that function call, it
uses an existing one, pointing it at the output strbuf via
stripctrl_retarget.

This adds flexibility (now you can use the same convenient string-
sanitising function with a StripCtrl configured in any way you like)
and also saves pointless setting-up and tearing-down of identical sccs
all the time.

The existing call sites in PSCP and PSFTP now use a static
StripCtrlChars instance that was made at program startup.
This commit is contained in:
Simon Tatham 2019-03-09 16:03:40 +00:00
Родитель cfef137ea2
Коммит d049f0ab6c
4 изменённых файлов: 20 добавлений и 13 удалений

6
misc.h
Просмотреть файл

@ -387,10 +387,10 @@ StripCtrlChars *stripctrl_new_term_fn(
void stripctrl_retarget(StripCtrlChars *sccpub, BinarySink *new_bs_out); void stripctrl_retarget(StripCtrlChars *sccpub, BinarySink *new_bs_out);
void stripctrl_reset(StripCtrlChars *sccpub); void stripctrl_reset(StripCtrlChars *sccpub);
void stripctrl_free(StripCtrlChars *sanpub); void stripctrl_free(StripCtrlChars *sanpub);
char *stripctrl_string_ptrlen(ptrlen str); char *stripctrl_string_ptrlen(StripCtrlChars *sccpub, ptrlen str);
static inline char *stripctrl_string(const char *str) static inline char *stripctrl_string(StripCtrlChars *sccpub, const char *str)
{ {
return stripctrl_string_ptrlen(ptrlen_from_asciz(str)); return stripctrl_string_ptrlen(sccpub, ptrlen_from_asciz(str));
} }
#endif #endif

10
pscp.c
Просмотреть файл

@ -256,8 +256,9 @@ static NORETURN void bump(const char *fmt, ...)
* version of a string for display, and free it automatically * version of a string for display, and free it automatically
* afterwards. * afterwards.
*/ */
#define with_stripctrl(varname, input) \ static StripCtrlChars *string_scc;
for (char *varname = stripctrl_string(input); varname; \ #define with_stripctrl(varname, input) \
for (char *varname = stripctrl_string(string_scc, input); varname; \
sfree(varname), varname = NULL) sfree(varname), varname = NULL)
/* /*
@ -1889,7 +1890,8 @@ static void sink(const char *targ, const char *src)
stat_bytes = 0; stat_bytes = 0;
stat_starttime = time(NULL); stat_starttime = time(NULL);
stat_lasttime = 0; stat_lasttime = 0;
stat_name = stripctrl_string(stripslashes(destfname, true)); stat_name = stripctrl_string(
string_scc, stripslashes(destfname, true));
received = 0; received = 0;
while (received < act.size) { while (received < act.size) {
@ -2319,6 +2321,8 @@ int psftp_main(int argc, char *argv[])
stderr_bs = BinarySink_UPCAST(stderr_scc); stderr_bs = BinarySink_UPCAST(stderr_scc);
} }
string_scc = stripctrl_new(NULL, false, L'\0');
if (list) { if (list) {
if (argc != 1) if (argc != 1)
usage(); usage();

Просмотреть файл

@ -70,8 +70,9 @@ static Seat psftp_seat[1] = {{ &psftp_seat_vt }};
* version of a string for display, and free it automatically * version of a string for display, and free it automatically
* afterwards. * afterwards.
*/ */
#define with_stripctrl(varname, input) \ static StripCtrlChars *string_scc;
for (char *varname = stripctrl_string(input); varname; \ #define with_stripctrl(varname, input) \
for (char *varname = stripctrl_string(string_scc, input); varname; \
sfree(varname), varname = NULL) sfree(varname), varname = NULL)
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
@ -2863,6 +2864,8 @@ int psftp_main(int argc, char *argv[])
stderr_bs = BinarySink_UPCAST(stderr_scc); stderr_bs = BinarySink_UPCAST(stderr_scc);
} }
string_scc = stripctrl_new(NULL, false, L'\0');
/* /*
* If the loaded session provides a hostname, and a hostname has not * If the loaded session provides a hostname, and a hostname has not
* otherwise been specified, pop it in `userhost' so that * otherwise been specified, pop it in `userhost' so that

Просмотреть файл

@ -95,7 +95,7 @@ void stripctrl_reset(StripCtrlChars *sccpub)
* start converting a fresh piece of data to send to a channel * start converting a fresh piece of data to send to a channel
* that hasn't seen the previous output. * that hasn't seen the previous output.
*/ */
memset(&scc->term_utf8_decode, 0, sizeof(scc->term_utf8_decode)); memset(&scc->utf8, 0, sizeof(scc->utf8));
memset(&scc->mbs_in, 0, sizeof(scc->mbs_in)); memset(&scc->mbs_in, 0, sizeof(scc->mbs_in));
memset(&scc->mbs_out, 0, sizeof(scc->mbs_out)); memset(&scc->mbs_out, 0, sizeof(scc->mbs_out));
} }
@ -358,12 +358,12 @@ static void stripctrl_term_BinarySink_write(
} }
} }
char *stripctrl_string_ptrlen(ptrlen str) char *stripctrl_string_ptrlen(StripCtrlChars *sccpub, ptrlen str)
{ {
strbuf *out = strbuf_new(); strbuf *out = strbuf_new();
StripCtrlChars *scc = stripctrl_new(BinarySink_UPCAST(out), false, L'?'); stripctrl_retarget(sccpub, BinarySink_UPCAST(out));
put_datapl(scc, str); put_datapl(sccpub, str);
stripctrl_free(scc); stripctrl_retarget(sccpub, NULL);
return strbuf_to_str(out); return strbuf_to_str(out);
} }