grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / c / Shell / convertRedir.c
blob5f0d15d843f526162fc88b54362b8c705a080140
1 /*
2 Copyright (C) 1995-2016, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <proto/dos.h>
8 #include "buffer.h"
9 #include "Shell.h"
11 LONG convertRedir(ShellState *ss, Buffer *in, Buffer *out)
13 STRPTR s = in->buf + in->cur;
14 BOOL newIn = FALSE, newOut = FALSE;
15 BOOL append = FALSE;
16 TEXT file[FILE_MAX];
18 if (*s == '<')
20 if (ss->newIn) /* multiple < not allowed */
21 return ERROR_TOO_MANY_LEVELS;
23 newIn = TRUE; /* new input */
24 ++s;
27 if (*s == '>')
29 if (ss->newOut) /* multiple > or >> not allowed */
30 return ERROR_TOO_MANY_LEVELS;
32 if (*++s == '>')
34 append = TRUE;
35 ++s;
38 newOut = TRUE; /* new output */
41 in->cur = s - in->buf;
43 switch (bufferReadItem(file, FILE_MAX, in, DOSBase))
45 case ITEM_QUOTED:
46 case ITEM_UNQUOTED:
47 break;
48 default:
49 return ERROR_LINE_TOO_LONG;
52 if (newIn)
54 if (!(ss->newIn = Open(file, MODE_OLDFILE)))
55 return IoErr();
57 ss->oldIn = SelectInput(ss->newIn);
60 if (newOut)
62 LONG mode = append ? MODE_READWRITE : MODE_NEWFILE;
64 if (!(ss->newOut = Open(file, mode)))
65 return IoErr();
67 if (append && Seek(ss->newOut, 0, OFFSET_END) == -1)
68 return IoErr();
70 ss->oldOut = SelectOutput(ss->newOut);
73 return 0;