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

  1. /*    MODULE - LPSERV.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 <p_cons.h>
  9. #include <epoc.h>
  10. #include "lp.h"
  11.  
  12. #define MSG_Q_SIZE 4
  13.  
  14. typedef struct
  15.     {
  16.     E_MESSAGE m; /* Defined in EPOC.H */
  17.     UWORD arg1; /* First argument */
  18.     UWORD arg2; /* Second argument */
  19.     } MESS;
  20.  
  21. GLREF_D VOID *winHandle; /* The open consol channel handle */
  22.  
  23. LOCAL_C VOID panic(TEXT *mess,INT err)
  24. /*
  25. Output error message and terminate program.
  26. */
  27.     {
  28.     TEXT buf[256];
  29.  
  30.     if (err)
  31.     {
  32.     p_errs(&buf[0],err);
  33.     p_printf("%s failed - %s",mess,&buf[0]);
  34.     }
  35.     else
  36.     p_printf("%s",mess);
  37.     p_getch();
  38.     p_exit(TRUE);
  39.     }
  40.  
  41. GLDEF_C VOID main(VOID)
  42. /*
  43. Act as a link paste server.
  44. */
  45.     {
  46.     INT ret,iState;
  47.     HANDLE wsrvPid,clientPid;
  48.     WORD kStat,mStat;
  49.     ULONG fmt;
  50.     MESS *pM;
  51.     P_CON_KBREC k;
  52.     TEXT buf[0x40];
  53. /*
  54. As we are going to be a server we will be sent messages, so that we need
  55. to initialise message reception.
  56. */
  57.     if ((ret=p_minit(MSG_Q_SIZE,sizeof(MESS)-sizeof(E_MESSAGE)))<0)
  58.     panic("Initialise messaging",ret);
  59. /*
  60. Print a banner message and get the console open.
  61. */
  62.     p_printf("Link Paste Server V1.00F");
  63.     p_printf("Press ESC to exit");
  64. /*
  65. Next we need the pid of the window server as we will send it
  66. a message when we go to background to register ourselves as
  67. the link paste server. No point checking the return value as
  68. if the window server has disappeared there is nobody to print
  69. an error message for us anyway!
  70. */
  71.     wsrvPid=p_pidfind("SYS$WSRV.*");
  72. /*
  73. Read from the console asynchronously, include events
  74. so that we can detect when we go to background.
  75. */
  76.     p_ioc4(winHandle,P_EVENT_READ,&kStat,&k);
  77. /*
  78. Read a message asynchronously.
  79. */
  80.     p_mreceive(&mStat,&pM);
  81. /*
  82. Set clientPid to zero to indicate that we are inactive.
  83. */
  84.     clientPid=0;
  85. /*
  86. Now enter the main event processing loop.
  87. */
  88.     FOREVER
  89.     {
  90.     p_iowait(); /* Wait for something to happen */
  91.     if (mStat!=E_FILE_PENDING) /* We have received a message */
  92.         {
  93.         if (mStat!=0) /* Just some extra error checking */
  94.         panic("Message receive",ret);
  95.         switch (pM->m.type) /* Some kind of message has arrived */
  96.         {
  97.         case TY_LINKSV_STEP: /* Someone wants some data */
  98.         if (clientPid==0) /* First request, so setup the info */
  99.             {
  100.             ret=E_GEN_NSUP;
  101.             switch (pM->arg1)
  102.                 {
  103.             case DF_LINK_TEXT_VAL:
  104.             case DF_LINK_TABTEXT_VAL:
  105.             case DF_LINK_PARAS_VAL:
  106.                 clientPid=pM->m.pid; /* Register our client */
  107.                 p_logon(clientPid,TY_LINKSV_DEATH); /* Request a death message from EPOC */
  108.                 iState=0; /* Set the server state */
  109.                 ret=0; /* We can support the requested type */
  110.                 }
  111.             break; /* Reply with the result in ret */
  112.             }
  113.         if (!pM->arg1) /* If the client requests no data, give up */
  114.             iState=1; /* Jump to the end of data state */
  115.         switch (iState) /* Handle the various states */
  116.             {
  117.         case 0:
  118.             p_getdat(&buf[0]); /* Get the current date and time as text */
  119. /*
  120. Copy the data over to the client, after checking that we don't have more
  121. than the requested length. We all return the length copied as the result.
  122. */
  123.             ret=p_slen(&buf[0]); /* The length of the data */
  124.             if (ret>pM->arg2) /* pM->arg2 has the maximum length */
  125.             ret=pM->arg2;
  126. /*
  127. No need to do any conversion here as the data is the same for all renderings.
  128. */
  129.             p_pcpyto(clientPid,(VOID *)pM->arg1,&buf[0],ret); /* Copy over the data */
  130.             iState++; /* Advance to the next state */
  131.             break;
  132.         default:
  133.             ret=E_FILE_EOF; /* Signifies no more data */
  134.             p_logoff(clientPid,TY_LINKSV_DEATH); /* No longer interested if the client dies */
  135.             clientPid=0; /* We are no longer active */
  136.             }
  137.         break;
  138.         case TY_LINKSV_DEATH: /* Out client has died */
  139.         clientPid=0; /* Mark ourselves as inactive again */
  140.         break; /* Supervisor does not require a return result */
  141.         default: /* Some one has sent us a naughty message */
  142.         ret=E_GEN_NSUP; /* Just return a not supported error */
  143.         }
  144.         p_mfree(pM,ret); /* Return the message to the queue and return a result */
  145. /*
  146. Get another message asynchronously.
  147. */
  148.         p_mreceive(&mStat,&pM);
  149.         }
  150.     else if (kStat!=E_FILE_PENDING) /* We have received a console event */
  151.         {
  152.         if (kStat!=0) /* Just some extra error checking */
  153.         panic("Read console",ret);
  154.         if (k.keycode==CONS_EVENT_BACKGROUND) /* We are going to background */
  155.         {
  156.         fmt=DF_LINK_TEXT|DF_LINK_TABTEXT|DF_LINK_PARAS; /* We are prepared to render in any format */
  157.         ret=p_msendreceivew(wsrvPid,SY_LINK_SERVER,&fmt); /* Inform the window server */
  158.         if (ret<0)
  159.             panic("Wsrv link server request",ret);
  160.         }
  161.         else if (k.keycode==0x1b) /* User requested that we exit */
  162.         p_exit(TRUE);
  163. /*
  164. We've used the event so we must queue another read.
  165. */
  166.         p_ioc4(winHandle,P_EVENT_READ,&kStat,&k);
  167.         }
  168.     else
  169.         panic("Stray signal death",0);
  170.     }
  171.     }
  172.  
  173.