home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / programm / language / bcpl4ami.lzh / tripos / myrun.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-07  |  8.1 KB  |  241 lines

  1. /* Myrun.c - a C language replacement for RUN
  2. Compile and link with Manx 3.4:
  3.   cc myrun
  4.   ln myrun.o bcpllib.o -lc
  5. By Bill Kinnersley - Dec 18, 1987
  6. Mail:   Physics Dept.
  7.         Montana State University
  8.         Bozeman, MT 59717
  9. BITNET: iphwk@mtsunix1
  10. INTERNET: iphwk%mtsunix1.bitnet@cunyvm.cuny.edu
  11. UUCP: ...psuvax1!mtsunix1.bitnet!iphwk
  12. */
  13. #include <stdio.h>
  14. #include <libraries/dosextens.h>
  15. #include <functions.h>
  16. #include <exec/memory.h>
  17. #include "BCPL.h"
  18. #define DOSTRUE -1L
  19.  
  20. struct pathlist {
  21.         BPTR next;
  22.         BPTR lock;
  23. };
  24.  
  25. long cli_init();
  26. struct pathlist *copypath();
  27. long *gv;
  28.  
  29. main() {
  30.         struct RootNode *root;
  31.         struct DosLibrary *doslib;
  32.         struct Task *mytask;
  33.         struct Process *myproc, *newproc;
  34.         struct CommandLineInterface *cli;
  35.         struct DosPacket *pkt;
  36.         struct FileLock *lock;
  37.         struct FileHandle *cis, *cos, *myfh;
  38.         char *oldbuf, *mybuf;
  39.         long clitask, *tskarr, taskno, end, size, *buf, i,
  40.                 stsz, sz, *mysegarray, *newsegarray, res, res2, pri;
  41.         BPTR cd=0, procname=0;
  42.  
  43.         BCPLInit();
  44.         myproc = (struct Process *)FindTask(0L);
  45.         res2 = myproc->pr_Result2;
  46. /* Here's where RUN finds the command line when called by EXECUTE
  47.    Unfortunately, the Manx startup code overwrites Result2 */
  48.         res2 = 0;
  49.         cis = (struct FileHandle *)BADDR(myproc->pr_CIS);
  50.         cos = (struct FileHandle *)BADDR(myproc->pr_COS);
  51.         newproc = 0;
  52.         newsegarray = (long *)AllocMem(20L, MEMF_PUBLIC);*newsegarray++ = 20;
  53.         doslib = (struct DosLibrary *)OpenLibrary("dos.library", 0L);
  54.         root = (struct RootNode *)doslib->dl_Root;
  55.         tskarr = (long *)BADDR(root->rn_TaskArray);
  56.         taskno = 0;
  57.         mytask = &myproc->pr_Task;
  58.         pri = mytask->tc_Node.ln_Pri;
  59.         mysegarray = (long *)BADDR(myproc->pr_SegList);
  60.         gv = (long *)myproc->pr_GlobVec;
  61.         cli = (struct CommandLineInterface *)BADDR(myproc->pr_CLI);
  62.         if (res2) {     /* called by EXECUTE */
  63.                 oldbuf = (char *)BADDR(res2);
  64.                 end = *oldbuf++;
  65.         }
  66.         else {
  67.                 end = cis->fh_End;
  68.                 oldbuf = (char *)BADDR(cis->fh_Buf);
  69.         }
  70.         size = 4L*(end/4L + 13L);
  71. printf("end=%ld oldbuf=%lx bufsize=%ld\n", end, oldbuf, size);
  72.         buf = (long *)AllocMem(size, MEMF_CLEAR); *buf++ = size;
  73.         myfh = (struct FileHandle *)buf;
  74.         mybuf = (char *)((long)myfh + 44L);
  75.         for (i=0; i<end; i++) mybuf[i] = oldbuf[i];
  76.         mybuf[end] = '\n';
  77. for (i=0; i<=end; i++) printf("%c",mybuf[i]);
  78.         newsegarray[0] = 4;
  79.         newsegarray[1] = mysegarray[1];
  80.         newsegarray[2] = mysegarray[2];
  81.         newsegarray[3] = 0;
  82.         newsegarray[4] = (long)root->rn_ConsoleSegment;
  83. printf("copied segarray %ld %lx %lx %lx %lx\n",newsegarray[0],newsegarray[1],
  84. newsegarray[2],newsegarray[3],newsegarray[4]);
  85.         procname = MakeBSTR("Background CLI");
  86.         stsz = 3200;
  87.         Forbid();
  88.         sz = tskarr[0];
  89.         for (taskno=1; taskno<=sz; taskno++) if (!tskarr[taskno]) break;
  90.         if (!(clitask = BCPL(CREATEPROCB, bptr(newsegarray), stsz>>2, pri,
  91.                 procname, bptr(gv)))) {
  92.                 if (taskno) tskarr[taskno] = 0;
  93.                 Permit();
  94.                 if (!res2) printf("RUN failed\n");
  95.                 FreeMem(newsegarray, 20L);
  96.                 FreeMem(myfh, size);
  97.                 FreeBSTR(procname);
  98.                 BCPLQuit();
  99.                 exit(20);
  100.         }
  101. printf("clitask=%lx\n",clitask);
  102.         tskarr[taskno] = (long)clitask;
  103.         newproc =(struct Process *)((long)clitask-(long)sizeof(struct Task));
  104.         newproc->pr_TaskNum = taskno;
  105.         Permit();
  106.         myfh->fh_Buf = (long)bptr(mybuf);
  107.         myfh->fh_End = end + 1L;
  108.         cd = cli ? DupLock(myproc->pr_CurrentDir) : 0;
  109. printf("ready to dospacket\n");
  110.         if (!BCPL(SENDPKT, DOSTRUE, clitask, cli_init, 0L, 0L, bptr(cli),
  111.                 bptr(cis), bptr(cos), bptr(myfh), cd, res2))
  112.                 printf("[CLI %ld]\n", taskno);
  113. printf("dospacketed\n");
  114.         FreeBSTR(procname);
  115.         BCPLQuit();
  116.         exit(0);
  117. }
  118.  
  119. struct clistartup {
  120.         long unused[5];
  121.         BPTR cli, cis, cos, fh, cd;
  122.         long res2;
  123. };
  124.  
  125. long retval;
  126. long cli_init(arg) BPTR arg; {
  127.         struct clistartup *pkt; /* this MUST be the FIRST local */
  128.         struct Process *mytask;
  129.         struct CommandLineInterface *oldcli, *mycli;
  130.         struct FileHandle *fh, *input;
  131.         BPTR output;
  132.         long max, i, putpkt, flag, flagset, *mysegarr, inisint, outisint;
  133.         char *prompt, *newprompt, *curdir, *newcurdir;
  134.  
  135. #asm
  136.         movem.l a1/a3,-(a7)
  137.         asl.l   #2,d1
  138.         move.l  d1,-4(a5)       ;this initializes pkt */
  139. #endasm
  140.         geta4();
  141.         oldcli = (struct CommandLineInterface *)BADDR(pkt->cli);
  142.         fh = (struct FileHandle *)BADDR(pkt->fh);
  143.         mytask = (struct Process *)FindTask(0L);
  144.         mycli = (struct CommandLineInterface *)BADDR(mytask->pr_CLI);
  145.         flag = !(pkt->res2);
  146.         output = 0;
  147.         inisint = FALSE;
  148.         flagset = -4;
  149.         mycli->cli_CurrentInput = mycli->cli_StandardInput = pkt->fh;
  150.         if (!flag) {
  151.                 input = (struct FileHandle *)BADDR(pkt->cis);
  152.                 if (input) {
  153.                         mycli->cli_StandardInput = pkt->cis;
  154.                         mytask->pr_ConsoleTask = (APTR)input->fh_Type;
  155.                         inisint = (long)input->fh_Port;
  156.                         flagset |= 2;
  157.                 }
  158.                 output = pkt->cos;
  159.         }
  160.         if (!output) {
  161.                 flagset |= 1;
  162.                 output = Open("*", MODE_NEWFILE);
  163.         }
  164.         outisint = (long)((struct FileHandle *)BADDR(output))->fh_Port;
  165.         mycli->cli_Background = (outisint && inisint) ? 0 : DOSTRUE;
  166.         CurrentDir(pkt->cd);
  167.         init(mycli, output, oldcli);
  168.         mysegarr = (long *)BADDR(mytask->pr_SegList);
  169.         mysegarr[3] = 0;
  170.         retval = flag ? gv[42] /* putpkt */ : flagset;
  171. printf("FINISHED retval=%lx\n", retval);
  172.         mytask->pr_Result2 = bptr(pkt);
  173. #asm
  174.         dseg
  175.         public  _a0
  176.         cseg
  177.         lea     _retval,a1
  178.         move.l  (a1),d1
  179.         lea     _a0,a0
  180.         move.l  0(a0),a2
  181.         move.l  8(a0),a6
  182.         suba.l  a0,a0
  183.         movem.l (a7)+,a1/a3
  184.         unlk    a5
  185.         jmp     (a6)
  186. #endasm
  187. }
  188.  
  189. init(cli, output, oldcli)
  190. struct CommandLineInterface *cli, *oldcli; BPTR output; {
  191.         long stsz, max, i;
  192.         BPTR path, b;
  193.         char *prompt, *curdir, *newprompt, *newcurdir, *bstr;
  194.  
  195.         b = oldcli ? oldcli->cli_Prompt : MakeBSTR("%N>");
  196.         prompt = (char *)BADDR(b);
  197.         newprompt = (char *)BADDR(cli->cli_Prompt);
  198.         max = prompt[0];
  199.         for (i=0; i<=max; i++) newprompt[i] = prompt[i];
  200.  
  201.         b = oldcli ? oldcli->cli_SetName : MakeBSTR("SYS:");
  202.         curdir = (char *)BADDR(b);
  203.         newcurdir = (char *)BADDR(cli->cli_SetName);
  204.         max = curdir[0];
  205.         for (i=0; i<=max; i++) newcurdir[i] = curdir[i];
  206.  
  207.         bstr = (char *)BADDR(cli->cli_CommandFile);
  208.         bstr[0] = 0;
  209.  
  210.         stsz = oldcli ? oldcli->cli_DefaultStack : 1000;
  211.         cli->cli_DefaultStack = stsz;
  212.  
  213.         path = oldcli ? bptr(copypath(oldcli->cli_CommandDir)) : 0;
  214.         cli->cli_CommandDir = path;
  215.  
  216.         cli->cli_StandardOutput = cli->cli_CurrentOutput = output;
  217.         cli->cli_FailLevel = 10;
  218. }
  219.  
  220. struct pathlist *copypath(arg) BPTR arg; {
  221. /* The PATH is stored in cli_CommandDir as a linked list of Locks */
  222. /* Each newly created CLI must inherit the PATH of its creator */
  223.         struct pathlist *oldlist, *newlist, *link, *lastlink;
  224.         long *mem;
  225.  
  226.         newlist = NULL;
  227.         lastlink = (struct pathlist *)&newlist;
  228.         oldlist = (struct pathlist *)BADDR(arg);
  229.         while (oldlist) {
  230.                 if (!(mem = (long *)AllocMem(12L, 1L))) break;
  231.                 mem[0] = 12L;
  232.                 link = (struct pathlist *)&mem[1];
  233.                 link->next = NULL;
  234.                 link->lock = DupLock(oldlist->lock);
  235.                 lastlink->next = bptr(link);
  236.                 lastlink = link;
  237.                 oldlist = (struct pathlist *)BADDR(oldlist->next);
  238.         }
  239.         return (struct pathlist *)(BADDR(newlist));
  240. }
  241.