home *** CD-ROM | disk | FTP | other *** search
/ Network CD 2 / Network CD - Volume 2.iso / programs / internet / dnet / dshterm1_0.lha / client / mterm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-04  |  8.3 KB  |  310 lines

  1. /**********************************************************
  2.  *                                                        *
  3.  * Minimal Term for DNET (uc) Stone, SST                  *
  4.  * Further modifications by Unknown, SST                  *
  5.  * Based on FTerm by Matt Dillon                          *
  6.  *                                                        *
  7.  * MTerm portname [port] [networknumber]                  *
  8.  *                                                        *
  9.  * Set ya TABS to 4!                                      *
  10.  * Email me!! c9107253@mystra.newcastle.edu.au            *
  11.  * For any reason!! (eg DNet, AmiNet, Demos, amigas...)   *
  12.  * Please send any changes U make!                        *
  13.  *                                                        *
  14.  * Mega-thanx to Azza of the SST for hours of DNET source *
  15.  * modification.... yes it compiles under SAS 6!          *
  16.  **********************************************************/
  17. /* DNET TEXT SERVER */
  18.  
  19. #include "defs.h"
  20. #include <exec/ports.h>
  21. #include <dos/dos.h>
  22. #include <st/textmessage.h>
  23. #include <st/st_proto.h>
  24.  
  25. /* Commands this DnetClient->TextServer knows */
  26. /* Note format: "CMD","CMD2","Help message" where CMD2 is a shortcut */
  27.  
  28. char *cmds[] = {
  29.   "FLUSH","F","Flush input buffers",
  30.   "LOG","L","Log to file (eg LOG ram:capture1)",
  31.   "LISTLOG","LL","List log files open",
  32.   "MULTILOG","ML","Log to multiple files (eg MULTILOG 1 ram:capture1)",
  33.   "CLOSELOG","CL","Close log file",
  34.   "CLOSEMULTILOG","CML","Close log file 'n'"
  35. };
  36.  
  37. /* Strings per command  */
  38. #define CMDN        3
  39. /* Commands a client may send to us at anytime */
  40. #define SCMDS_FLUSH            0
  41. #define SCMDS_LOG            1
  42. #define SCMDS_MULTILOG        2
  43. #define SCMDS_CLOSELOG        3
  44. #define SCMDS_CLOSEMULTILOG    4
  45. #define    SCMDS_LISTLOG        5
  46. #define CMDCOUNT            6
  47.  
  48.  
  49. /* Some default things */
  50. char    *WHITE        = " \t\n\r",
  51.         *eolntok    = "\n\r";
  52.  
  53.  
  54. long    out, err, in;
  55.  
  56. /* FOLLOWING GLOBALS COPIED FROM FTERM.C */
  57. char    Buf[512],
  58.         Cooked,     /*  bit 0 = cooked,  bit 1 = local echo */
  59.         *progname;
  60.  
  61. /* File things for capture */
  62. #define MAXNOOFFILES 20
  63. BPTR    mainlogfile = NULL,
  64.         auxlogfiles[MAXNOOFFILES];
  65.  
  66. void    *chan;
  67.  
  68. extern struct IntuitionBase *IntuitionBase;
  69. extern struct GfxBase *GfxBase;
  70.  
  71. #define SIGNALS (SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D | SIGBREAKF_CTRL_E | SIGBREAKF_CTRL_F)
  72.  
  73.  
  74. /*********************************************************/
  75.         char *sPrintCommands(char *str)
  76. /*********************************************************
  77.     Print out the commands we understand into a string
  78. **********************************************************/
  79. {
  80. short i;
  81. char  linebuf[400];
  82.     linebuf[399] = '\0';
  83.     if(!str) return(NULL);
  84.     strcpy(str, "Known commands :-\n");
  85.     for(i = 0; i < CMDCOUNT*CMDN; i += CMDN) {
  86.         spf(linebuf,399,"%s\tor\t%s\t:%s\n",cmds[i],cmds[i+1],cmds[i+2]);
  87.         strcat(str,linebuf);
  88.     }
  89.     return(str);
  90. }
  91.  
  92. /*********************************************************/
  93.     short DoCommands(char *string, struct TextMessage *reply)
  94. /*********************************************************
  95.             Decode a command passed to us and do
  96.         stuff from there (eg open log files etc)
  97. **********************************************************/
  98. {
  99. char    *argument, *command, *rest, linebuf[400];
  100. short    i;
  101.     linebuf[399] = '\0';
  102.     command = strtok(string,WHITE);
  103.     rest = strtok(NULL,NULL);
  104.     strupper(command);
  105.  
  106.     for(i = 0; i < CMDCOUNT*CMDN; i += CMDN) {
  107.         if( (strncmp(command,cmds[i],strlen(cmds[i])) == 0) ||
  108.             (strncmp(command,cmds[i+1],strlen(cmds[i])) == 0) )
  109.  
  110.             switch((i+CMDN-1)/CMDN) {
  111.                 case SCMDS_FLUSH:
  112.                     DIoctl(chan, CIO_FLUSH, 0, 0);
  113.                     if(TMREPLYPORT(reply))
  114.                         strcpy(AllocString0(&(reply->RepString),30),"Input Flushed\n");
  115.                     return(1);
  116.  
  117.                 case SCMDS_LOG:
  118.                     argument = strtok(rest,WHITE);
  119.                     SafeClose(&mainlogfile);
  120.                     if(mainlogfile = Open(argument,MODE_NEWFILE))
  121.                         spf(linebuf,399,"Writing all output to '%s'\n",argument);
  122.                     else
  123.                         spf(linebuf,399,"Couldn't write to '%s'\n",argument);
  124.  
  125.                     if(TMREPLYPORT(reply))
  126.                         strcpy(AllocString0(&(reply->RepString),1000),linebuf);
  127.                     return(1);
  128.  
  129.                 case SCMDS_CLOSELOG:
  130.                     SafeClose(&mainlogfile);
  131.                     return(1);
  132.  
  133.                 default:
  134.                     if(TMREPLYPORT(reply)) {
  135.                         spf(linebuf,399,"Sorry... '%s' unimplemented\n",cmds[i]);
  136.                         strcpy(AllocString0(&(reply->RepString),1000),linebuf);
  137.                     }
  138.                 return(0);
  139.             }
  140.     }
  141.     if(TMREPLYPORT(reply)) {
  142.         spf(linebuf,399,"Unknown command: '%s'\n",command);
  143.         strcpy(AllocString0(&(reply->RepString),1000),linebuf);
  144.     }
  145.     return(0);
  146. }
  147.  
  148.  
  149. /**********************************************************/
  150.              int main(int argc, char **argv)
  151. /**********************************************************
  152.                 Do the minimal term stuff!
  153.         Wait on our message ports for incoming
  154.  **********************************************************/
  155. {
  156. long    pmask, dmask, mask, i, j;
  157.  
  158. char    notdone = 1,
  159.         *host    = NULL,
  160.         *portname = argv[1];
  161.  
  162. UWORD    port    =    PORT_IALPHATERM;
  163. struct    MsgPort        *ourport = NULL;
  164. struct    TextMessage    *currtxtmsg;
  165.  
  166.     out = Output(); in = Input(); err = Open("*", MODE_OLDFILE);
  167.  
  168.     IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0);
  169.     GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0);
  170.  
  171.     for(i = 0; i < MAXNOOFFILES; i++ ) auxlogfiles[i] = NULL;
  172.     progname = argv[0];
  173.  
  174.     fpf(out,"MTERM for DNET [port->Dnet->Screen] (uc) Stone, SST\n");
  175.     fpf(out,"email: c9107253@cs.newcastle.edu.au (1993)\n");
  176.     if( argc<2 ) {
  177.         fpf(out,"Use: %s [>file] portname [dnet_portnumber] [network]\n",argv[0]);
  178.         fpf(out,"(The portname and port number are different concepts!)\n");
  179.         return(5);
  180.     }
  181.  
  182. /*    onbreak(brk); */
  183.  
  184.     if(argc > 2)
  185.         port = (UWORD) atoi(argv[2]);
  186.     if(argc > 3)
  187.         host = argv[3];
  188.  
  189.     fpf(out,"Opening port %ld on network %s\n",port,host?host:"0");
  190.  
  191. /* DO THAT FUNKY DNET STUFF STUFF */
  192.     if( ! (chan = DOpen(host, port, 20, 15)) ) {
  193.         fpf(out,"Unable to connect\n");
  194.         goto e3; }
  195.  
  196.     DQueue(chan, 32);
  197.     dmask   = 1 << ((PORT *)chan)->mp_SigBit;
  198.  
  199. /* GIVE THE OTHER SIDE A TERMINAL SIZE */
  200.     DIoctl(chan, CIO_SETROWS, 25, 0); DIoctl(chan, CIO_SETCOLS, 80, 0);
  201.  
  202.     if( !(ourport = CreatePort(portname,0)) ) {
  203.         fpf(out,"Can't create a port...\n");
  204.         goto e3; }
  205.  
  206.     fpf(out,"Reading from port '%s'\n",portname);
  207.  
  208.     pmask = 1 << ourport->mp_SigBit;
  209.  
  210. /* HIT ME, U CAN'T HURT ME! */
  211.     while (notdone) {
  212.         mask = Wait(dmask | pmask | SIGNALS);
  213.  
  214.         if(mask & pmask)
  215.             while (currtxtmsg = (struct TextMessage *)GetMsg(ourport))
  216.             {
  217.                 TextMessageInitReply(currtxtmsg);
  218.  
  219.                 switch(currtxtmsg->Command) {
  220.                     case TM_TEXT:
  221.                         i = 0;
  222.                         for(j = STRLEN(&(currtxtmsg->String))
  223.                                                 ; j >= 256 ; j -= 256) {
  224.                             DWrite(chan,&(currtxtmsg->String.String[i]), 256);
  225.                             i += 256;
  226.                         }
  227.                         if(j > 0)
  228.                             DWrite(chan,&(currtxtmsg->String.String[i]), j);
  229.  
  230.                         break;
  231.  
  232.                     case TM_SETWIDTH:
  233.                             DIoctl(chan, CIO_SETCOLS, currtxtmsg->Args[0], 0);
  234.                         break;
  235.  
  236.                     case TM_SETHEIGHT:
  237.                             DIoctl(chan, CIO_SETROWS, currtxtmsg->Args[0], 0);
  238.                         break;
  239.  
  240.                     case TM_EXIT:
  241.                         fpf(out,"\nRemote quit command sent\n");
  242.                         notdone = 0;
  243.                         break;
  244.  
  245.                     case TM_COMMAND:
  246.                         DoCommands(currtxtmsg->String.String, currtxtmsg);
  247.                         break;
  248.  
  249.                     case TM_HELP:
  250.                         if(TMREPLYPORT(currtxtmsg))
  251.                             sPrintCommands(AllocString0(&(currtxtmsg->RepString), 2000));
  252.                         break; 
  253.                 }
  254.                 TextMessageReply(&currtxtmsg);
  255.             }
  256.  
  257.         if (mask & dmask) {
  258.             int n;
  259.             if ((n = DNRead(chan, Buf, sizeof(Buf))) > 0) {
  260.                 /* WE GOT SOME TEXT! PRINT TO STD OUTPUT! */
  261.                 Write(out, Buf, n);
  262.                 if(mainlogfile) Write(mainlogfile, Buf, n);
  263.  
  264.             } else if (n == -2) {
  265.                 short val, cmd;
  266.                 char aux;
  267.                 cmd = DGetIoctl(chan, &val, &aux);
  268.                 switch(cmd) {
  269.                     case CIO_MODE:
  270.                         if(Cooked = val)
  271.                             fpf(out,"Mode change Cooked unimplemented...\n");
  272.                         else
  273.                             fpf(out,"Mode change Raw unimplemented...\n");
  274.                         break;
  275.                     case CIO_SETROWS:
  276.                         break;
  277.                     case CIO_SETCOLS:
  278.                         break;
  279.                 }
  280.  
  281.             } else if (n < 0) {
  282.                 /* FINE, WE SHOULD QUIT THEN... */
  283.                 fpf(out,"Ok ");
  284.                 notdone = 0;
  285.             }
  286.             if(mask & SIGBREAKF_CTRL_C) notdone = 0;
  287.             if(mask & SIGBREAKF_CTRL_D) notdone = 0; 
  288.             if(mask & SIGBREAKF_CTRL_E) notdone = 0; 
  289.             if(mask & SIGBREAKF_CTRL_F) notdone = 0; 
  290.  
  291.         }
  292.  
  293.     }
  294. e3:
  295.     fpf(out,"Closing...");
  296.     if(chan) DClose(chan);
  297.  
  298.     for(i = 0; i < MAXNOOFFILES; i++ ) SafeClose(&auxlogfiles[i]);
  299.     SafeClose(&mainlogfile);
  300.  
  301. /* remove any pending messages */
  302.     TextMessageSafeCleanup(ourport);
  303.  
  304.     CloseLibrary((LIB *)IntuitionBase);
  305.     CloseLibrary((LIB *)GfxBase);
  306.     SafeClose(&err);
  307.     fpf(out," done!\n");
  308.     return(0);
  309. }
  310.