summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWerner Almesberger <werner@almesberger.net>2016-09-03 15:18:40 -0300
committerWerner Almesberger <werner@almesberger.net>2016-09-03 19:18:29 -0300
commitb987ddd5026d6e96f96475d2f380e121c40cad29 (patch)
treefd3d41cf340c4b34989b9c35d9ff65fdc0db3659
parent8fe67338b3f797208623c88893f56d1c1a9d1b30 (diff)
downloadeeshow-b987ddd5026d6e96f96475d2f380e121c40cad29.tar.gz
eeshow-b987ddd5026d6e96f96475d2f380e121c40cad29.tar.bz2
eeshow-b987ddd5026d6e96f96475d2f380e121c40cad29.zip
select driver by filename extension instead of driver spec
-rw-r--r--gfx/cro.c11
-rw-r--r--gfx/diff.c1
-rw-r--r--gfx/fig.c5
-rw-r--r--gfx/gfx.h3
-rw-r--r--main/eediff.c2
-rw-r--r--main/eeplot.c59
6 files changed, 64 insertions, 17 deletions
diff --git a/gfx/cro.c b/gfx/cro.c
index 588aad2..13b1daa 100644
--- a/gfx/cro.c
+++ b/gfx/cro.c
@@ -450,7 +450,6 @@ void cro_color_override(struct cro_ctx *cc, int color)
static const struct gfx_ops real_cro_ops = {
- .name = "cairo",
.line = cr_line,
.poly = cr_poly,
.circ = cr_circ,
@@ -921,8 +920,11 @@ cairo_surface_t *cro_img_surface(struct cro_ctx *cc)
/* ----- Operations -------------------------------------------------------- */
+static const char *const cro_png_ext[] = { "png" };
+
const struct gfx_ops cro_png_ops = {
- .name = "png",
+ .ext = cro_png_ext,
+ .n_ext = ARRAY_ELEMENTS(cro_png_ext),
.line = record_line,
.poly = record_poly,
.circ = record_circ,
@@ -934,8 +936,11 @@ const struct gfx_ops cro_png_ops = {
.end = cr_png_end,
};
+static const char *const cro_pdf_ext[] = { "pdf" };
+
const struct gfx_ops cro_pdf_ops = {
- .name = "pdf",
+ .ext = cro_pdf_ext,
+ .n_ext = ARRAY_ELEMENTS(cro_pdf_ext),
.line = record_line,
.poly = record_poly,
.circ = record_circ,
diff --git a/gfx/diff.c b/gfx/diff.c
index 08554a0..d43b292 100644
--- a/gfx/diff.c
+++ b/gfx/diff.c
@@ -508,7 +508,6 @@ void diff_to_canvas(cairo_t *cr, int cx, int cy, float scale,
const struct gfx_ops diff_ops = {
- .name = "diff",
.line = diff_line,
.poly = diff_poly,
.circ = diff_circ,
diff --git a/gfx/fig.c b/gfx/fig.c
index a60e41b..c0fa470 100644
--- a/gfx/fig.c
+++ b/gfx/fig.c
@@ -372,8 +372,11 @@ static int fig_end(void *ctx)
/* ----- Operations -------------------------------------------------------- */
+static const char *const fig_ext[] = { "fig" };
+
const struct gfx_ops fig_ops = {
- .name = "fig",
+ .ext = fig_ext,
+ .n_ext = ARRAY_ELEMENTS(fig_ext),
.line = fig_line,
.rect = fig_rect,
.poly = fig_poly,
diff --git a/gfx/gfx.h b/gfx/gfx.h
index c53c661..da28b6b 100644
--- a/gfx/gfx.h
+++ b/gfx/gfx.h
@@ -22,7 +22,8 @@
struct gfx;
struct gfx_ops {
- const char *name;
+ const char *const *ext;
+ int n_ext;
void (*line)(void *ctx, int sx, int sy, int ex, int ey,
int color, unsigned layer);
void (*rect)(void *ctx, int sx, int sy, int ex, int ey,
diff --git a/main/eediff.c b/main/eediff.c
index 2531953..3340d6b 100644
--- a/main/eediff.c
+++ b/main/eediff.c
@@ -127,7 +127,7 @@ int main(int argc, char **argv)
if (dashdash == argc) {
gfx_argc = 1;
gfx_argv = alloc_type_n(char *, 2);
- gfx_argv[0] = (char *) diff_ops.name;
+ gfx_argv[0] = "diff";
gfx_argv[1] = NULL;
} else {
gfx_argc = argc - dashdash - 1;
diff --git a/main/eeplot.c b/main/eeplot.c
index 6e05a9b..b0d78da 100644
--- a/main/eeplot.c
+++ b/main/eeplot.c
@@ -16,6 +16,7 @@
#include <stdio.h>
#include <unistd.h>
#include <string.h>
+#include <strings.h> // for strncasecmp
#include <cairo/cairo.h>
@@ -42,6 +43,49 @@ static struct gfx_ops const *ops_list[] = {
};
+static const struct gfx_ops *find_by_output(int argc, char **argv)
+{
+ const char *arg, *dot, *colon, *ext;
+ const struct gfx_ops *const *ops;
+ int i;
+ unsigned n;
+
+ for (i = 1; i != argc; i++)
+ if (!strncmp(argv[i], "-o", 2))
+ break;
+ if (i == argc)
+ fatal("required option \"-o output\" is missing");
+ arg = argv[i] + 2;
+ if (!*arg) {
+ arg = argv[i + 1];
+ if (!arg)
+ fatal("-o requires an argument");
+ }
+
+ dot = strrchr(arg, '.');
+ colon = strchr(arg, ':');
+ if (colon && dot && dot < colon)
+ usage(*argv);
+ if (colon) {
+ ext = arg;
+ n = colon - arg;
+ } else if (dot) {
+ ext = dot + 1;
+ n = strlen(ext);
+ } else {
+ fatal("either extension or \"type:\" prefix required for "
+ "output file");
+ }
+
+ for (ops = ops_list; ops != ARRAY_END(ops_list); ops++)
+ for (i = 0; i != (*ops)->n_ext; i++)
+ if (!strncasecmp((*ops)->ext[i], ext, n) ||
+ strlen((*ops)->ext[i]) == n)
+ return *ops;
+ fatal("graphics backend for \"%s\" not found\n", ext);
+}
+
+
void usage(const char *name)
{
fprintf(stderr,
@@ -101,7 +145,7 @@ int main(int argc, char **argv)
struct file_names *fn = &file_names;
int gfx_argc;
char **gfx_argv;
- const struct gfx_ops **ops = ops_list;
+ const struct gfx_ops *ops;
struct gfx *gfx;
int retval;
@@ -147,10 +191,12 @@ int main(int argc, char **argv)
if (!file_names.pro && !file_names.sch)
fatal("project or top sheet name required");
+ ops = find_by_output(argc, argv);
+
if (dashdash == argc) {
gfx_argc = 1;
gfx_argv = alloc_type_n(char *, 2);
- gfx_argv[0] = (char *) (*ops)->name;
+ gfx_argv[0] = (char *) ops->ext[0];
gfx_argv[1] = NULL;
} else {
gfx_argc = argc - dashdash - 1;
@@ -159,13 +205,6 @@ int main(int argc, char **argv)
gfx_argv = alloc_type_n(char *, gfx_argc + 1);
memcpy(gfx_argv, argv + dashdash + 1,
sizeof(const char *) * (gfx_argc + 1));
-
- for (ops = ops_list; ops != ARRAY_END(ops_list); ops++)
- if (!strcmp((*ops)->name, *gfx_argv))
- goto found;
- fatal("graphics backend \"%s\" not found\n", *gfx_argv);
-found:
- ;
}
if (file_names.pro) {
@@ -174,7 +213,7 @@ found:
fn = pro_parse_file(&pro_file, &file_names);
}
- gfx = gfx_init(*ops);
+ gfx = gfx_init(ops);
if (!gfx_args(gfx, gfx_argc, gfx_argv))
return 1;
if (!gfx_multi_sheet(gfx))