summaryrefslogtreecommitdiff
path: root/tools/osinfo-query.c
blob: fb4aa7979d07bb858b1da668491dd42d75e26b59 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/*
 * Copyright (C) 2012 Red Hat, Inc
 *
 * osinfo-query: query the contents of the database
 *
 * 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, see <http://www.gnu.org/licenses/>
 */

#include <glib.h>
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <locale.h>
#include <glib/gi18n.h>

#include <osinfo/osinfo.h>


struct OsinfoLabel {
    const gchar *prop;
    const gchar *label;
    gboolean enabled;
    gsize width;
};

typedef OsinfoList * (*osinfo_list_func)(OsinfoDb *db);

struct OsinfoType {
    const gchar *name;
    osinfo_list_func listFunc;
    GType entityType;
    GType filterType;
    GType listType;
    struct OsinfoLabel *labels;
};

static struct OsinfoLabel os_labels[] = {
    { OSINFO_PRODUCT_PROP_SHORT_ID,
      N_("Short ID"), TRUE, 20 },
    { OSINFO_PRODUCT_PROP_NAME,
      N_("Name"), TRUE, 50 },
    { OSINFO_PRODUCT_PROP_VERSION,
      N_("Version"), TRUE, 8 },
    { OSINFO_OS_PROP_FAMILY,
      N_("Family"), FALSE, 12 },
    { OSINFO_OS_PROP_DISTRO,
      N_("Distro"), FALSE, 12 },
    { OSINFO_PRODUCT_PROP_VENDOR,
      N_("Vendor"), FALSE, 25 },
    { OSINFO_PRODUCT_PROP_RELEASE_DATE,
      N_("Release date"), FALSE, 12 },
    { OSINFO_PRODUCT_PROP_EOL_DATE,
      N_("End of life"), FALSE, 12 },
    { OSINFO_PRODUCT_PROP_CODENAME,
      N_("Code name"), FALSE, 10 },
    { OSINFO_ENTITY_PROP_ID,
      N_("ID"), TRUE, 40 },
    { NULL, NULL, 0, 0},
};

static struct OsinfoLabel platform_labels[] = {
    { OSINFO_PRODUCT_PROP_SHORT_ID,
      N_("Short ID"), TRUE, 20 },
    { OSINFO_PRODUCT_PROP_NAME,
      N_("Name"), TRUE, 50 },
    { OSINFO_PRODUCT_PROP_VERSION,
      N_("Version"), TRUE, 8 },
    { OSINFO_PRODUCT_PROP_VENDOR,
      N_("Vendor"), TRUE, 25 },
    { OSINFO_PRODUCT_PROP_RELEASE_DATE,
      N_("Release date"), FALSE, 12 },
    { OSINFO_PRODUCT_PROP_EOL_DATE,
      N_("End of life"), FALSE, 12 },
    { OSINFO_PRODUCT_PROP_CODENAME,
      N_("Code name"), FALSE, 10 },
    { OSINFO_ENTITY_PROP_ID,
      N_("ID"), TRUE, 40 },
    { NULL, NULL, 0, 0 },
};

static struct OsinfoLabel device_labels[] = {
    { OSINFO_DEVICE_PROP_VENDOR,
      N_("Vendor"), TRUE, 20 },
    { OSINFO_DEVICE_PROP_VENDOR_ID,
      N_("Vendor ID"), TRUE, 12 },
    { OSINFO_DEVICE_PROP_PRODUCT,
      N_("Product"), TRUE, 20 },
    { OSINFO_DEVICE_PROP_PRODUCT_ID,
      N_("Product ID"), TRUE, 12 },
    { OSINFO_PRODUCT_PROP_NAME,
      N_("Name"), TRUE, 14 },
    { OSINFO_DEVICE_PROP_CLASS,
      N_("Class"), TRUE, 15 },
    { OSINFO_DEVICE_PROP_BUS_TYPE,
      N_("Bus"), TRUE, 8 },
    { OSINFO_ENTITY_PROP_ID,
      N_("ID"), TRUE, 40 },
    { NULL, NULL, 0, 0 },
};

static struct OsinfoLabel deployment_labels[] = {
    { OSINFO_ENTITY_PROP_ID,
      N_("ID"), TRUE, 40 },
    { NULL, NULL, 0, 0 },
};


static gboolean toggle_fields(struct OsinfoLabel *labels,
                              const gchar *fieldStr,
                              GError **error)
{
    gboolean ret = FALSE;
    gchar **fields;
    gsize i, j;

    if (!fieldStr)
        return TRUE;

    fields = g_strsplit(fieldStr, ",", 0);

    for (j = 0; labels[j].prop; j++) {
        labels[j].enabled = FALSE;
    }

    for (i = 0; fields[i] != NULL; i++) {
        gboolean found = FALSE;
        for (j = 0; labels[j].prop; j++) {
            if (g_str_equal(fields[i], labels[j].prop)) {
                labels[j].enabled = TRUE;
                found = TRUE;
            }
        }
        if (!found) {
            g_set_error(error, OSINFO_ERROR, 0,
                        _("Unknown property name %s"), fields[i]);
            goto cleanup;
        }
    }


    ret = TRUE;

 cleanup:
    g_strfreev(fields);
    return ret;
}

static gboolean build_filter(struct OsinfoLabel *labels,
                             OsinfoFilter *filter,
                             gint argc, char **argv,
                             GError **error)
{
    gboolean ret = FALSE;
    gsize i, j;

    for (i = 0; i < argc; i++) {
        const gchar *tmp = strchr(argv[i], '=');
        if (!tmp) {
            g_set_error(error, OSINFO_ERROR, 0, "%s", _("Syntax error in condition, expecting KEY=VALUE"));
            goto cleanup;
        }
        gchar *key = g_strndup(argv[i], tmp-argv[i]);
        gchar *val = g_strdup(tmp+1);
        gboolean found = FALSE;

        for (j = 0; labels[j].prop != NULL; j++) {
            if (g_str_equal(key, labels[j].prop))
                found = TRUE;
        }

        if (!found) {
            g_set_error(error, OSINFO_ERROR, 0,
                        _("Unknown property name %s"), key);
            goto cleanup;
        }

        osinfo_filter_add_constraint(filter, key, val);
        g_free(key);
        g_free(val);
    }

    ret = TRUE;
 cleanup:
    return ret;
}


static gint sort_entity(gconstpointer a,
                        gconstpointer b,
                        gpointer data)
{
    OsinfoEntity *entityA = OSINFO_ENTITY(a);
    OsinfoEntity *entityB = OSINFO_ENTITY(b);
    gchar *key = data;
    const gchar *valA;
    const gchar *valB;

    valA = osinfo_entity_get_param_value(entityA, key);
    valB = osinfo_entity_get_param_value(entityB, key);

    if (!valA && !valB)
        return 0;

    if (!valA && valB)
        return 1;

    if (valA && !valB)
        return 1;

    return strcmp(valA, valB);
}


static gboolean print_entity_text(OsinfoEntity *entity,
                                  const struct OsinfoLabel *labels)
{
    gsize i;
    gboolean first = TRUE;
    for (i = 0; labels[i].prop != NULL; i++) {
        gsize pad;
        gchar *padstr;
        const gchar *val = osinfo_entity_get_param_value(entity, labels[i].prop);
        if (!labels[i].enabled)
            continue;

        if (first)
            g_print(" ");
        else
            g_print(" | ");
        first = FALSE;

        if (val && (strlen(val) > labels[i].width))
            pad = 0;
        else
            pad = labels[i].width - (val ? strlen(val) : 0);

        padstr = g_new0(gchar, pad+1);
        padstr[pad] = '\0';

        if (pad)
            memset(padstr, ' ', pad);

        g_print("%s%s",
                val ? val : "", padstr);
        g_free(padstr);
    }
    g_print("\n");

    return FALSE;
}

static gboolean print_results_text(OsinfoList *list,
                                   const struct OsinfoLabel *labels,
                                   const gchar *sortKey)
{
    gboolean ret = FALSE;
    GList *entities = osinfo_list_get_elements(list);
    GList *tmp;
    gsize i;
    gboolean first = TRUE;

    tmp = entities = g_list_sort_with_data(entities, sort_entity,
                                           (gchar*)(sortKey ? sortKey :
                                                    labels[0].prop));

    for (i = 0; labels[i].prop != NULL; i++) {
        gsize pad;
        gchar *padstr;
        if (!labels[i].enabled)
            continue;

        if (first)
            g_print(" ");
        else
            g_print(" | ");
        first = FALSE;

        if (strlen(gettext(labels[i].label)) > labels[i].width)
            pad = 0;
        else
            pad = labels[i].width - strlen(gettext(labels[i].label));

        padstr = g_new0(gchar, pad+1);
        padstr[pad] = '\0';

        if (pad)
            memset(padstr, ' ', pad);

        g_print("%s%s",
                gettext(labels[i].label), padstr);
        g_free(padstr);
    }
    g_print("\n");

    first = TRUE;
    for (i = 0; labels[i].prop != NULL; i++) {
        gchar *padstr;
        if (!labels[i].enabled)
            continue;

        if (first)
            g_print("-");
        else
            g_print("-+-");
        first = FALSE;

        padstr = g_new0(gchar, labels[i].width+1);
        memset(padstr, '-', labels[i].width);
        padstr[labels[i].width] = '\0';

        g_print("%s", padstr);
        g_free(padstr);
    }
    g_print("\n");

    /* No match */
    if (tmp == NULL)
        goto end;

    while (tmp) {
        OsinfoEntity *entity = OSINFO_ENTITY(tmp->data);

        print_entity_text(entity, labels);
        tmp = tmp->next;
    }

    ret = TRUE;

end:
    g_list_free(entities);
    // cleanup:
    return ret;
}

gint main(gint argc, gchar **argv)
{
    GOptionContext *context;
    GError *error = NULL;
    gint ret = EXIT_FAILURE;
    const gchar *type = NULL;
    OsinfoLoader *loader = NULL;
    OsinfoDb *db = NULL;
    OsinfoList *entities = NULL;
    OsinfoFilter *filter = NULL;
    OsinfoList *results = NULL;
    struct OsinfoLabel *labels = NULL;
    gsize i;
    const gchar *sortKey = NULL;
    const gchar *fields = NULL;

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

    struct OsinfoType types[] = {
        { "os",
          (osinfo_list_func)osinfo_db_get_os_list,
          OSINFO_TYPE_OS,
          OSINFO_TYPE_PRODUCTFILTER,
          OSINFO_TYPE_OSLIST,
          os_labels },
        { "platform",
          (osinfo_list_func)osinfo_db_get_platform_list,
          OSINFO_TYPE_PLATFORM,
          OSINFO_TYPE_PRODUCTFILTER,
          OSINFO_TYPE_PLATFORMLIST,
          platform_labels },
        { "device",
          (osinfo_list_func)osinfo_db_get_device_list,
          OSINFO_TYPE_DEVICE,
          OSINFO_TYPE_FILTER,
          OSINFO_TYPE_DEVICELIST,
          device_labels },
        { "deployment",
          (osinfo_list_func)osinfo_db_get_deployment_list,
          OSINFO_TYPE_DEPLOYMENT,
          OSINFO_TYPE_FILTER,
          OSINFO_TYPE_DEPLOYMENTLIST,
          deployment_labels },
    };

    GOptionEntry entries[] = {
        { "sort", 's', 0, G_OPTION_ARG_STRING, &sortKey,
          _("Sort column"), NULL },
        { "fields", 'f', 0, G_OPTION_ARG_STRING, &fields,
          _("Display fields"), NULL },
        { NULL, 0, 0, 0, NULL, NULL, NULL }
    };


    context = g_option_context_new(_("- Query the OS info database"));

    g_option_context_add_main_entries(context, entries, GETTEXT_PACKAGE);

    if (!g_option_context_parse(context, &argc, &argv, &error)) {
        g_printerr(_("Error while parsing commandline options: %s\n"),
                   error->message);
        g_printerr("%s\n", g_option_context_get_help(context, FALSE, NULL));
        goto error;
    }

    if (argc < 2) {
        g_printerr(_("Missing data type parameter\n"));
        goto error;
    }

    type = argv[1];

    loader = osinfo_loader_new();
    osinfo_loader_process_default_path(loader, &error);
    if (error != NULL) {
        g_printerr(_("Error loading OS data: %s\n"), error->message);
        goto error;
    }

    db = osinfo_loader_get_db(loader);


    for (i = 0; i < (sizeof(types)/sizeof(types[0])); i++) {
        if (g_str_equal(types[i].name, type)) {
            entities = types[i].listFunc(db);
            filter = g_object_new(types[i].filterType, NULL);
            results = g_object_new(types[i].listType,
                                   "element-type", types[i].entityType,
                                   NULL);
            labels = types[i].labels;
        }
    }

    if (!entities) {
        g_printerr(_("Unknown type '%s' requested\n"), type);
        goto error;
    }

    if (!build_filter(labels, filter, argc-2, argv+2, &error)) {
        g_printerr(_("Unable to construct filter: %s\n"), error->message);
        goto error;
    }

    if (!toggle_fields(labels, fields, &error)) {
        g_printerr(_("Unable to set field visibility: %s\n"), error->message);
        goto error;
    }

    osinfo_list_add_filtered(results, entities, filter);

    if (!print_results_text(results, labels, sortKey)) {
        /*
         * The os wasn't found, no need to print an error,
         * just be sure to return EXIT_FAILURE.
         */
        goto error;
    }

    ret = EXIT_SUCCESS;

 error:
    g_clear_error(&error);
    g_option_context_free(context);

    if (entities)
        g_object_unref(entities);
    if (filter)
        g_object_unref(filter);
    if (results)
        g_object_unref(results);
    if (loader)
        g_object_unref(loader);

    return ret;
}

/*
=pod

=head1 NAME

osinfo-query - Query information in the database

=head1 SYNOPSIS

osinfo-query [OPTIONS...] TYPE [CONDITION-1 [CONDITION-2 ...]]

=head1 DESCRIPTION

The C<osinfo-query> command allows extraction of information from the
database. B<TYPE> can be one of C<os>, C<platform>, C<device>, or
C<deployment>. With no conditions specified, all entities of the given
type will be listed.

  # List all operating systems
  $ osinfo-query os
   Short ID             | Name       ...
  ----------------------+-----------
   centos-6.0           | CentOS 6.0 ...
   centos-6.1           | CentOS 6.1 ...
   ...

Conditions allow filtering based on specific properties of an entity.
For example, to filter only distros from the Fedora Project, use

  # List all operating systems
  $ osinfo-query os vendor="Fedora Project"
   Short ID             | Name          ...
  ----------------------+--------------
   fedora1              | Fedora Core 1 ...
   fedora2              | Fedora Core 2 ...
   ...

The set of fields which are printed can be controlled using the B<--fields>
command line argument:

  # List all operating systems
  $ osinfo-query --fields=short-id,version os vendor="Fedora Project"
   Short ID             | Version
  ----------------------+----------
   fedora1              | 1
   fedora2              | 2
   ...


=head1 OPTIONS

=over 8

=item B<-s PROPERTY>, B<--sort-key PROPERTY>

Set the data sorting key. Defaults sorting the first column

=item B<-f PROPERTY1,PROPERTY2,...>, B<--fields PROPERTY1,PROPERTY2,...>

Set the visibility of properties in output

=back

=head1 PROPERTY NAMES

=head2 OS

Valid property names for the C<os> type are:

=over 4

=item B<short-id>

The short OS identifier

=item B<name>

The long OS name

=item B<version>

The OS version string

=item B<family>

The OS kernel family

=item B<vendor>

The OS vendor

=item B<release-date>

The OS release date

=item B<eol-date>

The OS end-of-life date

=item B<codename>

The OS code name

=item B<id>

The OS identifier

=back


=head2 PLATFORM

Valid property names for the C<platform> type are:

=over 4

=item B<short-id>

The short platform identifier

=item B<name>

The long platform name

=item B<version>

The platform version string

=item B<vendor>

The platform vendor

=item B<release-date>

The platform release date

=item B<eol-date>

The platform end-of-life date

=item B<codename>

The platform code name

=item B<id>

The platform identifier

=back


=head2 DEVICE

Valid property names for the C<device> type are:

=over 4

=item B<name>

The device name

=item B<product>

The device product name

=item B<product-id>

The device product ID string

=item B<vendor>

The device vendor name

=item B<vendor-id>

The device vendor ID string

=item B<class>

The device type class

=item B<bus>

The device bus type

=item B<id>

The device identifier

=back


=head2 DEPLOYMENT

Valid property names for the C<deployment> type are:

=over 4

=item B<id>

The deployment identifier

=back


=head1 EXIT STATUS

The exit status will be 0 if matching entries were found,
or 1 if not matches were found

=head1 SEE ALSO

L<osinfo-db-validate(1)>, L<osinfo-detect(1)>

=head1 AUTHORS

Daniel P. Berrange <berrange@redhat.com>

=head1 COPYRIGHT

Copyright (C) 2012, 2014 Red Hat, Inc.

=head1 LICENSE

C<osinfo-query> is distributed under the termsof the GNU LGPL v2+
license. This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE

=cut
*/