home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_07 / 9n07039a < prev    next >
Text File  |  1990-12-31  |  4KB  |  113 lines

  1. /* A simple function for line input */
  2. /* alternate version using BIOS and direct screen access */
  3. /* compiled with Turbo C 2.0 */
  4. /* Philip J. Erdelsky - 01 JAN 1991 */
  5. /* object code may be used freely */
  6.  
  7. static int key() {
  8.   int k = bioskey(0);
  9.   return (k&0xFF)!=0 ? k&0xFF : k;
  10. }
  11.  
  12. int line_input(int far *screen, char *text, int text_length,
  13.                int display_length) {
  14.   /* start display as though operator had struck Home or End key */
  15.   int k = 0x4700; /* Home 0x4700 or End 0x4F00 */
  16.   int finished = 0;
  17.   int cursor_position;
  18.   int position_within_text;
  19.   while (1) {
  20.     switch (k) {
  21.       /* finish line input and exit if k==0x000D (Enter key) */
  22.       /* other terminating keys could be added, if desired */
  23.       case 0x000D:
  24.         finished = 1;
  25.         /* fall through to next case */
  26.       /* move cursor to left end of text if k==0x4700 (Home key) */
  27.       case 0x4700:
  28.         cursor_position = position_within_text = 0;
  29.         break;
  30.       /* move cursor to right end of text if k==0x4F00 (End key) */
  31.       case 0x4F00:
  32.         position_within_text = strlen(text);
  33.         cursor_position =
  34.           position_within_text<display_length ? position_within_text :
  35.           display_length-1;
  36.         break;
  37.       /* move cursor left one place if k==0x4B00 (left arrow key) */
  38.       case 0x4B00:
  39.         if (position_within_text>0) {
  40.           position_within_text--;
  41.           if (cursor_position>0) cursor_position--;
  42.         }
  43.         break;
  44.       /* move cursor right one place if k==0x4D00 (right arrow key) */
  45.       case 0x4D00:
  46.         if (position_within_text<strlen(text)) {
  47.           position_within_text++;
  48.           if (cursor_position<display_length-1)
  49.             cursor_position++;
  50.         }
  51.         break;
  52.       /* move cursor left one place and delete character under cursor */
  53.       /* if k==0x0008 (backspace key) */
  54.       case 0x0008:
  55.         if (position_within_text==0) break;
  56.         position_within_text--;
  57.         if (cursor_position>0) cursor_position--;
  58.         /* fall through to next case */
  59.       /* delete character under cursor if k==0x5300 (Del key) */
  60.       case 0x5300:
  61.         if (text[position_within_text]!=0)
  62.           strcpy(text+position_within_text, text+position_within_text+1);
  63.         break;
  64.       /* insert new character to left of character under cursor */
  65.       default:
  66.         if (' '<=k && k<='~' && strlen(text)<text_length) {
  67.           char *t = text+position_within_text;
  68.           do {
  69.             int temp = *t;
  70.             *t++ = k;
  71.             k = temp;
  72.           } while (k!=0);
  73.           *t = 0;
  74.           position_within_text++;
  75.           if (cursor_position<display_length-1)
  76.             cursor_position++;
  77.         }
  78.     }
  79.     /* redisplay entire line */
  80.     {
  81.       int hidden_characters_at_left =
  82.         position_within_text-cursor_position;
  83.       char *t = text+hidden_characters_at_left;
  84.       int i;
  85.       /* display arrows at left end if necessary */
  86.       screen[-1] = hidden_characters_at_left!=0 ? 0x0FAE : 0x0700|' ';
  87.       /* display text and trailing spaces, if any */
  88.       /* use reverse video to mark cursor position */
  89.       for (i=0; i<display_length; i++) {
  90.         int c;
  91.         screen[i] = (i==cursor_position && !finished ? 0x7000 : 0x0700) |
  92.                     (*t==0 ? ' ' : (c = *t++)==' ' ? 0xFA : c);
  93.       }
  94.       /* display arrows at right end if necessary */
  95.       screen[i] = *t!=0 ? 0x0FAF : 0x0700|' ';
  96.     }
  97.     if (finished) return k;
  98.     k = key();
  99.   }
  100. }
  101.  
  102. /* test program */
  103.  
  104. void main(int argc, char **argv) {
  105.   char text[40];
  106.   int k;
  107.   int far *screen = 
  108.     (int far *) ((biosequip()&0x30)==0x30 ? 0xB0000000L : 0xB8000000L);
  109.   strcpy(text, argc>1 ? argv[1] : "");
  110.   k = line_input(screen+12*80+2, text, sizeof(text)-1, 20);
  111.   printf("\nreturn value=%04X text=\"%s\"\n", k, text);
  112. }
  113.