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

b2/: move file opening from boom.c to lang.l and combine setup code

This commit is contained in:
Werner Almesberger 2012-05-01 15:04:24 -03:00
parent 82d532e4a2
commit 9a06757420
3 changed files with 43 additions and 49 deletions

View File

@ -12,29 +12,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
#include <string.h> #include <string.h>
#include <fcntl.h>
#include "lang.h" #include "lang.h"
static void open_stdin(const char *name)
{
int fd;
fd = open(name, O_RDONLY);
if (fd < 0) {
perror(name);
exit(1);
}
if (dup2(fd, 0) < 0) {
perror("dup2");
exit(1);
}
}
static void usage(const char *name) static void usage(const char *name)
{ {
fprintf(stderr, fprintf(stderr,
@ -51,7 +33,7 @@ static void usage(const char *name)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
void (*parse)(void) = parse_hierarchy; void (*parse)(const char *name) = parse_hierarchy;
int i; int i;
for (i = 1; i != argc; i++) { for (i = 1; i != argc; i++) {
@ -67,8 +49,7 @@ int main(int argc, char **argv)
else else
usage(*argv); usage(*argv);
} else { } else {
open_stdin(argv[i]); parse(argv[i]);
parse();
} }
} }
return 0; return 0;

View File

@ -13,11 +13,11 @@
#ifndef LANG_H #ifndef LANG_H
#define LANG_H #define LANG_H
void parse_hierarchy(void); void parse_hierarchy(const char *name);
void parse_characteristics(void); void parse_characteristics(const char *name);
void parse_inventory(void); void parse_inventory(const char *name);
void parse_currencies(void); void parse_currencies(const char *name);
void parse_providers(void); void parse_providers(const char *name);
void yywarnf(const char *fmt, ...); void yywarnf(const char *fmt, ...);
void yyerrorf(const char *fmt, ...); void yyerrorf(const char *fmt, ...);

View File

@ -12,6 +12,8 @@
#include <stdarg.h> #include <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "util.h" #include "util.h"
#include "param.h" #include "param.h"
@ -27,48 +29,59 @@ static int expose_nl;
static int lineno; static int lineno;
void parse_hierarchy(void) static void open_stdin(const char *name)
{ {
start_token = START_HIERARCHY; int fd;
expose_nl = 0;
fd = open(name, O_RDONLY);
if (fd < 0) {
perror(name);
exit(1);
}
if (dup2(fd, 0) < 0) {
perror("dup2");
exit(1);
}
}
static void do_parse(const char *name, int start, int nl)
{
open_stdin(name);
start_token = start;
expose_nl = nl;
lineno = 1; lineno = 1;
yyparse(); yyparse();
} }
void parse_characteristics(void) void parse_hierarchy(const char *name)
{ {
start_token = START_CHAR; do_parse(name, START_HIERARCHY, 0);
expose_nl = 1;
lineno = 1;
yyparse();
} }
void parse_inventory(void) void parse_characteristics(const char *name)
{ {
start_token = START_INVENTORY; do_parse(name, START_CHAR, 1);
expose_nl = 1;
lineno = 1;
yyparse();
} }
void parse_currencies(void) void parse_inventory(const char *name)
{ {
start_token = START_EXCHANGE; do_parse(name, START_INVENTORY, 1);
expose_nl = 1;
lineno = 1;
yyparse();
} }
void parse_providers(void) void parse_currencies(const char *name)
{ {
start_token = START_PROVIDERS; do_parse(name, START_EXCHANGE, 1);
expose_nl = 1; }
lineno = 1;
yyparse();
void parse_providers(const char *name)
{
do_parse(name, START_PROVIDERS, 1);
} }
%} %}