tags:

views:

34

answers:

3

Hello,

I am making a C application in Unix that uses raw tty input.

I am calling write() to characters on the display, but I want to manipulate the cursor:

ssize_t
write(int d, const void *buf, size_t nbytes);

I've noticed that if buf has the value 8 (I mean char tmp = 8, then passing &tmp), it will move the cursor/pointer backward on the screen.

I was wondering where I could find all the codes, for example, I wish to move the cursor forward but I cannot seem to find it via Google.

Is there a page that lists all the code for the write() function please?

Thank you very much,

Jary

+1  A: 

If you're using write just so you have low-level cursor control, I think you are using the wrong tool for the job. There are command codes for many types of terminal. VT100 codes, for example, are sequences of the form "\x1b[...", but rather than sending raw codes, you'd be much better off using a library like ncurses.

Jack Kelly
+2  A: 

8 is just the ascii code for backspace. You can type man ascii and look at all the values (the man page on my Ubuntu box has friendlier names for the values). If you want to do more complicated things you may want to look at a library like ncurses.

vanza
Thanks a lot! That helped.
Jary
+2  A: 

You have just discovered that character code 8 is backspace (control-H).

You would probably be best off using the curses library to manage the screen. However, you can find out what control sequences curses knows about by using infocmp to decompile the terminfo entry for your terminal. The format isn't particularly easy to understand, but it is relatively comprehensive. The alternative is to find a manual for the terminal, which tends to be rather hard.

For instance, I'm using a color Xterm window; infocmp says:

#   Reconstructed via infocmp from file: /usr/share/terminfo/78/xterm-color
xterm-color|nxterm|generic color xterm,
    am, km, mir, msgr, xenl,
    colors#8, cols#80, it#8, lines#24, ncv@, pairs#64,
    acsc=``aaffggiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~,
    bel=^G, bold=\E[1m, clear=\E[H\E[2J, cr=^M,
    csr=\E[%i%p1%d;%p2%dr, cub=\E[%p1%dD, cub1=^H,
    cud=\E[%p1%dB, cud1=^J, cuf=\E[%p1%dC, cuf1=\E[C,
    cup=\E[%i%p1%d;%p2%dH, cuu=\E[%p1%dA, cuu1=\E[A,
    dch=\E[%p1%dP, dch1=\E[P, dl=\E[%p1%dM, dl1=\E[M, ed=\E[J,
    el=\E[K, enacs=\E)0, home=\E[H, ht=^I, hts=\EH, il=\E[%p1%dL,
    il1=\E[L, ind=^J,
    is2=\E[m\E[?7h\E[4l\E>\E7\E[r\E[?1;3;4;6l\E8, kbs=^H,
    kcub1=\EOD, kcud1=\EOB, kcuf1=\EOC, kcuu1=\EOA,
    kdch1=\E[3~, kf1=\E[11~, kf10=\E[21~, kf11=\E[23~,
    kf12=\E[24~, kf13=\E[25~, kf14=\E[26~, kf15=\E[28~,
    kf16=\E[29~, kf17=\E[31~, kf18=\E[32~, kf19=\E[33~,
    kf2=\E[12~, kf20=\E[34~, kf3=\E[13~, kf4=\E[14~,
    kf5=\E[15~, kf6=\E[17~, kf7=\E[18~, kf8=\E[19~, kf9=\E[20~,
    kfnd=\E[1~, kich1=\E[2~, kmous=\E[M, knp=\E[6~, kpp=\E[5~,
    kslt=\E[4~, meml=\El, memu=\Em, op=\E[m, rc=\E8, rev=\E[7m,
    ri=\EM, rmacs=^O, rmcup=\E[2J\E[?47l\E8, rmir=\E[4l,
    rmkx=\E[?1l\E>, rmso=\E[m, rmul=\E[m,
    rs2=\E[m\E[?7h\E[4l\E>\E7\E[r\E[?1;3;4;6l\E8, sc=\E7,
    setab=\E[4%p1%dm, setaf=\E[3%p1%dm, sgr0=\E[m, smacs=^N,
    smcup=\E7\E[?47h, smir=\E[4h, smkx=\E[?1h\E=, smso=\E[7m,
    smul=\E[4m, tbc=\E[3g, u6=\E[%i%d;%dR, u7=\E[6n,
    u8=\E[?1;2c, u9=\E[c,

That contains information about box drawing characters, code sequences generated by function keys, various cursor movement sequences, and so on.

You can find out more about X/Open Curses (v4.2) in HTML. However, that is officially obsolete, superseded by X/Open Curses v7, which you can download for free in PDF.

Jonathan Leffler
Thank you very much. That is a very useful solution, I will try that. Thanks!
Jary
By way of further explanation, in the infocmp output above there's a list of key=value assocations, such as home=\E[H. This particular one means you can move the cursor "home" - back to the top left of the screen - by printing first an ASCII 27 character (called ESCAPE or ESC - you'll see it in `man ascii`), then a left square bracket (ASCII 91 decimal), then the letter H (ASCII 72). Basically, for each type of terminal software/device capable of moving the cursor home, infocmp will report the characters to use, so software can look up "home" in the list and work on all the terminals.
Tony
It's good to play with those sequences just a bit... for example, on many terminals ESCAPE[31m through ESCAPE[37m will change the foreground colour of the text you print afterwards, then ESCAPE[30m will reset it to the default. This is useful for setting the shell prompt to something that visually stands out from your typing and command output - a situation where the full ncurses library wouldn't be much use (you use it when writing your own applications). You can google "shell prompts" for more examples of that kind of thing.
Tony
Thanks a lot! I was able to get it to work, thanks!
Jary