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

Re: [Lost] [Patch] Shell: Umgebungsvariablen anzeigen/setzen/löschen



Die Hilfe kann ich entweder in einem separaten Patch machen (da fehlt nämlich noch wesentlich mehr) oder wir warten einfach auf stultus' Hilfesystem. ;-)

Die anderen beiden Sachen sind geändert.
Index: cmds/set.c
===================================================================
--- cmds/set.c	(Revision 0)
+++ cmds/set.c	(Revision 0)
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2008 The LOST Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the LOST Project
+ * by Kevin Wolf.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the LOST Project
+ *     and its contributors.
+ * 4. Neither the name of the LOST Project nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <env.h>
+
+#include "types.h"
+#include "stdlib.h"
+#include "stdio.h"
+#include "dir.h"
+#include "unistd.h"
+#include "config.h"
+
+void set_display_usage();
+void set_list_vars();
+
+int shell_command_set(int argc, char* argv[], const char* args)
+{
+
+    switch (argc) {
+        case 1:
+            set_list_vars();
+            break;
+
+        case 2:
+            unsetenv(argv[1]);
+            break;
+
+        case 3:
+            setenv(argv[1], argv[2], 1);
+            break;
+
+        default:
+            set_display_usage();
+            return EXIT_FAILURE;
+    }
+    
+
+    return EXIT_SUCCESS;
+}
+
+void set_list_vars()
+{
+    int i, cnt;
+
+    cnt = getenv_count();
+    for (i = 0; i < cnt; i++) {
+        printf("%s = %s\n", getenv_name_by_index(i), getenv_index(i));
+    }
+}
+
+void set_display_usage()
+{
+    puts("\nAufruf: set [<Variable> [<Wert>]]\n");
+}
+
Index: shell.c
===================================================================
--- shell.c	(Revision 704)
+++ shell.c	(Arbeitskopie)
@@ -77,7 +77,8 @@
     {"help",        &shell_command_help},
     {"exit",        &shell_command_exit},
     {"start",       &shell_command_start},
-    {"cd",          &shell_command_cd}
+    {"cd",          &shell_command_cd},
+    {"set",         &shell_command_set}
 #ifdef CONFIG_SHELL_BUILTIN_ONLY
    ,{"bincat",      &shell_command_bincat},
     {"cat",         &shell_command_cat},
Index: shell.h
===================================================================
--- shell.h	(Revision 704)
+++ shell.h	(Arbeitskopie)
@@ -44,6 +44,7 @@
 int shell_command_exit(int argc, char* argv[], const char* args);
 int shell_command_start(int argc, char* argv[], const char* args);
 int shell_command_cd(int argc, char* argv[], const char* args);
+int shell_command_set(int argc, char* argv[], const char* args);
 
 #include "config.h"
 #ifdef CONFIG_SHELL_BUILTIN_ONLY
Index: ../../include/env.h
===================================================================
--- ../../include/env.h	(Revision 0)
+++ ../../include/env.h	(Revision 0)
@@ -0,0 +1,52 @@
+/*  
+ * Copyright (c) 2008 LOST Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the LOST Project
+ * by Kevin Wolf.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the LOST Project
+ *     and its contributors.
+ * 4. Neither the name of the LOST Project nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR 
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _ENV_H_
+#define _ENV_H_
+
+#include <stdint.h>
+
+void init_envvars();
+char* getenv(const char* name);
+char* getenv_index(int index);
+char* getenv_name_by_index(int index);
+int getenv_count();
+int setenv(const char* name, const char* value, int overwrite);
+void unsetenv(const char* name);
+
+char* getcwd(char* dest, size_t size);
+int chdir(const char* path);
+
+#endif
Index: ../../lib/envvars.c
===================================================================
--- ../../lib/envvars.c	(Revision 704)
+++ ../../lib/envvars.c	(Arbeitskopie)
@@ -45,7 +45,7 @@
     char* value;
 } envvar_t;
 
-list_t* envvar_list;
+static list_t* envvar_list;
 
 void rpc_get_envvars(pid_t pid, dword correlation_id, size_t data_size, 
     void* data);
@@ -178,8 +178,54 @@
     return envvar->value;
 }
 
+/**
+ * Wert einer Umgebungsvariable auslesen
+ *
+ * @param index Index der Umgebungsvariable
+ *
+ * @return Pointer auf den Inhalt, oder NULL wenn sie nicht existiert.
+ */
+char* getenv_index(int index)
+{
+    envvar_t* envvar = list_get_element_at(envvar_list, index);
+    
+    if (envvar == NULL) {
+        return NULL;
+    }
+    
+    return envvar->value;
+}
 
 /**
+ * Name einer Umgebungsvariable auslesen
+ *
+ * @param index Index der Umgebungsvariable
+ *
+ * @return Pointer auf den Namen, oder NULL wenn die Variable nicht existiert.
+ */
+char* getenv_name_by_index(int index)
+{
+    envvar_t* envvar = list_get_element_at(envvar_list, index);
+    
+    if (envvar == NULL) {
+        return NULL;
+    }
+    
+    return envvar->name;
+}
+
+/**
+ * Anzahl der Umgebungsvariablen abfragen
+ *
+ * @return Anzahl der definierten Umgebungsvariablen
+ */
+int getenv_count()
+{
+    return list_size(envvar_list);
+}
+
+
+/**
  * Wert einer Umgebungsvariable setzen oder eine anlegen.
  *
  * @param name Name der Umgebungsvariable