// cl fix_texmfcnf.cpp /EHcs /link /MANIFESTUAC:"level='requireAdministrator' uiAccess='false'" /manifest:embed #include #include #include #include #include #include #include using namespace std; #define popen _popen #define pclose _pclose vector get_texmfcnf(){ FILE *fp = popen("kpsewhich --all texmf.cnf","r"); if(!fp)throw(string("kpsewhichの実行に失敗")); char buf[1024]; vector files; while(fgets(buf,1024,fp)){ if(*buf && buf[strlen(buf) - 1] == '\n')buf[strlen(buf) - 1] = '\0'; files.push_back(buf); } pclose(fp); return files; } vector split(const string &str,char delim){ vector rv; stringstream stream(str); string buf; while(getline(stream,buf,delim))rv.push_back(buf); return rv; } void trim(string &str){ while(str[0] == ' ')str.erase(str.begin()); while(str != "" && str[str.length() - 1] == ' ')str.erase(str.begin() + str.length() - 1); } void copy(const string &from,const string &to){ ifstream fin(from.c_str(),ios::binary); if(fin.fail())throw(from + " が開けませんでした."); ofstream fout(to.c_str(),ios::binary); if(fout.fail())throw(to + " が開けませんでした."); char buf[1024]; while(!fin.eof()){ fin.read(buf,sizeof(char)*1024); fout.write(buf,fin.gcount()); } } int main(int argc,char *argv[]){ try{ vector texmfcnfs = get_texmfcnf(); bool changed = false; bool found = false; for(vector::iterator ite = texmfcnfs.begin() ; ite != texmfcnfs.end() ; ++ite){ string texmfcnf = *ite; string output = ""; ifstream fp(texmfcnf.c_str()); if(fp.fail()){ throw(texmfcnf + " が開けませんでした."); } string line; while(getline(fp,line)){ string::size_type r = line.find("shell_escape_commands"); if(r != string::npos){ r += string("shell_escape_commands").length(); while(line[r] == ' ')++r; if(line[r] == '='){ found = true; ++r; while(line[r] == ' ')++r; output += line.substr(0,r); line = line.substr(r); do{ bool cont = false; if(line != "" && line[line.length() - 1] == '\\')cont = true; vector programs = split(line,','); bool first = true; for(vector::iterator ite2 = programs.begin() ; ite2 != programs.end() ; ++ite2){ string program = *ite2; string p = program; trim(p); if(p != "mpost" && p != "pmpost" && p != "upmpost"){ if(first)output += program; else output += "," + program; first = false; }else changed = true; } output += "\n"; if(!cont)break; }while(getline(fp,line)); }else output += line + "\n"; }else output += line + "\n"; } fp.close(); if(changed){ copy(texmfcnf,texmfcnf + ".bak"); ofstream fp(texmfcnf.c_str()); if(fp.fail())throw(texmfcnf + " が開けませんでした."); fp << output << endl; fp.close(); cout << texmfcnf << " における shell_escape_commands から mpost, pmpost, upmpost を削除しました." << endl; break; }else{ if(found){ cout << texmfcnf << " に shell_escape_commands を見つけましたが, mpost, pmpost, upmpost は含まれていないようです." << endl; break; } } } } catch(string s){ cout << s << endl; } cout << "何かキーを押してください." << endl; getchar(); return 0; }