1 /*-------------------------------------------------------------------------
2 MySystem - SDCC Support function
4 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999)
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
35 #define EXE_EXT ".exe"
41 set *binPathSet = NULL; /* set of binary paths */
45 * get command and arguments from command line
49 split_command(const char *cmd_line, char **command, char **params)
51 const char *p, *cmd_start;
56 /* skip leading spaces */
57 for (p = cmd_line; isspace(*p); p++)
74 while (*p != '\0' && !isspace(*p))
78 while (*p != '\0' && *p != delim)
82 if (command != NULL) {
84 str = Safe_alloc(len + 1);
85 strncpy(str, cmd_start, len);
92 /* skip spaces before parameters */
98 *params = Safe_strdup(p);
103 * merge command and parameters to command line
107 merge_command(const char *command, const char *params)
112 /* allocate extra space for ' ' and '\0' */
113 len = strlen(command) + strlen(params) + 2;
114 cmd_line = (char *)Safe_alloc(len);
115 SNPRINTF(cmd_line, len, "%s %s", command, params);
122 * check if the path is absolute
126 has_path(const char *path)
128 if (strrchr(path, DIR_SEPARATOR_CHAR) == NULL)
130 /* try *nix deir separator on WIN32 */
131 if (strrchr(path, UNIX_DIR_SEPARATOR_CHAR) == NULL)
141 * 1) if the command is specified by absolute or relative path, try it
142 * 2) try to find the command in predefined path's
147 get_path(const char *cmd)
149 char *cmdLine = NULL;
153 char cmdPath[PATH_MAX];
156 /* get the command */
157 split_command(cmd, &command, &args);
159 if (!has_path(command)) {
160 /* try to find the command in predefined binary paths */
161 if ((path = (char *)setFirstItem(binPathSet)) != NULL) {
164 SNPRINTF(cmdPath, sizeof cmdPath,
165 "%s" DIR_SEPARATOR_STRING "%s", path, command);
168 /* Try if cmdPath or cmdPath.exe exist */
169 if (0 == access(cmdPath, 0) ||
170 0 == access(strncatz(cmdPath, EXE_EXT, sizeof cmdPath), 0)) {
173 if (0 == access(cmdPath, X_OK)) {
175 /* compose the command line */
176 cmdLine = merge_command(cmdPath, args);
179 } while ((path = (char *)setNextItem(binPathSet)) != NULL);
182 cmdLine = merge_command(command, args);
186 * the command is defined with absolute path:
189 return Safe_strdup(cmd);
200 * call an external program with arguements
204 my_system(const char *cmd)
207 char *cmdLine = get_path(cmd);
209 assert(cmdLine != NULL);
211 if (options.verboseExec) {
212 printf("+ %s\n", cmdLine);
223 * pipe an external program with arguements
227 #define popen_read(cmd) _popen((cmd), "rt")
229 #define popen_read(cmd) popen((cmd), "r")
233 my_popen(const char *cmd)
236 char *cmdLine = get_path(cmd);
238 assert(cmdLine != NULL);
240 if (options.verboseExec) {
241 printf("+ %s\n", cmdLine);
244 fp = popen_read(cmdLine);