Esempio n. 1
0
void startScripting(char* loadedFrom)
{
	uint32_t size = 0;


	uint8_t* address = NULL;

	const char* partitionScript = nvram_getvar("partition-script"); /*get the partition where the script is in NVRAM*/
	const char* fileScript = nvram_getvar("file-script"); /*get the path to the file script in NVRAM*/
	const char* scriptingLinux = nvram_getvar("scripting-linux"); /*tells whether to run the script before booting linux. Accepted values : true or 1, anything else if false*/
	const char* scriptingOpeniboot = nvram_getvar("scripting-openiboot"); /*tells whether to run the script before launching openiboot console. Accepted values : true or 1, anything else if false*/

	//uint32_t numberOfLines = 1; /*number of lines in the script files - To be used with the next commented section of code for debug*/
	//bufferPrintf("partition-script %s\r\n",partitionScript);  /*For debug*/
	//bufferPrintf("file-script %s\r\n",fileScript);  /*For debug*/

	if(!partitionScript)
		return;

	if(!fileScript)
		return;

	/* ------ extracting the script file at address 0x09000000 ----- */
	if(strcmp(loadedFrom, "linux") == 0)
	{
		if(!scriptingLinux || (strcmp(scriptingLinux, "true") != 0 && strcmp(scriptingLinux, "1") != 0))
		{
			return; /* terminate the function if scripting is not asked*/
		}
	}

	if(strcmp(loadedFrom, "openiboot") == 0)
	{
		if(!scriptingOpeniboot || (strcmp(scriptingOpeniboot, "true") != 0 && strcmp(scriptingOpeniboot, "1") != 0))
		{
			return; /* terminate the function if scripting is not asked*/
		}
	}

	Volume* volume;
	io_func* io;
	io = bdev_open(parseNumber(partitionScript));
	if(io == NULL) {
		bufferPrintf("fs: cannot read partition!\r\n");
		return;
	}

	volume = openVolume(io);
	if(volume == NULL) {
		bufferPrintf("fs: cannot openHFS volume!\r\n");
		return;
	}

	HFSPlusCatalogRecord* record;
	char* name;
	record = getRecordFromPath(fileScript, volume, &name, NULL);
	if(record != NULL) {
		if(record->recordType == kHFSPlusFolderRecord)
		{
			bufferPrintf("this path is a folder, not a file\r\n");
			return;
		} else
		{
			size = readHFSFile((HFSPlusCatalogFile*)record, &address, volume);
			if(!address)
				return;
			//bufferPrintf("size = %d\r\n",size);  /*size of script file, used later*/
		}
	} else {
		bufferPrintf("No such file or directory\r\n");
		return;
	}

	closeVolume(volume);
	CLOSE(io);

	char* addrBOF = (char*) address; /* pointer on the begening of the file*/
	char* addrEOF = (char*) (address + size); /*pointer 1 byte after the file */
	char* addr; /*pointer on the current space on memory*/

#if 0
	//addrEOF = (void*)address+size+1;
	/* ----- counting how many lines are present in the script file by the '\n' -----  */
	addr = addrBOF;
	while(addr < addrEOF)
	{
		if(*addr=='\r'){
			numberOfLines++;
		}
		addr++;
	}
	bufferPrintf("number of lines : %d\r\n", numberOfLines);
#endif

	char* bufferLine = malloc(100);

	/* ----- extracting each line to the buffer -----*/
	addr = addrBOF;
	while(addr < addrEOF)
	{
		int charAt = 0;
		while((*addr != '\n') && (addr < addrEOF))
		{
			if(*addr != '\r')
			{
				bufferLine[charAt] = *addr;
				//bufferPrintf("reading char : %c\r\n",*addr);
				charAt++;
			}
			addr++;
		}

		bufferLine[charAt]='\0';

		bufferPrintf("\r\n%s\r\n", bufferLine);

		bufferLine[charAt]='\n';
		bufferLine[charAt + 1] = '\0';

		if(scriptCommand(bufferLine))
		{
			//bufferPrintf("command sent\r\n");
		}
		/*else { //error in command, function scriptCommand returned false
		}*/
		addr++;
	}

	free(bufferLine);
	free(address);
}
static DECLCALLBACK(int) scriptRun(PVM pVM, RTFILE File)
{
    RTPrintf("info: running script...\n");
    uint64_t cb;
    int rc = RTFileGetSize(File, &cb);
    if (RT_SUCCESS(rc))
    {
        if (cb == 0)
            return VINF_SUCCESS;
        if (cb < _1M)
        {
            char *pszBuf = (char *)RTMemAllocZ(cb + 1);
            if (pszBuf)
            {
                rc = RTFileRead(File, pszBuf, cb, NULL);
                if (RT_SUCCESS(rc))
                {
                    pszBuf[cb] = '\0';

                    /*
                     * Now process what's in the buffer.
                     */
                    char *psz = pszBuf;
                    while (psz && *psz)
                    {
                        /* skip blanks. */
                        while (RT_C_IS_SPACE(*psz))
                            psz++;
                        if (!*psz)
                            break;

                        /* end of line */
                        char *pszNext;
                        char *pszEnd = strchr(psz, '\n');
                        if (!pszEnd)
                            pszEnd = strchr(psz, '\r');
                        if (!pszEnd)
                            pszNext = pszEnd = strchr(psz, '\0');
                        else
                            pszNext = pszEnd + 1;

                        if (*psz != ';' && *psz != '#' && *psz != '/')
                        {
                            /* strip end */
                            *pszEnd = '\0';
                            while (pszEnd > psz && RT_C_IS_SPACE(pszEnd[-1]))
                                *--pszEnd = '\0';

                            /* process the line */
                            RTPrintf("debug: executing script line '%s'\n",  psz);
                            rc = scriptCommand(pVM, psz, pszEnd - psz);
                            if (RT_FAILURE(rc))
                            {
                                RTPrintf("error: '%s' failed: %Rrc\n", psz, rc);
                                break;
                            }
                        }
                        /* else comment line */

                        /* next */
                        psz = pszNext;
                    }

                }
                else
                    RTPrintf("error: failed to read script file: %Rrc\n", rc);
                RTMemFree(pszBuf);
            }
            else
            {
                RTPrintf("error: Out of memory. (%d bytes)\n", cb + 1);
                rc = VERR_NO_MEMORY;
            }
        }
        else
            RTPrintf("error: script file is too large (0x%llx bytes)\n", cb);
    }
    else
        RTPrintf("error: couldn't get size of script file: %Rrc\n", rc);

    return rc;
}
Esempio n. 3
0
 void ScriptParser::AddSectionDefinition( std::unique_ptr<ISectionDefinition> sectionTypeToAdd )
 {
     std::wstring scriptCommand(sectionTypeToAdd->GetScriptCommand());
     to_lower(scriptCommand);
     sectionTypes.emplace(std::make_pair(std::move(scriptCommand), std::move(sectionTypeToAdd)));
 }