"f(%d)\n", arg);
x = arg;
x++;
}
int main(int ac, char *av[]){
h();
f(1);
g();
printf("x=%d y=%d\n", x, y);
return 0;
}
--------
b.c
--------
#include "header.h"
void g(){
y = f(5);
}
--------
c.c
--------
#include "header.h"
void h(){
f();
}
:
abs@wizard$ cc a.c b.c c.c -o aaa
a.c:
b.c:
"b.c", line 4: operand cannot have void type: op "="
"b.c", line 4: assignment type mismatch:
int "=" void
cc: acomp failed for b.c
c.c:
"c.c", line 4: prototype mismatch: 0 args passed, 1 expected
cc: acomp failed for c.c
. , 1992-95 - 348 - UNIX
8. .
UNIX - .
: write() ;
read()- - .
-
:
- ,
.
-
.
, -
. |-
.
:
- ,
.
- ; -
.
- ( ).
- , -
, - ,
.
UNIX curses ( -
termcap). -
,
/etc/termcap. ,
( , . ).
curses. -
-lcurses,
:
cc progr.c -Ox -o progr -lcurses -lm
: /usr/lib/libcurses.a ( )
/usr/lib/libm.a ( , sin, fabs).
. ,
( , stdio ( printf,
scanf, fread, fseek, ...), (strlen, strcat, sleep,
malloc, rand, ...)) /lib/libc.a
-lc.
#include <curses.h>
/usr/include/curses.h, , -
curses, ... ( ,
).
!
curses-,
stdio ;
____________________
|- (-
; (, -
"host" "site")) ( )
. - . -
( - ). - .. -
- !
. , 1992-95 - 349 - UNIX
printf, putchar. , curses -
, -
curses,
, curses -
.
| |
| CURSES---
| printw,addch,move
| |
V V
STDIO --printf,putchar---->
, curses "" -
.
, , curses,
-
(.. wmove, waddch, waddstr, wprintw
, !). , -
refresh() (""), -
(
). refresh -
. , -
- , "" -
. -
,
- .
( -
) . -
: "" - -
,
.
(/). , ,
( , "-
" ). - -
. -
( ), .
"" ( -
) - ,
. IBM PC 80x25 16
:
struct symbol{ /* IBM PC family */
char chr; /* */
char attr; /* () */
} mem[ 25 ] [ 80 ]; /* 25 80 */
:
-------------------------------------------
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | #
------------------|------------------------
|blink| R | G | B | intensity | r | g | b |
------------------|------------------------
background () | foreground ( )
R - red () G - green () B - blue ()
blink - ( !)
intensity -
. , 1992-95 - 350 - UNIX
: (0,0),
X , Y .
3 : ,
(- 3 , -
). , . 4 3
. 2**4=16 :
I R G B
BLACK 0 0 0 0 0
BLUE 0 0 0 1 1
GREEN 0 0 1 0 2
CYAN 0 0 1 1 3 (-)
RED 0 1 0 0 4
MAGENTA 0 1 0 1 5
BROWN 0 1 1 0 6
LIGHTGRAY 0 1 1 1 7 - (-)
DARKGRAY 1 0 0 0 8 -
LIGHTBLUE 1 0 0 1 9 -
LIGHTGREEN 1 0 1 0 10 -
LIGHTCYAN 1 0 1 1 11 -
LIGHTRED 1 1 0 0 12 -
LIGHTMAGENTA 1 1 0 1 13 -
YELLOW 1 1 1 0 14
WHITE 1 1 1 1 15 ()-
IBM PC - (80x25,
16 ) 0xB800:0x0000. MS DOS
make far pointer: MK_FP ( far huge !).
XENIX|- ioctl, -
, UNIX
. XENIX
" ".
8.1.
/*#! /bin/cc fall.c -o fall -lx
* " ".
* IBM PC XENIX.
*
* ;
* .
* nap() -lx
* IBM PC ioctl().
*/
#include <stdio.h>
#include <fcntl.h> /* O_RDWR */
#include <signal.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/at_ansi.h>
#include <sys/kd.h> /* for System V/4 and Interactive UNIX only */
/*#include <sys/machdep.h> for XENIX and SCO UNIX only */
#include <sys/sysmacros.h>
____________________
|- XENIX - ( "") UNIX IBM PC, -
Microsoft Santa Cruz Operation (SCO).
. , 1992-95 - 351 - UNIX
#ifdef M_I386
# define far /* 32- far */
#endif
char far *screen; /* */
/* far - "" (32-) (segment,offset) */
int segm; /* */
#define COLS 80 /* */
#define LINES 25 /* */
#define DELAY 20 /* () */
int ega; /* EGA */
/* */
static struct port_io_struct PORT[ 4 /* 4 */] = {
/* */
/* .dir .port .data */
/* flip/flop:
* 0x3C0 /
* .
*/
{ IN_ON_PORT, 0x3DA, -1 },
/* IN- */
/* 3c0 / */
{ OUT_ON_PORT, 0x3C0, -1 /* */ },
{ OUT_ON_PORT, 0x3C0, -1 /* */ },
/* OUT- */
/* , #5 3c0 */
{ OUT_ON_PORT, 0x3C0, 0x20 }
};
void closescr(nsig){ /* */
setbgcolor(0); /* */
exit(0);
}
/* VGA/EGA/CGA */
void openscr () {
static struct videodev {
char *dev; int mapmode;
} vd[] = {
{ "/dev/vga", MAPVGA },
{ "/dev/ega", MAPEGA },
{ "/dev/cga", MAPCGA },
{ NULL, -1 }
}, *v; /* */
for(v=vd; v->dev;v++ )
if((ega = open (v->dev, O_RDWR)) >= 0 ) goto ok;
fprintf( stderr, "Can't open video adapter\n" );
exit(1);
. , 1992-95 - 352 - UNIX
ok:
/* fprintf(stderr, "Adapter:%s\n", v->dev); */
/* */
#ifdef M_I386
screen = (char *) ioctl (ega, v->mapmode, 0);
#else
segm = ioctl (ega, v->mapmode, 0);
screen = sotofar (segm, 0); /* (segment,offset) to far pointer */
#endif
signal( SIGINT, closescr );
}
/* "" ""
* (x,y) .
*/
#define GET(x,y) screen[ ((x) + (y) * COLS ) * 2 ]
#define PUT(x,y, c) screen[ ((x) + (y) * COLS ) * 2 ] = (c)
#define GETATTR(x,y) screen[ ((x) + (y) * COLS ) * 2 + 1 ]
#define PUTATTR(x,y, a) screen[ ((x) + (y) * COLS ) * 2 + 1 ] = (a)
/* ? */
#define white(c,a) ((isspace(c) || c==0) && (attr & 0160)==0)
/* */
void setbgcolor( color ){
PORT[1].data = 0; /* 0 */
/* 16 (0x00...0xFF) */
PORT[2].data = color ;
/* ,
* RGBrgb (r- , g- , b- , RGB-
* )
*/
/* */
if( ioctl( ega, EGAIO, PORT ) < 0 ){
fprintf( stderr, "Can't out port\n" );
perror( "out" );
}
}
. , 1992-95 - 353 - UNIX
void main(ac, av) char **av;{
void fall();
openscr();
if( ac == 1 ){
setbgcolor(020); /* - */
fall(); /* */
} else {
if(*av[1] == 'g')
/* graphics 640x350 16-colors */
ioctl( ega, SW_CG640x350, NULL);
/* ,
* ,
* screen=ioctl(ega, v->mapmode, NULL);
* .
*/
/* text 80x25 16-colors */
else ioctl( ega, SW_ENHC80x25, NULL);
}
closescr(0);
}
/* */
void fall(){
register i, j;
int rest;
int nextcol;
int n;
int changed = 1; /* 0, */
char mask [ COLS ];
while( changed ){
changed = 0;
for( i = 0 ; i < COLS ; i++ )
mask[ i ] = 0;
for( i = 0 ; i < COLS ; i++ ){
rest = COLS - i; /* */
nextcol = rand() % rest;
j = 0; /* mask */
n = 0; /* */
for(;;){
if( mask[j] == 0 ){
if( n == nextcol ) break;
n++;
} j++;
}
changed += fallColumn( j );
mask[j] = 1;
}
}
}
. , 1992-95 - 354 - UNIX
/* */
int fallColumn( x ){
register int y;
char ch, attr;
int firstspace = (-1);
int firstnospace = (-1);
Again:
/* find the falled array */
for( y=LINES-1; y >= 0 ; y-- ){
ch = GET( x, y );
attr = GETATTR( x,y );
if( white(ch, attr)){
firstspace = y;
goto FindNoSpace;
}
}
AllNoSpaces:
return 0; /* */
FindNoSpace: /* */
for( ; y >= 0 ; y-- ){
ch = GET( x, y );
attr = GETATTR( x, y );
if( !white(ch, attr)){
firstnospace = y;
goto Fall;
}
}
AllSpaces: /* */
return 0;
Fall:
/* "" */
for( y = firstnospace ; y < firstspace ; y++ ){
/* */
ch = GET( x, y );
attr = GETATTR( x, y );
PUT( x, y, 0 );
PUTATTR( x, y, 0 );
PUT( x, y+1 , ch );
PUTATTR( x, y+1, attr );
nap( DELAY ); /* DELAY */
}
return 1; /* - */
}
8.2.
. MS DOS:
. , 1992-95 - 355 - UNIX
#include <dos.h> /* MK_FP */
char far *screen =
MK_FP(0xB800 /**/, 0x0000 /**/);
struct symb{
char chr; char attr;
} far *scr, far *ptr;
#define COLS 80 /* */
#define LINES 25 /* */
#define SCR(x,y) scr[(x) + COLS * (y)]
/* x 0..79, y 0..24 */
void main(){
int x, y;
char c;
scr = (struct symb far *) screen;
/*
* scr = (struct symb far *) MK_FP(0xB800,0x0000);
*/
/* */
for(x=0; x < COLS/2; x++ )
for( y=0; y < LINES; y++ ){
c = SCR(x,y).chr;
SCR(x,y).chr = SCR(COLS-1-x, y).chr;
SCR(COLS-1-x, y).chr = c;
}
/* : */
for(x=0; x < COLS; x++)
for(y=0; y < LINES; y++)
SCR(x,y).attr = (0xE | (0x1 << 4));
/* + */
/* () */
(void) getch();
}
, , :
#include <dos.h> /* MS DOS */
#define COLS 80
#define LINES 25
struct symb {
char chr; char attr;
} (far *scr)[ COLS ] = MK_FP(0xB800, 0);
void main(void){
register x, y;
for(y=0; y < LINES; y++)
for(x=0; x < COLS; ++x){
scr[y][x].chr = '?';
scr[y][x].attr = (y << 4) | (x & 0xF);
}
getch();
}
, , !
. , 1992-95 - 356 - UNIX
, . -
,
(, ).
, far
( -
ptr).
8.3. IBM PC () -
( XENIX).
8.4. , -
. :
typedef struct {
short xlen, ylen;
char *save;
} Pict;
extern void *malloc(unsigned);
Pict *gettext (int x, int y, int xlen, int ylen){
Pict *n = (Pict *) malloc(sizeof *n);
register char *s; register i, j;
n->xlen = xlen; n->ylen = ylen;
s = n->save = (char *) malloc( 2 * xlen * ylen );
for(i=y; i < y+ylen; i++)
for(j=x; j < x+xlen; j++){
*s++ = SCR(j,i).chr ;
*s++ = SCR(j,i).attr;
}
return n;
}
xlen, ylen ( ).
puttext ; free(buf) -
.
void puttext (Pict *n, int x, int y){
register char *s = n->save;
register i, j;
for(i=y; i < y + n->ylen; i++)
for(j=x; j < x + n->xlen; j++){
SCR(j,i).chr = *s++;
SCR(j,i).attr = *s++;
}
}
/* */
void deltext(Pict *n){ free(n->save); free(n); }
, - printf
.
#include <stdarg.h>
/* : */
static char currentColor = 0x1F;
int videoprintf (int x, int y, char *fmt, ...){
char buf[512], *s;
va_list var;
. , 1992-95 - 357 - UNIX
/* clipping ( ) */
if( y < 0 || y >= LINES ) return x;
va_start(var, fmt);
vsprintf(buf, fmt, var);
va_end(var);
for(s=buf; *s; s++, x++){
/* */
if(x < 0 ) continue;
if(x >= COLS) break;
SCR(x,y).chr = *s;
SCR(x,y).attr = currentColor;
}
return x;
}
void setcolor (int col){ currentColor = col; }
8.5. , ""
(pop-up window):
Pict *save;
save = gettext (x,y,xlen,ylen);
// ...
// (x,y) - (x+xlen-1,y+ylen-1)
// -...
// ... , , ...
// , ,
// "".
puttext (save,x,y);
deltext (save);
"" ; -
.
c = message(x, y, text);
text. -
.
text : char *text[]; ( - NULL).
8.6. , "" .
( !), $:
##########
##########$
##########$
$$$$$$$$$$
0x07 ( ). -
(puttext-) (
). xlen*ylen, xlen+ylen-1 .
8.7. , .
.
. , 1992-95 - 358 - UNIX
8.8. "" ,
:
##############
###### ##############
### ###### ##############
###### ##############
##############
box(x,y,width,height),
(x,y) (width,height).
(x0,y0) (W,H). "" :
void zoom(int x0, int y0, int W, int H){
int x, y, w, h, hprev; /* */
for(hprev=0, w=1; w < W; w++){
h = H * w; h /= W; /* W/H == w/h */
if(h == hprev) continue;
hprev = h;
x = x0 + (W - w)/2; /* */
y = y0 + (H - h)/2; /* */
box(x, y, w, h);
delay(10); /* 10 . */
}
box(x0, y0, W, H);
}
8.9. , curses, IBM PC
XENIX. .
8.10. " " ( :
, . -
,
). - curses
. : :
carry(n, from, to, by) = if( n > 0 ){
carry( n-1, from, by, to );
( from, to );
carry( n-1, by, to, from );
}
: carry( n, 0, 1, 2 );
n - (n > 0).
from - ( ).
to - .
by - ( ).
n (2**n)-1 .
8.11. , (" "). -
.maze ( !).
:
#include <setjmp.h>
jmp_buf jmp; int found = 0;
maze(){ /* */
if( setjmp(jmp) == 0 ){ /* */
if( (x_, y_))
GO( x_, y_);
. , 1992-95 - 359 - UNIX
}
}
GO(x, y){ /* (x, y) */
if( (x, y)){ found = 1; /* */
(x, y); longjmp(jmp, 1);}
(x, y);
if( (x-1,y)) GO(x-1, y); /* */
if( (x,y-1)) GO(x, y-1); /* */
if( (x+1,y)) GO(x+1, y); /* */
if( (x,y+1)) GO(x, y+1); /* */
(x, y);
}
#define (x, y) [y][x] = '*'
#define (x, y) [y][x] = ' '
#define (x, y) (x == x_ && y == y_)
/* "": ([y][x] == '$') */
(x, y){ /* @ # */
if( (x, y)) return 0; /* */
return ([y][x] == ' ');
}
( curses-).
, .
8.12. termcap :
- .
- .
- / .
8.13. , .
.
/*#!/bin/cc termio.c -O -o termio -ltermcap
* man termio, termcap screen.
* System-V.
* /etc/termcap
* .
* .
*/
#include <stdio.h> /* standard input/output */
#include <sys/types.h> /* system typedefs */
#include <termio.h> /* terminal input/output */
#include <signal.h> /* signals */
#include <fcntl.h> /* file control */
#include <time.h> /* time structure */
void setsigs(), drawItem(), drawTitle(), prSelects(), printTime();
. , 1992-95 - 360 - UNIX
/* TERMCAP ---------------------------------*/
extern char *getenv (); /* */
extern char *tgetstr (); /* /termcap/ */
extern char *tgoto (); /* %- /termcap/ */
static char Tbuf[2048], /* , 1024 */
/* Tbuf[]
* tinit(), */
Strings[256], /* */
*p; /* . */
char *tname; /* */
int COLS, /* */
LINES; /* */
char *CM; /* : cursor motion */
char *CL; /* : clear screen */
char *CE; /* : clear end of line */
char *SO,
*SE; /* : standout Start End */
char *BOLD,
*NORM; /* : boldface and NoStandout */
int BSflag; /* back space '\b' */
void tinit () { /* */
p = Strings;
/* Tbuf */
switch (tgetent (Tbuf, tname = getenv ("TERM"))) {
case -1:
printf (" TERMCAP (/etc/termcap).\n");
exit (1);
case 0:
printf (" %s .\n", tname);
exit (2);
case 1:
break; /* OK */
}
COLS = tgetnum ("co"); /* . */
LINES = tgetnum ("li");
CM = tgetstr ("cm", &p); /* . */
CL = tgetstr ("cl", &p); /* */
CE = tgetstr ("ce", &p); /* p. */
SO = tgetstr ("so", &p); /* p */
SE = tgetstr ("se", &p); /* , - */
BOLD = tgetstr ("md", &p); /* - */
NORM = tgetstr ("me", &p);
BSflag = tgetflag( "bs" ); /* :
1 - , 0 - */
}
. , 1992-95 - 361 - UNIX
/* , .
, tputs
, : (*f)(c);
, putchar,
,
,
.
*/
void put (c) char c;
{ putchar (c); }
/* */
void clearScreen () {
if (CL == NULL) /* tputs() */
return; /* ( ) */
tputs (CL, 1, put); /* - put(c) 1 */
/* 1 , : */
/* 1 '\b' */
}
/* , */
void clearEOL () { /* clear to the end of line */
if (CE == NULL)
return;
tputs (CE, 1, put);
}
/* */
void gotoXY (x, y) { /* y - -. */
if (x < 0 || y < 0 || x >= COLS || y >= LINES) {
printf (" (%d,%d) \n", x, y);
return;
}
/* CM - , 2 .
* tgoto() */
tputs (tgoto (CM, x, y), 1, put);
}
/* */
void standout () {
if (SO) tputs (SO, 1, put);
}
/* */
void standend () {
if (SE) tputs (SE, 1, put);
/* else normal(); */
}
/* */
void bold () {
if (BOLD) tputs (BOLD, 1, put);
}
. , 1992-95 - 362 - UNIX
/* */
void normal () {
if (NORM) tputs (NORM, 1, put);
else standend();
}
/* --------------------------------- */
#define ESC '\033'
#define ctrl(c) ((c) & 037 )
int curMode = 0;
int inited = 0;
struct termio old,
new;
int fdtty;
void ttinit () {
/* " " */
fdtty = open ("/dev/tty", O_RDWR | O_NDELAY);
/* */
ioctl (fdtty, TCGETA, &old);
new = old;
/* input flags */
/* '\r' '\n' */
new.c_iflag &= ~ICRNL;
if ((old.c_cflag & CSIZE) == CS8) /* 8- */
new.c_iflag &= ~ISTRIP; /* & 0177 */
/* output flags */
/* TAB3 - '\t' */
/* ONLCR - '\n' '\r\n' */
new.c_oflag &= ~(TAB3 | ONLCR);
/* local flags */
/* ICANON, CBREAK */
/* */
new.c_lflag &= ~(ICANON | ECHO);
/* control chars */ /* ... */
new.c_cc[VMIN] = 1; /* 1 */
new.c_cc[VTIME] = 0; /* 0 */
/* CBREAK */
/* ,
* . 0 ,
* */
new.c_cc[VINTR] = ctrl ('C'); /* , SIGINT */
new.c_cc[VQUIT] = '\0'; /* , SIGQUIT */
new.c_cc[VERASE] = '\0'; /* ( )*/
new.c_cc[VKILL] = '\0'; /* */
/* : DEL, CTRL/\, BACKSPACE, CTRL/U */
setsigs ();
inited = 1; /* */
}
. , 1992-95 - 363 - UNIX
void openVisual () { /* open visual mode ( "" ) */
if (!inited)
ttinit ();
if (curMode == 1)
return;
/* new */
ioctl (fdtty, TCSETAW, &new);
curMode = 1; /* */
}
void closeVisual () { /* canon mode ( ) */
if (!inited)
ttinit ();
if (curMode == 0)
return;
ioctl (fdtty, TCSETAW, &old);
curMode = 0; /* */
}
/* */
void die (nsig) {
normal();
closeVisual (); /* (
* ) ,
* . */
gotoXY (0, LINES - 1);
putchar ('\n');
if (nsig)
printf (" #%d\n", nsig);
exit (nsig);
}
void setsigs () {
register ns;
/* ; . */
/* UNIX 15 . */
for (ns = 1; ns <= 15; ns++)
signal (ns, die);
}
. , 1992-95 - 364 - UNIX
/* -------------------------------------------- */
struct menu {
char *m_text; /* */
int m_label; /* ? */
} menuText[] = {
/* Beatles */
{ "Across the Universe", 0 } ,
{ "All I've got to do", 0 } ,
{ "All my loving", 0 } ,
{ "All together now", 0 } ,
{ "All You need is love",0 } ,
{ "And I love her", 0 } ,
{ "And your bird can sing", 0 } ,
{ "Another girl", 0 } ,
{ "Any time at all", 0 } ,
{ "Ask me why", 0 } ,
{ NULL, 0 }
};
#define Y_TOP 6
int nitems; /* */
int nselected = 0; /* */
char title[] =
" - , - , ESC - , \
ENTER - , TAB - ";
# define TIMELINE 1
void main (ac, av) char **av; {
char **line;
register i;
int c;
int n; /* */
extern char readkey (); /* forward */
extern char *ttyname (); /* */