initial checkin
authorEnrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
Tue, 19 Oct 2004 21:10:37 +0000 (21:10 +0000)
committerEnrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
Tue, 19 Oct 2004 21:10:37 +0000 (21:10 +0000)
git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@1745 94cd875c-1c1d-0410-91d2-eb244daf1a30

util-vserver/lib_internal/filecfg-iteratemultiline.c [new file with mode: 0644]
util-vserver/lib_internal/testsuite/filecfg-ml.c [new file with mode: 0644]
util-vserver/vserver-start/defaulttty.c [new file with mode: 0644]

diff --git a/util-vserver/lib_internal/filecfg-iteratemultiline.c b/util-vserver/lib_internal/filecfg-iteratemultiline.c
new file mode 100644 (file)
index 0000000..535c08d
--- /dev/null
@@ -0,0 +1,55 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//  
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//  
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//  
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#include "filecfg.h"
+
+bool
+FileCfg_iterateOverMultiLine(char const *str,
+                            FileCfg_MultiLineHandler handler,
+                            void *data)
+{
+  char const   *ptr    = str;
+  size_t       line_nr = 1;
+  
+  while (*ptr!='\0') {
+    while (*ptr==' ' || *ptr=='\t') ++ptr;     // left-trim line
+    
+    char const *eol = strchr(ptr, '\n');
+    if (eol==0) eol=ptr+strlen(ptr);   // handle unterminated lines
+
+    if (*ptr!='#') {   // skip commented lines
+      size_t   len = eol-ptr;
+      while (len>0 && (ptr[len-1]==' ' || ptr[len-1]=='\t'))
+       --len;          // right-trim line
+
+       // handle only non-empty lines
+      if (len>0 && !(*handler)(data, ptr, len))
+       return false;   // some parsing-error occured...
+    }
+    
+    ++line_nr;
+    ptr = eol;
+    if (*ptr) ++ptr;
+  }
+
+  return true;
+}
diff --git a/util-vserver/lib_internal/testsuite/filecfg-ml.c b/util-vserver/lib_internal/testsuite/filecfg-ml.c
new file mode 100644 (file)
index 0000000..a25cf5c
--- /dev/null
@@ -0,0 +1,52 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//  
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//  
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//  
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#include <lib_internal/filecfg.h>
+
+char const data[] = "\
+line 1\n\
+#line 2\n\
+\n\
+line 4\n\
+ \n\
+line 6\n\
+ \tline 7\n\
+line 8\t  \n\
+";
+
+static bool
+test(void *x_p, char const *str, size_t len)
+{
+  size_t       *x = x_p;
+
+  if (strncmp(str, "line ", 5)!=0) abort();
+  if (!isdigit(str[len-1]))        abort();
+  ++*x;
+  return true;
+}
+
+int main()
+{
+  size_t       l = 1;
+  FileCfg_iterateOverMultiLine(data, test, &l);
+  if (l!=6) abort();
+}
diff --git a/util-vserver/vserver-start/defaulttty.c b/util-vserver/vserver-start/defaulttty.c
new file mode 100644 (file)
index 0000000..8bce32b
--- /dev/null
@@ -0,0 +1,79 @@
+// $Id$    --*- c -*--
+
+// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+//  
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//  
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//  
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#include "vserver-start.h"
+#include <pathconfig.h>
+
+#include <lib_internal/string.h>
+
+#include <unistd.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#define ENSC_WRAPPERS_UNISTD   1
+#define ENSC_WRAPPERS_FCNTL    1
+#include <wrappers.h>
+
+
+inline static bool
+checkTTY(char const /*@null@*/ *p)
+{
+  return p!=0 && access(p, R_OK|W_OK)==0;
+}
+
+void
+setDefaultTTY(PathInfo const *cfgdir, char const *dflt)
+{
+  PathInfo     subpath = ENSC_STRING_FIXED("/apps/init/tty");
+  char         buf[ENSC_PI_APPSZ(*cfgdir, subpath)];
+  char const * new_tty = 0;
+
+  do {
+    PathInfo   ttypath = *cfgdir;
+    
+    PathInfo_append(&ttypath, &subpath, buf);
+    new_tty = String_c_str(&ttypath, buf);
+    if (checkTTY(new_tty)) break;
+
+    new_tty = CONFDIR "/.defaults/apps/init/tty";
+    if (checkTTY(new_tty)) break;
+
+    new_tty = dflt;
+    if (checkTTY(new_tty)) break;
+
+    new_tty = "/dev/null";
+  } while (false);
+
+  int          fd_in  = Eopen(new_tty, O_RDONLY, 0);
+  if (fd_in!=0) {
+    Edup2(fd_in,  0);
+    Eclose(fd_in);
+  }
+  
+  int          fd_out = Eopen(new_tty, O_WRONLY, 0600);
+  if (fd_out!=1) {
+    Edup2(fd_out, 1);
+    Eclose(fd_out);
+  }
+  
+  Edup2(1, 2);
+}