using namespace std; #include #include #include #include #include #include #include "StdString.h" #include "Processor.h" #include "Util.h" bool IsAVar(const CString& var) { return var[0] == '$'; } CString StripVar(const CString& var) { if (!IsAVar(var)) return var; return var.substr(1, var.npos); } Processor::Processor(CString& path, handleFileFunc f, getPathFunc p, askFunc a, bool i) : mDoGoto(false), mHandleFile(f), mGetPath(p), mAsk(a), mError(Processor::DefaultError), mInstalling(i) { char cwd[MAXPATHLEN]; getwd(cwd); mCWD = cwd; mConditionals["INSTALLING"] = i; mPath = path; } Processor::~Processor() { if (mInstalling) unlink(mPath); } void Processor::ProcessLine(const CString& line, unsigned& nextLine) { CStringArray parts; split(line, ":", parts); nextLine++; if (parts.size() == 0) return; #pragma mark LABEL if (parts[0] == "LABEL") { printf("%u: %s\n", nextLine - 1, line.c_str()); if (parts.size() != 2) goto error; if (mDoGoto && parts[1] == mLabel) { mDoGoto = false; mLabel = ""; } mLabels[parts[1]] = nextLine; return; } if (mDoGoto) return; printf("%u: %s\n", nextLine - 1, line.c_str()); #pragma mark FILE if (parts[0] == "FILE") { if (parts.size() == 3) { CString file, dir; if (parts[1][0] == '/') { file = parts[1].substr(1); dir = "/"; } else { file = parts[1]; dir = mCWD; } if (mInstalling) { if (ResolveConditional(parts[2])) rm_rf(dir + '/' + file); (*mHandleFile)(file, dir, mPath); } else { if (IsADirectory((dir == "/" ? dir : dir + "/" ) + file)) { CStringArray list, dirs; int fd = open(".", O_RDONLY, 0); chdir(dir); FileListingForDirectoryWithIgnore(file, list, dirs, mIgnore); fchdir(fd); close(fd); if (list.size() == 0) //empty dir or ignored dir (*mHandleFile)(file, dir, mPath); for (unsigned i=0; i