home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / p / perl4036.zip / os2 / os2.c < prev    next >
C/C++ Source or Header  |  1992-10-05  |  6KB  |  271 lines

  1. /* $RCSfile: os2.c,v $$Revision: 4.0.1.2 $$Date: 92/06/08 14:32:30 $
  2.  *
  3.  *    (C) Copyright 1989, 1990 Diomidis Spinellis.
  4.  *
  5.  *    You may distribute under the terms of either the GNU General Public
  6.  *    License or the Artistic License, as specified in the README file.
  7.  *
  8.  * $Log:    os2.c,v $
  9.  * Revision 4.0.1.2  92/06/08  14:32:30  lwall
  10.  * patch20: new OS/2 support
  11.  *
  12.  * Revision 4.0.1.1  91/06/07  11:23:06  lwall
  13.  * patch4: new copyright notice
  14.  *
  15.  * Revision 4.0  91/03/20  01:36:21  lwall
  16.  * 4.0 baseline.
  17.  *
  18.  * Revision 3.0.1.2  90/11/10  01:42:38  lwall
  19.  * patch38: more msdos/os2 upgrades
  20.  *
  21.  * Revision 3.0.1.1  90/10/15  17:49:55  lwall
  22.  * patch29: Initial revision
  23.  *
  24.  * Revision 3.0.1.1  90/03/27  16:10:41  lwall
  25.  * patch16: MSDOS support
  26.  *
  27.  * Revision 1.1  90/03/18  20:32:01  dds
  28.  * Initial revision
  29.  *
  30.  */
  31.  
  32. #define INCL_DOS
  33. #define INCL_NOPM
  34. #include <os2.h>
  35.  
  36. #ifdef __32BIT__
  37. #define DosCwait DosWaitChild
  38. #endif
  39.  
  40. /*
  41.  * Various Unix compatibility functions for OS/2
  42.  */
  43.  
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <errno.h>
  47. #include <process.h>
  48.  
  49. #include "EXTERN.h"
  50. #include "perl.h"
  51.  
  52. char os2_scriptdir[CCHMAXPATH] = "c:/lib/perl";
  53.  
  54. /* dummies */
  55.  
  56. int userinit()
  57. { return -1; }
  58.  
  59. int syscall()
  60. { return -1; }
  61.  
  62. /* priorities */
  63.  
  64. int setpriority(int class, int pid, int val)
  65. {
  66.   int flag = 0;
  67.  
  68.   if ( _osmode == DOS_MODE )
  69.     return 0;
  70.  
  71.   if ( pid < 0 )
  72.   {
  73.     flag++;
  74.     pid = -pid;
  75.   }
  76.  
  77.   return DosSetPrty(flag ? PRTYS_PROCESSTREE : PRTYS_PROCESS, class, val, pid);
  78. }
  79.  
  80. int getpriority(int which /* ignored */, int pid)
  81. {
  82.   int val;
  83. #ifdef __32BIT__
  84.   PTIB ptib;
  85.   PPIB ppib;
  86. #endif
  87.  
  88.   if ( _osmode == DOS_MODE )
  89.     return 0;
  90.  
  91. #ifdef __32BIT__
  92.   if ( DosGetInfoBlocks(&ptib, &ppib) )
  93.     return -1;
  94.   else
  95.     return ptib -> tib_ptib2 -> tib2_ulpri;
  96. #else
  97.   if ( DosGetPrty(PRTYS_PROCESS, &val, pid) )
  98.     return -1;
  99.   else
  100.     return val;
  101. #endif
  102. }
  103.  
  104.  
  105. /* wait for specific pid */
  106.  
  107. int wait4pid(int pid, int *status, int flags)
  108. {
  109.   RESULTCODES res;
  110.   int endpid, rc;
  111.   if ( _osmode == DOS_MODE )
  112.     return -1;
  113.   if ( DosCwait(DCWA_PROCESS, flags ? DCWW_NOWAIT : DCWW_WAIT,
  114.                 &res, &endpid, pid) )
  115.     return -1;
  116.   *status = res.codeResult;
  117.   return endpid;
  118. }
  119.  
  120. /* Just pretend that everyone is a superuser */
  121.  
  122. int setuid()
  123. { return 0; }
  124.  
  125. int setgid()
  126. { return 0; }
  127.  
  128. int getuid(void)
  129. { return 0; }
  130.  
  131. int geteuid(void)
  132. { return 0; }
  133.  
  134. int getgid(void)
  135. { return 0; }
  136.  
  137. int getegid(void)
  138. { return 0; }
  139.  
  140. /*
  141.  * The following code is based on the do_exec and do_aexec functions
  142.  * in file doio.c
  143.  */
  144.  
  145. int
  146. do_aspawn(really,arglast)
  147. STR *really;
  148. int *arglast;
  149. {
  150.     register STR **st = stack->ary_array;
  151.     register int sp = arglast[1];
  152.     register int items = arglast[2] - sp;
  153.     register char **a;
  154.     char **argv;
  155.     char *tmps;
  156.     int status;
  157.  
  158.     if (items) {
  159.     New(1101,argv, items+1, char*);
  160.     a = argv;
  161.     for (st += ++sp; items > 0; items--,st++) {
  162.         if (*st)
  163.         *a++ = str_get(*st);
  164.         else
  165.         *a++ = "";
  166.     }
  167.     *a = Nullch;
  168.     if (really && *(tmps = str_get(really)))
  169.         status = spawnvp(P_WAIT,tmps,argv);
  170.     else
  171.         status = spawnvp(P_WAIT,argv[0],argv);
  172.     Safefree(argv);
  173.     }
  174.     return status;
  175. }
  176.  
  177. int
  178. do_spawn(cmd)
  179. char *cmd;
  180. {
  181.     register char **a;
  182.     register char *s, *t;
  183.     char **argv;
  184.     char flags[10];
  185.     int status;
  186.     char *shell, *cmd2;
  187.  
  188.     /* save an extra exec if possible */
  189.     if ((shell = getenv("COMSPEC")) == 0)
  190.     shell = "cmd.exe";
  191.  
  192.     /* see if there are shell metacharacters in it */
  193.     if (strchr(cmd, '>') || strchr(cmd, '<') || strchr(cmd, '|')
  194.         || strchr(cmd, '&') || strchr(cmd, '^'))
  195.         return spawnlp(P_WAIT,shell,shell,"/C",cmd,(char*)0);
  196.  
  197.     New(1102,argv, strlen(cmd) / 2 + 2, char*);
  198.     New(1103,cmd2, strlen(cmd) + 2, char);
  199.     s = cmd;
  200.     t = cmd2;
  201.     a = argv;
  202.  
  203.     while (*s) {
  204.     while (*s && isspace(*s)) s++;
  205.     if (*s)
  206.         *(a++) = t;
  207.     while (*s && !isspace(*s))
  208.       if (*s == '\\') {
  209.         s++;
  210.         *t++ = *s++;
  211.       }
  212.       else if (*s == '"') {
  213.         s++;
  214.         while (*s && *s != '"') *t++ = *s++;
  215.         s++;
  216.       }
  217.       else
  218.         *t++ = *s++;
  219.     *t++ = '\0';
  220.     }
  221.  
  222.     *a = Nullch;
  223.  
  224.     if (argv[0])
  225.     if ((status = spawnvp(P_WAIT,argv[0],argv)) == -1)
  226.         status = spawnlp(P_WAIT,shell,shell,"/C",cmd,(char*)0);
  227.  
  228.     Safefree(cmd2);
  229.     Safefree(argv);
  230.  
  231.     return status;
  232. }
  233.  
  234. usage(char *myname)
  235. {
  236.   printf("\nUsage: %s [-acdnpPsSuvw] [-0[octal]] [-l[octal]] [-Dnumber]"
  237.      "\n            [-i[extension]] [-Idirectory] [-x[directory]]"
  238.          "\n            [-e \"command\"] [filename] [arguments]\n", myname);
  239.  
  240.   printf("\n  -a  autosplit mode with -n or -p"
  241.          "\n  -c  syntaxcheck only"
  242.          "\n  -d  run scripts under debugger"
  243.          "\n  -n  assume 'while (<>) { ...script... }' loop arround your script"
  244.          "\n  -p  assume loop like -n but print line also like sed"
  245.          "\n  -P  run script through C preprocessor befor compilation"
  246.          "\n  -s  enable some switch parsing for switches after script name"
  247.          "\n  -S  look for the script using PATH environment variable"
  248.          "\n  -u  dump core after compiling the script");
  249.   printf("\n  -v  print version number and patchlevel of perl"
  250.          "\n  -w  turn warnings on for compilation of your script\n"
  251.          "\n  -0[octal]       specify record separator (0, if no argument)"
  252.          "\n  -l[octal]       automatic line-ending processing ($/, if no argument)"
  253.          "\n  -Dnumber        set debugging flags (argument is a bit mask)"
  254.          "\n  -i[extension]   edit <> files in place (make backup if extension supplied)"
  255.          "\n  -Idirectory     specify include directory in conjunction with -P"
  256.          "\n  -e command      one line of script, multiple -e options are allowed"
  257.          "\n                  [filename] can be ommitted, when -e is used"
  258.          "\n  -x[directory]   strip off text before #!perl line and perhaps cd to directory\n");
  259. }
  260.  
  261. void os2init(int *argc, char ***argv)
  262. {
  263.   char *ptr;
  264.  
  265.   if ( (ptr = getenv("PERLDIR")) != NULL )
  266.     strcpy(os2_scriptdir, ptr);
  267.   
  268.   _response(argc, argv);
  269.   _wildcard(argc, argv);
  270. }
  271.