2013-04-12 23:00:27 +00:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2017-03-11 11:33:49 +00:00
|
|
|
| PHP Version 7 |
|
2013-04-12 23:00:27 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2013-09-28 17:17:33 +00:00
|
|
|
| Copyright (c) 1997-2013 The PHP Group |
|
2013-04-12 23:00:27 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2013-09-28 17:17:33 +00:00
|
|
|
| http://www.opensource.org/licenses/mit-license.php MIT License |
|
2013-04-12 23:00:27 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2013-09-28 17:17:33 +00:00
|
|
|
| Author: Jani Taskinen <jani.taskinen@iki.fi> |
|
|
|
|
| Author: Patrick Reilly <preilly@php.net> |
|
2013-04-12 23:00:27 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2016-01-06 09:01:35 +00:00
|
|
|
#include "php_v8js_macros.h"
|
|
|
|
|
2013-04-12 23:00:27 +00:00
|
|
|
extern "C" {
|
|
|
|
#include "php.h"
|
|
|
|
#include "zend_exceptions.h"
|
|
|
|
}
|
|
|
|
|
2015-08-01 17:41:50 +00:00
|
|
|
static void v8js_commonjs_split_terms(const char *identifier, std::vector<char *> &terms)
|
2013-04-12 23:00:27 +00:00
|
|
|
{
|
2015-08-01 17:07:45 +00:00
|
|
|
char *term = (char *) emalloc(PATH_MAX), *ptr = term;
|
2013-04-12 23:00:27 +00:00
|
|
|
|
|
|
|
while (*identifier > 0) {
|
|
|
|
if (*identifier == '/') {
|
2015-08-01 17:54:23 +00:00
|
|
|
if (ptr > term) {
|
2013-04-12 23:00:27 +00:00
|
|
|
// Terminate term string and add to terms vector
|
|
|
|
*ptr++ = 0;
|
2015-08-01 17:07:45 +00:00
|
|
|
terms.push_back(estrdup(term));
|
2013-04-12 23:00:27 +00:00
|
|
|
|
|
|
|
// Reset term string
|
|
|
|
ptr = term;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*ptr++ = *identifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
identifier++;
|
|
|
|
}
|
|
|
|
|
2015-08-01 17:54:23 +00:00
|
|
|
if (ptr > term) {
|
2013-04-12 23:00:27 +00:00
|
|
|
// Terminate term string and add to terms vector
|
|
|
|
*ptr++ = 0;
|
2015-08-01 17:07:45 +00:00
|
|
|
terms.push_back(estrdup(term));
|
2013-04-12 23:00:27 +00:00
|
|
|
}
|
|
|
|
|
2015-08-01 17:07:45 +00:00
|
|
|
efree(term);
|
2013-04-12 23:00:27 +00:00
|
|
|
}
|
|
|
|
|
2015-08-01 17:41:50 +00:00
|
|
|
void v8js_commonjs_normalise_identifier(const char *base, const char *identifier, char *normalised_path, char *module_name)
|
2013-04-12 23:00:27 +00:00
|
|
|
{
|
|
|
|
std::vector<char *> id_terms, terms;
|
2014-12-13 00:30:23 +00:00
|
|
|
v8js_commonjs_split_terms(identifier, id_terms);
|
2013-04-12 23:00:27 +00:00
|
|
|
|
|
|
|
// If we have a relative module identifier then include the base terms
|
|
|
|
if (!strcmp(id_terms.front(), ".") || !strcmp(id_terms.front(), "..")) {
|
2014-12-13 00:30:23 +00:00
|
|
|
v8js_commonjs_split_terms(base, terms);
|
2013-04-12 23:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
terms.insert(terms.end(), id_terms.begin(), id_terms.end());
|
|
|
|
|
|
|
|
std::vector<char *> normalised_terms;
|
|
|
|
|
|
|
|
for (std::vector<char *>::iterator it = terms.begin(); it != terms.end(); it++) {
|
|
|
|
char *term = *it;
|
|
|
|
|
|
|
|
if (!strcmp(term, "..")) {
|
|
|
|
// Ignore parent term (..) if it's the first normalised term
|
|
|
|
if (normalised_terms.size() > 0) {
|
2015-08-01 17:30:55 +00:00
|
|
|
// Remove the parent normalized term (and free it)
|
|
|
|
efree(normalised_terms.back());
|
2013-04-12 23:00:27 +00:00
|
|
|
normalised_terms.pop_back();
|
|
|
|
}
|
2015-08-01 17:30:55 +00:00
|
|
|
|
|
|
|
// free the ".." term
|
|
|
|
efree(term);
|
2013-04-12 23:00:27 +00:00
|
|
|
} else if (strcmp(term, ".")) {
|
|
|
|
// Add the term if it's not the current term (.)
|
|
|
|
normalised_terms.push_back(term);
|
2015-08-01 17:30:55 +00:00
|
|
|
} else {
|
|
|
|
// Discard "." term
|
|
|
|
efree(term);
|
2013-04-12 23:00:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialise the normalised path string
|
|
|
|
*normalised_path = 0;
|
|
|
|
*module_name = 0;
|
|
|
|
|
|
|
|
strcat(module_name, normalised_terms.back());
|
2015-08-01 17:07:45 +00:00
|
|
|
|
|
|
|
efree(normalised_terms.back());
|
2013-04-12 23:00:27 +00:00
|
|
|
normalised_terms.pop_back();
|
|
|
|
|
|
|
|
for (std::vector<char *>::iterator it = normalised_terms.begin(); it != normalised_terms.end(); it++) {
|
|
|
|
char *term = *it;
|
|
|
|
|
|
|
|
if (strlen(normalised_path) > 0) {
|
|
|
|
strcat(normalised_path, "/");
|
|
|
|
}
|
|
|
|
|
|
|
|
strcat(normalised_path, term);
|
2015-08-01 17:30:55 +00:00
|
|
|
efree(term);
|
2013-04-12 23:00:27 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-02 20:41:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
* End:
|
|
|
|
* vim600: noet sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: noet sw=4 ts=4
|
|
|
|
*/
|