summaryrefslogtreecommitdiff
path: root/misc/xine-list.c
blob: ec7b7f694548c849cf1a06de6dc1139aba46ef2f (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * Copyright (C) 2008 the xine-project
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#include <xine.h>

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

#define XINE_LIST_VERSION_N(x,y) #x"."#y
#define XINE_LIST_VERSION XINE_LIST_VERSION_N(XINE_MAJOR_VERSION,XINE_MINOR_VERSION)

int main (int argc, char *argv[])
{
  int optstate = 0;
  int which = 'm';
  int lf = 0;

  for (;;)
  {
#define OPTS "hvaemp"
#ifdef HAVE_GETOPT_LONG
    static const struct option longopts[] = {
      { "help", no_argument, NULL, 'h' },
      { "version", no_argument, NULL, 'v' },
      { "mime-types", no_argument, NULL, 'm' },
      { "extensions", no_argument, NULL, 'e' },
      { "all", no_argument, NULL, 'a' },
      { NULL }
    };
    int index = 0;
    int opt = getopt_long (argc, argv, OPTS, longopts, &index);
#else
    int opt = getopt(argc, argv, OPTS);
#endif
    if (opt == -1)
      break;

    switch (opt)
    {
    case 'h':
      optstate |= 1;
      break;
    case 'v':
      optstate |= 4;
      break;
    case 'a':
    case 'e':
    case 'm':
      which = opt;
      break;
    case 'p':
      lf = 1;
      break;
    default:
      optstate |= 2;
      break;
    }
  }

  if (optstate & 1)
    printf ("\
xine-list-"XINE_LIST_VERSION" %s\n\
using xine-lib %s\n\
usage: %s [options]\n\
options:\n\
  -h, --help		this help text\n\
  -m, --mime-types	list just the supported MIME types\n\
  -e, --extensions	list just the recognised filename extensions\n\
  -a, --all		list everything\n\
  -p, --pretty-print	add line feeds\n\
\n", XINE_VERSION, xine_get_version_string (), argv[0]);
  else if (optstate & 4)
    printf ("\
xine-list %s\n\
using xine-lib %s\n\
(c) 2008 the xine project team\n\
This is free software; see the source for copying conditions.  There is NO\n\
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,\n\
to the extent permitted by law.\n",
	     XINE_VERSION, xine_get_version_string ());

  if (optstate & 2)
  {
    fputs ("xine-list: invalid option (try -h or --help)\n", stderr);
    return 1;
  }

  if (optstate)
    return 0;

  xine_t *xine = xine_new ();
  xine_init (xine);

  char *text = NULL;
  char *sep, *sep2;
  switch (which)
  {
  case 'a':
  case 'm':
    text = xine_get_mime_types (xine);
    if (!text || !*text)
      goto read_fail;
    sep = sep2 = text - 1;
    for (;;)
    {
      text = sep + 1;
      sep = strchr (text, ';') ? : text + strlen (text);
      sep2 = which == 'a' ? sep : strchr (text, ':') ? : sep;
      if (!*sep)
        break;
      if (printf ("%.*s;", (int)(sep2 - text), text) < 0 || (lf && puts ("") < 0))
        goto write_fail;
    }
    break;

  case 'e':
    text = xine_get_file_extensions (xine);
    if (!text || !*text)
      goto read_fail;
    sep = text - 1;
    do
    {
      text = sep + 1;
      sep = strchr (text, ' ') ? : text + strlen (text);
      if (sep[-1] != '/' &&
          printf ("%.*s%s", (int)(sep - text), text, lf ? "\n" : *sep ? " " : "") < 0)
        goto write_fail;
    } while (*sep);
    break;
  }

  return 0;

 read_fail:
  fputs ("xine-list: failed to read types info\n", stderr);
  return 1;

 write_fail:
  perror ("xine-list");
  return 1;
}