xaizek / vifm (License: GPLv2+) (since 2018-12-07)
Vifm is a file manager with curses interface, which provides Vi[m]-like environment for managing objects within file systems, extended with some useful ideas from mutt.
Commit b58851bd5b280a7f172b3fdc7a94f3ec4973d55a

Introduce engine/var:var_out_of_str()
Like var_from_str(), but takes ownership of the argument instead of
copying it.

This makes some code cleaner in addition to being more efficient.
Author: xaizek
Author date (UTC): 2024-11-02 13:55
Committer name: xaizek
Committer date (UTC): 2024-11-02 16:20
Parent(s): 75e08a839b906f952575090b3e9d4fcec54cfb3a
Signing key: 99DC5E4DB05F6BE2
Tree: 317b9e5a3f0f909b9873852c6ad578025d49cc69
File Lines added Lines deleted
src/builtin_functions.c 6 22
src/engine/var.c 10 0
src/engine/var.h 4 0
File src/builtin_functions.c changed (mode: 100644) (index c4b9a87e8..dc2737360)
... ... escape_builtin(const call_info_t *call_info)
178 178 return var_error(); return var_error();
179 179 } }
180 180
181 var_t result = var_from_str(escaped);
182 free(escaped);
183
184 return result;
181 return var_out_of_str(escaped);
185 182 } }
186 183
187 184 /* Checks whether executable exists at absolute path or in directories listed in /* Checks whether executable exists at absolute path or in directories listed in
 
... ... expand_builtin(const call_info_t *call_info)
222 219 char *macro_expanded = ma_expand(env_expanded, NULL, NULL, MER_DISPLAY); char *macro_expanded = ma_expand(env_expanded, NULL, NULL, MER_DISPLAY);
223 220 free(env_expanded); free(env_expanded);
224 221
225 var_t result = var_from_str(macro_expanded);
226 free(macro_expanded);
227
228 return result;
222 return var_out_of_str(macro_expanded);
229 223 } }
230 224
231 225 /* Returns cached value of an external command. Cache validity is bound to a /* Returns cached value of an external command. Cache validity is bound to a
 
... ... type_of_link_target(const dir_entry_t *entry)
412 406 static var_t static var_t
413 407 fnameescape_builtin(const call_info_t *call_info) fnameescape_builtin(const call_info_t *call_info)
414 408 { {
415 var_t result;
416
417 409 char *const str_val = var_to_str(call_info->argv[0]); char *const str_val = var_to_str(call_info->argv[0]);
418 410 char *const escaped = posix_like_escape(str_val, /*type=*/1); char *const escaped = posix_like_escape(str_val, /*type=*/1);
419 411 free(str_val); free(str_val);
420 412
421 result = var_from_str(escaped);
422 free(escaped);
423 return result;
413 return var_out_of_str(escaped);
424 414 } }
425 415
426 416 /* Retrieves type of current pane as a string. */ /* Retrieves type of current pane as a string. */
 
... ... input_builtin(const call_info_t *call_info)
469 459
470 460 /* Not returning var_error() on cancellation to allow handling of it by the /* Not returning var_error() on cancellation to allow handling of it by the
471 461 * user. */ * user. */
472 var_t result = var_from_str(cb_data.response == NULL ? "" : cb_data.response);
473
474 free(cb_data.response);
475
476 return result;
462 return (cb_data.response == NULL) ? var_from_str("")
463 : var_out_of_str(cb_data.response);
477 464
478 465 fail: fail:
479 466 free(prompt); free(prompt);
 
... ... term_builtin(const call_info_t *call_info)
667 654 static var_t static var_t
668 655 execute_cmd(var_t cmd_arg, int interactive, int preserve_stdin) execute_cmd(var_t cmd_arg, int interactive, int preserve_stdin)
669 656 { {
670 var_t result;
671 657 char *cmd; char *cmd;
672 658 FILE *cmd_stream; FILE *cmd_stream;
673 659 size_t cmd_out_len; size_t cmd_out_len;
 
... ... execute_cmd(var_t cmd_arg, int interactive, int preserve_stdin)
703 689 --cmd_out_len; --cmd_out_len;
704 690 } }
705 691
706 result = var_from_str(result_str);
707 free(result_str);
708 return result;
692 return var_out_of_str(result_str);
709 693 } }
710 694
711 695 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */ /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
File src/engine/var.c changed (mode: 100644) (index 423cb5e20..aa33ffd6e)
... ... var_from_str(const char str_val[])
60 60 return var_new(VTYPE_STRING, value); return var_new(VTYPE_STRING, value);
61 61 } }
62 62
63 var_t
64 var_out_of_str(char str_val[])
65 {
66 /* Alternatively, could return var_error() in this case. */
67 assert(str_val != NULL && "Can't make a NULL string variable.");
68
69 var_t new_var = { .type = VTYPE_STRING, { .string = str_val } };
70 return new_var;
71 }
72
63 73 var_t var_t
64 74 var_error(void) var_error(void)
65 75 { {
File src/engine/var.h changed (mode: 100644) (index 80b52afa6..d7e515b71)
... ... var_t var_from_int(int int_val);
63 63 * variable. */ * variable. */
64 64 var_t var_from_str(const char str_val[]); var_t var_from_str(const char str_val[]);
65 65
66 /* Takes ownership of the passed in non-NULL string to make a string variable.
67 * Never fails. */
68 var_t var_out_of_str(char str_val[]);
69
66 70 /* Returns variable, which signals about failed operation. */ /* Returns variable, which signals about failed operation. */
67 71 var_t var_error(void); var_t var_error(void);
68 72
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/vifm

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/vifm

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master