handling the setting of the TERM variable.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hey list:

Here is a patch that handles the setting of the TERM variable with a term=value argument in the cmdline.  As always comments are appreciated.

--
Joel Andres Granados
Red Hat / Brno, Czech Republic
diff --git a/loader2/init.c b/loader2/init.c
index e16a8bd..a68ad94 100644
--- a/loader2/init.c
+++ b/loader2/init.c
@@ -133,6 +133,7 @@ void unmountFilesystems(void);
 void disableSwap(void);
 void shutDown(int noKill, int doReboot, int doPowerOff);
 static int getNoKill(void);
+static char *getTermDef(void);
 struct termios ts;
 
 static int mystrstr(char *str1, char *str2) {
@@ -341,8 +342,7 @@ static void doklog(char * fn) {
 
 static int setupTerminal(int fd) {
     struct winsize winsize;
-    int fdn, len;
-    char buf[65535];
+    char *termdef;
 
     if (ioctl(fd, TIOCGWINSZ, &winsize)) {
         printf("failed to get winsize");
@@ -357,17 +357,32 @@ static int setupTerminal(int fd) {
         fatal_error(1);
     }
 
-    /* use the no-advanced-video vt100 definition */
-    env[ENV_TERM] = "TERM=vt100-nav";
-
-    /* unless the user specifies that they want utf8 */
-    if ((fdn = open("/proc/cmdline", O_RDONLY, 0)) != -1) {
-        len = read(fdn, buf, sizeof(buf) - 1);
-        close(fdn);
-        if (len > 0 && mystrstr(buf, "utf8"))
-            env[ENV_TERM] = "TERM=vt100";
+    /*
+     * We will set the TERM varirable.  If the argument is not set we
+     * fall back to vt100-nav.  If both term and utf8 are set we will
+     * go with the term argument.
+     */
+    termdef = getTermDef();
+
+    /*
+     * We must prevent the corruption of this argument.  If the user
+     * sets this arg to something that is not sane, we should provide
+     * the default value.  Additionally, to add more vts, simply add
+     * to the list.
+     */
+    int i, inAllowedVts = 0;
+    char * allowedVts[] = { "TERM=vt100", "TERM=vt220", "TERM=vt520" , NULL };
+    for (i=0 ; allowedVts[i] != NULL ; i++ ){
+        if ( strcmp(allowedVts[i], termdef) == 0 ){
+            inAllowedVts = 1;
+            break;
+        }
     }
 
+    if (termdef == '\0' || !inAllowedVts )
+        env[ENV_TERM] = "TERM=vt100-nav";
+    else
+        env[ENV_TERM] = termdef;
     return 0;
 }
 
@@ -455,6 +470,36 @@ static int getNoKill(void) {
     return 0;
 }
 
+static char *getTermDef(void) {
+    int fd, len;
+    char buf[1024];
+    char *retVal;
+
+    /* Read Command line */
+    if ((fd = open("/proc/cmdline", O_RDONLY,0)) > 0) {
+        len = read(fd, buf, sizeof(buf) - 1);
+        close(fd);
+    }
+
+    /*
+     * Apparently the carriage return at the end of the variable might
+     * be an issue.  So we set the delimitors in strtok accordingly.
+     */
+    for ( retVal = strtok(buf, " ") ; retVal != '\0' ; retVal = strtok(NULL, " \n")) {
+        if( strncmp( retVal, "term=", 5) == 0 ){
+            /*
+             * We need the whole argument "term=value".  The only thing left
+             * to do is put the term part in uppercase.
+             */
+            int i;
+            for ( i=0 ; i<4 ; i++ )
+                retVal[i] = toupper(retVal[i]);
+            break;
+        }
+    }
+    return retVal;
+}
+
 static int getInitPid(void) {
     int fd = 0, pid = -1, ret;
     char * buf = calloc(1, 10);
_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/anaconda-devel-list

[Index of Archives]     [Kickstart]     [Fedora Users]     [Fedora Legacy List]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]
  Powered by Linux