14 is_comment_line(const char* p)
16 while (*p && isspace(*p)) {
23 path_append(const string& base, const string& leaf)
26 if (base.length() > 0 && leaf.length() > 0) {
34 is_whitespace_line(const char* p)
46 is_exclude_line(const char* p) {
51 else if (isspace(*p)) {
62 split_line(const char* p, vector<string>* out)
65 enum { WHITE, TEXT } state = WHITE;
82 out->push_back(string(q, p-q));
91 out->push_back(string(q, p-q));
96 add_file(vector<FileRecord>* files, const string& listFile, int listLine,
97 const string& sourceName, const string& outName)
100 rec.listFile = listFile;
101 rec.listLine = listLine;
102 rec.sourceName = sourceName;
103 rec.outName = outName;
104 files->push_back(rec);
108 replace_variables(const string& input,
109 const map<string, string>& variables,
111 if (variables.empty()) {
115 // Abort if the variable prefix is not found
116 if (input.find("${") == string::npos) {
120 string result = input;
122 // Note: rather than be fancy to detect recursive replacements,
123 // we simply iterate till a given threshold is met.
130 for (map<string, string>::const_iterator it = variables.begin();
131 it != variables.end(); ++it) {
132 string::size_type pos = 0;
133 while((pos = result.find(it->first, pos)) != string::npos) {
134 result = result.replace(pos, it->first.length(), it->second);
135 pos += it->second.length();
139 if (did_replace && --retries == 0) {
141 fprintf(stderr, "Recursive replacement detected during variables "
142 "substitution. Full list of variables is: ");
144 for (map<string, string>::const_iterator it = variables.begin();
145 it != variables.end(); ++it) {
146 fprintf(stderr, " %s=%s\n",
147 it->first.c_str(), it->second.c_str());
152 } while (did_replace);
158 read_list_file(const string& filename,
159 const map<string, string>& variables,
160 vector<FileRecord>* files,
161 vector<string>* excludes)
170 f = fopen(filename.c_str(), "r");
172 fprintf(stderr, "Could not open list file (%s): %s\n",
173 filename.c_str(), strerror(errno));
178 err = fseek(f, 0, SEEK_END);
180 fprintf(stderr, "Could not seek to the end of file %s. (%s)\n",
181 filename.c_str(), strerror(errno));
188 err = fseek(f, 0, SEEK_SET);
190 fprintf(stderr, "Could not seek to the beginning of file %s. (%s)\n",
191 filename.c_str(), strerror(errno));
196 buf = (char*)malloc(size+1);
198 // (potentially large)
199 fprintf(stderr, "out of memory (%ld)\n", size);
204 if (1 != fread(buf, size, 1, f)) {
205 fprintf(stderr, "error reading file %s. (%s)\n",
206 filename.c_str(), strerror(errno));
216 if (*p == '\r' || *p == '\n') {
225 for (i=0; i<lineCount; i++) {
228 if (is_whitespace_line(p) || is_comment_line(p)) {
231 else if (is_exclude_line(p)) {
232 while (*p != '-') p++;
234 excludes->push_back(string(p));
237 vector<string> words;
239 split_line(p, &words);
243 for (size_t k=0; k<words.size(); k++) {
244 printf("'%s' ", words[k].c_str());
249 if (words.size() == 1) {
252 string w0 = replace_variables(words[0], variables, &error);
257 add_file(files, filename, i+1, w0, w0);
259 else if (words.size() == 2) {
263 w0 = replace_variables(words[0], variables, &error);
265 w1 = replace_variables(words[1], variables, &error);
271 add_file(files, filename, i+1, w0, w1);
274 fprintf(stderr, "%s:%d: bad format: %s\n", filename.c_str(),
294 locate(FileRecord* rec, const vector<string>& search)
298 for (vector<string>::const_iterator it=search.begin();
299 it!=search.end(); it++) {
300 string full = path_append(*it, rec->sourceName);
302 err = stat(full.c_str(), &st);
304 rec->sourceBase = *it;
305 rec->sourcePath = full;
306 rec->sourceMod = st.st_mtime;
307 rec->sourceIsDir = S_ISDIR(st.st_mode);
312 fprintf(stderr, "%s:%d: couldn't locate source file: %s\n",
313 rec->listFile.c_str(), rec->listLine, rec->sourceName.c_str());
318 stat_out(const string& base, FileRecord* rec)
320 rec->outPath = path_append(base, rec->outName);
324 err = stat(rec->outPath.c_str(), &st);
326 rec->outMod = st.st_mtime;
327 rec->outIsDir = S_ISDIR(st.st_mode);
330 rec->outIsDir = false;
335 dir_part(const string& filename)
337 int pos = filename.rfind('/');
341 return filename.substr(0, pos);
345 add_more(const string& entry, bool isDir,
346 const FileRecord& rec, vector<FileRecord>*more)
349 r.listFile = rec.listFile;
350 r.listLine = rec.listLine;
351 r.sourceName = path_append(rec.sourceName, entry);
352 r.sourcePath = path_append(rec.sourceBase, r.sourceName);
354 int err = stat(r.sourcePath.c_str(), &st);
356 r.sourceMod = st.st_mtime;
358 r.sourceIsDir = isDir;
359 r.outName = path_append(rec.outName, entry);
364 matches_excludes(const char* file, const vector<string>& excludes)
366 for (vector<string>::const_iterator it=excludes.begin();
367 it!=excludes.end(); it++) {
368 if (0 == fnmatch(it->c_str(), file, FNM_PERIOD)) {
376 list_dir(const string& path, const FileRecord& rec,
377 const vector<string>& excludes,
378 vector<FileRecord>* more)
382 string full = path_append(rec.sourceBase, rec.sourceName);
383 full = path_append(full, path);
385 DIR *d = opendir(full.c_str());
393 while (NULL != (ent = readdir(d))) {
394 if (0 == strcmp(".", ent->d_name)
395 || 0 == strcmp("..", ent->d_name)) {
398 if (matches_excludes(ent->d_name, excludes)) {
401 string entry = path_append(path, ent->d_name);
402 #ifdef HAVE_DIRENT_D_TYPE
403 bool is_directory = (ent->d_type == DT_DIR);
405 // If dirent.d_type is missing, then use stat instead
406 struct stat stat_buf;
407 stat(entry.c_str(), &stat_buf);
408 bool is_directory = S_ISDIR(stat_buf.st_mode);
410 add_more(entry, is_directory, rec, more);
412 dirs.push_back(entry);
417 for (vector<string>::iterator it=dirs.begin(); it!=dirs.end(); it++) {
418 list_dir(*it, rec, excludes, more);
425 list_dir(const FileRecord& rec, const vector<string>& excludes,
426 vector<FileRecord>* files)
428 return list_dir("", rec, excludes, files);