gint main (gint argc, gchar *argv[]) { G_PASTE_INIT_GETTEXT (); if (argc > 1) { if (is_help (argv[1])) { show_help (argv[0]); return EXIT_SUCCESS; } else if (is_version (argv[1])) { show_version (); return EXIT_SUCCESS; } } int status = EXIT_SUCCESS; G_PASTE_CLEANUP_ERROR_FREE GError *error = NULL; G_PASTE_CLEANUP_UNREF GPasteClient *client = g_paste_client_new_sync (&error); if (!client) failure_exit (error); if (!isatty (fileno (stdin)) && argc == 1) { /* We are being piped */ G_PASTE_CLEANUP_STRING_FREE GString *data = g_string_new (""); gchar c; while ((c = fgetc (stdin)) != EOF) data = g_string_append_c (data, c); data->str[data->len - 1] = '\0'; g_paste_client_add_sync (client, data->str, &error); } else { const gchar *arg1, *arg2, *arg3; switch (argc) { case 1: show_history (client, FALSE, FALSE, &error); break; case 2: arg1 = argv[1]; if (!g_strcmp0 (arg1, "about")) g_paste_client_about_sync (client, &error); else if (!g_strcmp0 (arg1, "dr") || !g_strcmp0 (arg1, "daemon-reexec")) { g_paste_client_reexecute_sync (client, &error); if (error && error->code == G_DBUS_ERROR_NO_REPLY) { printf (_("Successfully reexecuted the daemon\n")); return EXIT_SUCCESS; } } else if (!g_strcmp0 (arg1, "dv") || !g_strcmp0 (arg1, "daemon-version")) { G_PASTE_CLEANUP_FREE gchar *v = g_paste_client_get_version (client); printf ("%s\n", v); } else if (!g_strcmp0 (arg1, "e") || !g_strcmp0 (arg1, "empty")) { g_paste_client_empty_sync (client, &error); } else if (!g_strcmp0 (arg1, "h") || !g_strcmp0 (arg1, "history")) { show_history (client, FALSE, FALSE, &error); } else if (!g_strcmp0 (arg1, "hs") || !g_strcmp0 (arg1, "history-size")) { guint32 size = g_paste_client_get_history_size_sync (client, &error); if (!error) printf ("%u\n", size); } else if (!g_strcmp0 (arg1, "lh") || !g_strcmp0 (arg1, "list-histories")) { G_PASTE_CLEANUP_STRFREEV GStrv histories = g_paste_client_list_histories_sync (client, &error); if (!error) { for (GStrv h = histories; *h; ++h) printf ("%s\n", *h); } } else if (!g_strcmp0 (arg1, "rh") || !g_strcmp0 (arg1, "raw-history")) { show_history (client, TRUE, FALSE, &error); } else if (!g_strcmp0 (arg1, "s") || !g_strcmp0 (arg1, "settings") || !g_strcmp0 (arg1, "p") || !g_strcmp0 (arg1, "preferences")) { status = spawn ("Settings", &error); } else if (!g_strcmp0 (arg1, "start") || !g_strcmp0 (arg1, "d") || !g_strcmp0 (arg1, "daemon")) { g_paste_client_track_sync (client, TRUE, &error); } else if (!g_strcmp0 (arg1, "stop") || !g_strcmp0 (arg1, "q") || !g_strcmp0 (arg1, "quit")) { g_paste_client_track_sync (client, FALSE, &error); } else if (!g_strcmp0 (arg1, "zh") || !g_strcmp0 (arg1, "zero-history")) { show_history (client, FALSE, TRUE, &error); } #if G_PASTE_CONFIG_ENABLE_APPLET else if (!g_strcmp0 (arg1, "applet")) { status = spawn ("Applet", &error); } #endif #if G_PASTE_CONFIG_ENABLE_UNITY else if (!g_strcmp0 (arg1, "app-indicator")) { status = spawn ("AppIndicator", &error); } #endif else { show_help (argv[0]); status = EXIT_FAILURE; } break; case 3: arg1 = argv[1]; arg2 = argv[2]; if (!g_strcmp0 (arg1, "a") || !g_strcmp0 (arg1, "add")) { g_paste_client_add_sync (client, arg2, &error); } else if (!g_strcmp0 (arg1, "bh") || !g_strcmp0 (arg1, "backup-history")) { g_paste_client_backup_history_sync (client, arg2, &error); } else if (!g_strcmp0 (arg1, "d") || !g_strcmp0 (arg1, "del") || !g_strcmp0 (arg1, "delete") || !g_strcmp0 (arg1, "rm") || !g_strcmp0 (arg1, "remove")) { g_paste_client_delete_sync (client, _strtoull (arg2), &error); } else if (!g_strcmp0 (arg1, "dp") || !g_strcmp0 (arg1, "delete-password")) { g_paste_client_delete_password_sync (client, arg2, &error); } else if (!g_strcmp0 (arg1, "dh") || !g_strcmp0 (arg1, "delete-history")) { g_paste_client_delete_history_sync (client, arg2, &error); } else if (!g_strcmp0 (arg1, "f") || !g_strcmp0 (arg1, "file")) { g_paste_client_add_file_sync (client, arg2, &error); } else if (!g_strcmp0 (arg1, "g") || !g_strcmp0 (arg1, "get")) { printf ("%s", g_paste_client_get_element_sync (client, _strtoull (arg2), &error)); } else if (!g_strcmp0 (arg1, "gr") || !g_strcmp0 (arg1, "get-raw")) { printf ("%s", g_paste_client_get_raw_element_sync (client, _strtoull (arg2), &error)); } else if (!g_strcmp0 (arg1, "s") || !g_strcmp0 (arg1, "set") || !g_strcmp0 (arg1, "select")) { g_paste_client_select_sync (client, _strtoull (arg2), &error); } else if (!g_strcmp0 (arg1, "sh") || !g_strcmp0 (arg1, "switch-history")) { g_paste_client_switch_history_sync (client, arg2, &error); } else { show_help (argv[0]); status = EXIT_FAILURE; } break; case 4: arg1 = argv[1]; arg2 = argv[2]; arg3 = argv[3]; if (!g_strcmp0 (arg1, "ap") || !g_strcmp0 (arg1, "add-password")) { g_paste_client_add_password_sync (client, arg2, arg3, &error); } else if (!g_strcmp0 (arg1, "rp") || !g_strcmp0 (arg1, "rename-password")) { g_paste_client_rename_password_sync (client, arg2, arg3, &error); } else if (!g_strcmp0 (arg1, "sp") || !g_strcmp0 (arg1, "set-password")) { g_paste_client_set_password_sync (client, _strtoull (arg2), arg3, &error); } else { show_help (argv[0]); status = EXIT_FAILURE; } break; default: show_help (argv[0]); status = EXIT_FAILURE; break; } } if (error) failure_exit (error); return status; }
gint main (gint argc, gchar *argv[]) { G_PASTE_INIT_GETTEXT (); g_set_prgname (argv[0]); struct option long_options[] = { { "decoration", required_argument, NULL, 'd' }, { "help", no_argument, NULL, 'h' }, { "oneline", no_argument, NULL, 'o' }, { "raw" , no_argument, NULL, 'r' }, { "separator" , required_argument, NULL, 's' }, { "version", no_argument, NULL, 'v' }, { "zero", no_argument, NULL, 'z' }, { NULL, no_argument, NULL, '\0' } }; gboolean help = FALSE, version = FALSE; gboolean oneline = FALSE, raw = FALSE, zero = FALSE; const gchar *decoration = NULL, *separator = NULL; gint c; while ((c = getopt_long(argc, argv, "d:hors:vz", long_options, NULL)) != -1) { switch (c) { case 'd': decoration = optarg; break; case 'h': help = TRUE; break; case 'o': oneline = TRUE; break; case 'r': raw = TRUE; break; case 's': separator = optarg; break; case 'v': version = TRUE; break; case 'z': zero = TRUE; break; default: return EXIT_FAILURE; } } argv += optind; argc -= optind; if (help || (argc > 0 && !g_strcmp0 (argv[0], "help"))) { show_help (); return EXIT_SUCCESS; } else if (version || (argc > 0 && is_version (argv[0]))) { show_version (); return EXIT_SUCCESS; } int status = EXIT_SUCCESS; g_autoptr (GError) error = NULL; g_autoptr (GPasteClient) client = g_paste_client_new_sync (&error); if (!client) failure_exit (error); gboolean was_valid_pipe = FALSE; if (!isatty (fileno (stdin))) { /* We are being piped */ G_PASTE_CLEANUP_STRING_FREE GString *data = g_string_new (NULL); while ((c = fgetc (stdin)) != EOF) data = g_string_append_c (data, (guchar)c); data->str[data->len - 1] = '\0'; if (!argc) { g_paste_client_add_sync (client, data->str, &error); was_valid_pipe = TRUE; } else if (argc == 2) { const gchar *arg1 = argv[0]; const gchar *arg2 = argv[1]; if (!g_strcmp0 (arg1, "ap") || !g_strcmp0 (arg1, "add-password")) { g_paste_client_add_password_sync (client, arg2, data->str, &error); was_valid_pipe = TRUE; } } } if (was_valid_pipe) {} else if (argc > 0 && (!g_strcmp0 (argv[0], "merge") || !g_strcmp0 (argv[0], "m"))) { --argc; ++argv; guint32 *indexes = alloca (argc * sizeof (guint32)); for (gint i = 0; i < argc; ++i) indexes[i] = _strtoull (argv[i]); g_paste_client_merge_sync (client, decoration, separator, indexes, argc, &error); } else { const gchar *arg1, *arg2, *arg3; switch (argc) { case 0: show_history (client, oneline, raw, zero, &error); break; case 1: arg1 = argv[0]; if (!g_strcmp0 (arg1, "about")) g_paste_client_about_sync (client, &error); else if (!g_strcmp0 (arg1, "dr") || !g_strcmp0 (arg1, "daemon-reexec")) { g_paste_client_reexecute_sync (client, &error); if (error && error->code == G_DBUS_ERROR_NO_REPLY) { printf (_("Successfully reexecuted the daemon\n")); return EXIT_SUCCESS; } } else if (!g_strcmp0 (arg1, "dv") || !g_strcmp0 (arg1, "daemon-version")) { g_autofree gchar *v = g_paste_client_get_version (client); printf ("%s\n", v); } else if (!g_strcmp0 (arg1, "e") || !g_strcmp0 (arg1, "empty")) { g_paste_client_empty_sync (client, &error); } else if (!g_strcmp0 (arg1, "h") || !g_strcmp0 (arg1, "history")) { show_history (client, oneline, raw, zero, &error); } else if (!g_strcmp0 (arg1, "hs") || !g_strcmp0 (arg1, "history-size")) { guint32 size = g_paste_client_get_history_size_sync (client, &error); if (!error) printf ("%u\n", size); } else if (!g_strcmp0 (arg1, "lh") || !g_strcmp0 (arg1, "list-histories")) { g_auto (GStrv) histories = g_paste_client_list_histories_sync (client, &error); if (!error) { for (GStrv h = histories; *h; ++h) printf ("%s\n", *h); } } else if (!g_strcmp0 (arg1, "settings") || !g_strcmp0 (arg1, "p") || !g_strcmp0 (arg1, "preferences")) { if (!g_paste_util_activate_ui_sync ("prefs", NULL, &error)) { g_critical ("%s Ui: %s", _("Couldn't spawn"), error->message); return EXIT_FAILURE; } } else if (!g_strcmp0 (arg1, "show-history")) { g_paste_client_show_history_sync (client, &error); } else if (!g_strcmp0 (arg1, "start") || !g_strcmp0 (arg1, "d") || !g_strcmp0 (arg1, "daemon")) { g_paste_client_track_sync (client, TRUE, &error); } else if (!g_strcmp0 (arg1, "stop") || !g_strcmp0 (arg1, "q") || !g_strcmp0 (arg1, "quit")) { g_paste_client_track_sync (client, FALSE, &error); } else if (!g_strcmp0 (arg1, "ui")) { status = spawn ("Ui"); } else if (g_paste_util_has_applet () && !g_strcmp0 (arg1, "applet")) { status = spawn ("Applet"); } else if (g_paste_util_has_unity () && !g_strcmp0 (arg1, "app-indicator")) { status = spawn ("AppIndicator"); } else { show_help (); status = EXIT_FAILURE; } break; case 2: arg1 = argv[0]; arg2 = argv[1]; if (!g_strcmp0 (arg1, "a") || !g_strcmp0 (arg1, "add")) { g_paste_client_add_sync (client, arg2, &error); } else if (!g_strcmp0 (arg1, "bh") || !g_strcmp0 (arg1, "backup-history")) { g_paste_client_backup_history_sync (client, arg2, &error); } else if (!g_strcmp0 (arg1, "d") || !g_strcmp0 (arg1, "del") || !g_strcmp0 (arg1, "delete") || !g_strcmp0 (arg1, "rm") || !g_strcmp0 (arg1, "remove")) { g_paste_client_delete_sync (client, _strtoull (arg2), &error); } else if (!g_strcmp0 (arg1, "dp") || !g_strcmp0 (arg1, "delete-password")) { g_paste_client_delete_password_sync (client, arg2, &error); } else if (!g_strcmp0 (arg1, "dh") || !g_strcmp0 (arg1, "delete-history")) { g_paste_client_delete_history_sync (client, arg2, &error); } else if (!g_strcmp0 (arg1, "f") || !g_strcmp0 (arg1, "file")) { g_paste_client_add_file_sync (client, arg2, &error); } else if (!g_strcmp0 (arg1, "g") || !g_strcmp0 (arg1, "get")) { const gchar *value = (!raw) ? g_paste_client_get_element_sync (client, _strtoull (arg2), &error) : g_paste_client_get_raw_element_sync (client, _strtoull (arg2), &error); if (!error) printf ("%s", value); } else if (!g_strcmp0 (arg1, "search")) { gsize hits; g_autofree guint32 *results = g_paste_client_search_sync (client, arg2, &hits, &error); if (!error) { if (hits > 0) { for (gsize i = 0; i < hits; ++i) { guint32 index = results[i]; gchar *line = g_paste_client_get_element_sync (client, index, &error); if (error) break; print_history_line (line, index, oneline, raw, zero); } } } } else if (!g_strcmp0 (arg1, "s") || !g_strcmp0 (arg1, "set") || !g_strcmp0 (arg1, "select")) { g_paste_client_select_sync (client, _strtoull (arg2), &error); } else if (!g_strcmp0 (arg1, "sh") || !g_strcmp0 (arg1, "switch-history")) { g_paste_client_switch_history_sync (client, arg2, &error); } else if (!g_strcmp0 (arg1, "u") || !g_strcmp0 (arg1, "upload")) { g_paste_client_upload_sync (client, _strtoull (arg2), &error); } else { show_help (); status = EXIT_FAILURE; } break; case 3: arg1 = argv[0]; arg2 = argv[1]; arg3 = argv[2]; if (!g_strcmp0 (arg1, "ap") || !g_strcmp0 (arg1, "add-password")) { g_paste_client_add_password_sync (client, arg2, arg3, &error); } else if (!g_strcmp0 (arg1, "rp") || !g_strcmp0 (arg1, "rename-password")) { g_paste_client_rename_password_sync (client, arg2, arg3, &error); } else if (!g_strcmp0 (arg1, "sp") || !g_strcmp0 (arg1, "set-password")) { g_paste_client_set_password_sync (client, _strtoull (arg2), arg3, &error); } else { show_help (); status = EXIT_FAILURE; } break; default: show_help (); status = EXIT_FAILURE; break; } } if (error) failure_exit (error); return status; }
gint main (gint argc, gchar *argv[]) { G_PASTE_INIT_GETTEXT (); if (argc > 1) { if (is_help (argv[1])) { show_help (argv[0]); return EXIT_SUCCESS; } else if (is_version (argv[1])) { show_version (); return EXIT_SUCCESS; } } int status = EXIT_SUCCESS; G_PASTE_CLEANUP_ERROR_FREE GError *error = NULL; G_PASTE_CLEANUP_UNREF GPasteClient *client = g_paste_client_new_sync (&error); if (!client) failure_exit (error); if (!isatty (fileno (stdin))) { /* We are being piped */ G_PASTE_CLEANUP_STRING_FREE GString *data = g_string_new (""); gchar c; while ((c = fgetc (stdin)) != EOF) data = g_string_append_c (data, c); data->str[data->len - 1] = '\0'; g_paste_client_add_sync (client, data->str, &error); } else { const gchar *arg1, *arg2; switch (argc) { case 1: show_history (client, FALSE, FALSE, &error); break; case 2: arg1 = argv[1]; if (!g_strcmp0 (arg1, "start") || !g_strcmp0 (arg1, "d") || !g_strcmp0 (arg1, "daemon")) { g_paste_client_track_sync (client, TRUE, &error); } else if (!g_strcmp0 (arg1, "stop") || !g_strcmp0 (arg1, "q") || !g_strcmp0 (arg1, "quit")) { g_paste_client_track_sync (client, FALSE, &error); } else if (!g_strcmp0 (arg1, "e") || !g_strcmp0 (arg1, "empty")) { g_paste_client_empty_sync (client, &error); } #if G_PASTE_CONFIG_ENABLE_APPLET else if (!g_strcmp0 (arg1, "applet")) { g_spawn_command_line_async (PKGLIBEXECDIR "/gpaste-applet", &error); if (error) { g_critical ("%s: %s", _("Couldn't spawn gpaste-applet.\n"), error->message); g_clear_error (&error); status = EXIT_FAILURE; } } #endif else if (!g_strcmp0 (arg1, "s") || !g_strcmp0 (arg1, "settings") || !g_strcmp0 (arg1, "p") || !g_strcmp0 (arg1, "preferences")) { execl (PKGLIBEXECDIR "/gpaste-settings", "GPaste-Settings", NULL); } else if (!g_strcmp0 (arg1, "dr") || !g_strcmp0 (arg1, "daemon-reexec")) { g_paste_client_reexecute_sync (client, &error); if (error && error->code == G_DBUS_ERROR_NO_REPLY) { printf (_("Successfully reexecuted the daemon\n")); return EXIT_SUCCESS; } } else if (!g_strcmp0 (arg1, "h") || !g_strcmp0 (arg1, "history")) { show_history (client, FALSE, FALSE, &error); } else if (!g_strcmp0 (arg1, "hs") || !g_strcmp0 (arg1, "history-size")) { guint32 size = g_paste_client_get_history_size_sync (client, &error); if (!error) printf ("%u\n", size); } else if (!g_strcmp0 (arg1, "rh") || !g_strcmp0 (arg1, "raw-history")) { show_history (client, TRUE, FALSE, &error); } else if (!g_strcmp0 (arg1, "zh") || !g_strcmp0 (arg1, "zero-history")) { show_history (client, FALSE, TRUE, &error); } else if (!g_strcmp0 (arg1, "lh") || !g_strcmp0 (arg1, "list-histories")) { G_PASTE_CLEANUP_STRFREEV GStrv histories = g_paste_client_list_histories_sync (client, &error); if (!error) { for (GStrv h = histories; *h; ++h) printf ("%s\n", *h); } } else if (!g_strcmp0 (argv[1], "about")) { g_paste_client_about_sync (client, &error); } else { show_help (argv[0]); status = EXIT_FAILURE; } break; case 3: arg1 = argv[1]; arg2 = argv[2]; if (!g_strcmp0 (arg1, "bh")|| !g_strcmp0 (arg1, "backup-history")) { g_paste_client_backup_history_sync (client, arg2, &error); } else if (!g_strcmp0 (arg1, "sh") || !g_strcmp0 (arg1, "switch-history")) { g_paste_client_switch_history_sync (client, arg2, &error); } else if (!g_strcmp0 (arg1, "dh") || !g_strcmp0 (arg1, "delete-history")) { g_paste_client_delete_history_sync (client, arg2, &error); } else if (!g_strcmp0 (arg1, "a") || !g_strcmp0 (arg1, "add")) { g_paste_client_add_sync (client, arg2, &error); } else if (!g_strcmp0 (arg1, "g")|| !g_strcmp0 (arg1, "get")) { printf ("%s", g_paste_client_get_element_sync (client, g_ascii_strtoull (arg2, NULL, 0), &error)); } else if (!g_strcmp0 (arg1, "s") || !g_strcmp0 (arg1, "set") || !g_strcmp0 (arg1, "select")) { g_paste_client_select_sync (client, g_ascii_strtoull (arg2, NULL, 0), &error); } else if (!g_strcmp0 (arg1, "d") || !g_strcmp0 (arg1, "delete")) { g_paste_client_delete_sync (client, g_ascii_strtoull (arg2, NULL, 0), &error); } else if (!g_strcmp0 (arg1, "f") || !g_strcmp0 (arg1, "file")) { g_paste_client_add_file_sync (client, arg2, &error); } else { show_help (argv[0]); status = EXIT_FAILURE; } break; default: show_help (argv[0]); status = EXIT_FAILURE; break; } } if (error) failure_exit (error); return status; }