Пример #1
0
void dbg_lua_handle_hook(struct dbg_state* state, void* ud, freed_bstring type, uint16_t pos)
{
    struct lua_debugst* ds;
    unsigned int i;
    char* cstr;

    // Convert the name to lowercase.
    btolower(type.ref);
    cstr = bstr2cstr(type.ref, '0');

    printd(LEVEL_EVERYTHING, "firing hook %s.\n", cstr);

    // Loop through all of the modules.
    for (i = 0; i < list_size(&modules); i++)
    {
        ds = list_get_at(&modules, i);

        // Set stack top (I don't know why the top of the
        // stack is negative!)
        lua_settop(ds->state, 0);

        // Search handler table for entries.
        lua_getglobal(ds->state, HANDLER_TABLE_HOOKS_NAME);
        lua_getfield(ds->state, -1, cstr);
        if (!lua_istable(ds->state, -1))
        {
            // No such entry.
            printd(LEVEL_EVERYTHING, "no matching hook for %s.\n", cstr);
            lua_pop(ds->state, 2);
            continue;
        }

        // Call the handler function.
        printd(LEVEL_EVERYTHING, "calling hook %s.\n", cstr);
        lua_getfield(ds->state, -1, HANDLER_FIELD_FUNCTION_NAME);
        dbg_lua_push_state(ds, state, ud);
        lua_pushnumber(ds->state, pos);
        if (lua_pcall(ds->state, 2, 0, 0) != 0)
        {
            printd(LEVEL_ERROR, "error: unable to call debugger hook handler for '%s'.\n", type.ref->data);
            printd(LEVEL_ERROR, "%s\n", lua_tostring(ds->state, -1));
            lua_pop(ds->state, 2);
            continue;
        }
    }

    // Clean up.
    bcstrfree(cstr);
    bautodestroy(type);
}
Пример #2
0
static void header_field_cb(void *data, const char *field, size_t flen,
        const char *value, size_t vlen)
{
    Request *req = (Request *)data;

    if(hash_isfull(req->headers)) {
        log_err("Request had more than %d headers allowed by limits.header_count.",
                MAX_HEADER_COUNT);
    } else {
        bstring vstr = blk2bstr(value, vlen);
        bstring fstr = blk2bstr(field, flen);
        btolower(fstr);
        Request_set(req, fstr, vstr, 0);

        bdestroy(fstr); // we still own the key
    }
}
Пример #3
0
int Route_load(tst_t *settings, Pair *pair)
{
    const char *name = bdata(Pair_key(pair));
    char *sql = NULL;
    Value *val = Pair_value(pair);
    bstring type = NULL;
    int rc = 0;

    check(val, "Error loading route: %s", bdata(Pair_key(pair)));
    check(Value_is(val, CLASS), "Expected a Class but got a %s instead.",
            Value_type_name(val->type));
    Class *cls = val->as.cls;
    type = bstrcpy(Class_ident(cls));
    btolower(type);

    if(cls->id == -1) {
        if(biseqcstr(type, "dir")) {
            rc = Dir_load(settings, cls->params);
        } else if(biseqcstr(type, "proxy")) {
            rc = Proxy_load(settings, cls->params);
        } else if(biseqcstr(type, "handler")) {
            rc = Handler_load(settings, cls->params);
        } else {
            sentinel("Invalid type of route target: %s", bdata(Class_ident(cls)));
        }

        check(rc != -1, "Failed to create target for route %s", name);
        cls->id = rc;
    }

    sql = sqlite3_mprintf(bdata(&ROUTE_SQL), name, HOST_ID, cls->id, bdata(type));

    rc = DB_exec(sql, NULL, NULL);
    check(rc == 0, "Failed to intialize route.");

    sqlite3_free(sql);
    bdestroy(type);
    return 0;

error:
    if(sql) sqlite3_free(sql);
    bdestroy(type);
    return -1;
}
Пример #4
0
int dbg_lua_add_base(lua_State* L, const char* table)
{
    int handlers, entry;
    bstring lowered;
    lowered = bfromcstr(luaL_checkstring(L, 1));
    btolower(lowered);
    lua_getglobal(L, table);
    handlers = lua_gettop(L);
    lua_newtable(L);
    entry = lua_gettop(L);
    lua_pushvalue(L, 2);
    lua_setfield(L, entry, HANDLER_FIELD_FUNCTION_NAME);
    // entry is now at the top of the stack.
    lua_setfield(L, handlers, lowered->data);
    printd(LEVEL_DEBUG, "registered debugger command / hook '%s' with custom Lua handler.\n", lowered->data);
    lua_pop(L, 1);
    bdestroy(lowered);
    return 0;
}
Пример #5
0
int pp_lua_add_preprocessor_directive(lua_State* L)
{
	int handlers, entry;
	bstring lowered;
	lowered = bfromcstr(luaL_checkstring(L, 1));
	btolower(lowered);
	lua_getglobal(L, HANDLER_TABLE_NAME);
	handlers = lua_gettop(L);
	lua_newtable(L);
	entry = lua_gettop(L);
	lua_pushvalue(L, 2);
	lua_setfield(L, entry, HANDLER_FIELD_FUNCTION_NAME);
	// entry is now at the top of the stack.
	lua_setfield(L, handlers, lowered->data);
	printd(LEVEL_DEBUG, "registered preprocessor directive '%s' with custom Lua handler.\n", lowered->data);
	lua_pop(L, 1);
	bdestroy(lowered);
	return 0;
}
Пример #6
0
bstring MIME_match_ext(bstring path, bstring def)
{
    bstring lower_path = NULL;
    bstring type = NULL;
    int rc = 0;

    lower_path = bstrcpy(path);
    check(lower_path != NULL, "failed to create lower_path");
    rc = btolower(lower_path);
    check(rc == 0, "failed to lower case lower_path");

    type = tst_search_suffix(MIME_MAP, bdata(lower_path), blength(lower_path));

    bdestroy(lower_path);
    lower_path = NULL;

    return type == NULL ? def : type;
error:
    if(lower_path != NULL)
        bdestroy(lower_path);
    return def;
}
Пример #7
0
/* ----------------------------------------------------------
 * FUNCTION     : parse_line
 * DESCRIPTION  : This function will process a line of data
 *              : from a configuration file.
 * INPUT        : 0 - Line (bstring)
 * ---------------------------------------------------------- */
void parse_line (bstring line)
{
    bstring param, value;
    struct bstrList *list;
    int i;

    /* Check to see if this line has something to read. */
    if (line->data[0] == '\0' || line->data[0] == '#')
       return;

    /* Check to see if this line has a comment in it. */
    if ((list = bsplit(line, '#')) != NULL) {
        if ((bassign(line, list->entry[0])) == -1) {
            log_message("warning:  'bassign' in function 'parse_line' failed.");
        }
        if (list != NULL)
            bstrListDestroy(list);
    }

    /* Seperate line into a parameter and a value. */
    if ((i = bstrchr(line, ' ')) == BSTR_ERR)
        return;
    if ((param = bmidstr(line, 0, i)) == NULL)
        return;
    if ((value = bmidstr(line, i + 1, line->slen - i)) == NULL)
        return;

    /* Normalize Strings */
    if ((btolower(param)) != 0)
        log_message("warning:  'btolower' in function 'parse_line' failed.");
    if ((bltrim(value)) != 0)
        log_message("warning:  'bltrim' in function 'parse_line' failed.");
    if ((brtrim(value)) != 0)
        log_message("warning:  'brtrim' in function 'parse_line' failed.");

    /* Do something based upon value. */
    if ((biseqcstr(param, "daemon")) == 1) {
        /* DAEMON */
        if (!gc.daemon_mode) {
            if (value->data[0] == '1')
                gc.daemon_mode = 1;
            else
                gc.daemon_mode = 0;
        }

    } else if ((biseqcstr(param, "pid_file")) == 1) {
            /* PID FILE */
        gc.pid_file = bstrcpy(value);

    } else if ((biseqcstr(param, "sig_file")) == 1) {
        /* SIGNATURE FILE */
        gc.sig_file = bstrcpy(value);
   
    } else if ((biseqcstr(param, "mac_file")) == 1) {
        /* MAC / VENDOR RESOLUTION FILE */
        gc.mac_file = bstrcpy(value);

    } else if ((biseqcstr(param, "output")) == 1) {
        /* OUTPUT */
        conf_module_plugin(value, &activate_output_plugin);

    } else if ((biseqcstr(param, "user")) == 1) {
        /* USER */
        gc.priv_user = bstrcpy(value);

    } else if ((biseqcstr(param, "group")) == 1) {
        /* GROUP */
        gc.priv_group = bstrcpy(value);

    } else if ((biseqcstr(param, "interface")) == 1) {
        /* INTERFACE */
        gc.dev = bstr2cstr(value, '-');

    } else if ((biseqcstr(param, "filter")) == 1) {
        /* FILTER */
        gc.pcap_filter = bstr2cstr(value, '-');

    } else if ((biseqcstr(param, "network")) == 1) {
        /* NETWORK */
        parse_networks(bdata(value));

    }

    verbose_message("config - PARAM:  |%s| / VALUE:  |%s|", bdata(param), bdata(value));

    /* Clean Up */
    if (param != NULL)
        bdestroy(param);
    if (value != NULL)
        bdestroy(value);
}
Пример #8
0
int bbdocument_convert(char filetype[],char document[],const int dokument_size, buffer *outbuffer, const char titlefromadd[], char *subname, char *documenturi, unsigned int lastmodified, char *acl_allow, char *acl_denied, struct hashtable **metahash) {

	FILE *filconfp=NULL;
	char filconvertetfile_real[216] = "";
	char filconvertetfile_out_txt[216] = "";
	char filconvertetfile_out_html[216] = "";
	int exeocbuflen;
	int i;
	char *documentfinishedbuftmp;
	char fileconverttemplate[1024];
	struct fileFilterFormat *fileFilter = NULL;

        #ifdef DEBUG_TIME
                struct timeval start_time, end_time;
	#endif

	printf("bbdocument_convert: dokument_size %i, title \"%s\",filetype \"%s\"\n",dokument_size,titlefromadd,filetype);

	//konverterer filnavn til liten case
	for (i=0;i < strlen(filetype);i++) {
		//printf("%c\n",filetype[i]);
		filetype[i] = btolower(filetype[i]);
	}

	//hvis vi har et html dokument kan vi bruke dette direkte
	//er dog noe uefektist her, ved at vi gjør minnekopiering
	if ((strcmp(filetype,"htm") == 0) || (strcmp(filetype,"html") == 0 )) {
		if (titlefromadd[0]=='\0') {
			bmemcpy(outbuffer, document, dokument_size);
		}
		else {
			// Noen dokumenter kan ha lagt ved tittel ved add uten å ha tittel i html-en (f.eks epost).
			// Legg til korrekt tittel i dokumentet.
			// Html-parseren tar kun hensyn til den første tittelen, så det skal holde å legge den til
			// øverst i dokumentet.
			bprintf(outbuffer, "<title>%s</title>\n", titlefromadd);
			bmemcpy(outbuffer, document, dokument_size);
		}
		return 1;
	}
	else if (strcmp(filetype,"hnxt") == 0) {
		ntobr(document, dokument_size);
		bprintf(outbuffer, html_tempelate, titlefromadd, document);
		return 1;
	}

	#ifdef DEBUG
	printf("strcmp done\n");
	#endif

	struct fileFilterFormat *fileFilterOrginal;



	
	if (NULL == (fileFilterOrginal = hashtable_search(h_fileFilter,filetype) )) {
		printf("don't have converter for \"%s\"\n",filetype);

		#ifdef DEBUG
			printf("writing to unknownfiltype.log\n");
			FILE *fp;
			if ((fp = fopen(bfile("logs/unknownfiltype.log"),"ab")) == NULL) {
				perror(bfile("logs/unknownfiltype.log"));
			}
			else {
				printf("title %s\n",titlefromadd);
				printf("filetype %s\n",filetype);
				fprintf(fp,"%s: %s\n",titlefromadd,filetype);
				fclose(fp);
			}	
			printf("writing to unknownfiltype.log. done\n");
		#endif

		return 0;
	}

	//hvis dette er en fil av type text trenger vi ikke og konvertere den.
	if (strcmp((*fileFilterOrginal).format,"text") == 0) {
		#ifdef DEBUG
			printf("fileFilter ses it is a file of format text. Can use it direktly\n");
		#endif

		char *cpbuf;
		int cpbufsize;

		//konvertere alle \n til <br>
		cpbufsize = (dokument_size + 512 +1);
		cpbuf = malloc(cpbufsize);

		memcpy(cpbuf,document,dokument_size);
		cpbuf[dokument_size] = '\0';

		//stripper < og > tegn, da html parseren vil tro det er html tagger.
		//de er jo som kjent på formater < og >
		stripTags(cpbuf,dokument_size);

		#ifdef DEBUG
		printf("document %i\n",strlen(document));
		#endif

		bprintf(outbuffer, html_text_tempelate, titlefromadd, cpbuf);
		//printf("documentfinishedbuf %i\n", buffer_length(outbuffer));
		free(cpbuf);

                return 1;
	}

	//vi må lage en kopi av filfilter infoen, da vi skal endre den.
	fileFilter = malloc(sizeof(struct fileFilterFormat));

	memcpy(fileFilter, fileFilterOrginal, sizeof(struct fileFilterFormat));

	#ifdef DEBUG
		printf("have converter for file type\n");
	#endif


	/*****************************************************************************
		Vi har konverter. Må skrive til fil får å kunne sende den med
	*****************************************************************************/

	pid_t pid = getpid();

	sprintf(fileconverttemplate, "%s-%d", filconvertetfile, rand());
	sprintf(filconvertetfile_real,"%s-%u.%s",fileconverttemplate, (unsigned int)pid,filetype);
	sprintf(filconvertetfile_out_txt,"%s-%u.txt",fileconverttemplate, (unsigned int)pid);
	sprintf(filconvertetfile_out_html,"%s-%u.html",fileconverttemplate, (unsigned int)pid);

	#ifdef DEBUG
	printf("bbdocument_convert: filconvertetfile_real \"%s\"\n",filconvertetfile_real);
	#endif
	if ((filconfp = fopen(filconvertetfile_real,"wb")) == NULL) {
		perror(filconvertetfile_real);
		exit(1);
	}
	flock(fileno(filconfp),LOCK_EX);
	fwrite(document,1,dokument_size,filconfp);
	fclose(filconfp);

	//reåpner den read only, ogi lager en delt lås på filen, slik at vi ungår at perl /tmp watch sletter den.
	if ((filconfp = fopen(filconvertetfile_real,"rb")) == NULL) {
		perror(filconvertetfile_real);
		exit(1);
	}
	flock(fileno(filconfp),LOCK_SH);

	//convert to text.
	/*****************************************************************************/


	strsandr((*fileFilter).command,"#file",filconvertetfile_real);
	strsandr((*fileFilter).command,"#outtxtfile",filconvertetfile_out_txt);
	strsandr((*fileFilter).command,"#outhtmlfile",filconvertetfile_out_html);
	exeocbuflen = (dokument_size * 2) + 513; // XXX: Find a better way //(*documentfinishedbufsize);
	if ((documentfinishedbuftmp = malloc(exeocbuflen)) == NULL) {
		perror("Can't malloc documentfinishedbuftmp");
		return 0;
	}

	switch (fileFilter->filtertype) {
		case FILTER_EXEOC:
			run_filter_exeoc(
				documentfinishedbuftmp,  
				exeocbuflen,
				fileFilter, 
				metahash
			);
			break;
		case FILTER_PERL_PLUGIN:
			run_filter_perlplugin(
				documentfinishedbuftmp,
				exeocbuflen ,
				fileFilter,
				metahash
			);
			break;
		default:
			errx(1, "Unknown filtertype '%d'", fileFilter->filtertype);
	}

#ifdef USE_LIBEXTRACTOR
	if (fileFilter->attrwhitelist != NULL)
		add_libextractor_attr(metahash, filconvertetfile_real, fileFilter->attrwhitelist);
#endif


//<<<<<<< bbdocument.c

//=======
	//her parser vi argumenter selv, og hver space blir en ny argyment, selv om vi 
	//bruker "a b", som ikke riktig blir to argumenter her, a og b
	//splitter på space får å lage en argc
	/*TokCount = split((*fileFilter).command, " ", &splitdata);
	//#ifdef DEBUG
	printf("splitet comand in %i, program is \"%s\"\n",TokCount,splitdata[0]);
	//#endif
	printf("running: %s\n",(*fileFilter).command);
	//sender med størelsen på buferen nå. Vil få størelsen på hva vi leste tilbake
	char *execobuf = malloc(exeocbuflen);
//>>>>>>> 1.64

//<<<<<<< bbdocument.c
//=======
	char *envpairpath = strdup(/tmp/converter-metadata-XXXXXX);
	char envpair[PATH_MAX];
	mktemp(envpairpath);
	sprintf(envpair, "SDMETAFILE=%s", envpairpath);
	free(envpairpath);
	envpairpath = envpair + strlen("SDMETAFILE=");
        char *shargs[] = { "/usr/bin/env", NULL, "/bin/sh", "-c", NULL, NULL, };
	shargs[1] = envpair;
        shargs[4] = fileFilter->command;

        #ifdef DEBUG_TIME
                gettimeofday(&start_time, NULL);
        #endif

	if (!exeoc_timeout(shargs, execobuf, &exeocbuflen, &ret, 120)) {
		printf("dident get any data from exeoc. But can be a filter that creates files, sow we wil continue\n");
		execobuf[0] = '\0';
		exeocbuflen = 0;
	}

        #ifdef DEBUG_TIME
                gettimeofday(&end_time, NULL);
                printf("Time debug: exeoc_timeout() time: %f\n",getTimeDifference(&start_time, &end_time));
        #endif
	*/
/*
	if (metahash) {
		FILE *metafp;

		*metahash = create_hashtable(3, ht_stringhash, ht_stringcmp);

		if ((metafp = fopen(envpairpath, "r")) != NULL) {
			char *key, *value, line[2048];

			while (fgets(line, sizeof(line), metafp)) {
				char *p, *p2;

				// Comment
				if (line[0] == '#')
					continue;

				key = line;
				p = strchr(key, '=');
				if (p == NULL) {
					fprintf(stderr, "Invalid format on meta spec file: %s\n", line);
					continue;
				}
				p2 = p;
				while (isspace(*(p2-1)))
					p2--;
				*p2 = '\0';
				p++; // Skip past = 
				while (isspace(*p))
					p++;
				value = p;
				while (isspace(*key))
					key++;

				if (value[strlen(value)-1] == '\n')
					value[strlen(value)-1] = '\0';
				printf("Got pair: %s = %s\n", key, value);
				hashtable_insert(*metahash, strdup(key), strdup(value));
			}
			fclose(metafp);
			unlink(envpairpath);
		} else {
			printf("Couldn't open %s\n", envpairpath);
		}
	} */

//>>>>>>> 1.64
#ifdef DEBUG
	//printf("did convert to %i bytes (strlen %i)\n",exeocbuflen,strlen(documentfinishedbuftmp));
#endif

	if (strcmp((*fileFilter).outputformat,"text") == 0) {
                //stripper < og > tegn, da html parseren vil tro det er html tagger.
                //de er jo som kjent på formater < og >
                stripTags(documentfinishedbuftmp,strlen(documentfinishedbuftmp));

		bprintf(outbuffer, html_text_tempelate,titlefromadd,documentfinishedbuftmp);
	}
	else if (strcmp((*fileFilter).outputformat,"html") == 0) {
		//html trenger ikke å konvertere
		//dette er altså outputformat html. Ikke filtype outputformat. Filtupe hondteres lengere oppe
		//ToDo: må vel kopiere inn noe data her???
		bprintf(outbuffer, "%s", documentfinishedbuftmp);
		// Ved filkonvertering vil tittelen som sendes med (from add) være filnavnet.
		// Den vil vi kun bruke dersom dokumentet i seg selv ikke har en tittel.
		// Derfor legges den tittelen til nederst i dokumentet:
		bprintf(outbuffer, "<title>%s</title>\n", titlefromadd);
	}
	else if (strcmp((*fileFilter).outputformat,"textfile") == 0) {
		FILE *fh;
		struct stat inode; 
		char *cpbuf;
		printf("filconvertetfile_out_txt: \"%s\"\n",filconvertetfile_out_txt);

		if ((fh = fopen(filconvertetfile_out_txt,"rb")) == NULL) {
			printf("can't open out file \"%s\"\n",filconvertetfile_out_txt);
			perror(filconvertetfile_out_txt);
			goto bbdocument_convert_error;
		}		
       		fstat(fileno(fh),&inode);


                if ((cpbuf = malloc(inode.st_size +1)) == NULL) {
			perror("malloc");
			goto bbdocument_convert_error;
		}
                
        	fread(cpbuf,1,inode.st_size,fh);
		cpbuf[inode.st_size] = '\0';

		printf("did read back %i bytes from file \"%s\"\n",(int)inode.st_size,filconvertetfile_out_txt);

		printf("strlen cpbuf: %i\n",strlen(cpbuf));

                //stripper < og > tegn, da html parseren vil tro det er html tagger.
                //de er jo som kjent på formater < og >
                stripTags(cpbuf,inode.st_size);

		fclose(fh);

		//printf("have size %i\n",(*documentfinishedbufsize));

		bprintf(outbuffer, html_text_tempelate, titlefromadd, cpbuf);
		free(cpbuf);

		//seltter filen vi lagde
		unlink(filconvertetfile_out_txt);
	}
	else if (strcmp((*fileFilter).outputformat,"htmlfile") == 0) {
		FILE *fh;
		struct stat inode; 
		size_t n;
		char buf[4096];
		if ((fh = fopen(filconvertetfile_out_html,"rb")) == NULL) {
			printf("can't open out file \"%s\"\n",filconvertetfile_out_html);
			perror(filconvertetfile_out_html);
			goto bbdocument_convert_error;
		}		
       		fstat(fileno(fh),&inode);
#if 0
		if ((*documentfinishedbufsize) > inode.st_size) {
			(*documentfinishedbufsize) = inode.st_size;
		}
#endif
		while ((n = fread(buf, 1, sizeof(buf)-1, fh)) > 0) {
			bmemcpy(outbuffer, buf, n);
		}

		fclose(fh);
		unlink(filconvertetfile_out_html);
		bprintf(outbuffer, "<title>%s</title>\n", titlefromadd);
	}
	else if (strcmp(fileFilter->outputformat, "dir") == 0 || strcmp(fileFilter->outputformat, "diradd") == 0) {
		char *p, *pstart;
		/* Does len do anything any more? */
		int len, failed = 0;
		int type; /* 1 for dir, 2 for diradd */

		type = (strcmp(fileFilter->outputformat, "dir") == 0) ? 1 : 2;

		len = exeocbuflen;
		p = strdup(documentfinishedbuftmp);
		pstart = p;
		if (p == NULL) {
			goto bbdocument_convert_error;
		}
		bprintf(outbuffer, html_text_tempelate, titlefromadd, "");
		while (*p != '\0') {
			char *ft, *path;
			char *part = NULL;

			ft = p;
			for (; *p != ' '; p++)
				len--;
			*p = '\0';
			if (type == 2) {
				part = ++p;
				for (; *p != ' '; p++)
					len--;
				*p = '\0';
			}
			path = ++p;
			/* XXX: strchr() */
			for (; *p != '\n'; p++)
				len--;

			if (*p == '\n')
				*p++ = '\0';

			/* We have a new file, let's get to work on it */
			//printf("########Got: %s: %s\n", ft, path);
			{
				char *docbuf;
				int docbufsize;
				struct stat st;
				FILE *fp;

				if (stat(path, &st) == -1) { /* Unable to access file, move on to the next */
					fprintf(stderr, "File: %s\n", path);
					perror("stat");
					failed++;
					continue;
				}

				docbuf = malloc(st.st_size + 1); /* Make room for our lovely '\0' */
				if (docbuf == NULL) {
					perror("malloc");
					failed++;
					free(docbuf);
					continue;
				}
				docbufsize = st.st_size;
				if ((fp = fopen(path, "r")) == NULL) {
					perror("fopen");
					failed++;
					free(docbuf);
					continue;
				}
				fread(docbuf, 1, docbufsize, fp);
				fclose(fp);
				unlink(path);
				docbuf[docbufsize] = '\0';

				//runarb: 18 jan 2008: har var titel "", ikke titlefromadd, som gjorde at 24so crawling mistet titler.
				if (bbdocument_convert(ft, docbuf, docbufsize, outbuffer, titlefromadd, subname, documenturi, lastmodified, acl_allow, acl_denied,  NULL) == 0) {
					fprintf(stderr, "Failed on bbdocument_convert.\n");
					failed++;
					free(docbuf);
					continue;
				}
				
				free(docbuf);
			}
		}
		if (type == 2) {
			assert(0);
#if 0
			*documentfinishedbufsize = 1;
			*documentfinishedbuf = strdup(".");
#endif
		}
		//printf("Got this: %d %d<<\n%s\n", strlen(*documentfinishedbuf), *documentfinishedbufsize, *documentfinishedbuf);
		free(pstart);
	}
	else {
		printf("unknown dokument outputformat \"%s\"\n",fileFilter->outputformat);
		free(documentfinishedbuftmp);
		goto bbdocument_convert_error;
	}

	free(documentfinishedbuftmp);

	unlink(filconvertetfile_real);
	unlink(filconvertetfile_out_txt);
	unlink(filconvertetfile_out_html);

	fclose(filconfp);

	#ifndef DEBUG
		//runarb: 13okr2007: hvorfor ver denne komentert ut? Det hoper seg opp med filer
		//unlink(filconvertetfile_real);
	#endif

	//printf("documentfinishedbuf is: \n...\n%s\n...\n", *documentfinishedbuf);

	free(fileFilter);

	return 1;

	bbdocument_convert_error:
		if (filconvertetfile_real[0] != '\0') {
			unlink(filconvertetfile_real);
		}
		if (filconvertetfile_out_txt[0] != '\0') {
			unlink(filconvertetfile_out_txt);
		}
		if (filconvertetfile_out_html[0] != '\0') {
			unlink(filconvertetfile_out_html);
		}

		if (filconfp != NULL) {
			fclose(filconfp);
		}
		if (fileFilter != fileFilter) {
			free(fileFilter);
		}

		if (fileFilter != NULL) {
			free(fileFilter);
		}
		return 0;
}
Пример #9
0
static bstring skip_to_endif(state_t* state, bool stop_at_else, bool* stopped_at_else)
{
    char c;
    bool fresh_line = true;
    bstring temp = bfromcstr("");
    bstring temp_output = bfromcstr("");
    bstring output = bfromcstr("");
    int if_open = 1; 
    
    while (ppimpl_has_input(state))
    {
        c = ppimpl_get_input(state);
        switch(c)
        {
            case '#':
            case '.':
                if (!fresh_line)
                {
                    bconchar(output, c);
                    break;
                }
                bassigncstr(temp_output, "#");
                // first skip spaces
                while (ppimpl_has_input(state))
                {
                    c = ppimpl_get_input(state);
                    bconchar(temp_output, c);
                    if (c != ' ' && c != '\t')
                        break;
                }
                // read pp directive
                bassigncstr(temp, "");
                bconchar(temp, c);
                while (ppimpl_has_input(state))
                {
                    c = ppimpl_get_input(state);
                    bconchar(temp_output, c);
                    if (c == ' ' || c == '\t' || c == '\n')
                        break;
                    bconchar(temp, c);
                }
                
                btolower(temp);
                
                if (biseq(temp, bfromcstr("endif")))
                {
                    if_open--;
                    if (if_open == 0)
                    {
                        if (c != '\n') skip_to_endln(state);
                        *stopped_at_else = false;
                        return output;
                    }
                }
                else if (biseq(temp, bfromcstr("if")))
                {
                    if_open++;
                }
                else if (biseq(temp, bfromcstr("else")) && stop_at_else)
                {
                    if (if_open == 1)
                    {
                        if (c != '\n') skip_to_endln(state);
                        *stopped_at_else = true;
                        return output;
                    }
                }
                bconcat(output, temp_output);
                fresh_line = (c == '\n');
                break;
                
            case '\n':
                fresh_line = true;
                bconchar(output, c);
                break;
            case ' ':
            case '\t':
                bconchar(output, c);
                break;
                
            default:
                fresh_line = false;
                bconchar(output, c);
                break;
        }
    }
    
    // No .ENDIF was found.
    dhalt(ERR_PP_ASM_NO_ENDIF_TO_IF, ppimpl_get_location(state));

    // dhalt will trigger before this, but the compiler warns about
    // control potentially reaching the end of this function.
    return NULL;
}
Пример #10
0
void dbg_lua_handle_command(struct dbg_state* state, void* ud, freed_bstring name, list_t* parameters)
{
    struct lua_debugst* ds;
    struct customarg_entry* carg;
    char* cstr;
    unsigned int i, k;
    int paramtbl;

    // Convert the name to lowercase.
    btolower(name.ref);
    cstr = bstr2cstr(name.ref, '0');

    // Loop through all of the modules.
    for (k = 0; k < list_size(&modules); k++)
    {
        ds = list_get_at(&modules, k);

        // Set stack top (I don't know why the top of the
        // stack is negative!)
        lua_settop(ds->state, 0);

        // Search handler table for entries.
        lua_getglobal(ds->state, HANDLER_TABLE_COMMANDS_NAME);
        lua_getfield(ds->state, -1, cstr);
        if (!lua_istable(ds->state, -1))
        {
            // No such entry.
            lua_pop(ds->state, 2);
            continue;
        }

        // Call the handler function.
        lua_getfield(ds->state, -1, HANDLER_FIELD_FUNCTION_NAME);
        dbg_lua_push_state(ds, state, ud);
        lua_newtable(ds->state);
        paramtbl = lua_gettop(ds->state);
        for (i = 0; i < list_size(parameters); i++)
        {
            carg = list_get_at(parameters, i);

            lua_newtable(ds->state);
            if (carg->type == DBG_CUSTOMARG_TYPE_PATH)
                lua_pushstring(ds->state, "PATH");
            else if (carg->type == DBG_CUSTOMARG_TYPE_PARAM)
                lua_pushstring(ds->state, "PARAM");
            else if (carg->type == DBG_CUSTOMARG_TYPE_STRING)
                lua_pushstring(ds->state, "STRING");
            else
                lua_pushstring(ds->state, "NUMBER");
            lua_setfield(ds->state, -2, "type");
            if (carg->type == DBG_CUSTOMARG_TYPE_PATH)
                lua_pushstring(ds->state, carg->path->data);
            else if (carg->type == DBG_CUSTOMARG_TYPE_PARAM)
                lua_pushstring(ds->state, carg->param->data);
            else if (carg->type == DBG_CUSTOMARG_TYPE_STRING)
                lua_pushstring(ds->state, carg->string->data);
            else
                lua_pushnumber(ds->state, carg->number);
            lua_setfield(ds->state, -2, "value");
            lua_rawseti(ds->state, paramtbl, i + 1);
        }
        if (lua_pcall(ds->state, 2, 0, 0) != 0)
        {
            printd(LEVEL_ERROR, "error: unable to call debugger command handler for '%s'.\n", name.ref->data);
            printd(LEVEL_ERROR, "%s\n", lua_tostring(ds->state, -1));
            bautodestroy(name);
            bcstrfree(cstr);
            lua_pop(ds->state, 2);
            list_iterator_stop(&modules);
            list_destroy(parameters);
            return;
        }

        bautodestroy(name);
        bcstrfree(cstr);
        lua_pop(ds->state, 2);
        list_iterator_stop(&modules);
        list_destroy(parameters);

        // The command may have started the virtual machine, check the
        // status of the VM and execute if needed.
        if (state->get_vm()->halted == false)
            vm_execute(state->get_vm(), NULL);

        return;
    }

    // There is no command to handle this.
    printd(LEVEL_ERROR, "no such command found.\n");

    // Clean up.
    list_destroy(parameters);
    bautodestroy(name);
    bcstrfree(cstr);
}
Пример #11
0
void pp_lua_handle(struct pp_state* state, void* scanner, bstring name, list_t* parameters)
{
	struct lua_preproc* pp;
	struct customarg_entry* carg;
	char* cstr;
	bstring dot;
	unsigned int i;
	int paramtbl;

	// Convert the name to lowercase.
	btolower(name);
	cstr = bstr2cstr(name, '0');

	// Loop through all of the modules.
	list_iterator_start(&modules);
	while (list_iterator_hasnext(&modules))
	{
		pp = list_iterator_next(&modules);

		// Set stack top (I don't know why the top of the
		// stack is negative!)
		lua_settop(pp->state, 0);

		// Search handler table for entries.
		lua_getglobal(pp->state, HANDLER_TABLE_NAME);
		lua_getfield(pp->state, -1, cstr);
		if (!lua_istable(pp->state, -1))
		{
			// No such entry.
			lua_pop(pp->state, 2);
			continue;
		}

		// Call the handler function.
		lua_getfield(pp->state, -1, HANDLER_FIELD_FUNCTION_NAME);
		pp_lua_push_state(pp, state, scanner);
		lua_newtable(pp->state);
		paramtbl = lua_gettop(pp->state);
		for (i = 0; i < list_size(parameters); i++)
		{
			carg = list_get_at(parameters, i);

			lua_newtable(pp->state);
			if (carg->expr != NULL)
				lua_pushstring(pp->state, "EXPRESSION");
			else if (carg->string != NULL)
				lua_pushstring(pp->state, "STRING");
			else if (carg->word != NULL)
				lua_pushstring(pp->state, "WORD");
			else
				lua_pushstring(pp->state, "NUMBER");
			lua_setfield(pp->state, -2, "type");
			if (carg->expr != NULL)
				luaX_pushexpression(pp->state, carg->expr);
			else if (carg->string != NULL)
				lua_pushstring(pp->state, carg->string->data);
			else if (carg->word != NULL)
				lua_pushstring(pp->state, carg->word->data);
			else
				lua_pushnumber(pp->state, carg->number);
			lua_setfield(pp->state, -2, "value");
			lua_rawseti(pp->state, paramtbl, i + 1);
		}
		if (lua_pcall(pp->state, 2, 0, 0) != 0)
		{
			printd(LEVEL_ERROR, "error: unable to call preprocessor handler for '%s'.\n", name->data);
			printd(LEVEL_ERROR, "%s\n", lua_tostring(pp->state, -1));
			bdestroy(name);
			bcstrfree(cstr);
			lua_pop(pp->state, 2);
			list_iterator_stop(&modules);
			list_destroy(parameters);
			return;
		}

		bdestroy(name);
		bcstrfree(cstr);
		lua_pop(pp->state, 2);
		list_iterator_stop(&modules);
		list_destroy(parameters);
		return;
	}
	list_iterator_stop(&modules);

	// There is no custom preprocessor module that handles this directive, however
	// it could be a directive that is recognised by the underlying assembler / compiler,
	// so pass it through as output.
	dot = bfromcstr(".");
	bconcat(dot, name);
	btoupper(dot);
	bconchar(dot, ' ');
	list_iterator_start(parameters);
	while (list_iterator_hasnext(parameters))
	{
		carg = list_iterator_next(parameters);

		// Output the parameter based on the type.
		if (carg->word != NULL)
			bconcat(dot, carg->word);
		else if (carg->string != NULL)
		{
			bconchar(dot, '"');
			bescape(carg->string);
			bconcat(dot, carg->string);
			bconchar(dot, '"');
		}
		else
			bformata(dot, "%i", carg->number);
	}
	list_iterator_stop(parameters);
	handle_pp_lua_print_line(dot->data, scanner);

	// Clean up.
	list_destroy(parameters);
	bdestroy(name);
	bcstrfree(cstr);
}