]> git.draconx.ca Git - upkg.git/blobdiff - src/upkg.c
upkg: Add gnulib getopt_long support and implement --help/--version.
[upkg.git] / src / upkg.c
index 02d8e5737e1a2c2ceab18c9d12425341b3cb940c..61f67a48f14a08c18b23c629ef7b0a0d01a85217 100644 (file)
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <getopt.h>
+
+#include <glib-object.h>
+
 #include "upkg.h"
+#include "module.h"
+#include "uobject.h"
+#include "exportable.h"
+
+static const char *progname = "upkg";
+
+static const char *sopts = "VH";
+static const struct option lopts[] = {
+       { "version",  0, NULL, 'V' },
+       { "help",     0, NULL, 'H' },
+       { 0 }
+};
+
+void print_version(void)
+{
+       printf("%s\n", PACKAGE_STRING);
+       puts("\
+Copyright (C) 2009 Nick Bowler.\n\
+This is free software: you are free to change and redistribute it.\n\
+There is NO WARRANTY, to the extent permitted by law."
+       );
+}
+
+void print_usage(void)
+{
+       printf("Usage: %s [options] package\n", progname);
+}
+
+void print_help(void)
+{
+       print_usage();
+       puts("Detailed help coming soon.  Until then, I'll just list my options.");
+       for (unsigned i = 0; lopts[i].name; i++) {
+               const struct option *o = &lopts[i];
+               printf("\t--%s", o->name);
+
+               if (o->has_arg == 1) {
+                       printf("=val");
+               } else if (o->has_arg == 2) {
+                       printf("[=val]");
+               }
+
+               printf(" (-%c)\n", o->val);
+       }
+}
 
 void print_upkg_flags(const char *prefix, unsigned long flags)
 {
@@ -37,16 +87,46 @@ void print_upkg_flags(const char *prefix, unsigned long flags)
                printf("%sRequired\n", prefix);
 }
 
+void print_guid(unsigned char guid[static 16])
+{
+       for (unsigned i = 0; i < 16; i++) {
+               printf("%02hhx", guid[i]);
+               if (i == 15)
+                       putchar('\n');
+               else
+                       putchar(' ');
+       }
+}
 
 int main(int argc, char **argv)
 {
        struct upkg *pkg;
+       int opt;
+
+       if (argc > 0) progname = argv[0];
+
+       while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
+               switch (opt) {
+               case 'V':
+                       print_version();
+                       return EXIT_SUCCESS;
+               case 'H':
+                       print_help();
+                       return EXIT_SUCCESS;
+               default:
+                       print_usage();
+                       return EXIT_FAILURE;
+               }
+       }
 
        if (argc < 2) {
                fprintf(stderr, "usage: upkg file\n");
                return EXIT_FAILURE;
        }
 
+       if (module_init() != 0)
+               return EXIT_FAILURE;
+
        pkg = upkg_fopen(argv[1]);
        if (!pkg) {
                fprintf(stderr, "failed to open package!\n");
@@ -55,6 +135,8 @@ int main(int argc, char **argv)
 
        printf("Version: %u\n",  pkg->version);
        printf("License: %u\n",  pkg->license);
+       printf("GUID: ");
+       print_guid(pkg->guid);
 
        printf("Flags:   %lx\n", pkg->flags);
        print_upkg_flags("\t", pkg->flags);
@@ -67,6 +149,22 @@ int main(int argc, char **argv)
        printf("Exports: %lu\n", pkg->export_count);
        printf("Imports: %lu\n", pkg->import_count);
 
+       GObject *music = g_object_new(module_get_class("Engine", "Music"), NULL);
+       if (!music)
+               return EXIT_FAILURE;
+       struct upkg_file *f = upkg_export_open(pkg, 0);
+       u_object_deserialize(music, f);
+
+       char name[256];
+       u_object_export_name(music, name, sizeof name);
+       FILE *fp = fopen(name, "wb");
+       if (!fp) return EXIT_FAILURE;
+       u_object_export(music, fp);
+       fclose(fp);
+       printf("Wrote %s\n", name);
+
        upkg_close(pkg);
+       module_exit();
+
        return 0;
 }