1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00
cde/src/lib/libcmd/fmt.c
Martijn Dekker a1f5c99204 INIT: remove proto, ratz (re: 46593a89, 6137b99a); major cleanup
This takes another step towards cleaning up the build system. We
now do not even pretend to be theoretically compatible with
pre-1989 K&R C compilers or with C++ compilers. In practice, this
had already been broken for many years due to bit rot.

Commit 46593a89 already removed the license handling enormity that
depended on proto, so now we can cleanly remove it altogether. But
we do need to leave some backwards compatibility stubs to keep the
build system compatible with older AST code; it should remain
possible to build older ksh versions with the current build system
(the bin/ and src/cmd/INIT/ directories) for testing purposes.

So as of now there is no more __MANGLE__d rubbish in your generated
header files. This is only about a quarter of a century overdue...

This commit also includes a huge amount of code cleanup to remove
thousands of unused K&R C fallbacks and other cruft, particularly
in libast. This code base should now be a little easier to
understand for people who are familiar with a modern(ish) C
standard.

ratz is now also removed; this was a standalone and simplified 2005
version of gunzip. As of 6137b99a, none of our code uses it, even
theoretically. And the real g(un)zip is now everywhere.

src/cmd/INIT/proto.c, src/cmd/INIT/ratz.c:
- Removed.

COPYRIGHT:
- Remove zlib license; this only applied to ratz.

bin/package, src/cmd/INIT/package.sh:
- Related cleanups.
- Unset LC_ALL before invoking a new shell, respecting the user's
  locale again and avoiding multibyte character corruption on the
  command line.

src/cmd/INIT/proto.sh:
- Add stub for backwards compatibility with Mamfiles that depend on
  proto. It does nothing but pass input without modification and is
  now installed as the new arch/*/bin/proto by src/cmd/INIT/Mamfile.

src/cmd/INIT/iffe.sh:
- Ignore the proto-related -e (--package) and -p (--prototyped)
  options; keep parsing them for backwards compatibility.
- Trim the macros passed to every test to their standard C
  versions, removing K&R C and C++ versions. These are now
  considered to be for backwards compatibility only.

src/cmd/INIT/iffe.tst:
- Remove proto(1) mangling code.
  By the way, iffe can be regression-tested as follows:
        $ bin/package use   # set up environment in a child shell
        $ regress src/cmd/INIT/iffe.tst
        $ exit              # leave package environment

src/cmd/INIT/make.probe, src/cmd/INIT/probe.win32:
- Remove code to handle C++.

src/lib/libast/features/common:
- As in iffe.sh above, trim macros designed for compatibility with
  C++ and ancient C compilers to their standard C versions and
  comment that they are for backwards compatibility with AST code.
  This is needed to keep all the old ast and ksh code compiling.

src/cmd/ksh93/sh/init.c,
src/cmd/ksh93/sh/name.c:
- Clarify libshell ABI compatibility function versions of macros.
  A "proto workaround" comment in the original code mislead me into
  thinking this had something to do with the removed proto(1), but
  it's unrelated. Call the workaround macro BYPASS_MACRO instead.

src/cmd/ksh93/include/defs.h:
- sh_sigcheck() macro: allow &sh as an argument: parenthesise shp.

src/cmd/ksh93/sh/nvtype.c:
- Remove unused nv_mkstruct() function. (re: d0a5cab1)

**/features/*:
- Remove obsolete iffe 'set prototyped' option.

**/Mamfile:
- Remove all references to the ast/prototyped.h header.
- Remove all use of the proto command. Simply copy instead.

*** 850-ish source files: ***
- Remove all '#pragma prototyped' directives.
- Remove all C++ compat code conditional upon defined(__cplusplus).
- Remove all use of the _ARG_ macro, which on standard C expands to
  its argument:
        #define _ARG_(x)        x
  (on K&R C, it expanded to nothing)
- Remove all use of _BEGIN_EXTERNS_ and _END_EXTERNS_ macros (empty
  on standard C; this was for C++ compatibility)
- Reduce all #if __STD_C (standard code) #else (K&R code) #endif
  blocks to the standard code only, without use of the macro.
- Same for _STD_ macro which seems to have had the same function.
- Change all instances of 'Void_t' to standard 'void'.
2021-12-24 07:05:22 +00:00

638 lines
13 KiB
C

/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1992-2012 AT&T Intellectual Property *
* Copyright (c) 2020-2021 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.eclipse.org/org/documents/epl-v10.html *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* *
***********************************************************************/
static const char usage[] =
"[-?\n@(#)$Id: fmt (AT&T Research) 2007-01-02 $\n]"
"[--catalog?" ERROR_CATALOG "]"
"[+NAME?fmt - simple text formatter]"
"[+DESCRIPTION?\bfmt\b reads the input files and left justifies space "
"separated words into lines \awidth\a characters or less in length and "
"writes the lines to the standard output. The standard input is read if "
"\b-\b or no files are specified. Blank lines and interword spacing are "
"preserved in the output. Indentation is preserved, and lines with "
"identical indentation are joined and justified.]"
"[+?\bfmt\b is meant to format mail messages prior to sending, but may "
"also be useful for other simple tasks. For example, in \bvi\b(1) the "
"command \b:!}fmt\b will justify the lines in the current paragraph.]"
"[c:crown-margin?Preserve the indentation of the first two lines within "
"a paragraph, and align the left margin of each subsequent line with "
"that of the second line.]"
"[o:optget?Format concatenated \boptget\b(3) usage strings.]"
"[s:split-only?Split lines only; do not join short lines to form longer "
"ones.]"
"[u:uniform-spacing?One space between words, two after sentences.]"
"[w:width?Set the output line width to \acolumns\a.]#[columns:=72]"
"\n\n"
"[ file ... ]"
"\n\n"
"[+SEE ALSO?\bmailx\b(1), \bnroff\b(1), \btroff\b(1), \bvi\b(1), "
"\boptget\b(3)]"
;
#include <cmd.h>
#include <ctype.h>
typedef struct Fmt_s
{
long flags;
char* outp;
char* outbuf;
char* endbuf;
Sfio_t* in;
Sfio_t* out;
int indent;
int nextdent;
int nwords;
int prefix;
int quote;
int retain;
int section;
} Fmt_t;
#define INDENT 4
#define TABSZ 8
#define isoption(fp,c) ((fp)->flags&(1L<<((c)-'a')))
#define setoption(fp,c) ((fp)->flags|=(1L<<((c)-'a')))
#define clroption(fp,c) ((fp)->flags&=~(1L<<((c)-'a')))
static void
outline(Fmt_t* fp)
{
register char* cp = fp->outbuf;
int n = 0;
int c;
int d;
if (!fp->outp)
return;
while (fp->outp[-1] == ' ')
fp->outp--;
*fp->outp = 0;
while (*cp++ == ' ')
n++;
if (n >= TABSZ)
{
n /= TABSZ;
cp = &fp->outbuf[TABSZ*n];
while (n--)
*--cp = '\t';
}
else
cp = fp->outbuf;
fp->nwords = 0;
if (!isoption(fp, 'o'))
sfputr(fp->out, cp, '\n');
else if (*cp)
{
n = fp->indent;
if (*cp != '[')
{
if (*cp == ' ')
cp++;
n += INDENT;
}
while (n--)
sfputc(fp->out, ' ');
if (fp->quote)
{
if ((d = (fp->outp - cp)) <= 0)
c = 0;
else if ((c = fp->outp[-1]) == 'n' && d > 1 && fp->outp[-2] == '\\')
c = '}';
sfprintf(fp->out, "\"%s%s\"\n", cp, c == ']' || c == '{' || c == '}' ? "" : " ");
}
else
sfputr(fp->out, cp, '\n');
if (fp->nextdent)
{
fp->indent += fp->nextdent;
fp->endbuf -= fp->nextdent;
fp->nextdent = 0;
}
}
fp->outp = 0;
}
static void
split(Fmt_t* fp, char* buf, int splice)
{
register char* cp;
register char* ep;
register char* qp;
register int c = 1;
register int q = 0;
register int n;
int prefix;
for (ep = buf; *ep == ' '; ep++);
prefix = ep - buf;
/*
* preserve blank lines
*/
if ((*ep == 0 || *buf == '.') && !isoption(fp, 'o'))
{
if (*ep)
prefix = strlen(buf);
outline(fp);
strcpy(fp->outbuf, buf);
fp->outp = fp->outbuf+prefix;
outline(fp);
return;
}
if (fp->prefix < prefix && !isoption(fp, 'c'))
outline(fp);
if (!fp->outp || prefix < fp->prefix)
fp->prefix = prefix;
while (c)
{
cp = ep;
while (*ep == ' ')
ep++;
if (cp != ep && isoption(fp, 'u'))
cp = ep-1;
while (c = *ep)
{
if (c == ' ')
break;
ep++;
/*
* skip over \space
*/
if (c == '\\' && *ep)
ep++;
}
n = (ep-cp);
if (n && isoption(fp, 'o'))
{
for (qp = cp; qp < ep; qp++)
if (*qp == '\\')
qp++;
else if (*qp == '"')
q = !q;
if (*(ep-1) == '"')
goto skip;
}
if (fp->nwords > 0 && &fp->outp[n] >= fp->endbuf && !fp->retain && !q)
outline(fp);
skip:
if (fp->nwords == 0)
{
if (fp->prefix)
memset(fp->outbuf, ' ', fp->prefix);
fp->outp = &fp->outbuf[fp->prefix];
while (*cp == ' ')
cp++;
n = (ep-cp);
}
memcpy(fp->outp, cp, n);
fp->outp += n;
fp->nwords++;
}
if (isoption(fp, 's') || *buf == 0)
outline(fp);
else if (fp->outp)
{
/*
* two spaces at ends of sentences
*/
if (!isoption(fp, 'o') && strchr(".:!?", fp->outp[-1]))
*fp->outp++ = ' ';
if (!splice && !fp->retain && (!fp->quote || (fp->outp - fp->outbuf) < 2 || fp->outp[-2] != '\\' || fp->outp[-1] != 'n' && fp->outp[-1] != 't' && fp->outp[-1] != ' '))
*fp->outp++ = ' ';
}
}
static int
dofmt(Fmt_t* fp)
{
register int c;
int b;
int x;
int splice;
char* cp;
char* dp;
char* ep;
char* lp;
char* tp;
char buf[8192];
cp = 0;
while (cp || (cp = sfgetr(fp->in, '\n', 0)) && !(splice = 0) && (lp = cp + sfvalue(fp->in) - 1) || (cp = sfgetr(fp->in, '\n', SF_LASTR)) && (splice = 1) && (lp = cp + sfvalue(fp->in)))
{
if (isoption(fp, 'o'))
{
if (!isoption(fp, 'i'))
{
setoption(fp, 'i');
b = 0;
while (cp < lp)
{
if (*cp == ' ')
b += 1;
else if (*cp == '\t')
b += INDENT;
else
break;
cp++;
}
fp->indent = roundof(b, INDENT);
}
else
while (cp < lp && (*cp == ' ' || *cp == '\t'))
cp++;
if (!isoption(fp, 'q') && cp < lp)
{
setoption(fp, 'q');
if (*cp == '"')
{
ep = lp;
while (--ep > cp)
if (*ep == '"')
{
fp->quote = 1;
break;
}
else if (*ep != ' ' && *ep != '\t')
break;
}
}
}
again:
dp = buf;
ep = 0;
for (b = 1;; b = 0)
{
if (cp >= lp)
{
cp = 0;
break;
}
c = *cp++;
if (isoption(fp, 'o'))
{
if (c == '\\')
{
x = 0;
c = ' ';
cp--;
while (cp < lp)
{
if (*cp == '\\')
{
cp++;
if ((lp - cp) < 1)
{
c = '\\';
break;
}
if (*cp == 'n')
{
cp++;
c = '\n';
if ((lp - cp) > 2)
{
if (*cp == ']' || *cp == '@' && *(cp + 1) == '(')
{
*dp++ = '\\';
*dp++ = 'n';
c = *cp++;
break;
}
if (*cp == '\\' && *(cp + 1) == 'n')
{
cp += 2;
*dp++ = '\n';
break;
}
}
}
else if (*cp == 't' || *cp == ' ')
{
cp++;
x = 1;
c = ' ';
}
else
{
if (x && dp != buf && *(dp - 1) != ' ')
*dp++ = ' ';
*dp++ = '\\';
c = *cp++;
break;
}
}
else if (*cp == ' ' || *cp == '\t')
{
cp++;
c = ' ';
x = 1;
}
else
{
if (x && c != '\n' && dp != buf && *(dp - 1) != ' ')
*dp++ = ' ';
break;
}
}
if (c == '\n')
{
c = 0;
goto flush;
}
if (c == ' ' && (dp == buf || *(dp - 1) == ' '))
continue;
}
else if (c == '"')
{
if (b || cp >= lp)
{
if (fp->quote)
continue;
fp->section = 0;
}
}
else if (c == '\a')
{
*dp++ = '\\';
c = 'a';
}
else if (c == '\b')
{
*dp++ = '\\';
c = 'b';
}
else if (c == '\f')
{
*dp++ = '\\';
c = 'f';
}
else if (c == '\v')
{
*dp++ = '\\';
c = 'v';
}
else if (c == ']' && (cp >= lp || *cp != ':' && *cp != '#' && *cp != '!'))
{
if (cp < lp && *cp == ']')
{
cp++;
*dp++ = c;
}
else
{
fp->section = 1;
fp->retain = 0;
flush:
*dp++ = c;
*dp = 0;
split(fp, buf, 0);
outline(fp);
goto again;
}
}
else if (fp->section)
{
if (c == '[')
{
if (b)
fp->retain = 1;
else
{
cp--;
c = 0;
goto flush;
}
fp->section = 0;
}
else if (c == '{')
{
x = 1;
for (tp = cp; tp < lp; tp++)
{
if (*tp == '[' || *tp == '\n')
break;
if (*tp == ' ' || *tp == '\t' || *tp == '"')
continue;
if (*tp == '\\' && (lp - tp) > 1)
{
if (*++tp == 'n')
break;
if (*tp == 't' || *tp == '\n')
continue;
}
x = 0;
break;
}
if (x)
{
if (fp->endbuf > (fp->outbuf + fp->indent + 2*INDENT))
fp->nextdent = 2*INDENT;
goto flush;
}
else
fp->section = 0;
}
else if (c == '}')
{
if (fp->indent && (b || *(cp - 2) != 'f'))
{
if (b)
{
fp->indent -= 2*INDENT;
fp->endbuf += 2*INDENT;
}
else
{
cp--;
c = 0;
}
goto flush;
}
else
fp->section = 0;
}
else if (c == ' ' || c == '\t')
continue;
else
fp->section = 0;
}
else if (c == '?' && (cp >= lp || *cp != '?'))
{
if (fp->retain)
{
cp--;
while (cp < lp && *cp != ' ' && *cp != '\t' && *cp != ']' && dp < &buf[sizeof(buf)-3])
*dp++ = *cp++;
if (cp < lp && (*cp == ' ' || *cp == '\t'))
*dp++ = *cp++;
*dp = 0;
split(fp, buf, 0);
dp = buf;
ep = 0;
fp->retain = 0;
if (fp->outp >= fp->endbuf)
outline(fp);
continue;
}
}
else if (c == ' ' || c == '\t')
for (c = ' '; *cp == ' ' || *cp == '\t'; cp++);
}
else if (c == '\b')
{
if (dp > buf)
{
dp--;
if (ep)
ep--;
}
continue;
}
else if (c == '\t')
{
/*
* expand tabs
*/
if (!ep)
ep = dp;
c = isoption(fp, 'o') ? 1 : TABSZ - (dp - buf) % TABSZ;
if (dp >= &buf[sizeof(buf) - c - 3])
{
cp--;
break;
}
while (c-- > 0)
*dp++ = ' ';
continue;
}
else if (!isprint(c))
continue;
if (dp >= &buf[sizeof(buf) - 3])
{
tp = dp;
while (--tp > buf)
if (isspace(*tp))
{
cp -= dp - tp;
dp = tp;
break;
}
ep = 0;
break;
}
if (c != ' ')
ep = 0;
else if (!ep)
ep = dp;
*dp++ = c;
}
if (ep)
*ep = 0;
else
*dp = 0;
split(fp, buf, splice);
}
return 0;
}
int
b_fmt(int argc, char** argv, Shbltin_t* context)
{
register int n;
char* cp;
Fmt_t fmt;
char outbuf[8 * 1024];
fmt.flags = 0;
fmt.out = sfstdout;
fmt.outbuf = outbuf;
fmt.outp = 0;
fmt.endbuf = &outbuf[72];
fmt.indent = 0;
fmt.nextdent = 0;
fmt.nwords = 0;
fmt.prefix = 0;
fmt.quote = 0;
fmt.retain = 0;
fmt.section = 1;
cmdinit(argc, argv, context, ERROR_CATALOG, 0);
for (;;)
{
switch (n = optget(argv, usage))
{
case 'c':
case 'o':
case 's':
case 'u':
setoption(&fmt, n);
continue;
case 'w':
if (opt_info.num < TABSZ || opt_info.num>= sizeof(outbuf))
error(2, "width out of range");
fmt.endbuf = &outbuf[opt_info.num];
continue;
case ':':
error(2, "%s", opt_info.arg);
break;
case '?':
error(ERROR_usage(2), "%s", opt_info.arg);
UNREACHABLE();
}
break;
}
argv += opt_info.index;
if (error_info.errors)
{
error(ERROR_usage(2), "%s", optusage(NiL));
UNREACHABLE();
}
if (isoption(&fmt, 'o'))
setoption(&fmt, 'c');
if (isoption(&fmt, 's'))
clroption(&fmt, 'u');
if (cp = *argv)
argv++;
do {
if (!cp || streq(cp, "-"))
fmt.in = sfstdin;
else if (!(fmt.in = sfopen(NiL, cp, "r")))
{
error(ERROR_system(0), "%s: cannot open", cp);
error_info.errors = 1;
continue;
}
dofmt(&fmt);
if (fmt.in != sfstdin)
sfclose(fmt.in);
} while (cp = *argv++);
outline(&fmt);
if (sfsync(sfstdout))
error(ERROR_system(0), "write error");
return error_info.errors != 0;
}