1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 1997 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 31*7c478bd9Sstevel@tonic-gate 32*7c478bd9Sstevel@tonic-gate #include <stdio.h> 33*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 34*7c478bd9Sstevel@tonic-gate #include <ctype.h> 35*7c478bd9Sstevel@tonic-gate #include <wctype.h> 36*7c478bd9Sstevel@tonic-gate #include <widec.h> 37*7c478bd9Sstevel@tonic-gate #include <dlfcn.h> 38*7c478bd9Sstevel@tonic-gate #include <locale.h> 39*7c478bd9Sstevel@tonic-gate #include <sys/param.h> 40*7c478bd9Sstevel@tonic-gate #include <string.h> 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate /* 43*7c478bd9Sstevel@tonic-gate * fmt -- format the concatenation of input files or standard input 44*7c478bd9Sstevel@tonic-gate * onto standard output. Designed for use with Mail ~| 45*7c478bd9Sstevel@tonic-gate * 46*7c478bd9Sstevel@tonic-gate * Syntax: fmt [ -width | -w width ] [ -cs ] [ name ... ] 47*7c478bd9Sstevel@tonic-gate * Author: Kurt Shoens (UCB) 12/7/78 48*7c478bd9Sstevel@tonic-gate */ 49*7c478bd9Sstevel@tonic-gate 50*7c478bd9Sstevel@tonic-gate #define NOSTR ((wchar_t *) 0) /* Null string pointer for lint */ 51*7c478bd9Sstevel@tonic-gate #define MAXLINES 100 /* maximum mail header lines to verify */ 52*7c478bd9Sstevel@tonic-gate 53*7c478bd9Sstevel@tonic-gate wchar_t outbuf[BUFSIZ]; /* Sandbagged output line image */ 54*7c478bd9Sstevel@tonic-gate wchar_t *outp; /* Pointer in above */ 55*7c478bd9Sstevel@tonic-gate int filler; /* Filler amount in outbuf */ 56*7c478bd9Sstevel@tonic-gate 57*7c478bd9Sstevel@tonic-gate int pfx; /* Current leading blank count */ 58*7c478bd9Sstevel@tonic-gate int width = 72; /* Width that we will not exceed */ 59*7c478bd9Sstevel@tonic-gate int nojoin = 0; /* split lines only, don't join short ones */ 60*7c478bd9Sstevel@tonic-gate int errs = 0; /* Current number of errors */ 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gate enum crown_type {c_none, c_reset, c_head, c_lead, c_fixup, c_body}; 63*7c478bd9Sstevel@tonic-gate enum crown_type crown_state; /* Crown margin state */ 64*7c478bd9Sstevel@tonic-gate int crown_head; /* The header offset */ 65*7c478bd9Sstevel@tonic-gate int crown_body; /* The body offset */ 66*7c478bd9Sstevel@tonic-gate /* currently-known initial strings found in mail headers */ 67*7c478bd9Sstevel@tonic-gate wchar_t *headnames[] = { 68*7c478bd9Sstevel@tonic-gate L"Apparently-To", L"Bcc", L"bcc", L"Cc", L"cc", L"Confirmed-By", 69*7c478bd9Sstevel@tonic-gate L"Content", L"content-length", L"From", L"Date", L"id", 70*7c478bd9Sstevel@tonic-gate L"Message-I", L"MIME-Version", L"Precedence", L"Return-Path", 71*7c478bd9Sstevel@tonic-gate L"Received", L"Reply-To", L"Status", L"Subject", L"To", L"X-IMAP", 72*7c478bd9Sstevel@tonic-gate L"X-Lines", L"X-Sender", L"X-Sun", L"X-Status", L"X-UID", 73*7c478bd9Sstevel@tonic-gate 0}; 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate enum hdr_type { 76*7c478bd9Sstevel@tonic-gate off, /* mail header processing is off */ 77*7c478bd9Sstevel@tonic-gate not_in_hdr, /* not currently processing a mail header */ 78*7c478bd9Sstevel@tonic-gate in_hdr, /* currently filling hdrbuf with potential hdr lines */ 79*7c478bd9Sstevel@tonic-gate flush_hdr, /* flush hdrbuf; not a header, no special processing */ 80*7c478bd9Sstevel@tonic-gate do_hdr /* process hdrbuf as a mail header */ 81*7c478bd9Sstevel@tonic-gate }; 82*7c478bd9Sstevel@tonic-gate /* current state of hdrbuf */ 83*7c478bd9Sstevel@tonic-gate enum hdr_type hdr_state = not_in_hdr; 84*7c478bd9Sstevel@tonic-gate 85*7c478bd9Sstevel@tonic-gate wchar_t *hdrbuf[MAXLINES]; /* buffer to hold potential mail header lines */ 86*7c478bd9Sstevel@tonic-gate int h_lines; /* index into lines of hdrbuf */ 87*7c478bd9Sstevel@tonic-gate 88*7c478bd9Sstevel@tonic-gate int (*(split))(); 89*7c478bd9Sstevel@tonic-gate extern int scrwidth(wchar_t); 90*7c478bd9Sstevel@tonic-gate 91*7c478bd9Sstevel@tonic-gate static void fill_hdrbuf(wchar_t line[]); 92*7c478bd9Sstevel@tonic-gate static void header_chk(void); 93*7c478bd9Sstevel@tonic-gate static void process_hdrbuf(void); 94*7c478bd9Sstevel@tonic-gate 95*7c478bd9Sstevel@tonic-gate /* 96*7c478bd9Sstevel@tonic-gate * Drive the whole formatter by managing input files. Also, 97*7c478bd9Sstevel@tonic-gate * cause initialization of the output stuff and flush it out 98*7c478bd9Sstevel@tonic-gate * at the end. 99*7c478bd9Sstevel@tonic-gate */ 100*7c478bd9Sstevel@tonic-gate 101*7c478bd9Sstevel@tonic-gate main(int argc, char **argv) 102*7c478bd9Sstevel@tonic-gate { 103*7c478bd9Sstevel@tonic-gate register FILE *fi; 104*7c478bd9Sstevel@tonic-gate char sobuf[BUFSIZ]; 105*7c478bd9Sstevel@tonic-gate register char *cp; 106*7c478bd9Sstevel@tonic-gate int nofile; 107*7c478bd9Sstevel@tonic-gate char *locale; 108*7c478bd9Sstevel@tonic-gate int csplit(), msplit(); 109*7c478bd9Sstevel@tonic-gate void _wckind_init(); 110*7c478bd9Sstevel@tonic-gate 111*7c478bd9Sstevel@tonic-gate outp = NOSTR; 112*7c478bd9Sstevel@tonic-gate setbuf(stdout, sobuf); 113*7c478bd9Sstevel@tonic-gate setlocale(LC_ALL, ""); 114*7c478bd9Sstevel@tonic-gate locale = setlocale(LC_CTYPE, ""); 115*7c478bd9Sstevel@tonic-gate if (strcmp(locale, "C") == 0) { 116*7c478bd9Sstevel@tonic-gate split = csplit; 117*7c478bd9Sstevel@tonic-gate } else { 118*7c478bd9Sstevel@tonic-gate split = msplit; 119*7c478bd9Sstevel@tonic-gate (void) _wckind_init(); 120*7c478bd9Sstevel@tonic-gate } 121*7c478bd9Sstevel@tonic-gate if (argc < 2) { 122*7c478bd9Sstevel@tonic-gate single: 123*7c478bd9Sstevel@tonic-gate fmt(stdin); 124*7c478bd9Sstevel@tonic-gate oflush(); 125*7c478bd9Sstevel@tonic-gate exit(0); 126*7c478bd9Sstevel@tonic-gate } 127*7c478bd9Sstevel@tonic-gate nofile = 1; 128*7c478bd9Sstevel@tonic-gate while (--argc) { 129*7c478bd9Sstevel@tonic-gate cp = *++argv; 130*7c478bd9Sstevel@tonic-gate if (setopt(cp)) 131*7c478bd9Sstevel@tonic-gate continue; 132*7c478bd9Sstevel@tonic-gate nofile = 0; 133*7c478bd9Sstevel@tonic-gate if ((fi = fopen(cp, "r")) == NULL) { 134*7c478bd9Sstevel@tonic-gate perror(cp); 135*7c478bd9Sstevel@tonic-gate errs++; 136*7c478bd9Sstevel@tonic-gate continue; 137*7c478bd9Sstevel@tonic-gate } 138*7c478bd9Sstevel@tonic-gate fmt(fi); 139*7c478bd9Sstevel@tonic-gate fclose(fi); 140*7c478bd9Sstevel@tonic-gate } 141*7c478bd9Sstevel@tonic-gate if (nofile) 142*7c478bd9Sstevel@tonic-gate goto single; 143*7c478bd9Sstevel@tonic-gate oflush(); 144*7c478bd9Sstevel@tonic-gate exit(errs); 145*7c478bd9Sstevel@tonic-gate /* NOTREACHED */ 146*7c478bd9Sstevel@tonic-gate } 147*7c478bd9Sstevel@tonic-gate 148*7c478bd9Sstevel@tonic-gate /* 149*7c478bd9Sstevel@tonic-gate * Read up characters from the passed input file, forming lines, 150*7c478bd9Sstevel@tonic-gate * doing ^H processing, expanding tabs, stripping trailing blanks, 151*7c478bd9Sstevel@tonic-gate * and sending each line down for analysis. 152*7c478bd9Sstevel@tonic-gate */ 153*7c478bd9Sstevel@tonic-gate 154*7c478bd9Sstevel@tonic-gate fmt(FILE *fi) 155*7c478bd9Sstevel@tonic-gate { 156*7c478bd9Sstevel@tonic-gate wchar_t linebuf[BUFSIZ], canonb[BUFSIZ]; 157*7c478bd9Sstevel@tonic-gate register wchar_t *cp, *cp2; 158*7c478bd9Sstevel@tonic-gate register int col; 159*7c478bd9Sstevel@tonic-gate wchar_t c; 160*7c478bd9Sstevel@tonic-gate char cbuf[BUFSIZ]; /* stores wchar_t string as char string */ 161*7c478bd9Sstevel@tonic-gate 162*7c478bd9Sstevel@tonic-gate c = getwc(fi); 163*7c478bd9Sstevel@tonic-gate while (c != EOF) { 164*7c478bd9Sstevel@tonic-gate /* 165*7c478bd9Sstevel@tonic-gate * Collect a line, doing ^H processing. 166*7c478bd9Sstevel@tonic-gate * Leave tabs for now. 167*7c478bd9Sstevel@tonic-gate */ 168*7c478bd9Sstevel@tonic-gate 169*7c478bd9Sstevel@tonic-gate cp = linebuf; 170*7c478bd9Sstevel@tonic-gate while (c != L'\n' && c != EOF && cp-linebuf < BUFSIZ-1) { 171*7c478bd9Sstevel@tonic-gate if (c == L'\b') { 172*7c478bd9Sstevel@tonic-gate if (cp > linebuf) 173*7c478bd9Sstevel@tonic-gate cp--; 174*7c478bd9Sstevel@tonic-gate c = getwc(fi); 175*7c478bd9Sstevel@tonic-gate continue; 176*7c478bd9Sstevel@tonic-gate } 177*7c478bd9Sstevel@tonic-gate if (!(iswprint(c)) && c != L'\t') { 178*7c478bd9Sstevel@tonic-gate c = getwc(fi); 179*7c478bd9Sstevel@tonic-gate continue; 180*7c478bd9Sstevel@tonic-gate } 181*7c478bd9Sstevel@tonic-gate *cp++ = c; 182*7c478bd9Sstevel@tonic-gate c = getwc(fi); 183*7c478bd9Sstevel@tonic-gate } 184*7c478bd9Sstevel@tonic-gate *cp = L'\0'; 185*7c478bd9Sstevel@tonic-gate 186*7c478bd9Sstevel@tonic-gate /* 187*7c478bd9Sstevel@tonic-gate * Toss anything remaining on the input line. 188*7c478bd9Sstevel@tonic-gate */ 189*7c478bd9Sstevel@tonic-gate 190*7c478bd9Sstevel@tonic-gate while (c != L'\n' && c != EOF) 191*7c478bd9Sstevel@tonic-gate c = getwc(fi); 192*7c478bd9Sstevel@tonic-gate /* 193*7c478bd9Sstevel@tonic-gate * Expand tabs on the way to canonb. 194*7c478bd9Sstevel@tonic-gate */ 195*7c478bd9Sstevel@tonic-gate 196*7c478bd9Sstevel@tonic-gate col = 0; 197*7c478bd9Sstevel@tonic-gate cp = linebuf; 198*7c478bd9Sstevel@tonic-gate cp2 = canonb; 199*7c478bd9Sstevel@tonic-gate while (c = *cp++) { 200*7c478bd9Sstevel@tonic-gate if (c != L'\t') { 201*7c478bd9Sstevel@tonic-gate col += scrwidth(c); 202*7c478bd9Sstevel@tonic-gate if (cp2-canonb < BUFSIZ-1) 203*7c478bd9Sstevel@tonic-gate *cp2++ = c; 204*7c478bd9Sstevel@tonic-gate continue; 205*7c478bd9Sstevel@tonic-gate } 206*7c478bd9Sstevel@tonic-gate do { 207*7c478bd9Sstevel@tonic-gate if (cp2-canonb < BUFSIZ-1) 208*7c478bd9Sstevel@tonic-gate *cp2++ = L' '; 209*7c478bd9Sstevel@tonic-gate col++; 210*7c478bd9Sstevel@tonic-gate } while ((col & 07) != 0); 211*7c478bd9Sstevel@tonic-gate } 212*7c478bd9Sstevel@tonic-gate 213*7c478bd9Sstevel@tonic-gate /* 214*7c478bd9Sstevel@tonic-gate * Swipe trailing blanks from the line. 215*7c478bd9Sstevel@tonic-gate */ 216*7c478bd9Sstevel@tonic-gate 217*7c478bd9Sstevel@tonic-gate for (cp2--; cp2 >= canonb && *cp2 == L' '; cp2--); 218*7c478bd9Sstevel@tonic-gate *++cp2 = '\0'; 219*7c478bd9Sstevel@tonic-gate 220*7c478bd9Sstevel@tonic-gate /* special processing to look for mail header lines */ 221*7c478bd9Sstevel@tonic-gate switch (hdr_state) { 222*7c478bd9Sstevel@tonic-gate case off: 223*7c478bd9Sstevel@tonic-gate prefix(canonb); 224*7c478bd9Sstevel@tonic-gate case not_in_hdr: 225*7c478bd9Sstevel@tonic-gate /* look for an initial mail header line */ 226*7c478bd9Sstevel@tonic-gate /* skip initial blanks */ 227*7c478bd9Sstevel@tonic-gate for (cp = canonb; *cp == L' '; cp++); 228*7c478bd9Sstevel@tonic-gate /* 229*7c478bd9Sstevel@tonic-gate * Need to convert string from wchar_t to char, 230*7c478bd9Sstevel@tonic-gate * since this is what ishead() expects. Since we 231*7c478bd9Sstevel@tonic-gate * only want to make sure cp points to a "From" line 232*7c478bd9Sstevel@tonic-gate * of the email, we don't have to alloc 233*7c478bd9Sstevel@tonic-gate * BUFSIZ * MB_LEN_MAX to cbuf. 234*7c478bd9Sstevel@tonic-gate */ 235*7c478bd9Sstevel@tonic-gate wcstombs(cbuf, cp, (BUFSIZ - 1)); 236*7c478bd9Sstevel@tonic-gate if (ishead(cbuf)) { 237*7c478bd9Sstevel@tonic-gate hdr_state = in_hdr; 238*7c478bd9Sstevel@tonic-gate fill_hdrbuf(canonb); 239*7c478bd9Sstevel@tonic-gate } else { 240*7c478bd9Sstevel@tonic-gate /* no mail header line; process normally */ 241*7c478bd9Sstevel@tonic-gate prefix(canonb); 242*7c478bd9Sstevel@tonic-gate } 243*7c478bd9Sstevel@tonic-gate break; 244*7c478bd9Sstevel@tonic-gate case in_hdr: 245*7c478bd9Sstevel@tonic-gate /* already saw 1st mail header line; look for more */ 246*7c478bd9Sstevel@tonic-gate if (canonb[0] == L'\0') { 247*7c478bd9Sstevel@tonic-gate /* 248*7c478bd9Sstevel@tonic-gate * blank line means end of mail header; 249*7c478bd9Sstevel@tonic-gate * verify current mail header buffer 250*7c478bd9Sstevel@tonic-gate * then process it accordingly 251*7c478bd9Sstevel@tonic-gate */ 252*7c478bd9Sstevel@tonic-gate header_chk(); 253*7c478bd9Sstevel@tonic-gate process_hdrbuf(); 254*7c478bd9Sstevel@tonic-gate /* now process the current blank line */ 255*7c478bd9Sstevel@tonic-gate prefix(canonb); 256*7c478bd9Sstevel@tonic-gate } else 257*7c478bd9Sstevel@tonic-gate /* 258*7c478bd9Sstevel@tonic-gate * not a blank line--save this line as 259*7c478bd9Sstevel@tonic-gate * a potential mail header line 260*7c478bd9Sstevel@tonic-gate */ 261*7c478bd9Sstevel@tonic-gate fill_hdrbuf(canonb); 262*7c478bd9Sstevel@tonic-gate break; 263*7c478bd9Sstevel@tonic-gate } 264*7c478bd9Sstevel@tonic-gate if (c != EOF) 265*7c478bd9Sstevel@tonic-gate c = getwc(fi); 266*7c478bd9Sstevel@tonic-gate } 267*7c478bd9Sstevel@tonic-gate /* 268*7c478bd9Sstevel@tonic-gate * end of this file--make sure we process the stuff in 269*7c478bd9Sstevel@tonic-gate * hdrbuf before we're finished 270*7c478bd9Sstevel@tonic-gate */ 271*7c478bd9Sstevel@tonic-gate if (hdr_state == in_hdr) { 272*7c478bd9Sstevel@tonic-gate header_chk(); 273*7c478bd9Sstevel@tonic-gate process_hdrbuf(); 274*7c478bd9Sstevel@tonic-gate } 275*7c478bd9Sstevel@tonic-gate } 276*7c478bd9Sstevel@tonic-gate 277*7c478bd9Sstevel@tonic-gate /* 278*7c478bd9Sstevel@tonic-gate * Take a line devoid of tabs and other garbage and determine its 279*7c478bd9Sstevel@tonic-gate * blank prefix. If the indent changes, call for a linebreak. 280*7c478bd9Sstevel@tonic-gate * If the input line is blank, echo the blank line on the output. 281*7c478bd9Sstevel@tonic-gate * Finally, if the line minus the prefix is a mail header, try to keep 282*7c478bd9Sstevel@tonic-gate * it on a line by itself. 283*7c478bd9Sstevel@tonic-gate */ 284*7c478bd9Sstevel@tonic-gate 285*7c478bd9Sstevel@tonic-gate prefix(wchar_t line[]) 286*7c478bd9Sstevel@tonic-gate { 287*7c478bd9Sstevel@tonic-gate register wchar_t *cp; 288*7c478bd9Sstevel@tonic-gate register int np; 289*7c478bd9Sstevel@tonic-gate register int i; 290*7c478bd9Sstevel@tonic-gate int nosplit = 0; /* flag set if line should not be split */ 291*7c478bd9Sstevel@tonic-gate 292*7c478bd9Sstevel@tonic-gate if (line[0] == L'\0') { 293*7c478bd9Sstevel@tonic-gate oflush(); 294*7c478bd9Sstevel@tonic-gate putchar('\n'); 295*7c478bd9Sstevel@tonic-gate if (crown_state != c_none) 296*7c478bd9Sstevel@tonic-gate crown_state = c_reset; 297*7c478bd9Sstevel@tonic-gate return; 298*7c478bd9Sstevel@tonic-gate } 299*7c478bd9Sstevel@tonic-gate for (cp = line; *cp == L' '; cp++); 300*7c478bd9Sstevel@tonic-gate np = cp - line; 301*7c478bd9Sstevel@tonic-gate 302*7c478bd9Sstevel@tonic-gate /* 303*7c478bd9Sstevel@tonic-gate * The following horrible expression attempts to avoid linebreaks 304*7c478bd9Sstevel@tonic-gate * when the indent changes due to a paragraph. 305*7c478bd9Sstevel@tonic-gate */ 306*7c478bd9Sstevel@tonic-gate 307*7c478bd9Sstevel@tonic-gate if (crown_state == c_none && np != pfx && (np > pfx || abs(pfx-np) > 8)) 308*7c478bd9Sstevel@tonic-gate oflush(); 309*7c478bd9Sstevel@tonic-gate /* 310*7c478bd9Sstevel@tonic-gate * if this is a mail header line, don't split it; flush previous 311*7c478bd9Sstevel@tonic-gate * line, if any, so we don't join this line to it 312*7c478bd9Sstevel@tonic-gate */ 313*7c478bd9Sstevel@tonic-gate if (hdr_state == do_hdr) { 314*7c478bd9Sstevel@tonic-gate nosplit = 1; 315*7c478bd9Sstevel@tonic-gate oflush(); 316*7c478bd9Sstevel@tonic-gate } 317*7c478bd9Sstevel@tonic-gate /* flush previous line so we don't join this one to it */ 318*7c478bd9Sstevel@tonic-gate if (nojoin) 319*7c478bd9Sstevel@tonic-gate oflush(); 320*7c478bd9Sstevel@tonic-gate /* nroff-type lines starting with '.' are not split nor joined */ 321*7c478bd9Sstevel@tonic-gate if (!nosplit && (nosplit = (*cp == L'.'))) 322*7c478bd9Sstevel@tonic-gate oflush(); 323*7c478bd9Sstevel@tonic-gate pfx = np; 324*7c478bd9Sstevel@tonic-gate switch (crown_state) { 325*7c478bd9Sstevel@tonic-gate case c_reset: 326*7c478bd9Sstevel@tonic-gate crown_head = pfx; 327*7c478bd9Sstevel@tonic-gate crown_state = c_head; 328*7c478bd9Sstevel@tonic-gate break; 329*7c478bd9Sstevel@tonic-gate case c_lead: 330*7c478bd9Sstevel@tonic-gate crown_body = pfx; 331*7c478bd9Sstevel@tonic-gate crown_state = c_body; 332*7c478bd9Sstevel@tonic-gate break; 333*7c478bd9Sstevel@tonic-gate case c_fixup: 334*7c478bd9Sstevel@tonic-gate crown_body = pfx; 335*7c478bd9Sstevel@tonic-gate crown_state = c_body; 336*7c478bd9Sstevel@tonic-gate if (outp) { 337*7c478bd9Sstevel@tonic-gate wchar_t s[BUFSIZ]; 338*7c478bd9Sstevel@tonic-gate 339*7c478bd9Sstevel@tonic-gate *outp = L'\0'; 340*7c478bd9Sstevel@tonic-gate wscpy(s, &outbuf[crown_head]); 341*7c478bd9Sstevel@tonic-gate outp = NOSTR; 342*7c478bd9Sstevel@tonic-gate split(s); 343*7c478bd9Sstevel@tonic-gate } 344*7c478bd9Sstevel@tonic-gate break; 345*7c478bd9Sstevel@tonic-gate } 346*7c478bd9Sstevel@tonic-gate if (nosplit) { 347*7c478bd9Sstevel@tonic-gate /* put whole input line onto outbuf and print it out */ 348*7c478bd9Sstevel@tonic-gate pack(cp); 349*7c478bd9Sstevel@tonic-gate oflush(); 350*7c478bd9Sstevel@tonic-gate } else 351*7c478bd9Sstevel@tonic-gate /* 352*7c478bd9Sstevel@tonic-gate * split puts current line onto outbuf, but splits it 353*7c478bd9Sstevel@tonic-gate * at word boundaries, if it exceeds desired length 354*7c478bd9Sstevel@tonic-gate */ 355*7c478bd9Sstevel@tonic-gate split(cp); 356*7c478bd9Sstevel@tonic-gate if (nojoin) 357*7c478bd9Sstevel@tonic-gate /* 358*7c478bd9Sstevel@tonic-gate * flush current line so next lines, if any, 359*7c478bd9Sstevel@tonic-gate * won't join to this one 360*7c478bd9Sstevel@tonic-gate */ 361*7c478bd9Sstevel@tonic-gate oflush(); 362*7c478bd9Sstevel@tonic-gate } 363*7c478bd9Sstevel@tonic-gate 364*7c478bd9Sstevel@tonic-gate /* 365*7c478bd9Sstevel@tonic-gate * Split up the passed line into output "words" which are 366*7c478bd9Sstevel@tonic-gate * maximal strings of non-blanks with the blank separation 367*7c478bd9Sstevel@tonic-gate * attached at the end. Pass these words along to the output 368*7c478bd9Sstevel@tonic-gate * line packer. 369*7c478bd9Sstevel@tonic-gate */ 370*7c478bd9Sstevel@tonic-gate 371*7c478bd9Sstevel@tonic-gate csplit(wchar_t line[]) 372*7c478bd9Sstevel@tonic-gate { 373*7c478bd9Sstevel@tonic-gate register wchar_t *cp, *cp2; 374*7c478bd9Sstevel@tonic-gate wchar_t word[BUFSIZ]; 375*7c478bd9Sstevel@tonic-gate static const wchar_t *srchlist = (const wchar_t *) L".:!?"; 376*7c478bd9Sstevel@tonic-gate 377*7c478bd9Sstevel@tonic-gate cp = line; 378*7c478bd9Sstevel@tonic-gate while (*cp) { 379*7c478bd9Sstevel@tonic-gate cp2 = word; 380*7c478bd9Sstevel@tonic-gate 381*7c478bd9Sstevel@tonic-gate /* 382*7c478bd9Sstevel@tonic-gate * Collect a 'word,' allowing it to contain escaped 383*7c478bd9Sstevel@tonic-gate * white space. 384*7c478bd9Sstevel@tonic-gate */ 385*7c478bd9Sstevel@tonic-gate 386*7c478bd9Sstevel@tonic-gate while (*cp && !(iswspace(*cp))) { 387*7c478bd9Sstevel@tonic-gate if (*cp == '\\' && iswspace(cp[1])) 388*7c478bd9Sstevel@tonic-gate *cp2++ = *cp++; 389*7c478bd9Sstevel@tonic-gate *cp2++ = *cp++; 390*7c478bd9Sstevel@tonic-gate } 391*7c478bd9Sstevel@tonic-gate 392*7c478bd9Sstevel@tonic-gate /* 393*7c478bd9Sstevel@tonic-gate * Guarantee a space at end of line. 394*7c478bd9Sstevel@tonic-gate * Two spaces after end of sentence punctuation. 395*7c478bd9Sstevel@tonic-gate */ 396*7c478bd9Sstevel@tonic-gate 397*7c478bd9Sstevel@tonic-gate if (*cp == L'\0') { 398*7c478bd9Sstevel@tonic-gate *cp2++ = L' '; 399*7c478bd9Sstevel@tonic-gate if (wschr(srchlist, cp[-1]) != NULL) 400*7c478bd9Sstevel@tonic-gate *cp2++ = L' '; 401*7c478bd9Sstevel@tonic-gate } 402*7c478bd9Sstevel@tonic-gate while (iswspace(*cp)) 403*7c478bd9Sstevel@tonic-gate *cp2++ = *cp++; 404*7c478bd9Sstevel@tonic-gate *cp2 = L'\0'; 405*7c478bd9Sstevel@tonic-gate pack(word); 406*7c478bd9Sstevel@tonic-gate } 407*7c478bd9Sstevel@tonic-gate } 408*7c478bd9Sstevel@tonic-gate 409*7c478bd9Sstevel@tonic-gate msplit(wchar_t line[]) 410*7c478bd9Sstevel@tonic-gate { 411*7c478bd9Sstevel@tonic-gate register wchar_t *cp, *cp2, prev; 412*7c478bd9Sstevel@tonic-gate wchar_t word[BUFSIZ]; 413*7c478bd9Sstevel@tonic-gate static const wchar_t *srchlist = (const wchar_t *) L".:!?"; 414*7c478bd9Sstevel@tonic-gate 415*7c478bd9Sstevel@tonic-gate cp = line; 416*7c478bd9Sstevel@tonic-gate while (*cp) { 417*7c478bd9Sstevel@tonic-gate cp2 = word; 418*7c478bd9Sstevel@tonic-gate prev = *cp; 419*7c478bd9Sstevel@tonic-gate 420*7c478bd9Sstevel@tonic-gate /* 421*7c478bd9Sstevel@tonic-gate * Collect a 'word,' allowing it to contain escaped 422*7c478bd9Sstevel@tonic-gate * white space. 423*7c478bd9Sstevel@tonic-gate */ 424*7c478bd9Sstevel@tonic-gate 425*7c478bd9Sstevel@tonic-gate while (*cp) { 426*7c478bd9Sstevel@tonic-gate if (iswspace(*cp)) 427*7c478bd9Sstevel@tonic-gate break; 428*7c478bd9Sstevel@tonic-gate if (_wckind(*cp) != _wckind(prev)) 429*7c478bd9Sstevel@tonic-gate if (wcsetno(*cp) != 0 || wcsetno(prev) != 0) 430*7c478bd9Sstevel@tonic-gate break; 431*7c478bd9Sstevel@tonic-gate if (*cp == '\\' && iswspace(cp[1])) 432*7c478bd9Sstevel@tonic-gate *cp2++ = *cp++; 433*7c478bd9Sstevel@tonic-gate prev = *cp; 434*7c478bd9Sstevel@tonic-gate *cp2++ = *cp++; 435*7c478bd9Sstevel@tonic-gate } 436*7c478bd9Sstevel@tonic-gate 437*7c478bd9Sstevel@tonic-gate /* 438*7c478bd9Sstevel@tonic-gate * Guarantee a space at end of line. 439*7c478bd9Sstevel@tonic-gate * Two spaces after end of sentence punctuation. 440*7c478bd9Sstevel@tonic-gate */ 441*7c478bd9Sstevel@tonic-gate 442*7c478bd9Sstevel@tonic-gate if (*cp == L'\0') { 443*7c478bd9Sstevel@tonic-gate *cp2++ = L' '; 444*7c478bd9Sstevel@tonic-gate if (wschr(srchlist, cp[-1]) != NULL) 445*7c478bd9Sstevel@tonic-gate *cp2++ = L' '; 446*7c478bd9Sstevel@tonic-gate } 447*7c478bd9Sstevel@tonic-gate while (iswspace(*cp)) 448*7c478bd9Sstevel@tonic-gate *cp2++ = *cp++; 449*7c478bd9Sstevel@tonic-gate *cp2 = L'\0'; 450*7c478bd9Sstevel@tonic-gate pack(word); 451*7c478bd9Sstevel@tonic-gate } 452*7c478bd9Sstevel@tonic-gate } 453*7c478bd9Sstevel@tonic-gate 454*7c478bd9Sstevel@tonic-gate /* 455*7c478bd9Sstevel@tonic-gate * Output section. 456*7c478bd9Sstevel@tonic-gate * Build up line images from the words passed in. Prefix 457*7c478bd9Sstevel@tonic-gate * each line with correct number of blanks. The buffer "outbuf" 458*7c478bd9Sstevel@tonic-gate * contains the current partial line image, including prefixed blanks. 459*7c478bd9Sstevel@tonic-gate * "outp" points to the next available space therein. When outp is NOSTR, 460*7c478bd9Sstevel@tonic-gate * there ain't nothing in there yet. At the bottom of this whole mess, 461*7c478bd9Sstevel@tonic-gate * leading tabs are reinserted. 462*7c478bd9Sstevel@tonic-gate */ 463*7c478bd9Sstevel@tonic-gate 464*7c478bd9Sstevel@tonic-gate /* 465*7c478bd9Sstevel@tonic-gate * Pack a word onto the output line. If this is the beginning of 466*7c478bd9Sstevel@tonic-gate * the line, push on the appropriately-sized string of blanks first. 467*7c478bd9Sstevel@tonic-gate * If the word won't fit on the current line, flush and begin a new 468*7c478bd9Sstevel@tonic-gate * line. If the word is too long to fit all by itself on a line, 469*7c478bd9Sstevel@tonic-gate * just give it its own and hope for the best. 470*7c478bd9Sstevel@tonic-gate */ 471*7c478bd9Sstevel@tonic-gate 472*7c478bd9Sstevel@tonic-gate pack(wchar_t word[]) 473*7c478bd9Sstevel@tonic-gate { 474*7c478bd9Sstevel@tonic-gate register wchar_t *cp; 475*7c478bd9Sstevel@tonic-gate register int s, t; 476*7c478bd9Sstevel@tonic-gate 477*7c478bd9Sstevel@tonic-gate if (outp == NOSTR) 478*7c478bd9Sstevel@tonic-gate leadin(); 479*7c478bd9Sstevel@tonic-gate t = wscol(word); 480*7c478bd9Sstevel@tonic-gate *outp = L'\0'; 481*7c478bd9Sstevel@tonic-gate s = wscol(outbuf); 482*7c478bd9Sstevel@tonic-gate if (t+s <= width) { 483*7c478bd9Sstevel@tonic-gate for (cp = word; *cp; *outp++ = *cp++); 484*7c478bd9Sstevel@tonic-gate return; 485*7c478bd9Sstevel@tonic-gate } 486*7c478bd9Sstevel@tonic-gate if (s > filler) { 487*7c478bd9Sstevel@tonic-gate oflush(); 488*7c478bd9Sstevel@tonic-gate leadin(); 489*7c478bd9Sstevel@tonic-gate } 490*7c478bd9Sstevel@tonic-gate for (cp = word; *cp; *outp++ = *cp++); 491*7c478bd9Sstevel@tonic-gate } 492*7c478bd9Sstevel@tonic-gate 493*7c478bd9Sstevel@tonic-gate /* 494*7c478bd9Sstevel@tonic-gate * If there is anything on the current output line, send it on 495*7c478bd9Sstevel@tonic-gate * its way. Set outp to NOSTR to indicate the absence of the current 496*7c478bd9Sstevel@tonic-gate * line prefix. 497*7c478bd9Sstevel@tonic-gate */ 498*7c478bd9Sstevel@tonic-gate 499*7c478bd9Sstevel@tonic-gate oflush(void) 500*7c478bd9Sstevel@tonic-gate { 501*7c478bd9Sstevel@tonic-gate if (outp == NOSTR) 502*7c478bd9Sstevel@tonic-gate return; 503*7c478bd9Sstevel@tonic-gate *outp = L'\0'; 504*7c478bd9Sstevel@tonic-gate tabulate(outbuf); 505*7c478bd9Sstevel@tonic-gate outp = NOSTR; 506*7c478bd9Sstevel@tonic-gate } 507*7c478bd9Sstevel@tonic-gate 508*7c478bd9Sstevel@tonic-gate /* 509*7c478bd9Sstevel@tonic-gate * Take the passed line buffer, insert leading tabs where possible, and 510*7c478bd9Sstevel@tonic-gate * output on standard output (finally). 511*7c478bd9Sstevel@tonic-gate */ 512*7c478bd9Sstevel@tonic-gate 513*7c478bd9Sstevel@tonic-gate tabulate(wchar_t line[]) 514*7c478bd9Sstevel@tonic-gate { 515*7c478bd9Sstevel@tonic-gate register wchar_t *cp, *cp2; 516*7c478bd9Sstevel@tonic-gate register int b, t; 517*7c478bd9Sstevel@tonic-gate 518*7c478bd9Sstevel@tonic-gate 519*7c478bd9Sstevel@tonic-gate /* Toss trailing blanks in the output line */ 520*7c478bd9Sstevel@tonic-gate cp = line + wslen(line) - 1; 521*7c478bd9Sstevel@tonic-gate while (cp >= line && *cp == L' ') 522*7c478bd9Sstevel@tonic-gate cp--; 523*7c478bd9Sstevel@tonic-gate *++cp = L'\0'; 524*7c478bd9Sstevel@tonic-gate /* Count the leading blank space and tabulate */ 525*7c478bd9Sstevel@tonic-gate for (cp = line; *cp == L' '; cp++); 526*7c478bd9Sstevel@tonic-gate b = cp - line; 527*7c478bd9Sstevel@tonic-gate t = b >> 3; 528*7c478bd9Sstevel@tonic-gate b &= 07; 529*7c478bd9Sstevel@tonic-gate if (t > 0) 530*7c478bd9Sstevel@tonic-gate do 531*7c478bd9Sstevel@tonic-gate putc('\t', stdout); 532*7c478bd9Sstevel@tonic-gate while (--t); 533*7c478bd9Sstevel@tonic-gate if (b > 0) 534*7c478bd9Sstevel@tonic-gate do 535*7c478bd9Sstevel@tonic-gate putc(' ', stdout); 536*7c478bd9Sstevel@tonic-gate while (--b); 537*7c478bd9Sstevel@tonic-gate while (*cp) 538*7c478bd9Sstevel@tonic-gate putwc(*cp++, stdout); 539*7c478bd9Sstevel@tonic-gate putc('\n', stdout); 540*7c478bd9Sstevel@tonic-gate } 541*7c478bd9Sstevel@tonic-gate 542*7c478bd9Sstevel@tonic-gate /* 543*7c478bd9Sstevel@tonic-gate * Initialize the output line with the appropriate number of 544*7c478bd9Sstevel@tonic-gate * leading blanks. 545*7c478bd9Sstevel@tonic-gate */ 546*7c478bd9Sstevel@tonic-gate 547*7c478bd9Sstevel@tonic-gate leadin() 548*7c478bd9Sstevel@tonic-gate { 549*7c478bd9Sstevel@tonic-gate register int b; 550*7c478bd9Sstevel@tonic-gate register wchar_t *cp; 551*7c478bd9Sstevel@tonic-gate register int l; 552*7c478bd9Sstevel@tonic-gate 553*7c478bd9Sstevel@tonic-gate switch (crown_state) { 554*7c478bd9Sstevel@tonic-gate case c_head: 555*7c478bd9Sstevel@tonic-gate l = crown_head; 556*7c478bd9Sstevel@tonic-gate crown_state = c_lead; 557*7c478bd9Sstevel@tonic-gate break; 558*7c478bd9Sstevel@tonic-gate 559*7c478bd9Sstevel@tonic-gate case c_lead: 560*7c478bd9Sstevel@tonic-gate case c_fixup: 561*7c478bd9Sstevel@tonic-gate l = crown_head; 562*7c478bd9Sstevel@tonic-gate crown_state = c_fixup; 563*7c478bd9Sstevel@tonic-gate break; 564*7c478bd9Sstevel@tonic-gate 565*7c478bd9Sstevel@tonic-gate case c_body: 566*7c478bd9Sstevel@tonic-gate l = crown_body; 567*7c478bd9Sstevel@tonic-gate break; 568*7c478bd9Sstevel@tonic-gate 569*7c478bd9Sstevel@tonic-gate default: 570*7c478bd9Sstevel@tonic-gate l = pfx; 571*7c478bd9Sstevel@tonic-gate break; 572*7c478bd9Sstevel@tonic-gate } 573*7c478bd9Sstevel@tonic-gate filler = l; 574*7c478bd9Sstevel@tonic-gate for (b = 0, cp = outbuf; b < l; b++) 575*7c478bd9Sstevel@tonic-gate *cp++ = L' '; 576*7c478bd9Sstevel@tonic-gate outp = cp; 577*7c478bd9Sstevel@tonic-gate } 578*7c478bd9Sstevel@tonic-gate 579*7c478bd9Sstevel@tonic-gate /* 580*7c478bd9Sstevel@tonic-gate * Is s1 a prefix of s2?? 581*7c478bd9Sstevel@tonic-gate */ 582*7c478bd9Sstevel@tonic-gate 583*7c478bd9Sstevel@tonic-gate ispref(wchar_t *s1, wchar_t *s2) 584*7c478bd9Sstevel@tonic-gate { 585*7c478bd9Sstevel@tonic-gate 586*7c478bd9Sstevel@tonic-gate while (*s1 != L'\0' && *s2 != L'\0') 587*7c478bd9Sstevel@tonic-gate if (*s1++ != *s2++) 588*7c478bd9Sstevel@tonic-gate return (0); 589*7c478bd9Sstevel@tonic-gate return (1); 590*7c478bd9Sstevel@tonic-gate } 591*7c478bd9Sstevel@tonic-gate 592*7c478bd9Sstevel@tonic-gate /* 593*7c478bd9Sstevel@tonic-gate * Set an input option 594*7c478bd9Sstevel@tonic-gate */ 595*7c478bd9Sstevel@tonic-gate 596*7c478bd9Sstevel@tonic-gate setopt(cp) 597*7c478bd9Sstevel@tonic-gate register char *cp; 598*7c478bd9Sstevel@tonic-gate { 599*7c478bd9Sstevel@tonic-gate static int ws = 0; 600*7c478bd9Sstevel@tonic-gate 601*7c478bd9Sstevel@tonic-gate if (*cp == '-') { 602*7c478bd9Sstevel@tonic-gate if (cp[1] == 'c' && cp[2] == '\0') { 603*7c478bd9Sstevel@tonic-gate crown_state = c_reset; 604*7c478bd9Sstevel@tonic-gate return (1); 605*7c478bd9Sstevel@tonic-gate } 606*7c478bd9Sstevel@tonic-gate if (cp[1] == 's' && cp[2] == '\0') { 607*7c478bd9Sstevel@tonic-gate nojoin = 1; 608*7c478bd9Sstevel@tonic-gate return (1); 609*7c478bd9Sstevel@tonic-gate } 610*7c478bd9Sstevel@tonic-gate if (cp[1] == 'w' && cp[2] == '\0') { 611*7c478bd9Sstevel@tonic-gate ws++; 612*7c478bd9Sstevel@tonic-gate return (1); 613*7c478bd9Sstevel@tonic-gate } 614*7c478bd9Sstevel@tonic-gate width = atoi(cp+1); 615*7c478bd9Sstevel@tonic-gate } else if (ws) { 616*7c478bd9Sstevel@tonic-gate width = atoi(cp); 617*7c478bd9Sstevel@tonic-gate ws = 0; 618*7c478bd9Sstevel@tonic-gate } else 619*7c478bd9Sstevel@tonic-gate return (0); 620*7c478bd9Sstevel@tonic-gate if (width <= 0 || width >= BUFSIZ-2) { 621*7c478bd9Sstevel@tonic-gate fprintf(stderr, "fmt: bad width: %d\n", width); 622*7c478bd9Sstevel@tonic-gate exit(1); 623*7c478bd9Sstevel@tonic-gate } 624*7c478bd9Sstevel@tonic-gate return (1); 625*7c478bd9Sstevel@tonic-gate } 626*7c478bd9Sstevel@tonic-gate 627*7c478bd9Sstevel@tonic-gate 628*7c478bd9Sstevel@tonic-gate #define LIB_WDRESOLVE "/usr/lib/locale/%s/LC_CTYPE/wdresolve.so" 629*7c478bd9Sstevel@tonic-gate #define WCHKIND "_wdchkind_" 630*7c478bd9Sstevel@tonic-gate 631*7c478bd9Sstevel@tonic-gate static int _wckind_c_locale(); 632*7c478bd9Sstevel@tonic-gate 633*7c478bd9Sstevel@tonic-gate static int (*__wckind)() = _wckind_c_locale; 634*7c478bd9Sstevel@tonic-gate static void *dlhandle = NULL; 635*7c478bd9Sstevel@tonic-gate 636*7c478bd9Sstevel@tonic-gate 637*7c478bd9Sstevel@tonic-gate void 638*7c478bd9Sstevel@tonic-gate _wckind_init() 639*7c478bd9Sstevel@tonic-gate { 640*7c478bd9Sstevel@tonic-gate char *locale; 641*7c478bd9Sstevel@tonic-gate char path[MAXPATHLEN + 1]; 642*7c478bd9Sstevel@tonic-gate 643*7c478bd9Sstevel@tonic-gate 644*7c478bd9Sstevel@tonic-gate if (dlhandle != NULL) { 645*7c478bd9Sstevel@tonic-gate (void) dlclose(dlhandle); 646*7c478bd9Sstevel@tonic-gate dlhandle = NULL; 647*7c478bd9Sstevel@tonic-gate } 648*7c478bd9Sstevel@tonic-gate 649*7c478bd9Sstevel@tonic-gate locale = setlocale(LC_CTYPE, NULL); 650*7c478bd9Sstevel@tonic-gate if (strcmp(locale, "C") == 0) 651*7c478bd9Sstevel@tonic-gate goto c_locale; 652*7c478bd9Sstevel@tonic-gate 653*7c478bd9Sstevel@tonic-gate (void) sprintf(path, LIB_WDRESOLVE, locale); 654*7c478bd9Sstevel@tonic-gate 655*7c478bd9Sstevel@tonic-gate if ((dlhandle = dlopen(path, RTLD_LAZY)) != NULL) { 656*7c478bd9Sstevel@tonic-gate __wckind = (int (*)(int))dlsym(dlhandle, WCHKIND); 657*7c478bd9Sstevel@tonic-gate if (__wckind != NULL) 658*7c478bd9Sstevel@tonic-gate return; 659*7c478bd9Sstevel@tonic-gate (void) dlclose(dlhandle); 660*7c478bd9Sstevel@tonic-gate dlhandle = NULL; 661*7c478bd9Sstevel@tonic-gate } 662*7c478bd9Sstevel@tonic-gate 663*7c478bd9Sstevel@tonic-gate c_locale: 664*7c478bd9Sstevel@tonic-gate __wckind = _wckind_c_locale; 665*7c478bd9Sstevel@tonic-gate } 666*7c478bd9Sstevel@tonic-gate 667*7c478bd9Sstevel@tonic-gate 668*7c478bd9Sstevel@tonic-gate int 669*7c478bd9Sstevel@tonic-gate _wckind(wc) 670*7c478bd9Sstevel@tonic-gate wchar_t wc; 671*7c478bd9Sstevel@tonic-gate { 672*7c478bd9Sstevel@tonic-gate return (*__wckind) (wc); 673*7c478bd9Sstevel@tonic-gate } 674*7c478bd9Sstevel@tonic-gate 675*7c478bd9Sstevel@tonic-gate 676*7c478bd9Sstevel@tonic-gate static int 677*7c478bd9Sstevel@tonic-gate _wckind_c_locale(wc) 678*7c478bd9Sstevel@tonic-gate wchar_t wc; 679*7c478bd9Sstevel@tonic-gate { 680*7c478bd9Sstevel@tonic-gate int ret; 681*7c478bd9Sstevel@tonic-gate 682*7c478bd9Sstevel@tonic-gate /* 683*7c478bd9Sstevel@tonic-gate * DEPEND_ON_ANSIC: L notion for the character is new in 684*7c478bd9Sstevel@tonic-gate * ANSI-C, k&r compiler won't work. 685*7c478bd9Sstevel@tonic-gate */ 686*7c478bd9Sstevel@tonic-gate if (iswascii(wc)) 687*7c478bd9Sstevel@tonic-gate ret = (iswalnum(wc) || wc == L'_') ? 0 : 1; 688*7c478bd9Sstevel@tonic-gate else 689*7c478bd9Sstevel@tonic-gate ret = wcsetno(wc) + 1; 690*7c478bd9Sstevel@tonic-gate 691*7c478bd9Sstevel@tonic-gate return (ret); 692*7c478bd9Sstevel@tonic-gate } 693*7c478bd9Sstevel@tonic-gate 694*7c478bd9Sstevel@tonic-gate /* 695*7c478bd9Sstevel@tonic-gate * header_chk - 696*7c478bd9Sstevel@tonic-gate * Called when done looking for a set mail header lines. 697*7c478bd9Sstevel@tonic-gate * Either a blank line was seen, or EOF was reached. 698*7c478bd9Sstevel@tonic-gate * 699*7c478bd9Sstevel@tonic-gate * Verifies if current hdrbuf of potential mail header lines 700*7c478bd9Sstevel@tonic-gate * is really a mail header. A mail header must be at least 2 701*7c478bd9Sstevel@tonic-gate * lines and more than half of them must start with one of the 702*7c478bd9Sstevel@tonic-gate * known mail header strings in headnames. 703*7c478bd9Sstevel@tonic-gate * 704*7c478bd9Sstevel@tonic-gate * header_chk sets hdr_state to do_hdr if hdrbuf contained a valid 705*7c478bd9Sstevel@tonic-gate * mail header. Otherwise, it sets hdr_state to flush_hdr. 706*7c478bd9Sstevel@tonic-gate * 707*7c478bd9Sstevel@tonic-gate * h_lines = hdrbuf index for next line to be saved; 708*7c478bd9Sstevel@tonic-gate * also indicates current # of lines in potential header 709*7c478bd9Sstevel@tonic-gate */ 710*7c478bd9Sstevel@tonic-gate static void 711*7c478bd9Sstevel@tonic-gate header_chk(void) 712*7c478bd9Sstevel@tonic-gate { 713*7c478bd9Sstevel@tonic-gate wchar_t *cp; /* ptr to current char of line */ 714*7c478bd9Sstevel@tonic-gate wchar_t **hp; /* ptr to current char of a valid */ 715*7c478bd9Sstevel@tonic-gate /* mail header string */ 716*7c478bd9Sstevel@tonic-gate int l; /* index */ 717*7c478bd9Sstevel@tonic-gate /* 718*7c478bd9Sstevel@tonic-gate * number of lines in hdrbuf that look 719*7c478bd9Sstevel@tonic-gate * like mail header lines (start with 720*7c478bd9Sstevel@tonic-gate * a known mail header prefix) 721*7c478bd9Sstevel@tonic-gate */ 722*7c478bd9Sstevel@tonic-gate int hdrcount = 0; 723*7c478bd9Sstevel@tonic-gate /* header must have at least 2 lines (h_lines > 1) */ 724*7c478bd9Sstevel@tonic-gate if (h_lines < 2) { 725*7c478bd9Sstevel@tonic-gate hdr_state = flush_hdr; 726*7c478bd9Sstevel@tonic-gate return; 727*7c478bd9Sstevel@tonic-gate } 728*7c478bd9Sstevel@tonic-gate /* 729*7c478bd9Sstevel@tonic-gate * go through each line in hdrbuf and see how many 730*7c478bd9Sstevel@tonic-gate * look like mail header lines 731*7c478bd9Sstevel@tonic-gate */ 732*7c478bd9Sstevel@tonic-gate for (l = 0; l < h_lines; l++) { 733*7c478bd9Sstevel@tonic-gate /* skip initial blanks */ 734*7c478bd9Sstevel@tonic-gate for (cp = hdrbuf[l]; *cp == L' '; cp++); 735*7c478bd9Sstevel@tonic-gate for (hp = &headnames[0]; *hp != (wchar_t *) 0; hp++) 736*7c478bd9Sstevel@tonic-gate if (ispref(*hp, cp)) { 737*7c478bd9Sstevel@tonic-gate hdrcount++; 738*7c478bd9Sstevel@tonic-gate break; 739*7c478bd9Sstevel@tonic-gate } 740*7c478bd9Sstevel@tonic-gate } 741*7c478bd9Sstevel@tonic-gate /* 742*7c478bd9Sstevel@tonic-gate * if over half match, we'll assume this is a header; 743*7c478bd9Sstevel@tonic-gate * set hdr_state to indicate whether to treat 744*7c478bd9Sstevel@tonic-gate * these lines as mail header (do_hdr) or not (flush_hdr) 745*7c478bd9Sstevel@tonic-gate */ 746*7c478bd9Sstevel@tonic-gate if (hdrcount > h_lines / 2) 747*7c478bd9Sstevel@tonic-gate hdr_state = do_hdr; 748*7c478bd9Sstevel@tonic-gate else 749*7c478bd9Sstevel@tonic-gate hdr_state = flush_hdr; 750*7c478bd9Sstevel@tonic-gate } 751*7c478bd9Sstevel@tonic-gate 752*7c478bd9Sstevel@tonic-gate /* 753*7c478bd9Sstevel@tonic-gate * fill_hdrbuf - 754*7c478bd9Sstevel@tonic-gate * Save given input line into next element of hdrbuf, 755*7c478bd9Sstevel@tonic-gate * as a potential mail header line, to be processed later 756*7c478bd9Sstevel@tonic-gate * once we decide whether or not the contents of hdrbuf is 757*7c478bd9Sstevel@tonic-gate * really a mail header, via header_chk(). 758*7c478bd9Sstevel@tonic-gate * 759*7c478bd9Sstevel@tonic-gate * Does not allow hdrbuf to exceed MAXLINES lines. 760*7c478bd9Sstevel@tonic-gate * Dynamically allocates space for each line. If we are unable 761*7c478bd9Sstevel@tonic-gate * to allocate space for the current string, stop special mail 762*7c478bd9Sstevel@tonic-gate * header preservation at this point and continue formatting 763*7c478bd9Sstevel@tonic-gate * without it. 764*7c478bd9Sstevel@tonic-gate */ 765*7c478bd9Sstevel@tonic-gate static void 766*7c478bd9Sstevel@tonic-gate fill_hdrbuf(wchar_t line[]) 767*7c478bd9Sstevel@tonic-gate { 768*7c478bd9Sstevel@tonic-gate wchar_t *cp; /* pointer to characters in input line */ 769*7c478bd9Sstevel@tonic-gate int i; /* index into characters a hdrbuf line */ 770*7c478bd9Sstevel@tonic-gate 771*7c478bd9Sstevel@tonic-gate if (h_lines >= MAXLINES) { 772*7c478bd9Sstevel@tonic-gate /* 773*7c478bd9Sstevel@tonic-gate * if we run over MAXLINES potential mail header 774*7c478bd9Sstevel@tonic-gate * lines, stop checking--this is most likely NOT a 775*7c478bd9Sstevel@tonic-gate * mail header; flush out the hdrbuf, then process 776*7c478bd9Sstevel@tonic-gate * the current 'line' normally. 777*7c478bd9Sstevel@tonic-gate */ 778*7c478bd9Sstevel@tonic-gate hdr_state = flush_hdr; 779*7c478bd9Sstevel@tonic-gate process_hdrbuf(); 780*7c478bd9Sstevel@tonic-gate prefix(line); 781*7c478bd9Sstevel@tonic-gate return; 782*7c478bd9Sstevel@tonic-gate } 783*7c478bd9Sstevel@tonic-gate hdrbuf[h_lines] = (wchar_t *)malloc(sizeof (wchar_t) * 784*7c478bd9Sstevel@tonic-gate (wslen(line) + 1)); 785*7c478bd9Sstevel@tonic-gate if (hdrbuf[h_lines] == NULL) { 786*7c478bd9Sstevel@tonic-gate perror("malloc"); 787*7c478bd9Sstevel@tonic-gate fprintf(stderr, "fmt: unable to do mail header preservation\n"); 788*7c478bd9Sstevel@tonic-gate errs++; 789*7c478bd9Sstevel@tonic-gate /* 790*7c478bd9Sstevel@tonic-gate * Can't process mail header; flush current contents 791*7c478bd9Sstevel@tonic-gate * of mail header and continue with no more mail 792*7c478bd9Sstevel@tonic-gate * header processing 793*7c478bd9Sstevel@tonic-gate */ 794*7c478bd9Sstevel@tonic-gate if (h_lines == 0) 795*7c478bd9Sstevel@tonic-gate /* hdrbuf is empty; process this line normally */ 796*7c478bd9Sstevel@tonic-gate prefix(line); 797*7c478bd9Sstevel@tonic-gate else { 798*7c478bd9Sstevel@tonic-gate hdr_state = flush_hdr; 799*7c478bd9Sstevel@tonic-gate for (i = 0; i < h_lines; i++) { 800*7c478bd9Sstevel@tonic-gate prefix(hdrbuf[i]); 801*7c478bd9Sstevel@tonic-gate free(hdrbuf[i]); 802*7c478bd9Sstevel@tonic-gate } 803*7c478bd9Sstevel@tonic-gate h_lines = 0; 804*7c478bd9Sstevel@tonic-gate } 805*7c478bd9Sstevel@tonic-gate hdr_state = off; 806*7c478bd9Sstevel@tonic-gate return; 807*7c478bd9Sstevel@tonic-gate } 808*7c478bd9Sstevel@tonic-gate /* save this line as a potential mail header line */ 809*7c478bd9Sstevel@tonic-gate for (i = 0, cp = line; (hdrbuf[h_lines][i] = *cp) != L'\0'; i++, cp++); 810*7c478bd9Sstevel@tonic-gate h_lines++; 811*7c478bd9Sstevel@tonic-gate } 812*7c478bd9Sstevel@tonic-gate 813*7c478bd9Sstevel@tonic-gate /* 814*7c478bd9Sstevel@tonic-gate * process_hdrbuf - 815*7c478bd9Sstevel@tonic-gate * Outputs the lines currently stored in hdrbuf, according 816*7c478bd9Sstevel@tonic-gate * to the current hdr_state value, assumed to be either do_hdr 817*7c478bd9Sstevel@tonic-gate * or flush_hdr. 818*7c478bd9Sstevel@tonic-gate * This should be called after doing a header_chk() to verify 819*7c478bd9Sstevel@tonic-gate * the hdrbuf and set the hdr_state flag. 820*7c478bd9Sstevel@tonic-gate */ 821*7c478bd9Sstevel@tonic-gate static void 822*7c478bd9Sstevel@tonic-gate process_hdrbuf(void) 823*7c478bd9Sstevel@tonic-gate { 824*7c478bd9Sstevel@tonic-gate int i; 825*7c478bd9Sstevel@tonic-gate 826*7c478bd9Sstevel@tonic-gate for (i = 0; i < h_lines; i++) { 827*7c478bd9Sstevel@tonic-gate prefix(hdrbuf[i]); 828*7c478bd9Sstevel@tonic-gate free(hdrbuf[i]); 829*7c478bd9Sstevel@tonic-gate } 830*7c478bd9Sstevel@tonic-gate hdr_state = not_in_hdr; 831*7c478bd9Sstevel@tonic-gate h_lines = 0; 832*7c478bd9Sstevel@tonic-gate } 833