Home | History | Annotate | Line # | Download | only in PSD.doc
      1  1.8  andvar .\"	$NetBSD: ex2.c,v 1.8 2022/07/06 12:33:41 andvar Exp $
      2  1.5   perry .\"
      3  1.1     cgd .\" Copyright (c) 1992, 1993
      4  1.1     cgd .\"	 The Regents of the University of California.  All rights reserved.
      5  1.1     cgd .\"
      6  1.1     cgd .\" Redistribution and use in source and binary forms, with or without
      7  1.1     cgd .\" modification, are permitted provided that the following conditions
      8  1.1     cgd .\" are met:
      9  1.1     cgd .\" 1. Redistributions of source code must retain the above copyright
     10  1.1     cgd .\"    notice, this list of conditions and the following disclaimer.
     11  1.1     cgd .\" 2. Redistributions in binary form must reproduce the above copyright
     12  1.1     cgd .\"    notice, this list of conditions and the following disclaimer in the
     13  1.1     cgd .\"    documentation and/or other materials provided with the distribution.
     14  1.7     agc .\" 3. Neither the name of the University nor the names of its contributors
     15  1.1     cgd .\"    may be used to endorse or promote products derived from this software
     16  1.1     cgd .\"    without specific prior written permission.
     17  1.1     cgd .\"
     18  1.1     cgd .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     19  1.1     cgd .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  1.1     cgd .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  1.1     cgd .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     22  1.1     cgd .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  1.1     cgd .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  1.1     cgd .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  1.1     cgd .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  1.1     cgd .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.1     cgd .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.1     cgd .\" SUCH DAMAGE.
     29  1.1     cgd .\"
     30  1.3     cgd .\"     @(#)ex2.c	8.1 (Berkeley) 6/8/93
     31  1.1     cgd .\"
     32  1.1     cgd #include <curses.h>
     33  1.1     cgd #include <stdio.h>
     34  1.1     cgd #include <signal.h>
     35  1.1     cgd 
     36  1.1     cgd #define YSIZE LINES
     37  1.1     cgd #define XSIZE COLS
     38  1.1     cgd 
     39  1.1     cgd static int quit();
     40  1.1     cgd 
     41  1.1     cgd /*
     42  1.1     cgd  * This program fills the screen up with characters and the allows the user to
     43  1.1     cgd  * manipulate the text on the screen using some basic commands.
     44  1.1     cgd  * Nothing fancy, just a demonstration of the elementary features of the
     45  1.1     cgd  * curses(3) package.
     46  1.1     cgd  */
     47  1.1     cgd main()
     48  1.1     cgd {
     49  1.1     cgd 	int i, j, c, n, d = 0;
     50  1.1     cgd 	char id[100];
     51  1.1     cgd 	int hh = 0;
     52  1.1     cgd 	int curx, cury, base, arg;
     53  1.6  simonb 
     54  1.1     cgd 	initscr();
     55  1.1     cgd 	signal(SIGINT, quit);
     56  1.1     cgd 	crmode();
     57  1.1     cgd 	noecho();
     58  1.1     cgd 	nonl();
     59  1.1     cgd 	delwin(stdscr);
     60  1.1     cgd 	stdscr = newwin(YSIZE, XSIZE, 0, 0);
     61  1.1     cgd 	flushok(stdscr, TRUE);
     62  1.1     cgd 	scrollok(stdscr, TRUE);
     63  1.1     cgd 	erase();
     64  1.1     cgd 	refresh();
     65  1.1     cgd 
     66  1.1     cgd 	move(0,0);
     67  1.1     cgd 	refresh();
     68  1.1     cgd 	for (i = 0; i < YSIZE + 2; i++) {
     69  1.4     mrg 		(void)snprintf(id, sizeof id, "%d: ", i);
     70  1.1     cgd 		addstr(id);
     71  1.1     cgd 		for (j = 0; j < XSIZE - strlen(id); j++)
     72  1.1     cgd 			addch('0' + (i % 10));
     73  1.1     cgd 	}
     74  1.1     cgd 	c = getchar();
     75  1.1     cgd 	base = 2;
     76  1.1     cgd 	curx = cury = 0;
     77  1.1     cgd 	move(0, 0);
     78  1.1     cgd 	refresh();
     79  1.1     cgd 
     80  1.1     cgd 	/*
     81  1.1     cgd 	 * The screen manipulator has the following commands:
     82  1.1     cgd 	 * 'D' - clear to the end of the current line.
     83  1.1     cgd 	 * 'B' - clear to the bottom of the screen.
     84  1.1     cgd 	 * 'E' - erase the screen.
     85  1.1     cgd 	 * 's' - enter standout mode.
     86  1.1     cgd 	 * 'e' - exit standout mode.
     87  1.1     cgd 	 * 'd' n - delete n lines below cursor line.
     88  1.1     cgd 	 * 'i' n - insert n lines below cursor line.
     89  1.1     cgd 	 * 'q' - quit.
     90  1.1     cgd 	 * 'f' - move cursor one position to the right.
     91  1.1     cgd 	 * 'b' - move cursor one position to the left.
     92  1.1     cgd 	 * 'n' - move cursor one line down.
     93  1.1     cgd 	 * 'p' - move cursor one line up.
     94  1.8  andvar 	 * 'h' - home cursor.
     95  1.1     cgd 	 * 'l' - force refresh.
     96  1.1     cgd 	 * 'r' - simulate a carriage return.
     97  1.1     cgd 	 *
     98  1.1     cgd 	 * All other characters are ignored.
     99  1.1     cgd 	 */
    100  1.1     cgd 	for(;;) {
    101  1.1     cgd 		switch(c = getchar()) {
    102  1.1     cgd 		case 'D':
    103  1.1     cgd 			clrtoeol();
    104  1.1     cgd 			refresh();
    105  1.1     cgd 			continue;
    106  1.1     cgd 		case 'B':
    107  1.1     cgd 			clrtobot();
    108  1.1     cgd 			refresh();
    109  1.1     cgd 			continue;
    110  1.1     cgd 		case 'E':
    111  1.1     cgd 			erase();
    112  1.1     cgd 			refresh();
    113  1.1     cgd 			continue;
    114  1.1     cgd 		case 's':
    115  1.1     cgd 			standout();
    116  1.1     cgd 			continue;
    117  1.1     cgd 		case 'e':
    118  1.1     cgd 			standend();
    119  1.1     cgd 			continue;
    120  1.1     cgd 		case 'd':
    121  1.1     cgd 			arg = getchar() - '0';
    122  1.1     cgd 			for (i = 0; i < arg; i++)
    123  1.1     cgd 				deleteln();
    124  1.1     cgd 			refresh();
    125  1.1     cgd 			continue;
    126  1.1     cgd 		case 'i':
    127  1.1     cgd 			arg = getchar() - '0';
    128  1.1     cgd 			for (i = 0; i < arg; i++)
    129  1.1     cgd 				insertln();
    130  1.1     cgd 			refresh();
    131  1.1     cgd 			continue;
    132  1.1     cgd 		case 'q':
    133  1.1     cgd 			quit();
    134  1.1     cgd 		case 'f':
    135  1.1     cgd 			if (curx < XSIZE - 1)
    136  1.1     cgd 				curx++;
    137  1.1     cgd 			else {
    138  1.1     cgd 				cury++;
    139  1.1     cgd 				curx = 0;
    140  1.1     cgd 			}
    141  1.1     cgd 			break;
    142  1.1     cgd 		case 'b':
    143  1.1     cgd 			if (curx == 0) {
    144  1.1     cgd 				cury--;
    145  1.1     cgd 				curx = XSIZE - 1;
    146  1.1     cgd 			} else
    147  1.1     cgd 				curx--;
    148  1.1     cgd 			break;
    149  1.1     cgd 		case 'n':
    150  1.1     cgd 			cury++;
    151  1.1     cgd 			break;
    152  1.1     cgd 		case 'p':
    153  1.1     cgd 			cury--;
    154  1.1     cgd 			break;
    155  1.1     cgd 		case 'h':
    156  1.1     cgd 			curx = cury = 0;
    157  1.1     cgd 			break;
    158  1.1     cgd 		case 'l':
    159  1.1     cgd 			wrefresh(curscr);
    160  1.1     cgd 			continue;
    161  1.1     cgd 		case 'r':   /* return */
    162  1.1     cgd 		{
    163  1.1     cgd 			int x, y;
    164  1.1     cgd 			getyx(stdscr, y, x);
    165  1.1     cgd 			move(y+1, 0);
    166  1.1     cgd 			insertln();
    167  1.1     cgd 			move(y, x);
    168  1.1     cgd 			clrtoeol();
    169  1.1     cgd 			refresh();
    170  1.1     cgd 			continue;
    171  1.1     cgd 		}
    172  1.1     cgd 		default:
    173  1.1     cgd 			continue;
    174  1.1     cgd 		}
    175  1.1     cgd 
    176  1.1     cgd 		if (cury < 0) {
    177  1.1     cgd 			base--;
    178  1.1     cgd 			move(0, 0);
    179  1.1     cgd 			insertln();
    180  1.4     mrg 			(void)snprintf(id, sizeof id, "%d: ", base);
    181  1.1     cgd 			addstr(id);
    182  1.6  simonb 			for (j = 0; j < XSIZE - strlen(id) - 2; j++)
    183  1.1     cgd 				addch('0' + (base % 10));
    184  1.1     cgd 			cury++;
    185  1.1     cgd 		} else if (cury >= YSIZE) {
    186  1.1     cgd 			move(0, 0);
    187  1.1     cgd 			deleteln();
    188  1.1     cgd 			move(YSIZE - 1, 0);
    189  1.4     mrg 			(void)snprintf(id, sizeof id, "%d: ", base + YSIZE);
    190  1.1     cgd 			addstr(id);
    191  1.1     cgd 			for (j = 0; j < XSIZE - strlen(id) - 2; j++)
    192  1.1     cgd 				addch('0' + ((base + YSIZE) % 10));
    193  1.1     cgd 			cury--;
    194  1.1     cgd 			base++;
    195  1.1     cgd 		}
    196  1.1     cgd 		move(cury, curx);
    197  1.1     cgd 		refresh();
    198  1.1     cgd 	}
    199  1.1     cgd }
    200  1.1     cgd 
    201  1.1     cgd int
    202  1.1     cgd quit()
    203  1.1     cgd {
    204  1.1     cgd 	erase();
    205  1.1     cgd 	refresh();
    206  1.1     cgd 	endwin();
    207  1.1     cgd 	exit(0);
    208  1.1     cgd }
    209