grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / c / Shell / convertArg.c
blobc4c72de9d8f7be215f7c159696561d1b4c403eda
1 /*
2 Copyright (C) 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <string.h>
7 #include "Shell.h"
9 /* subsitute one script argument and leaves the input after .ket */
10 LONG convertArg(ShellState *ss, Buffer *in, Buffer *out, BOOL *quoted)
12 STRPTR s = in->buf + in->cur;
13 STRPTR p = s;
14 STRPTR q = ++s;
15 LONG i;
16 BOOL scriptarg = FALSE;
18 if (s[0] == ss->dollar && s[1] == ss->dollar && s[2] == ss->ket)
20 TEXT buf[16];
21 LONG len = l2a(ss->cliNumber, buf);
22 bufferAppend(buf, len, out, SysBase);
23 in->cur += 4;
24 return 0;
27 if (!*quoted && *p == '<' && *q == '>') /* Run <>NIL: ... */
28 return convertRedir(ss, in, out);
30 for (; *q != ss->ket && *q != ss->dollar && *q != '\0'; ++q)
32 if (!*quoted)
33 switch (*q)
35 case '"':
36 case ' ':
37 if (*p == '<') /* input redirection */
38 return convertRedir(ss, in, out);
40 bufferAppend(s, q - s, out, SysBase);
41 return 0;
45 for (i = 0; i < ss->argcount; ++i)
47 struct SArg *a = ss->args + i;
48 STRPTR arg = NULL;
49 IPTR val = ss->arg[i];
50 UBYTE t = a->type;
51 LONG j, len = a->namelen;
53 if (q - s != len)
54 continue;
56 if (strncmp(s, a->name, len) != 0)
57 continue;
58 else
59 scriptarg = TRUE;
61 if (val)
63 if (t & SWITCH)
64 arg = val ? a->name : NULL;
65 else if (t & TOGGLE)
67 arg = val ? "1" : "0";
68 len = 1;
70 else if (t & MULTIPLE)
72 STRPTR *m = (STRPTR *) val;
74 for (j = 0; (arg = m[j]); ++j)
76 if (j > 0)
77 bufferAppend(" ", 1, out, SysBase);
79 len = cliLen(arg);
80 bufferAppend(arg, len, out, SysBase);
83 else
85 arg = (STRPTR) val;
86 len = a->len;
89 else if (s[len] == ss->dollar) /* default arg */
91 arg = s + len + 1;
92 len = q - arg;
94 else
96 arg = (STRPTR) a->def;
97 len = a->deflen;
100 if (arg)
101 bufferAppend(arg, len, out, SysBase);
102 break;
105 if(scriptarg)
107 while (*q != '\0' && *q++ != ss->ket);
108 in->cur = q - in->buf;
110 else
111 bufferCopy(in, out, 1, SysBase);
113 return 0;