home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 318 / utilsrc / cruft.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  6.4 KB  |  348 lines

  1.  
  2. /* misc cruft; move to lib later */
  3.  
  4. #include <ctype.h>
  5. #include <stdio.h>
  6. #include <osbind.h>
  7.  
  8. long _stksize = 8192;
  9.  
  10. extern char ** environ;
  11.  
  12. long * drf_addr;    /* mondo disgusto... */
  13.  
  14. int gulam();
  15.  
  16. puts(str)
  17. char * str;
  18. {
  19.   fputs(str, stdout);
  20.   if (str[strlen(str) - 1] != '\n')
  21.     fputs("\n", stdout);
  22. }
  23.  
  24. int ftruncate(fhandle, len)
  25. int fhandle, len;
  26. {
  27.   /* no op for now... */
  28.   return(0);
  29. }
  30.  
  31. char * sys_siglist[] = 
  32.     {
  33.     "sig 0",
  34.     "sig 1",
  35.     "sig 2",
  36.     "sig 3"
  37.     };
  38.  
  39. int chdir(dir)
  40. char * dir;
  41. {
  42.   return(0);
  43. }
  44.  
  45. /* only used by touch... */
  46. int fstat(foo, bar)
  47. int foo, bar;
  48. {
  49.   return(0);
  50. }
  51.  
  52. char * xlate_cmd(cmd)
  53. /* (declare (values xlated_command)) */
  54. char * cmd;
  55. {
  56.   char * result;
  57.  
  58. /*  printf("xlate('%s')->", cmd); */
  59.   result = (char * )getenv(cmd);
  60. /*
  61.   if (result)
  62.     printf("'%s'\n", result);
  63.     else
  64.     printf("NIL\n");
  65. */
  66.   return(result);
  67. }
  68.  
  69. gspawnv(cmd, args)
  70. char * cmd;
  71. char ** args;
  72. {
  73.   int result;
  74.   char * xcmd;
  75.   char * line;
  76.   char ** foo;
  77.  
  78.   line = (char * )alloca(258);
  79.   line[0] = '\0';
  80. /*
  81.   fprintf(stderr, "\n  exec '%s'", cmd);
  82.   for (foo = args ; *foo ; foo++)
  83.     fprintf(stderr, " '%s'", *foo);
  84.   fprintf(stderr, "\n");
  85. */
  86.   if (xcmd = xlate_cmd(cmd))
  87.     {
  88.     line[1] = '\0';
  89.     if (args && *args)
  90.         {
  91.         args++;
  92.         while (*args)
  93.             {
  94.             strcat(line + 1, *args);
  95.             strcat(line + 1, " ");
  96.             args++;
  97.             }
  98.         }
  99.     line[0] = strlen(line+1);
  100. /*    return(spawnv(line, args)); */
  101. /*    printf("argstring '%s' len %d\n", line+1, line[0]); */
  102.     result = Pexec(PE_LOADGO, xcmd, line, 0L);
  103. /*    printf(" --> %d\n", result); */
  104.     return(result);
  105.     }
  106.  
  107.   strcat(line, cmd);
  108.   if (args) args++;
  109.   for ( ; *args ; args++)
  110.     {
  111. /*    fputs(" '", stderr); */
  112.     strcat(line, " ");
  113. /*    fputs(*args, stderr); */
  114.     strcat(line, *args);
  115. /*    fputs("'", stderr); */
  116.     }
  117. /*  fprintf(stderr, "\n"); */
  118.   result = gulam(line);
  119.   return(result);
  120. }
  121.  
  122. char char_upcase(c)
  123. char c;
  124. {
  125.   return(islower(c) ? toupper(c) : c);
  126. }
  127.  
  128. /* like strcmp, but case-insensitive */
  129.  
  130. int ci_strcmp(s1, s2)
  131. char    * s1;
  132. char    * s2;
  133. {
  134.   char ch1, ch2;
  135.  
  136.   if (!s1 && s2) return(-1);
  137.   if (!s2 && s1) return(1);
  138.   if (!s1 && !s2) return(0);
  139.  
  140. /*  printf("  ci-strcmp('%s', '%s')->", s1, s2); */
  141.  
  142.   while (char_upcase(*s1) == char_upcase(*s2++))
  143.     if (!*s1++)
  144.         {
  145. /*        printf("0\n");        */
  146.         return 0;
  147.         }
  148. /*  printf("%d\n", *s1 - *(s2 - 1)); */
  149.   return *s1 - *--s2;
  150. }
  151.  
  152. string_downcase(str)
  153. char * str;
  154. {
  155.   for ( ; *str ; str++)
  156.     if (isupper(*str))
  157.         *str = tolower(*str);
  158. }
  159.  
  160. /* for being smart about dirs */
  161. dir_unalias(name, truename)
  162. char * name, * truename;
  163. {
  164.   int len = strlen(name);
  165.  
  166.   if ((len == 1) && (name[0] == '.'))
  167.     {
  168.     getwd(truename);
  169.     string_downcase(truename);
  170.     }
  171. /* more later... */
  172.     else
  173.     strcpy(truename, name);
  174. }
  175.  
  176.  
  177.  
  178. /*
  179.  * system - execute a command and return status
  180.  */
  181. int system(cmd)
  182. register char *cmd;
  183. {
  184.     char command[128], tail[130];
  185.     register char *p, *save;
  186.     register int n;
  187.  
  188.     if (gulamp())            /* jrd */
  189.     return(gulam(cmd));        /* jrd */
  190.  
  191.     /* Break up command into command and command tail */
  192.     for(p = save = command; !isspace(*cmd); *p++ = *cmd++)    /* copy */;
  193.     *p = '\0';
  194.  
  195.     while(isspace(*cmd)) cmd++;                /* skip blanks */
  196.     if((n = strlen(cmd)) > 128)
  197.     {
  198.         fprintf(stderr,"Command '%s' too long\n",save);
  199.         return -1;
  200.     }
  201.  
  202.     tail[0] = (char) n;
  203.     strcpy(&tail[1],cmd);
  204.  
  205. /*    return (int)Pexec(0,command, tail, (char *)NULL); */
  206.     n = (int)Pexec(0,command, tail, (char *)NULL);
  207.     printf("%d\n\n", n);
  208.     return n;
  209. }
  210.  
  211.  
  212.  
  213. /* excerpt from gulam doc...
  214. *Gulam* can be used in four ways.  Two of these four use `TOS'
  215. variable named `_shell_p' (at `0x04f6L').  This (supposedly)
  216. contains a pointer to a routine that takes a string as an argument and
  217. executes it as a shell command.  *Gulam* has slightly extented this.
  218. `*_shell_p', as set by *Gulam*, points the bottom of a (jump)
  219. table, which currently is:
  220.  
  221.              .long   0x86420135        / our magic number
  222.              jmp     getlineviaue_
  223.      togu_:  jmp     callgulam_
  224.  
  225. Thus, `*((long *) 0x04f6L)' == address of `togu_'.  Before
  226. invoking either routine, it always is a good idea to check if the magic
  227. number is present.  Let `mp = *((long *) 0x04f6L) - 12L'.  Then,
  228. `*mp' better be `0x86420135'.
  229. */
  230.  
  231. /* the magic number really seems to be 0x00420135 -- jrd */
  232.  
  233. /* to be exec'ed in super mode */
  234. long deref_s()
  235. {
  236.   return(*drf_addr);
  237. }
  238.  
  239. long deref(addr)
  240. long *addr;
  241. {
  242.   drf_addr = (long * )addr;
  243. /*  return(Supexec(deref_s)); */
  244. /*  asm("moveml #0x3FFC,sp@-"); */
  245.   asm("movel #_deref_s,sp@-");
  246.   asm("movew #38,sp@-");
  247.   asm("trap #14");
  248.   asm("addl #6,sp");
  249. /*  asm("moveml sp@+,#0x3FFC"); */
  250. }
  251.  
  252. int gulamp()
  253. {
  254.   long vec;
  255.  
  256.   vec = 0x4F6;        /* Current CLI vector */
  257.   vec = deref(vec);
  258.  
  259. /* for floppettes...
  260.   foo = 0x19ADC;
  261.  
  262.   foo = 0x1C26E;
  263. */
  264.  
  265. /*   printf("gulamp:     *0x04F6=%lx\n", vec);        */
  266.   vec -= 10;
  267.   vec = deref(vec);
  268. /*  printf("gulamp:  *0x04F6-10=%lx\n", vec);        */
  269.  
  270.   return (vec == 0x00420135);
  271. }
  272.  
  273. extern long _start;
  274.  
  275. dump_region(addr)
  276. char * addr;
  277. {
  278.   int i, j;
  279.   char * b;
  280.  
  281.   printf("%X: %X\n", addr, &_start);
  282.   for (b = addr - 32, i = j = 0; i < 64 ; b++, i++, j++)
  283.     {
  284.     printf(" %02X", (*b & 0xFF));
  285.     if (j == 15)
  286.         {
  287.         printf("\n");
  288.         j = -1;
  289.         }
  290.     }
  291.   printf("\n");
  292. }
  293.  
  294. int gulam(str)
  295. char *str;
  296. {
  297.   long saveregs[14];
  298.   short (*togu)();
  299.   short result;
  300.   char * temp;
  301.   long save_ssp;
  302.  
  303.   temp = (char * )alloca(258);
  304. /*  dump_region(str); */
  305. /*  printf("gulam(%s)\n", str); */
  306.   strcpy(temp, str);
  307. /*   togu = deref(0x4F6L); */
  308.  
  309.   save_ssp = Super(0L);
  310.   togu = *(long *)0x4F6L;
  311.   Super(save_ssp);
  312. /*
  313.   printf("gulam->%X, arg=%X, temp=%X\n", togu, str, temp);
  314. */
  315.   result = gulam_internal(togu, temp);
  316.  
  317. /*  dump_region(str); */
  318.   return((int )result);
  319. };
  320.  
  321. long save_a6, save_sp;
  322.  
  323. int gulam_internal(fun, str)
  324. short (*fun)();
  325. char * str;
  326. {
  327.   int result;
  328.  
  329.   asm("moveml #0x3FFC,sp@-");
  330.  
  331. /*  result = ((*togu)(temp, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); */
  332.   asm("movel a6,_save_a6");
  333.   asm("movel sp,_save_sp");
  334.   asm("movel a6@(12),sp@-");
  335.   asm("movel a6@(8),a0");
  336. /*  asm(".word 0x4afc"); */
  337.    asm("jsr a0@"); 
  338.   asm("movel _save_a6,a6");
  339.   asm("movel _save_sp,sp");
  340.   asm("extw d0");        /* cuz it returns word */
  341.   asm("movel d0,a6@(-4)");    /* set result */
  342.   asm("moveml sp@+,#0x3FFC");
  343.  
  344.   return(result);
  345. }
  346.  
  347.  
  348.