home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / emacs-18.59-bin.lha / lib / emacs / 18.59 / etc / sh.c < prev    next >
C/C++ Source or Header  |  1993-06-05  |  2KB  |  146 lines

  1. ;/*
  2. SC LINK NOSTKCHK DEF SASC=1 sh.c
  3. QUIT
  4. */
  5. /*
  6.  * original version: David Gay
  7.  *
  8.  * 05/18/93 ch added SAS support, external verbose flag and primitive_parse
  9.  */
  10. #include <exec/types.h>
  11. #include <dos/dostags.h>
  12. #include <stdio.h>
  13.  
  14. #ifdef SASC
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #endif
  18.  
  19. #include <proto/dos.h>
  20.  
  21. /*
  22.   translates:
  23.  
  24.   echo "string1;string2" ; cd xx:c ; copy xx yy
  25.  
  26.       into
  27.  
  28.   echo "string1;string2" \n cd xx:c \n copy xx yy
  29.  
  30.   note:
  31.     this is a really primitive function ;-) , it may be 
  32.     changed if necessary
  33.  
  34. */
  35.  
  36. #define QUOTE    '"'
  37.  
  38. void primitive_parse(char *s)
  39. {
  40.     int c;
  41.  
  42.     while(c = *s++)
  43.     {
  44.     if(c == QUOTE)
  45.     {
  46.         while((c = *s++) && (c != QUOTE))
  47.         ;
  48.         if(!c)
  49.         break;
  50.     }
  51.     else if(c == ';')
  52.         *(s-1) = '\n';
  53.     }
  54. }
  55.  
  56. int execute(char *cmd, int debug)
  57. {
  58.     long rc;
  59.     char *s;
  60.  
  61.     while (*cmd == ' ') cmd++;
  62.     if (strncmp(cmd, "exec", 4) == 0 && (cmd[4] == ' ' || cmd[4] == '\t')) 
  63.     cmd += 4;
  64.     
  65.     s = cmd;
  66.     primitive_parse(s);
  67.  
  68.     if(debug)
  69.     fprintf(stderr,"/etc/sh: preparsed line:\n%s\n", cmd);
  70.  
  71.     if ((rc = SystemTags(cmd, SYS_UserShell, TRUE, TAG_END)) == -1)
  72.     {
  73.     fprintf(stderr, "Failed to execute command %s\n", cmd);
  74.     return 20;
  75.     }
  76.     return rc;
  77. }
  78.  
  79. void main(int argc, char **argv)
  80. {
  81.   int command;
  82.   char *command_string;
  83.   char *program_name = argv[0];
  84.   struct RDArgs *args;
  85.   long opts[1];
  86.   static char options[] = "";
  87.   long debug = 0;
  88.   char *shenv;
  89.  
  90.   /* Throw out AmigaDOS args so that Input() is clean */
  91.   if (args = ReadArgs(options, opts, NULL)) FreeArgs(args);
  92.  
  93.   shenv = getenv("EMACS_SH_DEBUG");
  94.  
  95.   if(shenv)
  96.       if(strstr(shenv, "-v")) /* external verbose flag */
  97.       debug = 1;
  98.   
  99.   command = 0;
  100.   /* Simplistic argument parsing */
  101.   argv++;
  102.   argc--;
  103.   while (argc > 0 && argv[0][0] == '-')
  104.     {
  105.       switch (argv[0][1])
  106.     {
  107.     case 'c':
  108.       if (argc == 1) goto usage;
  109.       command = 1;
  110.       command_string = argv[1];
  111.       argv++;
  112.       argc--;
  113.       break;
  114.     case 'v':
  115.       debug = 1;
  116.       break;
  117.     case 'i':
  118.       /* ignored for now */
  119.       break;
  120.     default: goto usage;
  121.     }
  122.       argc--;
  123.       argv++;
  124.     }
  125.   if (argc != 0) goto usage;
  126.  
  127.   if (command) 
  128.   {
  129.       if(debug)
  130.       fprintf(stderr,"%s: command_string = %s\n", argv[0], command_string);
  131.       exit(execute(command_string, debug));
  132.   }
  133.   else exit(Execute("", Input(), NULL) ? 0 : 1);
  134.  
  135.  usage:
  136.   fprintf(stderr, "%s [-i] [-v] [-c command]\n", program_name);
  137.   exit(1);
  138. }
  139.  
  140.  
  141. /*
  142.  * Local variables:
  143.  * compile-command: "sc link nostkchk def SASC=1 sh.c"
  144.  * end:
  145.  */
  146.