use m4_normalize on the output strings to remove trailing spaces/tabs and consolidate...
[AROS.git] / workbench / c / shellcommands / Ask.c
blob7234929acb3b220ffc93ff97bdc379af3b7e5c2e
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Ask CLI command
6 Lang: English
7 */
9 /******************************************************************************
11 NAME
13 Ask <prompt>
15 SYNOPSIS
17 PROMPT/A
19 LOCATION
23 FUNCTION
25 Prompts the user for an input. Possible inputs are y for yes
26 and n or Return for no. Selecting y sets the return code to 5.
28 INPUTS
30 PROMPT -- the string is displayed in the window
32 RESULT
34 NOTES
36 EXAMPLE
38 BUGS
40 SEE ALSO
42 RequestChoice
44 INTERNALS
46 HISTORY
48 ******************************************************************************/
50 #include <stdio.h>
51 #include <string.h>
52 #include <proto/exec.h>
53 #include <proto/dos.h>
54 #include <dos/dos.h>
55 #include <proto/utility.h>
57 #include <aros/shcommands.h>
59 static int stripwhites(char * buffer);
60 static char * skipwhites(char * buffer);
62 AROS_SH1(Ask, 41.3,
63 AROS_SHA(STRPTR, ,PROMPT,/A,NULL))
65 AROS_SHCOMMAND_INIT
67 char buffer[100];
68 int ready = 0;
69 int error = RETURN_OK;
71 struct UtilityBase *UtilityBase =
72 (struct UtilityBase *)OpenLibrary("utility.library", 37);
75 if (!UtilityBase)
77 return RETURN_FAIL;
80 while (ready == 0)
82 Printf("%s ", SHArg(PROMPT));
83 Flush(Output());
85 if (FGets(Input(), buffer, 100) == (STRPTR)buffer)
87 char *tmpbuf;
88 int tmplen;
90 tmpbuf = skipwhites(buffer);
91 tmplen = stripwhites(tmpbuf);
93 if (tmplen == 0)
95 ready = 1;
97 else if (tmplen == 1)
99 if (Strnicmp(tmpbuf, "y", 1) == 0)
101 error = RETURN_WARN;
102 ready = 1;
104 else if (Strnicmp(tmpbuf, "n", 1) == 0)
106 ready = 1;
109 else if (tmplen == 2)
111 if (Strnicmp(tmpbuf, "no", 2) == 0)
113 ready = 1;
116 else if (tmplen == 3)
118 if (Strnicmp(tmpbuf, "yes", 3) == 0)
120 error = RETURN_WARN;
121 ready = 1;
125 else
127 ready = 1;
131 CloseLibrary((struct Library *)UtilityBase);
133 return error;
135 AROS_SHCOMMAND_EXIT
138 static int stripwhites(char * buffer)
140 int len;
141 len = strlen(buffer);
143 while ((len != 0) &&
144 (buffer[len - 1] == ' ' ||
145 buffer[len - 1] == '\t' ||
146 buffer[len - 1] == '\n'))
148 len--;
151 return len;
155 static char *skipwhites(char *buffer)
157 while (buffer[0] == ' ' || buffer[0] == '\t')
159 buffer++;
162 return buffer;