summaryrefslogtreecommitdiff
path: root/tools/eos-profile-tool/eos-profile-main.c
blob: 8a639cba7f7380a1d5e7f79e5f29c604c96e7518 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include <locale.h>
#include <glib.h>
#include <glib/gi18n.h>

#include "eos-profile-cmds.h"

static const EosProfileCmd profile_commands[] = {
  {
    .name = "help",
    .description = "Prints this help string",
    .usage = NULL,
    .parse_args = eos_profile_cmd_help_parse_args,
    .main = eos_profile_cmd_help_main,
  },
  {
    .name = "show",
    .description = "Prints a report from a capture file",
    .usage = "show <FILE> [FILE…]",
    .parse_args = eos_profile_cmd_show_parse_args,
    .main = eos_profile_cmd_show_main,
  },
  {
    .name = "convert",
    .description = "Converts a capture file to another format",
    .usage = "convert [OPTIONS…] <FILE>",
    .parse_args = eos_profile_cmd_convert_parse_args,
    .main = eos_profile_cmd_convert_main,
  },
  {
    .name = "diff",
    .description = "Compares FILES",
    .usage = "diff [OPTIONS…] <FILES>",
    .parse_args = eos_profile_cmd_diff_parse_args,
    .main = eos_profile_cmd_diff_main,
  },
};

void
eos_profile_foreach_cmd (EosProfileCmdCallback cb,
                         gpointer              data)
{
  for (int i = 0; i < G_N_ELEMENTS (profile_commands); i++)
    {
      if (cb (&profile_commands[i], data))
        break;
    }
}

int
main (int argc,
      char *argv[])
{
  setlocale (LC_ALL, "");
  textdomain (GETTEXT_PACKAGE);

  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");

  const char *cmd = NULL;

  if (argc < 2 ||
      g_strcmp0 (argv[1], "-h") == 0 ||
      g_strcmp0 (argv[1], "-?") == 0 ||
      g_strcmp0 (argv[1], "--help") == 0)
    {
      cmd = "help";
    }
  else
    cmd = argv[1];

  for (int i = 0; i < G_N_ELEMENTS (profile_commands); i++)
    {
      if (g_strcmp0 (cmd, profile_commands[i].name) == 0)
        {
          argc -= 1;
          argv += 1;

          if (!profile_commands[i].parse_args (argc, argv))
            {
              const char *usage = profile_commands[i].usage;

              if (usage == NULL)
                usage = profile_commands[i].name;

              g_printerr ("Usage: eos-profile %s\n", usage);

              return EXIT_FAILURE;
            }

          return profile_commands[i].main ();
        }
    }

  g_printerr ("Usage: eos-profile <COMMAND> [OPTIONS...]\n");

  return EXIT_FAILURE;
}