home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / amix / AmigaDOS-Emu.zoo / run.c < prev    next >
C/C++ Source or Header  |  1992-01-14  |  3KB  |  150 lines

  1. #include <stdarg.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #pragma pack(2)
  5. #include <exec/types.h>
  6. #include <exec/nodes.h>
  7. #include <exec/lists.h>
  8. #include <exec/ports.h>
  9. #include <exec/libraries.h>
  10. #include <exec/tasks.h>
  11. #include <libraries/dosextens.h>
  12. #pragma pack()
  13.  
  14. extern int optind;
  15. extern char *optarg;
  16.  
  17. #include "arun.h"
  18.  
  19.  
  20. static unsigned char dosCmdBuf[1024];        /* Yuck */
  21. static unsigned long dosCmdLen;
  22.  
  23. unsigned char *progname;
  24.  
  25.  
  26. static int usage(void)
  27. {
  28.     fprintf(stderr,
  29.         "Usage: %s [-v {verbosity}] {filename} {args}\n",
  30.         progname);
  31.     return -1;
  32. }
  33.  
  34.  
  35. int main(int argc, unsigned char *argv[])
  36. {
  37.     unsigned long *seg;
  38.     int i;
  39.  
  40.     progname = *argv;
  41.  
  42.     while ((i=getopt(argc, (char **)argv, "v:")) != -1)
  43.     switch (i)
  44.     {
  45.     case 'v':
  46.         verbosity = atoi(optarg);
  47.         break;
  48.     default:
  49.         fprintf(stderr, "Invalid option `%c'", i);
  50.         return usage();
  51.     }
  52.  
  53.     argv += optind, argc -= optind;
  54.  
  55.     if (argc < 1)
  56.     return usage();
  57.  
  58.     if (!(seg=LoadSeg(argv[0])))
  59.     panic("can't LoadSeg %s", argv[0]);
  60.  
  61.     amiga_init();
  62.  
  63.     /* Process argument list */
  64.     {
  65.     register unsigned char *p1, *p2;
  66.     struct CommandLineInterface *cli;
  67.  
  68.     cli = (struct CommandLineInterface *)BTOC(CurrentProcess.pr_CLI);
  69.     i = strlen(argv[0]);
  70.     if (i > 255)
  71.         i = 255;
  72.     p1 = Malloc(i+1);
  73.     p1[0] = i;
  74.     strncpy(p1+1, argv[0], i);
  75.     cli->cli_CommandName = CTOB(p1);
  76.  
  77.     p1 = dosCmdBuf;
  78.     for ( i=1 ; i<argc ; ++i )
  79.     {
  80.         p2 = argv[i];
  81.         while ( *p1++ = *p2++ )
  82.         ;
  83.         p1[-1] = ' ';
  84.     }
  85.     if (p1>dosCmdBuf)
  86.         --p1;
  87.     *p1 = '\0';
  88.     dosCmdLen = p1-dosCmdBuf;
  89.     }
  90.  
  91.     verbose("Program entry at 0x%x", seg+1);
  92.     i = runseg(seg, dosCmdBuf, dosCmdLen);
  93.     verbose("Program returned 0x%x", i);
  94.     return i;
  95. }
  96.  
  97.  
  98. void *Malloc(unsigned nbytes)
  99. {
  100.     void *p = malloc(nbytes);
  101.     if (!p)
  102.     panic("out of memory");
  103.     return p;
  104. }
  105.  
  106. void *Calloc(unsigned nelem, unsigned elsize)
  107. {
  108.     void *p = calloc(nelem, elsize);
  109.     if (!p)
  110.     panic("out of memory");
  111.     return p;
  112. }
  113.  
  114. void *Realloc(void *oldptr, unsigned newsize)
  115. {
  116.     register void *p;
  117.  
  118.     if (oldptr)
  119.     p=realloc(oldptr, newsize);
  120.     else
  121.     p=malloc(newsize);
  122.  
  123.     if (!p)
  124.     panic("out of memory");
  125.  
  126.     return p;
  127. }
  128.  
  129.  
  130. int verbosity=1;
  131. void errmsg(char *fmt, ...)
  132. {
  133.     va_list l;
  134.     va_start(l, fmt);
  135.     (void)vfprintf(stderr, fmt, l);
  136.     va_end(l);
  137.     (void)putc('\n', stderr);
  138. }
  139.  
  140. void panic(char *fmt, ...)
  141. {
  142.     va_list l;
  143.     (void)fprintf(stderr, "%s: ", progname);
  144.     va_start(l, fmt);
  145.     (void)vfprintf(stderr, fmt, l);
  146.     va_end(l);
  147.     (void)putc('\n', stderr);
  148.     exit(-1);
  149. }
  150.