grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / devs / printtotool / printtotool.c
blob7433e5629ca3f04e9d285e5eb6c6aea13a8575ff
1 /*
2 * Copyright (C) 2012, The AROS Development Team. All rights reserved.
3 * Author: Jason S. McMullan <jason.mcmullan@gmail.com>
5 * Licensed under the AROS PUBLIC LICENSE (APL) Version 1.1
6 */
8 #include <aros/debug.h>
10 #include <exec/errors.h>
12 #include <proto/exec.h>
13 #include <proto/dos.h>
14 #include <proto/asl.h>
16 #include LC_LIBDEFS_FILE
18 #include "printtotool_intern.h"
20 static BPTR OpenTool(struct PrintToToolBase *PrintToToolBase, CONST_STRPTR tool)
22 BPTR pipe_r, pipe_w;
24 if ((pipe_w = Open("PIPE:*", MODE_NEWFILE))) {
25 TEXT buff[64];
26 if (NameFromFH(pipe_w, buff, sizeof buff)) {
27 if ((pipe_r = Open(buff, MODE_OLDFILE))) {
28 BPTR con = Open("CON://///AUTO/CLOSE/WAIT", MODE_NEWFILE);
29 if (SystemTags(tool, SYS_Input, pipe_r,
30 SYS_Output, con,
31 SYS_Asynch, TRUE,
32 TAG_END) >= 0) {
33 return pipe_w;
35 if (con != BNULL)
36 Close(con);
37 Close(pipe_r);
38 } else {
39 D(bug("%s: Can't open read side of %s\n", __func__, buff));
41 } else {
42 D(bug("%s: Can't get name of autopipe\n", __func__));
44 Close(pipe_w);
45 } else {
46 D(bug("%s: Can't open autopipe\n", __func__));
49 return BNULL;
52 BOOL pu_OpenFile(struct PrintToToolBase *PrintToToolBase,
53 struct PrintToToolUnit *pu)
55 struct FileRequester *fr;
56 BOOL ok;
58 ObtainSemaphore(&pu->pu_Lock);
59 if (!pu->pu_FileReq) {
60 fr = AllocAslRequestTags(ASL_FileRequest,
61 ASLFR_TitleText,"Tool to print to...",
62 ASLFR_InitialDrawer,"C:",
63 ASLFR_RejectIcons, TRUE,
64 TAG_END);
65 if (fr == NULL) {
66 D(bug("%s: Can't create requestor\n"));
67 ReleaseSemaphore(&pu->pu_Lock);
68 return IOERR_OPENFAIL;
70 pu->pu_FileReq = fr;
71 } else {
72 fr = pu->pu_FileReq;
75 D(bug("fr = %p\n", fr));
76 ok = AslRequestTags(fr, TAG_END);
77 D(bug("ok = %d (%s %s)\n", ok, fr-fr_Drawer, fr->fr_File));
79 if (ok) {
80 BPTR here;
82 if (fr->fr_Drawer) {
83 BPTR lock = Lock(fr->fr_Drawer, SHARED_LOCK);
84 if (!lock)
85 return IOERR_OPENFAIL;
86 here = CurrentDir(lock);
87 } else {
88 here = CurrentDir(BNULL);
89 CurrentDir(here);
92 if (fr->fr_File) {
93 pu->pu_File = OpenTool(PrintToToolBase, fr->fr_File);
94 } else {
95 pu->pu_File = BNULL;
97 D(bug("opened = %d\n", opened));
99 CurrentDir(here);
102 if (pu->pu_File == BNULL) {
103 ReleaseSemaphore(&pu->pu_Lock);
104 return IOERR_OPENFAIL;
107 return 0;
110 AROS_LH1(void, BeginIO,
111 AROS_LHA(struct IOStdReq *, io, A1),
112 struct PrintToToolBase *, PrintToToolBase, 5, PrintToTool)
114 AROS_LIBFUNC_INIT
116 struct PrintToToolUnit *pu = (struct PrintToToolUnit *)io->io_Unit;
117 LONG err;
119 D(bug("BeginIO: io_Command = %d\n", io->io_Command));
120 D(bug("pu_File=%p\n", BADDR(pu->pu_File)));
121 if (pu->pu_File == BNULL) {
122 err = pu_OpenFile(PrintToToolBase, pu);
123 if (err != 0) {
124 D(bug("err = %d\n", err));
125 io->io_Error = err;
126 goto end;
130 switch (io->io_Command) {
131 case CMD_FLUSH:
132 case CMD_UPDATE:
133 io->io_Error = 0;
134 break;
135 case CMD_WRITE:
136 err = Write(pu->pu_File, io->io_Data, io->io_Length);
137 if (err < 0) {
138 io->io_Error = IoErr();
139 io->io_Actual = 0;
140 } else {
141 io->io_Error = 0;
142 io->io_Actual = err;
144 break;
145 default:
146 io->io_Error = IOERR_NOCMD;
147 break;
150 end:
151 if (!(io->io_Flags & IOF_QUICK))
152 ReplyMsg(&io->io_Message);
153 return;
155 AROS_LIBFUNC_EXIT
158 AROS_LH1(LONG, AbortIO,
159 AROS_LHA(struct IORequest *, io, A1),
160 struct PrintToToolBase *, PrintToToolBase, 6, PrintToTool)
162 AROS_LIBFUNC_INIT
163 return IOERR_NOCMD;
164 AROS_LIBFUNC_EXIT
167 static int GM_UNIQUENAME(Init)(struct PrintToToolBase *PrintToToolBase)
169 int i;
171 for (i = 0; i < PRINTTOTOOL_UNITS; i++) {
172 InitSemaphore(&PrintToToolBase->pt_Unit[i].pu_Lock);
175 if ((PrintToToolBase->pt_DOSBase = OpenLibrary("dos.library", 35))) {
176 if ((PrintToToolBase->pt_AslBase = OpenLibrary("asl.library", 36))) {
177 D(bug("initted\n"));
178 return TRUE;
180 CloseLibrary(PrintToToolBase->pt_DOSBase);
183 return FALSE;
186 static int GM_UNIQUENAME(Expunge)(struct PrintToToolBase *PrintToToolBase)
188 int i;
190 for (i = 0; i < PRINTTOTOOL_UNITS; i++) {
191 FreeFileRequest(PrintToToolBase->pt_Unit[i].pu_FileReq);
194 CloseLibrary(PrintToToolBase->pt_AslBase);
195 CloseLibrary(PrintToToolBase->pt_DOSBase);
196 D(bug("expunged\n"));
198 return TRUE;
201 static int GM_UNIQUENAME(Open)(struct PrintToToolBase *PrintToToolBase, struct IOStdReq* io, ULONG unitnum, ULONG flags)
203 D(bug("open unit %d\n", unitnum));
204 if (unitnum < 0 || unitnum >= PRINTTOTOOL_UNITS)
205 return FALSE;
207 io->io_Unit = (struct Unit *)&PrintToToolBase->pt_Unit[unitnum];
208 D(bug("io unit %p\n", io->io_Unit));
209 return TRUE;
212 static int GM_UNIQUENAME(Close)(struct PrintToToolBase *PrintToToolBase, struct IOStdReq *io)
214 struct PrintToToolUnit *pu = (struct PrintToToolUnit *)io->io_Unit;
216 D(bug("close: %p , %p\n", pu, BADDR(pu->pu_File)));
217 io->io_Unit = NULL;
218 if (pu->pu_File == BNULL)
219 return TRUE;
221 Close(pu->pu_File);
222 pu->pu_File = BNULL;
223 ReleaseSemaphore(&pu->pu_Lock);
225 return TRUE;
228 ADD2OPENDEV(GM_UNIQUENAME(Open), 0)
229 ADD2CLOSEDEV(GM_UNIQUENAME(Close), 0)
231 ADD2INITLIB(GM_UNIQUENAME(Init), 0)
232 ADD2EXPUNGELIB(GM_UNIQUENAME(Expunge), 0)