#Input not working in Raw Mode

210 messages · Page 1 of 1 (latest)

native burrowBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

oak nebula
#

This code is all sorts of wrong, I don't even know where to begin.

ivory sentinel
#

begin which is the most important

muted oak
#

Also explain what goes wrong

#

Because its hard to deduce what not working "correctly"

ivory sentinel
#

but I am sure that the problem arises in this function

#

by what I have done unit now

#

by all that printing stuff onto the terminal

muted oak
ivory sentinel
ivory sentinel
muted oak
#

Ok

ivory sentinel
#

I am only printing the output once

#

but it shows it 2 times

oak nebula
#

First of all, where is NoOfCharType defined? The function certainly doesn't initialize it. Secondly, what's this ReadKey()? The name is descriptive but it's not standard C and I don't know why you're using anything other than standard C for this case. Third, that's not the range of visible characters, if you use a weird locale that might not apply. Why are your newlines terminated in an \r, are you redirecting your shell's output to a file you intend to read on Windows? Why are you using EXIT_FAILURE and EXIT_SUCCESS which are for the process error codes? Why do you have a random semicolon in the code? Why are you using braces for cases? Why are you using a switch statement at all since all you are doing is the equivalent of an if statement? Why are you printing error messages inside the input function? So many questions.

#

Why is a function called ReadInput printing stuff?

ivory sentinel
#

this is not my whole code

oak nebula
#

But it's poorly written and I am telling you these are all problems.

ivory sentinel
#

to give it an illusion of it being in normal mode

#

so that I could do terminal stuff

oak nebula
#

It doesn't matter, ReadInput is printing a prompt and interpreting input. It's not reading input.

ivory sentinel
#

I am making a shell

oak nebula
#

I understand what you are making.

ivory sentinel
#

so what's the problem

ivory sentinel
oak nebula
#

And that is what ReadInput means? Why didn't you name it DrawCircles?

#

That's not what you are doing.

ivory sentinel
oak nebula
#

Anyway, first of all, to actually read your prompt, you only need to fgets() into a string.

#

Yes I do.

ivory sentinel
#

then you should know what I am doing

oak nebula
#

ReadInput is a name that is not at all what you are doing. The name makes no sense.

ivory sentinel
#

you know what rawmode means atleast right?

oak nebula
#

Yes, and it's not even a shell thing, it's a terminal thing.

#

This is a hodgepodge.

ivory sentinel
#

it's a terminal

#

ofc

oak nebula
#

A shell is not a terminal.

#

Like at all.

ivory sentinel
#

you need to enter the raw mode to do that

#

I have made one without a raw mode

oak nebula
#

That's a shell feature, not a terminal feature.

ivory sentinel
#

how does it do that

#

that's my question

#

explain

#

how does shell know you pressed the arrow up key without pressing enter

oak nebula
#

By interpreting an escape sequenced passed by the terminal, usually.

#

sequence*

ivory sentinel
#

absolutely

#

you either don't understand what I am saying or you don't know your stuff right

oak nebula
#

I understand what you are saying, you just don't understand what you are saying is nonsense. And if you stop it with the attitude I could potentially explain it to you.

#

You have a bad mental image of how it works and there are many things wrong with how you structured the code as well.

#

So, shall I continue?

ivory sentinel
#

what I am saying is escape sequences doesn't give you the input

#

you cannot get that feature without entering the raw mode

ivory sentinel
#

but you don't

oak nebula
#

What does this have to do with anything I've said?

ivory sentinel
#

this is exactly what I am replying to

#

Actually make that feature

#

without endering the raw mode

#

the feature to get your history inside a shell

#

by clicking the up arrow key

oak nebula
#

Yes. You press your little up arrow, the terminal (which takes in input from the keyboard) then passes an escape code down to your process (the shell) via stdin.

oak nebula
#

I don't see what the problem is.

#

Yes.

ivory sentinel
#

nope

#

make that feature and dm me then I admit defeat

oak nebula
#

You use the word shell and terminal interchangeably, why are you contradicting?

ivory sentinel
#

make it

#

and dm me

#

I would be available

ivory sentinel
#

you probably have misunderstood

native burrowBOT
#

@ivory sentinel

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

oak nebula
#

Now, can I explain why your code is utter trash and looks like it was written by an idiot?

#

Because it is absolutely horrid in so many ways.

ivory sentinel
#

!f

native burrowBOT
#

You would get the up error doing something like this:


#include <stdio.h>
#include <termios.h>
#include <unistd.h>

void enable_raw_mode(struct termios* orig_termios) {
  tcgetattr(STDIN_FILENO, orig_termios);
  struct termios raw = *orig_termios;
  raw.c_lflag &= ~(ECHO | ICANON);
  tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}

void disable_raw_mode(struct termios* orig_termios) {
  tcsetattr(STDIN_FILENO, TCSAFLUSH, orig_termios);
}

int main() {
  struct termios orig_termios;
  enable_raw_mode(&orig_termios);

  puts("Press keys (press 'q' to quit)...");

  while (true) {
    char c;
    if (read(STDIN_FILENO, &c, 1) == 1) {
      if (c == '\x1b') {
        char seq[2];
        if (read(STDIN_FILENO, &seq[0], 1) == 1 &&
            read(STDIN_FILENO, &seq[1], 1) == 1) {
          if (seq[0] == '[' && seq[1] == 'A') {
            printf("Up arrow detected!\n");
            // Fetch previous command from history
          }
        }
      } else if (c == 'q') {
        break;
      } else {
        printf("You pressed: %c\n", c);
      }
    }
  }

  disable_raw_mode(&orig_termios);

  return 0;
}

Rooftop Joe
oak nebula
#

Actually you're too entitled and arrogant to deserve real help.

ivory sentinel
ivory sentinel
#

I was having a bad day today

#

you made it worse

#

thank you

oak nebula
#

Of course I can but I cannot communicate with you if all you do is argue about how you know everything when you're here asking trivial things but are unwilling to listen and instead piss on those who know better? If you knew better you wouldn't be asking for help.

#

You may think you know better but if you're not even willing to listen then what is my role here?

ivory sentinel
#

why are you not taking up the challenge

#

make it

#

once

oak nebula
#

Well if something doesn't make sense we can discuss it until it does make sense.

ivory sentinel
#

can't you

oak nebula
#

I did make what you asked, I'm not implementing a whole shell for you.

ivory sentinel
#

give me your repo

oak nebula
#

My repo of what?

ivory sentinel
#

git repo of the shell

#

If I am wrong I will apologise

oak nebula
#

My git repo of the shell you're working on? What are you on about?

ivory sentinel
#

the git repository of that

oak nebula
#

I didn't say anything about anything I made.

ivory sentinel
#

then

#

I'm sorry but excuse me

oak nebula
#

Yes, the code above does what you asked, which was how to detect the damn key in raw mode. As for the shell itself...

ivory sentinel
#

I have made a whole text editor

#

if you know

oak nebula
#

Am I supposed to clap?

ivory sentinel
#

I just said I know how to operate in raw

#

mode

oak nebula
#

Why are you telling me about a text editor?

ivory sentinel
oak nebula
#

You should probably use X/Curses as a standard API instead of duplicating standard functionality by getting escape code from the terminal. It handles windows and all sorts of things.

#

For the text editor, that is.

ivory sentinel
#

used termios

#

and it is better to use termios as xcurses doesn't give that control by the mebers of this server

oak nebula
#

Termios is POSIX, I was talking about X/Curses, which is what somethim like Vim, maybe nano, Emacs, etc. would use.

#

Control?

#

Idk what you are talking about.

ivory sentinel
#

BTW

#

can you just

#

make it

#

that feature to detect a up arrow

#

without endering the raw mode

#

make it

#

I am not telling you to make an entire shell

#

make it and then tag me dm me whatever

oak nebula
#

Backspace for what? Deleting characters?

ivory sentinel
#

without endering the raw mode

#

and pressing enter

oak nebula
#

You can't detect the up arrow accept after finishing input if you don't enter raw mode.

#

This has nothing to do with my complaint that ReadInput() wasn't doing what it claimed to do.

ivory sentinel
#

then

oak nebula
#

That is the underlying mechanism. Since you asked me if I knew how it worked.

ivory sentinel
#

you have to enter the raw mode to do that

#

otherwise prove to me

#

CAN YOU PROVE IT

#

yes or no

oak nebula
#

So why the hell are you on about raw mode?

#

I have no idea why you even mentioned it.

ivory sentinel
oak nebula
#

It's completely irrelevant.

#

It does.

ivory sentinel
#

that's the shit I said for this long time

#

you cannot make that feature without entering the raw mode

#

CAn you make it

oak nebula
#

But you said it with no context and later started demaning code that doesn't use raw mode for reasons that are beyond me.

ivory sentinel
#

I need a yes or no answer

#

nope

oak nebula
#

Why are you trying to do it in canonical mode? You can't.

ivory sentinel
#

I said with the context

#

how do you implement that feature without entering the raw mode

oak nebula
#

Yes. And why did you ask that? What is wrong with raw mode?

#

Raw mode is what you should be using.

ivory sentinel
oak nebula
#

You asking a question that you probably already knew the answer to and which had nothing to do with my complaints. So I don't even know why we're even talking about raw mode.

#

Or half-knew cause idk what you thought the interaction between terminals and shells was or whether they were the same.

ivory sentinel
#

I had asked you properly

#

how do you implement it

#

without entering the raw

#

mode

#

you didn't answer me

#

and assumed something else

#

that's not my problem

#

okay

oak nebula
#

And I said several times that you cannot. I repeatedly said it. I'll even quote all the lines.

ivory sentinel
#

so all of this was a misunderstanding

oak nebula
#

I was talking about something else and you were so great at listening you started shouting at me asking whether I knew about raw mode.

ivory sentinel
ivory sentinel
#

that's not my mistake

#

and btw

#

I was already having a bad day

#

now with this it's just

#

huh

#

whatever

#

I'm having a f*cking headache

#

I would look into this code when I am fresh tomorrow

#

bye

native burrowBOT
#

@ivory sentinel Has your question been resolved? If so, type !solved :)

fervent fable
#

@ivory sentinel seems it got heated pretty quick for no apparent reason as far as i can tell. i'm unable to understand what the problem you're having (it seem you deleted some posts, which isn't a good thing to do if you expecting help). in any case - since there's a snippet up there, based on that one could do

/* --- snip --- */

int enable_raw_mode(struct termios *orig_termios) {
  /* --- snip --- */
}

int disable_raw_mode(struct termios *orig_termios) {
  /* --- snip --- */
}

void check_arrows(char *buf, size_t size) {
  if (size != 3) { return; }

  uint32_t val = 0;
  for (size_t i = 0; i < size; i++) { val |= buf[i] << (size - i - 1) * 8; }

  // 1B 5B the first 2 chars in an arrow sequence
  if (val >> 8 != 0x1b5b) { return; }

  switch (val & 0xff) {
    case 'A':
      ...
      break;
    case 'B':
      ...
      break;
    case 'C':
      ...
      break;
    case 'D':
      ...
      break;
    default:
      break;
  }
}

void print_buf(char *buf, size_t size) {
  printf(">>> ");
  for (size_t i = 0; i < size; i++) { printf("%c ", buf[i]); }
  printf("\n");
}

int main(void) {
  struct termios orig_termios;
  if (enable_raw_mode(&orig_termios) == -1) {
    perror("raw");
    return 1;
  }

  puts("Press keys (press 'q' to quit)...");

  while (true) {
    enum {
      SIZE = 128,
    };

    char buf[SIZE];

    ssize_t ret = read(STDIN_FILENO, buf, sizeof buf);
    if (ret == -1) {
      perror("read");
      goto cleanup;
    }

    if (*buf == 'q') { break; }

    check_arrows(buf, (size_t)ret);
    print_buf(buf, (size_t)ret);
  }

cleanup:
  (void)disable_raw_mode(&orig_termios);
}```
reading anything other than arrows is then becoming trivial. one can simply append the read char/chars into an array up until some specific char (a `'\n'` for example)
sudden bronze
#

Lol

ivory sentinel
#

I was having a bad day and it was getting late already so when the argument kinda stopped I just deleted the entire complaint (because I had poted it on cpp-help before accidently and I was having my problem solved there) to not have any more arguments lol

ivory sentinel
#

!solved