msgmerge.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* msgfmt utility (C) 2012 rofl0r
  2. * released under the MIT license, see LICENSE for details */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include <assert.h>
  8. #include "poparser.h"
  9. #include "StringEscape.h"
  10. __attribute__((noreturn))
  11. static void syntax(void) {
  12. fprintf(stdout,
  13. "Usage: msgmerge [OPTION] def.po ref.pot\n");
  14. exit(1);
  15. }
  16. __attribute__((noreturn))
  17. static void version(void) {
  18. fprintf(stdout,
  19. "these are not (GNU gettext-tools) 99.9999.9999\n");
  20. exit(0);
  21. }
  22. #define streq(A, B) (!strcmp(A, B))
  23. #define strstarts(S, W) (memcmp(S, W, sizeof(W) - 1) ? NULL : (S + (sizeof(W) - 1)))
  24. struct fiLes {
  25. FILE *out;
  26. /* we can haz 3 different input files:
  27. * the .pot, which is the file containing only the ripped out strings from the program
  28. * (and no translations)
  29. * a .po, which is contains translations and strings made from a previous .pot from that same source file,
  30. * a compendium, which is basically a huge po file containing all sorts of strings (msgid's) and translations (msgstr's)
  31. */
  32. FILE *po;
  33. FILE *pot;
  34. FILE *compend;
  35. };
  36. /* currently we only output input strings as output strings
  37. * i.e. there is no translation lookup at all */
  38. int process_line_callback(struct po_info* info, void* user) {
  39. char convbuf[8192];
  40. FILE* out = (FILE*) user;
  41. size_t l;
  42. if(info->type == pe_msgid) {
  43. l = escape(info->text, convbuf, sizeof(convbuf));
  44. fprintf(out, "msgid \"%s\"\nmsgstr \"%s\"\n", convbuf, convbuf);
  45. }
  46. return 0;
  47. }
  48. int process(struct fiLes *files, int update, int backup) {
  49. (void) update; (void) backup;
  50. struct po_parser pb, *p = &pb;
  51. char line[4096], conv[8192], *lb;
  52. poparser_init(p, conv, sizeof(conv), process_line_callback, files->out);
  53. while((lb = fgets(line, sizeof(line), files->pot))) {
  54. poparser_feed_line(p, lb, sizeof(line) - (size_t)(lb - line));
  55. }
  56. poparser_finish(p);
  57. return 0;
  58. }
  59. void set_file(int out, char* fn, FILE** dest) {
  60. if(streq(fn, "-")) {
  61. *dest = out ? stdout : stdin;
  62. } else {
  63. *dest = fopen(fn, out ? "w" : "r");
  64. }
  65. if(!*dest) {
  66. perror("fopen");
  67. exit(1);
  68. }
  69. }
  70. int getbackuptype(char* str) {
  71. if(!str || !*str || streq(str, "none") || streq(str, "off"))
  72. return 0;
  73. else if(streq(str, "t") || streq(str, "numbered"))
  74. return 1;
  75. else if(streq(str, "nil") || streq(str, "existing"))
  76. return 2;
  77. else if(streq(str, "simple") || streq(str, "never"))
  78. return 3;
  79. else syntax();
  80. }
  81. int main(int argc, char**argv) {
  82. if(argc == 1) syntax();
  83. int arg = 1;
  84. struct expect {
  85. int out;
  86. int po;
  87. int pot;
  88. int compend;
  89. } expect_fn = {
  90. .out = 0,
  91. .po = 1,
  92. .pot = 0,
  93. .compend = 0,
  94. };
  95. struct fiLes files = {0,0,0,0};
  96. char* backup_suffix = getenv("SIMPLE_BACKUP_SUFFIX");
  97. if(!backup_suffix) backup_suffix = "~";
  98. int update = 0;
  99. int backup = getbackuptype(getenv("VERSION_CONTROL"));
  100. char* dest;
  101. set_file(1, "-", &files.out);
  102. #define A argv[arg]
  103. for(; arg < argc; arg++) {
  104. if(A[0] == '-') {
  105. if(A[1] == '-') {
  106. if(
  107. streq(A+2, "strict") ||
  108. streq(A+2, "properties-input") ||
  109. streq(A+2, "properties-output") ||
  110. streq(A+2, "stringtable-input") ||
  111. streq(A+2, "stringtable-output") ||
  112. streq(A+2, "no-fuzzy-matching") ||
  113. streq(A+2, "multi-domain") ||
  114. streq(A+2, "previous") ||
  115. streq(A+2, "escape") ||
  116. streq(A+2, "no-escape") ||
  117. streq(A+2, "force-po") ||
  118. streq(A+2, "indent") ||
  119. streq(A+2, "add-location") ||
  120. streq(A+2, "no-location") ||
  121. streq(A+2, "no-wrap") ||
  122. streq(A+2, "sort-output") ||
  123. streq(A+2, "sort-by-file") ||
  124. strstarts(A+2, "lang=") ||
  125. strstarts(A+2, "color") || // can be --color or --color=xxx
  126. strstarts(A+2, "style=") ||
  127. strstarts(A+2, "width=") ||
  128. streq(A+2, "verbose") ||
  129. streq(A+2, "quiet") ||
  130. streq(A+2, "silent") ) {
  131. } else if(streq(A+2, "version")) {
  132. version();
  133. } else if((dest = strstarts(A+2, "output-file="))) {
  134. set_file(1, dest, &files.out);
  135. } else if((dest = strstarts(A+2, "compendium="))) {
  136. set_file(1, dest, &files.compend);
  137. } else if((dest = strstarts(A+2, "suffix="))) {
  138. backup_suffix = dest;
  139. } else if((dest = strstarts(A+2, "directory="))) {
  140. goto nodir;
  141. } else if((dest = strstarts(A+2, "backup"))) {
  142. if (*dest == '=')
  143. backup = getbackuptype(dest + 1);
  144. else
  145. backup = 0;
  146. } else if(streq(A+2, "update")) {
  147. set_update:
  148. update = 1;
  149. abort();
  150. } else if(streq(A+2, "help")) syntax();
  151. } else if(streq(A + 1, "o")) {
  152. expect_fn.out = 1;
  153. } else if(streq(A + 1, "C")) {
  154. expect_fn.compend = 1;
  155. } else if(streq(A + 1, "U")) {
  156. goto set_update;
  157. } else if(
  158. streq(A+1, "m") ||
  159. streq(A+1, "N") ||
  160. streq(A+1, "P") ||
  161. streq(A+1, "e") ||
  162. streq(A+1, "E") ||
  163. streq(A+1, "i") ||
  164. streq(A+1, "p") ||
  165. streq(A+1, "w") ||
  166. streq(A+1, "s") ||
  167. streq(A+1, "F") ||
  168. streq(A+1, "V") ||
  169. streq(A+1, "q")
  170. ) {
  171. } else if (streq(A+1, "v")) {
  172. version();
  173. } else if (streq(A+1, "D")) {
  174. // no support for -D at this time
  175. nodir:
  176. fprintf(stderr, "EINVAL\n");
  177. exit(1);
  178. } else if (streq(A+1, "h")) syntax();
  179. } else if(expect_fn.out) {
  180. set_file(1, A, &files.out);
  181. expect_fn.out = 0;
  182. } else if(expect_fn.compend) {
  183. set_file(1, A, &files.compend);
  184. expect_fn.compend = 0;
  185. } else if(expect_fn.po) {
  186. set_file(0, A, &files.po);
  187. expect_fn.po = 0;
  188. expect_fn.pot = 1;
  189. } else if(expect_fn.pot) {
  190. set_file(0, A, &files.pot);
  191. expect_fn.pot = 0;
  192. }
  193. }
  194. if(!files.out || !files.po || !files.pot) syntax();
  195. int ret = process(&files, update, backup);
  196. FILE** filearr = (FILE**) &files;
  197. unsigned i;
  198. for (i = 0; i < 4; i++) {
  199. if(filearr[i] != NULL) fflush(filearr[i]);
  200. }
  201. for (i = 0; i < 4; i++) {
  202. if(
  203. filearr[i] != NULL &&
  204. filearr[i] != stdout &&
  205. filearr[i] != stdin
  206. ) fclose(filearr[i]);
  207. }
  208. return ret;
  209. }