summaryrefslogtreecommitdiff
path: root/ARexx.c
blob: ebcee2d50a35906b7c62fa1bd43e45dbd8959c20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
**  Bobi - The Ultimate Amiga Bob Manipulator
**
**  ARexx.c - ARexx-Port für Bobi (created from TopScan/ARexx.c)
**
**  COPYRIGHT (C) 1989-1993 BY CHRISTIAN A. WEBER, ZUERICH, SWITZERLAND.
**  ALL RIGHTS RESERVED. NO PART OF THIS SOFTWARE MAY BE COPIED, REPRODUCED,
**  OR TRANSMITTED IN ANY FORM OR BY ANY MEANS, WITHOUT THE PRIOR WRITTEN
**  PERMISSION OF THE AUTHOR. NO WARRANTY. USE AT YOUR OWN RISK.
*/

#include <proto/exec.h>
#include <rexx/rxslib.h>
#include <rexx/storage.h>
#include <rexx/errors.h>
#include <clib/rexxsyslib_protos.h>
#include <pragmas/rexxsyslib_pragmas.h>
#include <proto/dos.h>
#include <proto/intuition.h>

#include <string.h>

#include "Bobi.h"
#include "BobStructure.h"

#define MYREXXPORTNAME	"rexx_bobi"
#define MYREXXSUFFIX	"brx"


/****************************************************************************
**	Globals
*/

BYTE	arexxflag;			/* TRUE wenn ein Kommando von ARexx am laufen ist */
char	arexxfilename[200];	/* Filename z.B. für LoadBobs() */
BYTE	debugflag;

extern struct Screen		*mainscreen;
extern struct MyBob			*BobTable[];
extern WORD					numbobs,actbobnum,options,mainx0,mainy0;


/****************************************************************************
**	Locals
*/

static struct Library		*RexxSysBase;
static struct MsgPort		*myrexxport;		/* TopScan's rexx port */
static LONG					commandcounter;		/* Zählt abgeschickte Kommandos */
static char					resultstring[256];	/* Puffer für ARexx-Resultate */


/****************************************************************************
**	ASCII String holen, Anführungszeichen interpretieren.
**	Der Char*-Zeiger wird auf den Text nach dem String gesetzt.
*/

static void GetRexxString(char **strptr,char *dest)
{
	char *s=*strptr;

	/*
	**	Führende Spaces überlesen
	*/
	while(*s == ' ') s++;

	/*
	**	Text kopieren
	*/
	if(*s == '"')		/* Anführungszeichen --> plain copy */
	{
		s++;
		while(*s)
		{
			if(*s == '\\')		/* Escape-Sequenz */
			{
				switch(*++s)
				{
					case '\0':					goto ende;
					case 'n':	*dest++ = '\n';	break;
					default:	*dest++ = *s;	break;
				}
				s++;
			}
			else if(*s == '"') break;
 			else *dest++ = *s++;
		}
ende: ;
	}
	else				/* Keine Anführungszeichen --> bis Space */
	{
		while(*s)
		{
			if(*s == '\\')		/* Escape-Sequenz */
			{
				switch(*++s)
				{
					case '\0':					goto ende2;
					case 'n':	*dest++ = '\n';	break;
					default:	*dest++ = *s;	break;
				}
				s++;
			}
			else if(*s == ' ') break;
 			else *dest++ = *s++;
		}
ende2: ;
	}
	*dest = '\0';

	/*
	**	Spaces am Schluss überlesen
	*/
	while(*s == ' ') s++;

	*strptr = s;
}


/****************************************************************************
**	ASCII nach Integer wandeln, Nachkommastellen werden unterdrückt.
**	Der Char*-Zeiger wird auf den Text nach der Zahl gesetzt.
*/

static LONG GetRexxInteger(char **strptr)
{
	char	*s=*strptr;
	LONG	zahl=0,sign=1;

	while(*s == ' ') s++;

	if(*s == '-')
	{
		sign=-1;
		s++;
	}
	else if(*s == '+') s++;

	while((*s >= '0') && (*s <= '9'))
	{
		zahl = zahl*10+(*s++-'0');
	}

	while(*s == ' ') s++;

	*strptr = s;
	return zahl*sign;
}


/****************************************************************************
**	Die Interface-Routinen zu den ARexx-Kommandos
*/

static LONG RX_BobiToFrontFunc(char *args)
{
	ScreenToFront(mainscreen);
	return RC_OK;
}

static LONG RX_BobiToBackFunc(char *args)
{
	ScreenToBack(mainscreen);
	return RC_OK;
}

static LONG RX_ClearAllFunc(char *args)
{
	ClearAll();
	return RC_OK;
}

static LONG RX_LoadBobsFunc(char *args)
{
	GetRexxString(&args,arexxfilename);
	LoadBobsFunc();
	return RC_OK;
}

static LONG RX_InsertBobsFunc(char *args)
{
	GetRexxString(&args,arexxfilename);
	InsertBobsFunc();
	return RC_OK;
}

static LONG RX_SaveBobsFunc(char *args)
{
	GetRexxString(&args,arexxfilename);
	SaveBobsFunc();
	return RC_OK;
}

static LONG RX_GenerateCodeFunc(char *args)
{
	GetRexxString(&args,arexxfilename);
	GenerateCodeFunc();
	return RC_OK;
}

static LONG RX_LoadIFFFunc(char *args)
{
	GetRexxString(&args,arexxfilename);
	LoadPicFunc();
	return RC_OK;
}

static LONG RX_ClosePictureFunc(char *args)
{
	CloseScreenFunc();
	return RC_OK;
}

static LONG RX_GetBobFunc(char *args)
{
	int x,y,w,h;

	x = GetRexxInteger(&args);	y = GetRexxInteger(&args);
	w = GetRexxInteger(&args);	h = GetRexxInteger(&args);

	if(GetBob(x,y,w,h))
	{
		actbobnum++;
		return RC_OK;
	}
	else return RC_ERROR;
}

static LONG RX_InsertNewFunc(char *args)
{
	InsertNewBobFunc();
	return RC_OK;
}

static LONG RX_SetOrgFunc(char *args)
{
	if((numbobs>0) && (numbobs!=actbobnum))
	{
		BobTable[actbobnum]->X0 = GetRexxInteger(&args);
		BobTable[actbobnum]->Y0 = GetRexxInteger(&args);
		return RC_OK;
	}
	else return RC_ERROR;
}

#if 0
static LONG RX_DebugFunc(char *args)
{
	debugflag=GetRexxInteger(&args);
	return RC_OK;
}
#endif


/****************************************************************************
**	Ein Rexx-Kommando auswerten, geht auch wenn ARexx NICHT läuft
*/

LONG PerformRexxCommand(char *commandline)
{
	static struct MyRexxCmd
	{
		char	*Name;				/* Name des Kommandos */
		LONG	(*Func)(char *);	/* Aufzurufende Routine */
	}
	rexxcmdtab[] =
	{
		"BOBITOFRONT",			RX_BobiToFrontFunc,
		"BOBITOBACK",			RX_BobiToBackFunc,

		"CLEARALL",				RX_ClearAllFunc,
		"LOADBOBS",				RX_LoadBobsFunc,
		"INSERTBOBS",			RX_InsertBobsFunc,
		"SAVEBOBS",				RX_SaveBobsFunc,
		"GENERATECODE",			RX_GenerateCodeFunc,

		"LOADIFF",				RX_LoadIFFFunc,
		"CLOSEPICTURE",			RX_ClosePictureFunc,

		"GETBOB",				RX_GetBobFunc,
		"INSERTNEW",			RX_InsertNewFunc,
		"SETORG",				RX_SetOrgFunc,

#if 0
		"DEFAULTORG",			RX_DefaultOrgFunc,
		"SETCOLLISION",			RX_SetCollisionFunc,
		"FLIPX",				RX_FlipXFunc,
		"FLIPY",				RX_FlipYFunc,
		"ROTATE",				RX_RotateFunc,
		"ZOOM",					RX_ZoomFunc,
		"DELETEBOB",			RX_DeleteBobFunc,

		"LOADANIM",				RX_LoadAnimFunc,
		"SAVEANIM",				RX_SaveAnimFunc,
		"ANIMFIRST",			RX_AnimFirstFunc,
		"ANIMLAST",				RX_AnimLastFunc,
		"ANIMMODE",				RX_AnimModeFunc,
		"STARTANIM",			RX_StartAnimFunc,
		"STOPANIM",				RX_StopAnimFunc,

		"TOOLWINDOW",			RX_ToolWindowFunc,
		"LAYERMODE",			RX_LayerModeFunc,
		"ORGGRID",				RX_OrgGridFunc,
		"BOBBORDERS",			RX_BobBordersFunc,
		"BOBCOLLISION",			RX_BobCollisionFunc,
		"SETMAINORG",			RX_SetMainOrgFunc,
		"LOADOFFSETS",			RX_LoadOffsetsFunc,
		"SAVEOFFSETS",			RX_SaveOffsetsFunc,
		"REMAKELABELS",			RX_RemakeLabelsFunc,
		"REMAKECOLLISION",		RX_RemakeCollisionFunc
#endif
		NULL,					NULL				/* End-Markierung */
	};

	struct MyRexxCmd *cptr;

	*resultstring = '\0';			/* Resultat löschen */

	for(cptr=rexxcmdtab; cptr->Name; cptr++)
	{
		char *s,*d;
		for(s=commandline,d=cptr->Name; *d; s++,d++)
		{
			if(*s != *d) break;
		}

		if(*s <= ' ')				/* Kommandoname fertig <-> das richtige */
		{
			LONG result;

			while(*s == ' ') s++;		/* Spaces überhüpfen */

			arexxflag = TRUE;
			result = cptr->Func(s);		/* Funktion aufrufen */
			arexxflag = FALSE;

			return result;
		}
	}
	return RC_ERROR;
}


/****************************************************************************
**	ARexx initialisieren, 1x gerufen bei Programmstart
**	Öffnet rexxsyslib und erstellt rexx_topscan Port
**	Resultat: 0 falls ARexx nicht läuft, sonst Signalmaske von neuem RexxPort
*/

ULONG InitARexx(void)
{
	if(RexxSysBase=OpenLibrary(RXSNAME,0))
	{
		if(myrexxport=CreatePort(MYREXXPORTNAME,0))
		{
			return 1U<<myrexxport->mp_SigBit;
		}
	}
	CloseARexx();
	return 0;
}


/****************************************************************************
**	ARexx cleanup, 1x gerufen bei Programmende
**	Zerstört den ARexx-Port und schliesst rexxsyslib, falls kein Kommando
**	am Laufen ist, sonst wird FALSE zurückgegeben
*/

ULONG CloseARexx(void)
{
	if(commandcounter)
	{
		ShowMonoReq2("Can't quit yet, there are\nARexx-Commands pending.");
		return FALSE;
	}

	if(myrexxport)
	{
		DeletePort(myrexxport);
		myrexxport = NULL;
	}

	if(RexxSysBase)
	{
		CloseLibrary(RexxSysBase);
		RexxSysBase = NULL;
	}

	return TRUE;
}


/****************************************************************************
**	RexxMessage holen und Kommando starten falls ARexx läuft
*/

void RexxMsgHandler(void)
{
	struct RexxMsg *msg;
	if(!myrexxport) return;		/* Sofort zurück falls ARexx nicht läuft */

	while (msg = (struct RexxMsg *)GetMsg(myrexxport))
	{
		if(msg->rm_Node.mn_Node.ln_Type != NT_REPLYMSG)
		/*
		**	Es ist eine gültige Message und keine Antwort.
		*/
		{
			if(IsRexxMsg(msg))
			{
				/* Sollen wir ein Kommando auführen ? */
				if((msg->rm_Action & RXCODEMASK) == RXCOMM)
				{
					msg->rm_Result1 = 0;
					msg->rm_Result2 = 0;

					if(msg->rm_Result1 = PerformRexxCommand(msg->rm_Args[0]))
					{
						/* Func returned an error */
					}
					else if((*resultstring)	&& (msg->rm_Action & RXFF_RESULT))
					{
						msg->rm_Result2 = (LONG)
							CreateArgstring(resultstring,strlen(resultstring));
					}
				}
//				else INTERNAL_ERROR;
			}
//			else INTERNAL_ERROR;
			ReplyMsg(msg);
		}
		else
		/*
		**	Diese Message ist ein reply auf eine Message welche wir
		**	früher mal geschickt haben
		*/
		{
			DeleteArgstring(msg->rm_Args[0]);
			if(msg->rm_Stdin) Close(msg->rm_Stdin);
			DeleteRexxMsg(msg);
			commandcounter--;
		}
	}
}


/****************************************************************************
**	Eine RexxMessage schicken (Speicher wird von RexxMsgHandler freigegeben)
**	Parameter: Kommando-String
*/

void SendRexxCommand(char *command)
{
	BPTR window;

	if(window=Open("CON:0/20/600/160/Bobi ARexx Output Window/AUTO/CLOSE",MODE_OLDFILE))
	{
		struct MsgPort *masterport;

		Forbid();
		if(masterport=FindPort(RXSDIR))
		{
			struct RexxMsg *msg;

			if(msg=CreateRexxMsg(myrexxport,MYREXXSUFFIX,MYREXXPORTNAME))
			{
				if(msg->rm_Args[0] = CreateArgstring(command,strlen(command)))
				{
					msg->rm_Action	= RXCOMM;
					msg->rm_Stdin	= window;
					msg->rm_Stdout	= window;
					window			= NULL;
					commandcounter++;
					PutMsg(masterport,(struct Message *)msg);
				}
			}
		}
		Permit();

		if(window) Close(window);	/* Schliessen falls Error */
	}
}