From mboxrd@z Thu Jan 1 00:00:00 1970 MIME-version: 1.0 Message-id: <082FE5DA-8AC5-4FE8-A37C-9C8C0623A088@mac.com> From: Pietro Gagliardi To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net> In-reply-to: <8ccc8ba40901230250u2ddb93d7hcdb57c123a111ffa@mail.gmail.com> Content-type: multipart/signed; protocol="application/pgp-signature"; micalg=pgp-sha1; boundary=Apple-Mail-9-999761158 Date: Fri, 23 Jan 2009 06:27:11 -0500 References: <8ccc8ba40901230250u2ddb93d7hcdb57c123a111ffa@mail.gmail.com> Content-transfer-encoding: 7bit Subject: Re: [9fans] Small program "PlanKey" (paraphrase of DOSKey) Topicbox-Message-UUID: 85517388-ead4-11e9-9d60-3106f5b1d025 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --Apple-Mail-9-999761158 Content-Type: multipart/alternative; boundary=Apple-Mail-8-999761128 --Apple-Mail-8-999761128 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Erik modified rc directly to add a history system. On Jan 23, 2009, at 5:50 AM, Francisco J Ballesteros wrote: > Russ posted two script called " and "" time ago on this list, > IIRC. > > > On Fri, Jan 23, 2009 at 11: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-8-999761128 Content-Type: text/html; charset=US-ASCII Content-Transfer-Encoding: quoted-printable
Erik modified rc directly = to add a history system.

On Jan 23, 2009, = at 5:50 AM, Francisco J Ballesteros wrote:

Russ = posted two script called " and "" time ago on this = list,
IIRC.


On Fri, Jan 23, 2009 at 11:03 AM, pavel.klinkovsky@gmail.com<= br><pavel.klinkovsky@gmail.com&= gt; 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)
=             &n= bsp; sysfatal("%r");
=       write(cfd, "rawon", = 5);

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

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

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

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

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

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

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

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

=       close(cfd);

=       exits(nil);
}




= --Apple-Mail-8-999761128-- --Apple-Mail-9-999761158 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) iEYEARECAAYFAkl5qY8ACgkQuv7AVNQDs+woEwCfcryJw/oyZ+iwGwz73omqlBF4 MLMAn0RcHU7Lueg5dzAeU886lP+y+qjU =uD5G -----END PGP SIGNATURE----- --Apple-Mail-9-999761158--