From 6fc9e6f21ed8e6ebc2c6254426d1dc33f6bce636 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Wed, 21 Nov 2012 22:23:48 -0800 Subject: [PATCH] Make tok_first return a wcstring instead of a wchar_t* --- builtin.cpp | 7 +++---- reader.cpp | 8 +++----- tokenizer.cpp | 32 +++++++++++++++++--------------- tokenizer.h | 4 ++-- 4 files changed, 25 insertions(+), 26 deletions(-) diff --git a/builtin.cpp b/builtin.cpp index 6229eaeb6..af0da3281 100644 --- a/builtin.cpp +++ b/builtin.cpp @@ -3254,10 +3254,9 @@ static int builtin_fg(parser_t &parser, wchar_t **argv) j->command_wcstr()); } - wchar_t *ft = tok_first(j->command_wcstr()); - if (ft != 0) - env_set(L"_", ft, ENV_EXPORT); - free(ft); + const wcstring ft = tok_first(j->command_wcstr()); + if (! ft.empty()) + env_set(L"_", ft.c_str(), ENV_EXPORT); reader_write_title(); make_first(j); diff --git a/reader.cpp b/reader.cpp index a925527f8..af7e2e979 100644 --- a/reader.cpp +++ b/reader.cpp @@ -2200,14 +2200,12 @@ void set_env_cmd_duration(struct timeval *after, struct timeval *before) void reader_run_command(parser_t &parser, const wchar_t *cmd) { - wchar_t *ft; struct timeval time_before, time_after; - ft= tok_first(cmd); + wcstring ft = tok_first(cmd); - if (ft != 0) - env_set(L"_", ft, ENV_GLOBAL); - free(ft); + if (! ft.empty()) + env_set(L"_", ft.c_str(), ENV_GLOBAL); reader_write_title(); diff --git a/tokenizer.cpp b/tokenizer.cpp index 378fed1dd..84702b554 100644 --- a/tokenizer.cpp +++ b/tokenizer.cpp @@ -642,24 +642,26 @@ const wchar_t *tok_string(tokenizer_t *tok) return tok?tok->orig_buff:0; } -wchar_t *tok_first(const wchar_t *str) +wcstring tok_first(const wchar_t *str) { - wchar_t *res=0; - - CHECK(str, 0); - - tokenizer_t t(str, TOK_SQUASH_ERRORS); - switch (tok_last_type(&t)) + wcstring result; + if (str) { - case TOK_STRING: -// fwprintf( stderr, L"Got token %ls\n", tok_last( &t )); - res = wcsdup(tok_last(&t)); - break; - default: - break; + tokenizer_t t(str, TOK_SQUASH_ERRORS); + switch (tok_last_type(&t)) + { + case TOK_STRING: + { + const wchar_t *tmp = tok_last(&t); + if (tmp != NULL) + result = tmp; + break; + } + default: + break; + } } - - return res; + return result; } int tok_get_pos(tokenizer_t *tok) diff --git a/tokenizer.h b/tokenizer.h index 0ff7f10ef..23a4361ff 100644 --- a/tokenizer.h +++ b/tokenizer.h @@ -156,9 +156,9 @@ const wchar_t *tok_string(tokenizer_t *tok); convenience function, used to retrieve the first token of a string. This can be useful for error messages, etc. - The string should be freed. After use. + On failure, returns the empty string. */ -wchar_t *tok_first(const wchar_t *str); +wcstring tok_first(const wchar_t *str); /** Indicates whether a character can be part of a string, or is a string separator.