home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / NDK / NDK_3.5 / Examples / Printer / PrintDT / printdt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-30  |  4.0 KB  |  199 lines

  1. /*
  2.  * COPYRIGHT:
  3.  *
  4.  *   Unless otherwise noted, all files are Copyright (c) 1999 Amiga, Inc.
  5.  *   All rights reserved.
  6.  *
  7.  * DISCLAIMER:
  8.  *
  9.  *   This software is provided "as is". No representations or warranties
  10.  *   are made with respect to the accuracy, reliability, performance,
  11.  *   currentness, or operation of this software, and all use is at your
  12.  *   own risk. Neither Amiga nor the authors assume any responsibility
  13.  *   or liability whatsoever with respect to your use of this software.
  14.  */
  15.  
  16.  
  17. #include <devices/printer.h>
  18. #include <datatypes/datatypesclass.h>
  19.  
  20. #include <clib/exec_protos.h>
  21. #include <clib/dos_protos.h>
  22. #include <clib/datatypes_protos.h>
  23. #include <clib/alib_stdio_protos.h>
  24. #include <clib/alib_protos.h>
  25. #include <stdlib.h>
  26.  
  27. extern "C" void kprintf(const char *, ...);
  28. extern "C" void HookEntry();
  29.  
  30. // *************************************************************
  31.  
  32. BPTR stdout;
  33.  
  34. static int consolehook(struct Hook *, struct IORequest *, struct PrtErrMsg *msg)
  35. {
  36.     kprintf("%s: %s\n",msg->pe_ES->es_Title,msg->pe_ES->es_TextFormat);
  37.     puts(msg->pe_ES->es_Title);
  38.     puts(": ");
  39.     printf(msg->pe_ES->es_TextFormat,((ULONG *) msg->pe_ArgList)[0]);
  40.     return 0;
  41. }
  42.  
  43. static struct Hook ConsoleHook =
  44. {
  45.     { NULL, NULL },
  46.     (HOOKFUNC) HookEntry,
  47.     (HOOKFUNC) consolehook,
  48.     NULL
  49. };
  50.  
  51. // *************************************************************
  52.  
  53. union IOPrinterReq
  54. {
  55.     struct IORequest ior;
  56.     struct IOStdReq sio;
  57.     struct IOPrtErrReq per;
  58.     struct IOPrtPrefsReq prq;
  59.     struct IODRPReq drp;
  60. };
  61.  
  62. // *************************************************************
  63.  
  64. static BOOL Opened;
  65. static UBYTE PrefsBuf[8192];
  66. static ULONG PrefsLen;
  67.  
  68. static void ClosePrinter(union IOPrinterReq *ior)
  69. {
  70.     if (Opened)
  71.     {
  72.         ior->per.io_Command = PRD_SETERRHOOK;
  73.         ior->per.io_Hook = PDHOOK_NONE;
  74.         DoIO(&ior->ior);
  75.         CloseDevice(&ior->ior);
  76.         Opened = FALSE;
  77.     }
  78. }
  79.  
  80. static int OpenPrinter(union IOPrinterReq *ior, int unit, BOOL wb)
  81. {
  82.     int err;
  83.     if (Opened)
  84.         return 0;
  85.     if (err = OpenDevice("printer.device",unit,&ior->ior,0))
  86.         return err;
  87.     ior->per.io_Command = PRD_SETERRHOOK;
  88.     ior->per.io_Hook = wb ? PDHOOK_STD : &ConsoleHook;
  89.     if (err = DoIO(&ior->ior))
  90.     {
  91.         CloseDevice(&ior->ior);
  92.         return err;
  93.     }
  94.     Opened = TRUE;
  95.     if (PrefsLen)
  96.     {
  97.         ior->sio.io_Command = PRD_WRITEPREFS;
  98.         ior->sio.io_Length = PrefsLen;
  99.         ior->sio.io_Data = PrefsBuf;
  100.         if (err = DoIO(&ior->ior))
  101.         {
  102.             ClosePrinter(ior);
  103.         }
  104.     }
  105.     return err;
  106. }
  107.  
  108. // *************************************************************
  109.  
  110. int printfile(STRPTR file, union IOPrinterReq *ior, int unit, BOOL wb)
  111. {
  112.     Object *o;
  113.     if (OpenPrinter(ior,unit,wb))
  114.         return 0;
  115.     o = NewDTObject(file,
  116.         DTA_SourceType,DTST_FILE,
  117.         TAG_END);
  118.     if (!o)
  119.     {
  120.         ClosePrinter(ior);
  121.         return IoErr();
  122.     }
  123.     DoMethod(o,
  124.         DTM_PRINT,
  125.         NULL,
  126.         ior,
  127.         NULL);
  128.     DisposeDTObject(o);
  129.     ClosePrinter(ior);
  130.     return 0;
  131. }
  132.  
  133. void main(void)
  134. {
  135.     LONG err = 0;
  136.     LONG rc = 0;
  137.     LONG args[5] = { };
  138.     struct RDArgs *rdargs;
  139.     stdout = Output();
  140.     rdargs = ReadArgs("FILE/A/M,OPTIONS/S",args,NULL);
  141.     if (rdargs)
  142.     {
  143.         struct MsgPort *port = CreateMsgPort();
  144.         if (port)
  145.         {
  146.             union IOPrinterReq *ior = (union IOPrinterReq *) CreateIORequest(port,sizeof(union IOPrinterReq));
  147.             if (ior)
  148.             {
  149.                 if (!OpenPrinter(ior,0,FALSE))
  150.                 {
  151.                     STRPTR *files = (STRPTR *) args[0];
  152.                     if (args[1])
  153.                     {
  154.                         ior->prq.io_Command = PRD_EDITPREFS;
  155.                         ior->prq.io_TagList = NULL;
  156.                         if (err = DoIO(&ior->ior))
  157.                         {
  158.                             if (err != PDERR_CANCEL)
  159.                                 rc = 10;
  160.                             err = 0;
  161.                             ClosePrinter(ior);
  162.                             goto abortprint;
  163.                         }
  164.                     }
  165.                     ior->sio.io_Command = PRD_READPREFS;
  166.                     ior->sio.io_Length = sizeof(PrefsBuf);
  167.                     ior->sio.io_Data = PrefsBuf;
  168.                     ior->sio.io_Offset = 0;
  169.                     if (DoIO(&ior->ior))
  170.                     {
  171.                         ClosePrinter(ior);
  172.                         goto abortprint;
  173.                     }
  174.                     PrefsLen = ior->sio.io_Actual;
  175.                     ClosePrinter(ior);
  176.                     while (*files)
  177.                     {
  178.                         if (err = printfile(*files,ior,0,FALSE))
  179.                             break;
  180.                         files++;
  181.                     }
  182. abortprint:
  183.                     ;
  184.                 }
  185.                 DeleteIORequest(&ior->ior);
  186.             }
  187.             DeleteMsgPort(port);
  188.         }
  189.     }
  190.     else
  191.     {
  192.         err = IoErr();
  193.         rc = 20;
  194.     }
  195.     if (err)
  196.         PrintFault(err,NULL);
  197.     exit(rc);
  198. }
  199.