home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / System / lpDaemon SRC / lpd Sources / PAP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-24  |  7.0 KB  |  294 lines  |  [TEXT/KAHL]

  1. /************************************************************************
  2.  *                                                                        *
  3.  * PAP.C                                                                *
  4.  *                                                                        *
  5.  * With thanks to Mike Schuster whose article in MacTutor showed how to    *
  6.  * access the PAP code in the LaserWriter.  Also thanks to the CAP team    *
  7.  * at for their PAP code and printer access code.                        *
  8.  *                                                                        *
  9.  ************************************************************************/
  10.  
  11. #include "PAP.H"
  12.  
  13. static Handle    papHndl = NIL;     /* Handle to PAP manager resource */
  14. static Ptr        papPtr = NIL;     /* Points to locked PAP manager */
  15. static Handle    entHndl = NIL;    /* Handle to locked entity name */
  16.  
  17. Str255 LaserWriterName = "\pLaserWriter";
  18.  
  19. void SetExtnsFolder(void);
  20.  
  21.  
  22. /************************************************************************
  23.  ************************************************************************/
  24. OSErr PAPOpen(integer *prefnum, EntityPtr printer,
  25.                 integer quantum, PAPStatusRec *status, integer *ostate)
  26. {
  27. asm    {
  28.     CLR.W    -(SP)
  29.     MOVE.L    prefnum, -(SP)
  30.     MOVE.L    printer, -(SP)
  31.     MOVE.W    quantum, -(SP)
  32.     MOVE.L    status, -(SP)
  33.     MOVE.L    ostate, -(SP)
  34.     MOVE.L    papPtr, A0
  35.     JSR        0(A0)
  36.     MOVE.W    (SP)+, D0
  37.     }
  38. }
  39.  
  40. /************************************************************************
  41.  ************************************************************************/
  42. OSErr PAPRead(integer prefnum, Ptr data, integer *size,
  43.                                         integer *eof, integer *rstate)
  44. {
  45. asm    {
  46.     CLR.W    -(SP)
  47.     MOVE.W    prefnum, -(SP)
  48.     MOVE.L    data, -(SP)
  49.     MOVE.L    size, -(SP)
  50.     MOVE.L    eof, -(SP)
  51.     MOVE.L    rstate, -(SP)
  52.     MOVE.L    papPtr, A0
  53.     JSR        4(A0)
  54.     MOVE.W    (SP)+, D0
  55.     }
  56. }
  57.  
  58. /************************************************************************
  59.  ************************************************************************/
  60. OSErr PAPWrite(integer prefnum, Ptr data, integer size,
  61.                                         integer eof, integer *wstate)
  62. {
  63. asm    {
  64.     CLR.W    -(SP)
  65.     MOVE.W    prefnum, -(SP)
  66.     MOVE.L    data, -(SP)
  67.     MOVE.W    size, -(SP)
  68.     MOVE.W    eof, -(SP)
  69.     MOVE.L    wstate, -(SP)
  70.     MOVE.L    papPtr, A0
  71.     JSR        8(A0)
  72.     MOVE.W    (SP)+, D0
  73.     }
  74. }
  75.  
  76. /************************************************************************
  77.  * This is the PAPStatus routine using the LaserWriter builtin PAP, but    *
  78.  * it doesn't always return a status, so the real PAP status is below    *
  79.  ************************************************************************/
  80. OSErr papStatus(EntityPtr prName, PAPStatusRec *status, AddrBlock *prtAddr);
  81. OSErr PAPStatus(EntityPtr prName, PAPStatusRec *status, AddrBlock *prtAddr)
  82. {
  83. asm    {
  84.     CLR.W    -(SP)
  85.     MOVE.L    prName, -(SP)
  86.     MOVE.L    status, -(SP)
  87.     MOVE.L    prtAddr, -(SP)
  88.     MOVE.L    papPtr, A0
  89.     JSR        12(A0)
  90.     MOVE.W    (SP)+, D0
  91.     }
  92. }
  93. /************************************************************************/
  94.  
  95. /************************************************************************
  96.  ************************************************************************/
  97. OSErr PAPClose(integer prefnum)
  98. {
  99. asm    {
  100.     SUBQ.L    #2, SP
  101.     MOVE.W    prefnum, -(SP)
  102.     MOVE.L    papPtr, A0
  103.     JSR        16(A0)
  104.     MOVE.W    (SP)+, D0
  105.     }
  106. }
  107.  
  108. /************************************************************************
  109.  ************************************************************************/
  110. Handle PAPLoad()
  111. {
  112.     integer    LWref;
  113.     integer wdRef;
  114.     LongInt wdID;
  115.     integer vol;
  116.  
  117.     if (entHndl) return entHndl;    /* already loaded */
  118.  
  119.     papHndl = NIL;
  120.     papPtr = NIL;
  121.     entHndl = NIL;
  122.  
  123.     GetWDir(&wdRef, &wdID, &vol);
  124.     SetExtnsFolder();
  125.  
  126.     if ( (LWref = OpenResFile(LaserWriterName)) == -1 )
  127.         {
  128.         log_printf("Cannot open laserwriter... \n");
  129.         SetWDir(wdRef, wdID, vol);
  130.         return NIL;
  131.         }
  132.  
  133.     if ( (papHndl = GetResource('PDEF', 10) ) == NIL || ResError() )
  134.         {
  135.         log_printf("Cannot find PDEF... \n");
  136.         CloseResFile(LWref);
  137.         SetWDir(wdRef, wdID, vol);
  138.         return NIL;
  139.         }
  140.     DetachResource(papHndl);
  141.     HLock(papHndl);
  142.     papPtr = *papHndl;
  143.  
  144.     if ( ( entHndl = GetResource('PAPA', -8192) ) == NIL || ResError() )
  145.         {
  146.         HUnlock(papHndl);
  147.         DisposHandle(papHndl);
  148.         CloseResFile(LWref);
  149.         log_printf("Cannot find name...\n");
  150.         SetWDir(wdRef, wdID, vol);
  151.         return NIL;
  152.         }
  153.  
  154.     DetachResource(entHndl);
  155.     HLock(entHndl);
  156.  
  157.     CloseResFile(LWref);
  158.  
  159.     SetWDir(wdRef, wdID, vol);
  160.     return entHndl;
  161. }
  162.  
  163.  
  164. /************************************************************************
  165.  ************************************************************************/
  166. OSErr PAPUnload()
  167. {
  168.     integer    res;
  169.  
  170.     if (papPtr)
  171. asm    {
  172.     MOVE.L    papPtr, A0
  173.     CLR.W    -(SP)
  174.     JSR        20(A0)
  175.     MOVE.W    (SP)+, res
  176.     }
  177.  
  178.     if (entHndl)
  179.         {
  180.         HUnlock(entHndl);
  181.         DisposHandle(entHndl);
  182.         entHndl = NIL;
  183.         }
  184.  
  185.     if (papHndl)
  186.         {
  187.         HUnlock(papHndl);
  188.         DisposHandle(papHndl);
  189.         papHndl = NIL;
  190.         papPtr = NIL;
  191.         }
  192.     return res;
  193. }
  194.  
  195. /************************************************************************
  196.  *                                                                        *
  197.  * The following routine was derived from the same routine in the PAP     *
  198.  * code in the CAP package.                                                *
  199.  *                                                                        *
  200.  ************************************************************************/
  201.  
  202. OSErr FindPrinter(EntityPtr printer, AddrBlock *addr);
  203.  
  204. OSErr papStatus(EntityPtr printer,
  205.                         PAPStatusRec *status, AddrBlock *prtAddr)
  206. {
  207.     ATPParamBlock    abr;
  208.     ATPParamBlock    *atpr;
  209.     BDSElement        bds[1];
  210.     PAPUserBytes    *pub;
  211.     OSErr            err;
  212.     AddrBlock        addr;
  213.  
  214.     EntityName        bf;
  215.     Byte            *prtname = (Byte*)&bf;
  216.  
  217.     BlockMove(printer, &bf, sizeof(EntityName));
  218.  
  219.     if (prtAddr->aNet == 0 || prtAddr->aNode == 0 || prtAddr->aSocket == 0)
  220.         {
  221.         if (err = FindPrinter(&bf, &addr) )
  222.             {
  223.             psprintf(status->statusStr, "%%Can't find <%p:%p@%p>",
  224.                     prtname,
  225.                     prtname+(prtname[0]+1),
  226.                     prtname+(prtname[0]+1)+(prtname[(prtname[0]+1)]+1));
  227.             return err;
  228.             }
  229.         *prtAddr = addr;
  230.         }
  231.     else
  232.         addr = *prtAddr;
  233.  
  234.     atpr = &abr;
  235.     atpr->ATPuserData = 0;
  236.         pub = (PAPUserBytes*) &atpr->ATPuserData;
  237.         pub->PAPtype = papSendStatus;
  238.  
  239.     atpr->ATPaddrBlock = addr;
  240.     atpr->ATPatpSocket = 0;
  241.     atpr->ATPreqLength = 0;
  242.     atpr->ATPreqPointer = NIL;
  243.     atpr->ATPbdsPointer = (Ptr)bds;
  244.         bds[0].buffPtr = (Ptr)status;
  245.         bds[0].buffSize = sizeof(PAPStatusRec);
  246.     atpr->ATPnumOfBuffs = 1;    /* actually numOfBuffs */
  247.     atpr->ATPatpFlags = 0;
  248.  
  249.     atpr->ATPtimeOutVal = 1;
  250.     atpr->ATPretryCount = 3;
  251.  
  252.     if ((err = PSendRequest(&abr, FALSE)) == reqFailed)
  253.         {
  254.         psprintf(status->statusStr, "%%<%p:%p@%p> not responding",
  255.                     prtname,
  256.                     prtname+(prtname[0]+1),
  257.                     prtname+(prtname[0]+1)+(prtname[(prtname[0]+1)]+1));
  258.         return reqFailed;
  259.         }
  260.  
  261.     if (atpr->ATPnumOfResps < 1 && bds[0].dataSize < min_PAPpkt_size)
  262.         return -1;
  263.     pub = (PAPUserBytes*)&bds[0].userBytes;
  264.     if (pub->PAPtype != papStatusReply)
  265.         return -1;
  266.  
  267.     return noErr;
  268. }
  269.  
  270. /************************************************************************
  271.  ************************************************************************/
  272. OSErr FindPrinter(EntityPtr printer, AddrBlock *addr)
  273. {
  274.     char            buffer[100];
  275.     MPPParamBlock    mpp;
  276.     MPPParamBlock    *nbp = &mpp;
  277.     OSErr            err;
  278.  
  279.     nbp->NBPinterval = 8;
  280.     nbp->NBPcount = 3;
  281.     nbp->NBPentityPtr = (Ptr)printer;
  282.     nbp->NBPretBuffPtr = buffer;
  283.     nbp->NBPretBuffSize = 100;
  284.     nbp->NBPmaxToGet = 1;
  285.  
  286.     if ( (err = PLookupName(&mpp, FALSE)) == noErr )
  287.         err = NBPExtract(buffer, nbp->NBPnumGotten, 1, printer, addr);
  288.  
  289.     if (err != noErr)
  290.         log_printf("NBP Name lookup error %d\n", err);
  291.  
  292.     return err;
  293. }
  294.