1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin *                                                                      *
3da2e3ebdSchin *               This software is part of the ast package               *
4*b30d1939SAndy Fiddaman *          Copyright (c) 2000-2011 AT&T Intellectual Property          *
5da2e3ebdSchin *                      and is licensed under the                       *
6*b30d1939SAndy Fiddaman *                 Eclipse Public License, Version 1.0                  *
77c2fbfb3SApril Chin *                    by AT&T Intellectual Property                     *
8da2e3ebdSchin *                                                                      *
9da2e3ebdSchin *                A copy of the License is available at                 *
10*b30d1939SAndy Fiddaman *          http://www.eclipse.org/org/documents/epl-v10.html           *
11*b30d1939SAndy Fiddaman *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12da2e3ebdSchin *                                                                      *
13da2e3ebdSchin *              Information and Software Systems Research               *
14da2e3ebdSchin *                            AT&T Research                             *
15da2e3ebdSchin *                           Florham Park NJ                            *
16da2e3ebdSchin *                                                                      *
17da2e3ebdSchin *                 Glenn Fowler <gsf@research.att.com>                  *
18da2e3ebdSchin *                                                                      *
19da2e3ebdSchin ***********************************************************************/
20da2e3ebdSchin #pragma prototyped
21da2e3ebdSchin /*
22da2e3ebdSchin  * Glenn Fowler
23da2e3ebdSchin  * AT&T Research
24da2e3ebdSchin  */
26da2e3ebdSchin static const char usage[] =
27da2e3ebdSchin "[-?\n@(#)$Id: msgget (AT&T Research) 2001-04-21 $\n]"
28da2e3ebdSchin USAGE_LICENSE
29da2e3ebdSchin "[+NAME?msgget - get a message from a message catalog]"
30da2e3ebdSchin "[+DESCRIPTION?\bmsgget\b gets the message corresponding to the parameters."
31da2e3ebdSchin "	If \alocale\a is \b-\b then the current locale is used. \acommand\a"
32da2e3ebdSchin "	may be specified for command specific messages. \acatalog\a specifies"
33da2e3ebdSchin "	the message catalog name. [\aset\a.]]\anumber\a identifies the message"
34da2e3ebdSchin "	by message \anumber\a and an optional message \aset\a; if specified as"
35da2e3ebdSchin "	\b-\b then the message set and number are determined by looking up"
36da2e3ebdSchin "	\atext\a in the corresponding \bC\b locale message catalog.]"
38da2e3ebdSchin "\n"
39da2e3ebdSchin "\nlocale [command:]catalog [set.]number [ text ]\n"
40da2e3ebdSchin "\n"
42da2e3ebdSchin "[+SEE ALSO?\biconv\b(1), \bmsgcc\b(1), \bmsggen\b(1)]"
43da2e3ebdSchin ;
45da2e3ebdSchin #include <ast.h>
46da2e3ebdSchin #include <error.h>
47da2e3ebdSchin #include <mc.h>
49da2e3ebdSchin int
main(int argc,char ** argv)50da2e3ebdSchin main(int argc, char** argv)
51da2e3ebdSchin {
52da2e3ebdSchin 	register Mc_t*	mc;
53da2e3ebdSchin 	register char*	s;
54da2e3ebdSchin 	char*		loc;
55da2e3ebdSchin 	char*		cmd;
56da2e3ebdSchin 	char*		cat;
57da2e3ebdSchin 	char*		msg;
58da2e3ebdSchin 	int		set;
59da2e3ebdSchin 	int		num;
60da2e3ebdSchin 	Sfio_t*		sp;
61da2e3ebdSchin 	char		path[PATH_MAX];
63da2e3ebdSchin 	NoP(argc);
64da2e3ebdSchin 	error_info.id = "msgget";
65da2e3ebdSchin 	for (;;)
66da2e3ebdSchin 	{
67da2e3ebdSchin 		switch (optget(argv, usage))
68da2e3ebdSchin 		{
69da2e3ebdSchin 		case '?':
70da2e3ebdSchin 			error(ERROR_USAGE|4, "%s", opt_info.arg);
71da2e3ebdSchin 			continue;
72da2e3ebdSchin 		case ':':
73da2e3ebdSchin 			error(2, "%s", opt_info.arg);
74da2e3ebdSchin 			continue;
75da2e3ebdSchin 		}
76da2e3ebdSchin 		break;
77da2e3ebdSchin 	}
78da2e3ebdSchin 	argv += opt_info.index;
79da2e3ebdSchin 	if (error_info.errors || !(loc = *argv++) || !(cmd = *argv++) || !(s = *argv++))
80da2e3ebdSchin 		error(ERROR_USAGE|4, "%s", optusage(NiL));
81da2e3ebdSchin 	if (streq(s, "-"))
82da2e3ebdSchin 		set = num = 0;
83da2e3ebdSchin 	else
84da2e3ebdSchin 		mcindex(s, NiL, &set, &num);
85da2e3ebdSchin 	if (!(msg = *argv++))
86da2e3ebdSchin 		msg = "";
87da2e3ebdSchin 	else if (*argv)
88da2e3ebdSchin 		error(ERROR_USAGE|4, "%s", optusage(NiL));
89da2e3ebdSchin 	if (streq(loc, "-"))
90da2e3ebdSchin 		loc = 0;
91da2e3ebdSchin 	if (cat = strchr(cmd, ':'))
92da2e3ebdSchin 		*cat++ = 0;
93*b30d1939SAndy Fiddaman 	if (!mcfind(loc, cmd, LC_MESSAGES, 0, path, sizeof(path)) && (!cat || !mcfind(loc, cat, LC_MESSAGES, 0, path, sizeof(path))))
94da2e3ebdSchin 	{
95da2e3ebdSchin 		if (cat)
96da2e3ebdSchin 			*--cat = ':';
97da2e3ebdSchin 		error(3, "%s: cannot locate message catalog", cmd);
98da2e3ebdSchin 	}
99da2e3ebdSchin 	if (!(sp = sfopen(NiL, path, "r")))
100da2e3ebdSchin 		error(ERROR_SYSTEM|3, "%s: cannot read message catalog", path);
101da2e3ebdSchin 	if (!(mc = mcopen(sp)))
102da2e3ebdSchin 		error(3, "%s: invalid message catalog", path);
103da2e3ebdSchin 	if (set)
104da2e3ebdSchin 		s = mcget(mc, set, num, msg);
105da2e3ebdSchin 	else
106da2e3ebdSchin 		s = errorx(loc, cmd, cat, msg);
107da2e3ebdSchin 	sfputr(sfstdout, s, '\n');
108da2e3ebdSchin 	return error_info.errors != 0;
109da2e3ebdSchin }