diff options
author | ridiculousfish <corydoras@ridiculousfish.com> | 2015-09-21 11:24:49 -0700 |
---|---|---|
committer | ridiculousfish <corydoras@ridiculousfish.com> | 2015-09-22 18:08:00 -0700 |
commit | c1bd3b5824027f23b41bbac77c5e28f856c5dc18 (patch) | |
tree | f406ad2f859df99f65c059f8c4019d5a2db1e459 /src/builtin_commandline.cpp | |
parent | fb615843b329b4d3c303b9cebe2af134a52b5bfd (diff) |
Eliminate global variables associated with builtin IO
This change eliminates global variables like stdout_buffer. Instead we wrap up
the IO information into a new struct io_streams_t, and thread that through
every builtin. This makes the intent clearer, gives us a place to hang new IO
data, and eliminates the ugly global state management like builtin_push_io.
Diffstat (limited to 'src/builtin_commandline.cpp')
-rw-r--r-- | src/builtin_commandline.cpp | 74 |
1 files changed, 33 insertions, 41 deletions
diff --git a/src/builtin_commandline.cpp b/src/builtin_commandline.cpp index 352baf1c..971ba208 100644 --- a/src/builtin_commandline.cpp +++ b/src/builtin_commandline.cpp @@ -186,7 +186,8 @@ static void replace_part(const wchar_t *begin, static void write_part(const wchar_t *begin, const wchar_t *end, int cut_at_cursor, - int tokenize) + int tokenize, + io_streams_t &streams) { size_t pos = get_cursor_pos()-(begin-get_buffer()); @@ -221,7 +222,7 @@ static void write_part(const wchar_t *begin, } } - stdout_buffer.append(out); + streams.out.append(out); free(buff); } @@ -235,8 +236,8 @@ static void write_part(const wchar_t *begin, // debug( 0, L"woot2 %ls -> %ls", buff, esc ); wcstring tmp = wcstring(begin, end - begin); unescape_string_in_place(&tmp, UNESCAPE_INCOMPLETE); - stdout_buffer.append(tmp); - stdout_buffer.append(L"\n"); + streams.out.append(tmp); + streams.out.append(L"\n"); } } @@ -246,7 +247,7 @@ static void write_part(const wchar_t *begin, The commandline builtin. It is used for specifying a new value for the commandline. */ -static int builtin_commandline(parser_t &parser, wchar_t **argv) +static int builtin_commandline(parser_t &parser, io_streams_t &streams, wchar_t **argv) { wgetopter_t w; int buffer_part=0; @@ -293,9 +294,9 @@ static int builtin_commandline(parser_t &parser, wchar_t **argv) return 1; } - stderr_buffer.append(argv[0]); - stderr_buffer.append(L": Can not set commandline in non-interactive mode\n"); - builtin_print_help(parser, argv[0], stderr_buffer); + streams.err.append(argv[0]); + streams.err.append(L": Can not set commandline in non-interactive mode\n"); + builtin_print_help(parser, streams, argv[0], streams.err); return 1; } @@ -341,11 +342,10 @@ static int builtin_commandline(parser_t &parser, wchar_t **argv) case 0: if (long_options[opt_index].flag != 0) break; - append_format(stderr_buffer, - BUILTIN_ERR_UNKNOWN, + streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0], long_options[opt_index].name); - builtin_print_help(parser, argv[0], stderr_buffer); + builtin_print_help(parser, streams, argv[0], streams.err); return 1; @@ -416,11 +416,11 @@ static int builtin_commandline(parser_t &parser, wchar_t **argv) break; case 'h': - builtin_print_help(parser, argv[0], stdout_buffer); + builtin_print_help(parser, streams, argv[0], streams.out); return 0; case L'?': - builtin_unknown_option(parser, argv[0], argv[w.woptind-1]); + builtin_unknown_option(parser, streams, argv[0], argv[w.woptind-1]); return 1; } } @@ -434,22 +434,20 @@ static int builtin_commandline(parser_t &parser, wchar_t **argv) */ if (buffer_part || cut_at_cursor || append_mode || tokenize || cursor_mode || line_mode || search_mode || paging_mode) { - append_format(stderr_buffer, - BUILTIN_ERR_COMBO, + streams.err.append_format(BUILTIN_ERR_COMBO, argv[0]); - builtin_print_help(parser, argv[0], stderr_buffer); + builtin_print_help(parser, streams, argv[0], streams.err); return 1; } if (argc == w.woptind) { - append_format(stderr_buffer, - BUILTIN_ERR_MISSING, + streams.err.append_format(BUILTIN_ERR_MISSING, argv[0]); - builtin_print_help(parser, argv[0], stderr_buffer); + builtin_print_help(parser, streams, argv[0], streams.err); return 1; } for (i=w.woptind; i<argc; i++) @@ -466,11 +464,10 @@ static int builtin_commandline(parser_t &parser, wchar_t **argv) } else { - append_format(stderr_buffer, - _(L"%ls: Unknown input function '%ls'\n"), + streams.err.append_format(_(L"%ls: Unknown input function '%ls'\n"), argv[0], argv[i]); - builtin_print_help(parser, argv[0], stderr_buffer); + builtin_print_help(parser, streams, argv[0], streams.err); return 1; } } @@ -484,7 +481,7 @@ static int builtin_commandline(parser_t &parser, wchar_t **argv) const wchar_t *buffer = reader_get_buffer(); if (reader_get_selection(&start, &len)) { - stdout_buffer.append(buffer + start, len); + streams.out.append(buffer + start, len); } return 0; } @@ -495,45 +492,41 @@ static int builtin_commandline(parser_t &parser, wchar_t **argv) if ((search_mode || line_mode || cursor_mode || paging_mode) && (argc-w.woptind > 1)) { - append_format(stderr_buffer, - argv[0], + streams.err.append_format(argv[0], L": Too many arguments\n", NULL); - builtin_print_help(parser, argv[0], stderr_buffer); + builtin_print_help(parser, streams, argv[0], streams.err); return 1; } if ((buffer_part || tokenize || cut_at_cursor) && (cursor_mode || line_mode || search_mode || paging_mode)) { - append_format(stderr_buffer, - BUILTIN_ERR_COMBO, + streams.err.append_format(BUILTIN_ERR_COMBO, argv[0]); - builtin_print_help(parser, argv[0], stderr_buffer); + builtin_print_help(parser, streams, argv[0], streams.err); return 1; } if ((tokenize || cut_at_cursor) && (argc-w.woptind)) { - append_format(stderr_buffer, - BUILTIN_ERR_COMBO2, + streams.err.append_format(BUILTIN_ERR_COMBO2, argv[0], L"--cut-at-cursor and --tokenize can not be used when setting the commandline"); - builtin_print_help(parser, argv[0], stderr_buffer); + builtin_print_help(parser, streams, argv[0], streams.err); return 1; } if (append_mode && !(argc-w.woptind)) { - append_format(stderr_buffer, - BUILTIN_ERR_COMBO2, + streams.err.append_format(BUILTIN_ERR_COMBO2, argv[0], L"insertion mode switches can not be used when not in insertion mode"); - builtin_print_help(parser, argv[0], stderr_buffer); + builtin_print_help(parser, streams, argv[0], streams.err); return 1; } @@ -561,11 +554,10 @@ static int builtin_commandline(parser_t &parser, wchar_t **argv) new_pos = wcstol(argv[w.woptind], &endptr, 10); if (*endptr || errno) { - append_format(stderr_buffer, - BUILTIN_ERR_NOT_NUMBER, + streams.err.append_format(BUILTIN_ERR_NOT_NUMBER, argv[0], argv[w.woptind]); - builtin_print_help(parser, argv[0], stderr_buffer); + builtin_print_help(parser, streams, argv[0], streams.err); } current_buffer = reader_get_buffer(); @@ -575,7 +567,7 @@ static int builtin_commandline(parser_t &parser, wchar_t **argv) } else { - append_format(stdout_buffer, L"%lu\n", (unsigned long)reader_get_cursor_pos()); + streams.out.append_format( L"%lu\n", (unsigned long)reader_get_cursor_pos()); return 0; } @@ -585,7 +577,7 @@ static int builtin_commandline(parser_t &parser, wchar_t **argv) { size_t pos = reader_get_cursor_pos(); const wchar_t *buff = reader_get_buffer(); - append_format(stdout_buffer, L"%lu\n", (unsigned long)parse_util_lineno(buff, pos)); + streams.out.append_format( L"%lu\n", (unsigned long)parse_util_lineno(buff, pos)); return 0; } @@ -644,7 +636,7 @@ static int builtin_commandline(parser_t &parser, wchar_t **argv) { case 0: { - write_part(begin, end, cut_at_cursor, tokenize); + write_part(begin, end, cut_at_cursor, tokenize, streams); break; } |