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

  1. /* A simple function for line input */
  2. /* Philip J. Erdelsky - 01 JAN 1991 */
  3. /* object code may be used freely */
  4.  
  5. static int key(void) {
  6.   int k = bdos(8)&0xFF;
  7.   /* two-byte sequence for special key is collapsed into single code */
  8.   if (k==0) k = bdos(8)&0xFF|0x100;
  9.   return k;
  10. }
  11.  
  12. static void display(int c) {bdos(2,c);}
  13.  
  14. int line_input(char *text, int text_length, int display_length) {
  15.   /* start display as though operator had struck Home or End key */
  16.   int k = 0x147; /* Home 0x147 or End 0x14F */
  17.   int finished = 0;
  18.   int desired_cursor_position;
  19.   int position_within_text;
  20.   int actual_cursor_position = 0;
  21.   while (1) {
  22.     switch (k) {
  23.       /* finish line input and exit if k==0x00D (Enter key) */
  24.       /* other terminating keys could be added, if desired */
  25.       case 0x00D:
  26.         finished = 1;
  27.         /* fall through to next case */
  28.       /* move cursor to left end of text if k==0x147 (Home key) */
  29.       case 0x147:
  30.         desired_cursor_position = position_within_text = 0;
  31.         break;
  32.       /* move cursor to right end of text if k==0x14F (End key) */
  33.       case 0x14F:
  34.         position_within_text = strlen(text);
  35.         desired_cursor_position =
  36.           position_within_text<display_length ? position_within_text :
  37.           display_length-1;
  38.         break;
  39.       /* move cursor left one place if k==0x14B (left arrow key) */
  40.       case 0x14B:
  41.         if (position_within_text>0) {
  42.           position_within_text--;
  43.           if (desired_cursor_position>0) desired_cursor_position--;
  44.         }
  45.         break;
  46.       /* move cursor right one place if k==0x14D (right arrow key) */
  47.       case 0x14D:
  48.         if (position_within_text<strlen(text)) {
  49.           position_within_text++;
  50.           if (desired_cursor_position<display_length-1)
  51.             desired_cursor_position++;
  52.         }
  53.         break;
  54.       /* move cursor left one place and delete character under */
  55.       /* cursor if k==0x008 (backspace key) */
  56.       case 0x008:
  57.         if (position_within_text==0) break;
  58.         position_within_text--;
  59.         if (desired_cursor_position>0) desired_cursor_position--;
  60.         /* fall through to next case */
  61.       /* delete character under cursor if k==0x153 (Del key) */
  62.       case 0x153:
  63.         if (text[position_within_text]!=0)
  64.           strcpy(text+position_within_text, text+position_within_text+1);
  65.         break;
  66.       /* insert new character to left of character under cursor */
  67.       default:
  68.         if (' '<=k && k<='~' && strlen(text)<text_length) {
  69.           char *t = text+position_within_text;
  70.           do {
  71.             int temp = *t;
  72.             *t++ = k;
  73.             k = temp;
  74.           } while (k!=0);
  75.           *t = 0;
  76.           position_within_text++;
  77.           if (desired_cursor_position<display_length-1)
  78.             desired_cursor_position++;
  79.         }
  80.     }
  81.     /* redisplay entire line */
  82.     {
  83.       int hidden_characters_at_left =
  84.         position_within_text-desired_cursor_position;
  85.       char *t = text+hidden_characters_at_left;
  86.       /* move cursor to left end of line */
  87.       while (actual_cursor_position!=0)
  88.         {display('\b'); actual_cursor_position--;}
  89.       /* display arrows at left end if necessary */
  90.       display('\b');                 /* 0xAE = left arrows */
  91.       display(hidden_characters_at_left!=0 ? 0xAE : ' ');
  92.       /* display text and trailing spaces, if any */
  93.       while (actual_cursor_position<display_length) {
  94.         int c;
  95.         display(*t==0 ? ' ' : (c = *t++)==' ' ? 0xFA : c);
  96.         actual_cursor_position++;         /* 0xFA = tiny dot */
  97.       }
  98.       /* display arrows at right end if necessary */
  99.       display(*t!=0 ? 0xAF : ' '); /* 0xAF = right arrows */
  100.       actual_cursor_position++;
  101.       /* move cursor back to desired position */
  102.       while (actual_cursor_position>desired_cursor_position)
  103.         {display('\b'); actual_cursor_position--;}
  104.     }
  105.     if (finished) return k;
  106.     k = key();
  107.   }
  108. }
  109.  
  110. /* test program */
  111.  
  112. void main(int argc, char **argv) {
  113.   char text[40];
  114.   int k;
  115.   strcpy(text, argc>1 ? argv[1] : "");
  116.   printf("> ");
  117.   k = line_input(text, sizeof(text)-1, 20);
  118.   printf("\nreturn value=%03X text=\"%s\"\n", k, text);
  119. }
  120.