(main): Call m17n-view; not mview.
[m17n/m17n-lib.git] / example / mdate.c
1 /* mdate.c -- Show system date/time in all locales.     -*- coding: euc-jp; -*-
2    Copyright (C) 2003, 2004
3      National Institute of Advanced Industrial Science and Technology (AIST)
4      Registration Number H15PRO112
5
6    This file is part of the m17n library.
7
8    The m17n library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Lesser General Public License
10    as published by the Free Software Foundation; either version 2.1 of
11    the License, or (at your option) any later version.
12
13    The m17n library is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    Lesser General Public License for more details.
17
18    You should have received a copy of the GNU Lesser General Public
19    License along with the m17n library; if not, write to the Free
20    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21    02111-1307, USA.  */
22
23 /***en
24     @enpage m17n-date display date and time
25
26     @section m17n-date-synopsis SYNOPSIS
27
28     m17n-date [ OPTION ... ]
29
30     @section m17n-date-description DESCRIPTION
31
32     Display the system date and time in many locales on a window.
33
34     The following OPTIONs are available.
35
36     <ul>
37
38     <li> --version
39
40     Print version number.
41
42     <li> -h, --help
43
44     Print this message.
45     </ul>
46 */
47 /***ja
48     @japage m17n-date Æü»þ¤òɽ¼¨¤¹¤ë
49
50     @section m17n-date-synopsis ¥·¥Î¥×¥·¥¹
51
52     m17n-date [ OPTION ... ]
53
54     @section m17n-date-description ÀâÌÀ
55
56     ¥·¥¹¥Æ¥à¤ÎÆü»þ¤ò¤µ¤Þ¤¶¤Þ¤Ê¥í¥±¡¼¥ë¤Ç¥¦¥£¥ó¥É¥¦¤Ëɽ¼¨¤¹¤ë¡£ 
57
58     °Ê²¼¤Î¥ª¥×¥·¥ç¥ó¤¬ÍøÍѤǤ­¤ë¡£ 
59
60     <ul>
61
62     <li> --version
63
64     ¥Ð¡¼¥¸¥ç¥óÈÖ¹æ¤òɽ¼¨¤¹¤ë¡£ 
65
66     <li> -h, --help
67
68     ¤³¤Î¥á¥Ã¥»¡¼¥¸¤òɽ¼¨¤¹¤ë¡£
69
70     </ul>
71 */
72
73 #ifndef FOR_DOXYGEN
74
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <string.h>
78 #include <time.h>
79 #include <sys/time.h>
80 #include <locale.h>
81
82 #include <m17n.h>
83 #include <m17n-misc.h>
84
85 #define VERSION "1.2.0"
86
87 /* Return a plist of all locales currently avairable on the system.
88    The keys and values of the plist are locale name symbols and
89    pointers to MLocale respectively.  */
90
91 MPlist *
92 list_system_locales ()
93 {
94   FILE *p;
95   char buf[1024];
96   MPlist *plist;
97
98   /* Run the command "locale -a" to get a list of locales.  */
99   if (! (p = popen ("locale -a", "r")))
100     {
101       fprintf (stderr, "Can't run `locale -a'.\n");
102       exit (1);
103     }
104
105   plist = mplist ();
106   /* Read from the pipe one line by one.  */
107   while (fgets (buf, 1024, p))
108     {
109       MLocale *locale;
110       int len = strlen (buf);
111
112       if (buf[len - 1] == '\n')
113         buf[len - 1] = '\0';
114       locale = mlocale_set (LC_TIME, buf);
115
116       /* If the locale is surely usable, it is not duplicated, and we
117          know the corresponding coding system, add it to the list.  */
118       if (locale
119           && ! mplist_get (plist, mlocale_get_prop (locale, Mname))
120           && mlocale_get_prop (locale, Mcoding) != Mnil)
121         mplist_add (plist, mlocale_get_prop (locale, Mname), locale);
122     }
123   pclose (p);
124   return plist;
125 }
126
127
128 /* Print the usage of this program (the name is PROG), and exit with
129    EXIT_CODE.  */
130
131 void
132 help_exit (char *prog, int exit_code)
133 {
134   char *p = prog;
135
136   while (*p)
137     if (*p++ == '/')
138       prog = p;
139
140   printf ("Usage: %s [ OPTION ...]\n", prog);
141   printf ("Display the system date and time in many locales on a window.\n");
142   printf ("The following OPTIONs are available.\n");
143   printf ("  %-13s %s", "--version", "Print version number.\n");
144   printf ("  %-13s %s", "-h, --help", "Print this message.\n");
145   exit (exit_code);
146 }
147
148
149 /* Format MSG by FMT and print the result to the stderr, and exit.  */
150
151 #define FATAL_ERROR(fmt, arg)   \
152   do {                          \
153     fprintf (stderr, fmt, arg); \
154     exit (1);                   \
155   } while (0)
156
157
158 int
159 main (int argc, char **argv)
160 {
161   time_t current_time_t = time (NULL);
162   struct tm *current_time_tm = localtime (&current_time_t);
163   /* List of all locales.  */
164   MPlist *locale_list, *plist;
165   /* Text to be shown.  */
166   MText *mt;
167   int i;
168
169   for (i = 1; i < argc; i++)
170     {
171       if (! strcmp (argv[i], "--help")
172                || ! strcmp (argv[i], "-h")
173                || ! strcmp (argv[i], "-?"))
174         help_exit (argv[0], 0);
175       else if (! strcmp (argv[i], "--version"))
176         {
177           printf ("m17n-date (m17n library) %s\n", VERSION);
178           printf ("Copyright (C) 2003 AIST, JAPAN\n");
179           exit (0);
180         }
181       else
182         help_exit (argv[0], 1);
183     }
184
185
186   /* Initialize the m17n library.  */
187   M17N_INIT ();
188   if (merror_code != MERROR_NONE)
189     FATAL_ERROR ("%s\n", "Fail to initialize the m17n library.");
190
191   /* Get a locale list in LOCALE_LIST, and generate an M-text that
192      contains date string in each locale.  */
193   locale_list = list_system_locales ();
194   mt = mtext ();
195   plist = locale_list;
196   while (mplist_key (plist) != Mnil)
197     {
198       char *name = msymbol_name (mplist_key (plist));
199       char fmtbuf[256];
200       int len;
201       MLocale *locale = mplist_value (plist);
202       MSymbol coding = mlocale_get_prop (locale, Mcoding);
203       /* One line text for this locale.  The format is:
204          "LOCALE-NAME:  DATE-AND-TIME-STRING".  */
205       MText *thisline;
206
207       sprintf (fmtbuf, "%16s: ", name);
208       len = strlen (fmtbuf);
209       thisline = mconv_decode_buffer (coding, (unsigned char *) fmtbuf, len);
210       if (thisline)
211         {
212           mlocale_set (LC_TIME, name);
213           if (mtext_ftime (thisline, "%c", current_time_tm, NULL) > 0)
214             {
215               mtext_cat_char (thisline, '\n');
216               mtext_cat (mt, thisline);
217             }
218           m17n_object_unref (thisline);
219         }
220       plist = mplist_next (plist);
221     }
222
223   /* Show the generated M-text by another example program "mview".  */
224   {
225     FILE *p = popen ("m17n-view", "w");
226
227     if (!p)
228       FATAL_ERROR ("%s\n", "Can't run the program mview!");
229     mconv_encode_stream (Mcoding_utf_8, mt, p);
230     fclose (p);
231   }
232
233   /* Clear away.  */
234   m17n_object_unref (locale_list);
235   m17n_object_unref (mt);
236   M17N_FINI ();
237
238   exit (0);
239 }
240 #endif /* not FOR_DOXYGEN */