2014-07-18 07:29:46 +02:00
|
|
|
/* autocomplete.c
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Toxic All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Toxic.
|
|
|
|
*
|
|
|
|
* Toxic is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Toxic is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Toxic. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-08-03 09:44:48 +02:00
|
|
|
#include <limits.h>
|
2014-07-18 07:29:46 +02:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
2016-09-25 03:07:04 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/dir.h>
|
2014-07-18 07:29:46 +02:00
|
|
|
#else
|
2016-09-25 03:07:04 +02:00
|
|
|
#include <dirent.h>
|
2014-07-18 07:29:46 +02:00
|
|
|
#endif /* ifdef __APPLE__ */
|
|
|
|
|
|
|
|
#include "windows.h"
|
|
|
|
#include "toxic.h"
|
|
|
|
#include "misc_tools.h"
|
|
|
|
#include "line_info.h"
|
|
|
|
#include "execute.h"
|
2014-07-21 21:06:56 +02:00
|
|
|
#include "configdir.h"
|
2014-07-18 07:29:46 +02:00
|
|
|
|
2014-07-18 18:42:53 +02:00
|
|
|
static void print_matches(ToxWindow *self, Tox *m, const void *list, int n_items, int size)
|
|
|
|
{
|
|
|
|
if (m)
|
|
|
|
execute(self->chatwin->history, self, m, "/clear", GLOBAL_COMMAND_MODE);
|
|
|
|
|
|
|
|
const char *L = (char *) list;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < n_items; ++i)
|
2014-08-02 21:35:57 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", &L[i * size]);
|
2014-07-18 18:42:53 +02:00
|
|
|
|
2014-07-25 04:43:32 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, ""); /* formatting */
|
2014-07-18 18:42:53 +02:00
|
|
|
}
|
|
|
|
|
2014-07-18 07:29:46 +02:00
|
|
|
/* puts match in match buffer. if more than one match, add first n chars that are identical.
|
2016-06-12 05:49:56 +02:00
|
|
|
* e.g. if matches contains: [foo, foobar, foe] we put fo in match.
|
|
|
|
*
|
|
|
|
* Returns the length of the match.
|
|
|
|
*/
|
|
|
|
static size_t get_str_match(ToxWindow *self, char *match, size_t match_sz, char (*matches)[MAX_STR_SIZE], int n)
|
2014-07-18 07:29:46 +02:00
|
|
|
{
|
|
|
|
if (n == 1) {
|
2016-06-12 05:49:56 +02:00
|
|
|
return snprintf(match, match_sz, "%s", matches[0]);
|
2014-07-18 07:29:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
2014-07-18 18:42:53 +02:00
|
|
|
for (i = 0; i < MAX_STR_SIZE; ++i) {
|
2014-07-21 21:06:56 +02:00
|
|
|
char ch1 = matches[0][i];
|
2014-07-18 07:29:46 +02:00
|
|
|
int j;
|
|
|
|
|
|
|
|
for (j = 0; j < n; ++j) {
|
2014-07-21 21:06:56 +02:00
|
|
|
char ch2 = matches[j][i];
|
|
|
|
|
|
|
|
if (ch1 != ch2 || !ch1) {
|
2016-06-12 05:49:56 +02:00
|
|
|
snprintf(match, match_sz, "%s", matches[0]);
|
2014-07-18 07:29:46 +02:00
|
|
|
match[i] = '\0';
|
2016-06-12 05:49:56 +02:00
|
|
|
return i;
|
2014-07-18 07:29:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-12 05:49:56 +02:00
|
|
|
return snprintf(match, match_sz, "%s", matches[0]);
|
2014-07-18 07:29:46 +02:00
|
|
|
}
|
|
|
|
|
2014-07-18 18:42:53 +02:00
|
|
|
/* looks for all instances in list that begin with the last entered word in line according to pos,
|
2014-07-18 07:29:46 +02:00
|
|
|
then fills line with the complete word. e.g. "Hello jo" would complete the line
|
2014-07-18 18:42:53 +02:00
|
|
|
with "Hello john". If multiple matches, prints out all the matches and semi-completes line.
|
2014-07-18 07:29:46 +02:00
|
|
|
|
|
|
|
list is a pointer to the list of strings being compared, n_items is the number of items
|
|
|
|
in the list, and size is the size of each item in the list.
|
|
|
|
|
|
|
|
Returns the difference between the old len and new len of line on success, -1 if error */
|
2014-07-18 18:42:53 +02:00
|
|
|
int complete_line(ToxWindow *self, const void *list, int n_items, int size)
|
2014-07-18 07:29:46 +02:00
|
|
|
{
|
2014-07-18 18:42:53 +02:00
|
|
|
ChatContext *ctx = self->chatwin;
|
|
|
|
|
2015-08-27 09:38:08 +02:00
|
|
|
if (ctx->pos <= 0 || ctx->len <= 0 || ctx->pos > ctx->len || ctx->len >= MAX_STR_SIZE || size > MAX_STR_SIZE)
|
2014-07-18 07:29:46 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
const char *L = (char *) list;
|
|
|
|
const char *endchrs = " ";
|
|
|
|
char ubuf[MAX_STR_SIZE];
|
|
|
|
|
|
|
|
/* work with multibyte string copy of buf for simplicity */
|
|
|
|
if (wcs_to_mbs_buf(ubuf, ctx->line, sizeof(ubuf)) == -1)
|
|
|
|
return -1;
|
|
|
|
|
2014-09-26 09:10:44 +02:00
|
|
|
/* TODO: generalize this */
|
|
|
|
bool dir_search = !strncmp(ubuf, "/sendfile", strlen("/sendfile"))
|
2016-09-25 03:07:04 +02:00
|
|
|
|| !strncmp(ubuf, "/avatar", strlen("/avatar"));
|
2014-07-18 09:10:41 +02:00
|
|
|
|
2017-06-01 22:46:12 +02:00
|
|
|
#ifdef PYTHON
|
2017-05-23 02:47:56 +02:00
|
|
|
dir_search = dir_search || !strncmp(ubuf, "/run", strlen("/run"));
|
2017-06-01 22:46:12 +02:00
|
|
|
#endif
|
2017-05-23 02:47:56 +02:00
|
|
|
|
2014-07-18 07:29:46 +02:00
|
|
|
/* isolate substring from space behind pos to pos */
|
|
|
|
char tmp[MAX_STR_SIZE];
|
|
|
|
snprintf(tmp, sizeof(tmp), "%s", ubuf);
|
|
|
|
tmp[ctx->pos] = '\0';
|
|
|
|
|
2014-07-18 09:10:41 +02:00
|
|
|
const char *s = dir_search ? strchr(tmp, '\"') : strrchr(tmp, ' ');
|
2015-11-08 09:57:01 +01:00
|
|
|
char *sub = calloc(1, strlen(ubuf) + 1);
|
2014-07-18 07:29:46 +02:00
|
|
|
|
|
|
|
if (sub == NULL)
|
2015-07-04 07:19:16 +02:00
|
|
|
exit_toxic_err("failed in complete_line", FATALERR_MEMORY);
|
2014-07-18 07:29:46 +02:00
|
|
|
|
2014-07-18 09:10:41 +02:00
|
|
|
if (!s && !dir_search) {
|
2014-07-18 07:29:46 +02:00
|
|
|
strcpy(sub, tmp);
|
|
|
|
|
|
|
|
if (sub[0] != '/')
|
|
|
|
endchrs = ": ";
|
2014-07-18 09:10:41 +02:00
|
|
|
} else if (s) {
|
2014-07-18 07:29:46 +02:00
|
|
|
strcpy(sub, &s[1]);
|
|
|
|
|
2014-07-18 09:10:41 +02:00
|
|
|
if (dir_search) {
|
2014-07-18 07:29:46 +02:00
|
|
|
int sub_len = strlen(sub);
|
|
|
|
int si = char_rfind(sub, '/', sub_len);
|
2014-07-18 09:10:41 +02:00
|
|
|
|
|
|
|
if (si || *sub == '/')
|
|
|
|
memmove(sub, &sub[si + 1], sub_len - si);
|
2014-07-18 07:29:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-27 09:38:08 +02:00
|
|
|
if (!sub[0]) {
|
2014-07-18 07:29:46 +02:00
|
|
|
free(sub);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int s_len = strlen(sub);
|
|
|
|
int n_matches = 0;
|
|
|
|
char matches[n_items][MAX_STR_SIZE];
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
/* put all list matches in matches array */
|
|
|
|
for (i = 0; i < n_items; ++i) {
|
2015-11-08 09:57:01 +01:00
|
|
|
char str[MAX_CMDNAME_SIZE + 1];
|
|
|
|
snprintf(str, sizeof(str), "%s", &L[i * size]);
|
2014-07-18 07:29:46 +02:00
|
|
|
|
|
|
|
if (strncasecmp(str, sub, s_len) == 0)
|
|
|
|
strcpy(matches[n_matches++], str);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(sub);
|
|
|
|
|
|
|
|
if (!n_matches)
|
|
|
|
return -1;
|
|
|
|
|
2014-07-18 18:42:53 +02:00
|
|
|
if (!dir_search && n_matches > 1)
|
|
|
|
print_matches(self, NULL, matches, n_matches, MAX_STR_SIZE);
|
|
|
|
|
|
|
|
char match[MAX_STR_SIZE];
|
2016-06-12 05:49:56 +02:00
|
|
|
size_t match_len = get_str_match(self, match, sizeof(match), matches, n_matches);
|
|
|
|
|
|
|
|
if (match_len == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-07-18 07:29:46 +02:00
|
|
|
|
|
|
|
if (dir_search) {
|
|
|
|
if (n_matches == 1)
|
2015-11-08 09:57:01 +01:00
|
|
|
endchrs = char_rfind(match, '.', match_len) ? "\"" : "/";
|
2014-07-18 07:29:46 +02:00
|
|
|
else
|
|
|
|
endchrs = "";
|
|
|
|
} else if (n_matches > 1) {
|
|
|
|
endchrs = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
/* put match in correct spot in buf and append endchars */
|
|
|
|
int n_endchrs = strlen(endchrs);
|
|
|
|
int strt = ctx->pos - s_len;
|
2015-11-08 09:57:01 +01:00
|
|
|
int diff = match_len - s_len + n_endchrs;
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2014-07-25 23:55:21 +02:00
|
|
|
if (ctx->len + diff >= MAX_STR_SIZE)
|
2014-07-18 07:29:46 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
char tmpend[MAX_STR_SIZE];
|
2015-08-27 09:38:08 +02:00
|
|
|
snprintf(tmpend, sizeof(tmpend), "%s", &ubuf[ctx->pos]);
|
2015-11-08 09:57:01 +01:00
|
|
|
|
2016-06-12 05:49:56 +02:00
|
|
|
if (match_len + n_endchrs + strlen(tmpend) >= sizeof(ubuf)) {
|
2015-11-08 09:57:01 +01:00
|
|
|
return -1;
|
2016-06-12 05:49:56 +02:00
|
|
|
}
|
2015-11-08 09:57:01 +01:00
|
|
|
|
2014-07-18 07:29:46 +02:00
|
|
|
strcpy(&ubuf[strt], match);
|
2015-11-08 09:57:01 +01:00
|
|
|
strcpy(&ubuf[strt + match_len], endchrs);
|
|
|
|
strcpy(&ubuf[strt + match_len + n_endchrs], tmpend);
|
2014-07-18 07:29:46 +02:00
|
|
|
|
|
|
|
/* convert to widechar and copy back to original buf */
|
|
|
|
wchar_t newbuf[MAX_STR_SIZE];
|
|
|
|
|
2016-06-12 05:49:56 +02:00
|
|
|
if (mbs_to_wcs_buf(newbuf, ubuf, sizeof(newbuf) / sizeof(wchar_t)) == -1) {
|
2014-07-18 07:29:46 +02:00
|
|
|
return -1;
|
2016-06-12 05:49:56 +02:00
|
|
|
}
|
2014-07-18 07:29:46 +02:00
|
|
|
|
|
|
|
wcscpy(ctx->line, newbuf);
|
|
|
|
|
|
|
|
ctx->len += diff;
|
|
|
|
ctx->pos += diff;
|
|
|
|
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
|
2014-09-26 09:10:44 +02:00
|
|
|
/* transforms a tab complete starting with the shorthand "~" into the full home directory.*/
|
|
|
|
static void complt_home_dir(ToxWindow *self, char *path, int pathsize, const char *cmd, int cmdlen)
|
2014-07-21 21:06:56 +02:00
|
|
|
{
|
|
|
|
ChatContext *ctx = self->chatwin;
|
|
|
|
|
2015-01-25 21:52:07 +01:00
|
|
|
char homedir[MAX_STR_SIZE] = {0};
|
2014-07-21 21:06:56 +02:00
|
|
|
get_home_dir(homedir, sizeof(homedir));
|
|
|
|
|
|
|
|
char newline[MAX_STR_SIZE];
|
2014-09-26 09:10:44 +02:00
|
|
|
snprintf(newline, sizeof(newline), "%s \"%s%s", cmd, homedir, path + 1);
|
|
|
|
snprintf(path, pathsize, "%s", &newline[cmdlen]);
|
2014-07-21 21:06:56 +02:00
|
|
|
|
|
|
|
wchar_t wline[MAX_STR_SIZE];
|
|
|
|
|
2015-08-27 09:38:08 +02:00
|
|
|
if (mbs_to_wcs_buf(wline, newline, sizeof(wline) / sizeof(wchar_t)) == -1)
|
2014-07-21 21:06:56 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
int newlen = wcslen(wline);
|
|
|
|
|
2014-07-25 23:55:21 +02:00
|
|
|
if (ctx->len + newlen >= MAX_STR_SIZE)
|
2014-07-21 21:06:56 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
wmemcpy(ctx->line, wline, newlen + 1);
|
|
|
|
ctx->pos = newlen;
|
|
|
|
ctx->len = ctx->pos;
|
|
|
|
}
|
|
|
|
|
2014-09-26 09:10:44 +02:00
|
|
|
/* attempts to match /command "<incomplete-dir>" line to matching directories.
|
2014-07-18 07:29:46 +02:00
|
|
|
|
2014-07-21 21:06:56 +02:00
|
|
|
if only one match, auto-complete line.
|
2014-07-28 07:33:12 +02:00
|
|
|
return diff between old len and new len of ctx->line, -1 if no matches or > 1 match */
|
|
|
|
#define MAX_DIRS 512
|
2014-07-18 07:29:46 +02:00
|
|
|
|
2014-09-26 09:10:44 +02:00
|
|
|
int dir_match(ToxWindow *self, Tox *m, const wchar_t *line, const wchar_t *cmd)
|
2014-07-18 07:29:46 +02:00
|
|
|
{
|
|
|
|
char b_path[MAX_STR_SIZE];
|
|
|
|
char b_name[MAX_STR_SIZE];
|
2014-09-26 09:10:44 +02:00
|
|
|
char b_cmd[MAX_STR_SIZE];
|
|
|
|
const wchar_t *tmpline = &line[wcslen(cmd) + 2]; /* start after "/command \"" */
|
2014-07-18 07:29:46 +02:00
|
|
|
|
2014-07-28 07:33:12 +02:00
|
|
|
if (wcs_to_mbs_buf(b_path, tmpline, sizeof(b_path)) == -1)
|
2015-03-17 20:25:15 +01:00
|
|
|
return -1;
|
2014-07-21 21:06:56 +02:00
|
|
|
|
2014-09-26 09:10:44 +02:00
|
|
|
if (wcs_to_mbs_buf(b_cmd, cmd, sizeof(b_cmd)) == -1)
|
|
|
|
return -1;
|
|
|
|
|
2014-08-17 05:23:03 +02:00
|
|
|
if (b_path[0] == '~')
|
2014-09-26 09:10:44 +02:00
|
|
|
complt_home_dir(self, b_path, sizeof(b_path), b_cmd, strlen(b_cmd) + 2);
|
2014-07-18 07:29:46 +02:00
|
|
|
|
|
|
|
int si = char_rfind(b_path, '/', strlen(b_path));
|
|
|
|
|
|
|
|
if (!b_path[0]) { /* list everything in pwd */
|
2014-07-21 21:06:56 +02:00
|
|
|
b_path[0] = '.';
|
2015-03-17 20:25:15 +01:00
|
|
|
b_path[1] = '\0';
|
2014-07-18 07:29:46 +02:00
|
|
|
} else if (!si && b_path[0] != '/') { /* look for matches in pwd */
|
|
|
|
char tmp[MAX_STR_SIZE];
|
|
|
|
snprintf(tmp, sizeof(tmp), ".%s", b_path);
|
2015-08-27 09:38:08 +02:00
|
|
|
snprintf(b_path, sizeof(b_path), "%s", tmp);
|
2014-07-18 07:29:46 +02:00
|
|
|
}
|
|
|
|
|
2015-08-27 09:38:08 +02:00
|
|
|
snprintf(b_name, sizeof(b_name), "%s", &b_path[si + 1]);
|
2014-07-18 07:29:46 +02:00
|
|
|
b_path[si + 1] = '\0';
|
|
|
|
int b_name_len = strlen(b_name);
|
|
|
|
DIR *dp = opendir(b_path);
|
|
|
|
|
|
|
|
if (dp == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2017-09-01 23:36:17 +02:00
|
|
|
char dirnames[MAX_DIRS][NAME_MAX + 1];
|
2014-07-18 07:29:46 +02:00
|
|
|
struct dirent *entry;
|
|
|
|
int dircount = 0;
|
|
|
|
|
|
|
|
while ((entry = readdir(dp)) && dircount < MAX_DIRS) {
|
2015-03-17 20:25:15 +01:00
|
|
|
if (strncmp(entry->d_name, b_name, b_name_len) == 0
|
2016-09-25 03:07:04 +02:00
|
|
|
&& strcmp(".", entry->d_name) && strcmp("..", entry->d_name)) {
|
2014-07-18 07:29:46 +02:00
|
|
|
snprintf(dirnames[dircount], sizeof(dirnames[dircount]), "%s", entry->d_name);
|
|
|
|
++dircount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 17:28:04 +01:00
|
|
|
closedir(dp);
|
|
|
|
|
2014-07-18 07:29:46 +02:00
|
|
|
if (dircount == 0)
|
|
|
|
return -1;
|
|
|
|
|
2014-07-24 00:34:32 +02:00
|
|
|
if (dircount > 1) {
|
|
|
|
qsort(dirnames, dircount, NAME_MAX, qsort_strcasecmp_hlpr);
|
2014-07-18 18:42:53 +02:00
|
|
|
print_matches(self, m, dirnames, dircount, NAME_MAX);
|
2014-07-24 00:34:32 +02:00
|
|
|
}
|
2014-07-18 07:29:46 +02:00
|
|
|
|
2014-07-18 18:42:53 +02:00
|
|
|
return complete_line(self, dirnames, dircount, NAME_MAX);
|
2014-07-18 07:29:46 +02:00
|
|
|
}
|