View Full Version : Retrieve cursor pos

10-31-2003, 03:42 PM
Our OS is Unixware 7 and we work in a high-level application generating development environment. There is a problem with it that I am trying to work around. I am considering calling a program with a system call that will simply return the line number that the cursor is on. I have found the curses.h way to determine the line, but it apparently won't work as necessary because it creates a new window with initscr(); so it will always return 0.

Basically what I need to know if there is any way to retrieve the current line number of the cursor as it exists before running the program (and without modifying the screen contents).

If you can assist in any way I'd appreciate it. Thanks.

10-31-2003, 04:09 PM
So what is the program asking "where is the cursor" written in?

Why isn't it using curses to start with

Why can't it use curses now?

11-01-2003, 12:02 AM
Perhaps I didn't do a good enough job explaining the problem. The development application/environment is called ProIV (and I'm quite certain no one here has ever heard of it) and that is also the language it is written in. In this environment resides a screen application that displays multiple records on a single screen using one line per record. The way the application populates the screen is it reads each record from disk then displays each and allows the user to arrow the cursor up and down between records on screen without any more file accesses (until the user arrows out of the scope of the screen and another page needs to be displayed). I need to know upon exit of the screen which record the cursor the user has the cursor sitting on but it can't be done programatically from within the screen.

The only workaround I could fathom is, upon exit of the screen, to run outside of the ProIV environment (with a system call) the program that I am hoping you can help me figure out how to write which will return to the screen application the line number of the cursor so I will then be able to determine which record the user has selected.

After re-reading your post I realize I seriously overexplained my answer. To re-explain, the program asking where the cursor is is written in ProIV and is a very high-level language and is basically a application generator so it is nothing near a lower level language (such as C++) where I could do such things as locating the cursor with the OS. I can only do what is provided for by ProIV.

11-01-2003, 01:52 AM
My first thought was this, use the ANSI terminal cursor position read function, but it doesn't work as expected (not for me at least). I guess you need a real terminal to be able to intercept the information returned....


# from http://sunsite.nus.edu.sg/pub/os2/os2faq/os2faq.2.0062.html
# Escape Code Sequence Function
# Cursor Controls
# ESC[#;#H or ESC[#;#f Moves cursor to line #, column #
# ESC[#A Moves cursor up # lines
# ESC[#B Moves cursor down # lines
# ESC[#C Moves cursor forward # spaces
# ESC[#D Moves cursor back # spaces
# ESC[#;#R Reports current cursor line and column
# ESC[s Saves cursor position for recall later
# ESC[u Return to saved cursor position
# Erase Functions
# ESC[2J Clear screen and home cursor
# ESC[K Clear to end of line

# clear screen
echo -ne "\\e[2J"

# move cursor
echo -ne "\\e[10;5H"

# print something
echo -ne "Hello"

# read cursor position - by sending the Device status report
# see http://vt100.net/docs/vt100-ug/chapter3.html#DSR
echo -ne "\\e[6n"
# this 'works', but the result gets printed to the screen!!! (not good)

Note: this is the bash echo command. The way you generate escape sequences using other echo commands is specific, so you'd need to read up on them if you wanted to try stuff.

Enough of that, this is plan B

myproIVprog | tee spy
Try running your proIV program as shown above. If it works, you should find a copy of everything sent to the screen inside the file as well. With a bit of effort, you should be able to spot cursor movements, track them and hence figure out the final cursor position.
Yes, it's a HACK!!!

11-01-2003, 05:53 PM
I'm definitely going to give that a try Monday morning (the first option I mean ;P). Thanks a lot Salem.