void CustomXmlDataWriter::writeValue(const Variant& value) { CustomXmlData data; bool isOk = value.tryCast(data); assert(isOk); if (!isOk) { stream_.setState(std::ios_base::failbit); return; } beginOpenTag("name"); endOpenTag(); stream_ << quoted(data.name_); closeTag("name"); beginOpenTag("filename"); endOpenTag(); stream_ << quoted(data.filename_); closeTag("filename"); beginOpenTag("createdBy"); endOpenTag(); stream_ << quoted(data.createdBy_); closeTag("createdBy"); beginOpenTag("visibility"); endOpenTag(); stream_ << data.visibility_; closeTag("visibility"); beginOpenTag("position"); endOpenTag(); stream_ << data.position_; closeTag("position"); }
Value Parser::parse() { switch (eats()) { case '(': read(); return list(); case '\'': read(); return quoted(sym_quote); case '`': read(); return quoted(sym_quasiquote); case ',': read(); if (c == '@') { read(); return quoted(sym_unquote_splicing); } else return quoted(sym_unquote); case ')': parse_error("too many right-parens"); case EOF: parse_error("unexpected end of file"); } return atom(); }
char *cclenter(const char *argp) /* add a character class */ { int i, c, c2; int j; uschar *p = (uschar *) argp; uschar *op, *bp; static uschar *buf = 0; static int bufsz = 100; op = p; if (buf == 0 && (buf = (uschar *) malloc(bufsz)) == NULL) FATAL("out of space for character class [%.10s...] 1", p); bp = buf; for (i = 0; (c = *p++) != 0; ) { if (c == '\\') { c = quoted(&p); } else if (c == '-' && i > 0 && bp[-1] != 0) { if (*p != 0) { c = bp[-1]; c2 = *p++; if (c2 == '\\') c2 = quoted(&p); if (collate_range_cmp(c, c2) > 0) { bp--; i--; continue; } for (j = 0; j < NCHARS; j++) { if ((collate_range_cmp(c, j) > 0) || collate_range_cmp(j, c2) > 0) continue; if (!adjbuf((char **) &buf, &bufsz, bp-buf+2, 100, (char **) &bp, "cclenter1")) FATAL("out of space for character class [%.10s...] 2", p); *bp++ = j; i++; } continue; } } if (!adjbuf((char **) &buf, &bufsz, bp-buf+2, 100, (char **) &bp, "cclenter2")) FATAL("out of space for character class [%.10s...] 3", p); *bp++ = c; i++; } *bp = 0; dprintf( ("cclenter: in = |%s|, out = |%s|\n", op, buf) ); xfree(op); return (char *) tostring((char *) buf); }
std::string quote(const std::string& str) { std::string quoted("\""); quoted += str; quoted += "\""; return quoted; }
SQRESULT tostring(HSQUIRRELVM sqvm) { native_string result("["); sqxtd::vm vm{sqvm}; try { sqxtd::array array{validate<sqxtd::array>(vm.stack.at(-1)) .with_error(_SC("array::tostring: invalid receiver of type `%s` " "(are you calling the _tostring() metamethod on " "the `array` default delegate directly?)"), vm.stack.typename_at(-1)).value()}; for (auto &object : array) { result += quoted(object).tostring(); result += ", "; } } catch (object::invalid_cast) { return SQ_ERROR; } // no commas to delete for empty array if (result.length() > 1) { result.erase(result.length()-2, result.length()); } result += "]"; vm.stack.push(result); return 1; }
SQRESULT tostring(HSQUIRRELVM sqvm) { sqxtd::vm vm{sqvm}; native_string result("{\n"); try { sqxtd::table table {validate<sqxtd::table>(vm.stack.at(-1)) .with_error(_SC("table::tostring: invalid receiver of type `%s` " "(are you calling the _tostring() metamethod on " "the `table` default delegate directly?)"), vm.stack.typename_at(-1)).value()}; for (auto &pair : table) { auto key_value = format_key_value(pair.first.tostring(), quoted(pair.second).tostring()); result += indent_string(key_value); result += "\n"; } } catch (object::invalid_cast) { return SQ_ERROR; } result += "}"; vm.stack.push(result); return 1; }
csv_line_grammar<Iterator, Skipper>::csv_line_grammar() : csv_line_grammar::base_type(line) { qi::_r1_type _r1; qi::_r2_type _r2; qi::lit_type lit; qi::char_type char_; unesc_char.add ("\\a", '\a') ("\\b", '\b') ("\\f", '\f') ("\\n", '\n') ("\\r", '\r') ("\\t", '\t') ("\\v", '\v') ("\\\\",'\\') ("\\\'", '\'') ("\\\"", '\"') ("\"\"", '\"') // double quote ; line = -lit("\r") > -lit("\n") > column(_r1, _r2) % lit(_r1) ; column = quoted(_r2) | *(char_ - lit(_r1)) ; quoted = lit(_r1) > text(_r1) > lit(_r1) // support unmatched quotes or not (??) ; text = *(unesc_char | (char_ - lit(_r1))) ; BOOST_SPIRIT_DEBUG_NODES((line)(column)(quoted)); }
tokenlist* parse(tokenlist* tl, sexp** ps) { sexp* s; token* tok = tl->tok; switch (tok->type) { case OPENPAR_TOKEN: if (tl->next == NULL) { error("Unexpected end of input"); } if (tl->next->tok->type == CLOSEPAR_TOKEN) { *ps = &nil_sexp; return tl->next->next; } tl = tl->next; s = new_sexp(PAIR_SEXP); *ps = s; while (1) { tl = parse(tl, &car(s)); if (tl == NULL) { error("Unexpected end of input"); } if (tl->tok->type == CLOSEPAR_TOKEN) { cdr(s) = &nil_sexp; return tl->next; } cdr(s) = new_sexp(PAIR_SEXP); s = cdr(s); } case CLOSEPAR_TOKEN: error("Unexpected ')'"); case SYMBOL_TOKEN: *ps = symbol_sexp(tok->symbol); return tl->next; case NUMBER_TOKEN: *ps = number_sexp(tok->number); return tl->next; case BOOLEAN_TOKEN: *ps = boolean_sexp(tok->number); return tl->next; case QUOTE_TOKEN: tl = parse(tl->next, &s); *ps = quoted(s); return tl; case QQ_TOKEN: tl = parse(tl->next, &s); *ps = quasiquoted(s); return tl; case UNQUOTE_TOKEN: tl = parse(tl->next, &s); *ps = unquoted(s); return tl; case SPLICE_TOKEN: tl = parse(tl->next, &s); *ps = unquote_spliced(s); return tl; case STRING_TOKEN: *ps = string_sexp(tok->symbol); return tl->next; } }
char *cclenter(const char *argp) /* add a character class */ { int i, c, c2; uschar *p = (uschar *) argp; uschar *op, *bp; static uschar *buf = 0; static int bufsz = 100; op = p; if (buf == 0 && (buf = (uschar *) malloc(bufsz)) == NULL) FATAL("out of space for character class [%.10s...] 1", p); bp = buf; for (i = 0; (c = *p++) != 0; ) { if (c == '\\') { c = quoted((char **) &p); } else if (c == '-' && i > 0 && bp[-1] != 0) { if (*p != 0) { c = bp[-1]; c2 = *p++; if (c2 == '\\') c2 = quoted((char **) &p); if (c > c2) { /* empty; ignore */ bp--; i--; continue; } while (c < c2) { if (!adjbuf((char **) &buf, &bufsz, bp-buf+2, 100, (char **) &bp, "cclenter1")) FATAL("out of space for character class [%.10s...] 2", p); *bp++ = ++c; i++; } continue; } } if (!adjbuf((char **) &buf, &bufsz, bp-buf+2, 100, (char **) &bp, "cclenter2")) FATAL("out of space for character class [%.10s...] 3", p); *bp++ = c; i++; } *bp = 0; dprintf( ("cclenter: in = |%s|, out = |%s|\n", op, buf) ); xfree(op); return (char *) tostring((char *) buf); }
/** handle nested comments and simple minded attributes */ void lqGvSynCol::highlightBlock(const QString &text) { QString begComm("/\\*"); QString number("\\d+(?:\\.\\d+)?"); QString symbol("\\w+"); QString quoted("\"[^\"]*\""); QString edges("--|->"); QRegExp tokens(QString("(%1)|(%2)|(%3)|(%4)|(%5)|#").arg(begComm, number, symbol, quoted, edges)); QRegExp endComm("\\*/"); // simple state machine if (currentBlock().blockNumber() > 0) setCurrentBlockState(previousBlockState()); else setCurrentBlockState(0); for (int i = 0, j, l; ; i = j + l) if (currentBlockState()) { // in multiline comment if ((j = endComm.indexIn(text, i)) == -1) { setFormat(i, text.length() - i, fmt[Comm]); break; } setFormat(i, j - i + (l = 2), fmt[Comm]); setCurrentBlockState(0); } else { if ((j = tokens.indexIn(text, i)) == -1) break; QStringList ml = tokens.capturedTexts(); Q_ASSERT(ml.length() == 5+1); if ((l = ml[1].length())) { // begin multiline comment setFormat(j, l, fmt[Comm]); setCurrentBlockState(1); } else if ((l = ml[2].length())) { // number setFormat(j, l, fmt[Number]); } else if ((l = ml[3].length())) { // symbol if (keyws.contains(ml[3])) setFormat(j, l, fmt[Keyw]); else if (attrs.contains(ml[3])) setFormat(j, l, fmt[Attr]); else setFormat(j, l, fmt[Unknown]); } else if ((l = ml[4].length())) { // quoted setFormat(j, l, fmt[Quoted]); } else if ((l = ml[5].length())) { // edge setFormat(j, l, fmt[Edge]); } else { // single line comment setFormat(j, text.length() - i, fmt[Comm]); break; } } // rather useless - anyway textChanged will fire after completion if (currentBlock().blockNumber() == document()->blockCount() - 1) emit completed(); }
object_t *eval(object_t *exp, object_t *env) { object_t *ret = NULL; if (evaluate_to_self(exp)) { ret = exp; } else if (quoted(exp)) { ret = car(cdr(exp)); } else if (definition(exp)) { object_t *symb = car(cdr(exp)), *val = car(cdr(cdr(exp))); if (val == NULL) { create_new_variable(symb, get_nil(), env); } else { create_new_variable(symb, eval(val, env), env); } ret = symb; } else if (is_symbol(exp)) { //printf("\nfound symbol: %s\n\n", exp->values.symbol.value); ret = find_variable_value(exp, env); } else if (function(exp)) { object_t *arguments = make_arguments(cdr(exp), env); object_t *func = eval(car(exp), env); if (func == NULL || func == get_nil() || func->type != t_primitive) { fprintf(stderr, "func: %d\n", (unsigned int)func); //fprintf(stderr, "type: %d\n", func->type); die("Not a primitive!\n"); } else { ret = (func->values.primitive.function)(arguments); } } else if (maybe_eval_to_function(exp)) { object_t *c = car(exp); object_t *func = eval(c, env); if (!(func == NULL || nilp(func))) { object_t *arguments = make_arguments(cdr(exp), env); ret = (func->values.primitive.function)(arguments); } else { die("Not a function!\n"); } } else { die("Can't eval!\n"); } return ret; }
inline std::ostream & operator<<(std::ostream & os, const if_not_empty & s) { if(s.value.length() > 100) { color::shell_command prev = color::current; return os << s.name << ": " << color::white << s.value.length() << prev << " bytes" << '\n'; } else if(!s.value.empty()) { return os << s.name << ": " << quoted(s.value) << '\n'; } else { return os; } }
EString ImapParser::string() { char c = nextChar(); if ( c == '"' ) return quoted(); else if ( c == '{' || c == '~' ) return literal(); setError( "Expected string, but saw: " + following() ); return ""; }
// import - prints the preamble, then loops over all files and import them one by one void SpectraSTMs2LibImporter::import() { for (vector<string>::iterator i = m_impFileNames.begin(); i != m_impFileNames.end(); i++) { string fullName(*i); makeFullPath(fullName); string quoted("\"" + fullName + "\""); string desc = m_params.constructDescrStr(quoted, ".ms2"); m_preamble.push_back(desc); } m_lib->writePreamble(m_preamble); for (vector<string>::iterator i = m_impFileNames.begin(); i != m_impFileNames.end(); i++) { readFromFile(*i); } }
/** * Quotes the given string, escaping special characters as necessary. */ QString LuaTableWriter::quote(const QString &str) { QString quoted("\""); for (const QChar c : str) { switch (c.unicode()) { case '\\': quoted.append(QLatin1String("\\\\")); break; case '"': quoted.append(QLatin1String("\\\"")); break; case '\n': quoted.append(QLatin1String("\\n")); break; default: quoted.append(c); } } quoted.append(QLatin1Char('"')); return quoted; }
void RxCompile::element() { if (match('^')) emit(&startOfLine); else if (match('$')) emit(&endOfLine); else if (match("\\A")) emit(&startOfString); else if (match("\\Z")) emit(&endOfString); else if (match("\\<")) emit(&startOfWord); else if (match("\\>")) emit(&endOfWord); else if (match("(?i)")) ignoringCase = true; else if (match("(?-i)")) ignoringCase = false; else if (match("(?q)")) quoted(); else if (match("(?-q)")) ; else { int start = pat.size(); simple(); int len = pat.size() - start; if (match("??")) insert(start, new Branch(len + 1, 1)); else if (match('?')) insert(start, new Branch(1, len + 1)); else if (match("+?")) emit(new Branch(1, -len)); else if (match('+')) emit(new Branch(-len, 1)); else if (match("*?")) { emit(new Branch(1, -len)); insert(start, new Branch(len + 2, 1)); } else if (match('*')) { emit(new Branch(-len, 1)); insert(start, new Branch(1, len + 2)); } } }
/** this is a very limited - and much bugged - approach to * highlighting Prolog syntax. * Just a quick alternative to properly interfacing SWI-Prolog goodies, * that proved much more difficult to do rightly than I foreseen */ void plMiniSyntax::setup() { QString number("\\d+(?:\\.\\d+)?"); QString symbol("[a-z][A-Za-z0-9_]*"); QString var("[A-Z_][A-Za-z0-9_]*"); QString quoted("\"[^\"]*\""); QString oper("[\\+\\-\\*\\/\\=\\^<>~:\\.,;\\?@#$\\\\&{}`]+"); tokens = QRegExp(QString("(%1)|(%2)|(%3)|(%4)|(%5)|%").arg(number, symbol, var, quoted, oper)); fmt[Comment].setForeground(Qt::darkGreen); fmt[Number].setForeground(QColor("blueviolet")); fmt[Atom].setForeground(Qt::blue); fmt[String].setForeground(Qt::magenta); fmt[Variable].setForeground(QColor("brown")); fmt[Operator].setFontWeight(QFont::Bold); fmt[Unknown].setForeground(Qt::darkRed); }
std::string IFunction::Attribute::asQuotedString()const { std::string attr; try { attr = boost::get<std::string>(m_data); } catch(...) { throw std::runtime_error("Trying to access a "+type()+" attribute " "as string"); } std::string quoted(attr); if( *(attr.begin()) != '\"' ) quoted = "\"" + attr; if( *(quoted.end() - 1) != '\"' ) quoted += "\""; return quoted; }
void tst_qregexp::escape_new4() { QFETCH(QString, pattern); QFETCH(QString, expected); QBENCHMARK { const int n = pattern.size(); const QChar *patternData = pattern.data(); // try to prevent copy if no escape is needed int i = 0; for (int i = 0; i != n; ++i) { const QChar c = patternData[i]; if (needsEscaping(c.unicode())) break; } if (i == n) { verify(pattern, expected); // no escaping needed, "return pattern" should be done here. return; } const QLatin1Char backslash('\\'); QString quoted(n * 2, backslash); QChar *quotedData = quoted.data(); for (int j = 0; j != i; ++j) *quotedData++ = *patternData++; int escaped = 0; for (; i != n; ++i) { const QChar c = *patternData; if (needsEscaping(c.unicode())) { ++escaped; ++quotedData; } *quotedData = c; ++quotedData; ++patternData; } quoted.resize(n + escaped); verify(quoted, expected); // "return quoted" } }
void tst_qregexp::escape_new2() { QFETCH(QString, pattern); QFETCH(QString, expected); QBENCHMARK { int count = pattern.count(); const QLatin1Char backslash('\\'); QString quoted(count * 2, backslash); const QChar *patternData = pattern.data(); QChar *quotedData = quoted.data(); int escaped = 0; for ( ; --count >= 0; ++patternData) { const QChar c = *patternData; switch (c.unicode()) { case '$': case '(': case ')': case '*': case '+': case '.': case '?': case '[': case '\\': case ']': case '^': case '{': case '|': case '}': ++escaped; ++quotedData; } *quotedData = c; ++quotedData; } quoted.resize(pattern.size() + escaped); verify(quoted, expected); } }
bool option_parser::run(std::istream &is, bool ignore_errors) { // quoted value. leftmost and rightmost quotation marks are the delimiters. boost::regex quoted("(\\w+)\\h*=\\h*\"(.*)\"[^\"]*"); // unquoted value. whitespaces get trimmed. boost::regex unquoted("(\\w+)\\h*=\\h*(.*?)\\h*"); boost::smatch match; std::string line; while (std::getline(is, line)) { if (boost::regex_match(line, match, quoted) || boost::regex_match(line, match, unquoted)) { std::string option = match[1]; auto it = m_parsers.find(option); if (it != m_parsers.end()) { try { it->second.parse(match[2]); } catch (std::exception &e) { std::cerr << "Error while processing option \"" << option << "\": " << e.what() << "\n"; if (!ignore_errors) return false; } } else { std::cerr << "Unknown option: " << option << "\n"; if (!ignore_errors) return false; } } } return true; }
string load_string( const string& filename ) { #if 0 // read file contents into char array FILE* f = fopen( filename.c_str(), "rb" ); flut_error_if( f == NULL, "Could not open " + quoted( filename ) ); fseek( f, 0, SEEK_END ); int file_len = ftell( f ); rewind( f ); string s( file_len, '\0' ); fread( reinterpret_cast< void* >( &s[ 0 ] ), sizeof( char ), file_len, f ); return s; #else // this method uses a stringbuf, which may be slower but more stable std::ifstream ifstr( filename ); std::stringstream buf; buf << ifstr.rdbuf(); return buf.str(); #endif }
void SQLSyntaxHighligter::highlightBlock(const QString &text) { highlightBlockHelper(text, m_keyword_list, m_sql_keyword_format); highlightBlockHelper(text, m_function_list, m_sql_function_format); foreach(QString command, m_commant_list) { if(text.startsWith(command, Qt::CaseInsensitive)) setFormat(0, command.length(), m_sql_command_format); } if (m_database) { QStringList tables = m_database->tables(QSql::AllTables); QStringList foundTables = highlightBlockHelper(text, tables, m_sql_table_format); if (!m_table.isEmpty()) foundTables << m_table; QStringList columns; foreach(QString oneTable, foundTables) { QSqlRecord record = m_database->record(oneTable); for (int i = 0; i < record.count(); ++i) { columns << record.fieldName(i); } } highlightBlockHelper(text, columns, m_sql_column_format); { // highlight quote QRegExp quoted("\"[^\"]*\""); int pos = -1; while ((pos = text.indexOf(quoted, pos+1)) >= 0) { setFormat(pos, quoted.matchedLength(), m_sql_quoted_format); pos += quoted.matchedLength(); } } }
std::string Value::iterateFormatted(const Value& value, const int indent) { std::stringstream stream; if (value.size() > 0) { // Stream a table begin if (indent > 0) { stream << "{\n"; } unsigned count = 0; for (auto& pair : value) { /// pair.first is the key, which is a string /// pair.second is the value, which may be a table // Set the initial indent stream << indented(indent); // Stream the key stream << pair.first << " = "; // Process any children this table has stream << iterateFormatted(pair.second, indent + 1); // Commas are required between elements, but not after the last one if (indent > 0 && count < value.size() - 1) { stream << ", "; } stream << "\n"; ++count; } // Stream a table end if (indent > 0) { stream << indented(indent - 1); stream << "}"; } } else { switch (value.type) { case Boolean: stream << std::boolalpha << value.b; break; case Number: stream << value.n; break; case String: stream << quoted(value.s); break; default: break; // wrong // also could be an empty table } } return stream.str(); }
/* Compile a pattern */ void *compre(char *pat) { int i, j, inclass; char c, *p, *s; Reprog *program; if (!compile_time) { /* search cache for dynamic pattern */ for (i = 0; i < npats; i++) if (!strcmp(pat, pattern[i].re)) { pattern[i].use++; return((void *) pattern[i].program); } } /* Preprocess Pattern for compilation */ p = re; s = pat; inclass = 0; while (c = *s++) { if (c == '\\') { quoted(&s, &p, re+MAXRE); continue; } else if (!inclass && c == '(' && *s == ')') { if (p < re+MAXRE-2) { /* '()' -> '[]*' */ *p++ = '['; *p++ = ']'; c = '*'; s++; } else overflow(); } else if (c == '['){ /* '[-' -> '[\-' */ inclass = 1; if (*s == '-') { if (p < re+MAXRE-2) { *p++ = '['; *p++ = '\\'; c = *s++; } else overflow(); } /* '[^-' -> '[^\-'*/ else if (*s == '^' && s[1] == '-'){ if (p < re+MAXRE-3) { *p++ = '['; *p++ = *s++; *p++ = '\\'; c = *s++; } else overflow(); } else if (*s == '['){ /* skip '[[' */ if (p < re+MAXRE-1) *p++ = c; else overflow(); c = *s++; } else if (*s == '^' && s[1] == '[') { /* skip '[^['*/ if (p < re+MAXRE-2) { *p++ = c; *p++ = *s++; c = *s++; } else overflow(); } else if (*s == ']') { /* '[]' -> '[]*' */ if (p < re+MAXRE-2) { *p++ = c; *p++ = *s++; c = '*'; inclass = 0; } else overflow(); } } else if (c == '-' && *s == ']') { /* '-]' -> '\-]' */ if (p < re+MAXRE-1) *p++ = '\\'; else overflow(); } else if (c == ']') inclass = 0; if (p < re+MAXRE-1) *p++ = c; else overflow(); } *p = 0; program = regcomp(re); /* compile pattern */ if (!compile_time) { if (npats < NPATS) /* Room in cache */ i = npats++; else { /* Throw out least used */ int use = pattern[0].use; i = 0; for (j = 1; j < NPATS; j++) { if (pattern[j].use < use) { use = pattern[j].use; i = j; } } xfree(pattern[i].program); xfree(pattern[i].re); } pattern[i].re = tostring(pat); pattern[i].program = program; pattern[i].use = 1; } return((void *) program); }
int relex(void) /* lexical analyzer for reparse */ { int c, n; int cflag; static uschar *buf = 0; static int bufsz = 100; uschar *bp; struct charclass *cc; int i; switch (c = *prestr++) { case '|': return OR; case '*': return STAR; case '+': return PLUS; case '?': return QUEST; case '.': return DOT; case '\0': prestr--; return '\0'; case '^': case '$': case '(': case ')': return c; case '\\': rlxval = quoted((char **) &prestr); return CHAR; default: rlxval = c; return CHAR; case '[': if (buf == 0 && (buf = (uschar *) malloc(bufsz)) == NULL) FATAL("out of space in reg expr %.10s..", lastre); bp = buf; if (*prestr == '^') { cflag = 1; prestr++; } else cflag = 0; n = 2 * strlen((const char *) prestr)+1; if (!adjbuf((char **) &buf, &bufsz, n, n, (char **) &bp, "relex1")) FATAL("out of space for reg expr %.10s...", lastre); for (; ; ) { if ((c = *prestr++) == '\\') { *bp++ = '\\'; if ((c = *prestr++) == '\0') FATAL("nonterminated character class %.20s...", lastre); *bp++ = c; /* } else if (c == '\n') { */ /* FATAL("newline in character class %.20s...", lastre); */ } else if (c == '[' && *prestr == ':') { /* POSIX char class names, Dag-Erling Smorgrav, [email protected] */ for (cc = charclasses; cc->cc_name; cc++) if (strncmp((const char *) prestr + 1, (const char *) cc->cc_name, cc->cc_namelen) == 0) break; if (cc->cc_name != NULL && prestr[1 + cc->cc_namelen] == ':' && prestr[2 + cc->cc_namelen] == ']') { prestr += cc->cc_namelen + 3; for (i = 0; i < NCHARS; i++) { if (!adjbuf((char **) &buf, &bufsz, bp-buf+1, 100, (char **) &bp, "relex2")) FATAL("out of space for reg expr %.10s...", lastre); if (cc->cc_func(i)) { *bp++ = i; n++; } } } else *bp++ = c; } else if (c == '\0') { FATAL("nonterminated character class %.20s", lastre); } else if (bp == buf) { /* 1st char is special */ *bp++ = c; } else if (c == ']') { *bp++ = 0; rlxstr = (uschar *) tostring((char *) buf); if (cflag == 0) return CCL; else return NCCL; } else *bp++ = c; } } }
int run(int argc, char **argv, int is_gui) { char python[256]; /* python executable's filename*/ char *pyopt; /* Python option */ char script[256]; /* the script's filename */ int scriptf; /* file descriptor for script file */ char **newargs, **newargsp, **parsedargs; /* argument array for exec */ char *ptr, *end; /* working pointers for string manipulation */ char *cmdline; int i, parsedargc; /* loop counter */ /* compute script name from our .exe name*/ GetModuleFileNameA(NULL, script, sizeof(script)); end = script + strlen(script); while( end>script && *end != '.') *end-- = '\0'; *end-- = '\0'; strcat(script, (GUI ? "-script.pyw" : "-script.py")); /* figure out the target python executable */ scriptf = open(script, O_RDONLY); if (scriptf == -1) { return fail("Cannot open %s\n", script); } end = python + read(scriptf, python, sizeof(python)); close(scriptf); ptr = python-1; while(++ptr < end && *ptr && *ptr!='\n' && *ptr!='\r') {;} *ptr-- = '\0'; if (strncmp(python, "#!", 2)) { /* default to python.exe if no #! header */ strcpy(python, "#!python.exe"); } parsedargs = parse_argv(python+2, &parsedargc); /* Using spawnv() can fail strangely if you e.g. find the Cygwin Python, so we'll make sure Windows can find and load it */ ptr = find_exe(parsedargs[0], script); if (!ptr) { return fail("Cannot find Python executable %s\n", parsedargs[0]); } /* printf("Python executable: %s\n", ptr); */ /* Argument array needs to be parsedargc + argc, plus 1 for null sentinel */ newargs = (char **)calloc(parsedargc + argc + 1, sizeof(char *)); newargsp = newargs; *newargsp++ = quoted(ptr); for (i = 1; i<parsedargc; i++) *newargsp++ = quoted(parsedargs[i]); *newargsp++ = quoted(script); for (i = 1; i < argc; i++) *newargsp++ = quoted(argv[i]); *newargsp++ = NULL; /* printf("args 0: %s\nargs 1: %s\n", newargs[0], newargs[1]); */ if (is_gui) { /* Use exec, we don't need to wait for the GUI to finish */ execv(ptr, (const char * const *)(newargs)); return fail("Could not exec %s", ptr); /* shouldn't get here! */ } /* * distribute-issue207: using CreateProcessA instead of spawnv */ cmdline = join_executable_and_args(ptr, newargs, parsedargc + argc); return create_and_wait_for_subprocess(cmdline); }
TextStream& operator<<(TextStream& stream, const Base& v) { stream << v.i << ' ' << v.f << ' ' << quoted( v.s ); return stream; }
int relex(void) /* lexical analyzer for reparse */ { int c, n; int cflag; static char *buf = 0; static int bufsz = 100; char *bp; switch (c = *prestr++) { case '|': return OR; case '*': return STAR; case '+': return PLUS; case '?': return QUEST; case '.': return DOT; case '\0': prestr--; return '\0'; case '^': case '$': case '(': case ')': return c; case '\\': rlxval = quoted(&prestr); return CHAR; default: rlxval = c; return CHAR; case '[': if (buf == 0 && (buf = (char *) malloc(bufsz)) == NULL) ERROR "out of space in reg expr %.10s..", lastre FATAL; bp = buf; if (*prestr == '^') { cflag = 1; prestr++; } else cflag = 0; n = 2 * strlen(prestr)+1; if (!adjbuf(&buf, &bufsz, n, n, &bp, 0)) ERROR "out of space for reg expr %.10s...", lastre FATAL; for (; ; ) { if ((c = *prestr++) == '\\') { *bp++ = '\\'; if ((c = *prestr++) == '\0') ERROR "nonterminated character class %.20s...", lastre FATAL; *bp++ = c; } else if (c == '\n') { ERROR "newline in character class %.20s...", lastre FATAL; } else if (c == '\0') { ERROR "nonterminated character class %.20s", lastre FATAL; } else if (bp == buf) { /* 1st char is special */ *bp++ = c; } else if (c == ']') { *bp++ = 0; rlxstr = tostring(buf); if (cflag == 0) return CCL; else return NCCL; } else *bp++ = c; } } }
/*! * \brief 二重引用符で囲まれた文字列を展開する。 */ std::string expand_quote( std::string const& str ) { // boost-1.35.x以上対策 std::string::size_type n = str.find_first_not_of( " \t\r\n" ); std::string quoted( str, n ); if ( quoted.size() < 2 || quoted[0] != '"' || quoted[quoted.size() - 1] != '"' ) { throw std::invalid_argument( "argument is not quoted" ); } std::string result; for ( std::string::const_iterator iter( quoted.begin() + 1 ), last( quoted.end() - 1 ); iter != last; ++iter ) { if ( *iter == '\\' ) { if ( ++iter == last ) { throw std::invalid_argument( "argument is not quoted" ); } char c = *iter; switch ( c ) { case 'a': c = '\a'; break; case 'b': c = '\b'; break; case 'f': c = '\f'; break; case 'n': c = '\n'; break; case 'r': c = '\r'; break; case 't': c = '\t'; break; case 'v': c = '\v'; break; case 'x': if ( std::isxdigit( static_cast< unsigned char >( *iter ) ) ) { c = 0; for ( std::string::const_iterator bound( iter + 2 ); iter != bound && std::isxdigit( static_cast< unsigned char >( *iter ) ); ++iter ) { c <<= 4; int t = std::tolower( static_cast< unsigned char >( *iter ) ); static char const xdigits[] = "0123456789abcdef"; c += std::strchr( xdigits, t ) - xdigits; } } break; default: if ( '0' <= c && c <= '7' ) // '\ooo' { c = 0; for ( std::string::const_iterator bound( iter + 3 ); iter != bound && ( '0' <= *iter && *iter <= '7' ); ++iter ) { c = ( c << 3 ) + *iter - '0'; } } // 国際文字名(\uhhhh, \Uhhhhhhhh)未対応 // 二文字表記(<:等)未対応 // 三文字表記(??/等)未対応 break; } result.push_back( c ); } else { result.push_back( *iter ); } } return result; }