LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-06-2010, 07:11 AM   #1
Mogget
Member
 
Registered: Dec 2008
Location: Norway
Distribution: Debian
Posts: 43

Rep: Reputation: 15
Cannot get ncurses to delete a char with '\b' on screen in linux (c++)


Hello.

I've modified a program to show * instead of letters when typing a password. I'm trying to make it so that when pressing backspace a * will be removed.

Here's a rough example. The problem is that when trying to do this in the real program, '\b' is not recognized as backspace. Does anyone have an idea why?
Code:
#include <ncurses.h>

int main() {
   char input;
   input = getch();

   if( input == '\b' ) {
       printw("\b \b");
   }
}
Here's the complete source if you wish to see that.
Code:
#include <iostream>
#include <cstring>          // USED FOR STRNCPY AND STRCMP
#include <ncurses.h>        // USED FOR GETCH() AND LATER FOR VISUALS
using namespace std;

const int PASSLEN = 10;     // MAXIMUM LENGTH OF PASSWORD

class password {

    private:
        char mypassword[PASSLEN];       // SAVES PASSWORD SENT AS A PARAMETER AT PROGRAM START
        char secondpassword[PASSLEN];   // STORES THE PASSWORD WE TYPE IN
    public:
        password(const char argvalue[]);
        void savechar(const int index, const char character);
        char* returnpassword(const bool origsecond);

};

int main(/*int argc, char *argv[]*/) {

    char *argv[] = {" ", "test"};

    /* if(argc != 2) { // CHECKING IF WE HAVE GOTTEN ONE PARAMETER AND NOTHING ELSE

        cout << "\tUsage: " << argv[0] << " \"YOUR PASSWORD\"\n";
        return 1;

    }*/

    bool c = false;  // GETS PASSWORD UNTIL THIS IS TRUE
    char input;      // VARIABLE TO SAVE TEMP INPUT
    int p;           // STORES THE NUMBER OF CHARS IN PASS WE HAVE GOTTEN
    password pass(argv[1]);

    initscr();  // INITIALIZING NCURSES
    noecho();   // WHATS TYPED IN WILL NOT BE SHOWN ON SCREEN
    cbreak();   // DISSABLE LINE BUFFERING, ALLOW CONTROL STATEMENTS

    while(c == false) {

        printw("Password: ");

        for(p = 0;p < (PASSLEN - 1);p++) {

            printw("%d", p);

			input = getch();    // GET CHARACTER FOR PASSWORD
			if( input != '\n' && input != '\b' ) {  // CHECKING IF IT'S A BACKSPACE OR RETURN

				if( isprint( input ) != 0 ) {   // IF THE CHARACTER TYPED IN
				    pass.savechar(p, input);    // IS PRINTABLE, THEN IT GETS
				}                               // A * PRINTED ON SCREEN AND
				if( iscntrl( input ) == 0 ) {   // THE CHAR IS SAVED FOR COMPARISON
					printw("*");
				}
			}
			else if( input == '\n' ) {
			    pass.savechar(p, '\0');
				break;
			}
			else if( input == '\b' ) {
				if( p > 0 ) {
				    printw("\b \b");
				    pass
				    --p;
				}
				--p;
			}

		}

		if( p == (PASSLEN - 1) ) {
		    printw("\a\n\nTo many characters entered!\nPlease try again.\n");
		} else if( strcmp(pass.returnpassword(true), pass.returnpassword(false)) != 0 ) {
			printw("\a\n\n\tLogin Unsuccessful!\n\n");
		} else {
			printw("\n\n\tLogin Successful!\n\n");
			printw("Password entered: "); printw(pass.returnpassword(false)); printw("\n");
			c = true;
		}

    }

    endwin();   // STOPPING NCURSES
    return 0;

}

password::password(const char argvalue[]) {

    strncpy(mypassword, argvalue, PASSLEN);

}

void password::savechar(const int index, const char character) {

    secondpassword[index] = character;

}

char* password::returnpassword(const bool origsecond) {

    if(origsecond == true) {

        return mypassword;

    } else {

        return secondpassword;

    }

}
Any help is greatly appreciated.
 
Old 01-06-2010, 11:55 AM   #2
rnesius
LQ Newbie
 
Registered: Jun 2009
Location: St. Cloud, MN
Distribution: Ubuntu LTS
Posts: 15

Rep: Reputation: 2
Have you tried checking against the ASCII code for the Backspace character by using a different constant or escape sequence? (ctrl-h) or \010 (in octal).

I suggest trying to figure out what is stored in that character variable and then find a way to specify that as a literal. It seems the \b escape code isn't getting it done.

That said, I'd test for KEY_BACKSPACE, not '\b', for anything returned by character grab from ncurses.
 
Old 01-06-2010, 01:15 PM   #3
Mogget
Member
 
Registered: Dec 2008
Location: Norway
Distribution: Debian
Posts: 43

Original Poster
Rep: Reputation: 15
Thank you for your answer. Your answer made me think a little and i pulled out gdb and debugged the code. It seems that my backspace button is not the same as '\b' but in my case '\177' (DEL) button.

I'm not sure if this is ncurses, bash, ubuntu or the kernel driver that is to blame.

So if anyone gets the problem that your backspace doesn't work see if it might be using something else on the ascii chart.
 
Old 01-07-2010, 09:31 AM   #4
rnesius
LQ Newbie
 
Registered: Jun 2009
Location: St. Cloud, MN
Distribution: Ubuntu LTS
Posts: 15

Rep: Reputation: 2
Keymappings

You're getting the DEL key from your character grab for your specific terminal mapping. It's possible running your program on another terminal will result in your program breaking because on another terminal you may really get \010 (BACKSPACE). So generally it's best to check against constants defined for keys in ncurses.h or possibly in keymap files.

In otherwords, for portability I'd check for DELETE and BACKSPACE.

These are the kind of "gotchas" you run into when writing code that uses terminal command sets for graphics. :-/

In any case, glad you got it working.
 
1 members found this post helpful.
Old 01-07-2010, 01:58 PM   #5
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by rnesius View Post
You're getting the DEL key from your character grab for your specific terminal mapping. It's possible running your program on another terminal will result in your program breaking because on another terminal you may really get \010 (BACKSPACE). So generally it's best to check against constants defined for keys in ncurses.h or possibly in keymap files.

In otherwords, for portability I'd check for DELETE and BACKSPACE.

These are the kind of "gotchas" you run into when writing code that uses terminal command sets for graphics. :-/

In any case, glad you got it working.
BTW backspace is not \10 its actually \8.
 
0 members found this post helpful.
Old 01-07-2010, 03:19 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by smeezekitty View Post
BTW backspace is not \10 its actually \8.
010 (octal) == 8 (decimal)
 
Old 01-07-2010, 03:39 PM   #7
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by ntubski View Post
010 (octal) == 8 (decimal)
]
WTF? no body uses octal.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Ncurses: printw(); prints to screen even if refresh(); is not called - why? violagirl23 Programming 3 03-10-2008 08:12 AM
how to print text in color by ncurses without opening a ncurses window Greatrebel Programming 0 12-20-2006 09:15 AM
printing the same char to screen loads of different times. pritchardtom Programming 3 11-24-2005 03:03 AM
Trailing whitespaces ( space char) and Ncurses terminal refreshes bretzeltux Programming 0 03-02-2004 02:47 PM
ncurses - init screen after systemcall lea Programming 3 10-24-2002 11:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration