exec.library: Allocate/Deallocate functions must remain generic and not handler MemHe...
[AROS.git] / workbench / c / RequestChoice.c
blob6d1eeb67d545ed6303439474b8cd4d57b14fa473
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: RequestChoice CLI command
6 Lang: English
7 */
9 /*****************************************************************************
11 NAME
13 RequestChoice
15 SYNOPSIS
17 TITLE/A,BODY/A,GADGETS/A/M,PUBSCREEN/K
19 LOCATION
23 FUNCTION
25 Allows AmigaDOS scripts to have access to the EasyRequest() function
26 for input.
28 INPUTS
30 TITLE - The text to display in the title bar of the requester.
32 BODY - The text to display in the body of the requester.
34 GADGETS - The text for each of the buttons.
36 PUBSCREEN - The name of the public screen to open the requester
37 upon.
39 RESULT
41 Standard DOS return codes.
43 NOTES
45 To place a newline into the body of the requester use *n or *N.
47 To place a quotation mark in the body of the requester use *".
49 The CLI template gives the GADGETS option as ALWAYS given; this
50 is different from the original program. This way, we do not have
51 to check to see if the gadgets have been given.
53 EXAMPLE
55 RequestChoice "This is a title" "This is*Na body" Okay|Cancel
57 This is self-explanitory, except for the "*N". This is the
58 equivalent of using a '\n' in C to get a newline in the body
59 of the requester. This requester will open on the Workbench
60 screen.
62 RequestChoice Title="This is a title" Body="This is*Na body"
63 Gadgets=Okay|Cancel PubScreen=DOPUS.1
65 This will do exactly the same as before except that it will
66 open on the Directory Opus public screen.
68 BUGS
70 SEE ALSO
72 intuition.library/EasyRequestArgs()
74 INTERNALS
76 HISTORY
78 ******************************************************************************/
80 #include <proto/dos.h>
81 #include <proto/exec.h>
82 #include <proto/intuition.h>
84 #include <dos/dos.h>
85 #include <dos/rdargs.h>
86 #include <exec/libraries.h>
87 #include <exec/memory.h>
88 #include <exec/types.h>
89 #include <intuition/intuition.h>
90 #include <intuition/screens.h>
92 #include <string.h>
94 #define ARG_TEMPLATE "TITLE/A,BODY/A,GADGETS/A/M,PUBSCREEN/K"
95 #define ARG_TITLE 0
96 #define ARG_BODY 1
97 #define ARG_GADGETS 2
98 #define ARG_PUBSCREEN 3
99 #define TOTAL_ARGS 4
101 const TEXT version[] = "$VER: RequestChoice 41.2 (01.05.2011)\n";
103 static char ERROR_HEADER[] = "RequestChoice";
105 static int Do_RequestChoice(STRPTR, STRPTR, STRPTR *, STRPTR);
106 static STRPTR FilterBodyText(STRPTR Body);
107 static STRPTR ComposeGadgetText(STRPTR * Gadgets);
109 int __nocommandline;
111 int main(void)
113 struct RDArgs * rda;
114 IPTR * args[TOTAL_ARGS] = { NULL, NULL, NULL, NULL };
115 int Return_Value;
117 Return_Value = RETURN_OK;
119 rda = ReadArgs(ARG_TEMPLATE, (IPTR *)args, NULL);
120 if (rda)
122 Return_Value = Do_RequestChoice((STRPTR)args[ARG_TITLE],
123 (STRPTR)args[ARG_BODY],
124 (STRPTR *)args[ARG_GADGETS],
125 (STRPTR)args[ARG_PUBSCREEN]);
126 FreeArgs(rda);
128 else
130 PrintFault(IoErr(), ERROR_HEADER);
131 Return_Value = RETURN_FAIL;
134 return (Return_Value);
135 } /* main */
138 STRPTR ComposeGadgetText(STRPTR *);
140 int Do_RequestChoice(STRPTR Title,
141 STRPTR Body,
142 STRPTR * Gadgets,
143 STRPTR PubScreen)
145 struct Screen * Scr;
146 struct EasyStruct ChoiceES;
147 STRPTR GadgetText;
148 STRPTR BodyText;
149 LONG Result;
150 IPTR args[1];
151 int Return_Value;
153 Return_Value = RETURN_OK;
154 Result = 0L;
156 GadgetText = ComposeGadgetText(Gadgets);
157 if (!GadgetText)
158 return RETURN_FAIL;
160 BodyText = FilterBodyText(Body);
161 if (!BodyText)
162 return RETURN_FAIL;
164 /* Make sure we can open the requester on the specified screen.
166 * If the PubScreen argument is not specified it will contain
167 * NULL, and hence open on the Workbench screen.
169 Scr = (struct Screen *)LockPubScreen((UBYTE *)PubScreen);
170 if (Scr != NULL)
172 ChoiceES.es_StructSize = sizeof(struct EasyStruct);
173 ChoiceES.es_Flags = 0L;
174 ChoiceES.es_Title = Title;
175 ChoiceES.es_TextFormat = BodyText;
176 ChoiceES.es_GadgetFormat = GadgetText;
178 /* Open the requester.
180 Result = EasyRequestArgs(Scr->FirstWindow, &ChoiceES, NULL, NULL);
181 if (Result != -1)
183 args[0] = (IPTR)Result;
185 VPrintf("%ld\n", args);
187 else
189 Return_Value = RETURN_FAIL;
190 SetIoErr(ERROR_NO_FREE_STORE);
191 PrintFault(IoErr(), ERROR_HEADER);
193 UnlockPubScreen(NULL, Scr);
195 else
197 Return_Value = RETURN_FAIL;
198 SetIoErr(ERROR_NO_FREE_STORE);
199 PrintFault(IoErr(), ERROR_HEADER);
202 FreeVec(GadgetText);
203 FreeVec(BodyText);
205 return Return_Value;
207 } /* Do_RequestChoice */
210 // Combine gadget strings and separate them by "|".
211 // Replace all "%" by "%%" because EasyRequest
212 // uses printf-like formatting.
213 static STRPTR ComposeGadgetText(STRPTR * Gadgets)
215 STRPTR GadgetText, BufferPos;
216 int GadgetLength = 0;
217 int CurrentGadget;
218 int PercentCnt = 0;
219 int StrLen;
220 int i;
222 for (CurrentGadget = 0; Gadgets[CurrentGadget]; CurrentGadget++)
224 StrLen = strlen(Gadgets[CurrentGadget]);
225 GadgetLength += StrLen + 1;
227 // Count "%"
228 for (i = 0; i < StrLen; i++)
230 if (Gadgets[CurrentGadget][i] == '%')
232 PercentCnt++;
237 GadgetLength += PercentCnt;
239 GadgetText = AllocVec(GadgetLength, MEMF_ANY);
240 if (!GadgetText)
242 SetIoErr(ERROR_NO_FREE_STORE);
243 PrintFault(IoErr(), ERROR_HEADER);
244 return NULL;
247 BufferPos = GadgetText;
248 for (CurrentGadget = 0; Gadgets[CurrentGadget]; CurrentGadget++)
250 int LabelLength = strlen(Gadgets[CurrentGadget]);
252 for (i = 0; i < LabelLength; i++)
254 if (Gadgets[CurrentGadget][i] == '%')
256 *BufferPos = '%';
257 BufferPos++;
258 *BufferPos = '%';
259 BufferPos++;
261 else
263 *BufferPos = Gadgets[CurrentGadget][i];
264 BufferPos++;
267 if (Gadgets[CurrentGadget + 1])
269 *BufferPos = '|';
270 BufferPos++;
272 else
273 *BufferPos = '\0';
276 return GadgetText;
278 } /* ComposeGadgetText */
281 // Replace % by %% because EasyRequest uses
282 // printf-like formatting characters.
283 // Result string must be freed with FreeVec().
284 static STRPTR FilterBodyText(STRPTR Body)
286 STRPTR GadgetText;
287 int GadgetLength = 0;
288 int StrLen;
289 int PercentCnt = 0;
290 int i, j;
292 if (!Body)
293 return NULL;
295 // Count % characters
296 StrLen = strlen(Body);
297 for (i = 0; i < StrLen; i++)
299 if (Body[i] == '%')
301 PercentCnt++;
305 GadgetLength = StrLen + PercentCnt + 1;
307 GadgetText = AllocVec(GadgetLength, MEMF_ANY);
308 if (!GadgetText)
310 SetIoErr(ERROR_NO_FREE_STORE);
311 PrintFault(IoErr(), ERROR_HEADER);
312 return NULL;
315 for (i = 0, j = 0; i < StrLen; i++)
317 if (Body[i] == '%')
319 GadgetText[j++] = '%';
320 GadgetText[j++] = '%';
322 else
324 GadgetText[j++] = Body[i];
328 GadgetText[j] = '\0';
330 return GadgetText;
332 } /* FilterBodyText */