home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / disks / disk458.lzh / Csh / src / rawconsole.c < prev    next >
C/C++ Source or Header  |  1991-02-15  |  9KB  |  289 lines

  1. /*
  2.  * RawConsole.c
  3.  *
  4.  * Shell 2.07M  17-Jun-87
  5.  * console handling, command line editing support for Shell
  6.  * using new console packets from 1.2.
  7.  * Written by Steve Drew. (c) 14-Oct-86.
  8.  * 16-Dec-86 Slight mods to rawgets() for Disktrashing.
  9.  *
  10.  * Version 4.01A by Carlo Borreo & Cesare Dieni 17-Feb-90
  11.  *
  12.  */
  13.  
  14. #if RAW_CONSOLE
  15.  
  16. char *tyahdptr;
  17.  
  18. myget()
  19. {
  20. if (*tyahdptr) return *tyahdptr++;
  21. return getchar();
  22. }
  23.  
  24. extern int aux; /* for use with aux: */
  25.  
  26. #define SETRAW setrawcon(-1L);
  27. #define SETCON setrawcon(0L);
  28.  
  29. int width;
  30.  
  31. newwidth()
  32. {
  33. extern struct Window *w;
  34.  
  35. width=(w->Width- (w->BorderLeft + w->BorderRight)) / w->RPort->TxWidth;
  36. }
  37.  
  38. char *rawgets(line,prompt)
  39. char *line, *prompt;
  40. {
  41. char *get_var();
  42. char *gets();
  43. register int n, pl;
  44. register int max, i;
  45. unsigned char c1,c2,c3;
  46. char fkeys[5];
  47. char *s;
  48. char *ps;
  49. char typeahd[256];
  50. int fkey, savn;
  51. int insert = 1;
  52. int recall = -1;
  53. struct HIST *hist;
  54.  
  55. newwidth();
  56.  
  57. if (aux) {
  58.     printf("%s",prompt);
  59.     fflush(stdout);
  60.     }
  61. if (!IsInteractive(Input()) || aux ) return(gets(line));
  62. if (WaitForChar((long)Input(), 100L) ||   /* don't switch to 1L ...*/
  63.         stdin->_bp < stdin->_bend) {     /* else causes read err's*/
  64.     gets(line);
  65.     return(line);
  66.     }
  67. SETRAW;
  68. printf("\015%s\2336n",prompt);
  69. savn = pl = n = 0;
  70. tyahdptr = typeahd;
  71. while((typeahd[n]=getchar()) != 'R') {
  72.     if ((unsigned char)typeahd[n] == 155) savn = n;
  73.     n++;
  74.     }
  75.     /* typeahd now contains possible type a head chars
  76.        followed by <CSI> cursor position report.
  77.     */
  78. typeahd[savn]  = '\0';
  79. if (typeahd[n-2] != ';') pl = (typeahd[n-2] - 48) * 10;
  80. pl += typeahd[n-1] - 49;
  81. ps = line + pl;
  82. line[max = i = pl] = '\0';
  83.  
  84. if (s = get_var (LEVEL_SET, "_insert")) insert = atoi(s) ? 1 : 0;
  85.  
  86. while( (c1 = myget()) != 255) {
  87.         switch(c1) {
  88.             case 155:
  89.                  c2 = myget();
  90.                  switch(c2) {
  91.                      case 'A':                  /* up arrow   */
  92.                         n = ++recall;
  93.                      case 'B':                  /* down arrow */
  94.                         line[pl] = '\0';
  95.                         if (recall >= 0 || c2 == 'A') {
  96.                             if (c2 == 'B') n = --recall;
  97.                             if (recall >= 0) {
  98.                                 for(hist = H_head; hist && n--;
  99.                                     hist = hist->next);
  100.                                 if (hist) strcpy(&line[pl],hist->line);
  101.                                 else recall = H_len;
  102.                             }
  103.                         }
  104.                         if (i != pl)
  105.                             printf("\233%dD",i);
  106.                         printf("\015\233J%s%s",prompt,ps);
  107.                         i = max = strlen(ps) + pl;
  108.                         break;
  109.                      case 'C':                  /* right arrow*/
  110.                         if (i < max) {
  111.                             i++;
  112.                             printf("\233C");
  113.                         }
  114.                         break;
  115.                      case 'D':                  /* left arrow */
  116.                         if (i > pl) {
  117.                             i--;
  118.                             printf("\233D");
  119.                         }
  120.                         break;
  121.                      case 'T':           /* shift-up   */
  122.                        n = recall = H_len-1;
  123.                      case 'S':           /* shift-down */
  124.                        line[pl] = '\0';
  125.                        if (c2 == 'S') {
  126.                            n = recall = 0;
  127.                            if (H_head) strcpy(&line[pl], H_head->line);
  128.                        }
  129.                        else if (H_tail) strcpy(&line[pl], H_tail->line);
  130.                        printf("\015\233J%s%s", prompt, ps);
  131.                        i = max = strlen(ps) + pl;
  132.                        break;
  133.                     case ' ':                   /* shift -> <-*/
  134.                         c3 = myget();
  135.                                      switch(c3) {
  136.                     case('@'):      /* shift ->   */
  137.                         while (ps[i-pl-1] == ' ' && i<max) {
  138.                           i++;
  139.                           printf("\233C");
  140.                 }
  141.                         while (ps[i-pl-1] != ' ' && i<max) {
  142.                           i++;
  143.                     printf("\233C");
  144.                        }
  145.                         break;
  146.                     case('A'):      /* shift <-   */
  147.                         while (ps[i-pl-1] == ' ' && i>pl) {
  148.                             i--;
  149.                             printf("\233D");
  150.                         }
  151.                         while (ps[i-pl-1] != ' ' && i>pl) {
  152.                             i--;
  153.                             printf("\233D");
  154.                         }
  155.                         break;
  156.                         default:
  157.                         break;
  158.                     }
  159.                         break;
  160.                     default:
  161.                         c3 = myget();
  162.                         if (c3 == '~') {
  163.                             fkey = c2;
  164.                             fkeys[0] = 'f';
  165.                             if (c2 == '?') {
  166.                                 strcpy(ps,"help");
  167.                                 goto done;
  168.                             }
  169.                         }
  170.                         else if (myget() != '~') { /* window was resized */
  171.                             while(myget() != '|');
  172.                             newwidth();
  173.                             break;
  174.                         }
  175.                         else {
  176.                             fkey = c3;
  177.                             fkeys[0] = 'F';
  178.                         }
  179.                         sprintf(fkeys+1,"%d",fkey - 47);
  180.                         if (s = get_var(LEVEL_SET, fkeys))
  181.                                 tyahdptr = strcpy(typeahd,s);
  182.                         break;
  183.                     }
  184.                 break;
  185.             case 8:
  186.                 if (i > pl) {
  187.                     i--;
  188.                     printf("\010");
  189.                 }
  190.                 else break;
  191.             case 127:
  192.                 if (i < max) {
  193.                     int j,t,l = 0;
  194.                     movmem(&line[i+1],&line[i],max-i);
  195.                     --max;
  196.                     printf("\233P");
  197.                     j = width - i % width - 1;   /* amount to end     */
  198.                     t = max/width - i/width;     /* no of lines       */
  199.                     for(n = 0; n < t; n++) {
  200.                         l += j;                  /* no. of char moved */
  201.                         if (j) printf("\233%dC",j); /* goto eol       */
  202.                         printf("%c\233P",line[width*(i/width+n+1)-1]);
  203.                         j = width-1;
  204.                     }
  205.                     if (t)
  206.                     printf("\233%dD",l+t);   /* get back */
  207.                 }
  208.                 break;
  209.             case 18:
  210.                 n = i/width;
  211.                 if (n) printf("\233%dF",n);
  212.                 printf("\015\233J%s%s",prompt,ps);
  213.                 i = max;
  214.                 break;
  215.             case 27:
  216.                 break;
  217.             case 1:
  218.                 insert ^= 1;
  219.                 break;
  220.             case 21:
  221.             case 24:
  222.             case 26:
  223.                 if (i>pl) printf("\233%dD",i-pl);
  224.                 i = pl;
  225.                 if (c1 == 26) break;
  226.             case 11:        /* ^K */
  227.                 printf("\233J");
  228.                 max = i;
  229.                 line[i] = '\0';
  230.                 break;
  231.             case 28:        /* ^\ */
  232.                 SETCON;
  233.                 return(NULL);
  234.             case 5:
  235.                 if (i!=max) printf("\233%dC",max - i);
  236.                 i = max;
  237.                 break;
  238.             case 10:
  239.             case 13:
  240.                 line[max] = '\0';
  241. done:           printf("\233%dC\n",max - i);
  242.  
  243.                 SETCON;
  244.                 strcpy(line, ps);
  245.                 return(line);
  246.             default:
  247.                 c1 &= 0x7f;
  248.                 if (c1 == 9) c1 = 32;
  249.                 if (c1 > 31 & i < 256) {
  250.                     if (i < max && insert) {
  251.                         int j,t,l = 0;
  252.                         movmem(&line[i], &line[i+1], max - i);
  253.                         printf("\233@%c",c1);
  254.                         t = max/width - i/width;
  255.                         j = width - i % width - 1;
  256.                         for(n = 0; n < t; n++) {
  257.                             l += j;
  258.                             if (j) printf("\233%dC",j);
  259.                             printf("\233@%c",line[width*(i/width+n+1)]);
  260.                             j = width-1;
  261.                         }
  262.                         if (t) printf("\233%dD",l + t);
  263.                         ++max;
  264.                     }
  265.                     else {
  266.                         if(i == pl && max == i) printf("\015%s%s",prompt,ps);
  267.                         putchar(c1);
  268.                     }
  269.                     line[i++] = c1;
  270.                     if (max < i) max = i;
  271.                     line[max] = '\0';
  272.                 }
  273.         }
  274.     }
  275. SETCON;
  276. return(NULL);
  277. }
  278.  
  279. setrawcon(flag) /* -1L=RAW:, 0L=CON: */
  280. long flag;
  281. {
  282. long packargs[8];
  283.  
  284. packargs[0]=flag;
  285. SendPacket(994L, packargs, Myprocess->pr_ConsoleTask);
  286. }
  287.  
  288. #endif
  289.