home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / fish / telecom / uucp_442 / src / gutil / gnote.c < prev    next >
C/C++ Source or Header  |  1990-10-08  |  3KB  |  206 lines

  1.  
  2. /*
  3.  *  GNOTE.C
  4.  *
  5.  *  $Header: Beta:src/uucp/src/GUtil/RCS/gnote.c,v 1.1 90/02/02 11:58:55 dillon Exp Locker: dillon $
  6.  *
  7.  *  (C) Copyright 1989-1990 by Matthew Dillon,  All Rights Reserved.
  8.  *
  9.  *  GNOTE hellofile notefile
  10.  *
  11.  *  Allow the user to enter a note and append it to notefile.  See
  12.  *  Getty. Password entry should have a '*' in front of the executable
  13.  *  name for Getty to provide automatic stdin/stdout
  14.  *
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include <time.h>
  19. #include "protos.h"
  20. #include "version.h"
  21.  
  22. IDENT(".01");
  23.  
  24. char *GetLine();
  25. void PutDaChar();
  26. void FlushChars();
  27.  
  28. main(ac, av)
  29. char *av[];
  30. {
  31.     time_t t;
  32.     char *ptr;
  33.     FILE *fi;
  34.  
  35.     time(&t);
  36.  
  37.     if (ac < 3) {
  38.     printf("gnote: expect 2 arguments, got %d\n", ac - 1);
  39.     fflush(stdout);
  40.     Delay(50*2);
  41.     exit(1);
  42.     }
  43.     printf("%s %s", Ident, ctime(&t));
  44.     if (fi = fopen(av[1], "r")) {
  45.     char buf[128];
  46.     while (fgets(buf, sizeof(buf), fi)) {
  47.         short len = strlen(buf);
  48.         if (len > 0 && buf[len-1] == '\n')
  49.         --len;
  50.         buf[len++] = 13;
  51.         buf[len++] = 10;
  52.         buf[len++] = 0;
  53.         fputs(buf, stdout);
  54.     }
  55.     fclose(fi);
  56.     }
  57.     fflush(stdout);
  58.  
  59.     if ((fi = fopen(av[2], "a")) == NULL) {
  60.     puts("couldn't open note file");
  61.     fflush(stdout);
  62.     Delay(50*2);
  63.     exit(1);
  64.     }
  65.     fprintf(fi, "**NOTE** %s", ctime(&t));
  66.  
  67.     while (ptr = GetLine()) {
  68.     if (strcmp(ptr, ".") == 0)
  69.         break;
  70.     if (strncmp(ptr, "**NOTE**", 8) == 0)
  71.         fprintf(fi, " ");
  72.     fprintf(fi, "%s\n", ptr);
  73.     }
  74.     puts("Thank you, hanging up in 8 seconds");
  75.     fflush(stdout);
  76.     fclose(fi);
  77.     Delay(50*8);
  78.     return(0);
  79. }
  80.  
  81.  
  82. char *
  83. GetLine()
  84. {
  85.     static char buf[256];
  86.     static char tab[256];
  87.     short col = 0;
  88.     short i = 0;
  89.     short limit = 5*60;     /*    5 minute idle timeout    */
  90.     short lcnt = 0;
  91.     short c;
  92.  
  93.     for (;;) {
  94.     c = GetDaChar();
  95.     if (c == EOF) {
  96.         clearerr(stdin);
  97.         ++lcnt;
  98.         if (lcnt == limit - 30) {
  99.         puts("\r\n(idle, hangup in 30)\r\n");
  100.         fflush(stdout);
  101.         }
  102.         if (lcnt == limit)
  103.         break;
  104.         continue;
  105.     }
  106.     lcnt = 0;
  107.  
  108.     switch(c) {
  109.     case 10:
  110.     case 13:
  111.         PutDaChar('\r');
  112.         PutDaChar('\n');
  113.         FlushChars();
  114.         buf[i] = 0;
  115.         return(buf);
  116.     case 8:
  117.     case 127:
  118.         if (i) {
  119.         short n = 1;
  120.         --i;
  121.         if (buf[i] == 9)
  122.             n = tab[i];
  123.         col -= n;
  124.         while (n--) {
  125.             PutDaChar('\010');
  126.             PutDaChar(' ');
  127.             PutDaChar('\010');
  128.         }
  129.         }
  130.         break;
  131.     case 'x'&0x1F:
  132.         PutDaChar('^');
  133.         PutDaChar('X');
  134.         PutDaChar('\r');
  135.         PutDaChar('\n');
  136.         i = 0;
  137.         break;
  138.     case 'r'&0x1F:
  139.         PutDaChar('\r');
  140.         PutDaChar('\n');
  141.         FlushChars();
  142.         buf[i] = 0;
  143.         printf("%s", buf);
  144.         fflush(stdout);
  145.         break;
  146.     case 9:
  147.         if (i < sizeof(buf) - 2) {
  148.         tab[i] = 8 - (col & 7);
  149.         col += tab[i] - 1;  /*    because we ++ below */
  150.         }
  151.         /* fall through */
  152.     default:
  153.         if (i < sizeof(buf) - 2) {
  154.         PutDaChar(c);
  155.         buf[i++] = c;
  156.         ++col;
  157.         } else {
  158.         PutDaChar('g' & 0x1F);
  159.         }
  160.         break;
  161.     }
  162.     }
  163.     return(NULL);
  164. }
  165.  
  166. void
  167. FlushChars()
  168. {
  169.     PutDaChar(256);
  170. }
  171.  
  172. void
  173. PutDaChar(c)
  174. {
  175.     static char buf[256];
  176.     static short len;
  177.  
  178.     if (c == 256 || len == sizeof(buf)) {
  179.     if (len)
  180.         write(1, buf, len);
  181.     len = 0;
  182.     }
  183.     if (c != 256)
  184.     buf[len++] = c;
  185. }
  186.  
  187.  
  188. GetDaChar()
  189. {
  190.     static char buf[256];
  191.     static short idx, len;
  192.  
  193.     if (idx == len) {
  194.     if (read(0, buf, 0) < 0)
  195.         FlushChars();
  196.     idx = 0;
  197.     len = read(0, buf, sizeof(buf));
  198.     if (len <= 0) {
  199.         len = 0;
  200.         return(EOF);
  201.     }
  202.     }
  203.     return((int)buf[idx++]);
  204. }
  205.  
  206.