sh.tchar.c revision 70a587dd392ff1dbaa2875c6c33921f08ea85273
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley Software License Agreement
* specifies the terms and conditions for redistribution.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* datatype. This also includes two conversion functions between tchar and
* char arrays.
*
* T. Kurosaka, Palo Alto, California, USA
* March 1989
*
* Implementation Notes:
* Many functions defined here use a "char" buffer chbuf[]. In the
* first attempt, there used to be only one chbuf defined as static
* (private) variable and shared by these functions. csh linked with that
* version of this file misbehaved in interpreting "eval `tset ....`".
* (in general, builtin function with back-quoted expression).
* This bug seemed to be caused by sharing of chbuf
* by these functions simultanously (thru vfork() mechanism?). We could not
* identify which two functions interfere each other so we decided to
* have each of these function its private instance of chbuf.
* The size of chbuf[] might be much bigger than necessary for some functions.
*/
#ifdef DBG
#else /* !DBG */
#define NDEBUG /* Disable assert(). */
#endif /* !DBG */
#include <assert.h>
#include "sh.h"
#ifdef MBCHAR
#include <widec.h> /* For wcsetno() */
#endif
#include <fcntl.h>
#include <unistd.h>
/*
* strtots(to, from): convert a char string 'from' into a tchar buffer 'to'.
* 'to' is assumed to have the enough size to hold the conversion result.
* When 'to' is NOSTR(=(tchar *)0), strtots() attempts to allocate a space
* automatically using xalloc(). It is caller's responsibility to
* free the space allocated in this way, by calling xfree(ptr).
* In either case, strtots() returns the pointer to the conversion
* result (i.e. 'to', if 'to' wasn't NOSTR, or the allocated space.).
* When a conversion or allocateion failed, NOSTR is returned.
*/
tchar *
{
int i;
int i;
if (i < 0) {
return (NOSTR);
}
/* Allocate space for the resulting tchar array. */
}
if (i < 0) {
return (NOSTR);
}
return (to);
}
char *
{
char *pmb;
int len;
int i;
int i1;
char junk[MB_LEN_MAX];
/* Get sum of byte counts for each char in from. */
i = 0;
i1 = 1;
}
i += i1;
}
/* Allocate that much. */
}
len = 1;
}
}
*pmb = (char)0;
return (to);
}
/*
* mbstotcs(to, from, tosize) is similar to strtots() except that
* this returns # of tchars of the resulting tchar string.
* When NULL is give as the destination, no real conversion is carried out,
* and the function reports how many tchar characters would be made in
* the converted result including the terminating 0.
* tchar *to; - Destination buffer, or NULL.
* char *from; - Source string.
* int tosize; - Size of to, in terms of # of tchars.
*/
int
{
int chcnt = 0;
int j;
/* Just count how many tchar would be in the result. */
while (*pmb) {
j = 1;
}
pmb += j;
chcnt++;
}
chcnt++; /* For terminator. */
return (chcnt); /* # of chars including terminating zero. */
} else { /* Do the real conversion. */
while (*pmb) {
j = 1;
}
pmb += j;
break;
}
}
/* Terminate with zero only when space is left. */
++chcnt;
}
return (chcnt); /* # of chars including terminating zero. */
}
}
/* tchar version of STRING functions. */
/*
* Returns the number of
* non-NULL tchar elements in tchar string argument.
*/
int
{
int n;
n = 0;
while (*s++) {
n++;
}
return (n);
}
/*
* Concatenate tchar string s2 on the end of s1. S1's space must be large
* enough. Return s1.
*/
tchar *
{
while (*s1++)
;
--s1;
;
return (os1);
}
/*
* Compare tchar strings: s1>s2: >0 s1==s2: 0 s1<s2: <0
* BUGS: Comparison between two characters are done by subtracting two chars
* after converting each to an unsigned long int value. It might not make
* a whole lot of sense to do that if the characters are in represented
* as wide characters and the two characters belong to different codesets.
* Therefore, this function should be used only to test the equallness.
*/
int
{
return (0);
}
}
}
/*
* This is only used in sh.glob.c for sorting purpose.
*/
int
{
}
/*
* Copy tchar string s2 to s1. s1 must be large enough.
* return s1
*/
tchar *
{
;
return (os1);
}
/*
* Return the ptr in sp at which the character c appears;
* NULL if not found
*/
tchar *
{
do {
if (*sp == c) {
return (sp);
}
} while (*sp++);
return (NULL);
}
/*
* Return the ptr in sp at which the character c last
* appears; NOSTR if not found
*/
tchar *
{
tchar *r;
r = NOSTR;
do {
if (*sp == c) {
r = sp;
}
} while (*sp++);
return (r);
}
/* Additional misc functions. */
/* Calculate the display width of a string. */
int
{
#ifdef MBCHAR
int w = 0;
int p_col;
w += p_col;
}
return (w);
#else /* !MBCHAR --- one char always occupies one column. */
#endif
}
/*
* Two getenv() substitute functions. They differ in the type of arguments.
* BUGS: Both returns the pointer to an allocated space where the env var's
* values is stored. This space is freed automatically on the successive
* call of either function. Therefore the caller must copy the contents
* if it needs to access two env vars. There is an arbitary limitation
* on the number of chars of a env var name.
*/
tchar *
{
}
tchar *
{
char *val;
if (pbuf) {
}
return (NOSTR);
}
}
/* Followings are the system call interface for tchar strings. */
/*
* creat() and open() replacement.
* BUGS: An unusually long file name could be dangerous.
*/
int
{
int fd;
if (fd != -1) {
}
return (fd);
}
/*VARARGS2*/
int
int flags;
int mode; /* May be omitted. */
{
int fd;
if (fd != -1) {
}
return (fd);
}
/*
* mkstemp replacement
*/
int
{
int fd;
if (fd != -1) {
}
return (fd);
}
/*
* read() and write() reaplacement.
* int d;
* tchar *buf; - where the result be stored. Not NULL terminated.
* int nchreq; - # of tchars requrested.
*/
int
{
#ifdef MBCHAR
/*
* We would have to read more than tchar bytes
* when there are multibyte characters in the file.
*/
int i, j, fflags;
unsigned char *s; /* Byte being scanned for a multibyte char. */
/* Points to the pos where next read() to read the data into. */
unsigned char *p;
tchar *t;
int b_len;
int nchread = 0; /* Count how many bytes has been read. */
int nbytread = 0; /* Total # of bytes read. */
/* # of bytes needed to complete the last char just read. */
int delta;
unsigned char *q; /* q points to the first invalid byte. */
int mb_cur_max = MB_CUR_MAX;
#ifdef DBG
tprintf("Entering read_(d=%d, buf=0x%x, nchreq=%d);\n",
#endif /* DBG */
/*
* Step 1: We collect the exact number of bytes that make
* nchreq characters into chbuf.
* We must be careful not to read too many bytes as we
* cannot push back such over-read bytes.
* The idea we use here is that n multibyte characters are stored
* in no less than n but less than n*MB_CUR_MAX bytes.
*/
delta = 0;
p = s = chbuf;
t = buf;
int m; /* # of bytes to try to read this time. */
int k; /* # of bytes successfully read. */
/*
* Let's say the (N+1)'th byte bN is actually the first
* byte of a three-byte character c.
* In that case, p, s, q look like this:
*
* /-- already read--\ /-- not yet read --\
* chbuf[]: b0 b1 ..... bN bN+1 bN+2 bN+2 ...
* ^ ^ ^
* | | |
* p s q
* \----------/
* c hasn't been completed
*
* Just after the next read(), p and q will be adavanced to:
*
* /-- already read-----------------------\ /-- not yet -
* chbuf[]: b0 b1 ..... bN bN+1 bN+2 bN+2 ... bX bX+1 bX+2...
* ^ ^ ^
* | | |
* s p q
* \----------/
* c has been completed
* but hasn't been scanned
*/
k = read(d, p, m);
/*
* when child sets O_NDELAY or O_NONBLOCK on stdin
* and exits and we are interactive then turn the modes off
* and retry
*/
if (k == 0) {
goto retry;
}
} else if (k < 0) {
fflags &= ~O_NONBLOCK;
goto retry;
}
return (-1);
}
nbytread += k;
q = p + k;
delta = 0;
/* Try scaning characters in s..q-1 */
while (s < q) {
/* Convert the collected bytes into tchar array. */
if (*s == 0) {
/* NUL is treated as a normal char here. */
*t++ = 0;
s++;
nchread++;
continue;
}
if ((b_len = q - s) > mb_cur_max) {
b_len = mb_cur_max;
}
/*
* Needs more byte to complete this char
* In order to read() more than delta
* bytes.
*/
break;
}
wc = (unsigned char)*s;
j = 1;
}
*t++ = wc;
nchread++;
s += j;
}
if (k < m) {
/* We've read as many bytes as possible. */
while (s < q) {
if ((b_len = q - s) > mb_cur_max) {
b_len = mb_cur_max;
}
wc = (unsigned char)*s;
j = 1;
}
*t++ = wc;
nchread++;
s += j;
}
return (nchread);
}
p = q;
}
return (nchread);
}
/*
* We may have (MB_CUR_MAX - 1) unread data in the buffer.
* Here, the last converted data was an illegal character which was
* treated as one byte character. We don't know at this point
* whether or not the remaining data is in legal sequence.
* We first attempt to convert the remaining data.
*/
do {
break;
*t++ = wc;
nchread++;
s += j;
delta -= j;
} while (delta > 0);
if (delta == 0)
return (nchread);
/*
* There seem to be ugly sequence in the buffer. Fill up till
* mb_cur_max and see if we can get a right sequence.
*/
while (delta < mb_cur_max) {
break;
delta++;
q++;
*t = wc;
return (nchread + 1);
}
}
/*
* no luck. we have filled MB_CUR_MAX bytes in the buffer.
* Ideally we should return with leaving such data off and
* put them into a local buffer for next read, but we don't
* have such.
* So, stop reading further, and treat them as all single
* byte characters.
*/
while (s < q) {
b_len = q - s;
wc = (unsigned char)*s;
j = 1;
}
*t++ = wc;
nchread++;
s += j;
}
return (nchread);
#else /* !MBCHAR */
/* One byte always represents one tchar. Easy! */
int i;
unsigned char *s;
tchar *t;
int nchread;
#ifdef DBG
tprintf("Entering read_(d=%d, buf=0x%x, nchreq=%d);\n",
#endif /* DBG */
/*
* when child sets O_NDELAY or O_NONBLOCK on stdin
* and exits and we are interactive then turn the modes off
* and retry
*/
if (nchread == 0) {
goto retry;
}
} else if (nchread < 0) {
fflags &= ~O_NONBLOCK;
goto retry;
}
len = 0;
} else {
*t++ = ((tchar)*s++);
}
}
return (nchread);
#endif
}
/*
* BUG: write_() returns -1 on failure, or # of BYTEs it has written.
* For consistency and symmetry, it should return the number of
* characters it has actually written, but that is technically
* difficult although not impossible. Anyway, the return
* value of write() has never been used by the original csh,
* so this bug should be OK.
*/
int
{
#ifdef MBCHAR
unsigned char *pc;
int i, j;
#ifdef DBG
tprintf("Entering write_(d=%d, buf=0x%x, nch=%d);\n",
#endif /* DBG */
i = nch;
while (i--) {
/*
* Convert to tchar string.
* NUL is treated as normal char here.
*/
*pc++ = 0;
} else {
j = 1;
}
pc += j;
}
}
#else /* !MBCHAR */
/* One byte always represents one tchar. Easy! */
int i;
unsigned char *s;
tchar *t;
#ifdef DBG
tprintf("Entering write_(d=%d, buf=0x%x, nch=%d);\n",
#endif /* DBG */
*s++ = (char)((*t++)&0xff);
}
#endif
}
#include <dirent.h> /* DIR */
int
{
}
int
{
}
int
{
}
tchar *
{
int rc;
if (rc == 0) {
return (0);
} else {
}
}
int
{
}
DIR *
{
}
}
int
{
int ret;
extern int closedir();
return (ret);
}
int
{
return (-1);
}
return (-1);
}
return (0); /* Succeeded. */
}
int
{
int i;
if (i < 0) {
return (-1);
}
chbuf[i] = (char)0; /* readlink() doesn't put NULL. */
if (i < 0) {
return (-1);
}
return (i - 1); /* Return # of tchars EXCLUDING the terminating NULL. */
}
/* checks that it's a number */
int
{
char *c = chbuf;
while (*c)
if (!isdigit(*(c++)))
return (-1);
return (0);
}
int
{
}
tchar *
{
while (1) {
while (*sname++ != '/')
;
} else {
return (sname);
}
}
}