2017-10-08 02:28:13 +02:00
|
|
|
/*
|
|
|
|
* main.cpp
|
|
|
|
*
|
|
|
|
* Created on: 07.10.2017
|
|
|
|
* Author: julian
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/mman.h> // for mmap()
|
|
|
|
#include <sys/stat.h> // for fstat()
|
|
|
|
#include <fcntl.h> // for open()
|
|
|
|
#include <unistd.h> // for close()
|
2017-10-08 18:44:27 +02:00
|
|
|
#include <errno.h> // for perror()
|
2017-10-08 02:28:13 +02:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <cstring>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
2017-10-08 18:44:27 +02:00
|
|
|
#include <set>
|
2017-10-08 02:28:13 +02:00
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
#include "fs.hpp"
|
|
|
|
#include "memory_string.hpp"
|
2017-10-08 02:28:13 +02:00
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
using namespace std;
|
2017-10-08 02:28:13 +02:00
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
class InputExtractor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
class Exception : public std::runtime_error {
|
|
|
|
public:
|
|
|
|
Exception(const std::string &str) : std::runtime_error(str) {}
|
|
|
|
};
|
2017-10-08 02:28:13 +02:00
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
typedef std::vector<Path::Path> List;
|
2017-10-08 02:28:13 +02:00
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
List operator()(const Path::Path &file, const MemoryString &str);
|
2017-10-08 02:28:13 +02:00
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
std::string macroExpand(const std::string &input);
|
|
|
|
List Include(Path::Path);
|
2017-10-08 02:28:13 +02:00
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
protected:
|
|
|
|
std::map<std::string, std::string> macros;
|
|
|
|
std::set<Path::Path> includes;
|
2017-10-08 02:28:13 +02:00
|
|
|
};
|
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
#include <functional>
|
2017-10-08 02:28:13 +02:00
|
|
|
|
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
template <typename iterator>
|
|
|
|
std::string readTill(iterator &start, const iterator &end, std::function<bool(const iterator&)> limiter) {
|
2017-10-08 19:09:50 +02:00
|
|
|
iterator current = start;
|
|
|
|
|
|
|
|
while(current != end && !limiter(current)) {
|
|
|
|
++current;
|
2017-10-08 02:28:13 +02:00
|
|
|
}
|
|
|
|
|
2017-10-08 19:09:50 +02:00
|
|
|
return std::string(start, current);
|
2017-10-08 02:28:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
template <typename iterator>
|
2017-10-08 19:18:41 +02:00
|
|
|
std::string readBrackets(iterator ¤t, const iterator &end, const char * brackets) {
|
|
|
|
// auto current = begin;
|
2017-10-08 02:28:13 +02:00
|
|
|
|
2017-10-08 19:18:41 +02:00
|
|
|
if (current == end || *current != brackets[0]) {
|
|
|
|
std::cout << brackets[0] << "!=" << *current;
|
2017-10-08 18:44:27 +02:00
|
|
|
return std::string();
|
2017-10-08 02:28:13 +02:00
|
|
|
}
|
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
// skip first opening bracket
|
|
|
|
current++;
|
2017-10-08 03:53:03 +02:00
|
|
|
|
2017-10-08 02:28:13 +02:00
|
|
|
int depth = 1;
|
2017-10-08 18:44:27 +02:00
|
|
|
auto bbegin = current, bend = current;
|
2017-10-08 02:28:13 +02:00
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
while(depth > 0 && current != end) {
|
|
|
|
if (*current== brackets[0]) {
|
2017-10-08 02:28:13 +02:00
|
|
|
depth++;
|
2017-10-08 18:44:27 +02:00
|
|
|
} else if (*current == brackets[1]) {
|
2017-10-08 02:28:13 +02:00
|
|
|
depth--;
|
2017-10-08 18:44:27 +02:00
|
|
|
} else {
|
|
|
|
bend = ++current;
|
2017-10-08 02:28:13 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-08 18:44:27 +02:00
|
|
|
|
2017-10-08 02:28:13 +02:00
|
|
|
// advance beyond last bracket
|
2017-10-08 18:44:27 +02:00
|
|
|
if (current != end)
|
|
|
|
current++;
|
2017-10-08 02:28:13 +02:00
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
|
|
|
|
return std::string(bbegin, bend);
|
2017-10-08 02:28:13 +02:00
|
|
|
}
|
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
std::string InputExtractor::macroExpand(const std::string &input) {
|
2017-10-08 02:28:13 +02:00
|
|
|
std::string result;
|
2017-10-08 18:44:27 +02:00
|
|
|
std::map<std::string, std::string>::iterator lookup;
|
2017-10-08 02:28:13 +02:00
|
|
|
//cout << "expanding: " << input << endl;
|
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
std::string::const_iterator current = input.begin();
|
|
|
|
while(current != input.end()) {
|
|
|
|
if (*current == '\\') {
|
|
|
|
current++;
|
|
|
|
std::string::const_iterator start = current;
|
2017-10-08 02:28:13 +02:00
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
while(current != input.end() && *current != '\\') {
|
|
|
|
if ((lookup = macros.find(std::string(start, current))) != macros.end()) {
|
2017-10-08 02:28:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lookup == macros.end()) {
|
2017-10-08 18:44:27 +02:00
|
|
|
throw Exception("unknown macro in macro expansion: " + std::string(start, current));
|
2017-10-08 02:28:13 +02:00
|
|
|
}
|
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
result += lookup->second;
|
2017-10-08 02:28:13 +02:00
|
|
|
} else {
|
2017-10-08 18:44:27 +02:00
|
|
|
result += *current;
|
2017-10-08 02:28:13 +02:00
|
|
|
}
|
2017-10-08 18:44:27 +02:00
|
|
|
++current;
|
2017-10-08 02:28:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
typedef std::map<std::string, std::function<void(InputExtractor::List&, std::string)>> CommandList;
|
|
|
|
|
2017-10-08 03:24:20 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
2017-10-08 02:28:13 +02:00
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
bool Exists(std::string path) {
|
|
|
|
std::ifstream file(path);
|
|
|
|
if (!file) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
InputExtractor::List InputExtractor::Include(Path::Path path) {
|
|
|
|
path = Path::Clean(path);
|
|
|
|
List list;
|
|
|
|
|
|
|
|
std::cout << "including file " << path << "...";
|
|
|
|
|
|
|
|
// look for include
|
|
|
|
if (includes.find(path) != includes.end()) {
|
|
|
|
cout << "SKIP" << endl;
|
|
|
|
return list; // already been there
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << endl;
|
|
|
|
|
|
|
|
// add to include list
|
|
|
|
includes.insert(path);
|
2017-10-08 02:28:13 +02:00
|
|
|
|
|
|
|
int fd = open(path.c_str(), O_RDONLY);
|
|
|
|
if (fd == -1) {
|
|
|
|
cerr << "cannot open " << path << endl;
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat fileinfo;
|
|
|
|
if (fstat(fd, &fileinfo) == -1) {
|
|
|
|
perror("stat");
|
|
|
|
close(fd);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
void * memptr = mmap(NULL, fileinfo.st_size, PROT_READ, MAP_SHARED, fd, 0);
|
|
|
|
|
|
|
|
if (memptr == NULL) {
|
|
|
|
perror("mmap");
|
|
|
|
close(fd);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
// Substring str((const char *)memptr, (const char*)memptr + fileinfo.st_size);
|
|
|
|
MemoryString file((char*)memptr, fileinfo.st_size);
|
2017-10-08 02:28:13 +02:00
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
std::string basedir = Path::Dir(path);
|
|
|
|
list = (*this)(basedir, file); // follow include
|
2017-10-08 03:24:20 +02:00
|
|
|
|
2017-10-08 02:28:13 +02:00
|
|
|
// cleanup
|
|
|
|
munmap(memptr, fileinfo.st_size);
|
|
|
|
close(fd);
|
|
|
|
return list;
|
2017-10-08 18:44:27 +02:00
|
|
|
|
|
|
|
cout << path << "done" << endl;
|
2017-10-08 02:28:13 +02:00
|
|
|
}
|
|
|
|
|
2017-10-08 19:03:39 +02:00
|
|
|
Path::Path fixFilename(const Path::Path &file, const Path::Path &root) {
|
|
|
|
Path::Path temp(file);
|
|
|
|
|
|
|
|
// try to make path absolute
|
|
|
|
if (Path::isRelative(temp)) {
|
|
|
|
// add current file's path
|
|
|
|
temp = Path::Join(root, temp);
|
|
|
|
}
|
|
|
|
if (Path::isRelative(temp)) {
|
|
|
|
// add process working directory
|
|
|
|
temp = Path::Join(fs::cwd(), temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Path::Clean(temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
InputExtractor::List InputExtractor::operator()(const Path::Path &file, const MemoryString &str){
|
2017-10-08 02:28:13 +02:00
|
|
|
List result;
|
|
|
|
CommandList IncludeCommands;
|
|
|
|
|
2017-10-08 19:03:39 +02:00
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
IncludeCommands["input"] = [&file, this](List &l, std::string a) {
|
2017-10-08 03:36:45 +02:00
|
|
|
if (a.empty()) return;
|
2017-10-08 18:44:27 +02:00
|
|
|
if (Path::Extension(a) != ".tex") a += ".tex";
|
2017-10-08 19:03:39 +02:00
|
|
|
a = fixFilename(a, file);
|
2017-10-08 18:44:27 +02:00
|
|
|
|
2017-10-08 02:28:13 +02:00
|
|
|
l.push_back(a);
|
|
|
|
// try to extract all inputs of that file
|
|
|
|
auto sub = Include(a);
|
2017-10-08 18:44:27 +02:00
|
|
|
if (!sub.empty())
|
|
|
|
std::copy(sub.begin(), sub.end(), std::inserter(l, l.end()));
|
2017-10-08 02:28:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
IncludeCommands["include"] = IncludeCommands["input"];
|
2017-10-08 19:04:20 +02:00
|
|
|
IncludeCommands["lstinputlisting"] = [file](List &l, std::string a){ if (!a.empty()) { l.push_back(fixFilename(a, file)); } };
|
2017-10-08 02:28:13 +02:00
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
MemoryString::const_iterator current = str.begin();
|
2017-10-08 02:28:13 +02:00
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
while(current != str.end()) {
|
|
|
|
if (*current == '%') {
|
2017-10-08 02:28:13 +02:00
|
|
|
// line commment
|
2017-10-08 18:44:27 +02:00
|
|
|
while(current != str.end() && *current != '\n')
|
|
|
|
current++;
|
2017-10-08 02:28:13 +02:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2017-10-08 18:44:27 +02:00
|
|
|
if (*current != '\\') {
|
|
|
|
// skip non macros
|
|
|
|
current++;
|
2017-10-08 02:28:13 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read macro name
|
2017-10-08 18:44:27 +02:00
|
|
|
current++;
|
2017-10-08 02:28:13 +02:00
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
if (current == str.end())
|
2017-10-08 02:28:13 +02:00
|
|
|
throw Exception("unexpected EOF");
|
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
auto start = current, end = current;
|
2017-10-08 02:28:13 +02:00
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
auto limiter = [](char c) -> bool { return isspace(c) || c == '{' || c == '\\'; };
|
|
|
|
|
|
|
|
for(; current != str.end() && !limiter(*current); end = (++current+1))
|
|
|
|
{
|
|
|
|
auto searchHit = IncludeCommands.find(std::string(start, end));
|
|
|
|
if (searchHit != IncludeCommands.end()) {
|
|
|
|
// handle crosslink
|
|
|
|
current++;
|
|
|
|
if (current == str.end()) continue;
|
|
|
|
|
|
|
|
cout << searchHit->first << "[" << readBrackets(current, str.end(), "[]") << "]";
|
|
|
|
auto inner = readBrackets(current, str.end(), "{}");
|
|
|
|
cout << ":" << inner << endl;
|
|
|
|
|
|
|
|
// add to results
|
|
|
|
searchHit->second(result, macroExpand(inner));
|
|
|
|
break;
|
|
|
|
} else if (std::string(start, end) == std::string("def")) {
|
|
|
|
// define new macro
|
|
|
|
current++;
|
|
|
|
if (current == str.end() || *current != '\\') {
|
2017-10-08 02:28:13 +02:00
|
|
|
continue;
|
|
|
|
}
|
2017-10-08 18:44:27 +02:00
|
|
|
current++;
|
2017-10-08 02:28:13 +02:00
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
std::function<bool(const MemoryString::const_iterator&)> limiter = [](const MemoryString::const_iterator &it) -> bool { return std::string("{ \t\n").find(*it) != std::string::npos; };
|
|
|
|
|
|
|
|
std::string name = readTill(current, str.end(), limiter);
|
2017-10-08 02:28:13 +02:00
|
|
|
|
|
|
|
cout << "new macro definition: " << name << endl;
|
2017-10-08 18:44:27 +02:00
|
|
|
macros.insert(std::pair<std::string, std::string>(name, readBrackets(current, str.end(), "{}")));
|
2017-10-08 02:28:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char ** args) {
|
|
|
|
// find all the files the given tex files depend on
|
|
|
|
int fd = 0;
|
|
|
|
struct stat filestat;
|
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
|
2017-10-08 02:28:13 +02:00
|
|
|
for(;argc > 1; --argc) {
|
2017-10-08 18:44:27 +02:00
|
|
|
|
|
|
|
Path::Path filename = args[argc-1];
|
|
|
|
InputExtractor parser;
|
|
|
|
|
|
|
|
InputExtractor::List list = parser.Include(filename);
|
|
|
|
|
|
|
|
// output results in makefile rule style
|
|
|
|
|
|
|
|
Path::Path outfile_name = Path::Basename(filename) + ".d";
|
|
|
|
cout << "writing dependecy rules to " << outfile_name << "...";
|
|
|
|
|
|
|
|
std::ofstream outfile(outfile_name);
|
|
|
|
|
|
|
|
if (!outfile) {
|
|
|
|
cout << "could not create file!" << endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
outfile << filename << ":";
|
|
|
|
|
|
|
|
for (auto it = list.begin(); it != list.end(); it++) {
|
|
|
|
outfile << *it << "\t\\\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << "done" << endl;
|
|
|
|
|
|
|
|
/*
|
2017-10-08 02:28:13 +02:00
|
|
|
char * filename = args[argc-1];
|
2017-10-08 18:44:27 +02:00
|
|
|
cout << "opening " << filename << "...";
|
2017-10-08 02:28:13 +02:00
|
|
|
|
|
|
|
// try to open file
|
|
|
|
fd = open(filename, O_RDONLY);
|
|
|
|
if (fd == -1) {
|
|
|
|
perror("could not open input file");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fstat(fd, &filestat);
|
|
|
|
//cout << "file size: " << filestat.st_size << endl;
|
|
|
|
|
|
|
|
// try to mmap file
|
|
|
|
void * memory_area = mmap(NULL, filestat.st_size, PROT_READ, MAP_SHARED, fd, 0);
|
|
|
|
if (memory_area == nullptr) {
|
|
|
|
perror("could not mmap the input");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
cout << "start parsing" << endl;
|
2017-10-08 02:28:13 +02:00
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
MemoryString file((char*)memory_area, filestat.st_size);
|
2017-10-08 02:28:13 +02:00
|
|
|
|
|
|
|
try {
|
2017-10-08 18:44:27 +02:00
|
|
|
InputExtractor::List list = InputExtractor()(file);
|
|
|
|
|
|
|
|
Path::Path outfilename = Path::Basename(Path::Path(filename)) + ".d";
|
|
|
|
|
|
|
|
cout << "writing makedeps file to " << outfilename << "..." << endl;
|
|
|
|
|
2017-10-08 02:28:13 +02:00
|
|
|
// write in makefile style
|
2017-10-08 18:44:27 +02:00
|
|
|
std::ofstream output(outfilename);
|
2017-10-08 02:28:13 +02:00
|
|
|
if (!output) {
|
|
|
|
std::cout << "could not create output file" << std::endl;
|
|
|
|
} else {
|
2017-10-08 03:36:45 +02:00
|
|
|
output << filename << ": ";
|
2017-10-08 03:24:20 +02:00
|
|
|
|
2017-10-08 02:28:13 +02:00
|
|
|
for (auto it = list.begin(); it != list.end(); it++) {
|
2017-10-08 18:44:27 +02:00
|
|
|
if (Path::isRelative(*it)) {
|
|
|
|
if (Path::isRelative(filename)) {
|
|
|
|
*it = Path::Join(fs::cwd(), filename, *it);
|
2017-10-08 03:24:20 +02:00
|
|
|
} else {
|
2017-10-08 18:44:27 +02:00
|
|
|
*it = Path::Join(Path::Path(filename), *it);
|
2017-10-08 03:24:20 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-08 18:44:27 +02:00
|
|
|
|
|
|
|
cout << "depends: " << *it << endl;
|
|
|
|
output << '\t' << *it << "\t\\\n";
|
2017-10-08 02:28:13 +02:00
|
|
|
}
|
|
|
|
output << endl;
|
|
|
|
}
|
|
|
|
|
2017-10-08 18:44:27 +02:00
|
|
|
cout << filename << done;
|
|
|
|
|
2017-10-08 02:28:13 +02:00
|
|
|
} catch(InputExtractor::Exception &e) {
|
|
|
|
cout << e.what() << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
munmap(memory_area, filestat.st_size);
|
|
|
|
close(fd);
|
2017-10-08 18:44:27 +02:00
|
|
|
|
|
|
|
*/
|
2017-10-08 02:28:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|