]> mj.ucw.cz Git - paperjam.git/blob - paperjam.cc
Cropmarks
[paperjam.git] / paperjam.cc
1 /*
2  *      PaperJam -- Main program
3  *
4  *      (c) 2018 Martin Mares <mj@ucw.cz>
5  */
6
7 #include <cassert>
8 #include <cstdarg>
9 #include <cstdlib>
10 #include <cstdio>
11 #include <getopt.h>
12
13 #include "jam.h"
14
15 /*** Options ***/
16
17 const char *in_name;
18 const char *out_name;
19 bool recalc_bbox;
20 int debug_mode;
21 int debug_indent;
22
23 /*** Messages ***/
24
25 void debug(const char *msg, ...)
26 {
27   if (!debug_mode)
28     return;
29   va_list args;
30   va_start(args, msg);
31   fprintf(stderr, "%*s", debug_indent, "");
32   vfprintf(stderr, msg, args);
33   fputc('\n', stderr);
34   va_end(args);
35 }
36
37 void warn(const char *msg, ...)
38 {
39   va_list args;
40   va_start(args, msg);
41   fprintf(stderr, "Warning: ");
42   vfprintf(stderr, msg, args);
43   fputc('\n', stderr);
44   va_end(args);
45 }
46
47 void die(const char *msg, ...)
48 {
49   va_list args;
50   va_start(args, msg);
51   vfprintf(stderr, msg, args);
52   fputc('\n', stderr);
53   va_end(args);
54   exit(1);
55 }
56
57 /*** Arguments ***/
58
59 enum opt {
60   OPT_HELP = 256,
61   OPT_VERSION,
62 };
63
64 static const struct option long_opts[] = {
65   { "debug",    0, 0, 'd' },
66   { "help",     0, 0, OPT_HELP },
67   { "version",  0, 0, OPT_VERSION },
68   { "bbox",     0, 0, 'b' },
69   { 0,          0, 0, 0 }
70 };
71
72 static const char short_opts[] = "bd";
73
74 static void usage()
75 {
76   printf("Usage: paperjam [<options>] <commands> <in> <out>\n\
77 \n\
78 Options:\n\
79 -b, --bbox              Recalculate bounding boxes\n\
80 -d, --debug             Show debugging messages\n\
81 \n\
82 Commands:\n\
83 ");
84   parser_help();
85 }
86
87 int main(int argc, char **argv)
88 {
89   int c;
90   while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) >= 0)
91     switch (c)
92       {
93       case 'b':
94         recalc_bbox = 1;
95         break;
96       case 'd':
97         debug_mode++;
98         break;
99       case OPT_VERSION:
100         printf("This is paperjam with no version yet.\n");      // FIXME
101         return 0;
102       case OPT_HELP:
103         usage();
104         return 0;
105       default:
106         exit(1);
107       }
108
109   if (optind + 3 != argc)
110     die("Exactly three positional parameters should be given");
111
112   list<cmd *> cmds;
113   parse(argv[optind], cmds);
114   in_name = argv[optind+1];
115   out_name = argv[optind+2];
116
117   try
118     {
119       process(cmds);
120     }
121   catch (exception& e)
122     {
123       die("%s", e.what());
124     }
125
126   return 0;
127 }