home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / emacs-18.59-src.tgz / emacs-18.59-src.tar / fsf / emacs18 / etc / amiga-env.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  11KB  |  440 lines

  1. /* env.c - manipulate environment and execute a program
  2.    in that environment
  3.    Mly 861126
  4.  
  5.    Copyright (C) 1986 Free Software Foundation, Inc.
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 1, or (at your option)
  10.     any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.  In other words, you are welcome to use, share and improve this program.
  22.  You are forbidden to forbid anyone else to use, share and improve
  23.  what you give them.   Help stamp out software-hoarding!  */
  24.  
  25. /* 
  26.  
  27.    If first argument is "-", then a new environment is constructed
  28.    from scratch; otherwise the environment is inherited from the parent
  29.    process, except as modified by other options.
  30.    
  31.    So, "env - foo" will invoke the "foo" program in a null environment,
  32.    whereas "env foo" would invoke "foo" in the same environment as that
  33.    passed to "env" itself.
  34.  
  35.    Subsequent arguments are interpreted as follows:
  36.    
  37.    * "variable=value" (ie an arg containing a "=" character)
  38.      means to set the specified environment variable to that value.
  39.      `value' may be of zero length ("variable=").  Note that setting
  40.      a variable to a zero-length value is different from unsetting it.
  41.  
  42.    * "-u variable" or "-unset variable"
  43.      means to unset that variable
  44.      If that variable isn't set, does nothing.
  45.  
  46.    * "-s variable value" or "-set variable value"
  47.      same as "variable=value"
  48.  
  49.    * "-" or "--"
  50.      are used to indicate that the following argument is the program
  51.      to invoke.  This is only necessary when the program's name
  52.      begins with "-" or contains a "="
  53.  
  54.    * anything else
  55.      The first remaining argument specifies a program to invoke
  56.      (it is searched for according to the specification of the PATH
  57.      environment variable) and any arguments following that are
  58.      passed as arguments to that program
  59.  
  60.      If no program-name is specified following the environment
  61.      specifications the the resulting environment is printed
  62.      (The is like specifying a program-name of "printenv")
  63.  
  64.    Examples:
  65.      If the environment passed to "env" is
  66.      { USER=rms EDITOR=emacs PATH=.:/gnubin:/hacks }
  67.  
  68.      * "env DISPLAY=gnu:0 nemacs"
  69.         calls "nemacs" in the envionment
  70.     { EDITOR=emacs USER=rms DISPLAY=gnu }
  71.  
  72.      * "env - USER=foo /hacks/hack bar baz"
  73.        will call the "hack" program on arguments "bar" and "baz"
  74.        in an environment in which the only variable is "USER"
  75.        Note that the "-" option will clear out the PATH variable,
  76.        so one should be careful to specify in which directory
  77.        to find the program to call
  78.        
  79.      * "env -u EDITOR USER=foo PATH=/energy -- e=mc2 bar baz"
  80.        The program "/energy/e=mc2" is called with environment
  81.        { USER=foo PATH=/energy }
  82.  
  83. */
  84.  
  85. #include <exec/types.h>
  86. #include <dos/dostags.h>
  87. #include <dos/var.h>
  88. #include <proto/dos.h>
  89. #include <proto/exec.h>
  90.  
  91. #ifdef EMACS
  92. #define NO_SHORTNAMES
  93. #include "../src/config.h"
  94. #endif /* EMACS */
  95.  
  96. #include <stdio.h>
  97. #include <errno.h>
  98. #include <setjmp.h>
  99.  
  100. extern int execvp ();
  101. extern char *index ();
  102.  
  103. char *xmalloc (), *xrealloc ();
  104. char *concat ();
  105.  
  106. char *progname;
  107. void setenv ();
  108. void fatal ();
  109.  
  110. #define index strchr
  111.  
  112. struct MsgPort *end_port;
  113. struct {
  114.   struct Message msg;
  115.   int rc;
  116. } end_msg;
  117. int gargc;
  118. char **gargv;
  119. jmp_buf unixexit_buf;
  120.  
  121. void __saveds unix_start(void)
  122. {
  123.   int rc;
  124.  
  125.   if (!(rc = setjmp(unixexit_buf)))
  126.     {
  127.       unixmain(gargc, gargv);
  128.       rc = 1;
  129.     }
  130.   end_msg.rc = rc - 1;
  131.   end_msg.msg.mn_Length = sizeof(end_msg);
  132.   end_msg.msg.mn_Node.ln_Type = NT_MESSAGE;
  133.   PutMsg(end_port, &end_msg);
  134. }
  135.  
  136. void unixexit(int rc)
  137. {
  138.   longjmp(unixexit_buf, rc + 1);
  139. }
  140.  
  141. main(int argc, char **argv)
  142. {
  143.   int rc = 1;
  144.   long stacksize;
  145.   struct Process *us = (struct Process *)FindTask(0);
  146.  
  147.   if (us->pr_CLI) stacksize = ((struct CommandLineInterface *)BADDR(us->pr_CLI))->cli_DefaultStack << 2;
  148.   else stacksize = us->pr_StackSize;
  149.  
  150.   gargc = argc;
  151.   gargv = argv;
  152.   
  153.   end_port = CreateMsgPort();
  154.  
  155.   if (end_port && CreateNewProcTags(NP_Entry, unix_start,
  156.                     NP_Input, Input(), NP_CloseInput, 0UL,
  157.                     NP_Output, Output(), NP_CloseOutput, 0UL,
  158.                     NP_StackSize, stacksize,
  159.                     NP_Cli, TRUE, TAG_END))
  160.     {
  161.       while (!GetMsg(end_port)) WaitPort(end_port);
  162.       rc = end_msg.rc;
  163.     }
  164.   if (end_port) DeleteMsgPort(end_port);
  165.   Delay(1);
  166.   exit(rc);
  167. }
  168.  
  169. #define exit unixexit
  170.  
  171. unixmain (argc, argv)
  172.      register int argc;
  173.      register char **argv;
  174. {
  175.   register char *tem;
  176.  
  177.   progname = argv[0];
  178.   argc--;
  179.   argv++;
  180.  
  181.   /* "-" flag means to not inherit parent's environment */
  182.   /* This is ignored on the amiga */
  183.   if (argc && !strcmp (*argv, "-"))
  184.     {
  185.       argc--;
  186.       argv++;
  187.     }
  188.  
  189.   while (argc > 0)
  190.     {
  191.       tem = index (*argv, '=');
  192.       if (tem)
  193.     /* If arg contains a "=" it specifies to set a variable */
  194.     {
  195.       *tem = '\000';
  196.       setenv (*argv, tem + 1);
  197.       argc--; argv++;
  198.       continue;
  199.     }
  200.       
  201.       if (**argv != '-')
  202.     /* Remaining args are program name and args to pass it */
  203.     break;
  204.  
  205.       if (argc < 2)
  206.     fatal ("No argument following \"%s\" switch", *argv);
  207.        if (!strcmp (*argv, "-u") ||
  208.            !strcmp (*argv, "-unset"))
  209.     /* Unset a variable */
  210.     {
  211.       argc--; argv++;
  212.       setenv (*argv, 0);
  213.       argc--; argv++;
  214.     }
  215.       else if (!strcmp (*argv, "-s") ||
  216.            !strcmp (*argv, "-set"))
  217.     /* Set a variable */
  218.     {
  219.       argc--; argv++;
  220.       tem = *argv;
  221.       if (argc < 2)
  222.         fatal ("No value specified for variable \"%s\"",
  223.            tem);
  224.       argc--; argv++;
  225.       setenv (tem, *argv);
  226.       argc--; argv++;
  227.     }
  228.       else if (!strcmp (*argv, "-") || !strcmp (*argv, "--"))
  229.     {
  230.       argc--; argv++;
  231.       break;
  232.     }
  233.       else
  234.     {
  235.       fatal ("unknown switch \"%s\"", *argv);
  236.     }
  237.     }
  238.  
  239.   /* If no program specified print the environment and exit */
  240.   if (argc <= 0)
  241.     {
  242.       printenv();
  243.       exit (0);
  244.     }
  245.   else
  246.     {
  247.       extern int errno, sys_nerr;
  248.       extern char *sys_errlist[];
  249.  
  250.       (void) execvp (*argv, argv);
  251.  
  252.       fprintf (stderr, "%s: Cannot execute \"%s\"",
  253.            progname, *argv);
  254.       if (errno < sys_nerr)
  255.     fprintf (stderr, ": %s\n" , sys_errlist[errno]);
  256.       else
  257.     putc ('\n', stderr);
  258.       exit (errno != 0 ? errno : 1);
  259.     }
  260. }
  261.  
  262. int execvp(program, argv)
  263. char *program, **argv;
  264. {
  265.   int index, comsize;
  266.   char *combuf, *bp;
  267.   long err, rc;
  268.   
  269.   combuf = xmalloc(256);
  270.   comsize = 256;
  271.  
  272.   bp = combuf;
  273.   for (index = 0; argv[index] != 0; index++)
  274.     {
  275.       char *s = argv[index];
  276.       int len;
  277.  
  278.       len = 3;
  279.       while (*s) len += 1 + 2 * (*s++ == '"');
  280.       if (bp + len + 1 >= combuf + comsize)
  281.     {
  282.       char *newbuf;
  283.       int new_comsize;
  284.  
  285.       new_comsize = 2 * comsize + len;
  286.       newbuf = xmalloc(new_comsize);
  287.       memcpy(newbuf, combuf, comsize);
  288.  
  289.       bp = newbuf + (bp - combuf);
  290.       combuf = newbuf;
  291.       comsize = new_comsize;
  292.     }
  293.       *bp++ = ' ';
  294.       *bp++ = '"';
  295.       s = argv[index];
  296.       while (*s)
  297.     {
  298.       if (*s == '"' || *s == '*') *bp++ = '*';
  299.       *bp++ = *s++;
  300.     }
  301.       *bp++ = '"';
  302.     }
  303.   *bp = '\0';
  304.   rc = SystemTags(combuf,
  305.           SYS_UserShell, 1UL,
  306.           TAG_END);
  307.   err = IoErr();
  308.   free(combuf);
  309.   if (rc != -1) exit(rc);
  310.   
  311.   errno = convert_oserr(err);
  312.   return -1;
  313. }
  314.  
  315. int convert_oserr(int ioerr)
  316. {
  317.   extern int _OSERR;
  318.  
  319.   _OSERR = ioerr;
  320.   switch (ioerr)
  321.     {
  322.     case 0: return 0;
  323.     case ERROR_NO_FREE_STORE: return ENOMEM;
  324.     case ERROR_TASK_TABLE_FULL: return EAGAIN;
  325.     case ERROR_BAD_TEMPLATE: case ERROR_REQUIRED_ARG_MISSING:
  326.     case ERROR_KEY_NEEDS_ARG: case ERROR_TOO_MANY_ARGS:
  327.     case ERROR_UNMATCHED_QUOTES: case ERROR_LINE_TOO_LONG: return EINVAL;
  328.     case ERROR_OBJECT_IN_USE: return EBUSY;
  329.     case ERROR_OBJECT_EXISTS: return EEXIST;
  330.     case ERROR_DIR_NOT_FOUND: return ENOENT;
  331.     case ERROR_OBJECT_NOT_FOUND: return ENOENT;
  332.     case ERROR_BAD_STREAM_NAME: return EINVAL;
  333.     case ERROR_OBJECT_TOO_LARGE: return E2BIG;
  334.     case ERROR_ACTION_NOT_KNOWN: return EINVAL;
  335.     case ERROR_INVALID_COMPONENT_NAME: return EINVAL;
  336.     case ERROR_INVALID_LOCK: return EINVAL;
  337.     case ERROR_OBJECT_WRONG_TYPE: return EINVAL;
  338.     case ERROR_DISK_WRITE_PROTECTED: return EACCES;
  339.     case ERROR_SEEK_ERROR: return EIO;
  340.     case ERROR_DISK_FULL: return ENOSPC;
  341.     case ERROR_DELETE_PROTECTED: return EACCES;
  342.     case ERROR_WRITE_PROTECTED: return EACCES;
  343.     case ERROR_READ_PROTECTED: return EACCES;
  344.     case ERROR_RENAME_ACROSS_DEVICES: return EXDEV;
  345.     default: return EOSERR;
  346.     }
  347. }
  348.  
  349. printenv(void)
  350. /* Effect: Prints a UNIX style environment from the AmigaOS environment.
  351. */
  352. {
  353.   struct LocalVar *scan_env;
  354.   struct Process *us = (struct Process *)FindTask(0);
  355.  
  356.   for (scan_env = (struct LocalVar *)us->pr_LocalVars.mlh_Head;
  357.        scan_env->lv_Node.ln_Succ;
  358.        scan_env = (struct LocalVar *)scan_env->lv_Node.ln_Succ)
  359.     if (scan_env->lv_Node.ln_Type == LV_VAR &&
  360.     !(scan_env->lv_Flags & (GVF_GLOBAL_ONLY | GVF_BINARY_VAR)))
  361.       {
  362.     /* We only handle local text variables */
  363.     printf("%s=", scan_env->lv_Node.ln_Name);
  364.     fwrite(scan_env->lv_Value, 1, scan_env->lv_Len, stdout);
  365.     putchar('\n');
  366.       }
  367. }
  368.  
  369. void
  370. setenv (var, val)
  371.   register char *var, *val;
  372. {
  373.   if (val) SetVar(var, val, -1, LV_VAR | GVF_LOCAL_ONLY);
  374.   else DeleteVar(var, LV_VAR | GVF_LOCAL_ONLY);
  375. }
  376.  
  377. void
  378. fatal (msg, arg1, arg2)
  379.      char *msg, *arg1, *arg2;
  380. {
  381.   fprintf (stderr, "%s: ", progname);
  382.   fprintf (stderr, msg, arg1, arg2);
  383.   putc ('\n', stderr);
  384.   exit (1);
  385. }
  386.  
  387.  
  388. extern char *malloc (), *realloc ();
  389.  
  390. void
  391. memory_fatal ()
  392. {
  393.   fatal ("Out of memory");
  394. }
  395.  
  396. char *
  397. xmalloc (size)
  398.      int size;
  399. {
  400.   register char *value;
  401.   value = (char *) malloc (size);
  402.   if (!value) memory_fatal ();
  403.   return (value);
  404. }
  405.  
  406. char *
  407. xrealloc (ptr, size)
  408.      char *ptr;
  409.      int size;
  410. {
  411.   register char *value;
  412.   value = (char *) realloc (ptr, size);
  413.   if (!value) memory_fatal ();
  414.   return (value);
  415. }
  416.  
  417. /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3.  */
  418.  
  419. char *
  420. concat (s1, s2, s3)
  421.      char *s1, *s2, *s3;
  422. {
  423.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  424.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  425.  
  426.   strcpy (result, s1);
  427.   strcpy (result + len1, s2);
  428.   strcpy (result + len1 + len2, s3);
  429.   *(result + len1 + len2 + len3) = 0;
  430.  
  431.   return result;
  432. }
  433.  
  434.  
  435. /*
  436.  * Local variables:
  437.  * compile-command: "lc -L -v amiga-env.c"
  438.  * end:
  439.  */
  440.