00001
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005 #include "header.h"
00006
00007 static int eq(char * s1, char * s2) {
00008 int s1_len = strlen(s1);
00009 int s2_len = strlen(s2);
00010 return s1_len == s2_len && memcmp(s1, s2, s1_len) == 0;
00011 }
00012
00013 static void print_arglist(void) {
00014 fprintf(stderr, "Usage: snowball <file> [options]\n\n"
00015 "options are: [-o[utput] file]\n"
00016 " [-s[yntax]]\n"
00017 #ifndef DISABLE_JAVA
00018 " [-j[ava]]\n"
00019 #endif
00020 " [-c++]\n"
00021 " [-w[idechars]]\n"
00022 " [-u[tf8]]\n"
00023 " [-n[ame] class name]\n"
00024 " [-ep[refix] string]\n"
00025 " [-vp[refix] string]\n"
00026 " [-i[nclude] directory]\n"
00027 " [-r[untime] path to runtime headers]\n"
00028 " [-p[arentclassname] parent class name]\n"
00029 );
00030 exit(1);
00031 }
00032
00033 static void check_lim(int i, int argc) {
00034 if (i >= argc) {
00035 fprintf(stderr, "argument list is one short\n");
00036 print_arglist();
00037 }
00038 }
00039
00040 static FILE * get_output(symbol * b) {
00041 char * s = b_to_s(b);
00042 FILE * output = fopen(s, "w");
00043 if (output == 0) {
00044 fprintf(stderr, "Can't open output %s\n", s);
00045 exit(1);
00046 }
00047 free(s);
00048 return output;
00049 }
00050
00051 static void read_options(struct options * o, int argc, char * argv[]) {
00052 char * s;
00053 int i = 2;
00054
00055
00056
00057 o->output_file = 0;
00058 o->syntax_tree = false;
00059 o->externals_prefix = "";
00060 o->variables_prefix = 0;
00061 o->runtime_path = 0;
00062 o->parent_class_name = 0;
00063 o->name = "";
00064 o->make_lang = LANG_C;
00065 o->widechars = false;
00066 o->includes = 0;
00067 o->includes_end = 0;
00068 o->utf8 = false;
00069
00070
00071
00072 repeat {
00073 if (i >= argc) break;
00074 s = argv[i++];
00075 { if (eq(s, "-o") || eq(s, "-output")) {
00076 check_lim(i, argc);
00077 o->output_file = argv[i++];
00078 continue;
00079 }
00080 if (eq(s, "-n") || eq(s, "-name")) {
00081 check_lim(i, argc);
00082 o->name = argv[i++];
00083 continue;
00084 }
00085 #ifndef DISABLE_JAVA
00086 if (eq(s, "-j") || eq(s, "-java")) {
00087 o->make_lang = LANG_JAVA;
00088 o->widechars = true;
00089 continue;
00090 }
00091 #endif
00092 if (eq(s, "-c++")) {
00093 o->make_lang = LANG_CPLUSPLUS;
00094 continue;
00095 }
00096 if (eq(s, "-w") || eq(s, "-widechars")) {
00097 o->widechars = true;
00098 o->utf8 = false;
00099 continue;
00100 }
00101 if (eq(s, "-s") || eq(s, "-syntax")) {
00102 o->syntax_tree = true;
00103 continue;
00104 }
00105 if (eq(s, "-ep") || eq(s, "-eprefix")) {
00106 check_lim(i, argc);
00107 o->externals_prefix = argv[i++];
00108 continue;
00109 }
00110 if (eq(s, "-vp") || eq(s, "-vprefix")) {
00111 check_lim(i, argc);
00112 o->variables_prefix = argv[i++];
00113 continue;
00114 }
00115 if (eq(s, "-i") || eq(s, "-include")) {
00116 check_lim(i, argc);
00117
00118 {
00119 NEW(include, p);
00120 symbol * b = add_s_to_b(0, argv[i++]);
00121 b = add_s_to_b(b, "/");
00122 p->next = 0; p->b = b;
00123
00124 if (o->includes == 0) o->includes = p; else
00125 o->includes_end->next = p;
00126 o->includes_end = p;
00127 }
00128 continue;
00129 }
00130 if (eq(s, "-r") || eq(s, "-runtime")) {
00131 check_lim(i, argc);
00132 o->runtime_path = argv[i++];
00133 continue;
00134 }
00135 if (eq(s, "-u") || eq(s, "-utf8")) {
00136 o->utf8 = true;
00137 o->widechars = false;
00138 continue;
00139 }
00140 if (eq(s, "-p") || eq(s, "-parentclassname")) {
00141 check_lim(i, argc);
00142 o->parent_class_name = argv[i++];
00143 continue;
00144 }
00145 fprintf(stderr, "'%s' misplaced\n", s);
00146 print_arglist();
00147 }
00148 }
00149 }
00150
00151 extern int main(int argc, char * argv[]) {
00152
00153 NEW(options, o);
00154 if (argc == 1) print_arglist();
00155 read_options(o, argc, argv);
00156 {
00157 symbol * filename = add_s_to_b(0, argv[1]);
00158 symbol * u = get_input(filename);
00159 if (u == 0) {
00160 fprintf(stderr, "Can't open input %s\n", argv[1]);
00161 exit(1);
00162 }
00163 {
00164 struct tokeniser * t = create_tokeniser(u);
00165 struct analyser * a = create_analyser(t);
00166 t->widechars = o->widechars;
00167 t->includes = o->includes;
00168 a->utf8 = t->utf8 = o->utf8;
00169 read_program(a);
00170 if (t->error_count > 0) exit(1);
00171 if (o->syntax_tree) print_program(a);
00172 close_tokeniser(t);
00173 unless (o->syntax_tree) {
00174 struct generator * g;
00175
00176 char * s = o->output_file;
00177 unless (s) {
00178 fprintf(stderr, "Please include the -o option\n");
00179 print_arglist();
00180 exit(1);
00181 }
00182 if (o->make_lang == LANG_C || o->make_lang == LANG_CPLUSPLUS) {
00183 symbol * b = add_s_to_b(0, s);
00184 b = add_s_to_b(b, ".h");
00185 o->output_h = get_output(b);
00186 b[SIZE(b) - 1] = 'c';
00187 if (o->make_lang == LANG_CPLUSPLUS) {
00188 b = add_s_to_b(b, "c");
00189 }
00190 o->output_c = get_output(b);
00191 lose_b(b);
00192
00193 g = create_generator_c(a, o);
00194 generate_program_c(g);
00195 close_generator_c(g);
00196 fclose(o->output_c);
00197 fclose(o->output_h);
00198 }
00199 #ifndef DISABLE_JAVA
00200 if (o->make_lang == LANG_JAVA) {
00201 symbol * b = add_s_to_b(0, s);
00202 b = add_s_to_b(b, ".java");
00203 o->output_java = get_output(b);
00204 lose_b(b);
00205 g = create_generator_java(a, o);
00206 generate_program_java(g);
00207 close_generator_java(g);
00208 fclose(o->output_java);
00209 }
00210 #endif
00211 }
00212 close_analyser(a);
00213 }
00214 lose_b(u);
00215 lose_b(filename);
00216 }
00217 { struct include * p = o->includes;
00218 until (p == 0)
00219 { struct include * q = p->next;
00220 lose_b(p->b); FREE(p); p = q;
00221 }
00222 }
00223 FREE(o);
00224 unless (space_count == 0) fprintf(stderr, "%d blocks unfreed\n", space_count);
00225 return 0;
00226 }
00227