Find the answer to your Linux question:
Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 28
Hi I have written my own command line interpreter Now I need to add the up and down arrow key support But in the intial version I had not used ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141

    CLI


    Hi
    I have written my own command line interpreter

    Now I need to add the up and down arrow key support
    But in the intial version I had not used ncurses and was using normal
    fprintf statemens for printing it to the console or a file

    Now since i have to detect the up and down arrow key i have to use ncurses. I will have to change the whole fprintf statements to printw statements and even handle the cursor positions

    Is there anyother method that i can implement the up and down arrow key without including ncurses. or more clearly is there anyway of detecting the up and down arrow key press without using ncurses

    Thanks

  2. #2
    Linux Newbie
    Join Date
    Oct 2004
    Posts
    158
    This is a simplified version of what ncurses uses to read individual keystrokes. This is set up to get a single keystroke and reset the terminal back to the way it was, you may want to leave the new terminal settings on until you get a return.

    Code:
    #include <termios.h>
    #include <unistd.h>
    #include <assert.h>
    #include <string.h>
    /*------------------------------------------------*/
    int getch&#40;void&#41; &#123;
          int c=0;
    
          struct termios org_opts, new_opts;
          int res=0;
              //-----  store old settings -----------
          res=tcgetattr&#40;STDIN_FILENO, &org_opts&#41;;
          assert&#40;res==0&#41;;
              //---- set new terminal parms --------
          memcpy&#40;&new_opts, &org_opts, sizeof&#40;new_opts&#41;&#41;;
          new_opts.c_lflag &= ~&#40;ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL&#41;;
          tcsetattr&#40;STDIN_FILENO, TCSANOW, &new_opts&#41;;
          c=getchar&#40;&#41;;
              //------  restore old settings ---------
          res=tcsetattr&#40;STDIN_FILENO, TCSANOW, &org_opts&#41;;
          assert&#40;res==0&#41;;
          return&#40;c&#41;;
    &#125;

  3. #3
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    You may be interested in just using the GNU readline library, if that's enough for you. It's really convenient to use. Here's an example of a main loop that I wrote for a program a while ago:
    Code:
        char *p, **a;
        
        ...
        while&#40;!done&#41; &#123;
            if&#40;&#40;p = readline&#40;"> "&#41;&#41; == NULL&#41; &#123;
                printf&#40;"\n"&#41;;
                break;
            &#125;
            a = tokenize&#40;p&#41;;
            if&#40;calen&#40;a&#41; > 0&#41; &#123;
                docmd&#40;calen&#40;a&#41;, a&#41;;
                add_history&#40;p&#41;;
            &#125;
            free&#40;p&#41;;
            freeca&#40;a&#41;;
        &#125;

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    Thanks guys

    I will check this out and let you know how i proceed with this logics


    Regards

    Sharon

  6. #5
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Dolda , are tokenize , freeca , calen , docmd your own functions ?

    Jim , I'm trying to understand your programme.Which parts of it
    ensure that:
    a) The arrow keys won't be interpreted but will be sent to your programme ?
    b) That your programme will receive characters as they are being typed
    ie before ENTER is pressed ?

  7. #6
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    jim,
    i just executed your program with some additonal code i added and i am able to get which ever character i pressed on the keyboard

    but for the arrow keys i am just getting the value 27 for which ever arrow key i press so i will not be able to differentiate between them

    anyway that was a new logic i learnt about terminals

    and moreover i need to support all the arrow keys so that editing of the command line is possible after typing the whole command using the left and right arrow key and history implementation using the up and down arrow keys.

    thats means the cursor position manipulation also comes into effect over there. and i have to manage the cursor position each time i print something on the screen.

    dolda i too have the same doubt as Santa like those functions are they your own functions???

    i know to write a new CLI using ncurses and i just wrote one separately today but to incorporate this new design in the old one is really difficult as that one was almost over and now i was told to implement the arrow keys for further developement

  8. #7
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Quote Originally Posted by Santa's little helper
    Dolda , are tokenize , freeca , calen , docmd your own functions ?
    Yup. The `ca' stands for character array. Other than that, I think the names make them self-explanatory. freeca, calen and docmd are really simple functions:
    Code:
    void freeca&#40;char **ca&#41;
    &#123;
        char **c;
        
        for&#40;c = ca; *c; c++&#41;
            free&#40;*c&#41;;
        free&#40;ca&#41;;
    &#125;
    
    const int calen&#40;char **a&#41;
    &#123;
        int i;
        
        for&#40;i = 0; *a; a++, i++&#41;;
        return&#40;i&#41;;
    &#125;
    
    struct &#123;
        char *cmd;
        int &#40;*fun&#41;&#40;int argc, char **argv&#41;;
    &#125; commands&#91;&#93; = &#123;
        &#123;"quit", c_quit&#125;,
        &#123;"magic", c_magic&#125;,
        &#123;NULL, NULL&#125;
    &#125;;
    
    int docmd&#40;int argc, char **argv&#41;
    &#123;
        int i;
        
        for&#40;i = 0; commands&#91;i&#93;.cmd; i++&#41; &#123;
            if&#40;!strcmp&#40;commands&#91;i&#93;.cmd, argv&#91;0&#93;&#41;&#41;
                return&#40;commands&#91;i&#93;.fun&#40;argc, argv&#41;&#41;;
        &#125;
        fprintf&#40;stderr, "audiocd&#58; %s&#58; no such command\n", argv&#91;0&#93;&#41;;
        return&#40;1&#41;;
    &#125;
    tokenize is pretty simple too, but it would take bit too much space (about 50 lines) to provide it here, unless would ask me to. It just tokenizes a string into a `char **' with support for a couple of quoting styles.

    Edit: Corrected the indentation mangling made by phpBB.

  9. #8
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Quote Originally Posted by sharonenoch
    but for the arrow keys i am just getting the value 27 for which ever arrow key i press so i will not be able to differentiate between them
    Are you sure about that? If you call Jim's function repeatedly, you should be getting multiple returns for arrow keys. For example:
    Code:
    #include <termios.h>
    #include <unistd.h>
    #include <assert.h>
    #include <string.h>
    #include <stdio.h>
    
    &#91;Jim's getch&#40;&#41; function&#93;
    
    int main&#40;int argc, char **argv&#41;
    &#123;
        while&#40;1&#41; &#123;
            printf&#40;"%i\n", getch&#40;&#41;&#41;;
        &#125;
    &#125;
    It gives the following output:
    Code:
    &#91;Up arrow pressed&#93;
    27
    91
    65
    &#91;Right arrow pressed&#93;
    27
    91
    67
    &#91;Down arrow pressed&#93;
    27
    91
    66
    &#91;Left arrow pressed&#93;
    27
    91
    68
    As you can see, you can differentiate between using the last returned character.

  10. #9
    Linux Newbie
    Join Date
    Oct 2004
    Posts
    158
    Quote Originally Posted by Santa's little helper
    Dolda , are tokenize , freeca , calen , docmd your own functions ?

    Jim , I'm trying to understand your programme.Which parts of it
    ensure that:
    a) The arrow keys won't be interpreted but will be sent to your programme ?
    b) That your programme will receive characters as they are being typed
    ie before ENTER is pressed ?
    Chiefly the ICANON - canonical input. It reads each keystroke rather than each line. This only works for a tty.

  11. #10
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Quote Originally Posted by dolda2000
    Quote Originally Posted by sharonenoch
    but for the arrow keys i am just getting the value 27 for which ever arrow key i press so i will not be able to differentiate between them
    Are you sure about that? If you call Jim's function repeatedly, you should be getting multiple returns for arrow keys.
    I haven't tried it but this sounds correct.The problem is that I don't
    think there is a specific sequence of characters which corresponds
    to each of the "special" keys ; different monitors (or emulators) may
    have different sequences so you need to look into the terminfo database
    to see which special keys are supported and what is the character sequence
    for each one of them.

    Another difficulty is the following: say you get the sequence 27 91 67.
    You have to distinguish if it was a single key press which produced
    the sequence or more than one key presses which produced some of
    the codes.I'm not sure how that would work in the context of Jim's
    programme.By the way there is a book called Advanced C
    programming for displays : character displays, windows, and keyboards
    for the UNIX and MS-DOS operating systems
    by Marc J. Rochkind.
    It presents a full implementation of curses for Unix using just the usual
    I/O functions of Unix like read,write,fcntl etc. But it doesn't give many details
    about the keyboard reading part.It might still be useful though.

Page 1 of 3 1 2 3 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •