home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / telecomm / uemlsrc / termio.c < prev    next >
C/C++ Source or Header  |  1987-08-24  |  21KB  |  602 lines

  1. /*
  2.  * The functions in this file
  3.  * negotiate with the operating system
  4.  * for characters, and write characters in
  5.  * a barely buffered fashion on the display.
  6.  * All operating systems.
  7.  */
  8. #include        <stdio.h>
  9. #include        <ctype.h>
  10. #include        <osbind.h>
  11. #include        "ed.h"
  12.  
  13. #if ST
  14. #define BORDER  0
  15. #define CURSOR  1
  16. #define DESK    2
  17. #define LETTER  3
  18. #define BLACK   0
  19. #define WHITE   0x777
  20. #define COBALT  0x007
  21. #define TEAL    0x055
  22. #define GREEN   0x070
  23. #define GREY    0x555
  24. #define RED     0x700
  25. #define MAGENTA 0x707
  26. #define YELLOW  0x770
  27. /* the first four are the saved values */
  28. int     bordcol;        /* #0   */
  29. int     curscol;        /* #1   */
  30. int     deskcol;        /* #2   */
  31. int     letcol;         /* #3   */
  32. /* these are the internal values */
  33. int     bcolor;         /* #0   */
  34. int     ccolor;         /* #1   */
  35. int     dcolor;         /* #2   */
  36. int     lcolor;         /* #3   */
  37. char    *colornames[4] = {"Magenta","Magenta","Magenta","Magenta"};
  38. #endif
  39.  
  40. #if     VMS
  41. #include        <stsdef.h>
  42. #include        <ssdef.h>
  43. #include        <descrip.h>
  44. #include        <iodef.h>
  45. #include        <ttdef.h>
  46.  
  47. #define NIBUF   128                     /* Input  buffer size           */
  48. #define NOBUF   1024                    /* MM says bug buffers win!     */
  49. #define EFN     0                       /* Event flag                   */
  50.  
  51. char    obuf[NOBUF];                    /* Output buffer                */
  52. int     nobuf;                          /* # of bytes in above          */
  53. char    ibuf[NIBUF];                    /* Input buffer                 */
  54. int     nibuf;                          /* # of bytes in above          */
  55. int     ibufi;                          /* Read index                   */
  56. int     oldmode[2];                     /* Old TTY mode bits            */
  57. int     newmode[2];                     /* New TTY mode bits            */
  58. short   iochan;                         /* TTY I/O channel              */
  59. #endif
  60.  
  61. #if     MSDOS
  62. #include        <dos.h>
  63. #endif
  64.  
  65. #if V7
  66. #include        <sgtty.h>               /* for stty/gtty functions */
  67. struct  sgttyb  ostate;                 /* saved tty state */
  68. struct  sgttyb  nstate;                 /* values for editor mode */
  69. #endif
  70.  
  71. /*
  72.  * This function is called once
  73.  * to set up the terminal device streams.
  74.  * On VMS, it translates SYS$INPUT until it
  75.  * finds the terminal, then assigns a channel to it
  76.  * and sets it raw. On CPM it is a no-op.  On the ST
  77.  * it is called to update the screen color and every
  78.  * time we return from spawn or the term mode.
  79.  */
  80. ttopen()
  81. {
  82. #if     VMS
  83.         struct  dsc$descriptor  idsc;
  84.         struct  dsc$descriptor  odsc;
  85.         char    oname[40];
  86.         int     iosb[2];
  87.         int     status;
  88.  
  89.         odsc.dsc$a_pointer = "SYS$INPUT";
  90.         odsc.dsc$w_length  = strlen(odsc.dsc$a_pointer);
  91.         odsc.dsc$b_dtype   = DSC$K_DTYPE_T;
  92.         odsc.dsc$b_class   = DSC$K_CLASS_S;
  93.         idsc.dsc$b_dtype   = DSC$K_DTYPE_T;
  94.         idsc.dsc$b_class   = DSC$K_CLASS_S;
  95.         do {
  96.                 idsc.dsc$a_pointer = odsc.dsc$a_pointer;
  97.                 idsc.dsc$w_length  = odsc.dsc$w_length;
  98.                 odsc.dsc$a_pointer = &oname[0];
  99.                 odsc.dsc$w_length  = sizeof(oname);
  100.                 status = LIB$SYS_TRNLOG(&idsc, &odsc.dsc$w_length, &odsc);
  101.                 if (status!=SS$_NORMAL && status!=SS$_NOTRAN)
  102.                         exit(status);
  103.                 if (oname[0] == 0x1B) {
  104.                         odsc.dsc$a_pointer += 4;
  105.                         odsc.dsc$w_length  -= 4;
  106.                 }
  107.         } while (status == SS$_NORMAL);
  108.         status = SYS$ASSIGN(&odsc, &iochan, 0, 0);
  109.         if (status != SS$_NORMAL)
  110.                 exit(status);
  111.         status = SYS$QIOW(EFN, iochan, IO$_SENSEMODE, iosb, 0, 0,
  112.                           oldmode, sizeof(oldmode), 0, 0, 0, 0);
  113.         if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL)
  114.                 exit(status);
  115.         newmode[0] = oldmode[0];
  116.         newmode[1] = oldmode[1] | TT$M_PASSALL | TT$M_NOECHO;
  117.         status = SYS$QIOW(EFN, iochan, IO$_SETMODE, iosb, 0, 0,
  118.                           newmode, sizeof(newmode), 0, 0, 0, 0);
  119.         if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL)
  120.                 exit(status);
  121. #endif
  122. #if     ST
  123.         static char first = TRUE;
  124.         char colornm[80];
  125.         register int i;
  126.         char temp[40];
  127.  
  128.         if (first)
  129.                 {
  130.                 i = 0;
  131.                 Crawio(0x1b);   /* clear and home */
  132.                 Crawio('E');
  133.                 bcolor = WHITE; /* border */
  134.                 strcpy(colorname[i++],"White");
  135.                 if(!alias(temp,"border"))
  136.                         insalias("border","white");
  137.                 ccolor = WHITE; /* cursor */
  138.                 strcpy(colorname[i++],"White");
  139.                 if(!alias(temp,"cursor"))
  140.                         insalias("cursor","white");
  141.                 dcolor = BLACK; /* screen (desk) */
  142.                 strcpy(colorname[i++],"Black");
  143.                 if(!alias(temp,"desk"))
  144.                         insalias("desk","black");
  145.                 lcolor = WHITE; /* letters */
  146.                 strcpy(colorname[i++],"White");
  147.                 if(!alias(temp,"letter"))
  148.                         insalias("letter","white");
  149.                 first = FALSE;
  150.                 }
  151.         /* check for user preferences */
  152.         i = 0;
  153.         if (alias(colornm,"border"))
  154.                 {
  155.                 if (strcmp(colornm,"red")==NULL)
  156.                         {
  157.                         bcolor = RED;
  158.                         strcpy(colornames[0],"Red");
  159.                         }
  160.                 else if (strcmp(colornm,"black")==NULL)
  161.                         {
  162.                         bcolor = BLACK;
  163.                         strcpy(colornames[0],"Black");
  164.                         }
  165.                 else if (strcmp(colornm,"white")==NULL)
  166.                         {
  167.                         bcolor = WHITE;
  168.                         strcpy(colornames[0],"White");
  169.                         }
  170.                 else if (strcmp(colornm,"cobalt")==NULL)
  171.                         {
  172.                         bcolor = COBALT;
  173.                         strcpy(colornames[0],"Cobalt");
  174.                         }
  175.                 else if (strcmp(colornm,"green")==NULL)
  176.                         {
  177.                         bcolor = GREEN;
  178.                         strcpy(colornames[0],"Green");
  179.                         }
  180.                 else if (strcmp(colornm,"magenta")==NULL)
  181.                         {
  182.                         bcolor = MAGENTA;
  183.                         strcpy(colornames[0],"Magenta");
  184.                         }
  185.                 else if (strcmp(colornm,"yellow")==NULL)
  186.                         {
  187.                         bcolor = YELLOW;
  188.                         strcpy(colornames[0],"Yellow");
  189.                         }
  190.                 else if (strcmp(colornm,"grey")==NULL)
  191.                         {
  192.                         bcolor = GREY;
  193.                         strcpy(colornames[0],"Grey");
  194.                         }
  195.                 else if (strcmp(colornm,"teal")==NULL)
  196.                         {
  197.                         bcolor = TEAL;
  198.                         strcpy(colornames[0],"Teal");
  199.                         }
  200.                 }
  201.         if (alias(colornm,"cursor"))
  202.                 {
  203.                 if (strcmp(colornm,"red")==NULL)
  204.                         {
  205.                         ccolor = RED;
  206.                         strcpy(colornames[1],"Red");
  207.                         }
  208.                 else if (strcmp(colornm,"black")==NULL)
  209.                         {
  210.                         ccolor = BLACK;
  211.                         strcpy(colornames[1],"Black");
  212.                         }
  213.                 else if (strcmp(colornm,"white")==NULL)
  214.                         {
  215.                         ccolor = WHITE;
  216.                         strcpy(colornames[1],"White");
  217.                         }
  218.                 else if (strcmp(colornm,"cobalt")==NULL)
  219.                         {
  220.                         ccolor = COBALT;
  221.                         strcpy(colornames[1],"Cobalt");
  222.                         }
  223.                 else if (strcmp(colornm,"green")==NULL)
  224.                         {
  225.                         ccolor = GREEN;
  226.                         strcpy(colornames[1],"Green");
  227.                         }
  228.                 else if (strcmp(colornm,"magenta")==NULL)
  229.                         {
  230.                         ccolor = MAGENTA;
  231.                         strcpy(colornames[1],"Magenta");
  232.                         }
  233.                 else if (strcmp(colornm,"yellow")==NULL)
  234.                         {
  235.                         ccolor = YELLOW;
  236.                         strcpy(colornames[1],"Yellow");
  237.                         }
  238.                 else if (strcmp(colornm,"grey")==NULL)
  239.                         {
  240.                         ccolor = GREY;
  241.                         strcpy(colornames[1],"Grey");
  242.                         }
  243.                 else if (strcmp(colornm,"teal")==NULL)
  244.                         {
  245.                         ccolor = TEAL;
  246.                         strcpy(colornames[1],"Teal");
  247.                         }
  248.                 }
  249.         if (alias(colornm,"desk"))
  250.                 {
  251.                 if (strcmp(colornm,"red")==NULL)
  252.                         {
  253.                         dcolor = RED;
  254.                         strcpy(colornames[2],"Red");
  255.                         }
  256.                 else if (strcmp(colornm,"black")==NULL)
  257.                         {
  258.                         dcolor = BLACK;
  259.                         strcpy(colornames[2],"Black");
  260.                         }
  261.                 else if (strcmp(colornm,"white")==NULL)
  262.                         {
  263.                         dcolor = WHITE;
  264.                         strcpy(colornames[2],"White");
  265.                         }
  266.                 else if (strcmp(colornm,"cobalt")==NULL)
  267.                         {
  268.                         dcolor = COBALT;
  269.                         strcpy(colornames[2],"Cobalt");
  270.                         }
  271.                 else if (strcmp(colornm,"green")==NULL)
  272.                         {
  273.                         dcolor = GREEN;
  274.                         strcpy(colornames[2],"Green");
  275.                         }
  276.                 else if (strcmp(colornm,"magenta")==NULL)
  277.                         {
  278.                         dcolor = MAGENTA;
  279.                         strcpy(colornames[2],"Magenta");
  280.                         }
  281.                 else if (strcmp(colornm,"yellow")==NULL)
  282.                         {
  283.                         dcolor = YELLOW;
  284.                         strcpy(colornames[2],"Yellow");
  285.                         }
  286.                 else if (strcmp(colornm,"grey")==NULL)
  287.                         {
  288.                         dcolor = GREY;
  289.                         strcpy(colornames[2],"Grey");
  290.                         }
  291.                 else if (strcmp(colornm,"teal")==NULL)
  292.                         {
  293.                         dcolor = TEAL;
  294.                         strcpy(colornames[2],"Teal");
  295.                         }
  296.                 }
  297.         if (alias(colornm,"letter"))
  298.                 {
  299.                 if (strcmp(colornm,"red")==NULL)
  300.                         {
  301.                         lcolor = RED;
  302.                         strcpy(colornames[3],"Red");
  303.                         }
  304.                 else if (strcmp(colornm,"black")==NULL)
  305.                         {
  306.                         lcolor = BLACK;
  307.                         strcpy(colornames[3],"Black");
  308.                         }
  309.                 else if (strcmp(colornm,"white")==NULL)
  310.                         {
  311.                         lcolor = WHITE;
  312.                         strcpy(colornames[3],"White");
  313.                         }
  314.                 else if (strcmp(colornm,"cobalt")==NULL)
  315.                         {
  316.                         lcolor = COBALT;
  317.                         strcpy(colornames[3],"Cobalt");
  318.                         }
  319.                 else if (strcmp(colornm,"green")==NULL)
  320.                         {
  321.                         lcolor = GREEN;
  322.                         strcpy(colornames[3],"Green");
  323.                         }
  324.                 else if (strcmp(colornm,"magenta")==NULL)
  325.                         {
  326.                         lcolor = MAGENTA;
  327.                         strcpy(colornames[3],"Magenta");
  328.                         }
  329.                 else if (strcmp(colornm,"yellow")==NULL)
  330.                         {
  331.                         lcolor = YELLOW;
  332.                         strcpy(colornames[3],"Yellow");
  333.                         }
  334.                 else if (strcmp(colornm,"grey")==NULL)
  335.                         {
  336.                         lcolor = GREY;
  337.                         strcpy(colornames[3],"Grey");
  338.                         }
  339.                 else if (strcmp(colornm,"teal")==NULL)
  340.                         {
  341.                         lcolor = TEAL;
  342.                         strcpy(colornames[3],"Teal");
  343.                         }
  344.                 }
  345.         Setcolor(BORDER,bcolor);
  346.         Setcolor(CURSOR,ccolor);
  347.         Setcolor(DESK,dcolor);
  348.         Setcolor(LETTER,lcolor);
  349.         Crawio(0x1b);   /* No wrap mode */
  350.         Crawio('w');
  351.         Crawio(0x1b);   /* Turn on standout mode */
  352.         Crawio('c');    /* background == dcolor # */
  353.         Crawio('2');
  354.         Crawio(0x1b);
  355.         Crawio('b');    /* foreground == lcolor # */
  356.         Crawio('3');
  357. #endif
  358. #if     MSDOS
  359. #endif
  360. #if     V7
  361.         gtty(1, &ostate);                       /* save old state */
  362.         gtty(1, &nstate);                       /* get base of new state */
  363.         nstate.sg_flags |= RAW;
  364.         nstate.sg_flags &= ~(ECHO|CRMOD);       /* no echo for now... */
  365.         stty(1, &nstate);                       /* set mode */
  366. #endif
  367. }
  368.  
  369. /*
  370.  * This function gets called just
  371.  * before we go back home to the command interpreter.
  372.  * On VMS it puts the terminal back in a reasonable state.
  373.  * Another no-operation on ST.
  374.  */
  375. ttclose()
  376. {
  377. #if     VMS
  378.         int     status;
  379.         int     iosb[1];
  380.  
  381.         ttflush();
  382.         status = SYS$QIOW(EFN, iochan, IO$_SETMODE, iosb, 0, 0,
  383.                  oldmode, sizeof(oldmode), 0, 0, 0, 0);
  384.         if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL)
  385.                 exit(status);
  386.         status = SYS$DASSGN(iochan);
  387.         if (status != SS$_NORMAL)
  388.                 exit(status);
  389. #endif
  390. #if     ST
  391.         Crawio(0x1b);   /* turn off standout mode */
  392.         Crawio('c');
  393.         Crawio('0');
  394.         Crawio(0x1b);
  395.         Crawio('b');
  396.         Crawio('3');
  397.         Setcolor(BORDER,bordcol);
  398.         Setcolor(CURSOR,curscol);
  399.         Setcolor(DESK,deskcol); /* reset color the way I like it */
  400.         Setcolor(LETTER,letcol);
  401. #endif
  402. #if     MSDOS
  403. #endif
  404. #if     V7
  405.         stty(1, &ostate);
  406. #endif
  407. }
  408.  
  409. /*
  410.  * Write a character to the display.
  411.  * On VMS, terminal output is buffered, and
  412.  * we just put the characters in the big array,
  413.  * after cheching for overflow. On ST terminal I/O
  414.  * unbuffered, so we just write the byte out.
  415.  * Ditto on MS-DOS (use the very very raw console
  416.  * output routine).
  417.  */
  418. #if ST  /* use assembly */
  419. #else
  420. ttputc(c)
  421. register int c;
  422. {
  423. #if     VMS
  424.         if (nobuf >= NOBUF)
  425.                 ttflush();
  426.         obuf[nobuf++] = c;
  427. #endif
  428. #if     MSDOS
  429.         dosb(CONDIO, c, 0);
  430. #endif
  431. #if     V7
  432.         fputc(c, stdout);
  433. #endif
  434. }
  435. #endif
  436. /*
  437.  * Flush terminal buffer. Does real work
  438.  * where the terminal output is buffered up. A
  439.  * no-operation on systems where byte at a time
  440.  * terminal I/O is done.
  441.  */
  442. ttflush()
  443. {
  444. #if     VMS
  445.         int     status;
  446.         int     iosb[2];
  447.  
  448.         status = SS$_NORMAL;
  449.         if (nobuf != 0) {
  450.                 status = SYS$QIOW(EFN, iochan, IO$_WRITELBLK|IO$M_NOFORMAT,
  451.                          iosb, 0, 0, obuf, nobuf, 0, 0, 0, 0);
  452.                 if (status == SS$_NORMAL)
  453.                         status = iosb[0] & 0xFFFF;
  454.                 nobuf = 0;
  455.         }
  456.         return (status);
  457. #endif
  458. #if     ST
  459. #endif
  460. #if     MSDOS
  461. #endif
  462. #if     V7
  463.         fflush(stdout);
  464. #endif
  465. }
  466.  
  467. /*
  468.  * Read a character from the terminal,
  469.  * performing no editing and doing no echo at all.
  470.  * More complex in VMS that almost anyplace else, which
  471.  * figures. Very simple on ST, because the system can
  472.  * do exactly what you want.
  473.  */
  474. #if     ST
  475. /* ST needs to get scancode, so we do it in assembly */
  476. #else
  477. ttgetc()
  478. {
  479. #if     VMS
  480.         int     status;
  481.         int     iosb[2];
  482.         int     term[2];
  483.  
  484.         while (ibufi >= nibuf) {
  485.                 ibufi = 0;
  486.                 term[0] = 0;
  487.                 term[1] = 0;
  488.                 status = SYS$QIOW(EFN, iochan, IO$_READLBLK|IO$M_TIMED,
  489.                          iosb, 0, 0, ibuf, NIBUF, 0, term, 0, 0);
  490.                 if (status != SS$_NORMAL)
  491.                         exit(status);
  492.                 status = iosb[0] & 0xFFFF;
  493.                 if (status!=SS$_NORMAL && status!=SS$_TIMEOUT)
  494.                         exit(status);
  495.                 nibuf = (iosb[0]>>16) + (iosb[1]>>16);
  496.                 if (nibuf == 0) {
  497.                         status = sys$qiow(EFN, iochan, IO$_READLBLK,
  498.                                  iosb, 0, 0, ibuf, 1, 0, term, 0, 0);
  499.                         if (status != SS$_NORMAL
  500.                         || (status = (iosb[0]&0xFFFF)) != SS$_NORMAL)
  501.                                 exit(status);
  502.                         nibuf = (iosb[0]>>16) + (iosb[1]>>16);
  503.                 }
  504.         }
  505.         return (ibuf[ibufi++] & 0xFF);          /* Allow multinational  */
  506. #endif
  507. #if     MSDOS
  508.         return (dosb(CONRAW, 0, 0));
  509. #endif
  510. #if     V7
  511.         return(fgetc(stdin));
  512. #endif
  513. }
  514. #endif
  515. #if ST
  516. /* save current color values so they can be reinstalled on exit */
  517.  
  518. savecolor()
  519. {
  520.         bordcol=(int)Getcolor(BORDER);
  521.         deskcol=(int)Getcolor(DESK);
  522.         curscol=(int)Getcolor(CURSOR);
  523.         letcol=(int)Getcolor(LETTER);
  524. }
  525.  
  526. paintbuffer(f,n)
  527. register int f, n;
  528. {
  529.         char mycolor[20];
  530.         char clrtwo[20];
  531.         register char *ptr;
  532.         extern char *index();
  533.  
  534.         if((f=mlreply("New background color: ",mycolor,12)) != TRUE)
  535.                 return(f);
  536.         /* don't fail because of stray spaces */
  537.         if ((ptr=index(mycolor,' '))!=NULL)
  538.                 *ptr = '\0';
  539.         else if ((ptr=index(mycolor,'\t'))!=NULL)
  540.                 *ptr = '\0';
  541.         n = strlen(mycolor);
  542.         /* adjust case */
  543.         for (f=0;f<=n;f++)
  544.                 if (f == 0)
  545.                         mycolor[f] = toupper(mycolor[f]);
  546.                 else
  547.                         mycolor[f] = tolower(mycolor[f]);
  548.         for (f=0;f<=LETTER;f++)
  549.                 {
  550.                 if (strcmp(colornames[f],mycolor)==0)
  551.                         {
  552.                         switch(f)
  553.                                 {
  554.                                 case BORDER:
  555.                                         strcpy(mycolor,colornames[BORDER]);
  556.                                         mycolor[0] = tolower(mycolor[0]);
  557.                                         strcpy(clrtwo,colornames[DESK]);
  558.                                         clrtwo[0] = tolower(clrtwo[0]);
  559.                                         insalias("desk",mycolor);
  560.                                         insalias("border",clrtwo);
  561.                                         if (strcmp(colornames[BORDER],
  562.                                             colornames[LETTER])==NULL)
  563.                                                 insalias("letter",clrtwo);
  564.                                         break;
  565.                                 case CURSOR:
  566.                                         strcpy(mycolor,colornames[CURSOR]);
  567.                                         mycolor[0] = tolower(mycolor[0]);
  568.                                         strcpy(clrtwo,colornames[DESK]);
  569.                                         clrtwo[0] = tolower(clrtwo[0]);
  570.                                         insalias("desk",mycolor);
  571.                                         insalias("cursor",clrtwo);
  572.                                         break;
  573.                                 case DESK:      /* no sense in this */
  574.                                         break;
  575.                                 case LETTER:
  576.                                         strcpy(mycolor,colornames[LETTER]);
  577.                                         mycolor[0] = tolower(mycolor[0]);
  578.                                         strcpy(clrtwo,colornames[DESK]);
  579.                                         clrtwo[0] = tolower(clrtwo[0]);
  580.                                         insalias("desk",mycolor);
  581.                                         insalias("letter",clrtwo);
  582.                                         break;
  583.                                 default:
  584.                                         return(FALSE);
  585.                                 }
  586.                         ttopen();
  587.                         return(TRUE);
  588.                         }
  589.                 }
  590.         mlwrite("No match");
  591.         return(FALSE);
  592. }
  593.  
  594. #else
  595. paintbuffer(f,n)
  596. int f,n;
  597. {
  598.         mlwrite("Buy a color computer like an ST");
  599.         return(FALSE);
  600. }
  601. #endif
  602.