9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: "pavel.klinkovsky@gmail.com" <pavel.klinkovsky@gmail.com>
To: 9fans@9fans.net
Subject: [9fans] Small program "PlanKey" (paraphrase of DOSKey)
Date: Fri, 23 Jan 2009 10:03:49 +0000	[thread overview]
Message-ID: <e3a04c0c-caee-46f7-b080-7d86bc66a5f8@m4g2000vbp.googlegroups.com> (raw)

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 = 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);
}



             reply	other threads:[~2009-01-23 10:03 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-23 10:03 pavel.klinkovsky [this message]
2009-01-23 10:50 ` Francisco J Ballesteros
2009-01-23 11:27   ` Pietro Gagliardi
2009-01-23 11:35 ` Pietro Gagliardi
2009-01-23 12:56 ` roger peppe
2009-01-23 14:14   ` erik quanstrom
2009-01-26 12:41     ` cej
2009-01-26 10:31   ` pavel.klinkovsky
2009-01-27  3:29   ` Roman Shaposhnik
2009-01-27  3:42     ` erik quanstrom
2009-01-27  4:43       ` Roman Shaposhnik
2009-01-27  6:35         ` Akshat Kumar
2009-01-27  6:44           ` Akshat Kumar
2009-01-27 20:01           ` Roman V. Shaposhnik
2009-01-27 20:10             ` erik quanstrom
2009-01-27 22:01               ` Roman V. Shaposhnik
2009-01-27 22:26                 ` erik quanstrom
2009-01-27 22:58                   ` sqweek
2009-01-27 23:42                     ` erik quanstrom
2009-01-28  0:35                 ` Anthony Sorace
2009-01-28  5:59                   ` lucio
2009-01-28 19:00                     ` erik quanstrom
2009-01-28 22:19                     ` Joel C. Salomon
2009-01-28 22:51                       ` Francisco J Ballesteros
2009-01-29  0:26                         ` Akshat Kumar
2009-01-29  2:09                           ` erik quanstrom
2009-01-29  8:49                             ` cej
2009-01-29 11:50                               ` Anthony Sorace
2009-01-29 13:04                                 ` roger peppe
2009-01-29 13:53                                 ` cej
2009-01-29 16:14                                   ` jimmy brisson
2009-01-29 13:53                                 ` erik quanstrom
2009-01-29 15:20                                   ` Akshat Kumar
2009-01-29 16:40                                   ` Roman V. Shaposhnik
2009-01-29 16:52                                     ` erik quanstrom
2009-01-29 17:03                                       ` Akshat Kumar
2009-01-29 17:09                                         ` Brian L. Stuart
2009-01-27  7:09     ` Russ Cox
2009-01-27 22:12       ` Roman V. Shaposhnik
2009-01-27 22:25         ` Christopher Nielsen
2009-01-27 22:31           ` Roman V. Shaposhnik
2009-01-27 22:43             ` Christopher Nielsen
2009-01-27 22:50               ` Roman V. Shaposhnik
2009-01-27 23:11                 ` Christopher Nielsen
2009-01-27 23:40                   ` Roman V. Shaposhnik
2009-01-28  3:15                     ` Christopher Nielsen
2009-01-28  7:22                     ` Eris Discordia
2009-01-27 23:01         ` sqweek
2009-01-26 10:31 ` pavel.klinkovsky
2009-01-27  3:21 ` Roman Shaposhnik
2009-01-27  9:37 ` Pavel Klinkovsky

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e3a04c0c-caee-46f7-b080-7d86bc66a5f8@m4g2000vbp.googlegroups.com \
    --to=pavel.klinkovsky@gmail.com \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).