📄 sty.c
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/lib/sty.c,v 1.3 2003/01/15 14:04:36 josh Exp $ *//* * Copyright (C) 1999-2005 Wind River Systems, Inc. * All rights reserved. Provided under license only. * Distribution or other use of this software is only * permitted pursuant to the terms of a license agreement * from Wind River Systems (and is otherwise prohibited). * Refer to that license agreement for terms of use. *//**************************************************************************** * Copyright 1993-1997 Epilogue Technology Corporation. * Copyright 1998 Integrated Systems, Inc. * All rights reserved. ****************************************************************************//* * $Log: sty.c,v $ * Revision 1.3 2003/01/15 14:04:36 josh * directory structure shifting * * Revision 1.2 2001/11/08 15:56:28 tneale * Updated for newest file layout * * Revision 1.1.1.1 2001/11/05 17:48:43 tneale * Tornado shuffle * * Revision 2.14 2001/01/19 22:23:54 paul * Update copyright. * * Revision 2.13 2000/03/17 00:12:47 meister * Update copyright message * * Revision 2.12 1999/05/21 16:45:22 sra * vsnprintf() => VSNPRINTF(). * * Revision 2.11 1998/07/02 06:55:39 sra * Make Snark restartable under pSOS, and other minor cleanups. * * Revision 2.10 1998/06/03 21:22:15 sar * Updated code to use the common string macros * * Revision 2.9 1998/02/25 04:57:42 sra * Update copyrights. * * Revision 2.8 1997/03/20 06:53:14 sra * DFARS-safe copyright text. Zap! * * Revision 2.7 1997/03/19 22:06:00 sra * Perhaps we should include install.h before testing install options? * * Revision 2.6 1997/03/19 20:19:50 sra * Remove gratuitous Attache dependencies. * * Revision 2.5 1997/03/19 04:47:35 sra * Get rid of some gratuitous historical dependencies on Attache. * * Revision 2.4 1997/02/25 10:58:16 sra * Update copyright notice, dust under the bed. * * Revision 2.3 1997/02/19 08:10:29 sra * More fun merging snmptalk into snark, general snark cleanup. * * Revision 2.2 1996/10/29 03:02:38 sra * Duh, maybe we should free the history list when we close the sty? * Another ancient bug nailed thanks to Purify.... * * Revision 2.1 1996/03/22 10:05:39 sra * Update copyrights prior to Attache 3.2 release. * * Revision 2.0 1995/05/10 22:38:15 sra * Attache release 3.0. * * Revision 1.5 1995/04/28 03:44:03 sra * Check for null pointer properly in sty_yank(). * * Revision 1.4 1995/01/06 00:52:48 sra * Update copyright notice for 2.1 release. * * Revision 1.3 1993/07/31 01:25:44 sra * Include <snark.h> when building, for vsnprintf(). * * Revision 1.2 1993/07/30 23:45:30 sra * Cosmetic changes to make Borland C++ 3.1 happy. * * Revision 1.1 1993/07/05 21:53:30 sra * Initial revision * *//* [clearcase]modification history-------------------01a,19apr05,job update copyright notices*//* * Software terminal support for snark. * * As much of this code as possible was taken from Bridgham's * doskbd.c package. *//* ANSI C files. */#include <stdio.h>#include <stdarg.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include <wrn/wm/common/install.h>/* Local files. */#include <wrn/wm/demo/snarklib.h>/* The console -- see usage note in snark/h/sty.h. */struct sty *cty;/* Internal cruft. */static sty_listener_t sty_listener;static void sty_read_loop(struct sty *);/* * low level ring buffer routines *//* private */#define sty_ring_empty(_sty_) \ ((_sty_)->ring_start == (_sty_)->ring_end)#define sty_ring_full(_sty_) \ ((_sty_)->ring_end - (_sty_)->ring_start + 1 == 0)static int sty_getch(struct sty *sty){ if (!sty_ring_empty(sty)) { int c = sty->ring_buf[sty->ring_start++]; if (sty->ring_start >= sizeof(sty->ring_buf)) sty->ring_start = 0; return c; } return -1;}/* public */unsigned sty_window(struct sty *sty){ if (!sty) return 0; return sizeof(sty->ring_buf) + sty->ring_start - sty->ring_end;}void sty_receive(struct sty *sty, unsigned char *text, unsigned length){ if (!sty || !text) return; do { for ( ; length > 0 && !sty_ring_full(sty); length--) { sty->ring_buf[sty->ring_end++] = *text++; if (sty->ring_end >= sizeof(sty->ring_buf)) sty->ring_end = 0; } if (sty->app_receiver) sty_read_loop(sty); } while (length > 0 && !sty_ring_full(sty)); while (length-- > 0) sty_puts(sty, "\007");}/* * command line editing level routines */static void sty_erase_char(struct sty *sty){ if (sty->line_len > 0) { sty->line_len--; if (sty->flags & STY_FLAG_ECHOING) sty_puts(sty, "\010 \010"); }}static void sty_erase_line(struct sty *sty){ if (sty->flags & STY_FLAG_ECHOING) while (sty->line_len-- > 0) sty_puts(sty, "\010 \010"); sty->line_len = 0;}static void sty_yank(register struct sty *sty){ if (sty->history[sty->history_ptr] == 0) return; for ( ; sty->line_len > 0; sty->line_len--) sty_puts(sty, "\010 \010"); while ((sty->line_buf[sty->line_len] = sty->history[sty->history_ptr][sty->line_len]) != '\0') sty->line_len++; sty_puts(sty, (char *) sty->line_buf);}#define sty_historical_interest(_sty_) \ ((_sty_)->line_len != 0 && \ (!(_sty_)->history[STY_HISTORY_SIZE - 1] || \ STRCMP((_sty_)->line_buf, (_sty_)->history[STY_HISTORY_SIZE - 1]) != 0))/* implements the main editing loop */static void sty_read_loop(struct sty *sty){ void (*receiver)(struct sty *, char *, void *); int c, i; while ((c = sty_getch(sty)) != -1) { if (sty->flags & STY_FLAG_RAWMODE) { if (sty->flags & STY_FLAG_ECHOING) sty_putc(sty, c); sty->line_buf[0] = (char) c; receiver = sty->app_receiver; sty->app_receiver = 0; receiver(sty, sty->line_buf, sty->app_cookie); return; } else { switch (c) { case '\n': case '\r': /* send the line on */ if (sty->flags & STY_FLAG_ECHOING) sty_puts(sty, "\n"); sty->line_buf[sty->line_len] = '\0'; if (sty_historical_interest(sty)) { if (sty->history[0]) GLUE_FREE(sty->history[0]); for (i = 0; i < STY_HISTORY_SIZE - 1; i++) sty->history[i] = sty->history[i+1]; sty->history[STY_HISTORY_SIZE - 1] = GLUE_ALLOC(STRLEN(sty->line_buf)+1); if (sty->history[STY_HISTORY_SIZE - 1]) STRCPY(sty->history[STY_HISTORY_SIZE - 1], sty->line_buf); } sty->history_ptr = 0; sty->line_len = 0; receiver = sty->app_receiver; sty->app_receiver = 0; receiver(sty, sty->line_buf, sty->app_cookie); return; case '\177': /* delete */ case 'H' & 077: /* backspace (C-H) */ sty_erase_char(sty); continue; case 'U' & 077: /* C-U */ sty_erase_line(sty); continue; case 'P' & 077: /* control-p */ sty->history_ptr--; if ((sty->history_ptr < 0) || (sty->history[sty->history_ptr] == 0)) sty->history_ptr = STY_HISTORY_SIZE - 1; sty_yank(sty); continue; case 'N' & 077: /* control-n */ sty->history_ptr++; if (sty->history_ptr >= STY_HISTORY_SIZE - 1) { if (sty->history[STY_HISTORY_SIZE - 1] == 0) { sty->history_ptr = STY_HISTORY_SIZE - 1; continue; } for (sty->history_ptr = 0; sty->history[sty->history_ptr] == 0; sty->history_ptr++) ; /* find the oldest */ } sty_yank(sty); continue; case 'Y' & 077: /* control-y */ sty->history_ptr = STY_HISTORY_SIZE - 1; sty_yank(sty); continue; case '\f': /* control-l */ sty_puts(sty, "\033[;H\033[2J"); if (sty->prompt) sty_puts(sty, sty->prompt); sty_write(sty, sty->line_buf, sty->line_len); continue; default: if (isascii(c) && isprint(c) && sty->line_len < sizeof(sty->line_buf)-1) { sty->line_buf[sty->line_len++] = (char) c; if (sty->flags & STY_FLAG_ECHOING) sty_putc(sty, c); } continue; } } } /* * If we get here, the keyboard buffer is empty. If the driver is * null, we've hit EOF, otherwise we need to wait for more input. */ if (!sty->driver) { sty->line_len = 0; receiver = sty->app_receiver; sty->app_receiver = 0; receiver(sty, 0, sty->app_cookie); }}void sty_read (struct sty *sty, void (*receiver)(struct sty *, char *, void *), char *prompt, void *cookie){ sty->app_receiver = receiver; sty->app_cookie = cookie; sty->prompt = prompt; if (prompt) sty_puts(sty, prompt); sty_read_loop(sty);}/* * Output routines. */void sty_write(struct sty *sty, char *text, size_t len){ if (sty && sty->driver && text && len) sty->driver->write(sty, (unsigned char *) text, len, sty->driver_cookie);}void sty_putc(struct sty *sty, int c){ char text[1]; text[0] = (char) c; sty_write(sty, text, 1);}void sty_puts(struct sty *sty, char *text){ sty_write(sty, text, STRLEN(text));}int sty_printf(struct sty *sty, char *fmt, ...){ va_list ap; int val; char buffer[2000]; va_start(ap, fmt); val = VSNPRINTF(buffer, sizeof(buffer), fmt, ap); va_end(ap); sty_puts(sty, buffer); return val;}void sty_discard(struct sty *sty){ sty->ring_start = sty->ring_end = 0; sty_erase_line(sty);}sty_listener_t sty_listen(sty_listener_t new_listener){ sty_listener_t old_listener = sty_listener; sty_listener = new_listener; return old_listener;}void sty_open (struct sty *sty, struct sty_driver *driver, unsigned flags, void *cookie){ if (!sty) return; MEMSET((void *) sty, 0, sizeof(*sty)); sty->driver = driver; sty->flags = flags; sty->driver_cookie = cookie; if (sty_listener) sty_listener(sty);}void sty_init(){ sty_listener = 0; cty = 0;}void sty_close(struct sty *sty){ struct sty_driver *driver; int i; if (sty) { for (i = 0; i < STY_HISTORY_SIZE; i++) { if (sty->history[i]) { GLUE_FREE(sty->history[i]); sty->history[i] = 0; } } driver = sty->driver; sty->driver = 0; if (driver && driver->close) driver->close(sty, sty->driver_cookie); }}void cty_set(struct sty *sty){ cty = sty;#if 0 sty_puts(cty, "XX ITS 2268 SYSTEM JOB USING THIS CONSOLE.\n");#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -