home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / dev / lang / sgmls / src / portproc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-10  |  2.3 KB  |  106 lines

  1. /* portproc.c -
  2.  
  3.    Semi-portable implementation of run_process().
  4.  
  5.      Written by James Clark (jjc@jclark.com).
  6. */
  7.  
  8. #include "config.h"
  9.  
  10. #ifdef SUPPORT_SUBDOC
  11.  
  12. #include "std.h"
  13. #include "entity.h"
  14. #include "appl.h"
  15. #include "alloc.h"
  16.  
  17. /* This code shows how you might use system() to implement run_process().
  18. ANSI C says very little about the behaviour of system(), and so this
  19. is necessarily system dependent. */
  20.  
  21. /* Characters that are significant to the shell and so need quoting. */
  22. #define SHELL_MAGIC "$\\\"';&()|<>^ \t\n"
  23. /* Character with which to quote shell arguments. */
  24. #define SHELL_QUOTE_CHAR '\''
  25. /* String that can be used to get SHELL_QUOTE_CHAR into a quoted argument. */
  26. #define SHELL_ESCAPE_QUOTE "'\\''"
  27. /* Character that can be used to separate arguments to the shell. */
  28. #define SHELL_ARG_SEP ' '
  29.  
  30. static UNS shell_quote P((char *, char *));
  31.  
  32. int run_process(argv)
  33. char **argv;
  34. {
  35.       char **p;
  36.       char *s, *command;
  37.       int ret;
  38.       UNS len = 0;
  39.  
  40.       for (p = argv; *p; p++)
  41.        len += shell_quote(*p, (char *)0);
  42.       len += p - argv;
  43.       s = command = xmalloc(len);
  44.       for (p = argv; *p; ++p) {
  45.        if (s > command)
  46.         *s++ = SHELL_ARG_SEP;
  47.        s += shell_quote(*p, s);
  48.       }
  49.       *s++ = '\0';
  50.       errno = 0;
  51.       ret = system(command);
  52.       if (ret < 0)
  53.        appl_error(E_EXEC, argv[0], strerror(errno));
  54.       free(command);
  55.       return ret;
  56. }
  57.  
  58. /* Quote a string so that it appears as a single argument to the
  59. shell (as used for system()).  Store the quoted argument in result, if
  60. result is not NULL.  Return the length. */
  61.  
  62. static
  63. UNS shell_quote(s, result)
  64. char *s, *result;
  65. {
  66.      UNS len = 0;
  67.      int quoted = 0;
  68.  
  69.      if (strpbrk(s, SHELL_MAGIC)) {
  70.       quoted = 1;
  71.       len++;
  72.       if (result)
  73.            result[0] = SHELL_QUOTE_CHAR;
  74.      }
  75.      for (; *s; s++) {
  76.       if (*s == SHELL_QUOTE_CHAR) {
  77.            if (result)
  78.             strcpy(result + len, SHELL_ESCAPE_QUOTE);
  79.            len += strlen(SHELL_ESCAPE_QUOTE);
  80.       }
  81.       else {
  82.            if (result)
  83.             result[len] = *s;
  84.            len++;
  85.       }
  86.      }
  87.      if (quoted) {
  88.       if (result)
  89.            result[len] = SHELL_QUOTE_CHAR;
  90.       len++;
  91.      }
  92.      return len;
  93. }
  94.  
  95. #endif /* SUPPORT_SUBDOC */
  96.  
  97. /*
  98. Local Variables:
  99. c-indent-level: 5
  100. c-continued-statement-offset: 5
  101. c-brace-offset: -5
  102. c-argdecl-indent: 0
  103. c-label-offset: -5
  104. End:
  105. */
  106.