From mboxrd@z Thu Jan 1 00:00:00 1970 MIME-version: 1.0 Message-id: From: Pietro Gagliardi To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net> In-reply-to: Content-type: multipart/signed; protocol="application/pgp-signature"; micalg=pgp-sha1; boundary=Apple-Mail-12-1000284520 Date: Fri, 23 Jan 2009 06:35:54 -0500 References: Content-transfer-encoding: 7bit Subject: Re: [9fans] Small program "PlanKey" (paraphrase of DOSKey) Topicbox-Message-UUID: 85be601a-ead4-11e9-9d60-3106f5b1d025 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --Apple-Mail-12-1000284520 Content-Type: multipart/alternative; boundary=Apple-Mail-11-1000284496 --Apple-Mail-11-1000284496 Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit This code is interesting. I do see one problem: commands that take up more than one line won't be stored in the history properly, so each line will get its own entry. But this is a nice use of pipes! On Jan 23, 2009, at 5:03 AM, pavel.klinkovsky@gmail.com wrote: > Hi all, > > In Plan9 I missed the simple way to repeat previous (or previous of > previous etc.) command in the terminal. > I prepared a very small (an stupid) program to allow that. > > Compile the following source code and run it in that way: > 8.out | rc -i > > You can insert the commands, and if you want to walk through the > "history", just press CTRL+K (backward) or CTRL+L (foreward). > > That is all, folks. ;-) > > Pavel > > > #include > #include > > enum > { > KCtrlD = 0x04, > KCtrlK = 0x0B, > KCtrlL = 0x0C, > KDelete = 0x7F, > > MaxLen = 128, > MaxDepth = 10 > }; > > struct Line > { > int len; > char buf[MaxLen]; > }; > typedef struct Line Line; > > void > main(void) > { > // Console control file descriptor > int cfd; > // End flag > int end = 0; > // Actual read character > char c; > // Array of BackSpaces > char bs[MaxLen]; > // Array of Lines > Line line[MaxDepth]; > // Index of actual line > int act = 0; > // Index of stored line > int stored = 0; > > memset(bs, '\b', sizeof(bs)); > memset(line, 0, sizeof(line)); > > cfd = open("/dev/consctl", OWRITE); > if (cfd < 0) > sysfatal("%r"); > write(cfd, "rawon", 5); > > while (!end) { > if (read(0, &c, sizeof(c)) < 0) > sysfatal("%r"); > > switch (c) { > case KCtrlD: > case KDelete: > end++; > break; > > case '\b': > if (line[act].len > 0) { > line[act].len--; > write(2, &c, sizeof(c)); > } > break; > > case '\n': > if (line[act].len > 0) > write(1, line[act].buf, line[act].len); > write(1, &c, sizeof(c)); > write(2, &c, sizeof(c)); > act = stored = (act + 1) % MaxDepth; > line[act].len = 0; > break; > > case KCtrlK: > write(2, bs, line[act].len); > stored = (stored + MaxDepth - 1) % MaxDepth; > line[act] = line[stored]; > write(2, line[act].buf, line[act].len); > break; > > case KCtrlL: > write(2, bs, line[act].len); > stored = (stored + 1) % MaxDepth; > line[act] = line[stored]; > write(2, line[act].buf, line[act].len); > break; > > case -0x11: > read(0, &c, sizeof(c)); > read(0, &c, sizeof(c)); > break; > > default: > if (line[act].len < MaxLen) { > line[act].buf[line[act].len++] = c; > write(2, &c, sizeof(c)); > } > break; > } > } > > close(cfd); > > exits(nil); > } > --Apple-Mail-11-1000284496 Content-Type: text/html; charset=US-ASCII Content-Transfer-Encoding: quoted-printable
This code is interesting. = I do see one problem: commands that take up more than one line won't be = stored in the history properly, so each line will get its own entry. But = this is a nice use of pipes!

On Jan 23, 2009, at 5:03 = AM, pavel.klinkovsky@gmail.com = wrote:

Hi all,

In Plan9 I missed the simple way to = repeat previous (or previous of
previous etc.) command in the = terminal.
I prepared a very small (an stupid) program to allow = that.

Compile the following source code and run it in that = way:
8.out | rc -i

You can insert the commands, and if you = want to walk through the
"history", just press CTRL+K (backward) or = CTRL+L (foreward).

That is all, folks. = ;-)

Pavel


#include <u.h>
#include = <libc.h>

enum
{
KCtrlD =3D 0x04,
KCtrlK =3D = 0x0B,
= KCtrlL =3D 0x0C,
KDelete =3D 0x7F,

MaxLen =3D = 128,
= MaxDepth =3D 10
};

struct Line
{
int = len;
= char buf[MaxLen];
};
typedef struct Line = Line;

void
main(void)
{
// Console control file = descriptor
= int cfd;
// End flag
int end =3D= 0;
= // Actual read character
char c;
// Array = of BackSpaces
= char bs[MaxLen];
// Array of Lines
Line = line[MaxDepth];
// Index of actual line
int act =3D= 0;
= // Index of stored line
int stored =3D 0;

= memset(bs, '\b', sizeof(bs));
memset(line, 0, = sizeof(line));

cfd =3D open("/dev/consctl", = OWRITE);
= if (cfd < 0)
sysfatal("%r");
= write(cfd, "rawon", 5);

while (!end) {
if = (read(0, &c, sizeof(c)) < 0)
sysfatal("%r");

switch = (c) {
= = case KCtrlD:
case KDelete:
= end++;
= = = break;

case '\b':
if = (line[act].len > 0) {
line[act].len--;
write(2, = &c, sizeof(c));
}
break;

case = '\n':
= = = if (line[act].len > 0)
write(1, line[act].buf, = line[act].len);
write(1, &c, = sizeof(c));
= = = write(2, &c, sizeof(c));
act =3D stored =3D (act + 1) % = MaxDepth;
= = = line[act].len =3D 0;
break;

case = KCtrlK:
= = = write(2, bs, line[act].len);
stored =3D (stored + MaxDepth - = 1) % MaxDepth;
line[act] =3D = line[stored];
= = = write(2, line[act].buf, line[act].len);
= break;

case KCtrlL:
write(2, = bs, line[act].len);
stored =3D (stored + 1) % = MaxDepth;
= = = line[act] =3D line[stored];
write(2, line[act].buf, = line[act].len);
break;

case = -0x11:
= = = read(0, &c, sizeof(c));
read(0, &c, = sizeof(c));
= = = break;

default:
if = (line[act].len < MaxLen) {
line[act].buf[line[act].len++] =3D = c;
= = = = write(2, &c, sizeof(c));
}
break;
= }
= }

= close(cfd);

= exits(nil);
}


= --Apple-Mail-11-1000284496-- --Apple-Mail-12-1000284520 content-type: application/pgp-signature; x-mac-type=70674453; name=PGP.sig content-description: This is a digitally signed message part content-disposition: inline; filename=PGP.sig content-transfer-encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.8 (Darwin) iEYEARECAAYFAkl5q5oACgkQuv7AVNQDs+z4ywCeMaqfp7Hdii+HgQfili3lUFH2 7JwAoJsg5LT/JX50KNbX6jW+BbwmPn/T =KyZ+ -----END PGP SIGNATURE----- --Apple-Mail-12-1000284520--