/* aux.c -- functions that do not fit in any other file */
/*
* This file is part of CliFM
*
* Copyright (C) 2016-2022, L. Abramovich <johndoe.arch@outlook.com>
* All rights reserved.
* CliFM 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 2 of the License, or
* (at your option) any later version.
*
* CliFM 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "helpers.h"
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <limits.h>
#include <readline/readline.h>
#include "aux.h"
#include "exec.h"
#include "misc.h"
#include "checks.h"
#ifndef _NO_HIGHLIGHT
# include "highlight.h"
#endif
#ifdef RL_READLINE_VERSION
# if RL_READLINE_VERSION >= 0x0801
# define _READLINE_HAS_ACTIVATE_MARK
# endif /* RL_READLINE_VERSION >= 0x0801 */
#endif /* RL_READLINE_VERSION */
static char *
find_digit(char *str)
{
if (!str || !*str)
return (char *)NULL;
while (*str) {
if (*str >= '1' && *str <= '9')
return str;
str++;
}
return (char *)NULL;
}
/* Check whether a given command needs ELN's to be expanded/completed/suggested
* Returns 1 if yes or 0 if not */
int
__expand_eln(const char *text)
{
char *l = rl_line_buffer;
if (!l || !*l || !is_number(text))
return 0;
int a = atoi(text); /* Only expand numbers matching ELN's */
if (a <= 0 || a > (int)files)
return 0;
if (nwords == 1) { /* First word */
if (file_info[a - 1].dir && autocd == 0)
return 0;
if (file_info[a - 1].dir == 0 && auto_open == 0)
return 0;
}
char *p = strchr(l, ' ');
char t = ' ';
if (!p && (p = find_digit(l)) )
t = *p;
if (!p)
return 1;
*p = '\0';
flags |= STATE_COMPLETING;
if (is_internal_c(l) && !is_internal_f(l)) {
*p = t;
flags &= ~STATE_COMPLETING;
return 0;
}
flags &= ~STATE_COMPLETING;
*p = t;
return 1;
}
/* Sleep for MSEC milliseconds */
/* Taken from https://stackoverflow.com/questions/1157209/is-there-an-alternative-sleep-function-in-c-to-milliseconds */
static int
msleep(long msec)
{
struct timespec ts;
int res;
if (msec < 0) {
errno = EINVAL;
return (-1);
}
ts.tv_sec = msec / 1000;