home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / jsage / zsus / progpack / cshellv1.lbr / CSHELL.CZ / CSHELL.C
Encoding:
C/C++ Source or Header  |  1992-05-13  |  4.4 KB  |  251 lines

  1. /*
  2.  
  3.     CSHELL --- add pipes and batch files to ZCPR3 and C80.
  4.  
  5.     Edward Schram
  6.     1609 Valle Vista
  7.     Vallejo, Ca, 94589
  8.         (707)557-1424
  9. */
  10.  
  11. #define ZCPR3
  12. #include<stdio.h>
  13.  
  14. char cmd[191];
  15. int cmdptr;
  16. main()
  17. {
  18.     static char *prompt="$> ";
  19.     char shname[13],c;
  20.     int du,i;
  21.     struct fcb *efcb;
  22.  
  23. /* Initialize the Z-System pointers */
  24.  
  25.     t_init();
  26.  
  27. /* See if possible to run a Z-Shell  */
  28.  
  29.     if(!envptr)
  30.     error("ZCPR3: required to run pipe shell.");
  31.  
  32.     if(!c_get_sh())
  33.     error("ZCPR3 SHELL STACK: required to run.");
  34.  
  35. /* Okay to run the program see if set up needs to be done.  */
  36.  
  37.     if(qshell() != 1)
  38.     {
  39.  
  40. /* Yes setup the shell entry          */
  41.  
  42. /* Set the fcb pointer to the external fcb if there else default */
  43.  
  44.     if((efcb = gefcb()) == 0)
  45.         efcb = 0x80;
  46.  
  47. /* Plug the default name in the default buffer in case it's used */
  48.  
  49.     makfcb("CSHELL.COM",0x80);
  50.  
  51. /* Try to find the thing in the path   */
  52.  
  53.     if((du = pfnd(efcb,1)) == -1)
  54.         error("Can't find program.");
  55.  
  56. /* Stick in the disk and user    */
  57.  
  58.     shname[0] = (du >> 8) + 'A';
  59.     du &= 0xff;
  60.     shname[1] = du / 10 + '0';
  61.     shname[2] = du % 10 + '0';
  62.  
  63. /* and a colon must be there */
  64.  
  65.     shname[3] = ':';
  66.  
  67. /* and now for the name */
  68.  
  69.     for(i=0;i<8;++i)
  70.     {
  71.         if((c = (efcb->filename[i] & 0x7f)) == ' ')
  72.         break;
  73.         shname[4+i] = c;
  74.     }
  75.  
  76. /* Remember to end with 0 */
  77.  
  78.     shname[4+i] = '\0';
  79.  
  80. /* and push the entry   */
  81.  
  82.     if(shpsh(shname))
  83.         error("SHELL PUSH: Stack full or length error.");
  84.     
  85.     puts("\nShell Stack entry pushed.\n\n");
  86.     }
  87.  
  88. /* The entire main() is initialization these routines simply
  89.     get the command parse it and send it to ZCPR3 */
  90.  
  91.     cmdptr=0;
  92. /* print DU:DIRNAME>  */
  93.     cpm_prompt();
  94. /* Read the cmd from the console or $$$.SUB or <FILE */
  95.     get_cmd(cmd,190);
  96. /* Parse (filter it) */
  97.     parse();
  98. /* and send it */
  99.     putcl(cmd);
  100. }
  101.  
  102. /* This took me a long time fiqure it out and let me know
  103.    how I finally managed to do it  */
  104.  
  105. parse()
  106. {
  107.     int i,x;
  108.     char cmmds[20][10],args[20][128];
  109.     char flag[20];
  110.  
  111.     i=0;
  112.     
  113.     while(getwrd(cmd,&cmdptr,cmmds[i]))
  114.     {
  115.     while((cmd[cmdptr] == ' ') || (cmd[cmdptr]== '\t'))
  116.         cmdptr++;
  117.  
  118.     x= 0;
  119.     while((cmd[cmdptr] != ';') && (cmd[cmdptr] != '|') && (cmd[cmdptr]) && (cmd[cmdptr] != '\n'))
  120.         args[i][x++] = cmd[cmdptr++];
  121.  
  122.  
  123.         flag[i] = cmd[cmdptr++];
  124.         args[i++][x] = '\0';
  125.     }
  126.     cmmds[i][0] = 0;
  127.  
  128.     for(i=0;i<190;i++) 
  129.     cmd[i]='\0';
  130.  
  131.     i=0;
  132.  
  133.     while(cmmds[i][0])
  134.     {
  135.     strcat(cmd,cmmds[i]);
  136.     strcat(cmd," ");
  137.     if(args[i][0])
  138.     {
  139.         strcat(cmd,args[i]);
  140.         strcat(cmd," ");
  141.     }
  142.     if (i != 0)
  143.         if(flag[i-1] == '|')
  144.         {
  145.         strcat(cmd,"<");
  146.         strcat(cmd,cmmds[i]);
  147.         strcat(cmd," ");
  148.         }
  149.     if(flag[i] == '|' && cmmds[i+1][0])
  150.         {
  151.         strcat(cmd,">");
  152.         strcat(cmd,cmmds[i+1]);
  153.         strcat(cmd," ");
  154.         }
  155.     strcat(cmd,";");
  156.     if(i != 0)
  157.        if(flag[i-1] == '|')
  158.         {
  159.             strcat(cmd,"era ");
  160.             strcat(cmd,cmmds[i]);
  161.             strcat(cmd,";");
  162.         }
  163.     i++;
  164.     }
  165. }
  166.  
  167. /* I admit it the following code is stolen right out of the
  168.    source code of ZCPR3..  I didn't want to reinvent the wheel */
  169.  
  170.  
  171. get_cmd(line,len)
  172. char *line;
  173. int len;
  174. {
  175.     int disk,user,du,x;
  176.     char buff[128];
  177.     struct fcb submit;
  178.  
  179.     setmem(line,len,0);
  180.  
  181.     disk=getdsk();
  182.     user=getuser();
  183.  
  184.     makfcb("$$$.SUB",&submit);
  185.     if((du = pfnd(&submit,1)) == -1)
  186.         return(gets(line,len));
  187.  
  188.     setdsk(du>>8);
  189.     setusr(du & 0x1f);
  190.     
  191.     if(!bopen(&submit))
  192.         return(err_sub(disk,user,line,len));
  193.  
  194.     submit.rec = submit.rec_count-1;
  195.     if(!rran(&submit,buff))
  196.         return(err_sub(disk,user,line,len));
  197.  
  198. /* Why are these 2 lines needed?????????
  199.    Submit adds garbage to the front of the command in the record.  */
  200.  
  201.     x=0;
  202.     while(buff[x++] < ' '+1);
  203.  
  204.     strcpy(line,&buff[x-1]);
  205.     submit.s2 = '\0';
  206.     submit.rec_count--;
  207.  
  208.     if(!bclose(&submit))
  209.         return(err_sub(disk,user,line,len));
  210.  
  211.     puts("\b:BATCH_MODE>");
  212.     setdsk(disk);
  213.     setusr(user);
  214.     puts(cmd);
  215. }
  216.  
  217. err_sub(disk,user,line,len)
  218. int disk,user;
  219. char *line;
  220. int len;
  221. {
  222.     unlink("$$$.SUB");
  223.     setusr(user);
  224.     setdsk(disk);
  225.     return(gets(line,len));
  226. }
  227.  
  228. rran()
  229. {
  230. #asm
  231.     pop    h
  232.     pop    b    ; b= buffer area
  233.     pop    d    ; d= fcb
  234.     push    d
  235.     push    b
  236.     push    h
  237.     push    d
  238.     mov    e,c
  239.     mov    d,b
  240.     mvi    c,26
  241.     call    5
  242.     pop    d
  243.     mvi    c,20
  244.     call    5
  245.     ora    a
  246.     lxi    h,1
  247.     rz
  248.     dcx    h
  249. #endasm
  250. }
  251.