home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume11 / musbus / part02 / edit.dat < prev    next >
Encoding:
Text File  |  1987-09-16  |  4.5 KB  |  203 lines

  1. /*
  2.  *  COBBLED program - test for editing
  3.  *
  4.  *  edit [ chars_per_sec ]
  5.  *
  6.  *  $Header: edit.dat,v 3.3 86/01/30 07:53:17 kenj Beta $
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <signal.h>
  11.  
  12. #define MEAN    5
  13. #define GRANULE    5
  14.  
  15. int    thres;
  16. int    est_mean = MEAN;
  17.  
  18. main(argc, argv)
  19. int    argc;
  20. char    *argv[];
  21. {
  22.     int    onalarm();
  23.  
  24.     if (argc == 2) {
  25.         est_mean = atoi(argv[1]);
  26.         if (est_mean <= 0) {
  27.             fprintf(stderr, "edit: bad mean, reset to %d chars/sec\n", MEAN);
  28.             est_mean = MEAN;
  29.         }
  30.     }
  31.  
  32. /**
  33.  ** BEGIN BLOCK OF INSERTED TEXT TO BOOST FILE SIZE
  34. This is some text which has been added to the middle of
  35. the file to increase the file size to the point where it is realistic.
  36.  
  37. How 'bout some random pascal code?
  38.  
  39. program elcheapo(input, output);
  40.  
  41. const
  42.     DISPSIZE = 10;    { display is DISPSIZE characters long }
  43.     SIGN = 1;        { sign at display[SIGN] }
  44.     NSTART = 2;        { number starts is display[NSTART] }
  45.     NEND = 9;        { . and ends at display[NEND] }
  46.     ERRFLAG = 10;    { error flag display[ERFLAG] }
  47.  
  48. type
  49.     st = (OK, ERROR);
  50.     vt = (INTEGER, REAL);
  51.     key = (DOT,PLUS,MINUS,DIV,MULT,EQUAL,ON,OFF,CLEAR,NEG,noop);
  52.     number = record value: real;
  53.             scale: integer;
  54.             vtype: vt;
  55.          end;
  56.  
  57. var
  58.     r, m: number;
  59.     ukey: integer;
  60.     code, lastop: key;
  61.     done: boolean;
  62.     state: st;
  63.     disp: array [1..DISPSIZE] of char;
  64.  
  65. function power(base, n: integer): integer;
  66. var i, value: integer;
  67. begin
  68.     value := 1;
  69.     for i := 1 to n do value := value * base;
  70.     power := value
  71. end; { power }
  72.  
  73. procedure getkey(var code: integer);
  74. var c: char;
  75. begin
  76.     read(c);
  77.     if (c >= '0') and (c <= '9') then code := ord(c) - ord('0')
  78.     else
  79.     begin   if c = '.' then code := 10
  80.         else if c = '+' then code := 11
  81.         else if c = '-' then code := 12
  82.         else if c = '/' then code := 13
  83.         else if c = '*' then code := 14
  84.         else if c = '=' then code := 15
  85.         else if c = 'c' then code := 18
  86.         else if c = 'n' then code := 19
  87.         else if c = 'o' then
  88.         begin   read(c);
  89.             if c = 'n' then code := 16
  90.             else code := 17
  91.         end
  92.         else code := 20
  93.     end;
  94.     readln
  95. end; { getkey }
  96.  
  97. function mapkey(code: integer): key;
  98. begin
  99.     if code = 10 then mapkey := DOT
  100.     else if code = 11 then mapkey := PLUS
  101.     else if code = 12 then mapkey := MINUS
  102.     else if code = 13 then mapkey := DIV
  103.     else if code = 14 then mapkey := MULT
  104.     else if code = 15 then mapkey := EQUAL
  105.     else if code = 16 then mapkey := ON
  106.     else if code = 17 then mapkey := OFF
  107.     else if code = 18 then mapkey := CLEAR
  108.     else if code = 19 then mapkey := NEG
  109.     else mapkey := noop
  110. end; { mapkey }
  111.  
  112.  
  113. begin
  114.     done := false;
  115.     state := ERROR;
  116.     while not done do
  117.     begin   getkey(ukey);
  118.         code := mapkey(ukey);
  119.         if (code = ON) or (code = CLEAR) then
  120.         begin clear(m);
  121.           clear(r);
  122.           state := OK;
  123.           lastop := noop;
  124.           display(r)
  125.         end
  126.         else if code = OFF then done := true
  127.         else if state = OK then
  128.         begin   if (ukey >= 0) and (ukey <= 9) then
  129.             begin   if r.vtype = INTEGER then r.value := r.value*10 + ukey
  130.                 else
  131.                 begin   r.scale := r.scale + 1;
  132.                     r.value := r.value + ukey / power(10, r.scale)
  133.                 end;
  134.                 display(r);
  135.                 if lastop = EQUAL then lastop := noop
  136.             end
  137.             else if (code = PLUS) or (code = MINUS) or (code = MULT) or
  138.                 (code = DIV) then
  139.             begin if lastop = noop then m := r
  140.               else if lastop <> EQUAL then
  141.               begin eval(m, r, lastop);
  142.                 display(m)
  143.               end;
  144.               clear(r);
  145.               lastop := code
  146.             end
  147.             else case code of
  148.             DOT:
  149.             begin if r.vtype = REAL then state := ERROR
  150.                   else r.vtype := REAL;
  151.                   display(r)
  152.             end;
  153.             EQUAL:
  154.             begin if (lastop <> noop) and (lastop <> EQUAL) then
  155.                   begin eval(m, r, lastop);
  156.                         display(m)
  157.                   end;    
  158.                   clear(r);
  159.                   lastop := EQUAL
  160.             end;
  161.             NEG:
  162.             begin r.value := -r.value;
  163.                   display(r)
  164.             end;
  165.         end { of case }
  166.           end { of else }
  167.     end { of while }
  168. end.
  169.  ** END BLOCK OF INSERTED TEXT TO BOOST FILE SIZE
  170.  **/
  171.  
  172.     /* (busy) wait for lock file ... */
  173.     while ((f = open("lock", 0)) < 0) sleep(3);
  174.     close(f);
  175.     signal(SIGALRM, onalarm);
  176.     alarm(GRANULE);
  177.     while (read(0, &c, 1) == 1) {
  178.         if (i % 20 == 0 && thres - i > est_mean * GRANULE)
  179.             sleep(rand() & 0x3);
  180.         write(1, &c, 1);
  181.         write(2, &c, 1);    /* copy onto std error */
  182.         i++;
  183.         if (i > thres) {
  184.             pause();
  185.         }
  186.     }
  187. }
  188. Filler to make 200 lines & 4500 bytes.
  189. This
  190. is
  191. so
  192. boring
  193. you
  194. wouldn't
  195. believe it.
  196.  
  197. onalarm()
  198. {
  199.     thres += est_rate;
  200.     signal(SIGALRM, onalarm);
  201.     alarm(GRANULE);
  202. }
  203.