00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 #include <stdio.h>
00071 #include <stdlib.h>
00072 #include <string.h>
00073 #include <errno.h>
00074
00075 #if HAVE_UNISTD_H
00076 #include <unistd.h>
00077 #endif
00078
00079 #include <sys/types.h>
00080 #include <sys/stat.h>
00081
00082 #include "gwlib/gwlib.h"
00083 #include "ws.h"
00084
00085
00086
00087
00088 #undef malloc
00089 #undef realloc
00090 #undef free
00091
00092
00093
00094
00095
00096 static void usage(void);
00097
00098
00099 static void pragma_meta(const WsUtf8String *property_name,
00100 const WsUtf8String *content,
00101 const WsUtf8String *scheme,
00102 void *context);
00103
00104
00105
00106
00107 static char *program;
00108
00109
00110 static int eval_data = 0;
00111
00112
00113
00114 int main(int argc, char *argv[])
00115 {
00116 int i;
00117 WsCompilerParams params;
00118 WsCompilerPtr compiler;
00119 WsResult result;
00120 int opt;
00121
00122 program = strrchr(argv[0], '/');
00123 if (program)
00124 program++;
00125 else
00126 program = argv[0];
00127
00128
00129
00130 memset(¶ms, 0, sizeof(params));
00131
00132
00133 while ((opt = getopt(argc, argv, "adhsv")) != EOF) {
00134 switch (opt) {
00135 case 'a':
00136 params.print_assembler = 1;
00137 break;
00138
00139 case 'd':
00140 eval_data = 1;
00141 break;
00142
00143 case 'h':
00144 usage();
00145 exit(0);
00146 break;
00147
00148 case 'l':
00149 params.use_latin1_strings = 1;
00150 break;
00151
00152 case 'p':
00153 params.meta_name_cb = pragma_meta;
00154 params.meta_name_cb_context = "meta name";
00155
00156 params.meta_http_equiv_cb = pragma_meta;
00157 params.meta_http_equiv_cb_context = "meta http equiv";
00158 break;
00159
00160 case 's':
00161 params.print_symbolic_assembler = 1;
00162 break;
00163
00164 case 'v':
00165 params.verbose = 1;
00166 break;
00167
00168 case '?':
00169 printf("Try `%s -h' for a complete list of options.\n",
00170 program);
00171 exit(1);
00172 }
00173 }
00174
00175
00176
00177 compiler = ws_create(¶ms);
00178 if (compiler == NULL) {
00179 fprintf(stderr, "wsc: could not create compiler\n");
00180 exit(1);
00181 }
00182
00183 for (i = optind; i < argc; i++) {
00184 FILE *ifp, *ofp;
00185 char *outname;
00186
00187 ifp = fopen(argv[i], "rb");
00188 if (ifp == NULL) {
00189 fprintf(stderr, "wsc: could not open input file `%s': %s'\n",
00190 argv[i], strerror(errno));
00191 exit(1);
00192 }
00193
00194
00195 outname = malloc(strlen(argv[i]) + 1 + 1);
00196 if (outname == NULL) {
00197 fprintf(stderr, "wmlsc: could not create output file name: %s\n",
00198 strerror(errno));
00199 exit(1);
00200 }
00201 strcpy(outname, argv[i]);
00202 strcat(outname, "c");
00203
00204 ofp = fopen(outname, "wb");
00205 if (ofp == NULL) {
00206 fprintf(stderr, "wsc: could not create output file `%s': %s\n",
00207 outname, strerror(errno));
00208 exit(1);
00209 }
00210
00211 if (eval_data) {
00212
00213 struct stat stat_st;
00214 unsigned char *data;
00215 unsigned char *output;
00216 size_t output_len;
00217
00218 if (stat(argv[i], &stat_st) == -1) {
00219 fprintf(stderr, "wsc: could not stat input file `%s': %s\n",
00220 argv[i], strerror(errno));
00221 exit(1);
00222 }
00223
00224
00225 data = malloc(stat_st.st_size);
00226 if (data == NULL) {
00227 fprintf(stderr, "wsc: could not allocate input buffer: %s\n",
00228 strerror(errno));
00229 exit(1);
00230 }
00231 if (fread(data, 1, stat_st.st_size, ifp) < (size_t) stat_st.st_size) {
00232 fprintf(stderr, "wsc: could not read data: %s\n",
00233 strerror(errno));
00234 exit(1);
00235 }
00236 result = ws_compile_data(compiler, argv[i], data, stat_st.st_size,
00237 &output, &output_len);
00238 if (result == WS_OK) {
00239
00240 if (fwrite(output, 1, output_len, ofp) != output_len) {
00241 fprintf(stderr,
00242 "wsc: could not save output to file `%s': %s\n",
00243 outname, strerror(errno));
00244 exit(1);
00245 }
00246 }
00247 free(data);
00248 ws_free_byte_code(output);
00249 } else {
00250
00251 result = ws_compile_file(compiler, argv[i], ifp, ofp);
00252 }
00253
00254
00255 fclose(ifp);
00256 fclose(ofp);
00257
00258 if (result != WS_OK) {
00259 remove(outname);
00260 fprintf(stderr, "wsc: compilation failed: %s\n",
00261 ws_result_to_string(result));
00262 exit(1);
00263 }
00264 free(outname);
00265 }
00266
00267 ws_destroy(compiler);
00268
00269 return 0;
00270 }
00271
00272
00273
00274 static void usage(void)
00275 {
00276 printf("Usage: %s OPTION... FILE...\n\
00277 \n\
00278 -a disassemble resulting byte-code and print it to the\n\
00279 standard output\n\
00280 -d use ws_eval_data() function instead of ws_eval_file()\n\
00281 -h print this help message and exit successfully\n\
00282 -l encode strings in ISO-8859/1 (ISO latin1) instead of using\n\
00283 UTF-8\n\
00284 -p print pragmas\n\
00285 -s print symbolic byte-code assembler to the standard output\n\
00286 -v print verbose progress messages\n\
00287 \n",
00288 program);
00289 }
00290
00291
00292 static void pragma_meta(const WsUtf8String *property_name,
00293 const WsUtf8String *content,
00294 const WsUtf8String *scheme, void *context)
00295 {
00296 FILE *fp = stdout;
00297 char *what = (char *) context;
00298 char *property_name_l = (char *) ws_utf8_to_latin1(property_name, '?', NULL);
00299 char *content_l = (char *) ws_utf8_to_latin1(content, '?', NULL);
00300 char *scheme_l = (char *) ws_utf8_to_latin1(scheme, '?', NULL);
00301
00302 fprintf(fp, "%s: name=\"%s\", content=\"%s\",",
00303 what,
00304 property_name_l ? property_name_l : "",
00305 content_l ? content_l : "");
00306
00307 if (scheme)
00308 fprintf(fp, ", scheme=\"%s\"",
00309 scheme_l ? scheme_l : "");
00310
00311 fprintf(fp, "\n");
00312
00313 ws_utf8_free_data((unsigned char *) property_name_l);
00314 ws_utf8_free_data((unsigned char *) content_l);
00315 ws_utf8_free_data((unsigned char *) scheme_l);
00316 }
See file LICENSE for details about the license agreement for using,
modifying, copying or deriving work from this software.