File: wmlc.c

package info (click to toggle)
html2wml 0.4.11%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 816 kB
  • sloc: perl: 2,380; ansic: 56; makefile: 34; sh: 4
file content (83 lines) | stat: -rw-r--r-- 1,759 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * wmlc.c - very simple WML compiler based on Kannel Gateway library
 * 
 * Sebastien Aperghis-Tramoni <maddingue@free.fr>
 */

/* ANSI includes */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

/* Kannel Gateway includes */
#include "gwlib/gwlib.h"
#include "gw/wml_compiler.h"

static void usage(void) {
	fputs("usage: wmlc input.wml [output.wmlc]\n", stderr);
	exit(0);
}

static void fatal(const char * message) {
	fputs("wmlc: ", stderr);
	fputs(message, stderr);
	if(errno != 0) {
		fputs(": ", stderr);
		fputs(strerror(errno), stderr);
	}
	fputs("\n", stderr);
	exit(-1);
}

int main(int argc, char ** argv) {
	char * input = NULL;  
	char * output = NULL;  
	FILE * out_fd = NULL;
	Octstr * wml_text = NULL;
	Octstr * charset = NULL;
	Octstr * wml_binary = NULL;
	int err = 0;
	
	if((argc < 2) || (argc > 3)) usage();
	if(strncmp(argv[1], "-h", 2) == 0) usage();
	
	input = argv[1];
	
	if(argc == 2) {
		size_t len = strlen(input);
		output = (char *) gw_malloc((len+2)*sizeof(char));
		if(output == NULL) fatal("malloc failed");
		strncpy(output, input, len);
		output[len] = 'c';
		output[len+1] = '\0';
	}
	
	/* initialize Kannel Gateway */
	gwlib_init();
	
	/* read WML file */
	wml_text = octstr_read_file(input);
	if(wml_text == NULL) fatal("cannot read WML file");
	
	/* compile WML */
	err = wml_compile(wml_text, charset, &wml_binary);
	
	/* write WML binary */
	if(strncmp(output, "-", 1) == 0) {
		out_fd = stdout;
	} else {
		out_fd = fopen(output, "w");
		if(out_fd == NULL) fatal("cannot write WML binary");
	}
	octstr_print(out_fd, wml_binary);
	fclose(out_fd);
	octstr_destroy(wml_binary);
	
	/* cleans up */
	if (charset != NULL) octstr_destroy(charset);
	octstr_destroy(wml_text);
	
	return 0;
}