|
| 1 | +#include "cpp-terminal/args.hpp" |
| 2 | + |
| 3 | +#include "cpp-terminal/platforms/conversion.hpp" |
| 4 | + |
| 5 | +#if defined(_WIN32) |
| 6 | + #include <memory> |
| 7 | +// clang-format off |
| 8 | + #include <windows.h> |
| 9 | + #include <processenv.h> |
| 10 | +// clang-format on |
| 11 | +#elif defined(__APPLE__) |
| 12 | + #include <crt_externs.h> |
| 13 | +#else |
| 14 | + #include <algorithm> |
| 15 | + #include <fstream> |
| 16 | + #include <limits> |
| 17 | +#endif |
| 18 | + |
| 19 | +void Term::Arguments::parse() |
| 20 | +{ |
| 21 | + if(m_parsed == true) return; |
| 22 | +#if defined(_WIN32) |
| 23 | + int argc{0}; |
| 24 | + std::unique_ptr<LPWSTR[], void (*)(wchar_t**)> wargv = std::unique_ptr<LPWSTR[], void (*)(wchar_t**)>(CommandLineToArgvW(GetCommandLineW(), &argc), [](wchar_t** ptr) { LocalFree(ptr); }); |
| 25 | + if(wargv == nullptr) |
| 26 | + { |
| 27 | + m_parsed = false; |
| 28 | + return; |
| 29 | + } |
| 30 | + else |
| 31 | + { |
| 32 | + m_args.reserve(static_cast<std::size_t>(argc)); |
| 33 | + for(std::size_t i = 0; i != static_cast<std::size_t>(argc); ++i) { m_args.push_back(Term::Private::to_utf8(&wargv.get()[i][0])); } |
| 34 | + m_parsed = true; |
| 35 | + } |
| 36 | +#elif defined(__APPLE__) |
| 37 | + int argc{*_NSGetArgc()}; |
| 38 | + m_args.reserve(argc); |
| 39 | + char** argv{*_NSGetArgv()}; |
| 40 | + for(std::size_t i = 0; i != argc; ++i) { m_args.push_back(argv[i]); } |
| 41 | + m_parsed = true; |
| 42 | +#else |
| 43 | + std::string cmdline; |
| 44 | + std::fstream fs; |
| 45 | + std::fstream::iostate old_iostate{fs.exceptions()}; |
| 46 | + try |
| 47 | + { |
| 48 | + fs.exceptions(std::ifstream::failbit | std::ifstream::badbit); |
| 49 | + fs.open("/proc/self/cmdline", std::fstream::in | std::fstream::binary); |
| 50 | + fs.ignore(std::numeric_limits<std::streamsize>::max()); |
| 51 | + cmdline.resize(fs.gcount()); |
| 52 | + fs.seekg(0, std::ios_base::beg); |
| 53 | + fs.get(&cmdline[0], cmdline.size()); |
| 54 | + fs.exceptions(old_iostate); |
| 55 | + if(fs.is_open()) fs.close(); |
| 56 | + const std::size_t argc = static_cast<std::size_t>(std::count(cmdline.begin(), cmdline.end(), '\0')); |
| 57 | + m_args.reserve(argc); |
| 58 | + for(std::string::iterator it = cmdline.begin(); it != cmdline.end(); it = std::find(it, cmdline.end(), '\0') + 1) { m_args.push_back(cmdline.data() + (it - cmdline.begin())); } |
| 59 | + m_parsed = true; |
| 60 | + } |
| 61 | + catch(...) |
| 62 | + { |
| 63 | + fs.exceptions(old_iostate); |
| 64 | + if(fs.is_open()) fs.close(); |
| 65 | + m_parsed = false; |
| 66 | + m_args.clear(); |
| 67 | + m_parsed = false; |
| 68 | + } |
| 69 | +#endif |
| 70 | +} |
| 71 | + |
| 72 | +std::size_t Term::Arguments::argc() |
| 73 | +{ |
| 74 | + parse(); |
| 75 | + return m_args.size(); |
| 76 | +} |
| 77 | + |
| 78 | +std::vector<std::string> Term::Arguments::argv() |
| 79 | +{ |
| 80 | + parse(); |
| 81 | + return m_args; |
| 82 | +} |
| 83 | + |
| 84 | +bool Term::Arguments::m_parsed = false; |
| 85 | + |
| 86 | +std::vector<std::string> Term::Arguments::m_args = std::vector<std::string>(); |
0 commit comments