Skip to content
This repository
Browse code

formatting; split kbd constants into kbd.h

  • Loading branch information...
commit 5e8c5d8bb8a54bca73ddc3e2ac0750b56bf96dcc 1 parent ea6e370
authored
248  console.c
@@ -7,6 +7,7 @@
7 7
 #include "param.h"
8 8
 #include "mmu.h"
9 9
 #include "proc.h"
  10
+#include "kbd.h"
10 11
 
11 12
 struct spinlock console_lock;
12 13
 int panicked = 0;
@@ -49,7 +50,6 @@ cons_putc(int c)
49 50
   ind |= inb(crtport + 1);
50 51
 
51 52
   c &= 0xff;
52  
-
53 53
   if(c == '\n'){
54 54
     ind -= (ind % 80);
55 55
     ind += 80;
@@ -101,48 +101,53 @@ printint(int xx, int base, int sgn)
101 101
 void
102 102
 cprintf(char *fmt, ...)
103 103
 {
104  
-  int i, state = 0, c, locking = 0;
105  
-  uint *ap = (uint*)(void*)&fmt + 1;
  104
+  int i, c, state, locking;
  105
+  uint *argp;
  106
+  char *s;
106 107
 
107  
-  if(use_console_lock){
108  
-    locking = 1;
  108
+  locking = use_console_lock;
  109
+  if(locking)
109 110
     acquire(&console_lock);
110  
-  }
111 111
 
  112
+  argp = (uint*)(void*)&fmt + 1;
  113
+  state = 0;
112 114
   for(i = 0; fmt[i]; i++){
113 115
     c = fmt[i] & 0xff;
114  
-    if(state == 0){
115  
-      if(c == '%'){
  116
+    switch(state){
  117
+    case 0:
  118
+      if(c == '%')
116 119
         state = '%';
117  
-      } else {
118  
-        cons_putc(c);
119  
-      }
120  
-    } else if(state == '%'){
121  
-      if(c == 'd'){
122  
-        printint(*ap, 10, 1);
123  
-        ap++;
124  
-      } else if(c == 'x' || c == 'p'){
125  
-        printint(*ap, 16, 0);
126  
-        ap++;
127  
-      } else if(c == 's'){
128  
-        char *s = (char*)*ap;
129  
-        ap++;
130  
-        if(s == 0){
131  
-          cons_putc('0');
132  
-        }else{
133  
-          while(*s != 0){
134  
-            cons_putc(*s);
135  
-            s++;
136  
-          }
137  
-        }
138  
-      } else if(c == '%'){
  120
+      else
139 121
         cons_putc(c);
140  
-      } else {
141  
-        // Unknown % sequence.  Print it to draw attention.
  122
+      break;
  123
+    
  124
+    case '%':
  125
+      switch(c){
  126
+      case 'd':
  127
+        printint(*argp++, 10, 1);
  128
+        break;
  129
+      case 'x':
  130
+      case 'p':
  131
+        printint(*argp++, 16, 0);
  132
+        break;
  133
+      case 's':
  134
+        s = (char*)*argp++;
  135
+        if(s == 0)
  136
+          s = "(null)";
  137
+        for(; *s; s++)
  138
+          cons_putc(*s);
  139
+        break;
  140
+      case '%':
  141
+        cons_putc('%');
  142
+        break;
  143
+      default:
  144
+        // Print unknown % sequence to draw attention.
142 145
         cons_putc('%');
143 146
         cons_putc(c);
  147
+        break;
144 148
       }
145 149
       state = 0;
  150
+      break;
146 151
     }
147 152
   }
148 153
 
@@ -175,157 +180,31 @@ console_write(int minor, char *buf, int n)
175 180
   int i;
176 181
 
177 182
   acquire(&console_lock);
178  
-
179  
-  for(i = 0; i < n; i++) {
  183
+  for(i = 0; i < n; i++)
180 184
     cons_putc(buf[i] & 0xff);
181  
-  }
182  
-
183 185
   release(&console_lock);
184 186
 
185 187
   return n;
186 188
 }
187 189
 
188  
-
189  
-#define KBSTATP         0x64    // kbd controller status port(I)
190  
-#define KBS_DIB         0x01    // kbd data in buffer
191  
-#define KBDATAP         0x60    // kbd data port(I)
192  
-
193  
-#define NO              0
194  
-
195  
-#define SHIFT           (1<<0)
196  
-#define CTL             (1<<1)
197  
-#define ALT             (1<<2)
198  
-
199  
-#define CAPSLOCK        (1<<3)
200  
-#define NUMLOCK         (1<<4)
201  
-#define SCROLLLOCK      (1<<5)
202  
-
203  
-#define E0ESC           (1<<6)
204  
-
205  
-// Special keycodes
206  
-#define KEY_HOME        0xE0
207  
-#define KEY_END         0xE1
208  
-#define KEY_UP          0xE2
209  
-#define KEY_DN          0xE3
210  
-#define KEY_LF          0xE4
211  
-#define KEY_RT          0xE5
212  
-#define KEY_PGUP        0xE6
213  
-#define KEY_PGDN        0xE7
214  
-#define KEY_INS         0xE8
215  
-#define KEY_DEL         0xE9
216  
-
217  
-static uchar shiftcode[256] =
218  
-{
219  
-  [0x1D] CTL,
220  
-  [0x2A] SHIFT,
221  
-  [0x36] SHIFT,
222  
-  [0x38] ALT,
223  
-  [0x9D] CTL,
224  
-  [0xB8] ALT
225  
-};
226  
-
227  
-static uchar togglecode[256] =
228  
-{
229  
-  [0x3A] CAPSLOCK,
230  
-  [0x45] NUMLOCK,
231  
-  [0x46] SCROLLLOCK
232  
-};
233  
-
234  
-static uchar normalmap[256] =
235  
-{
236  
-  NO,   0x1B, '1',  '2',  '3',  '4',  '5',  '6',  // 0x00
237  
-  '7',  '8',  '9',  '0',  '-',  '=',  '\b', '\t',
238  
-  'q',  'w',  'e',  'r',  't',  'y',  'u',  'i',  // 0x10
239  
-  'o',  'p',  '[',  ']',  '\n', NO,   'a',  's',
240  
-  'd',  'f',  'g',  'h',  'j',  'k',  'l',  ';',  // 0x20
241  
-  '\'', '`',  NO,   '\\', 'z',  'x',  'c',  'v',
242  
-  'b',  'n',  'm',  ',',  '.',  '/',  NO,   '*',  // 0x30
243  
-  NO,   ' ',  NO,   NO,   NO,   NO,   NO,   NO,
244  
-  NO,   NO,   NO,   NO,   NO,   NO,   NO,   '7',  // 0x40
245  
-  '8',  '9',  '-',  '4',  '5',  '6',  '+',  '1',
246  
-  '2',  '3',  '0',  '.',  NO,   NO,   NO,   NO,   // 0x50
247  
-  [0x97] KEY_HOME,
248  
-  [0x9C] '\n',      // KP_Enter
249  
-  [0xB5] '/',       // KP_Div
250  
-  [0xC8] KEY_UP,
251  
-  [0xC9] KEY_PGUP,
252  
-  [0xCB] KEY_LF,
253  
-  [0xCD] KEY_RT,
254  
-  [0xCF] KEY_END,
255  
-  [0xD0] KEY_DN,
256  
-  [0xD1] KEY_PGDN,
257  
-  [0xD2] KEY_INS,
258  
-  [0xD3] KEY_DEL
259  
-};
260  
-
261  
-static uchar shiftmap[256] =
262  
-{
263  
-  NO,   033,  '!',  '@',  '#',  '$',  '%',  '^',  // 0x00
264  
-  '&',  '*',  '(',  ')',  '_',  '+',  '\b', '\t',
265  
-  'Q',  'W',  'E',  'R',  'T',  'Y',  'U',  'I',  // 0x10
266  
-  'O',  'P',  '{',  '}',  '\n', NO,   'A',  'S',
267  
-  'D',  'F',  'G',  'H',  'J',  'K',  'L',  ':',  // 0x20
268  
-  '"',  '~',  NO,   '|',  'Z',  'X',  'C',  'V',
269  
-  'B',  'N',  'M',  '<',  '>',  '?',  NO,   '*',  // 0x30
270  
-  NO,   ' ',  NO,   NO,   NO,   NO,   NO,   NO,
271  
-  NO,   NO,   NO,   NO,   NO,   NO,   NO,   '7',  // 0x40
272  
-  '8',  '9',  '-',  '4',  '5',  '6',  '+',  '1',
273  
-  '2',  '3',  '0',  '.',  NO,   NO,   NO,   NO,   // 0x50
274  
-  [0x97] KEY_HOME,
275  
-  [0x9C] '\n',      // KP_Enter
276  
-  [0xB5] '/',       // KP_Div
277  
-  [0xC8] KEY_UP,
278  
-  [0xC9] KEY_PGUP,
279  
-  [0xCB] KEY_LF,
280  
-  [0xCD] KEY_RT,
281  
-  [0xCF] KEY_END,
282  
-  [0xD0] KEY_DN,
283  
-  [0xD1] KEY_PGDN,
284  
-  [0xD2] KEY_INS,
285  
-  [0xD3] KEY_DEL
286  
-};
287  
-
288  
-#define C(x) (x - '@')
289  
-
290  
-static uchar ctlmap[256] =
291  
-{
292  
-  NO,      NO,      NO,      NO,      NO,      NO,      NO,      NO,
293  
-  NO,      NO,      NO,      NO,      NO,      NO,      NO,      NO,
294  
-  C('Q'),  C('W'),  C('E'),  C('R'),  C('T'),  C('Y'),  C('U'),  C('I'),
295  
-  C('O'),  C('P'),  NO,      NO,      '\r',    NO,      C('A'),  C('S'),
296  
-  C('D'),  C('F'),  C('G'),  C('H'),  C('J'),  C('K'),  C('L'),  NO,
297  
-  NO,      NO,      NO,      C('\\'), C('Z'),  C('X'),  C('C'),  C('V'),
298  
-  C('B'),  C('N'),  C('M'),  NO,      NO,      C('/'),  NO,      NO,
299  
-  [0x97] KEY_HOME,
300  
-  [0xB5] C('/'),    // KP_Div
301  
-  [0xC8] KEY_UP,
302  
-  [0xC9] KEY_PGUP,
303  
-  [0xCB] KEY_LF,
304  
-  [0xCD] KEY_RT,
305  
-  [0xCF] KEY_END,
306  
-  [0xD0] KEY_DN,
307  
-  [0xD1] KEY_PGDN,
308  
-  [0xD2] KEY_INS,
309  
-  [0xD3] KEY_DEL
310  
-};
311  
-
312  
-static uchar *charcode[4] = {
313  
-  normalmap,
314  
-  shiftmap,
315  
-  ctlmap,
316  
-  ctlmap
317  
-};
318  
-
319 190
 #define KBD_BUF 64
320  
-uchar kbd_buf[KBD_BUF];
321  
-int kbd_r;
322  
-int kbd_w;
323  
-struct spinlock kbd_lock;
324  
-static uint shift;
  191
+struct {
  192
+  uchar buf[KBD_BUF];
  193
+  int r;
  194
+  int w;
  195
+  struct spinlock lock;
  196
+} kbd;
325 197
 
326 198
 void
327 199
 kbd_intr(void)
328 200
 {
  201
+  static uint shift;
  202
+  static uchar *charcode[4] = {
  203
+    normalmap,
  204
+    shiftmap,
  205
+    ctlmap,
  206
+    ctlmap
  207
+  };
329 208
   uint st, data, c;
330 209
 
331 210
   acquire(&kbd_lock);
@@ -387,6 +266,7 @@ kbd_intr(void)
387 266
   release(&kbd_lock);
388 267
 }
389 268
 
  269
+//PAGEBREAK: 25
390 270
 int
391 271
 console_read(int minor, char *dst, int n)
392 272
 {
@@ -394,31 +274,31 @@ console_read(int minor, char *dst, int n)
394 274
   int c;
395 275
 
396 276
   target = n;
397  
-  acquire(&kbd_lock);
  277
+  acquire(&kbd.lock);
398 278
   while(n > 0){
399  
-    while(kbd_r == kbd_w){
  279
+    while(kbd.r == kbd.w){
400 280
       if(cp->killed){
401  
-        release(&kbd_lock);
  281
+        release(&kbd.lock);
402 282
         return -1;
403 283
       }
404  
-      sleep(&kbd_r, &kbd_lock);
  284
+      sleep(&kbd.r, &kbd.lock);
405 285
     }
406  
-    c = kbd_buf[kbd_r++];
  286
+    c = kbd.buf[kbd.r++];
407 287
     if(c == C('D')){  // EOF
408 288
       if(n < target){
409 289
         // Save ^D for next time, to make sure
410 290
         // caller gets a 0-byte result.
411  
-        kbd_r--;
  291
+        kbd.r--;
412 292
       }
413 293
       break;
414 294
     }
415 295
     *dst++ = c;
416 296
     cons_putc(c);
417 297
     --n;
418  
-    if(kbd_r >= KBD_BUF)
419  
-      kbd_r = 0;
  298
+    if(kbd.r >= KBD_BUF)
  299
+      kbd.r = 0;
420 300
   }
421  
-  release(&kbd_lock);
  301
+  release(&kbd.lock);
422 302
 
423 303
   return target - n;
424 304
 }
@@ -427,13 +307,13 @@ void
427 307
 console_init(void)
428 308
 {
429 309
   initlock(&console_lock, "console");
430  
-  initlock(&kbd_lock, "kbd");
  310
+  initlock(&kbd.lock, "kbd");
431 311
 
432 312
   devsw[CONSOLE].write = console_write;
433 313
   devsw[CONSOLE].read = console_read;
  314
+  use_console_lock = 1;
434 315
 
435 316
   irq_enable(IRQ_KBD);
436 317
   ioapic_enable(IRQ_KBD, 0);
437  
-
438  
-  use_console_lock = 1;
439 318
 }
  319
+
112  kbd.h
... ...
@@ -0,0 +1,112 @@
  1
+// PC keyboard interface constants
  2
+
  3
+#define KBSTATP         0x64    // kbd controller status port(I)
  4
+#define KBS_DIB         0x01    // kbd data in buffer
  5
+#define KBDATAP         0x60    // kbd data port(I)
  6
+
  7
+#define NO              0
  8
+
  9
+#define SHIFT           (1<<0)
  10
+#define CTL             (1<<1)
  11
+#define ALT             (1<<2)
  12
+
  13
+#define CAPSLOCK        (1<<3)
  14
+#define NUMLOCK         (1<<4)
  15
+#define SCROLLLOCK      (1<<5)
  16
+
  17
+#define E0ESC           (1<<6)
  18
+
  19
+// Special keycodes
  20
+#define KEY_HOME        0xE0
  21
+#define KEY_END         0xE1
  22
+#define KEY_UP          0xE2
  23
+#define KEY_DN          0xE3
  24
+#define KEY_LF          0xE4
  25
+#define KEY_RT          0xE5
  26
+#define KEY_PGUP        0xE6
  27
+#define KEY_PGDN        0xE7
  28
+#define KEY_INS         0xE8
  29
+#define KEY_DEL         0xE9
  30
+
  31
+// C('A') == Control-A
  32
+#define C(x) (x - '@')
  33
+
  34
+static uchar shiftcode[256] =
  35
+{
  36
+  [0x1D] CTL,
  37
+  [0x2A] SHIFT,
  38
+  [0x36] SHIFT,
  39
+  [0x38] ALT,
  40
+  [0x9D] CTL,
  41
+  [0xB8] ALT
  42
+};
  43
+
  44
+static uchar togglecode[256] =
  45
+{
  46
+  [0x3A] CAPSLOCK,
  47
+  [0x45] NUMLOCK,
  48
+  [0x46] SCROLLLOCK
  49
+};
  50
+
  51
+static uchar normalmap[256] =
  52
+{
  53
+  NO,   0x1B, '1',  '2',  '3',  '4',  '5',  '6',  // 0x00
  54
+  '7',  '8',  '9',  '0',  '-',  '=',  '\b', '\t',
  55
+  'q',  'w',  'e',  'r',  't',  'y',  'u',  'i',  // 0x10
  56
+  'o',  'p',  '[',  ']',  '\n', NO,   'a',  's',
  57
+  'd',  'f',  'g',  'h',  'j',  'k',  'l',  ';',  // 0x20
  58
+  '\'', '`',  NO,   '\\', 'z',  'x',  'c',  'v',
  59
+  'b',  'n',  'm',  ',',  '.',  '/',  NO,   '*',  // 0x30
  60
+  NO,   ' ',  NO,   NO,   NO,   NO,   NO,   NO,
  61
+  NO,   NO,   NO,   NO,   NO,   NO,   NO,   '7',  // 0x40
  62
+  '8',  '9',  '-',  '4',  '5',  '6',  '+',  '1',
  63
+  '2',  '3',  '0',  '.',  NO,   NO,   NO,   NO,   // 0x50
  64
+  [0x9C] '\n',      // KP_Enter
  65
+  [0xB5] '/',       // KP_Div
  66
+  [0xC8] KEY_UP,    [0xD0] KEY_DN,
  67
+  [0xC9] KEY_PGUP,  [0xD1] KEY_PGDN,
  68
+  [0xCB] KEY_LF,    [0xCD] KEY_RT,
  69
+  [0x97] KEY_HOME,  [0xCF] KEY_END,
  70
+  [0xD2] KEY_INS,   [0xD3] KEY_DEL
  71
+};
  72
+
  73
+static uchar shiftmap[256] =
  74
+{
  75
+  NO,   033,  '!',  '@',  '#',  '$',  '%',  '^',  // 0x00
  76
+  '&',  '*',  '(',  ')',  '_',  '+',  '\b', '\t',
  77
+  'Q',  'W',  'E',  'R',  'T',  'Y',  'U',  'I',  // 0x10
  78
+  'O',  'P',  '{',  '}',  '\n', NO,   'A',  'S',
  79
+  'D',  'F',  'G',  'H',  'J',  'K',  'L',  ':',  // 0x20
  80
+  '"',  '~',  NO,   '|',  'Z',  'X',  'C',  'V',
  81
+  'B',  'N',  'M',  '<',  '>',  '?',  NO,   '*',  // 0x30
  82
+  NO,   ' ',  NO,   NO,   NO,   NO,   NO,   NO,
  83
+  NO,   NO,   NO,   NO,   NO,   NO,   NO,   '7',  // 0x40
  84
+  '8',  '9',  '-',  '4',  '5',  '6',  '+',  '1',
  85
+  '2',  '3',  '0',  '.',  NO,   NO,   NO,   NO,   // 0x50
  86
+  [0x9C] '\n',      // KP_Enter
  87
+  [0xB5] '/',       // KP_Div
  88
+  [0xC8] KEY_UP,    [0xD0] KEY_DN,
  89
+  [0xC9] KEY_PGUP,  [0xD1] KEY_PGDN,
  90
+  [0xCB] KEY_LF,    [0xCD] KEY_RT,
  91
+  [0x97] KEY_HOME,  [0xCF] KEY_END,
  92
+  [0xD2] KEY_INS,   [0xD3] KEY_DEL
  93
+};
  94
+
  95
+static uchar ctlmap[256] =
  96
+{
  97
+  NO,      NO,      NO,      NO,      NO,      NO,      NO,      NO,
  98
+  NO,      NO,      NO,      NO,      NO,      NO,      NO,      NO,
  99
+  C('Q'),  C('W'),  C('E'),  C('R'),  C('T'),  C('Y'),  C('U'),  C('I'),
  100
+  C('O'),  C('P'),  NO,      NO,      '\r',    NO,      C('A'),  C('S'),
  101
+  C('D'),  C('F'),  C('G'),  C('H'),  C('J'),  C('K'),  C('L'),  NO,
  102
+  NO,      NO,      NO,      C('\\'), C('Z'),  C('X'),  C('C'),  C('V'),
  103
+  C('B'),  C('N'),  C('M'),  NO,      NO,      C('/'),  NO,      NO,
  104
+  [0x9C] '\r',      // KP_Enter
  105
+  [0xB5] C('/'),    // KP_Div
  106
+  [0xC8] KEY_UP,    [0xD0] KEY_DN,
  107
+  [0xC9] KEY_PGUP,  [0xD1] KEY_PGDN,
  108
+  [0xCB] KEY_LF,    [0xCD] KEY_RT,
  109
+  [0x97] KEY_HOME,  [0xCF] KEY_END,
  110
+  [0xD2] KEY_INS,   [0xD3] KEY_DEL
  111
+};
  112
+
1  runoff.list
@@ -60,5 +60,6 @@ ioapic.h
60 60
 lapic.c
61 61
 ioapic.c
62 62
 picirq.c
  63
+kbd.h
63 64
 console.c
64 65
 8253pit.c

0 notes on commit 5e8c5d8

Please sign in to comment.
Something went wrong with that request. Please try again.