2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
8 #include <aros/debug.h>
9 /******************************************************************************
18 NOT/S,WARN/S,ERROR/S,FAIL/S,,EQ/K,GT/K,GE/K,VAL/S,EXISTS/K
26 Carry out all the commands in a block if a given conditional is true.
27 (A block is a run of command lines ended with an Else or EndIf
28 command.) For every If command there must be a corresponding EndIf.
29 If the condition is false, command execution will skip to the
30 corresponding Else of EndIf command.
34 NOT -- Negates the value of the condition
36 WARN -- True if the previous return code was greater
38 ERROR -- True if the previous return code was greater
40 FAIL -- True if the previous return code was greater
43 EQ, GE, GT -- True if the first value is equal, greater than
44 or equal respectively greater than the second.
46 VAL -- Indicate that the comparison should treat the
47 strings as numerical values.
49 EXISTS <string> -- True if the file or directory <string> exists.
56 ERROR and FAIL will only be appropriate if the fail level of the
57 script is set via FailAt (the standard fail level is 10 and if any
58 return code exceeds or equals this value, the script will be aborted).
63 echo "500 is greater than 200"
65 If EXISTS S:User-Startup
66 echo "User-Startup script found in S:"
67 Execute S:User-Startup
81 10.01.2000 SDuvan implemented
83 ******************************************************************************/
86 #include <aros/debug.h>
89 #include <dos/dosextens.h>
90 #include <dos/rdargs.h>
91 #include <dos/stdio.h>
92 #include <proto/dos.h>
93 #include <proto/utility.h>
94 #include <proto/exec.h>
95 #include "dos_commanderrors.h"
97 #include <aros/shcommands.h>
99 static BOOL
doeval(STRPTR arg1
, STRPTR arg2
, BYTE op
, IPTR numeric
, APTR DOSBase
, APTR UtilityBase
);
101 #undef NOT /* In case <intuition/intuition.h> got included */
104 AROS_SHA(BOOL
, ,NOT
,/S
, FALSE
),
105 AROS_SHA(BOOL
, ,WARN
,/S
,FALSE
),
106 AROS_SHA(BOOL
, ,ERROR
,/S
, FALSE
),
107 AROS_SHA(BOOL
, ,FAIL
,/S
,FALSE
),
108 AROS_SHA(STRPTR
, , , ,NULL
),
109 AROS_SHA(STRPTR
, ,EQ
,/K
,NULL
),
110 AROS_SHA(STRPTR
, ,GT
,/K
,NULL
),
111 AROS_SHA(STRPTR
, ,GE
,/K
,NULL
),
112 AROS_SHA(BOOL
, ,VAL
,/S
,FALSE
),
113 AROS_SHA(STRPTR
, ,EXISTS
,/K
,NULL
))
119 struct UtilityBase
*UtilityBase
;
120 struct CommandLineInterface
*cli
= Cli();
122 UtilityBase
= (struct UtilityBase
*)OpenLibrary("utility.library", 39);
127 if((cli
!= NULL
) && (cli
->cli_CurrentInput
!= cli
->cli_StandardInput
))
129 D(bug("Current input = %p, Standard input = %p\n",
130 cli
->cli_CurrentInput
, cli
->cli_StandardInput
));
134 if(cli
->cli_ReturnCode
>= RETURN_WARN
)
137 else if(SHArg(ERROR
))
139 if(cli
->cli_ReturnCode
>= RETURN_ERROR
)
144 if(cli
->cli_ReturnCode
>= RETURN_FAIL
)
149 result
= doeval(SHArg( ), SHArg(EQ
), 0, SHArg(VAL
), DOSBase
, UtilityBase
);
153 result
= doeval(SHArg( ), SHArg(GT
), 1, SHArg(VAL
), DOSBase
, UtilityBase
);
157 result
= doeval(SHArg( ), SHArg(GE
), 2, SHArg(VAL
), DOSBase
, UtilityBase
);
159 else if(SHArg(EXISTS
))
161 BPTR lock
= Lock(SHArg(EXISTS
), SHARED_LOCK
);
163 if((lock
!= BNULL
) || (IoErr() == ERROR_OBJECT_IN_USE
))
169 if(SHArg(NOT
)) /* NOT */
173 /* We have determined the result -- now we've got to act on it. */
179 int level
= 1; /* If block level */
180 BOOL found
= FALSE
; /* Have we found a matching Else or
183 SelectInput(cli
->cli_CurrentInput
);
189 status
= ReadItem(buffer
, sizeof(buffer
), NULL
);
191 if(status
== ITEM_ERROR
)
194 if(status
== ITEM_NOTHING
)
196 if (a
== ENDSTREAMCH
)
202 switch(FindArg("IF,ELSE,ENDIF", buffer
))
206 // printf("Found If\n");
223 /* Take care of long and empty lines */
227 } while (a
!= '\n' && a
!= ENDSTREAMCH
);
231 PrintFault(ERROR_NO_MATCHING_ELSEENDIF
, "If");
237 PrintFault(ERROR_SCRIPT_ONLY
, "If");
240 CloseLibrary((struct Library
*)UtilityBase
);
247 static BOOL
doeval(STRPTR arg1
, STRPTR arg2
, BYTE op
, IPTR numeric
, APTR DOSBase
, APTR UtilityBase
)
249 STRPTR s1
= (STRPTR
)arg1
;
250 STRPTR s2
= (STRPTR
)arg2
;
259 StrToLong(s1
, &val1
);
260 StrToLong(s2
, &val2
);
265 result
= (val1
== val2
);
269 result
= (val1
> val2
);
273 result
= (val1
>= val2
);
282 match
= Stricmp(s1
, s2
);
287 result
= (match
== 0);
291 result
= (match
> 0);
295 result
= (match
>= 0);
301 } /* if (s1 && s2) */