You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2006/10/15 07:22:11 UTC

svn commit: r464124 - in /lucene/lucy/trunk: ./ charmonizer/ charmonizer/interpreters/ charmonizer/metaquote/ charmonizer/modules/ charmonizer/src/ charmonizer/src/Charmonizer/

Author: marvin
Date: Sat Oct 14 22:22:10 2006
New Revision: 464124

URL: http://svn.apache.org/viewvc?view=rev&rev=464124
Log:
Reimplement Charmonizer as C augmented by the metaquote source filter.

Added:
    lucene/lucy/trunk/charmonizer/charmonize.c
    lucene/lucy/trunk/charmonizer/metaquote/
    lucene/lucy/trunk/charmonizer/metaquote/metaquote.c
    lucene/lucy/trunk/charmonizer/src/
    lucene/lucy/trunk/charmonizer/src/Charmonizer/
    lucene/lucy/trunk/charmonizer/src/Charmonizer.charm
    lucene/lucy/trunk/charmonizer/src/Charmonizer.harm
    lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.charm
    lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.harm
    lucene/lucy/trunk/charmonizer/src/Charmonizer/FuncMacro.charm
    lucene/lucy/trunk/charmonizer/src/Charmonizer/FuncMacro.harm
    lucene/lucy/trunk/charmonizer/src/Charmonizer/Integers.charm
    lucene/lucy/trunk/charmonizer/src/Charmonizer/Integers.harm
    lucene/lucy/trunk/charmonizer/src/Charmonizer/LargeFiles.charm
    lucene/lucy/trunk/charmonizer/src/Charmonizer/LargeFiles.harm
    lucene/lucy/trunk/charmonizer/src/Charmonizer/UnusedVars.charm
    lucene/lucy/trunk/charmonizer/src/Charmonizer/UnusedVars.harm
    lucene/lucy/trunk/charmonizer/src/Charmonizer/VariadicMacros.charm
    lucene/lucy/trunk/charmonizer/src/Charmonizer/VariadicMacros.harm
Removed:
    lucene/lucy/trunk/charmonizer/interpreters/
    lucene/lucy/trunk/charmonizer/modules/
Modified:
    lucene/lucy/trunk/Build.PL
    lucene/lucy/trunk/charmonizer/README

Modified: lucene/lucy/trunk/Build.PL
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/Build.PL?view=diff&rev=464124&r1=464123&r2=464124
==============================================================================
--- lucene/lucy/trunk/Build.PL (original)
+++ lucene/lucy/trunk/Build.PL Sat Oct 14 22:22:10 2006
@@ -1,23 +1,179 @@
+use 5.008003;
 use strict;
 use warnings;
-use 5.008003;
+
 use Module::Build;
 
-use lib 'charmonizer/interpreters/perl';
-use Charmonizer;
+my $subclass_code = <<'END_BUILD_CLASS';
 
-my $charmonizer = Charmonizer->new(
-    modules => [
-        'charmonizer/modules/start.charm',
-        'charmonizer/modules/integers.charm',
-        'charmonizer/modules/end.charm',
-    ],
-    out_path => 'lucyport.h'
+package Lucy::Build;
+use strict;
+use warnings;
+use base qw( Module::Build );
+
+use File::Spec::Functions qw( catdir catfile splitpath );
+use File::Path qw( mkpath );
+use File::Find qw( find );
+use ExtUtils::CBuilder;
+use Config;
+
+my $METAQUOTE_EXE_PATH     = 'metaquote';
+my $CHARMONIZE_EXE_PATH    = 'charmonize';
+my $CHARMONIZER_SOURCE_DIR = catdir(qw( charmonizer src ));
+
+# Compile the metaquote source filter utility.
+sub ACTION_metaquote {
+    my $self        = shift;
+    my $source_path = catfile(qw( charmonizer metaquote metaquote.c ));
+
+    # don't compile if we're up to date
+    return if $self->up_to_date( [$source_path], $METAQUOTE_EXE_PATH );
+
+    # compile
+    print "\nBuilding $METAQUOTE_EXE_PATH...\n\n";
+    my $cbuilder = ExtUtils::CBuilder->new;
+    my $o_file = $cbuilder->compile( source => $source_path );
+    $cbuilder->link_executable(
+        objects  => [$o_file],
+        exe_file => $METAQUOTE_EXE_PATH,
+    );
+
+    # clean both the object file and the executable
+    $self->add_to_cleanup( $o_file, $METAQUOTE_EXE_PATH );
+}
+
+# Build the charmonize executable.
+sub ACTION_charmonizer {
+    my $self         = shift;
+    my $filtered_dir = catdir(qw( charmonizer filtered_src ));
+    if ( !-d $filtered_dir ) {
+        mkpath($filtered_dir) or die "can't mkpath '$filtered_dir': $!";
+    }
+
+    $self->dispatch('metaquote');
+
+    my $charm_source_files
+        = $self->_find_charm_source_files($CHARMONIZER_SOURCE_DIR);
+    my $filtered_files = $self->_metaquote_charm_files($charm_source_files);
+    my $charmonize_c   = catfile(qw( charmonizer charmonize.c ));
+    my @all_source     = ( $charmonize_c, @$filtered_files );
+
+    # don't compile if we're up to date
+    return if $self->up_to_date( \@all_source, $CHARMONIZE_EXE_PATH );
+
+    print "Building $CHARMONIZE_EXE_PATH...\n\n";
+
+    my $cbuilder = ExtUtils::CBuilder->new;
+
+    my @o_files;
+    for (@all_source) {
+        next unless /\.c$/;
+        my $o_file = $cbuilder->object_file($_);
+        push @o_files, $o_file;
+
+        next if $self->up_to_date( $_, $o_file );
+
+        $cbuilder->compile(
+            source       => $_,
+            include_dirs => [$filtered_dir],
+        );
+    }
+
+    my $exe_path = $cbuilder->link_executable(
+        objects  => \@o_files,
+        exe_file => $CHARMONIZE_EXE_PATH,
+    );
+
+    $self->add_to_cleanup( $filtered_dir, @$filtered_files, @o_files,
+        $CHARMONIZE_EXE_PATH, );
+}
+
+sub _find_charm_source_files {
+    my ( $self, $dir ) = @_;
+    my @files;
+    find(
+        {   wanted => sub {
+                if ( $File::Find::name =~ /\.c?harm$/ ) {
+                    push @files, $File::Find::name;
+                }
+            },
+            no_chdir => 1,
+        },
+        $dir,
+    );
+    return \@files;
+}
+
+sub _metaquote_charm_files {
+    my ( $self, $charm_files ) = @_;
+    my @filtered_files;
+
+    for my $source_path (@$charm_files) {
+        my $dest_path = $source_path;
+        $dest_path =~ s#(.*)src#$1filtered_src#;
+        $dest_path =~ s#\.charm#.c#;
+        $dest_path =~ s#\.harm#.h#;
+
+        push @filtered_files, $dest_path;
+
+        next if ( $self->up_to_date( $source_path, $dest_path ) );
+
+        # create directories if need be
+        my ( undef, $dir, undef ) = splitpath($dest_path);
+        if ( !-d $dir ) {
+            $self->add_to_cleanup($dir);
+            mkpath $dir or die "Couldn't mkpath $dir";
+        }
+
+        # run the metaquote filter
+        system("./$METAQUOTE_EXE_PATH $source_path $dest_path");
+    }
+
+    return \@filtered_files;
+}
+
+# Run the charmonizer executable, creating the lucyconf.h file.
+sub ACTION_lucyconf {
+    my $self = shift;
+
+    $self->dispatch('charmonizer');
+
+    my $lucyconf_path = "lucyconf.h";
+    return if $self->up_to_date( $CHARMONIZE_EXE_PATH, $lucyconf_path );
+
+    print "\nWriting $lucyconf_path...\n\n";
+    system(
+        "./$CHARMONIZE_EXE_PATH --outpath=$lucyconf_path --cc=$Config{cc}");
+
+    $self->add_to_cleanup($lucyconf_path);
+}
+
+sub ACTION_code {
+    my $self = shift;
+    $self->dispatch('lucyconf');
+    $self->SUPER::ACTION_code(@_);
+}
+
+END_BUILD_CLASS
+
+my $class = Module::Build->subclass(
+    class => 'Lucy::Build',
+    code  => $subclass_code,
 );
 
-$charmonizer->run;
+my $builder = $class->new(
+    module_name        => 'Lucy',
+    license            => 'apache',
+    dist_author        => 'Marvin Humphrey <marvin at rectangular dot com>',
+    dist_version_from  => 'src/perl/Lucy.pm',
+    create_makefile_pl => 'passthrough',
+    add_to_cleanup     => [
+        'Lucy-*',       'typemap', 'MANIFEST.bak', 'lastmod',
+        'perltidy.ERR', '*.o',
+    ],
+);
 
-# TODO: Write Lucy. :)  Then we can install it.
+$builder->create_build_script();
 
 __END__
 

Modified: lucene/lucy/trunk/charmonizer/README
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/README?view=diff&rev=464124&r1=464123&r2=464124
==============================================================================
--- lucene/lucy/trunk/charmonizer/README (original)
+++ lucene/lucy/trunk/charmonizer/README Sat Oct 14 22:22:10 2006
@@ -1,161 +1,75 @@
 NAME
 
-    Charmonizer - Mini-language targeted at writing portable C headers.
+    Charmonizer - write portable C header files
 
-OVERVIEW
+SYNOPSIS
 
-    Charmonizer is a tool for writing portable C header files.  It is
-    implemented using a small domain-specific language, also called
-    "Charmonizer", and a "native" interpreter, written in a language such as
-    Perl or Ruby.
-    
-    The Charmonizer language is neither succinct, nor flexible, nor powerful,
-    because the project's primary design specs did not require it to be any of
-    those things:
-    
-      1. It must write portable C header files.
-      2. It must require only a C compiler and the native build enviroment.
-      3. It must be easy to write a robust, reliable Charmonizer interpreter.
-    
-    Charmonizer source code consists of a linear series of function calls.
-    There are no conditionals, no loops, and no variables.  Of course it's
-    challenging to get much done without conditionals or variables, but
-    Charmonizer is able to rely upon C's preprocessor for those.  
-    
-    Charmonizer doesn't require complex functionality because basically all
-    it needs to do is attempt compilation of miniature C programs and capture
-    output from them when they build successfully.  If the C preprocessor had
-    exception handling along the lines of Perl's "eval $string" functionality
-    (ha ha), Charmonizer-the-language wouldn't be necessary.
-
-PROGRAM STRUCTURE
-
-    Each run of Charmonizer follows this outline:
-    
-        I.   Initialize a temporary target file named _charmonize.c 
-        II.  Process a series of .charm files, appending _charmonize.c as
-             they indicate.
-        III. Compile _charmonize.c, run it, and capture its output to a new
-             file: the portable C header file.
-
-    Each of these steps requires a Charmonizer interpreter.  Consult the
-    documentation of your favorite among them for details.
-
-CHARMONIZER SOURCE FILES
-
-    Charmonizer source files, as mentioned above, consist of a linear series of
-    function calls.  The first function called must always be CH_api_version.
-
-    The preferred file extension is '.charm'.
-  
-LANGUAGE SYNTAX
-
-    Functions names begin with 'CH_', and all function calls must be
-    terminated with a corresponding close directive beginning with 'CH_end_'.
-    For instance, if Charmonizer had a CH_print function, here's how it
-    might work:
-    
-        CH_print
-          string Hello, world.
-        CH_end_print
-    
-    However, Charmonizer doesn't have a CH_print function, because it
-    doesn't need one.
-    
-    Files are parsed line by line.  Leading whitespace at the front of any
-    line gets ignored (unless it is part of a quoted string), so Charmonizer
-    files can be sensibly indented.
-    
-    Each line falls into one of five categories:
-    
-      * blank 
-      * start of a function
-      * end of a function
-      * name-value parameter pair 
-      * part of a multi-line quoted string
-    
-    Quoted strings begin with 'CH_quote' and are terminated by
-    'CH_end_quote'.  The strings are completely literal -- no interpolation,
-    and no escapes.  (If for some odd reason you want to embed the text
-    "CH_quote" within a string, you're out of luck.)
-
-        CH_append_raw
-          string CH_quote
-            #define TYPEDEF_PREFIX "lucy_"
-          CH_end_quote
-        CH_end_append_raw
-
-    C-style multi-line comments delimited by '/*' and '*/' are allowed.
-    Comment delimiters within quoted strings are treated as literal text, not
-    comments.
-    
-        /* This is a valid comment. */
-        CH_append_raw
-          string CH_quote
-            /* This comment will end up in _charmonize.c */
-            /* This un-terminated comment will mess up _charmonize.c,
-             * but it's not invalid Charmonizer syntax, as it's 
-             * within a quoted string.
-          CH_end_quote
-        CH_end_append_raw /* This is another valid comment. */
-    
-    All functions accept only named parameters in the form of key-value pairs.
-    Parameter names must consist of only pure ASCII alphanumerics plus the
-    underscore, and cannot start with a digit.  
-
-        CH_do_something
-          an-illegal-parameter oops
-          4nother_illegal_parameter 4RGH!
-          a_valid_parameter Ah! Just right. /* value is "Ah! Just right." */
-        CH_end_do_something
-    
-    The value of the parameter starts with the first non-whitespace character
-    following the key and is terminated immediately following the last
-    non-whitespace character in the line (not including comments, which are
-    stripped beforehand).  In the event you need a value with leading or
-    trailing whitespace (e.g. a terminating newline, which won't ordinarily be
-    there) use the CH_quote mechanism.
-
-FUNCTIONS
-
-    CH_append_raw
-
-            CH_append_raw
-              string CH_quote
-                #define CONSTANT_PREFIX "LUCY_"
-              CH_end_quote
-            CH_end_append_raw
-
-        Append verbatim contents of the specified string to the target file.
-
-
-    CH_append_output
-
-            CH_append_output
-              source CH_quote
-                #include <stdio.h>
-                int main() 
-                {
-                    printf("#define SIZEOF_LONG %d\n", (int)sizeof(long));
-                    return 0;
-                }
-              CH_end_quote
-            CH_end_append_output
-
-        Compile the supplied source code and append its output to the target
-        file.  If the code fails to compile, do nothing.
-
-    CH_api_version
-    
-            CH_api_version
-              api_version 0.01
-            CH_end_api_version
-
-        Indicate the version of Charmonizer that a .charm file conforms to.
-
-AUTHOR
-
-    Marvin Humphrey, <marvin at rectangular dot com>
+    #include "Charmonizer.h"
+    #include "Charmonizer/Integers.h"
+    #include "Charmonizer/LargeFiles.h"
+
+    int main() {
+        /* open and begin config file */
+        FILE *config_fh = fopen("myconf.h", "w");
+        if (config_fh == NULL) {
+            fprintf(stderr, "Error opening myconf.h\n")
+            exit(1);
+        }
+        printf(config_fh, "/* Auto-generated file - do not edit!! */\n\n");
+        
+        /* set up Charmonizer */
+        chaz_set_compiler("gcc");
+        
+        /* run desired Charmonizer modules */
+        chaz_Integers_run(config_fh);
+        chaz_LargeFiles_run(config_fh);
+
+        return 0;
+    }
+
+DESCRIPTION
+
+    Charmonizer is a tool for writing portable C header files.  It works by
+    attempting to compile lots of little programs and analyzing the output
+    from those that build successfully.  It is intended to work with any ISO
+    C90-compilant compiler and (eventually) on any operating system, without
+    requiring a specific shell.
+
+    Charmonizer is organized as one central library, "Charmonizer.h", and a
+    suite of topically oriented modules, e.g. "Charmonizer/Integers.h",
+    "Charmonizer/LargeFiles.h".  Each module exports 1 primary function,
+    ModuleName_run(FILE* conf_fh), which appends its output to the supplied
+    file handle.  A typical Charmonizer application proceeds in two phases:
+
+        1) Set up the Charmonizer environment and open a config file 
+           for writing.
+        2) Run selected modules, building up the config file bit by bit.
+
+    The config file can be further customized either by writing your own data
+    to it or possibly by setting options which affect the behavior of certain
+    modules.
+
+METAQUOTE UTILITY
+
+    Charmonizer is a C program which writes C code.  Writing C from C is
+    ordinarily quite awkward, because when you try to embed more than a line
+    or two of C source code inside C string literals, things get ugly fast.  
+
+    To get around this problem, Charmonizer uses a source filter.  In the
+    "real" Charmonizer source code, longer code fragments are surrounded by
+    METAQUOTE tags instead of literal double quotes.  
+    
+        char hello_source[] = METAQUOTE
+            #include <stdio.h>
+            int main() { 
+                printf("Hello, world.\n");
+                return 0;
+            }
+        METAQUOTE;
+
+    These source files are run through the "metaquote" utility -- which
+    transforms everything between paired METAQUOTE tags into concatenated
+    string literals.  The result is hideous but valid C.
 
 COPYRIGHT AND LICENSE
 
@@ -174,5 +88,4 @@
      * implied.  See the License for the specific language governing
      * permissions and limitations under the License.
      */
-
 

Added: lucene/lucy/trunk/charmonizer/charmonize.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/charmonize.c?view=auto&rev=464124
==============================================================================
--- lucene/lucy/trunk/charmonizer/charmonize.c (added)
+++ lucene/lucy/trunk/charmonizer/charmonize.c Sat Oct 14 22:22:10 2006
@@ -0,0 +1,161 @@
+/* charmonize.c -- write lucyconf.h config file.
+ */
+
+#include "Charmonizer/Core.h"
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include "Charmonizer.h"
+#include "Charmonizer/FuncMacro.h"
+#include "Charmonizer/Integers.h"
+#include "Charmonizer/LargeFiles.h"
+#include "Charmonizer/UnusedVars.h"
+#include "Charmonizer/VariadicMacros.h"
+
+/* Path to where the config file will be written.
+ */
+static char *outpath;
+
+/* Process command line args, set up Charmonizer, etc. 
+ */
+static void
+init(int argc, char **argv);
+
+/* Assign the location where the config file will be written.
+ */
+static void 
+set_outpath(char* path);
+
+/* Start the config file.
+ */
+static void 
+start_conf_file();
+
+/* Write the last bit of the config file.
+ */
+static void 
+finish_conf_file(FILE *fh);
+
+int main(int argc, char **argv) 
+{
+    FILE *config_fh;
+
+    /* parse commmand line args, init Charmonizer, open outfile */
+    init(argc, argv);
+    config_fh = fopen(outpath, "w");
+    if (config_fh == NULL) {
+        fprintf(stderr, "Couldn't open '%s': %s", strerror(errno));
+        exit(1);
+    }
+    start_conf_file(config_fh);
+
+    /* modules section */
+    chaz_FuncMacro_run(config_fh);
+    chaz_Integers_run(config_fh);
+    chaz_LargeFiles_run(config_fh);
+    chaz_UnusedVars_run(config_fh);
+    chaz_VariadicMacros_run(config_fh);
+
+    /* write tail of config and clean up */
+    finish_conf_file(config_fh);
+    if (fclose(config_fh)) {
+        fprintf(stderr, 
+            "Error closing file '%s': %s", outpath, strerror(errno));
+        exit(1);
+    }
+    free(outpath);
+
+    return 0;
+}
+
+static void 
+init(int argc, char **argv) 
+{
+    int i;
+    char *compiler;
+    
+    /* start seting up Charmonizer */
+    chaz_set_prefixes("LUCY_", "Lucy_", "lucy_", "lucy_");
+
+    compiler = NULL;
+    outpath  = NULL;
+
+    /* process command line args */
+    for (i = 1; i < argc; i++) {
+        if (strncmp(argv[i], "--cc=", 5) == 0) {
+            compiler = argv[i] + 5;
+        }
+        else if (strncmp(argv[i], "--outpath=", 10) == 0) {
+            outpath = strdup(argv[i] + 10);
+        }
+    }
+
+    /* default to using "cc" to invoke the compiler */
+    compiler = compiler == NULL ? "cc" : compiler;
+    chaz_set_compiler(compiler);
+
+    /* require an outpath */
+    if (outpath == NULL) {
+        fprintf(stderr, 
+            "Usage: ./charmonize --outpath=OUTPATH [--cc=COMPILER]");
+        exit(1);
+    }
+}
+
+static void 
+start_conf_file(FILE *conf_fh) 
+{
+    fprintf(conf_fh,
+        "/* Header file auto-generated by Charmonizer. \n"
+        " * DO NOT EDIT THIS FILE!!\n"
+        " */\n\n"
+        "#ifndef H_LUCY_CONF\n"
+        "#define H_LUCY_CONF 1\n\n"
+    );
+}
+
+static void
+finish_conf_file(FILE *conf_fh) 
+{
+    fprintf(conf_fh,
+        "/* conf end */\n"
+        "#ifdef LUCY_USE_SHORT_NAMES\n"
+        "# define bool_t                    lucy_bool_t\n"
+        "# define i8_t                      lucy_i8_t\n"
+        "# define u8_t                      lucy_u8_t\n"
+        "# define i16_t                     lucy_i16_t\n"
+        "# define u16_t                     lucy_u16_t\n"
+        "# define i32_t                     lucy_i32_t\n"
+        "# define u32_t                     lucy_u32_t\n"
+        "# define i64_t                     lucy_i64_t\n"
+        "# define u64_t                     lucy_u64_t\n"
+        "# define Unused_Var(x)             Lucy_Unused_Var(x)\n"
+        "# define Unreachable_Return(type)  Lucy_Unreachable_Return(type)\n"
+        "# define FSeek                     lucy_FSeek\n"
+        "# define FTell                     lucy_FTell\n"
+        "#endif\n"
+    );
+
+    fprintf(conf_fh, 
+        "\n\n#endif /* H_LUCY_CONF */\n\n"
+    );
+}
+
+
+/**
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Added: lucene/lucy/trunk/charmonizer/metaquote/metaquote.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/metaquote/metaquote.c?view=auto&rev=464124
==============================================================================
--- lucene/lucy/trunk/charmonizer/metaquote/metaquote.c (added)
+++ lucene/lucy/trunk/charmonizer/metaquote/metaquote.c Sat Oct 14 22:22:10 2006
@@ -0,0 +1,163 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <stdarg.h>
+
+/* Replace text between paired METAQUOTE tags with concatenated C string
+ * literals.
+ */
+char*
+metaquote(char *source, size_t source_len, size_t *dest_len);
+
+/* Print a message to stderr and exit.
+ */
+void
+die(char *format, ...);
+
+/* Verify fseek and ftell.  Verify fread, too, but weakly.
+ */
+#define Check_IO_Op(op) \
+    if ((op) == -1) \
+        die("IO operation failed at line %d: %s", __LINE__, strerror(errno))
+
+int main(int argc, char **argv) 
+{
+    FILE   *in_fh, *out_fh;
+    char   *source, *dest;
+    size_t  source_len, dest_len;
+    int     chars_read;
+
+    /* die unless both input and output filenames were supplied */
+    if (argc != 3)
+        die("Usage: metaquote INPUT_FILEPATH OUTPUT_FILEPATH");
+
+    /* open the input file */
+    in_fh = fopen(argv[1], "r");
+    if (in_fh == NULL)
+        die("Can't open file '%s' for reading: %s", argv[1], strerror(errno));
+
+    /* get the length of the file */
+    Check_IO_Op( fseek(in_fh, 0, SEEK_END) );
+    Check_IO_Op( source_len = ftell(in_fh) );
+    Check_IO_Op( fseek(in_fh, 0, SEEK_SET) );
+
+    /* slurp input file and metaquote the contents */
+    source = (char*)malloc(source_len * sizeof(char));
+    Check_IO_Op( fread(source, sizeof(char), source_len, in_fh) );
+    dest = metaquote(source, source_len, &dest_len);
+    
+    /* blast out the metaquoted text */
+    out_fh = fopen(argv[2], "w");
+    if (out_fh == NULL)
+        die("Can't open file '%s' for writing: %s", argv[2], strerror(errno));
+    if (fwrite(dest, sizeof(char), dest_len, out_fh) != dest_len)
+        die("Error writing to '%s': %s", argv[2], strerror(errno));
+
+    /* clean up */
+    if (fclose(in_fh))
+        die("Error closing file '%s': %s", argv[1], strerror(errno));
+    if (fclose(out_fh))
+        die("Error closing file '%s': %s", argv[2], strerror(errno));
+    free(source);
+    free(dest);
+
+    return 0;
+}
+
+char*
+metaquote(char *source, size_t source_len, size_t *dest_len) 
+{
+    char *source_limit = source + source_len;
+    char *dest = (char*)malloc(100);
+    char *dest_start = dest;
+    char *dest_limit = dest + (100);
+    int   inside_metaquote = 0;
+
+    while (source < source_limit) {
+        size_t chars_left = source_limit - source;
+
+        /* reallocate dest if necessary */
+        if (dest_limit - dest < 20) {
+            size_t cur_len = dest - dest_start;
+            dest_start = (char*)realloc(dest_start, cur_len + 100 );
+            dest = dest_start + cur_len;
+            dest_limit = dest_start + cur_len + 100;
+        }
+
+        /* see if we're starting or ending a METAQUOTE */
+        if (chars_left >= 9 && strncmp(source, "METAQUOTE", 9) == 0) {
+            inside_metaquote = inside_metaquote == 0 ? 1 : 0;
+            *dest++ = '"';
+            source += 9;
+            continue;
+        }
+        /* if not inside a METAQUOTE, just copy source to dest */
+        else if (!inside_metaquote) {
+            *dest++ = *source++;
+            continue;
+        }
+
+        /* inside a METAQUOTE, so render as a series of C string literals */
+        else {
+            switch(*source) {
+            case '\\':
+                *dest++ = '\\';
+                *dest++ = '\\';
+                break;
+
+            case '"':
+                *dest++ = '\\';
+                *dest++ = '"';
+                break;
+
+            case '\r':
+                if ((source + 1 < source_limit) && (*(source+1) == '\n'))
+                    source++;
+                /* fall through */
+
+            case '\n':
+                strncpy(dest, "\\n\"\n    \"", 9);
+                dest += 9;
+                break;
+
+            default:
+                *dest++ = *source;
+            }
+
+            source++;
+        }
+    }
+
+    *dest_len = dest - dest_start;
+
+    return dest_start;
+}
+
+void 
+die(char* format, ...) 
+{
+    va_list args;
+    va_start(args, format);
+    vfprintf(stderr, format, args);
+    va_end(args);
+    fprintf(stderr, "\n");
+    exit(1);
+}
+
+/**
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Added: lucene/lucy/trunk/charmonizer/src/Charmonizer.charm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer.charm?view=auto&rev=464124
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer.charm (added)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer.charm Sat Oct 14 22:22:10 2006
@@ -0,0 +1,58 @@
+#define CHAZ_USE_SHORT_NAMES
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "Charmonizer.h"
+#include "Charmonizer/Core.h"
+
+void 
+chaz_set_compiler(char* comp_cmd) 
+{
+    size_t len = strlen(comp_cmd);
+
+    /* security check */
+    if (len > 100)
+        die("compiler command greater than 100 characters: %s", comp_cmd);
+
+    /* assign new val */
+    free(compiler);
+    compiler = (char*)malloc(len * (sizeof(char)) + 1);
+    compiler[len] = '\0';
+    strncpy(compiler, comp_cmd, len);
+}
+
+void
+chaz_set_prefixes(char *constant_pre, char *macro_pre, char* typedef_pre,  
+                  char *function_pre) {
+    free(constant_prefix);
+    free(macro_prefix);
+    free(typedef_prefix);
+    free(function_prefix);
+    constant_prefix  = strdup(constant_pre);
+    macro_prefix     = strdup(macro_pre);
+    typedef_prefix   = strdup(typedef_pre);
+    function_prefix  = strdup(function_pre);
+}
+
+char*
+chaz_slurp_file(char* filepath, size_t *len_ptr) {
+    return slurp_file(filepath, len_ptr);
+}
+
+/**
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Added: lucene/lucy/trunk/charmonizer/src/Charmonizer.harm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer.harm?view=auto&rev=464124
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer.harm (added)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer.harm Sat Oct 14 22:22:10 2006
@@ -0,0 +1,40 @@
+#ifndef H_CHAZ
+#define H_CHAZ 1
+
+#include <stddef.h>
+
+/* Tell Charmonizer the name of the compiler it should use.
+ */
+void
+chaz_set_compiler(char *compiler_string);
+
+/* These prefixes will be prepended to every symbol Charmonizer generates.
+ * By default, they are blank.
+ */ 
+void
+chaz_set_prefixes(char *constant_prefix, char *macro_prefix, 
+                  char *typedef_prefix,  char *function_prefix);
+
+/* Read an entire file into memory.
+ */
+char*
+chaz_slurp_file(char* filepath, size_t *len_ptr);
+
+#endif /* include guard */
+
+/**
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Added: lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.charm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.charm?view=auto&rev=464124
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.charm (added)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.charm Sat Oct 14 22:22:10 2006
@@ -0,0 +1,203 @@
+#define CHAZ_USE_SHORT_NAMES
+
+#include "Charmonizer/Core.h"
+#include <errno.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+
+char *compiler        = NULL;
+char *constant_prefix = NULL;
+char *macro_prefix    = NULL;
+char *typedef_prefix  = NULL;
+char *function_prefix = NULL;
+
+char*
+capture_output(char *source, size_t source_len, size_t *output_len) 
+{
+    char *captured_output = NULL;
+    FILE *app_source_fh, *targ_fh, *garbage_fh;
+    char  command[256];
+    int   command_len = 0;
+    int   successful;
+    
+    /* clear out previous versions and test to make sure removal worked */
+    if ( !remove_and_verify(TRY_APP_PATH) ) 
+        die("Failed to delete file '%s'", TRY_APP_PATH);
+    if ( !remove_and_verify(TARGET_PATH) )
+        die("Failed to delete file '%s'", TARGET_PATH);
+
+    /* write the source file */
+    app_source_fh = fopen(TRY_SOURCE_PATH, "w");
+    if (app_source_fh == NULL)
+        die("Error opening file '%s': %s", TRY_SOURCE_PATH, strerror(errno));
+    fwrite(source, source_len, sizeof(char), app_source_fh);
+    if (fclose(app_source_fh))
+        die("Error closing file '%s': %s", TRY_SOURCE_PATH, strerror(errno));
+
+    
+    /* security note: snprintf isn't available, so we just have to make sure
+     * that the compiler command is short enough at an earlier stage.
+     */
+    sprintf(command, "%s " TRY_SOURCE_PATH " -o " TRY_APP_PATH "\0", compiler);
+
+    /* Compile the source.
+     * Unfortunately there's no completely portable way to redirect stderr
+     * yet be able to recover it later, so we're stuck seeing the
+     * errors when something fails.
+     */
+    system(command);
+
+    /* see if compilation was successful */
+    garbage_fh = fopen(TRY_APP_PATH, "r");
+    successful = garbage_fh != NULL;
+    if (garbage_fh != NULL && fclose(garbage_fh)) 
+        die("Error closing file '%s': %s", TRY_SOURCE_PATH, strerror(errno));
+
+    /* if compilation was successful... */
+    if (successful) {
+        /* run the app */
+        sprintf(command, "./" TRY_APP_PATH "\0");
+        system(command);
+
+        /* slurp the app's output and write it to our fh */
+        captured_output = slurp_file(TARGET_PATH, output_len);
+    }
+    else {
+        *output_len = 0;
+    }
+
+    /* clean up */
+    remove(TRY_SOURCE_PATH);
+    remove(TRY_APP_PATH);
+    remove(TARGET_PATH);
+
+    return captured_output;
+}
+
+char*
+slurp_file(char *file_path, size_t *len_ptr) 
+{
+    FILE   *const file = fopen(file_path, "r");
+    char   *contents;
+    size_t  len;
+    long    check_val;
+
+    /* sanity check */
+    if (file == NULL)
+        die("Error opening file '%s': %s", file_path, strerror(errno));
+
+    /* find length; return NULL if the file has a zero-length */
+    len = flength(file);
+    if (len == 0) {
+        *len_ptr = 0;
+        return NULL;
+    }
+
+    /* allocate memory and read the file */
+    contents = (char*)malloc(len * sizeof(char));
+    if (contents == NULL)
+        die("Out of memory at %d, %s", __FILE__, __LINE__);
+    check_val = fread(contents, sizeof(char), len, file);
+
+    /* weak error check, because CRLF might result in fewer chars read */
+    if (check_val <= 0)
+        die("Tried to read %d characters of '%s', got %d", (int)len,
+            file_path, check_val);
+
+    /* set length pointer for benefit of caller */
+    *len_ptr = check_val;
+
+    /* clean up */
+    if (fclose(file))
+        die("Error closing file '%s': %s", file_path, strerror(errno));
+
+    return contents;
+}
+
+long 
+flength(FILE *f) 
+{
+    const long bookmark = ftell(f);
+    long check_val;
+    long len;
+
+    /* seek to end of file and check length */
+    check_val = fseek(f, 0, SEEK_END);
+    if (check_val == -1)
+        die("fseek error : %s\n", strerror(errno));
+    len = ftell(f);
+    if (len == -1)
+        die("fseek error : %s\n", strerror(errno));
+
+    /* return to where we were */
+    check_val = fseek(f, bookmark, SEEK_SET);
+    if (check_val == -1)
+        die("fseek error : %s\n", strerror(errno));
+
+    return len;
+}
+
+void 
+die(char* format, ...) 
+{
+    va_list args;
+    va_start(args, format);
+    vfprintf(stderr, format, args);
+    va_end(args);
+    fprintf(stderr, "\n");
+    exit(1);
+}
+
+void 
+warn(char* format, ...) 
+{
+    va_list args;
+    va_start(args, format);
+    vfprintf(stderr, format, args);
+    va_end(args);
+    fprintf(stderr, "\n");
+}
+
+int
+remove_and_verify(char *file_path) 
+{
+    /* try to remove the file */
+    remove(file_path);
+
+    /* return what *might* be success or failure */
+    return can_open_file(file_path) ? 0 : 1;
+}
+
+int
+can_open_file(char *file_path) 
+{
+    FILE *garbage_fh;
+
+    /* use fopen as a portable test for the existence of a file */
+    garbage_fh = fopen(file_path, "r");
+    if (garbage_fh == NULL) {
+        return 0;
+    }
+    else {
+        fclose(garbage_fh);
+        return 1;
+    }
+}
+
+/**
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Added: lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.harm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.harm?view=auto&rev=464124
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.harm (added)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.harm Sat Oct 14 22:22:10 2006
@@ -0,0 +1,119 @@
+/* Chaz/Core.h -- symbols used by modules in the Charmonizer core distro.
+ */
+
+#ifndef H_CHAZ_CORE
+#define H_CHAZ_CORE 1
+
+#include <stdio.h>
+#include <stddef.h>
+
+typedef int chaz_bool_t;
+#ifndef true
+# define true 1
+# define false 0
+#endif
+
+/* Temporary files used by Charmonizer. 
+ */
+#define CHAZ_CORE_TRY_SOURCE_PATH  "_charmonizer_try.c"
+#define CHAZ_CORE_TRY_APP_PATH     "_charmonizer_try"
+#define CHAZ_CORE_TARGET_PATH      "_charmonizer_target"
+
+/* Global variables.
+ */
+extern char *chaz_Core_compiler;
+extern char *chaz_Core_constant_prefix;
+extern char *chaz_Core_macro_prefix;
+extern char *chaz_Core_typedef_prefix;
+extern char *chaz_Core_function_prefix;
+
+/* Attempt to compile the supplied source code.  If successful, capture the 
+ * output of the program and return a pointer to a newly allocated buffer.
+ * If the compilation fails, return NULL.  The length of the captured 
+ * output will be placed into the buffer pointed to by [output_len].
+ */
+char*
+chaz_Core_capture_output(char *source, size_t source_len, size_t *output_len);
+
+/* Read an entire file into memory.
+ */
+char* 
+chaz_Core_slurp_file(char *file_path, size_t *len_ptr);
+
+/* Get the length of a file (may overshoot on text files under DOS).
+ */
+long  
+chaz_Core_flength(FILE *f);
+
+/* Print an error message to stderr and exit.
+ */
+void  
+chaz_Core_die(char *format, ...);
+
+/* Print an error message to stderr.
+ */
+void
+chaz_Core_warn(char *format, ...);
+
+/* Attept to delete a file.  Don't error if the file wasn't there to begin
+ * with.  Return 1 if it seems like the file is gone because an attempt to
+ * open it for reading fails (this doesn't guarantee that the file is gone,
+ * but it works well enough for our purposes).  Return 0 if we can still 
+ * read the file.
+ */
+int
+chaz_Core_remove_and_verify(char *file_path);
+
+/* Attempt to open a file for reading, then close it immediately.
+ */
+int
+chaz_Core_can_open_file(char *file_path);
+
+/* Print a "chapter heading" when starting a module. 
+ */
+#define Chaz_Core_Start_Run(filehandle) \
+    fprintf(filehandle, "\n/* %s */\n", __FILE__)
+
+/* Leave a little whitespace at the end of each module.
+ */
+#define Chaz_Core_End_Run(filehandle) \
+    fprintf(filehandle, "\n")
+
+#ifdef CHAZ_USE_SHORT_NAMES
+# define TRY_SOURCE_PATH        CHAZ_CORE_TRY_SOURCE_PATH
+# define TRY_APP_PATH           CHAZ_CORE_TRY_APP_PATH
+# define TARGET_PATH            CHAZ_CORE_TARGET_PATH
+# define compiler               chaz_Core_compiler
+# define constant_prefix        chaz_Core_constant_prefix
+# define macro_prefix           chaz_Core_macro_prefix
+# define typedef_prefix         chaz_Core_typedef_prefix
+# define function_prefix        chaz_Core_function_prefix
+# define capture_output         chaz_Core_capture_output 
+# define slurp_file             chaz_Core_slurp_file 
+# define flength                chaz_Core_flength 
+# define die                    chaz_Core_die 
+# define warn                   chaz_Core_warn 
+# define remove_and_verify      chaz_Core_remove_and_verify 
+# define can_open_file          chaz_Core_can_open_file 
+# define Start_Run              Chaz_Core_Start_Run
+# define End_Run                Chaz_Core_End_Run
+#endif
+
+#endif /* H_CHAZ_CORE */
+
+/**
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Added: lucene/lucy/trunk/charmonizer/src/Charmonizer/FuncMacro.charm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/FuncMacro.charm?view=auto&rev=464124
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/FuncMacro.charm (added)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/FuncMacro.charm Sat Oct 14 22:22:10 2006
@@ -0,0 +1,95 @@
+#define CHAZ_USE_SHORT_NAMES
+
+#include "Charmonizer/Core.h"
+#include "Charmonizer/FuncMacro.h"
+#include <string.h>
+#include <stdio.h>
+
+
+static char iso_func_code[] = METAQUOTE
+    #include <stdio.h>
+    int main() {
+        freopen("_charmonizer_target", "w", stdout);
+        printf("%s", __func__);
+        return 0;
+    }
+METAQUOTE;
+
+static char gnuc_func_code[] = METAQUOTE
+    #include <stdio.h>
+    int main() {
+        freopen("_charmonizer_target", "w", stdout);
+        printf("%s", __FUNCTION__);
+        return 0;
+    }
+METAQUOTE;
+
+
+void
+chaz_FuncMacro_run(FILE *conf_fh) 
+{
+    char *output;
+    size_t output_len;
+    chaz_bool_t has_funcmac      = false;
+    chaz_bool_t has_iso_funcmac  = false;
+    chaz_bool_t has_gnuc_funcmac = false;
+
+    Start_Run(conf_fh);
+    
+    /* check for ISO func macro */
+    output = capture_output(iso_func_code, strlen(iso_func_code), 
+        &output_len);
+    if (output != NULL && strncmp(output, "main", 4) == 0) {
+        has_funcmac     = true;
+        has_iso_funcmac = true;
+    }
+
+    /* check for GNUC func macro */
+    output = capture_output(gnuc_func_code, strlen(gnuc_func_code), 
+        &output_len);
+    if (output != NULL && strncmp(output, "main", 4) == 0) {
+        has_funcmac      = true;
+        has_gnuc_funcmac = true;
+    }
+
+    /* write out common defines */
+    if (has_funcmac) {
+        char *macro_text = has_iso_funcmac 
+            ? "__func__"
+            : "__FUNCTION__";
+        fprintf(conf_fh, 
+            "#define %sHAS_FUNC_MACRO\n"
+            "#define %sFunc_Macro %s\n",
+            constant_prefix, macro_prefix, macro_text);
+    }
+
+    /* write out specific defines */
+    if (has_iso_funcmac) {
+       fprintf(conf_fh, "#define %sHAS_ISO_FUNC_MACRO\n",
+           constant_prefix);
+    }
+    if (has_gnuc_funcmac) {
+       fprintf(conf_fh, "#define %sHAS_GNUC_FUNC_MACRO\n",
+           constant_prefix);
+    }
+
+    End_Run(conf_fh);
+}
+
+
+/**
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Added: lucene/lucy/trunk/charmonizer/src/Charmonizer/FuncMacro.harm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/FuncMacro.harm?view=auto&rev=464124
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/FuncMacro.harm (added)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/FuncMacro.harm Sat Oct 14 22:22:10 2006
@@ -0,0 +1,35 @@
+/* Charmonizer/FuncMacro.h
+ */
+
+#ifndef H_CHAZ_FUNC_MACRO
+#define H_CHAZ_FUNC_MACRO 
+
+#include <stdio.h>
+
+/* Run the FuncMacro module.
+ */
+void chaz_FuncMacro_run(FILE *conf_fh);
+
+#ifdef CHAZ_USE_SHORT_NAMES
+# define FuncMacro_run    chaz_FuncMacro_run
+#endif
+
+#endif /* H_CHAZ_FUNC_MACRO */
+
+
+/**
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Added: lucene/lucy/trunk/charmonizer/src/Charmonizer/Integers.charm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/Integers.charm?view=auto&rev=464124
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/Integers.charm (added)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/Integers.charm Sat Oct 14 22:22:10 2006
@@ -0,0 +1,185 @@
+#define CHAZ_USE_SHORT_NAMES
+
+#include "Charmonizer/Core.h"
+#include "Charmonizer/Integers.h"
+#include <string.h>
+#include <stdio.h>
+
+static char sizes_code[] = 
+    METAQUOTE
+        #include <stdio.h>
+        int main () {
+            freopen("_charmonizer_target", "w", stdout);
+            printf("%d %d %d %d %d",
+                (int)sizeof(char),
+                (int)sizeof(short),
+                (int)sizeof(int),
+                (int)sizeof(long),
+                (int)sizeof(void *)
+            );
+            return 0;
+        }
+    METAQUOTE;
+
+static char inttypes_code[] = 
+    METAQUOTE
+        #include <stdio.h>
+        #include <inttypes.h>
+        int main()
+        {
+            freopen("_charmonizer_target", "w", stdout);
+            printf("We have inttypes.h\n");
+            return 0;
+        }
+    METAQUOTE;
+
+static char long_long_code[] = 
+    METAQUOTE
+        #include <stdio.h>
+        int main() 
+        {
+            freopen("_charmonizer_target", "w", stdout);
+            long long foo = 4;
+            printf("%d", (int)sizeof(long long));
+            return 0;
+        }
+    METAQUOTE;
+
+void
+chaz_Integers_run(FILE *conf_fh) 
+{
+    char *output;
+    size_t output_len;
+    int sizeof_char       = -1; 
+    int sizeof_short      = -1; 
+    int sizeof_int        = -1; 
+    int sizeof_ptr        = -1; 
+    int sizeof_long       = -1;
+    int sizeof_long_long  = -1;
+    chaz_bool_t has_long_long = false;
+    chaz_bool_t has_inttypes  = false;
+
+    Start_Run(conf_fh);
+
+    /* Record sizeof() for several common integer types. */
+    output = capture_output(sizes_code, strlen(sizes_code), &output_len);
+    if (output != NULL) {
+        char *end_ptr = output;
+        
+        sizeof_char  = strtol(output, &end_ptr, 10);
+        output       = end_ptr;
+        sizeof_short = strtol(output, &end_ptr, 10);
+        output       = end_ptr;
+        sizeof_int   = strtol(output, &end_ptr, 10);
+        output       = end_ptr;
+        sizeof_long  = strtol(output, &end_ptr, 10);
+        output       = end_ptr;
+        sizeof_ptr   = strtol(output, &end_ptr, 10);
+    }
+
+    /* Determine whether long longs are available. */
+    output = capture_output(long_long_code, strlen(long_long_code),
+        &output_len);
+    if (output != NULL) {
+        has_long_long    = true;
+        sizeof_long_long = strtol(output, NULL, 10);
+    }
+
+    /* Determine whether inttypes.h is available. */
+    output = capture_output(inttypes_code, strlen(inttypes_code),
+        &output_len);
+    if (output != NULL) {
+        has_inttypes = true;
+    }
+
+    /* write out some conditional defines */
+    if (has_inttypes) {
+        fprintf(conf_fh, "#define %sHAS_INTTYPES_H\n", constant_prefix);
+    }
+    if (has_long_long) {
+        fprintf(conf_fh, "#define %sHAS_LONG_LONG\n", constant_prefix);
+    }
+
+    /* write out sizes */
+    fprintf(conf_fh, 
+        "#define %sSIZEOF_CHAR %d\n"
+        "#define %sSIZEOF_SHORT %d\n"
+        "#define %sSIZEOF_INT %d\n"
+        "#define %sSIZEOF_LONG %d\n"
+        "#define %sSIZEOF_PTR %d\n",
+        constant_prefix, sizeof_char,
+        constant_prefix, sizeof_short,
+        constant_prefix, sizeof_int,
+        constant_prefix, sizeof_long,
+        constant_prefix, sizeof_ptr
+    );
+    if (has_long_long) {
+        fprintf(conf_fh, "#define %sSIZEOF_LONG_LONG %d\n",
+            constant_prefix, sizeof_long_long);
+    }
+
+    /* write typedefs */
+    fprintf(conf_fh, "typedef int %sbool_t;\n", typedef_prefix);
+    if (sizeof_char == 1) {
+        fprintf(conf_fh,
+            "typedef char %si8_t;\n"
+            "typedef unsigned char %su8_t;\n",
+            typedef_prefix, typedef_prefix
+        );
+    }
+    if (sizeof_short == 2) {
+        fprintf(conf_fh,
+            "typedef short %si16_t;\n"
+            "typedef unsigned short %su16_t;\n",
+            typedef_prefix, typedef_prefix
+        );
+    }
+    if (sizeof_int == 4 || sizeof_long == 4) {
+        char *type = sizeof_int == 4 ? "int" : "long";
+        fprintf(conf_fh,
+            "typedef %s %si32_t;\n"
+            "typedef unsigned %s %su32_t;\n",
+            type, typedef_prefix, 
+            type, typedef_prefix
+        );
+    }
+    if (sizeof_long == 8 || sizeof_long_long == 8) {
+        char *type = sizeof_long == 8 ? "long" : "long long";
+        fprintf(conf_fh,
+            "typedef %s %si64_t;\n"
+            "typedef unsigned %s %su64_t;\n",
+            type, typedef_prefix, 
+            type, typedef_prefix
+        );
+    }
+
+    /* true and false */
+    fprintf(conf_fh,
+        "#ifndef true\n"
+        "# define true 1\n"
+        "#endif\n"
+        "#ifndef false\n"
+        "# define false 0\n"
+        "#endif\n"
+    );
+
+    End_Run(conf_fh);
+}
+
+
+/**
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Added: lucene/lucy/trunk/charmonizer/src/Charmonizer/Integers.harm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/Integers.harm?view=auto&rev=464124
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/Integers.harm (added)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/Integers.harm Sat Oct 14 22:22:10 2006
@@ -0,0 +1,72 @@
+/* Charmonizer/Integers.h -- info about integer types and sizes.
+ *
+ * Defines the following symbols:
+ * 
+ * SIZEOF_CHAR
+ * SIZEOF_SHORT
+ * SIZEOF_INT
+ * SIZEOF_LONG
+ * SIZEOF_PTR
+ *
+ * If long longs are available these symbols will be defined:
+ * 
+ * HAS_LONG_LONG
+ * SIZEOF_LONG_LONG 
+ *
+ * If the inttypes.h header file is available, this will be defined:
+ * 
+ * HAS_INTTYPES_H
+ * 
+ * The following typedefs will be created if a suitable integer type exists,
+ * as will most often be the case.  However, if for example a char is 64 bits
+ * (as on certain Crays), no 8-bit types will be defined, or if no 64-bit
+ * integer type is availabe, no 64-bit types will be defined, etc.
+ * 
+ * bool_t
+ * i8_t
+ * u8_t
+ * i16_t
+ * u16_t
+ * i32_t
+ * u32_t
+ * i64_t
+ * u64_t
+ *
+ * These symbols will be defined if they are not already: 
+ * 
+ * true
+ * false
+ */
+
+#ifndef H_CHAZ_INTEGERS
+#define H_CHAZ_INTEGERS 
+
+#include <stdio.h>
+
+/* Run the Integers module.
+ */
+void chaz_Integers_run(FILE *conf_fh);
+
+#ifdef CHAZ_USE_SHORT_NAMES
+# define Integers_run    chaz_Integers_run
+#endif
+
+#endif /* H_CHAZ_INTEGERS */
+
+
+/**
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Added: lucene/lucy/trunk/charmonizer/src/Charmonizer/LargeFiles.charm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/LargeFiles.charm?view=auto&rev=464124
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/LargeFiles.charm (added)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/LargeFiles.charm Sat Oct 14 22:22:10 2006
@@ -0,0 +1,122 @@
+#define CHAZ_USE_SHORT_NAMES
+
+#include "Charmonizer/Core.h"
+#include "Charmonizer/LargeFiles.h"
+#include <string.h>
+#include <stdio.h>
+
+static char ftello_code[] = METAQUOTE
+    #include <stdio.h>
+    #include <sys/types.h>
+    int main() {
+        off_t foo;
+        freopen("_charmonizer_target", "w", stdout);
+        printf("%d", (int)sizeof(off_t));
+        foo = ftello(stdout);
+        fseeko(stdout, 0, SEEK_SET);
+        return 0;
+    }
+METAQUOTE;
+
+static char ftello64_code[] = METAQUOTE
+    #include <stdio.h>
+    int main() {
+        off64_t foo;
+        freopen("_charmonizer_target", "w", stdout);
+        printf("%d", (int)sizeof(off64_t));
+        foo = ftello64(stdout);
+        fseeko64(stdout, 0, SEEK_SET);
+        return 0;
+    }
+METAQUOTE;
+
+static char sizeof_long_code[] = METAQUOTE
+    #include <stdio.h>
+    int main() {
+        freopen("_charmonizer_target", "w", stdout);
+        printf("%d\n", (int)sizeof(long));
+        return 0;
+    }
+METAQUOTE;
+
+void
+chaz_LargeFiles_run(FILE *conf_fh) 
+{
+    char *output;
+    char fseek_command[10];
+    char ftell_command[10];
+    size_t output_len;
+    chaz_bool_t has_off64_t = false;
+    int sizeof_off_t = -1;
+    int sizeof_long = -1;
+    chaz_bool_t success = false;
+
+    Start_Run(conf_fh);
+
+    /* check for ftello, fseeko, and 64-bit off_t */
+    output = capture_output(ftello_code, strlen(ftello_code), &output_len);
+    if (output != NULL) {
+        sizeof_off_t = strtol(output, NULL, 10);
+        if (sizeof_off_t == 8) {
+            success = true;
+            strcpy(fseek_command, "fseeko");
+            strcpy(ftell_command, "ftello");
+        }
+    }
+
+    /* check for ftello64, fseek064, and off64_t */
+    if (!success) {
+        output = capture_output(ftello64_code, strlen(ftello64_code), 
+            &output_len);
+        if (output != NULL) {
+            success = true;
+            has_off64_t = true;
+            strcpy(fseek_command, "fseeko64");
+            strcpy(ftell_command, "ftello64");
+        }
+    }
+
+    /* if longs are 8 bytes, then ftell/fseek will work fine */
+    if (!success){
+        output = capture_output(sizeof_long_code, strlen(sizeof_long_code),
+            &output_len);
+        if (output != NULL) {
+            sizeof_long = strtol(output, NULL, 10);
+            /* if longs are 8 bytes, ftell/fseek will work */
+            if (sizeof_long == 8) {
+                success = true;
+                strcpy(fseek_command, "fseek");
+                strcpy(ftell_command, "ftell");
+            }
+        }
+    }
+
+    if (success) {
+        fprintf(conf_fh, 
+            "#define %sFTell %s\n" 
+            "#define %sFSeek %s\n",
+            function_prefix, ftell_command,
+            function_prefix, fseek_command
+        );
+    }
+    
+    End_Run(conf_fh);
+}
+
+
+/**
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Added: lucene/lucy/trunk/charmonizer/src/Charmonizer/LargeFiles.harm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/LargeFiles.harm?view=auto&rev=464124
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/LargeFiles.harm (added)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/LargeFiles.harm Sat Oct 14 22:22:10 2006
@@ -0,0 +1,35 @@
+/* Charmonizer/LargeFiles.h
+ */
+
+#ifndef H_CHAZ_LARGE_FILES
+#define H_CHAZ_LARGE_FILES
+
+#include <stdio.h>
+
+/* Run the LargeFiles module.
+ */
+void chaz_LargeFiles_run(FILE *conf_fh);
+
+#ifdef CHAZ_USE_SHORT_NAMES
+# define LargeFiles_run    chaz_LargeFiles_run
+#endif
+
+#endif /* H_CHAZ_LARGE_FILES */
+
+
+/**
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Added: lucene/lucy/trunk/charmonizer/src/Charmonizer/UnusedVars.charm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/UnusedVars.charm?view=auto&rev=464124
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/UnusedVars.charm (added)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/UnusedVars.charm Sat Oct 14 22:22:10 2006
@@ -0,0 +1,39 @@
+#define CHAZ_USE_SHORT_NAMES
+
+#include "Charmonizer/Core.h"
+#include "Charmonizer/UnusedVars.h"
+#include <string.h>
+#include <stdio.h>
+
+
+void
+chaz_UnusedVars_run(FILE *conf_fh) 
+{
+    Start_Run(conf_fh);
+    
+    fprintf(conf_fh, 
+        "#define %sUnused_Var(x) ((void)x)\n"
+        "#define %sUnreachable_Return(type) return (type)0\n",
+        macro_prefix, macro_prefix 
+    );
+
+    End_Run(conf_fh);
+}
+
+
+/**
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Added: lucene/lucy/trunk/charmonizer/src/Charmonizer/UnusedVars.harm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/UnusedVars.harm?view=auto&rev=464124
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/UnusedVars.harm (added)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/UnusedVars.harm Sat Oct 14 22:22:10 2006
@@ -0,0 +1,35 @@
+/* Charmonizer/UnusedVars.h
+ */
+
+#ifndef H_CHAZ_UNUSED_VARS
+#define H_CHAZ_UNUSED_VARS 
+
+#include <stdio.h>
+
+/* Run the UnusedVars module.
+ */
+void chaz_UnusedVars_run(FILE *conf_fh);
+
+#ifdef CHAZ_USE_SHORT_NAMES
+# define UnusedVars_run    chaz_UnusedVars_run
+#endif
+
+#endif /* H_CHAZ_UNUSED_VARS */
+
+
+/**
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Added: lucene/lucy/trunk/charmonizer/src/Charmonizer/VariadicMacros.charm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/VariadicMacros.charm?view=auto&rev=464124
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/VariadicMacros.charm (added)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/VariadicMacros.charm Sat Oct 14 22:22:10 2006
@@ -0,0 +1,82 @@
+#define CHAZ_USE_SHORT_NAMES
+
+#include "Charmonizer/Core.h"
+#include "Charmonizer/VariadicMacros.h"
+#include <string.h>
+#include <stdio.h>
+
+
+static char iso_code[] = METAQUOTE
+    #include <stdio.h>
+    #define ISO_TEST(fmt, ...) \
+        printf(fmt, __VA_ARGS__)
+    int main() {
+        freopen("_charmonizer_target", "w", stdout);
+        ISO_TEST("%d %d", 1, 1);
+        return 0;
+    }
+METAQUOTE;
+
+static char gnuc_code[] = METAQUOTE
+    #include <stdio.h>
+    #define GNU_TEST(fmt, args...) \
+        printf(fmt, ##args)
+    int main() {
+        freopen("_charmonizer_target", "w", stdout);
+        GNU_TEST("%d %d", 1, 1);
+        return 0;
+    }
+METAQUOTE;
+
+void
+chaz_VariadicMacros_run(FILE *conf_fh) 
+{
+    char *output;
+    size_t output_len;
+    chaz_bool_t has_varmacros      = false;
+    chaz_bool_t has_iso_varmacros  = false;
+    chaz_bool_t has_gnuc_varmacros = false;
+
+    Start_Run(conf_fh);
+
+    /* test for ISO-style variadic macros */
+    output = capture_output(iso_code, strlen(iso_code), &output_len);
+    if (output != NULL) {
+        has_varmacros = true;
+        fprintf(conf_fh, "#define %sHAS_VARIADIC_MACROS\n", constant_prefix);
+        fprintf(conf_fh, "#define %sHAS_ISO_VARIADIC_MACROS\n", 
+            constant_prefix);
+    }
+
+    /* test for GNU-style variadic macros */
+    output = capture_output(gnuc_code, strlen(gnuc_code), &output_len);
+    if (output != NULL) {
+        if (has_varmacros == false) {
+            has_varmacros = true;
+            fprintf(conf_fh, "#define %sHAS_VARIADIC_MACROS\n", 
+                constant_prefix);
+        }
+        fprintf(conf_fh, "#define %sHAS_GNUC_VARIADIC_MACROS\n", 
+            constant_prefix);
+    }
+
+    End_Run(conf_fh);
+}
+
+
+/**
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+

Added: lucene/lucy/trunk/charmonizer/src/Charmonizer/VariadicMacros.harm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/VariadicMacros.harm?view=auto&rev=464124
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/VariadicMacros.harm (added)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/VariadicMacros.harm Sat Oct 14 22:22:10 2006
@@ -0,0 +1,35 @@
+/* Charmonizer/VariadicMacros.h
+ */
+
+#ifndef H_CHAZ_VARIADIC_MACROS
+#define H_CHAZ_VARIADIC_MACROS 
+
+#include <stdio.h>
+
+/* Run the VariadicMacros module.
+ */
+void chaz_VaradicMacros_run(FILE *conf_fh);
+
+#ifdef CHAZ_USE_SHORT_NAMES
+# define VariadicMacros_run    chaz_VaradicMacros_run
+#endif
+
+#endif /* H_CHAZ_VARIADIC_MACROS */
+
+
+/**
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+