"Fossies" - the Fresh Open Source Software Archive

Member "fdupes-2.1.2/ncurses-getcommand.c" (12 Aug 2020, 6235 Bytes) of package /linux/privat/fdupes-2.1.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ncurses-getcommand.c" see the Fossies "Dox" file reference documentation.

    1 /* FDUPES Copyright (c) 2018 Adrian Lopez
    2 
    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:
   10 
   11    The above copyright notice and this permission notice shall be
   12    included in all copies or substantial portions of the Software.
   13 
   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. */
   21 
   22 #include "config.h"
   23 #include <stdlib.h>
   24 #include <signal.h>
   25 #include "ncurses-getcommand.h"
   26 
   27 #define KEY_ESCAPE 27
   28 
   29 extern volatile sig_atomic_t got_sigint;
   30 
   31 /* get command and arguments from user input */
   32 void get_command_arguments(wchar_t **arguments, wchar_t *input)
   33 {
   34   size_t l;
   35   size_t x;
   36 
   37   l = wcslen(input);
   38 
   39   for (x = 0; x < l; ++x)
   40     if (input[x] == L' ')
   41       break;
   42 
   43   if (input[x] == L' ')
   44     *arguments = input + x + 1;
   45   else
   46     *arguments = input + x;
   47 }
   48 
   49 int get_command_text(wchar_t **commandbuffer, size_t *commandbuffersize, WINDOW *promptwin, struct prompt_info *prompt, int cancel_on_erase, int append)
   50 {
   51   int docommandinput;
   52   int keyresult;
   53   wint_t wch;
   54   wint_t oldch;
   55   size_t length;
   56   size_t newsize;
   57   wchar_t *realloccommandbuffer;
   58   size_t c;
   59 
   60   set_prompt_active_state(prompt, 1);
   61   wrefresh(promptwin);
   62 
   63   if (*commandbuffer == 0)
   64   {
   65     *commandbuffersize = 80;
   66     *commandbuffer = malloc(*commandbuffersize * sizeof(wchar_t));
   67     if (*commandbuffer == 0)
   68     {
   69       set_prompt_active_state(prompt, 0);
   70       return GET_COMMAND_ERROR_OUT_OF_MEMORY;
   71     }
   72   }
   73 
   74   if (!append)
   75   {
   76     (*commandbuffer)[0] = L'\0';
   77   }
   78   else
   79   {
   80     print_prompt(promptwin, prompt, *commandbuffer);
   81 
   82     prompt->cursor = wcswidth(*commandbuffer, wcslen(*commandbuffer));
   83 
   84     wmove(promptwin, 0, wcslen(prompt->text) + prompt->cursor - prompt->offset);
   85 
   86     wrefresh(promptwin);
   87   }
   88 
   89   docommandinput = 1;
   90   do
   91   {
   92     do
   93     {
   94       keyresult = wget_wch(promptwin, &wch);
   95 
   96       if (got_sigint)
   97       {
   98         got_sigint = 0;
   99 
  100         (*commandbuffer)[0] = '\0';
  101 
  102         set_prompt_active_state(prompt, 0);
  103 
  104         return GET_COMMAND_CANCELED;
  105       }
  106     } while (keyresult == ERR);
  107 
  108     if (keyresult == OK)
  109     {
  110       switch (wch)
  111       {
  112         case KEY_ESCAPE:
  113           prompt->offset = 0;
  114           prompt->cursor = 0;
  115           docommandinput = 0;
  116 
  117           (*commandbuffer)[0] = '\0';
  118 
  119           set_prompt_active_state(prompt, 0);
  120 
  121           return GET_COMMAND_CANCELED;
  122 
  123         case '\n':
  124           prompt->offset = 0;
  125           prompt->cursor = 0;
  126           docommandinput = 0;
  127           continue;
  128 
  129         case '\t':
  130           continue;
  131 
  132         default:
  133           if (!iswprint(wch))
  134             continue;
  135 
  136           length = wcslen(*commandbuffer);
  137 
  138           if (length + 1 >= *commandbuffersize)
  139           {
  140             newsize = *commandbuffersize * 2;
  141 
  142             realloccommandbuffer = (wchar_t*)realloc(*commandbuffer, newsize * sizeof(wchar_t));
  143             if (realloccommandbuffer == 0)
  144             {
  145               set_prompt_active_state(prompt, 0);
  146               return GET_COMMAND_ERROR_OUT_OF_MEMORY;
  147             }
  148 
  149             *commandbuffer = realloccommandbuffer;
  150             *commandbuffersize = newsize;
  151           }
  152 
  153           for (c = length + 1; c >= prompt->cursor + 1; --c)
  154             (*commandbuffer)[c] = (*commandbuffer)[c-1];
  155 
  156           (*commandbuffer)[prompt->cursor] = wch;
  157 
  158           set_prompt_active_state(prompt, 1);
  159 
  160           update_prompt(promptwin, prompt, *commandbuffer, wcwidth(wch));
  161 
  162           break;
  163       }
  164     }
  165     else if (keyresult == KEY_CODE_YES)
  166     {
  167       switch (wch)
  168       {
  169         case KEY_BACKSPACE:
  170           length = wcslen(*commandbuffer);
  171 
  172           if (length == 0)
  173           {
  174             set_prompt_active_state(prompt, 0);
  175 
  176             if (cancel_on_erase)
  177               return GET_COMMAND_CANCELED;
  178           }
  179 
  180           oldch = (*commandbuffer)[prompt->cursor];
  181 
  182           if (prompt->cursor > 0)
  183             for (c = prompt->cursor; c <= length; ++c)
  184               (*commandbuffer)[c-1] = (*commandbuffer)[c];
  185 
  186           update_prompt(promptwin, prompt, *commandbuffer, oldch != 0 ? -wcwidth(oldch) : -1);
  187 
  188           break;
  189 
  190         case KEY_DC:
  191           length = wcslen(*commandbuffer);
  192 
  193           if (prompt->cursor < length)
  194             for (c = prompt->cursor; c <= length; ++c)
  195               (*commandbuffer)[c] = (*commandbuffer)[c+1];
  196 
  197           break;
  198 
  199         case KEY_LEFT:
  200           length = wcslen(*commandbuffer);
  201 
  202           oldch = (*commandbuffer)[prompt->cursor];
  203 
  204           update_prompt(promptwin, prompt, *commandbuffer, oldch != 0 ? -wcwidth(oldch) : -1);
  205 
  206           break;
  207 
  208         case KEY_RIGHT:
  209           length = wcslen(*commandbuffer);
  210 
  211           oldch = (*commandbuffer)[prompt->cursor];
  212 
  213           if (prompt->cursor + wcwidth((*commandbuffer)[prompt->cursor]) <= length)
  214             update_prompt(promptwin, prompt, *commandbuffer, wcwidth(oldch));
  215 
  216           break;
  217 
  218         case KEY_NPAGE:
  219           return GET_COMMAND_KEY_NPAGE;
  220 
  221         case KEY_PPAGE:
  222           return GET_COMMAND_KEY_PPAGE;
  223 
  224         case KEY_SF:
  225           return GET_COMMAND_KEY_SF;
  226 
  227         case KEY_SR:
  228           return GET_COMMAND_KEY_SR;
  229 
  230         case KEY_RESIZE:
  231           return GET_COMMAND_RESIZE_REQUESTED;
  232       }
  233     }
  234 
  235     print_prompt(promptwin, prompt, *commandbuffer);
  236 
  237     wmove(promptwin, 0, wcslen(prompt->text) + prompt->cursor - prompt->offset);
  238 
  239     wrefresh(promptwin);
  240   } while (docommandinput);
  241 
  242   set_prompt_active_state(prompt, 0);
  243 
  244   return GET_COMMAND_OK;
  245 }