]> mj.ucw.cz Git - paperjam.git/blob - paperjam.cc
Shuffling code around
[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   { 0,          0, 0, 0 }
69 };
70
71 static const char short_opts[] = "bd";
72
73 static void usage()
74 {
75   printf("Usage: paperjam [<options>] <commands> <in> <out>\n\
76 \n\
77 Options:\n\
78 -b, --bbox              Recalculate bounding boxes\n\
79 -d, --debug             Show debugging messages\n\
80 \n\
81 Commands: (FIXME)\n\
82 ");
83   parser_help();
84 }
85
86 int main(int argc, char **argv)
87 {
88   int c;
89   while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) >= 0)
90     switch (c)
91       {
92       case 'b':
93         recalc_bbox = 1;
94         break;
95       case 'd':
96         debug_mode++;
97         break;
98       case OPT_VERSION:
99         printf("This is paperjam with no version yet.\n");      // FIXME
100         return 0;
101       case OPT_HELP:
102         usage();
103         return 0;
104       default:
105         exit(1);
106       }
107
108   if (optind + 3 != argc)
109     die("Exactly three positional parameters should be given");
110
111   list<cmd *> cmds;
112   parse(argv[optind], cmds);
113   in_name = argv[optind+1];
114   out_name = argv[optind+2];
115
116   try
117     {
118       process(cmds);
119     }
120   catch (exception& e)
121     {
122       die("%s", e.what());
123     }
124
125   return 0;
126 }