tools/elf4rom/libs/libelf-0.8.10/po/gmo2msg.c
changeset 34 92d87f2e53c2
equal deleted inserted replaced
33:1af5c1be89f8 34:92d87f2e53c2
       
     1 /*
       
     2  * gmo2msg.c - create X/Open message source file for libelf.
       
     3  * Copyright (C) 1996 - 2005 Michael Riepe
       
     4  *
       
     5  * This library is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU Library General Public
       
     7  * License as published by the Free Software Foundation; either
       
     8  * version 2 of the License, or (at your option) any later version.
       
     9  *
       
    10  * This library is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13  * Library General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU Library General Public
       
    16  * License along with this library; if not, write to the Free Software
       
    17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    18  */
       
    19 
       
    20 #ifndef lint
       
    21 static const char rcsid[] = "@(#) $Id: gmo2msg.c,v 1.10 2005/05/21 15:39:28 michael Exp $";
       
    22 #endif /* lint */
       
    23 
       
    24 #include <stdlib.h>
       
    25 #include <string.h>
       
    26 #include <unistd.h>
       
    27 #include <stdio.h>
       
    28 #include <libintl.h>
       
    29 
       
    30 #define DOMAIN	"libelf"
       
    31 
       
    32 static const char *msgs[] = {
       
    33 #define __err__(a,b)	b,
       
    34 #include <errors.h>
       
    35 #undef __err__
       
    36 };
       
    37 
       
    38 int
       
    39 main(int argc, char **argv) {
       
    40     char buf[1024], *lang, *progname, *s;
       
    41     unsigned i;
       
    42     FILE *fp;
       
    43 
       
    44     setlocale(LC_ALL, "");
       
    45 
       
    46     if (*argv && (progname = strrchr(argv[0], '/'))) {
       
    47 	progname++;
       
    48     }
       
    49     else if (!(progname = *argv)) {
       
    50 	progname = "gmo2msg";
       
    51     }
       
    52 
       
    53     if (argc <= 1 || !(lang = argv[1])) {
       
    54 	fprintf(stderr, "Usage: gmo2msg <language>\n");
       
    55 	exit(1);
       
    56     }
       
    57 
       
    58     /*
       
    59      * Fool gettext...
       
    60      */
       
    61     unlink(DOMAIN ".mo");
       
    62     unlink("LC_MESSAGES");
       
    63     unlink(lang);
       
    64     sprintf(buf, "%s.gmo", lang);
       
    65     if (link(buf, DOMAIN ".mo") == -1) {
       
    66 	fprintf(stderr, "Cannot link %s to " DOMAIN ".mo\n", buf);
       
    67 	perror("");
       
    68 	exit(1);
       
    69     }
       
    70     symlink(".", "LC_MESSAGES");
       
    71     symlink(".", lang);
       
    72     textdomain(DOMAIN);
       
    73     getcwd(buf, sizeof(buf));
       
    74     bindtextdomain(DOMAIN, buf);
       
    75 
       
    76     sprintf(buf, "%s.msg", lang);
       
    77     unlink(buf);
       
    78     if (!(fp = fopen(buf, "w"))) {
       
    79 	perror(buf);
       
    80 	exit(1);
       
    81     }
       
    82 
       
    83     fprintf(fp, "$set 1 Automatically created from %s.gmo by %s\n", lang, progname);
       
    84 
       
    85     /*
       
    86      * Translate messages.
       
    87      */
       
    88     setlocale(LC_MESSAGES, lang);
       
    89     if ((s = gettext("")) && (s = strdup(s))) {
       
    90 	s = strtok(s, "\n");
       
    91 	while (s) {
       
    92 	    fprintf(fp, "$ %s\n", s);
       
    93 	    s = strtok(NULL, "\n");
       
    94 	}
       
    95     }
       
    96     /*
       
    97      * Assume that messages contain printable ASCII characters ONLY.
       
    98      * That means no tabs, linefeeds etc.
       
    99      */
       
   100     for (i = 0; i < sizeof(msgs)/sizeof(*msgs); i++) {
       
   101 	s = gettext(msgs[i]);
       
   102 	if (s != msgs[i] && strcmp(s, msgs[i]) != 0) {
       
   103 	    fprintf(fp, "$ \n$ Original message: %s\n", msgs[i]);
       
   104 	    fprintf(fp, "%u %s\n", i + 1, s);
       
   105 	}
       
   106     }
       
   107     setlocale(LC_MESSAGES, "");
       
   108 
       
   109     if (fclose(fp)) {
       
   110 	perror("writing output file");
       
   111 	exit(1);
       
   112     }
       
   113 
       
   114     /*
       
   115      * Cleanup.
       
   116      */
       
   117     unlink(DOMAIN ".mo");
       
   118     unlink("LC_MESSAGES");
       
   119     unlink(lang);
       
   120     exit(0);
       
   121 }