home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / stg_v4.lzh / pop.c < prev    next >
C/C++ Source or Header  |  1994-11-11  |  13KB  |  599 lines

  1. /*
  2.  * pop - utility to create a new window
  3.  *
  4.  * Version History:
  5.  * V0.? - Original version in C by StG (probably late '87)
  6.  * V1.0 - First assembly version by StG (source ate by disk) for StGNet
  7.  * V2.0 - Disassembled by Bug and hacked for more options
  8.  *        (not released due to bugs in VDG support)
  9.  * V3.0 - Rewrote by Bug and added -c option
  10.  *        (modeled after my type utility in the RBW)
  11.  * V4.0 - Rewrite by Bug in C with VDG support fixed (help from StG)
  12.  * V5.0 - trashed and recoded by StG for OSK support, new var naming style
  13.  * V5.1 - included patches for awkward SELECT in KDwin and tmode pag setting
  14.  * V5.2 - added auto termcap features from edit
  15.  * V5.3 - fixed window flip problem (caused by DWSET bug) and added -s opt
  16.  * V5.4 - use SHELL variable for default program and options
  17.  * V5.5 - created setenv() to fix environ mod'ing problems, added set to PORT
  18.  * V6.0 - changed 0/1 path order (KD finally matched coco) and added modes
  19.  *
  20.  * Future enhancements:
  21.  *
  22.  * -v = vertical split screen (put []'s on ends of label)
  23.  * -h = horizontal split screen
  24.  * -d = auto SRCDBG mode (pop 2 windows)
  25.  *  $ = separation character for multiple commands (for split, etc)
  26.  * -f = select a font file(?)
  27.  * -i = interactive mode (mousey)
  28. */
  29.  
  30. #include <stdio.h>
  31. #include <sgstat.h>
  32.  
  33. #define VER "V6.0"
  34.  
  35. #define ERR (-1)
  36. #define MAXBUF 512
  37. #define dash(v) while(*++v && **v=='-') while (*++*v) switch(tolower(**v))
  38.  
  39. extern int errno;
  40. int os9fork();
  41. int chain();
  42. char *modlink();
  43. char *malloc();
  44. char *getenv();
  45. extern char **environ;
  46.  
  47. int iPopCurWin=0;
  48. int iNoLabel=0;
  49. int iNoSelect=0;
  50. int iPopVdg=0;
  51. int iWait=0;
  52. int iNoTCap=0;
  53. char acBuf[MAXBUF];
  54. struct sgbuf sPathDesc;
  55. char acWinName[32];
  56. char *pcWinDesc;
  57. char *pcInitMod;
  58. char **ppcWinTypes;
  59. int iCol,iRow,iFor,iBack,iBord;
  60. int iTemp;
  61.  
  62. int hFile;
  63. char acEnt[MAXBUF];
  64. char *gen1="Win|KD Windows Termcap created by POP:\\\n";
  65. char *gen2="    :cl=\\E[;H\\E[2J:cm=\\E[%i%d;%dH:nd=\\E[C:up=\\E[A:\\\n";
  66. char *gen3="    :so=\\E[7m:se=\\E[m:us=\\E[4m:ue=\\E[m:md=\\E[1m:mr=\\E[7m:\\\n";
  67. char *gen4="    :mb=\\E[5m:me=\\E[m:ce=\\E[K:ho=\\E[H:bs:am:pt:\n";
  68.  
  69. #ifndef _OSK
  70. int iWinType=2;
  71. #else
  72. int iWinType=0;
  73. #endif
  74.  
  75. char *apcWinTypeList[]=
  76. {
  77. /*   Fg Bg # XX-YY Desc */
  78. #ifndef _OSK
  79.     "  0   2 1 = 40x25 Text",
  80.     "  0   2 2 = 80x25 Text",
  81.     "  0   1 5 = 80x24 ???x??? Graphics",
  82.     "  0   2 6 = 40x24 ???x??? Graphics",
  83.     "  0   2 7 = 80x24 ???x??? Graphics",
  84.     "  0   2 8 = 40x24 ???x??? Graphics",
  85. #else
  86.     "255 002 0 = 80x26 640x208 16  Color",
  87.     "255 002 1 = 80x26 640x416 16  Color Interlaced",
  88.     "255 002 2 = 80x52 640x416 16  Color Interlaced",
  89.     "255 002 3 = 40x26 320x208 256 Color",
  90.     "255 002 4 = 40x26 320x416 256 Color Interlaced",
  91.     "255 002 5 = 40x52 320x416 256 Color Interlaced",
  92.     "255 002 6 = 90x30 720x240 16  Color Overscan",
  93.     "255 002 7 = 90x60 720x480 16  Color Interlaced/Overscan",
  94.     "255 002 8 = 48x30 384x240 256 Color Overscan",
  95.     "255 002 9 = 48x60 384x480 256 Color Interlaced/Overscan",
  96. #endif
  97.     0
  98. };
  99.  
  100. char *pcTemp;
  101.     
  102. /*
  103. _gs_winfo(path,data)
  104. int path;
  105. char *data;
  106. {
  107. #asm
  108.     move.l 0(sp),d0
  109.     movea.l 4(sp),a0
  110.     move.l #$A1,d1
  111.     os9 I$GetStt
  112.     bcs err
  113.  
  114.     move.l a0,d0
  115.     bra dun
  116.  
  117. err:
  118.     move.l d1,errno(a6)
  119.     moveq.l #-1,d0
  120. dun:
  121. #endasm
  122. }
  123. */
  124.  
  125. #ifdef BOGUS
  126.  
  127. setenv(pcVar,pcSet)
  128. char *pcVar,*pcSet;
  129. {
  130.     static char **ppcEnviron=0;
  131.     static int iEnviron;
  132.     char **ppcTemp;
  133.  
  134.     if (environ!=ppcEnviron)
  135.     {
  136.         iEnviron=0;
  137.         ppcTemp=environ;
  138.         while (*ppcTemp)
  139.         {
  140.             iEnviron++;
  141.             ppcTemp++;
  142.         }
  143.         iEnviron+=8;
  144.         ppcEnviron=(char**)malloc(4*iEnviron+4);
  145.         if (!ppcEnviron) exit(errno);
  146.         ppcTemp=ppcEnviron;
  147.         while (*environ) *ppcTemp++=*environ++;
  148.         *ppcTemp=0;
  149.         environ=ppcEnviron;
  150.     }
  151.     ppcTemp=ppcEnviron;
  152.     while (*ppcTemp)
  153.     {
  154.         if (!strncmp(*ppcTemp,pcVar,strlen(pcVar)))
  155.         {
  156.             if (*(*ppcTemp+strlen(pcVar))=='=') break;
  157.         }
  158.         ppcTemp++;
  159.     }
  160.     if (!*ppcTemp)
  161.     {
  162.         if (ppcTemp-ppcEnviron>=iEnviron)
  163.         {
  164.             iEnviron+=8;
  165.             ppcTemp=(char**)malloc(4*iEnviron+4);
  166.             if (!ppcTemp) exit(errno);
  167.             environ=ppcTemp;
  168.             while (*ppcEnviron) *ppcTemp++=*ppcEnviron++;
  169.             *ppcTemp=0;
  170.             ppcEnviron=environ;
  171.         }
  172.     }
  173.     *ppcTemp=malloc(strlen(pcVar)+1+strlen(pcSet)+1);
  174.     if (!*ppcTemp) exit(errno);
  175.     strcpy(*ppcTemp,pcVar);
  176.     strcat(*ppcTemp,"=");
  177.     strcat(*ppcTemp,pcSet);
  178.     *++ppcTemp=0;
  179.  
  180.     return(0);
  181. }
  182. #endif
  183. #include "setenv.c"
  184.  
  185. /* debugging code!
  186. pargs(ppcList)
  187. char **ppcList;
  188. {
  189.     printf("%08X:->\n",ppcList);
  190.     while (*ppcList)
  191.     {
  192.         printf("%08X:%s\n",*ppcList,*ppcList);
  193.         ppcList++;
  194.     }
  195.     printf("%08X.\n",*ppcList);
  196. }
  197. */
  198.  
  199. char **
  200. args(pcCmd)
  201. char *pcCmd;
  202. {
  203.     char **ppcRoot,**ppcTemp,*pcTemp,*pcLast,*pcCopy;
  204.     int iArgs=0;
  205.  
  206.     pcTemp=pcCmd;
  207.     iArgs++;
  208.     while (*pcTemp) if (*pcTemp++==' ') iArgs++;
  209.  
  210.     ppcTemp=ppcRoot=(char**)malloc(4*iArgs+4);
  211.     if (!ppcRoot) exit(errno);
  212.  
  213.     pcLast=pcTemp=pcCmd;
  214.     while (*pcTemp)
  215.     {
  216.         while (*pcTemp) if (*pcTemp++==' ') break;
  217.         if (!*pcTemp) pcTemp++;
  218.  
  219.         pcCopy=*ppcTemp=malloc(pcTemp-pcLast);
  220.         if (!pcCopy) exit(errno);
  221.  
  222.         while (pcTemp-pcLast) *pcCopy++=*pcLast++;
  223.         *--pcCopy=0;
  224.  
  225.         ppcTemp++;
  226.         if (!--iArgs) break;
  227.     }
  228.     *ppcTemp=0;
  229.  
  230.     return(ppcRoot);
  231. }
  232.  
  233. main(argc,argv)
  234. char **argv;
  235. {
  236. #ifdef OSK
  237.     strcpy(acBuf,"\x01\x06\x01\x0c");
  238.     iTemp=4;
  239.     while (iTemp) acBuf[--iTemp]+=0x68;
  240.     pcInitMod=modlink(acBuf,0,0);
  241.     if ((int)pcInitMod==ERR) exit(errno);
  242.     iTemp=*((unsigned short *)(pcInitMod+0x50));
  243.     if
  244.     (
  245.         *(pcInitMod+iTemp+0)!='I' ||
  246.         *(pcInitMod+iTemp+1)!='M' ||
  247.         *(pcInitMod+iTemp+2)!='S'
  248.     )
  249.     if
  250.     (
  251.         *(pcInitMod+iTemp+0)!='M' ||
  252.         *(pcInitMod+iTemp+1)!='M' ||
  253.         *(pcInitMod+iTemp+2)!='1'
  254.     )
  255.     {
  256.         printf("pop: cant identify system\n");
  257.         printf("Please call StG @ (317) 241-6401 and report error\n");
  258.         exit(1);
  259.     }
  260.     munlink(pcInitMod);
  261. #endif
  262.  
  263.     iFor=iBack=iBord=-1;
  264.  
  265.     /* process options */
  266.     dash(argv)
  267.     {
  268.     case 'c': iPopCurWin++; break;
  269.     case 'l': iNoLabel++; break;
  270.     case 's': iNoSelect++; break;
  271.     case 'w': iWait++; break;
  272.     case 't': iNoTCap++; break;
  273.     case 'f': if (*++*argv=='=') ++*argv; iFor=atoi(*argv); *(*argv+1)=0; break;
  274.     case 'b': if (*++*argv=='=') ++*argv; iBack=atoi(*argv); *(*argv+1)=0; break;
  275.     case 'o': if (*++*argv=='=') ++*argv; iBord=atoi(*argv); *(*argv+1)=0; break;
  276.  
  277. #ifndef _OSK
  278.     case 'v': iPopVDG++; break;
  279. #endif
  280.  
  281.     case '?':
  282.         printf("Pop {-opt} {program} - create new window\n");
  283.         printf("     -l = don't label the window\n");
  284.         printf("     -s = don't switch to window\n");
  285.         printf("     -c = replace current window\n");
  286.         printf("     -w = wait on program to die\n");
  287.         printf("     -t = don't complain if no access to termcap\n");
  288.         printf("     -f=# = set foreground color\n");
  289.         printf("     -b=# = set background color\n");
  290.         printf("     -o=# = set border color\n");
  291. #ifndef _OSK
  292.         printf("     -v = pop a VDG window\n");
  293. #endif
  294.         ppcWinTypes=apcWinTypeList;
  295.         while (*ppcWinTypes)
  296.         {
  297.             printf("     -%s",*ppcWinTypes+8);
  298.             if (atoi(*ppcWinTypes+8)==iWinType) printf(" [default]");
  299.             printf("\n");
  300.             ppcWinTypes++;
  301.         }
  302.  
  303. printf("\nPOP %s (c) 1991 by StG Computers inc.\n",VER);
  304. printf("Written by Chris Swinefurth (The Bug) and StG\n");
  305. printf("\nThis program is SHAREWARE!  If you like it, please send $10 to:\n");
  306. printf("    StG Computers inc., P.O. Box 24285, Speedway IN 46224\n");
  307.         exit(0);
  308.  
  309.     default:
  310.         if (**argv!='0' && !atoi(*argv))
  311.         {
  312.             printf("Unknown option code: %s\n",*argv);
  313.             exit(1);
  314.         }
  315.  
  316.         iWinType=atoi(*argv);
  317.         ppcWinTypes=apcWinTypeList;
  318.         while (*ppcWinTypes)
  319.         {
  320.             if (atoi(*ppcWinTypes+8)==iWinType) break;
  321.             ppcWinTypes++;
  322.         }
  323.         if (!*ppcWinTypes)
  324.         {
  325.             printf("Unknown window type: %d\n",iWinType);
  326.             exit(1);
  327.         }
  328.     }
  329.  
  330.     ppcWinTypes=apcWinTypeList;
  331.     while (*ppcWinTypes)
  332.     {
  333.         if (atoi(*ppcWinTypes+8)==iWinType) break;
  334.         ppcWinTypes++;
  335.     }
  336.     if (!*ppcWinTypes) exit(-1);
  337.  
  338.     pcTemp=getenv("SHELL");
  339.     if (!pcTemp) pcTemp="SHELL";
  340.     if (!*argv) argv=args(pcTemp);
  341.  
  342.     /* in case stdin is from a script file, reopen as stdout */
  343.     close(0);
  344.     dup(1);
  345.  
  346. #ifndef _OSK
  347.     if (iPopCurWin)  /* insure user is not trying to pop a vdg over current */
  348.     {
  349.         /* get path descriptor */
  350.         _gs_opt(0,&sPathDesc);
  351.  
  352.         if (!(sPathDesc.sg_parity & 0x80) !! iPopVDG)
  353.         {
  354.             printf("Cannot pop current window with/from VDG\n);
  355.             exit(1;
  356.         }
  357.     }
  358. #endif OSK
  359.  
  360.     if (!iPopCurWin)
  361.     {
  362. #ifndef BOGUS
  363.         close(1);
  364. #else
  365.         close(0);
  366. #endif
  367.         errno=1;
  368.         if (open("/w",3)==ERR)
  369.         {
  370.             writeln(2,"pop: cant open /w\n",80);
  371.             exit(errno);
  372.         }
  373.     }
  374.  
  375.     *acWinName='/';
  376. #ifndef BOGUS
  377.     _gs_devn(1,acWinName+1);
  378. #else
  379.     _gs_devn(0,acWinName+1);
  380. #endif
  381.     strhcpy(acWinName+1,acWinName+1);
  382.  
  383. #ifndef OSK
  384.     if (iPopVDG)
  385.     {
  386.         if ((pcWinDesc=modlink(acWinName+1,0x0f,0x01))==ERR)
  387.         {
  388.             writeln(2,"pop: cant link to window descriptor\n",80);
  389.             exit(errno);
  390.         }
  391.         *(pcWinDesc+0x26)=0x01;  /* set VDG type */
  392.  
  393.         dup(2);
  394.         dup(1);
  395.         close(1);
  396.         errno=1;
  397.         if (open(acWinName,3))!=1)
  398.         {
  399.             writeln(2,"pop: cant open VDG window\n",80);
  400.             exit(errno);
  401.         }
  402.  
  403.         /* double check type code */
  404.         _gs_opt(1,&sPathDesc);
  405.         if (sPathDesc.sg_parity!=0x01)
  406.         {
  407.             writeln(2,"pop: window did not open as VDG\n",80);
  408.             exit(1);
  409.         }
  410.         *(pcWinDesc+0x26)=0x80;  /* set back to normal */
  411.         munlink(pcWinDesc);  /* and unlink descriptor module */
  412.  
  413.         close(0);
  414.         close(2);
  415.         dup(1);
  416.         dup(1);
  417.  
  418.         goto exec;
  419.     }
  420. #endif
  421.     
  422.  
  423.     if (iPopCurWin)
  424.     {
  425.         /* end current window */
  426.         write(1,"\x1B\x24",2);
  427.     }
  428.  
  429.     iCol=atoi(*ppcWinTypes+12);
  430.     iRow=atoi(*ppcWinTypes+15);
  431.     if (iFor==-1) iFor=atoi(*ppcWinTypes);
  432.     if (iBack==-1) iBack=atoi(*ppcWinTypes+4);
  433.     if (iBord==-1) iBord=atoi(acWinName+2);
  434.  
  435.     /* define new window */    
  436.     *(acBuf+0)=0x1b;
  437.     *(acBuf+1)=0x20;
  438.     *(acBuf+2)=iWinType;
  439.     *(acBuf+3)=0;  /* start X */
  440.     *(acBuf+4)=0;  /* start Y */
  441.     *(acBuf+5)=iCol;  /* size X */
  442.     *(acBuf+6)=iRow;  /* size Y */
  443.     *(acBuf+7)=iFor;  /* foreground color */
  444.     *(acBuf+8)=iBack;  /* background color */
  445.     *(acBuf+9)=iBord;  /* border color */
  446.  
  447.     *(acBuf+10)=0;    /* nulls to overide bug in DWSET */
  448.     *(acBuf+11)=0;
  449.  
  450. #ifndef BOGUS
  451.     write(1,acBuf,12);
  452.     if (!iNoSelect) write(1,"\x1b\x21",2);
  453.     write(1,"\x0c",1);
  454. #else
  455.     write(0,acBuf,12);
  456.     if (!iNoSelect) write(0,"\x1b\x21",2);
  457.     write(0,"\x0c",1);
  458. #endif
  459.  
  460.     
  461. /* hack because border color isn't getting set in KDwin 
  462. #ifdef OSK
  463.     *(acBuf+0)=0x1b;
  464.     *(acBuf+1)=0x34;
  465.     *(acBuf+2)=iBord;
  466.     write(0,acBuf,3);
  467. #endif
  468. */
  469.  
  470.     /* keep a copy of stderr on path 3 in case... */
  471.     dup(2);
  472.  
  473.     /* reset stdin and stderr to new window */
  474. #ifndef BOGUS
  475.     close(0);
  476.     close(2);
  477.     dup(1);
  478.     dup(1);
  479. #else
  480.     close(1);
  481.     close(2);
  482.     dup(0);
  483.     dup(0);
  484. #endif
  485.  
  486.     if (!iNoLabel)
  487.     {
  488.         write(1,"\x1f\x20",2);
  489.  
  490.         iTemp=0;
  491.         while (iTemp<iCol) acBuf[iTemp++]=' ';
  492.         acBuf[iTemp++]=0;
  493.         iTemp=iCol-strlen(acWinName+1);
  494.         iTemp/=2;
  495.         strncpy(acBuf+iTemp,acWinName+1,strlen(acWinName+1));
  496.         write(1,acBuf,iCol);
  497.  
  498.         write(1,"\x1f\x21",2);
  499.  
  500.         *(acBuf+0)=0x1b;
  501.         *(acBuf+1)=0x25;
  502.         *(acBuf+2)=0;
  503.         *(acBuf+3)=1;
  504.         *(acBuf+4)=iCol;
  505.         *(acBuf+5)=--iRow;
  506.         write(1,acBuf,6);
  507.     }
  508.  
  509.     _gs_opt(0,&sPathDesc);
  510.     sPathDesc.sg_page=iRow-1;
  511.     _ss_opt(0,&sPathDesc);
  512.  
  513. #ifdef OSK
  514.     hFile=open("/dd/sys/termcap",3);
  515.     if (hFile==ERR)
  516.     {
  517.         hFile=open("/h0/sys/termcap",3);
  518.         if (hFile==ERR)
  519.         {
  520.             hFile=open("/d0/sys/termcap",3);
  521.             if (hFile==ERR)
  522.             {
  523.                 if (!iNoTCap)
  524.                 {
  525.                     writeln(2,"pop: cant open termcap file",80);
  526.                     prerr(2,(short)errno);
  527.                 }
  528.                 goto exec;
  529.             }
  530.         }
  531.     }
  532.  
  533.     while ((iTemp=readln(hFile,acBuf,MAXBUF))>0)
  534.     {
  535.         *(acBuf+iTemp)=0;
  536.         if (!strncmp(acBuf,gen1,4)) goto genok;
  537.     }
  538.  
  539.     writeln(hFile,"\n",80);
  540.     writeln(hFile,gen1,80);
  541.     writeln(hFile,gen2,80);
  542.     writeln(hFile,gen3,80);
  543.     writeln(hFile,gen4,80);
  544.     writeln(hFile,"\n",80);
  545.  
  546. genok:
  547.     sprintf(acEnt,"Win%dx%d|KD Window %d X %d:co#%d:li#%d:tc=Win:\n",iCol,iRow,iCol,iRow,iCol,iRow);
  548.  
  549.     while ((iTemp=readln(hFile,acBuf,MAXBUF))>0)
  550.     {
  551.         *(acBuf+iTemp)=0;
  552.         if (!strcmp(acBuf,acEnt)) goto entok;
  553.     }
  554.  
  555.     writeln(hFile,acEnt,80);
  556.  
  557. entok:
  558.     sprintf(acEnt,"Win%dx%d",iCol,iRow);
  559.     iTemp=0;
  560.  
  561.     if (!getenv("TERM") || strcmp(getenv("TERM"),acEnt))
  562.     {
  563.         /* TERM entry needs set/changed */
  564.         if (iPopCurWin)
  565.         {
  566.             sprintf(acEnt,"TERM=Win%dx%d",iCol,iRow);
  567.             printf("Termcap entry is incorrect - please enter:\nsetenv %s\n",acEnt);
  568.         }
  569.         else
  570.         {
  571.             setenv("TERM",acEnt);
  572.         }
  573.     }
  574.     if (!getenv("PORT"))
  575.     {
  576.         setenv("PORT",acWinName);
  577.     }
  578. #endif
  579.  
  580. exec:
  581.     if (iPopCurWin) exit(0);
  582.     
  583.     if (iWait)
  584.     {
  585.         if (os9exec(chain,*argv,argv,environ,0,0)==ERR)
  586.         {
  587.             writeln(3,"pop: cant chain program\n",80);
  588.             exit(errno);
  589.         }
  590.         exit(-1);
  591.     }
  592.  
  593.     if (os9exec(os9fork,*argv,argv,environ,0,0)==ERR)
  594.     {
  595.         writeln(3,"pop: cant fork program\n",80);
  596.         exit(errno);
  597.     }
  598. }
  599.