home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / HSRC_100 / MISC.C < prev    next >
C/C++ Source or Header  |  1993-01-11  |  16KB  |  555 lines

  1. /*=============================================================================
  2.  
  3.                               HydraCom Version 1.00
  4.  
  5.                          A sample implementation of the
  6.                    HYDRA Bi-Directional File Transfer Protocol
  7.  
  8.                              HydraCom was written by
  9.                    Arjen G. Lentz, LENTZ SOFTWARE-DEVELOPMENT
  10.                   COPYRIGHT (C) 1991-1993; ALL RIGHTS RESERVED
  11.  
  12.                        The HYDRA protocol was designed by
  13.                  Arjen G. Lentz, LENTZ SOFTWARE-DEVELOPMENT and
  14.                              Joaquim H. Homrighausen
  15.                   COPYRIGHT (C) 1991-1993; ALL RIGHTS RESERVED
  16.  
  17.  
  18.   Revision history:
  19.   06 Sep 1991 - (AGL) First tryout
  20.   .. ... .... - Internal development
  21.   11 Jan 1993 - HydraCom version 1.00, Hydra revision 001 (01 Dec 1992)
  22.  
  23.  
  24.   For complete details of the Hydra and HydraCom licensing restrictions,
  25.   please refer to the license agreements which are published in their entirety
  26.   in HYDRACOM.C and LICENSE.DOC, and also contained in the documentation file
  27.   HYDRACOM.DOC
  28.  
  29.   Use of this file is subject to the restrictions contained in the Hydra and
  30.   HydraCom licensing agreements. If you do not find the text of this agreement
  31.   in any of the aforementioned files, or if you do not have these files, you
  32.   should immediately contact LENTZ SOFTWARE-DEVELOPMENT and/or Joaquim
  33.   Homrighausen at one of the addresses listed below. In no event should you
  34.   proceed to use this file without having accepted the terms of the Hydra and
  35.   HydraCom licensing agreements, or such other agreement as you are able to
  36.   reach with LENTZ SOFTWARE-DEVELOMENT and Joaquim Homrighausen.
  37.  
  38.  
  39.   Hydra protocol design and HydraCom driver:         Hydra protocol design:
  40.   Arjen G. Lentz                                     Joaquim H. Homrighausen
  41.   LENTZ SOFTWARE-DEVELOPMENT                         389, route d'Arlon
  42.   Langegracht 7B                                     L-8011 Strassen
  43.   3811 BT  Amersfoort                                Luxembourg
  44.   The Netherlands
  45.   FidoNet 2:283/512, AINEX-BBS +31-33-633916         FidoNet 2:270/17
  46.   arjen_lentz@f512.n283.z2.fidonet.org               joho@ae.lu
  47.  
  48.   Please feel free to contact us at any time to share your comments about our
  49.   software and/or licensing policies.
  50.  
  51. =============================================================================*/
  52.  
  53. #include "hydracom.h"
  54.  
  55.  
  56. static char *chatstart = "\007\007 * Chat mode start\r\n";
  57. static char *chatend   = "\007\007\r\n * Chat mode end\r\n";
  58. static char *chattime  = "\007\007\r\n * Chat mode end - timeout\r\n";
  59.  
  60.  
  61. static void loc_puts (char *s)
  62. {
  63.         while (*s) {
  64.               if (*s == '\007')
  65. #if WIN_AGL
  66.                  (void) win_bell();
  67. #else
  68.                  putc(7,stderr);
  69. #endif
  70.               else
  71. #if WIN_AGL
  72.                  win_putc(local_win,*s);
  73. #else
  74.                  putch(*s);
  75. #endif
  76.               s++;
  77.         }
  78. }/*loc_puts()*/
  79.  
  80.  
  81. int keyabort (void)
  82. {
  83. #define CHATLEN 256
  84.         static byte     chatbuf1[CHATLEN + 5],
  85.                         chatbuf2[CHATLEN + 5],
  86.                        *curbuf = chatbuf1;
  87.         static boolean  warned = false;
  88.         boolean         esc = false;
  89.         char           *p;
  90.         word            c;
  91.  
  92.         if (chattimer > 0L) {
  93.            if (time(NULL) > chattimer) {
  94.               chattimer = lasttimer = 0L;
  95.               hydra_devsend("CON",(byte *) chattime,strlen(chattime));
  96.               loc_puts(&chattime[2]);
  97.            }
  98.            else if ((time(NULL) + 10L) > chattimer && !warned) {
  99.               loc_puts("\007\r\n * Warning: chat mode timeout in 10 seconds\r\n");
  100.               warned = true;
  101.            }
  102.         }
  103.         else if (chattimer != lasttimer) {
  104.            if (chattimer ==  0L) {
  105.               if (nobell) p = " * Remote has chat facility with bell disabled\n";
  106.               else        p = " * Remote has chat facility with bell enabled\n";
  107.               hydra_devsend("CON",(byte *) p,(int) strlen(p));
  108.               loc_puts(" * Hydra session in progress, chat facility now available\r\n");
  109.            }
  110.            else if (chattimer == -1L)
  111.               loc_puts(" * Hydra session in init state, can't chat yet\r\n");
  112.            else if (chattimer == -2L)
  113.               loc_puts(" * Remote has no chat facility available\r\n");
  114.            else if (chattimer == -3L) {
  115.               if (lasttimer > 0L) loc_puts("\r\n");
  116.               loc_puts(" * Hydra session in exit state, can't chat anymore\r\n");
  117.            }
  118.            lasttimer = chattimer;
  119.         }
  120.  
  121. #if WIN_AGL
  122.         while (win_keyscan()) {
  123. #else
  124.         while (kbhit()) {
  125. #endif
  126.               switch (c = get_key()) {
  127.                      case Esc:
  128.                           esc = true;
  129.                           break;
  130.  
  131.                      case Alt_C:
  132.                           if (chattimer == 0L) {
  133.                              hydra_devsend("CON",(byte *) chatstart,strlen(chatstart));
  134.                              loc_puts(&chatstart[2]);
  135.                              chattimer = lasttimer = time(NULL) + CHAT_TIMEOUT;
  136.                           }
  137.                           else if (chattimer > 0L) {
  138.                              chattimer = lasttimer = 0L;
  139.                              hydra_devsend("CON",(byte *) chatend,strlen(chatend));
  140.                              loc_puts(&chatend[2]);
  141.                           }
  142.                           else
  143.                              loc_puts("\007");
  144.                           break;
  145.  
  146.                      default:
  147.                           if (c < ' ' || c > 126)
  148.                              break;
  149.  
  150.                      case '\r':
  151.                      case '\a':
  152.                      case '\b':
  153.                           if (chattimer <= 0L)
  154.                              break;
  155.  
  156.                           chattimer = time(NULL) + CHAT_TIMEOUT;
  157.                           warned = false;
  158.  
  159.                           if (chatfill >= CHATLEN)
  160.                              loc_puts("\007");
  161.                           else {
  162.                              switch (c) {
  163.                                     case '\r':
  164.                                          curbuf[chatfill++] = '\n';
  165.                                          loc_puts("\r\n");
  166.                                          break;
  167.  
  168.                                     case '\b':
  169.                                          if (chatfill > 0 && curbuf[chatfill - 1] != '\n')
  170.                                             chatfill--;
  171.                                          else {
  172.                                             curbuf[chatfill++] = '\b';
  173.                                             curbuf[chatfill++] = ' ';
  174.                                             curbuf[chatfill++] = '\b';
  175.                                          }
  176.                                          loc_puts("\b \b");
  177.                                          break;
  178.  
  179.                                     default:
  180.                                          curbuf[chatfill++] = (byte) c;
  181.                                          if (c != 7)
  182. #if WIN_AGL
  183.                                             win_putc(local_win,c);
  184. #else
  185.                                             putch(c);
  186. #endif
  187.                                          break;
  188.                              }
  189.                           }
  190.                           break;
  191.               }
  192.         }
  193.  
  194.         if (chatfill > 0 && hydra_devsend("CON",curbuf,chatfill)) {
  195.            curbuf = (curbuf == chatbuf1) ? chatbuf2 : chatbuf2;
  196.            chatfill = 0;
  197.         }
  198.  
  199.         return (esc);
  200. }/*keyabort()*/
  201.  
  202.  
  203. void rem_chat (byte *data, word len)
  204. {
  205.         len = len;
  206.  
  207. #if !WIN_AGL
  208.         local_x = wherex();
  209.         local_y = wherey();
  210.         window(1,11,80,17);
  211.         gotoxy(remote_x,remote_y);
  212. #endif
  213.  
  214.         while (*data) {
  215.               switch (*data) {
  216.                      case '\a':
  217.                           if (!nobell) {
  218. #if WIN_AGL
  219.                              (void) win_bell();
  220. #else
  221.                              putc(7,stderr);
  222. #endif
  223.                           }
  224.                           break;
  225.  
  226.                      case '\n':
  227. #if WIN_AGL
  228.                           win_putc(remote_win,'\r');
  229. #else
  230.                           putch('\r');
  231. #endif
  232.                           /* fallthrough to default */
  233.  
  234.                      default:
  235. #if WIN_AGL
  236.                           win_putc(remote_win,(int) *data);
  237. #else
  238.                           putch((int) *data);
  239. #endif
  240.                           break;
  241.               }
  242.               data++;
  243.         }
  244.  
  245. #if !WIN_AGL
  246.         remote_x = wherex();
  247.         remote_y = wherey();
  248.         window(1,19,80,25);
  249.         gotoxy(local_x,local_y);
  250. #endif
  251. }/*rem_chat()*/
  252.  
  253.  
  254. int parse(char *string)
  255. {
  256.         int ac = 0;
  257.         char *p;
  258.  
  259.         p = strchr(string,';');
  260.         if (p) *p = '\0';
  261.         strupr(string);
  262.  
  263.         av[ac] = strtok(string," \t\r\n\032");
  264.  
  265.         while (av[ac]) {
  266.               if (++ac > MAXARGS) {
  267.                  message(6,"!Too many arguments!");
  268.                  endprog(2);
  269.               }
  270.               av[ac]=strtok(NULL," \t\r\n\032");
  271.         }
  272.  
  273.         return (ac);
  274. }/*parse()*/
  275.  
  276.  
  277. void splitpath(char *filepath,char *path,char *file)
  278. {
  279.         char *p,*q;
  280.  
  281.         for (p=filepath;*p;p++) ;
  282.         while (p!=filepath && *p!=':' && *p!='\\' && *p!='/') --p;
  283.         if (*p==':' || *p=='\\' || *p=='/') ++p;        /* begin     */
  284.         q=filepath;
  285.         while (q!=p) *path++=*q++;                      /* copy path */
  286.         *path='\0';
  287.         strcpy(file,p);
  288. }/*splitpath()*/
  289.  
  290.  
  291. void mergepath(char *filepath,char *path,char *file)
  292. {
  293.         strcpy(filepath,path);
  294.         strcat(filepath,file);
  295. }/*mergepath()*/
  296.  
  297.  
  298. int fexist (char *filename)
  299. {
  300.         struct stat f;
  301.  
  302.         return ((stat(filename,&f) != -1) ? 1 : 0);
  303. }/*fexist()*/
  304.  
  305.  
  306. int get_key (void)
  307. {
  308. #if WIN_AGL
  309.         if (didsome)
  310.            return (win_keygetc());
  311.         else
  312. #endif
  313.              {
  314.            register int c = getch();
  315.  
  316.            return (c ? c : getch() | 0x100);
  317.         }
  318. }/*get_key()*/
  319.  
  320.  
  321. void any_key (void)
  322. {
  323.         fprintf(stderr,"Press any key to continue");
  324.         get_key();
  325.         fprintf(stderr,"\r                          \r");
  326. }/*any_key()*/
  327.  
  328.  
  329. int get_str (char *prompt, char *s, int maxlen)
  330. {
  331.         int i = (int) strlen(s);
  332.         char c;
  333.  
  334.         cprint("\r%s: %s",prompt,s);
  335.         for (;;) {
  336.             switch (c = get_key()) {
  337.                    case 13:
  338.                         s[i] = '\0';
  339.                         cprint("\n");
  340.                         return (i);
  341.  
  342.                    case 27:
  343.                         if (i) {
  344.                            do cprint("\b \b");
  345.                            while (--i);
  346.                         }
  347.                         s[0] = '\0';
  348.                         cprint("<aborted>\n");
  349.                         return (-1);
  350.  
  351.                    case 8:
  352.                    case 127:
  353.                         if (i) {
  354.                            --i;
  355.                            cprint("\b \b");
  356.                         }
  357.                         break;
  358.  
  359.                    default:
  360.                         if (i == maxlen || c < 32 || c > 126) {
  361. #if WIN_AGL
  362.                            (void) win_bell();
  363. #else
  364.                            putc(7,stderr);
  365. #endif
  366.                         }
  367.                         else {
  368.                            cprint("%c",c);
  369.                            s[i++] = c;
  370.                         }
  371.                         break;
  372.             }/*switch*/
  373.         }/*for*/
  374. }/*get_str()*/
  375.  
  376.  
  377. void resultlog (boolean xmit, char *fname, long bytes, long xfertime)
  378. {          /* Omen's DSZ compatible logfile - for RBBS-PC XFER-?.DEF reports */
  379.         FILE *fp;
  380.  
  381.         if (opuslog) {
  382.            if ((fp = sfopen(opuslog,"at",DENY_WRITE)) != NULL) {
  383.               if (fname) {
  384.                  fprintf(fp, "%s %s%s %ld", xmit ? "Sent" : "Got",
  385.                              xmit ? "" : download, fname, bytes);
  386.                  if (mailer)
  387.                     fprintf(fp," %ld",xfertime);
  388.                  fprintf(fp,"\n");
  389.               }
  390.               fclose(fp);
  391.            }
  392.            else
  393.               message(3,"-Couldn't append opus log-file %s",opuslog);
  394.         }
  395.  
  396.         if (result) {
  397.            if ((fp = sfopen(result,"at",DENY_WRITE)) != NULL) {
  398.               if (fname) {
  399.                  fprintf(fp, "%c %6ld %5u bps %4ld cps 0 errors     0 1024 %s -1\n",
  400.                              xmit ? 'H' : 'R',
  401.                              bytes, cur_speed,
  402.                              xfertime ? (bytes / xfertime) : 9999L,
  403.                              fname);
  404.               }
  405.               fclose(fp);
  406.            }
  407.            else
  408.               message(3,"-Couldn't append result-file %s",result);
  409.         }
  410. }/*resultlog()*/
  411.  
  412.  
  413. static char *mon[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  414.                          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  415.  
  416. char *h_revdate (long revstamp)
  417. {
  418.         static char  buf[12];
  419.         struct tm   *t;
  420.  
  421.         t = localtime(&revstamp);
  422.         sprintf(buf, "%02d %s %d",
  423.                      t->tm_mday, mon[t->tm_mon], t->tm_year + 1900);
  424.  
  425.         return (buf);
  426. }/*h_revdate()*/
  427.  
  428.  
  429. void message (int level, char *fmt,...)
  430. {
  431.         char       buf[255];
  432.         long       tim;
  433.         struct tm *t;
  434.         va_list    arg_ptr;
  435.  
  436.         tim = time(NULL);
  437.         t = localtime(&tim);
  438.  
  439.         va_start(arg_ptr,fmt);
  440.         sprintf(buf, "%c %02d %03s %02d:%02d:%02d %-4s ",
  441.                      *fmt, t->tm_mday, mon[t->tm_mon],
  442.                      t->tm_hour, t->tm_min, t->tm_sec, LOGID);
  443.         vsprintf(&buf[23], &fmt[1], arg_ptr);
  444.         va_end(arg_ptr);
  445.  
  446.         if (level >= loglevel && logfp)
  447.            fprintf(logfp, "%s\n", buf);
  448.  
  449. #if WIN_AGL
  450.         if (!file_win)
  451.            cprint("\r%s\n",buf);
  452.         else {
  453.            if (!log_first)
  454.               log_first = true;
  455.            else
  456.               win_putc(log_win,'\n');
  457.            win_puts(log_win,buf);
  458.         }
  459. #else
  460.         if (!file_x)
  461.            cprint("\r%s\n",buf);
  462.         else {
  463.            local_x = wherex();
  464.            local_y = wherey();
  465.            window(1,2,80,6);
  466.            if (log_y) {
  467.               gotoxy(1,log_y);
  468.               putch('\n');
  469.            }
  470.            cputs(buf);
  471.            log_y = wherey();
  472.            window(1,19,80,25);
  473.            gotoxy(local_x,local_y);
  474.         }
  475. #endif
  476. }/*message()*/
  477.  
  478.  
  479. void cprint (char *fmt, ...)
  480. {
  481.         char    buf[255];
  482.         va_list arg_ptr;
  483.  
  484.         va_start(arg_ptr,fmt);
  485.         vsprintf(buf, fmt, arg_ptr);
  486.         va_end(arg_ptr);
  487.  
  488. #if WIN_AGL
  489.         if (didsome)
  490.            win_puts(0,buf);
  491.         else
  492. #endif
  493.            fputs(buf,stdout);
  494. }/*cprint()*/
  495.  
  496.  
  497. void hydra_gotoxy (int x, int y)
  498. {
  499. #if WIN_AGL
  500.         win_setpos(file_win,x,y);
  501. #else
  502.         file_x = x;
  503.         file_y = y;
  504. #endif
  505. }/*hydra_gotoxy()*/
  506.  
  507.  
  508. void hydra_printf (char *fmt, ...)
  509. {
  510.         char    buf[255];
  511.         va_list arg_ptr;
  512.  
  513.         va_start(arg_ptr,fmt);
  514.         vsprintf(buf, fmt, arg_ptr);
  515.         va_end(arg_ptr);
  516.  
  517. #if WIN_AGL
  518.         win_puts(file_win,buf);
  519. #else
  520.         local_x = wherex();
  521.         local_y = wherey();
  522.         window(1,8,80,9);
  523.  
  524.         gotoxy(file_x,file_y);
  525.         cputs(buf);
  526.  
  527.         file_x = wherex();
  528.         file_y = wherey();
  529.         window(1,19,80,25);
  530.         gotoxy(local_x,local_y);
  531. #endif
  532. }/*hydra_printf()*/
  533.  
  534.  
  535. void hydra_clreol (void)
  536. {
  537. #if WIN_AGL
  538.         win_clreol(file_win);
  539. #else
  540.         local_x = wherex();
  541.         local_y = wherey();
  542.         window(1,8,80,9);
  543.  
  544.         gotoxy(file_x,file_y);
  545.         clreol();
  546.  
  547.         file_x = wherex();
  548.         file_y = wherey();
  549.         window(1,19,80,25);
  550.         gotoxy(local_x,local_y);
  551. #endif
  552. }/*hydra_clreol()*/
  553.  
  554. /* end of misc.c */
  555.