revert between 56095 -> 55830 in arch
[AROS.git] / workbench / c / Print.c
blobac5125dec29ff01700e116e1e97c304d9420500f
1 /*
2 Copyright © 2013-2014, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: English
7 */
9 /******************************************************************************
11 NAME
13 Print
15 SYNOPSIS
17 FILE/M
18 PRTUNIT/N
20 LOCATION
24 FUNCTION
26 Print a file, using datatypes.library
28 INPUTS
30 FILE f -- Filename to print
31 PRTUNIT n -- Printer unit
33 RESULT
35 Files will be printed to the specified printer unit
37 NOTES
39 EXAMPLE
41 BUGS
43 SEE ALSO
45 INTERNALS
47 HISTORY
49 ******************************************************************************/
51 #define DEBUG 0
52 #include <aros/debug.h>
54 #include <exec/memory.h>
55 #include <proto/exec.h>
56 #include <proto/dos.h>
57 #include <proto/alib.h>
58 #include <proto/datatypes.h>
60 #include <datatypes/datatypesclass.h>
61 #include <datatypes/pictureclass.h>
63 #include <aros/shcommands.h>
65 static inline BOOL isPrintable(struct Library *DataTypesBase, Object *obj)
67 ULONG *dtm;
69 for (dtm = GetDTMethods(obj); (SIPTR)dtm != -1; dtm++) {
70 if (*dtm == DTM_PRINT)
71 return TRUE;
74 return FALSE;
77 ULONG doPrinting(struct ExecBase *SysBase, struct Library *DataTypesBase, Object *dto, int unit)
79 struct dtPrint msg;
80 struct MsgPort *mp;
81 struct IORequest *pio;
82 ULONG retval = PDERR_CANCEL;
83 struct Process *pr = (struct Process *)FindTask(NULL);
85 if ((mp = CreateMsgPort())) {
86 if ((pio = CreateIORequest(mp, sizeof(union printerIO)))) {
87 if (0 == OpenDevice("printer.device", unit, pio, 0)) {
88 msg.MethodID = DTM_PRINT;
89 msg.dtp_GInfo = NULL;
90 msg.dtp_PIO = (union printerIO *)pio;
91 msg.dtp_AttrList = NULL;
93 retval = DoDTMethodA(dto, pr->pr_WindowPtr, NULL, (Msg)&msg);
95 CloseDevice(pio);
96 } else {
97 retval = -13;
99 DeleteIORequest(pio);
100 } else {
101 retval = -14;
103 DeleteMsgPort(mp);
104 } else {
105 retval = -15;
108 return retval;
112 AROS_SH2H(Print , 44.1, "Print a file using DataTypes\n",
113 AROS_SHAH(LONG * , ,PRTUNIT,/K/N, 0 , "Printer unit\n"),
114 AROS_SHAH(STRPTR *, ,FILES ,/M, NULL , "File(s) to print\n"))
116 AROS_SHCOMMAND_INIT
118 LONG result = RETURN_FAIL;
119 struct Library *DataTypesBase;
120 STRPTR *files = SHArg(FILES);
121 LONG unit = SHArg(PRTUNIT) ? *SHArg(PRTUNIT) : 0;
122 LONG err;
124 if (files != NULL) {
125 if ((DataTypesBase = OpenLibrary("datatypes.library", 0))) {
126 Object *o;
127 result = RETURN_OK;
128 for (; *files; files++) {
129 STRPTR file = *files;
130 if ((o = NewDTObject(file, PDTA_Remap, FALSE, TAG_END))) {
131 if (isPrintable(DataTypesBase, o)) {
132 err = doPrinting(SysBase, DataTypesBase, o, unit);
133 if (err) {
134 Printf("Can't print \"%s\": Printer Error %d\n", file, err);
135 result = RETURN_FAIL;
137 } else {
138 Printf("\"%s\" is not a DataType printable file\n", file);
139 result = RETURN_FAIL;
141 DisposeDTObject(o);
142 } else {
143 Printf("Can't open %s as a DataType object\n", file);
146 CloseLibrary(DataTypesBase);
147 } else {
148 Printf("Can't open datatypes.library\n");
150 } else {
151 /* No file supplied - quiet success */
152 result = RETURN_OK;
155 return result;
157 AROS_SHCOMMAND_EXIT