home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: DFÜ und Kommunikation / SOS-DFUE.ISO / programm / dos / utility / pccp076 / termplay.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-27  |  14.4 KB  |  703 lines

  1. /*    Copyright (C) 1992 Peter Edward Cann, all rights reserved.
  2.  */
  3.  
  4. #include<stdio.h>
  5. #include<bios.h>
  6. #include<dos.h>
  7. #include<fcntl.h>
  8. #include<graph.h>
  9. #include"emu.h"
  10. #include"color.h"
  11.  
  12. #define LISTSIZ 16
  13.  
  14. struct
  15.     {
  16.     short index;
  17.     short row;
  18.     char rowset;
  19.     short column;
  20.     char colset;
  21.     short list[LISTSIZ];
  22.     short listindex;
  23.     }
  24.     funcstor[NFUNCS];
  25.  
  26. clrfuncstor(seqn)
  27.     short seqn;
  28.     {
  29.     int i;
  30.     if(funcstor[seqn].listindex==LISTSIZ)
  31.         funcstor[seqn].listindex--;
  32.     for(i=funcstor[seqn].listindex;i>=0;i--)
  33.         funcstor[seqn].list[i]=0;
  34.     funcstor[seqn].row=funcstor[seqn].column=funcstor[seqn].listindex=funcstor[seqn].index=funcstor[seqn].rowset=funcstor[seqn].colset=0;
  35.     }
  36.  
  37. int bold, faint, blink, inverse, bkcolor, fgcolor;
  38. struct videoconfig far *vconf;
  39.  
  40. atthndl()
  41.     {
  42.     int adds, workingbk, workingfg;
  43.     adds=0;
  44.     if((vconf->adapter!=_MDPA)&&(vconf->adapter!=_HGC))
  45.         {
  46.         /* Color Adapter */
  47.         workingbk=bkcolor;
  48.         workingfg=fgcolor;
  49.         if(faint&&(fgcolor==WHITE))
  50.             workingfg=GRAY;
  51.         if(bold)
  52.             adds+=BOLDADD;
  53.         if(blink)
  54.             adds+=BLINKADD;
  55.         if(inverse)
  56.             {
  57.             _settextcolor(workingbk+adds);
  58.             _setbkcolor((long)(workingfg&0x07));
  59.             }
  60.         else
  61.             {
  62.             _settextcolor(workingfg+adds);
  63.             _setbkcolor((long)(workingbk&0x07));
  64.             }
  65.         }
  66.     else
  67.         {
  68.         workingbk=BLACK;
  69.         if(bold&&!inverse)
  70.             workingfg=M_UNDER;
  71.         else
  72.             if(inverse)
  73.                 workingfg=WHITE;
  74.             else
  75.                 workingfg=M_NORMAL;
  76.         if(faint)
  77.             if(inverse)
  78.                 if(bkcolor!=BLACK)
  79.                     adds+=FAINTADD;
  80.                 else;
  81.             else
  82.                 if(fgcolor!=BLACK)
  83.                     adds+=FAINTADD;
  84.         if(blink)
  85.             adds+=BLINKADD;
  86.         if(inverse)
  87.             {
  88.             _settextcolor(workingbk+adds);
  89.             _setbkcolor((long)(workingfg&0x07));
  90.             }
  91.         else
  92.             {
  93.             _settextcolor(workingfg+adds);
  94.             _setbkcolor((long)(workingbk&0x07));
  95.             }
  96.         }
  97.     }
  98.  
  99.  
  100. ansiatthndl(seqn)
  101.     short seqn;
  102.     {
  103.     int i, adds, workingbk, workingfg;
  104.     if(emu.funcs[seqn].func==ANSIATTRIB)
  105.         for(i=0;i<funcstor[seqn].listindex;i++)
  106.             switch(funcstor[seqn].list[i])
  107.                 {
  108.                 /*at the moment this is strictly ANSI subset*/
  109.                 case 0:
  110.                     bkcolor=BLACK;
  111.                     fgcolor=WHITE;
  112.                     bold=faint=blink=inverse=0;
  113.                     break;
  114.                 case 1:
  115.                     bold=1;
  116.                     break;
  117.                 case 2:
  118.                     faint=1;
  119.                     break;
  120.                 case 5:
  121.                 case 6:
  122.                     blink=1;
  123.                     break;
  124.                 case 7:
  125.                     inverse=1;
  126.                     break;
  127.                 case 30:
  128.                     fgcolor=BLACK;
  129.                     break;
  130.                 case 31:
  131.                     fgcolor=RED;
  132.                     break;
  133.                 case 32:
  134.                     fgcolor=GREEN;
  135.                     break;    
  136.                 case 33:
  137.                     fgcolor=YELLOW;
  138.                     break;
  139.                 case 34:
  140.                     fgcolor=BLUE;
  141.                     break;
  142.                 case 35:
  143.                     fgcolor=MAGENTA;
  144.                     break;
  145.                 case 36:
  146.                     fgcolor=CYAN;
  147.                     break;
  148.                 case 37:
  149.                     fgcolor=WHITE;
  150.                     break;
  151.                 case 40:
  152.                     bkcolor=BLACK;
  153.                     break;
  154.                 case 41:
  155.                     bkcolor=RED;
  156.                     break;
  157.                 case 42:
  158.                     bkcolor=GREEN;
  159.                     break;    
  160.                 case 43:
  161.                     bkcolor=YELLOW;
  162.                     break;
  163.                 case 44:
  164.                     bkcolor=BLUE;
  165.                     break;
  166.                 case 45:
  167.                     bkcolor=MAGENTA;
  168.                     break;
  169.                 case 46:
  170.                     bkcolor=CYAN;
  171.                     break;
  172.                 case 47:
  173.                     bkcolor=WHITE;
  174.                     break;
  175.                 default:
  176.                     break;
  177.                 }
  178.     atthndl();
  179.     }
  180.  
  181. int wrap_p;
  182.  
  183. wrapctl()
  184.     {
  185.     if(wrap_p)
  186.         _wrapon(_GWRAPON);
  187.     else
  188.         _wrapon(_GWRAPOFF);
  189.     }
  190.  
  191. char fpname[256];
  192.  
  193. updstatus(c)
  194.     {
  195.     struct rccoord posptr;
  196.     short tc;
  197.     long bc;
  198.     char str[80];
  199.     posptr=_gettextposition();
  200.     tc=_gettextcolor();
  201.     bc=_getbkcolor();
  202.     _settextwindow(1,1,1,80);
  203.     _settextposition(1,1);
  204.     _settextcolor(BLACK);
  205.     _setbkcolor((long)WHITE);
  206.     _wrapon(_GWRAPOFF);
  207.     sprintf(str, "Next=%02x  Bold=%d  Faint=%d  Blink=%d  Inverse=%d", c, bold, faint, blink, inverse);
  208.     _outtext(str);
  209.     _settextwindow(2,1,25,80);
  210.     wrapctl();
  211.     _settextcolor(tc);
  212.     _setbkcolor(bc);
  213.     _settextposition(posptr.row, posptr.col);
  214.     }
  215.  
  216. int graphics;
  217.  
  218. showchar(c)
  219.     unsigned char c;
  220.     {
  221.     unsigned char str[2];
  222.     if(graphics)
  223.         if(emu.gchars[c])
  224.             c=emu.gchars[c];
  225.     str[0]=c;
  226.     str[1]='\0';
  227.     _outtext(str);
  228.     }
  229.  
  230. int savedrow, savedcol;
  231.  
  232. perffunc(seqn)
  233.     short seqn;
  234.     {
  235.     struct rccoord posptr;
  236.     int i;
  237.     switch(emu.funcs[seqn].func)
  238.         {
  239.         case CLEAR:
  240.             _clearscreen(_GCLEARSCREEN);
  241.             updstatus();
  242.             _settextposition(1,1);
  243.             break;
  244.         case HOME:
  245.             _settextposition(1,1);
  246.             break;
  247.         case CLREOL:
  248.             posptr=_gettextposition();
  249.             _wrapon(_GWRAPOFF);
  250.             for(i=posptr.col;i<=80;i++)
  251.                 _outtext(" ");
  252.             _settextposition(posptr.row, posptr.col);
  253.             wrapctl();
  254.             break;
  255.         case UP:
  256.             posptr=_gettextposition();
  257.             if(posptr.row>1)
  258.                 _settextposition(posptr.row-1, posptr.col);
  259.             break;
  260.         case DOWN:
  261.             posptr=_gettextposition();
  262.             if(posptr.row<24)
  263.                 _settextposition(posptr.row+1, posptr.col);
  264.             else
  265.                 {
  266.                 showchar('\n');
  267.                 _settextposition(posptr.row, posptr.col);
  268.                 }
  269.             break;
  270.         case LEFT:
  271.             posptr=_gettextposition();
  272.             if(posptr.col>1)
  273.                 _settextposition(posptr.row, posptr.col-1);
  274.             break;
  275.         case RIGHT:
  276.             posptr=_gettextposition();
  277.             if(posptr.col<80)
  278.                 _settextposition(posptr.row, posptr.col+1);
  279.             break;
  280.         case GOTO:
  281.             funcstor[seqn].row-=(emu.firstrowaddr-1);
  282.             funcstor[seqn].column-=(emu.firstcoladdr-1);
  283.             if(funcstor[seqn].row<1)
  284.                 funcstor[seqn].row=1;
  285.             if(funcstor[seqn].row>24)
  286.                 funcstor[seqn].row=24;
  287.             if(funcstor[seqn].column<1)
  288.                 funcstor[seqn].column=1;
  289.             if(funcstor[seqn].column>80)
  290.                 funcstor[seqn].row=80;
  291.             _settextposition((emu.tophi_p?25-funcstor[seqn].row:funcstor[seqn].row), funcstor[seqn].column);
  292.             break;
  293.         case NORMAL:
  294.             fgcolor=WHITE;
  295.             bkcolor=BLACK;
  296.             blink=faint=bold=inverse=0;
  297.             atthndl();
  298.             break;
  299.         case BLINK:
  300.             blink=1;
  301.             atthndl();
  302.             break;
  303.         case NOBLINK:
  304.             blink=0;
  305.             atthndl();
  306.             break;
  307.         case BOLD:
  308.             bold=1;
  309.             atthndl();
  310.             break;
  311.         case NOBOLD:
  312.             bold=0;
  313.             atthndl();
  314.             break;
  315.         case FAINT:
  316.             faint=1;
  317.             atthndl();
  318.             break;
  319.         case NOFAINT:
  320.             faint=0;
  321.             atthndl();
  322.             break;
  323.         case INVERSE:
  324.             inverse=1;
  325.             atthndl();
  326.             break;
  327.         case NOINVERSE:
  328.             inverse=0;
  329.             atthndl();
  330.             break;
  331.         case UPN:
  332.             posptr=_gettextposition();
  333.             if(posptr.row-funcstor[seqn].row<1)
  334.                 _settextposition(1, posptr.col);
  335.             else
  336.                 _settextposition(posptr.row-funcstor[seqn].row, posptr.col);
  337.             break;
  338.         case DOWNN:
  339.             posptr=_gettextposition();
  340.             if(posptr.row+funcstor[seqn].row>24)
  341.                 {
  342.                 _settextposition(24,1);
  343.                 for(i=25-(posptr.row+funcstor[seqn].row);i<0;++i)
  344.                     showchar('\n');
  345.                 _settextposition(24, posptr.col);
  346.                 }
  347.             else
  348.                 _settextposition(posptr.row+funcstor[seqn].row, posptr.col);
  349.             break;
  350.         case LEFTN:
  351.             posptr=_gettextposition();
  352.             if(posptr.col-funcstor[seqn].column<1)
  353.                 _settextposition(posptr.row, 1);
  354.             else
  355.                 _settextposition(posptr.row, posptr.col-funcstor[seqn].column);
  356.             break;
  357.         case RIGHTN:
  358.             posptr=_gettextposition();
  359.             if(posptr.col+funcstor[seqn].column>80)
  360.                 _settextposition(posptr.row, 80);
  361.             else
  362.                 _settextposition(posptr.row, posptr.col+funcstor[seqn].column);
  363.             break;
  364.         case ANSIATTRIB:
  365.             ansiatthndl(seqn);
  366.             break;
  367.         case WRAP:
  368.             wrap_p=1;
  369.             wrapctl();
  370.             break;
  371.         case NOWRAP:
  372.             wrap_p=0;
  373.             wrapctl();
  374.             break;
  375.         case GOTOLINE:
  376.             funcstor[seqn].row-=(emu.firstrowaddr-1);
  377.             posptr=_gettextposition();
  378.             if(funcstor[seqn].row<1)
  379.                 funcstor[seqn].row=1;
  380.             if(funcstor[seqn].row>24)
  381.                 funcstor[seqn].row=24;
  382.             _settextposition(emu.tophi_p?25-funcstor[seqn].row:funcstor[seqn].row, 1);
  383.             break;
  384.         case GOTOCOL:
  385.             funcstor[seqn].column-=(emu.firstcoladdr-1);
  386.             posptr=_gettextposition();
  387.             if(funcstor[seqn].column<1)
  388.                 funcstor[seqn].column=1;
  389.             if(funcstor[seqn].column>80)
  390.                 funcstor[seqn].row=80;
  391.             _settextposition(posptr.row, funcstor[seqn].column);
  392.             break;
  393.         case BINATTR:
  394.             if((funcstor[seqn].row-emu.attroffset)&emu.blinkmask)
  395.                 blink=1;
  396.             else
  397.                 blink=0;
  398.             if((funcstor[seqn].row-emu.attroffset)&emu.inversemask)
  399.                 inverse=1;
  400.             else
  401.                 inverse=0;
  402.             if((funcstor[seqn].row-emu.attroffset)&emu.boldmask)
  403.                 bold=1;
  404.             else
  405.                 bold=0;
  406.             if((funcstor[seqn].row-emu.attroffset)&emu.faintmask)
  407.                 faint=1;
  408.             else
  409.                 faint=0;
  410.             atthndl();
  411.             break;
  412.         case GRAPHCHAR:
  413.             showchar(emu.gchars[funcstor[seqn].row]);
  414.             break;
  415.         case BEGGRAPH:
  416.             graphics=1;
  417.             break;
  418.         case ENDGRAPH:
  419.             graphics=0;
  420.             break;
  421.         case TAB:
  422.             posptr=_gettextposition();
  423.             _settextposition(posptr.row, posptr.col+(8-((posptr.col-1)%8)));
  424.             break;
  425.         case BELL:
  426.             putch(7);
  427.             break;
  428.         case DTAB:
  429.             posptr=_gettextposition();
  430.             for(i=0;i<(8-((posptr.col-1)%8));++i)
  431.                 _outtext(" ");
  432.  
  433.         case CRLF:
  434.             showchar('\n');
  435.             break;
  436.         case SAVEPOS:
  437.             posptr=_gettextposition();
  438.             savedrow=posptr.row;
  439.             savedcol=posptr.col;
  440.             break;
  441.         case RESTOREPOS:
  442.             _settextposition(savedrow, savedcol);
  443.             break;
  444.         default:
  445.             break;
  446.         }
  447.     }
  448.  
  449. dispport(c)
  450.     unsigned char c;
  451.     {
  452.     int seqn, done, ok;
  453.     done=0;
  454.     ok=0;
  455.     for(seqn=0;seqn<NFUNCS;seqn++)
  456.         {
  457.         if(emu.funcs[seqn].codes[funcstor[seqn].index]&0xff00)
  458.             switch(emu.funcs[seqn].codes[funcstor[seqn].index])
  459.                 {
  460.                 case END:
  461.                     if(funcstor[seqn].index)
  462.                         {
  463.                         done=1;
  464.                         perffunc(seqn);
  465.                         }
  466.                     break;
  467.                 case BINCOL:
  468.                     funcstor[seqn].column=c-emu.bincoloff;
  469.                     ok=1;
  470.                     if(emu.funcs[seqn].codes[++funcstor[seqn].index]==END)
  471.                         {
  472.                         done=1;
  473.                         perffunc(seqn);
  474.                         }
  475.                     break;
  476.                 case BINROW:
  477.                     funcstor[seqn].row=c-emu.binrowoff;
  478.                     ok=1;
  479.                     if(emu.funcs[seqn].codes[++funcstor[seqn].index]==END)
  480.                         {
  481.                         done=1;
  482.                         perffunc(seqn);
  483.                         }
  484.                     break;
  485.                 case ASCDECCOL:
  486.                     if((c>='0')&&(c<='9'))
  487.                         {
  488.                         ok=1;
  489.                         funcstor[seqn].column*=10;
  490.                         funcstor[seqn].column+=(c-'0');
  491.                         funcstor[seqn].colset=1;
  492.                         }
  493.                     else
  494.                         if(funcstor[seqn].colset&&(c==emu.funcs[seqn].codes[++funcstor[seqn].index]))
  495.                             {
  496.                             ok=1;
  497.                             if(emu.funcs[seqn].codes[++funcstor[seqn].index]==END)
  498.                                 {
  499.                                 done=1;
  500.                                 perffunc(seqn);
  501.                                 }
  502.                             }
  503.                         else
  504.                             clrfuncstor(seqn);
  505.                     break;
  506.                 case ASCDECROW:
  507.                     if((c>='0')&&(c<='9'))
  508.                         {
  509.                         ok=1;
  510.                         funcstor[seqn].row*=10;
  511.                         funcstor[seqn].row+=(c-'0');
  512.                         funcstor[seqn].rowset=1;
  513.                         }
  514.                     else
  515.                         if(funcstor[seqn].rowset&&(c==emu.funcs[seqn].codes[++funcstor[seqn].index]))
  516.                             {
  517.                             ok=1;
  518.                             if(emu.funcs[seqn].codes[++funcstor[seqn].index]==END)
  519.                                 {
  520.                                 done=1;
  521.                                 perffunc(seqn);
  522.                                 }
  523.                             }
  524.                         else
  525.                             clrfuncstor(seqn);
  526.                     break;
  527.                 case ASCDECSEMILIST:
  528.                     if((c>='0')&&(c<='9')&&(funcstor[seqn].listindex<LISTSIZ))
  529.                         {
  530.                         ok=1;
  531.                         funcstor[seqn].list[funcstor[seqn].listindex]*=10;
  532.                         funcstor[seqn].list[funcstor[seqn].listindex]+=(c-'0');
  533.                         }
  534.                     else
  535.                         {
  536.                         if(funcstor[seqn].listindex<LISTSIZ)
  537.                             funcstor[seqn].listindex++;
  538.                         
  539.                         if(c==';')
  540.                             ok=1;
  541.                         else
  542.                             if(c==emu.funcs[seqn].codes[++funcstor[seqn].index])
  543.                                 {
  544.                                 ok=1;
  545.                                 if(emu.funcs[seqn].codes[++funcstor[seqn].index]==END)
  546.                                     {
  547.                                     done=1;
  548.                                     perffunc(seqn);
  549.                                     }
  550.                                 }
  551.                             else
  552.                                 clrfuncstor(seqn);
  553.                         }
  554.                     break;
  555.                 case GRABCHAR:
  556.                     ok=1;
  557.                     funcstor[seqn].row=c;
  558.                     if(emu.funcs[seqn].codes[++funcstor[seqn].index]==END)
  559.                         {
  560.                         done=1;
  561.                         perffunc(seqn);
  562.                         }
  563.                     break;
  564.                 case GRAPHCHAR_T:
  565.                     if(emu.gchars[c])
  566.                         {
  567.                         ok=1;
  568.                         funcstor[seqn].row=c;
  569.                         if(emu.funcs[seqn].codes[funcstor[seqn].index]==END)
  570.                             {
  571.                             done=1;
  572.                             perffunc(seqn);
  573.                             }
  574.                         }
  575.                     else
  576.                         clrfuncstor(seqn);
  577.                     break;
  578.                 }
  579.         else
  580.             if(c==emu.funcs[seqn].codes[funcstor[seqn].index])
  581.                 {
  582.                 ok=1;
  583.                 if(emu.funcs[seqn].codes[++funcstor[seqn].index]==END)
  584.                     {
  585.                     done=1;
  586.                     perffunc(seqn);
  587.                     }
  588.                 }
  589.             else
  590.                 clrfuncstor(seqn);
  591.         if(done)
  592.             {
  593.             ok=1;
  594.             for(seqn=0;seqn<NFUNCS;seqn++)
  595.                 clrfuncstor(seqn);
  596.             break;
  597.             }
  598.         }
  599.     if(!ok)
  600.         showchar(c);
  601.     }
  602.  
  603. initdisp()
  604.     {
  605.     int i;
  606.     for(i=0;i<NFUNCS;i++)
  607.         {
  608.         funcstor[i].listindex=LISTSIZ;
  609.         clrfuncstor(i);
  610.         }
  611.     bold=faint=blink=inverse=0;
  612.     fgcolor=WHITE;
  613.     bkcolor=BLACK;
  614.     graphics=0;
  615.     wrap_p=emu.default_wrap_p;
  616.     }
  617.  
  618. main(argc, argv)
  619.     int argc;
  620.     char **argv;
  621.     {
  622.     FILE *playfd;
  623.     char c;
  624.     struct rccoord posptr;
  625.     int follow, emufd, i;
  626.     _getvideoconfig(vconf);
  627.     _setvideomode(_DEFAULTMODE);
  628.     if(argc!=3)
  629.         {
  630.         printf("Copyright (C) 1992 Peter Edward Cann, all rights reserved.\n");
  631.         printf("USAGE: termplay <emu or - > <script>\n");
  632.         printf("<emu> is an emulation file base pathname.\n");
  633.         printf("<script> is a file of codes to play.\n");
  634.         printf("The environment variable PCCPPATH is used for the emulation file if set.\n");
  635.         exit(1);
  636.         }
  637.     if(!strcmp(getenv("REMOTE"), "YES"))
  638.         {
  639.         printf("You appear to be logged in remotely, judging by the environment\n");
  640.         printf("variable REMOTE, so this is probably a very bad idea.\n");
  641.         printf("Are you sure you want to run TERMPLAY? (y or n) --> ");
  642.         if(getchar()!='y') /* Note getchar() and not getch()! */
  643.             {
  644.             printf("n\nI didn't think so!\n");
  645.             exit(99);
  646.             }
  647.         else
  648.             printf("y\nOK, you're the boss!\n");
  649.         }
  650.     fpname[0]='\0';
  651.     if(argv[1][0]!='-')
  652.         {
  653.         if((getenv("PCCPPATH")==NULL)||(argv[1][0]=='\\')||(argv[1][0]&&(argv[1][1]==':'))||(argv[1][0]=='.'))
  654.             sprintf(fpname, "%s.emu", argv[1]);
  655.         else
  656.             sprintf(fpname, "%s\\%s.emu", getenv("PCCPPATH"), argv[1]);
  657.         if((emufd=open(fpname, O_RDONLY|O_BINARY))==-1)
  658.             {
  659.             printf("Error opening emulation file %s.\n", fpname);
  660.             exit(2);
  661.             }
  662.         else
  663.             if(read(emufd, &emu, sizeof(emu))!=sizeof(emu))
  664.                 {
  665.                 printf("Error reading emulation file %s.\n", fpname);
  666.                 exit(3);
  667.                 }
  668.             else;
  669.         }
  670.     else
  671.         {
  672.         nullemu();
  673.         emu.funcs[0].func=LEFT;
  674.         emu.funcs[0].codes[0]='\b';
  675.         emu.funcs[0].codes[1]=END;
  676.         emu.funcs[1].func=DTAB;
  677.         emu.funcs[1].codes[0]='\t';
  678.         emu.funcs[1].codes[1]=END;
  679.         emu.funcs[2].func=BELL;
  680.         emu.funcs[2].codes[0]='\007';
  681.         emu.funcs[2].codes[1]=END;
  682.         }
  683.     if((playfd=fopen(argv[2], "r"))==NULL)
  684.         {
  685.         printf("Couldn't open script file %s.\n");
  686.         exit(10);
  687.         }
  688.     setmode(fileno(playfd), O_BINARY);
  689.     _clearscreen(_GCLEARSCREEN);
  690.     initdisp();
  691.     atthndl();
  692.     _settextposition(1,1);
  693.     if(vconf->adapter==_VGA)
  694.         _remappalette(GRAY, (long)0x00202020); /* Lighten up gray */
  695.     while(1)
  696.         {
  697.         c=getc(playfd);
  698.         updstatus(c);
  699.         getch();
  700.         dispport(c);
  701.         }
  702.     }
  703.