From f59e072562926a6395436d2801cabad2feb05285 Mon Sep 17 00:00:00 2001 From: Navaneeth Date: Wed, 17 Nov 2010 10:54:24 +0530 Subject: [PATCH] more cleanup to the library. --- CMakeLists.txt | 1 + compile.c | 58 ++++++----------- lex.c | 5 +- parser.c | 31 ++++----- parser.h | 2 +- tl.c | 57 ++++------------ trie.c | 11 ++-- util.h | 3 - error.h => varnam-result-codes.h | 13 ++-- varnam.c | 108 ++++++++----------------------- module.h => varnam.h | 28 ++++++-- vstgen.c | 7 +- 12 files changed, 121 insertions(+), 203 deletions(-) rename error.h => varnam-result-codes.h (77%) rename module.h => varnam.h (56%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 49211fa..c29fa16 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,7 @@ list (APPEND VARNAM_SOURCES parser.c vstgen.c tl.c + varnam.c ) # Build foreign functions diff --git a/compile.c b/compile.c index 6475e87..b6e6f08 100644 --- a/compile.c +++ b/compile.c @@ -19,10 +19,11 @@ #include #include "util.h" -#include "module.h" #include "sexpr/sexp.h" #include "parser.h" #include "trie.h" +#include "varnam.h" +#include "varnam-result-codes.h" const char compile_usage[] = "compile: varnam compile [--help] [output_directory]\n"; @@ -79,19 +80,18 @@ static void handle_error(struct parser_result *pr) varnam_error(":: %d errors. Compilation aborted.", error_count); } -static int compile(struct compile_parameters *params) +static int compile_scheme_file(varnam *handle, const char *scheme_file, const char *output_directory) { char output_file_path[MAX_PATH_LENGTH], *error_message = NULL; struct parser_result *pr = NULL; int status; - struct path_info *pinfo = splitpath(params->scheme_file); - const char *directory = params->output_directory ? params->output_directory : pinfo->directory; + struct path_info *pinfo = splitpath(scheme_file); + const char *directory = output_directory ? output_directory : pinfo->directory; const char *extension = ".vst"; if(pinfo->filename == 0 || strcmp(pinfo->filename, "") == 0) { varnam_error("compile: invalid scheme file specified. aborting."); - varnam_info(compile_usage); return VARNAM_ERROR; } @@ -99,17 +99,11 @@ static int compile(struct compile_parameters *params) strcat(output_file_path, pinfo->filename); strcat(output_file_path, extension); - varnam_info(":: Varnam Scheme File Compiler"); - varnam_info(":: Copyright (C) Navaneeth.K.N"); - varnam_info("::"); - varnam_info(":: Compiling scheme file : %s", params->scheme_file); - varnam_info(":: Output will be written to : %s", output_file_path); - varnam_info("::"); - - if(parser_init(params->scheme_file) != VARNAM_OK) { - varnam_error(":: Compilation failed for '%s'", params->scheme_file); + status = parser_init(scheme_file); + if(status != VARNAM_SUCCESS) { + varnam_error(":: Compilation failed for '%s'", scheme_file); xfree( pinfo ); - return VARNAM_ERROR; + return status; } pr = parser_parse(); @@ -121,39 +115,29 @@ static int compile(struct compile_parameters *params) } status = varnam_generate_symbols(output_file_path, pr->result, &error_message); - if(status != VARNAM_OK) { - varnam_error(":: Compilation failed for %s", params->scheme_file); + if(status != VARNAM_SUCCESS) { + varnam_error(":: Compilation failed for %s", scheme_file); xfree( pinfo ); parser_destroy( pr ); - return VARNAM_ERROR; + return status; } varnam_info("::"); - varnam_info(":: Successfully compiled '%s'", params->scheme_file); + varnam_info(":: Successfully compiled '%s'", scheme_file); varnam_info(":: Created %s", output_file_path); xfree( pinfo ); parser_destroy( pr ); - return VARNAM_OK; + return VARNAM_SUCCESS; } -int compile_scheme_file(int argc, char **argv) +int varnam_compile(varnam *handle, const char *scheme_file, const char *output_directory) { - struct compile_parameters params; - if(argc == 0) { - varnam_error(":: compile: expected scheme file but found none.\n%s", compile_usage); - return 1; - } - else if(strcmp(argv[0], "--help") == 0) { - varnam_info(compile_usage); - return 1; - } - - params.scheme_file = argv[0]; - params.output_directory = 0; - if(argc >= 2) { - params.output_directory = argv[1]; - } - return compile(¶ms) == VARNAM_OK ? 0 : 1; + if(handle == NULL || scheme_file == NULL) + return VARNAM_MISUSE; + + return compile_scheme_file(handle, scheme_file, output_directory); } + + diff --git a/lex.c b/lex.c index 842cba3..9f099bc 100644 --- a/lex.c +++ b/lex.c @@ -22,6 +22,7 @@ #include "util.h" #include "sexpr/sexp.h" #include "lex.h" +#include "varnam-result-codes.h" #define PAGE_SIZE 4096 #define READ_ONLY_MODE "r" @@ -180,7 +181,7 @@ static int handle_null_expression() return VARNAM_ERROR; } - return VARNAM_OK; + return VARNAM_SUCCESS; } lex_statuscodes lex_status() @@ -209,7 +210,7 @@ int lex_init(const char *filename) change_status(LEX_ERRORED, "error opening input file"); return VARNAM_ERROR; } - return VARNAM_OK; + return VARNAM_SUCCESS; } /* diff --git a/parser.c b/parser.c index 86a1450..8132e30 100644 --- a/parser.c +++ b/parser.c @@ -21,6 +21,7 @@ #include "trie.h" #include "sexpr/sexp.h" #include "parser.h" +#include "varnam-result-codes.h" #include #include @@ -191,7 +192,7 @@ static int install_symbol(struct symbol *sym) else { return VARNAM_ERROR; } - return VARNAM_OK; + return VARNAM_SUCCESS; } static struct symbol *make_symbol(const char *name, @@ -224,7 +225,7 @@ static int argcount_ok(struct execution_context *context, "'%s' expects %d arguments but found %d", function_name, expected, actual); return VARNAM_ERROR; } - return VARNAM_OK; + return VARNAM_SUCCESS; } static struct token *create_token(const char *pattern, @@ -257,7 +258,7 @@ static int pattern_valid(struct execution_context *context, "Empty patterns are not allowed in function '%s", function_name); return VARNAM_ERROR; } - return VARNAM_OK; + return VARNAM_SUCCESS; } static int value_valid(struct execution_context *context, @@ -270,12 +271,12 @@ static int value_valid(struct execution_context *context, "Empty values are not allowed in function '%s", function_name); return VARNAM_ERROR; } - return VARNAM_OK; + return VARNAM_SUCCESS; } static int symbol_length_ok(struct execution_context *context, const char *sym) { - if(sym == NULL) return VARNAM_OK; + if(sym == NULL) return VARNAM_SUCCESS; if(strlen(sym) >= PARSER_SYMBOL_MAX) { snprintf(context->error_message, @@ -283,7 +284,7 @@ static int symbol_length_ok(struct execution_context *context, const char *sym) "'%s' exceeds allowed symbol size %d", sym, PARSER_SYMBOL_MAX); return VARNAM_ERROR; } - return VARNAM_OK; + return VARNAM_SUCCESS; } static const char *builtin_set(struct execution_context *context, unsigned int argcount) @@ -560,8 +561,8 @@ static int eval_exp_recursive(struct execution_context *context, sexp_t *exp) } result = s->handler(context, exp, s); - if(result != VARNAM_OK) { - return VARNAM_ERROR; + if(result != VARNAM_SUCCESS) { + return result; } /** @@ -569,7 +570,7 @@ static int eval_exp_recursive(struct execution_context *context, sexp_t *exp) * make sense to keep it there **/ pop_exp( context ); - return VARNAM_OK; + return VARNAM_SUCCESS; } static int eval_function(struct execution_context *context, @@ -662,7 +663,7 @@ static int eval_function(struct execution_context *context, **/ push_arg(context, result); } - return VARNAM_OK; + return VARNAM_SUCCESS; } else { return VARNAM_ERROR; @@ -693,7 +694,7 @@ static void eval_exp(struct parser_result *res, sexp_t *exp) context.result = res->result; status = eval_exp_recursive(&context, exp); - if(status != VARNAM_OK) { + if(status != VARNAM_SUCCESS) { err = (struct parser_error *) xmalloc(sizeof (struct parser_error)); assert(err); @@ -747,15 +748,15 @@ struct parser_result *parser_parse() return result; } -unsigned int parser_init(const char *filename) +int parser_init(const char *filename) { struct symbol *set, *vo, *co, *cc, *nu, *sy, *ot; int ls; ls = lex_init(filename); - if(ls != VARNAM_OK) { + if(ls != VARNAM_SUCCESS) { varnam_error("%s\n", lex_message()); - return VARNAM_ERROR; + return ls; } memset(&st[0], 0, sizeof(st)); @@ -798,7 +799,7 @@ unsigned int parser_init(const char *filename) ot->handler = &eval_function; install_symbol(ot); - return VARNAM_OK; + return VARNAM_SUCCESS; } void parser_destroy(struct parser_result *pr) diff --git a/parser.h b/parser.h index afc1139..ba18085 100644 --- a/parser.h +++ b/parser.h @@ -59,7 +59,7 @@ struct parser_result { struct trie *result; }; -unsigned int parser_init(const char *filename); +int parser_init(const char *filename); struct parser_result *parser_parse(); void parser_destroy(struct parser_result *result); diff --git a/tl.c b/tl.c index d30224d..7357334 100644 --- a/tl.c +++ b/tl.c @@ -19,6 +19,8 @@ #include "util.h" #include "parser.h" #include "foreign/sqlite3.h" +#include "varnam.h" +#include "varnam-result-codes.h" #include const char tl_usage[] = @@ -77,7 +79,7 @@ static int can_find_solution(sqlite3 *db, struct token *last, const char *lookup rc = sqlite3_step( stmt ); if( rc == SQLITE_ROW ) { if( sqlite3_column_int( stmt, 0 ) > 0 ) { - result = VARNAM_OK; + result = VARNAM_SUCCESS; } } } @@ -129,57 +131,20 @@ static int tokenize(sqlite3 *db, if( strlen( remaining ) > 0 ) return tokenize( db, remaining, string ); - return VARNAM_OK; + return VARNAM_SUCCESS; } -struct strbuf *transliterate(const char *scheme_file, const char *input) +int varnam_transliterate(varnam *handle, const char *input, struct strbuf *output) { sqlite3 *db; int rc; - struct strbuf *string; + + if(handle == NULL || input == NULL || output == NULL) + return VARNAM_MISUSE; - rc = sqlite3_open( scheme_file, &db ); - if( rc ) { - varnam_error("Can't open %s: %s\n", scheme_file, sqlite3_errmsg(db)); - sqlite3_close(db); - return VARNAM_ERROR; - } - - string = strbuf_init( 20 ); - if( tokenize( db, input, string ) != VARNAM_OK ) { - strbuf_destroy( string ); - string = NULL; - } + db = handle->internal->db; + rc = tokenize( db, input, output ); - sqlite3_close( db ); - return string; + return rc; } -int transliterate_input(int argc, char **argv) -{ - char *scheme_file; - char *input; - struct strbuf *output; - - if(argc == 0) { - varnam_info( "transliterate: invalid usage" ); - varnam_info( tl_usage ); - return 1; - } - else if(argc == 1) { - varnam_info( "transliterate: no input text found" ); - varnam_info( tl_usage ); - return 1; - } - - scheme_file = argv[0]; - input = argv[1]; - output = transliterate( scheme_file, input ); - if( output == NULL ) { - varnam_error( "transliteration failed! "); - return 1; - } - varnam_info("%s", output->buffer); - strbuf_destroy(output); - return 0; -} diff --git a/trie.c b/trie.c index 152bec9..a03731e 100644 --- a/trie.c +++ b/trie.c @@ -22,6 +22,7 @@ #include #include "util.h" #include "trie.h" +#include "varnam-result-codes.h" static struct trie *trie_new(const char *label, void *value) { @@ -200,17 +201,19 @@ struct trie *trie_add_child(struct trie *parent, const char *label, void *value) static int iterate_trie_recursive(struct trie *t, itfunction function, unsigned int depth, void *userdata) { + int rc; while(t != NULL) { - if(function(t, depth, userdata) != VARNAM_OK) { + if(function(t, depth, userdata) != VARNAM_SUCCESS) { return VARNAM_ERROR; } - if(iterate_trie_recursive(t->child, function, depth + 1, userdata) != VARNAM_OK) { - return VARNAM_ERROR; + rc = iterate_trie_recursive(t->child, function, depth + 1, userdata); + if(rc != VARNAM_SUCCESS) { + return rc; } t = t->next; } - return VARNAM_OK; + return VARNAM_SUCCESS; } int trie_iterate(struct trie *t, itfunction function, void *userdata) diff --git a/util.h b/util.h index bbd2611..49acc1c 100644 --- a/util.h +++ b/util.h @@ -25,9 +25,6 @@ #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) -#define VARNAM_OK 1 -#define VARNAM_ERROR 0 - void varnam_info(const char *format, ...); void varnam_error(const char *format, ...); diff --git a/error.h b/varnam-result-codes.h similarity index 77% rename from error.h rename to varnam-result-codes.h index c16da7f..3e99138 100644 --- a/error.h +++ b/varnam-result-codes.h @@ -1,4 +1,4 @@ -/* error.h +/* varnam-result-codes.h * * Copyright (C) Navaneeth.K.N * @@ -17,11 +17,12 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifndef ERROR_H_INCLUDED_103446 -#define ERROR_H_INCLUDED_103446 +#ifndef VARANAM_RESULT_CODES_INCLUDED_103446 +#define VARANAM_RESULT_CODES_INCLUDED_103446 -struct error_details { - -}; +#define VARNAM_SUCCESS 0 +#define VARNAM_MISUSE 1 +#define VARNAM_ERROR 2 +#define VARNAM_MEMORY_ERROR 3 #endif diff --git a/varnam.c b/varnam.c index db7b158..49c8385 100644 --- a/varnam.c +++ b/varnam.c @@ -17,95 +17,43 @@ License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include -#include "module.h" +#include "varnam.h" #include "util.h" +#include "varnam-result-codes.h" +#include -const char varnam_usage[] = - "varnam [--version] [--help] | \n"; - -const char varnam_version[] = - "Varnam version 0.0.1.a"; - -struct command_struct { - const char *command; - const char *description; - int(*function)(int argc, char **argv); -}; - -static struct command_struct commands[] = { - { "compile", "Compiles the provided scheme file to symbol table", compile_scheme_file }, - { "tl", "Transliterate given input", transliterate_input } -}; - -static void varnam_print_usage(void) +int varnam_init(const char *symbols_file, size_t file_length, varnam **handle, char **msg) { - varnam_info("usage : %s", varnam_usage); -} + int rc; + varnam *c; + struct varnam_internal *vp; -static void print_nchar(char c, size_t n) -{ - while(n--) - fprintf(stdout, "%c", c); -} + if(symbols_file == NULL || file_length <= 0) + return VARNAM_MISUSE; -static void varnam_print_commands() -{ - size_t i, longest = 0; - varnam_info("Following commands are available : \n"); + c = (varnam *) xmalloc(sizeof (varnam)); + if(!c) + return VARNAM_MEMORY_ERROR; - for (i = 0; i < ARRAY_SIZE(commands); i++) { - struct command_struct *t = commands + i; - if( longest < strlen( t->command ) ) - longest = strlen( t->command ); - } + vp = (struct varnam_internal *) xmalloc(sizeof (struct varnam_internal *)); + if(!vp) + return VARNAM_MEMORY_ERROR; - for (i = 0; i < ARRAY_SIZE(commands); i++) { - struct command_struct *t = commands + i; - fprintf(stdout, " %s ", t->command); - print_nchar(' ', longest - strlen( t->command )); - fprintf(stdout, "%s\n", t->description); - } - varnam_info(""); -} - -static int execute_command(int argc, char **argv) -{ - const char *cmd_to_execute = argv[0]; - struct command_struct *cmd = 0; - unsigned i; + c->internal = vp; - for (i = 0; i < ARRAY_SIZE(commands); i++) { - struct command_struct *t = commands + i; - if (strcmp(t->command, cmd_to_execute) == 0) { - cmd = t; - break; - } - } - - if(cmd == NULL) { - varnam_error("varnam : unrecognized command '%s'.\n", cmd_to_execute); - varnam_print_usage(); - varnam_print_commands(); + rc = sqlite3_open(symbols_file, &vp->db); + if( rc ) { + varnam_error("Can't open %s: %s\n", symbols_file, sqlite3_errmsg(vp->db)); + sqlite3_close(vp->db); return VARNAM_ERROR; } - argv++; - argc--; - return cmd->function(argc, argv); -} -int main(int argc, char **argv) -{ - if(argc == 1) { - varnam_print_usage(); - varnam_print_commands(); - return VARNAM_OK; - } - else if(strcmp(argv[1], "--version") == 0) { - varnam_info("%s\n", varnam_version); - return VARNAM_OK; - } - argv++; - argc--; - return execute_command(argc, argv); + c->symbols_file = (char *) xmalloc(file_length + 1); + if(!c->symbols_file) + return VARNAM_MEMORY_ERROR; + + strncpy(c->symbols_file, symbols_file, file_length + 1); + handle = &c; + return VARNAM_SUCCESS; } + diff --git a/module.h b/varnam.h similarity index 56% rename from module.h rename to varnam.h index 9efaf0d..2df2af8 100644 --- a/module.h +++ b/varnam.h @@ -1,6 +1,6 @@ -/* module.h +/* varnam.h * - * Copyright (C) 2010 Navaneeth.K.N + * Copyright (C) Navaneeth.K.N * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -17,10 +17,26 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifndef MODULE_H_INCLUDED_081242 -#define MODULE_H_INCLUDED_081242 +#ifndef VARNAM_H_INCLUDED_091620 +#define VARNAM_H_INCLUDED_091620 -int compile_scheme_file(int argc, char **argv); -int transliterate_input(int argc, char **argv); +#include "foreign/sqlite3.h" +#include "util.h" +#include + +struct varnam_internal { + sqlite3 *db; +}; + +typedef struct varnam { + char *symbols_file; + struct varnam_internal *internal; +} varnam; + +int varnam_init(const char *symbols_file, size_t file_length, varnam **handle, char **msg); + +int varnam_compile(varnam *handle, const char *scheme_file, const char *output_directory); + +int varnam_transliterate(varnam *handle, const char *input, struct strbuf *output); #endif diff --git a/vstgen.c b/vstgen.c index bdf5a30..8b437a8 100644 --- a/vstgen.c +++ b/vstgen.c @@ -19,6 +19,7 @@ #include "trie.h" #include "util.h" #include "parser.h" +#include "varnam-result-codes.h" #include "foreign/sqlite3.h" static const char *token_type_tostring(enum token_type type) @@ -53,7 +54,7 @@ static int callback(struct trie* t, unsigned int depth, void *userdata) int rc; char sql[500]; - if(t->root) return VARNAM_OK; + if(t->root) return VARNAM_SUCCESS; assert(userdata); assert(t->value); @@ -72,7 +73,7 @@ static int callback(struct trie* t, unsigned int depth, void *userdata) } varnam_info(":: (%d/%d) Processing : %s", ++current, totalitems, t->label); - return VARNAM_OK; + return VARNAM_SUCCESS; } extern int varnam_generate_symbols(const char *output_file_path, struct trie *result, char **error_message) @@ -133,5 +134,5 @@ extern int varnam_generate_symbols(const char *output_file_path, struct trie *re varnam_info(":: Symbols generated"); sqlite3_close( db ); - return VARNAM_OK; + return VARNAM_SUCCESS; }