home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / demo3.zoo / demo / misc / hpmgr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-24  |  11.9 KB  |  689 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*
  9. **    hp2621 terminal emulator
  10. */
  11. #ifndef lint
  12. static char rcsid[] = "$Header: hpmgr.c,v 4.2 88/06/22 14:37:42 bianchi Exp $";
  13. #endif
  14.  
  15. #include "term.h"
  16. #include <ctype.h>
  17.  
  18. #define MAX_ROW        24
  19. #define MAX_COL        80
  20.  
  21. #define C_X    (MAX_COL*f_w)        /* width of the screen */
  22. #define C_Y    (MAX_ROW*f_h)        /* height of the screen */
  23. /*
  24. FILE *outfile;
  25. */
  26.  
  27. extern int more_out;
  28. static char *m_fields[16];
  29.  
  30. int cur_x = 0;        /* cursor location */
  31. int cur_y = 0;
  32.  
  33. extern char *shiftline();
  34. extern char *del_line();
  35. int banner_space;
  36. int x,y,w,h,f_w,f_h;
  37.  
  38. int in_mode = 0;    /* insert mode flag */
  39.  
  40. #define    min(a,b)    ((a)<(b)?(a):(b))
  41. #define    max(a,b)    ((a)>(b)?(a):(b))
  42.  
  43. int margin;
  44.  
  45. main(argc,argv)
  46. int argc;
  47. char **argv;
  48. {
  49.     extern int verboseflag;
  50.     int dummy;
  51.  
  52.     ckmgrterm( *argv );
  53.  
  54.     if (argc > 1 && !(strcmp(argv[1],"-v")))
  55.     {
  56.         verboseflag = 1;
  57.     }
  58.  
  59. /*
  60.     if ((outfile = fopen("hpmgr.out","w+")) == NULL)
  61.     {
  62.         printf("can't create logfile\n");
  63.         exit(1);
  64.     }
  65. */
  66.  
  67.     m_termin = stdin;
  68.     m_termout = stdout;
  69.  
  70.     m_push(P_MENU|P_POSITION|P_EVENT|P_FLAGS);
  71.     m_setmode(M_ABS);
  72.     get_size(&x,&y,&w,&h);
  73.     get_font(&f_w,&f_h);
  74.     get_margin(&margin);
  75.  
  76.     banner_space = f_h+2;
  77.  
  78.     m_setmode(M_BACKGROUND);
  79.  
  80.     if ((w != C_X ) || ((C_Y + banner_space )!= h))
  81.     {
  82.         m_shapewindow(x,y,C_X+(2*margin),
  83.                   C_Y + banner_space + (2*margin));
  84.     }
  85.  
  86.     get_size(&x,&y,&w,&h);
  87.     m_clear();
  88.  
  89.     /* print the banner line */
  90.     m_printstr("                   HP2621 EMULATOR");
  91.     m_flush();
  92.     m_func(B_INVERT);
  93.     m_bitwrite(0,0,w,banner_space-1);
  94.     m_flush();
  95.     m_func(B_OR);
  96.  
  97.     m_textregion(0,banner_space,C_X,C_Y);
  98.     m_clear();
  99.     m_flush();
  100.  
  101.     do_env("TERM=","h2");
  102.     do_env("TERMCAP=","");
  103.     getpty((char**)0);
  104.     printf("\n\rPANIC -- getpty failed!! something is wrong!!\n\r");
  105. }
  106.  
  107.  
  108.  
  109. int
  110. get_margin(x)
  111. int *x;
  112.  
  113.    { 
  114.    register int count;
  115.  
  116.    if ((count = get_info(G_SYSTEM,m_fields)) >= 4) {
  117.       *x = atoi(m_fields[3]); 
  118.       return(1);
  119.       }
  120.    else 
  121.     return(-count);
  122.    }
  123.  
  124. inmassage(ptr,cnt)
  125. char *ptr; int cnt;
  126. {
  127.     return(cnt);
  128. }
  129.  
  130. outmassage(ptr,cnt)
  131. char *ptr; int cnt;
  132. {
  133.     char *start = ptr;
  134.     char *to, *sptr, *shiftptr;
  135.     int newcnt = 0;
  136.     char newbuf[1024];
  137.     char smallb[1024];
  138.     static char holdb[1024];
  139.     
  140.     static int gotesc = 0;
  141.     static int gotamp = 0;
  142.     static int gota = 0;
  143.     static int gotc = 0;
  144.     static int goty = 0;
  145.     static int gotr = 0;
  146.     static int gotd = 0;
  147.     static int gotj = 0;
  148.  
  149.     static int xval = 0;
  150.     static int yval = 0;
  151.  
  152. /*
  153. fprintf(outfile,"outmassage --  more = %d  cnt = %d\n",more_out,cnt);
  154. fflush(outfile);
  155. */
  156.     /*
  157.     **    check to see if we have more data on hold
  158.     */
  159.     if (cnt == -2)
  160.     {
  161.         if (more_out)
  162.         {
  163.             to = ptr;
  164.             mystrncpy(&to,holdb,more_out);
  165.             cnt = more_out;
  166.             more_out = 0;
  167.         }
  168.         else
  169.         {
  170.             printf("panic -- outmassage got cnt = %d and more_out = %d\n",
  171.                             cnt,more_out);
  172.             exit(1);
  173.         }
  174.     }
  175.     else
  176.     {
  177.         if (cnt <= 0)
  178.             return(cnt);
  179.     }
  180.  
  181.     for(to = newbuf;(cnt > 0) && (newcnt < (1024-100));ptr++,cnt--)
  182.     {
  183.  
  184.         *ptr &= 0177;
  185. /*
  186. fputc(*ptr,outfile);
  187. */
  188.  
  189.         if (gotesc)
  190.         {
  191.             switch (*ptr)
  192.             {
  193.                 case '\000':
  194.                     goto out;
  195.                 case ESC :
  196.                     gotamp = gota = gotc = gotj = gotd  =
  197.                         goty = gotr = 0;
  198.                     goto out;
  199.                 case '&' :
  200.                     gotamp = 1;
  201.                     goto out;
  202.                 case 'a' :
  203.                     gota = 1;
  204.                     goto out;
  205.                 case 'y' :
  206.                     goty = 1;
  207.                     goto out;
  208.                 case 'r' :
  209.                     gotr = 1;
  210.                     goto out;
  211.                 case 'c' :
  212.                     gotc = 1;
  213.                     goto out;
  214.  
  215.                 case 'd' :
  216.                     gotd = 1;
  217.                     goto out;
  218.                 case '1' :
  219.                 case '2' :
  220.                 case '3' :
  221.                     /*
  222.                     **    skip tabs for now
  223.                     */
  224.                     if (!gotamp)
  225.                     {
  226.                         gotesc = 0;
  227.                         goto out;
  228.                     }
  229.                 case '0' :
  230.                 case '4' :
  231.                 case '5' :
  232.                 case '6' :
  233.                 case '7' :
  234.                 case '8' :
  235.                 case '9' :
  236.                     if (gotc || goty || gotr)
  237.                     {
  238.                         yval *=10;
  239.                         yval += (*ptr) - '0';
  240.                     }
  241.                     else
  242.                     {
  243.                         xval *=10;
  244.                         xval += (*ptr) - '0';
  245.                     }
  246.                     goto out;
  247.                 case 'j' :
  248.                     gotj = 1;
  249.                     goto out;
  250.                 /*
  251.                 **    begin standout mode
  252.                 */
  253.                 case 'D' :
  254.                     if (gotamp && gotd)
  255.                     {
  256.                         *to++ = ESC;
  257.                         *to++ = 'i';
  258.                         newcnt += 2;
  259.                     }
  260.                     gotesc = gotamp = gotj = gotd = 0;
  261.                     goto out;
  262.                 /*
  263.                 **    skip keyboard mode for now
  264.                 **    but  handle end of standout mode
  265.                 */
  266.                 case '@' :
  267.                     if (gotamp && gotd)
  268.                     {
  269.                         *to++ = ESC;
  270.                         *to++ = 'n';
  271.                         newcnt += 2;
  272.                     }
  273.                     gotesc = gotamp = gotj = gotd = 0;
  274.                     goto out;
  275.                 case 'B' :
  276.                     if (gotamp)
  277.                     {
  278.                         gotesc = gotamp = gotj = 0;
  279.                         goto out;
  280.                     }
  281.                     goto parsepanic;
  282.                 
  283.                 /*
  284.                 **    clear screen
  285.                 */
  286.                 case 'J' :
  287.                     *to++ = ESC;
  288.                     *to++ = 'C';
  289.                     newcnt += 2;
  290.                     gotesc = 0;
  291.                     goto out;
  292.  
  293.                 /*
  294.                 **    home cursor
  295.                 */
  296.                 case 'H' :
  297.                     sprintf(smallb,"%c%d,%dM",ESC,0,0);    
  298.                     mystrncpy(&to,smallb,strlen(smallb));
  299.                     newcnt += strlen(smallb);
  300.                     cur_x = cur_y = gotesc = 0;
  301.                     goto out;
  302.  
  303.                 /*
  304.                 **    clear to eol
  305.                 */
  306.                 case 'K' :
  307.                     *to++ = ESC;
  308.                     *to++ = 'c';
  309.                     newcnt += 2;
  310.                     gotesc =0;
  311.                     goto out;
  312.                 
  313.                 /*
  314.                 **    delete line
  315.                 */
  316.                 case 'M' :
  317.                     *to++ = ESC;
  318.                     *to++ = 'd';
  319.                     newcnt += 2;
  320.                     gotesc = 0;
  321.                     goto out;
  322.                     
  323.                 /*
  324.                 **    end insert mode 
  325.                 */
  326.                 case 'R' :
  327.                     if (gota && gotamp)
  328.                     {
  329.                         goto vertaddr;
  330.                     }
  331.                     in_mode = 0;
  332.                     gotesc = 0;
  333.                     goto out;
  334.                 /*
  335.                 **    start insert mode 
  336.                 */
  337.                 case 'Q' :
  338.                     in_mode = 1;
  339.                     gotesc = 0;
  340.                     goto out;
  341.  
  342.                 /*
  343.                 **    open line 
  344.                 */
  345.                 case 'L' :
  346.                     *to++ = ESC;
  347.                     *to++ = 'a';
  348.                     newcnt += 2;
  349.                     gotesc=0;
  350.                     goto out;
  351.                     
  352.                 /*
  353.                 **    up line 
  354.                 */
  355.                 case 'A' :
  356.                     *to++ = ESC;
  357.                     *to++ = 'u';
  358.                     newcnt += 2;
  359.                     gotesc=0;
  360.                     goup();
  361.                     goto out;
  362.                     
  363.                 case 'i' :
  364.                     cur_x -= (cur_x%8?cur_x%8:8);
  365.                     sprintf(smallb,"%c%d,%dM", ESC,cur_x,cur_y);    
  366.                     mystrncpy(&to,smallb,strlen(smallb));
  367.                     newcnt += strlen(smallb);
  368.                     gotesc = 0;
  369.                     goto out;
  370.  
  371.                 /*
  372.                 **    delete char
  373.                 */
  374.                 case 'P' :
  375.                     shiftptr = del_line();
  376.                     mystrncpy(&to,shiftptr,strlen(shiftptr));
  377.                     newcnt += strlen(shiftptr);
  378.                     gotesc=0;
  379.                     goto out;
  380.                     
  381.                 /*
  382.                 **    non destructive space
  383.                 **    and horizontal motion
  384.                 */
  385.                 case 'C' :
  386.                     if (gotamp)
  387.                     {
  388.                         if (gota)
  389.                         {
  390.                             if(gotr||goty)
  391.                             {
  392.                                 goto fulladdr;
  393.                             }
  394.                             sprintf(smallb,
  395.                                 "%c%d,%dM",ESC,xval,cur_y);    
  396.                             mystrncpy(&to,smallb,strlen(smallb));
  397.                             newcnt += strlen(smallb);
  398.                             cur_x = xval;
  399.                             gotesc = gotamp = gota =
  400.                                 gotc = xval = yval = 0;
  401.                         }
  402.                         else
  403.                         {
  404.                             goto parsepanic;
  405.                         }
  406.                     }
  407.                     else
  408.                     {
  409.                         *to++ = ESC;
  410.                         *to++ = 'r';
  411.                         newcnt += 2;
  412.                         gotesc=0;
  413.                         goright();
  414.                     }
  415.                     goto out;
  416.                     
  417.  
  418.                 case 'Y' :
  419.             vertaddr:
  420.                     if (gotamp && gota)
  421.                     {
  422.             fulladdr:
  423.                         if (goty || gotr)
  424.                         {
  425.                             int tmp;
  426.                             tmp = xval;
  427.                             xval = yval;
  428.                             yval = tmp;
  429.                         }
  430.                         if (gotc || goty || gotr)
  431.                         {
  432.                             /*
  433.                             **    2-D cursor motion
  434.                             */
  435. /*
  436. fprintf(outfile,"xval = %d, yval = %d",xval,yval);
  437. fflush(outfile);
  438. */
  439.                             if (yval > MAX_ROW-1)
  440.                             {
  441.                                 fprintf(stderr,
  442.                                 "PANIC -- got address > 23 = %d", yval);
  443.                                 sleep(10);
  444.                             }
  445.                             sprintf(smallb,"%c%d,%dM",
  446.                                 ESC,xval,yval);    
  447.                             mystrncpy(&to,smallb,strlen(smallb));
  448.                             newcnt += strlen(smallb);
  449.                             cur_x = xval;
  450.                             cur_y = yval;
  451.                             gotesc = gotamp = gota = gotc =
  452.                                 xval = yval = goty = gotr = 0;
  453.                         }
  454.                         else
  455.                         {
  456.                             /*
  457.                             **    vertical motion
  458.                             */
  459.                             if (xval > MAX_ROW-1)
  460.                             {
  461.                                 fprintf(stderr,
  462.                                 "PANIC -- got address > 23 = %d", xval);
  463.                                 sleep(10);
  464.                             }
  465.                             sprintf(smallb,
  466.                                 "%c%d,%dM",ESC,cur_x,xval);    
  467.                             mystrncpy(&to,smallb,strlen(smallb));
  468.                             newcnt += strlen(smallb);
  469.                             cur_y = xval;
  470.                             gotesc = gotamp = gota =
  471.                                 gotr = goty = gotc = xval = yval = 0;
  472.                         }
  473.                         goto out;
  474.                     }
  475.                     else
  476.                     {
  477.                         goto parsepanic;
  478.                     }
  479.                 default : 
  480.           parsepanic:;
  481. /*
  482.                     fprintf(outfile,"|BAD ESCAPE CODE esc = %d gotamp = %d gota = %d gotc = %d gotj = %d goty = %d gotr = %d code = %o|\n",
  483.                     gotesc,gotamp,gota,gotc,
  484.                         gotj,goty,gotr,*ptr);
  485. */
  486.                     printf("|BAD ESCAPE CODE esc = %d gotamp = %d gota = %d gotc = %d gotj = %d goty = %d gotr = %d code = %o|\n",
  487.                     gotesc,gotamp,gota,gotc,
  488.                         gotj,goty,gotr,*ptr);
  489.                     sleep(10);
  490.                     gotesc = gotamp = gota = gotc = xval = yval = 0;
  491.                     goto out;
  492.             }
  493.         }
  494.  
  495.         switch (*ptr)
  496.         {
  497.             case '\000' :
  498.                 break;
  499.             case ESC:
  500.                 gotamp = gota = gotc = gotj =
  501.                     goty = gotr = xval = yval = 0;
  502.                 gotesc = 1;
  503.                 break;
  504.             case '\010' :
  505.                 goleft();
  506.                 goto dochar;
  507.             case '\012' :
  508.                 godown();
  509.                 goto dochar;
  510.             case '\015' :
  511.                 cur_x = 0;
  512.                 goto dochar;
  513.             case '\011' :
  514.                 cur_x += (8 - (cur_x%8));
  515.                 goto dochar;
  516.             default:
  517.                 if (in_mode && isprint(*ptr))
  518.                 {
  519.                     shiftptr = shiftline();
  520.                     mystrncpy(&to,shiftptr,strlen(shiftptr));
  521.                     newcnt += strlen(shiftptr);
  522.                 }
  523.                 goright();
  524.         dochar :
  525.                 *to++ = *ptr;
  526.                 newcnt++;
  527.                 break;
  528.         }
  529.     out:;
  530.     }
  531.  
  532.     /*
  533.     **    make sure we didn't over run the end of the buffer
  534.     */
  535.     if ((newcnt < 0) || (newcnt >= 1024))
  536.     {
  537.         printf("panic: newcnt = %d, out of range\n",newcnt);
  538.     }
  539.     /*
  540.     **    did we run out of buffer space before finishing the input?
  541.     */
  542.     if (cnt)
  543.     {
  544.         to = holdb;
  545.         mystrncpy(&to,ptr,cnt);
  546.         more_out = cnt;
  547.     }
  548.     strncpy(start,newbuf,newcnt);
  549. /*
  550.     fprintf(outfile,"newcnt = %d\n",newcnt);
  551.     if ((newcnt > 0) && (newcnt < 1024))
  552.         fwrite(start,sizeof(char),newcnt,outfile);
  553. */
  554.     return(newcnt);
  555. }
  556.  
  557. goright()
  558. {
  559.     if (cur_x == (MAX_COL-1))
  560.     {
  561.         cur_x = 0;
  562.         godown();
  563.     }
  564.     else
  565.     {
  566.         cur_x++;
  567.     }
  568. }
  569.  
  570. goleft()
  571. {
  572.     cur_x = max(0,cur_x - 1);
  573. }
  574.  
  575. goup()
  576. {
  577.     cur_y = max(0,cur_y - 1);
  578. }
  579.  
  580. godown()
  581. {
  582.     cur_y = min(MAX_ROW - 1, cur_y + 1);
  583. }
  584.  
  585. char *
  586. shiftline()
  587. {
  588.     static char shiftbuf[1024];
  589.     static char buf2[1024];
  590.  
  591. /*
  592. fprintf(outfile,"cur_x = %d cur_y = %d\n\n",cur_x, cur_y);
  593. fflush(outfile);
  594. */
  595.  
  596.     /*    m_func     */
  597.     sprintf(shiftbuf,"%c%d%c",ESC,B_COPY,E_BITBLT);
  598.  
  599.     /*    m_bitcopy */
  600.     sprintf(buf2,"%c%d,%d,%d,%d,%d,%d%c",
  601.           ESC,
  602.           (cur_x+1)*f_w,            /* dest  x */
  603.           banner_space + (cur_y*f_h),        /* dest  y */
  604.           f_w*((MAX_COL-1)-cur_x),        /* width  */
  605.           f_h,                    /* height */
  606.           cur_x*f_w,                /* src  x */
  607.           banner_space + (cur_y*f_h),        /* src  y */
  608.           E_BITBLT);
  609.     strcat(shiftbuf,buf2);
  610.  
  611.     /*    m_func        */
  612.     sprintf(buf2,"%c%d%c",ESC,B_CLEAR,E_BITBLT);
  613.     strcat(shiftbuf,buf2);
  614.  
  615. /*
  616.     sprintf(buf2,"%c%d,%d,%d,%d%c",
  617.           ESC,
  618.           cur_x*f_w,             x
  619.           banner_space + (cur_y*f_h),     y 
  620.           f_w,                width 
  621.           f_h,                height
  622.           E_BITBLT);
  623.     strcat(shiftbuf,buf2);
  624.  
  625. */
  626.     /*    m_func        */
  627.     sprintf(buf2,"%c%d%c",ESC,B_OR,E_BITBLT);
  628.     strcat(shiftbuf,buf2);
  629.     return(shiftbuf);
  630. }
  631.  
  632. char *
  633. del_line()
  634. {
  635.     static char shiftbuf[1024];
  636.     static char buf2[1024];
  637.     /*    m_func     */
  638.     sprintf(shiftbuf,"%c%d%c",ESC,B_COPY,E_BITBLT);
  639.  
  640.     /*    m_bitcopy */
  641.     sprintf(buf2,"%c%d,%d,%d,%d,%d,%d%c",
  642.           ESC,
  643.           cur_x*f_w,                /* dest x */
  644.           banner_space + (cur_y*f_h),        /* dest y */
  645.           f_w*((MAX_COL-1)-cur_x),        /* width  */
  646.           f_h,                    /* height */
  647.           (cur_x+1)*f_w,            /* src  x */
  648.           banner_space + (cur_y*f_h),        /* src  y */
  649.           E_BITBLT);
  650.     strcat(shiftbuf,buf2);
  651.  
  652.     /*    m_func        */
  653.     sprintf(buf2,"%c%d%c",ESC,B_CLEAR,E_BITBLT);
  654.     strcat(shiftbuf,buf2);
  655.  
  656.     sprintf(buf2,"%c%d,%d,%d,%d%c",
  657.           ESC,
  658.           C_X-f_w,            /*  x */
  659.           banner_space + (cur_y*f_h),    /*  y */
  660.           f_w,                /* width  */
  661.           f_h,                /* height */
  662.           E_BITBLT);
  663.     strcat(shiftbuf,buf2);
  664.  
  665.     /*    m_func        */
  666.     sprintf(buf2,"%c%d%c",ESC,B_OR,E_BITBLT);
  667.     strcat(shiftbuf,buf2);
  668.     return(shiftbuf);
  669. }
  670.  
  671. mystrncpy(to,from,cnt)
  672. char **to, *from; int cnt;
  673. {
  674.     while(cnt > 0)
  675.     {
  676.         **to = *from;
  677.         (*to)++;    /* increment the pointer,NOT the pointer to the pointer */
  678.         from++;
  679.         cnt--;
  680.     }
  681. }
  682. cleanup()
  683. {
  684.     m_pop();
  685.     m_textreset();
  686.     m_clear();
  687.     exit(0);
  688. }
  689.