1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-11-26 23:19:44 +02:00

sch2fig/: new option -Dvar=value to set <var> in template

This commit is contained in:
Werner Almesberger 2016-07-26 03:15:13 -03:00
parent 1d059abcb4
commit 2c9c526255
3 changed files with 43 additions and 6 deletions

View File

@ -420,7 +420,32 @@ static void fig_header(void)
}
void fig_init(const char *template)
static bool apply_vars(char *buf, int n_vars, const char **vars)
{
char *p;
const char **var, *eq;
int var_len, value_len;
p = strchr(buf, '<');
if (!p)
return 0;
for (var = vars; var != vars + n_vars; var++) {
eq = strchr(*var, '=');
assert(eq);
var_len = eq - *var;
if (strncmp(p + 1, *var, var_len))
continue;
value_len = strlen(eq + 1);
memmove(p + value_len, p + var_len + 2,
strlen(p + var_len + 2) + 1);
memcpy(p, eq + 1, value_len);
return 1;
}
return 0;
}
void fig_init(const char *template, int n_vars, const char **vars)
{
FILE *file;
char buf[1000];
@ -435,7 +460,9 @@ void fig_init(const char *template)
perror(template);
exit(1);
}
while (fgets(buf, sizeof(buf), file))
while (fgets(buf, sizeof(buf), file)) {
while (apply_vars(buf, n_vars, vars));
printf("%s", buf);
}
fclose(file);
}

View File

@ -53,6 +53,6 @@ void fig_text(int x, int y, const char *s, unsigned size,
/* inititalization */
void fig_init(const char *template);
void fig_init(const char *template, int n_vars, const char **vars);
#endif /* !FIG_H */

View File

@ -59,7 +59,8 @@ static void read_file(const char *name, void *ctx,
static void usage(const char *name)
{
fprintf(stderr,
"usage: %s [-t template.fig] [file.lib ...] file.sch\n", name);
"usage: %s [-t template.fig] [-Dvar=value ...] [file.lib ...] file.sch\n",
name);
exit(1);
}
@ -69,12 +70,21 @@ int main(int argc, char **argv)
const char *template = NULL;
char c;
int arg;
int n_vars = 0;
const char **vars = NULL;
while ((c = getopt(argc, argv, "t:")) != EOF)
while ((c = getopt(argc, argv, "t:D:")) != EOF)
switch (c) {
case 't':
template = optarg;
break;
case 'D':
if (!strchr(optarg, '='))
usage(*argv);
n_vars++;
vars = realloc(vars, sizeof(const char *) * n_vars);
vars[n_vars - 1] = optarg;
break;
default:
usage(*argv);
}
@ -82,7 +92,7 @@ int main(int argc, char **argv)
if (argc - optind < 1)
usage(*argv);
fig_init(template);
fig_init(template, n_vars, vars);
for (arg = optind; arg != argc; arg++) {
if (arg == argc - 1) {
struct sch_ctx ctx;