This source file includes following definitions.
- gotoxy
- __set_origin
- scrollback
- scrollfront
- set_origin
- hide_cursor
- set_cursor
- scrup
- scrdown
- lf
- ri
- cr
- bs
- del
- csi_J
- csi_K
- update_attr
- default_attr
- csi_m
- respond_string
- respond_num
- cursor_report
- status_report
- respond_ID
- invert_screen
- set_mode
- setterm_command
- insert_char
- insert_line
- delete_char
- delete_line
- csi_at
- csi_L
- csi_P
- csi_M
- save_cur
- restore_cur
- reset_terminal
- con_write
- do_keyboard_interrupt
- memsetw
- console_print
- con_init
- kbdsave
- get_scrmem
- set_scrmem
- blank_screen
- unblank_screen
- update_screen
- do_screendump
- con_open
- highlight
- inword
- atedge
- limit
- set_selection
- paste_selection
- clear_selection
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 #include <linux/sched.h>
41 #include <linux/timer.h>
42 #include <linux/tty.h>
43 #include <linux/config.h>
44 #include <linux/kernel.h>
45 #include <linux/string.h>
46 #include <linux/errno.h>
47 #include <linux/kd.h>
48 #include <linux/keyboard.h>
49
50 #include <asm/io.h>
51 #include <asm/system.h>
52 #include <asm/segment.h>
53
54 #include "vt_kern.h"
55
56 #ifdef CONFIG_SELECTION
57 #include <linux/ctype.h>
58
59
60 int set_selection(const int arg);
61 int paste_selection(struct tty_struct *tty);
62 static void clear_selection(void);
63
64
65 #define SEL_BUFFER_SIZE TTY_BUF_SIZE
66 static int sel_cons;
67 static int sel_start = -1;
68 static int sel_end;
69 static char sel_buffer[SEL_BUFFER_SIZE] = { '\0' };
70 #endif
71
72 #define NPAR 16
73
74 extern void vt_init(void);
75 extern void register_console(void (*proc)(const char *));
76
77 unsigned long video_num_columns;
78 unsigned long video_num_lines;
79
80 static unsigned char video_type;
81 static unsigned long video_mem_base;
82 static unsigned long video_mem_term;
83 static unsigned long video_size_row;
84 static unsigned char video_page;
85 static unsigned short video_port_reg;
86 static unsigned short video_port_val;
87 static int can_do_color = 0;
88 static int printable = 0;
89
90 static struct {
91 unsigned short vc_video_erase_char;
92 unsigned char vc_attr;
93 unsigned char vc_def_color;
94 unsigned char vc_color;
95 unsigned char vc_s_color;
96 unsigned char vc_ulcolor;
97 unsigned char vc_halfcolor;
98 unsigned long vc_origin;
99 unsigned long vc_scr_end;
100 unsigned long vc_pos;
101 unsigned long vc_x,vc_y;
102 unsigned long vc_top,vc_bottom;
103 unsigned long vc_state;
104 unsigned long vc_npar,vc_par[NPAR];
105 unsigned long vc_video_mem_start;
106 unsigned long vc_video_mem_end;
107 unsigned long vc_saved_x;
108 unsigned long vc_saved_y;
109
110 unsigned long vc_charset : 1;
111 unsigned long vc_s_charset : 1;
112 unsigned long vc_decscnm : 1;
113 unsigned long vc_decom : 1;
114 unsigned long vc_decawm : 1;
115 unsigned long vc_deccm : 1;
116 unsigned long vc_decim : 1;
117
118 unsigned long vc_intensity : 2;
119 unsigned long vc_underline : 1;
120 unsigned long vc_blink : 1;
121 unsigned long vc_reverse : 1;
122 unsigned long vc_s_intensity : 2;
123 unsigned long vc_s_underline : 1;
124 unsigned long vc_s_blink : 1;
125 unsigned long vc_s_reverse : 1;
126
127 unsigned long vc_ques : 1;
128 unsigned long vc_need_wrap : 1;
129 unsigned long vc_tab_stop[5];
130 unsigned char vc_kbdmode;
131 unsigned char * vc_translate;
132 unsigned char * vc_G0_charset;
133 unsigned char * vc_G1_charset;
134 unsigned char * vc_saved_G0;
135 unsigned char * vc_saved_G1;
136
137 } vc_cons [NR_CONSOLES];
138
139 unsigned short *vc_scrbuf[NR_CONSOLES];
140 static unsigned short * vc_scrmembuf;
141 static int console_blanked = 0;
142
143 #define origin (vc_cons[currcons].vc_origin)
144 #define scr_end (vc_cons[currcons].vc_scr_end)
145 #define pos (vc_cons[currcons].vc_pos)
146 #define top (vc_cons[currcons].vc_top)
147 #define bottom (vc_cons[currcons].vc_bottom)
148 #define x (vc_cons[currcons].vc_x)
149 #define y (vc_cons[currcons].vc_y)
150 #define state (vc_cons[currcons].vc_state)
151 #define npar (vc_cons[currcons].vc_npar)
152 #define par (vc_cons[currcons].vc_par)
153 #define ques (vc_cons[currcons].vc_ques)
154 #define attr (vc_cons[currcons].vc_attr)
155 #define saved_x (vc_cons[currcons].vc_saved_x)
156 #define saved_y (vc_cons[currcons].vc_saved_y)
157 #define translate (vc_cons[currcons].vc_translate)
158 #define G0_charset (vc_cons[currcons].vc_G0_charset)
159 #define G1_charset (vc_cons[currcons].vc_G1_charset)
160 #define saved_G0 (vc_cons[currcons].vc_saved_G0)
161 #define saved_G1 (vc_cons[currcons].vc_saved_G1)
162 #define video_mem_start (vc_cons[currcons].vc_video_mem_start)
163 #define video_mem_end (vc_cons[currcons].vc_video_mem_end)
164 #define video_erase_char (vc_cons[currcons].vc_video_erase_char)
165 #define decscnm (vc_cons[currcons].vc_decscnm)
166 #define decom (vc_cons[currcons].vc_decom)
167 #define decawm (vc_cons[currcons].vc_decawm)
168 #define deccm (vc_cons[currcons].vc_deccm)
169 #define decim (vc_cons[currcons].vc_decim)
170 #define need_wrap (vc_cons[currcons].vc_need_wrap)
171 #define color (vc_cons[currcons].vc_color)
172 #define s_color (vc_cons[currcons].vc_s_color)
173 #define def_color (vc_cons[currcons].vc_def_color)
174 #define foreground (color & 0x0f)
175 #define background (color & 0xf0)
176 #define charset (vc_cons[currcons].vc_charset)
177 #define s_charset (vc_cons[currcons].vc_s_charset)
178 #define intensity (vc_cons[currcons].vc_intensity)
179 #define underline (vc_cons[currcons].vc_underline)
180 #define blink (vc_cons[currcons].vc_blink)
181 #define reverse (vc_cons[currcons].vc_reverse)
182 #define s_intensity (vc_cons[currcons].vc_s_intensity)
183 #define s_underline (vc_cons[currcons].vc_s_underline)
184 #define s_blink (vc_cons[currcons].vc_s_blink)
185 #define s_reverse (vc_cons[currcons].vc_s_reverse)
186 #define ulcolor (vc_cons[currcons].vc_ulcolor)
187 #define halfcolor (vc_cons[currcons].vc_halfcolor)
188 #define kbdmode (vc_cons[currcons].vc_kbdmode)
189 #define tab_stop (vc_cons[currcons].vc_tab_stop)
190 #define vcmode (vt_cons[currcons].vc_mode)
191 #define vtmode (vt_cons[currcons].vt_mode)
192 #define vtpid (vt_cons[currcons].vt_pid)
193 #define vtnewvt (vt_cons[currcons].vt_newvt)
194
195 #define set_kbd(x) set_vc_kbd_flag(kbd_table+currcons,x)
196 #define clr_kbd(x) clr_vc_kbd_flag(kbd_table+currcons,x)
197 #define is_kbd(x) vc_kbd_flag(kbd_table+currcons,x)
198
199 #define decarm VC_REPEAT
200 #define decckm VC_CKMODE
201 #define kbdapplic VC_APPLIC
202 #define kbdraw VC_RAW
203 #define lnm VC_CRLF
204
205 int blankinterval = 10*60*HZ;
206 static int screen_size = 0;
207
208
209
210
211 #define VT100ID "\033[?1;2c"
212 #define VT102ID "\033[?6c"
213
214 static unsigned char * translations[] = {
215
216 (unsigned char *)
217 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
218 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
219 " !\"#$%&'()*+,-./0123456789:;<=>?"
220 "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
221 "`abcdefghijklmnopqrstuvwxyz{|}~\0"
222 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
223 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
224 "\040\255\233\234\376\235\174\025\376\376\246\256\252\055\376\376"
225 "\370\361\375\376\376\346\024\371\376\376\247\257\254\253\376\250"
226 "\376\376\376\376\216\217\222\200\376\220\376\376\376\376\376\376"
227 "\376\245\376\376\376\376\231\376\235\376\376\376\232\376\376\341"
228 "\205\240\203\376\204\206\221\207\212\202\210\211\215\241\214\213"
229 "\376\244\225\242\223\376\224\366\233\227\243\226\201\376\376\230",
230
231 (unsigned char *)
232 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
233 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
234 " !\"#$%&'()*+,-./0123456789:;<=>?"
235 "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^ "
236 "\004\261\007\007\007\007\370\361\040\007\331\277\332\300\305\007"
237 "\007\304\007\007\303\264\301\302\263\007\007\007\007\007\234\0"
238 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
239 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
240 "\040\255\233\234\376\235\174\025\376\376\246\256\252\055\376\376"
241 "\370\361\375\376\376\346\024\371\376\376\247\257\254\253\376\250"
242 "\376\376\376\376\216\217\222\200\376\220\376\376\376\376\376\376"
243 "\376\245\376\376\376\376\231\376\376\376\376\376\232\376\376\341"
244 "\205\240\203\376\204\206\221\207\212\202\210\211\215\241\214\213"
245 "\376\244\225\242\223\376\224\366\376\227\243\226\201\376\376\230",
246
247 (unsigned char *)
248 "\000\001\002\003\004\005\006\007\000\011\000\013\000\000\000\000"
249 "\020\021\022\023\024\025\026\027\030\031\032\000\034\035\036\037"
250 "\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057"
251 "\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077"
252 "\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117"
253 "\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137"
254 "\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157"
255 "\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177"
256 "\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217"
257 "\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237"
258 "\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257"
259 "\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277"
260 "\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317"
261 "\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337"
262 "\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357"
263 "\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377"
264 };
265
266 #define NORM_TRANS (translations[0])
267 #define GRAF_TRANS (translations[1])
268 #define NULL_TRANS (translations[2])
269
270 static unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
271 8,12,10,14, 9,13,11,15 };
272
273
274
275
276
277
278 static void gotoxy(int currcons, int new_x, int new_y)
279 {
280 int max_y;
281
282 if (new_x < 0)
283 x = 0;
284 else
285 if (new_x >= video_num_columns)
286 x = video_num_columns - 1;
287 else
288 x = new_x;
289 if (decom) {
290 new_y += top;
291 max_y = bottom;
292 } else
293 max_y = video_num_lines;
294 if (new_y < 0)
295 y = 0;
296 else
297 if (new_y >= max_y)
298 y = max_y - 1;
299 else
300 y = new_y;
301 pos = origin + y*video_size_row + (x<<1);
302 need_wrap = 0;
303 }
304
305
306
307
308 static unsigned short __real_origin;
309 static unsigned short __origin;
310
311 static inline void __set_origin(unsigned short offset)
312 {
313 unsigned long flags;
314 #ifdef CONFIG_SELECTION
315 clear_selection();
316 #endif
317 save_flags(flags); cli();
318 __origin = offset;
319 outb_p(12, video_port_reg);
320 outb_p(offset >> 8, video_port_val);
321 outb_p(13, video_port_reg);
322 outb_p(offset, video_port_val);
323 restore_flags(flags);
324 }
325
326 void scrollback(int lines)
327 {
328 if (!lines)
329 lines = video_num_lines/2;
330 lines *= video_num_columns;
331 lines = __origin - lines;
332 if (lines < 0)
333 lines = 0;
334 __set_origin(lines);
335 }
336
337 void scrollfront(int lines)
338 {
339 if (!lines)
340 lines = video_num_lines/2;
341 lines *= video_num_columns;
342 lines = __origin + lines;
343 if (lines > __real_origin)
344 lines = __real_origin;
345 __set_origin(lines);
346 }
347
348 static void set_origin(int currcons)
349 {
350 if (video_type != VIDEO_TYPE_EGAC && video_type != VIDEO_TYPE_EGAM)
351 return;
352 if (currcons != fg_console || console_blanked || vcmode == KD_GRAPHICS)
353 return;
354 __real_origin = (origin-video_mem_base) >> 1;
355 __set_origin(__real_origin);
356 }
357
358 static inline void hide_cursor(int currcons)
359 {
360 outb_p(14, video_port_reg);
361 outb_p(0xff&((scr_end-video_mem_base)>>9), video_port_val);
362 outb_p(15, video_port_reg);
363 outb_p(0xff&((scr_end-video_mem_base)>>1), video_port_val);
364 }
365
366 static inline void set_cursor(int currcons)
367 {
368 unsigned long flags;
369
370 if (currcons != fg_console || console_blanked || vcmode == KD_GRAPHICS)
371 return;
372 if (__real_origin != __origin)
373 set_origin(__real_origin);
374 save_flags(flags); cli();
375 if (deccm) {
376 outb_p(14, video_port_reg);
377 outb_p(0xff&((pos-video_mem_base)>>9), video_port_val);
378 outb_p(15, video_port_reg);
379 outb_p(0xff&((pos-video_mem_base)>>1), video_port_val);
380 } else
381 hide_cursor(currcons);
382 restore_flags(flags);
383 }
384
385 static void scrup(int currcons, unsigned int t, unsigned int b)
386 {
387 int hardscroll = 1;
388
389 if (b > video_num_lines || t >= b)
390 return;
391 if (video_type != VIDEO_TYPE_EGAC && video_type != VIDEO_TYPE_EGAM)
392 hardscroll = 0;
393 else if (t || b != video_num_lines)
394 hardscroll = 0;
395 if (hardscroll) {
396 origin += video_size_row;
397 pos += video_size_row;
398 scr_end += video_size_row;
399 if (scr_end > video_mem_end) {
400 __asm__("cld\n\t"
401 "rep\n\t"
402 "movsl\n\t"
403 "movl _video_num_columns,%1\n\t"
404 "rep\n\t"
405 "stosw"
406 :
407 :"a" (video_erase_char),
408 "c" ((video_num_lines-1)*video_num_columns>>1),
409 "D" (video_mem_start),
410 "S" (origin)
411 :"cx","di","si");
412 scr_end -= origin-video_mem_start;
413 pos -= origin-video_mem_start;
414 origin = video_mem_start;
415 } else {
416 __asm__("cld\n\t"
417 "rep\n\t"
418 "stosw"
419 :
420 :"a" (video_erase_char),
421 "c" (video_num_columns),
422 "D" (scr_end-video_size_row)
423 :"cx","di");
424 }
425 set_origin(currcons);
426 } else {
427 __asm__("cld\n\t"
428 "rep\n\t"
429 "movsl\n\t"
430 "movl _video_num_columns,%%ecx\n\t"
431 "rep\n\t"
432 "stosw"
433 :
434 :"a" (video_erase_char),
435 "c" ((b-t-1)*video_num_columns>>1),
436 "D" (origin+video_size_row*t),
437 "S" (origin+video_size_row*(t+1))
438 :"cx","di","si");
439 }
440 }
441
442 static void scrdown(int currcons, unsigned int t, unsigned int b)
443 {
444 if (b > video_num_lines || t >= b)
445 return;
446 __asm__("std\n\t"
447 "rep\n\t"
448 "movsl\n\t"
449 "addl $2,%%edi\n\t"
450 "movl _video_num_columns,%%ecx\n\t"
451 "rep\n\t"
452 "stosw\n\t"
453 "cld"
454 :
455 :"a" (video_erase_char),
456 "c" ((b-t-1)*video_num_columns>>1),
457 "D" (origin+video_size_row*b-4),
458 "S" (origin+video_size_row*(b-1)-4)
459 :"ax","cx","di","si");
460 }
461
462 static void lf(int currcons)
463 {
464 if (y+1<bottom) {
465 y++;
466 pos += video_size_row;
467 return;
468 } else
469 scrup(currcons,top,bottom);
470 need_wrap = 0;
471 }
472
473 static void ri(int currcons)
474 {
475 if (y>top) {
476 y--;
477 pos -= video_size_row;
478 return;
479 } else
480 scrdown(currcons,top,bottom);
481 need_wrap = 0;
482 }
483
484 static inline void cr(int currcons)
485 {
486 pos -= x<<1;
487 need_wrap = x = 0;
488 }
489
490 static inline void bs(int currcons)
491 {
492 if (x) {
493 pos -= 2;
494 x--;
495 need_wrap = 0;
496 }
497 }
498
499 static inline void del(int currcons)
500 {
501 if (x) {
502 pos -= 2;
503 x--;
504 *(unsigned short *)pos = video_erase_char;
505 need_wrap = 0;
506 }
507 }
508
509 static void csi_J(int currcons, int vpar)
510 {
511 unsigned long count;
512 unsigned long start;
513
514 switch (vpar) {
515 case 0:
516 count = (scr_end-pos)>>1;
517 start = pos;
518 break;
519 case 1:
520 count = ((pos-origin)>>1)+1;
521 start = origin;
522 break;
523 case 2:
524 count = video_num_columns * video_num_lines;
525 start = origin;
526 break;
527 default:
528 return;
529 }
530 __asm__("cld\n\t"
531 "rep\n\t"
532 "stosw\n\t"
533 :
534 :"c" (count),
535 "D" (start),"a" (video_erase_char)
536 :"cx","di");
537 need_wrap = 0;
538 }
539
540 static void csi_K(int currcons, int vpar)
541 {
542 long count;
543 long start;
544
545 switch (vpar) {
546 case 0:
547 count = video_num_columns-x;
548 start = pos;
549 break;
550 case 1:
551 start = pos - (x<<1);
552 count = x+1;
553 break;
554 case 2:
555 start = pos - (x<<1);
556 count = video_num_columns;
557 break;
558 default:
559 return;
560 }
561 __asm__("cld\n\t"
562 "rep\n\t"
563 "stosw\n\t"
564 :
565 :"c" (count),
566 "D" (start),"a" (video_erase_char)
567 :"cx","di");
568 need_wrap = 0;
569 }
570
571
572
573
574 static void update_attr(int currcons)
575 {
576 attr = color;
577 if (can_do_color) {
578 if (underline)
579 attr = (attr & 0xf0) | ulcolor;
580 else if (intensity == 0)
581 attr = (attr & 0xf0) | halfcolor;
582 }
583 if (reverse ^ decscnm)
584 attr = (attr & 0x88) | (((attr >> 4) | (attr << 4)) & 0x77);
585 if (blink)
586 attr ^= 0x80;
587 if (intensity == 2)
588 attr ^= 0x08;
589 if (!can_do_color) {
590 if (underline)
591 attr = (attr & 0xf8) | 0x01;
592 else if (intensity == 0)
593 attr = (attr & 0xf0) | 0x08;
594 }
595 if (decscnm)
596 video_erase_char = ((color & 0x88) | (((color >> 4) |
597 (color << 4)) & 0x77) << 8) | ' ';
598 else
599 video_erase_char = (color << 8) | ' ';
600 }
601
602 static void default_attr(int currcons)
603 {
604 intensity = 1;
605 underline = 0;
606 reverse = 0;
607 blink = 0;
608 color = def_color;
609 }
610
611 static void csi_m(int currcons)
612 {
613 int i;
614
615 for (i=0;i<=npar;i++)
616 switch (par[i]) {
617 case 0:
618 default_attr(currcons);
619 break;
620 case 1:
621 intensity = 2;
622 break;
623 case 2:
624 intensity = 0;
625 break;
626 case 4:
627 underline = 1;
628 break;
629 case 5:
630 blink = 1;
631 break;
632 case 7:
633 reverse = 1;
634 break;
635 case 21:
636 case 22:
637 intensity = 1;
638 break;
639 case 24:
640 underline = 0;
641 break;
642 case 25:
643 blink = 0;
644 break;
645 case 27:
646 reverse = 0;
647 break;
648 case 39:
649 color = (def_color & 0x0f) | background;
650 break;
651 case 49:
652 color = (def_color & 0xf0) | foreground;
653 break;
654 default:
655 if (par[i] >= 30 && par[i] <= 37)
656 color = color_table[par[i]-30]
657 | background;
658 else if (par[i] >= 40 && par[i] <= 47)
659 color = (color_table[par[i]-40]<<4)
660 | foreground;
661 break;
662 }
663 update_attr(currcons);
664 }
665
666 static void respond_string(char * p, int currcons, struct tty_struct * tty)
667 {
668 while (*p) {
669 put_tty_queue(*p, &tty->read_q);
670 p++;
671 }
672 TTY_READ_FLUSH(tty);
673 }
674
675 static void respond_num(unsigned int n, int currcons, struct tty_struct * tty)
676 {
677 char buff[3];
678 int i = 0;
679
680 do {
681 buff[i++] = (n%10)+'0';
682 n /= 10;
683 } while(n && i < 3);
684 while (i--) {
685 put_tty_queue(buff[i], &tty->read_q);
686 }
687
688 }
689
690 static void cursor_report(int currcons, struct tty_struct * tty)
691 {
692 put_tty_queue('\033', &tty->read_q);
693 put_tty_queue('[', &tty->read_q);
694 respond_num(y + (decom ? top+1 : 1), currcons, tty);
695 put_tty_queue(';', &tty->read_q);
696 respond_num(x+1, currcons, tty);
697 put_tty_queue('R', &tty->read_q);
698 TTY_READ_FLUSH(tty);
699 }
700
701 static inline void status_report(int currcons, struct tty_struct * tty)
702 {
703 respond_string("\033[0n", currcons, tty);
704 }
705
706 static inline void respond_ID(int currcons, struct tty_struct * tty)
707 {
708 respond_string(VT102ID, currcons, tty);
709 }
710
711 static void invert_screen(int currcons) {
712 unsigned char *p;
713
714 if (can_do_color)
715 for (p = (unsigned char *)origin+1; p < (unsigned char *)scr_end; p+=2)
716 *p = (*p & 0x88) | (((*p >> 4) | (*p << 4)) & 0x77);
717 else
718 for (p = (unsigned char *)origin+1; p < (unsigned char *)scr_end; p+=2)
719 *p ^= *p & 0x07 == 1 ? 0x70 : 0x77;
720 }
721
722 static void set_mode(int currcons, int on_off)
723 {
724 int i;
725
726 for (i=0; i<=npar; i++)
727 if (ques) switch(par[i]) {
728 case 1:
729 if (on_off)
730 set_kbd(decckm);
731 else
732 clr_kbd(decckm);
733 break;
734 case 3:
735 csi_J(currcons,2);
736 gotoxy(currcons,0,0);
737 break;
738 case 5:
739 if (decscnm != on_off) {
740 decscnm = on_off;
741 invert_screen(currcons);
742 update_attr(currcons);
743 }
744 break;
745 case 6:
746 decom = on_off;
747 gotoxy(currcons,0,0);
748 break;
749 case 7:
750 decawm = on_off;
751 break;
752 case 8:
753 if (on_off)
754 set_kbd(decarm);
755 else
756 clr_kbd(decarm);
757 break;
758 case 25:
759 deccm = on_off;
760 set_cursor(currcons);
761 break;
762 } else switch(par[i]) {
763 case 4:
764 decim = on_off;
765 break;
766 case 20:
767 if (on_off)
768 set_kbd(lnm);
769 else
770 clr_kbd(lnm);
771 break;
772 }
773 }
774
775 static void setterm_command(int currcons)
776 {
777 switch(par[0]) {
778 case 1:
779 if (can_do_color && par[1] < 16) {
780 ulcolor = color_table[par[1]];
781 if (underline)
782 update_attr(currcons);
783 }
784 break;
785 case 2:
786 if (can_do_color && par[1] < 16) {
787 halfcolor = color_table[par[1]];
788 if (intensity == 0)
789 update_attr(currcons);
790 }
791 break;
792 case 8:
793 def_color = attr;
794 default_attr(currcons);
795 update_attr(currcons);
796 break;
797 case 9:
798 blankinterval = ((par[1] < 60) ? par[1] : 60) * 60 * HZ;
799 break;
800 }
801 }
802
803 static void insert_char(int currcons)
804 {
805 unsigned int i = x;
806 unsigned short tmp, old = video_erase_char;
807 unsigned short * p = (unsigned short *) pos;
808
809 while (i++ < video_num_columns) {
810 tmp = *p;
811 *p = old;
812 old = tmp;
813 p++;
814 }
815 need_wrap = 0;
816 }
817
818 static void insert_line(int currcons)
819 {
820 scrdown(currcons,y,bottom);
821 need_wrap = 0;
822 }
823
824 static void delete_char(int currcons)
825 {
826 unsigned int i = x;
827 unsigned short * p = (unsigned short *) pos;
828
829 while (++i < video_num_columns) {
830 *p = *(p+1);
831 p++;
832 }
833 *p = video_erase_char;
834 need_wrap = 0;
835 }
836
837 static void delete_line(int currcons)
838 {
839 scrup(currcons,y,bottom);
840 need_wrap = 0;
841 }
842
843 static void csi_at(int currcons, unsigned int nr)
844 {
845 if (nr > video_num_columns)
846 nr = video_num_columns;
847 else if (!nr)
848 nr = 1;
849 while (nr--)
850 insert_char(currcons);
851 }
852
853 static void csi_L(int currcons, unsigned int nr)
854 {
855 if (nr > video_num_lines)
856 nr = video_num_lines;
857 else if (!nr)
858 nr = 1;
859 while (nr--)
860 insert_line(currcons);
861 }
862
863 static void csi_P(int currcons, unsigned int nr)
864 {
865 if (nr > video_num_columns)
866 nr = video_num_columns;
867 else if (!nr)
868 nr = 1;
869 while (nr--)
870 delete_char(currcons);
871 }
872
873 static void csi_M(int currcons, unsigned int nr)
874 {
875 if (nr > video_num_lines)
876 nr = video_num_lines;
877 else if (!nr)
878 nr=1;
879 while (nr--)
880 delete_line(currcons);
881 }
882
883 static void save_cur(int currcons)
884 {
885 saved_x = x;
886 saved_y = y;
887 s_intensity = intensity;
888 s_underline = underline;
889 s_blink = blink;
890 s_reverse = reverse;
891 s_charset = charset;
892 s_color = color;
893 saved_G0 = G0_charset;
894 saved_G1 = G1_charset;
895 }
896
897 static void restore_cur(int currcons)
898 {
899 gotoxy(currcons,saved_x,saved_y);
900 intensity = s_intensity;
901 underline = s_underline;
902 blink = s_blink;
903 reverse = s_reverse;
904 charset = s_charset;
905 color = s_color;
906 G0_charset = saved_G0;
907 G1_charset = saved_G1;
908 translate = charset ? G1_charset : G0_charset;
909 update_attr(currcons);
910 need_wrap = 0;
911 }
912
913 enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
914 EShash, ESsetG0, ESsetG1, ESignore };
915
916 static void reset_terminal(int currcons, int do_clear)
917 {
918 top = 0;
919 bottom = video_num_lines;
920 state = ESnormal;
921 ques = 0;
922 translate = NORM_TRANS;
923 G0_charset = NORM_TRANS;
924 G1_charset = GRAF_TRANS;
925 charset = 0;
926 need_wrap = 0;
927
928 decscnm = 0;
929 decom = 0;
930 decawm = 1;
931 deccm = 1;
932 decim = 0;
933
934 set_kbd(decarm);
935 clr_kbd(decckm);
936 clr_kbd(kbdapplic);
937 clr_kbd(lnm);
938 kbd_table[currcons].flags =
939 (kbd_table[currcons].flags & ~LED_MASK) |
940 (kbd_table[currcons].default_flags & LED_MASK);
941 kbdmode = 0;
942 set_leds();
943
944 default_attr(currcons);
945 update_attr(currcons);
946
947 tab_stop[0] = 0x01010100;
948 tab_stop[1] =
949 tab_stop[2] =
950 tab_stop[3] =
951 tab_stop[4] = 0x01010101;
952
953 if (do_clear) {
954 gotoxy(currcons,0,0);
955 csi_J(currcons,2);
956 save_cur(currcons);
957 }
958 }
959
960 void con_write(struct tty_struct * tty)
961 {
962 int c;
963 unsigned int currcons;
964
965 wake_up_interruptible(&tty->write_q.proc_list);
966 currcons = tty->line - 1;
967 if (currcons >= NR_CONSOLES) {
968 printk("con_write: illegal tty (%d)\n", currcons);
969 return;
970 }
971 #ifdef CONFIG_SELECTION
972
973
974 if (!EMPTY(&tty->write_q) && currcons == sel_cons)
975 clear_selection();
976 #endif
977 while (!tty->stopped && (c = get_tty_queue(&tty->write_q)) >= 0) {
978 if (state == ESnormal && translate[c]) {
979 if (need_wrap) {
980 cr(currcons);
981 lf(currcons);
982 }
983 if (decim)
984 insert_char(currcons);
985 c = translate[c];
986 *(unsigned short *) pos = (attr << 8) + c;
987 if (x == video_num_columns - 1)
988 need_wrap = decawm;
989 else {
990 x++;
991 pos+=2;
992 }
993 continue;
994 }
995
996
997
998
999
1000 switch (c) {
1001 case 7:
1002 kd_mksound(0x637, HZ/8);
1003 continue;
1004 case 8:
1005 bs(currcons);
1006 continue;
1007 case 9:
1008 pos -= (x << 1);
1009 while (x < video_num_columns - 1) {
1010 x++;
1011 if (tab_stop[x >> 5] & (1 << (x & 31)))
1012 break;
1013 }
1014 pos += (x << 1);
1015 continue;
1016 case 10: case 11: case 12:
1017 lf(currcons);
1018 if (!is_kbd(lnm))
1019 continue;
1020 case 13:
1021 cr(currcons);
1022 continue;
1023 case 14:
1024 charset = 1;
1025 translate = G1_charset;
1026 continue;
1027 case 15:
1028 charset = 0;
1029 translate = G0_charset;
1030 continue;
1031 case 24: case 26:
1032 state = ESnormal;
1033 continue;
1034 case 27:
1035 state = ESesc;
1036 continue;
1037 case 127:
1038 del(currcons);
1039 continue;
1040 case 128+27:
1041 state = ESsquare;
1042 continue;
1043 }
1044 switch(state) {
1045 case ESesc:
1046 state = ESnormal;
1047 switch (c) {
1048 case '[':
1049 state = ESsquare;
1050 continue;
1051 case 'E':
1052 cr(currcons);
1053 lf(currcons);
1054 continue;
1055 case 'M':
1056 ri(currcons);
1057 continue;
1058 case 'D':
1059 lf(currcons);
1060 continue;
1061 case 'H':
1062 tab_stop[x >> 5] |= (1 << (x & 31));
1063 continue;
1064 case 'Z':
1065 respond_ID(currcons,tty);
1066 continue;
1067 case '7':
1068 save_cur(currcons);
1069 continue;
1070 case '8':
1071 restore_cur(currcons);
1072 continue;
1073 case '(':
1074 state = ESsetG0;
1075 continue;
1076 case ')':
1077 state = ESsetG1;
1078 continue;
1079 case '#':
1080 state = EShash;
1081 continue;
1082 case 'c':
1083 reset_terminal(currcons,1);
1084 continue;
1085 case '>':
1086 clr_kbd(kbdapplic);
1087 continue;
1088 case '=':
1089 set_kbd(kbdapplic);
1090 continue;
1091 }
1092 continue;
1093 case ESsquare:
1094 for(npar = 0 ; npar < NPAR ; npar++)
1095 par[npar] = 0;
1096 npar = 0;
1097 state = ESgetpars;
1098 if (c == '[') {
1099 state=ESfunckey;
1100 continue;
1101 }
1102 ques = (c=='?');
1103 if (ques)
1104 continue;
1105 case ESgetpars:
1106 if (c==';' && npar<NPAR-1) {
1107 npar++;
1108 continue;
1109 } else if (c>='0' && c<='9') {
1110 par[npar] *= 10;
1111 par[npar] += c-'0';
1112 continue;
1113 } else state=ESgotpars;
1114 case ESgotpars:
1115 state = ESnormal;
1116 switch(c) {
1117 case 'h':
1118 set_mode(currcons,1);
1119 continue;
1120 case 'l':
1121 set_mode(currcons,0);
1122 continue;
1123 case 'n':
1124 if (!ques)
1125 if (par[0] == 5)
1126 status_report(currcons,tty);
1127 else if (par[0] == 6)
1128 cursor_report(currcons,tty);
1129 continue;
1130 }
1131 if (ques) {
1132 ques = 0;
1133 continue;
1134 }
1135 switch(c) {
1136 case 'G': case '`':
1137 if (par[0]) par[0]--;
1138 gotoxy(currcons,par[0],y);
1139 continue;
1140 case 'A':
1141 if (!par[0]) par[0]++;
1142 gotoxy(currcons,x,y-par[0]);
1143 continue;
1144 case 'B': case 'e':
1145 if (!par[0]) par[0]++;
1146 gotoxy(currcons,x,y+par[0]);
1147 continue;
1148 case 'C': case 'a':
1149 if (!par[0]) par[0]++;
1150 gotoxy(currcons,x+par[0],y);
1151 continue;
1152 case 'D':
1153 if (!par[0]) par[0]++;
1154 gotoxy(currcons,x-par[0],y);
1155 continue;
1156 case 'E':
1157 if (!par[0]) par[0]++;
1158 gotoxy(currcons,0,y+par[0]);
1159 continue;
1160 case 'F':
1161 if (!par[0]) par[0]++;
1162 gotoxy(currcons,0,y-par[0]);
1163 continue;
1164 case 'd':
1165 if (par[0]) par[0]--;
1166 gotoxy(currcons,x,par[0]);
1167 continue;
1168 case 'H': case 'f':
1169 if (par[0]) par[0]--;
1170 if (par[1]) par[1]--;
1171 gotoxy(currcons,par[1],par[0]);
1172 continue;
1173 case 'J':
1174 csi_J(currcons,par[0]);
1175 continue;
1176 case 'K':
1177 csi_K(currcons,par[0]);
1178 continue;
1179 case 'L':
1180 csi_L(currcons,par[0]);
1181 continue;
1182 case 'M':
1183 csi_M(currcons,par[0]);
1184 continue;
1185 case 'P':
1186 csi_P(currcons,par[0]);
1187 continue;
1188 case 'c':
1189 if (!par[0])
1190 respond_ID(currcons,tty);
1191 continue;
1192 case 'g':
1193 if (!par[0])
1194 tab_stop[x >> 5] &= ~(1 << (x & 31));
1195 else if (par[0] == 3) {
1196 tab_stop[0] =
1197 tab_stop[1] =
1198 tab_stop[2] =
1199 tab_stop[3] =
1200 tab_stop[4] = 0;
1201 }
1202 continue;
1203 case 'm':
1204 csi_m(currcons);
1205 continue;
1206 case 'r':
1207 if (!par[0])
1208 par[0]++;
1209 if (!par[1])
1210 par[1] = video_num_lines;
1211
1212 if (par[0] < par[1] &&
1213 par[1] <= video_num_lines) {
1214 top=par[0]-1;
1215 bottom=par[1];
1216 gotoxy(currcons,0,0);
1217 }
1218 continue;
1219 case 's':
1220 save_cur(currcons);
1221 continue;
1222 case 'u':
1223 restore_cur(currcons);
1224 continue;
1225 case '@':
1226 csi_at(currcons,par[0]);
1227 continue;
1228 case ']':
1229 setterm_command(currcons);
1230 continue;
1231 }
1232 continue;
1233 case ESfunckey:
1234 state = ESnormal;
1235 continue;
1236 case EShash:
1237 state = ESnormal;
1238 if (c == '8') {
1239
1240 video_erase_char =
1241 (video_erase_char & 0xff00) | 'E';
1242 csi_J(currcons, 2);
1243 video_erase_char =
1244 (video_erase_char & 0xff00) | ' ';
1245 }
1246 continue;
1247 case ESsetG0:
1248 if (c == '0')
1249 G0_charset = GRAF_TRANS;
1250 else if (c == 'B')
1251 G0_charset = NORM_TRANS;
1252 else if (c == 'U')
1253 G0_charset = NULL_TRANS;
1254 if (charset == 0)
1255 translate = G0_charset;
1256 state = ESnormal;
1257 continue;
1258 case ESsetG1:
1259 if (c == '0')
1260 G1_charset = GRAF_TRANS;
1261 else if (c == 'B')
1262 G1_charset = NORM_TRANS;
1263 else if (c == 'U')
1264 G1_charset = NULL_TRANS;
1265 if (charset == 1)
1266 translate = G1_charset;
1267 state = ESnormal;
1268 continue;
1269 default:
1270 state = ESnormal;
1271 }
1272 }
1273 if (vcmode == KD_GRAPHICS)
1274 return;
1275 set_cursor(currcons);
1276 }
1277
1278 void do_keyboard_interrupt(void)
1279 {
1280 TTY_READ_FLUSH(TTY_TABLE(0));
1281 timer_active &= ~(1<<BLANK_TIMER);
1282 if (vt_cons[fg_console].vc_mode == KD_GRAPHICS)
1283 return;
1284 if (console_blanked) {
1285 timer_table[BLANK_TIMER].expires = 0;
1286 timer_active |= 1<<BLANK_TIMER;
1287 } else if (blankinterval) {
1288 timer_table[BLANK_TIMER].expires = jiffies + blankinterval;
1289 timer_active |= 1<<BLANK_TIMER;
1290 }
1291 }
1292
1293 void * memsetw(void * s,unsigned short c,int count)
1294 {
1295 __asm__("cld\n\t"
1296 "rep\n\t"
1297 "stosw"
1298 :
1299 :"a" (c),"D" (s),"c" (count)
1300 :"cx","di");
1301 return s;
1302 }
1303
1304 void console_print(const char * b)
1305 {
1306 int currcons = fg_console;
1307 unsigned char c;
1308
1309 if (!printable || currcons<0 || currcons>=NR_CONSOLES)
1310 return;
1311 while ((c = *(b++)) != 0) {
1312 if (c == 10 || c == 13 || need_wrap) {
1313 if (c != 13)
1314 lf(currcons);
1315 cr(currcons);
1316 if (c == 10 || c == 13)
1317 continue;
1318 }
1319 *(unsigned short *) pos = (attr << 8) + c;
1320 if (x == video_num_columns - 1) {
1321 need_wrap = 1;
1322 continue;
1323 }
1324 x++;
1325 pos+=2;
1326 }
1327 set_cursor(currcons);
1328 if (vt_cons[fg_console].vc_mode == KD_GRAPHICS)
1329 return;
1330 timer_active &= ~(1<<BLANK_TIMER);
1331 if (console_blanked) {
1332 timer_table[BLANK_TIMER].expires = 0;
1333 timer_active |= 1<<BLANK_TIMER;
1334 } else if (blankinterval) {
1335 timer_table[BLANK_TIMER].expires = jiffies + blankinterval;
1336 timer_active |= 1<<BLANK_TIMER;
1337 }
1338 }
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350 long con_init(long kmem_start)
1351 {
1352 char *display_desc = "????";
1353 int currcons = 0;
1354 long base;
1355 int orig_x = ORIG_X;
1356 int orig_y = ORIG_Y;
1357
1358 vc_scrmembuf = (unsigned short *) kmem_start;
1359 video_num_columns = ORIG_VIDEO_COLS;
1360 video_size_row = video_num_columns * 2;
1361 video_num_lines = ORIG_VIDEO_LINES;
1362 video_page = ORIG_VIDEO_PAGE;
1363 screen_size = (video_num_lines * video_size_row);
1364 kmem_start += NR_CONSOLES * screen_size;
1365 timer_table[BLANK_TIMER].fn = blank_screen;
1366 timer_table[BLANK_TIMER].expires = 0;
1367 if (blankinterval) {
1368 timer_table[BLANK_TIMER].expires = jiffies+blankinterval;
1369 timer_active |= 1<<BLANK_TIMER;
1370 }
1371
1372 if (ORIG_VIDEO_MODE == 7)
1373 {
1374 video_mem_base = 0xb0000;
1375 video_port_reg = 0x3b4;
1376 video_port_val = 0x3b5;
1377 if ((ORIG_VIDEO_EGA_BX & 0xff) != 0x10)
1378 {
1379 video_type = VIDEO_TYPE_EGAM;
1380 video_mem_term = 0xb8000;
1381 display_desc = "EGA+";
1382 }
1383 else
1384 {
1385 video_type = VIDEO_TYPE_MDA;
1386 video_mem_term = 0xb2000;
1387 display_desc = "*MDA";
1388 }
1389 }
1390 else
1391 {
1392 can_do_color = 1;
1393 video_mem_base = 0xb8000;
1394 video_port_reg = 0x3d4;
1395 video_port_val = 0x3d5;
1396 if ((ORIG_VIDEO_EGA_BX & 0xff) != 0x10)
1397 {
1398 video_type = VIDEO_TYPE_EGAC;
1399 video_mem_term = 0xc0000;
1400 display_desc = "EGA+";
1401 }
1402 else
1403 {
1404 video_type = VIDEO_TYPE_CGA;
1405 video_mem_term = 0xba000;
1406 display_desc = "*CGA";
1407 }
1408 }
1409
1410
1411
1412 base = (long)vc_scrmembuf;
1413 for (currcons = 0; currcons<NR_CONSOLES; currcons++) {
1414 pos = origin = video_mem_start = base;
1415 scr_end = video_mem_end = (base += screen_size);
1416 vc_scrbuf[currcons] = (unsigned short *) origin;
1417 vcmode = KD_TEXT;
1418 vtmode.mode = VT_AUTO;
1419 vtmode.waitv = 0;
1420 vtmode.relsig = 0;
1421 vtmode.acqsig = 0;
1422 vtmode.frsig = 0;
1423 vtpid = -1;
1424 vtnewvt = -1;
1425 clr_kbd(kbdraw);
1426 def_color = 0x07;
1427 ulcolor = 0x0f;
1428 halfcolor = 0x08;
1429 reset_terminal(currcons, currcons);
1430 }
1431 currcons = fg_console = 0;
1432
1433 video_mem_start = video_mem_base;
1434 video_mem_end = video_mem_term;
1435 origin = video_mem_start;
1436 scr_end = video_mem_start + video_num_lines * video_size_row;
1437 gotoxy(currcons,0,0);
1438 save_cur(currcons);
1439 gotoxy(currcons,orig_x,orig_y);
1440 update_screen(fg_console);
1441 printable = 1;
1442 printk("Console: %s %s %ldx%ld, %d virtual consoles\n",
1443 can_do_color?"colour":"mono",
1444 display_desc,
1445 video_num_columns,video_num_lines,
1446 NR_CONSOLES);
1447 register_console(console_print);
1448 return kmem_start;
1449 }
1450
1451
1452
1453
1454
1455 void kbdsave(int new_console)
1456 {
1457 }
1458
1459 static void get_scrmem(int currcons)
1460 {
1461 memcpy((void *)vc_scrbuf[currcons],(void *)origin, screen_size);
1462 video_mem_start = (unsigned long)vc_scrbuf[currcons];
1463 origin = video_mem_start;
1464 scr_end = video_mem_end = video_mem_start+screen_size;
1465 pos = origin + y*video_size_row + (x<<1);
1466 }
1467
1468 static void set_scrmem(int currcons)
1469 {
1470 video_mem_start = video_mem_base;
1471 video_mem_end = video_mem_term;
1472 origin = video_mem_start;
1473 scr_end = video_mem_start + screen_size;
1474 pos = origin + y*video_size_row + (x<<1);
1475 memcpy((void *)video_mem_base, (void *)vc_scrbuf[fg_console], screen_size);
1476 }
1477
1478 void blank_screen(void)
1479 {
1480 if (console_blanked)
1481 return;
1482 timer_table[BLANK_TIMER].fn = unblank_screen;
1483 get_scrmem(fg_console);
1484 hide_cursor(fg_console);
1485 console_blanked = 1;
1486 memsetw((void *)video_mem_base, 0x0020, video_mem_term-video_mem_base );
1487 }
1488
1489 void unblank_screen(void)
1490 {
1491 if (!console_blanked)
1492 return;
1493 timer_table[BLANK_TIMER].fn = blank_screen;
1494 if (blankinterval) {
1495 timer_table[BLANK_TIMER].expires = jiffies + blankinterval;
1496 timer_active |= 1<<BLANK_TIMER;
1497 }
1498 console_blanked = 0;
1499 set_scrmem(fg_console);
1500 set_origin(fg_console);
1501 set_cursor(fg_console);
1502 }
1503
1504 void update_screen(int new_console)
1505 {
1506 static int lock = 0;
1507
1508 if (new_console == fg_console || lock)
1509 return;
1510 lock = 1;
1511 kbdsave(new_console);
1512 get_scrmem(fg_console);
1513 fg_console = new_console;
1514 set_scrmem(fg_console);
1515 set_origin(fg_console);
1516 set_cursor(new_console);
1517 set_leds();
1518 lock = 0;
1519 }
1520
1521 int do_screendump(int arg)
1522 {
1523 char *sptr, *buf = (char *)arg;
1524 int currcons, l;
1525
1526 if (!suser())
1527 return -EPERM;
1528 l = verify_area(VERIFY_WRITE, buf,2+video_num_columns*video_num_lines);
1529 if (l)
1530 return l;
1531 currcons = get_fs_byte(buf+1);
1532 if ((currcons<0) || (currcons>NR_CONSOLES))
1533 return -EIO;
1534 put_fs_byte((char)(video_num_lines),buf++);
1535 put_fs_byte((char)(video_num_columns),buf++);
1536 currcons = (currcons ? currcons-1 : fg_console);
1537 sptr = (char *) origin;
1538 for (l=video_num_lines*video_num_columns; l>0 ; l--, sptr++)
1539 put_fs_byte(*sptr++,buf++);
1540 return(0);
1541 }
1542
1543
1544
1545
1546
1547 int con_open(struct tty_struct *tty, struct file * filp)
1548 {
1549 tty->write = con_write;
1550 tty->ioctl = vt_ioctl;
1551 if (tty->line > NR_CONSOLES)
1552 return -ENODEV;
1553 return 0;
1554 }
1555
1556 #ifdef CONFIG_SELECTION
1557
1558 #define hwscroll_offset (currcons == fg_console ? ((__real_origin - __origin) << 1) : 0)
1559
1560
1561 static void highlight(const int currcons, const int s, const int e)
1562 {
1563 unsigned char *p, *p1, *p2;
1564
1565 p1 = (unsigned char *)origin - hwscroll_offset + s + 1;
1566 p2 = (unsigned char *)origin - hwscroll_offset + e + 1;
1567 if (p1 > p2)
1568 {
1569 p = p1;
1570 p1 = p2;
1571 p2 = p;
1572 }
1573 for (p = p1; p <= p2; p += 2)
1574 *p = (*p & 0x88) | ((*p << 4) & 0x70) | ((*p >> 4) & 0x07);
1575 }
1576
1577
1578 static inline int inword(const char c) { return (isalnum(c) || c == '_'); }
1579
1580
1581 static inline int atedge(const int p)
1582 {
1583 return (!(p % video_size_row) || !((p + 2) % video_size_row));
1584 }
1585
1586
1587 static inline short limit(const int v, const int l, const int u)
1588 {
1589 return (v < l) ? l : ((v > u) ? u : v);
1590 }
1591
1592
1593 int set_selection(const int arg)
1594 {
1595 unsigned short *args, xs, ys, xe, ye;
1596 int currcons = fg_console;
1597 int sel_mode, new_sel_start, new_sel_end, spc;
1598 char *bp, *obp, *spos;
1599 int i, ps, pe;
1600 char *off = (char *)origin - hwscroll_offset;
1601
1602 unblank_screen();
1603 args = (unsigned short *)(arg + 1);
1604 xs = get_fs_word(args++) - 1;
1605 ys = get_fs_word(args++) - 1;
1606 xe = get_fs_word(args++) - 1;
1607 ye = get_fs_word(args++) - 1;
1608 sel_mode = get_fs_word(args);
1609
1610 xs = limit(xs, 0, video_num_columns - 1);
1611 ys = limit(ys, 0, video_num_lines - 1);
1612 xe = limit(xe, 0, video_num_columns - 1);
1613 ye = limit(ye, 0, video_num_lines - 1);
1614 ps = ys * video_size_row + (xs << 1);
1615 pe = ye * video_size_row + (xe << 1);
1616
1617 if (ps > pe)
1618 {
1619 int tmp = ps;
1620 ps = pe;
1621 pe = tmp;
1622 }
1623
1624 switch (sel_mode)
1625 {
1626 case 0:
1627 default:
1628 new_sel_start = ps;
1629 new_sel_end = pe;
1630 break;
1631 case 1:
1632 spc = isspace(*(off + ps));
1633 for (new_sel_start = ps; ; ps -= 2)
1634 {
1635 if ((spc && !isspace(*(off + ps))) ||
1636 (!spc && !inword(*(off + ps))))
1637 break;
1638 new_sel_start = ps;
1639 if (!(ps % video_size_row))
1640 break;
1641 }
1642 spc = isspace(*(off + pe));
1643 for (new_sel_end = pe; ; pe += 2)
1644 {
1645 if ((spc && !isspace(*(off + pe))) ||
1646 (!spc && !inword(*(off + pe))))
1647 break;
1648 new_sel_end = pe;
1649 if (!((pe + 2) % video_size_row))
1650 break;
1651 }
1652 break;
1653 case 2:
1654 new_sel_start = ps - ps % video_size_row;
1655 new_sel_end = pe + video_size_row
1656 - pe % video_size_row - 2;
1657 break;
1658 }
1659
1660 if (new_sel_end > new_sel_start &&
1661 !atedge(new_sel_end) && isspace(*(off + new_sel_end)))
1662 {
1663 for (pe = new_sel_end + 2; ; pe += 2)
1664 {
1665 if (!isspace(*(off + pe)) || atedge(pe))
1666 break;
1667 }
1668 if (isspace(*(off + pe)))
1669 new_sel_end = pe;
1670 }
1671 if (sel_cons != currcons)
1672 {
1673 clear_selection();
1674 sel_cons = currcons;
1675 }
1676 if (sel_start == -1)
1677 highlight(sel_cons, new_sel_start, new_sel_end);
1678 else if (new_sel_start == sel_start)
1679 {
1680 if (new_sel_end == sel_end)
1681 return 0;
1682 else if (new_sel_end > sel_end)
1683 highlight(sel_cons, sel_end + 2, new_sel_end);
1684 else
1685 highlight(sel_cons, new_sel_end + 2, sel_end);
1686 }
1687 else if (new_sel_end == sel_end)
1688 {
1689 if (new_sel_start < sel_start)
1690 highlight(sel_cons, new_sel_start, sel_start - 2);
1691 else
1692 highlight(sel_cons, sel_start, new_sel_start - 2);
1693 }
1694 else
1695 {
1696 clear_selection();
1697 highlight(sel_cons, new_sel_start, new_sel_end);
1698 }
1699 sel_start = new_sel_start;
1700 sel_end = new_sel_end;
1701 obp = bp = sel_buffer;
1702 for (i = sel_start; i <= sel_end; i += 2)
1703 {
1704 spos = (char *)off + i;
1705 *bp++ = *spos;
1706 if (!isspace(*spos))
1707 obp = bp;
1708 if (! ((i + 2) % video_size_row))
1709 {
1710
1711
1712 if (obp != bp)
1713 {
1714 bp = obp;
1715 *bp++ = '\r';
1716 }
1717 obp = bp;
1718 }
1719
1720
1721 if (bp - sel_buffer > SEL_BUFFER_SIZE - 3)
1722 break;
1723 }
1724 *bp = '\0';
1725 return 0;
1726 }
1727
1728
1729
1730 int paste_selection(struct tty_struct *tty)
1731 {
1732 char *bp = sel_buffer;
1733
1734 if (! *bp)
1735 return 0;
1736 unblank_screen();
1737 while (*bp)
1738 {
1739 put_tty_queue(*bp, &tty->read_q);
1740 bp++;
1741 }
1742 TTY_READ_FLUSH(tty);
1743 return 0;
1744 }
1745
1746
1747
1748 static void clear_selection()
1749 {
1750 if (sel_start != -1)
1751 {
1752 highlight(sel_cons, sel_start, sel_end);
1753 sel_start = -1;
1754 }
1755 }
1756 #endif