summaryrefslogtreecommitdiffstats
path: root/pomatic.x
blob: 7293eacdc46a75ae02099e787de4786b2fc687d4 (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
%{

#include <sys/time.h>
#include <sys/wait.h>
#include <errno.h>
#include <pwd.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

char message[65536];
enum {id, str} string = id, mode = id;
char *msgid = NULL, *msgstr = NULL;
char *filter = "cat";

%}

%%

[ \t]+ {};
^#.* {};
^msgid {
	struct passwd *pwd;
	time_t now;
	struct tm tm;
	if(string == str) {
		if(msgid) {
			printf("msgid %s", msgid);
		} else {
			printf("msgid \"\"\n");
		}
		if(strcmp(msgid, "\"\"\n") == 0) {
			pwd = getpwuid(getuid());
			now = time(NULL);
			tm = *(gmtime(&now));
			if(pwd) {
				if(strchr(pwd->pw_gecos, ',')) {
					char *p = strchr(pwd->pw_gecos, ',');
					*p = '\0';
				}
			}
			printf("msgstr \"\"\n");
			printf("\"Project-Id-Version: PACKAGE\\n\"\n");
			printf("\"POT-Creation-Date: %04d-%02d-%02d %02d:%02d-0000\\n\"\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min);
			printf("\"PO-Revision-Date: %04d-%02d-%02d %02d:%02d-0000\\n\"\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min);
			printf("\"Last-Translator: %s\\n\"\n", pwd ? pwd->pw_gecos : "the unknown translator");
			printf("\"Language-Team: en_US.%s\\n\"\n", filter);
			printf("\"MIME-Version: 1.0\\n\"\n");
			printf("\"Content-Type: text/plain; charset=ISO-8859-1\\n\"\n");
			printf("\"Content-Transfer-Encoding: 8bit\\n\"\n\n");
		} else
		if ( ((mode == id) && msgid) || ((mode == str) && msgstr) ) {
			int i, ipipe[2], opipe[2];
			pid_t childpid;
			FILE *msgpipe;
			char *text = (mode == id) ? msgid : msgstr;

			pipe(ipipe);
			pipe(opipe);
			childpid = fork();

			if(childpid == 0) {
				/* child */
				dup2(ipipe[0], STDIN_FILENO);
				close(ipipe[1]);
				close(opipe[0]);
				dup2(opipe[1], STDOUT_FILENO);
				exit(execlp(filter, filter, NULL));
			}

			close(ipipe[0]);
			close(opipe[1]);

			write(ipipe[1], text, strlen(text));
			close(ipipe[1]);
			msgpipe = fdopen(opipe[0], "r");

			printf("msgstr \"\"\n");
			while(fgets(message, sizeof(message), msgpipe)) {
				for(i = 0; message[i]; i++) {
					if(i > 0)
					if(message[i] == '"')
					if(message[i - 1] != '\\')
					if(message[i - 1] != '\n')
					if(message[i + 1] != '\n')
						fputc('\\', stdout);
					fputc(message[i], stdout);
				}
			}
			waitpid(childpid, NULL, 0);
			fclose(msgpipe);
		}

		if(msgid)
			free(msgid);
		msgid = NULL;

		if(msgstr)
			free(msgstr);
		msgstr = NULL;
	}
	string = id;
};
^msgstr {
	string = str;
};
\".*\"\n {
	char *tmp;
	char **target = NULL;
	if(string == str) {
		target = &msgstr;
	} else {
		target = &msgid;
	}
	tmp = malloc((*target ? strlen(*target) : 0) + strlen(yytext) + 1);
	strcpy(tmp, *target ? *target : "");
	strcat(tmp, yytext);
	free(*target);
	*target = tmp;
};

%%

int
main(int argc, char **argv)
{
	int base = 0;
	yyin = stdin;
	if(argc == 1) {
		printf("Usage: %s [-s] [potfile] [filter]\n",
		       strrchr(argv[0], '/') ?
		       strrchr(argv[0], '/') + 1 :
		       argv[0]);
		return 1;
	}
	if(argc > 1) {
		if(strcmp(argv[1], "-s") == 0) {
			mode = str;
			base++;
		}
	}
	if(argc > base + 1) {
		yyin = fopen(argv[base + 1], "r");
	}
	if(argc > base + 2) {
		filter = argv[base + 2];
	}
	
	while(yyin && !feof(yyin))
		yylex();

	return 0;
}