1 /* FDUPES Copyright (c) 2018 Adrian Lopez
3 Permission is hereby granted, free of charge, to any person
4 obtaining a copy of this software and associated documentation files
5 (the "Software"), to deal in the Software without restriction,
6 including without limitation the rights to use, copy, modify, merge,
7 publish, distribute, sublicense, and/or sell copies of the Software,
8 and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be
12 included in all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
25 #include "ncurses-getcommand.h"
29 extern volatile sig_atomic_t got_sigint;
31 /* get command and arguments from user input */
32 void get_command_arguments(wchar_t **arguments, wchar_t *input)
39 for (x = 0; x < l; ++x)
44 *arguments = input + x + 1;
46 *arguments = input + x;
49 int get_command_text(wchar_t **commandbuffer, size_t *commandbuffersize, WINDOW *promptwin, struct prompt_info *prompt, int cancel_on_erase, int append)
57 wchar_t *realloccommandbuffer;
60 set_prompt_active_state(prompt, 1);
63 if (*commandbuffer == 0)
65 *commandbuffersize = 80;
66 *commandbuffer = malloc(*commandbuffersize * sizeof(wchar_t));
67 if (*commandbuffer == 0)
69 set_prompt_active_state(prompt, 0);
70 return GET_COMMAND_ERROR_OUT_OF_MEMORY;
76 (*commandbuffer)[0] = L'\0';
80 print_prompt(promptwin, prompt, *commandbuffer);
82 prompt->cursor = wcswidth(*commandbuffer, wcslen(*commandbuffer));
84 wmove(promptwin, 0, wcslen(prompt->text) + prompt->cursor - prompt->offset);
94 keyresult = wget_wch(promptwin, &wch);
100 (*commandbuffer)[0] = '\0';
102 set_prompt_active_state(prompt, 0);
104 return GET_COMMAND_CANCELED;
106 } while (keyresult == ERR);
117 (*commandbuffer)[0] = '\0';
119 set_prompt_active_state(prompt, 0);
121 return GET_COMMAND_CANCELED;
136 length = wcslen(*commandbuffer);
138 if (length + 1 >= *commandbuffersize)
140 newsize = *commandbuffersize * 2;
142 realloccommandbuffer = (wchar_t*)realloc(*commandbuffer, newsize * sizeof(wchar_t));
143 if (realloccommandbuffer == 0)
145 set_prompt_active_state(prompt, 0);
146 return GET_COMMAND_ERROR_OUT_OF_MEMORY;
149 *commandbuffer = realloccommandbuffer;
150 *commandbuffersize = newsize;
153 for (c = length + 1; c >= prompt->cursor + 1; --c)
154 (*commandbuffer)[c] = (*commandbuffer)[c-1];
156 (*commandbuffer)[prompt->cursor] = wch;
158 set_prompt_active_state(prompt, 1);
160 update_prompt(promptwin, prompt, *commandbuffer, wcwidth(wch));
165 else if (keyresult == KEY_CODE_YES)
170 length = wcslen(*commandbuffer);
174 set_prompt_active_state(prompt, 0);
177 return GET_COMMAND_CANCELED;
180 oldch = (*commandbuffer)[prompt->cursor];
182 if (prompt->cursor > 0)
183 for (c = prompt->cursor; c <= length; ++c)
184 (*commandbuffer)[c-1] = (*commandbuffer)[c];
186 update_prompt(promptwin, prompt, *commandbuffer, oldch != 0 ? -wcwidth(oldch) : -1);
191 length = wcslen(*commandbuffer);
193 if (prompt->cursor < length)
194 for (c = prompt->cursor; c <= length; ++c)
195 (*commandbuffer)[c] = (*commandbuffer)[c+1];
200 length = wcslen(*commandbuffer);
202 oldch = (*commandbuffer)[prompt->cursor];
204 update_prompt(promptwin, prompt, *commandbuffer, oldch != 0 ? -wcwidth(oldch) : -1);
209 length = wcslen(*commandbuffer);
211 oldch = (*commandbuffer)[prompt->cursor];
213 if (prompt->cursor + wcwidth((*commandbuffer)[prompt->cursor]) <= length)
214 update_prompt(promptwin, prompt, *commandbuffer, wcwidth(oldch));
219 return GET_COMMAND_KEY_NPAGE;
222 return GET_COMMAND_KEY_PPAGE;
225 return GET_COMMAND_KEY_SF;
228 return GET_COMMAND_KEY_SR;
231 return GET_COMMAND_RESIZE_REQUESTED;
235 print_prompt(promptwin, prompt, *commandbuffer);
237 wmove(promptwin, 0, wcslen(prompt->text) + prompt->cursor - prompt->offset);
240 } while (docommandinput);
242 set_prompt_active_state(prompt, 0);
244 return GET_COMMAND_OK;