17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
22*779fc935Sceastha /*
23*779fc935Sceastha * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24*779fc935Sceastha * Use is subject to license terms.
25*779fc935Sceastha */
26*779fc935Sceastha
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
30*779fc935Sceastha /*
31*779fc935Sceastha * FUNCTION PAGE INDEX
32*779fc935Sceastha * Function Page Description
33*779fc935Sceastha * append 16 Append chars to end of line.
34*779fc935Sceastha * begtrunc 16 Truncate characters from beginning of line.
35*779fc935Sceastha * center 5 Center text in the work area.
36*779fc935Sceastha * cnvtspec 7 Convert tab spec to tab positions.
37*779fc935Sceastha * endtrunc 16 Truncate chars from end of line.
38*779fc935Sceastha * inputtabs 17 Expand according to input tab specs.
39*779fc935Sceastha * main 3 MAIN
40*779fc935Sceastha * inputn 5 Read a command line option number.
41*779fc935Sceastha * options 4 Process command line options.
42*779fc935Sceastha * outputtabs 19 Contract according to output tab specs.
43*779fc935Sceastha * prepend 16 Prepend chars to line.
44*779fc935Sceastha * process 15 Process one line of input.
45*779fc935Sceastha * readline 14 Read one line from the file.
46*779fc935Sceastha * readspec 12 Read a tabspec from a file.
47*779fc935Sceastha * sstrip 18 Strip SCCS SID char from beginning of line.
48*779fc935Sceastha * sadd 18 Add SCCS SID chars to end of line.
49*779fc935Sceastha * type 14 Determine type of a character.
50*779fc935Sceastha */
517c478bd9Sstevel@tonic-gate
52*779fc935Sceastha #include <stdlib.h>
53*779fc935Sceastha #include <string.h>
54*779fc935Sceastha #include <stdio.h>
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate #define MAXOPTS 50
577c478bd9Sstevel@tonic-gate #define NCOLS 512
587c478bd9Sstevel@tonic-gate #define MAXLINE 512
597c478bd9Sstevel@tonic-gate #define NUMBER '0'
607c478bd9Sstevel@tonic-gate #define LINELEN 80
617c478bd9Sstevel@tonic-gate
62*779fc935Sceastha static int tabtbl[500] = { /* Table containing tab stops */
63*779fc935Sceastha 1, 9, 17, 25, 33, 41, 49, 57, 65, 73, 0,
64*779fc935Sceastha /* Default tabs */
65*779fc935Sceastha 1, 10, 16, 36, 72, 0, /* IBM 370 Assembler */
66*779fc935Sceastha 1, 10, 16, 40, 72, 0, /* IBM 370 Assembler (alt.) */
67*779fc935Sceastha 1, 8, 12, 16, 20, 55, 0, /* COBOL */
68*779fc935Sceastha 1, 6, 10, 14, 49, 0, /* COBOL (crunched) */
69*779fc935Sceastha 1, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50, 54, 58, 62, 67, 0,
70*779fc935Sceastha /* COBOL (crunched, many cols.) */
71*779fc935Sceastha 1, 7, 11, 15, 19, 23, 0, /* FORTRAN */
72*779fc935Sceastha 1, 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53, 57, 61, 0,
73*779fc935Sceastha /* PL/1 */
74*779fc935Sceastha 1, 10, 55, 0, /* SNOBOL */
75*779fc935Sceastha 1, 12, 20, 44, 0 }, /* UNIVAC Assembler */
76*779fc935Sceastha
77*779fc935Sceastha *nexttab = &tabtbl[87], /* Pointer to next empty slot */
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate *spectbl[40] = { /* Table of pointers into tabtbl */
807c478bd9Sstevel@tonic-gate &tabtbl[0], /* Default specification */
817c478bd9Sstevel@tonic-gate &tabtbl[11], /* -a specification */
827c478bd9Sstevel@tonic-gate &tabtbl[17], /* -a2 specification */
837c478bd9Sstevel@tonic-gate &tabtbl[23], /* -c specification */
847c478bd9Sstevel@tonic-gate &tabtbl[30], /* -c2 specification */
857c478bd9Sstevel@tonic-gate &tabtbl[36], /* -c3 specification */
867c478bd9Sstevel@tonic-gate &tabtbl[54], /* -f specification */
877c478bd9Sstevel@tonic-gate &tabtbl[61], /* -p specification */
887c478bd9Sstevel@tonic-gate &tabtbl[78], /* -s specification */
897c478bd9Sstevel@tonic-gate &tabtbl[82] }, /* -u specification */
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate savek; /* Stores char count stripped from front of line. */
92*779fc935Sceastha static int nextspec = 10, /* Index to next slot */
937c478bd9Sstevel@tonic-gate sitabspec = -1, /* Index to "standard input" spec. */
947c478bd9Sstevel@tonic-gate effll = 80, /* Effective line length */
957c478bd9Sstevel@tonic-gate optionf = 0, /* 'f' option set */
967c478bd9Sstevel@tonic-gate soption = 0, /* 's' option used. */
977c478bd9Sstevel@tonic-gate files = 0, /* Number of input files */
987c478bd9Sstevel@tonic-gate kludge = 0, /* Kludge to allow reread of 1st line */
99*779fc935Sceastha okludge = 0, /* Kludge to indicate reading "o" option */
1007c478bd9Sstevel@tonic-gate lock = 0; /* Lock to prevent file indirection */
1017c478bd9Sstevel@tonic-gate
102*779fc935Sceastha static char pachar = ' ', /* Prepend/append character */
1037c478bd9Sstevel@tonic-gate work[3*NCOLS+1], /* Work area */
1047c478bd9Sstevel@tonic-gate *pfirst, /* Pointer to beginning of line */
1057c478bd9Sstevel@tonic-gate *plast, /* Pointer to end of line */
1067c478bd9Sstevel@tonic-gate *wfirst = &work[0], /* Pointer to beginning of work area */
1077c478bd9Sstevel@tonic-gate *wlast = &work[3*NCOLS], /* Pointer to end of work area */
1087c478bd9Sstevel@tonic-gate siline[NCOLS], /* First standard input line */
1097c478bd9Sstevel@tonic-gate savchr[8], /* Holds char stripped from line start */
110*779fc935Sceastha format[80] = "-8"; /* Array to hold format line */
1117c478bd9Sstevel@tonic-gate
112*779fc935Sceastha static struct f {
1137c478bd9Sstevel@tonic-gate char option;
1147c478bd9Sstevel@tonic-gate int param;
1157c478bd9Sstevel@tonic-gate } optl[MAXOPTS], /* List of command line options */
1167c478bd9Sstevel@tonic-gate *flp = optl; /* Pointer to next open slot */
117*779fc935Sceastha
118*779fc935Sceastha static void append(int);
119*779fc935Sceastha static void begtrunc(int);
120*779fc935Sceastha static void center(void);
121*779fc935Sceastha static int cnvtspec(char *);
122*779fc935Sceastha static void endtrunc(int);
123*779fc935Sceastha static int inputn(char *);
124*779fc935Sceastha static void inputtabs(int);
125*779fc935Sceastha static void options(int, char **);
126*779fc935Sceastha static void outputtabs(int);
127*779fc935Sceastha static void prepend(int);
128*779fc935Sceastha static void process(FILE *);
129*779fc935Sceastha static char *readline(FILE *, char *);
130*779fc935Sceastha static int readspec(char *);
131*779fc935Sceastha static void sadd(void);
132*779fc935Sceastha static void sstrip(void);
133*779fc935Sceastha static char type(char);
134*779fc935Sceastha
135*779fc935Sceastha int
main(int argc,char ** argv)136*779fc935Sceastha main(int argc, char **argv)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate char *scan; /* String scan pointer */
1397c478bd9Sstevel@tonic-gate FILE *fp; /* Pointer to current file */
1407c478bd9Sstevel@tonic-gate
141*779fc935Sceastha options(argc, argv);
142*779fc935Sceastha if (optionf) { /* Write tab spec format line. */
143*779fc935Sceastha (void) fputs("<:t", stdout);
144*779fc935Sceastha (void) fputs(format, stdout);
145*779fc935Sceastha (void) fputs(" d:>\n", stdout);
146*779fc935Sceastha }
1477c478bd9Sstevel@tonic-gate if (files) {
1487c478bd9Sstevel@tonic-gate while (--argc) {
1497c478bd9Sstevel@tonic-gate scan = *++argv;
1507c478bd9Sstevel@tonic-gate if (*scan != '-') {
151*779fc935Sceastha if ((fp = fopen(scan, "r")) == NULL) {
152*779fc935Sceastha (void) fprintf(stderr,
153*779fc935Sceastha "newform: can't open %s\n", scan);
1547c478bd9Sstevel@tonic-gate exit(1);
1557c478bd9Sstevel@tonic-gate }
156*779fc935Sceastha process(fp);
157*779fc935Sceastha (void) fclose(fp);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate }
160*779fc935Sceastha } else {
1617c478bd9Sstevel@tonic-gate process(stdin);
162*779fc935Sceastha }
163*779fc935Sceastha return (0);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate
167*779fc935Sceastha static void
options(int argc,char ** argv)168*779fc935Sceastha options(int argc, char **argv) /* Process command line options */
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate int n; /* Temporary number holder */
171*779fc935Sceastha char *scan; /* Pointer to individual option strings */
172*779fc935Sceastha char c; /* Option character */
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate /* changes to option parsing includes checks for exceeding */
1757c478bd9Sstevel@tonic-gate /* initial buffer sizes */
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate while (--argc > 0) {
1787c478bd9Sstevel@tonic-gate scan = *++argv;
1797c478bd9Sstevel@tonic-gate if (*scan++ == '-') {
1807c478bd9Sstevel@tonic-gate switch (c = *scan++) {
1817c478bd9Sstevel@tonic-gate case 'a':
1827c478bd9Sstevel@tonic-gate flp->option = 'a';
1837c478bd9Sstevel@tonic-gate flp->param = inputn(scan);
184*779fc935Sceastha if (flp->param <= NCOLS)
1857c478bd9Sstevel@tonic-gate flp++;
1867c478bd9Sstevel@tonic-gate else {
187*779fc935Sceastha (void) fprintf(stderr, "newform: "
188*779fc935Sceastha "prefix request larger than "
189*779fc935Sceastha "buffer, %d\n", NCOLS);
1907c478bd9Sstevel@tonic-gate exit(1);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate break;
1937c478bd9Sstevel@tonic-gate case 'b':
1947c478bd9Sstevel@tonic-gate case 'e':
1957c478bd9Sstevel@tonic-gate flp->option = c;
1967c478bd9Sstevel@tonic-gate flp->param = inputn(scan);
1977c478bd9Sstevel@tonic-gate flp++;
1987c478bd9Sstevel@tonic-gate break;
1997c478bd9Sstevel@tonic-gate case 'p':
2007c478bd9Sstevel@tonic-gate flp->option = 'p';
2017c478bd9Sstevel@tonic-gate flp->param = inputn(scan);
202*779fc935Sceastha if (flp->param <= NCOLS)
2037c478bd9Sstevel@tonic-gate flp++;
2047c478bd9Sstevel@tonic-gate else {
205*779fc935Sceastha (void) fprintf(stderr, "newform: "
206*779fc935Sceastha "prefix request larger than "
207*779fc935Sceastha "buffer, %d\n", NCOLS);
2087c478bd9Sstevel@tonic-gate exit(1);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate break;
2117c478bd9Sstevel@tonic-gate case 'c':
2127c478bd9Sstevel@tonic-gate flp->option = 'c';
2137c478bd9Sstevel@tonic-gate flp->param = *scan ? *scan : ' ';
2147c478bd9Sstevel@tonic-gate flp++;
2157c478bd9Sstevel@tonic-gate break;
2167c478bd9Sstevel@tonic-gate case 'f':
2177c478bd9Sstevel@tonic-gate flp->option = 'f';
2187c478bd9Sstevel@tonic-gate optionf++;
2197c478bd9Sstevel@tonic-gate flp++;
2207c478bd9Sstevel@tonic-gate break;
2217c478bd9Sstevel@tonic-gate case 'i':
2227c478bd9Sstevel@tonic-gate flp->option = 'i';
2237c478bd9Sstevel@tonic-gate flp->param = cnvtspec(scan);
2247c478bd9Sstevel@tonic-gate flp++;
2257c478bd9Sstevel@tonic-gate break;
2267c478bd9Sstevel@tonic-gate case 'o':
227*779fc935Sceastha if (*scan == '-' && *(scan+1) == '0' &&
228*779fc935Sceastha *(scan+2) == '\0')
229*779fc935Sceastha break;
230*779fc935Sceastha /* Above allows the -o-0 option to be ignored. */
2317c478bd9Sstevel@tonic-gate flp->option = 'o';
232*779fc935Sceastha (void) strcpy(format, scan);
2337c478bd9Sstevel@tonic-gate okludge++;
2347c478bd9Sstevel@tonic-gate flp->param = cnvtspec(scan);
2357c478bd9Sstevel@tonic-gate okludge--;
236*779fc935Sceastha if (flp->param == 0)
237*779fc935Sceastha (void) strcpy(format, "-8");
2387c478bd9Sstevel@tonic-gate flp++;
2397c478bd9Sstevel@tonic-gate break;
2407c478bd9Sstevel@tonic-gate case 'l':
2417c478bd9Sstevel@tonic-gate flp->option = 'l';
2427c478bd9Sstevel@tonic-gate flp->param = ((n = inputn(scan)) ? n : 72);
2437c478bd9Sstevel@tonic-gate if (flp->param <= (3*NCOLS))
2447c478bd9Sstevel@tonic-gate flp++;
2457c478bd9Sstevel@tonic-gate else {
246*779fc935Sceastha (void) fprintf(stderr, "newform: "
247*779fc935Sceastha "line length request larger "
248*779fc935Sceastha "than buffer, %d \n", (3*NCOLS));
2497c478bd9Sstevel@tonic-gate exit(1);
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate break;
2527c478bd9Sstevel@tonic-gate case 's':
2537c478bd9Sstevel@tonic-gate flp->option = 's';
2547c478bd9Sstevel@tonic-gate flp++;
2557c478bd9Sstevel@tonic-gate soption++;
2567c478bd9Sstevel@tonic-gate break;
2577c478bd9Sstevel@tonic-gate default:
2587c478bd9Sstevel@tonic-gate goto usageerr;
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate else
2627c478bd9Sstevel@tonic-gate files++;
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate return;
2657c478bd9Sstevel@tonic-gate usageerr:
266*779fc935Sceastha (void) fprintf(stderr, "usage: newform [-s] [-itabspec] [-otabspec] ");
267*779fc935Sceastha (void) fprintf(stderr, "[-pn] [-en] [-an] [-f] [-cchar]\n\t\t");
268*779fc935Sceastha (void) fprintf(stderr, "[-ln] [-bn] [file ...]\n");
2697c478bd9Sstevel@tonic-gate exit(1);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate /* _________________________________________________________________ */
2727c478bd9Sstevel@tonic-gate
273*779fc935Sceastha static int
inputn(char * scan)274*779fc935Sceastha inputn(char *scan) /* Read a command option number */
275*779fc935Sceastha /* Pointer to string of digits */
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate int n; /* Number */
2787c478bd9Sstevel@tonic-gate char c; /* Character being scanned */
2797c478bd9Sstevel@tonic-gate
2807c478bd9Sstevel@tonic-gate n = 0;
2817c478bd9Sstevel@tonic-gate while ((c = *scan++) >= '0' && c <= '9')
2827c478bd9Sstevel@tonic-gate n = n * 10 + c - '0';
283*779fc935Sceastha return (n);
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate /* _________________________________________________________________ */
2867c478bd9Sstevel@tonic-gate
287*779fc935Sceastha static void
center(void)288*779fc935Sceastha center(void) /* Center the text in the work area. */
2897c478bd9Sstevel@tonic-gate {
290*779fc935Sceastha char *tfirst; /* Pointer for moving buffer down */
291*779fc935Sceastha char *tlast; /* Pointer for moving buffer up */
292*779fc935Sceastha char *tptr; /* Temporary */
2937c478bd9Sstevel@tonic-gate
2947c478bd9Sstevel@tonic-gate if (plast - pfirst > MAXLINE) {
295*779fc935Sceastha (void) fprintf(stderr, "newform: internal line too long\n");
2967c478bd9Sstevel@tonic-gate exit(1);
297*779fc935Sceastha }
2987c478bd9Sstevel@tonic-gate if (pfirst < &work[NCOLS]) {
2997c478bd9Sstevel@tonic-gate tlast = plast + (&work[NCOLS] - pfirst);
3007c478bd9Sstevel@tonic-gate tptr = tlast;
3017c478bd9Sstevel@tonic-gate while (plast >= pfirst) *tlast-- = *plast--;
3027c478bd9Sstevel@tonic-gate pfirst = ++tlast;
3037c478bd9Sstevel@tonic-gate plast = tptr;
304*779fc935Sceastha } else {
3057c478bd9Sstevel@tonic-gate tfirst = &work[NCOLS];
3067c478bd9Sstevel@tonic-gate tptr = tfirst;
3077c478bd9Sstevel@tonic-gate while (pfirst <= plast) *tfirst++ = *pfirst++;
3087c478bd9Sstevel@tonic-gate plast = --tfirst;
3097c478bd9Sstevel@tonic-gate pfirst = tptr;
310*779fc935Sceastha }
3117c478bd9Sstevel@tonic-gate }
312*779fc935Sceastha
313*779fc935Sceastha static int
cnvtspec(char * p)314*779fc935Sceastha cnvtspec(char *p) /* Convert tab specification to tab positions. */
315*779fc935Sceastha /* Pointer to spec string. */
3167c478bd9Sstevel@tonic-gate {
3177c478bd9Sstevel@tonic-gate int state, /* DFA state */
3187c478bd9Sstevel@tonic-gate spectype, /* Specification type */
3197c478bd9Sstevel@tonic-gate number[40], /* Array of read-in numbers */
3207c478bd9Sstevel@tonic-gate tp, /* Pointer to last number */
3217c478bd9Sstevel@tonic-gate ix; /* Temporary */
322*779fc935Sceastha int tspec = 0; /* Tab spec pointer */
3237c478bd9Sstevel@tonic-gate char c, /* Temporary */
324*779fc935Sceastha *filep; /* Pointer to file name */
325*779fc935Sceastha FILE *fp; /* File pointer */
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate state = 0;
3287c478bd9Sstevel@tonic-gate while (state >= 0) {
3297c478bd9Sstevel@tonic-gate c = *p++;
3307c478bd9Sstevel@tonic-gate switch (state) {
3317c478bd9Sstevel@tonic-gate case 0:
3327c478bd9Sstevel@tonic-gate switch (type(c)) {
3337c478bd9Sstevel@tonic-gate case '\0':
3347c478bd9Sstevel@tonic-gate spectype = 0;
3357c478bd9Sstevel@tonic-gate state = -1;
3367c478bd9Sstevel@tonic-gate break;
3377c478bd9Sstevel@tonic-gate case NUMBER:
3387c478bd9Sstevel@tonic-gate state = 1;
3397c478bd9Sstevel@tonic-gate tp = 0;
3407c478bd9Sstevel@tonic-gate number[tp] = c - '0';
3417c478bd9Sstevel@tonic-gate break;
3427c478bd9Sstevel@tonic-gate case '-':
3437c478bd9Sstevel@tonic-gate state = 3;
3447c478bd9Sstevel@tonic-gate break;
3457c478bd9Sstevel@tonic-gate default:
3467c478bd9Sstevel@tonic-gate goto tabspecerr;
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate break;
3497c478bd9Sstevel@tonic-gate case 1:
3507c478bd9Sstevel@tonic-gate switch (type(c)) {
3517c478bd9Sstevel@tonic-gate case '\0':
3527c478bd9Sstevel@tonic-gate spectype = 11;
3537c478bd9Sstevel@tonic-gate state = -1;
3547c478bd9Sstevel@tonic-gate break;
3557c478bd9Sstevel@tonic-gate case NUMBER:
3567c478bd9Sstevel@tonic-gate state = 1;
3577c478bd9Sstevel@tonic-gate number[tp] = number[tp] * 10 + c - '0';
3587c478bd9Sstevel@tonic-gate break;
3597c478bd9Sstevel@tonic-gate case ',':
3607c478bd9Sstevel@tonic-gate state = 2;
3617c478bd9Sstevel@tonic-gate break;
3627c478bd9Sstevel@tonic-gate default:
3637c478bd9Sstevel@tonic-gate goto tabspecerr;
3647c478bd9Sstevel@tonic-gate }
3657c478bd9Sstevel@tonic-gate break;
3667c478bd9Sstevel@tonic-gate case 2:
3677c478bd9Sstevel@tonic-gate if (type(c) == NUMBER) {
3687c478bd9Sstevel@tonic-gate state = 1;
3697c478bd9Sstevel@tonic-gate number[++tp] = c - '0';
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate else
3727c478bd9Sstevel@tonic-gate goto tabspecerr;
3737c478bd9Sstevel@tonic-gate
3747c478bd9Sstevel@tonic-gate break;
3757c478bd9Sstevel@tonic-gate case 3:
3767c478bd9Sstevel@tonic-gate switch (type(c)) {
3777c478bd9Sstevel@tonic-gate case '-':
3787c478bd9Sstevel@tonic-gate state = 4;
3797c478bd9Sstevel@tonic-gate break;
3807c478bd9Sstevel@tonic-gate case 'a':
3817c478bd9Sstevel@tonic-gate state = 5;
3827c478bd9Sstevel@tonic-gate break;
3837c478bd9Sstevel@tonic-gate case 'c':
3847c478bd9Sstevel@tonic-gate state = 7;
3857c478bd9Sstevel@tonic-gate break;
3867c478bd9Sstevel@tonic-gate case 'f':
3877c478bd9Sstevel@tonic-gate state = 10;
3887c478bd9Sstevel@tonic-gate break;
3897c478bd9Sstevel@tonic-gate case 'p':
3907c478bd9Sstevel@tonic-gate state = 11;
3917c478bd9Sstevel@tonic-gate break;
3927c478bd9Sstevel@tonic-gate case 's':
3937c478bd9Sstevel@tonic-gate state = 12;
3947c478bd9Sstevel@tonic-gate break;
3957c478bd9Sstevel@tonic-gate case 'u':
3967c478bd9Sstevel@tonic-gate state = 13;
3977c478bd9Sstevel@tonic-gate break;
3987c478bd9Sstevel@tonic-gate case NUMBER:
3997c478bd9Sstevel@tonic-gate state = 14;
4007c478bd9Sstevel@tonic-gate number[0] = c - '0';
4017c478bd9Sstevel@tonic-gate break;
4027c478bd9Sstevel@tonic-gate default:
4037c478bd9Sstevel@tonic-gate goto tabspecerr;
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate break;
4067c478bd9Sstevel@tonic-gate case 4:
4077c478bd9Sstevel@tonic-gate if (c == '\0') {
4087c478bd9Sstevel@tonic-gate spectype = 12;
4097c478bd9Sstevel@tonic-gate state = -1;
410*779fc935Sceastha } else {
4117c478bd9Sstevel@tonic-gate filep = --p;
4127c478bd9Sstevel@tonic-gate spectype = 13;
4137c478bd9Sstevel@tonic-gate state = -1;
414*779fc935Sceastha }
4157c478bd9Sstevel@tonic-gate break;
4167c478bd9Sstevel@tonic-gate case 5:
4177c478bd9Sstevel@tonic-gate if (c == '\0') {
4187c478bd9Sstevel@tonic-gate spectype = 1;
4197c478bd9Sstevel@tonic-gate state = -1;
420*779fc935Sceastha } else if (c == '2')
4217c478bd9Sstevel@tonic-gate state = 6;
4227c478bd9Sstevel@tonic-gate else
4237c478bd9Sstevel@tonic-gate goto tabspecerr;
4247c478bd9Sstevel@tonic-gate break;
4257c478bd9Sstevel@tonic-gate case 6:
4267c478bd9Sstevel@tonic-gate if (c == '\0') {
4277c478bd9Sstevel@tonic-gate spectype = 2;
4287c478bd9Sstevel@tonic-gate state = -1;
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate else
4317c478bd9Sstevel@tonic-gate goto tabspecerr;
4327c478bd9Sstevel@tonic-gate break;
4337c478bd9Sstevel@tonic-gate case 7:
4347c478bd9Sstevel@tonic-gate switch (c) {
4357c478bd9Sstevel@tonic-gate case '\0':
4367c478bd9Sstevel@tonic-gate spectype = 3;
4377c478bd9Sstevel@tonic-gate state = -1;
4387c478bd9Sstevel@tonic-gate break;
4397c478bd9Sstevel@tonic-gate case '2':
4407c478bd9Sstevel@tonic-gate state = 8;
4417c478bd9Sstevel@tonic-gate break;
4427c478bd9Sstevel@tonic-gate case '3':
4437c478bd9Sstevel@tonic-gate state = 9;
4447c478bd9Sstevel@tonic-gate break;
4457c478bd9Sstevel@tonic-gate default:
4467c478bd9Sstevel@tonic-gate goto tabspecerr;
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate break;
4497c478bd9Sstevel@tonic-gate case 8:
4507c478bd9Sstevel@tonic-gate if (c == '\0') {
4517c478bd9Sstevel@tonic-gate spectype = 4;
4527c478bd9Sstevel@tonic-gate state = -1;
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate else
4557c478bd9Sstevel@tonic-gate goto tabspecerr;
4567c478bd9Sstevel@tonic-gate break;
4577c478bd9Sstevel@tonic-gate case 9:
4587c478bd9Sstevel@tonic-gate if (c == '\0') {
4597c478bd9Sstevel@tonic-gate spectype = 5;
4607c478bd9Sstevel@tonic-gate state = -1;
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate else
4637c478bd9Sstevel@tonic-gate goto tabspecerr;
4647c478bd9Sstevel@tonic-gate break;
4657c478bd9Sstevel@tonic-gate case 10:
4667c478bd9Sstevel@tonic-gate if (c == '\0') {
4677c478bd9Sstevel@tonic-gate spectype = 6;
4687c478bd9Sstevel@tonic-gate state = -1;
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate else
4717c478bd9Sstevel@tonic-gate goto tabspecerr;
4727c478bd9Sstevel@tonic-gate break;
4737c478bd9Sstevel@tonic-gate case 11:
4747c478bd9Sstevel@tonic-gate if (c == '\0') {
4757c478bd9Sstevel@tonic-gate spectype = 7;
4767c478bd9Sstevel@tonic-gate state = -1;
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate else
4797c478bd9Sstevel@tonic-gate goto tabspecerr;
4807c478bd9Sstevel@tonic-gate break;
4817c478bd9Sstevel@tonic-gate case 12:
4827c478bd9Sstevel@tonic-gate if (c == '\0') {
4837c478bd9Sstevel@tonic-gate spectype = 8;
4847c478bd9Sstevel@tonic-gate state = -1;
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate else
4877c478bd9Sstevel@tonic-gate goto tabspecerr;
4887c478bd9Sstevel@tonic-gate break;
4897c478bd9Sstevel@tonic-gate case 13:
4907c478bd9Sstevel@tonic-gate if (c == '\0') {
4917c478bd9Sstevel@tonic-gate spectype = 9;
4927c478bd9Sstevel@tonic-gate state = -1;
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate else
4957c478bd9Sstevel@tonic-gate goto tabspecerr;
4967c478bd9Sstevel@tonic-gate break;
4977c478bd9Sstevel@tonic-gate case 14:
4987c478bd9Sstevel@tonic-gate if (type(c) == NUMBER) {
4997c478bd9Sstevel@tonic-gate state = 14;
5007c478bd9Sstevel@tonic-gate number[0] = number[0] * 10 + c - '0';
501*779fc935Sceastha } else if (c == '\0') {
5027c478bd9Sstevel@tonic-gate spectype = 10;
5037c478bd9Sstevel@tonic-gate state = -1;
504*779fc935Sceastha } else
5057c478bd9Sstevel@tonic-gate goto tabspecerr;
5067c478bd9Sstevel@tonic-gate break;
5077c478bd9Sstevel@tonic-gate }
508*779fc935Sceastha }
509*779fc935Sceastha if (spectype <= 9)
510*779fc935Sceastha return (spectype);
5117c478bd9Sstevel@tonic-gate if (spectype == 10) {
5127c478bd9Sstevel@tonic-gate spectype = nextspec++;
5137c478bd9Sstevel@tonic-gate spectbl[spectype] = nexttab;
5147c478bd9Sstevel@tonic-gate *nexttab = 1;
515*779fc935Sceastha if (number[0] == 0) number[0] = 1; /* Prevent infinite loop. */
5167c478bd9Sstevel@tonic-gate while (*nexttab < LINELEN) {
5177c478bd9Sstevel@tonic-gate *(nexttab + 1) = *nexttab;
5187c478bd9Sstevel@tonic-gate *++nexttab += number[0];
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate *nexttab++ = '\0';
521*779fc935Sceastha return (spectype);
522*779fc935Sceastha }
5237c478bd9Sstevel@tonic-gate if (spectype == 11) {
5247c478bd9Sstevel@tonic-gate spectype = nextspec++;
5257c478bd9Sstevel@tonic-gate spectbl[spectype] = nexttab;
5267c478bd9Sstevel@tonic-gate *nexttab++ = 1;
5277c478bd9Sstevel@tonic-gate for (ix = 0; ix <= tp; ix++) {
5287c478bd9Sstevel@tonic-gate *nexttab++ = number[ix];
5297c478bd9Sstevel@tonic-gate if ((number[ix] >= number[ix+1]) && (ix != tp))
5307c478bd9Sstevel@tonic-gate goto tabspecerr;
5317c478bd9Sstevel@tonic-gate }
5327c478bd9Sstevel@tonic-gate *nexttab++ = '\0';
533*779fc935Sceastha return (spectype);
534*779fc935Sceastha }
5357c478bd9Sstevel@tonic-gate if (lock == 1) {
536*779fc935Sceastha (void) fprintf(stderr,
537*779fc935Sceastha "newform: tabspec indirection illegal\n");
5387c478bd9Sstevel@tonic-gate exit(1);
539*779fc935Sceastha }
5407c478bd9Sstevel@tonic-gate lock = 1;
5417c478bd9Sstevel@tonic-gate if (spectype == 12) {
5427c478bd9Sstevel@tonic-gate if (sitabspec >= 0) {
5437c478bd9Sstevel@tonic-gate tspec = sitabspec;
544*779fc935Sceastha } else {
545*779fc935Sceastha if (readline(stdin, siline) != NULL) {
5467c478bd9Sstevel@tonic-gate kludge = 1;
5477c478bd9Sstevel@tonic-gate tspec = readspec(siline);
5487c478bd9Sstevel@tonic-gate sitabspec = tspec;
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate }
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate if (spectype == 13) {
553*779fc935Sceastha if ((fp = fopen(filep, "r")) == NULL) {
554*779fc935Sceastha (void) fprintf(stderr,
555*779fc935Sceastha "newform: can't open %s\n", filep);
5567c478bd9Sstevel@tonic-gate exit(1);
5577c478bd9Sstevel@tonic-gate }
558*779fc935Sceastha (void) readline(fp, work);
559*779fc935Sceastha (void) fclose(fp);
560*779fc935Sceastha tspec = readspec(work);
561*779fc935Sceastha }
5627c478bd9Sstevel@tonic-gate lock = 0;
563*779fc935Sceastha return (tspec);
5647c478bd9Sstevel@tonic-gate tabspecerr:
565*779fc935Sceastha (void) fprintf(stderr, "newform: tabspec in error\n");
566*779fc935Sceastha (void) fprintf(stderr,
567*779fc935Sceastha "tabspec is \t-a\t-a2\t-c\t-c2\t-c3\t-f\t-p\t-s\n");
568*779fc935Sceastha (void) fprintf(stderr,
569*779fc935Sceastha "\t\t-u\t--\t--file\t-number\tnumber,..,number\n");
5707c478bd9Sstevel@tonic-gate exit(1);
571*779fc935Sceastha /* NOTREACHED */
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate
574*779fc935Sceastha static int
readspec(char * p)575*779fc935Sceastha readspec(char *p) /* Read a tabspec from a file */
576*779fc935Sceastha /* Pointer to buffer to process */
5777c478bd9Sstevel@tonic-gate {
5787c478bd9Sstevel@tonic-gate int state, /* Current state */
5797c478bd9Sstevel@tonic-gate firsttime, /* Flag to indicate spec found */
5807c478bd9Sstevel@tonic-gate value; /* Function value */
5817c478bd9Sstevel@tonic-gate char c, /* Char being looked at */
5827c478bd9Sstevel@tonic-gate *tabspecp, /* Pointer to spec string */
5837c478bd9Sstevel@tonic-gate *restore = " ", /* Character to be restored */
5847c478bd9Sstevel@tonic-gate repch; /* Character to replace with */
5857c478bd9Sstevel@tonic-gate
5867c478bd9Sstevel@tonic-gate state = 0;
5877c478bd9Sstevel@tonic-gate firsttime = 1;
5887c478bd9Sstevel@tonic-gate while (state >= 0) {
5897c478bd9Sstevel@tonic-gate c = *p++;
5907c478bd9Sstevel@tonic-gate switch (state) {
5917c478bd9Sstevel@tonic-gate case 0:
5927c478bd9Sstevel@tonic-gate state = (c == '<') ? 1 : 0;
5937c478bd9Sstevel@tonic-gate break;
5947c478bd9Sstevel@tonic-gate case 1:
5957c478bd9Sstevel@tonic-gate state = (c == ':') ? 2 : 0;
5967c478bd9Sstevel@tonic-gate break;
5977c478bd9Sstevel@tonic-gate case 2:
5987c478bd9Sstevel@tonic-gate state = (c == 't') ? 4
5997c478bd9Sstevel@tonic-gate : ((c == ' ') || (c == '\t')) ? 2 : 3;
6007c478bd9Sstevel@tonic-gate break;
6017c478bd9Sstevel@tonic-gate case 3:
6027c478bd9Sstevel@tonic-gate state = ((c == ' ') || (c == '\t')) ? 2 : 3;
6037c478bd9Sstevel@tonic-gate break;
6047c478bd9Sstevel@tonic-gate case 4:
6057c478bd9Sstevel@tonic-gate if (firsttime) {
6067c478bd9Sstevel@tonic-gate tabspecp = --p;
6077c478bd9Sstevel@tonic-gate p++;
6087c478bd9Sstevel@tonic-gate firsttime = 0;
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate if ((c == ' ') || (c == '\t') || (c == ':')) {
6117c478bd9Sstevel@tonic-gate repch = *(restore = p - 1);
6127c478bd9Sstevel@tonic-gate *restore = '\0';
6137c478bd9Sstevel@tonic-gate }
6147c478bd9Sstevel@tonic-gate state = (c == ':') ? 6
6157c478bd9Sstevel@tonic-gate : ((c == ' ') || (c == '\t')) ? 5 : 4;
6167c478bd9Sstevel@tonic-gate break;
6177c478bd9Sstevel@tonic-gate case 5:
6187c478bd9Sstevel@tonic-gate state = (c == ':') ? 6 : 5;
6197c478bd9Sstevel@tonic-gate break;
6207c478bd9Sstevel@tonic-gate case 6:
6217c478bd9Sstevel@tonic-gate state = (c == '>') ? -2 : 5;
6227c478bd9Sstevel@tonic-gate break;
6237c478bd9Sstevel@tonic-gate }
6247c478bd9Sstevel@tonic-gate if (c == '\n') state = -1;
6257c478bd9Sstevel@tonic-gate }
626*779fc935Sceastha if (okludge)
627*779fc935Sceastha (void) strcpy(format, tabspecp);
6287c478bd9Sstevel@tonic-gate value = (state == -1) ? 0 : cnvtspec(tabspecp);
6297c478bd9Sstevel@tonic-gate *restore = repch;
630*779fc935Sceastha return (value);
6317c478bd9Sstevel@tonic-gate }
632*779fc935Sceastha
633*779fc935Sceastha static char *
readline(FILE * fp,char * area)634*779fc935Sceastha readline(FILE *fp, char *area) /* Read one line from the file. */
635*779fc935Sceastha /* fp - File to read from */
636*779fc935Sceastha /* area - Array of characters to read into */
6377c478bd9Sstevel@tonic-gate {
6387c478bd9Sstevel@tonic-gate int c; /* Current character */
6397c478bd9Sstevel@tonic-gate char *xarea, /* Temporary pointer to character array */
6407c478bd9Sstevel@tonic-gate *temp; /* Array pointer */
6417c478bd9Sstevel@tonic-gate
6427c478bd9Sstevel@tonic-gate
6437c478bd9Sstevel@tonic-gate
6447c478bd9Sstevel@tonic-gate /* check for existence of stdin before attempting to read */
6457c478bd9Sstevel@tonic-gate /* kludge refers to reading from stdin to get tabspecs for option -i-- */
6467c478bd9Sstevel@tonic-gate
6477c478bd9Sstevel@tonic-gate xarea = area;
6487c478bd9Sstevel@tonic-gate if (kludge && (fp == stdin)) {
6497c478bd9Sstevel@tonic-gate if (fp != NULL) {
6507c478bd9Sstevel@tonic-gate temp = siline;
651*779fc935Sceastha while ((*area++ = *temp++) != '\n')
652*779fc935Sceastha ;
6537c478bd9Sstevel@tonic-gate kludge = 0;
654*779fc935Sceastha return (xarea);
655*779fc935Sceastha } else
656*779fc935Sceastha return (NULL);
657*779fc935Sceastha } else {
6587c478bd9Sstevel@tonic-gate
6597c478bd9Sstevel@tonic-gate /* check for exceeding size of buffer when reading valid input */
6607c478bd9Sstevel@tonic-gate
661*779fc935Sceastha while (wlast - area) {
662*779fc935Sceastha switch (c = getc(fp)) {
6637c478bd9Sstevel@tonic-gate case EOF:
6647c478bd9Sstevel@tonic-gate if (area == xarea)
665*779fc935Sceastha return (NULL);
666*779fc935Sceastha /* FALLTHROUGH */
667*779fc935Sceastha case '\n': /* EOF falls through to here */
6687c478bd9Sstevel@tonic-gate *area = '\n';
669*779fc935Sceastha return (xarea);
6707c478bd9Sstevel@tonic-gate }
671*779fc935Sceastha *area = c;
672*779fc935Sceastha area++;
6737c478bd9Sstevel@tonic-gate }
674*779fc935Sceastha (void) printf("newform: input line larger than buffer area \n");
6757c478bd9Sstevel@tonic-gate exit(1);
6767c478bd9Sstevel@tonic-gate }
677*779fc935Sceastha /* NOTREACHED */
6787c478bd9Sstevel@tonic-gate }
6797c478bd9Sstevel@tonic-gate /* _________________________________________________________________ */
6807c478bd9Sstevel@tonic-gate
681*779fc935Sceastha static char
type(char c)682*779fc935Sceastha type(char c) /* Determine type of a character */
683*779fc935Sceastha /* Character to check */
6847c478bd9Sstevel@tonic-gate {
685*779fc935Sceastha return ((c >= '0') && (c <= '9') ? NUMBER : c);
6867c478bd9Sstevel@tonic-gate }
687*779fc935Sceastha
688*779fc935Sceastha static void
process(FILE * fp)689*779fc935Sceastha process(FILE *fp) /* Process one line of input */
690*779fc935Sceastha /* File pointer for current input */
6917c478bd9Sstevel@tonic-gate {
6927c478bd9Sstevel@tonic-gate struct f *lp; /* Pointer to structs */
6937c478bd9Sstevel@tonic-gate char chrnow; /* For int to char conversion. */
6947c478bd9Sstevel@tonic-gate
695*779fc935Sceastha while (readline(fp, &work[NCOLS]) != NULL) {
6967c478bd9Sstevel@tonic-gate effll = 80;
6977c478bd9Sstevel@tonic-gate pachar = ' ';
6987c478bd9Sstevel@tonic-gate pfirst = plast = &work[NCOLS];
6997c478bd9Sstevel@tonic-gate while (*plast != '\n') plast++;
7007c478bd9Sstevel@tonic-gate
7017c478bd9Sstevel@tonic-gate /* changes to line parsing includes checks for exceeding */
7027c478bd9Sstevel@tonic-gate /* line size when modifying text */
7037c478bd9Sstevel@tonic-gate
704*779fc935Sceastha for (lp = optl; lp < flp; lp++) {
7057c478bd9Sstevel@tonic-gate switch (lp->option) {
7067c478bd9Sstevel@tonic-gate case 'a':
7077c478bd9Sstevel@tonic-gate append(lp->param);
7087c478bd9Sstevel@tonic-gate break;
7097c478bd9Sstevel@tonic-gate case 'b':
7107c478bd9Sstevel@tonic-gate if (lp->param <= (plast - pfirst))
7117c478bd9Sstevel@tonic-gate begtrunc(lp->param);
7127c478bd9Sstevel@tonic-gate else
713*779fc935Sceastha (void) fprintf(stderr,
714*779fc935Sceastha "newform: truncate "
715*779fc935Sceastha "request larger than line, %d \n",
716*779fc935Sceastha (plast - pfirst));
7177c478bd9Sstevel@tonic-gate break;
7187c478bd9Sstevel@tonic-gate case 'c':
7197c478bd9Sstevel@tonic-gate chrnow = lp->param;
7207c478bd9Sstevel@tonic-gate pachar = chrnow ? chrnow : ' ';
7217c478bd9Sstevel@tonic-gate break;
7227c478bd9Sstevel@tonic-gate case 'e':
7237c478bd9Sstevel@tonic-gate if (lp->param <= (plast - pfirst))
7247c478bd9Sstevel@tonic-gate endtrunc(lp->param);
7257c478bd9Sstevel@tonic-gate else
726*779fc935Sceastha (void) fprintf(stderr,
727*779fc935Sceastha "newform: truncate "
728*779fc935Sceastha "request larger than line, %d \n",
729*779fc935Sceastha (plast - pfirst));
7307c478bd9Sstevel@tonic-gate break;
7317c478bd9Sstevel@tonic-gate case 'f':
7327c478bd9Sstevel@tonic-gate /* Ignored */
7337c478bd9Sstevel@tonic-gate break;
7347c478bd9Sstevel@tonic-gate case 'i':
7357c478bd9Sstevel@tonic-gate inputtabs(lp->param);
7367c478bd9Sstevel@tonic-gate break;
7377c478bd9Sstevel@tonic-gate case 'l': /* New eff line length */
7387c478bd9Sstevel@tonic-gate effll = lp->param ? lp->param : 72;
7397c478bd9Sstevel@tonic-gate break;
7407c478bd9Sstevel@tonic-gate case 's':
7417c478bd9Sstevel@tonic-gate sstrip();
7427c478bd9Sstevel@tonic-gate break;
7437c478bd9Sstevel@tonic-gate case 'o':
7447c478bd9Sstevel@tonic-gate outputtabs(lp->param);
7457c478bd9Sstevel@tonic-gate break;
7467c478bd9Sstevel@tonic-gate case 'p':
7477c478bd9Sstevel@tonic-gate prepend(lp->param);
7487c478bd9Sstevel@tonic-gate break;
7497c478bd9Sstevel@tonic-gate }
7507c478bd9Sstevel@tonic-gate }
751*779fc935Sceastha if (soption) sadd();
752*779fc935Sceastha *++plast = '\0';
753*779fc935Sceastha (void) fputs(pfirst, stdout);
754*779fc935Sceastha }
7557c478bd9Sstevel@tonic-gate }
756*779fc935Sceastha
757*779fc935Sceastha static void
append(int n)758*779fc935Sceastha append(int n) /* Append characters to end of line. */
759*779fc935Sceastha /* Number of characters to append. */
7607c478bd9Sstevel@tonic-gate {
7617c478bd9Sstevel@tonic-gate if (plast - pfirst < effll) {
7627c478bd9Sstevel@tonic-gate n = n ? n : effll - (plast - pfirst);
7637c478bd9Sstevel@tonic-gate if (plast + n > wlast) center();
7647c478bd9Sstevel@tonic-gate while (n--) *plast++ = pachar;
7657c478bd9Sstevel@tonic-gate *plast = '\n';
7667c478bd9Sstevel@tonic-gate }
7677c478bd9Sstevel@tonic-gate }
7687c478bd9Sstevel@tonic-gate /* _________________________________________________________________ */
7697c478bd9Sstevel@tonic-gate
770*779fc935Sceastha static void
prepend(int n)771*779fc935Sceastha prepend(int n) /* Prepend characters to line. */
772*779fc935Sceastha /* Number of characters to prepend. */
7737c478bd9Sstevel@tonic-gate {
7747c478bd9Sstevel@tonic-gate if (plast - pfirst < effll) {
7757c478bd9Sstevel@tonic-gate n = n ? n : effll - (plast - pfirst);
7767c478bd9Sstevel@tonic-gate if (pfirst - n < wfirst) center();
7777c478bd9Sstevel@tonic-gate while (n--) *--pfirst = pachar;
7787c478bd9Sstevel@tonic-gate }
7797c478bd9Sstevel@tonic-gate }
7807c478bd9Sstevel@tonic-gate /* _________________________________________________________________ */
7817c478bd9Sstevel@tonic-gate
782*779fc935Sceastha static void
begtrunc(int n)783*779fc935Sceastha begtrunc(int n) /* Truncate characters from beginning of line. */
784*779fc935Sceastha /* Number of characters to truncate. */
7857c478bd9Sstevel@tonic-gate {
7867c478bd9Sstevel@tonic-gate if (plast - pfirst > effll) {
7877c478bd9Sstevel@tonic-gate n = n ? n : plast - pfirst - effll;
7887c478bd9Sstevel@tonic-gate pfirst += n;
7897c478bd9Sstevel@tonic-gate if (pfirst >= plast)
7907c478bd9Sstevel@tonic-gate *(pfirst = plast = &work[NCOLS]) = '\n';
7917c478bd9Sstevel@tonic-gate }
7927c478bd9Sstevel@tonic-gate }
7937c478bd9Sstevel@tonic-gate /* _________________________________________________________________ */
7947c478bd9Sstevel@tonic-gate
795*779fc935Sceastha static void
endtrunc(int n)796*779fc935Sceastha endtrunc(int n) /* Truncate characters from end of line. */
797*779fc935Sceastha /* Number of characters to truncate. */
7987c478bd9Sstevel@tonic-gate {
7997c478bd9Sstevel@tonic-gate if (plast - pfirst > effll) {
8007c478bd9Sstevel@tonic-gate n = n ? n : plast - pfirst - effll;
8017c478bd9Sstevel@tonic-gate plast -= n;
8027c478bd9Sstevel@tonic-gate if (pfirst >= plast)
8037c478bd9Sstevel@tonic-gate *(pfirst = plast = &work[NCOLS]) = '\n';
8047c478bd9Sstevel@tonic-gate else
8057c478bd9Sstevel@tonic-gate *plast = '\n';
8067c478bd9Sstevel@tonic-gate }
8077c478bd9Sstevel@tonic-gate }
808*779fc935Sceastha
809*779fc935Sceastha static void
inputtabs(int p)810*779fc935Sceastha inputtabs(int p) /* Expand according to input tab specifications. */
811*779fc935Sceastha /* Pointer to tab specification. */
8127c478bd9Sstevel@tonic-gate {
8137c478bd9Sstevel@tonic-gate int *tabs; /* Pointer to tabs */
8147c478bd9Sstevel@tonic-gate char *tfirst, /* Pointer to new buffer start */
8157c478bd9Sstevel@tonic-gate *tlast; /* Pointer to new buffer end */
816*779fc935Sceastha char c; /* Character being scanned */
8177c478bd9Sstevel@tonic-gate int logcol; /* Logical column */
8187c478bd9Sstevel@tonic-gate
8197c478bd9Sstevel@tonic-gate tabs = spectbl[p];
8207c478bd9Sstevel@tonic-gate tfirst = tlast = work;
8217c478bd9Sstevel@tonic-gate logcol = 1;
8227c478bd9Sstevel@tonic-gate center();
8237c478bd9Sstevel@tonic-gate while (pfirst <= plast) {
8247c478bd9Sstevel@tonic-gate if (logcol >= *tabs) tabs++;
8257c478bd9Sstevel@tonic-gate switch (c = *pfirst++) {
8267c478bd9Sstevel@tonic-gate case '\b':
8277c478bd9Sstevel@tonic-gate if (logcol > 1) logcol--;
8287c478bd9Sstevel@tonic-gate *tlast++ = c;
8297c478bd9Sstevel@tonic-gate if (logcol < *tabs) tabs--;
8307c478bd9Sstevel@tonic-gate break;
8317c478bd9Sstevel@tonic-gate case '\t':
8327c478bd9Sstevel@tonic-gate while (logcol < *tabs) {
8337c478bd9Sstevel@tonic-gate *tlast++ = ' ';
8347c478bd9Sstevel@tonic-gate logcol++;
8357c478bd9Sstevel@tonic-gate }
8367c478bd9Sstevel@tonic-gate tabs++;
8377c478bd9Sstevel@tonic-gate break;
8387c478bd9Sstevel@tonic-gate default:
8397c478bd9Sstevel@tonic-gate *tlast++ = c;
8407c478bd9Sstevel@tonic-gate logcol++;
8417c478bd9Sstevel@tonic-gate break;
8427c478bd9Sstevel@tonic-gate }
8437c478bd9Sstevel@tonic-gate }
8447c478bd9Sstevel@tonic-gate pfirst = tfirst;
8457c478bd9Sstevel@tonic-gate plast = --tlast;
8467c478bd9Sstevel@tonic-gate }
847*779fc935Sceastha /*
848*779fc935Sceastha * Add SCCS SID (generated by a "get -m" command) to the end of each line.
849*779fc935Sceastha * Sequence is as follows for EACH line:
850*779fc935Sceastha * Check for at least 1 tab. Err if none.
851*779fc935Sceastha * Strip off all char up to & including first tab.
852*779fc935Sceastha * If more than 8 char were stripped, the 8 th is replaced by
853*779fc935Sceastha * a '*' & the remainder are discarded.
854*779fc935Sceastha * Unless user specified an "a", append blanks to fill
855*779fc935Sceastha * out line to eff. line length (default= 72 char).
856*779fc935Sceastha * Truncate lines > eff. line length (default=72).
857*779fc935Sceastha * Add stripped char to end of line.
858*779fc935Sceastha */
859*779fc935Sceastha static void
sstrip(void)860*779fc935Sceastha sstrip(void)
8617c478bd9Sstevel@tonic-gate {
862*779fc935Sceastha int i, k;
863*779fc935Sceastha char *c, *savec;
8647c478bd9Sstevel@tonic-gate
8657c478bd9Sstevel@tonic-gate k = -1;
8667c478bd9Sstevel@tonic-gate c = pfirst;
867*779fc935Sceastha while (*c != '\t' && *c != '\n') {
868*779fc935Sceastha k++;
869*779fc935Sceastha c++;
870*779fc935Sceastha }
871*779fc935Sceastha if (*c != '\t') {
872*779fc935Sceastha (void) fprintf(stderr, "not -s format\r\n");
873*779fc935Sceastha exit(1);
874*779fc935Sceastha }
8757c478bd9Sstevel@tonic-gate
8767c478bd9Sstevel@tonic-gate savec = c;
8777c478bd9Sstevel@tonic-gate c = pfirst;
8787c478bd9Sstevel@tonic-gate savek = (k > 7) ? 7 : k;
879*779fc935Sceastha for (i = 0; i <= savek; i++) savchr[i] = *c++; /* Tab not saved */
880*779fc935Sceastha if (k > 7) savchr[7] = '*';
8817c478bd9Sstevel@tonic-gate
8827c478bd9Sstevel@tonic-gate pfirst = ++savec; /* Point pfirst to char after tab */
8837c478bd9Sstevel@tonic-gate }
8847c478bd9Sstevel@tonic-gate /* ================================================================= */
8857c478bd9Sstevel@tonic-gate
886*779fc935Sceastha static void
sadd(void)887*779fc935Sceastha sadd(void)
8887c478bd9Sstevel@tonic-gate {
889*779fc935Sceastha int i;
8907c478bd9Sstevel@tonic-gate
891*779fc935Sceastha for (i = 0; i <= savek; i++) *plast++ = savchr[i];
8927c478bd9Sstevel@tonic-gate *plast = '\n';
8937c478bd9Sstevel@tonic-gate }
894*779fc935Sceastha
895*779fc935Sceastha static void
outputtabs(int p)896*779fc935Sceastha outputtabs(int p) /* Contract according to output tab specifications. */
897*779fc935Sceastha /* Pointer to tab specification. */
8987c478bd9Sstevel@tonic-gate {
8997c478bd9Sstevel@tonic-gate int *tabs; /* Pointer to tabs */
9007c478bd9Sstevel@tonic-gate char *tfirst, /* Pointer to new buffer start */
9017c478bd9Sstevel@tonic-gate *tlast, /* Pointer to new buffer end */
9027c478bd9Sstevel@tonic-gate *mark; /* Marker pointer */
903*779fc935Sceastha char c; /* Character being scanned */
9047c478bd9Sstevel@tonic-gate int logcol; /* Logical column */
9057c478bd9Sstevel@tonic-gate
9067c478bd9Sstevel@tonic-gate tabs = spectbl[p];
9077c478bd9Sstevel@tonic-gate tfirst = tlast = pfirst;
9087c478bd9Sstevel@tonic-gate logcol = 1;
9097c478bd9Sstevel@tonic-gate while (pfirst <= plast) {
9107c478bd9Sstevel@tonic-gate if (logcol == *tabs) tabs++;
9117c478bd9Sstevel@tonic-gate switch (c = *pfirst++) {
9127c478bd9Sstevel@tonic-gate case '\b':
9137c478bd9Sstevel@tonic-gate if (logcol > 1) logcol--;
9147c478bd9Sstevel@tonic-gate *tlast++ = c;
9157c478bd9Sstevel@tonic-gate if (logcol < *tabs) tabs--;
9167c478bd9Sstevel@tonic-gate break;
9177c478bd9Sstevel@tonic-gate case ' ':
9187c478bd9Sstevel@tonic-gate mark = tlast;
9197c478bd9Sstevel@tonic-gate do {
9207c478bd9Sstevel@tonic-gate *tlast++ = ' ';
9217c478bd9Sstevel@tonic-gate logcol++;
9227c478bd9Sstevel@tonic-gate if (logcol == *tabs) {
9237c478bd9Sstevel@tonic-gate *mark++ = '\t';
9247c478bd9Sstevel@tonic-gate tlast = mark;
9257c478bd9Sstevel@tonic-gate tabs++;
9267c478bd9Sstevel@tonic-gate }
9277c478bd9Sstevel@tonic-gate } while (*pfirst++ == ' ');
9287c478bd9Sstevel@tonic-gate pfirst--;
9297c478bd9Sstevel@tonic-gate break;
9307c478bd9Sstevel@tonic-gate default:
9317c478bd9Sstevel@tonic-gate logcol++;
9327c478bd9Sstevel@tonic-gate *tlast++ = c;
9337c478bd9Sstevel@tonic-gate break;
9347c478bd9Sstevel@tonic-gate }
9357c478bd9Sstevel@tonic-gate }
9367c478bd9Sstevel@tonic-gate pfirst = tfirst;
9377c478bd9Sstevel@tonic-gate plast = --tlast;
9387c478bd9Sstevel@tonic-gate }
939