|
From: <jsa...@us...> - 2008-05-29 22:34:47
|
Revision: 1248
http://como.svn.sourceforge.net/como/?rev=1248&view=rev
Author: jsanjuas
Date: 2008-05-29 15:34:45 -0700 (Thu, 29 May 2008)
Log Message:
-----------
. adding support for loading an arg to a module from a file
. adding support for nested config files with the import keyword
. minor cosmetics, updating the todo list
Modified Paths:
--------------
src/branches/2.0/TODO.txt
src/branches/2.0/base/config-lexic.l
src/branches/2.0/base/config-syntax.y
src/branches/2.0/libcomo/util-io.c
Modified: src/branches/2.0/TODO.txt
===================================================================
--- src/branches/2.0/TODO.txt 2008-05-29 19:16:14 UTC (rev 1247)
+++ src/branches/2.0/TODO.txt 2008-05-29 22:34:45 UTC (rev 1248)
@@ -75,10 +75,8 @@
- Examine the utility of the following includes, which right now CoMo2.0 can compile without
- heap.h (companion of unused heap.c)
macutils.h (companion of unused macutils.c)
mempool.h (companion of unused mempool.c)
- pattern_search.h (companion of unused pattern_search.c)
ptr_array.h (companion of unused ptr_array.c)
- Remove commented code or old structs in include/*.h
Modified: src/branches/2.0/base/config-lexic.l
===================================================================
--- src/branches/2.0/base/config-lexic.l 2008-05-29 19:16:14 UTC (rev 1247)
+++ src/branches/2.0/base/config-lexic.l 2008-05-29 22:34:45 UTC (rev 1248)
@@ -46,11 +46,15 @@
#define dbg_return(x) return x
#endif
+#define MAX_INCLUDE_DEPTH 10
+YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
+int include_stack_ptr;
int ycline;
void config_lexic_init(void)
{
ycline = 1;
+ include_stack_ptr = 0;
}
%}
@@ -62,6 +66,7 @@
%option nounput
%s comment
+%s import
%%
@@ -71,6 +76,28 @@
<comment>\n ycline++; /* and newlines */
<comment>"*"+"/" BEGIN(INITIAL);
+import BEGIN(import);
+<import>\"[^"]*\" {
+ char *str;
+
+ if (include_stack_ptr >= MAX_INCLUDE_DEPTH)
+ error("reading cfg: imports nested too deeply\n");
+
+ str = como_strdup(yytext + 1); /* remove "s */
+ str[strlen(str) - 1] = '\0';
+
+ include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
+ yyin = fopen(str, "r" );
+ if (yyin == NULL)
+ error("reading cfg: cannot open imported " \
+ "file `%s' for reading\n", str);
+
+ yy_switch_to_buffer(yy_create_buffer(yyin,YY_BUF_SIZE));
+
+ BEGIN(INITIAL);
+ }
+<import>[ \t]* /* skip spaces */
+
db-path { dbg_return(TOK_DBPATH); }
librarydir { dbg_return(TOK_LIBDIR); }
storage-path { dbg_return(TOK_STORAGEPATH); }
@@ -91,7 +118,6 @@
hashsize { dbg_return(TOK_HASHSIZE); }
streamsize { dbg_return(TOK_STREAMSIZE); }
args { dbg_return(TOK_ARGS); }
-args-file { dbg_return(TOK_ARGSFILE); }
on-demand { dbg_return(TOK_ONDEMAND); }
end { dbg_return(TOK_END); }
= { dbg_return(TOK_EQUALS); }
@@ -104,6 +130,8 @@
shed-method { dbg_return(TOK_SHEDMETHOD); }
min_srate { dbg_return(TOK_MINSRATE); }
+"<-" { dbg_return(TOK_LEFTARROW); }
+
{DIGIT}*"."{DIGIT}+ {
yclval.fpnumber = atof(yytext);
dbg_return(TOK_FPNUMBER);
@@ -154,4 +182,14 @@
warn("line %d: junk `%s' found\n", ycline, yctext);
}
+<<EOF>> {
+ include_stack_ptr--;
+ if (include_stack_ptr < 0)
+ yyterminate();
+ else {
+ yy_delete_buffer(YY_CURRENT_BUFFER);
+ yy_switch_to_buffer(include_stack[include_stack_ptr]);
+ }
+ }
+
%%
Modified: src/branches/2.0/base/config-syntax.y
===================================================================
--- src/branches/2.0/base/config-syntax.y 2008-05-29 19:16:14 UTC (rev 1247)
+++ src/branches/2.0/base/config-syntax.y 2008-05-29 22:34:45 UTC (rev 1248)
@@ -38,7 +38,11 @@
#define YYDEBUG 1
#define YYFPRINTF fwarn
-#include <strings.h> /* bzero */
+#include <strings.h> /* bzero */
+#include <sys/types.h> /* stat, open */
+#include <sys/stat.h> /* stat, open */
+#include <unistd.h> /* stat */
+#include <fcntl.h> /* open */
#define LOG_DOMAIN "CONFIG"
#include "como.h"
@@ -58,6 +62,7 @@
int mode;
char *what;
+static char *get_file_contents();
void config_lexic_init();
/* global variables */
@@ -88,9 +93,9 @@
%token TOK_DBPATH TOK_LIBDIR TOK_MEMSIZE TOK_QUERY_PORT TOK_NAME TOK_LOCATION
%token TOK_TYPE TOK_COMMENT TOK_SNIFFER TOK_FILESIZE TOK_MODULE TOK_DESCRIPTION
%token TOK_SOURCE TOK_OUTPUT TOK_FILTER TOK_HASHSIZE TOK_STREAMSIZE TOK_ARGS
-%token TOK_ARGSFILE TOK_ONDEMAND TOK_END TOK_NEWLINE TOK_EQUALS TOK_COMMA
+%token TOK_ONDEMAND TOK_END TOK_NEWLINE TOK_EQUALS TOK_COMMA
%token TOK_STORAGEPATH TOK_ASNFILE TOK_SHEDMETHOD TOK_ALIAS TOK_VIRTUAL_NODE
-%token TOK_SOURCE_MODULE TOK_MINSRATE
+%token TOK_SOURCE_MODULE TOK_MINSRATE TOK_LEFTARROW
%token <string> TOK_STRING
%token <number> TOK_NUMBER
%token <fpnumber> TOK_FPNUMBER
@@ -188,21 +193,20 @@
| TOK_HASHSIZE TOK_NUMBER { mdl.hashsize = $2; }
| TOK_STREAMSIZE TOK_NUMBER { mdl.streamsize = $2; }
| TOK_SHEDMETHOD TOK_STRING {
- #ifdef LOADSHED
- mdl.shed_method = $2;
- #endif
- }
+ #ifdef LOADSHED
+ mdl.shed_method = $2;
+ #endif
+ }
| TOK_MINSRATE TOK_NUMBER {
- #ifdef LOADSHED
- mdl.minimum_srate = $2;
- #endif
- }
+ #ifdef LOADSHED
+ mdl.minimum_srate = $2;
+ #endif
+ }
| TOK_MINSRATE TOK_FPNUMBER {
- #ifdef LOADSHED
- mdl.minimum_srate = $2;
- #endif
- }
- /*| TOK_ARGSFILE TOK_STRING (TODO) */
+ #ifdef LOADSHED
+ mdl.minimum_srate = $2;
+ #endif
+ }
| TOK_ONDEMAND { mdl.ondemand = 1; }
| error TOK_NEWLINE { report_parse_error(); }
;
@@ -210,13 +214,60 @@
args_list: args_list TOK_COMMA arg | arg
;
-arg: TOK_STRING TOK_EQUALS TOK_STRING { hash_insert_string(mdl.args, $1, $3); }
+arg:
+ TOK_STRING TOK_EQUALS TOK_STRING {
+ hash_insert_string(mdl.args, $1, $3);
+ }
+ | TOK_STRING TOK_LEFTARROW TOK_STRING {
+ hash_insert_string(mdl.args, $1, get_file_contents($3));
+ free($3);
+ }
;
%%
#include "config-lexic.c"
+static char *
+get_file_contents(char *path)
+{
+ struct stat st;
+ char *buffer;
+ int r, fd;
+
+ #define GFC_ERROR_STR \
+ "could not read file `%s' (referenced by configuration file " \
+ "or command line)\n"
+
+ r = stat(path, &st);
+ if (r != 0) {
+ warn(GFC_ERROR_STR, path);
+ return como_strdup("");
+ }
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0) {
+ warn(GFC_ERROR_STR, path);
+ return como_strdup("");
+ }
+
+ buffer = como_malloc(st.st_size);
+ r = como_read(fd, buffer, st.st_size);
+ if (r != st.st_size) {
+ free(buffer);
+ warn(GFC_ERROR_STR, path);
+ return como_strdup("");
+ }
+
+ r = close(fd);
+ if (r < 0) {
+ free(buffer);
+ warn(GFC_ERROR_STR, path);
+ return como_strdup("");
+ }
+ return buffer;
+}
+
void ycerror(char *fmt, ...)
{
extern int ycline;
Modified: src/branches/2.0/libcomo/util-io.c
===================================================================
--- src/branches/2.0/libcomo/util-io.c 2008-05-29 19:16:14 UTC (rev 1247)
+++ src/branches/2.0/libcomo/util-io.c 2008-05-29 22:34:45 UTC (rev 1248)
@@ -34,7 +34,7 @@
/*
* -- como_read
*
- * keeps writing until complete.
+ * keeps reading until complete.
*/
ssize_t
como_read(int fd, void * buf, size_t count)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|