/* * pk_parse_os_release: * * Internal helper to parse os-release **/ static gboolean pk_parse_os_release (gchar **id, gchar **name, gchar **version_id, GError **error) { const gchar *filename = "/etc/os-release"; gboolean ret; g_autofree gchar *contents = NULL; g_autoptr(GKeyFile) key_file = NULL; g_autoptr(GString) str = NULL; /* load data */ if (!g_file_test (filename, G_FILE_TEST_EXISTS)) filename = "/usr/lib/os-release"; if (!g_file_get_contents (filename, &contents, NULL, error)) return FALSE; /* make a valid GKeyFile from the .ini data by prepending a header */ str = g_string_new (contents); g_string_prepend (str, "[os-release]\n"); key_file = g_key_file_new (); ret = g_key_file_load_from_data (key_file, str->str, -1, G_KEY_FILE_NONE, error); if (!ret) { return FALSE; } /* get keys */ if (id != NULL) { g_autofree gchar *tmp = g_key_file_get_string (key_file, "os-release", "ID", error); if (tmp == NULL) return FALSE; *id = g_shell_unquote (tmp, error); if (*id == NULL) return FALSE; } if (name != NULL) { g_autofree gchar *tmp = g_key_file_get_string (key_file, "os-release", "NAME", error); if (tmp == NULL) return FALSE; *name = g_shell_unquote (tmp, error); if (*name == NULL) return FALSE; } if (version_id != NULL) { g_autofree gchar *tmp = g_key_file_get_string (key_file, "os-release", "VERSION_ID", error); if (tmp == NULL) return FALSE; *version_id = g_shell_unquote (tmp, error); if (*version_id == NULL) return FALSE; } return TRUE; }
/* Only called for non-desktop files */ static char * get_app_name (const char *commandline, GError **error) { char *basename; char *unquoted; char **argv; int argc; if (!g_shell_parse_argv (commandline, &argc, &argv, error)) { return NULL; } unquoted = g_shell_unquote (argv[0], NULL); if (unquoted) { basename = g_path_get_basename (unquoted); } else { basename = g_strdup (argv[0]); } g_free (unquoted); g_strfreev (argv); return basename; }
gchar * gst_pipeline_string_get_property_value (const gchar *pipeline_str, const gchar *propertyname) { gchar **pipeline_nodes = NULL; gchar *node = NULL; gchar *node_value = NULL; gchar **node_split = NULL; gint i = 0; g_assert (pipeline_str != NULL); pipeline_nodes = g_strsplit (pipeline_str, " ", -1); while ((node = pipeline_nodes[i++])) { /* Split into key = value pair */ node_split = g_strsplit_set (node, "=", -1); if (node_split != NULL && node_split[1] != NULL) { if (!strcmp (node_split[0], propertyname)) { node_value = g_shell_unquote(node_split[1], NULL); } } g_strfreev (node_split); if (node_value != NULL) break; } g_strfreev (pipeline_nodes); return node_value; }
static GHashTable * parse_os_release (const char *contents, const char *split) { char **lines = g_strsplit (contents, split, -1); char **iter; GHashTable *ret = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); for (iter = lines; *iter; iter++) { char *line = *iter; char *eq; const char *quotedval; char *val; if (g_str_has_prefix (line, "#")) continue; eq = strchr (line, '='); if (!eq) continue; *eq = '\0'; quotedval = eq + 1; val = g_shell_unquote (quotedval, NULL); if (!val) continue; g_hash_table_insert (ret, line, val); } return ret; }
/** * g_shell_parse_argv: * @command_line: command line to parse * @argcp: (out) (optional): return location for number of args, or %NULL * @argvp: (out) (optional) (array length=argcp zero-terminated=1): return * location for array of args, or %NULL * @error: (optional): return location for error, or %NULL * * Parses a command line into an argument vector, in much the same way * the shell would, but without many of the expansions the shell would * perform (variable expansion, globs, operators, filename expansion, * etc. are not supported). The results are defined to be the same as * those you would get from a UNIX98 /bin/sh, as long as the input * contains none of the unsupported shell expansions. If the input * does contain such expansions, they are passed through * literally. Possible errors are those from the #G_SHELL_ERROR * domain. Free the returned vector with g_strfreev(). * * Returns: %TRUE on success, %FALSE if error set **/ bool g_shell_parse_argv (const std::string& cmd, int& argcp, std::vector<std::string>& argvp) { /* Code based on poptParseArgvString() from libpopt */ std::vector<std::string> tokens; tokenize_command_line (tokens, cmd); if (tokens.empty()) { return false; } /* Because we can't have introduced any new blank space into the * tokens (we didn't do any new expansions), we don't need to * perform field splitting. If we were going to honor IFS or do any * expansions, we would have to do field splitting on each word * here. Also, if we were going to do any expansion we would need to * remove any zero-length words that didn't contain quotes * originally; but since there's no expansion we know all words have * nonzero length, unless they contain quotes. * * So, we simply remove quotes, and don't do any field splitting or * empty word removal, since we know there was no way to introduce * such things. */ argcp = tokens.size(); argvp.resize(argcp); for (unsigned int i=0; i< tokens.size(); i++) { argvp[i] = g_shell_unquote (tokens[i]); } return true; }
gchar * read_hostname (const char *path) { gchar *contents = NULL, *result = NULL, *tmp; gchar **all_lines = NULL; guint line_num, i; if (!g_file_get_contents (path, &contents, NULL, NULL)) return NULL; all_lines = g_strsplit (contents, "\n", 0); line_num = g_strv_length (all_lines); for (i = 0; i < line_num; i++) { g_strstrip (all_lines[i]); if (all_lines[i][0] == '#' || all_lines[i][0] == '\0') continue; if (g_str_has_prefix (all_lines[i], "hostname")) { tmp = strstr (all_lines[i], "="); tmp++; result = g_shell_unquote (tmp, NULL); break; } } g_strfreev (all_lines); g_free (contents); return result; }
static void reread_os_release (Manager *manager) { gs_unref_object GFile *etc_os_release = g_file_new_for_path ("/etc/os-release"); gs_free char *contents = NULL; const char *operating_system_value = NULL; char **lines = NULL; char **iter = NULL; gsize len; GError *local_error = NULL; gs_unref_hashtable GHashTable *os_release_keys = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); if (!g_file_load_contents (etc_os_release, NULL, &contents, &len, NULL, &local_error)) goto out; if (!g_utf8_validate (contents, len, NULL)) { g_set_error (&local_error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, "Invalid UTF-8"); goto out; } lines = g_strsplit (contents, "\n", -1); for (iter = lines; iter && *iter; iter++) { char *line = *iter; char *eq; char *value; const char *quotedval; if (g_str_has_prefix (line, "#")) continue; eq = strchr (line, '='); if (!eq) continue; *eq = '\0'; quotedval = eq + 1; value = g_shell_unquote (quotedval, NULL); if (!value) continue; g_hash_table_insert (os_release_keys, g_strdup (line), value); } operating_system_value = g_hash_table_lookup (os_release_keys, "PRETTY_NAME"); if (operating_system_value) g_object_set (manager, "operating-system", operating_system_value, NULL); out: g_strfreev (lines); if (local_error) { g_warning ("Failed to load /etc/os-release: %s", local_error->message); g_error_free (local_error); } }
static VALUE shell_unquote(VALUE self, VALUE quoted_string) { GError *error = NULL; gchar *str = g_shell_unquote(RVAL2CSTR(quoted_string), &error); if (str == NULL) RAISE_GERROR(error); return CSTR2RVAL_FREE(str); }
/* {{{ proto string Glib::shellUnquote(string ununquoted_string) Unquotes a string as the shell (/bin/sh) would. Only handles quotes; if a string contains file globs, arithmetic operators, variables, backticks, redirections, or other special-to-the-shell features, the result will be different from the result a real shell would produce (the variables, backticks, etc. will be passed through literally instead of being expanded). */ PHP_METHOD(Glib, shellUnquote) { char *string, *unquoted_string; int length; GError *error = NULL; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &string, &length) == FAILURE) { return; } unquoted_string = g_shell_unquote((const gchar*)string, &error); if (php_glib_handle_gerror(&error TSRMLS_CC)) { return; } RETURN_STRING(unquoted_string, 1); }
/* Thanks to the freedesktop.org, things are much more complicated now... */ const char* vfs_get_desktop_dir() { char* def; if( G_LIKELY(is_desktop_set) ) return desktop_dir; /* glib provides API for this since ver. 2.14, but I think my implementation is better. */ #if GLIB_CHECK_VERSION( 2, 14, 0 ) && 0 /* Delete && 0 to use the one provided by glib */ desktop_dir = g_get_user_special_dir( G_USER_DIRECTORY_DESKTOP ); #else def = g_build_filename( g_get_user_config_dir(), "user-dirs.dirs", NULL ); if( def ) { int fd = open( def, O_RDONLY ); g_free( def ); if( G_LIKELY( fd != -1 ) ) { struct stat s; // skip stat64 if( G_LIKELY( fstat( fd, &s ) != -1 ) ) { char* buf = g_malloc( s.st_size + 1 ); if( (s.st_size = read( fd, buf, s.st_size )) != -1 ) { char* line; buf[ s.st_size ] = 0; line = strstr( buf, "XDG_DESKTOP_DIR=" ); if( G_LIKELY( line ) ) { char* eol; line += 16; if( G_LIKELY( ( eol = strchr( line, '\n' ) ) ) ) *eol = '\0'; line = g_shell_unquote( line, NULL ); if( g_str_has_prefix(line, "$HOME") ) { desktop_dir = g_build_filename( g_get_home_dir(), line + 5, NULL ); g_free( line ); } else desktop_dir = line; } } g_free( buf ); } close( fd ); } } if( ! desktop_dir ) desktop_dir = g_build_filename( g_get_home_dir(), "Desktop", NULL ); #endif #if 0 /* FIXME: what should we do if the user has no desktop dir? */ if( ! g_file_test( desktop_dir, G_FILE_TEST_IS_DIR ) ) { g_free( desktop_dir ); desktop_dir = NULL; } #endif is_desktop_set = TRUE; return desktop_dir; }
static void print_completions (const char *arg) { GFile *f; GFile *parent; char *basename; char *unescaped, *t; unescaped = g_shell_unquote (arg, NULL); if (unescaped == NULL) unescaped = g_strdup (arg); if (*unescaped == '~') { t = unescaped; unescaped = g_strconcat (g_get_home_dir(), t+1, NULL); g_free (t); } f = g_file_new_for_commandline_arg (unescaped); if (g_str_has_suffix (arg, "/") || *arg == 0) { parent = g_object_ref (f); basename = g_strdup (""); } else { parent = g_file_get_parent (f); basename = g_file_get_basename (f); } if (parent == NULL || strchr (arg, '/') == NULL || !g_file_query_exists (parent, NULL)) { GMount *mount; mount = g_file_find_enclosing_mount (f, NULL, NULL); if (mount == NULL) print_mounts (unescaped); else g_object_unref (mount); } if (parent != NULL) { GFileEnumerator *enumerator; enumerator = g_file_enumerate_children (parent, G_FILE_ATTRIBUTE_STANDARD_NAME "," G_FILE_ATTRIBUTE_STANDARD_TYPE, nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS : 0, NULL, NULL); if (enumerator != NULL) { GFileInfo *info; while ((info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL) { const char *name; GFileType type; name = g_file_info_get_name (info); type = g_file_info_get_attribute_uint32 (info, G_FILE_ATTRIBUTE_STANDARD_TYPE); if (name != NULL && g_str_has_prefix (name, basename)) { GFile *entry; entry = g_file_get_child (parent, name); show_completed_file (entry, type == G_FILE_TYPE_DIRECTORY, arg); g_object_unref (entry); } g_object_unref (info); } g_file_enumerator_close (enumerator, NULL, NULL); } g_object_unref (parent); } g_object_unref (f); g_free (basename); g_free (unescaped); }
int main(int argc, char *argv[]) { IAnjutaProject *project = NULL; AnjutaProjectNode *node; AnjutaProjectNode *child; AnjutaProjectNode *sibling; AnjutaProjectNode *root = NULL; char **command; GOptionContext *context; GError *error = NULL; /* Initialize program */ g_type_init (); /* Parse options */ context = g_option_context_new ("list [args]"); g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); g_option_context_set_summary (context, "test new autotools project manger"); if (!g_option_context_parse (context, &argc, &argv, &error)) { exit (1); } if (argc < 2) { printf ("PROJECT: %s", g_option_context_get_help (context, TRUE, NULL)); exit (1); } open_output (); /* Execute commands */ for (command = &argv[1]; *command != NULL; command++) { if (g_ascii_strcasecmp (*command, "load") == 0) { GFile *file = g_file_new_for_commandline_arg (*(++command)); if (project == NULL) { gint best = 0; gint probe; GType type; GTypeModule *module; /* Register project types */ module = g_object_new (dummy_type_module_get_type (), NULL); amp_project_register (module); /* Check for project type */ probe = amp_project_probe (file, NULL); if (probe > best) { best = probe; type = AMP_TYPE_PROJECT; } if (best == 0) { print_error ("Error: No backend for loading project in %s", *command); break; } else { project = IANJUTA_PROJECT (amp_project_new (file, NULL, NULL)); } } root = ianjuta_project_get_root (project, &error); ianjuta_project_load_node (project, root, &error); g_object_unref (file); } else if (g_ascii_strcasecmp (*command, "list") == 0) { list_root (project, root); } else if (g_ascii_strcasecmp (*command, "move") == 0) { if (AMP_IS_PROJECT (project)) { amp_project_move (AMP_PROJECT (project), *(++command)); } } else if (g_ascii_strcasecmp (*command, "save") == 0) { ianjuta_project_save_node (project, root, NULL); } else if (g_ascii_strcasecmp (*command, "remove") == 0) { node = get_node (project, root, *(++command)); ianjuta_project_remove_node (project, node, NULL); } else if (g_ascii_strcasecmp (*command, "dump") == 0) { if (g_ascii_strcasecmp (command[1], "makefile") == 0) { node = get_node (project, root, command[2]); amp_project_dump (AMP_PROJECT (project), node, DUMP_MAKEFILE); command +=2; } else if (g_ascii_strcasecmp (command[1], "configure") == 0) { amp_project_dump (AMP_PROJECT (project), root, DUMP_CONFIGURE); command +=1; } else { print_error ("Error: unknown command %s %s", *command, command[1]); break; } } else if (g_ascii_strcasecmp (command[0], "add") == 0) { node = get_node (project, root, command[2]); if (g_ascii_strcasecmp (command[1], "group") == 0) { if ((command[4] != NULL) && (g_ascii_strcasecmp (command[4], "before") == 0)) { sibling = get_node (project, root, command[5]); child = ianjuta_project_add_node_before (project, node, sibling, ANJUTA_PROJECT_GROUP, NULL, command[3], &error); command += 2; } else if ((command[4] != NULL) && (g_ascii_strcasecmp (command[4], "after") == 0)) { sibling = get_node (project, root, command[5]); child = ianjuta_project_add_node_after (project, node, sibling, ANJUTA_PROJECT_GROUP, NULL, command[3], &error); command += 2; } else { child = ianjuta_project_add_node_before (project, node, NULL, ANJUTA_PROJECT_GROUP, NULL, command[3], &error); } } else if (g_ascii_strcasecmp (command[1], "target") == 0) { if ((command[5] != NULL) && (g_ascii_strcasecmp (command[5], "before") == 0)) { sibling = get_node (project, root, command[6]); child = ianjuta_project_add_node_before (project, node, sibling, ANJUTA_PROJECT_TARGET | get_target_type (project, command[4]), NULL, command[3], &error); command += 2; } else if ((command[5] != NULL) && (g_ascii_strcasecmp (command[5], "after") == 0)) { sibling = get_node (project, root, command[6]); child = ianjuta_project_add_node_after (project, node, sibling, ANJUTA_PROJECT_TARGET | get_target_type (project, command[4]), NULL, command[3], &error); command += 2; } else { child = ianjuta_project_add_node_before (project, node, NULL, ANJUTA_PROJECT_TARGET | get_target_type (project, command[4]), NULL, command[3], &error); } command++; } else if (g_ascii_strcasecmp (command[1], "source") == 0) { GFile *file = get_file (node, command[3]); if ((command[4] != NULL) && (g_ascii_strcasecmp (command[4], "before") == 0)) { sibling = get_node (project, root, command[5]); child = ianjuta_project_add_node_before (project, node, sibling, ANJUTA_PROJECT_SOURCE, file, NULL, &error); command += 2; } else if ((command[4] != NULL) && (g_ascii_strcasecmp (command[4], "after") == 0)) { sibling = get_node (project, root, command[5]); child = ianjuta_project_add_node_after (project, node, sibling, ANJUTA_PROJECT_SOURCE, file, NULL, &error); command += 2; } else { child = ianjuta_project_add_node_before (project, node, NULL, ANJUTA_PROJECT_SOURCE, file, NULL, &error); } g_object_unref (file); } else if (g_ascii_strcasecmp (command[1], "module") == 0) { if ((command[4] != NULL) && (g_ascii_strcasecmp (command[4], "before") == 0)) { sibling = get_node (project, root, command[5]); child = ianjuta_project_add_node_before (project, node, sibling, ANJUTA_PROJECT_MODULE, NULL, command[3], &error); command += 2; } else if ((command[4] != NULL) && (g_ascii_strcasecmp (command[4], "after") == 0)) { sibling = get_node (project, root, command[5]); child = ianjuta_project_add_node_after (project, node, sibling, ANJUTA_PROJECT_MODULE, NULL, command[3], &error); command += 2; } else { child = ianjuta_project_add_node_before (project, node, NULL, ANJUTA_PROJECT_MODULE, NULL, command[3], &error); } } else if (g_ascii_strcasecmp (command[1], "package") == 0) { if ((command[4] != NULL) && (g_ascii_strcasecmp (command[4], "before") == 0)) { sibling = get_node (project, root, command[5]); child = ianjuta_project_add_node_before (project, node, sibling, ANJUTA_PROJECT_PACKAGE, NULL, command[3], &error); command += 2; } else if ((command[4] != NULL) && (g_ascii_strcasecmp (command[4], "after") == 0)) { sibling = get_node (project, root, command[5]); child = ianjuta_project_add_node_after (project, node, sibling, ANJUTA_PROJECT_PACKAGE, NULL, command[3], &error); command += 2; } else { child = ianjuta_project_add_node_before (project, node, NULL, ANJUTA_PROJECT_PACKAGE, NULL, command[3], &error); } } else { print_error ("Error: unknown command %s", *command); break; } command += 3; } else if (g_ascii_strcasecmp (command[0], "set") == 0) { if (AMP_IS_PROJECT (project)) { AnjutaProjectPropertyInfo *info; node = get_node (project, root, command[1]); info = get_project_property (project, node, command[2]); if (info != NULL) { gchar *value = g_shell_unquote (command[3], NULL); ianjuta_project_set_property (project, node, info->id, NULL, value, NULL); g_free (value); } } command += 3; } else if (g_ascii_strcasecmp (command[0], "clear") == 0) { if (AMP_IS_PROJECT (project)) { AnjutaProjectPropertyInfo *info; node = get_node (project, root, command[1]); info = get_project_property (project, node, command[2]); if (info != NULL) { ianjuta_project_remove_property (project, node, info->id, NULL, NULL); } } command += 2; } else { print_error ("Error: unknown command %s", *command); break; } amp_project_wait_ready (project); if (error != NULL) { print_error ("Error: %s", error->message == NULL ? "unknown error" : error->message); g_error_free (error); error = NULL; } } /* Free objects */ if (project) g_object_unref (project); close_output (); return (0); }