I've got to stop editing things like that once I've posted them...
Anyway... it occurs to me that localizing the cursor access is more than just a stylistic preference: there's a possible race condition here, too, unless you can ensure that the function has exclusive access to the cursor. I'm surprised I didn't notice that before, epecially after reading Candy's earlier posting. That probably means that not only would you
need to limit access, using the [tt]inline[/tt] type modifier probably wouldn't be so wise after all.
In any case, the parts for [tt]cury[/tt] are probably inadequate, since they don't handle the case where the cursor is at the top or bottom lines already. I could be wrong, of course. Now, I'm not entirely certain how you are handling the cursor, but from the look of things, you're treating the screen as a two-dimensional array, with [tt]curx[/tt] holding the horizontal position and [tt]cury[/tt] holding the vertical. If so, then then top and bottom lines present (literal) edge cases. You would need to keep existing value of [tt]cury[/tt], but scroll the existing buffer up or down one line as appropriate (and in the case if the top line, you would need to have retained the previous screen entries in a separate buffer, or else not allow scrolling up at all).
This brings up a possible problem that hasn't been mentioned yet, probably because you seem to have only tested single lines so far:
Code: Select all
case '\n':
//enter/return
*buf=0;
return buf;
break;
Nothing is being done to update the cursor, here. Since it doesn't look like the [tt]getkey()[/tt] function or the rest of the code you posted does anything for that; the only references to [tt]cwin[/tt] in the second post are with what seem to be code for switching virtual terminals (and this is probably far too low-level a point for that!). The result is that the cursor won't get updated after a newline at all. For that matter, it doesn't seem that the cursor gets advanced at all, just reversed in the case of ... unless this is being done automatically elsewhere, this is certain to cause problems.
It's been a while since I've looked into any of these issues, so I may be completely off. YMMV.
Finally, I would recommend putting the character-decoding code into a separate single-character function such as [tt]decode_ch()[/tt] or something similar. This would allow you to debug the character-decoding code separate from the actual read loop, and will probably be needed later on anyway. Indeed, you will probably need a separate [tt]kgetchar()[/tt] at some point, and for that matter, you may at some point need to allow for multiple character decodings.