Merge branch 'sparc-dev' of ssh://scm.millennium.berkeley.edu/project/cs/radlab/src...
[akaros.git] / user / apps / parlib / run_binary_colored.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <parlib.h>
10
11 extern char * readline(const char *prompt);
12
13 #define READ_SIZE       1024
14 uint8_t* binary_buf;
15
16 static void fd_error() {
17         fprintf(stderr, "Error: Unable to run remote binary (fd error): %s\n", 
18                                                               strerror(errno));
19 }
20
21 static void malloc_error() {
22         fprintf(stderr, 
23                 "Error: Unable to run remote binary: No more memory avaialable!\n");
24 }
25
26 static void read_error(void* buf, int fd) {
27         free(binary_buf);
28         close(fd);
29         fprintf(stderr, "Error: Unable to run remote binary (read error): %s\n", 
30                                                                 strerror(errno));
31 }
32
33 static void realloc_error(void* buf, int fd) {
34         free(binary_buf);
35         close(fd);
36         fprintf(stderr, 
37                 "Error: Unable to run remote binary: No more memory available!\n");
38 }
39
40 void run_binary_colored()
41 {       
42         char* name = readline("\nEnter name of binary to execute: ");
43         if (name == NULL) {
44                 printf("Error reading from console.\n");
45                 return;
46         }
47         char* file_name = name;
48         //char * file_name = malloc(strlen(name) + 8);
49         //sprintf(file_name, "./apps/%s", name);
50         int fd = open(file_name, O_RDONLY, 0);
51         //free(file_name);
52         if(fd < 0) { fd_error(); return; };
53
54         char* colors = readline("\nEnter number of colors: ");
55         if (colors == NULL) {
56                 printf("Error reading from console.\n");
57                 return;
58         }
59         size_t num_colors = atoi(colors);
60         
61         int iters = 1;
62         binary_buf = malloc(READ_SIZE);
63         if(binary_buf == NULL) { malloc_error(); return; }
64         
65         int total_bytes_read = 0;
66         int bytes_read = read(fd, binary_buf, READ_SIZE);
67         if(bytes_read < 0) { read_error(binary_buf, fd); return; }
68         
69         while(bytes_read > 0) {
70                 total_bytes_read += bytes_read; 
71                 void* temp_buf = realloc(binary_buf, READ_SIZE*(++iters));
72                 if(temp_buf == NULL) { realloc_error(binary_buf, fd); return; } 
73                 binary_buf = temp_buf;
74                 bytes_read = read(fd, binary_buf+total_bytes_read, READ_SIZE);
75                 if(bytes_read < 0) { read_error(binary_buf, fd); return; }
76         }
77         printf("Loading Binary: %s, ROMSIZE: %d\n", name, total_bytes_read);
78         ssize_t error = sys_run_binary(binary_buf, NULL, 
79                             total_bytes_read, num_colors);
80         if(error < 0) {
81                 fprintf(stderr, "Error: Unable to run remote binary\n");
82         }
83         free(binary_buf);
84         close(fd);
85 }
86