Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / workbench / c / shellcommands / Else.c
blob1462e06fbaaba0c2f14813f792e97159e8409ce4
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: English
7 */
9 /*****************************************************************************
11 NAME
13 Else
15 SYNOPSIS
17 LOCATION
19 Sys:C
21 FUNCTION
23 Separate the 'true' and 'false' blocks of an If statement. The block
24 following an Else command is executed if the condition in the previous
25 If statement was false.
27 INPUTS
29 RESULT
31 NOTES
33 EXAMPLE
35 If EXISTS Sys:Devs
36 Copy random.device Sys:Devs/
37 Else
38 Echo "Cannot find Sys:Devs"
39 EndIf
41 BUGS
43 SEE ALSO
45 If, EndIf
47 INTERNALS
49 HISTORY
51 12.01.2000 SDuvan implemented
53 ******************************************************************************/
55 #include <dos/dos.h>
56 #include <dos/dosextens.h>
57 #include <dos/rdargs.h>
58 #include <dos/stdio.h>
59 #include <proto/dos.h>
60 #include <dos_commanderrors.h>
62 #include <aros/shcommands.h>
64 AROS_SH0(Else,41.1)
66 AROS_SHCOMMAND_INIT
68 struct CommandLineInterface *cli = Cli();
71 if ((cli != NULL) && (cli->cli_CurrentInput != cli->cli_StandardInput))
73 BOOL found = FALSE;
74 int level = 1;
75 char buffer[256], a = 0;
77 SelectInput(cli->cli_CurrentInput);
79 while (!found)
81 LONG status;
82 int temp;
84 status = ReadItem(buffer, sizeof(buffer), NULL);
86 if (status == ITEM_ERROR)
87 break;
89 if (status == ITEM_NOTHING)
91 if (a == ENDSTREAMCH)
92 break;
93 else
94 continue;
97 switch ((temp = FindArg("IF,ENDIF", buffer)))
99 case 0:
100 level++;
101 break;
103 case 1:
104 level--;
106 if (level == 0)
108 found = TRUE;
111 break;
114 /* Take care of long lines */
117 a = FGetC(Input());
118 } while(a != '\n' && a != ENDSTREAMCH);
122 if (!found)
124 PrintFault(ERROR_NO_MATCHING_ELSEENDIF, "Else");
126 return RETURN_FAIL;
129 else
131 PrintFault(ERROR_SCRIPT_ONLY, "Else");
133 return RETURN_ERROR;
136 return RETURN_OK;
138 AROS_SHCOMMAND_EXIT