+/*
+ * Set the hardware tabs on the terminal, using the ct (clear all tabs),
+ * st (set one tab) and ch (horizontal cursor addressing) capabilities.
+ * This is done before if and is, so they can patch in case we blow this.
+ */
+settabs()
+{
+ char caps[100];
+ char *capsp = caps;
+ char *clear_tabs, *set_tab, *set_column, *set_pos;
+ char *tg_out, *tgoto();
+ int c;
+
+ clear_tabs = tgetstr("ct", &capsp);
+ set_tab = tgetstr("st", &capsp);
+ set_column = tgetstr("ch", &capsp);
+ if (set_column == 0)
+ set_pos = tgetstr("cm", &capsp);
+
+ if (clear_tabs && set_tab) {
+ prc('\r'); /* force to be at left margin */
+ tputs(clear_tabs, 0, prc);
+ }
+ if (set_tab) {
+ for (c=8; c<columns; c += 8) {
+ /* get to that column. */
+ tg_out = "OOPS"; /* also returned by tgoto */
+ if (set_column)
+ tg_out = tgoto(set_column, 0, c);
+ if (*tg_out == 'O' && set_pos)
+ tg_out = tgoto(set_pos, c, lines-1);
+ if (*tg_out != 'O')
+ tputs(tg_out, 1, prc);
+ else {
+ prc(' '); prc(' '); prc(' '); prc(' ');
+ prc(' '); prc(' '); prc(' '); prc(' ');
+ }
+ /* set the tab */
+ tputs(set_tab, 0, prc);
+ }
+ prc('\r');
+ return 1;
+ }
+ return 0;
+}
+
+setmode(flag)
+int flag;
+/* flag serves several purposes:
+ * if called as the result of a signal, flag will be > 0.
+ * if called from terminal init, flag == -1 means reset "oldmode".
+ * called with flag == 0 at end of normal mode processing.
+ */
+{
+# ifndef USG
+ struct sgttyb *ttymode;
+# else
+ struct termio *ttymode;
+# endif
+# ifdef TIOCGETC
+ struct tchars *ttytchars;
+# endif
+
+ if (flag < 0) { /* unconditionally reset oldmode (called from init) */
+ ttymode = &oldmode;
+# ifdef TIOCGETC
+ ttytchars = &oldtchar;
+# endif
+ } else if (!bequal((char *)&mode, (char *)&oldmode, sizeof mode)) {
+ ttymode = &mode;
+# ifdef TIOCGETC
+ ttytchars = &tchar;
+# endif
+ } else { /* don't need it */
+# ifndef USG
+ ttymode = (struct sgttyb *)0;
+# else
+ ttymode = (struct termio *)0;
+# endif
+# ifdef TIOCGETC
+ ttytchars = (struct tchars *)0;
+# endif
+ }
+
+ if (ttymode)
+ {
+# ifdef USG
+ (void) ioctl(FILEDES, TCSETAW, (char *)ttymode);
+# else
+# ifndef V6
+ /* don't flush */
+ (void) ioctl(FILEDES, TIOCSETN, (char *)ttymode);
+# else
+ stty(FILEDES, ttymode);
+# endif
+# endif
+ }
+# ifdef TIOCGETC
+ if (ttytchars) {
+ (void) ioctl(FILEDES, TIOCSETC, (char *)ttytchars);
+ }
+# endif
+# ifdef CBVIRTTERM
+ if (VirTermNo != -2) {
+ int r1, r2;
+ extern int errno;
+
+ r1 = ioctl(FILEDES, LDGETT, (char *)&block);
+ block.st_flgs |= TM_SET;
+ block.st_termt = VirTermNo;
+ if (block.st_termt < 0)
+ block.st_termt = 0;
+ if (!HasAM)
+ block.st_flgs |= TM_ANL;
+ else
+ block.st_flgs &= ~TM_ANL;
+ r2 = ioctl(FILEDES, LDSETT, (char *)&block);
+ }
+# endif
+
+ if (flag > 0) /* trapped signal */
+ exit(1);
+}
+