예제 #1
0
/*
 * Function to compute the number of printable bytes in a multibyte
 * command string ("internationalization").
 */
static int
namencnt(char *cmd, int eucsize, int scrsize)
{
	int eucwcnt = 0, scrwcnt = 0;
	int neucsz, nscrsz;
	wchar_t	wchar;

	while (*cmd != '\0') {
		if ((neucsz = mbtowc(&wchar, cmd, MB_LEN_MAX)) < 0)
			return (8); /* default to use for illegal chars */
		if ((nscrsz = scrwidth(wchar)) == 0)
			return (8);
		if (eucwcnt + neucsz > eucsize || scrwcnt + nscrsz > scrsize)
			break;
		eucwcnt += neucsz;
		scrwcnt += nscrsz;
		cmd += neucsz;
	}
	return (eucwcnt);
}
예제 #2
0
파일: fmt.c 프로젝트: alhazred/onarm
static void
fmt(FILE *fi)
{
	wchar_t linebuf[BUFSIZ], canonb[BUFSIZ];
	wchar_t *cp, *cp2;
	int col;
	wchar_t	c;
	char	cbuf[BUFSIZ];	/* stores wchar_t string as char string */

	c = getwc(fi);
	while (c != EOF) {
		/*
		 * Collect a line, doing ^H processing.
		 * Leave tabs for now.
		 */

		cp = linebuf;
		while (c != L'\n' && c != EOF && cp-linebuf < BUFSIZ-1) {
			if (c == L'\b') {
				if (cp > linebuf)
					cp--;
				c = getwc(fi);
				continue;
			}
			if (!(iswprint(c)) && c != L'\t') {
				c = getwc(fi);
				continue;
			}
			*cp++ = c;
			c = getwc(fi);
		}
		*cp = L'\0';

		/*
		 * Toss anything remaining on the input line.
		 */

		while (c != L'\n' && c != EOF)
			c = getwc(fi);
		/*
		 * Expand tabs on the way to canonb.
		 */

		col = 0;
		cp = linebuf;
		cp2 = canonb;
		while (c = *cp++) {
			if (c != L'\t') {
				col += scrwidth(c);
				if (cp2-canonb < BUFSIZ-1)
					*cp2++ = c;
				continue;
			}
			do {
				if (cp2-canonb < BUFSIZ-1)
					*cp2++ = L' ';
				col++;
			} while ((col & 07) != 0);
		}

		/*
		 * Swipe trailing blanks from the line.
		 */

		for (cp2--; cp2 >= canonb && *cp2 == L' '; cp2--);
		*++cp2 = '\0';

			/* special processing to look for mail header lines */
		switch (hdr_state) {
		case off:
			prefix(canonb);
		case not_in_hdr:
			/* look for an initial mail header line */
			/* skip initial blanks */
			for (cp = canonb; *cp == L' '; cp++);
			/*
			 * Need to convert string from wchar_t to char,
			 * since this is what ishead() expects.  Since we
			 * only want to make sure cp points to a "From" line
			 * of the email, we don't have to alloc
			 * BUFSIZ * MB_LEN_MAX to cbuf.
			 */
			wcstombs(cbuf, cp, (BUFSIZ - 1));
			if (ishead(cbuf)) {
				hdr_state = in_hdr;
				fill_hdrbuf(canonb);
			} else {
				/* no mail header line; process normally */
				prefix(canonb);
			}
			break;
		case in_hdr:
			/* already saw 1st mail header line; look for more */
			if (canonb[0] == L'\0') {
				/*
				 * blank line means end of mail header;
				 * verify current mail header buffer
				 * then process it accordingly
				 */
				header_chk();
				process_hdrbuf();
				/* now process the current blank line */
				prefix(canonb);
			} else
				/*
				 * not a blank line--save this line as
				 * a potential mail header line
				 */
				fill_hdrbuf(canonb);
			break;
		}
		if (c != EOF)
			c = getwc(fi);
	}
	/*
	 * end of this file--make sure we process the stuff in
	 * hdrbuf before we're finished
	 */
	if (hdr_state == in_hdr) {
		header_chk();
		process_hdrbuf();
	}
}