[1] | 1 | /* |
---|
| 2 | This file is part of MutekP. |
---|
| 3 | |
---|
| 4 | MutekP is free software; you can redistribute it and/or modify it |
---|
| 5 | under the terms of the GNU General Public License as published by |
---|
| 6 | the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | (at your option) any later version. |
---|
| 8 | |
---|
| 9 | MutekP is distributed in the hope that it will be useful, but |
---|
| 10 | WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 12 | General Public License for more details. |
---|
| 13 | |
---|
| 14 | You should have received a copy of the GNU General Public License |
---|
| 15 | along with MutekP; if not, write to the Free Software Foundation, |
---|
| 16 | Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 17 | |
---|
| 18 | UPMC / LIP6 / SOC (c) 2008 |
---|
| 19 | Copyright Ghassan Almaless <ghassan.almaless@gmail.com> |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #include <ctype.h> |
---|
| 23 | #include <libk.h> |
---|
| 24 | #include <string.h> |
---|
| 25 | #include <thread.h> |
---|
| 26 | #include <cluster.h> |
---|
| 27 | #include <sys-vfs.h> |
---|
| 28 | #include <driver.h> |
---|
[11] | 29 | #include <chdev.h> |
---|
[1] | 30 | #include <kminiShell.h> |
---|
| 31 | #include <stdarg.h> |
---|
| 32 | |
---|
| 33 | kdmsg_channel_t ksh_tty = {{.id = 0}}; |
---|
| 34 | |
---|
| 35 | char getChar() |
---|
| 36 | { |
---|
| 37 | struct device_s *tty; |
---|
| 38 | dev_request_t rq; |
---|
| 39 | char ch; |
---|
| 40 | size_t size; |
---|
| 41 | |
---|
| 42 | ch = 0; |
---|
| 43 | size = 0; |
---|
| 44 | tty = ttys_tbl[ksh_tty.id]; |
---|
| 45 | |
---|
| 46 | while(size != 1 && size != -1) |
---|
| 47 | { |
---|
| 48 | rq.dst = (uint32_t*) &ch; |
---|
| 49 | rq.count = 1; |
---|
| 50 | rq.flags = 0; |
---|
| 51 | size = tty->op.dev.read(tty, &rq); |
---|
| 52 | } |
---|
| 53 | return ch; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | ssize_t ksh_getLine(char *buff, int count) |
---|
| 58 | { |
---|
| 59 | struct device_s *tty; |
---|
| 60 | dev_request_t rq; |
---|
| 61 | ssize_t size; |
---|
| 62 | |
---|
| 63 | tty = ttys_tbl[ksh_tty.id]; |
---|
| 64 | |
---|
| 65 | rq.dst = (uint32_t*) buff; |
---|
| 66 | rq.count = count; |
---|
| 67 | rq.flags = 0; |
---|
| 68 | size = tty->op.dev.read(tty, &rq); |
---|
| 69 | return size; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | void ksh_write(void *buff, size_t size) |
---|
| 73 | { |
---|
| 74 | struct device_s *tty; |
---|
| 75 | dev_request_t rq; |
---|
| 76 | |
---|
| 77 | tty = ttys_tbl[ksh_tty.id]; |
---|
| 78 | rq.src = buff; |
---|
| 79 | rq.count = size; |
---|
| 80 | rq.flags = 0; |
---|
| 81 | (void)tty->op.dev.write(tty, &rq); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | static char ksh_msg_buff[MSG_STR_LEN]; |
---|
| 85 | |
---|
| 86 | void ksh_print(const char *fmt, ...) |
---|
| 87 | { |
---|
| 88 | va_list ap; |
---|
| 89 | int count; |
---|
| 90 | |
---|
| 91 | va_start (ap, fmt); |
---|
| 92 | count = iprintk(&ksh_msg_buff[0], current_cid, 1, (char *) fmt, ap); |
---|
| 93 | assert(count < (MSG_STR_LEN-1)); |
---|
| 94 | va_end (ap); |
---|
| 95 | count = (count < (MSG_STR_LEN -1)) ? count : MSG_STR_LEN - 1; |
---|
| 96 | ksh_msg_buff[count + 1] = 0; |
---|
| 97 | ksh_write(ksh_msg_buff, count); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | void getString(char *buff,size_t size) |
---|
| 101 | { |
---|
| 102 | char ch = 0; |
---|
| 103 | char *str = buff; |
---|
| 104 | char *val = NULL; |
---|
| 105 | char line[128]; |
---|
| 106 | char *line_ptr = &line[0]; |
---|
| 107 | ssize_t count,i; |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | while(size > 0) |
---|
| 111 | { |
---|
| 112 | count = ksh_getLine(line_ptr,128); |
---|
| 113 | i=0; |
---|
| 114 | |
---|
| 115 | while((count > 0) && (size > 0)) |
---|
| 116 | { |
---|
| 117 | ch=line[i++]; |
---|
| 118 | |
---|
| 119 | if(ch == '\r') |
---|
| 120 | continue; |
---|
| 121 | |
---|
| 122 | if(ch == '\n') |
---|
| 123 | goto GETSTRING_END; |
---|
| 124 | |
---|
| 125 | if(!isprint(ch)) |
---|
| 126 | { |
---|
| 127 | if((ch == 0x7F) || (ch == 0x08)) |
---|
| 128 | str--; |
---|
| 129 | count ++; |
---|
| 130 | size ++; |
---|
| 131 | continue; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | *str ++ = ch; |
---|
| 135 | ch = 0; |
---|
| 136 | count --; |
---|
| 137 | size --; |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | GETSTRING_END: |
---|
| 142 | val = buff; |
---|
| 143 | *str = 0; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | void cmdLineAnalyser(ms_args_t *args) |
---|
| 147 | { |
---|
| 148 | char buff[128]; |
---|
| 149 | char *str = &buff[0]; |
---|
| 150 | char *arg = NULL; |
---|
| 151 | uint_t i; |
---|
| 152 | |
---|
| 153 | getString(buff,128); |
---|
| 154 | i=0; |
---|
| 155 | |
---|
| 156 | while((*str != '\0') && (i < MS_MAX_ARG_LEN)) |
---|
| 157 | { |
---|
| 158 | while((*str) && (isspace(*str))) str++; |
---|
| 159 | |
---|
| 160 | if(!(*str)) break; |
---|
| 161 | |
---|
| 162 | arg = args->argv[i]; |
---|
| 163 | |
---|
| 164 | while((*str) && (!isspace(*str))) |
---|
| 165 | *arg++ = *str ++; |
---|
| 166 | |
---|
| 167 | i++; *arg = 0; |
---|
| 168 | } |
---|
| 169 | args->argv[i][0] = 0; |
---|
| 170 | args->argc = i; |
---|
| 171 | } |
---|