1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-11-23 01:41:53 +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 <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.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)
{
fprintf(stderr,
@ -51,7 +33,7 @@ static void usage(const char *name)
int main(int argc, char **argv)
{
void (*parse)(void) = parse_hierarchy;
void (*parse)(const char *name) = parse_hierarchy;
int i;
for (i = 1; i != argc; i++) {
@ -67,8 +49,7 @@ int main(int argc, char **argv)
else
usage(*argv);
} else {
open_stdin(argv[i]);
parse();
parse(argv[i]);
}
}
return 0;

View File

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

View File

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