diff options
| author | christos <christos@NetBSD.org> | 2009-02-10 23:06:31 +0000 |
|---|---|---|
| committer | christos <christos@NetBSD.org> | 2009-02-10 23:06:31 +0000 |
| commit | bb0dd6147818d65259296b79d555e7cb09841601 (patch) | |
| tree | db5fd89bb5c1bee43481045702c2cdf6bb38f18d | |
| parent | 78466aa1e74c62c92e0cb9371b8c14c6a1174d8d (diff) | |
Add RFC 2045 MIME Quoted-Printable support.
| -rw-r--r-- | include/vis.h | 19 | ||||
| -rw-r--r-- | lib/libc/gen/unvis.3 | 14 | ||||
| -rw-r--r-- | lib/libc/gen/unvis.c | 118 | ||||
| -rw-r--r-- | lib/libc/gen/vis.3 | 20 | ||||
| -rw-r--r-- | lib/libc/gen/vis.c | 88 | ||||
| -rw-r--r-- | usr.bin/unvis/unvis.c | 44 | ||||
| -rw-r--r-- | usr.bin/vis/extern.h | 39 | ||||
| -rw-r--r-- | usr.bin/vis/foldit.c | 16 | ||||
| -rw-r--r-- | usr.bin/vis/vis.1 | 9 | ||||
| -rw-r--r-- | usr.bin/vis/vis.c | 125 |
10 files changed, 317 insertions, 175 deletions
diff --git a/include/vis.h b/include/vis.h index f3f2f608f59..3c694e64a11 100644 --- a/include/vis.h +++ b/include/vis.h @@ -1,4 +1,4 @@ -/* $NetBSD: vis.h,v 1.16 2005/09/13 01:44:32 christos Exp $ */ +/* $NetBSD: vis.h,v 1.17 2009/02/10 23:06:31 christos Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -39,24 +39,25 @@ /* * to select alternate encoding format */ -#define VIS_OCTAL 0x01 /* use octal \ddd format */ -#define VIS_CSTYLE 0x02 /* use \[nrft0..] where appropiate */ +#define VIS_OCTAL 0x001 /* use octal \ddd format */ +#define VIS_CSTYLE 0x002 /* use \[nrft0..] where appropiate */ /* * to alter set of characters encoded (default is to encode all * non-graphic except space, tab, and newline). */ -#define VIS_SP 0x04 /* also encode space */ -#define VIS_TAB 0x08 /* also encode tab */ -#define VIS_NL 0x10 /* also encode newline */ +#define VIS_SP 0x004 /* also encode space */ +#define VIS_TAB 0x008 /* also encode tab */ +#define VIS_NL 0x010 /* also encode newline */ #define VIS_WHITE (VIS_SP | VIS_TAB | VIS_NL) -#define VIS_SAFE 0x20 /* only encode "unsafe" characters */ +#define VIS_SAFE 0x020 /* only encode "unsafe" characters */ /* * other */ -#define VIS_NOSLASH 0x40 /* inhibit printing '\' */ -#define VIS_HTTPSTYLE 0x80 /* http-style escape % HEX HEX */ +#define VIS_NOSLASH 0x040 /* inhibit printing '\' */ +#define VIS_HTTPSTYLE 0x080 /* http-style escape % hex hex */ +#define VIS_MIMESTYLE 0x100 /* mime-style escape = HEX HEX */ /* * unvis return codes diff --git a/lib/libc/gen/unvis.3 b/lib/libc/gen/unvis.3 index cb71912d0e0..537f16f3622 100644 --- a/lib/libc/gen/unvis.3 +++ b/lib/libc/gen/unvis.3 @@ -1,4 +1,4 @@ -.\" $NetBSD: unvis.3,v 1.17 2005/11/10 17:23:26 christos Exp $ +.\" $NetBSD: unvis.3,v 1.18 2009/02/10 23:06:31 christos Exp $ .\" .\" Copyright (c) 1989, 1991, 1993 .\" The Regents of the University of California. All rights reserved. @@ -29,7 +29,7 @@ .\" .\" @(#)unvis.3 8.2 (Berkeley) 12/11/93 .\" -.Dd November 11, 2005 +.Dd February 10, 2009 .Dt UNVIS 3 .Os .Sh NAME @@ -98,8 +98,10 @@ function, but it allows you to add a flag that specifies the style the string .Ar src is encoded with. -Currently, the only supported flag is -.Dv VIS_HTTPSTYLE . +Currently, the supported flags are: +.Dv VIS_HTTPSTYLE +and +.Dv VIS_MIMESTYLE . .Pp The .Fn unvis @@ -149,6 +151,10 @@ If set to .Dv VIS_HTTPSTYLE , .Fn unvis will decode URI strings as specified in RFC 1808. +If set to +.Dv VIS_MIMESTYLE , +.Fn unvis +will decode MIME Quoted-Printable strings as specified in RFC 2045. .Pp The following code fragment illustrates a proper use of .Fn unvis . diff --git a/lib/libc/gen/unvis.c b/lib/libc/gen/unvis.c index 95f523b55a0..06b53a461dc 100644 --- a/lib/libc/gen/unvis.c +++ b/lib/libc/gen/unvis.c @@ -1,4 +1,4 @@ -/* $NetBSD: unvis.c,v 1.28 2005/09/13 01:44:09 christos Exp $ */ +/* $NetBSD: unvis.c,v 1.29 2009/02/10 23:06:31 christos Exp $ */ /*- * Copyright (c) 1989, 1993 @@ -34,7 +34,7 @@ #if 0 static char sccsid[] = "@(#)unvis.c 8.1 (Berkeley) 6/4/93"; #else -__RCSID("$NetBSD: unvis.c,v 1.28 2005/09/13 01:44:09 christos Exp $"); +__RCSID("$NetBSD: unvis.c,v 1.29 2009/02/10 23:06:31 christos Exp $"); #endif #endif /* LIBC_SCCS and not lint */ @@ -61,20 +61,21 @@ __weak_alias(strunvis,_strunvis) #define S_CTRL 4 /* control char started (^) */ #define S_OCTAL2 5 /* octal digit 2 */ #define S_OCTAL3 6 /* octal digit 3 */ -#define S_HEX1 7 /* hex digit */ -#define S_HEX2 8 /* hex digit 2 */ +#define S_HEX1 7 /* http hex digit */ +#define S_HEX2 8 /* http hex digit 2 */ +#define S_MIME1 9 /* mime hex digit 1 */ +#define S_MIME2 10 /* mime hex digit 2 */ +#define S_EATCRNL 11 /* mime eating CRNL */ #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') #define xtod(c) (isdigit(c) ? (c - '0') : ((tolower(c) - 'a') + 10)) +#define XTOD(c) (isdigit(c) ? (c - '0') : ((c - 'A') + 10)) /* * unvis - decode characters previously encoded by vis */ int -unvis(cp, c, astate, flag) - char *cp; - int c; - int *astate, flag; +unvis(char *cp, int c, int *astate, int flag) { unsigned char uc = (unsigned char)c; @@ -85,7 +86,7 @@ unvis(cp, c, astate, flag) if (*astate == S_OCTAL2 || *astate == S_OCTAL3 || *astate == S_HEX2) { *astate = S_GROUND; - return (UNVIS_VALID); + return UNVIS_VALID; } return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD); } @@ -96,69 +97,73 @@ unvis(cp, c, astate, flag) *cp = 0; if (c == '\\') { *astate = S_START; - return (0); + return UNVIS_NOCHAR; } if ((flag & VIS_HTTPSTYLE) && c == '%') { *astate = S_HEX1; - return (0); + return UNVIS_NOCHAR; + } + if ((flag & VIS_MIMESTYLE) && c == '=') { + *astate = S_MIME1; + return UNVIS_NOCHAR; } *cp = c; - return (UNVIS_VALID); + return UNVIS_VALID; case S_START: switch(c) { case '\\': *cp = c; *astate = S_GROUND; - return (UNVIS_VALID); + return UNVIS_VALID; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': *cp = (c - '0'); *astate = S_OCTAL2; - return (0); + return UNVIS_NOCHAR; case 'M': *cp = (char)0200; *astate = S_META; - return (0); + return UNVIS_NOCHAR; case '^': *astate = S_CTRL; - return (0); + return UNVIS_NOCHAR; case 'n': *cp = '\n'; *astate = S_GROUND; - return (UNVIS_VALID); + return UNVIS_VALID; case 'r': *cp = '\r'; *astate = S_GROUND; - return (UNVIS_VALID); + return UNVIS_VALID; case 'b': *cp = '\b'; *astate = S_GROUND; - return (UNVIS_VALID); + return UNVIS_VALID; case 'a': *cp = '\007'; *astate = S_GROUND; - return (UNVIS_VALID); + return UNVIS_VALID; case 'v': *cp = '\v'; *astate = S_GROUND; - return (UNVIS_VALID); + return UNVIS_VALID; case 't': *cp = '\t'; *astate = S_GROUND; - return (UNVIS_VALID); + return UNVIS_VALID; case 'f': *cp = '\f'; *astate = S_GROUND; - return (UNVIS_VALID); + return UNVIS_VALID; case 's': *cp = ' '; *astate = S_GROUND; - return (UNVIS_VALID); + return UNVIS_VALID; case 'E': *cp = '\033'; *astate = S_GROUND; - return (UNVIS_VALID); + return UNVIS_VALID; case '\n': /* * hidden newline @@ -184,12 +189,12 @@ unvis(cp, c, astate, flag) *astate = S_GROUND; return (UNVIS_SYNBAD); } - return (0); + return UNVIS_NOCHAR; case S_META1: *astate = S_GROUND; *cp |= c; - return (UNVIS_VALID); + return UNVIS_VALID; case S_CTRL: if (c == '?') @@ -197,7 +202,7 @@ unvis(cp, c, astate, flag) else *cp |= c & 037; *astate = S_GROUND; - return (UNVIS_VALID); + return UNVIS_VALID; case S_OCTAL2: /* second possible octal digit */ if (isoctal(uc)) { @@ -206,51 +211,88 @@ unvis(cp, c, astate, flag) */ *cp = (*cp << 3) + (c - '0'); *astate = S_OCTAL3; - return (0); + return UNVIS_NOCHAR; } /* * no - done with current sequence, push back passed char */ *astate = S_GROUND; - return (UNVIS_VALIDPUSH); + return UNVIS_VALIDPUSH; case S_OCTAL3: /* third possible octal digit */ *astate = S_GROUND; if (isoctal(uc)) { *cp = (*cp << 3) + (c - '0'); - return (UNVIS_VALID); + return UNVIS_VALID; } /* * we were done, push back passed char */ - return (UNVIS_VALIDPUSH); + return UNVIS_VALIDPUSH; case S_HEX1: if (isxdigit(uc)) { *cp = xtod(uc); *astate = S_HEX2; - return (0); + return UNVIS_NOCHAR; } /* * no - done with current sequence, push back passed char */ *astate = S_GROUND; - return (UNVIS_VALIDPUSH); + return UNVIS_VALIDPUSH; case S_HEX2: *astate = S_GROUND; if (isxdigit(uc)) { *cp = xtod(uc) | (*cp << 4); - return (UNVIS_VALID); + return UNVIS_VALID; + } + return UNVIS_VALIDPUSH; + + case S_MIME1: + if (uc == '\n' || uc == '\r') { + *astate = S_EATCRNL; + return UNVIS_NOCHAR; + } + if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) { + *cp = XTOD(uc); + *astate = S_MIME2; + return UNVIS_NOCHAR; + } +fprintf(stderr, "Bad hex digit1 %c\n", uc); + *astate = S_GROUND; + return UNVIS_SYNBAD; + + case S_MIME2: + if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) { + *astate = S_GROUND; + *cp = XTOD(uc) | (*cp << 4); + return UNVIS_VALID; + } +fprintf(stderr, "Bad hex digit2 %c\n", uc); + *astate = S_GROUND; + return UNVIS_SYNBAD; + + case S_EATCRNL: + switch (uc) { + case '\r': + case '\n': + return UNVIS_NOCHAR; + case '=': + *astate = S_MIME1; + return UNVIS_NOCHAR; + default: + *cp = uc; + return UNVIS_VALID; } - return (UNVIS_VALIDPUSH); default: /* * decoder in unknown state - (probably uninitialized) */ *astate = S_GROUND; - return (UNVIS_SYNBAD); + return UNVIS_SYNBAD; } } @@ -293,7 +335,7 @@ strunvisx(dst, src, flag) if (unvis(dst, c, &state, UNVIS_END) == UNVIS_VALID) dst++; *dst = '\0'; - return (dst - start); + return (int)(dst - start); } int diff --git a/lib/libc/gen/vis.3 b/lib/libc/gen/vis.3 index 615c3f14535..68c7b84e194 100644 --- a/lib/libc/gen/vis.3 +++ b/lib/libc/gen/vis.3 @@ -1,4 +1,4 @@ -.\" $NetBSD: vis.3,v 1.22 2006/04/09 15:24:34 jschauma Exp $ +.\" $NetBSD: vis.3,v 1.23 2009/02/10 23:06:31 christos Exp $ .\" .\" Copyright (c) 1989, 1991, 1993 .\" The Regents of the University of California. All rights reserved. @@ -29,7 +29,7 @@ .\" .\" @(#)vis.3 8.1 (Berkeley) 6/9/93 .\" -.Dd April 9, 2006 +.Dd February 10, 2009 .Dt VIS 3 .Os .Sh NAME @@ -210,7 +210,11 @@ sequence; two backslashes are used to represent a real backslash, except .Dv VIS_HTTPSTYLE that uses -.Ql % . +.Ql % , +or +.Dv VIS_MIMESTYLE +that uses +.Ql = . These are the visual formats: .Bl -tag -width VIS_CSTYLE .It (default) @@ -302,7 +306,15 @@ The form is .Ql %xx where .Em x -represents a hexadecimal digit. +represents a lower case hexadecimal digit. +.It Dv VIS_MIMESTYLE +Use MIME Quoted-Printable encoding as described in RFC 2045, only don't +break lines and don't handle CRLF. +The form is: +.Ql %XX +where +.Em X +represents an upper case hexadecimal digit. .El .Pp There is one additional flag, diff --git a/lib/libc/gen/vis.c b/lib/libc/gen/vis.c index 4f5bd38437c..26eaf885842 100644 --- a/lib/libc/gen/vis.c +++ b/lib/libc/gen/vis.c @@ -1,4 +1,4 @@ -/* $NetBSD: vis.c,v 1.38 2008/09/04 09:41:44 lukem Exp $ */ +/* $NetBSD: vis.c,v 1.39 2009/02/10 23:06:31 christos Exp $ */ /*- * Copyright (c) 1989, 1993 @@ -57,7 +57,7 @@ #include <sys/cdefs.h> #if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: vis.c,v 1.38 2008/09/04 09:41:44 lukem Exp $"); +__RCSID("$NetBSD: vis.c,v 1.39 2009/02/10 23:06:31 christos Exp $"); #endif /* LIBC_SCCS and not lint */ #include "namespace.h" @@ -91,6 +91,7 @@ static char *do_svis(char *, int, int, int, const char *); #define iswhite(c) (c == ' ' || c == '\t' || c == '\n') #define issafe(c) (c == '\b' || c == BELL || c == '\r') #define xtoa(c) "0123456789abcdef"[c] +#define XTOA(c) "0123456789ABCDEF"[c] #define MAXEXTRAS 5 @@ -130,6 +131,29 @@ do_hvis(char *dst, int c, int flag, int nextc, const char *extra) } /* + * This is do_mvis, for Quoted-Printable MIME (RFC 2045) + * NB: No handling of long lines or CRLF. + */ +static char * +do_mvis(char *dst, int c, int flag, int nextc, const char *extra) +{ + if ((c != '\n') && + /* Space at the end of the line */ + ((isspace(c) && (nextc == '\r' || nextc == '\n')) || + /* Out of range */ + (!isspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) || + /* Specific char to be escaped */ + strchr("#$@[\\]^`{|}~", c) != NULL)) { + *dst++ = '='; + *dst++ = XTOA(((unsigned int)c >> 4) & 0xf); + *dst++ = XTOA((unsigned int)c & 0xf); + } else { + dst = do_svis(dst, c, flag, nextc, extra); + } + return dst; +} + +/* * This is do_vis, the central code of vis. * dst: Pointer to the destination buffer * c: Character to encode @@ -211,6 +235,20 @@ do_svis(char *dst, int c, int flag, int nextc, const char *extra) return dst; } +typedef char *(*visfun_t)(char *, int, int, int, const char *); + +/* + * Return the appropriate encoding function depending on the flags given. + */ +static visfun_t +getvisfun(int flag) +{ + if (flag & VIS_HTTPSTYLE) + return do_hvis; + if (flag * VIS_MIMESTYLE) + return do_mvis; + return do_svis; +} /* * svis - visually encode characters, also encoding the characters @@ -220,6 +258,7 @@ char * svis(char *dst, int c, int flag, int nextc, const char *extra) { char *nextra = NULL; + visfun_t f; _DIAGASSERT(dst != NULL); _DIAGASSERT(extra != NULL); @@ -228,10 +267,8 @@ svis(char *dst, int c, int flag, int nextc, const char *extra) *dst = '\0'; /* can't create nextra, return "" */ return dst; } - if (flag & VIS_HTTPSTYLE) - dst = do_hvis(dst, c, flag, nextc, nextra); - else - dst = do_svis(dst, c, flag, nextc, nextra); + f = getvisfun(flag); + dst = (*f)(dst, c, flag, nextc, nextra); free(nextra); *dst = '\0'; return dst; @@ -260,6 +297,7 @@ strsvis(char *dst, const char *csrc, int flag, const char *extra) char *start; char *nextra = NULL; const unsigned char *src = (const unsigned char *)csrc; + visfun_t f; _DIAGASSERT(dst != NULL); _DIAGASSERT(src != NULL); @@ -269,16 +307,12 @@ strsvis(char *dst, const char *csrc, int flag, const char *extra) *dst = '\0'; /* can't create nextra, return "" */ return 0; } - if (flag & VIS_HTTPSTYLE) { - for (start = dst; (c = *src++) != '\0'; /* empty */) - dst = do_hvis(dst, c, flag, *src, nextra); - } else { - for (start = dst; (c = *src++) != '\0'; /* empty */) - dst = do_svis(dst, c, flag, *src, nextra); - } + f = getvisfun(flag); + for (start = dst; (c = *src++) != '\0'; /* empty */) + dst = (*f)(dst, c, flag, *src, nextra); free(nextra); *dst = '\0'; - return (dst - start); + return (int)(dst - start); } @@ -289,6 +323,7 @@ strsvisx(char *dst, const char *csrc, size_t len, int flag, const char *extra) char *start; char *nextra = NULL; const unsigned char *src = (const unsigned char *)csrc; + visfun_t f; _DIAGASSERT(dst != NULL); _DIAGASSERT(src != NULL); @@ -299,22 +334,14 @@ strsvisx(char *dst, const char *csrc, size_t len, int flag, const char *extra) return 0; } - if (flag & VIS_HTTPSTYLE) { - for (start = dst; len > 0; len--) { - c = *src++; - dst = do_hvis(dst, c, flag, - len > 1 ? *src : '\0', nextra); - } - } else { - for (start = dst; len > 0; len--) { - c = *src++; - dst = do_svis(dst, c, flag, - len > 1 ? *src : '\0', nextra); - } + f = getvisfun(flag); + for (start = dst; len > 0; len--) { + c = *src++; + dst = (*f)(dst, c, flag, len > 1 ? *src : '\0', nextra); } free(nextra); *dst = '\0'; - return (dst - start); + return (int)(dst - start); } #endif @@ -327,6 +354,7 @@ vis(char *dst, int c, int flag, int nextc) { char *extra = NULL; unsigned char uc = (unsigned char)c; + visfun_t f; _DIAGASSERT(dst != NULL); @@ -335,10 +363,8 @@ vis(char *dst, int c, int flag, int nextc) *dst = '\0'; /* can't create extra, return "" */ return dst; } - if (flag & VIS_HTTPSTYLE) - dst = do_hvis(dst, uc, flag, nextc, extra); - else - dst = do_svis(dst, uc, flag, nextc, extra); + f = getvisfun(flag); + dst = (*f)(dst, uc, flag, nextc, extra); free(extra); *dst = '\0'; return dst; diff --git a/usr.bin/unvis/unvis.c b/usr.bin/unvis/unvis.c index 6d1407fb263..32830f4a2d8 100644 --- a/usr.bin/unvis/unvis.c +++ b/usr.bin/unvis/unvis.c @@ -1,4 +1,4 @@ -/* $NetBSD: unvis.c,v 1.11 2008/07/21 14:19:27 lukem Exp $ */ +/* $NetBSD: unvis.c,v 1.12 2009/02/10 23:06:31 christos Exp $ */ /*- * Copyright (c) 1989, 1993 @@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1993\ #if 0 static char sccsid[] = "@(#)unvis.c 8.1 (Berkeley) 6/6/93"; #endif -__RCSID("$NetBSD: unvis.c,v 1.11 2008/07/21 14:19:27 lukem Exp $"); +__RCSID("$NetBSD: unvis.c,v 1.12 2009/02/10 23:06:31 christos Exp $"); #endif /* not lint */ #include <err.h> @@ -48,35 +48,41 @@ __RCSID("$NetBSD: unvis.c,v 1.11 2008/07/21 14:19:27 lukem Exp $"); #include <unistd.h> #include <vis.h> -int eflags; +static int eflags; -int main __P((int, char **)); -void process __P((FILE *fp, const char *filename)); +static void process(FILE *fp, const char *filename); int -main(argc, argv) - int argc; - char *argv[]; +main(int argc, char *argv[]) { FILE *fp; int ch; - while ((ch = getopt(argc, argv, "h")) != -1) + setprogname(argv[0]); + while ((ch = getopt(argc, argv, "hm")) != -1) switch((char)ch) { case 'h': eflags |= VIS_HTTPSTYLE; break; + case 'm': + eflags |= VIS_MIMESTYLE; + break; case '?': default: - (void) fprintf(stderr, "usage: unvis [-h] [file...]\n"); - exit(1); + (void)fprintf(stderr, + "Usage: %s [-h|-m] [file...]\n", getprogname()); + return 1; } argc -= optind; argv += optind; + if ((eflags & (VIS_HTTPSTYLE|VIS_MIMESTYLE)) == + (VIS_HTTPSTYLE|VIS_MIMESTYLE)) + errx(1, "Can't specify -m and -h at the same time"); + if (*argv) while (*argv) { - if ((fp=fopen(*argv, "r")) != NULL) + if ((fp = fopen(*argv, "r")) != NULL) process(fp, *argv); else warn("%s", *argv); @@ -84,13 +90,11 @@ main(argc, argv) } else process(stdin, "<stdin>"); - exit(0); + return 0; } -void -process(fp, filename) - FILE *fp; - const char *filename; +static void +process(FILE *fp, const char *filename) { int offset = 0, c, ret; int state = 0; @@ -101,10 +105,10 @@ process(fp, filename) again: switch(ret = unvis(&outc, (char)c, &state, eflags)) { case UNVIS_VALID: - putchar(outc); + (void)putchar(outc); break; case UNVIS_VALIDPUSH: - putchar(outc); + (void)putchar(outc); goto again; case UNVIS_SYNBAD: warnx("%s: offset: %d: can't decode", filename, offset); @@ -119,5 +123,5 @@ process(fp, filename) } } if (unvis(&outc, (char)0, &state, eflags | UNVIS_END) == UNVIS_VALID) - putchar(outc); + (void)putchar(outc); } diff --git a/usr.bin/vis/extern.h b/usr.bin/vis/extern.h new file mode 100644 index 00000000000..49b6f15b3ba --- /dev/null +++ b/usr.bin/vis/extern.h @@ -0,0 +1,39 @@ +/* $NetBSD: extern.h,v 1.1 2009/02/10 23:06:31 christos Exp $ */ + +/*- + * Copyright (c) 2009 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * 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. + */ + +int foldit(const char *, int, int, int); diff --git a/usr.bin/vis/foldit.c b/usr.bin/vis/foldit.c index 748d0d29582..789b14cb67c 100644 --- a/usr.bin/vis/foldit.c +++ b/usr.bin/vis/foldit.c @@ -1,4 +1,4 @@ -/* $NetBSD: foldit.c,v 1.6 2003/08/07 11:17:09 agc Exp $ */ +/* $NetBSD: foldit.c,v 1.7 2009/02/10 23:06:31 christos Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -34,19 +34,17 @@ #if 0 static char sccsid[] = "@(#)foldit.c 8.1 (Berkeley) 6/6/93"; #endif -__RCSID("$NetBSD: foldit.c,v 1.6 2003/08/07 11:17:09 agc Exp $"); +__RCSID("$NetBSD: foldit.c,v 1.7 2009/02/10 23:06:31 christos Exp $"); #endif /* not lint */ #include <stdio.h> - -int foldit __P((char *, int, int)); +#include <vis.h> +#include "extern.h" int -foldit(chunk, col, max) - char *chunk; - int col, max; +foldit(const char *chunk, int col, int max, int flags) { - char *cp; + const char *cp; /* * Keep track of column position. Insert hidden newline @@ -70,7 +68,7 @@ again: col++; } if (col > (max - 2)) { - printf("\\\n"); + printf(flags & VIS_MIMESTYLE ? "=\n" : "\\\n"); col = 0; goto again; } diff --git a/usr.bin/vis/vis.1 b/usr.bin/vis/vis.1 index 238972c7dc3..13c28067a11 100644 --- a/usr.bin/vis/vis.1 +++ b/usr.bin/vis/vis.1 @@ -1,4 +1,4 @@ -.\" $NetBSD: vis.1,v 1.13 2004/04/22 06:53:43 lukem Exp $ +.\" $NetBSD: vis.1,v 1.14 2009/02/10 23:06:31 christos Exp $ .\" .\" Copyright (c) 1989, 1991, 1993, 1994 .\" The Regents of the University of California. All rights reserved. @@ -29,7 +29,7 @@ .\" .\" @(#)vis.1 8.4 (Berkeley) 4/19/94 .\" -.Dd April 22, 2004 +.Dd February 10, 2009 .Dt VIS 1 .Os .Sh NAME @@ -37,7 +37,7 @@ .Nd display non-printable characters in a visual format .Sh SYNOPSIS .Nm -.Op Fl bcfhlnostw +.Op Fl bcfhlmnostw .Op Fl e Ar extra .Op Fl F Ar foldwidth .Op Ar file ... @@ -99,6 +99,9 @@ Encode using the URI encoding from RFC 1808. Mark newlines with the visible sequence .Ql \e$ , followed by the newline. +.It Fl m +Encode using the MIME Quoted-Printable encoding from RFC 2045. +.Pq Dv VIS_MIMESTYLE .It Fl n Turns off any encoding, except for the fact that backslashes are still doubled and hidden newline sequences inserted if diff --git a/usr.bin/vis/vis.c b/usr.bin/vis/vis.c index f4c8c026e07..f2666a29a98 100644 --- a/usr.bin/vis/vis.c +++ b/usr.bin/vis/vis.c @@ -1,4 +1,4 @@ -/* $NetBSD: vis.c,v 1.12 2008/07/21 14:19:27 lukem Exp $ */ +/* $NetBSD: vis.c,v 1.13 2009/02/10 23:06:31 christos Exp $ */ /*- * Copyright (c) 1989, 1993 @@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1993\ #if 0 static char sccsid[] = "@(#)vis.c 8.1 (Berkeley) 6/6/93"; #endif -__RCSID("$NetBSD: vis.c,v 1.12 2008/07/21 14:19:27 lukem Exp $"); +__RCSID("$NetBSD: vis.c,v 1.13 2009/02/10 23:06:31 christos Exp $"); #endif /* not lint */ #include <stdio.h> @@ -49,75 +49,87 @@ __RCSID("$NetBSD: vis.c,v 1.12 2008/07/21 14:19:27 lukem Exp $"); #include <err.h> #include <vis.h> -int eflags, fold, foldwidth=80, none, markeol, debug; -char *extra; +#include "extern.h" -int foldit __P((char *, int, int)); -int main __P((int, char **)); -void process __P((FILE *, char *)); +static int eflags, fold, foldwidth = 80, none, markeol; +#ifdef DEBUG +int debug; +#endif +static char *extra; + +static void process(FILE *); int -main(argc, argv) - int argc; - char *argv[]; +main(int argc, char *argv[]) { FILE *fp; int ch; int rval; - while ((ch = getopt(argc, argv, "bcfhlnostwe:F:d")) != -1) + while ((ch = getopt(argc, argv, "bcde:F:fhlmnostw")) != -1) switch((char)ch) { - case 'n': - none++; - break; - case 'w': - eflags |= VIS_WHITE; + case 'b': + eflags |= VIS_NOSLASH; break; case 'c': eflags |= VIS_CSTYLE; break; - case 't': - eflags |= VIS_TAB; - break; - case 's': - eflags |= VIS_SAFE; - break; - case 'o': - eflags |= VIS_OCTAL; - break; - case 'h': - eflags |= VIS_HTTPSTYLE; - break; - case 'b': - eflags |= VIS_NOSLASH; +#ifdef DEBUG + case 'd': + debug++; break; +#endif case 'e': extra = optarg; break; case 'F': - if ((foldwidth = atoi(optarg))<5) { + if ((foldwidth = atoi(optarg)) < 5) { errx(1, "can't fold lines to less than 5 cols"); /* NOTREACHED */ } - /*FALLTHROUGH*/ + markeol++; + break; case 'f': fold++; /* fold output lines to 80 cols */ break; /* using hidden newline */ + case 'h': + eflags |= VIS_HTTPSTYLE; + break; case 'l': markeol++; /* mark end of line with \$ */ break; -#ifdef DEBUG - case 'd': - debug++; + case 'm': + eflags |= VIS_MIMESTYLE; + if (foldwidth == 80) + foldwidth = 76; + break; + case 'n': + none++; + break; + case 'o': + eflags |= VIS_OCTAL; + break; + case 's': + eflags |= VIS_SAFE; + break; + case 't': + eflags |= VIS_TAB; + break; + case 'w': + eflags |= VIS_WHITE; break; -#endif case '?': default: - fprintf(stderr, - "usage: %s [-bcfhlnostw] [-e extra] [-F foldwidth]" - " [file ...]\n", getprogname()); - exit(1); + (void)fprintf(stderr, + "Usage: %s [-bcfhlmmnostw] [-e extra]" + " [-F foldwidth] [file ...]\n", getprogname()); + return 1; } + + if (eflags & (VIS_HTTPSTYLE|VIS_MIMESTYLE) == + (VIS_HTTPSTYLE|VIS_MIMESTYLE)) + errx(1, "Can't specify -m and -h at the same time"); + argc -= optind; argv += optind; @@ -125,9 +137,9 @@ main(argc, argv) if (*argv) while (*argv) { - if ((fp=fopen(*argv, "r")) != NULL) { - process(fp, *argv); - fclose(fp); + if ((fp = fopen(*argv, "r")) != NULL) { + process(fp); + (void)fclose(fp); } else { warn("%s", *argv); rval = 1; @@ -136,16 +148,15 @@ main(argc, argv) } else process(stdin, "<stdin>"); - exit(rval); + return rval; } -void -process(fp, filename) - FILE *fp; - char *filename; +static void +process(FILE *fp) { static int col = 0; - char *cp = "\0"+1; /* so *(cp-1) starts out != '\n' */ + static char nul[] = "\0"; + char *cp = nul + 1; /* so *(cp-1) starts out != '\n' */ int c, rachar; char buff[5]; @@ -166,30 +177,30 @@ process(fp, filename) *cp++ = '\n'; *cp = '\0'; } else if (extra) - (void) svis(buff, (char)c, eflags, (char)rachar, extra); + (void)svis(buff, (char)c, eflags, (char)rachar, extra); else - (void) vis(buff, (char)c, eflags, (char)rachar); + (void)vis(buff, (char)c, eflags, (char)rachar); cp = buff; if (fold) { #ifdef DEBUG if (debug) - printf("<%02d,", col); + (void)printf("<%02d,", col); #endif - col = foldit(cp, col, foldwidth); + col = foldit(cp, col, foldwidth, eflags); #ifdef DEBUG if (debug) - printf("%02d>", col); + (void)printf("%02d>", col); #endif } do { - putchar(*cp); + (void)putchar(*cp); } while (*++cp); c = rachar; } /* * terminate partial line with a hidden newline */ - if (fold && *(cp-1) != '\n') - printf("\\\n"); + if (fold && *(cp - 1) != '\n') + (void)printf(eflags & VIS_MIMESTYLE ? "=\n" : "\\\n"); } |
