/* Command-line shell for the "Pawn" Abstract Machine.
*
* Copyright (c) ITB CompuPhase, 2001-2010
*
* This file may be freely used. No warranties of any kind.
*/
#include <stdio.h>
#include <stdlib.h> /* for exit() */
#include <string.h> /* for memset() (on some compilers) */
#include "amx.h"
#include "amxaux.c"
void ErrorExit(AMX *amx, int errorcode)
{
printf("Run time error %d: \"%s\" on address %ld\n",
errorcode, aux_StrError(errorcode),
(amx != NULL) ? amx->cip : 0);
}
void PrintUsage(char *program)
{
printf("Usage: %s <filename>\n", program);
exit(1);
}
int usePawnFunction(AMX *amx, char *functionName, char *functionParameterValue)
{
size_t memsize;
void *program;
int index, err;
cell *address;
char output[128];
err = amx_FindPublic(&amx, functionName, &index); // searching pawn function with name given in functionName
if (err)
{
ErrorExit(&amx, err);
return -1;
}
err = amx_PushString(&amx, &address, functionParameterValue, 0, 0); // gives value functionParameterValue on first parameter of pawn function
if (err)
{
ErrorExit(&amx, err);
return -1;
}
err = amx_Exec(&amx, NULL, index);
if (err)
{
ErrorExit(&amx, err);
return -1;
}
amx_GetString(output, address, 0, sizeof output);
amx_Release(&amx, address);
printf("function %s returns \"%s\"\n", functionName, output);
}
int main(int argc, char *argv[])
{
extern int AMXAPI amx_ConsoleInit(AMX *amx);
extern int AMXAPI amx_ConsoleCleanup(AMX *amx);
extern int AMXAPI amx_CoreInit(AMX *amx);
extern int AMXAPI amx_CoreCleanup(AMX *amx);
size_t memsize;
void *program;
AMX amx;
int index, err;
cell *address;
char output[128];
char name[32];
while (1)
{
system("echo %cd%");
printf("\n");
printf("Enter name of program to load. e for exit: ");
scanf("%s", name);
if (strcmp(name, "e") == 0)
break;
size_t memsize;
void *program;
int err;
if ((memsize = aux_ProgramSize(name)) == 0)
{
printf("Program not found.\n");
return -1;
}
program = malloc(memsize);
if (program == NULL)
{
ErrorExit(NULL, AMX_ERR_MEMORY);
return -1;
}
err = aux_LoadProgram(&amx, name, program);
if (err)
{
ErrorExit(&amx, err);
return -1;
}
amx_ConsoleInit(&amx);
err = amx_CoreInit(&amx);
if (err)
{
ErrorExit(&amx, err);
return -1;
}
if (usePawnFunction(&amx, "rot13", "some text") == -1)
continue;
if (usePawnFunction(&amx, "rot13", "some other text") == -1)
continue;
if (usePawnFunction(&amx, "rot13", "some more blabla") == -1)
continue;
amx_ConsoleCleanup(&amx);
amx_CoreCleanup(&amx);
amx_Cleanup(&amx);
free(program);
}
return 0;
}