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