From 280619a4e86f8873fcf99c217f5504ade708b2b7 Mon Sep 17 00:00:00 2001 From: Tiago Cunha Date: Mon, 13 Jul 2009 18:03:18 +0000 Subject: [PATCH] Expand leading tilde on commands which expect a filename. --- TODO | 1 - cmd-string.c | 44 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/TODO b/TODO index 5cb241e4..60121c3c 100644 --- a/TODO +++ b/TODO @@ -91,7 +91,6 @@ - status-active-fg/bg/attr would be nice to highlight active window, as would options to set the highlight stuff - XXX once env stuff is in, default-path and VISUAL/EDITOR should be picked up when session is started -- expand leading ~ in source-file save-buffer etc - the copy mode next-word behaviour is emacs style (end of word) rather than vi style (start of word) even in vi keys mode, - when moving up from a blank line in copy mode it always assumes you were at the end of the line so it can be a bit funny. should remember last non-blank x position and use that - paste-buffer etc should be able to handle \0, so needs a size member and no more strlen diff --git a/cmd-string.c b/cmd-string.c index 0994e3fd..e2f451a6 100644 --- a/cmd-string.c +++ b/cmd-string.c @@ -1,4 +1,4 @@ -/* $Id: cmd-string.c,v 1.18 2009-07-09 18:03:28 nicm Exp $ */ +/* $Id: cmd-string.c,v 1.19 2009-07-13 18:03:18 tcunha Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott @@ -19,9 +19,11 @@ #include #include +#include #include #include #include +#include #include "tmux.h" @@ -33,6 +35,7 @@ int cmd_string_getc(const char *, size_t *); void cmd_string_ungetc(const char *, size_t *); char *cmd_string_string(const char *, size_t *, char, int); char *cmd_string_variable(const char *, size_t *); +char *cmd_string_expand_tilde(const char *, size_t *); int cmd_string_getc(const char *s, size_t *p) @@ -154,6 +157,17 @@ cmd_string_parse(const char *s, struct cmd_list **cmdlist, char **cause) rval = 0; goto out; + case '~': + if (have_arg == 0) { + if ((t = cmd_string_expand_tilde(s, &p)) == NULL) + goto error; + buf = xrealloc(buf, 1, len + strlen(t) + 1); + strlcpy(buf + len, t, strlen(t) + 1); + len += strlen(t); + xfree(t); + break; + } + /* FALLTHROUGH */ default: if (len >= SIZE_MAX - 2) goto error; @@ -309,3 +323,31 @@ error: xfree(buf); return (NULL); } + +char * +cmd_string_expand_tilde(const char *s, size_t *p) +{ + struct passwd *pw; + char *home, *path, *username; + + home = NULL; + if (cmd_string_getc(s, p) == '/') { + if ((home = getenv("HOME")) == NULL) { + if ((pw = getpwuid(getuid())) != NULL) + home = pw->pw_dir; + } + } else { + cmd_string_ungetc(s, p); + if ((username = cmd_string_string(s, p, '/', 0)) == NULL) + return (NULL); + if ((pw = getpwnam(username)) != NULL) + home = pw->pw_dir; + if (username != NULL) + xfree(username); + } + if (home == NULL) + return (NULL); + + xasprintf(&path, "%s/", home); + return (path); +}