home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v92.tgz / v92.tar / v92 / src / iconc / clocal.c < prev    next >
C/C++ Source or Header  |  1996-03-22  |  7KB  |  386 lines

  1. /*
  2.  *  clocal.c -- compiler functions needed for different systems.
  3.  */
  4. #include "::h:gsupport.h"
  5.  
  6. /*
  7.  * The following code is operating-system dependent [@tlocal.01].
  8.  *  Routines needed by different systems.
  9.  */
  10.  
  11. #if PORT
  12. /* place to put anything system specific */
  13. Deliberate Syntax Error
  14. #endif                    /* PORT */
  15.  
  16. #if ATARI_ST
  17.  
  18. unsigned long _STACK = 10240;   /*   MNEED ALSO, PLEASE */
  19.  
  20. char *getenv();
  21.  
  22. char iconpath[80];        /* the path returned by findpath() */
  23. int pathset = 0;        /* not yet setup */
  24.  
  25. char *findpath();
  26. char *getpathenv();
  27.  
  28. /*
  29.  Pass a command to a shell.
  30.  Find what/where is the shell by the environment variable SHELL
  31.  */
  32. int system(cmd)
  33. char *cmd;
  34. {
  35.     char *shell, tail[128];
  36.     int rc;
  37.     
  38.     if ((shell = getenv("SHELL")) == NULL) {
  39.         fprintf(stderr,"SHELL not found.\n");
  40.         return -1;
  41.     }
  42.  
  43.     strcpy(tail + 1, cmd);
  44.     *tail = strlen(tail) - 1;
  45.  
  46.     rc = Pexec(shell, tail, NULL);
  47.     return (rc >= 0) ? rc : -1;
  48. }
  49.  
  50. /*
  51.  Spawn a process,
  52.  with: variable arg list, and path found by environment.
  53.  */
  54. int Forkvp(cmd, argv)
  55. char *cmd, **argv;
  56. {
  57.     char file[80];
  58.     int n;
  59.     extern char *patharg;
  60.  
  61.     if (filefound(NULL, cmd) == 0) {
  62.         if (!pathset) {
  63.             if (patharg) {
  64.                 strcpy(iconpath, patharg);
  65.                 if ((n = strlen(iconpath)) > 0
  66.                         && iconpath[n-1] != '\\')
  67.                     strcat(iconpath, "\\");
  68.             } else {
  69.                 if (findpath(cmd) == NULL)
  70.                     return -1;
  71.             }
  72.             pathset = 1;
  73.         }
  74.         strcpy(file, iconpath);
  75.         strcat(file, cmd);
  76.     } else
  77.         strcpy(file, cmd);
  78.  
  79.     return Forkv(file, argv);    
  80. }
  81.  
  82. /*
  83.  Spawn a process with variable arg list.
  84.  */
  85. int Forkv(cmd, argv)
  86. char *cmd, **argv;
  87. {
  88.     char tail[128];
  89.     int i;
  90.  
  91.     *tail = tail[1] = '\0';
  92.     for (i = 1; argv[i]; i++) {
  93.         strcat(tail," ");
  94.         strcat(tail,argv[i]);
  95.     }
  96.     *tail = strlen(tail) - 1;
  97.  
  98.     return Pexec(cmd, tail, NULL);    
  99. }
  100.  
  101. /*
  102.  findpath() assumes that the name passed to it is complete.
  103.  (i.e. with the proper extension...)
  104.  */  
  105. char *findpath(prog)
  106. char *prog;
  107. {
  108.     char *ppath, *fpath;
  109.     int error, drvnum, drvmap, drvmask;
  110. /*    
  111.  First : check if ICON environment variable exist...
  112.  (because it may be a lot faster than check the whole PATH...)
  113.  */
  114.     if ((ppath = getenv("ICON")) != NULL) {
  115.         strcpy(iconpath, ppath);
  116.         if ((error = strlen(iconpath)) > 0) {
  117.             if (iconpath[error - 1] != '\\')
  118.                 strcat(iconpath, "\\");
  119.             if (filefound(iconpath, prog) != 0)
  120.                 return iconpath;
  121.         }
  122.     }
  123. /*    
  124.  Second : check with PATH...
  125.  */
  126.  
  127.     if ((ppath = getenv("PATH")) != NULL) {
  128.         error = 0;
  129.         while ((fpath = getpathenv(ppath, error)) != NULL) {
  130.             if (filefound(fpath, prog) != 0) {
  131.                 strcpy(iconpath, fpath);
  132.                 return iconpath;
  133.             }
  134.             error++;
  135.         }
  136.     }
  137.  
  138. /*
  139.  Not found here or with the ICON or PATH environment variable.
  140.  Check all the mounted disk drives root directory, in alphabetical order.
  141.  */
  142.     strcpy(iconpath, "a:\\");
  143.     drvmap = bios(10);    /* Drvmap() */ 
  144.     for (drvnum = 0,    drvmask = 1;
  145.          drvnum < 16;
  146.          drvnum++,        drvmask <<= 1, iconpath[0]++)
  147.         if ((drvmask & drvmap) && (filefound(iconpath, prog) != 0))
  148.                 return iconpath;
  149.  
  150. /*
  151.  Not found...
  152.  */
  153.     return NULL;
  154. }
  155.  
  156. int Pexec(file, tail, env) char *file, *tail, *env;
  157. {
  158.     int rc;
  159.  
  160.     if ((rc = gemdos(0x4b,0,file,tail, env)) != 0) {
  161.         switch (rc) {
  162.             case -33:
  163.                 fprintf(stderr,
  164.                 "Fork failed: file <%s> not found.\n", file);
  165.                 break;
  166.             case -39:
  167.                 fprintf(stderr,
  168.                 "Fork failed: <%s> Not enough memory.\n", file);
  169.                 break;
  170.             case -66:
  171.                 fprintf(stderr,
  172.                 "Fork failed: <%s> Bad load format.\n", file);
  173.                 break;
  174.             default:
  175.         }
  176.     }
  177.     return rc;
  178. }
  179.  
  180. /*
  181.  getpathenv() find the nth path entry in the env var PATH.
  182.  append a backslash to it, if necessary; strcat ready.
  183.  */
  184.  
  185. char *getpathenv(path, n) char *path; int n;
  186. {
  187.     char *Path2;
  188.     char *sep = " ,;";        /* path separator in PATH */
  189.     static char local[128];
  190.     int i, j;
  191.  
  192.     for (i = 0; i < n; i++) {
  193.         Path2 = strpbrk(path, sep);
  194.         if (Path2 == NULL)
  195.             break;
  196.         path = Path2 + 1;    /* Past the sep */
  197.         if ((j = strspn(path, sep)) > 0)
  198.             path += j;
  199.         
  200.     }
  201.     if (n == i) {
  202.         strcpy(local, path);
  203.         Path2 = strpbrk(local, sep);
  204.         if (Path2 != NULL) {
  205.             *Path2++ = '\\';
  206.             *Path2 = '\0';
  207.         } else
  208.             strcat(local, "\\");
  209.         if (strlen(local) == 1)
  210.             local[0] = '\0';
  211.         return &local[0];
  212.     }
  213.     return NULL;
  214. }
  215.     
  216. /*
  217.  test if a file exists.
  218.  */
  219. int filefound(path, file) char *path, *file;
  220. {
  221.     char filepath[80];
  222.  
  223.     if ((path != NULL) && (path[0] != '\0')) {
  224.         strcpy(filepath, path);
  225.         strcat(filepath, file);
  226.     } else
  227.         strcpy(filepath, file);
  228.  
  229.     if (gemdos(0x43, filepath, 0, 0) >= 0)
  230.         return 1;
  231.     return 0;
  232. }
  233.  
  234. /*
  235.  * This function returns a pointer to a static character string containing
  236.  * the environment value assigned to the variable passed. (Fonorow/Nowlin)
  237.  */
  238.  
  239. #define  BASEPAGE   _basepage
  240. #define  SETLEN     256
  241.  
  242. char    *getenv(var)
  243. char    *var;
  244. {
  245.     extern
  246.     long    *BASEPAGE;
  247.  
  248.     register
  249.     int    varlen;
  250.  
  251.     register
  252.     char    *lenv;
  253.  
  254.     static
  255.     char    val[SETLEN];
  256.  
  257. /*    calculate the variable length */
  258.     varlen = strlen(var);
  259.  
  260. /*    check all the variables in the environment */
  261.     for (lenv = (char *) BASEPAGE[11]; *lenv; lenv += strlen(lenv) + 1) {
  262.  
  263. /*        if this variable matches the variable name passed exactly */
  264.         if (*(lenv+varlen) == '=' &&
  265.             strncmp(var,lenv,varlen) == 0) {
  266.  
  267. /*            save the value and return a pointer to it */
  268.             strcpy(val,lenv+varlen+1);
  269.  
  270.             return val;
  271.         }
  272.     }
  273.  
  274. /*    the variable wasn't found so return null */
  275.     return (char *) 0;
  276. }
  277. #endif                    /* ATARI_ST */
  278.  
  279. #if MACINTOSH
  280. #if MPW
  281. /*
  282.  * These are stubs for several routines defined in the runtine
  283.  *  library that aren't necessary in MPW tools.  These routines are 
  284.  *  referenced by the Standard C Library I/O functions, but are never called.
  285.  *  Because they are referenced, the linker can't remove them.  The stubs in
  286.  *  this file provide dummy routines which are never called, but reduce the
  287.  *  size of the tool.
  288.  */
  289.  
  290.  
  291. /* Console Driver
  292.  
  293.    These drivers provide I/O to the screen (or a specified port) in
  294.    applications.  They aren't necessary in tools.  
  295. */
  296.  
  297. _coFAccess() {}
  298. _coClose() {}
  299. _coRead() {}
  300. _coWrite() {}
  301. _coIoctl() {}
  302. _coExit() {}
  303.  
  304.  
  305. /* File System Driver
  306.  
  307.    Tools use the file system drivers linked with the MPW Shell.
  308. */
  309.  
  310. _fsFAccess() {}
  311. _fsClose() {}
  312. _fsRead() {}
  313. _fsWrite() {}
  314. _fsIoctl() {}
  315. _fsExit() {}
  316.  
  317.  
  318. /* System Driver
  319.  
  320.    Tools use the system drivers linked with the MPW Shell.
  321. */
  322.  
  323. _syFAccess() {}
  324. _syClose() {}
  325. _syRead() {}
  326. _syWrite() {}
  327. _syIoctl() {}
  328. _syExit() {}
  329.  
  330.  
  331. /* Floating Point Conversion Routines
  332.  
  333.    These routines, called by printf, are only necessary if floating point
  334.    formatting is used.
  335. */
  336.  
  337. ecvt() {}
  338. fcvt() {}
  339. #endif                    /* MPW */
  340. #endif                    /* MACINTOSH */
  341.  
  342. #if MSDOS
  343.  
  344. #if MICROSOFT
  345.  
  346. pointer xmalloc(n)
  347.    long n;
  348.    {
  349.    return calloc((size_t)n,sizeof(char));
  350.    }
  351. #endif                    /* MICROSOFT */
  352.  
  353. #if MICROSOFT
  354. int _stack = (8 * 1024);
  355. #endif                    /* MICROSOFT */
  356.  
  357. #if TURBO
  358. extern unsigned _stklen = 8192;
  359. #endif                    /* TURBO */
  360.  
  361. #if ZTC_386
  362. #ifndef DOS386
  363. int _stack = (8 * 1024);
  364. #endif                    /* DOS386 */
  365. #endif                    /* ZTC_386 */
  366.  
  367. #endif                    /* MSDOS */
  368.  
  369. #if MVS || VM
  370. #endif                    /* MVS || VM */
  371.  
  372. #if OS2
  373. #endif                    /* OS2 */
  374.  
  375. #if UNIX
  376. #endif                    /* UNIX */
  377.  
  378. #if VMS
  379. #endif                    /* VMS */
  380.  
  381. /*
  382.  * End of operating-system specific code.
  383.  */
  384.  
  385. char *tjunk;            /* avoid empty module */
  386.