root/kernel/chr_drv/console.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. gotoxy
  2. __set_origin
  3. scrollback
  4. scrollfront
  5. set_origin
  6. hide_cursor
  7. set_cursor
  8. scrup
  9. scrdown
  10. lf
  11. ri
  12. cr
  13. bs
  14. del
  15. csi_J
  16. csi_K
  17. update_attr
  18. default_attr
  19. csi_m
  20. respond_string
  21. respond_num
  22. cursor_report
  23. status_report
  24. respond_ID
  25. invert_screen
  26. set_mode
  27. setterm_command
  28. insert_char
  29. insert_line
  30. delete_char
  31. delete_line
  32. csi_at
  33. csi_L
  34. csi_P
  35. csi_M
  36. save_cur
  37. restore_cur
  38. reset_terminal
  39. con_write
  40. do_keyboard_interrupt
  41. memsetw
  42. console_print
  43. con_init
  44. kbdsave
  45. get_scrmem
  46. set_scrmem
  47. blank_screen
  48. unblank_screen
  49. update_screen
  50. do_screendump
  51. con_open
  52. highlight
  53. inword
  54. atedge
  55. limit
  56. set_selection
  57. paste_selection
  58. clear_selection

   1 /*
   2  *  linux/kernel/console.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 /*
   8  *      console.c
   9  *
  10  * This module exports the console io functions:
  11  * 
  12  *      'long con_init(long)'
  13  *      'void con_open(struct tty_queue * queue, struct )'
  14  *      'void update_screen(int new_console)'
  15  *      'void blank_screen(void)'
  16  *      'void unblank_screen(void)'
  17  * 
  18  * Hopefully this will be a rather complete VT102 implementation.
  19  *
  20  * Beeping thanks to John T Kohl.
  21  * 
  22  * Virtual Consoles, Screen Blanking, Screen Dumping, Color, Graphics
  23  *   Chars, and VT100 enhancements by Peter MacDonald.
  24  *
  25  * Copy and paste function by Andrew Haylett.
  26  */
  27 
  28 /*
  29  *  NOTE!!! We sometimes disable and enable interrupts for a short while
  30  * (to put a word in video IO), but this will work even for keyboard
  31  * interrupts. We know interrupts aren't enabled when getting a keyboard
  32  * interrupt, as we use trap-gates. Hopefully all is well.
  33  */
  34 
  35 /*
  36  * Code to check for different video-cards mostly by Galen Hunt,
  37  * <g-hunt@ee.utah.edu>
  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 /* Routines for selection control. */
  60 int set_selection(const int arg);
  61 int paste_selection(struct tty_struct *tty);
  62 static void clear_selection(void);
  63 
  64 /* Variables for selection control. */
  65 #define SEL_BUFFER_SIZE 2048
  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 /* CONFIG_SELECTION */
  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;              /* Number of text columns       */
  78 unsigned long   video_num_lines;                /* Number of test lines         */
  79 
  80 static unsigned char    video_type;             /* Type of display being used   */
  81 static unsigned long    video_mem_base;         /* Base of video memory         */
  82 static unsigned long    video_mem_term;         /* End of video memory          */
  83 static unsigned long    video_size_row;         /* Bytes per row                */
  84 static unsigned char    video_page;             /* Initial video page           */
  85 static unsigned short   video_port_reg;         /* Video register select port   */
  86 static unsigned short   video_port_val;         /* Video register value port    */
  87 static int can_do_color = 0;
  88 static int printable = 0;
  89 
  90 static struct {
  91         unsigned short  vc_video_erase_char;    /* Background erase character */
  92         unsigned char   vc_attr;                /* Current attributes */
  93         unsigned char   vc_def_color;           /* Default colors */
  94         unsigned char   vc_color;               /* Foreground & background */
  95         unsigned char   vc_s_color;             /* Saved foreground & background */
  96         unsigned char   vc_ulcolor;             /* Colour for underline mode */
  97         unsigned char   vc_halfcolor;           /* Colour for half intensity mode */
  98         unsigned long   vc_origin;              /* Used for EGA/VGA fast scroll */
  99         unsigned long   vc_scr_end;             /* Used for EGA/VGA fast scroll */
 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;     /* Start of video RAM           */
 106         unsigned long   vc_video_mem_end;       /* End of video RAM (sort of)   */
 107         unsigned long   vc_saved_x;
 108         unsigned long   vc_saved_y;
 109         /* mode flags */
 110         unsigned long   vc_charset      : 1;    /* Character set G0 / G1 */
 111         unsigned long   vc_s_charset    : 1;    /* Saved character set */
 112         unsigned long   vc_decscnm      : 1;    /* Screen Mode */
 113         unsigned long   vc_decom        : 1;    /* Origin Mode */
 114         unsigned long   vc_decawm       : 1;    /* Autowrap Mode */
 115         unsigned long   vc_deccm        : 1;    /* Cursor Visible */
 116         unsigned long   vc_decim        : 1;    /* Insert Mode */
 117         /* attribute flags */
 118         unsigned long   vc_intensity    : 2;    /* 0=half-bright, 1=normal, 2=bold */
 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;    /* saved rendition */
 123         unsigned long   vc_s_underline  : 1;
 124         unsigned long   vc_s_blink      : 1;
 125         unsigned long   vc_s_reverse    : 1;
 126         /* misc */
 127         unsigned long   vc_ques         : 1;
 128         unsigned long   vc_need_wrap    : 1;
 129         unsigned long   vc_tab_stop[5];         /* Tab stops. 160 columns. */
 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         /* additional information is in vt_kern.h */
 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  * this is what the terminal answers to a ESC-Z or csi0c query.
 210  */
 211 #define VT100ID "\033[?1;2c"
 212 #define VT102ID "\033[?6c"
 213 
 214 static unsigned char * translations[] = {
 215 /* 8-bit Latin-1 mapped to the PC charater set: '\0' means non-printable */
 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 /* vt100 graphics */
 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 /* IBM graphics: minimal translations (CR, LF, LL, SO, SI and ESC) */
 247 (unsigned char *)
 248         "\000\001\002\003\004\005\006\007\010\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  * gotoxy() must verify all boundaries, because the arguments
 275  * might also be negative. If the given position is out of
 276  * bounds, the cursor is placed at the nearest margin.
 277  */
 278 static void gotoxy(int currcons, int new_x, int new_y)
     /* [previous][next][first][last][top][bottom][index][help] */
 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  * *Very* limited hardware scrollback support..
 307  */
 308 static unsigned short __real_origin;
 309 static unsigned short __origin;
 310 
 311 static inline void __set_origin(unsigned short offset)
     /* [previous][next][first][last][top][bottom][index][help] */
 312 {
 313 #ifdef CONFIG_SELECTION
 314         clear_selection();
 315 #endif /* CONFIG_SELECTION */
 316         cli();
 317         __origin = offset;
 318         outb_p(12, video_port_reg);
 319         outb_p(offset >> 8, video_port_val);
 320         outb_p(13, video_port_reg);
 321         outb_p(offset, video_port_val);
 322         sti();
 323 }
 324 
 325 void scrollback(int lines)
     /* [previous][next][first][last][top][bottom][index][help] */
 326 {
 327         if (!lines)
 328                 lines = video_num_lines/2;
 329         lines *= video_num_columns;
 330         lines = __origin - lines;
 331         if (lines < 0)
 332                 lines = 0;
 333         __set_origin(lines);
 334 }
 335 
 336 void scrollfront(int lines)
     /* [previous][next][first][last][top][bottom][index][help] */
 337 {
 338         if (!lines)
 339                 lines = video_num_lines/2;
 340         lines *= video_num_columns;
 341         lines = __origin + lines;
 342         if (lines > __real_origin)
 343                 lines = __real_origin;
 344         __set_origin(lines);
 345 }
 346 
 347 static void set_origin(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 348 {
 349         if (video_type != VIDEO_TYPE_EGAC && video_type != VIDEO_TYPE_EGAM)
 350                 return;
 351         if (currcons != fg_console || console_blanked || vcmode == KD_GRAPHICS)
 352                 return;
 353         __real_origin = (origin-video_mem_base) >> 1;
 354         __set_origin(__real_origin);
 355 }
 356 
 357 static inline void hide_cursor(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 358 {
 359         outb_p(14, video_port_reg);
 360         outb_p(0xff&((scr_end-video_mem_base)>>9), video_port_val);
 361         outb_p(15, video_port_reg);
 362         outb_p(0xff&((scr_end-video_mem_base)>>1), video_port_val);
 363 }
 364 
 365 static inline void set_cursor(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 366 {
 367         if (currcons != fg_console || console_blanked || vcmode == KD_GRAPHICS)
 368                 return;
 369         if (__real_origin != __origin)
 370                 set_origin(__real_origin);
 371         cli();
 372         if (deccm) {
 373                 outb_p(14, video_port_reg);
 374                 outb_p(0xff&((pos-video_mem_base)>>9), video_port_val);
 375                 outb_p(15, video_port_reg);
 376                 outb_p(0xff&((pos-video_mem_base)>>1), video_port_val);
 377         } else
 378                 hide_cursor(currcons);
 379         sti();
 380 }
 381 
 382 static void scrup(int currcons, unsigned int t, unsigned int b)
     /* [previous][next][first][last][top][bottom][index][help] */
 383 {
 384         int hardscroll = 1;
 385 
 386         if (b > video_num_lines || t >= b)
 387                 return;
 388         if (video_type != VIDEO_TYPE_EGAC && video_type != VIDEO_TYPE_EGAM)
 389                 hardscroll = 0;
 390         else if (t || b != video_num_lines)
 391                 hardscroll = 0;
 392         if (hardscroll) {
 393                 origin += video_size_row;
 394                 pos += video_size_row;
 395                 scr_end += video_size_row;
 396                 if (scr_end > video_mem_end) {
 397                         __asm__("cld\n\t"
 398                                 "rep\n\t"
 399                                 "movsl\n\t"
 400                                 "movl _video_num_columns,%1\n\t"
 401                                 "rep\n\t"
 402                                 "stosw"
 403                                 : /* no output */
 404                                 :"a" (video_erase_char),
 405                                 "c" ((video_num_lines-1)*video_num_columns>>1),
 406                                 "D" (video_mem_start),
 407                                 "S" (origin)
 408                                 :"cx","di","si");
 409                         scr_end -= origin-video_mem_start;
 410                         pos -= origin-video_mem_start;
 411                         origin = video_mem_start;
 412                 } else {
 413                         __asm__("cld\n\t"
 414                                 "rep\n\t"
 415                                 "stosw"
 416                                 : /* no output */
 417                                 :"a" (video_erase_char),
 418                                 "c" (video_num_columns),
 419                                 "D" (scr_end-video_size_row)
 420                                 :"cx","di");
 421                 }
 422                 set_origin(currcons);
 423         } else {
 424                 __asm__("cld\n\t"
 425                         "rep\n\t"
 426                         "movsl\n\t"
 427                         "movl _video_num_columns,%%ecx\n\t"
 428                         "rep\n\t"
 429                         "stosw"
 430                         : /* no output */
 431                         :"a" (video_erase_char),
 432                         "c" ((b-t-1)*video_num_columns>>1),
 433                         "D" (origin+video_size_row*t),
 434                         "S" (origin+video_size_row*(t+1))
 435                         :"cx","di","si");
 436         }
 437 }
 438 
 439 static void scrdown(int currcons, unsigned int t, unsigned int b)
     /* [previous][next][first][last][top][bottom][index][help] */
 440 {
 441         if (b > video_num_lines || t >= b)
 442                 return;
 443         __asm__("std\n\t"
 444                 "rep\n\t"
 445                 "movsl\n\t"
 446                 "addl $2,%%edi\n\t"     /* %edi has been decremented by 4 */
 447                 "movl _video_num_columns,%%ecx\n\t"
 448                 "rep\n\t"
 449                 "stosw\n\t"
 450                 "cld"
 451                 : /* no output */
 452                 :"a" (video_erase_char),
 453                 "c" ((b-t-1)*video_num_columns>>1),
 454                 "D" (origin+video_size_row*b-4),
 455                 "S" (origin+video_size_row*(b-1)-4)
 456                 :"ax","cx","di","si");
 457 }
 458 
 459 static void lf(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 460 {
 461         if (y+1<bottom) {
 462                 y++;
 463                 pos += video_size_row;
 464                 return;
 465         } else 
 466                 scrup(currcons,top,bottom);
 467         need_wrap = 0;
 468 }
 469 
 470 static void ri(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 471 {
 472         if (y>top) {
 473                 y--;
 474                 pos -= video_size_row;
 475                 return;
 476         } else
 477                 scrdown(currcons,top,bottom);
 478         need_wrap = 0;
 479 }
 480 
 481 static inline void cr(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 482 {
 483         pos -= x<<1;
 484         need_wrap = x = 0;
 485 }
 486 
 487 static inline void bs(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 488 {
 489         if (x) {
 490                 pos -= 2;
 491                 x--;
 492                 need_wrap = 0;
 493         }
 494 }
 495 
 496 static inline void del(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 497 {
 498         if (x) {
 499                 pos -= 2;
 500                 x--;
 501                 *(unsigned short *)pos = video_erase_char;
 502                 need_wrap = 0;
 503         }
 504 }
 505 
 506 static void csi_J(int currcons, int vpar)
     /* [previous][next][first][last][top][bottom][index][help] */
 507 {
 508         unsigned long count;
 509         unsigned long start;
 510 
 511         switch (vpar) {
 512                 case 0: /* erase from cursor to end of display */
 513                         count = (scr_end-pos)>>1;
 514                         start = pos;
 515                         break;
 516                 case 1: /* erase from start to cursor */
 517                         count = ((pos-origin)>>1)+1;
 518                         start = origin;
 519                         break;
 520                 case 2: /* erase whole display */
 521                         count = video_num_columns * video_num_lines;
 522                         start = origin;
 523                         break;
 524                 default:
 525                         return;
 526         }
 527         __asm__("cld\n\t"
 528                 "rep\n\t"
 529                 "stosw\n\t"
 530                 : /* no output */
 531                 :"c" (count),
 532                 "D" (start),"a" (video_erase_char)
 533                 :"cx","di");
 534         need_wrap = 0;
 535 }
 536 
 537 static void csi_K(int currcons, int vpar)
     /* [previous][next][first][last][top][bottom][index][help] */
 538 {
 539         long count;
 540         long start;
 541 
 542         switch (vpar) {
 543                 case 0: /* erase from cursor to end of line */
 544                         count = video_num_columns-x;
 545                         start = pos;
 546                         break;
 547                 case 1: /* erase from start of line to cursor */
 548                         start = pos - (x<<1);
 549                         count = x+1;
 550                         break;
 551                 case 2: /* erase whole line */
 552                         start = pos - (x<<1);
 553                         count = video_num_columns;
 554                         break;
 555                 default:
 556                         return;
 557         }
 558         __asm__("cld\n\t"
 559                 "rep\n\t"
 560                 "stosw\n\t"
 561                 : /* no output */
 562                 :"c" (count),
 563                 "D" (start),"a" (video_erase_char)
 564                 :"cx","di");
 565         need_wrap = 0;
 566 }
 567 
 568 /*
 569  *  I hope this works. The monochrome part is untested.
 570  */
 571 static void update_attr(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 572 {
 573         attr = color;
 574         if (can_do_color) {
 575                 if (underline)
 576                         attr = (attr & 0xf0) | ulcolor;
 577                 else if (intensity == 0)
 578                         attr = (attr & 0xf0) | halfcolor;
 579         }
 580         if (reverse ^ decscnm)
 581                 attr = (attr & 0x88) | (((attr >> 4) | (attr << 4)) & 0x77);
 582         if (blink)
 583                 attr ^= 0x80;
 584         if (intensity == 2)
 585                 attr ^= 0x08;
 586         if (!can_do_color) {
 587                 if (underline)
 588                         attr = (attr & 0xf8) | 0x01;
 589                 else if (intensity == 0)
 590                         attr = (attr & 0xf0) | 0x08;
 591         }
 592         if (decscnm)
 593                 video_erase_char = ((color & 0x88) | (((color >> 4) |
 594                         (color << 4)) & 0x77) << 8) | ' ';
 595         else
 596                 video_erase_char = (color << 8) | ' ';
 597 }
 598 
 599 static void default_attr(int currcons) {
     /* [previous][next][first][last][top][bottom][index][help] */
 600         intensity = 1;
 601         underline = 0;
 602         reverse = 0;
 603         blink = 0;
 604         color = def_color;
 605 }
 606 
 607 static void csi_m(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 608 {
 609         int i;
 610 
 611         for (i=0;i<=npar;i++)
 612                 switch (par[i]) {
 613                         case 0: /* all attributes off */
 614                                 default_attr(currcons);
 615                                 break;
 616                         case 1:
 617                                 intensity = 2;
 618                                 break;
 619                         case 2:
 620                                 intensity = 0;
 621                                 break;
 622                         case 4:
 623                                 underline = 1;
 624                                 break;
 625                         case 5:
 626                                 blink = 1;
 627                                 break;
 628                         case 7:
 629                                 reverse = 1;
 630                                 break;
 631                         case 21:
 632                         case 22:
 633                                 intensity = 1;
 634                                 break;
 635                         case 24:
 636                                 underline = 0;
 637                                 break;
 638                         case 25:
 639                                 blink = 0;
 640                                 break;
 641                         case 27:
 642                                 reverse = 0;
 643                                 break;
 644                         case 39:
 645                                 color = (def_color & 0x0f) | background;
 646                                 break;
 647                         case 49:
 648                                 color = (def_color & 0xf0) | foreground;
 649                                 break;
 650                         default:
 651                                 if (par[i] >= 30 && par[i] <= 37)
 652                                         color = color_table[par[i]-30]
 653                                                 | background; 
 654                                 else if (par[i] >= 40 && par[i] <= 47)
 655                                         color = (color_table[par[i]-40]<<4)
 656                                                 | foreground;
 657                                 break;
 658                 }
 659         update_attr(currcons);
 660 }
 661 
 662 static void respond_string(char * p, int currcons, struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 663 {
 664         while (*p) {
 665                 put_tty_queue(*p, &tty->read_q);
 666                 p++;
 667         }
 668         TTY_READ_FLUSH(tty);
 669 }
 670 
 671 static void respond_num(unsigned int n, int currcons, struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 672 {
 673         char buff[3];
 674         int i = 0;
 675 
 676         do {
 677                 buff[i++] = (n%10)+'0';
 678                 n /= 10;
 679         } while(n && i < 3);    /* We'll take no chances */
 680         while (i--) {
 681                 put_tty_queue(buff[i], &tty->read_q);
 682         }
 683         /* caller must flush */
 684 }
 685 
 686 static void cursor_report(int currcons, struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 687 {
 688         put_tty_queue('\033', &tty->read_q);
 689         put_tty_queue('[', &tty->read_q);
 690         respond_num(y + (decom ? top+1 : 1), currcons, tty);
 691         put_tty_queue(';', &tty->read_q);
 692         respond_num(x+1, currcons, tty);
 693         put_tty_queue('R', &tty->read_q);
 694         TTY_READ_FLUSH(tty);
 695 }
 696 
 697 static inline void status_report(int currcons, struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 698 {
 699         respond_string("\033[0n", currcons, tty);       /* Terminal ok */
 700 }
 701 
 702 static inline void respond_ID(int currcons, struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 703 {
 704         respond_string(VT102ID, currcons, tty);
 705 }
 706 
 707 static void invert_screen(int currcons) {
     /* [previous][next][first][last][top][bottom][index][help] */
 708         unsigned char *p;
 709 
 710         if (can_do_color)
 711                 for (p = (unsigned char *)origin+1; p < (unsigned char *)scr_end; p+=2)
 712                         *p = (*p & 0x88) | (((*p >> 4) | (*p << 4)) & 0x77);
 713         else
 714                 for (p = (unsigned char *)origin+1; p < (unsigned char *)scr_end; p+=2)
 715                         *p ^= *p & 0x07 == 1 ? 0x70 : 0x77;
 716 }
 717 
 718 static void set_mode(int currcons, int on_off)
     /* [previous][next][first][last][top][bottom][index][help] */
 719 {
 720         int i;
 721 
 722         for (i=0; i<=npar; i++)
 723                 if (ques) switch(par[i]) {      /* DEC private modes set/reset */
 724                         case 1:                 /* Cursor keys send ^[Ox/^[[x */
 725                                 if (on_off)
 726                                         set_kbd(decckm);
 727                                 else
 728                                         clr_kbd(decckm);
 729                                 break;
 730                         case 3: /* 80/132 mode switch unimplemented */
 731                                 csi_J(currcons,2);
 732                                 gotoxy(currcons,0,0);
 733                                 break;
 734                         case 5:                 /* Inverted screen on/off */
 735                                 if (decscnm != on_off) {
 736                                         decscnm = on_off;
 737                                         invert_screen(currcons);
 738                                         update_attr(currcons);
 739                                 }
 740                                 break;
 741                         case 6:                 /* Origin relative/absolute */
 742                                 decom = on_off;
 743                                 gotoxy(currcons,0,0);
 744                                 break;
 745                         case 7:                 /* Autowrap on/off */
 746                                 decawm = on_off;
 747                                 break;
 748                         case 8:                 /* Autorepeat on/off */
 749                                 if (on_off)
 750                                         set_kbd(decarm);
 751                                 else
 752                                         clr_kbd(decarm);
 753                                 break;
 754                         case 25:                /* Cursor on/off */
 755                                 deccm = on_off;
 756                                 set_cursor(currcons);
 757                                 break;
 758                 } else switch(par[i]) {         /* ANSI modes set/reset */
 759                         case 4:                 /* Insert Mode on/off */
 760                                 decim = on_off;
 761                                 break;
 762                         case 20:                /* Lf, Enter == CrLf/Lf */
 763                                 if (on_off)
 764                                         set_kbd(lnm);
 765                                 else
 766                                         clr_kbd(lnm);
 767                                 break;
 768                 }
 769 }
 770 
 771 static void setterm_command(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 772 {
 773         switch(par[0]) {
 774                 case 1: /* set color for underline mode */
 775                         if (can_do_color && par[1] < 16) {
 776                                 ulcolor = color_table[par[1]];
 777                                 if (underline)
 778                                         update_attr(currcons);
 779                         }
 780                         break;
 781                 case 2: /* set color for half intensity mode */
 782                         if (can_do_color && par[1] < 16) {
 783                                 halfcolor = color_table[par[1]];
 784                                 if (intensity == 0)
 785                                         update_attr(currcons);
 786                         }
 787                         break;
 788                 case 8: /* store colors as defaults */
 789                         def_color = attr;
 790                         default_attr(currcons);
 791                         update_attr(currcons);
 792                         break;
 793                 case 9: /* set blanking interval */
 794                         blankinterval = ((par[1] < 60) ? par[1] : 60) * 60 * HZ;
 795                         break;
 796         }
 797 }
 798 
 799 static void insert_char(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 800 {
 801         unsigned int i = x;
 802         unsigned short tmp, old = video_erase_char;
 803         unsigned short * p = (unsigned short *) pos;
 804 
 805         while (i++ < video_num_columns) {
 806                 tmp = *p;
 807                 *p = old;
 808                 old = tmp;
 809                 p++;
 810         }
 811         need_wrap = 0;
 812 }
 813 
 814 static void insert_line(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 815 {
 816         scrdown(currcons,y,bottom);
 817         need_wrap = 0;
 818 }
 819 
 820 static void delete_char(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 821 {
 822         unsigned int i = x;
 823         unsigned short * p = (unsigned short *) pos;
 824 
 825         while (++i < video_num_columns) {
 826                 *p = *(p+1);
 827                 p++;
 828         }
 829         *p = video_erase_char;
 830         need_wrap = 0;
 831 }
 832 
 833 static void delete_line(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 834 {
 835         scrup(currcons,y,bottom);
 836         need_wrap = 0;
 837 }
 838 
 839 static void csi_at(int currcons, unsigned int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
 840 {
 841         if (nr > video_num_columns)
 842                 nr = video_num_columns;
 843         else if (!nr)
 844                 nr = 1;
 845         while (nr--)
 846                 insert_char(currcons);
 847 }
 848 
 849 static void csi_L(int currcons, unsigned int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
 850 {
 851         if (nr > video_num_lines)
 852                 nr = video_num_lines;
 853         else if (!nr)
 854                 nr = 1;
 855         while (nr--)
 856                 insert_line(currcons);
 857 }
 858 
 859 static void csi_P(int currcons, unsigned int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
 860 {
 861         if (nr > video_num_columns)
 862                 nr = video_num_columns;
 863         else if (!nr)
 864                 nr = 1;
 865         while (nr--)
 866                 delete_char(currcons);
 867 }
 868 
 869 static void csi_M(int currcons, unsigned int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
 870 {
 871         if (nr > video_num_lines)
 872                 nr = video_num_lines;
 873         else if (!nr)
 874                 nr=1;
 875         while (nr--)
 876                 delete_line(currcons);
 877 }
 878 
 879 static void save_cur(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 880 {
 881         saved_x         = x;
 882         saved_y         = y;
 883         s_intensity     = intensity;
 884         s_underline     = underline;
 885         s_blink         = blink;
 886         s_reverse       = reverse;
 887         s_charset       = charset;
 888         s_color         = color;
 889         saved_G0        = G0_charset;
 890         saved_G1        = G1_charset;
 891 }
 892 
 893 static void restore_cur(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 894 {
 895         gotoxy(currcons,saved_x,saved_y);
 896         intensity       = s_intensity;
 897         underline       = s_underline;
 898         blink           = s_blink;
 899         reverse         = s_reverse;
 900         charset         = s_charset;
 901         color           = s_color;
 902         G0_charset      = saved_G0;
 903         G1_charset      = saved_G1;
 904         translate       = charset ? G1_charset : G0_charset;
 905         update_attr(currcons);
 906         need_wrap = 0;
 907 }
 908 
 909 enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey, 
 910         EShash, ESsetG0, ESsetG1, ESignore };
 911 
 912 static void reset_terminal(int currcons, int do_clear)
     /* [previous][next][first][last][top][bottom][index][help] */
 913 {
 914         top             = 0;
 915         bottom          = video_num_lines;
 916         state           = ESnormal;
 917         ques            = 0;
 918         translate       = NORM_TRANS;
 919         G0_charset      = NORM_TRANS;
 920         G1_charset      = GRAF_TRANS;
 921         charset         = 0;
 922         need_wrap       = 0;
 923 
 924         decscnm         = 0;
 925         decom           = 0;
 926         decawm          = 1;
 927         deccm           = 1;
 928         decim           = 0;
 929 
 930         set_kbd(decarm);
 931         clr_kbd(decckm);
 932         clr_kbd(kbdapplic);
 933         clr_kbd(lnm);
 934         kbd_table[currcons].flags =
 935                 (kbd_table[currcons].flags & ~LED_MASK) |
 936                 (kbd_table[currcons].default_flags & LED_MASK);
 937         kbdmode         = 0;
 938         set_leds();
 939 
 940         default_attr(currcons);
 941         update_attr(currcons);
 942 
 943         tab_stop[0]     = 0x01010100;
 944         tab_stop[1]     =
 945         tab_stop[2]     =
 946         tab_stop[3]     =
 947         tab_stop[4]     = 0x01010101;
 948 
 949         if (do_clear) {
 950                 gotoxy(currcons,0,0);
 951                 csi_J(currcons,2);
 952                 save_cur(currcons);
 953         }
 954 }
 955 
 956 void con_write(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 957 {
 958         int c;
 959         unsigned int currcons;
 960 
 961         wake_up_interruptible(&tty->write_q.proc_list);
 962         currcons = tty->line - 1;
 963         if (currcons >= NR_CONSOLES) {
 964                 printk("con_write: illegal tty (%d)\n", currcons);
 965                 return;
 966         }
 967 #ifdef CONFIG_SELECTION
 968         /* clear the selection as soon as any characters are to be written
 969            out on the console holding the selection. */
 970         if (!EMPTY(&tty->write_q) && currcons == sel_cons)
 971                 clear_selection();
 972 #endif /* CONFIG_SELECTION */
 973         while (!tty->stopped && (c = get_tty_queue(&tty->write_q)) >= 0) {
 974                 if (state == ESnormal && translate[c]) {
 975                         if (need_wrap) {
 976                                 cr(currcons);
 977                                 lf(currcons);
 978                         }
 979                         if (decim)
 980                                 insert_char(currcons);
 981                         c = translate[c];
 982                         *(unsigned short *) pos = (attr << 8) + c;
 983                         if (x == video_num_columns - 1)
 984                                 need_wrap = decawm;
 985                         else {
 986                                 x++;
 987                                 pos+=2;
 988                         }
 989                         continue;
 990                 }
 991 
 992                 /*
 993                  *  Control characters can be used in the _middle_
 994                  *  of an escape sequence.
 995                  */
 996                 switch (c) {
 997                         case 7:
 998                                 kd_mksound(0x637, HZ/8);
 999                                 continue;
1000                         case 8:
1001                                 bs(currcons);
1002                                 continue;
1003                         case 9:
1004                                 pos -= (x << 1);
1005                                 while (x < video_num_columns - 1) {
1006                                         x++;
1007                                         if (tab_stop[x >> 5] & (1 << (x & 31)))
1008                                                 break;
1009                                 }
1010                                 pos += (x << 1);
1011                                 continue;
1012                         case 10: case 11: case 12:
1013                                 lf(currcons);
1014                                 if (!is_kbd(lnm))
1015                                         continue;
1016                         case 13:
1017                                 cr(currcons);
1018                                 continue;
1019                         case 14:
1020                                 charset = 1;
1021                                 translate = G1_charset;
1022                                 continue;
1023                         case 15:
1024                                 charset = 0;
1025                                 translate = G0_charset;
1026                                 continue;
1027                         case 24: case 26:
1028                                 state = ESnormal;
1029                                 continue;
1030                         case 27:
1031                                 state = ESesc;
1032                                 continue;
1033                         case 127:
1034                                 del(currcons);
1035                                 continue;
1036                         case 128+27:
1037                                 state = ESsquare;
1038                                 continue;
1039                 }
1040                 switch(state) {
1041                         case ESesc:
1042                                 state = ESnormal;
1043                                 switch (c) {
1044                                   case '[':
1045                                         state = ESsquare;
1046                                         continue;
1047                                   case 'E':
1048                                         cr(currcons);
1049                                         lf(currcons);
1050                                         continue;
1051                                   case 'M':
1052                                         ri(currcons);
1053                                         continue;
1054                                   case 'D':
1055                                         lf(currcons);
1056                                         continue;
1057                                   case 'H':
1058                                         tab_stop[x >> 5] |= (1 << (x & 31));
1059                                         continue;
1060                                   case 'Z':
1061                                         respond_ID(currcons,tty);
1062                                         continue;
1063                                   case '7':
1064                                         save_cur(currcons);
1065                                         continue;
1066                                   case '8':
1067                                         restore_cur(currcons);
1068                                         continue;
1069                                   case '(':
1070                                         state = ESsetG0;
1071                                         continue;
1072                                   case ')':
1073                                         state = ESsetG1;
1074                                         continue;
1075                                   case '#':
1076                                         state = EShash;
1077                                         continue;
1078                                   case 'c':
1079                                         reset_terminal(currcons,1);
1080                                         continue;
1081                                   case '>':  /* Numeric keypad */
1082                                         clr_kbd(kbdapplic);
1083                                         continue;
1084                                   case '=':  /* Appl. keypad */
1085                                         set_kbd(kbdapplic);
1086                                         continue;
1087                                 }       
1088                                 continue;
1089                         case ESsquare:
1090                                 for(npar = 0 ; npar < NPAR ; npar++)
1091                                         par[npar] = 0;
1092                                 npar = 0;
1093                                 state = ESgetpars;
1094                                 if (c == '[') { /* Function key */
1095                                         state=ESfunckey;
1096                                         continue;
1097                                 }
1098                                 ques = (c=='?');
1099                                 if (ques)
1100                                         continue;
1101                         case ESgetpars:
1102                                 if (c==';' && npar<NPAR-1) {
1103                                         npar++;
1104                                         continue;
1105                                 } else if (c>='0' && c<='9') {
1106                                         par[npar] *= 10;
1107                                         par[npar] += c-'0';
1108                                         continue;
1109                                 } else state=ESgotpars;
1110                         case ESgotpars:
1111                                 state = ESnormal;
1112                                 switch(c) {
1113                                         case 'h':
1114                                                 set_mode(currcons,1);
1115                                                 continue;
1116                                         case 'l':
1117                                                 set_mode(currcons,0);
1118                                                 continue;
1119                                         case 'n':
1120                                                 if (!ques)
1121                                                         if (par[0] == 5)
1122                                                                 status_report(currcons,tty);
1123                                                         else if (par[0] == 6)
1124                                                                 cursor_report(currcons,tty);
1125                                                 continue;
1126                                 }
1127                                 if (ques) {
1128                                         ques = 0;
1129                                         continue;
1130                                 }
1131                                 switch(c) {
1132                                         case 'G': case '`':
1133                                                 if (par[0]) par[0]--;
1134                                                 gotoxy(currcons,par[0],y);
1135                                                 continue;
1136                                         case 'A':
1137                                                 if (!par[0]) par[0]++;
1138                                                 gotoxy(currcons,x,y-par[0]);
1139                                                 continue;
1140                                         case 'B': case 'e':
1141                                                 if (!par[0]) par[0]++;
1142                                                 gotoxy(currcons,x,y+par[0]);
1143                                                 continue;
1144                                         case 'C': case 'a':
1145                                                 if (!par[0]) par[0]++;
1146                                                 gotoxy(currcons,x+par[0],y);
1147                                                 continue;
1148                                         case 'D':
1149                                                 if (!par[0]) par[0]++;
1150                                                 gotoxy(currcons,x-par[0],y);
1151                                                 continue;
1152                                         case 'E':
1153                                                 if (!par[0]) par[0]++;
1154                                                 gotoxy(currcons,0,y+par[0]);
1155                                                 continue;
1156                                         case 'F':
1157                                                 if (!par[0]) par[0]++;
1158                                                 gotoxy(currcons,0,y-par[0]);
1159                                                 continue;
1160                                         case 'd':
1161                                                 if (par[0]) par[0]--;
1162                                                 gotoxy(currcons,x,par[0]);
1163                                                 continue;
1164                                         case 'H': case 'f':
1165                                                 if (par[0]) par[0]--;
1166                                                 if (par[1]) par[1]--;
1167                                                 gotoxy(currcons,par[1],par[0]);
1168                                                 continue;
1169                                         case 'J':
1170                                                 csi_J(currcons,par[0]);
1171                                                 continue;
1172                                         case 'K':
1173                                                 csi_K(currcons,par[0]);
1174                                                 continue;
1175                                         case 'L':
1176                                                 csi_L(currcons,par[0]);
1177                                                 continue;
1178                                         case 'M':
1179                                                 csi_M(currcons,par[0]);
1180                                                 continue;
1181                                         case 'P':
1182                                                 csi_P(currcons,par[0]);
1183                                                 continue;
1184                                         case 'c':
1185                                                 if (!par[0])
1186                                                         respond_ID(currcons,tty);
1187                                                 continue;
1188                                         case 'g':
1189                                                 if (!par[0])
1190                                                         tab_stop[x >> 5] &= ~(1 << (x & 31));
1191                                                 else if (par[0] == 3) {
1192                                                         tab_stop[0] =
1193                                                         tab_stop[1] =
1194                                                         tab_stop[2] =
1195                                                         tab_stop[3] =
1196                                                         tab_stop[4] = 0;
1197                                                 }
1198                                                 continue;
1199                                         case 'm':
1200                                                 csi_m(currcons);
1201                                                 continue;
1202                                         case 'r':
1203                                                 if (!par[0])
1204                                                         par[0]++;
1205                                                 if (!par[1])
1206                                                         par[1] = video_num_lines;
1207                                                 /* Minimum allowed region is 2 lines */
1208                                                 if (par[0] < par[1] &&
1209                                                     par[1] <= video_num_lines) {
1210                                                         top=par[0]-1;
1211                                                         bottom=par[1];
1212                                                         gotoxy(currcons,0,0);
1213                                                 }
1214                                                 continue;
1215                                         case 's':
1216                                                 save_cur(currcons);
1217                                                 continue;
1218                                         case 'u':
1219                                                 restore_cur(currcons);
1220                                                 continue;
1221                                         case '@':
1222                                                 csi_at(currcons,par[0]);
1223                                                 continue;
1224                                         case ']': /* setterm functions */
1225                                                 setterm_command(currcons);
1226                                                 continue;
1227                                 }
1228                                 continue;
1229                         case ESfunckey:
1230                                 state = ESnormal;
1231                                 continue;
1232                         case EShash:
1233                                 state = ESnormal;
1234                                 if (c == '8') {
1235                                         /* DEC screen alignment test. kludge :-) */
1236                                         video_erase_char =
1237                                                 (video_erase_char & 0xff00) | 'E';
1238                                         csi_J(currcons, 2);
1239                                         video_erase_char =
1240                                                 (video_erase_char & 0xff00) | ' ';
1241                                 }
1242                                 continue;
1243                         case ESsetG0:
1244                                 if (c == '0')
1245                                         G0_charset = GRAF_TRANS;
1246                                 else if (c == 'B')
1247                                         G0_charset = NORM_TRANS;
1248                                 else if (c == 'U')
1249                                         G0_charset = NULL_TRANS;
1250                                 if (charset == 0)
1251                                         translate = G0_charset;
1252                                 state = ESnormal;
1253                                 continue;
1254                         case ESsetG1:
1255                                 if (c == '0')
1256                                         G1_charset = GRAF_TRANS;
1257                                 else if (c == 'B')
1258                                         G1_charset = NORM_TRANS;
1259                                 else if (c == 'U')
1260                                         G1_charset = NULL_TRANS;
1261                                 if (charset == 1)
1262                                         translate = G1_charset;
1263                                 state = ESnormal;
1264                                 continue;
1265                         default:
1266                                 state = ESnormal;
1267                 }
1268         }
1269         if (vcmode == KD_GRAPHICS)
1270                 return;
1271         set_cursor(currcons);
1272 }
1273 
1274 void do_keyboard_interrupt(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1275 {
1276         TTY_READ_FLUSH(TTY_TABLE(0));
1277         timer_active &= ~(1<<BLANK_TIMER);
1278         if (vt_cons[fg_console].vc_mode == KD_GRAPHICS)
1279                 return;
1280         if (console_blanked) {
1281                 timer_table[BLANK_TIMER].expires = 0;
1282                 timer_active |= 1<<BLANK_TIMER;
1283         } else if (blankinterval) {
1284                 timer_table[BLANK_TIMER].expires = jiffies + blankinterval;
1285                 timer_active |= 1<<BLANK_TIMER;
1286         }
1287 }       
1288 
1289 void * memsetw(void * s,unsigned short c,int count)
     /* [previous][next][first][last][top][bottom][index][help] */
1290 {
1291 __asm__("cld\n\t"
1292         "rep\n\t"
1293         "stosw"
1294         : /* no output */
1295         :"a" (c),"D" (s),"c" (count)
1296         :"cx","di");
1297 return s;
1298 }
1299 
1300 void console_print(const char * b)
     /* [previous][next][first][last][top][bottom][index][help] */
1301 {
1302         int currcons = fg_console;
1303         unsigned char c;
1304 
1305         if (!printable || currcons<0 || currcons>=NR_CONSOLES)
1306                 return;
1307         while ((c = *(b++)) != 0) {
1308                 if (c == 10 || c == 13 || need_wrap) {
1309                         if (c != 13)
1310                                 lf(currcons);
1311                         cr(currcons);
1312                         if (c == 10 || c == 13)
1313                                 continue;
1314                 }
1315                 *(unsigned short *) pos = (attr << 8) + c;
1316                 if (x == video_num_columns - 1) {
1317                         need_wrap = 1;
1318                         continue;
1319                 }
1320                 x++;
1321                 pos+=2;
1322         }
1323         set_cursor(currcons);
1324         if (vt_cons[fg_console].vc_mode == KD_GRAPHICS)
1325                 return;
1326         timer_active &= ~(1<<BLANK_TIMER);
1327         if (console_blanked) {
1328                 timer_table[BLANK_TIMER].expires = 0;
1329                 timer_active |= 1<<BLANK_TIMER;
1330         } else if (blankinterval) {
1331                 timer_table[BLANK_TIMER].expires = jiffies + blankinterval;
1332                 timer_active |= 1<<BLANK_TIMER;
1333         }
1334 }
1335 
1336 /*
1337  *  long con_init(long);
1338  *
1339  * This routine initalizes console interrupts, and does nothing
1340  * else. If you want the screen to clear, call tty_write with
1341  * the appropriate escape-sequece.
1342  *
1343  * Reads the information preserved by setup.s to determine the current display
1344  * type and sets everything accordingly.
1345  */
1346 long con_init(long kmem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
1347 {
1348         char *display_desc = "????";
1349         int currcons = 0;
1350         long base;
1351         int orig_x = ORIG_X;
1352         int orig_y = ORIG_Y;
1353 
1354         vc_scrmembuf = (unsigned short *) kmem_start;
1355         video_num_columns = ORIG_VIDEO_COLS;
1356         video_size_row = video_num_columns * 2;
1357         video_num_lines = ORIG_VIDEO_LINES;
1358         video_page = ORIG_VIDEO_PAGE;
1359         screen_size = (video_num_lines * video_size_row);
1360         kmem_start += NR_CONSOLES * screen_size;
1361         timer_table[BLANK_TIMER].fn = blank_screen;
1362         timer_table[BLANK_TIMER].expires = 0;
1363         if (blankinterval) {
1364                 timer_table[BLANK_TIMER].expires = jiffies+blankinterval;
1365                 timer_active |= 1<<BLANK_TIMER;
1366         }
1367         
1368         if (ORIG_VIDEO_MODE == 7)       /* Is this a monochrome display? */
1369         {
1370                 video_mem_base = 0xb0000;
1371                 video_port_reg = 0x3b4;
1372                 video_port_val = 0x3b5;
1373                 if ((ORIG_VIDEO_EGA_BX & 0xff) != 0x10)
1374                 {
1375                         video_type = VIDEO_TYPE_EGAM;
1376                         video_mem_term = 0xb8000;
1377                         display_desc = "EGA+";
1378                 }
1379                 else
1380                 {
1381                         video_type = VIDEO_TYPE_MDA;
1382                         video_mem_term = 0xb2000;
1383                         display_desc = "*MDA";
1384                 }
1385         }
1386         else                            /* If not, it is color. */
1387         {
1388                 can_do_color = 1;
1389                 video_mem_base = 0xb8000;
1390                 video_port_reg  = 0x3d4;
1391                 video_port_val  = 0x3d5;
1392                 if ((ORIG_VIDEO_EGA_BX & 0xff) != 0x10)
1393                 {
1394                         video_type = VIDEO_TYPE_EGAC;
1395                         video_mem_term = 0xc0000;
1396                         display_desc = "EGA+";
1397                 }
1398                 else
1399                 {
1400                         video_type = VIDEO_TYPE_CGA;
1401                         video_mem_term = 0xba000;
1402                         display_desc = "*CGA";
1403                 }
1404         }
1405         
1406         /* Initialize the variables used for scrolling (mostly EGA/VGA) */
1407 
1408         base = (long)vc_scrmembuf;
1409         for (currcons = 0; currcons<NR_CONSOLES; currcons++) {
1410                 pos = origin = video_mem_start = base;
1411                 scr_end = video_mem_end = (base += screen_size);
1412                 vc_scrbuf[currcons] = (unsigned short *) origin;
1413                 vcmode          = KD_TEXT;
1414                 vtmode.mode     = VT_AUTO;
1415                 vtmode.waitv    = 0;
1416                 vtmode.relsig   = 0;
1417                 vtmode.acqsig   = 0;
1418                 vtmode.frsig    = 0;
1419                 vtpid           = -1;
1420                 vtnewvt         = -1;
1421                 clr_kbd(kbdraw);
1422                 def_color       = 0x07;   /* white */
1423                 ulcolor         = 0x0f;   /* bold white */
1424                 halfcolor       = 0x08;   /* grey */
1425                 reset_terminal(currcons, currcons);
1426         }
1427         currcons = fg_console = 0;
1428 
1429         video_mem_start = video_mem_base;
1430         video_mem_end = video_mem_term;
1431         origin = video_mem_start;
1432         scr_end = video_mem_start + video_num_lines * video_size_row;
1433         gotoxy(currcons,0,0);
1434         save_cur(currcons);
1435         gotoxy(currcons,orig_x,orig_y);
1436         update_screen(fg_console);
1437         printable = 1;
1438         printk("Console: %s %s %dx%d, %d virtual consoles\n",
1439                 can_do_color?"colour":"mono",
1440                 display_desc,
1441                 video_num_columns,video_num_lines,
1442                 NR_CONSOLES);
1443         register_console(console_print);
1444         return kmem_start;
1445 }
1446 
1447 /*
1448  * kbdsave doesn't need to do anything: it's all handled automatically
1449  * with the new data structures..
1450  */
1451 void kbdsave(int new_console)
     /* [previous][next][first][last][top][bottom][index][help] */
1452 {
1453 }
1454 
1455 static void get_scrmem(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
1456 {
1457         memcpy((void *)vc_scrbuf[currcons],(void *)origin, screen_size);
1458         video_mem_start = (unsigned long)vc_scrbuf[currcons];
1459         origin  = video_mem_start;
1460         scr_end = video_mem_end = video_mem_start+screen_size;
1461         pos = origin + y*video_size_row + (x<<1);
1462 }
1463 
1464 static void set_scrmem(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
1465 {
1466         video_mem_start = video_mem_base;
1467         video_mem_end = video_mem_term;
1468         origin  = video_mem_start;
1469         scr_end = video_mem_start + screen_size;
1470         pos = origin + y*video_size_row + (x<<1);
1471         memcpy((void *)video_mem_base, (void *)vc_scrbuf[fg_console], screen_size);
1472 }
1473 
1474 void blank_screen(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1475 {
1476         if (console_blanked)
1477                 return;
1478         timer_table[BLANK_TIMER].fn = unblank_screen;
1479         get_scrmem(fg_console);
1480         hide_cursor(fg_console);
1481         console_blanked = 1;
1482         memsetw((void *)video_mem_base, 0x0020, video_mem_term-video_mem_base );
1483 }
1484 
1485 void unblank_screen(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1486 {
1487         if (!console_blanked)
1488                 return;
1489         timer_table[BLANK_TIMER].fn = blank_screen;
1490         if (blankinterval) {
1491                 timer_table[BLANK_TIMER].expires = jiffies + blankinterval;
1492                 timer_active |= 1<<BLANK_TIMER;
1493         }
1494         console_blanked = 0;
1495         set_scrmem(fg_console);
1496         set_origin(fg_console);
1497         set_cursor(fg_console);
1498 }
1499 
1500 void update_screen(int new_console)
     /* [previous][next][first][last][top][bottom][index][help] */
1501 {
1502         static int lock = 0;
1503 
1504         if (new_console == fg_console || lock)
1505                 return;
1506         lock = 1;
1507         kbdsave(new_console);
1508         get_scrmem(fg_console); 
1509         fg_console = new_console;
1510         set_scrmem(fg_console); 
1511         set_origin(fg_console);
1512         set_cursor(new_console);
1513         set_leds();
1514         lock = 0;
1515 }
1516 
1517 int do_screendump(int arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1518 {
1519         char *sptr, *buf = (char *)arg;
1520         int currcons, l;
1521 
1522         if (!suser())
1523                 return -EPERM;
1524         l = verify_area(VERIFY_WRITE, buf,2+video_num_columns*video_num_lines);
1525         if (l)
1526                 return l;
1527         currcons = get_fs_byte(buf+1);
1528         if ((currcons<0) || (currcons>=NR_CONSOLES))
1529                 return -EIO;
1530         put_fs_byte((char)(video_num_lines),buf++);     
1531         put_fs_byte((char)(video_num_columns),buf++);
1532         currcons = (currcons ? currcons-1 : fg_console);
1533         sptr = (char *) origin;
1534         for (l=video_num_lines*video_num_columns; l>0 ; l--, sptr++)
1535                 put_fs_byte(*sptr++,buf++);     
1536         return(0);
1537 }
1538 
1539 /*
1540  * All we do is set the write and ioctl subroutines; later on maybe we'll
1541  * dynamically allocate the console screen memory.
1542  */
1543 int con_open(struct tty_struct *tty, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1544 {
1545         tty->write = con_write;
1546         tty->ioctl = vt_ioctl;
1547         if (tty->line > NR_CONSOLES)
1548                 return -ENODEV;
1549         return 0;
1550 }
1551 
1552 #ifdef CONFIG_SELECTION
1553 /* correction factor for when screen is hardware-scrolled */
1554 #define hwscroll_offset ((__real_origin - __origin) << 1)
1555 
1556 /* set reverse video on characters s-e of console with selection. */
1557 static void highlight(const int currcons, const int s, const int e)
     /* [previous][next][first][last][top][bottom][index][help] */
1558 {
1559         unsigned char *p, *p1, *p2;
1560 
1561         p1 = (unsigned char *)origin - hwscroll_offset + s + 1;
1562         p2 = (unsigned char *)origin - hwscroll_offset + e + 1;
1563         if (p1 > p2)
1564         {
1565                 p = p1;
1566                 p1 = p2;
1567                 p2 = p;
1568         }
1569         for (p = p1; p <= p2; p += 2)
1570                 *p = (*p & 0x88) | ((*p << 4) & 0x70) | ((*p >> 4) & 0x07);
1571 }
1572 
1573 /* is c in range [a-zA-Z0-9_]? */
1574 static inline int inword(const char c) { return (isalnum(c) || c == '_'); }
     /* [previous][next][first][last][top][bottom][index][help] */
1575 
1576 /* does screen address p correspond to character at LH/RH edge of screen? */
1577 static inline int atedge(const int p)
     /* [previous][next][first][last][top][bottom][index][help] */
1578 {
1579         return (!(p % video_size_row) || !((p + 2) % video_size_row));
1580 }
1581 
1582 /* constrain v such that l <= v <= u */
1583 static inline short limit(const int v, const int l, const int u)
     /* [previous][next][first][last][top][bottom][index][help] */
1584 {
1585         return (v < l) ? l : ((v > u) ? u : v);
1586 }
1587 
1588 /* set the current selection. Invoked by ioctl(). */
1589 int set_selection(const int arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1590 {
1591         unsigned short *args, xs, ys, xe, ye;
1592         int currcons = fg_console;
1593         int sel_mode, new_sel_start, new_sel_end, spc;
1594         char *bp, *obp, *spos;
1595         int i, ps, pe;
1596         char *off = (char *)origin - hwscroll_offset;
1597 
1598         unblank_screen();
1599         args = (unsigned short *)(arg + 1);
1600         xs = get_fs_word(args++) - 1;
1601         ys = get_fs_word(args++) - 1;
1602         xe = get_fs_word(args++) - 1;
1603         ye = get_fs_word(args++) - 1;
1604         sel_mode = get_fs_word(args);
1605 
1606         xs = limit(xs, 0, video_num_columns - 1);
1607         ys = limit(ys, 0, video_num_lines - 1);
1608         xe = limit(xe, 0, video_num_columns - 1);
1609         ye = limit(ye, 0, video_num_lines - 1);
1610         ps = ys * video_size_row + (xs << 1);
1611         pe = ye * video_size_row + (xe << 1);
1612 
1613         if (ps > pe)    /* make sel_start <= sel_end */
1614         {
1615                 int tmp = ps;
1616                 ps = pe;
1617                 pe = tmp;
1618         }
1619 
1620         switch (sel_mode)
1621         {
1622                 case 0: /* character-by-character selection */
1623                 default:
1624                         new_sel_start = ps;
1625                         new_sel_end = pe;
1626                         break;
1627                 case 1: /* word-by-word selection */
1628                         spc = isspace(*(off + ps));
1629                         for (new_sel_start = ps; ; ps -= 2)
1630                         {
1631                                 if ((spc && !isspace(*(off + ps))) ||
1632                                     (!spc && !inword(*(off + ps))))
1633                                         break;
1634                                 new_sel_start = ps;
1635                                 if (!(ps % video_size_row))
1636                                         break;
1637                         }
1638                         spc = isspace(*(off + pe));
1639                         for (new_sel_end = pe; ; pe += 2)
1640                         {
1641                                 if ((spc && !isspace(*(off + pe))) ||
1642                                     (!spc && !inword(*(off + pe))))
1643                                         break;
1644                                 new_sel_end = pe;
1645                                 if (!((pe + 2) % video_size_row))
1646                                         break;
1647                         }
1648                         break;
1649                 case 2: /* line-by-line selection */
1650                         new_sel_start = ps - ps % video_size_row;
1651                         new_sel_end = pe + video_size_row
1652                                     - pe % video_size_row - 2;
1653                         break;
1654         }
1655         /* select to end of line if on trailing space */
1656         if (new_sel_end > new_sel_start &&
1657                 !atedge(new_sel_end) && isspace(*(off + new_sel_end)))
1658         {
1659                 for (pe = new_sel_end + 2; ; pe += 2)
1660                 {
1661                         if (!isspace(*(off + pe)) || atedge(pe))
1662                                 break;
1663                 }
1664                 if (isspace(*(off + pe)))
1665                         new_sel_end = pe;
1666         }
1667         if (sel_cons != currcons)
1668         {
1669                 clear_selection();
1670                 sel_cons = currcons;
1671         }
1672         if (sel_start == -1)    /* no current selection */
1673                 highlight(sel_cons, new_sel_start, new_sel_end);
1674         else if (new_sel_start == sel_start)
1675         {
1676                 if (new_sel_end == sel_end)     /* no action required */
1677                         return 0;
1678                 else if (new_sel_end > sel_end) /* extend to right */
1679                         highlight(sel_cons, sel_end + 2, new_sel_end);
1680                 else                            /* contract from right */
1681                         highlight(sel_cons, new_sel_end + 2, sel_end);
1682         }
1683         else if (new_sel_end == sel_end)
1684         {
1685                 if (new_sel_start < sel_start)  /* extend to left */
1686                         highlight(sel_cons, new_sel_start, sel_start - 2);
1687                 else                            /* contract from left */
1688                         highlight(sel_cons, sel_start, new_sel_start - 2);
1689         }
1690         else    /* some other case; start selection from scratch */
1691         {
1692                 clear_selection();
1693                 highlight(sel_cons, new_sel_start, new_sel_end);
1694         }
1695         sel_start = new_sel_start;
1696         sel_end = new_sel_end;
1697         obp = bp = sel_buffer;
1698         for (i = sel_start; i <= sel_end; i += 2)
1699         {
1700                 spos = (char *)off + i;
1701                 *bp++ = *spos;
1702                 if (!isspace(*spos))
1703                         obp = bp;
1704                 if (! ((i + 2) % video_size_row))
1705                 {
1706                         /* strip trailing blanks from line and add newline,
1707                            unless non-space at end of line. */
1708                         if (obp != bp)
1709                         {
1710                                 bp = obp;
1711                                 *bp++ = '\n';
1712                         }
1713                         obp = bp;
1714                 }
1715                 /* check for space, leaving room for next character, possible
1716                    newline, and null at end. */
1717                 if (bp - sel_buffer > SEL_BUFFER_SIZE - 3)
1718                         break;
1719         }
1720         *bp = '\0';
1721         return 0;
1722 }
1723 
1724 /* insert the contents of the selection buffer into the queue of the
1725    tty associated with the current console. Invoked by ioctl(). */
1726 int paste_selection(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1727 {
1728         char *bp = sel_buffer;
1729 
1730         if (! *bp)
1731                 return 0;
1732         unblank_screen();
1733         while (*bp)
1734         {
1735                 put_tty_queue(*bp, &tty->read_q);
1736                 bp++;
1737         }
1738         TTY_READ_FLUSH(tty);
1739         return 0;
1740 }
1741 
1742 /* remove the current selection highlight, if any, from the console holding
1743    the selection. */
1744 static void clear_selection()
     /* [previous][next][first][last][top][bottom][index][help] */
1745 {
1746         if (sel_start != -1)
1747         {
1748                 highlight(sel_cons, sel_start, sel_end);
1749                 sel_start = -1;
1750         }
1751 }
1752 #endif /* CONFIG_SELECTION */

/* [previous][next][first][last][top][bottom][index][help] */