示例#1
0
文件: main.c 项目: 11mariom/ekg2
static char *logs_prepare_path(session_t *session, const char *logs_path, const char *uid, time_t sent) {
	char *uidtmp, datetime[5];
	struct tm *tm = NULL;
	string_t buf;

	if (!logs_path)
		return NULL;

	buf = string_init(NULL);

	while (*logs_path) {
		if ((char)*logs_path == '%' && (logs_path+1) != NULL) {
			switch (*(logs_path+1)) {
				case 'S':	string_append_n(buf, session ? session->uid : "_null_", -1);
						break;
				case 'P':	string_append_n(buf, config_profile ? config_profile : "_default_", -1);
						break;
				case 'u':	uidtmp = xstrdup(get_uid(session, uid));
						goto attach; /* avoid code duplication */
				case 'U':	uidtmp = xstrdup(get_nickname(session, uid));
attach:
						if (!uidtmp) uidtmp = xstrdup(uid);

						if (xstrchr(uidtmp, '/'))
							*(xstrchr(uidtmp, '/')) = 0; // strip resource
						string_append_n(buf, uidtmp ? uidtmp : uid, -1);
						xfree(uidtmp);
						break;
				case 'Y':	if (!tm) tm = localtime(&sent);
							snprintf(datetime, 5, "%4d", tm->tm_year+1900);
						string_append_n(buf, datetime, 4);
						break;
				case 'M':	if (!tm) tm = localtime(&sent);
							snprintf(datetime, 3, "%02d", tm->tm_mon+1);
						string_append_n(buf, datetime, 2);
						break;
				case 'D':	if (!tm) tm = localtime(&sent);
							snprintf(datetime, 3, "%02d", tm->tm_mday);
						string_append_n(buf, datetime, 2);
						break;
				default:	string_append_c(buf, *(logs_path+1));
			};

			logs_path++;
		} else if (*logs_path == '~' && (*(logs_path+1) == '/' || *(logs_path+1) == '\0')) {
			string_append_n(buf, home_dir, -1);
			//string_append_c(buf, '/');
		} else
			string_append_c(buf, *logs_path);
		logs_path++;
	};

	// sanityzacja sciezki - wywalic "../", zamienic znaki spec. na inne
	// zamieniamy szkodliwe znaki na minusy, spacje na podkreslenia
	// TODO
	xstrtr(buf->str, ' ', '_');

	return string_free(buf, 0);
}
示例#2
0
文件: autoacts.c 项目: hiciu/ekg2
int irc_autorejoin(session_t *s, int when, char *chan) {
	irc_private_t *j;
	string_t st;
	window_t *w;
	char *chanprefix;
	int rejoin;

#if 1	/* there's no need of doing it, already checked by irc_onkick_handler() or if it goes through irc_c_init() it's even better. */
	if (!s || !(j = s->priv) || (s->plugin != &irc_plugin))
		return -1;
#endif
	chanprefix = SOP(_005_CHANTYPES);
	rejoin = session_int_get(s, "REJOIN");

	if (!(rejoin&(1<<(when))))
		return -1;

	switch (when) {
		case IRC_REJOIN_CONNECT:
			st = string_init(NULL);
			for (w = windows; w; w = w->next) {
				if (!w->target || w->session != s)			/* check if sessions match and has w->target */
					continue;

				if (valid_plugin_uid(s->plugin, w->target) != 1)	/* check if window is correct for irc: */
					continue;

				if (!xstrchr(chanprefix, (w->target)[4]))		/* check if this is channel.. */
					continue;

				if (st->len)
					string_append_c(st, ',');
				if ((w->target)[4] == '!') {
					string_append_c(st, '!');
					string_append(st, w->target + 10);
				} else {
					string_append(st, w->target + 4);
				}
			}
			if (st->len)
				irc_write(s, "JOIN %s\r\n", st->str);
			string_free(st, 1);
			break;

		case IRC_REJOIN_KICK:
			irc_write(s, "JOIN %s\r\n", chan);
			break;

		default:
			return -1;
	}
	return 0;
}
示例#3
0
文件: vars.c 项目: porridge/ekg
/*
 * variable_digest()
 *
 * tworzy skrócon± wersjê listy zmiennej do zachowania w li¶cie kontaktów
 * na serwerze.
 */
char *variable_digest()
{
	string_t s = string_init(NULL);
	list_t l;

	for (l = variables; l; l = l->next) {
		struct variable *v = l->data;

		if (!v->ptr)
			continue;

		if ((v->type == VAR_INT || v->type == VAR_BOOL || v->type == VAR_MAP) && strcmp(v->name, "uin")) {
			string_append(s, v->short_name);
			string_append(s, itoa(*(int*)(v->ptr)));
		}
	}

	for (l = variables; l; l = l->next) {
		struct variable *v = l->data;
		char *val;

		if (!v->ptr)
			continue;

		if (!v->type == VAR_STR || !strcmp(v->name, "password") || !strcmp(v->name, "local_ip"))
			continue;
	
		val = *((char**)v->ptr);

		string_append(s, v->short_name);

		if (!val) {
			string_append_c(s, '-');
			continue;
		}

		if (strchr(val, ':') || val[0] == '+') {
			char *tmp = base64_encode(val);
			string_append_c(s, '+');
			string_append(s, tmp);
			string_append_c(s, ':');
			xfree(tmp);
		} else {
			string_append(s, val);
			string_append_c(s, ':');
		}
	}
	
	return string_free(s, 0);
}
示例#4
0
string_ptr string_join(vec_ptr vec, char sep)
{
    int        i;
    string_ptr result = string_alloc();

    for (i = 0; i < vec_length(vec); i++)
    {
        string_ptr s = vec_get(vec, i);
        if (i > 0)
            string_append_c(result, sep);
        string_append(result, s);
    }
    return result;
}
示例#5
0
void cmsg_append(byte color, cptr str)
{
    if (!_msg_count)
        cmsg_add(color, str);
    else
    {
        msg_ptr m = msg_get(0);

        assert(m);
        /* You tunnel into the granite wall. <x17> The heroism wears off.
           Not: You tunnel into the granite wall. The heroism wears off. <x17> */
        if (m->count > 1)
        {
            cmsg_add(color, str);
            return;
        }
        if (string_length(m->msg) && strlen(str) > 1)
            string_append_c(m->msg, ' ');
        if (color != m->color)
            string_printf(m->msg, "<color:%c>%s</color>", attr_to_attr_char(color), str);
        else
            string_append_s(m->msg, str);
    }
}
示例#6
0
文件: main.c 项目: hiciu/ekg2
/*
 * przygotowanie nazwy pliku z rozszerzeniem
 * %S - sesja nasza
 * %u - użytkownik (uid), z którym piszemy
 * %U - użytkownik (nick)   -||-
 * %Y, %M, %D - rok, miesiąc, dzień
 * zwraca ścieżkę, która należy ręcznie zwolnić przez xfree()
 */
static char *logs_prepare_fname(logs_log_t *log) {
	session_t *s;
	const char *logs_path;
	struct tm *tm = NULL;
	string_t buf;
	time_t sent = time(NULL);

	g_return_val_if_fail(log != NULL, NULL);
	g_return_val_if_fail(log->format != LOG_FORMAT_NONE, NULL);

	logs_path = (LOG_FORMAT_RAW == log->format) ? EKG_RAW_LOGS_PATH : config_logs_path;

	g_return_val_if_fail(logs_path != NULL, NULL);

	buf = g_string_new(NULL);
	s = session_find(log->session);

	while (*logs_path) {
		if (*logs_path == '%' && *(logs_path+1) != '\0') {
			char *append = NULL;
			logs_path++;
			switch (*logs_path) {
				case 'S':	append = g_strdup(s ? s->uid : "_null_");
						break;
				case 'P':	append = g_strdup(config_profile ? config_profile : "_default_");
						break;
				case 'U':
				case 'u':	append = g_strdup(*logs_path=='u' ? log->uid : get_nickname(s, log->uid));
						if (!append)
							append = g_strdup(log->uid);
						break;
				case 'Y':	if (!tm) tm = localtime(&sent);
						append = g_strdup_printf("%4d", tm->tm_year+1900);
						break;
				case 'M':	if (!tm) tm = localtime(&sent);
						append = g_strdup_printf("%02d", tm->tm_mon+1);
						break;
				case 'D':	if (!tm) tm = localtime(&sent);
						append = g_strdup_printf("%02d", tm->tm_mday);
						break;
				default:	g_string_append_c(buf, *logs_path);
			};
			if (append) {
				// XXX g_uri_escape_string( , , allow_utf8) ?
				char *tmp = g_uri_escape_string(append, G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT, TRUE);
				g_string_append(buf, tmp);
				g_free(tmp);
				g_free(append);
			}

		} else if (*logs_path == '~' && (*(logs_path+1) == '/' || *(logs_path+1) == '\0')) {
			g_string_append(buf, home_dir);
		} else
			string_append_c(buf, *logs_path);
		logs_path++;
	};

	switch (log->format) {
		case LOG_FORMAT_RAW:	g_string_append(buf, ".raw");	break;
		case LOG_FORMAT_SIMPLE:	g_string_append(buf, ".txt");	break;
		case LOG_FORMAT_IRSSI:	g_string_append(buf, ".log");	break;
		case LOG_FORMAT_XML:	g_string_append(buf, ".xml");	break;
	}

	// TODO sanityzacja sciezki - wywalic "../",
	xstrtr(buf->str, ' ', '_');

	return g_string_free(buf, FALSE);
}
示例#7
0
文件: main.c 项目: hiciu/ekg2
static char *fstring_reverse(fstring_t *fstr) {
	const char *str;
	const fstr_attr_t *attr;
	string_t asc;
	int i;

	if (!fstr)
		return NULL;

	attr = fstr->attr;
	str = fstr->str;

	if (!attr || !str)
		return NULL;

	asc = string_init(NULL);

	for (i = 0; str[i]; i++) {
#define prev	attr[i-1]
#define cur	attr[i]
		int reset = 0;

		if (i) {
			if (!(cur & FSTR_BOLD) && (prev & FSTR_BOLD))		reset = 1;
			if (!(cur & FSTR_BLINK) && (prev & FSTR_BLINK))		reset = 1;
			if (!(cur & FSTR_UNDERLINE) && (prev & FSTR_UNDERLINE))	reset = 1;
			if (!(cur & FSTR_REVERSE) && (prev & FSTR_REVERSE))	reset = 1;
			if ((cur & FSTR_NORMAL) && !(prev & FSTR_NORMAL))	reset = 1;	/* colors disappear */

			if (reset)
				string_append(asc, "%n");
		} else
			reset = 1;

	/* attr */
		if ((cur & FSTR_BLINK) &&	(reset || !(prev & FSTR_BLINK)))	string_append(asc, "%i");
//		if ((cur & FSTR_UNDERLINE) &&	(reset || !(prev & FSTR_UNDERLINE)))	string_append(asc, "%");
//		if ((cur & FSTR_REVERSE) &&	(reset || !(prev & FSTR_REVERSE)))	string_append(asc, "%");

		if (!(cur & FSTR_NORMAL)) {
		/* background color XXX */
#define BGCOLOR(x)	-1
			if (0 && ((reset || BGCOLOR(cur) != BGCOLOR(prev)))) {
				string_append_c(asc, '%');
				switch (BGCOLOR(cur)) {
					case (0): string_append_c(asc, 'l'); break;
					case (1): string_append_c(asc, 's'); break;
					case (2): string_append_c(asc, 'h'); break;
					case (3): string_append_c(asc, 'z'); break;
					case (4): string_append_c(asc, 'e'); break;
					case (5): string_append_c(asc, 'q'); break;
					case (6): string_append_c(asc, 'd'); break;
					case (7): string_append_c(asc, 'x'); break;
				}
			}
#undef BGCOLOR

		/* foreground color */
#define FGCOLOR(x)	((!(x & FSTR_NORMAL)) ? (x & FSTR_FOREMASK) : -1)
			if (((reset || FGCOLOR(cur) != FGCOLOR(prev)) || (i && (prev & FSTR_BOLD) != (cur & FSTR_BOLD)))) {
				string_append_c(asc, '%');
				switch ((cur & FSTR_FOREMASK)) {
					case (0): string_append_c(asc, (cur & FSTR_BOLD) ? 'K' : 'k'); break;
					case (1): string_append_c(asc, (cur & FSTR_BOLD) ? 'R' : 'r'); break;
					case (2): string_append_c(asc, (cur & FSTR_BOLD) ? 'G' : 'g'); break;
					case (3): string_append_c(asc, (cur & FSTR_BOLD) ? 'Y' : 'y'); break;
					case (4): string_append_c(asc, (cur & FSTR_BOLD) ? 'B' : 'b'); break;
					case (5): string_append_c(asc, (cur & FSTR_BOLD) ? 'M' : 'm'); break; /* | fioletowy	 | %m/%p  | %M/%P | %q	| */
					case (6): string_append_c(asc, (cur & FSTR_BOLD) ? 'C' : 'c'); break;
					case (7): string_append_c(asc, (cur & FSTR_BOLD) ? 'W' : 'w'); break;
				}
			}
#undef FGCOLOR
		} else {	/* no color */
			if ((cur & FSTR_BOLD) && (reset || !(prev & FSTR_BOLD)))
				string_append(asc, "%T");
		}

	/* str */
		if (str[i] == '%' || str[i] == '\\')
			string_append_c(asc, '\\');
		string_append_c(asc, str[i]);
	}

/* reset, and return. */
	string_append(asc, "%n");
	return string_free(asc, 0);

#undef prev
#undef cur
}
示例#8
0
文件: http.c 项目: andyn/httpdns
/**
 * HTTP PUT buffer contents to a remote URL
 * @param url URL to PUT the contents to
 * @param buf Buffer whose contents to send
 * @param count Size of buffer (number of bytes to send)
 * @return HTTP status code, 200 for OK. See the HTTP RFC for details.
 */
int http_put_buf(const char *url, const void *buf, size_t count) {
	char *host = url_get_field(url, URL_HOST);
	char *port = url_get_field(url, URL_PORT);
	char *path = url_get_field(url, URL_PATH);

	int response_code = -1;

	if (host && port && path) {
		String *buffer = string_new("");

		string_append_c(buffer, "PUT ");
		string_append_c(buffer, path);
		string_append_c(buffer, " HTTP/1.1\r\n");

		string_append_c(buffer, "Host: ");
		string_append_c(buffer, host);
		string_append_c(buffer, "\r\n");

		string_append_c(buffer, "Content-type: text/plain\r\n");

		if (count > 0) {
			char length_str[16];
			snprintf(length_str, sizeof length_str, "%zu", count);
			string_append_c(buffer, "Content-length: ");
			string_append_c(buffer, length_str);
			string_append_c(buffer, "\r\n");
		}

		string_append_c(buffer, "Connection: close\r\n");
		string_append_c(buffer, "Iam: anilakar\r\n");

		string_append_c(buffer, "\r\n");

		int http_socket = socket_tcp_connect(host, port);
		if (http_socket >= 0) {
			if (-1 == socket_write(http_socket, buffer->c_str, buffer->size) ||
				-1 == socket_write(http_socket, buf, count)) {
				VERBOSE("Could not write to HTTP server.");
			} else {
				char response[512]; // Need only the first line
				int r = socket_read(http_socket, response, sizeof response - 1);
				response[r] = '\0';
				sscanf(response, "HTTP/1.1 %d", &response_code);				
			}
		}
		socket_close(&http_socket);
		string_delete(buffer);

	}
	else {
		VERBOSE("Malformed URL: %s", url);
	}

	free(path);
	free(port);
	free(host);

	VERBOSE("HTTP Response: %d", response_code);
	return response_code;
}
示例#9
0
/*
 * ui_readline_print()
 *
 * wy¶wietla dany tekst na ekranie, uwa¿aj±c na trwaj±ce w danych chwili
 * readline().
 */
void ui_readline_print(window_t *w, int separate, const /*locale*/ char *xline)
{
	int old_end = rl_end, id = w->id;
	char *old_prompt = NULL, *line_buf = NULL;
	const char *line = NULL;

	if (config_timestamp) {
			/* XXX: recode timestamp? for fun or wcs? */
		string_t s = string_init(NULL);
		const char *p = xline;
		const char *buf = timestamp(formated_config_timestamp);

		string_append(s, "\033[0m");
		string_append(s, buf);
		string_append_c(s, ' ');
		
		while (*p) {
			string_append_c(s, *p);
			if (*p == '\n' && *(p + 1)) {
				string_append(s, buf);
				string_append_c(s, ' ');
			}
			p++;
		}

		line = line_buf = string_free(s, 0);
	} else
		line = xline;

	/* je¶li nie piszemy do aktualnego, to zapisz do bufora i wyjd¼ */
	if (id != window_current->id) {
		window_write(id, line);

		/* XXX trzeba jeszcze waln±æ od¶wie¿enie prompta */
		goto done;
	}

	/* je¶li mamy ukrywaæ wszystko, wychodzimy */
	if (pager_lines == -2)
		goto done;

	window_write(window_current->id, line);

	/* ukryj prompt, je¶li jeste¶my w trakcie readline */
	if (in_readline) {
		old_prompt = g_strdup(rl_prompt);
		rl_end = 0;
/*		set_prompt(NULL);*/
		rl_redisplay();
			/* XXX: string width instead?
			 * or cleartoeol? */
		printf("\r%*c\r", (int) xstrlen(old_prompt), ' ');
	}

	printf("%s", line);

	if (pager_lines >= 0) {
		pager_lines++;

		if (pager_lines >= screen_lines - 2) {
			const gchar *prompt = format_find("readline_more");
			char *lprompt = ekg_recode_to_locale(prompt);
			char *tmp;
				/* XXX: lprompt pretty const, make it static? */

			in_readline++;

			set_prompt(lprompt);

			pager_lines = -1;
			tmp = readline(lprompt);
			g_free(lprompt);
			in_readline--;
			if (tmp) {
				free(tmp); /* allocd by readline */
				pager_lines = 0;
			} else {
				printf("\n");
				pager_lines = -2;
			}
			printf("\033[A\033[K");		/* XXX brzydko */
		}
	}

	/* je¶li jeste¶my w readline, poka¿ z powrotem prompt */
	if (in_readline) {
		rl_end = old_end;
		set_prompt(old_prompt);
		g_free(old_prompt);
		rl_forced_update_display();
	}
	
done:
	if (line_buf)
		g_free(line_buf);
}
示例#10
0
文件: misc.c 项目: hiciu/ekg2
static void irc_access_parse(session_t *s, channel_t *chan, people_t *p, int flags) {
	userlist_t *ul;

	if (!s || !chan || !p)
		return;

#define dchar(x) debug("%c", x);

	for (ul = s->userlist; ul; ul = ul->next) {
		userlist_t *u = ul;
		ekg_resource_t *r = NULL, *rl;

		int i, j;

		if (!p->ident || !p->host) continue;

		if (xstrncmp(u->uid, "irc:", 4)) continue;	/* check for irc: */

		for (rl = u->resources; rl; rl = rl->next) {
			r = rl;

			if (r->priv_data == p) {
				const char *tmp = &(u->uid[4]);

				/* fast forward move.. */
				if (!(tmp = xstrchr(tmp, '!')) || !(tmp = xstrchr(tmp, '@')) || !(tmp = xstrchr(tmp, ':'))) {
					debug_error("%s:%d INTERNAL ERROR\n", __FILE__, __LINE__);
					goto next3;
				}
				tmp++;
				i = (tmp - u->uid);
				debug("irc, checkchan: %s\n", tmp);
				goto skip_identhost_check;
				break;	/* never here */
			}
		}
		r = NULL;
/* parse nick! */
		for (i = 4, j = 0; (u->uid[i] != '\0' && u->uid[i] != '!'); i++, j++) {
			dchar(u->uid[i]);

			if (u->uid[i] != p->ident[j]) {
				if (u->uid[i] == '*') j += do_sample_wildcard_match(&u->uid[i+1], &p->ident[j], '!');
				else if (u->uid[i] == '?') continue;
				else goto next;
			}
		} if (!u->uid[i]) goto next;
		dchar('!');
		i++;

/* parse ident@ */
		for (j = 0; (u->uid[i] != '\0' && u->uid[i] != '@'); i++, j++) {
			dchar(u->uid[i]);

			if (u->uid[i] != p->nick[j]) {
				if (u->uid[i] == '*') j += do_sample_wildcard_match(&u->uid[i+1], &p->ident[j], '@');
				else if (u->uid[i] == '?') continue;
				else goto next;
			}
		} if (!u->uid[i]) goto next;
		dchar('@');
		i++;

/* parse host: */
		for (j = 0; (u->uid[i] != '\0' && u->uid[i] != ':'); i++, j++) {
			dchar(u->uid[i]);

			if (u->uid[i] != p->host[j]) {
				if (u->uid[i] == '*') j += do_sample_wildcard_match(&u->uid[i+1], &p->ident[j], ':');
				else if (u->uid[i] == '?') continue;
				else goto next;
			}
		} if (!u->uid[i]) goto next;
		dchar('\n');
		i++;

		debug_error("irc_access_parse() %s!%s@%s MATCH with %s\n", p->ident, p->nick, p->host, u->uid+4);

skip_identhost_check:
/* let's rock with channels */
		{
			char **arr = array_make(&u->uid[i], ",", 0, 1, 0);

			int ismatch = 0;


			for (i=0; arr[i]; i++) {
				int k;
				debug_error("CHAN%d: %s: ", i, arr[i]);

				for (j = 0, k = 4 /* skip irc: */; arr[i][j]; j++, k++) {
					if (arr[i][j] != chan->name[k]) {
						if (arr[i][j] == '*') k += do_sample_wildcard_match(&arr[i][j+1], &chan->name[k], '\0');
						else if (arr[i][j] == '?') continue;
						else goto next2;
					}
				}
				if (chan->name[k] != '\0')
					goto next2;

				ismatch = 1;
				debug_error("MATCH\n");
				break;
next2:
				debug_error("NOT MATCH\n");
				continue;

			}
			g_strfreev(arr);

			if (!ismatch) continue;
		}
		if (!r) {
			char *tmp = irc_uid(p->nick);
			r = userlist_resource_add(u, tmp, 0);
			g_free(tmp);

			r->status	= EKG_STATUS_AVAIL;
			r->descr	= xstrdup(chan->name+4);
			r->priv_data	= p;

			if (u->status != EKG_STATUS_AVAIL) {
				xfree(u->descr);
				u->status	= EKG_STATUS_AVAIL;
				u->descr	= xstrdup("description... ?");
				query_emit(NULL, "userlist-changed", &(s->uid), &(u->uid));
			}
		} else {
			string_t str = string_init(r->descr);

			string_append_c(str, ',');
			string_append(str, chan->name+4);

			xfree(r->descr); r->descr = string_free(str, 0);
		}

		/* tutaj ladnie by wygladalo jakbysmy wywolali protocol-status.. ale jednak to jest b. kiepski pomysl */
		debug_error("USER: 0x%x PERMISION GRANTED ON CHAN: 0x%x\n", u, chan);
		continue;

next:
		dchar('\n');
		debug_error("irc_access_parse() %s!%s@%s NOT MATCH with %s\n", p->ident, p->nick, p->host, u->uid+4);
next3:
		continue;
	}
#undef dchar
}
示例#11
0
static _slot_info_ptr _choose(cptr verb, int options)
{
    _slot_info_ptr result = NULL;
    int            slot = 0;
    int            cmd;
    rect_t         r = _menu_rect();
    string_ptr     prompt = NULL;
    bool           done = FALSE;
    bool           exchange = FALSE;
    int            slot1 = _INVALID_SLOT, slot2 = _INVALID_SLOT;

    if (REPEAT_PULL(&cmd))
    {
        slot = A2I(cmd);
        if (0 <= slot && slot < _MAX_SLOTS)
            return &_spells[slot];
    }

    prompt = string_alloc();
    screen_save();
    while (!done)
    {
        string_clear(prompt);

        if (exchange)
        {
            if (slot1 == _INVALID_SLOT)
                string_append_s(prompt, "Select the first spell:");
            else
                string_append_s(prompt, "Select the second spell:");
        }
        else
        {
            string_printf(prompt, "%s which spell", verb);
            if (options & _ALLOW_EXCHANGE)
                string_append_s(prompt, " [Press 'X' to Exchange]");
            string_append_c(prompt, ':');
        }
        prt(string_buffer(prompt), 0, 0);
        _display(r, options);

        cmd = inkey_special(FALSE);

        if (cmd == ESCAPE || cmd == 'q' || cmd == 'Q')
            done = TRUE;

        if (options & _ALLOW_EXCHANGE)
        {
            if (!exchange && (cmd == 'x' || cmd == 'X'))
            {
                exchange = TRUE;
                slot1 = slot2 = _INVALID_SLOT;
            }
        }

        if ('a' <= cmd && cmd < 'a' + _MAX_SLOTS)
        {
            slot = A2I(cmd);
            if (exchange)
            {
                if (slot1 == _INVALID_SLOT)
                    slot1 = slot;
                else
                {
                    slot2 = slot;
                    if (slot1 != slot2)
                    {
                        _slot_info_t  tmp = _spells[slot1];
                        _spells[slot1] = _spells[slot2];
                        _spells[slot2] = tmp;
                    }
                    exchange = FALSE;
                    slot1 = slot2 = _INVALID_SLOT;
                }
            }
            else
            {
                if (_spells[slot].realm != REALM_NONE || (options & _ALLOW_EMPTY))
                {
                    result = &_spells[slot];
                    done = TRUE;
                }
            }
        }
    }

    if (result)
    {
        REPEAT_PUSH(I2A(slot));
    }

    screen_load();
    string_free(prompt);
    return result;
}
示例#12
0
/*
 * watch_handle_line()
 *
 * obs³uga deskryptorów przegl±danych WATCH_READ_LINE.
 */
void watch_handle_line(watch_t *w)
{
	char buf[1024], *tmp;
	int ret, res = 0;
	int (*handler)(int, int, const char *, void *) = w->handler;

	if (!w || w->removed == -1)
		return;	/* watch is running in another thread / context */

	w->removed = -1;
#ifndef NO_POSIX_SYSTEM
	ret = read(w->fd, buf, sizeof(buf) - 1);
#else
	ret = recv(w->fd, buf, sizeof(buf) - 1, 0);
	if (ret == -1 && WSAGetLastError() == WSAENOTSOCK) {
		printf("recv() failed Error: %d, using ReadFile()", WSAGetLastError());
		res = ReadFile(w->fd, &buf, sizeof(buf)-1, &ret, NULL);
		printf(" res=%d ret=%d\n", res, ret);
	}
	res = 0;
#endif

	if (ret > 0) {
		buf[ret] = 0;
		string_append(w->buf, buf);
#ifdef NO_POSIX_SYSTEM
		printf("RECV: %s\n", buf);
#endif
	}

	if (ret == 0 || (ret == -1 && errno != EAGAIN))
		string_append_c(w->buf, '\n');

	while ((tmp = xstrchr(w->buf->str, '\n'))) {
		size_t strlen = tmp - w->buf->str;		/* get len of str from begining to \n char */
		char *line = xstrndup(w->buf->str, strlen);	/* strndup() str with len == strlen */

		/* we strndup() str with len == strlen, so we don't need to call xstrlen() */
		if (strlen > 1 && line[strlen - 1] == '\r')
			line[strlen - 1] = 0;

		if ((res = handler(0, w->fd, line, w->data)) == -1) {
			xfree(line);
			break;
		}

		string_remove(w->buf, strlen + 1);

		xfree(line);
	}

	/* je¶li koniec strumienia, lub nie jest to ci±g³e przegl±danie,
	 * zwolnij pamiêæ i usuñ z listy */
	if (res == -1 || ret == 0 || (ret == -1 && errno != EAGAIN) || w->removed == 1) {
		int fd = w->fd;
		w->removed = 0;

		watch_free(w);
		close(fd);
		return;
	} 
	w->removed = 0;
}
示例#13
0
文件: vars.c 项目: porridge/ekg
/*
 * variable_help()
 *
 * wy¶wietla pomoc dotycz±c± danej zmiennej na podstawie pliku
 * ${datadir}/ekg/vars.txt.
 *
 *  - name - nazwa zmiennej.
 */
void variable_help(const char *name)
{
	FILE *f = fopen(DATADIR "/vars.txt", "r");
	char *line, *type = NULL, *def = NULL, *tmp;
	string_t s = string_init(NULL);
	int found = 0;

	if (!f) {
		print("help_set_file_not_found");
		return;
	}

	while ((line = read_file(f))) {
		if (strlen(line) >= 2 && line[0] == '/' && line[1] == '/') {
			xfree(line);
			continue;
		}

		if (!strcasecmp(line, name)) {
			found = 1;
			xfree(line);
			break;
		}

		xfree(line);
	}

	if (!found) {
		fclose(f);
		print("help_set_var_not_found", name);
		return;
	}

	line = read_file(f);
	
	if ((tmp = strstr(line, ": ")))
		type = xstrdup(tmp + 2);
	else
		type = xstrdup("?");
	
	xfree(line);

	tmp = NULL;
	
	line = read_file(f);
	if ((tmp = strstr(line, ": ")))
		def = xstrdup(tmp + 2);
	else
		def = xstrdup("?");
	xfree(line);

	print("help_set_header", name, type, def);

	xfree(type);
	xfree(def);

	if (tmp)			/* je¶li nie jest to ukryta zmienna... */
		xfree(read_file(f));	/* ... pomijamy liniê */

	while ((line = read_file(f))) {
		if (line[0] != '\t') {
			xfree(line);
			break;
		}

		if (!strncmp(line, "\t- ", 3) && strcmp(s->str, "")) {
			print("help_set_body", s->str);
			string_clear(s);
		}
		
		string_append(s, line + 1);

		if (line[strlen(line) - 1] != ' ')
			string_append_c(s, ' ');

		xfree(line);
	}

	if (strcmp(s->str, ""))
		print("help_set_body", s->str);

	string_free(s, 1);
	
	if (strcmp(format_find("help_set_footer"), ""))
		print("help_set_footer", name);

	fclose(f);
}
示例#14
0
/*
 * variable_help()
 *
 * it shows help about variable from file ${datadir}/ekg/vars.txt
 * or ${datadir}/ekg/plugins/{plugin_name}/vars.txt
 *
 * name - name of the variable
 */
void variable_help(const char *name) {
	GIOChannel *f; 
	gchar *type = NULL, *def = NULL, *tmp;
	const gchar *line, *seeking_name;
	string_t s;
	int found = 0;
	variable_t *v = variable_find(name);

	if (!v) {
		print("variable_not_found", name);
		return;
	}

	if (v->plugin && v->plugin->name) {
		char *tmp2;

		if (!(f = help_open("vars", v->plugin->name))) {
			print("help_set_file_not_found_plugin", v->plugin->name);
			return;
		}

		tmp2 = xstrchr(name, ':');
		if (tmp2)
			seeking_name = tmp2+1;
		else
			seeking_name = name;
	} else {
		if (!(f = help_open("vars", NULL))) {
			print("help_set_file_not_found");
			return;
		}
		
		seeking_name = name;
	}

	while ((line = read_line(f))) {
		if (!xstrcasecmp(line, seeking_name)) {
			found = 1;
			break;
		}
	}

	if (!found) {
		g_io_channel_unref(f);
		print("help_set_var_not_found", name);
		return;
	}

	line = read_line(f);
	
	if ((tmp = xstrstr(line, (": "))))
		type = xstrdup(tmp + 2);
	else
		type = xstrdup(("?"));
	
	line = read_line(f);
	if ((tmp = xstrstr(line, (": "))))
		def = xstrdup(tmp + 2);
	else
		def = xstrdup(("?"));

	print("help_set_header", name, type, def);

	xfree(type);
	xfree(def);

	if (tmp)		/* je¶li nie jest to ukryta zmienna... */
		read_line(f);	/* ... pomijamy liniê */
	s = string_init(NULL);
	while ((line = read_line(f))) {
		if (line[0] != '\t')
			break;

		if (!xstrncmp(line, ("\t- "), 3) && xstrcmp(s->str, (""))) {
			print("help_set_body", s->str);
			string_clear(s);
		}

		if (!xstrncmp(line, ("\t"), 1) && xstrlen(line) == 1) {
			string_append(s, ("\n\r"));
			continue;
		}
	
		string_append(s, line + 1);

		if (line[xstrlen(line) - 1] != ' ')
			string_append_c(s, ' ');
	}

	if (xstrcmp(s->str, ("")))
		print("help_set_body", s->str);

	string_free(s, 1);
	
	if (format_exists("help_set_footer"))
		print("help_set_footer", name);

	g_io_channel_unref(f);
}