Hint added.
[AROS.git] / workbench / c / Shell / readLine.c
blobe116ce47c1a1923a02d08a567fa8e47e29feb1c4
1 /*
2 Copyright (C) 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <dos/stdio.h>
8 #include <proto/dos.h>
10 #include "Shell.h"
12 #include <aros/debug.h>
14 #define PIPE_NAME "PIPE "
16 static BOOL checkPipe(STRPTR pchar, STRPTR mchar, STRPTR in, LONG inlen)
18 LONG c, n;
19 BOOL quoted = FALSE;
20 int mcharn = strlen(mchar), pcharn = strlen(pchar);
22 for (n = 0; n < inlen; n++)
24 c = in[n];
25 if (c == '"')
27 quoted = !quoted;
30 if (quoted)
31 continue;
33 if (mcharn > 0 && c == mchar[0] && memcmp(&in[n], mchar, mcharn) == 0)
34 return TRUE;
36 if (pcharn > 0 && c == pchar[0] && memcmp(&in[n], pchar, pcharn) == 0)
37 return TRUE;
39 return FALSE;
42 LONG readLine(ShellState *ss, struct CommandLineInterface *cli, Buffer *out, WORD *moreLeft)
44 BPTR fh = cli->cli_CurrentInput;
45 STRPTR buf = out->buf; /* pre-allocated by caller */
46 BOOL comment = FALSE;
47 BOOL quoted = FALSE;
48 LONG c, i, j, len;
49 TEXT pchar[3], mchar[3];
51 len = GetVar("_pchar", pchar, sizeof pchar, GVF_LOCAL_ONLY | LV_VAR);
52 if (len <= 0)
53 pchar[0] = 0;
54 pchar[2] = 0;
56 len = GetVar("_mchar", mchar, sizeof mchar, GVF_LOCAL_ONLY | LV_VAR);
57 if (len <= 0)
58 mchar[0] = 0;
59 mchar[2] = 0;
60 ss->pchar0 = pchar[0];
61 ss->mchar0 = mchar[0];
63 for (i = 0, j = 0; i < LINE_MAX; ++i)
65 c = FGetC(fh);
67 if (c == ENDSTREAMCH)
68 break;
70 if (c == '"')
71 quoted = !quoted;
73 if (!quoted && c == ';' && c != mchar[0] ) /* comment line */
75 comment = TRUE;
76 continue;
78 else if (c == '\n') /* end of line */
80 comment = FALSE;
82 /* '+' continuation */
83 if (j > 0 && buf[j-1]=='+') {
84 buf[j-1]=c;
85 continue;
88 else if (comment)
89 continue;
91 buf[j++] = c;
93 if (c == '\n') /* end of line */
94 break;
97 if (i >= LINE_MAX) {
98 D(bug("[Shell] ERROR_LINE_TOO_LONG\n"));
99 return ERROR_LINE_TOO_LONG;
102 buf[j] = '\0';
103 bufferAppend(buf, j, out, SysBase);
105 if (checkPipe(pchar, mchar, buf, j))
106 bufferInsert(PIPE_NAME, strlen(PIPE_NAME), out, SysBase);
108 *moreLeft = (c != ENDSTREAMCH);
110 D(bug("[Shell] readLine %d%s: %s", j, *moreLeft ? "" : "'", buf));
111 return 0;