| 1 | /** |
|---|
| 2 | * mm_crules.c -> Part of Crules Programming language |
|---|
| 3 | * |
|---|
| 4 | * Crules is the legal property of its developers. Please refer to the |
|---|
| 5 | * COPYRIGHT file distributed with this source distribution. |
|---|
| 6 | * |
|---|
| 7 | * This program is free software: you can redistribute it and/or modify |
|---|
| 8 | * it under the terms of the GNU General Public License as published by |
|---|
| 9 | * the Free Software Foundation, either version 3 of the License, or |
|---|
| 10 | * (at your option) any later version. |
|---|
| 11 | * |
|---|
| 12 | * This program is distributed in the hope that it will be useful, |
|---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | * GNU General Public License for more details. |
|---|
| 16 | * |
|---|
| 17 | * You should have received a copy of the GNU General Public License |
|---|
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 19 | **/ |
|---|
| 20 | |
|---|
| 21 | #ifdef HAVE_CONFIG_H |
|---|
| 22 | # include "config.h" |
|---|
| 23 | #else |
|---|
| 24 | # define CMAKE 1 |
|---|
| 25 | # include "config.h.cmake" |
|---|
| 26 | #endif |
|---|
| 27 | |
|---|
| 28 | #include <stdio.h> |
|---|
| 29 | #include <stdlib.h> |
|---|
| 30 | #include <string.h> |
|---|
| 31 | |
|---|
| 32 | #include <time.h> |
|---|
| 33 | |
|---|
| 34 | #include <crules/crules.h> |
|---|
| 35 | #include <crules/opcodes.h> |
|---|
| 36 | #include <crules/symbols.h> |
|---|
| 37 | #include <crules/objects.h> |
|---|
| 38 | #include <crules/backend.h> |
|---|
| 39 | #include <crules/runtime.h> |
|---|
| 40 | #include <crules/garbage.h> |
|---|
| 41 | #include <crules/operators.h> |
|---|
| 42 | |
|---|
| 43 | static int crules( const char * ); |
|---|
| 44 | |
|---|
| 45 | static void print_version( void ); |
|---|
| 46 | |
|---|
| 47 | static void print_help( const char * ); |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | *@param rules if not NULL should be the path to the, crules code |
|---|
| 51 | * to be run! Else if NULL will start an interactive session |
|---|
| 52 | * |
|---|
| 53 | *@return returns EXIT_SUCCESS or EXIT_FAILURE |
|---|
| 54 | **/ |
|---|
| 55 | static |
|---|
| 56 | int crules( const char* fin ) |
|---|
| 57 | { |
|---|
| 58 | int err= EXIT_SUCCESS; |
|---|
| 59 | crl_init_tables( ); |
|---|
| 60 | crl_init_signals( ); |
|---|
| 61 | |
|---|
| 62 | if( fin ) |
|---|
| 63 | { |
|---|
| 64 | crl_debug("trying to open '%s'!\n", fin); |
|---|
| 65 | FILE* f= fopen( fin, "rb" ); |
|---|
| 66 | if( f != (FILE*)0 ) |
|---|
| 67 | { |
|---|
| 68 | err= crl_parse( f ); |
|---|
| 69 | if( f != (FILE*)0 ) |
|---|
| 70 | { |
|---|
| 71 | fclose( f ); |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | else |
|---|
| 75 | { |
|---|
| 76 | crl_error("unable to open file %s\n", fin); |
|---|
| 77 | err= EXIT_FAILURE; |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | else |
|---|
| 81 | { |
|---|
| 82 | // print the nice header messages |
|---|
| 83 | time_t lt= time( NULL ); |
|---|
| 84 | fprintf( stdout, "Crules %s - ", VERSION ); |
|---|
| 85 | fprintf( stdout, "%s", ctime( < ) ); |
|---|
| 86 | #ifndef CMAKE |
|---|
| 87 | fprintf( stdout, "[ %s:%s ( %s:%s ) ]\n", CONFIG_CC, |
|---|
| 88 | CONFIG_CC_VER, SYSTEM_TYPE, MACHINE_TYPE ); |
|---|
| 89 | #endif |
|---|
| 90 | // read from stdin since no specified input |
|---|
| 91 | err= crl_parse( stdin ); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | crl_cleanup( ); |
|---|
| 95 | return err; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | /** |
|---|
| 99 | *@return prints the version info |
|---|
| 100 | **/ |
|---|
| 101 | static |
|---|
| 102 | void print_version( void ) |
|---|
| 103 | { |
|---|
| 104 | fprintf( crlout, "Crules interpreter version: %s\n", VERSION ); |
|---|
| 105 | fprintf( crlout, "Copyright (c) Philip Herron 2009-2010\n" ); |
|---|
| 106 | #ifndef CMAKE |
|---|
| 107 | fprintf( crlout, "Compiled for: %s %s\n", SYSTEM_TYPE, MACHINE_TYPE ); |
|---|
| 108 | fprintf( crlout, "Report suggestions and bugs to: %s\n", PACKAGE_BUGREPORT ); |
|---|
| 109 | #endif |
|---|
| 110 | #if defined(MAINTAINER_INFO) |
|---|
| 111 | fprintf( crlout, "%s!\n", MAINTAINER_INFO ); |
|---|
| 112 | #endif |
|---|
| 113 | |
|---|
| 114 | fprintf( crlout, "\nThis is free software; see the source for copying conditions. There is NO\n" ); |
|---|
| 115 | fprintf( crlout, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n" ); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | /** |
|---|
| 119 | *@return prints the help info |
|---|
| 120 | **/ |
|---|
| 121 | static |
|---|
| 122 | void print_help( const char *start ) |
|---|
| 123 | { |
|---|
| 124 | fprintf( crlout, "Crules usage: %s [OPTION]... [FILE]...\n", start ); |
|---|
| 125 | fprintf( crlout, " -h, --help \t prints this help\n"); |
|---|
| 126 | fprintf( crlout, " -v, --version \t prints the version\n"); |
|---|
| 127 | fprintf( crlout, " <file.crl> \t executes on the file else interactive\n\n"); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | /** |
|---|
| 131 | * @param argc the number or arguments |
|---|
| 132 | * @param argv the arguments themselves |
|---|
| 133 | * |
|---|
| 134 | * @returns the main method is the entry! |
|---|
| 135 | **/ |
|---|
| 136 | int main( int argc, char *argv[] ) |
|---|
| 137 | { |
|---|
| 138 | unsigned exit= EXIT_SUCCESS; |
|---|
| 139 | if( argc == 1 ) |
|---|
| 140 | { |
|---|
| 141 | //interactive session |
|---|
| 142 | return ( crules( NULL ) ); |
|---|
| 143 | } |
|---|
| 144 | else |
|---|
| 145 | { |
|---|
| 146 | int i= 1; |
|---|
| 147 | for( ; i<argc; ++i ) |
|---|
| 148 | { |
|---|
| 149 | if( strcmp("--version", argv[i]) == 0 ) |
|---|
| 150 | { |
|---|
| 151 | print_version( ); |
|---|
| 152 | exit= EXIT_SUCCESS; |
|---|
| 153 | break; |
|---|
| 154 | } |
|---|
| 155 | else if( strcmp("--help", argv[i]) == 0 ) |
|---|
| 156 | { |
|---|
| 157 | print_help( argv[0] ); |
|---|
| 158 | exit= EXIT_SUCCESS; |
|---|
| 159 | break; |
|---|
| 160 | } |
|---|
| 161 | //assume file! |
|---|
| 162 | else |
|---|
| 163 | { |
|---|
| 164 | exit= ( crules( argv[i] ) ); |
|---|
| 165 | break; |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | } |
|---|
| 169 | return exit; |
|---|
| 170 | } |
|---|