Move file_io to a matrix-run app. Cleaned up matrix and file_io.
authorPaul Pearce <pearce@eecs.berkeley.edu>
Mon, 1 Jun 2009 06:03:00 +0000 (02:03 -0400)
committerKevin Klues <klueska@cs.berkeley.edu>
Fri, 5 Jun 2009 01:24:38 +0000 (18:24 -0700)
Rewrote file_io to be an application executed from our "shell" app. Cleaned
up both matrix and file_io to be more presentable for the retreat. Changed
the tests in file_io to be more interactive. Done.

user/apps/parlib/Makefrag
user/apps/parlib/change_user.c [new file with mode: 0644]
user/apps/parlib/file_io.c
user/apps/parlib/matrix.c
user/parlib/src/newlib_backend.c

index 94f6ba1..8585466 100644 (file)
@@ -14,9 +14,11 @@ USER_APPS_PARLIB_LDDIRS    := -L$(OBJDIR)/$(USER_PARLIB_DIR) \
 USER_APPS_PARLIB_LDLIBS    := --start-group -lc -lm -lg -lparlib --end-group
 
 USER_APPS_PARLIB_LDOBJS    := $(OBJDIR)/$(USER_PARLIB_DIR)/entry.o \
-                                                         $(OBJDIR)/$(USER_APPS_PARLIB_DIR)/readline.o \
+                              $(OBJDIR)/$(USER_APPS_PARLIB_DIR)/readline.o \
+                              $(OBJDIR)/$(USER_APPS_PARLIB_DIR)/file_io.o \
                               $(OBJDIR)/$(USER_APPS_PARLIB_DIR)/clrscrn.o \
-                              $(OBJDIR)/$(USER_APPS_PARLIB_DIR)/draw_nanwan.o 
+                              $(OBJDIR)/$(USER_APPS_PARLIB_DIR)/draw_nanwan.o \
+                              $(OBJDIR)/$(USER_APPS_PARLIB_DIR)/change_user.o  
 
 USER_APPS_PARLIB_LDDEPENDS := $(USER_APPS_PARLIB_LDOBJS) \
                               $(OBJDIR)/$(USER_PARLIB_DIR)/libparlib.a \
diff --git a/user/apps/parlib/change_user.c b/user/apps/parlib/change_user.c
new file mode 100644 (file)
index 0000000..b8210b9
--- /dev/null
@@ -0,0 +1,35 @@
+#include <stdio.h>
+#include <unistd.h>
+
+#define DEFAULT_USER "nanwan"
+#define DEFAULT_HOST "ros"
+
+void set_default_user();
+void set_user();
+void change_user();
+
+extern char * readline(const char *prompt);
+
+
+char prompt[256];
+
+void set_default_user() {
+       set_user(DEFAULT_USER);
+}
+
+void set_user(char * user) {
+
+       sprintf(prompt, "%s@%s$ ", user, DEFAULT_HOST);
+}
+
+void change_user() {
+       char *s = readline("Enter new username: ");
+
+       if (s == NULL)
+               printf("Error: Could not change user!\n");      
+       else
+               set_user(s);
+}
+
+
+
index d9b01be..069385a 100644 (file)
@@ -1,3 +1,4 @@
+#include <string.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -5,36 +6,67 @@
 #include <stdio.h>
 #include <unistd.h>
 
-int main(int argc, char** argv)
-{
-       printf("Welcome to the ROS newlib test suite!\nEnter at you're own risk....\n");
+#define IN_BUF_SIZE 1024
 
-       printf("Size of stat struct: %u\n", sizeof(struct stat));
+extern char * readline(const char *prompt);
 
-       int fd = open("./test/matrix", O_RDWR, 0);
-       char buf[100];
-       int read_amt = read(fd, buf, 100);
-       printf("FD: %d\n", fd);
-       printf("read_amt: %d\n", read_amt);
-       printf("read: %s\n", buf);
+void file_io()
+{      
+       printf("Beginning Serial Based File IO Test...\n\n");
+       int in_fd = open("./test/input", O_RDWR, 0);
+       char buf[IN_BUF_SIZE];
+       int read_amt = read(in_fd, buf, IN_BUF_SIZE - 1);
+       buf[read_amt] = '\0';
+       printf("Opened:       input\n");
+       printf("FD:           %d\n", in_fd);
+       printf("Read:         %d bytes\n", read_amt);
+       printf("Data read:    %s", buf);
 
-       char buf2[] = "NANWAN!\n";
 
-       int fd = open("./test/nanwan", O_RDWR | O_CREAT, 
-                     S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
-       
-       lseek(fd, 0, SEEK_END); 
+       char * readline_result = readline("\nEnter filename for writing: ");
+       if (readline_result == NULL) {
+               printf("Error reading from console.\n");
+               return;
+       }
+
+       char * file_name = malloc(strlen(readline_result) + 1);
+       strcpy(file_name, readline_result);
+
+       readline_result = readline("Enter text to write to file: ");
+
+        if (readline_result == NULL) {
+                printf("Error reading from console.\n");
+                return;
+        }
+
+
+       char *buf2 = malloc(strlen(readline_result) + 1);
+       strcpy(buf2, readline_result);
 
-       int write_amt = write(fd, buf2, sizeof(buf2) -1);
-        printf("write_amt: %d\n", write_amt);        
-       printf("wrote: %s\n", buf2);
 
-       unlink ("./test/nanwan_link");
+       char * output_full_path = malloc(strlen(file_name) + 8);
+       sprintf(output_full_path, "./test/%s", file_name);
+
+       int out_fd = open(output_full_path, O_RDWR | O_CREAT | O_TRUNC,
+                      S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
+
+        printf("\nOpened:       %s\n", file_name);
+        printf("FD:           %d\n", out_fd);
+
+
+       int write_amt = write(out_fd, buf2, strlen(buf2));
+       
+        printf("Wrote:        %d bytes\n", write_amt);
 
 
-       link("./test/nanwan", "./test/nanwan_link");
-       unlink("./test/to_delete");
+        int in_fd2 = open(output_full_path, O_RDWR, 0);
+        read_amt = read(in_fd2, buf, IN_BUF_SIZE - 1);
+        buf[read_amt] = '\0';
+        printf("Data written: %s\n\n", buf);
 
+       printf("Closing remote file descriptor: %d.... %s\n", in_fd, ((close(in_fd) == 0) ? "successful" : "failure"));
+       printf("Closing remote file descriptor: %d.... %s\n", out_fd, ((close(out_fd) == 0) ? "successful" : "failure"));
+       close(in_fd2);
 
-       return 0;
+       printf("\nTests Complete.\n\n");
 }
index cd6d5bc..02910cd 100644 (file)
@@ -7,30 +7,43 @@
 extern char * readline(const char *prompt);
 extern void draw_nanwan();
 extern void clrscrn(int leaverows);
+extern void change_user();
+extern void set_default_user();
+extern void file_io();
+extern char prompt[256];
 
 void help() {
        printf("Possible commands to run:\n"
-              "  draw_nanwan: Draw a picture of Nanwan, our mascot giraffe\n"
+              "  draw_nanwan:      Draw a picture of Nanwan, our mascot giraffe\n"
               "  clear_screen:     Clear the Screen\n"
+              "  change_user:      Change Username\n"
+               "  file_io:          Run File Related IO Tests\n"
              );
 }
 
 int main(int argc, char** argv)
 {      
+       set_default_user();
        printf("Welcome to the Tessellation OS newlib test suite!\n");
        printf("Enter at you're own risk....\n");
        clrscrn(2);
        while(1) {
-               char* s = readline("nanwan@ros$ ");
-               printf("%s\n", s);
+               char* s = readline(prompt);
+
+               if (s == NULL)
+                       continue;
+
                if(strcmp(s, "draw_nanwan") == 0)
                        draw_nanwan();          
                else if(strcmp(s, "clear_screen") == 0)
                        clrscrn(0);             
-               else 
+               else if (strcmp(s, "change_user") == 0)
+                       change_user();
+               else if (strcmp(s, "file_io") == 0)
+                       file_io();
+               else
                        help(); 
 
        }       
        return 0;
 }
-
index fb074ec..fd5a12e 100644 (file)
@@ -115,8 +115,8 @@ int fstat(int file, struct stat *st)
        st->st_mode = S_IFCHR;
        
        // stdout hack
-       if (file == 1)
-               st->st_mode = 8592;
+//     if (file == 1)
+//             st->st_mode = 8592;
        return 0;
 
 
@@ -176,8 +176,8 @@ int isatty(int file)
        debug_in_out("ISATTY\n");
 
        // Cheap hack to avoid sending serial comm for stuff we know
-       if ((STDIN_FILENO == file) || (STDOUT_FILENO == file) || (STDERR_FILENO == file))
-               return 1;
+//     if ((STDIN_FILENO == file) || (STDOUT_FILENO == file) || (STDERR_FILENO == file))
+//             return 1;
 
        
        // Allocate a new buffer of proper size