English Amiga Board


Go Back   English Amiga Board > Coders > Coders. System

 
 
Thread Tools
Old 12 August 2018, 18:50   #21
meynaf
son of 68k
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 45
Posts: 3,345
Quote:
Originally Posted by Don_Adan View Post
Then check if first longword of running code is moveq #-1,d0, rts, and prevent this file from be called.
Code that returns -1 isn't necessarily at start.
meynaf is offline  
Old 13 August 2018, 02:24   #22
alkis
Registered User

 
Join Date: Dec 2010
Location: Athens/Greece
Age: 47
Posts: 458
Kind of work in progress, but this should give a basic starting point.

Code:
#include <dos/dos.h>
#include <dos/dostags.h>



#ifndef LEAN
#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#else
#include <proto/exec.h>
#include <proto/dos.h>
#endif

int foo(char *commandname) {
  // char buf[256];
  char old_program_name[256];
  int loadDisk = 0;
  struct Segment *cmdseg = NULL;
  struct CommandLineInterface *mycli = Cli();
  ULONG ll, *lp;

  struct Process *myprocess = (struct Process *) FindTask(NULL);
  BPTR myhomedir = myprocess->pr_HomeDir;
  BPTR mymod  = mycli->cli_Module;
  mycli->cli_Module = NULL;
  myprocess->pr_HomeDir = NULL;

  /* Split between actual command and arguments
     Note that AmigaOS (ReadArgs actually) expects args to end with \n
  */
  int argslen = 1;
  char *args = commandname;
  while (*args != ' ' && *args != '\n' && *args)
    ++args;

  char c = *args;
  *args++='\0';
  if (c != '\n' && c != '\0') {
    argslen = strlen(args);
  } else {
    args = "\n";
  }

  //  Printf("cmd: \"%s\" args: \"%s\"\n", commandname, args ? args : "");
  /* Search resident list for the command */
  Forbid();
  if (!(cmdseg = FindSegment(commandname, NULL, FALSE)))
    cmdseg = FindSegment(commandname, NULL, TRUE);
  if (cmdseg) {
    if ((cmdseg->seg_UC < CMD_DISABLED) || (cmdseg->seg_UC == CMD_SYSTEM))
      cmdseg = NULL;
    else if (cmdseg->seg_UC >= 0)
      cmdseg->seg_UC++;
  }
  Permit();

  if (!cmdseg) {
    cmdseg = (struct Segment *)NewLoadSeg(commandname, NULL);
    if (cmdseg) {
      myprocess->pr_HomeDir = Lock("", ACCESS_READ);
      loadDisk = 1;
    }
  }

  /* Traverse the PATH and look into each entry */
  APTR wp = myprocess->pr_WindowPtr;
  myprocess->pr_WindowPtr = (APTR) -1;   //  shutdown disk requesters
  if (!cmdseg) {
    for (ll = mycli->cli_CommandDir; ll; ll = *lp) {
      lp = (ULONG *) (4*ll);
      BPTR cd = CurrentDir(lp[1]);
      cmdseg = (struct Segment *)NewLoadSeg(commandname, NULL);
      (void) CurrentDir(cd);
      if (cmdseg) {
        myprocess->pr_HomeDir = DupLock(lp[1]);
        // Printf("Got it on %s\n", buf);
        loadDisk = 1;
        break;
      }
    }
  }
  myprocess->pr_WindowPtr = wp;   //  restore ability to show disk requesters

  int result = -1;
  if (cmdseg) {
    SetIoErr(0);
    (void)SetSignal(0, SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_E | SIGBREAKF_CTRL_F);

    GetProgramName(old_program_name, sizeof(old_program_name));
    SetProgramName(commandname);
    mycli->cli_Module = loadDisk ? cmdseg : cmdseg->seg_Seg;

    /* Make sure the proper I/O handles are in place. */
    BPTR old_out = SelectOutput(mycli->cli_StandardOutput);
    BPTR in = Open("*", MODE_OLDFILE);    
    BPTR old_in = SelectInput(in);
    if (myhomedir)
      myhomedir = SetProgramDir(myhomedir);

    /* Run the command */
    result = RunCommand(mycli->cli_Module,
                          mycli->cli_DefaultStack * BYTESPERLONG,
                          args,
                          argslen);

    UnGetC(in, 0);
    Flush(in);
    Flush(mycli->cli_StandardOutput);
    Close(in);
    if (myhomedir)
      myhomedir = SetProgramDir(myhomedir);

    /* Make sure the proper I/O handles are in place. */
    SelectInput(old_in);
    SelectOutput(old_out);

    if (loadDisk) {
      UnLoadSeg((struct Segment *)cmdseg);
    } else {
      Forbid();
      if (cmdseg->seg_UC > 0)
        cmdseg->seg_UC--;
      Permit();
    }
    SetProgramName(old_program_name);
  }

  mycli->cli_Module = mymod;
  if (myprocess->pr_HomeDir) {
    UnLock(myprocess->pr_HomeDir);
  }
  myprocess->pr_HomeDir = myhomedir;
  return result;
}

int main(int argc, char **argv) {
  char buf[80];
  BPTR inp = Input();
  while (FGets(inp, buf, sizeof(buf)-1)) {
    ULONG m1 = AvailMem(MEMF_FAST);
    int ret = foo(buf);
    ULONG m2 = AvailMem(MEMF_FAST);
    Printf("memory lost %ld bytes\n", m2-m1);
    Printf("Ret = %ld\n", ret);
  }

  return 0;
}
compiles with m68k-amigaos-gcc -Wall -DLEAN -noixemul -o exe exe.c

main sits on a loop and expects commands to be typed. Exit with ctrl \ (EOF)
alkis is online now  
Old 13 August 2018, 09:59   #23
meynaf
son of 68k
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 45
Posts: 3,345
Yes this is a lot more in the like of what I want

Now i have to understand the details and rewrite it in asm.
Also in need of a way to add a flag that would conditionnally make it asynchronous (without depending on "run" command).
meynaf is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
** Stack Overflow ** trying to run program in WinUAE Audiorulz4u support.WinUAE 10 22 May 2018 23:05
ClassicWB: How to run a program at startup? Foebane support.Apps 20 05 March 2018 10:54
Run AREXX program from PC side? MickGyver support.WinUAE 5 15 March 2017 11:05
Possible to run a program from CLI in background? VoltureX support.Apps 17 14 January 2012 04:16
Cant run AnimatED....anyone every used this great program? mrbob2 support.Apps 2 20 June 2009 14:13

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 01:33.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2018, vBulletin Solutions Inc.
Page generated in 0.06106 seconds with 15 queries