Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtdocbook / sgmls / portproc.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: portproc.c /main/3 1996/06/19 17:16:49 drk $ */
24 /* portproc.c -
25
26    Semi-portable implementation of run_process().
27
28      Written by James Clark (jjc@jclark.com).
29 */
30
31 #include "config.h"
32
33 #ifdef SUPPORT_SUBDOC
34
35 #include "std.h"
36 #include "entity.h"
37 #include "appl.h"
38
39 /* This code shows how you might use system() to implement run_process().
40 ANSI C says very little about the behaviour of system(), and so this
41 is necessarily system dependent. */
42
43 /* Characters that are significant to the shell and so need quoting. */
44 #define SHELL_MAGIC "$\\\"';&()|<>^ \t\n"
45 /* Character with which to quote shell arguments. */
46 #define SHELL_QUOTE_CHAR '\''
47 /* String that can be used to get SHELL_QUOTE_CHAR into a quoted argument. */
48 #define SHELL_ESCAPE_QUOTE "'\\''"
49 /* Character that can be used to separate arguments to the shell. */
50 #define SHELL_ARG_SEP ' '
51
52 static UNS shell_quote P((char *, char *));
53
54 int run_process(argv)
55 char **argv;
56 {
57       char **p;
58       char *s, *command;
59       int ret;
60       UNS len = 0;
61
62       for (p = argv; *p; p++)
63            len += shell_quote(*p, (char *)0);
64       len += p - argv;
65       s = command = xmalloc(len);
66       for (p = argv; *p; ++p) {
67            if (s > command)
68                 *s++ = SHELL_ARG_SEP;
69            s += shell_quote(*p, s);
70       }
71       *s++ = '\0';
72       errno = 0;
73       ret = system(command);
74       if (ret < 0)
75            appl_error(E_EXEC, argv[0], strerror(errno));
76       free(command);
77       return ret;
78 }
79
80 /* Quote a string so that it appears as a single argument to the
81 shell (as used for system()).  Store the quoted argument in result, if
82 result is not NULL.  Return the length. */
83
84 static
85 UNS shell_quote(s, result)
86 char *s, *result;
87 {
88      UNS len = 0;
89      int quoted = 0;
90
91      if (strpbrk(s, SHELL_MAGIC)) {
92           quoted = 1;
93           len++;
94           if (result)
95                result[0] = SHELL_QUOTE_CHAR;
96      }
97      for (; *s; s++) {
98           if (*s == SHELL_QUOTE_CHAR) {
99                if (result)
100                     strcpy(result + len, SHELL_ESCAPE_QUOTE);
101                len += strlen(SHELL_ESCAPE_QUOTE);
102           }
103           else {
104                if (result)
105                     result[len] = *s;
106                len++;
107           }
108      }
109      if (quoted) {
110           if (result)
111                result[len] = SHELL_QUOTE_CHAR;
112           len++;
113      }
114      return len;
115 }
116
117 #endif /* SUPPORT_SUBDOC */
118
119 /*
120 Local Variables:
121 c-indent-level: 5
122 c-continued-statement-offset: 5
123 c-brace-offset: -5
124 c-argdecl-indent: 0
125 c-label-offset: -5
126 End:
127 */