I am writing some small program in C–some log in, log out, create account, send-message-to-another-account deal–that I can build on and change and innovate and polish up to refine my skills in C.
What I am trying to figure out is how to implement part of the “compose message” option. After you are prompted for the recipient, subject, and body of the message, I want to be able to go back and edit them–without having to re-type the whole thing. So if I chose the “edit message” option it would write out what I had and let me go back and change something.
I’ve done something sort of like this in x86 assembly, or started to do something like this, so I understand basically how this would be implemented at the machine level, but I don’t know how to implement it in C. How do I do this?
Things like the following are baffling me in C:
- A good way to handle potentially lengthly input
- How to write out text and allow the user to edit it, without going back beyond a certain point
- How to control the position of the cursor
Also, if this has been done before, and there exists a library of functions for things like this (even a standard library I don’t know about) please note that I do want to roll my own for the purpose of learning.
I guess the method I have in my head is to read the message body one character at a time, so that I can account for carriage returns and create a multi-line message. But I’m not sure how I would backspace through it. I guess it would be really hard to do this from the command line(?).
If not impossible, to move the cursor back and erase characters which are already out of the input buffer… Would I have to “re-draw the screen” every time? Like can I just take control of the whole console and just read and write keystrokes to certain positions? Or is this too close to the machine? I sort of did it with assembly, but that used 16-bit interrupts, which I’m not allowed to use in C… This is what I wrote in assembly:
(image that used to be here has disappeared)
where the program would convert a byte value to two character codes representing that byte, then jump over to the right column and write the original byte (which showed up as a character), then jump back and write out two more hex numbers, in the next slot… And so on, left to right, top to bottom… It was easy, but I have no idea how I would implement that in C. All I can do is INT 21 style input and output, writing lines to the console which scrolls the window up and so forth.
There is no way in ANSI C to make a portable line-editor. If you roll your own, you will have to reroll it for every new operating system you want your program to work on.
If I may make a suggestion, I would use a pre-existing library to do all of that hard, platform-specific dirty work, and with that leg-up, learn how to handle things like arbitrary-length input and such. Then, when your code works (and is good), learn how to do all that dirty work, and take away the library-crutch. That way, you’re not tackling the whole thing – you’re breaking it down into more manageable parts.
Even this is a bit of an oversimplification. It took me quite a while to learn how to handle arbitrary-length input.
Also, know that, if you want your code to be portable, removing the library dependency will mean that, if you want to port it, you’ll have to either a) rewrite all that dirty-work code, or b) add the library back in.
To end this all on a joke, this is your brain with libraries:
This is your brain without libraries: