2016-07-22 06:04:42 +03:00
|
|
|
/*
|
|
|
|
* main.c - Convert Eeschema schematics to FIG
|
|
|
|
*
|
|
|
|
* Written 2016 by Werner Almesberger
|
|
|
|
* Copyright 2016 by Werner Almesberger
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-07-22 05:54:32 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2016-07-26 08:30:58 +03:00
|
|
|
#include <unistd.h>
|
2016-07-22 05:54:32 +03:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "fig.h"
|
2016-07-31 06:04:43 +03:00
|
|
|
#include "cairo.h"
|
2016-07-31 03:50:02 +03:00
|
|
|
#include "gfx.h"
|
2016-07-22 05:54:32 +03:00
|
|
|
#include "lib.h"
|
|
|
|
#include "sch.h"
|
|
|
|
|
|
|
|
|
|
|
|
static bool do_sch_parse(void *ctx, const char *line)
|
|
|
|
{
|
|
|
|
return sch_parse(ctx, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool do_lib_parse(void *ctx, const char *line)
|
|
|
|
{
|
|
|
|
return lib_parse(ctx, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void read_file(const char *name, void *ctx,
|
|
|
|
bool (*parse)(void *ctx, const char *line))
|
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
char buf[1000];
|
|
|
|
char *nl;
|
|
|
|
|
|
|
|
file = fopen(name, "r");
|
2016-07-22 06:19:19 +03:00
|
|
|
if (!file) {
|
|
|
|
perror(name);
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-07-22 05:54:32 +03:00
|
|
|
while (fgets(buf, sizeof(buf), file)) {
|
|
|
|
nl = strchr(buf, '\n');
|
|
|
|
if (nl)
|
|
|
|
*nl = 0;
|
|
|
|
if (!parse(ctx, buf))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void usage(const char *name)
|
|
|
|
{
|
2016-07-26 08:30:58 +03:00
|
|
|
fprintf(stderr,
|
2016-07-26 09:15:13 +03:00
|
|
|
"usage: %s [-t template.fig] [-Dvar=value ...] [file.lib ...] file.sch\n",
|
|
|
|
name);
|
2016-07-22 05:54:32 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2016-07-26 08:30:58 +03:00
|
|
|
const char *template = NULL;
|
|
|
|
char c;
|
2016-07-22 05:54:32 +03:00
|
|
|
int arg;
|
2016-07-26 09:15:13 +03:00
|
|
|
int n_vars = 0;
|
|
|
|
const char **vars = NULL;
|
2016-07-22 05:54:32 +03:00
|
|
|
|
2016-07-26 09:15:13 +03:00
|
|
|
while ((c = getopt(argc, argv, "t:D:")) != EOF)
|
2016-07-26 08:30:58 +03:00
|
|
|
switch (c) {
|
|
|
|
case 't':
|
|
|
|
template = optarg;
|
|
|
|
break;
|
2016-07-26 09:15:13 +03:00
|
|
|
case 'D':
|
|
|
|
if (!strchr(optarg, '='))
|
|
|
|
usage(*argv);
|
|
|
|
n_vars++;
|
|
|
|
vars = realloc(vars, sizeof(const char *) * n_vars);
|
|
|
|
vars[n_vars - 1] = optarg;
|
|
|
|
break;
|
2016-07-26 08:30:58 +03:00
|
|
|
default:
|
|
|
|
usage(*argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc - optind < 1)
|
2016-07-22 05:54:32 +03:00
|
|
|
usage(*argv);
|
|
|
|
|
2016-07-31 06:04:43 +03:00
|
|
|
//gfx_init(&fig_ops, template, n_vars, vars);
|
|
|
|
gfx_init(&cairo_ops, template, n_vars, vars);
|
2016-07-26 08:30:58 +03:00
|
|
|
for (arg = optind; arg != argc; arg++) {
|
2016-07-22 05:54:32 +03:00
|
|
|
if (arg == argc - 1) {
|
|
|
|
struct sch_ctx ctx;
|
|
|
|
|
|
|
|
sch_init(&ctx);
|
|
|
|
read_file(argv[arg], &ctx, do_sch_parse);
|
|
|
|
} else {
|
|
|
|
struct lib_ctx ctx;
|
|
|
|
|
|
|
|
lib_init(&ctx);
|
|
|
|
read_file(argv[arg], &ctx, do_lib_parse);
|
|
|
|
}
|
|
|
|
}
|
2016-07-31 05:02:15 +03:00
|
|
|
gfx_end();
|
2016-07-22 05:54:32 +03:00
|
|
|
}
|