2014-05-17 23:15:15 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* OSXCross Compiler Wrapper *
|
2015-02-08 10:27:26 +01:00
|
|
|
* Copyright (C) 2014, 2015 by Thomas Poechtrager *
|
2014-05-17 23:15:15 +02:00
|
|
|
* t.poechtrager@gmail.com *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software *
|
|
|
|
* Foundation, Inc., *
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
#include "proginc.h"
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
using namespace tools;
|
|
|
|
using namespace target;
|
|
|
|
|
|
|
|
namespace program {
|
|
|
|
namespace osxcross {
|
|
|
|
|
|
|
|
int env(int argc, char **argv) {
|
|
|
|
char epath[PATH_MAX + 1];
|
|
|
|
char *oldpath = getenv("PATH");
|
|
|
|
|
|
|
|
assert(oldpath);
|
|
|
|
|
|
|
|
if (!getExecutablePath(epath, sizeof(epath)))
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
// TODO: escape?
|
|
|
|
|
2014-09-27 10:07:15 +02:00
|
|
|
auto containsBadChars = [](const char * p, const char * desc)->bool {
|
2014-05-17 23:15:15 +02:00
|
|
|
if (!p)
|
2014-09-27 10:07:15 +02:00
|
|
|
return false;
|
2014-05-17 23:15:15 +02:00
|
|
|
|
|
|
|
const char *pp = p;
|
|
|
|
|
2014-12-24 09:54:27 +01:00
|
|
|
do {
|
2014-05-17 23:15:15 +02:00
|
|
|
auto badChar = [&](const char *p) {
|
2015-05-02 21:49:23 +02:00
|
|
|
err << desc << " should not contain '" << *p << "'" << warn.endl();
|
2014-05-17 23:15:15 +02:00
|
|
|
|
2014-12-24 09:54:27 +01:00
|
|
|
const char *start = p - std::min<size_t>(p - pp, 30);
|
2014-05-17 23:15:15 +02:00
|
|
|
|
2014-12-24 09:54:27 +01:00
|
|
|
size_t len = std::min<size_t>(strlen(start), 60);
|
2015-05-02 21:49:23 +02:00
|
|
|
|
|
|
|
std::cerr << std::endl;
|
2014-05-17 23:15:15 +02:00
|
|
|
std::cerr << std::string(start, len) << std::endl;
|
|
|
|
|
|
|
|
while (start++ != p)
|
|
|
|
std::cerr << " ";
|
|
|
|
|
|
|
|
std::cerr << "^" << std::endl;
|
|
|
|
};
|
|
|
|
switch (*p) {
|
|
|
|
case '"':
|
|
|
|
case '\'':
|
|
|
|
case '$':
|
|
|
|
case ' ':
|
|
|
|
case ';':
|
|
|
|
badChar(p);
|
2014-09-27 10:07:15 +02:00
|
|
|
return true;
|
2014-05-17 23:15:15 +02:00
|
|
|
}
|
2014-12-24 09:54:27 +01:00
|
|
|
} while (*p && *++p);
|
2014-09-27 10:07:15 +02:00
|
|
|
return false;
|
2014-05-17 23:15:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (argc <= 1) {
|
|
|
|
const std::string &pname = getParentProcessName();
|
|
|
|
|
|
|
|
if (pname == "csh" || pname == "tcsh") {
|
|
|
|
std::cerr << std::endl << "you are invoking this program from a C shell, "
|
|
|
|
<< std::endl << "please use " << std::endl << std::endl
|
|
|
|
<< "setenv PATH `" << epath << "/osxcross-env -v=PATH`"
|
2015-02-23 21:19:34 +01:00
|
|
|
<< std::endl << std::endl << "instead." << std::endl
|
|
|
|
<< std::endl;
|
2014-05-17 23:15:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto hasPath = [](const char * ov, const char * v, const char * vs)->bool {
|
|
|
|
// ov = old value
|
|
|
|
// v = value
|
|
|
|
// vs = value suffix
|
|
|
|
|
|
|
|
if (!ov || !v)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool hasPathSeparator = false;
|
|
|
|
|
|
|
|
for (auto p = ov; *p; ++p) {
|
|
|
|
if (*p == ':') {
|
|
|
|
hasPathSeparator = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string tmp;
|
|
|
|
|
|
|
|
auto check = [&](int t)->bool {
|
|
|
|
tmp.clear();
|
|
|
|
|
|
|
|
if (t == 0)
|
|
|
|
tmp = ':';
|
|
|
|
|
|
|
|
tmp += v;
|
|
|
|
|
|
|
|
if (vs)
|
|
|
|
tmp += vs;
|
|
|
|
|
|
|
|
if (t == 1)
|
|
|
|
tmp += ':';
|
|
|
|
|
|
|
|
return strstr(ov, tmp.c_str()) != nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
return ((hasPathSeparator && (check(0) || check(1))) || check(-1));
|
|
|
|
};
|
|
|
|
|
2015-02-23 21:19:34 +01:00
|
|
|
if (containsBadChars(oldpath, "PATH"))
|
2014-05-17 23:15:15 +02:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
std::stringstream path;
|
|
|
|
std::stringstream librarypath;
|
|
|
|
std::map<std::string, std::string> vars;
|
|
|
|
|
|
|
|
path << oldpath;
|
|
|
|
|
|
|
|
if (!hasPath(oldpath, epath, nullptr))
|
|
|
|
path << ":" << epath;
|
|
|
|
|
|
|
|
vars["PATH"] = path.str();
|
|
|
|
|
|
|
|
auto printVariable = [&](const std::string & var)->bool {
|
|
|
|
auto it = vars.find(var);
|
|
|
|
if (it == vars.end()) {
|
|
|
|
std::cerr << "unknown variable '" << var << "'" << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::cout << it->second << std::endl;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (argc <= 1) {
|
|
|
|
std::cout << std::endl;
|
|
|
|
for (auto &v : vars) {
|
|
|
|
std::cout << "export " << v.first << "=";
|
|
|
|
if (!printVariable(v.first))
|
|
|
|
return 1;
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (strncmp(argv[1], "-v=", 3))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
const char *var = argv[1] + 3;
|
|
|
|
return static_cast<int>(printVariable(var));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace osxcross
|
|
|
|
} // namespace program
|