]> mj.ucw.cz Git - paperjam.git/blobdiff - paperjam.cc
Implemented clip
[paperjam.git] / paperjam.cc
index e5648e1efeca17b2305bb9459d24c1d0fb4fbb4e..5aa4c6139fd76814a3d6297ced7a339ebd1201eb 100644 (file)
+/*
+ *     PaperJam -- Main program
+ *
+ *     (c) 2018 Martin Mares <mj@ucw.cz>
+ */
+
 #include <cassert>
 #include <cstdarg>
 #include <cstdlib>
 #include <cstdio>
+#include <getopt.h>
 
 #include "jam.h"
 
+/*** Options ***/
+
+const char *in_name;
+const char *out_name;
+bool recalc_bbox;
+int debug_mode;
+int debug_indent;
+
+/*** Messages ***/
+
+void debug(const char *msg, ...)
+{
+  if (!debug_mode)
+    return;
+  va_list args;
+  va_start(args, msg);
+  fprintf(stderr, "%*s", debug_indent, "");
+  vfprintf(stderr, msg, args);
+  fputc('\n', stderr);
+  va_end(args);
+}
+
+void warn(const char *msg, ...)
+{
+  va_list args;
+  va_start(args, msg);
+  fprintf(stderr, "Warning: ");
+  vfprintf(stderr, msg, args);
+  fputc('\n', stderr);
+  va_end(args);
+}
+
+void die(const char *msg, ...)
+{
+  va_list args;
+  va_start(args, msg);
+  vfprintf(stderr, msg, args);
+  fputc('\n', stderr);
+  va_end(args);
+  exit(1);
+}
+
+/*** Arguments ***/
+
+enum opt {
+  OPT_HELP = 256,
+  OPT_VERSION,
+};
+
+static const struct option long_opts[] = {
+  { "debug",   0, 0, 'd' },
+  { "help",    0, 0, OPT_HELP },
+  { "version", 0, 0, OPT_VERSION },
+  { "bbox",    0, 0, 'b' },
+  { 0,         0, 0, 0 }
+};
+
+static const char short_opts[] = "bd";
+
+static void usage()
+{
+  printf("Usage: paperjam [<options>] <commands> <in> <out>\n\
+\n\
+Options:\n\
+-b, --bbox             Recalculate bounding boxes\n\
+-d, --debug            Show debugging messages\n\
+\n\
+Commands:\n\
+");
+  parser_help();
+}
+
 int main(int argc, char **argv)
 {
-  if (argc != 4)
-    {
-      fprintf(stderr, "Usage: pdfjam <commands> <input> <output>\n");
-      return 1;
-    }
+  int c;
+  while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) >= 0)
+    switch (c)
+      {
+      case 'b':
+       recalc_bbox = 1;
+       break;
+      case 'd':
+       debug_mode++;
+       break;
+      case OPT_VERSION:
+       printf("This is paperjam with no version yet.\n");      // FIXME
+       return 0;
+      case OPT_HELP:
+       usage();
+       return 0;
+      default:
+       exit(1);
+      }
+
+  if (optind + 3 != argc)
+    die("Exactly three positional parameters should be given");
 
   list<cmd *> cmds;
-  parse(argv[1], &cmds);
+  parse(argv[optind], cmds);
+  in_name = argv[optind+1];
+  out_name = argv[optind+2];
+
+  try
+    {
+      process(cmds);
+    }
+  catch (exception& e)
+    {
+      die("%s", e.what());
+    }
 
   return 0;
 }