revert between 56095 -> 55830 in arch
[AROS.git] / workbench / tools / PrintFiles.c
blob60e61ef6e0c95221a685bec2a9ac631cc7a395e5
1 /*
2 Copyright © 2012, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Print multiple files with optional formfeed
6 Lang: English
7 */
9 #include <proto/exec.h>
10 #include <proto/dos.h>
11 #include <proto/alib.h>
12 #include <proto/datatypes.h>
13 #include <proto/icon.h>
15 #include <workbench/startup.h>
16 #include <datatypes/datatypesclass.h>
17 #include <datatypes/pictureclass.h>
19 //#define DEBUG 1
20 #include <aros/debug.h>
22 #define USAGE "Usage: PrintFiles [-f] [-u N] file [file] [file...] (-f=formfeed -u=unit number)\n"
24 const char *vers = "$VER: PrintFiles 1.1 (12.03.2012)";
26 static struct MsgPort *mp;
27 static union printerIO *io;
29 char __stdiowin[]="CON:/30/400/100/PrintFiles/AUTO/CLOSE/WAIT";
32 static BOOL initdevice(ULONG unit)
34 D(bug("[PrintFiles] init unit %d\n", unit));
36 if ((mp = CreateMsgPort()))
38 if ((io = CreateIORequest(mp, sizeof(union printerIO))))
40 if (0 == OpenDevice("printer.device", unit, (struct IORequest *)io, 0))
42 return TRUE;
44 else
46 Printf("Can't open printer.device %ld\n", unit);
49 else
51 PutStr("Can't create IO request\n");
54 else
56 PutStr("Can't create message port\n");
58 return FALSE;
62 static void cleanupdevice(void)
64 if (io)
66 CloseDevice((struct IORequest *)io);
67 DeleteIORequest(io);
68 io = NULL;
70 if (mp)
72 DeleteMsgPort(mp);
73 mp = NULL;
78 static void printfile(STRPTR filename, BOOL formfeed)
80 if (filename != NULL)
82 Object *o;
83 struct dtPrint msg;
84 if ((o = NewDTObject(filename, PDTA_Remap, FALSE, TAG_END)))
86 struct TagItem tags[] = {
87 { DTA_Special, SPECIAL_ASPECT | SPECIAL_CENTER },
88 { TAG_END }
90 msg.MethodID = DTM_PRINT;
91 msg.dtp_GInfo = NULL;
92 msg.dtp_PIO = (union printerIO *)io;
93 msg.dtp_AttrList = tags;
94 D(bug("[PrintFiles] Trying to print %s\n", filename));
95 if (0 == DoDTMethodA(o, NULL, NULL, (Msg)&msg))
97 if (formfeed)
99 D(bug("[PrintFiles] Sending formfeed\n"));
100 io->ios.io_Length = 1;
101 io->ios.io_Data = "\x0C";
102 io->ios.io_Command = CMD_WRITE;
103 DoIO((struct IORequest *)io);
106 else
108 Printf("Failed to print %s\n", filename);
110 DisposeDTObject(o);
116 static void read_icon(struct WBArg *wbarg, BOOL *formfeed, ULONG *unit)
118 struct DiskObject *dobj;
119 STRPTR *toolarray;
120 STRPTR result;
122 *formfeed = FALSE;
123 *unit = 0;
125 dobj = GetDiskObject(wbarg->wa_Name);
126 if (dobj)
128 toolarray = dobj->do_ToolTypes;
130 if (FindToolType(toolarray, "FORMFEED"))
132 *formfeed = TRUE;
134 result = FindToolType(toolarray, "UNIT");
135 if (result)
137 StrToLong(result, unit);
139 FreeDiskObject(dobj);
144 int main(int argc, char **argv)
146 ULONG unit = 0;
147 BOOL formfeed = FALSE;
148 ULONG i;
150 if (argc == 0)
152 // started from Workbench
153 struct WBStartup *wbmsg = (struct WBStartup *)argv;
154 struct WBArg *wbarg = wbmsg->sm_ArgList;
155 BPTR olddir = (BPTR)-1;
157 D(bug("[PrintFiles] numargs %d wa_lock %lx wa_name %s\n", wbmsg->sm_NumArgs, wbarg[0].wa_Lock, wbarg[0].wa_Name));
158 if (wbmsg->sm_NumArgs > 1 && wbarg[0].wa_Lock && *wbarg[0].wa_Name)
160 // handle program's icon
161 olddir = CurrentDir(wbarg->wa_Lock);
162 read_icon(wbarg, &formfeed, &unit);
163 if (olddir != (BPTR)-1)
164 CurrentDir(olddir);
165 if (initdevice(unit))
167 // handle project icons
168 for (i = 1; i < wbmsg->sm_NumArgs; i++)
170 D(bug("[PrintFiles] i %d wa_lock %lx wa_name %s\n", i, wbarg[i].wa_Lock, wbarg[i].wa_Name));
171 olddir = (BPTR)-1;
172 if ((wbarg[i].wa_Lock) && (*wbarg[i].wa_Name) )
174 olddir = CurrentDir(wbarg[i].wa_Lock);
176 printfile(wbarg[i].wa_Name, formfeed);
178 if (olddir != (BPTR)-1)
179 CurrentDir(olddir);
185 else
187 // started from CLI
189 if (argc == 1 || argv[1][0] == '?')
191 PutStr(USAGE);
192 return RETURN_ERROR;
195 // read options
196 i = 1;
197 while (i < argc && argv[i][0] == '-')
199 if (argv[i][1] == 'f')
201 formfeed = TRUE;
203 else if (argv[i][1] == 'u' && (i + 1 < argc))
205 i++;
206 StrToLong(argv[i], &unit);
208 else
210 PutStr(USAGE);
211 return RETURN_ERROR;
213 i++;
216 // print files
217 if (initdevice(unit))
219 while (i < argc)
221 printfile(argv[i], formfeed);
222 i++;
227 cleanupdevice();
229 return 0;