home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / linuxdoc-sgml-1.1 / sgmls-1.1 / portproc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  2.2 KB  |  105 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.  
  16. /* This code shows how you might use system() to implement run_process().
  17. ANSI C says very little about the behaviour of system(), and so this
  18. is necessarily system dependent. */
  19.  
  20. /* Characters that are significant to the shell and so need quoting. */
  21. #define SHELL_MAGIC "$\\\"';&()|<>^ \t\n"
  22. /* Character with which to quote shell arguments. */
  23. #define SHELL_QUOTE_CHAR '\''
  24. /* String that can be used to get SHELL_QUOTE_CHAR into a quoted argument. */
  25. #define SHELL_ESCAPE_QUOTE "'\\''"
  26. /* Character that can be used to separate arguments to the shell. */
  27. #define SHELL_ARG_SEP ' '
  28.  
  29. static UNS shell_quote P((char *, char *));
  30.  
  31. int run_process(argv)
  32. char **argv;
  33. {
  34.       char **p;
  35.       char *s, *command;
  36.       int ret;
  37.       UNS len = 0;
  38.  
  39.       for (p = argv; *p; p++)
  40.        len += shell_quote(*p, (char *)0);
  41.       len += p - argv;
  42.       s = command = xmalloc(len);
  43.       for (p = argv; *p; ++p) {
  44.        if (s > command)
  45.         *s++ = SHELL_ARG_SEP;
  46.        s += shell_quote(*p, s);
  47.       }
  48.       *s++ = '\0';
  49.       errno = 0;
  50.       ret = system(command);
  51.       if (ret < 0)
  52.        appl_error(E_EXEC, argv[0], strerror(errno));
  53.       free(command);
  54.       return ret;
  55. }
  56.  
  57. /* Quote a string so that it appears as a single argument to the
  58. shell (as used for system()).  Store the quoted argument in result, if
  59. result is not NULL.  Return the length. */
  60.  
  61. static
  62. UNS shell_quote(s, result)
  63. char *s, *result;
  64. {
  65.      UNS len = 0;
  66.      int quoted = 0;
  67.  
  68.      if (strpbrk(s, SHELL_MAGIC)) {
  69.       quoted = 1;
  70.       len++;
  71.       if (result)
  72.            result[0] = SHELL_QUOTE_CHAR;
  73.      }
  74.      for (; *s; s++) {
  75.       if (*s == SHELL_QUOTE_CHAR) {
  76.            if (result)
  77.             strcpy(result + len, SHELL_ESCAPE_QUOTE);
  78.            len += strlen(SHELL_ESCAPE_QUOTE);
  79.       }
  80.       else {
  81.            if (result)
  82.             result[len] = *s;
  83.            len++;
  84.       }
  85.      }
  86.      if (quoted) {
  87.       if (result)
  88.            result[len] = SHELL_QUOTE_CHAR;
  89.       len++;
  90.      }
  91.      return len;
  92. }
  93.  
  94. #endif /* SUPPORT_SUBDOC */
  95.  
  96. /*
  97. Local Variables:
  98. c-indent-level: 5
  99. c-continued-statement-offset: 5
  100. c-brace-offset: -5
  101. c-argdecl-indent: 0
  102. c-label-offset: -5
  103. End:
  104. */
  105.