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

[Xen-devel] POC: ncurses in stubdom


  • To: xen-devel@xxxxxxxxxxxxxxxxxxx
  • From: Ferenc Wagner <wferi@xxxxxxx>
  • Date: Tue, 28 Apr 2009 20:22:11 +0200
  • Delivery-date: Tue, 28 Apr 2009 11:22:41 -0700
  • List-id: Xen developer discussion <xen-devel.lists.xensource.com>

Ferenc Wagner <wferi@xxxxxxx> writes:

> Error opening terminal: unknown.
>
> Now let's try to hardwire the terminal type to xterm or similar...

Yeah, calling setenv("TERM","xterm",1) before initialization resulted
in a working ncurses stub domain!  Patches against Xen-3.3.1 attached.

 * One needs to apply xen_ncurses.patch only, that references the
 other two (so tune the paths).

 * fpathconf() is stubbed in main.c, because I don't understand the
 issue yet.

 * access() is also blatantly stubbed, as it's pointless in stub
 domains anyway.

 * Most other changes are debatable as well, but they're quite small
 and should be even smaller in the development version, where some
 issues are already fixed.

 * Example from http://www.captain.at/howto-curses-example.php.

I think something like this (but done right) would be useful in the
official packages as well.

Comments welcome!
-- 
Thanks,
Feri.
diff -ru xen-3.3.1.orig/extras/mini-os/lib/sys.c 
xen-3.3.1/extras/mini-os/lib/sys.c
--- xen-3.3.1.orig/extras/mini-os/lib/sys.c     2009-01-05 12:26:58.000000000 
+0100
+++ xen-3.3.1/extras/mini-os/lib/sys.c  2009-04-28 19:43:43.151850562 +0200
@@ -147,6 +147,16 @@
     return 1;
 }
 
+pid_t tcgetpgrp(int fd)
+{
+    return 1;
+}
+
+pid_t getpgrp(void)
+{
+    return 1;
+}
+
 char *getcwd(char *buf, size_t size)
 {
     snprintf(buf, size, "/");
@@ -364,6 +374,20 @@
     return -1;
 }
 
+int tcflush(int fd, int queue_selector)
+{
+    switch (files[fd].type) {
+        case FTYPE_CONSOLE:
+            /* Already flushed */
+            return 0;
+        default:
+            break;
+    }
+    printk("tcflush(%d): Bad descriptor\n", fd);
+    errno = EBADF;
+    return -1;
+}
+
 int close(int fd)
 {
     printk("close(%d)\n", fd);
@@ -472,6 +496,8 @@
 out:
     return ret;
 }
+/* We do not have symlinks */
+int lstat(const char *path, struct stat *buf) __attribute__((alias("stat")));
 
 int fstat(int fd, struct stat *buf)
 {
@@ -509,6 +535,12 @@
     return -1;
 }
 
+/* Dummy implementation: always true */
+int access(const char *path, int mode)
+{
+       return 0;
+}
+
 int ftruncate(int fd, off_t length)
 {
     switch (files[fd].type) {
diff -ru xen-3.3.1.orig/extras/mini-os/Makefile 
xen-3.3.1/extras/mini-os/Makefile
--- xen-3.3.1.orig/extras/mini-os/Makefile      2009-01-05 12:26:58.000000000 
+0100
+++ xen-3.3.1/extras/mini-os/Makefile   2009-04-28 19:43:43.151850562 +0200
@@ -20,7 +20,7 @@
 
 # Define some default flags for linking.
 LDLIBS := 
-APP_LDLIBS := 
+APP_LDLIBS := -lncurses
 LDARCHLIB := -L$(OBJ_DIR)/$(TARGET_ARCH_DIR) -l$(ARCH_LIB_NAME)
 LDFLAGS_FINAL := -T $(TARGET_ARCH_DIR)/minios-$(XEN_TARGET_ARCH).lds
 
diff -ru xen-3.3.1.orig/stubdom/c/main.c xen-3.3.1/stubdom/c/main.c
--- xen-3.3.1.orig/stubdom/c/main.c     2009-01-05 12:26:58.000000000 +0100
+++ xen-3.3.1/stubdom/c/main.c  2009-04-28 20:01:02.296994535 +0200
@@ -1,8 +1,84 @@
 #include <stdio.h>
 #include <unistd.h>
+#include <stdlib.h>
+
+#include <time.h>
+#include <ncurses/curses.h>
+
+long
+fpathconf (int fd, int name)
+{
+       return 0;
+}
+
+int current_getch;
+int doloop = 1;
+static WINDOW *mainwnd;
+static WINDOW *screen;
+WINDOW *my_win;
+
+int now_sec, now_min, now_hour, now_day, now_wday, now_month, now_year;
+time_t now;
+struct tm *now_tm;
+       
+void screen_init(void) {
+   mainwnd = initscr();
+   noecho();
+   cbreak();
+   nodelay(mainwnd, TRUE);
+   refresh(); // 1)
+   wrefresh(mainwnd);
+   screen = newwin(13, 27, 1, 1);
+   box(screen, ACS_VLINE, ACS_HLINE);
+}
+
+static void update_display(void) {
+   curs_set(0);
+   mvwprintw(screen,1,1,"-------- HEADER --------");
+   mvwprintw(screen,3,6,"TIME: %d:%d:%d", now_hour, now_min, now_sec);
+   mvwprintw(screen,5,6,"DATE: %d-%d-%d", now_day, now_month, now_year);
+   mvwprintw(screen,7,6,"PRESS q TO END");
+   mvwprintw(screen,10,1,"-------- FOOTER --------");
+   wrefresh(screen);
+   refresh();
+}
+
+void screen_end(void) {
+   endwin();
+}
+
+void maketime(void) {
+       // Get the current date/time
+       now = time (NULL);
+       now_tm = localtime (&now);
+   now_sec = now_tm->tm_sec;
+       now_min = now_tm->tm_min;
+       now_hour = now_tm->tm_hour;
+       now_day = now_tm->tm_mday;
+       now_wday = now_tm->tm_wday;
+       now_month = now_tm->tm_mon + 1;
+       now_year = now_tm->tm_year + 1900;
+}
 
 int main(void) {
         sleep(2);
-        printf("Hello, world!\n");
+        printf("Hello, ncurses world!\n");
+       sleep(2);
+
+       setenv("TERM","xterm",1);
+
+   screen_init();
+   while (doloop) {
+      current_getch = getch();
+      if (current_getch == 113) {
+         doloop = 0;
+      }
+      maketime();
+      update_display();
+      sleep(1);
+   }
+   screen_end();
+   printf("TEST ENDS\n");
+
         return 0;
 }
diff -ru xen-3.3.1.orig/stubdom/Makefile xen-3.3.1/stubdom/Makefile
--- xen-3.3.1.orig/stubdom/Makefile     2009-01-05 12:26:58.000000000 +0100
+++ xen-3.3.1/stubdom/Makefile  2009-04-28 19:43:43.159849073 +0200
@@ -20,6 +20,8 @@
 LWIP_VERSION=1.3.0
 GRUB_URL?=http://alpha.gnu.org/gnu/grub
 GRUB_VERSION=0.97
+NCURSES_URL?=ftp://invisible-island.net/ncurses
+NCURSES_VERSION=5.7
 
 WGET=wget -c
 
@@ -59,8 +61,8 @@
 TARGET_CPPFLAGS += -isystem $(CURDIR)/$(MINI_OS)/include/posix
 TARGET_CPPFLAGS += -isystem $(CROSS_PREFIX)/$(GNU_TARGET_ARCH)-xen-elf/include
 TARGET_CPPFLAGS += -isystem $(GCC_INSTALL)include
-TARGET_CPPFLAGS += -isystem $(CURDIR)/lwip/src/include
-TARGET_CPPFLAGS += -isystem $(CURDIR)/lwip/src/include/ipv4
+TARGET_CPPFLAGS += -isystem $(CURDIR)/lwip-$(XEN_TARGET_ARCH)/src/include
+TARGET_CPPFLAGS += -isystem $(CURDIR)/lwip-$(XEN_TARGET_ARCH)/src/include/ipv4
 TARGET_CPPFLAGS += -I$(CURDIR)/include
 
 TARGET_LDFLAGS += -nostdlib -L$(CROSS_PREFIX)/$(GNU_TARGET_ARCH)-xen-elf/lib
@@ -85,6 +87,7 @@
 newlib-$(NEWLIB_VERSION): newlib-$(NEWLIB_VERSION).tar.gz
        tar xzf $<
        patch -d $@ -p0 < newlib.patch
+       patch -d $@ -p0 < ~/xen/newlib_memalign.patch
        touch $@
 
 NEWLIB_STAMPFILE=$(CROSS_ROOT)/$(GNU_TARGET_ARCH)-xen-elf/lib/libc.a
@@ -117,6 +120,27 @@
          $(MAKE) libz.a && \
          $(MAKE) install )
 
+###############
+# Cross-ncurses
+###############
+
+ncurses-$(NCURSES_VERSION).tar.gz:
+       $(WGET) $(NCURSES_URL)/$@
+
+ncurses-$(XEN_TARGET_ARCH): ncurses-$(NCURSES_VERSION).tar.gz
+       tar xzf $<
+       mv ncurses-$(NCURSES_VERSION) $@
+       chmod -R o+w $@
+       patch -d $@ -p1 < ~/xen/ncurses.patch
+
+NCURSES_STAMPFILE=$(CROSS_ROOT)/$(GNU_TARGET_ARCH)-xen-elf/lib/libncurses.a
+.PHONY: cross-ncurses
+cross-ncurses: $(NCURSES_STAMPFILE)
+$(NCURSES_STAMPFILE): ncurses-$(XEN_TARGET_ARCH) $(NEWLIB_STAMPFILE)
+       ( cd $< && \
+         CFLAGS="$(TARGET_CPPFLAGS) $(TARGET_CFLAGS)" CC=$(CC) ./configure 
--prefix=$(CROSS_PREFIX)/$(GNU_TARGET_ARCH)-xen-elf --disable-database 
--with-fallbacks="ansi,linux,vt102,xterm" && \
+         $(MAKE) BUILD_CCFLAGS="-DHAVE_CONFIG_H -I../ncurses -I$(srcdir) 
-I$(INCDIR) -I../include" install.libs )
+
 ##############
 # Cross-libpci
 ##############
@@ -162,7 +186,7 @@
 #######
 
 .PHONY: $(CROSS_ROOT)
-$(CROSS_ROOT): cross-newlib cross-zlib cross-libpci
+$(CROSS_ROOT): cross-newlib cross-zlib cross-ncurses cross-libpci
 
 mk-headers-$(XEN_TARGET_ARCH):
        mkdir -p include/xen && \
diff -ru ncurses-5.7.orig/form/form.h ncurses-5.7/form/form.h
--- ncurses-5.7.orig/form/form.h        2009-04-28 17:39:41.421348906 +0200
+++ ncurses-5.7/form/form.h     2009-04-28 17:47:53.500200395 +0200
@@ -38,6 +38,9 @@
 #include <curses.h>
 #include <eti.h>
 
+/* Xen header bug workaround */
+#undef current
+
 #ifdef __cplusplus
   extern "C" {
 #endif
diff -ru ncurses-5.7.orig/ncurses/base/lib_newterm.c 
ncurses-5.7/ncurses/base/lib_newterm.c
--- ncurses-5.7.orig/ncurses/base/lib_newterm.c 2009-04-28 17:39:41.465351220 
+0200
+++ ncurses-5.7/ncurses/base/lib_newterm.c      2009-04-28 17:48:27.471847654 
+0200
@@ -48,6 +48,9 @@
 #include <term.h>              /* clear_screen, cup & friends, cur_term */
 #include <tic.h>
 
+/* Xen header bug workaround */
+#undef current
+
 MODULE_ID("$Id: lib_newterm.c,v 1.73 2008/08/16 21:20:48 Werner.Fink Exp $")
 
 #ifndef ONLCR                  /* Allows compilation under the QNX 4.2 OS */
diff -ru ncurses-5.7.orig/ncurses/tinfo/lib_baudrate.c 
ncurses-5.7/ncurses/tinfo/lib_baudrate.c
--- ncurses-5.7.orig/ncurses/tinfo/lib_baudrate.c       2009-04-28 
17:39:41.471850972 +0200
+++ ncurses-5.7/ncurses/tinfo/lib_baudrate.c    2009-04-28 17:42:55.999849179 
+0200
@@ -95,6 +95,24 @@
     int sp;                    /* the actual speed */
 };
 
+/* Copied from Linux' /usr/include/bits/termios.h, Mini-OS has no support for 
these */
+#define  B0     0000000         /* hang up */
+#define  B50    0000001
+#define  B75    0000002
+#define  B110   0000003
+#define  B134   0000004
+#define  B150   0000005
+#define  B200   0000006
+#define  B300   0000007
+#define  B600   0000010
+#define  B1200  0000011
+#define  B1800  0000012
+#define  B2400  0000013
+#define  B4800  0000014
+#define  B9600  0000015
+#define  B19200 0000016
+#define  B38400 0000017
+
 static struct speed const speeds[] =
 {
     {B0, 0},
@@ -223,7 +241,7 @@
        ospeed = _nc_ospeed(result);
 #else /* !USE_OLD_TTY */
 #ifdef TERMIOS
-       ospeed = cfgetospeed(&cur_term->Nttyb);
+       ospeed = B38400; /* not applicable */
 #else
        ospeed = cur_term->Nttyb.sg_ospeed;
 #endif
diff -ru ncurses-5.7.orig/ncurses/tinfo/lib_kernel.c 
ncurses-5.7/ncurses/tinfo/lib_kernel.c
--- ncurses-5.7.orig/ncurses/tinfo/lib_kernel.c 2009-04-28 17:39:41.467853660 
+0200
+++ ncurses-5.7/ncurses/tinfo/lib_kernel.c      2009-04-28 17:39:50.968287551 
+0200
@@ -48,6 +48,11 @@
 #include <curses.priv.h>
 #include <term.h>              /* cur_term */
 
+/* Copied from Linux' /usr/include/bits/termios.h, Mini-OS has no support for 
these */
+#define VERASE 2
+#define VKILL 3
+#define TCIFLUSH        0
+
 MODULE_ID("$Id: lib_kernel.c,v 1.24 2004/05/08 17:11:21 tom Exp $")
 
 static int
diff -ru ncurses-5.7.orig/ncurses/tty/lib_twait.c 
ncurses-5.7/ncurses/tty/lib_twait.c
--- ncurses-5.7.orig/ncurses/tty/lib_twait.c    2009-04-28 17:39:41.475852203 
+0200
+++ ncurses-5.7/ncurses/tty/lib_twait.c 2009-04-28 17:39:50.968287551 +0200
@@ -41,8 +41,6 @@
 **     comments, none of the original code remains - T.Dickey).
 */
 
-#include <curses.priv.h>
-
 #if defined __HAIKU__ && defined __BEOS__
 #undef __BEOS__
 #endif
@@ -68,6 +66,8 @@
 
 #undef CUR
 
+#include <curses.priv.h>
+
 MODULE_ID("$Id: lib_twait.c,v 1.59 2008/08/30 20:08:19 tom Exp $")
 
 static long
diff -ru ncurses-5.7.orig/ncurses/tty/tty_update.c 
ncurses-5.7/ncurses/tty/tty_update.c
--- ncurses-5.7.orig/ncurses/tty/tty_update.c   2009-04-28 17:39:41.475852203 
+0200
+++ ncurses-5.7/ncurses/tty/tty_update.c        2009-04-28 17:39:50.971849134 
+0200
@@ -42,8 +42,6 @@
  *
  *-----------------------------------------------------------------*/
 
-#include <curses.priv.h>
-
 #if defined __HAIKU__ && defined __BEOS__
 #undef __BEOS__
 #endif
@@ -75,6 +73,8 @@
 #endif
 #endif
 
+#include <curses.priv.h>
+
 #include <ctype.h>
 #include <term.h>
 
--- newlib/libc/include/malloc.h.orig   2009-04-28 17:58:18.919855472 +0200
+++ newlib/libc/include/malloc.h        2009-04-28 17:58:51.472346774 +0200
@@ -73,6 +73,7 @@
 #else
 extern _PTR _memalign_r _PARAMS ((struct _reent *, size_t, size_t));
 #endif
+#define posix_memalign memalign
 
 extern struct mallinfo mallinfo _PARAMS ((void));
 #ifdef __CYGWIN__
_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.