Repository navigation
Expand file tree
/
Copy pathcmd.c
More file actions
157 lines (147 loc) · 5.15 KB
/
Copy pathcmd.c
File metadata and controls
157 lines (147 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "includes.h"
#include "types.h"
#include "globals.h"
typedef struct command_entry {
struct command_entry *previous, *next;
char text[];
} command_entry;
static command_entry *history_last, *history_cursor;
static char *history_draft;
void command_history_add(const char *command)
{
history_cursor=NULL;
free(history_draft);
history_draft=NULL;
if (!*command || (history_last && !strcmp(history_last->text, command))) return;
command_entry *entry=malloc(sizeof(*entry)+strlen(command)+1);
if (!entry) return;
*entry=(command_entry){.previous=history_last};
strcpy(entry->text, command);
if (history_last) history_last->next=entry;
history_last=entry;
}
void command_history_move(int direction)
{
if (direction < 0)
{
if (!history_cursor)
{
if (!history_last) return;
char *draft=strdup(cmd);
if (!draft) return;
free(history_draft);
history_draft=draft;
history_cursor=history_last;
}
else if (history_cursor->previous) history_cursor=history_cursor->previous;
}
else
{
if (!history_cursor) return;
history_cursor=history_cursor->next;
}
snprintf(cmd, CMD_MAX, "%s", history_cursor ? history_cursor->text : history_draft);
cmd_len=cursor_pos=strlen(cmd);
cmd_offset=0;
}
void command_history_free(void)
{
while (history_last)
{
command_entry *previous=history_last->previous;
free(history_last);
history_last=previous;
}
history_cursor=NULL;
free(history_draft);
history_draft=NULL;
}
// Insert path bytes literally; quoting and escaping are left to the user.
int insert_command_path(const char *path, int directory)
{
size_t path_length=strlen(path);
int slash=directory && (!path_length || path[path_length-1] != '/');
size_t length=path_length+(directory ? slash : 1);
if (length >= CMD_MAX-cmd_len) { errno=ENAMETOOLONG; return -1; }
// A directory has no trailing space so a filename can be appended.
memmove(cmd+cursor_pos+length, cmd+cursor_pos, cmd_len-cursor_pos+1);
memcpy(cmd+cursor_pos, path, path_length);
if (slash) cmd[cursor_pos+path_length]='/';
if (!directory) cmd[cursor_pos+path_length]=' ';
cmd_len+=length;
cursor_pos+=length;
return 0;
}
// Handle a simple quoted/escaped cd path; compound commands retain shell semantics.
int command_cd(const char *command)
{
while (*command == ' ' || *command == '\t') command++;
if (strncmp(command, "cd", 2) || (command[2] && command[2] != ' ' && command[2] != '\t')) return 0;
command+=2;
while (*command == ' ' || *command == '\t') command++;
char path[CMD_MAX];
int length=0, quote=0, finished=0;
for (; *command; command++)
{
int ch=(unsigned char)*command;
if (!quote && strchr(";&|<>\n", ch)) return 0;
if (!quote && (ch == ' ' || ch == '\t')) { finished=1; continue; }
if (finished) { show_errormsg("cd accepts one directory path"); return 1; }
if (ch == quote) { quote=0; continue; }
if (!quote && (ch == '\'' || ch == '"')) { quote=ch; continue; }
if (ch == '\\' && quote != '\'')
{
if (!command[1]) { quote=1; break; }
ch=(unsigned char)*++command;
}
path[length++]=ch;
}
path[length]=0;
if (quote) { show_errormsg("Unclosed quote or escape in cd path"); return 1; }
if (change_panel_directory(active_panel, length ? path : "~") != 0)
show_errormsg(SPRINTF("Cannot change directory:\n%s\n%s", path, strerror(errno)));
return 1;
}
// Execute a selected path literally, without passing its name through a shell.
int execute_file(const char *filename)
{
// Interrupt the child without terminating the file manager waiting for it.
void (*interrupt_handler)(int)=signal(SIGINT, SIG_IGN);
void (*quit_handler)(int)=signal(SIGQUIT, SIG_IGN);
pid_t child=fork();
if (child == 0)
{
signal(SIGINT, interrupt_handler);
signal(SIGQUIT, quit_handler);
execl(filename, filename, (char *)NULL);
perror(filename);
_exit(127);
}
int status=-1;
if (child > 0)
while (waitpid(child, &status, 0) < 0)
if (errno != EINTR) { status=-1; break; }
int error=errno;
signal(SIGINT, interrupt_handler);
signal(SIGQUIT, quit_handler);
errno=error;
return status;
}
// Keep the prompt and command on one row, scrolling in terminal columns.
void update_cmd()
{
attron(COLOR_PAIR(COLOR_WHITE_ON_BLACK));
move(LINES-2, 0);
clrtoeol();
char prompt[CMD_MAX];
snprintf(prompt, sizeof(prompt), "%s@%s:%s# ", username, unameData.nodename, active_panel->path);
addstr(SHORTEN(prompt, COLS-4));
prompt_length=getcurx(stdscr);
int width=COLS-prompt_length, column=text_column(cmd, cursor_pos, 1);
cursor_pos=text_offset(cmd, cmd_len, column, 1);
if (column-cmd_offset >= width) cmd_offset=column-width+1;
if (column < cmd_offset) cmd_offset=column;
draw_text(stdscr, LINES-2, prompt_length, cmd, cmd_offset, width);
move(LINES-2, prompt_length+column-cmd_offset);
wnoutrefresh(stdscr);
}