POSIX way of getting columns in tty?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting POSIX way of getting columns in tty?
# 8  
Old 01-11-2009
Nobody is using reals teletypes nowadays, and few people still use true terminals. In the latter case, there are usually ways to get the actual width and it is safe to default to 80 characters should it fails.

I'm quite sure all the tty drivers available in modern Unix variants handle the non POSIX tty wxh geometry (termios TIOCGWINSZ) and the event triggered when it changes (SIGWINCH) when using a terminal emulator on a windowing system.

One of the issues is stty output isn't specified and for example Solaris reports "columns = xx" while Linux reports "columns xx".

You should check a sample stty output with the other Unix variants you plan to support and modify my script should other discrepancies appear then you'll have a reasonably "stable" solution.
# 9  
Old 01-11-2009
Quote:
Originally Posted by jlliagre
I'm quite sure all the tty drivers available in modern Unix variants handle the non POSIX tty wxh geometry (termios TIOCGWINSZ) and the event triggered when it changes (SIGWINCH) when using a terminal emulator on a windowing system.
I hope you will agree that Solaris 10 counts as a modern Unix variant. I wanted to use the serial system console on ttya. I connected a serial cable to second Solaris 10 system. On that 2nd system, I opened a terminal window and in that window in used tip to connect. That all works fine. But when I vary the window size on the 2nd system, the first system does not seem to be informed. And no fair telling me that I manually type "stty cols 100" (or whatever) after I change my window size.

The Solaris driver used to control ttya (and ttyb) is indeed a real terminal. The other kind of terminal is a pseudo terminal. In the case of a real terminal, where a real tty driver is truely controlling a real serial port, this isn't going to work. It's true that the old printing teletypes are almost completely gone. Ditto those "ascii terminals" too. But the serial port is still alive and well. We either use another unix system as above or a laptop running putty on XP or something like that to connect to a serial port.

This isn't to say that I dislike your solution. I think your solution is as good as it gets. I am arguing that the search for an even more generic solution is futile and that there no help to be had from Posix.

And also note that jlliagre replaced awk with a grep/sed combo. Solutions involving awk are very dangerous on Solaris. Sun absolutely refuses to retire the antique version of awk so you must either use nawk or fiddle with the path on Solaris.
# 10  
Old 01-11-2009
Thanks Perderabo, fpmurphy, and notably jlliagre.

So ultimately the answer: there is none. That's fine; then the question turns, "what's the best portable way to get the number of columns for the current tty". The answer seems to be: check the COLUMNS variable, and if not available, check the "non POSIX tty wxh geometry (termios TIOCGWINSZ)" and then fall-back to 80. Does that sound right?

Concerning "non POSIX tty wxh geometry (termios TIOCGWINSZ)", how can I do this from the shell?
# 11  
Old 01-11-2009
I think that jlliagre has the best solution available and that is what I would use. But it only tells you the window size when you check it. A program like vi can arrange to be notified of a change in window size and then redraw the screen. That is too much to try via a shell script.
# 12  
Old 01-12-2009
Quote:
Originally Posted by Perderabo
I hope you will agree that Solaris 10 counts as a modern Unix variant.
Sure, I would even say the most modern :-)
Quote:
I wanted to use the serial system console on ttya.
That case is indeed ruling out window size handling.
Quote:
And no fair telling me that I manually type "stty cols 100" (or whatever) after I change my window size.
Assuming your terminal use a xterm compatible emulator (dtterm, gnome-terminal, rxvt and the likes are ok) I think you could write a script that set these tty settings to their correct values. Of course that wouldn't be very useful.
Quote:
The Solaris driver used to control ttya (and ttyb) is indeed a real terminal.
You are correct although I doubt the OP was concerned about that case.
Quote:
But the serial port is still alive and well.
Its use is slowly fading though with modern LOM based on ethernet vs serial lines.
Quote:
I am arguing that the search for an even more generic solution is futile and that there no help to be had from Posix.
Agreed. POSIX is currently of no help here.
Quote:
And also note that jlliagre replaced awk with a grep/sed combo. Solutions involving awk are very dangerous on Solaris.
What specific risks involved with awk are you thinking of ?
Quote:
Sun absolutely refuses to retire the antique version of awk so you must either use nawk or fiddle with the path on Solaris.
I would say that differently: Sun is extremely reluctant to put its existing customers/users at risk by breaking compatibility from older Solaris releases to newer ones with providing a new and incompatible version of a commited standard command. That is the same reasoning that makes /bin/sh such a featureless and disappointing shell.
Of course, everyone is free to use nawk and ksh(93) like I do.
Quote:
A program like vi can arrange to be notified of a change in window size and then redraw the screen. That is too much to try via a shell script.
That is not that difficult to write although not particularly reliable if the shell script is interactive. eg:
Code:
$ cat winch.ksh
#!/bin/ksh

function winch
{
    eval $(/usr/openwin/bin/resize)
    echo new width=$COLUMNS
    echo new height=$LINES
}

trap winch WINCH
echo "Resize the window to exercise the handler:"
for i in 1 2 3 4 5 6 7 8 9
do
    echo $i
    sleep 2
done

# 13  
Old 01-12-2009
I'm going to keep it simple: I'll use the COLUMNS variable. Is there any environment or shell where this will not work? Should I call resize beforehand (just in case)?
# 14  
Old 01-12-2009
There will be environments where:
- the resize command is missing or not installed
- the terminal (emulator) used isn't handling the escape sequence used to query its size

The resize command is expecting a Bourne compatible shell too.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Python: Redirecting to tty and reading from tty

In bash, you can do something like this: #!/bin/bash echo -n "What is your name? " > /dev/tty read thename < /dev/tty How can I do the same in python? I have a python script that has the following content: #!/usr/bin/python2.7 import getpass import sys import telnetlib import... (2 Replies)
Discussion started by: SkySmart
2 Replies

2. Ubuntu

How to get columns TIME and TTY of commands ps -A?

Hi, Commands ps -A include four parameters are PID, TTY, TIME and CMD. I can not found pathnames of TTY and TIME which I can read from file in C language to get information display on screen. Thank you! Ex: PID TTY TIME CMD 1 ? 00:00:01 init (2 Replies)
Discussion started by: newbie_member
2 Replies

3. UNIX for Advanced & Expert Users

tty changes?

I am not sure if I am using the correct terminology but somehow my tty keeps changing on me. The man pages are confusing to me on what exactly the tty is. This is what I see when I run the tty command. Could anyone explain why my tty keeps changing? ~ $ tty /dev/pts/1 ~ $ tty /dev/pts/0 (6 Replies)
Discussion started by: cokedude
6 Replies

4. Solaris

tty hack

hi all, what i want to do when user open terminal like tty5 and do his work i want to see his terminal how can i do this (1 Reply)
Discussion started by: xxmasrawy
1 Replies

5. Programming

Posix

HI, When i am configuring php in SUN Solaris. I am getting the below error. configure: error: Your system seems to lack POSIX threads. Do i need to install POSIX? If so can somebody let me know where can i download POSIX for Solaris 8? Thanks, (2 Replies)
Discussion started by: Krrishv
2 Replies

6. AIX

Tty Over Run

dears i have two IBM p630 application servers, they are running hacmp 5.2 as the clustering software. from the output of errpt on one of the nodes i am receiving the following error: 9D30B78E 0530020007 T S tty1 RECEIVER OVER-RUN ON INPUT please are there any ideas about how... (0 Replies)
Discussion started by: TheEngineer
0 Replies

7. AIX

tty

Hi All can anyone tell me what is the meaning of tty,or give me an example of this? (1 Reply)
Discussion started by: magasem
1 Replies

8. UNIX for Dummies Questions & Answers

tty problem

Dear all, when I do a remote shell command from a FUJITSU Unix to an HP-UX, I always get this: 15:36:35 + rsh -l storto obs2 sh /users/obs/storto/MESO-NH/tmp_olive_map_00CC/job_prepex_70JY_C00CC_20041103_19 Not a terminal stty: : Erreur inconnue stty: : Erreur inconnue... (0 Replies)
Discussion started by: stortignauz
0 Replies

9. Shell Programming and Scripting

kill the tty

Hello Experts i'm having some trouble with a script. the purpose is to kill all processes from a distinct tty in a HP-UX machine, given the User TTY. it's to use like this: killtty pts/tnb pts/tr pts/tD here it is #!/usr/bin/ksh i=1 for i do || exit kill -9 `ps -ft "${i}" |... (9 Replies)
Discussion started by: vascobrito
9 Replies
Login or Register to Ask a Question