commit 2cb9f7e4be09d364d0a14715161863d532ed2d3f
parent 6c4ab50ceb59d26deea1f6c158e6b6fbfc3a59b6
Author: Jesus Galan Lopez (yiyus) <yiyu.jgl@gmail.com>
Date: Wed, 23 Jun 2010 01:16:42 +0200
move .ini handling to its own file
Diffstat:
6 files changed, 224 insertions(+), 196 deletions(-)
diff --git a/src/9vx/Makefrag b/src/9vx/Makefrag
@@ -32,6 +32,7 @@ all: 9vx/9vx
PLAN9_OBJS = \
$(addprefix 9vx/, \
bootcode.o \
+ conf.o \
devaudio.o \
devaudio-$(PLAN9AUDIO).o \
devfs-posix.o \
diff --git a/src/9vx/conf.c b/src/9vx/conf.c
@@ -0,0 +1,194 @@
+#include "u.h"
+#include "lib.h"
+#include "mem.h"
+#include "dat.h"
+#include "fns.h"
+
+#include "fs.h"
+
+#include "conf.h"
+
+#include "netif.h"
+#include "etherif.h"
+#include "vether.h"
+
+extern char* localroot;
+
+/*
+ * read configuration file
+ */
+int
+readini(char *fn)
+{
+ int blankline, incomment, inspace, n, fd;
+ static int nfields = 0;
+ static char *buf = inibuf;
+ char *cp, *p, *q;
+
+ if(strcmp(fn, "-") == 0)
+ fd = fileno(stdin);
+ else if((fd = open(fn, OREAD)) < 0)
+ return -1;
+
+ cp = buf;
+ *buf = 0;
+ while((n = read(fd, buf, BOOTARGSLEN-1)) > 0)
+ if(n<0)
+ return -1;
+ else
+ buf += n;
+ close(fd);
+ *buf = 0;
+
+ /*
+ * Strip out '\r', change '\t' -> ' '.
+ * Change runs of spaces into single spaces.
+ * Strip out trailing spaces, blank lines.
+ *
+ * We do this before we make the copy so that if we
+ * need to change the copy, it is already fairly clean.
+ * The main need is in the case when plan9.ini has been
+ * padded with lots of trailing spaces, as is the case
+ * for those created during a distribution install.
+ */
+ p = cp;
+ blankline = 1;
+ incomment = inspace = 0;
+ for(q = cp; *q; q++){
+ if(*q == '\r')
+ continue;
+ if(*q == '\t')
+ *q = ' ';
+ if(*q == ' '){
+ inspace = 1;
+ continue;
+ }
+ if(*q == '\n'){
+ if(!blankline){
+ if(!incomment)
+ *p++ = '\n';
+ blankline = 1;
+ }
+ incomment = inspace = 0;
+ continue;
+ }
+ if(inspace){
+ if(!blankline && !incomment)
+ *p++ = ' ';
+ inspace = 0;
+ }
+ if(blankline && *q == '#')
+ incomment = 1;
+ blankline = 0;
+ if(!incomment)
+ *p++ = *q;
+ }
+ if(p > cp && p[-1] != '\n')
+ *p++ = '\n';
+ *p++ = 0;
+
+ nfields += getfields(cp, &iniline[nfields], MAXCONF-nfields, 0, "\n");
+
+ return 0;
+}
+
+void
+inifields(void (*fp)(char*, char*))
+{
+ int i;
+ char *cp;
+
+ for(i = 0; i < MAXCONF; i++){
+ if(!iniline[i])
+ break;
+ cp = strchr(iniline[i], '=');
+ if(cp == 0)
+ continue;
+ *cp++ = 0;
+ if(cp - iniline[i] >= NAMELEN+1)
+ *(iniline[i]+NAMELEN-1) = 0;
+ (fp)(iniline[i], cp);
+ *(cp-1) = '=';
+ }
+}
+
+void
+iniopt(char *name, char *value)
+{
+ char *cp, *vedev;
+ int vetap;
+
+ if(*name == '*')
+ name++;
+ if(strcmp(name, "bootboot") == 0)
+ bootboot = 1;
+ else if(strcmp(name, "initrc") == 0)
+ initrc = 1;
+ else if(strcmp(name, "nofork") == 0)
+ nofork = 1;
+ else if(strcmp(name, "nogui") == 0){
+ nogui = 1;
+ usetty = 1;
+ }
+ else if(strcmp(name, "usetty") == 0)
+ usetty = 1;
+ else if(strcmp(name, "memsize") == 0)
+ memsize = atoi(value);
+ else if(strcmp(name, "netdev") == 0){
+ if(strncmp(value, "tap", 3) == 0) {
+ vetap = 1;
+ value += 4;
+ }
+ vedev = value;
+ cp = vedev;
+ if((value = strchr(vedev, ' ')) != 0){
+ cp = strchr(value+1, '=');
+ *value=0;
+ *cp=0;
+ }
+ addve(*vedev == 0 ? nil : vedev, vetap);
+ if(cp != vedev){
+ iniopt(value+1, cp+1);
+ *value=' ';
+ *cp='=';
+ }
+ }
+ else if(strcmp(name, "macaddr") == 0)
+ setmac(value);
+ else if(strcmp(name, "localroot") == 0 && !localroot)
+ localroot = value;
+ else if(strcmp(name, "user") == 0 && !username)
+ username = value;
+}
+
+void
+inienv(char *name, char *value)
+{
+ if(*name != '*')
+ ksetenv(name, value, 0);
+}
+
+/*
+ * Debugging: tell user what options we guessed.
+*/
+void
+printconfig(char *argv0, char **inifile, int n){
+ int i;
+
+ print("%s ", argv0);
+ for(i=0; i<n; i++)
+ print("-p %s ", inifile[i]);
+ if(bootboot | nofork | nogui | initrc | usetty)
+ print("-%s%s%s%s%s ", bootboot ? "b" : "", nofork ? "f " : "",
+ nogui ? "g" : "", initrc ? "i " : "", usetty ? "t " : "");
+ if(memsize != 0)
+ print("-m %d ", memsize);
+ for(i=0; i<nve; i++){
+ print("-n %s", ve[i].tap ? "tap ": "");
+ if(ve[i].dev != nil)
+ print("%s ", ve[i].dev);
+ if(ve[i].mac != nil)
+ print("-a %s ", ve[i].mac);
+ }
+ print("-r %s -u %s\n", localroot, username);
+}+
\ No newline at end of file
diff --git a/src/9vx/conf.h b/src/9vx/conf.h
@@ -0,0 +1,19 @@
+#define BOOTLINELEN 64
+#define BOOTARGSLEN (3584-0x200-BOOTLINELEN)
+#define MAXCONF 100
+
+char inibuf[BOOTARGSLEN];
+char *iniline[MAXCONF];
+int bootboot; /* run /boot/boot instead of bootscript */
+int initrc; /* run rc instead of init */
+int nofork; /* do not fork at init */
+int nogui; /* do not start the gui */
+int usetty; /* use tty for input/output */
+int memsize; /* memory size */
+char* username;
+
+int readini(char *fn);
+void inifields(void (*fp)(char*, char*));
+void iniopt(char*, char*);
+void inienv(char*, char*);
+void printconfig(char*, char**, int);
diff --git a/src/9vx/devfs-posix.c b/src/9vx/devfs-posix.c
@@ -34,7 +34,7 @@ enum
};
extern Path *addelem(Path*, char*, Chan*);
-char *localroot = "/home/rsc/plan9/4e";
+char *localroot;
static char *uidtoname(int);
static char *gidtoname(int);
diff --git a/src/9vx/main.c b/src/9vx/main.c
@@ -25,11 +25,13 @@
#include "arg.h"
#include "tos.h"
-#include "fs.h"
+#include "fs.h"
-#include "netif.h"
-#include "etherif.h"
-#include "vether.h"
+#include "conf.h"
+
+#include "netif.h"
+#include "etherif.h"
+#include "vether.h"
#define Image IMAGE
#include "draw.h"
@@ -37,10 +39,6 @@
#include "cursor.h"
#include "screen.h"
-#define BOOTLINELEN 64
-#define BOOTARGSLEN (3584-0x200-BOOTLINELEN)
-#define MAXCONF 100
-
extern Dev ipdevtab;
extern Dev pipdevtab;
extern Dev drawdevtab;
@@ -53,15 +51,6 @@ char* argv0;
char* conffile = "9vx";
Conf conf;
-static char inibuf[BOOTARGSLEN];
-static char *iniline[MAXCONF];
-static int bootboot; /* run /boot/boot instead of bootscript */
-static int memsize; /* memory size */
-static int nofork; /* do not fork at init */
-static int initrc; /* run rc instead of init */
-static int nogui; /* do not start the gui */
-static int usetty; /* use tty for input/output */
-static char* username;
static Mach mach0;
extern char* localroot;
@@ -73,11 +62,6 @@ static int singlethread;
static void bootinit(void);
static void siginit(void);
-static int readini(char *fn);
-static void inifields(void (*fp)(char*, char*));
-static void iniopt(char *name, char *value);
-static void inienv(char *name, char *value);
-
static char* getuser(void);
void
@@ -246,25 +230,7 @@ main(int argc, char **argv)
*/
siginit();
- /*
- * Debugging: tell user what options we guessed.
- */
- print("9vx ");
- for(i=0; i<n; i++)
- print("-p %s ", inifile[i]);
- if(bootboot | nofork | nogui | initrc | usetty)
- print("-%s%s%s%s%s ", bootboot ? "b" : "", nofork ? "f " : "",
- nogui ? "g" : "", initrc ? "i " : "", usetty ? "t " : "");
- if(memsize != 0)
- print("-m %d ", memsize);
- for(i=0; i<nve; i++){
- print("-n %s", ve[i].tap ? "tap ": "");
- if(ve[i].dev != nil)
- print("%s ", ve[i].dev);
- if(ve[i].mac != nil)
- print("-a %s ", ve[i].mac);
- }
- print("-r %s -u %s\n", localroot, username);
+ printconfig(argv0, inifile, n);
if(nve == 0)
ipdevtab = pipdevtab;
@@ -300,159 +266,6 @@ main(int argc, char **argv)
return 0; // Not reached
}
-/*
- * read configuration file
- */
-int
-readini(char *fn)
-{
- int blankline, incomment, inspace, n, fd;
- static int nfields = 0;
- static char *buf = inibuf;
- char *cp, *p, *q;
-
- if(strcmp(fn, "-") == 0)
- fd = fileno(stdin);
- else if((fd = open(fn, OREAD)) < 0)
- return -1;
-
- cp = buf;
- *buf = 0;
- while((n = read(fd, buf, BOOTARGSLEN-1)) > 0)
- buf += n;
- close(fd);
- *buf = 0;
- if(buf == cp)
- return -1;
-
- /*
- * Strip out '\r', change '\t' -> ' '.
- * Change runs of spaces into single spaces.
- * Strip out trailing spaces, blank lines.
- *
- * We do this before we make the copy so that if we
- * need to change the copy, it is already fairly clean.
- * The main need is in the case when plan9.ini has been
- * padded with lots of trailing spaces, as is the case
- * for those created during a distribution install.
- */
- p = cp;
- blankline = 1;
- incomment = inspace = 0;
- for(q = cp; *q; q++){
- if(*q == '\r')
- continue;
- if(*q == '\t')
- *q = ' ';
- if(*q == ' '){
- inspace = 1;
- continue;
- }
- if(*q == '\n'){
- if(!blankline){
- if(!incomment)
- *p++ = '\n';
- blankline = 1;
- }
- incomment = inspace = 0;
- continue;
- }
- if(inspace){
- if(!blankline && !incomment)
- *p++ = ' ';
- inspace = 0;
- }
- if(blankline && *q == '#')
- incomment = 1;
- blankline = 0;
- if(!incomment)
- *p++ = *q;
- }
- if(p > cp && p[-1] != '\n')
- *p++ = '\n';
- *p++ = 0;
-
- nfields += getfields(cp, &iniline[nfields], MAXCONF-nfields, 0, "\n");
-
- return 0;
-}
-
-void
-inifields(void (*fp)(char*, char*))
-{
- int i;
- char *cp;
-
- for(i = 0; i < MAXCONF; i++){
- if(!iniline[i])
- break;
- cp = strchr(iniline[i], '=');
- if(cp == 0)
- continue;
- *cp++ = 0;
- if(cp - iniline[i] >= NAMELEN+1)
- *(iniline[i]+NAMELEN-1) = 0;
- (fp)(iniline[i], cp);
- *(cp-1) = '=';
- }
-}
-
-void
-iniopt(char *name, char *value)
-{
- char *cp, *vedev;
- int vetap;
-
- if(*name == '*')
- name++;
- if(strcmp(name, "bootboot") == 0)
- bootboot = 1;
- else if(strcmp(name, "initrc") == 0)
- initrc = 1;
- else if(strcmp(name, "nofork") == 0)
- nofork = 1;
- else if(strcmp(name, "memsize") == 0)
- memsize = atoi(value);
- else if(strcmp(name, "localroot") == 0 && !localroot)
- localroot = value;
- else if(strcmp(name, "user") == 0 && !username)
- username = value;
- else if(strcmp(name, "usetty") == 0)
- usetty = 1;
- else if(strcmp(name, "macaddr") == 0)
- setmac(value);
- else if(strcmp(name, "netdev") == 0){
- if(strncmp(value, "tap", 3) == 0) {
- vetap = 1;
- value += 4;
- }
- vedev = value;
- cp = vedev;
- if((value = strchr(vedev, ' ')) != 0){
- cp = strchr(value+1, '=');
- *value=0;
- *cp=0;
- }
- addve(*vedev == 0 ? nil : vedev, vetap);
- if(cp != vedev){
- iniopt(value+1, cp+1);
- *value=' ';
- *cp='=';
- }
- }
- else if(strcmp(name, "nogui") == 0){
- nogui = 1;
- usetty = 1;
- }
-}
-
-void
-inienv(char *name, char *value)
-{
- if(*name != '*')
- ksetenv(name, value, 0);
-}
-
static char*
getuser(void)
{
diff --git a/src/9vx/mmu.c b/src/9vx/mmu.c
@@ -28,7 +28,7 @@ int tracemmu;
*
* This value may be changed with the -m switch.
*/
-int memsize = (256<<20);
+static int memsize = (256<<20);
static int pagefile;
static char* pagebase;