1: #ifndef lint
   2: char copyright[] =
   3: "@(#) Copyright (c) 1980 Regents of the University of California.\n\
   4:  All rights reserved.\n";
   5: #endif not lint
   6: 
   7: #ifndef lint
   8: static char sccsid[] = "@(#)worms.c	5.3 (Berkeley) 7/15/85";
   9: #endif not lint
  10: 
  11: #define BSD
  12: 
  13: /*
  14: 
  15: 	 @@@        @@@    @@@@@@@@@@     @@@@@@@@@@@    @@@@@@@@@@@@
  16: 	 @@@        @@@   @@@@@@@@@@@@    @@@@@@@@@@@@   @@@@@@@@@@@@@
  17: 	 @@@        @@@  @@@@      @@@@   @@@@           @@@@ @@@  @@@@
  18: 	 @@@   @@   @@@  @@@        @@@   @@@            @@@  @@@   @@@
  19: 	 @@@  @@@@  @@@  @@@        @@@   @@@            @@@  @@@   @@@
  20: 	 @@@@ @@@@ @@@@  @@@        @@@   @@@            @@@  @@@   @@@
  21: 	  @@@@@@@@@@@@   @@@@      @@@@   @@@            @@@  @@@   @@@
  22: 	   @@@@  @@@@     @@@@@@@@@@@@    @@@            @@@  @@@   @@@
  23: 	    @@    @@       @@@@@@@@@@     @@@            @@@  @@@   @@@
  24: 
  25: 				 Eric P. Scott
  26: 			  Caltech High Energy Physics
  27: 				 October, 1980
  28: 
  29: */
  30: #include <stdio.h>
  31: #ifdef USG
  32: #include <termio.h>
  33: #else
  34: #include <sgtty.h>
  35: #endif
  36: #include <signal.h>
  37: #define cursor(col,row) tputs(tgoto(CM,col,row),1,fputchar)
  38: extern char *UP;
  39: extern short ospeed;
  40: int Wrap;
  41: short *ref[24];
  42: static char flavor[]={
  43:     'O', '*', '#', '$', '%', '0'
  44: };
  45: static short xinc[]={
  46:      1,  1,  1,  0, -1, -1, -1,  0
  47: }, yinc[]={
  48:     -1,  0,  1,  1,  1,  0, -1, -1
  49: };
  50: static struct worm {
  51:     int orientation, head;
  52:     short *xpos, *ypos;
  53: } worm[40];
  54: static char *field;
  55: static int length=16, number=3, trail=' ';
  56: static struct options {
  57:     int nopts;
  58:     int opts[3];
  59: } normal[8]={
  60:     { 3, { 7, 0, 1 } },
  61:     { 3, { 0, 1, 2 } },
  62:     { 3, { 1, 2, 3 } },
  63:     { 3, { 2, 3, 4 } },
  64:     { 3, { 3, 4, 5 } },
  65:     { 3, { 4, 5, 6 } },
  66:     { 3, { 5, 6, 7 } },
  67:     { 3, { 6, 7, 0 } }
  68: }, upper[8]={
  69:     { 1, { 1, 0, 0 } },
  70:     { 2, { 1, 2, 0 } },
  71:     { 0, { 0, 0, 0 } },
  72:     { 0, { 0, 0, 0 } },
  73:     { 0, { 0, 0, 0 } },
  74:     { 2, { 4, 5, 0 } },
  75:     { 1, { 5, 0, 0 } },
  76:     { 2, { 1, 5, 0 } }
  77: }, left[8]={
  78:     { 0, { 0, 0, 0 } },
  79:     { 0, { 0, 0, 0 } },
  80:     { 0, { 0, 0, 0 } },
  81:     { 2, { 2, 3, 0 } },
  82:     { 1, { 3, 0, 0 } },
  83:     { 2, { 3, 7, 0 } },
  84:     { 1, { 7, 0, 0 } },
  85:     { 2, { 7, 0, 0 } }
  86: }, right[8]={
  87:     { 1, { 7, 0, 0 } },
  88:     { 2, { 3, 7, 0 } },
  89:     { 1, { 3, 0, 0 } },
  90:     { 2, { 3, 4, 0 } },
  91:     { 0, { 0, 0, 0 } },
  92:     { 0, { 0, 0, 0 } },
  93:     { 0, { 0, 0, 0 } },
  94:     { 2, { 6, 7, 0 } }
  95: }, lower[8]={
  96:     { 0, { 0, 0, 0 } },
  97:     { 2, { 0, 1, 0 } },
  98:     { 1, { 1, 0, 0 } },
  99:     { 2, { 1, 5, 0 } },
 100:     { 1, { 5, 0, 0 } },
 101:     { 2, { 5, 6, 0 } },
 102:     { 0, { 0, 0, 0 } },
 103:     { 0, { 0, 0, 0 } }
 104: }, upleft[8]={
 105:     { 0, { 0, 0, 0 } },
 106:     { 0, { 0, 0, 0 } },
 107:     { 0, { 0, 0, 0 } },
 108:     { 0, { 0, 0, 0 } },
 109:     { 0, { 0, 0, 0 } },
 110:     { 1, { 3, 0, 0 } },
 111:     { 2, { 1, 3, 0 } },
 112:     { 1, { 1, 0, 0 } }
 113: }, upright[8]={
 114:     { 2, { 3, 5, 0 } },
 115:     { 1, { 3, 0, 0 } },
 116:     { 0, { 0, 0, 0 } },
 117:     { 0, { 0, 0, 0 } },
 118:     { 0, { 0, 0, 0 } },
 119:     { 0, { 0, 0, 0 } },
 120:     { 0, { 0, 0, 0 } },
 121:     { 1, { 5, 0, 0 } }
 122: }, lowleft[8]={
 123:     { 3, { 7, 0, 1 } },
 124:     { 0, { 0, 0, 0 } },
 125:     { 0, { 0, 0, 0 } },
 126:     { 1, { 1, 0, 0 } },
 127:     { 2, { 1, 7, 0 } },
 128:     { 1, { 7, 0, 0 } },
 129:     { 0, { 0, 0, 0 } },
 130:     { 0, { 0, 0, 0 } }
 131: }, lowright[8]={
 132:     { 0, { 0, 0, 0 } },
 133:     { 1, { 7, 0, 0 } },
 134:     { 2, { 5, 7, 0 } },
 135:     { 1, { 5, 0, 0 } },
 136:     { 0, { 0, 0, 0 } },
 137:     { 0, { 0, 0, 0 } },
 138:     { 0, { 0, 0, 0 } },
 139:     { 0, { 0, 0, 0 } }
 140: };
 141: char *TE;
 142: main(argc,argv)
 143: int argc;
 144: char *argv[];
 145: {
 146:     extern fputchar();
 147:     char *malloc();
 148:     char *getenv();
 149:     char *tgetstr(), *tgoto();
 150:     int quit();
 151:     float ranf();
 152:     register int x, y;
 153:     register int n;
 154:     register struct worm *w;
 155:     register struct options *op;
 156:     register int h;
 157:     register short *ip;
 158:     char *AL, *BC, *CM, *EI, *HO, *IC, *IM, *IP, *SR;
 159:     int CO, IN, LI, last, bottom;
 160:     char *tcp;
 161:     register char *term;
 162:     char tcb[100];
 163: #ifdef USG
 164:     struct termio sg;
 165: #else
 166:     struct sgttyb sg;
 167: #endif
 168:     setbuf(stdout,malloc(BUFSIZ));
 169:     for (x=1;x<argc;x++) {
 170:     register char *p;
 171:     p=argv[x];
 172:     if (*p=='-') p++;
 173:     switch (*p) {
 174:     case 'f':
 175:         field="WORM";
 176:         break;
 177:     case 'l':
 178:         if (++x==argc) goto usage;
 179:         if ((length=atoi(argv[x]))<2||length>1024) {
 180:         fprintf(stderr,"%s: Invalid length\n",*argv);
 181:         exit(1);
 182:         }
 183:         break;
 184:     case 'n':
 185:         if (++x==argc) goto usage;
 186:         if ((number=atoi(argv[x]))<1||number>40) {
 187:         fprintf(stderr,"%s: Invalid number of worms\n",*argv);
 188:         exit(1);
 189:         }
 190:         break;
 191:     case 't':
 192:         trail='.';
 193:         break;
 194:     default:
 195:     usage:
 196:         fprintf(stderr,
 197:         "usage: %s [-field] [-length #] [-number #] [-trail]\n",*argv);
 198:         exit(1);
 199:         break;
 200:     }
 201:     }
 202:     if (!(term=getenv("TERM"))) {
 203:     fprintf(stderr,"%s: TERM: parameter not set\n",*argv);
 204:     exit(1);
 205:     }
 206:     if (tgetent(malloc(1024),term)<=0) {
 207:     fprintf(stderr,"%s: %s: unknown terminal type\n",*argv,term);
 208:     exit(1);
 209:     }
 210:     tcp=tcb;
 211:     if (!(CM=tgetstr("cm",&tcp))) {
 212:     fprintf(stderr,"%s: terminal not capable of cursor motion\n",*argv);
 213:     exit(1);
 214:     }
 215:     AL=tgetstr("al",&tcp);
 216:     BC=tgetflag("bs") ? "\b" : tgetstr("bc",&tcp);
 217:     if ((CO=tgetnum("co"))<=0) CO=80;
 218:     last=CO-1;
 219:     EI=tgetstr("ei",&tcp);
 220:     HO=tgetstr("ho",&tcp);
 221:     IC=tgetstr("ic",&tcp);
 222:     IM=tgetstr("im",&tcp);
 223:     IN=tgetflag("in");
 224:     IP=tgetstr("ip",&tcp);
 225:     if ((LI=tgetnum("li"))<=0) LI=24;
 226:     bottom=LI-1;
 227:     SR=tgetstr("sr",&tcp);
 228:     TE=tgetstr("te",&tcp);
 229:     UP=tgetstr("up",&tcp);
 230: #ifdef USG
 231:     ioctl(fileno(stdout),TCGETA,&sg);
 232:     ospeed=sg.c_cflag&CBAUD;
 233: #else
 234:     gtty(fileno(stdout),&sg);
 235:     ospeed=sg.sg_ospeed;
 236: #endif
 237:     Wrap=tgetflag("am");
 238:     ip=(short *)malloc(LI*CO*sizeof (short));
 239:     for (n=0;n<LI;) {
 240:     ref[n++]=ip; ip+=CO;
 241:     }
 242:     for (ip=ref[0],n=LI*CO;--n>=0;) *ip++=0;
 243:     if (Wrap) ref[bottom][last]=1;
 244:     for (n=number, w= &worm[0];--n>=0;w++) {
 245:     w->orientation=w->head=0;
 246:     if (!(ip=(short *)malloc(length*sizeof (short)))) {
 247:         fprintf(stderr,"%s: out of memory\n",*argv);
 248:         exit(1);
 249:     }
 250:     w->xpos=ip;
 251:     for (x=length;--x>=0;) *ip++ = -1;
 252:     if (!(ip=(short *)malloc(length*sizeof (short)))) {
 253:         fprintf(stderr,"%s: out of memory\n",*argv);
 254:         exit(1);
 255:     }
 256:     w->ypos=ip;
 257:     for (y=length;--y>=0;) *ip++ = -1;
 258:     }
 259:     signal(SIGINT, quit);
 260:     tputs(tgetstr("ti",&tcp),1,fputchar);
 261:     tputs(tgetstr("cl",&tcp),1,fputchar);
 262:     if (field) {
 263:     register char *p;
 264:     p=field;
 265:     for (y=bottom;--y>=0;) {
 266:         for (x=CO;--x>=0;) {
 267:         putchar(*p++);
 268:         if (!*p) p=field;
 269:         }
 270:             if (!Wrap) putchar('\n');
 271:             fflush(stdout);
 272:         }
 273:     if (Wrap) {
 274:         if (IM&&!IN) {
 275:         for (x=last;--x>0;) {
 276:             putchar(*p++);
 277:             if (!*p) p=field;
 278:         }
 279:         y= *p++; if (!*p) p=field;
 280:         putchar(*p);
 281:         if (BC) tputs(BC,1,fputchar);
 282:         else cursor(last-1,bottom);
 283:         tputs(IM,1,fputchar);
 284:         if (IC) tputs(IC,1,fputchar);
 285:         putchar(y);
 286:         if (IP) tputs(IP,1,fputchar);
 287:         tputs(EI,1,fputchar);
 288:         }
 289:         else if (SR||AL) {
 290:         if (HO) tputs(HO,1,fputchar);
 291:         else cursor(0,0);
 292:         if (SR) tputs(SR,1,fputchar);
 293:         else tputs(AL,LI,fputchar);
 294:         for (x=CO;--x>=0;) {
 295:             putchar(*p++);
 296:             if (!*p) p=field;
 297:         }
 298:         }
 299:         else for (x=last;--x>=0;) {
 300:             putchar(*p++);
 301:             if (!*p) p=field;
 302:         }
 303:     }
 304:     else for (x=CO;--x>=0;) {
 305:         putchar(*p++);
 306:         if (!*p) p=field;
 307:     }
 308:     }
 309:     fflush(stdout);
 310:     for (;;) {
 311:     for (n=0,w= &worm[0];n<number;n++,w++) {
 312:         if ((x=w->xpos[h=w->head])<0) {
 313:         cursor(x=w->xpos[h]=0,y=w->ypos[h]=bottom);
 314:         putchar(flavor[n%6]);
 315:         ref[y][x]++;
 316:         }
 317:         else y=w->ypos[h];
 318:         if (++h==length) h=0;
 319:         if (w->xpos[w->head=h]>=0) {
 320:         register int x1, y1;
 321:         x1=w->xpos[h]; y1=w->ypos[h];
 322:         if (--ref[y1][x1]==0) {
 323:             cursor(x1,y1); putchar(trail);
 324:         }
 325:         }
 326:             op= &(x==0 ? (y==0 ? upleft : (y==bottom ? lowleft : left)) :
 327:                 (x==last ? (y==0 ? upright : (y==bottom ? lowright : right)) :
 328:         (y==0 ? upper : (y==bottom ? lower : normal))))[w->orientation];
 329:         switch (op->nopts) {
 330:         case 0:
 331:         fflush(stdout);
 332:         abort();
 333:         return;
 334:         case 1:
 335:         w->orientation=op->opts[0];
 336:         break;
 337:         default:
 338:         w->orientation=op->opts[(int)(ranf()*(float)op->nopts)];
 339:         }
 340:         cursor(x+=xinc[w->orientation], y+=yinc[w->orientation]);
 341:         if (!Wrap||x!=last||y!=bottom) putchar(flavor[n%6]);
 342:         ref[w->ypos[h]=y][w->xpos[h]=x]++;
 343:     }
 344:     fflush(stdout);
 345:     }
 346: }
 347: quit()
 348: {
 349:     signal(SIGINT, SIG_IGN);
 350:     tputs(TE,1,fputchar);
 351:     exit(0);
 352: }
 353: fputchar(c)
 354: char c;
 355: {
 356:     putchar(c);
 357: }
 358: float ranf() {
 359: #ifdef BSD
 360:     return((float)rand()/2147483647.);
 361: #else
 362:     return((float)rand()/32767.);
 363: #endif
 364: }

Defined functions

fputchar defined in line 353; used 13 times
main defined in line 142; never used
quit defined in line 347; used 2 times
ranf defined in line 358; used 2 times

Defined variables

TE defined in line 141; used 2 times
Wrap defined in line 40; used 5 times
copyright defined in line 2; never used
field defined in line 54; used 9 times
flavor defined in line 42; used 2 times
length defined in line 55; used 7 times
normal defined in line 59; used 1 times
ref defined in line 41; used 6 times
sccsid defined in line 8; never used
worm defined in line 53; used 2 times
xinc defined in line 45; used 1 times

Defined struct's

options defined in line 56; used 2 times
  • in line 155(2)
worm defined in line 50; used 2 times
  • in line 154(2)

Defined macros

BSD defined in line 11; used 1 times
cursor defined in line 37; used 5 times
Last modified: 1985-07-16
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1459
Valid CSS Valid XHTML 1.0 Strict