Index

Example of how a Gosub is executed

struct strProgLine *Execute_Gosub(struct strRunCode *pr,struct strProgLine *p)
{
	struct strProgLine *pl;
	if ((pl = GetJumpAddress_AndChangeIfPossible(pr,p))==NULL) return NULL;
	pr->aCallStack[pr->iCallStackpointer] = p->pNext;
	pr->iCallStackpointer++;
	return pl->pNext;
}

Example the main function of how to execute code

int ExecuteCode(struct strRunCode *pr,struct strProgLine *pLineFirst)
{
	int iResult;
	int i;
	pr->iCallStackpointer = 0;
	pr->iStackpointer = 0;
	pr->iProgResult = 1;
	if (pr->sOut!=NULL)
	{
		pr->sOutEnd = pr->sOut;
	}
	if (BuildExecuteLabels(pr,pLineFirst) < 0)
	{
		ERRSTR;
		AddToLog("Returning");
		return -1; // Otherwise application will crash
	}
	for (i=0;i<MAXSTRINGSTACK;i++) pr->aStringStack[i].s = NULL; // For removing purpose
	if (AssignExecuteLevels(pr,pLineFirst) < 0) return -1;
	iResult = RecursiveExecute(pr,pLineFirst);
	AutoCloseResultsets(pr);
	return iResult;
}

Example of the main function with fcgi

int main(void)
{
	char *sRemoteIp;
	char *sExtra;
	char *sQuery;
	char *s;
	char *sIn;
	char *sHome;
	sExtra = NULL;
	// Must have full access to the log file, otherwize segfault
	sHome = getenv("DOCUMENT_ROOT"); // Only set after FCGI_Accept, name is as environment
	if (sHome == NULL)
	{
		sHome = getenv("docroot");
		if (sHome == NULL)
		{
			// cprintf("Cannot start without document root");
			return 1;
		}
	}
	if (*sHome == '\0')
	{
		// cprintf("Cannot start without document root");
		return 1;
	}
	if (StartProgram(sHome)< 0) return 1; 
	AddToLog("s:s","Root",sHome);
	while(FCGI_Accept() >= 0)
	{
		sRemoteIp = getenv("REMOTE_ADDR");
		AddToLog("s:s","RemoteIP",sRemoteIp);
		sQuery = getenv("QUERY_STRING");
		AddToLog("s:s","Query",sQuery);
		if (sQuery != NULL)
		{
			s = strchr(sQuery,'?');
			if (s!=NULL)
			{
				sExtra = s+1;
			}
			else sExtra = NULL;
			sIn = getenv("SCRIPT_NAME");
			AddToLog("s:s","Input",sIn);
			if (sIn != NULL)
			{
				printf("Content-type: text/html\r\n\r\n");
				HandleRequest(NULL,sIn,sRemoteIp,sExtra);
			}
		}
	}
	AddToLog("End program");
	return 0;
}

Example of mainfunction with sockets

int main(void)
{
	struct sockaddr pAddr;
	struct sockaddr_in pSin;
	int iServerSocket;
	int i;
	int iRes;
	unsigned long b=1;
	int iQuantityConnections = 100;
	struct strTCPserver_client *pClient;
	int iCounter;
	int iPort;
	iPort = 1080;
	pSin.sin_family = AF_INET;
	pSin.sin_port = htons(iPort);
	pSin.sin_addr.s_addr = INADDR_ANY;
	memcpy(&pAddr,&pSin,sizeof(struct sockaddr_in));
	iServerSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if (iServerSocket < 0) return 1;
	setsockopt(iServerSocket,SOL_SOCKET,SO_REUSEADDR,(char *)∧i,sizeof (i));
	iRes = bind(iServerSocket,&pAddr,sizeof(struct sockaddr));
	if (iRes < 0) return 1;
	ioctl((int)iServerSocket,FIONBIO,&b);
	pClient = (struct strTCPserver_client *)malloc(sizeof(struct strTCPserver_client) * iQuantityConnections);
	if (pClient == NULL) return 1;
	mhbuf->append_string = ServerAppendString;
	mhbuf->append_string_const = ServerAppendStringConst;
	mhbuf->append_string_len = ServerAppendStringLen;
	mhbuf->copy_string = ServerCopyString;
	while (1==1)
	{
		iCounter = 0;
		for (i=0;i*lt;iQuantityConnections;i++)
		{
			if(!pClient[i].con)
			{
				if(AcceptClient(iServerSocket,∧pClient[i]))
				{
					// Just do nothing
				}
			}
			else {
				switch(ReceiveClient(pClient[i].cs))
				{
				case 0:
					iCounter++;
					break;
				case 1:
					close(pClient[i].cs);
					pClient[i].con = 0;
					iCounter++;
					break;
				default:
					close(pClient[i].cs);
					pClient[i].con = 0;
				}
			}
		}
		if (iCounter == 0)
		{
			msleep(100); // But signaling must be off if 10 ms CPU usage 0.3% no matter what we use
		}
	}
	return 0;
}