From 3444ebc399faaa0c980ca4686909fff91e8aade3 Mon Sep 17 00:00:00 2001 From: Kiyoshi Aman Date: Sun, 2 Jun 2019 01:42:39 -0500 Subject: usr.bin/{gencat,locale}: remove as it will be handled at another level --- usr.bin/gencat/gencat.1 | 233 ------------- usr.bin/gencat/gencat.c | 879 ------------------------------------------------ usr.bin/locale/locale.1 | 113 ------- usr.bin/locale/locale.c | 707 -------------------------------------- 4 files changed, 1932 deletions(-) delete mode 100644 usr.bin/gencat/gencat.1 delete mode 100644 usr.bin/gencat/gencat.c delete mode 100644 usr.bin/locale/locale.1 delete mode 100644 usr.bin/locale/locale.c (limited to 'usr.bin') diff --git a/usr.bin/gencat/gencat.1 b/usr.bin/gencat/gencat.1 deleted file mode 100644 index 6fcc171..0000000 --- a/usr.bin/gencat/gencat.1 +++ /dev/null @@ -1,233 +0,0 @@ -.\" $NetBSD: gencat.1,v 1.14 2018/07/28 08:03:41 wiz Exp $ -.\" -.\" Copyright (c) 2007 The NetBSD Foundation, Inc. -.\" All rights reserved. -.\" -.\" This code is derived from software contributed to The NetBSD Foundation -.\" by Kee Hinckley and Brian Ginsbach. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS -.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS -.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -.\" POSSIBILITY OF SUCH DAMAGE. -.\" -.\" Written by Kee Hinckley -.\" -.Dd December 29, 2011 -.Dt GENCAT 1 -.Os -.Sh NAME -.Nm gencat -.Nd generates a Native Language Support (NLS) message catalog file -.Sh SYNOPSIS -.Nm -.Ar catfile -.Op Ar msgfile|- ... -.Sh DESCRIPTION -The -.Nm -utility generates a formatted message catalog -.Ar catfile -from stdin or one or more message source text files -.Ar msgfile . -The file -.Ar catfile -is created if it does not already exist. -If -.Ar catfile -does exist, its messages are included in the new -.Ar catfile . -The new message text defined in -.Ar msgfile -replaces the old message text currently in -.Ar catfile -when the set and message numbers match. -.Pp -The generated message catalog contains message -strings that will be retrieved using the -.Xr catgets 3 -library call. -These messages are dynamically loaded by the -Native Language Support (NLS) library at run time. -Error messages are grouped into sets, and a program can load a -particular set depending on which type, or language, of messages -is desired. -.Ss Message Text Source File Format -The message text source files are text files in the format described below. -Note that the fields of a message text source line are separated by -space or tab characters. -.\" XXX Required by POSIX; the code must be fixed first. Above line should be -.\" a single space or tab character; -.\" any other space or tab characters are considered to be part of the -.\" field contents. -.Bl -tag -width 3n -.It Li $set Ar n comment -Determines the set identifier to be used for all subsequent messages -until the next -.Li $set -or end-of-file. -The -.Ar n -is the set identifier which is defined as a number in the range -.Bo 1 , -.Dv NL_SETMAX Bc . -Set identifiers within a single source file need not be contiguous. -Any string following the set identifier is treated as a comment. -If no -.Li $set -directive is specified in a message text source file, -all messages will be located in the default message set -.Dv NL_SETD . -.It Li $delset Ar n comment -Removes message set -.Ar n -from the catalog. -The -.Ar n -is a set identifier in the range -.Bo 1 , -.Dv NL_SETMAX Bc . -If a message set was created earlier in the -current file, or in a file previously read by the -.Nm -command, this directive will remove it. -Any string following the set identifier is treated as a comment. -.It Li $ Ar comment -A line beginning with -.Li $ -followed by a space or tab character is treated as a comment. -.It Ar m message-text -A message line consists of a message identifier -.Ar m -in the range -.Bo 1 , -.Dv NL_MSGMAX Bc -and the -.Ar message-text . -The -.Ar message-text -is read until the end of the line or a quote character -(if one is specified). -The -.Ar message-text -is stored in the message catalog with -the set identifier specified by the last -.Li $set -directive, and the message identifier -.Ar m . -If the -.Ar message-text -is empty and there is a space or tab character -following the message identifier, -an empty string is stored in the message catalog. -If no -.Ar message-text -is provided, -and if there is no space or tab character following the message -identifier, -the message with the message identifier -.Ar m -in the current set is removed from the catalog. -Message identifiers need not be contiguous within a single set. -The length of -.Ar message-text -must be in the range -.Bo 0 , -.Dv NL_TEXTMAX Bc . -.It Li $quote Ar c -Sets an optional quote character to be used around the -.Ar message-text . -The quote character -.Ar c -may be any character other than white space. -If this is specified, then messages must begin and end with the -quote character. -.\" XXX Remove next sentence when code is fixed for POSIX conformance. -This is useful when messages must contain leading white space. -.\" XXX Replacement when above is removed. -.\" This is useful to make leading and trailing spaces or empty -.\" messages visible. -By default no quote character is used. -If an empty -.Li $quote -directive is specified, then the current quote character is unset. -.El -.Pp -Empty lines -.\" XXX Remove next line when the code is fixed for POSIX conformance. -and leading blanks -in a message text source file are ignored. -Any line beginning with any character other than those -described above is ignored as a syntax error. -.Pp -Text message strings may contain any characters and -the following special characters and escape sequences. -.Bl -column -offset indent ".Sy carriage return" ".Sy Symbol" ".Sy Sequence" -.It Sy Description Ta Sy Symbol Ta Sy Sequence -.It newline Ta NL(LF) Ta Li \en -.It horizontal tab Ta HT Ta Li \et -.It vertical tab Ta VT Ta Li \ev -.It backspace Ta BS Ta Li \eb -.It carriage return Ta CR Ta Li \er -.It form feed Ta FF Ta Li \ef -.It backslash Ta \e Ta Li \e\e -.It bit pattern Ta ddd Ta Li \eddd -.El -.Pp -A bit pattern, -.Li \eddd , -consists of a backslash followed by -one, two, or three octal digits representing the value of the character. -The current quote character, if defined, may be escaped with a backslash -to generate the quote character. -Any character following the backslash ('\e') other than those specified -is ignored. -.Pp -A backslash at the end of the line continues the message onto the next line. -The following two lines are an example of such a message: -.Pp -.Dl 1 This message continues \e -.D1 on the next line -.Pp -Producing the following message: -.Pp -.Dl 1 This message continues on the next line -.Sh EXIT STATUS -.Ex -std -.Sh SEE ALSO -.Xr catclose 3 , -.Xr catgets 3 , -.Xr catopen 3 , -.Xr nls 7 -.\" XXX Close but not quite; add when code is fixed. -.\".Sh STANDARDS -.\"The -.\".Nm -.\"utility is compliant with the -.\".St -p1003.1-2004 -.\"standard. -.Sh AUTHORS -.An -nosplit -The Native Language Support (NLS) message catalog facility was -contributed by -.An J.T. Conklin -.Aq Mt jtc@NetBSD.org . -This page was originally written by -.An Kee Hinckley -.Aq Mt nazgul@somewhere.com . diff --git a/usr.bin/gencat/gencat.c b/usr.bin/gencat/gencat.c deleted file mode 100644 index 444ce2d..0000000 --- a/usr.bin/gencat/gencat.c +++ /dev/null @@ -1,879 +0,0 @@ -/* $NetBSD: gencat.c,v 1.36 2013/11/27 17:38:11 apb Exp $ */ - -/* - * Copyright (c) 1996 The NetBSD Foundation, Inc. - * All rights reserved. - * - * This code is derived from software contributed to The NetBSD Foundation - * by J.T. Conklin. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS - * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: gencat.c,v 1.36 2013/11/27 17:38:11 apb Exp $"); -#endif - -/*********************************************************** -Copyright 1990, by Alfalfa Software Incorporated, Cambridge, Massachusetts. - - All Rights Reserved - -Permission to use, copy, modify, and distribute this software and its -documentation for any purpose and without fee is hereby granted, -provided that the above copyright notice appear in all copies and that -both that copyright notice and this permission notice appear in -supporting documentation, and that Alfalfa's name not be used in -advertising or publicity pertaining to distribution of the software -without specific, written prior permission. - -ALPHALPHA DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING -ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL -ALPHALPHA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR -ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, -WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, -ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS -SOFTWARE. - -If you make any modifications, bugfixes or other changes to this software -we'd appreciate it if you could send a copy to us so we can keep things -up-to-date. Many thanks. - Kee Hinckley - Alfalfa Software, Inc. - 267 Allston St., #3 - Cambridge, MA 02139 USA - nazgul@alfalfa.com - -******************************************************************/ - -#if HAVE_NBTOOL_CONFIG_H -#include "nbtool_config.h" -#endif - -#define _NLS_PRIVATE - -#include -#include - -#include /* Needed by arpa/inet.h on NetBSD */ -#include /* Needed for htonl() on POSIX systems */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifndef NL_SETMAX -#define NL_SETMAX 255 -#endif -#ifndef NL_MSGMAX -#define NL_MSGMAX 2048 -#endif - -struct _msgT { - long msgId; - char *str; - LIST_ENTRY(_msgT) entries; -}; - -struct _setT { - long setId; - LIST_HEAD(msghead, _msgT) msghead; - LIST_ENTRY(_setT) entries; -}; - -static LIST_HEAD(sethead, _setT) sethead = LIST_HEAD_INITIALIZER(sethead); -static struct _setT *curSet; - -static const char *curfile; -static char *curline = NULL; -static long lineno = 0; - -static char *cskip(char *); -__dead static void error(const char *); -static char *get_line(int); -static char *getmsg(int, char *, char); -static void warning(const char *, const char *); -static char *wskip(char *); -static char *xstrdup(const char *); -static void *xmalloc(size_t); -static void *xrealloc(void *, size_t); - -static void MCParse(int fd); -static void MCReadCat(int fd); -static void MCWriteCat(int fd); -static void MCDelMsg(int msgId); -static void MCAddMsg(int msgId, const char *msg); -static void MCAddSet(int setId); -static void MCDelSet(int setId); -__dead static void usage(void); - -#define CORRUPT "corrupt message catalog" -#define NOMEMORY "out of memory" - -static void -usage(void) -{ - fprintf(stderr, "usage: %s catfile [msgfile|- ...]\n", getprogname()); - exit(1); -} - -int -main(int argc, char *argv[]) -{ - int ofd, ifd; - char *catfile = NULL; - int c; - int updatecat = 0; - - while ((c = getopt(argc, argv, "")) != -1) { - switch (c) { - case '?': - default: - usage(); - /* NOTREACHED */ - } - } - argc -= optind; - argv += optind; - - if (argc < 1) { - usage(); - /* NOTREACHED */ - } - catfile = *argv++; - - if ((catfile[0] == '-') && (catfile[1] == '\0')) { - ofd = STDOUT_FILENO; - } else { - ofd = open(catfile, O_WRONLY | O_CREAT | O_EXCL, 0666); - if (ofd < 0) { - if (errno == EEXIST) { - if ((ofd = open(catfile, O_RDWR)) < 0) { - err(1, "Unable to open %s", catfile); - /* NOTREACHED */ - } - } else { - err(1, "Unable to create new %s", catfile); - /* NOTREACHED */ - } - curfile = catfile; - updatecat = 1; - MCReadCat(ofd); - if (lseek(ofd, (off_t)0, SEEK_SET) == (off_t)-1) { - err(1, "Unable to seek on %s", catfile); - /* NOTREACHED */ - } - } - } - - if (argc < 2 || (((*argv)[0] == '-') && ((*argv)[1] == '\0'))) { - if (argc > 2) - usage(); - /* NOTREACHED */ - MCParse(STDIN_FILENO); - } else { - for (; *argv; argv++) { - if ((ifd = open(*argv, O_RDONLY)) < 0) - err(1, "Unable to read %s", *argv); - curfile = *argv; - lineno = 0; - MCParse(ifd); - close(ifd); - } - } - - if (updatecat) { - if (ftruncate(ofd, 0) != 0) { - err(1, "Unable to truncate %s", catfile); - /* NOTREACHED */ - } - } - - MCWriteCat(ofd); - exit(0); -} - -static void -warning(const char *cptr, const char *msg) -{ - if (lineno) { - fprintf(stderr, "%s: %s on line %ld, %s\n", - getprogname(), msg, lineno, curfile); - fprintf(stderr, "%s\n", curline); - if (cptr) { - char *tptr; - for (tptr = curline; tptr < cptr; ++tptr) - putc(' ', stderr); - fprintf(stderr, "^\n"); - } - } else { - fprintf(stderr, "%s: %s, %s\n", getprogname(), msg, curfile); - } -} - -static void -error(const char *msg) -{ - warning(NULL, msg); - exit(1); -} - -static void * -xmalloc(size_t len) -{ - void *p; - - if ((p = malloc(len)) == NULL) - errx(1, NOMEMORY); - return (p); -} - -static void * -xrealloc(void *ptr, size_t size) -{ - if ((ptr = realloc(ptr, size)) == NULL) - errx(1, NOMEMORY); - return (ptr); -} - -static char * -xstrdup(const char *str) -{ - char *nstr; - - if ((nstr = strdup(str)) == NULL) - errx(1, NOMEMORY); - return (nstr); -} - -static char * -get_line(int fd) -{ - static long curlen = BUFSIZ; - static char buf[BUFSIZ], *bptr = buf, *bend = buf; - char *cptr, *cend; - long buflen; - - if (!curline) { - curline = xmalloc(curlen); - } - ++lineno; - - cptr = curline; - cend = curline + curlen; - for (;;) { - for (; bptr < bend && cptr < cend; ++cptr, ++bptr) { - if (*bptr == '\n') { - *cptr = '\0'; - ++bptr; - return (curline); - } else - *cptr = *bptr; - } - if (cptr == cend) { - cptr = curline = xrealloc(curline, curlen *= 2); - cend = curline + curlen; - } - if (bptr == bend) { - buflen = read(fd, buf, BUFSIZ); - if (buflen <= 0) { - if (cptr > curline) { - *cptr = '\0'; - return (curline); - } - return (NULL); - } - bend = buf + buflen; - bptr = buf; - } - } -} - -static char * -wskip(char *cptr) -{ - if (!*cptr || !isspace((unsigned char) *cptr)) { - warning(cptr, "expected a space"); - return (cptr); - } - while (*cptr && isspace((unsigned char) *cptr)) - ++cptr; - return (cptr); -} - -static char * -cskip(char *cptr) -{ - if (!*cptr || isspace((unsigned char) *cptr)) { - warning(cptr, "wasn't expecting a space"); - return (cptr); - } - while (*cptr && !isspace((unsigned char) *cptr)) - ++cptr; - return (cptr); -} - -static char * -getmsg(int fd, char *cptr, char quote) -{ - static char *msg = NULL; - static size_t msglen = 0; - size_t clen, i; - int in_quote = 0; - char *tptr; - - if (quote && *cptr == quote) { - ++cptr; - in_quote = 1; - } - - clen = strlen(cptr) + 1; - if (clen > msglen) { - if (msglen) - msg = xrealloc(msg, clen); - else - msg = xmalloc(clen); - msglen = clen; - } - tptr = msg; - - while (*cptr) { - if (quote && *cptr == quote) { - char *tmp; - tmp = cptr + 1; - if (!in_quote) { - /* XXX hard error? */ - warning(cptr, "unexpected quote character, ignoring"); - *tptr++ = *cptr++; - } else { - cptr++; - /* don't use wskip() */ - while (*cptr && isspace((unsigned char) *cptr)) -#ifndef _BACKWARDS_COMPAT - cptr++; -#else - *tptr++ = *cptr++; -#endif - /* XXX hard error? */ - if (*cptr) - warning(tmp, "unexpected extra characters, ignoring"); - in_quote = 0; -#ifndef _BACKWARDS_COMPAT - break; -#endif - } - } else { - if (*cptr == '\\') { - ++cptr; - switch (*cptr) { - case '\0': - cptr = get_line(fd); - if (!cptr) - error("premature end of file"); - msglen += strlen(cptr); - i = tptr - msg; - msg = xrealloc(msg, msglen); - tptr = msg + i; - break; - case 'n': - *tptr++ = '\n'; - ++cptr; - break; - case 't': - *tptr++ = '\t'; - ++cptr; - break; - case 'v': - *tptr++ = '\v'; - ++cptr; - break; - case 'b': - *tptr++ = '\b'; - ++cptr; - break; - case 'r': - *tptr++ = '\r'; - ++cptr; - break; - case 'f': - *tptr++ = '\f'; - ++cptr; - break; - case '\\': - *tptr++ = '\\'; - ++cptr; - break; - default: - if (quote && *cptr == quote) { - *tptr++ = *cptr++; - } else if (isdigit((unsigned char) *cptr)) { - *tptr = 0; - for (i = 0; i < 3; ++i) { - if (!isdigit((unsigned char) *cptr)) - break; - if (*cptr > '7') - warning(cptr, "octal number greater than 7?!"); - *tptr *= 8; - *tptr += (*cptr - '0'); - ++cptr; - } - } else { - warning(cptr, "unrecognized escape sequence"); - } - break; - } - } else { - *tptr++ = *cptr++; - } - } - } - - if (in_quote) - warning(cptr, "unterminated quoted message, ignoring"); - - *tptr = '\0'; - return (msg); -} - -static void -MCParse(int fd) -{ - char *cptr, *str; - int msgid = 0; - int setid = 0; - char quote = 0; - - while ((cptr = get_line(fd))) { - if (*cptr == '$') { - ++cptr; - if (strncmp(cptr, "set", 3) == 0) { - cptr += 3; - cptr = wskip(cptr); - setid = atoi(cptr); - MCAddSet(setid); - msgid = 0; - } else if (strncmp(cptr, "delset", 6) == 0) { - cptr += 6; - cptr = wskip(cptr); - setid = atoi(cptr); - MCDelSet(setid); - } else if (strncmp(cptr, "quote", 5) == 0) { - cptr += 5; - if (!*cptr) - quote = 0; - else { - cptr = wskip(cptr); - if (!*cptr) - quote = 0; - else - quote = *cptr; - } - } else if (isspace((unsigned char) *cptr)) { - ; - } else { - if (*cptr) { - cptr = wskip(cptr); - if (*cptr) - warning(cptr, "unrecognized line"); - } - } - } else { - /* - * First check for (and eat) empty lines.... - */ - if (!*cptr) - continue; - /* - * We have a digit? Start of a message. Else, - * syntax error. - */ - if (isdigit((unsigned char) *cptr)) { - msgid = atoi(cptr); - cptr = cskip(cptr); - if (*cptr) { - cptr = wskip(cptr); - if (!*cptr) { - MCAddMsg(msgid, ""); - continue; - } - } - } else { - warning(cptr, "neither blank line nor start of a message id"); - continue; - } - /* - * If no set directive specified, all messages - * shall be in default message set NL_SETD. - */ - if (setid == 0) { - setid = NL_SETD; - MCAddSet(setid); - } - /* - * If we have a message ID, but no message, - * then this means "delete this message id - * from the catalog". - */ - if (!*cptr) { - MCDelMsg(msgid); - } else { - str = getmsg(fd, cptr, quote); - MCAddMsg(msgid, str); - } - } - } -} - -static void -MCReadCat(int fd) -{ - void *msgcat; /* message catalog data */ - struct _nls_cat_hdr cat_hdr; - struct _nls_set_hdr *set_hdr; - struct _nls_msg_hdr *msg_hdr; - char *strings; - ssize_t n; - int m, s; - int msgno, setno; - - n = read(fd, &cat_hdr, sizeof(cat_hdr)); - if (n < (ssize_t)sizeof(cat_hdr)) { - if (n == 0) - return; /* empty file */ - else if (n == -1) - err(1, "header read"); - else - errx(1, CORRUPT); - } - if (ntohl((uint32_t)cat_hdr.__magic) != _NLS_MAGIC) - errx(1, "%s: bad magic number (%#x)", CORRUPT, cat_hdr.__magic); - - cat_hdr.__mem = ntohl(cat_hdr.__mem); - - cat_hdr.__nsets = ntohl(cat_hdr.__nsets); - cat_hdr.__msg_hdr_offset = ntohl(cat_hdr.__msg_hdr_offset); - cat_hdr.__msg_txt_offset = ntohl(cat_hdr.__msg_txt_offset); - if ((cat_hdr.__mem < 0) || - (cat_hdr.__msg_hdr_offset < 0) || - (cat_hdr.__msg_txt_offset < 0) || - (cat_hdr.__mem < (int32_t)(cat_hdr.__nsets * sizeof(struct _nls_set_hdr))) || - (cat_hdr.__mem < cat_hdr.__msg_hdr_offset) || - (cat_hdr.__mem < cat_hdr.__msg_txt_offset)) - errx(1, "%s: catalog header", CORRUPT); - - msgcat = xmalloc(cat_hdr.__mem); - - n = read(fd, msgcat, cat_hdr.__mem); - if (n < cat_hdr.__mem) { - if (n == -1) - err(1, "data read"); - else - errx(1, CORRUPT); - } - - set_hdr = (struct _nls_set_hdr *)msgcat; - msg_hdr = (struct _nls_msg_hdr *)((char *)msgcat + - cat_hdr.__msg_hdr_offset); - strings = (char *)msgcat + cat_hdr.__msg_txt_offset; - - setno = 0; - for (s = 0; s < cat_hdr.__nsets; s++, set_hdr++) { - set_hdr->__setno = ntohl(set_hdr->__setno); - if (set_hdr->__setno < setno) - errx(1, "%s: bad set number (%d)", - CORRUPT, set_hdr->__setno); - setno = set_hdr->__setno; - - MCAddSet(setno); - - set_hdr->__nmsgs = ntohl(set_hdr->__nmsgs); - set_hdr->__index = ntohl(set_hdr->__index); - if (set_hdr->__nmsgs < 0 || set_hdr->__index < 0) - errx(1, "%s: set header", CORRUPT); - - /* Get the data */ - msgno = 0; - for (m = 0; m < set_hdr->__nmsgs; m++, msg_hdr++) { - msg_hdr->__msgno = ntohl(msg_hdr->__msgno); - msg_hdr->__offset = ntohl(msg_hdr->__offset); - if (msg_hdr->__msgno < msgno) - errx(1, "%s: bad message number (%d)", - CORRUPT, msg_hdr->__msgno); - if ((msg_hdr->__offset < 0) || - ((strings + msg_hdr->__offset) > - ((char *)msgcat + cat_hdr.__mem))) - errx(1, "%s: message header", CORRUPT); - - msgno = msg_hdr->__msgno; - MCAddMsg(msgno, strings + msg_hdr->__offset); - } - } - free(msgcat); -} - -/* - * Write message catalog. - * - * The message catalog is first converted from its internal to its - * external representation in a chunk of memory allocated for this - * purpose. Then the completed catalog is written. This approach - * avoids additional housekeeping variables and/or a lot of seeks - * that would otherwise be required. - */ -static void -MCWriteCat(int fd) -{ - int nsets; /* number of sets */ - int nmsgs; /* number of msgs */ - int string_size; /* total size of string pool */ - int msgcat_size; /* total size of message catalog */ - void *msgcat; /* message catalog data */ - struct _nls_cat_hdr *cat_hdr; - struct _nls_set_hdr *set_hdr; - struct _nls_msg_hdr *msg_hdr; - char *strings; - struct _setT *set; - struct _msgT *msg; - int msg_index; - int msg_offset; - - /* determine number of sets, number of messages, and size of the - * string pool */ - nsets = 0; - nmsgs = 0; - string_size = 0; - - LIST_FOREACH(set, &sethead, entries) { - nsets++; - - LIST_FOREACH(msg, &set->msghead, entries) { - nmsgs++; - string_size += strlen(msg->str) + 1; - } - } - -#ifdef DEBUG - printf("number of sets: %d\n", nsets); - printf("number of msgs: %d\n", nmsgs); - printf("string pool size: %d\n", string_size); -#endif - - /* determine size and then allocate buffer for constructing external - * message catalog representation */ - msgcat_size = sizeof(struct _nls_cat_hdr) - + (nsets * sizeof(struct _nls_set_hdr)) - + (nmsgs * sizeof(struct _nls_msg_hdr)) - + string_size; - - msgcat = xmalloc(msgcat_size); - memset(msgcat, '\0', msgcat_size); - - /* fill in msg catalog header */ - cat_hdr = (struct _nls_cat_hdr *) msgcat; - cat_hdr->__magic = htonl(_NLS_MAGIC); - cat_hdr->__nsets = htonl(nsets); - cat_hdr->__mem = htonl(msgcat_size - sizeof(struct _nls_cat_hdr)); - cat_hdr->__msg_hdr_offset = - htonl(nsets * sizeof(struct _nls_set_hdr)); - cat_hdr->__msg_txt_offset = - htonl(nsets * sizeof(struct _nls_set_hdr) + - nmsgs * sizeof(struct _nls_msg_hdr)); - - /* compute offsets for set & msg header tables and string pool */ - set_hdr = (struct _nls_set_hdr *) ((char *) msgcat + - sizeof(struct _nls_cat_hdr)); - msg_hdr = (struct _nls_msg_hdr *) ((char *) msgcat + - sizeof(struct _nls_cat_hdr) + - nsets * sizeof(struct _nls_set_hdr)); - strings = (char *) msgcat + - sizeof(struct _nls_cat_hdr) + - nsets * sizeof(struct _nls_set_hdr) + - nmsgs * sizeof(struct _nls_msg_hdr); - - msg_index = 0; - msg_offset = 0; - LIST_FOREACH(set, &sethead, entries) { - - nmsgs = 0; - LIST_FOREACH(msg, &set->msghead, entries) { - int32_t msg_len = strlen(msg->str) + 1; - - msg_hdr->__msgno = htonl(msg->msgId); - msg_hdr->__msglen = htonl(msg_len); - msg_hdr->__offset = htonl(msg_offset); - - memcpy(strings, msg->str, msg_len); - strings += msg_len; - msg_offset += msg_len; - - nmsgs++; - msg_hdr++; - } - - set_hdr->__setno = htonl(set->setId); - set_hdr->__nmsgs = htonl(nmsgs); - set_hdr->__index = htonl(msg_index); - msg_index += nmsgs; - set_hdr++; - } - - /* write out catalog. XXX: should this be done in small chunks? */ - write(fd, msgcat, msgcat_size); -} - -static void -MCAddSet(int setId) -{ - struct _setT *p, *q; - - if (setId <= 0) { - error("setId's must be greater than zero"); - /* NOTREACHED */ - } - if (setId > NL_SETMAX) { - error("setId exceeds limit"); - /* NOTREACHED */ - } - - p = LIST_FIRST(&sethead); - q = NULL; - for (; p != NULL && p->setId < setId; q = p, p = LIST_NEXT(p, entries)) - continue; - - if (p && p->setId == setId) { - ; - } else { - p = xmalloc(sizeof(struct _setT)); - memset(p, '\0', sizeof(struct _setT)); - LIST_INIT(&p->msghead); - - p->setId = setId; - - if (q == NULL) { - LIST_INSERT_HEAD(&sethead, p, entries); - } else { - LIST_INSERT_AFTER(q, p, entries); - } - } - - curSet = p; -} - -static void -MCAddMsg(int msgId, const char *str) -{ - struct _msgT *p, *q; - - if (!curSet) - error("can't specify a message when no set exists"); - - if (msgId <= 0) { - error("msgId's must be greater than zero"); - /* NOTREACHED */ - } - if (msgId > NL_MSGMAX) { - error("msgID exceeds limit"); - /* NOTREACHED */ - } - - p = LIST_FIRST(&curSet->msghead); - q = NULL; - for (; p != NULL && p->msgId < msgId; q = p, p = LIST_NEXT(p, entries)) - continue; - - if (p && p->msgId == msgId) { - free(p->str); - } else { - p = xmalloc(sizeof(struct _msgT)); - memset(p, '\0', sizeof(struct _msgT)); - - if (q == NULL) { - LIST_INSERT_HEAD(&curSet->msghead, p, entries); - } else { - LIST_INSERT_AFTER(q, p, entries); - } - } - - p->msgId = msgId; - p->str = xstrdup(str); -} - -static void -MCDelSet(int setId) -{ - struct _setT *set; - struct _msgT *msg; - - if (setId <= 0) { - error("setId's must be greater than zero"); - /* NOTREACHED */ - } - if (setId > NL_SETMAX) { - error("setId exceeds limit"); - /* NOTREACHED */ - } - - set = LIST_FIRST(&sethead); - for (; set != NULL && set->setId < setId; set = LIST_NEXT(set, entries)) - continue; - - if (set && set->setId == setId) { - LIST_REMOVE(set, entries); - while ((msg = LIST_FIRST(&set->msghead)) != NULL) { - LIST_REMOVE(msg, entries); - free(msg->str); - free(msg); - } - free(set); - return; - } - warning(NULL, "specified set doesn't exist"); -} - -static void -MCDelMsg(int msgId) -{ - struct _msgT *msg; - - if (!curSet) - error("you can't delete a message before defining the set"); - - msg = LIST_FIRST(&curSet->msghead); - for (; msg != NULL && msg->msgId < msgId; msg = LIST_NEXT(msg, entries)) - continue; - - if (msg && msg->msgId == msgId) { - LIST_REMOVE(msg, entries); - free(msg->str); - free(msg); - return; - } - warning(NULL, "specified msg doesn't exist"); -} diff --git a/usr.bin/locale/locale.1 b/usr.bin/locale/locale.1 deleted file mode 100644 index 5cd4b0e..0000000 --- a/usr.bin/locale/locale.1 +++ /dev/null @@ -1,113 +0,0 @@ -.\" $NetBSD: locale.1,v 1.2 2003/07/04 07:28:14 wiz Exp $ -.\" -.\" Copyright (c) 2003 Alexey Zelkin -.\" All rights reserved. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" FreeBSD: src/usr.bin/locale/locale.1,v 1.3 2003/06/26 11:01:03 phantom Exp -.\" -.Dd July 4, 2003 -.Dt LOCALE 1 -.Os -.Sh NAME -.Nm locale -.Nd get locale-specific information -.Sh SYNOPSIS -.Nm -.Op Fl a | Fl m -.Nm -.Op Fl ck -.Op Ar keyword ... -.Sh DESCRIPTION -The -.Nm -utility is supposed to provide most locale specific information to -the standard output. -.Pp -When -.Nm -is invoked without arguments it will print out a summary of the -current locale environment depending on environment variable settings -and internal status. -.Pp -When -.Nm -is invoked with arguments and no options specified it will print out -.Em keyword Ns No 's -value determined using current locale settings. -.Pp -The following options are available: -.Bl -tag -width indent -.It Fl a -Write names of all available locales. -While looking for locales -.Nm -will respect the -.Ev PATH_LOCALE -environment variable, and use it instead of the system default -locale directory. -.It Fl c -Write the category name for the selected keywords. -.It Fl k -Write the name and value of the selected keywords. -.It Fl m -Write names of all available charmaps. -.El -.Sh IMPLEMENTATION DETAILS -Special -.Pf ( Fx Ns - -/ -.Nx Ns -specific ) -keyword -.Ar list -can be used to retrieve a human readable list of available keywords. -.Sh DIAGNOSTICS -.Ex -std locale -.Sh STANDARDS -.Nm -conforms to -.St -p1003.1-2001 . -.Sh HISTORY -.Nm -first appeared in -.Nx 2.0 . -.Sh AUTHORS -This implementation of -.Nm -was originally written by -.An Alexey Zelkin -.Aq phantom@FreeBSD.org -for -.Fx . -.Sh BUGS -Since -.Nx -does not support -.Em charmap Ns No s -in their -.Em POSIX -meaning -.Nm -emulates the -.Fl m -option via CODESETs listing of all available locales. diff --git a/usr.bin/locale/locale.c b/usr.bin/locale/locale.c deleted file mode 100644 index bd02d53..0000000 --- a/usr.bin/locale/locale.c +++ /dev/null @@ -1,707 +0,0 @@ -/* $NetBSD: locale.c,v 1.8 2012/01/20 16:31:30 joerg Exp $ */ - -/*- - * Copyright (c) 2002, 2003 Alexey Zelkin - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * FreeBSD: src/usr.bin/locale/locale.c,v 1.10 2003/06/26 11:05:56 phantom Exp - */ - -#include -#if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: locale.c,v 1.8 2012/01/20 16:31:30 joerg Exp $"); -#endif /* LIBC_SCCS and not lint */ - -/* - * XXX: implement missing era_* (LC_TIME) keywords (require libc & - * nl_langinfo(3) extensions) - * - * XXX: correctly handle reserved 'charmap' keyword and '-m' option (require - * localedef(1) implementation). Currently it's handled via - * nl_langinfo(CODESET). - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "citrus_namespace.h" -#include "citrus_region.h" -#include "citrus_lookup.h" -#include "setlocale_local.h" - -/* Local prototypes */ -void init_locales_list(void); -void init_locales_list_alias(void); -void list_charmaps(void); -void list_locales(void); -const char *lookup_localecat(int); -char *kwval_lconv(int); -int kwval_lookup(char *, char **, int *, int *); -void showdetails(char *); -void showkeywordslist(void); -void showlocale(void); -void usage(void); - -/* Global variables */ -static StringList *locales = NULL; - -int all_locales = 0; -int all_charmaps = 0; -int prt_categories = 0; -int prt_keywords = 0; -int more_params = 0; - -struct _lcinfo { - const char *name; - int id; -} lcinfo [] = { - { "LC_CTYPE", LC_CTYPE }, - { "LC_COLLATE", LC_COLLATE }, - { "LC_TIME", LC_TIME }, - { "LC_NUMERIC", LC_NUMERIC }, - { "LC_MONETARY", LC_MONETARY }, - { "LC_MESSAGES", LC_MESSAGES } -}; -#define NLCINFO (sizeof(lcinfo)/sizeof(lcinfo[0])) - -/* ids for values not referenced by nl_langinfo() */ -#define KW_ZERO 10000 -#define KW_GROUPING (KW_ZERO+1) -#define KW_INT_CURR_SYMBOL (KW_ZERO+2) -#define KW_CURRENCY_SYMBOL (KW_ZERO+3) -#define KW_MON_DECIMAL_POINT (KW_ZERO+4) -#define KW_MON_THOUSANDS_SEP (KW_ZERO+5) -#define KW_MON_GROUPING (KW_ZERO+6) -#define KW_POSITIVE_SIGN (KW_ZERO+7) -#define KW_NEGATIVE_SIGN (KW_ZERO+8) -#define KW_INT_FRAC_DIGITS (KW_ZERO+9) -#define KW_FRAC_DIGITS (KW_ZERO+10) -#define KW_P_CS_PRECEDES (KW_ZERO+11) -#define KW_P_SEP_BY_SPACE (KW_ZERO+12) -#define KW_N_CS_PRECEDES (KW_ZERO+13) -#define KW_N_SEP_BY_SPACE (KW_ZERO+14) -#define KW_P_SIGN_POSN (KW_ZERO+15) -#define KW_N_SIGN_POSN (KW_ZERO+16) -#define KW_INT_P_CS_PRECEDES (KW_ZERO+17) -#define KW_INT_P_SEP_BY_SPACE (KW_ZERO+18) -#define KW_INT_N_CS_PRECEDES (KW_ZERO+19) -#define KW_INT_N_SEP_BY_SPACE (KW_ZERO+20) -#define KW_INT_P_SIGN_POSN (KW_ZERO+21) -#define KW_INT_N_SIGN_POSN (KW_ZERO+22) - -struct _kwinfo { - const char *name; - int isstr; /* true - string, false - number */ - int catid; /* LC_* */ - int value_ref; - const char *comment; -} kwinfo [] = { - { "charmap", 1, LC_CTYPE, CODESET, "" }, /* hack */ - - { "decimal_point", 1, LC_NUMERIC, RADIXCHAR, "" }, - { "thousands_sep", 1, LC_NUMERIC, THOUSEP, "" }, - { "grouping", 1, LC_NUMERIC, KW_GROUPING, "" }, - { "radixchar", 1, LC_NUMERIC, RADIXCHAR, - "Same as decimal_point (BSD only)" }, /* compat */ - { "thousep", 1, LC_NUMERIC, THOUSEP, - "Same as thousands_sep (BSD only)" }, /* compat */ - - { "int_curr_symbol", 1, LC_MONETARY, KW_INT_CURR_SYMBOL, "" }, - { "currency_symbol", 1, LC_MONETARY, KW_CURRENCY_SYMBOL, "" }, - { "mon_decimal_point", 1, LC_MONETARY, KW_MON_DECIMAL_POINT, "" }, - { "mon_thousands_sep", 1, LC_MONETARY, KW_MON_THOUSANDS_SEP, "" }, - { "mon_grouping", 1, LC_MONETARY, KW_MON_GROUPING, "" }, - { "positive_sign", 1, LC_MONETARY, KW_POSITIVE_SIGN, "" }, - { "negative_sign", 1, LC_MONETARY, KW_NEGATIVE_SIGN, "" }, - - { "int_frac_digits", 0, LC_MONETARY, KW_INT_FRAC_DIGITS, "" }, - { "frac_digits", 0, LC_MONETARY, KW_FRAC_DIGITS, "" }, - { "p_cs_precedes", 0, LC_MONETARY, KW_P_CS_PRECEDES, "" }, - { "p_sep_by_space", 0, LC_MONETARY, KW_P_SEP_BY_SPACE, "" }, - { "n_cs_precedes", 0, LC_MONETARY, KW_N_CS_PRECEDES, "" }, - { "n_sep_by_space", 0, LC_MONETARY, KW_N_SEP_BY_SPACE, "" }, - { "p_sign_posn", 0, LC_MONETARY, KW_P_SIGN_POSN, "" }, - { "n_sign_posn", 0, LC_MONETARY, KW_N_SIGN_POSN, "" }, - { "int_p_cs_precedes", 0, LC_MONETARY, KW_INT_P_CS_PRECEDES, "" }, - { "int_p_sep_by_space", 0, LC_MONETARY, KW_INT_P_SEP_BY_SPACE, "" }, - { "int_n_cs_precedes", 0, LC_MONETARY, KW_INT_N_CS_PRECEDES, "" }, - { "int_n_sep_by_space", 0, LC_MONETARY, KW_INT_N_SEP_BY_SPACE, "" }, - { "int_p_sign_posn", 0, LC_MONETARY, KW_INT_P_SIGN_POSN, "" }, - { "int_n_sign_posn", 0, LC_MONETARY, KW_INT_N_SIGN_POSN, "" }, - - { "d_t_fmt", 1, LC_TIME, D_T_FMT, "" }, - { "d_fmt", 1, LC_TIME, D_FMT, "" }, - { "t_fmt", 1, LC_TIME, T_FMT, "" }, - { "am_str", 1, LC_TIME, AM_STR, "" }, - { "pm_str", 1, LC_TIME, PM_STR, "" }, - { "t_fmt_ampm", 1, LC_TIME, T_FMT_AMPM, "" }, - { "day_1", 1, LC_TIME, DAY_1, "" }, - { "day_2", 1, LC_TIME, DAY_2, "" }, - { "day_3", 1, LC_TIME, DAY_3, "" }, - { "day_4", 1, LC_TIME, DAY_4, "" }, - { "day_5", 1, LC_TIME, DAY_5, "" }, - { "day_6", 1, LC_TIME, DAY_6, "" }, - { "day_7", 1, LC_TIME, DAY_7, "" }, - { "abday_1", 1, LC_TIME, ABDAY_1, "" }, - { "abday_2", 1, LC_TIME, ABDAY_2, "" }, - { "abday_3", 1, LC_TIME, ABDAY_3, "" }, - { "abday_4", 1, LC_TIME, ABDAY_4, "" }, - { "abday_5", 1, LC_TIME, ABDAY_5, "" }, - { "abday_6", 1, LC_TIME, ABDAY_6, "" }, - { "abday_7", 1, LC_TIME, ABDAY_7, "" }, - { "mon_1", 1, LC_TIME, MON_1, "" }, - { "mon_2", 1, LC_TIME, MON_2, "" }, - { "mon_3", 1, LC_TIME, MON_3, "" }, - { "mon_4", 1, LC_TIME, MON_4, "" }, - { "mon_5", 1, LC_TIME, MON_5, "" }, - { "mon_6", 1, LC_TIME, MON_6, "" }, - { "mon_7", 1, LC_TIME, MON_7, "" }, - { "mon_8", 1, LC_TIME, MON_8, "" }, - { "mon_9", 1, LC_TIME, MON_9, "" }, - { "mon_10", 1, LC_TIME, MON_10, "" }, - { "mon_11", 1, LC_TIME, MON_11, "" }, - { "mon_12", 1, LC_TIME, MON_12, "" }, - { "abmon_1", 1, LC_TIME, ABMON_1, "" }, - { "abmon_2", 1, LC_TIME, ABMON_2, "" }, - { "abmon_3", 1, LC_TIME, ABMON_3, "" }, - { "abmon_4", 1, LC_TIME, ABMON_4, "" }, - { "abmon_5", 1, LC_TIME, ABMON_5, "" }, - { "abmon_6", 1, LC_TIME, ABMON_6, "" }, - { "abmon_7", 1, LC_TIME, ABMON_7, "" }, - { "abmon_8", 1, LC_TIME, ABMON_8, "" }, - { "abmon_9", 1, LC_TIME, ABMON_9, "" }, - { "abmon_10", 1, LC_TIME, ABMON_10, "" }, - { "abmon_11", 1, LC_TIME, ABMON_11, "" }, - { "abmon_12", 1, LC_TIME, ABMON_12, "" }, - { "era", 1, LC_TIME, ERA, "(unavailable)" }, - { "era_d_fmt", 1, LC_TIME, ERA_D_FMT, "(unavailable)" }, - { "era_d_t_fmt", 1, LC_TIME, ERA_D_T_FMT, "(unavailable)" }, - { "era_t_fmt", 1, LC_TIME, ERA_T_FMT, "(unavailable)" }, - { "alt_digits", 1, LC_TIME, ALT_DIGITS, "" }, - - { "yesexpr", 1, LC_MESSAGES, YESEXPR, "" }, - { "noexpr", 1, LC_MESSAGES, NOEXPR, "" }, - { "yesstr", 1, LC_MESSAGES, YESSTR, - "(POSIX legacy)" }, /* compat */ - { "nostr", 1, LC_MESSAGES, NOSTR, - "(POSIX legacy)" } /* compat */ - -}; -#define NKWINFO (sizeof(kwinfo)/sizeof(kwinfo[0])) - -int -main(int argc, char *argv[]) -{ - int ch; - int tmp; - - while ((ch = getopt(argc, argv, "ackm")) != -1) { - switch (ch) { - case 'a': - all_locales = 1; - break; - case 'c': - prt_categories = 1; - break; - case 'k': - prt_keywords = 1; - break; - case 'm': - all_charmaps = 1; - break; - default: - usage(); - } - } - argc -= optind; - argv += optind; - - /* validate arguments */ - if (all_locales && all_charmaps) - usage(); - if ((all_locales || all_charmaps) && argc > 0) - usage(); - if ((all_locales || all_charmaps) && (prt_categories || prt_keywords)) - usage(); - if ((prt_categories || prt_keywords) && argc <= 0) - usage(); - - /* process '-a' */ - if (all_locales) { - list_locales(); - exit(0); - } - - /* process '-m' */ - if (all_charmaps) { - list_charmaps(); - exit(0); - } - - /* check for special case '-k list' */ - tmp = 0; - if (prt_keywords && argc > 0) - while (tmp < argc) - if (strcasecmp(argv[tmp++], "list") == 0) { - showkeywordslist(); - exit(0); - } - - /* process '-c' and/or '-k' */ - if (prt_categories || prt_keywords || argc > 0) { - setlocale(LC_ALL, ""); - while (argc > 0) { - showdetails(*argv); - argv++; - argc--; - } - exit(0); - } - - /* no arguments, show current locale state */ - showlocale(); - - return (0); -} - -void -usage(void) -{ - printf("usage: locale [ -a | -m ]\n" - " locale [ -ck ] name ...\n"); - exit(1); -} - -/* - * Output information about all available locales - * - * XXX actually output of this function does not guarantee that locale - * is really available to application, since it can be broken or - * inconsistent thus setlocale() will fail. Maybe add '-V' function to - * also validate these locales? - */ -void -list_locales(void) -{ - size_t i; - - init_locales_list(); - for (i = 0; i < locales->sl_cur; i++) { - printf("%s\n", locales->sl_str[i]); - } -} - -/* - * qsort() helper function - */ -static int -scmp(const void *s1, const void *s2) -{ - return strcmp(*(const char **)s1, *(const char **)s2); -} - -/* - * Output information about all available charmaps - * - * XXX this function is doing a task in hackish way, i.e. by scaning - * list of locales, spliting their codeset part and building list of - * them. - */ -void -list_charmaps(void) -{ - size_t i; - char *s, *cs; - StringList *charmaps; - - /* initialize StringList */ - charmaps = sl_init(); - if (charmaps == NULL) - err(1, "could not allocate memory"); - - /* fetch locales list */ - init_locales_list(); - - /* split codesets and build their list */ - for (i = 0; i < locales->sl_cur; i++) { - s = locales->sl_str[i]; - if ((cs = strchr(s, '.')) != NULL) { - cs++; - if (sl_find(charmaps, cs) == NULL) - sl_add(charmaps, cs); - } - } - - /* add US-ASCII, if not yet added */ - if (sl_find(charmaps, "US-ASCII") == NULL) - sl_add(charmaps, "US-ASCII"); - - /* sort the list */ - qsort(charmaps->sl_str, charmaps->sl_cur, sizeof(char *), scmp); - - /* print results */ - for (i = 0; i < charmaps->sl_cur; i++) { - printf("%s\n", charmaps->sl_str[i]); - } -} - -/* - * Retrieve sorted list of system locales (or user locales, if PATH_LOCALE - * environment variable is set) - */ -void -init_locales_list(void) -{ - DIR *dirp; - struct dirent *dp; - char *s; - - /* why call this function twice ? */ - if (locales != NULL) - return; - - /* initialize StringList */ - locales = sl_init(); - if (locales == NULL) - err(1, "could not allocate memory"); - - /* get actual locales directory name */ - setlocale(LC_CTYPE, "C"); - if (_PathLocale == NULL) - errx(1, "unable to find locales storage"); - - /* open locales directory */ - dirp = opendir(_PathLocale); - if (dirp == NULL) - err(1, "could not open directory '%s'", _PathLocale); - - /* scan directory and store its contents except "." and ".." */ - while ((dp = readdir(dirp)) != NULL) { - /* exclude "." and "..", _LOCALE_ALIAS_NAME */ - if ((dp->d_name[0] != '.' || (dp->d_name[1] != '\0' && - (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) && - strcmp(_LOCALE_ALIAS_NAME, dp->d_name) != 0) { - s = strdup(dp->d_name); - if (s == NULL) - err(1, "could not allocate memory"); - sl_add(locales, s); - } - } - closedir(dirp); - - /* make sure that 'POSIX' and 'C' locales are present in the list. - * POSIX 1003.1-2001 requires presence of 'POSIX' name only here, but - * we also list 'C' for constistency - */ - if (sl_find(locales, "POSIX") == NULL) - sl_add(locales, "POSIX"); - - if (sl_find(locales, "C") == NULL) - sl_add(locales, "C"); - - init_locales_list_alias(); - - /* make output nicer, sort the list */ - qsort(locales->sl_str, locales->sl_cur, sizeof(char *), scmp); -} - -void -init_locales_list_alias(void) -{ - char aliaspath[PATH_MAX]; - struct _lookup *hlookup; - struct _region key, dat; - size_t n; - char *s, *t; - - _DIAGASSERT(locales != NULL); - _DIAGASSERT(_PathLocale != NULL); - - (void)snprintf(aliaspath, sizeof(aliaspath), - "%s/" _LOCALE_ALIAS_NAME, _PathLocale); - - if (_lookup_seq_open(&hlookup, aliaspath, - _LOOKUP_CASE_SENSITIVE) == 0) { - while (_lookup_seq_next(hlookup, &key, &dat) == 0) { - n = _region_size((const struct _region *)&key); - s = _region_head((const struct _region *)&key); - for (t = s; n > 0 && *s!= '/'; --n, ++s); - n = (size_t)(s - t); - s = malloc(n + 1); - if (s == NULL) - err(1, "could not allocate memory"); - memcpy(s, t, n); - s[n] = '\0'; - if (sl_find(locales, s) == NULL) - sl_add(locales, s); - else - free(s); - } - _lookup_seq_close(hlookup); - } -} - -/* - * Show current locale status, depending on environment variables - */ -void -showlocale(void) -{ - size_t i; - const char *lang, *vval, *eval; - - setlocale(LC_ALL, ""); - - lang = getenv("LANG"); - if (lang == NULL) { - lang = ""; - } - printf("LANG=\"%s\"\n", lang); - /* XXX: if LANG is null, then set it to "C" to get implied values? */ - - for (i = 0; i < NLCINFO; i++) { - vval = setlocale(lcinfo[i].id, NULL); - eval = getenv(lcinfo[i].name); - if (eval != NULL && !strcmp(eval, vval) - && strcmp(lang, vval)) { - /* - * Appropriate environment variable set, its value - * is valid and not overriden by LC_ALL - * - * XXX: possible side effect: if both LANG and - * overriden environment variable are set into same - * value, then it'll be assumed as 'implied' - */ - printf("%s=\"%s\"\n", lcinfo[i].name, vval); - } else { - printf("%s=\"%s\"\n", lcinfo[i].name, vval); - } - } - - vval = getenv("LC_ALL"); - if (vval == NULL) { - vval = ""; - } - printf("LC_ALL=\"%s\"\n", vval); -} - -/* - * keyword value lookup helper (via localeconv()) - */ -char * -kwval_lconv(int id) -{ - struct lconv *lc; - char *rval; - - rval = NULL; - lc = localeconv(); - switch (id) { - case KW_GROUPING: - rval = lc->grouping; - break; - case KW_INT_CURR_SYMBOL: - rval = lc->int_curr_symbol; - break; - case KW_CURRENCY_SYMBOL: - rval = lc->currency_symbol; - break; - case KW_MON_DECIMAL_POINT: - rval = lc->mon_decimal_point; - break; - case KW_MON_THOUSANDS_SEP: - rval = lc->mon_thousands_sep; - break; - case KW_MON_GROUPING: - rval = lc->mon_grouping; - break; - case KW_POSITIVE_SIGN: - rval = lc->positive_sign; - break; - case KW_NEGATIVE_SIGN: - rval = lc->negative_sign; - break; - case KW_INT_FRAC_DIGITS: - rval = &(lc->int_frac_digits); - break; - case KW_FRAC_DIGITS: - rval = &(lc->frac_digits); - break; - case KW_P_CS_PRECEDES: - rval = &(lc->p_cs_precedes); - break; - case KW_P_SEP_BY_SPACE: - rval = &(lc->p_sep_by_space); - break; - case KW_N_CS_PRECEDES: - rval = &(lc->n_cs_precedes); - break; - case KW_N_SEP_BY_SPACE: - rval = &(lc->n_sep_by_space); - break; - case KW_P_SIGN_POSN: - rval = &(lc->p_sign_posn); - break; - case KW_N_SIGN_POSN: - rval = &(lc->n_sign_posn); - break; - case KW_INT_P_CS_PRECEDES: - rval = &(lc->int_p_cs_precedes); - break; - case KW_INT_P_SEP_BY_SPACE: - rval = &(lc->int_p_sep_by_space); - break; - case KW_INT_N_CS_PRECEDES: - rval = &(lc->int_n_cs_precedes); - break; - case KW_INT_N_SEP_BY_SPACE: - rval = &(lc->int_n_sep_by_space); - break; - case KW_INT_P_SIGN_POSN: - rval = &(lc->int_p_sign_posn); - break; - case KW_INT_N_SIGN_POSN: - rval = &(lc->int_n_sign_posn); - break; - default: - break; - } - return (rval); -} - -/* - * keyword value and properties lookup - */ -int -kwval_lookup(char *kwname, char **kwval, int *cat, int *isstr) -{ - int rval; - size_t i; - - rval = 0; - for (i = 0; i < NKWINFO; i++) { - if (strcasecmp(kwname, kwinfo[i].name) == 0) { - rval = 1; - *cat = kwinfo[i].catid; - *isstr = kwinfo[i].isstr; - if (kwinfo[i].value_ref < KW_ZERO) { - *kwval = nl_langinfo(kwinfo[i].value_ref); - } else { - *kwval = kwval_lconv(kwinfo[i].value_ref); - } - break; - } - } - - return (rval); -} - -/* - * Show details about requested keyword according to '-k' and/or '-c' - * command line options specified. - */ -void -showdetails(char *kw) -{ - int isstr, cat, tmpval; - char *kwval; - - if (kwval_lookup(kw, &kwval, &cat, &isstr) == 0) { - /* - * invalid keyword specified. - * XXX: any actions? - */ - return; - } - - if (prt_categories) { - printf("%s\n", lookup_localecat(cat)); - } - - if (prt_keywords) { - if (isstr) { - printf("%s=\"%s\"\n", kw, kwval); - } else { - tmpval = (char) *kwval; - printf("%s=%d\n", kw, tmpval); - } - } - - if (!prt_categories && !prt_keywords) { - if (isstr) { - printf("%s\n", kwval); - } else { - tmpval = (char) *kwval; - printf("%d\n", tmpval); - } - } -} - -/* - * Convert locale category id into string - */ -const char * -lookup_localecat(int cat) -{ - size_t i; - - for (i = 0; i < NLCINFO; i++) - if (lcinfo[i].id == cat) { - return (lcinfo[i].name); - } - return ("UNKNOWN"); -} - -/* - * Show list of keywords - */ -void -showkeywordslist(void) -{ - size_t i; - -#define FMT "%-20s %-12s %-7s %-20s\n" - - printf("List of available keywords\n\n"); - printf(FMT, "Keyword", "Category", "Type", "Comment"); - printf("-------------------- ------------ ------- --------------------\n"); - for (i = 0; i < NKWINFO; i++) { - printf(FMT, - kwinfo[i].name, - lookup_localecat(kwinfo[i].catid), - (kwinfo[i].isstr == 0) ? "number" : "string", - kwinfo[i].comment); - } -} -- cgit v1.2.3-60-g2f50