#include #include #include "stdlib/kumirstdlib.hpp" #include "vm/variant.hpp" #include "vm/vm_bytecode.hpp" #include "vm/vm.hpp" #if !defined(WIN32) && !defined(_WIN32) #define LOCALE Kumir::UTF8 #else #define LOCALE Kumir::CP866 #endif int usage(const char * programName) { using namespace Kumir; bool russianLanguage = false; #if defined(WIN32) || defined(_WIN32) russianLanguage = true; Char _n = Char('\r\n'); #else char * env = getenv("LANG"); if (env) { std::string LANG(env); russianLanguage = LANG.find("ru")!=std::string::npos; } Char _n = Char('\n'); #endif String message; if (russianLanguage) { message = Core::fromUtf8("Вызов:"); message.push_back(_n); message += Core::fromUtf8("\t")+Core::fromUtf8(std::string(programName)); message += Core::fromUtf8(" [-ansi] ИМЯФАЙЛА.kod [ПАРАМ1 [ПАРАМ2 ... [ПАРАМn]]]"); message.push_back(_n); message.push_back(_n); message += Core::fromUtf8("\t-ansi\t\tИспользовть кодировку 1251 вместо 866 в терминале (только для Windows)"); message.push_back(_n); message += Core::fromUtf8("\tИМЯФАЙЛА.kod\tИмя выполнеяемой программы)"); message.push_back(_n); message += Core::fromUtf8("\tПАРАМ1...ПАРАМn\tАргументы главного алгоритма Кумир-программы"); message.push_back(_n); } else { message = Core::fromUtf8("Usage:"); message.push_back(_n); message += Core::fromUtf8("\t")+Core::fromUtf8(std::string(programName)); message += Core::fromUtf8(" [-ansi] FILENAME.kod [ARG1 [ARG2 ... [ARGn]]]"); message.push_back(_n); message.push_back(_n); message += Core::fromUtf8("\t-ansi\t\tUse codepage 1251 instead of 866 in console (Windows only)"); message.push_back(_n); message += Core::fromUtf8("\tFILENAME.kod\tKumir runtime file name)"); message.push_back(_n); message += Core::fromUtf8("\tARG1...ARGn\tKumir program main algorithm arguments"); message.push_back(_n); } std::cerr << Coder::encode(LOCALE, message); return 127; } class InteractionHandler : public VM::AbstractInteractionHandler { public: explicit InteractionHandler(int argc, char *argv[]); bool makeInputArgument(VM::Variable & reference); bool makeOutputArgument(const VM::Variable & reference); private: void output(const Kumir::String & s); inline void output(const std::string & s) { output(Kumir::Core::fromAscii(s)); } bool readScalarArgument(const Kumir::String & message, const Kumir::String & name, VM::ValueType type, VM::AnyValue & val); std::deque< Kumir::String > m_arguments; size_t i_currentArgument; }; InteractionHandler::InteractionHandler(int argc, char *argv[]) { i_currentArgument = 0; bool argumentsScope = false; for (int i=1; i it is a program, // so next arg is a program argument argumentsScope = true; } } else { m_arguments.push_back(Kumir::Coder::decode(LOCALE, arg)); } } } #define IS_HEX(x) ( (x>='0' && x<='9') || (x>='A' && x<='F') || (x>='a' && x<='f') ) Kumir::String decodeHttpStringValue(const std::string & s) { Kumir::String result; size_t cpos = 0; std::string utf8string; utf8string.reserve(s.length()); while (cposAn error occured on server\n"; std::cout << "\n"; std::cout << localMessage << std::endl; std::cout << "\n"; return 0; } } bool InteractionHandler::readScalarArgument(const Kumir::String &message, const Kumir::String &name, VM::ValueType type, VM::AnyValue &val) { Kumir::IO::InputStream stream; bool foundValue = false; #if !defined(WIN32) && !defined(_WIN32) char * REQUEST_METHOD = getenv("REQUEST_METHOD"); char * QUERY_STRING = getenv("QUERY_STRING"); if (REQUEST_METHOD && std::string(REQUEST_METHOD)==std::string("GET") && QUERY_STRING) { Kumir::String query_string = decodeHttpStringValue(std::string(QUERY_STRING)); Kumir::StringList pairs = Kumir::Core::splitString(query_string, Kumir::Char('&'), true); for (size_t i=0; i args; bool forceTextForm = false; for (int i=1; i0) { static const Kumir::String RUNTIME_ERROR = Kumir::Core::fromUtf8("ОШИБКА ВЫПОЛНЕНИЯ: "); static const Kumir::String RUNTIME_ERROR_AT = Kumir::Core::fromUtf8("ОШИБКА ВЫПОЛНЕНИЯ В СТРОКЕ "); static const Kumir::String COLON = Kumir::Core::fromAscii(": "); Kumir::String message; if (vm.effectiveLineNo()!=-1) { message = RUNTIME_ERROR_AT+ Kumir::Converter::sprintfInt(vm.effectiveLineNo()+1,10,0,0)+ COLON+ vm.error(); } else { message = RUNTIME_ERROR + vm.error(); } return showErrorMessage(message, 10); return 10; } } return 0; }