sh: Track if the current locale's charset is UTF-8 or not.

This commit is contained in:
Jilles Tjoelker 2011-05-06 22:31:27 +00:00
parent 4f58a95ce1
commit 6ed74a0a1c
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=221559
3 changed files with 24 additions and 0 deletions

View File

@ -76,6 +76,7 @@ __FBSDID("$FreeBSD$");
int rootpid;
int rootshell;
struct jmploc main_handler;
int localeisutf8;
static void read_profile(const char *);
static char *find_dot_file(char *);
@ -96,6 +97,7 @@ main(int argc, char *argv[])
char *shinit;
(void) setlocale(LC_ALL, "");
updatecharset();
state = 0;
if (setjmp(main_handler.loc)) {
switch (exception) {

View File

@ -47,6 +47,7 @@ __FBSDID("$FreeBSD$");
*/
#include <locale.h>
#include <langinfo.h>
#include "shell.h"
#include "output.h"
@ -361,6 +362,7 @@ setvareq(char *s, int flags)
if ((vp->flags & VEXPORT) && localevar(s)) {
change_env(s, 1);
(void) setlocale(LC_ALL, "");
updatecharset();
}
INTON;
return;
@ -379,6 +381,7 @@ setvareq(char *s, int flags)
if ((vp->flags & VEXPORT) && localevar(s)) {
change_env(s, 1);
(void) setlocale(LC_ALL, "");
updatecharset();
}
INTON;
}
@ -480,6 +483,7 @@ bltinsetlocale(void)
if (loc != NULL) {
setlocale(LC_ALL, loc);
INTON;
updatecharset();
return;
}
locdef = bltinlookup("LANG", 0);
@ -491,6 +495,7 @@ bltinsetlocale(void)
setlocale(locale_categories[i], loc);
}
INTON;
updatecharset();
}
/*
@ -505,12 +510,24 @@ bltinunsetlocale(void)
for (lp = cmdenviron ; lp ; lp = lp->next) {
if (localevar(lp->text)) {
setlocale(LC_ALL, "");
updatecharset();
return;
}
}
INTON;
}
/*
* Update the localeisutf8 flag.
*/
void
updatecharset(void)
{
char *charset;
charset = nl_langinfo(CODESET);
localeisutf8 = !strcmp(charset, "UTF-8");
}
/*
* Generate a list of exported variables. This routine is used to construct
@ -656,6 +673,7 @@ exportcmd(int argc, char **argv)
if ((vp->flags & VEXPORT) && localevar(vp->text)) {
change_env(vp->text, 1);
(void) setlocale(LC_ALL, "");
updatecharset();
}
goto found;
}
@ -850,6 +868,7 @@ unsetvar(const char *s)
if ((vp->flags & VEXPORT) && localevar(vp->text)) {
change_env(s, 0);
setlocale(LC_ALL, "");
updatecharset();
}
vp->flags &= ~VEXPORT;
vp->flags |= VUNSET;

View File

@ -81,6 +81,8 @@ extern struct var vhistsize;
extern struct var vterm;
#endif
extern int localeisutf8;
/*
* The following macros access the values of the above variables.
* They have to skip over the name. They return the null string
@ -112,6 +114,7 @@ char *lookupvar(const char *);
char *bltinlookup(const char *, int);
void bltinsetlocale(void);
void bltinunsetlocale(void);
void updatecharset(void);
char **environment(void);
int showvarscmd(int, char **);
int exportcmd(int, char **);