vendor/jsl-0.3.0/src/jsl.c in distil-0.10.4 vs vendor/jsl-0.3.0/src/jsl.c in distil-0.11.0

- old
+ new

@@ -61,15 +61,17 @@ #include "jsparse.h" #include "jsscope.h" #include "jsscript.h" #include "jsstr.h" -#ifdef XP_UNIX +#if defined(XP_UNIX) || defined(_MINGW) #include <unistd.h> #include <sys/types.h> #include <dirent.h> +#ifndef _MINGW #include <sys/wait.h> +#endif #include <sys/stat.h> #endif #if defined(XP_WIN) || defined(XP_OS2) #include <io.h> /* for isatty() */ @@ -178,19 +180,26 @@ typedef struct JSLPathList { JSCList links; char path[MAXPATHLEN+1]; } JSLPathList; +typedef struct JSLAliasList { + JSCList links; + char path[MAXPATHLEN+1]; + char alias[MAXPATHLEN+1]; +} JSLAliasList; + typedef struct JSLScriptList { JSCList links; char path[MAXPATHLEN+1]; JSObject *obj; JSLScriptDependencyList directDependencies; } JSLScriptList; JSLScriptList gScriptList; +JSLAliasList gScriptAliasList; JSLPathList gIncludePathList; /* settings */ JSBool gAlwaysUseOptionExplicit = JS_FALSE; @@ -303,12 +312,13 @@ #endif -#ifdef WIN32 +#if defined(WIN32) +#ifndef _MINGW struct dirent { char d_name[MAXPATHLEN]; }; typedef struct DIR { @@ -369,10 +379,11 @@ memset(dir, 0, sizeof(DIR)); free (dir); } return 0; } +#endif /* caller should allocate MAXPATHLEN; does not set errno; returns null on failure */ static char * JSL_RealPath(const char *path, char *resolved_path) { @@ -601,10 +612,40 @@ JS_ASSERT(placeholderPos); formatPos += strlen(placeholders[placeholderType]); } } +static JSLAliasList* +AllocAliasListItem(const char *alias, const char *path) +{ + JSLAliasList *aliasItem= malloc(sizeof(JSLAliasList)); + memset(aliasItem, 0, sizeof(JSLAliasList)); + + JS_INIT_CLIST(&aliasItem->links); + strncpy(aliasItem->path, path, MAXPATHLEN); + strncpy(aliasItem->alias, alias, MAXPATHLEN); + + return aliasItem; +} + +static void +AddAliasToList(JSLAliasList *aliasList, const char *alias, const char *path) +{ + JSLAliasList *aliasItem= AllocAliasListItem(alias, path); + JS_APPEND_LINK(&aliasItem->links, &aliasList->links); +} + +static void +FreeAliasList(JSContext *cx, JSLAliasList *aliasList) +{ + while (!JS_CLIST_IS_EMPTY(&aliasList->links)) { + JSLAliasList *item = (JSLAliasList*)JS_LIST_HEAD(&aliasList->links); + JS_REMOVE_LINK(&item->links); + JS_free(cx, item); + } +} + static JSLPathList* AllocPathListItem(const char *path) { JSLPathList *pathItem; @@ -802,10 +843,11 @@ ResolveScriptPath(const char *relpath, char *path, JSLScriptList *parentScript) { JSBool result; struct stat _stat; JSLPathList *inc; + JSLAliasList *alias; char workingDir[MAXPATHLEN+1]; workingDir[0] = 0; getcwd(workingDir, sizeof(workingDir)); @@ -830,10 +872,26 @@ /* if the file exists, return true */ if (result && -1!=stat(path, &_stat)) return JS_TRUE; + // Check for an alias match. Aliased files still must exist or the search + // will continue. + for (alias= (JSLAliasList*)JS_LIST_HEAD(&gScriptAliasList.links); + alias!=&gScriptAliasList; + alias= (JSLAliasList*)JS_NEXT_LINK(&alias->links)) + { + if (0==strcmp(relpath, alias->alias)) + { + if (-1==stat(alias->path, &_stat)) + continue; + strcpy(path, alias->path); + return JS_TRUE; + } + } + + // Nothing matches, so look in include folders... for (inc= (JSLPathList*)JS_LIST_HEAD(&gIncludePathList.links); inc!=&gIncludePathList; inc= (JSLPathList*)JS_NEXT_LINK(&inc->links)) { chdir(inc->path); @@ -1798,24 +1856,76 @@ /* yank ending quote */ if (linepos[0] && linepos[1]) goto ProcessSettingErr_Garbage; *linepos = 0; AddPathToList(&gIncludePathList, path); -/* - if (JS_FALSE) { -ProcessSettingErr_MissingPath: + } + else if (strncasecmp(linepos, "alias", strlen("alias")) == 0) { + char delimiter; + char *path; + char *alias; + + if (!enable) { fclose(file); - return LintConfError(cx, path, lineno, "invalid include setting: missing path"); -ProcessSettingErr_MissingQuote: - fclose(file); - return LintConfError(cx, path, lineno, "invalid include setting: missing or mismatched quote"); -ProcessSettingErr_Garbage: - fclose(file); - return LintConfError(cx, path, lineno, "invalid include setting: garbage after path"); + return LintConfError(cx, path, lineno, "-alias is an invalid setting"); } -*/ + + linepos += strlen("alias"); + + /* require (but skip) whitespace */ + if (!*linepos || !isspace(*linepos)) goto ProcessSettingErr_MissingPath; + while (*linepos && isspace(*linepos)) + linepos++; + if (!*linepos) goto ProcessSettingErr_MissingPath; + + /* allow quote */ + if (*linepos == '\'') { + delimiter = *linepos; + linepos++; } + else if (*linepos == '"') { + delimiter = *linepos; + linepos++; + } + else + delimiter = 0; + + /* read alias */ + if (!*linepos) goto ProcessSettingErr_MissingQuote; + alias = linepos; + while (*linepos) + { + if (delimiter && *linepos==delimiter) + break; + if (!delimiter && isspace(*linepos)) + break; + linepos++; + } + if (delimiter && *linepos!=delimiter) goto ProcessSettingErr_MissingQuote; + if (!delimiter && !isspace(*linepos)) goto ProcessSettingErr_MissingPath; + + /* terminate path */ + *linepos++= 0; + + /* require (but skip) whitespace */ + while (*linepos && isspace(*linepos)) + linepos++; + if (!*linepos) goto ProcessSettingErr_MissingPath; + + /* read path */ + if (!*linepos) goto ProcessSettingErr_MissingQuote; + path = linepos; + while (*linepos && *linepos != delimiter) + linepos++; + if (delimiter && !*linepos) goto ProcessSettingErr_MissingQuote; + + /* yank ending quote */ + if (linepos[0] && linepos[1]) goto ProcessSettingErr_Garbage; + *linepos = 0; + + AddAliasToList(&gScriptAliasList, alias, path); + } else if (strncasecmp(linepos, "output-format", strlen("output-format")) == 0) { linepos += strlen("output-format"); /* skip whitespace */ if (!*linepos || !isspace(*linepos)) { @@ -2302,9 +2412,10 @@ argc--; argv++; JS_INIT_CLIST(&gScriptList.links); + JS_INIT_CLIST(&gScriptAliasList.links); JS_INIT_CLIST(&gIncludePathList.links); JS_ASSERT(sizeof(placeholders) / sizeof(placeholders[0]) == JSLPlaceholder_Limit); rt = JS_NewRuntime(64L * 1024L * 1024L); if (!rt)