home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / forth / pfe-0.000 / pfe-0 / pfe-0.9.13 / src / shell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-17  |  5.4 KB  |  262 lines

  1. /*
  2.  * This file is part of the portable Forth environment written in ANSI C.
  3.  * Copyright (C) 1995  Dirk Uwe Zoller
  4.  *
  5.  * This library is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU Library General Public
  7.  * License as published by the Free Software Foundation; either
  8.  * version 2 of the License, or (at your option) any later version.
  9.  *
  10.  * This library is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13.  * See the GNU Library General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU Library General Public
  16.  * License along with this library; if not, write to the Free
  17.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  * This file is version 0.9.13 of 17-July-95
  20.  * Check for the latest version of this package via anonymous ftp at
  21.  *    roxi.rz.fht-mannheim.de:/pub/languages/forth/pfe-VERSION.tar.gz
  22.  * or    sunsite.unc.edu:/pub/languages/forth/pfe-VERSION.tar.gz
  23.  * or    ftp.cygnus.com:/pub/forth/pfe-VERSION.tar.gz
  24.  *
  25.  * Please direct any comments via internet to
  26.  *    duz@roxi.rz.fht-mannheim.de.
  27.  * Thank You.
  28.  */
  29. /*
  30.  * shell.c ---     os commands for pfe
  31.  * (duz 07May94)
  32.  */
  33.  
  34. #include "forth.h"
  35. #include "support.h"
  36. #include "compiler.h"
  37.  
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <limits.h>
  42. #include <fcntl.h>
  43.  
  44. #include "nonansi.h"
  45. #include "missing.h"
  46.  
  47.  
  48. #ifdef HAVE_PID
  49. Code (getpid)    { *--sp = (Cell)getpid (); }
  50. #endif
  51.  
  52. #ifdef HAVE_UID
  53. Code (getuid)    { *--sp = (Cell)getuid (); }
  54. Code (geteuid)    { *--sp = (Cell)geteuid (); }
  55. #endif
  56.  
  57. #ifdef HAVE_GID
  58. Code (getgid)    { *--sp = (Cell)getgid (); }
  59. #endif
  60.  
  61. #ifdef HAVE_UMASK
  62. Code (umask)    { *sp = (Cell)umask (*sp); }
  63. #endif
  64.  
  65. Code (home)    { strpush (getenv ("HOME")); }
  66. Code (user)    { strpush (getenv ("USER")); }
  67.  
  68. Code (cwd)    { strpush (getcwd (pocket (), PATH_LENGTH)); }
  69.  
  70. Code (pwd)
  71. {
  72.   outs (getcwd (pocket (), PATH_LENGTH));
  73.   space_();
  74. }
  75.  
  76. static void
  77. do_one (char *p, int (*syscall) (const char *))
  78. {
  79.   char name[PATH_LENGTH];
  80.  
  81.   store_filename (p + 1, (Byte)*p, name, sizeof name);
  82.   if (syscall (name))
  83.     file_errorz (name);
  84. }
  85.  
  86. #define SHWORD1(X)                \
  87. Code (APPEND (X,_execution))            \
  88. {                        \
  89.   do_one ((char *)ip, X);            \
  90.   SKIP_STRING;                    \
  91. }                        \
  92. Code (X)                    \
  93. {                        \
  94.   if (STATE)                    \
  95.     {                        \
  96.       compile1 ();                \
  97.       alloc_word (' ');                \
  98.     }                        \
  99.   else                        \
  100.     do_one (word (' '), X);            \
  101. }                        \
  102. COMPILES (X, APPEND (X,_execution),        \
  103.       SKIPS_STRING, DEFAULT_STYLE)
  104.  
  105. static void
  106. do_two (char *p1, char *p2, int (*syscall) (const char *, const char *))
  107. {
  108.   char nm1[PATH_LENGTH], nm2[PATH_LENGTH];
  109.  
  110.   store_filename (p1 + 1, *(Byte *)p1, nm1, sizeof nm1);
  111.   store_filename (p2 + 1, *(Byte *)p2, nm2, sizeof nm2);
  112.   if (syscall (nm1, nm2))
  113.     file_errorz (nm1);
  114. }
  115.  
  116. #define SHWORD2(X)                \
  117. Code (APPEND (X,_execution))            \
  118. {                        \
  119.   char *p = (char *)ip;                \
  120.   SKIP_STRING;                    \
  121.   do_two (p, (char *)ip, X);            \
  122.   SKIP_STRING;                    \
  123. }                        \
  124. Code (X)                    \
  125. {                        \
  126.   if (STATE)                    \
  127.     {                        \
  128.       compile1 ();                \
  129.       alloc_word (' ');                \
  130.       alloc_word (' ');                \
  131.     }                        \
  132.   else                        \
  133.     {                        \
  134.       char *p = pocket ();            \
  135.       strcpy (p, word (' '));            \
  136.       do_two (p, word (' '), X);        \
  137.     }                        \
  138. }                        \
  139. COMPILES (X, APPEND(X,_execution),        \
  140.       SKIPS_2STRINGS, DEFAULT_STYLE)
  141.  
  142. #ifdef S_IRUSR
  143. #define RWALL    (S_IRUSR | S_IWUSR | \
  144.          S_IRGRP | S_IWGRP | \
  145.          S_IROTH | S_IWOTH)
  146. #define RWXALL    (RWALL | S_IXUSR | S_IXGRP | S_IXOTH)
  147. #else
  148. #define RWALL    0666
  149. #define RWXALL    0777
  150. #endif
  151.  
  152. static int
  153. md (const char *s)
  154. {
  155. #if defined DOS_FILENAMES && !defined EMX
  156.                 /* an assumption: */
  157.   return mkdir (s);        /*   DOS like systems need no permissions. */
  158. #else                /*   Holds for Turbo-C and Watcom-C. */
  159.   return mkdir (s, RWXALL);
  160. #endif
  161. }
  162.  
  163. static int
  164. touch (const char *s)
  165. {
  166.   int result;
  167.  
  168. #ifdef HAVE_ACCESS
  169.   if (access (s, F_OK) == 0)
  170.     return utime (s, NULL);
  171. #endif
  172.   result = open (s, O_WRONLY | O_CREAT, RWALL);
  173.   if (result < 0)
  174.     return result;
  175.   close (result);
  176.   return 0;
  177. }
  178.  
  179. static int
  180. cp (const char *src, const char *dst)
  181. {
  182.   return copy (src, dst, LONG_MAX) == -1;
  183. }
  184.  
  185. static int
  186. ls (const char *p)
  187. {
  188.   cr_();
  189.   return systemf (LSCMD" %s", p);
  190. }
  191.  
  192. static int
  193. ll (const char *p)
  194. {
  195.   cr_();
  196.   return systemf (LLCMD" %s", p);
  197. }
  198.  
  199. /*
  200.  * For the macro SHWORD1 to work, it is required that remove is not a
  201.  * macro. If this system lacks remove() and this is normally fixed by
  202.  * #define remove unlink, then define remove as function here.
  203.  */
  204. #ifdef remove
  205. #undef remove
  206. int remove (const char *name) { return unlink (name); }
  207. #endif
  208.  
  209. #ifdef AIX1
  210. extern int link();
  211. extern int remove();
  212. extern int chdir();
  213. extern int rmdir();
  214. #endif
  215.  
  216. SHWORD1(remove);
  217. SHWORD1(touch);
  218. SHWORD1(chdir);
  219. SHWORD1(rmdir);
  220. SHWORD1(md);
  221. SHWORD1(ls);
  222. SHWORD1(ll);
  223. SHWORD2(move);
  224. SHWORD2(cp);
  225. #ifdef HAVE_LINK
  226. SHWORD2(link);
  227. #endif
  228.  
  229. LISTWORDS (shell) =
  230. {
  231. #ifdef HAVE_PID
  232.   CO ("$$",        getpid),
  233. #endif
  234. #ifdef HAVE_UID
  235.   CO ("$UID",        getuid),
  236.   CO ("$EUID",        geteuid),
  237. #endif
  238. #ifdef HAVE_GID
  239.   CO ("$GID",        getgid),
  240. #endif
  241. #ifdef HAVE_UMASK
  242.   CO ("UMASK",        umask),
  243. #endif
  244.   CO ("$HOME",        home),
  245.   CO ("$USER",        user),
  246.   CO ("$CWD",        cwd),
  247.   CO ("PWD",        pwd),
  248.   CS ("RM",        remove),
  249.   CS ("TOUCH",        touch),
  250.   CS ("CD",        chdir),
  251.   CS ("RMDIR",        rmdir),
  252.   CS ("MKDIR",        md),
  253.   CS ("MV",        move),
  254.   CS ("CP",        cp),
  255. #ifdef HAVE_LINK
  256.   CS ("LN",        link),
  257. #endif
  258.   CS ("LL",        ll),
  259.   CS ("LS",        ls),
  260. };
  261. COUNTWORDS (shell, "Shell words");
  262.