]> mj.ucw.cz Git - paperjam.git/blob - paperjam.cc
TODO
[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_level;
21 int debug_indent;
22
23 /*** Messages ***/
24
25 void debug(const char *msg, ...)
26 {
27   if (!debug_level)
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 void err(const char *msg, ...)
58 {
59   va_list args;
60   va_start(args, msg);
61   char buf[1024];
62   vsnprintf(buf, sizeof(buf), msg, args);
63   va_end(args);
64   throw paperjam_error(buf);
65 }
66
67 /*** Arguments ***/
68
69 enum opt {
70   OPT_HELP = 256,
71   OPT_VERSION,
72 };
73
74 static const struct option long_opts[] = {
75   { "debug",    0, 0, 'd' },
76   { "help",     0, 0, OPT_HELP },
77   { "version",  0, 0, OPT_VERSION },
78   { "bbox",     0, 0, 'b' },
79   { 0,          0, 0, 0 }
80 };
81
82 static const char short_opts[] = "bd";
83
84 static void usage()
85 {
86   printf("Usage: paperjam [<options>] <commands> <in> <out>\n\
87 \n\
88 Options:\n\
89 -b, --bbox              Recalculate bounding boxes\n\
90 -d, --debug             Show debugging messages\n\
91 \n\
92 <command> = <name>(<args>, <named-arg>[=<value>], ...) [<pipeline>]\n\
93 <pipeline> = { <stage>, <stage>, ... }\n\
94 <stage> = <page> <page> ... [: <commands>]\n\
95 \n\
96 Commands:\n\
97 ");
98   parser_help();
99 }
100
101 static void show_version()
102 {
103   printf("PaperJam " VERSION " -- a PDF processor\n");
104   printf("(c) " YEAR " Martin Mares, distributed under GNU GPL 2+\n");
105   printf("Built on " BUILD_DATE " from Git commit " BUILD_COMMIT "\n");
106 }
107
108 int main(int argc, char **argv)
109 {
110   int c;
111   while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) >= 0)
112     switch (c)
113       {
114       case 'b':
115         recalc_bbox = 1;
116         break;
117       case 'd':
118         debug_level++;
119         break;
120       case OPT_VERSION:
121         show_version();
122         return 0;
123       case OPT_HELP:
124         usage();
125         return 0;
126       default:
127         exit(1);
128       }
129
130   if (optind + 3 != argc)
131     die("Exactly three positional parameters should be given");
132
133   list<cmd *> cmds;
134   parse(argv[optind], cmds);
135   in_name = argv[optind+1];
136   out_name = argv[optind+2];
137
138   try
139     {
140       process(cmds);
141     }
142   catch (exception& e)
143     {
144       die("%s", e.what());
145     }
146
147   return 0;
148 }