| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
#include <errno.h> |
| 12 |
|
| 13 |
|
| 14 |
#include "gwlib/gwlib.h" |
| 15 |
#include "gw/wml_compiler.h" |
| 16 |
|
| 17 |
static void usage(void) { |
| 18 |
fputs("usage: wmlc input.wml [output.wmlc]\n", stderr); |
| 19 |
exit(0); |
| 20 |
} |
| 21 |
|
| 22 |
static void fatal(const char * message) { |
| 23 |
fputs("wmlc: ", stderr); |
| 24 |
fputs(message, stderr); |
| 25 |
if(errno != 0) { |
| 26 |
fputs(": ", stderr); |
| 27 |
fputs(strerror(errno), stderr); |
| 28 |
} |
| 29 |
fputs("\n", stderr); |
| 30 |
exit(-1); |
| 31 |
} |
| 32 |
|
| 33 |
int main(int argc, char ** argv) { |
| 34 |
char * input = NULL; |
| 35 |
char * output = NULL; |
| 36 |
FILE * out_fd = NULL; |
| 37 |
Octstr * wml_text = NULL; |
| 38 |
Octstr * charset = NULL; |
| 39 |
Octstr * wml_binary = NULL; |
| 40 |
int err = 0; |
| 41 |
|
| 42 |
if((argc < 2) || (argc > 3)) usage(); |
| 43 |
if(strncmp(argv[1], "-h", 2) == 0) usage(); |
| 44 |
|
| 45 |
input = argv[1]; |
| 46 |
|
| 47 |
if(argc == 2) { |
| 48 |
size_t len = strlen(input); |
| 49 |
output = (char *) gw_malloc((len+2)*sizeof(char)); |
| 50 |
if(output == NULL) fatal("malloc failed"); |
| 51 |
strncpy(output, input, len); |
| 52 |
output[len] = 'c'; |
| 53 |
output[len+1] = '\0'; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
gwlib_init(); |
| 58 |
|
| 59 |
|
| 60 |
wml_text = octstr_read_file(input); |
| 61 |
if(wml_text == NULL) fatal("cannot read WML file"); |
| 62 |
|
| 63 |
|
| 64 |
err = wml_compile(wml_text, charset, &wml_binary); |
| 65 |
|
| 66 |
|
| 67 |
if(strncmp(output, "-", 1) == 0) { |
| 68 |
out_fd = stdout; |
| 69 |
} else { |
| 70 |
out_fd = fopen(output, "w"); |
| 71 |
if(out_fd == NULL) fatal("cannot write WML binary"); |
| 72 |
} |
| 73 |
octstr_print(out_fd, wml_binary); |
| 74 |
fclose(out_fd); |
| 75 |
octstr_destroy(wml_binary); |
| 76 |
|
| 77 |
|
| 78 |
if (charset != NULL) octstr_destroy(charset); |
| 79 |
octstr_destroy(wml_text); |
| 80 |
|
| 81 |
return 0; |
| 82 |
} |
| 83 |
|