コード例 #1
0
static VexEndness arch_endness (VexArch va) {
   switch (va) {
   case VexArch_INVALID: failure_exit();
   case VexArchX86:    return VexEndnessLE;
   case VexArchAMD64:  return VexEndnessLE;
   case VexArchARM:    return VexEndnessLE;
   case VexArchARM64:  return VexEndnessLE;
   case VexArchPPC32:  return VexEndnessBE;
   case VexArchPPC64:
      /* ppc64 supports BE or LE at run time. So, on a LE system,
         returns LE, on a BE system, return BE. */
      return running_endness();
   case VexArchS390X:  return VexEndnessBE;
   case VexArchMIPS32:
   case VexArchMIPS64:
      /* mips32/64 supports BE or LE, but at compile time.
         If mips64 is compiled on a non mips system, the VEX lib
         is missing bit and pieces of code related to endianess.
         The mandatory code for this test is then compiled as BE.
         So, if this test runs on a mips system, returns the
         running endianess. Otherwise, returns BE as this one
         has the more chances to work. */
      {
         VexArch ga;
         get_guest_arch( &ga);

         if (ga == VexArchMIPS64 || ga == VexArchMIPS32)
            return running_endness();
         else
            return VexEndnessBE;
      }
   case VexArchTILEGX: return VexEndnessLE;
   default: failure_exit();
   }
}
コード例 #2
0
static Bool mode64 (VexArch va) {
   switch (va) {
   case VexArch_INVALID: failure_exit();
   case VexArchX86:    return False;
   case VexArchAMD64:  return True;
   case VexArchARM:    return False;
   case VexArchARM64:  return True;
   case VexArchPPC32:  return False;
   case VexArchPPC64:  return True;
   case VexArchS390X:  return True;
   case VexArchMIPS32: return False;
   case VexArchMIPS64: return True;
   case VexArchTILEGX: return True;
   default: failure_exit();
   }
}
コード例 #3
0
/* returns whatever kind of hwcaps needed to make
   the host and/or guest VexArch happy. */
static UInt arch_hwcaps (VexArch va) {
   switch (va) {
   case VexArch_INVALID: failure_exit();
   case VexArchX86:    return 0;
   case VexArchAMD64:  return 0;
   case VexArchARM:    return 7;
   case VexArchARM64:  return 0;
   case VexArchPPC32:  return 0;
   case VexArchPPC64:  return 0;
   case VexArchS390X:  return VEX_HWCAPS_S390X_LDISP;
   case VexArchMIPS32: return 0;
   case VexArchMIPS64: return 0;
   case VexArchTILEGX: return 0;
   default: failure_exit();
   }
}
コード例 #4
0
static VexEndness arch_endness (VexArch va) {
   switch (va) {
   case VexArch_INVALID: failure_exit();
   case VexArchX86:    return VexEndnessLE;
   case VexArchAMD64:  return VexEndnessLE;
   case VexArchARM:    return VexEndnessLE;
   case VexArchARM64:  return VexEndnessLE;
   case VexArchPPC32:  return VexEndnessBE;
   case VexArchPPC64:  return VexEndnessBE;
   case VexArchS390X:  return VexEndnessBE;
   case VexArchMIPS32: return VexEndnessBE;
   case VexArchMIPS64: return VexEndnessBE;
   case VexArchTILEGX: return VexEndnessLE;
   default: failure_exit();
   }
}
コード例 #5
0
ファイル: gpaste.c プロジェクト: sebastianst/GPaste
int
main (int argc, char *argv[])
{
    bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
    bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
    textdomain (GETTEXT_PACKAGE);

    int status = EXIT_SUCCESS;

    g_type_init ();

    GPasteClient *client = g_paste_client_new ();
    GError *error = NULL;
    const gchar *arg1, *arg2;

    if (!client)
    {
        if (argc == 2 && is_help (argv[1]))
        {
            show_help (argv[0]);
            return EXIT_SUCCESS;
        }

        failure_exit ();
    }

    if (!isatty (fileno (stdin)))
    {
        /* We are being piped */
        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 (client, data->str, &error);

        g_string_free (data, TRUE);
    }
    else
    {
        switch (argc)
        {
        case 1:
            show_history (client, FALSE, FALSE, &error);
            break;
        case 2:
            arg1 = argv[1];
            if (is_help (arg1))
            {
                show_help (argv[0]);
            }
            else if (g_strcmp0 (arg1, "start") == 0 ||
                     g_strcmp0 (arg1, "d") == 0 ||
                     g_strcmp0 (arg1, "daemon") == 0)
            {
                g_paste_client_track (client, TRUE, &error);
            }
            else if (g_strcmp0 (arg1, "stop") == 0 ||
                     g_strcmp0 (arg1, "q") == 0 ||
                     g_strcmp0 (arg1, "quit") == 0)
            {
                g_paste_client_track (client, FALSE, &error);
            }
            else if (g_strcmp0 (arg1, "e") == 0 ||
                     g_strcmp0 (arg1, "empty") == 0)
            {
                g_paste_client_empty (client, &error);
            }
            else if (g_strcmp0 (arg1, "v") == 0 ||
                     g_strcmp0 (arg1, "version") == 0 ||
                     g_strcmp0 (arg1, "-v") == 0 ||
                     g_strcmp0 (arg1, "--version") == 0)
            {
                printf ("%s\n", PACKAGE_STRING);
            }
#ifdef ENABLE_APPLET
            else if (g_strcmp0 (arg1, "applet") == 0)
            {
                g_spawn_command_line_async (PKGLIBEXECDIR "/gpaste-applet", &error);
                if (error)
                {
                    fprintf (stderr, _("Couldn't spawn gpaste-applet.\n"));
                    g_clear_error (&error);
                    status = EXIT_FAILURE;
                }
            }
#endif
            else if (g_strcmp0 (arg1, "s") == 0 ||
                     g_strcmp0 (arg1, "settings") == 0 ||
                     g_strcmp0 (arg1, "p") == 0 ||
                     g_strcmp0 (arg1, "preferences") == 0)
            {
                execl (PKGLIBEXECDIR "/gpaste-settings", "GPaste-Settings", NULL);
            }
            else if (g_strcmp0 (arg1, "dr") == 0 ||
                     g_strcmp0 (arg1, "daemon-reexec") == 0)
            {
                g_paste_client_reexecute (client, &error);
                if (error && error->code == G_DBUS_ERROR_NO_REPLY)
                {
                    printf (_("Successfully reexecuted the daemon\n"));
                }
            }
            else if (g_strcmp0 (arg1, "h") == 0 ||
                     g_strcmp0 (arg1, "history") == 0)
            {
                show_history (client, FALSE, FALSE, &error);
            }
            else if (g_strcmp0 (arg1, "rh") == 0 ||
                     g_strcmp0 (arg1, "raw-history") == 0)
            {
                show_history (client, TRUE, FALSE, &error);
            }
            else if (g_strcmp0 (arg1, "zh") == 0 ||
                     g_strcmp0 (arg1, "zero-history") == 0)
            {
                show_history (client, FALSE, TRUE, &error);
            }
            else if (g_strcmp0 (arg1, "lh") == 0 ||
                     g_strcmp0 (arg1, "list-histories") == 0)
            {
                gchar **histories = g_paste_client_list_histories (client, &error);
                if (!error)
                {
                    for (gchar **h = histories; *h; ++h)
                        printf ("%s\n", *h);
                    g_strfreev (histories);
                }
            }
            else
            {
                show_help (argv[0]);
                status = EXIT_FAILURE;
            }
            break;
        case 3:
            arg1 = argv[1];
            arg2 = argv[2];
            if (g_strcmp0 (arg1, "bh") == 0||
                g_strcmp0 (arg1, "backup-history") == 0)
            {
                g_paste_client_backup_history (client, arg2, &error);
            }
            else if (g_strcmp0 (arg1, "sh") == 0 ||
                     g_strcmp0 (arg1, "switch-history") == 0)
            {
                g_paste_client_switch_history (client, arg2, &error);
            }
            else if (g_strcmp0 (arg1, "dh") == 0 ||
                     g_strcmp0 (arg1, "delete-history") == 0)
            {
                g_paste_client_delete_history (client, arg2, &error);
            }
            else if (g_strcmp0 (arg1, "a") == 0 ||
                     g_strcmp0 (arg1, "add") == 0)
            {
                g_paste_client_add (client, arg2, &error);
            }
            else if (g_strcmp0 (arg1, "g") == 0||
                     g_strcmp0 (arg1, "get") == 0)
            {
                printf ("%s", g_paste_client_get_element (client, g_ascii_strtoull (arg2, NULL, 0), &error));
            }
            else if (g_strcmp0 (arg1, "s") == 0 ||
                     g_strcmp0 (arg1, "set") == 0 ||
                     g_strcmp0 (arg1, "select") == 0)
            {
                g_paste_client_select (client, g_ascii_strtoull (arg2, NULL, 0), &error);
            }
            else if (g_strcmp0 (arg1, "d") == 0 ||
                     g_strcmp0 (arg1, "delete") == 0)
            {
                g_paste_client_delete (client, g_ascii_strtoull (arg2, NULL, 0), &error);
            }
            else if (g_strcmp0 (arg1, "f") == 0 ||
                     g_strcmp0 (arg1, "file") == 0)
            {
               gchar *content = NULL;

               if (g_file_get_contents (arg2,
                                        &content,
                                        NULL, /* length */
                                        &error))
               {
                   g_paste_client_add (client, content, &error);
                   g_free (content);
               }
               else
               {
                   fprintf (stderr, _("Could not read file: %s\n"), arg2);
                   g_clear_error (&error);
                   status = EXIT_FAILURE;
               }
            }
            else
            {
                show_help (argv[0]);
                status = EXIT_FAILURE;
            }
            break;
        default:
            show_help (argv[0]);
            status = EXIT_FAILURE;
            break;
        }
    }

    if (error)
    {
        g_error_free (error);
        failure_exit ();
    }

    g_object_unref (client);

    return status;
}
コード例 #6
0
ファイル: gpaste.c プロジェクト: kapouer/GPaste
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;
}
コード例 #7
0
ファイル: gpaste.c プロジェクト: kghost/GPaste
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;
}
コード例 #8
0
ファイル: gpaste.c プロジェクト: stunttastic/GPaste
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;
}