home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 January
/
usenetsourcesnewsgroupsinfomagicjanuary1994.iso
/
sources
/
unix
/
volume18
/
ctrl
< prev
next >
Wrap
Text File
|
1989-04-19
|
6KB
|
367 lines
Subject: v18i110: Transform symbolic to real escape sequences
Newsgroups: comp.sources.unix
Sender: sources
Approved: rsalz@uunet.UU.NET
Submitted-by: Maarten Litmaath <maart@cs.vu.nl>
Posting-number: Volume 18, Issue 110
Archive-name: ctrl
with this little utility a lot of annoyance can be avoided. For example
unesc '^[[7m\fIStandout mode on a Sun console\fR.'
is equivalent to
echo "`esc`[7m\fIStandout mode on a Sun console\fR."
Regards,
Maarten Litmaath @ VU Amsterdam:
maart@cs.vu.nl, mcvax!botter!maart
: This is a shar archive. Extract with sh, not csh.
: This archive ends with exit, so do not worry about trailing junk.
: --------------------------- cut here --------------------------
PATH=/bin:/usr/bin:/usr/ucb
echo Extracting 'ctrl.1'
sed 's/^X//' > 'ctrl.1' << '+ END-OF-FILE ''ctrl.1'
X.TH CTRL 1 Mar\ 29\ 1989
X.SH NAME
Xctrl, unesc \- translate printable characters to
X.br
X.RS
X.RS
X.RS
Xcorresponding control characters
X.RE
X.RE
X.RE
X.br
Xesc, bell, beep \- generate ESC and BEL characters
X.SH SYNOPSIS
X.B ctrl
X.I string
X.br
X.B unesc
X.I symbolic escape sequence
X.br
X.B esc
X.br
X.B bell
X[
X.I count
X]
X.br
X.B beep
X[
X.I count
X]
X.SH DESCRIPTION
XThe use of
X.B ctrl
Xin all its forms is to avoid real escape sequences in ordinary ASCII files,
Xe.g.
X.I .login.
X.SH EXAMPLES
Xunesc '^[[7m\fIStandout mode on a Sun console\fR.'
X.br
X.br
X.RS
Xis equivalent to
X.RE
X.br
X.br
Xecho "`esc`[7m\fIStandout mode on a Sun console\fR."
X.br
X.br
Xctrl GG G
X.br
X.br
X.RS
Xis equivalent to
X.RE
X.br
X.br
Xbell 3
X.SH AUTHOR
XMaarten Litmaath @ VU Informatika Amsterdam
X.br
X(maart@cs.vu.nl)
+ END-OF-FILE ctrl.1
chmod 'u=rw,g=r,o=r' 'ctrl.1'
set `wc -c 'ctrl.1'`
count=$1
case $count in
759) :;;
*) echo 'Bad character count in ''ctrl.1' >&2
echo 'Count should be 759' >&2
esac
echo Extracting 'Makefile'
sed 's/^X//' > 'Makefile' << '+ END-OF-FILE ''Makefile'
Xctrl: ctrl.o myperror.o
X cc -O -x -n -s -o ctrl ctrl.o myperror.o
X
Xctrl.o: ctrl.c
X cc -O -x -c ctrl.c
X
Xmyperror.o: myperror.c
X cc -O -x -c myperror.c
X
Xinstall: ctrl
X sh install.sh
X @echo Ready.
+ END-OF-FILE Makefile
chmod 'u=rw,g=r,o=r' 'Makefile'
set `wc -c 'Makefile'`
count=$1
case $count in
199) :;;
*) echo 'Bad character count in ''Makefile' >&2
echo 'Count should be 199' >&2
esac
echo Extracting 'install.sh'
sed 's/^X//' > 'install.sh' << '+ END-OF-FILE ''install.sh'
X#!/bin/sh
X
X[ "x`echo -n x`" = "x-n x" ] && {
X C=\\c
X N=
X} || {
X C=
X N=-n
X}
X
Xecho $N 'Into which directory do you want "ctrl" c.s. to be placed? '$C
X
Xwhile :
Xdo
X read dir
X [ = = "$dir" -o != = "$dir" ] && dir=./$dir
X [ -d "$dir" -a -x "$dir" -a -w "$dir" ] && break
X echo "There is no writable directory '$dir'."
X echo $N 'Try again: '$C
Xdone
X
Xmv ctrl "$dir" || exit 1
X
Xcd "$dir" || exit 1
X
Xln ctrl unesc
Xln ctrl esc
Xln ctrl bell
Xln ctrl beep
+ END-OF-FILE install.sh
chmod 'u=rw,g=r,o=r' 'install.sh'
set `wc -c 'install.sh'`
count=$1
case $count in
442) :;;
*) echo 'Bad character count in ''install.sh' >&2
echo 'Count should be 442' >&2
esac
echo Extracting 'ctrl.c'
sed 's/^X//' > 'ctrl.c' << '+ END-OF-FILE ''ctrl.c'
X/*
X * ctrl.c - translate printable characters to corresponding control characters
X * doesn't use stdio, to keep the size small
X *
X * author: Maarten Litmaath @ VU Informatika Amsterdam (maart@cs.vu.nl)
X */
X
X#define BUF_SIZE 1024
X
X
Xint ctrl(), unesc(), esc(), bell();
X
Xstruct func {
X char *name;
X int (*func)();
X } ftab[] = {
X { "ctrl", ctrl },
X { "unesc", unesc },
X { "esc", esc },
X { 0, bell }
X };
X
X
Xmain(argc, argv)
Xint argc;
Xregister char **argv;
X{
X struct func *f = ftab;
X char buf[BUF_SIZE], *name, *rindex();
X int n, status = 0;
X void myperror();
X
X
X if (!(name = rindex(*argv, '/')))
X name = *argv;
X else
X ++name;
X ++argv;
X
X while (f->name && strcmp(f->name, name) != 0)
X ++f;
X
X n = (*f->func)(argv, buf);
X
X if (write(1, buf, n) != n) {
X myperror(name);
X status = 1;
X }
X
X _exit(status);
X}
X
X
Xint ctrl(argv, buf)
Xregister char **argv;
Xchar *buf;
X{
X register char *s, *p = buf, c;
X
X
X while (s = *argv++) {
X while (c = *s++)
X *p++ = '?' <= c && c <= '_' ? c ^ 0100 : c;
X *p++ = ' ';
X }
X
X if (p != buf)
X --p;
X
X *p++ = '\n';
X *p = '\0';
X
X return p - buf;
X}
X
X
Xint unesc(argv, buf)
Xregister char **argv;
Xchar *buf;
X{
X register char *s, *p = buf, c;
X
X
X while (s = *argv++) {
X while (c = *s++)
X switch (c) {
X case '\\':
X *p++ = *s ? *s++ : c;
X break;
X case '^':
X *p++ = '?' <= *s && *s <= '_' ? *s++ ^ 0100 :
X *s ? *s++ : c;
X break;
X default:
X *p++ = c;
X break;
X }
X *p++ = ' ';
X }
X
X if (p != buf)
X --p;
X
X *p++ = '\n';
X *p = '\0';
X
X return p - buf;
X}
X
X
Xint esc(argv, buf)
Xchar **argv;
Xregister char *buf;
X{
X *buf++ = '\033';
X *buf++ = '\n';
X *buf = '\0';
X return 2;
X}
X
X
Xint bell(argv, buf)
Xchar **argv, *buf;
X{
X register char *p = buf;
X register int n = *argv ? atoi(*argv) : 1;
X
X
X while (--n >= 0)
X *p++ = '\007';
X
X *p++ = '\n';
X *p = '\0';
X return p - buf;
X}
+ END-OF-FILE ctrl.c
chmod 'u=rw,g=r,o=r' 'ctrl.c'
set `wc -c 'ctrl.c'`
count=$1
case $count in
1818) :;;
*) echo 'Bad character count in ''ctrl.c' >&2
echo 'Count should be 1818' >&2
esac
echo Extracting 'myperror.c'
sed 's/^X//' > 'myperror.c' << '+ END-OF-FILE ''myperror.c'
X/*
X * myperror.c - unbuffered version of stdio perror(3s)
X *
X * author: Maarten Litmaath @ VU Informatika Amsterdam (maart@cs.vu.nl)
X */
X
X#define ERRBUF_SIZE 32
X
X
Xvoid myperror(s)
Xchar *s;
X{
X extern int errno, sys_nerr;
X extern char *sys_errlist[];
X register char *p;
X register int n = 0;
X int min = 0;
X char buf[ERRBUF_SIZE];
X
X
X if (s && *s) {
X (void) write(2, s, strlen(s));
X (void) write(2, ": ", 2);
X }
X
X if (0 <= errno && errno < sys_nerr)
X p = sys_errlist[errno];
X else {
X p ="Unknown error ";
X n = errno;
X }
X
X (void) write(2, p, strlen(p));
X
X if (n) {
X p = buf + sizeof buf;
X if (n < 0) {
X min = 1;
X n *= -1;
X }
X while (n) {
X *--p = '0' + n % 10;
X n /= 10;
X }
X if (min)
X *--p = '-';
X (void) write(2, p, buf + sizeof buf - p);
X }
X (void) write(2, "\n", 1);
X}
+ END-OF-FILE myperror.c
chmod 'u=rw,g=r,o=r' 'myperror.c'
set `wc -c 'myperror.c'`
count=$1
case $count in
789) :;;
*) echo 'Bad character count in ''myperror.c' >&2
echo 'Count should be 789' >&2
esac
exit 0