Operating System
terminal.c
Go to the documentation of this file.
1 
11 #include "../include/utilities.h"
12 #include "../include/mystring.h"
13 #include "../filesystem/API/fsapi.h"
14 
15 #define BACK_SPACE 8
16 #define BUFFER_SIZE 64
17 #define PROMT "yb@yb-thinkpad-e450:~$ "
18 #define HELP_MSG " run <program> : run program <program>\n\r" \
19  " \t e.g. run stoneQ: run program stoneQ.bin\n\r" \
20  " \t in program, press Q to exit\n\r" \
21  " ls : list all file in root directory\n\r"\
22  " showinfo: print my id 16337269 in 3-D mod"
23 #define LOW_8_MASK (0b11111111)
24 #define GET_LOW_8BITS(X) (X & LOW_8_MASK)
25 #define GET_HIGH_8BITS(X) (X >> 8)
26 #include "../include/stdio.h"
27 #include "../include/graphic.h"
28 #define TERMINAL_STYLE (TO_FN(G_PINKISH))
29 extern void kb_interupt_install();
30 extern void kb_interupt_uninstall();
31 char CMD_BUFFER[BUFFER_SIZE + 10] = {};
32 void parseCMD(int );
33 static FAT_ITEM* CUR_DIR = 0;
34 void resetTerminal();
35 int terminal() {
36  if (CUR_DIR == 0) {
38  }
40  int offsetx = 0;
41  int offsety = 0;
42  int CMDindex = 0;
43 
44  while(1) {
45  int try = kbhit();
46  if (!try) continue;
47 
48  int key = readkb();
49  switch(key) {
50  case('\r'):
51  putch('\n');
52  putch('\r');
53 
54  offsetx = 0;
55  offsety++;
56 
57  parseCMD(CMDindex);
59 
60  CMDindex = 0;
61  CMD_BUFFER[CMDindex] = 0;
62 
63  break;
64  case(BACK_SPACE):
65  if (offsetx > 0) {
66  putch(8);
67  putch(' ');
68  putch(8);
69  offsetx--;
70 
71  CMDindex--;
72  CMD_BUFFER[CMDindex] = 0;
73  }
74  break;
75  case('\t'):
76  break;
77  default:
78  putch(key);
79  offsetx++;
80  CMD_BUFFER[CMDindex++] = key;
81  CMD_BUFFER[CMDindex] = 0;
82  }
83  }
84  return 0;
85 }
86 
87 void parseCMD(int CMDindex) {
88  if (CMDindex == 0) return;
89  if (strstr(CMD_BUFFER, "run") == 0 && strchr(CMD_BUFFER, ' ') == 3) {
90  int16_t pos = strchr(CMD_BUFFER, ' ');
91  const char* fn = CMD_BUFFER + pos + 1;
92  int16_t code = __load_program(fn);
93  int (*userProgram)() = (int (*)())(0x6c00);
94  switch(code) {
95  case ERR_SYS_PROTC:
96  putln("ERROR: system protect file");
97  break;
98  case ERR_TYPE_FLDR:
99  putln("ERROR: folder not executable");
100  break;
101  case ERR_TYPE_DOC:
102  putln("ERROR: partition info protect");
103  break;
104  case ERR_NOT_FOUND:
105  putln("ERROR: file not found");
106  break;
107  case NO_ERR:
109  userProgram();
111  resetTerminal();
112  break;
113  }
114  } else if (strcmp(CMD_BUFFER, "help") == 0) {
115  putln(HELP_MSG);
116  } else if (strcmp(CMD_BUFFER, "ls") == 0) {
117  printf("%10s|%20s|%10s\n", "filename", "filesize(bytes)", "begin cluster");
118  FAT_ITEM* pfat = CUR_DIR;
119  if (__FAT_showable_item(pfat)) {
120  printf("%10s|%20d|%10d\n", pfat->filename, pfat->filesize, pfat->blow_cluster);
121  // __print_file_cluster_list(pfat);
122  newline();
123  }
124  while (__has_next_item(pfat)) {
125  pfat = __next_item(pfat);
126  if (__FAT_showable_item(pfat)) {
127  printf("%10s|%20d|%10d\n", pfat->filename, pfat->filesize, pfat->blow_cluster);
128  // __print_file_cluster_list(pfat);
129  newline();
130  // putiln(pfat->blow_cluster);
131  // putiln(pfat->filesize);
132  }
133  }
134  }
135  else if (strcmp(CMD_BUFFER, "gc") == 0) {
136  uint16_t cursor_position = get_cursor();
137  uint16_t col = GET_LOW_8BITS(cursor_position);
138  uint16_t row = GET_HIGH_8BITS(cursor_position);
139  printf("cursor row is %d, col is %d\n", row, col);
140  }
141  else if (strcmp(CMD_BUFFER, "scu") == 0) {
142  uint16_t ocursor_position = get_cursor();
143  uint16_t ocol = GET_LOW_8BITS(ocursor_position);
144  uint16_t orow = GET_HIGH_8BITS(ocursor_position);
145  __screen_scroll(0, (24 << 8) + 79, 1, 0);
146  uint16_t ncursor_position = get_cursor();
147  uint16_t ncol = GET_LOW_8BITS(ncursor_position);
148  uint16_t nrow = GET_HIGH_8BITS(ncursor_position);
149  printf("old row %d col %d, new row %d col %d\n", orow, ocol, nrow, ncol);
150  } else if (strcmp(CMD_BUFFER, "showinfo") == 0) {
151  __asm__ volatile(
152  "pushw %%ax\n"
153  "movb $2, %%ah\n"
154  "int $0x2B\n"
155  "popw %%ax\n"
156  : /* no output */
157  : /* no input */
158  : "cc"
159  );
160  }
161  else {
162  puts("ybsh: command not found: ");
163  putln(CMD_BUFFER);
164  }
165 }
167  clear_screen();
168  set_cursor(1, 1);
169 }
static void __screen_scroll(uint16_t row_col_lu, uint16_t row_col_rd, uint8_t lines, uint16_t direction)
Definition: utilities.h:68
#define ERR_TYPE_DOC
target type is partition doc, unable
Definition: fsErrorCode.h:11
static int16_t strstr(const char *src, const char *tar)
Definition: mystring.h:22
#define PROMT
Definition: terminal.c:17
void kb_interupt_install()
static FAT_ITEM * __next_item(FAT_ITEM *p)
Definition: fsapi.h:62
int terminal()
Definition: terminal.c:35
#define ERR_NOT_FOUND
file not found
Definition: fsErrorCode.h:13
void clear_screen()
static int16_t strchr(const char *str, char chr)
Definition: mystring.h:12
#define ERR_TYPE_FLDR
target type is folder, unable to execute the operation
Definition: fsErrorCode.h:9
char CMD_BUFFER[BUFFER_SIZE+10]
Definition: terminal.c:31
int16_t row
Definition: kbhit.c:11
#define GET_LOW_8BITS(X)
Definition: terminal.c:24
void kb_interupt_uninstall()
static int16_t __load_program(const char *targetFilename)
Definition: fsapi.h:171
#define NO_ERR
Definition: fsErrorCode.h:5
static int16_t newline()
Definition: stdio.h:115
uint32_t filesize
in bytes
Definition: FATMacro.h:36
static int16_t putch(char ch)
Definition: stdio.h:62
void resetTerminal()
Definition: terminal.c:166
#define GET_HIGH_8BITS(X)
Definition: terminal.c:25
__asm__(".globl _start\)
#define TERMINAL_STYLE
Definition: terminal.c:28
void parseCMD(int)
Definition: terminal.c:87
#define BACK_SPACE
&#39;&#39; ascii
Definition: terminal.c:15
int strcmp(const char *lhs, const char *rhs)
Definition: mystring.c:2
int kbhit()
static uint16_t get_cursor()
Definition: utilities.h:45
#define HELP_MSG
Definition: terminal.c:18
static int16_t puts_style(char const *str, uint8_t style)
Definition: stdio.h:137
uint8_t filename[8]
Definition: FATMacro.h:22
#define ERR_SYS_PROTC
system protected file, not allow executing the operation
Definition: fsErrorCode.h:7
static void set_cursor(uint8_t row, uint8_t col)
Definition: utilities.h:26
static int printf(const char *format,...)
Definition: stdio.h:190
static FAT_ITEM * CUR_DIR
current directory pointer
Definition: terminal.c:33
static int16_t putln(char const *str)
Definition: stdio.h:149
static FAT_ITEM * __get_root_dir()
Definition: fsapi.h:35
uint16_t blow_cluster
Definition: FATMacro.h:35
int readkb()
#define BUFFER_SIZE
buffer size for parcing command
Definition: terminal.c:16
static int16_t __has_next_item(const FAT_ITEM *p)
Definition: fsapi.h:42
static int16_t puts(char const *str)
Definition: stdio.h:146
static int16_t __FAT_showable_item(const FAT_ITEM *p)
Definition: fsapi.h:95