Document #: US-13268,EN ------------------------------------------------------------------------------ Major subject: analysis Minor subjects: tech_notes Keywords: Abstract: Switching Forms On and Off in UNIX Installations - Equivalent to INGRES Technical Note #27 or Release 5 note #67. Expert note: Switching Forms On and Off in UNIX Installations ================================================ Overview -------- The INGRES forms system in UNIX installations changes some terminal characteristics. Specifically, input character echoing is turned off, carriage returns are no longer mapped to new lines and character data is handled character by character rather than in lines. The terminal characteristics are always returned to the original mode on exit from the forms system, but there are cases where a developer may wish to turn the forms system terminal characteristics on and off without exiting the forms system. For example, a menu system written directly in C rather than EQUEL or OSL could be used with forms procedures as the menu choices. Another example would be starting a shell from within a forms application. This document describes a way to do this and includes source for a program which does the switching. o iiformtty.qc and iiformtty5.qc Procedures o Program Listing - iiformtty.qc for 4.2 BSD Systems iiformtty.qc and iiformtty5.qc Procedures ----------------------------------------- This procedure takes a toggle argument that will turn the forms system terminal characteristics on and off. It assumes that you turn the forms system on first with the ## forms statement and that you call iiformtty the first time with the OFF toggle. The iiformtty.qc procedure is used in 4.2 BSD systems and iiformtty5.qc is used in System V systems. Use this procedure any time that you need to switch back and forth between normal tty handling and the forms system. For a simple example, starting a shell from within a forms application with regular terminal handling can make good use of iiformtty. Without iiformtty, the developer is forced to start and quit the forms system within each forms program in order to reset the terminal characteristics. Not only is this a performance hit but INGRES only supports calling ## forms once in a program. Calling it more often can have unpredictable and unpleasant results in your applications. For the simple example noted above, here is a sample program. ## extern int blankform; #define OFF 0 #define ON 1 main() /* test program for form system switching */ ## { void iiformtty(); ## ingres supp30 ## forms ## addform blankform ## display #blankform ## initialize ## activate menuitem "shell" ## { iiformtty(OFF); system("/bin/sh"); iiformtty(ON); ## message "This is forms on again." ## sleep 2 ## redisplay ## } ## activate menuitem "quit" ## { ## breakdisplay ## } ## finalize ## endforms ## exit return(0); ## } This program demonstrates how iiformtty would be used. The important thing to notice is that the forms system is started up once and that the first call to iiformtty is with the OFF toggle. Iiformtty is not bullet proof enough to catch a user calling it first with the ON toggle and that would result in some strange terminal settings. The form used here is a blank form. The iiformtty code saves the current status of the tty when the OFF (0) toggle is the argument and adds carriage return mapping to newlines as well as input character echoing while restoring the line by line charac- ter processing. When the ON (1) toggle is the argument, the tty confi- guration needed by the forms system is restored and a ## clear screen is executed so that a ## redisplay will work correctly. Program Listing - iiformtty.qc for 4.2 BSD Systems -------------------------------------------------- /* iiformtty * This program should be used within an EQUEL program to turn the forms * terminal setup on and off. The ingres forms system uses terminals in * cbreak mode. By calling this routine with a 0 argument (off), the * current terminal state is saved and cbreak is turned to line mode along * with enabling carriage return mapping to newline and input character * echoing. By calling this routine with a 1 argument (on), the saved state * is restored. Thus, it doesn't make sense to turn iiformtty on until it's * been turned off once and doing so will set terminal characteristics to * an unknown value. * * ttbuf temp storage for terminal characteristics * ttp pointer to sgtty structure, used because ioctl needs a ptr * savestat persistent storage for forms system characteristics * * return codes * 0 everything worked ok * 1 can't get old terminal settings for some reason * 2 can't set new terminal settings for some reason * 3 some other arg besides 0 or 1 was passed to iiformtty * */ #include#include #define OFF 0 #define ON 1 iiformtty(toggle) int toggle; { struct sgttyb ttbuf, *ttp; static short savestat; ttp = &ttbuf; if (toggle == OFF) { if (ioctl(0,TIOCGETP,ttp) < 0) { ## message "Couldn't get old terminal settings..." ## sleep 2 return (1); } else { savestat = ttp->sg_flags; ttp->sg_flags = ((savestat | CRMOD | ECHO) & ~CBREAK); if (ioctl(0,TIOCSETP,ttp) < 0) { ## message "Couldn't set modes to normal, exiting..." ## sleep 2 return (2); } } } else if (toggle == ON) { if (ioctl(0,TIOCGETP,ttp) < 0) { printf("Couldn't get current settings, exiting...\n"); return (1); } else { ttp->sg_flags = savestat; if (ioctl(0,TIOCSETP,ttp) < 0) { printf("Couldn't reset to forms mode, exiting...\n"); return (2); } ## clear screen } } else return(3); } Program Listing - iiformtty5.qc for System V Systems ---------------------------------------------------- /* iiformtty * This program should be used within an EQUEL program to turn the forms * terminal setup on and off. The ingres forms system uses terminals in * cbreak mode. By calling this routine with a 0 argument (off), the * current terminal state is saved and cbreak is turned to line mode along * with enabling carriage return mapping to newline and input character * echoing. By calling this routine with a 1 argument (on), the saved state * is restored. Thus, it doesn't make sense to turn iiformtty on until it's * been turned off once and doing so will set terminal characteristics to * an unknown value. * * ttbuf temp storage for terminal characteristics * ttp pointer to sgtty structure, used because ioctl needs a ptr * savestat persistent storage for forms system characteristics * * return codes * 0 everything worked ok * 1 can't get old terminal settings for some reason * 2 can't set new terminal settings for some reason * 3 some other arg besides 0 or 1 was passed to iiformtty * */ #include #include #define OFF 0 #define ON 1 iiformtty(toggle) int toggle; { struct termio ttbuf, *ttp; static struct termio savestat; ttp = &ttbuf; if(toggle == OFF) { if (ioctl(0,TCGETA,ttp) < 0) { ## message "iiformtty: can't get old terminal settings." ## sleep 2 return(1); } else { savestat.c_lflag = ttp->c_lflag; savestat.c_iflag = ttp->c_iflag; savestat.c_oflag = ttp->c_oflag; ttp->c_lflag = (ttp->c_lflag | ICANON | ECHO ); ttp->c_iflag = (ttp->c_iflag | ICRNL); ttp->c_oflag = (ttp->c_oflag | ONLCR); if (ioctl(0, TCSETA, ttp) < 0) { ## message "iiformtty: can't set term to normal." ## sleep 2 return(2); } } } else if (toggle == ON) { if (ioctl(0, TCGETA, ttp) < 0) { printf("iiformtty: can't get current terminal settings\n"); return(1); } else { ttp->c_lflag = savestat.c_lflag; ttp->c_iflag = savestat.c_iflag; ttp->c_oflag = savestat.c_oflag; if (ioctl(0, TCSETA, ttp) < 0) { printf("iiformtty: can't reset to forms mode\n"); return(2); } ## clear screen } } else return(3); } Summary ------- This document describes a method to allow a developer to turn the INGRES forms system on or off without exiting the forms system. It can be used wherever there is a need to return to normal terminal charac- teristics after the forms system has been started in a program. Releases affected: all(all.unx) - Releases not affected: Errors: Bugs/SIRS: ------------------------------------------------------------------------------
© William Yuan 2000
Email William