home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / pocketbk / developmen / lpcs / LPCLNT.C < prev    next >
C/C++ Source or Header  |  1994-04-07  |  7KB  |  285 lines

  1. /*    MODULE - LPCLNT.C
  2. Written by Colly, October 1992
  3. */
  4. #include <p_std.h>
  5. #include <p_gen.h>
  6. #include <p_sys.h>
  7. #include <p_file.h>
  8. #include <epoc.h>
  9. #include "lp.h"
  10.  
  11. #define MAX_CH 8
  12.  
  13. GLREF_D VOID *winHandle; /* The open consol channel handle */
  14.  
  15. LOCAL_D HANDLE srvPid; /* The server we are talking to. */
  16. LOCAL_D WORD sStat; /* The I/O completion word for death notification */
  17. LOCAL_D TEXT buf[0x100]; /* The data receive buffer */
  18. LOCAL_D UINT addr; /* The address to be displayed */
  19. LOCAL_D INT lCnt; /* The current line count */
  20.  
  21. LOCAL_C VOID panic(TEXT *mess,INT err)
  22. /*
  23. Output error message and terminate program.
  24. */
  25.     {
  26.     TEXT buf[256];
  27.  
  28.     if (err)
  29.     {
  30.     p_errs(&buf[0],err);
  31.     p_printf("%s failed - %s",mess,&buf[0]);
  32.     }
  33.     else
  34.     p_printf("%s",mess);
  35.     p_getch();
  36.     p_exit(TRUE);
  37.     }
  38.  
  39. LOCAL_C INT disp(INT len)
  40. /*
  41. Display len bytes of data.
  42. */
  43.     {
  44.     INT c,d;
  45.     TEXT dis[MAX_CH+2],*p;
  46.  
  47.     
  48.     p=(&buf[0]);
  49.     while (len)
  50.     {
  51.     lCnt++;
  52.     p_print("%04x: ",addr);
  53.     d=(len>MAX_CH ? MAX_CH : len);
  54.     p_bcpy(&dis[0],p,d);
  55.     for (c=0;c<d;c++)
  56.         {
  57.         p_print("%02x ",dis[c]);
  58.         if (!p_isprint(dis[c]))
  59.         dis[c]='.';
  60.         }
  61.     for (;c<MAX_CH;c++)
  62.         {
  63.         p_print("   ");
  64.         dis[c]=' ';
  65.         }
  66.     dis[8]=0;
  67.     p_printf(" %s",&dis[0]);
  68.     if (lCnt==8)
  69.         {
  70.         lCnt=0;
  71.         p_print("Press Esc or ANY other key");
  72.         c=p_getch();
  73.         p_printf("");
  74.         if (c==0x1b)
  75.         return(TRUE);
  76.         }
  77.     addr+=d;
  78.     p+=d;
  79.     len-=d;
  80.     }
  81.     return(FALSE);
  82.     }
  83.  
  84. LOCAL_C INT talk(VOID *par)
  85. /*
  86. Send the STEP message to the server and wait for DEATH or the
  87. reply.
  88. */
  89.     {
  90.     INT ret,sigCount;
  91.     WORD stat;
  92.  
  93.     p_msendreceivea(srvPid,TY_LINKSV_STEP,par,&stat); /* Send the message and get a reply */
  94.     sigCount=0;
  95.     FOREVER
  96.     {
  97.     p_iowait(); /* Wait for something to happen */
  98.     if (sStat!=E_FILE_PENDING) /* The server has died */
  99.         {
  100.         p_mcancel(); /* Cancel the receive request */
  101.         p_waitstat(&stat); /* Wait for the signal */
  102.         ret=E_GEN_RECEIVER; /* Return the error */
  103.         break;
  104.         }
  105.     else if (stat!=E_FILE_PENDING) /* The result has been returned */
  106.         {
  107.         ret=stat;
  108.         break;
  109.         }
  110.     else
  111.         sigCount++; /* Count up all other signals */
  112.     }
  113.     while (sigCount--) /* We have got some spare signals */
  114.         p_iosignal(); /* Put them back */
  115.     return(ret);
  116.     }
  117.  
  118. GLDEF_C VOID main(VOID)
  119. /*
  120. Act as a link paste client.
  121. */
  122.     {
  123.     INT c,ret;
  124.     HANDLE wsrvPid;
  125.     ULONG fmt,*pfmt;
  126.     TEXT *f[4];
  127.     UWORD args[2];
  128.  
  129. /*
  130. Print a banner message and get the console open.
  131. */
  132.     p_printf("Link Paste Client V1.00F");
  133. /*
  134. We need the pid of the window server as we will send it
  135. a message when we go to background to register ourselves as
  136. the link paste server. No point checking the return value as
  137. if the window server has disappeared there is nobody to print
  138. an error message for us anyway!
  139. */
  140.     wsrvPid=p_pidfind("SYS$WSRV.*");
  141. /*
  142. Enter the main loop.
  143. */
  144.     FOREVER
  145.     {
  146.     p_print("\r\nPress (F)etch, (Q)uit < >\b\b");
  147.     FOREVER
  148.         {
  149.         c=p_toupper(p_getch());
  150.         if (c=='Q')
  151.         p_exit(FALSE);
  152.         else if (c=='F')
  153.         break;
  154.         else
  155.         p_print("\7");
  156.         }
  157.     p_printf("F");
  158. /*
  159. First we have to interrogate the system for the link server pid
  160. and the formats that the server can render the data. Note that
  161. we send the address of a ULONG to receive the format type data.
  162. */
  163.     pfmt=(&fmt);
  164.     srvPid=p_msendreceivew(wsrvPid,SY_LINK_PASTE,&pfmt); /* Query the window server */
  165.     if (srvPid<0) /* Some kind of error here */
  166.         {
  167.         p_errs(&buf[0],srvPid); /* Just display the error */
  168.         p_printf("Getting server info\r\n%s",&buf[0]);
  169.         continue;
  170.         }
  171.     if (!srvPid)
  172.         {
  173.         p_printf("No data available for Link Paste");
  174.         continue;
  175.         }
  176. /*
  177. Determine what formats are available. We won't ever get native here
  178. as we are never a link paste server.
  179. */
  180.     f[0]="PLAIN TEXT      - unavailable";
  181.     f[1]="TABBED TEXT     - unavailable";
  182.     f[2]="PARAGRAPH TEXT  - unavailable";
  183.     if (fmt&DF_LINK_TEXT)
  184.         f[0]="PLAIN TEXT      - P";
  185.     if (fmt&DF_LINK_TABTEXT)
  186.         f[1]="TABBED TEXT     - T";
  187.     if (fmt&DF_LINK_PARAS)
  188.         f[2]="PARAGRAPH TEXT  - R";
  189.     f[3]="Quit            - Q";
  190. /*
  191. Display the name of the server and the formats available.
  192. */
  193.     p_pname(srvPid,&buf[0]);
  194.     p_printf("Server is %s",&buf[0]);
  195.     for (ret=0;ret<4;ret++)
  196.         p_printf("%s",f[ret]);
  197.     p_print("Press P,Q,R or T < >\b\b");
  198.     FOREVER
  199.         {
  200.         c=p_toupper(p_getch());
  201.         switch (c)
  202.         {
  203.         case 'P':
  204.         if (fmt&DF_LINK_TEXT)
  205.             {
  206.             args[0]=DF_LINK_TEXT_VAL;
  207.             break;
  208.             }
  209.         continue;
  210.         case 'T':
  211.         if (fmt&DF_LINK_TABTEXT)
  212.             {
  213.             args[0]=DF_LINK_TABTEXT_VAL;
  214.             break;
  215.             }
  216.         continue;
  217.         case 'R':
  218.         if (fmt&DF_LINK_PARAS)
  219.             {
  220.             args[0]=DF_LINK_PARAS_VAL;
  221.             break;
  222.             }
  223.         continue;
  224.         case 'Q':
  225.         break;
  226.         default:
  227.         p_print("\7");
  228.         continue;
  229.         }
  230.         p_printf("%c",c);
  231.         break;
  232.         }
  233.     if (c=='Q') /* User does'nt want anything, so ask again */
  234.         continue;
  235. /*
  236. We are about to enter into conversation with the server. We
  237. need to request an asynchronous notification if the server
  238. dies at some point during the conversation.
  239. */
  240.     p_logona(srvPid,&sStat); /* Request notification */
  241. /*
  242. Finally here we go getting the data. First communicate with the
  243. server and ask it to render the data in the requested format.
  244. Note that we use a special routine which sends the message to the
  245. server asynchronously and waits for either the reply or the server
  246. dying.
  247. */
  248.     ret=talk(&args[0]); /* Request a rendering */
  249.     if (ret==0) /* Server prepared to render data */
  250.         {
  251.         lCnt=0;
  252.         addr=0;
  253.         FOREVER
  254.         {
  255.         args[0]=(UWORD)(&buf[0]);
  256.         args[1]=sizeof(buf);
  257.         ret=talk(&args[0]); /* Get the server data */
  258.         if (ret<0)
  259.             {
  260.             if (ret==E_FILE_EOF)
  261.             {
  262.             p_printf("---------- EOF ----------");
  263.             ret=0; /* Avoid an error print */
  264.             }
  265.             break;
  266.             }
  267.         if (disp(ret)) /* Display the data received */
  268.             {
  269.             args[0]=0;
  270.             talk(&args[0]); /* Inform the server that we are finished */
  271.             break;
  272.             }
  273.         }
  274.         }
  275.     if (sStat==E_FILE_PENDING) /* We are still logged onto the server */
  276.         p_logoffa(srvPid); /* Cancel the notification */
  277.     if (ret<0)
  278.         {
  279.         p_errs(&buf[0],ret); /* Just display the error */
  280.         p_printf("Error talking to the server\r\n%s",&buf[0]);
  281.         }
  282.     }
  283.     }
  284.  
  285.