Example #1
0
	torrent_handle add_magnet_uri(session& ses, std::string const& uri
		, add_torrent_params p, error_code& ec)
	{
		std::string name;
		std::string tracker;

		error_code e;
		boost::optional<std::string> display_name = url_has_argument(uri, "dn");
		if (display_name) name = unescape_string(display_name->c_str(), e);
		boost::optional<std::string> tracker_string = url_has_argument(uri, "tr");
		if (tracker_string) tracker = unescape_string(tracker_string->c_str(), e);
	
		boost::optional<std::string> btih = url_has_argument(uri, "xt");
		if (!btih)
		{
			ec = error_code(errors::missing_info_hash_in_uri, libtorrent_category);
			return torrent_handle();
		}

		if (btih->compare(0, 9, "urn:btih:") != 0)
		{
			ec = error_code(errors::missing_info_hash_in_uri, libtorrent_category);
			return torrent_handle();
		}

		sha1_hash info_hash;
		if (btih->size() == 40 + 9) from_hex(&(*btih)[9], 40, (char*)&info_hash[0]);
		else info_hash.assign(base32decode(btih->substr(9)));

		if (!tracker.empty()) p.tracker_url = tracker.c_str();
		p.info_hash = info_hash;
		if (!name.empty()) p.name = name.c_str();
		return ses.add_torrent(p, ec);
	}
Example #2
0
	torrent_handle add_magnet_uri(session& ses, std::string const& uri
		, std::string const& save_path
		, storage_mode_t storage_mode
		, bool paused
		, storage_constructor_type sc
		, void* userdata)
	{
		std::string name;
		std::string tracker;

		error_code ec;
		std::string display_name = url_has_argument(uri, "dn");
		if (!display_name.empty()) name = unescape_string(display_name.c_str(), ec);
		std::string tracker_string = url_has_argument(uri, "tr");
		if (!tracker_string.empty()) tracker = unescape_string(tracker_string.c_str(), ec);
	
		std::string btih = url_has_argument(uri, "xt");
		if (btih.empty()) return torrent_handle();

		if (btih.compare(0, 9, "urn:btih:") != 0) return torrent_handle();

		sha1_hash info_hash;
		if (btih.size() == 40 + 9) from_hex(&btih[9], 40, (char*)&info_hash[0]);
		else info_hash.assign(base32decode(btih.substr(9)));

		return ses.add_torrent(tracker.empty() ? 0 : tracker.c_str(), info_hash
			, name.empty() ? 0 : name.c_str(), save_path, entry()
			, storage_mode, paused, sc, userdata);
	}
Example #3
0
	torrent_handle add_magnet_uri(session& ses, std::string const& uri
		, fs::path const& save_path
		, storage_mode_t storage_mode
		, bool paused
		, storage_constructor_type sc
		, void* userdata)
	{
		std::string name;
		std::string tracker;

		boost::optional<std::string> display_name = url_has_argument(uri, "dn");
		if (display_name) name = unescape_string(display_name->c_str());
		boost::optional<std::string> tracker_string = url_has_argument(uri, "tr");
		if (tracker_string) tracker = unescape_string(tracker_string->c_str());
	
		boost::optional<std::string> btih = url_has_argument(uri, "xt");
		if (!btih) return torrent_handle();

		if (btih->compare(0, 9, "urn:btih:") != 0) return torrent_handle();

		sha1_hash info_hash;
		if (btih->size() == 40 + 9) info_hash = boost::lexical_cast<sha1_hash>(btih->substr(9));
		else info_hash.assign(base32decode(btih->substr(9)));

		return ses.add_torrent(tracker.empty() ? 0 : tracker.c_str(), info_hash
			, name.empty() ? 0 : name.c_str(), save_path, entry()
			, storage_mode, paused, sc, userdata);
	}
Example #4
0
	torrent_handle add_magnet_uri(session& ses, std::string const& uri
		, add_torrent_params p)
	{
		std::string name;
		std::string tracker;

		boost::optional<std::string> display_name = url_has_argument(uri, "dn");
		if (display_name) name = unescape_string(display_name->c_str());
		boost::optional<std::string> tracker_string = url_has_argument(uri, "tr");
		if (tracker_string) tracker = unescape_string(tracker_string->c_str());
	
		boost::optional<std::string> btih = url_has_argument(uri, "xt");
		if (!btih) return torrent_handle();

		if (btih->compare(0, 9, "urn:btih:") != 0) return torrent_handle();

		sha1_hash info_hash;
		if (btih->size() == 40 + 9) info_hash = boost::lexical_cast<sha1_hash>(btih->substr(9));
		else info_hash.assign(base32decode(btih->substr(9)));

		if (!tracker.empty()) p.tracker_url = tracker.c_str();
		p.info_hash = info_hash;
		if (!name.empty()) p.name = name.c_str();
		return ses.add_torrent(p);
	}
Example #5
0
	torrent_handle add_magnet_uri(session& ses, std::string const& uri
		, std::string const& save_path
		, storage_mode_t storage_mode
		, bool paused
		, storage_constructor_type sc
		, void* userdata)
	{
		add_torrent_params params(sc);
		params.storage_mode = storage_mode;
		params.userdata = userdata;
		params.save_path = save_path;

		if (paused) params.flags |= add_torrent_params::flag_paused;
		else params.flags &= ~add_torrent_params::flag_paused;

		error_code ec;
		std::string display_name = url_has_argument(uri, "dn");
		if (!display_name.empty()) params.name = unescape_string(display_name.c_str(), ec);
		std::string tracker_string = url_has_argument(uri, "tr");
		if (!tracker_string.empty()) params.trackers.push_back(unescape_string(tracker_string.c_str(), ec));
	
		std::string btih = url_has_argument(uri, "xt");
		if (btih.empty()) return torrent_handle();

		if (btih.compare(0, 9, "urn:btih:") != 0) return torrent_handle();

		if (btih.size() == 40 + 9) from_hex(&btih[9], 40, (char*)&params.info_hash[0]);
		else params.info_hash.assign(base32decode(btih.substr(9)));

		return ses.add_torrent(params);
	}
Example #6
0
TokenizerT *TKCreate(char *separators, char *ts) {
	
	/*
	 * Description: creates a new tokenizer struct from the token stream and delimiters
	 * Parameters: set of delimiters, token stream
	 * Modifies: nothing
	 * Returns: a pointer to a tokenizer struct on success, a null pointer on failure
	 *
	 */
    
	if(separators == NULL || ts == NULL){
		return NULL;
	}
	
	TokenizerT* tokenizer = (TokenizerT*)malloc(sizeof(TokenizerT));
	
	if(tokenizer == NULL){
		return NULL;
	}
	
	tokenizer->delimiters = unescape_string(separators);
	tokenizer->copied_string = unescape_string(ts);
	tokenizer->current_position = tokenizer->copied_string;
	
	return tokenizer;
}
void convert_string_literal(
  const std::string &src,
  std::string &dest)
{
  dest="";

  assert(src.size()>=2);
  assert(src[0]=='"' || src[0]=='L');
  assert(src[src.size()-1]=='"');

  if(src[0]=='L')
    unescape_string(std::string(src, 2, src.size()-3), dest);
  else
    unescape_string(std::string(src, 1, src.size()-2), dest);
}
Example #8
0
const string_ref *
hstcpcli::get_next_row()
{
  if (num_flds == 0) {
    DBG(fprintf(stderr, "GNR NF 0\n"));
    return 0;
  }
  if (flds.size() < num_flds) {
    flds.resize(num_flds);
  }
  char *start = readbuf.begin() + cur_row_offset;
  char *const finish = readbuf.begin() + response_end_offset - 1;
  if (start >= finish) { /* start[0] == nl */
    DBG(fprintf(stderr, "GNR FIN 0 %p %p\n", start, finish));
    return 0;
  }
  for (size_t i = 0; i < num_flds; ++i) {
    skip_one(start, finish);
    char *const fld_begin = start;
    read_token(start, finish);
    char *const fld_end = start;
    char *wp = fld_begin;
    if (is_null_expression(fld_begin, fld_end)) {
      /* null */
      flds[i] = string_ref();
    } else {
      unescape_string(wp, fld_begin, fld_end); /* in-place */
      flds[i] = string_ref(fld_begin, wp);
    }
  }
  cur_row_offset = start - readbuf.begin();
  return &flds[0];
}
Example #9
0
const string_ref *
hstcpcli::get_next_row_from_result(hstresult& result)
{
  if (result.num_flds == 0 || result.flds.elements < result.num_flds) {
    DBG(fprintf(stderr, "GNR NF 0\n"));
    return 0;
  }
  char *start = result.readbuf.begin() + result.cur_row_offset;
  char *const finish = result.readbuf.begin() + result.response_end_offset - 1;
  if (start >= finish) { /* start[0] == nl */
    DBG(fprintf(stderr, "GNR FIN 0 %p %p\n", start, finish));
    return 0;
  }
  for (size_t i = 0; i < result.num_flds; ++i) {
    skip_one(start, finish);
    char *const fld_begin = start;
    read_token(start, finish);
    char *const fld_end = start;
    char *wp = fld_begin;
    if (is_null_expression(fld_begin, fld_end)) {
      /* null */
      ((string_ref *) result.flds.buffer)[i] = string_ref();
    } else {
      unescape_string(wp, fld_begin, fld_end); /* in-place */
      ((string_ref *) result.flds.buffer)[i] = string_ref(fld_begin, wp);
    }
  }
  result.cur_row_offset = start - result.readbuf.begin();
  return (string_ref *) result.flds.buffer;
}
Example #10
0
void history_t::add_with_file_detection(const wcstring &str)
{
    ASSERT_IS_MAIN_THREAD();
    path_list_t potential_paths;
    
    tokenizer tokenizer;
    for( tok_init( &tokenizer, str.c_str(), TOK_SQUASH_ERRORS );
        tok_has_next( &tokenizer );
        tok_next( &tokenizer ) )
    {
        int type = tok_last_type( &tokenizer );
        if (type == TOK_STRING) {
            const wchar_t *token_cstr = tok_last(&tokenizer);
            if (token_cstr) {
                wcstring potential_path = token_cstr;
                if (unescape_string(potential_path, false) && string_could_be_path(potential_path)) {
                    potential_paths.push_front(potential_path);
                }
            }
        }
    }
    tok_destroy(&tokenizer);
    
    if (! potential_paths.empty()) {
        /* We have some paths. Make a context. */
        file_detection_context_t *context = new file_detection_context_t(this, str);
                        
        /* Store the potential paths. Reverse them to put them in the same order as in the command. */
        potential_paths.reverse();
        context->potential_paths.swap(potential_paths);
        iothread_perform(threaded_perform_file_detection, perform_file_detection_done, context);
    }
}
Example #11
0
static wcstring expand_unescape_string(const wcstring &in, int escape_special)
{
    wcstring tmp = in;
    unescape_string(tmp, escape_special);
    /* Need to detect error here */
    return tmp;
}
Example #12
0
static void init_prop_string_with_escape(GList ** config_list, void *pointer_to_var, gchar * name_of_var, gchar * default_value)
{
	*config_list = make_config_list_item(*config_list, pointer_to_var, 'e', name_of_var, 0);
	if (*(gchar **) pointer_to_var == NULL && default_value) {
		*(gchar **) pointer_to_var = unescape_string(default_value, FALSE);
	}
	DEBUG_MSG("init_prop_string, name_of_var=%s, default_value=%s\n", name_of_var, default_value);
}
Example #13
0
void preprocessor_line(
  const char *text,
  unsigned &line_no,
  irep_idt &file_name)
{
  const char *ptr=text;
  std::string line_number;
  
  // skip WS
  while(*ptr==' ' || *ptr=='\t') ptr++;

  // skip #
  if(*ptr!='#') return;
  ptr++;

  // skip WS
  while(*ptr==' ' || *ptr=='\t') ptr++;

  // skip "line"
  if(*ptr=='l')
  {
    while(*ptr!=0 && *ptr!=' ' && *ptr!='\t') ptr++;
  }

  // skip WS
  while(*ptr==' ' || *ptr=='\t') ptr++;

  // get line number
  while(isdigit(*ptr))
  {
    line_number+=*ptr;
    ptr++;
  }
  
  // skip until "
  while(*ptr!='\n' && *ptr!='"') ptr++;

  line_no=atoi(line_number.c_str());

  // skip "
  if(*ptr!='"')
    return;
  
  ptr++;
  
  std::string file_name_tmp;

  // get file name
  while(*ptr!='\n' && *ptr!='"')
  {
    file_name_tmp+=*ptr;
    ptr++;
  }

  std::string file_name_tmp2;
  unescape_string(file_name_tmp, file_name_tmp2);
  file_name=file_name_tmp2;
}
Example #14
0
void preprocessor_line(
  const char *text,
  parsert &parser)
{
  const char *ptr=text;
  std::string line_number;

  // skip WS
  while(*ptr==' ' || *ptr=='\t') ptr++;

  // skip #
  if(*ptr!='#')
    return;
  ptr++;

  // skip WS
  while(*ptr==' ' || *ptr=='\t') ptr++;

  // skip "line"
  if(*ptr=='l')
  {
    while(*ptr!=0 && *ptr!=' ' && *ptr!='\t') ptr++;
  }

  // skip WS
  while(*ptr==' ' || *ptr=='\t') ptr++;

  // get line number
  while(isdigit(*ptr))
  {
    line_number+=*ptr;
    ptr++;
  }

  // skip until "
  while(*ptr!='\n' && *ptr!='"') ptr++;

  parser.set_line_no(unsafe_string2unsigned(line_number));

  // skip "
  if(*ptr!='"')
    return;

  ptr++;

  std::string file_name_tmp;

  // get file name
  while(*ptr!='\n' && *ptr!='"')
  {
    file_name_tmp+=*ptr;
    ptr++;
  }

  std::string file_name_tmp2=unescape_string(file_name_tmp);
  parser.set_file(file_name_tmp2);
}
Example #15
0
 CharLiteral (Token _token, std::string _value) : Expression(_token, nullptr, Kind::CharLit, new BaseType(_token, "byte")) {
     _value = unescape_string(_value);
     if (_value.size() > 1) {
         // :(
         // TODO: error out.
     } else {
         value = _value[0];
     }
 }
	void streamtools_object::test<18>()
	{
		std::string str;
		std::string expected_result;

		str = "SecondLife is a 3D world \n";
		escape_string(str);
		expected_result = "SecondLife is a 3D world \\n";
		ensure_equals("escape_string: newline", str, expected_result);

		str = "SecondLife is a 3D world \\t \n";
		escape_string(str);
		expected_result = "SecondLife is a 3D world \\\\t \\n";
		ensure_equals("escape_string: backslash and newline", str, expected_result);

		str = "SecondLife is a 3D world \n \n \n \\\n";
		escape_string(str);
		expected_result = "SecondLife is a 3D world \\n \\n \\n \\\\\\n";
		ensure_equals("escape_string: multipel newline and backslash", str, expected_result);

		str = "SecondLife is a 3D world \t";
		escape_string(str);
		expected_result = "SecondLife is a 3D world \t";
		ensure_equals("unescape_string: leaves tab as is", str, expected_result);

		str = "\n";
		escape_string(str);
		expected_result = "\\n";
		ensure_equals("unescape_string: only a newline", str, expected_result);

		// serialization/deserialization escape->unescape
		str = "SecondLife is a 3D world \n \n \n \\\n";
		escape_string(str);
		unescape_string(str);
		expected_result = "SecondLife is a 3D world \n \n \n \\\n";
		ensure_equals("escape_string: should preserve with escape/unescape", str, expected_result);

		// serialization/deserialization  unescape->escape
		str = "SecondLife is a 3D world \\n \\n \\n \\\\";
		unescape_string(str);
		escape_string(str);
		expected_result = "SecondLife is a 3D world \\n \\n \\n \\\\";
		ensure_equals("escape_string: should preserve with unescape/escape", str, expected_result);
	}
Example #17
0
static dbus_bool_t
parse_section_start (BusDesktopFileParser *parser, DBusError *error)
{
  int line_end, eol_len;
  char *section_name;

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
    
  if (!_dbus_string_find_eol (&parser->data, parser->pos, &line_end, &eol_len))
    line_end = parser->len;
  
  if (line_end - parser->pos <= 2 ||
      _dbus_string_get_byte (&parser->data, line_end - 1) != ']')
    {
      report_error (parser, "Invalid syntax for section header", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error);
      parser_free (parser);
      return FALSE;
    }

  section_name = unescape_string (parser,
                                  &parser->data, parser->pos + 1, line_end - 1,
                                  error);

  if (section_name == NULL)
    {
      parser_free (parser);
      return FALSE;
    }

  if (!is_valid_section_name (section_name))
    {
      report_error (parser, "Invalid characters in section name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error);
      parser_free (parser);
      dbus_free (section_name);
      return FALSE;
    }

  if (open_section (parser, section_name) == NULL)
    {
      dbus_free (section_name);
      parser_free (parser);
      BUS_SET_OOM (error);
      return FALSE;
    }

  if (line_end == parser->len)
    parser->pos = parser->len;
  else
    parser->pos = line_end + eol_len;
  
  parser->line_num += 1;

  dbus_free (section_name);
  
  return TRUE;
}
	void streamtools_object::test<17>()
	{
		std::string str;
		std::string expected_result;

		str = "SecondLife is a 3D world \\n";
		unescape_string(str);
		expected_result = "SecondLife is a 3D world \n";
		ensure_equals("unescape_string: newline", str, expected_result);

		str = "SecondLife is a 3D world \\\\t \\n";
		unescape_string(str);
		expected_result = "SecondLife is a 3D world \\t \n";
		ensure_equals("unescape_string: backslash and newline", str, expected_result);

		str = "SecondLife is a 3D world \\ ";
		unescape_string(str);
		expected_result = "SecondLife is a 3D world \\ ";
		ensure_equals("unescape_string: insufficient to unescape", str, expected_result);

		str = "SecondLife is a 3D world \\n \\n \\n \\\\\\n";
		unescape_string(str);
		expected_result = "SecondLife is a 3D world \n \n \n \\\n";
		ensure_equals("unescape_string: multipel newline and backslash", str, expected_result);

		str = "SecondLife is a 3D world \\t";
		unescape_string(str);
		expected_result = "SecondLife is a 3D world \\t";
		ensure_equals("unescape_string: leaves tab as is", str, expected_result);

		str = "\\n";
		unescape_string(str);
		expected_result = "\n";
		ensure_equals("unescape_string: only a newline", str, expected_result);
	}
Example #19
0
void
get_spad_output(FILE *pfile,char *command,int com_type)
{
    int n, i;
    char buf[1024];

    send_command(command, com_type);
    n = get_int(spad_socket);
    for (i = 0; i < n; i++) {
        get_string_buf(spad_socket, buf, 1024);
        fprintf(pfile, "%s\n", buf);
    }
    unescape_string(command);
}
Example #20
0
/* We have to return an escaped string here */
bool autosuggest_suggest_special(const wcstring &str, const wcstring &working_directory, wcstring &outSuggestion) {
    if (str.empty())
        return false;
        
    ASSERT_IS_BACKGROUND_THREAD();
    
    /* Parse the string */
    wcstring parsed_command;
    wcstring_list_t parsed_arguments;
    int parsed_last_arg_pos = -1;
    if (! autosuggest_parse_command(str, &parsed_command, &parsed_arguments, &parsed_last_arg_pos))
        return false;
    
    bool result = false;
    if (parsed_command == L"cd" && ! parsed_arguments.empty()) {        
        /* We can possibly handle this specially */
        const wcstring escaped_dir = parsed_arguments.back();
        wcstring suggested_path;
        
        /* We always return true because we recognized the command. This prevents us from falling back to dumber algorithms; for example we won't suggest a non-directory for the cd command. */
        result = true;
        outSuggestion.clear();
        
        /* Unescape the parameter */
        wcstring unescaped_dir = escaped_dir;
        bool unescaped = unescape_string(unescaped_dir, UNESCAPE_INCOMPLETE);
        
        /* Determine the quote type we got from the input directory. */
        wchar_t quote = L'\0';
        parse_util_get_parameter_info(escaped_dir, 0, &quote, NULL, NULL);
        
        /* Big hack to avoid expanding a tilde inside quotes */
        path_flags_t path_flags = (quote == L'\0') ? PATH_EXPAND_TILDE : 0;
        if (unescaped && is_potential_cd_path(unescaped_dir, working_directory, path_flags, &suggested_path)) {
                    
            /* Note: this looks really wrong for strings that have an "unescapable" character in them, e.g. a \t, because parse_util_escape_string_with_quote will insert that character */
            wcstring escaped_suggested_path = parse_util_escape_string_with_quote(suggested_path, quote);

            /* Return it */
            outSuggestion = str;
            outSuggestion.erase(parsed_last_arg_pos);
            if (quote != L'\0') outSuggestion.push_back(quote);
            outSuggestion.append(escaped_suggested_path);
            if (quote != L'\0') outSuggestion.push_back(quote);
        }
    } else {
        /* Either an error or some other command, so we don't handle it specially */
    }
    return result;
}
Example #21
0
static chunk_t *
c_dec(const chunk_t *inp)
{
  char *s = tor_memdup_nulterm(inp->buf, inp->len);
  chunk_t *ch = tor_malloc(sizeof(chunk_t));
  char *r = NULL;
  (void) unescape_string(s, &r, &ch->len);
  tor_free(s);
  ch->buf = (uint8_t*) r;
  if (!ch->buf) {
    tor_free(ch);
  }
  return ch;
}
Example #22
0
static char *parse_key(const char **key, char *p, nx_json_unicode_encoder encoder) {
    // on '}' return with *p=='}'
    char c;
    while ((c = *p++)) {
        if (c == '"') {
            *key = unescape_string(p, &p, encoder);
            if (!*key)
                return 0;       // propagate error
            while (*p && IS_WHITESPACE(*p))
                p++;
            if (*p == ':')
                return p + 1;
            NX_JSON_REPORT_ERROR("unexpected chars", p);
            return 0;
        }
        else if (IS_WHITESPACE(c) || c == ',') {
            // continue
        }
        else if (c == '}') {
            return p - 1;
        }
        else if (c == '/') {
            if (*p == '/') {    // line comment
                char *ps = p - 1;
                p = strchr(p + 1, '\n');
                if (!p) {
                    NX_JSON_REPORT_ERROR("endless comment", ps);
                    return 0;   // error
                }
                p++;
            }
            else if (*p == '*') {       // block comment
                p = skip_block_comment(p + 1);
                if (!p)
                    return 0;
            }
            else {
                NX_JSON_REPORT_ERROR("unexpected chars", p - 1);
                return 0;       // error
            }
        }
        else {
            NX_JSON_REPORT_ERROR("unexpected chars", p - 1);
            return 0;           // error
        }
    }
    NX_JSON_REPORT_ERROR("unexpected chars", p - 1);
    return 0;                   // error
}
/**
 Parse message msg
 */
void env_universal_t::parse_message_internal(const wcstring &msgstr, var_table_t *vars, wcstring *storage)
{
    const wchar_t *msg = msgstr.c_str();
    
    //  debug( 3, L"parse_message( %ls );", msg );
    
    if (msg[0] == L'#')
        return;
    
    bool is_set_export = match(msg, SET_EXPORT_STR);
    bool is_set = ! is_set_export && match(msg, SET_STR);
    if (is_set || is_set_export)
    {
        const wchar_t *name, *tmp;
        const bool exportv = is_set_export;
        
        name = msg+(exportv?wcslen(SET_EXPORT_STR):wcslen(SET_STR));
        while (name[0] == L'\t' || name[0] == L' ')
            name++;
        
        tmp = wcschr(name, L':');
        if (tmp)
        {
            /* Use 'storage' to hold our key to avoid allocations */
            storage->assign(name, tmp - name);
            const wcstring &key = *storage;
            
            wcstring val;
            if (unescape_string(tmp + 1, &val, 0))
            {
                var_entry_t &entry = (*vars)[key];
                entry.exportv = exportv;
                entry.val.swap(val); //acquire the value
            }
        }
        else
        {
            debug(1, PARSE_ERR, msg);
        }
    }
    else
    {
        debug(1, PARSE_ERR, msg);
    }
}
Example #24
0
/**
   Read lines of input from the specified file, unescape them and
   insert them into the specified list.
*/
static void read_array( FILE* file, wcstring_list_t &comp )
{
	std::vector<char> buffer;
	int c;
	wchar_t *wcs;

	while( !feof( file ) )
	{
		buffer.clear();

		while( 1 )
		{
			c = getc( file );
			if( c == EOF ) 
			{
				break;
			}

			if( c == '\n' )
			{
				break;
			}

            buffer.push_back(static_cast<char>(c));
		}

		if( ! buffer.empty() )
		{
            buffer.push_back(0);
			
			wcs = str2wcs( &buffer.at(0) );
			if( wcs ) 
			{
                wcstring tmp = wcs;
                if (unescape_string(tmp, 0))
                {
                    comp.push_back(tmp);
				}				
				free( wcs );
			}
		}
	}

}
Example #25
0
static gboolean
parse_device_id (const gchar * id, gchar ** sgname, int * inputIndex)
{
    gchar ** parts;
    int numparts;
    GString * p1;
    GString * out1;
    int out2;

    out2 = 0;

    parts = g_strsplit (id, ":", -1);
    numparts = 0;
    while (parts[numparts])
        ++numparts;

    /* must be exactly 1 or 2 parts */
    if (numparts < 1 || numparts > 2) {
        g_strfreev (parts);
        return FALSE;
    }

    p1 = g_string_new (parts[0]);
    out1 = unescape_string (p1);
    g_string_free (p1, TRUE);

    if (numparts >= 2) {
        errno = 0;
        out2 = strtol (parts[1], NULL, 10);
        if (out2 == 0 && (errno == ERANGE || errno == EINVAL)) {
            g_string_free (out1, TRUE);
            g_strfreev (parts);
            return FALSE;
        }
    }

    g_strfreev (parts);

    *sgname = g_string_free (out1, FALSE);
    *inputIndex = out2;
    return TRUE;
}
Example #26
0
static bool parse_string(char *string, char **result)
{
    int len = strlen(string);

    if (len < 2) {
        return false;
    }

    if (string[0] == TOK_DELIM_STR && string[len - 1] == TOK_DELIM_STR) {
        char *unescaped = unescape_string(string);
        if (!unescaped) {
            return false;
        } else {
            *result = unescaped;
            return true;
        }

    } else {
        return false;
    }
}
Example #27
0
static int add_field(HTable table, char *name, char *value, char *buf, long int *pos)
{
    int value_type;
    char *s;
    HTable tbl;

    value_type = get_value_type(value);
    switch (value_type)
    {
        case TYPE_INT:
            table_set_int(table, name, atoi(value));
            break;
        case TYPE_STRING:
            s = unescape_string(value);
            if (! s)
                return -1;
            table_set_str(table, name, s);
            free(s);
            break;
        case TYPE_FLOAT:
            table_set_double(table, name, atof(value));
            break;
        case TYPE_TABLE:
            tbl = table_create();
            if (table_set_table(table, name, tbl))
            {
                table_free(tbl);
                return -1;
            }
            if (parse_table(tbl, buf, pos, 0))
                return -1;
            break;
        default:
            return -1;
    }
    return 0;
}
static gboolean
parse_section_start (GnomeThemeFileParser *parser, GError **error)
{
  gchar *line_end;
  gchar *section_name;

  line_end = strchr (parser->line, '\n');
  if (line_end == NULL)
    line_end = parser->line + strlen (parser->line);

  if (line_end - parser->line <= 2 ||
      line_end[-1] != ']')
    {
      report_error (parser, "Invalid syntax for section header", GNOME_THEME_FILE_PARSE_ERROR_INVALID_SYNTAX, error);
      parser_free (parser);
      return FALSE;
    }

  section_name = unescape_string (parser->line + 1, line_end - parser->line - 2);

  if (section_name == NULL)
    {
      report_error (parser, "Invalid escaping in section name", GNOME_THEME_FILE_PARSE_ERROR_INVALID_ESCAPES, error);
      parser_free (parser);
      return FALSE;
    }

  open_section (parser, section_name);
  
  parser->line = (line_end) ? line_end + 1 : NULL;
  parser->line_nr++;

  g_free (section_name);
  
  return TRUE;
}
static gboolean
parse_section_start (AnjutaPluginDescriptionParser *parser, GError **error)
{
  gchar *line_end;
  gchar *section_name;

  line_end = strchr (parser->line, '\n');
  if (line_end == NULL)
    line_end = parser->line + strlen (parser->line);

  if (line_end - parser->line <= 2 ||
      line_end[-1] != ']')
    {
      report_error (parser, "Invalid syntax for section header", ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR_INVALID_SYNTAX, error);
      parser_free (parser);
      return FALSE;
    }

  section_name = unescape_string (parser->line + 1, line_end - parser->line - 2);

  if (section_name == NULL)
    {
      report_error (parser, "Invalid escaping in section name", ANJUTA_PLUGIN_DESCRIPTION_PARSE_ERROR_INVALID_ESCAPES, error);
      parser_free (parser);
      return FALSE;
    }

  open_section (parser, section_name);
  
  parser->line = (line_end) ? line_end + 1 : NULL;
  parser->line_nr++;

  g_free (section_name);
  
  return TRUE;
}
/**
   The complete builtin. Used for specifying programmable
   tab-completions. Calls the functions in complete.c for any heavy
   lifting. Defined in builtin_complete.c
*/
static int builtin_complete(parser_t &parser, wchar_t **argv)
{
    ASSERT_IS_MAIN_THREAD();
    bool res=false;
    int argc=0;
    int result_mode=SHARED;
    int remove = 0;
    int authoritative = -1;

    wcstring short_opt;
    wcstring_list_t gnu_opt, old_opt;
    const wchar_t *comp=L"", *desc=L"", *condition=L"";

    bool do_complete = false;
    wcstring do_complete_param;

    wcstring_list_t cmd;
    wcstring_list_t path;

    static int recursion_level=0;

    argc = builtin_count_args(argv);

    woptind=0;

    while (! res)
    {
        static const struct woption
                long_options[] =
        {
            {
                L"exclusive", no_argument, 0, 'x'
            }
            ,
            {
                L"no-files", no_argument, 0, 'f'
            }
            ,
            {
                L"require-parameter", no_argument, 0, 'r'
            }
            ,
            {
                L"path", required_argument, 0, 'p'
            }
            ,
            {
                L"command", required_argument, 0, 'c'
            }
            ,
            {
                L"short-option", required_argument, 0, 's'
            }
            ,
            {
                L"long-option", required_argument, 0, 'l'
            }
            ,
            {
                L"old-option", required_argument, 0, 'o'
            }
            ,
            {
                L"description", required_argument, 0, 'd'
            }
            ,
            {
                L"arguments", required_argument, 0, 'a'
            }
            ,
            {
                L"erase", no_argument, 0, 'e'
            }
            ,
            {
                L"unauthoritative", no_argument, 0, 'u'
            }
            ,
            {
                L"authoritative", no_argument, 0, 'A'
            }
            ,
            {
                L"condition", required_argument, 0, 'n'
            }
            ,
            {
                L"do-complete", optional_argument, 0, 'C'
            }
            ,
            {
                L"help", no_argument, 0, 'h'
            }
            ,
            {
                0, 0, 0, 0
            }
        }
        ;

        int opt_index = 0;

        int opt = wgetopt_long(argc,
                               argv,
                               L"a:c:p:s:l:o:d:frxeuAn:C::h",
                               long_options,
                               &opt_index);
        if (opt == -1)
            break;

        switch (opt)
        {
            case 0:
                if (long_options[opt_index].flag != 0)
                    break;
                append_format(stderr_buffer,
                              BUILTIN_ERR_UNKNOWN,
                              argv[0],
                              long_options[opt_index].name);
                builtin_print_help(parser, argv[0], stderr_buffer);


                res = true;
                break;

            case 'x':
                result_mode |= EXCLUSIVE;
                break;

            case 'f':
                result_mode |= NO_FILES;
                break;

            case 'r':
                result_mode |= NO_COMMON;
                break;

            case 'p':
            case 'c':
            {
                wcstring tmp;
                if (unescape_string(woptarg, &tmp, UNESCAPE_SPECIAL))
                {
                    if (opt=='p')
                        path.push_back(tmp);
                    else
                        cmd.push_back(tmp);
                }
                else
                {
                    append_format(stderr_buffer, L"%ls: Invalid token '%ls'\n", argv[0], woptarg);
                    res = true;
                }
                break;
            }

            case 'd':
                desc = woptarg;
                break;

            case 'u':
                authoritative=0;
                break;

            case 'A':
                authoritative=1;
                break;

            case 's':
                short_opt.append(woptarg);
                break;

            case 'l':
                gnu_opt.push_back(woptarg);
                break;

            case 'o':
                old_opt.push_back(woptarg);
                break;

            case 'a':
                comp = woptarg;
                break;

            case 'e':
                remove = 1;
                break;

            case 'n':
                condition = woptarg;
                break;

            case 'C':
                do_complete = true;
                do_complete_param = woptarg ? woptarg : reader_get_buffer();
                break;

            case 'h':
                builtin_print_help(parser, argv[0], stdout_buffer);
                return 0;

            case '?':
                builtin_unknown_option(parser, argv[0], argv[woptind-1]);
                res = true;
                break;

        }

    }

    if (!res)
    {
        if (condition && wcslen(condition))
        {
            const wcstring condition_string = condition;
            parse_error_list_t errors;
            if (parse_util_detect_errors(condition_string, &errors))
            {
                append_format(stderr_buffer,
                              L"%ls: Condition '%ls' contained a syntax error",
                              argv[0],
                              condition);
                for (size_t i=0; i < errors.size(); i++)
                {
                    append_format(stderr_buffer, L"\n%s: ", argv[0]);
                    stderr_buffer.append(errors.at(i).describe(condition_string));
                }
                res = true;
            }
        }
    }

    if (!res)
    {
        if (comp && wcslen(comp))
        {
            if (parser.test_args(comp, 0, 0))
            {
                append_format(stderr_buffer,
                              L"%ls: Completion '%ls' contained a syntax error\n",
                              argv[0],
                              comp);

                parser.test_args(comp, &stderr_buffer, argv[0]);

                res = true;
            }
        }
    }

    if (!res)
    {
        if (do_complete)
        {
            const wchar_t *token;

            parse_util_token_extent(do_complete_param.c_str(), do_complete_param.size(), &token, 0, 0, 0);

            const wchar_t *prev_temporary_buffer = temporary_buffer;
            temporary_buffer = do_complete_param.c_str();

            if (recursion_level < 1)
            {
                recursion_level++;

                std::vector<completion_t> comp;
                complete(do_complete_param, comp, COMPLETION_REQUEST_DEFAULT);

                for (size_t i=0; i< comp.size() ; i++)
                {
                    const completion_t &next =  comp.at(i);

                    const wchar_t *prepend;

                    if (next.flags & COMPLETE_REPLACES_TOKEN)
                    {
                        prepend = L"";
                    }
                    else
                    {
                        prepend = token;
                    }


                    if (!(next.description).empty())
                    {
                        append_format(stdout_buffer, L"%ls%ls\t%ls\n", prepend, next.completion.c_str(), next.description.c_str());
                    }
                    else
                    {
                        append_format(stdout_buffer, L"%ls%ls\n", prepend, next.completion.c_str());
                    }
                }

                recursion_level--;
            }

            temporary_buffer = prev_temporary_buffer;

        }
        else if (woptind != argc)
        {
            append_format(stderr_buffer,
                          _(L"%ls: Too many arguments\n"),
                          argv[0]);
            builtin_print_help(parser, argv[0], stderr_buffer);

            res = true;
        }
        else if (cmd.empty() && path.empty())
        {
            /* No arguments specified, meaning we print the definitions of
             * all specified completions to stdout.*/
            complete_print(stdout_buffer);
        }
        else
        {
            int flags = COMPLETE_AUTO_SPACE;

            if (remove)
            {
                builtin_complete_remove(cmd,
                                        path,
                                        short_opt.c_str(),
                                        gnu_opt,
                                        old_opt);
            }
            else
            {
                builtin_complete_add(cmd,
                                     path,
                                     short_opt.c_str(),
                                     gnu_opt,
                                     old_opt,
                                     result_mode,
                                     authoritative,
                                     condition,
                                     comp,
                                     desc,
                                     flags);
            }

        }
    }

    return res ? 1 : 0;
}