OS X move terminal caret with arrow keys - c++

I made a simple C programme that demonstrates the issue. Here it is.
#include <stdio.h>
int main(int argc, const char * argv[]) {
char buffer[128];
memset(buffer, 0, sizeof(buffer));
printf("Type data:\n");
scanf("%126s", buffer);
printf("%s\n", buffer);
getchar();
return 0;
}
The problem is that when the application is waiting for user input with scanf() and user user wants to edit line he typed, and to do move caret with arrows, the caret is not moving but new ugly input is inserted.
For some reason it does not handle the keys in a way I expect. I'm not able to go to the previous typed line with the up key, as well.
Obviously I should deliberately enable this behaviour. Could you advice, how can I do that?

Use readline(). Here's a simple example:
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <ctype.h>
#define MAX_LINE_LEN 80
int main() {
char *line_buffer;
int n, i;
while (1) {
line_buffer = readline("Say something: ");
if (!line_buffer) break;
for (i=0; line_buffer[i]; i++) {
line_buffer[i] = toupper(line_buffer[i]);
}
printf("YOU SAID: %s\n",line_buffer);
}
putchar('\n');
return 0;
}
/* (Compile with cc foo.c -lreadline -o foo) */

Related

The program is not opening to me a shell

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char **argv){
FILE *fp = fopen("1.pass", "r");
struct {char pass[20], msg_err[20];} pwfile = {{0}};
char ptr[0];
if(!fp || argc != 2)
return -1;
fread(pwfile.pass, 1, 20, fp);
pwfile.pass[19] = 0;
ptr[atoi(argv[1])] = 0;
fread(pwfile.msg_err, 1, 19, fp);
fclose(fp);
if(!strcmp(pwfile.pass, argv[1])) {
setuid(0);
execl("/bin/sh", "sh", 0);
} else
puts(pwfile.msg_err);
return 0;
}
Hello there.
I give on input to the program a password. The program checks this password, which is in 1.pass. If all is correct - opens to me a shell, if it not correct - print ACCESS DENIED.
In my cases, it didn't open shell up to me, I don't know why. Any ideas what I missed?
char ptr[0];
ptr[atoi(argv[1])] = 0;
This is not legal C++, as there are no zero sized arrays in C++. Even if some C++ compilers offer zero sized arrays as an extension, this particular code doesn't do anything useful, and may or may not format your hard disk.

NCurses getch always returns ERR (-1)

I've just started to work with ROS and I'm trying to write a node that publish keys in a topic.
I have created a node on a Linux Ubuntu 16.04.4 using ncurses.
This is my code:
#include <curses.h>
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
int main(int argc, char **argv)
{
int ch;
nodelay(stdscr, TRUE);
ros::init(argc, argv, "keyboard_driver");
ros::NodeHandle n;
ros::Publisher key_pub = n.advertise<std_msgs::String>("keys", 1);
ros::Rate loop_rate(100);
while (ros::ok())
{
std_msgs::String msg;
std::stringstream ss;
if ((ch = getch()) != ERR)
{
ss << ch;
std::cout << ch;
msg.data = ss.str();
ROS_INFO("%s", msg.data.c_str());
key_pub.publish(msg);
}
ros::spinOnce();
loop_rate.sleep();
}
return 0;
}
I'm using ncurses to avoid terminal buffer.
The topic appears, but I don't get anything if, in another terminal, I run this command:
rostopic echo /keys
Debugging it I have found that getch() always return -1.
How can I do to make it work?
UPDATE
I have tried this small program, and it doesn't print anything:
#include <iostream>
#include <curses.h>
int main(int argc, char **argv)
{
int ch;
cbreak();
nodelay(stdscr, TRUE);
for(;;)
{
if ((ch = getch()) != ERR)
{
std::cout << ch;
}
}
return 0;
}
You've set nodelay so getch will return immediately with ERR if there's no data ready from the terminal. That's why getch is returning -1 (ERR). You haven't set cbreak or raw to disable terminal buffering, so you're still getting that -- no data will come from the terminal until Enter is hit.
So add a call to cbreak() at the start (just before or after the call to nodelay()) and it should work as you expect.
To use getch() you have to do the following:
#include <iostream>
#include <curses.h>
#include <signal.h>
#include <stdlib.h>
void quit(int sig)
{
endwin();
exit(0);
}
int main(int argc, char **argv)
{
int ch;
signal(SIGINT,quit);
initscr();
cbreak();
nodelay(stdscr, TRUE);
for(;;)
{
if ((ch = getch()) != ERR)
{
std::cout << ch;
}
}
return 0;
}
I forget to add initscr(); call at the beginning and endwin(); at the end of the program.
More info about how to use ncurses library here.

C++ KeyboardEmulator

I'm trying to code a KeyBoard Emulator for Linux, first I thought to use python to achieve it, but all the libraries were using X (and I don't want to use it). I decided to code in C++ to write in the keyboard buffer. After a few days of learning C++ and doing some research on Linux and how the input system works, I came up with this:
#include <stdio.h>
#include <fcntl.h>
#include <linux/input.h>
#include <sstream>
#include <unistd.h>
#define EV_PRESSED 1
#define EV_RELEASED 0
int Emulate(char character)
{
printf("Starting the keyboard buffer writer\n");
int fd = 0;
char *device = "/dev/input/event1";
//write to buffer
if( (fd = open(device, O_RDWR)) > 0 )
{
struct input_event event;
printf("The keyboard code is: %d \n", KEY_A);
event.type = EV_KEY;
event.value = EV_PRESSED;
event.code = KEY_A;
write(fd, &event, sizeof(struct input_event));
event.value = EV_RELEASED;
event.code = KEY_A;
write(fd, &event, sizeof(struct input_event));
close(fd);
}
return 0;
}
int main(int argc, char *argv[]){
for(int i=0; i < sizeof(argv[1])/sizeof(int); i++){
Emulate(argv[1][i]);//for each car in the argv 1 call Emulate
}
}
As you can see I'm writing manually KEY_A to press the a key. And I was wondering if there is a function or a way to change a char to the keycodes definded in the file /usr/include/linux/input-event-codes.h.
Thank you for your help !

glfwPollEvents() doesn't call keycallback on key press

I have the code down below, it seems to be working if I use glfwGetKey(), but as it is it doesn't exit the loop,nor even seem to call the input function. I tried passing *input instead of input, but no dice. What could be causing this?
#include <display.h>
#include <GL/glfw.h>
#include <stdlib.h>
#include <stdio.h>
bool running;
void GLFWCALL input( int key, int action )
{
//if(key == GLFW_KEY_ESC ){
running = false;
//}
printf("%d",key);
}
int main(int argc, char* argv[])
{
running = true;
if(argc==3){
int width = atoi(argv[1]);
int height = atoi(argv[2]);
Display d(width,height);
glfwSetKeyCallback( *input );
d.open();
while(running){
glfwPollEvents();
printf("Running");
}
printf("\n %d,%d %d\n", width,height,GLFW_KEY_ESC);
d.close();
return 1;
}
else {
printf("Usage: GLFW_play width height");
return 0;
}
}
I think the only problem with your program is that you don't call glfwInit() before the other glfw functions.
According to page 5 of the GLFW User Guide, you must call glfwInit before any other function from the library to ensure proper functionality.
Also, don't pass *input, just pass input.

Program that returns a list of drive letters

In the example of my computer the desired output should be: "C: E: F: H: N:" . I know it's possible, but what is the simpliest way to do that? Pottering in QueryDosDevice output
#ifndef UNICODE
#define UNICODE
#endif
#include <Windows.h>
#include <fstream>
#include <iostream>
const int REPORT_LENGTH = 5000;
int main(void)
{
TCHAR targetPath[REPORT_LENGTH];
std::ofstream oFile;
oFile.open("dos device query.txt");
QueryDosDevice(NULL,targetPath,REPORT_LENGTH);
for(int i=0; i<REPORT_LENGTH;i++)
if (targetPath[i]=='\0')(targetPath[i]='\n');
for(int i=0; i<REPORT_LENGTH; i++)
oFile<<static_cast<char>(targetPath[i]);
oFile.close();
return 0;
}
would be a huge waste of time and resources. Also function GetLogicalDriveStrings has betrayed me a lot.
#include <Windows.h>
int main()
{
TCHAR buffer[50];
GetLogicalDriveStrings(50,buffer);
MessageBox(0,buffer,"Drives in the system",MB_OK);
return 0;
}
It shows only the "C:\" volumine.
Example with GetLogicalDrives, albeit not with concatenating to a string (which is left as an exercise to the OP and the readers ;)):
#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
int __cdecl _tmain(int argc, _TCHAR *argv[])
{
// Get the bit mask of drive letters
DWORD drives = ::GetLogicalDrives();
// Go through all possible letters from a to z
for(int i = 0; i < 26; i++)
{
// Check if the respective bit is set
if(drives & (1 << i))
{
// ... and if so, print it
_tprintf(TEXT("Drive %c: exists\n"), _T('A') + i);
}
}
return 0;
}
GetLogicalDriveStrings() is the way to go, you just have to use to correctly. You are assuming it returns a single string containing all of the drive strings, but that is not true. It returns an array of strings, one for each drive, so you have to loop through the array instead:
#include <windows.h>
int main()
{
TCHAR buffer[(4*26)+1] = {0};
GetLogicalDriveStrings(sizeof(buffer) / sizeof(TCHAR), buffer);
for (LPTSTR lpDrive = buffer; *lpDrive != 0; lpDrive += 4)
MessageBox(NULL, lpDrive, "Drive in the system", MB_OK);
return 0;
}