Beispiel #1
0
char* word_to_string(word_t* wd, BOOL ghil)
{
	char* first_word = strdup(wd->string);
	char* ret;
	DIE(!first_word, "No memory in word_to_string()");

	if (wd->expand) {
		free(first_word);
		first_word = ExpandVariable(wd->string);
	}
	if (ghil) {
		char* tmp;
		ret = calloc(strlen(first_word) + 5, 1); 
		DIE(!ret, "No memory in word_to_string()");

		tmp = first_word;
		first_word = escape_quotes(first_word);
		free(tmp);

		_snprintf_s(ret, strlen(first_word) + 5, strlen(first_word) + 5, "\"%s\"", first_word);
		free(first_word);
	}
	else 
		ret = first_word;
	if (wd->next_word) {
		char* next_word = word_to_string(wd->next_word, ghil);
		DIE(!next_word, "No memory in word_to_string()");
		append(&ret, next_word);
		free(next_word);
	}

	if (wd->next_part) {
		char* next_part = word_to_string(wd->next_part, FALSE);
		char* tmp = malloc(strlen(ret) + strlen(next_part) + 1);
		if (!strcmp(next_part, "="))
			printf("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n");

		memcpy(tmp, ret, strlen(ret));
		memcpy(tmp + strlen(ret), next_part, strlen(next_part) + 1);
		free(next_part);
		ret = tmp;
	}

	return ret;
}
Beispiel #2
0
/* Print the coefficient of dec to string s. len(dec) > 0. dot is either
   NULL or a pointer to the location of a decimal point. */
static inline char *
coeff_to_string_dot(char *s, char *dot, const mpd_t *dec)
{
    mpd_uint_t x;
    mpd_ssize_t i;

    /* most significant word */
    x = mpd_msword(dec);
    s = word_to_string(s, x, mpd_word_digits(x), dot);

    /* remaining full words */
    for (i=dec->len-2; i >= 0; --i) {
        x = dec->data[i];
        s = word_to_string(s, x, MPD_RDIGITS, dot);
    }

    return s;
}
Beispiel #3
0
/* Print exponent x to string s. Undefined for MPD_SSIZE_MIN. */
static inline char *
exp_to_string(char *s, mpd_ssize_t x)
{
    char sign = '+';

    if (x < 0) {
        sign = '-';
        x = -x;
    }
    *s++ = sign;

    return word_to_string(s, x, mpd_word_digits(x), NULL);
}
Beispiel #4
0
unsigned int 
wordmap_search_query (GT4WordMap *map, unsigned long long query, parameters *p, int printall, unsigned int equalmmonly, unsigned int dosubtraction, GT4WordMap *querymap)
{
	static wordtable mm_table = {0};
	unsigned long long i, nwords = 0L;
	unsigned int count = 0L, currentcount = 0L, querycount = 0L;

	/* if no mismatches */
	if (!p->nmm) {
		return gt4_wordmap_lookup (map, query);
	}

	mm_table.wordlength = p->wordlength;

	/* find and set table size */
	if (!mm_table.nwords) {
		nwords = generate_mismatches (NULL, query, p->wordlength, 0, p->nmm, p->pm3, 0, 1, 0);
		wordtable_ensure_size (&mm_table, nwords, 0);
		if (debug_wordmap > 1) {
			fprintf (stderr, "MM Table size %llu, num mismatches %llu\n", mm_table.nwords, nwords);
		}
	}
	generate_mismatches (&mm_table, query, p->wordlength, 0, p->nmm, p->pm3, 0, 0, equalmmonly);
	if (debug_wordmap > 1) {
		fprintf (stderr, "MM Table size %llu\n", mm_table.nwords);
	}

	for (i = 0; i < mm_table.nwords; i++) {
		if (dosubtraction) {
			querycount = gt4_wordmap_lookup (querymap, mm_table.words[i]);
			currentcount = gt4_wordmap_lookup (map, mm_table.words[i]);
			if (currentcount > querycount) {
				if (debug_wordmap > 1) {
					fprintf (stderr, "%llu %llu %llu querycount %u currentcount %u\n", query, i, mm_table.words[i], querycount, currentcount);
				}
				mm_table.nwords = 0;
				return ~0L;
			}
			count += (currentcount - querycount);
		} else {
			currentcount = gt4_wordmap_lookup (map, mm_table.words[i]);
			count += currentcount;
			if (printall && currentcount > 0) {
				fprintf (stdout, "%s\t%u\n", word_to_string (mm_table.words[i], mm_table.wordlength), currentcount);
			}
		}
	}
	
	mm_table.nwords = 0;
	return count;
}
Beispiel #5
0
static HANDLE run_simple_command_nowait(simple_command_t* sc, HANDLE _in, HANDLE _out)
{
	BOOL success = TRUE;
	HANDLE  in  = _in, 
			out = _out, 
			err = INVALID_HANDLE_VALUE;
	HANDLE proc;
	DWORD open_mode;

	char* command = word_to_string(sc->verb, FALSE);

	if (!strcmp(command, "cd")) {
		BOOL rc = TRUE;
		if (sc->params != NULL) {
			char* path = word_to_string(sc->params, FALSE);
			rc = change_directory(path);
			free(path);
		}
		if (rc)
			return SPECIAL_COMMAND_HANDLE;
		return  INVALID_HANDLE_VALUE;
	}


	if (strchr(command, '=')) {
		set_variable(command);
		return SPECIAL_COMMAND_HANDLE;
	}

	if (sc->params != NULL) {
		char* params = word_to_string(sc->params, TRUE);
		append(&command, params);
		free(params);
	}
	
	if (sc->in != NULL) {
		SECURITY_ATTRIBUTES sa;
		char* file_name = word_to_string(sc->in, FALSE);

		ZeroMemory(&sa, sizeof(sa));
		sa.bInheritHandle = TRUE;

		in = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ, &sa, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
		if (in == INVALID_HANDLE_VALUE) {
			fprintf(stderr, "ERROR opening input");
			success = FALSE;
		}
		free(file_name);
	}

	if (sc->out != NULL) {
		SECURITY_ATTRIBUTES sa;
		char* file_name = word_to_string(sc->out, FALSE);

		ZeroMemory(&sa, sizeof(sa));
		sa.bInheritHandle = TRUE;
		if (sc->io_flags == IO_OUT_APPEND)
			open_mode = OPEN_ALWAYS;
		else
			open_mode = CREATE_ALWAYS;

		out = CreateFile(file_name, GENERIC_WRITE, FILE_SHARE_WRITE, &sa, open_mode, FILE_ATTRIBUTE_NORMAL, NULL);
		if (open_mode == OPEN_ALWAYS)
			seek_end_file(out);
		if (out == INVALID_HANDLE_VALUE) {
			fprintf(stderr, "ERROR opening output");
			success = FALSE;
		}
		free(file_name);
	}

	if (sc->err != NULL) {
		SECURITY_ATTRIBUTES sa;
		char* file_name;
		if (sc->io_flags == IO_ERR_APPEND)
			open_mode = OPEN_ALWAYS;
		else
			open_mode = CREATE_ALWAYS;

		if (sc->err == sc->out) {
			err = out;//DUPLICATEHANDLE?
		} else {

			file_name  = word_to_string(sc->err, FALSE);
			ZeroMemory(&sa, sizeof(sa));
			sa.bInheritHandle = TRUE;

			err = CreateFile(file_name, GENERIC_WRITE, FILE_SHARE_WRITE, &sa, open_mode, FILE_ATTRIBUTE_NORMAL, NULL);
			if (open_mode == OPEN_ALWAYS)
				seek_end_file(err);
			if (err == INVALID_HANDLE_VALUE) {
				fprintf(stderr, "ERROR opening Error");
				success = FALSE;
			}
			free(file_name);
		}
	}

	if (success)
		proc = WinCreateProcess(command, in, out, err);

	CloseHandle(in);
	CloseHandle(out);
	CloseHandle(err);
	free(command);

	if (!success)
		return INVALID_HANDLE_VALUE;
	return proc;
}