termcap.c revision 29501
1/* 2 * Copyright (c) 1994, Paul Richards. 3 * 4 * All rights reserved. 5 * 6 * This software may be used, modified, copied, distributed, and sold, in both 7 * source and binary form provided that the above copyright and these terms 8 * are retained, verbatim, as the first lines of this file. Under no 9 * circumstances is the author responsible for the proper functioning of this 10 * software, nor does the author assume any responsibility for damages 11 * incurred with its use. 12 */ 13 14#include "sysinstall.h" 15#include <stdarg.h> 16#include <fcntl.h> 17#include <sys/errno.h> 18#include <sys/ioctl.h> 19#include <machine/console.h> 20 21#define VTY_STATUS_LINE 24 22#define TTY_STATUS_LINE 23 23 24static void 25prompt_term(char **termp, char **termcapp) 26{ 27 char str[80]; 28 static struct { 29 const char *term, *termcap; 30 } lookup[] = { { "ansi", termcap_ansi }, 31 { "vt100", termcap_vt100 }, 32 { "cons25", termcap_cons25 }, 33 { "cons25-m", termcap_cons25_m } }; 34 35 if (RunningAsInit) { 36 while (1) { 37 int i; 38 39 printf("\nThese are the predefined terminal types available to\n"); 40 printf("sysinstall when running stand-alone. Please choose the\n"); 41 printf("closest match for your particular terminal.\n\n"); 42 printf("1 ...................... Standard ANSI terminal.\n"); 43 printf("2 ...................... VT100 or compatible terminal.\n"); 44 printf("3 ...................... FreeBSD system console (color).\n"); 45 printf("4 ...................... FreeBSD system console (monochrome).\n\n"); 46 printf("Your choice: (1-4) "); 47 fflush(stdout); 48 fgets(str, 80, stdin); 49 i = str[0] - '0'; 50 if (i > 0 && i < 5) { 51 *termp = (char *)lookup[i - 1].term; 52 *termcapp = (char *)lookup[i - 1].termcap; 53 break; 54 } 55 else 56 printf("\007Invalid choice, please try again.\n\n"); 57 } 58 } 59 else { 60 printf("\nPlease set your TERM variable before running this program.\n"); 61 printf("Defaulting to an ANSI compatible terminal - please press RETURN\n"); 62 fgets(str, 80, stdin); /* Just to make it interactive */ 63 *termp = (char *)"ansi"; 64 *termcapp = (char *)termcap_ansi; 65 } 66} 67 68int 69set_termcap(void) 70{ 71 char *term; 72 int stat; 73 struct ttysize ts; 74 75 term = getenv("TERM"); 76 stat = ioctl(STDERR_FILENO, GIO_COLOR, &ColorDisplay); 77 78 if (!RunningAsInit) { 79 if (getenv("SYSINSTALL_DEBUG")) 80 DebugFD = open("sysinstall.debug", O_WRONLY|O_CREAT|O_TRUNC, 0644); 81 else 82 DebugFD = -1; 83 if (DebugFD < 0) 84 DebugFD = open("/dev/null", O_RDWR, 0); 85 } 86 87 if (!OnVTY || (stat < 0)) { 88 if (!term) { 89 char *term, *termcap; 90 91 prompt_term(&term, &termcap); 92 if (setenv("TERM", term, 1) < 0) 93 return -1; 94 if (setenv("TERMCAP", termcap, 1) < 0) 95 return -1; 96 } 97 if (DebugFD < 0) 98 DebugFD = open("/dev/null", O_RDWR, 0); 99 } 100 else { 101 int i, on; 102 103 if (getpid() == 1) { 104 DebugFD = open("/dev/ttyv1", O_WRONLY); 105 if (DebugFD != -1) { 106 on = 1; 107 i = ioctl(DebugFD, TIOCCONS, (char *)&on); 108 msgDebug("ioctl(%d, TIOCCONS, NULL) = %d (%s)\n", 109 DebugFD, i, !i ? "success" : strerror(errno)); 110 } 111 } 112 if (ColorDisplay) { 113 if (!term) { 114 if (setenv("TERM", "cons25", 1) < 0) 115 return -1; 116 if (setenv("TERMCAP", termcap_cons25, 1) < 0) 117 return -1; 118 } 119 } 120 else { 121 if (!term) { 122 if (setenv("TERM", "cons25-m", 1) < 0) 123 return -1; 124 if (setenv("TERMCAP", termcap_cons25_m, 1) < 0) 125 return -1; 126 } 127 } 128 } 129 if (ioctl(0, TIOCGSIZE, &ts) == -1) { 130 msgDebug("Unable to get terminal size - errno %d\n", errno); 131 ts.ts_lines = 0; 132 } 133 StatusLine = ts.ts_lines ? ts.ts_lines - 1: (OnVTY ? VTY_STATUS_LINE : TTY_STATUS_LINE); 134 return 0; 135} 136