示例#1
0
文件: window.c 项目: jetlive/xynth
int s_window_new (s_window_t *window, s_window_type_t type, s_window_t *parent)
{
	if (!(type & (WINDOW_TYPE_MAIN | WINDOW_TYPE_CHILD | WINDOW_TYPE_TEMP | WINDOW_TYPE_POPUP))) {
		goto err;
	}
	window->type = type;
	if (window->surface->width <= 0) {
		window->surface->width = window->surface->linear_buf_width;
	}
	if (window->surface->height <= 0) {
		window->surface->height = window->surface->linear_buf_height;
	}
	if (window->type & (WINDOW_TYPE_TEMP | WINDOW_TYPE_CHILD)) {
		window->parent = parent;
	} else {
		window->parent = NULL;
	}

	s_socket_request(window, SOC_DATA_NEW);
	window->surface->vbuf = (unsigned char *) s_calloc(sizeof(char), window->surface->width *
	                                                                 window->surface->height *
	                                                                 window->surface->bytesperpixel);

	return 0;
err:	return 1;
}
示例#2
0
文件: lsyncd.c 项目: dreiss/lsyncd
/*
| Adds a logging category
|
| Returns true if OK.
*/
static bool
add_logcat( const char *name, int priority )
{
	struct logcat *lc;

	if( !strcmp( "all", name ) )
	{
		settings.log_level = 99;
		return true;
	}

	if( !strcmp( "scarce", name ) )
	{
		settings.log_level = LOG_WARNING;
		return true;
	}

	// categories must start with a capital letter.
	if( name[ 0 ] < 'A' || name[ 0 ] > 'Z' )
		{ return false; }

	if( !logcats[ name[ 0 ]- 'A' ] )
	{
		// an empty capital letter
		lc = logcats[name[0]-'A'] = s_calloc(2, sizeof(struct logcat));
	}
	else
	{
		// length of letter list
		int ll = 0;

		// counts list length
		for( lc = logcats[name[0]-'A']; lc->name; lc++ )
			{ ll++; }

		// enlarges list
		logcats[ name[ 0 ] - 'A'] =
			s_realloc(
				logcats[ name[ 0 ]-'A' ],
				( ll + 2 ) * sizeof( struct logcat )
			);

		// goes to the list end
		for( lc = logcats[ name[ 0 ] - 'A']; lc->name; lc++ )
		{
			if( !strcmp( name, lc->name ) )
			{
				// already there
				return true;
			}
		}
	}

	lc->name = s_strdup( name );
	lc->priority = priority;

	// terminates the list
	lc[ 1 ].name = NULL;
	return true;
}
示例#3
0
文件: eventq.c 项目: jetlive/xynth
int s_eventq_init (s_window_t *window)
{
	window->eventq = (s_eventq_t *) s_calloc(1, sizeof(s_eventq_t));
	window->eventq->queue = (s_list_t *) s_calloc(1, sizeof(s_list_t));
	if (s_thread_cond_init(&window->eventq->cond)) {
		goto err0;
	}
	if (s_thread_mutex_init(&window->eventq->mut)) {
		goto err1;
	}
        return 0;

err1:	s_thread_cond_destroy(window->eventq->cond);
err0:	s_free(window->eventq->queue);
	s_free(window->eventq);
	return 1;
}
示例#4
0
文件: window.c 项目: d33tah/whitix
void s_server_window_title (int id, char *title)
{
	int i;
        int v;
        int h;
        int yo;
	s_font_t *font;
	s_surface_t *srf;

        if ((id < 0) ||
	    (title == NULL) ||
	    (server->client[id].type & WINDOW_NOFORM)) {
		return;
	}
	
        for (v = 0; v < 2; v++) {
		font = server->theme.font[v];
		s_font_set_str(font, title);
		s_font_get_glyph(font);
		s_image_get_mat(font->glyph.img);
		s_image_get_handler(font->glyph.img);

		font->glyph.img->buf = (char *) s_calloc(1, font->glyph.img->w * font->glyph.img->h * server->window->surface->bytesperpixel);
		if (s_surface_create_from_data(&srf, font->glyph.img->w, font->glyph.img->h, server->window->surface->bitsperpixel, font->glyph.img->buf)) {
			goto out;
		}

		if ((i = font->glyph.img->w / server->theme.form[v][TOP_3].w) > 0) {
			while (i--) {
				yo = server->theme.text_v_off[v] - font->glyph.img->handler->y + (server->theme.form[v][TOP_3].handler->h - font->glyph.img->handler->h) / 2;
				if ((font->glyph.img->h + yo) > server->theme.form[v][TOP_3].h) {
					h = server->theme.form[v][TOP_3].h - yo;
				} else {
					h = font->glyph.img->h;
				}
				s_putboxpart(srf, i * server->theme.form[v][TOP_3].w, 0, server->theme.form[v][TOP_3].w, h, server->theme.form[v][TOP_3].w, server->theme.form[v][TOP_3].h, server->theme.form[v][TOP_3].buf, 0, yo);
			}
		}
		if ((i = font->glyph.img->w % server->theme.form[v][TOP_3].w) > 0) {
				s_putboxpart(srf, font->glyph.img->w - server->theme.form[v][TOP_3].w, 0, server->theme.form[v][TOP_3].w, font->glyph.img->h, server->theme.form[v][TOP_3].w, server->theme.form[v][TOP_3].h, server->theme.form[v][TOP_3].buf, 0, server->theme.text_v_off[v] - font->glyph.img->handler->y + (server->theme.form[v][TOP_3].handler->h - font->glyph.img->handler->h) / 2);
		}
		s_putboxrgba(srf, 0, 0, font->glyph.img->w, font->glyph.img->h, font->glyph.img->rgba);

		s_free(server->client[id].title.img[v].mat);
		s_free(server->client[id].title.img[v].buf);
		server->client[id].title.img[v].mat = (unsigned char *) s_malloc(font->glyph.img->w * font->glyph.img->h);
		server->client[id].title.img[v].buf = (char *) s_malloc(font->glyph.img->w * font->glyph.img->h * server->window->surface->bytesperpixel);
		server->client[id].title.img[v].w = font->glyph.img->w;
		server->client[id].title.img[v].h = font->glyph.img->h;
		server->client[id].title.hy[v] = font->glyph.img->handler->y;
		server->client[id].title.hh[v] = font->glyph.img->handler->h;
		memcpy(server->client[id].title.img[v].mat, font->glyph.img->mat, font->glyph.img->w * font->glyph.img->h);
		memcpy(server->client[id].title.img[v].buf, font->glyph.img->buf, font->glyph.img->w * font->glyph.img->h * server->window->surface->bytesperpixel);
		s_surface_destroy(srf);
out:		s_image_uninit(font->glyph.img);
		s_image_init(&(font->glyph.img));
	}
}
示例#5
0
文件: sprite.c 项目: Super-Man/seal2d
struct sprite_frame* sprite_frame_new(const char* key) {
    struct sprite_frame* f = STRUCT_NEW(sprite_frame);
    memset(f, 0, sizeof(struct sprite_frame));
    int len = strlen(key);
    f->key = s_calloc(len+1);
    strcpy(f->key, key);
    
    sprite_frame_cache_add(GAME->sprite_frame_cache, f);
    return f;
}
示例#6
0
/*
 * Simply allocate a new line and copy in cmd + sep + arg
 */
void
update_line(line_t *linep)
{
	size_t		size;
	const char	*fcn = "update_line()";

	BAM_DPRINTF(("%s: line before update: %s\n", fcn, linep->line));
	free(linep->line);
	size = strlen(linep->cmd) + strlen(linep->sep) + strlen(linep->arg) + 1;
	linep->line = s_calloc(1, size);
	(void) snprintf(linep->line, size, "%s%s%s", linep->cmd, linep->sep,
	    linep->arg);
	BAM_DPRINTF(("%s: line after update: %s\n", fcn, linep->line));
}
示例#7
0
/*
 * Simply allocate a new line and copy in cmd + sep + arg
 */
void
update_line(line_t *linep)
{
	size_t		size;
	const char	*fcn = "update_line()";

	BAM_DPRINTF((D_UPDATE_LINE_BEFORE, fcn, linep->line));
	free(linep->line);
	size = strlen(linep->cmd) + strlen(linep->sep) + strlen(linep->arg) + 1;
	linep->line = s_calloc(1, size);
	(void) snprintf(linep->line, size, "%s%s%s", linep->cmd, linep->sep,
	    linep->arg);
	BAM_DPRINTF((D_UPDATE_LINE_AFTER, fcn, linep->line));
}
示例#8
0
文件: pollfd.c 项目: jetlive/xynth
int s_pollfds_init (s_window_t *window)
{
	window->pollfds = (s_pollfds_t *) s_calloc(1, sizeof(s_pollfds_t));
	if (s_list_init(&(window->pollfds->list))) {
		goto err0;
	}
	if (s_thread_mutex_init(&(window->pollfds->mut))) {
		goto err1;
	}
	return 0;
err1:	s_list_uninit(window->pollfds->list);
err0:	s_free(window->pollfds);
	return -1;
}
示例#9
0
文件: window.c 项目: jetlive/xynth
int s_window_init (s_window_t **window)
{
	s_window_t *w;

	w = (s_window_t *) s_calloc(1, sizeof(s_window_t));
	if (s_pollfds_init(w))       { goto err1; }
	if (s_timers_init(w))        { goto err2; }
	if (s_socket_init(w))        { goto err3; }
        if (s_handlers_init(w))      { goto err4; }
	if (s_childs_init(w))        { goto err5; }
	if (s_eventq_init(w))        { goto err6; }
	if (s_event_init(&w->event)) { goto err7; }

	w->running = 1;
	w->id = -1;
	w->pri = -1;
	w->mapped = -1;
	w->viewable = -1;
	w->resizeable = 1;
	w->alwaysontop = 0;
	w->cursor = CURSOR_TYPE_ARROW;
	w->mouse_entered = 0;
	*window = w;

	if (s_surface_init(w))                     { goto err8; }
	if (s_socket_request(w, SOC_DATA_DISPLAY)) { goto err9; }
	if (s_surface_attach(w))                   { goto err10; }

	return 0;
err10:
err9:	s_surface_uninit(w);
err8:	s_event_uninit(w->event);
err7:	s_eventq_uninit(w);
err6:	s_childs_uninit(w);
err5:	s_handlers_uninit(w);
err4:
err3:	s_timers_uninit(w);
err2:	s_pollfds_uninit(w);
err1:	s_free(w);
	debugf(DCLI | DFAT, "Cannot connect to server");
	return -1;
}
示例#10
0
文件: ipc.c 项目: artfwo/serialosc
static int
read_strdata(int fd, size_t n, ...)
{
	const char **cur;
	uint16_t magic;
	size_t slen;
	va_list ap;
	char *buf;

	va_start(ap, n);

	while (n--) {
		cur = va_arg(ap, const char **);
		*cur = NULL;

		if (read(fd, &slen, sizeof(slen)) < sizeof(slen))
			return -1;

		if (!(buf = s_calloc(slen + 1, sizeof(char)))) {
			/* XXX: proper error handling? i.e. what do we do with the
			        strings that are already allocated? we can't NULL
			        all of them at the start, I guess that's up to the
					caller? */
			continue;
		}

		if (read(fd, buf, slen) < slen) {
			s_free(buf);
			return -1;
		}

		*cur = buf;

		if (read(fd, &magic, sizeof(magic)) < sizeof(magic)
			|| magic != IPC_MAGIC)
			return -1;
	}

	va_end(ap);

	return 0;
}
示例#11
0
文件: image.c 项目: anarsoul/libxynth
int s_image_get_mat (s_image_t *img)
{
        int i;
        unsigned int *d;
        unsigned char *m;

        if (img->mat != NULL) {
		s_image_free_mat(img);
	}
	
        d = img->rgba;
        i = img->w * img->h;
        
	img->mat = (unsigned char *) s_calloc(i, sizeof(char));

	m = img->mat;
	while (i--) {
		*m++ = (~*d++ & 0xFF);
	}

	return 0;
}
示例#12
0
__nis_table_mapping_t *
new_merged_mapping(const char *match,
	__nis_table_mapping_t	*intbl)
{

	__nis_table_mapping_t	*outtable = NULL;

	outtable = (__nis_table_mapping_t *)
		s_calloc(1, sizeof (__nis_table_mapping_t));
	if (outtable == NULL)
		return (NULL);
	initialize_table_mapping(outtable);
	outtable->dbId = s_strndup(match, strlen(match));
	if (outtable->dbId == NULL) {
		free_table_mapping(outtable);
		outtable = NULL;
		return (NULL);
	}
	if (merge_table_mapping(intbl, outtable)) {
		free_table_mapping(outtable);
		outtable = NULL;
	}
	return (outtable);
}
示例#13
0
文件: event.c 项目: d33tah/whitix
int s_event_init (s_event_t **event)
{
	(*event) = (s_event_t *) s_calloc(1, sizeof(s_event_t));
	(*event)->mouse = (s_mouse_t *) s_calloc(1, sizeof(s_mouse_t));
	(*event)->keybd = (s_keybd_t *) s_calloc(1, sizeof(s_keybd_t));
	(*event)->expose = (s_expose_t *) s_calloc(1, sizeof(s_expose_t));
	(*event)->expose->rect = (s_rect_t *) s_calloc(1, sizeof(s_rect_t));
	(*event)->desktop = (s_desktop_t *) s_calloc(1, sizeof(s_desktop_t));
	if (s_list_init(&((*event)->desktop->clients))) {
		goto err0;
	}
	return 0;
err0:	s_free((*event)->desktop);
	s_free((*event)->expose);
	s_free((*event)->keybd);
	s_free((*event)->mouse);
	s_free(*event);
	return -1;
}
示例#14
0
文件: pollfd.c 项目: jetlive/xynth
int s_pollfd_init (s_pollfd_t **pfd)
{
	(*pfd) = (s_pollfd_t *) s_calloc(1, sizeof(s_pollfd_t));
        (*pfd)->fd = -1;
	return 0;
}
示例#15
0
/*
 * Actually processes events; returns a reply event
 */
static void
process_event(int cmd, int seq_num, nvlist_t *nvl, nvlist_t **ret)
{
	int i;
	int error;
	uint_t nvl_nrsrcs = 0;
	pid_t pid;
	uint32_t flag = (uint32_t)0;
	uint64_t pid64 = (uint64_t)0;
	size_t buflen = 0;
	size_t interval_size = 0;
	timespec_t *interval = NULL;
	nvlist_t *change_data = NULL;
	nvlist_t *event_data = NULL;
	rcm_info_t *info = NULL;
	char *modname = NULL;
	char *buf = NULL;
	char **rsrcnames = NULL;
	char **nvl_rsrcs = NULL;

	rcm_log_message(RCM_TRACE2, "servicing door command=%d\n", cmd);

	rcm_print_nvlist(nvl);

	/*
	 * Extract data from the door argument nvlist.  Not all arguments
	 * are needed; sanity checks are performed later.
	 */
	(void) nvlist_lookup_string_array(nvl, RCM_RSRCNAMES, &nvl_rsrcs,
	    &nvl_nrsrcs);
	(void) nvlist_lookup_string(nvl, RCM_CLIENT_MODNAME, &modname);
	(void) nvlist_lookup_uint64(nvl, RCM_CLIENT_ID, (uint64_t *)&pid64);
	pid = (pid_t)pid64;
	(void) nvlist_lookup_uint32(nvl, RCM_REQUEST_FLAG, (uint32_t *)&flag);
	(void) nvlist_lookup_byte_array(nvl, RCM_SUSPEND_INTERVAL,
	    (uchar_t **)&interval, &interval_size);
	(void) nvlist_lookup_byte_array(nvl, RCM_CHANGE_DATA, (uchar_t **)&buf,
	    &buflen);
	if (buf != NULL && buflen > 0) {
		(void) nvlist_unpack(buf, buflen, &change_data, 0);
		buf = NULL;
		buflen = 0;
	}
	(void) nvlist_lookup_byte_array(nvl, RCM_EVENT_DATA, (uchar_t **)&buf,
	    &buflen);
	if (buf != NULL && buflen > 0)
		(void) nvlist_unpack(buf, buflen, &event_data, 0);

	rsrcnames = s_calloc(nvl_nrsrcs + 1, sizeof (char *));
	for (i = 0; i < nvl_nrsrcs; i++) {
		rsrcnames[i] = nvl_rsrcs[i];
	}
	rsrcnames[nvl_nrsrcs] = NULL;

	/*
	 * Switch off the command being performed to do the appropriate
	 * sanity checks and dispatch the arguments to the appropriate
	 * implementation routine.
	 */
	switch (cmd) {
	case CMD_REGISTER:
		if ((modname == NULL) || (rsrcnames == NULL) ||
		    (rsrcnames[0] == NULL))
			goto faildata;
		error = add_resource_client(modname, rsrcnames[0], pid, flag,
		    &info);
		break;

	case CMD_UNREGISTER:
		if ((modname == NULL) || (rsrcnames == NULL) ||
		    (rsrcnames[0] == NULL))
			goto faildata;
		error = remove_resource_client(modname, rsrcnames[0], pid,
		    flag);
		break;

	case CMD_GETINFO:
		if ((rsrcnames == NULL) &&
		    ((flag & (RCM_DR_OPERATION | RCM_MOD_INFO)) == 0))
			goto faildata;
		if ((error = get_resource_info(rsrcnames, flag, seq_num, &info))
		    == EINVAL) {
			rcm_log_message(RCM_DEBUG,
			    "invalid argument in get info request\n");
			generate_reply_event(EINVAL, NULL, ret);
			return;
		}
		break;

	case CMD_SUSPEND:
		if ((rsrcnames == NULL) || (rsrcnames[0] == NULL) ||
		    (interval == NULL))
			goto faildata;
		error = process_resource_suspend(rsrcnames, pid, flag, seq_num,
		    interval, &info);
		break;

	case CMD_RESUME:
		if ((rsrcnames == NULL) || (rsrcnames[0] == NULL))
			goto faildata;
		error = notify_resource_resume(rsrcnames, pid, flag, seq_num,
		    &info);
		break;

	case CMD_OFFLINE:
		if ((rsrcnames == NULL) || (rsrcnames[0] == NULL))
			goto faildata;
		error = process_resource_offline(rsrcnames, pid, flag, seq_num,
		    &info);
		break;

	case CMD_ONLINE:
		if ((rsrcnames == NULL) || (rsrcnames[0] == NULL))
			goto faildata;
		error = notify_resource_online(rsrcnames, pid, flag, seq_num,
		    &info);
		break;

	case CMD_REMOVE:
		if ((rsrcnames == NULL) || (rsrcnames[0] == NULL))
			goto faildata;
		error = notify_resource_remove(rsrcnames, pid, flag, seq_num,
		    &info);
		break;

	case CMD_EVENT:
		if ((rsrcnames == NULL) || (rsrcnames[0] == NULL) ||
		    (event_data == NULL))
			goto faildata;
		error = notify_resource_event(rsrcnames[0], pid, flag, seq_num,
		    event_data, &info);
		nvlist_free(event_data);
		break;

	case CMD_REQUEST_CHANGE:
		if ((rsrcnames == NULL) || (rsrcnames[0] == NULL) ||
		    (change_data == NULL))
			goto faildata;
		error = request_capacity_change(rsrcnames[0], pid, flag,
		    seq_num, change_data, &info);
		nvlist_free(change_data);
		break;

	case CMD_NOTIFY_CHANGE:
		if ((rsrcnames == NULL) || (rsrcnames[0] == NULL) ||
		    (change_data == NULL))
			goto faildata;
		error = notify_capacity_change(rsrcnames[0], pid, flag, seq_num,
		    change_data, &info);
		nvlist_free(change_data);
		break;

	case CMD_GETSTATE:
		if ((rsrcnames == NULL) || (rsrcnames[0] == NULL))
			goto faildata;
		error = get_resource_state(rsrcnames[0], pid, &info);
		break;

	default:
		rcm_log_message(RCM_WARNING,
		    gettext("unknown door command: %d\n"), cmd);
		generate_reply_event(EFAULT, NULL, ret);
		(void) free(rsrcnames);
		return;
	}

	rcm_log_message(RCM_TRACE2, "finish processing event 0x%x\n", cmd);
	generate_reply_event(error, info, ret);
	(void) free(rsrcnames);
	return;

faildata:
	rcm_log_message(RCM_WARNING,
	    gettext("data error in door arguments for cmd 0x%x\n"), cmd);

	generate_reply_event(EFAULT, NULL, ret);
	(void) free(rsrcnames);
}
示例#16
0
文件: image.c 项目: anarsoul/libxynth
int s_image_init (s_image_t **img)
{
	(*img) = (s_image_t *) s_calloc(1, sizeof(s_image_t));
	s_image_layers_init(*img);
	return 0;
}
示例#17
0
文件: menu.c 项目: d33tah/whitix
void start_menu_setup (s_window_t *twindow, s_config_t *cfg)
{
	int i;
	int j;
	char *str;
	char *ptr;
	char *tmp;
	s_config_cat_t *cat;
	s_config_var_t *var;
	smenu_prog_t *sprog;
        tbar_data_t *tbar_data;

        tbar_data = (tbar_data_t *) twindow->data;

	i = 0;
	while (!s_list_eol(cfg->category, i)) {
		cat = (s_config_cat_t *) s_list_get(cfg->category, i++);
		if (strcmp(cat->name, "taskbar_prog") == 0) {
			j = 0;
			while (!s_list_eol(cat->variable, j)) {
				sprog = (smenu_prog_t *) s_calloc(1, sizeof(smenu_prog_t));
				s_list_init(&(sprog->progs));

				var = (s_config_var_t *) s_list_get(cat->variable, j++);

				sprog->name = strdup(var->name);
				tmp = strdup(var->value);
				str = tmp;
				ptr = strchr(str, '|');
				*ptr = '\0';
				sprog->icon = strdup(str);
				str = ptr + 1;
				ptr = strchr(str, '|');
				*ptr = '\0';
				sprog->exec = strdup(str);
				str = ptr + 1;
				sprog->menu = strdup(str);
				s_free(tmp);

				if ((sprog->name != NULL) && (*(sprog->name) != '\0')) {
					if ((sprog->exec != NULL) && (*(sprog->exec) != '\0')) {
						sprog->type = SMENU_PROG;
					} else {
						sprog->type = SMENU_MENU;
					}
					if ((sprog->menu != NULL) && (*(sprog->menu) != '\0')) {
						smenu_prog_t *sp;
						if ((sp = start_menu_list_find_menu(sprog, tbar_data->tbar_smenu->progs)) != NULL) {
							s_list_add(sp->progs, sprog, -1);
						} else {
							goto add_top;
						}
					} else {
add_top:					s_list_add(tbar_data->tbar_smenu->progs, sprog, -1);
					}
				} else {
					s_free(sprog->name);
					s_free(sprog->icon);
					s_free(sprog->exec);
					s_free(sprog->menu);
					s_free(sprog->progs);
					s_free(sprog);
				}
			}
		}
	}
}
示例#18
0
int
append_domainContext(__nis_table_mapping_t **table_map,
char   *DomainLabel, char *Domain)
{
	__nis_table_mapping_t *tmp_map = *table_map;
	char *lasts;
	char *tmp_dbId = NULL;
	char *id = NULL;
	int  domain_specific = 0;
	char *myself = "append_domainContext";

	if (!DomainLabel || !Domain || !tmp_map)
		return (-1);
	if (tmp_map->dbId == NULL || tmp_map->objName == NULL) {
		p_error = parse_bad_map_error;
		return (-1);
	}
	tmp_dbId = s_strndup(tmp_map->dbId, strlen(tmp_map->dbId));
	if (!tmp_dbId)
		return (-1);
	if (strchr(tmp_map->dbId, COMMA_CHAR)) {
		domain_specific = 1;
		id = (char *)strtok_r(tmp_dbId, COMMA_STRING, &lasts);
		if (id)
			id = (char *)strtok_r(NULL, COMMA_STRING, &lasts);
		else {
			free(tmp_dbId);
			return (-1);
		}
		if (!id) {
			free(tmp_dbId);
			return (-1);
		}
		if (strcasecmp(id, DomainLabel)) {
			free(tmp_dbId);
			return (0);
		}
	} else {
		if (getfullmapname(&tmp_map->dbId, DomainLabel)) {
			free(tmp_dbId);
			return (-1);
		}
		append_dot(&tmp_map->dbId);
	}
	if (tmp_dbId)
		free(tmp_dbId);
	tmp_dbId = NULL;

	if (getfullmapname(&tmp_map->objName, DomainLabel))
		return (-1);
	append_dot(&tmp_map->objName);

	/*
	 * If domain specific mapping doesn't have objectDN,
	 * then don't touch. Most probably, pass for the generic mapping
	 * will handle this by coping over it's own objectDN
	 */
	if (domain_specific && tmp_map->objectDN == NULL)
		return (0);

	if (tmp_map->objectDN == NULL) {
		/* Allocate memory to objectDN */
		tmp_map->objectDN = (__nis_object_dn_t *)
			s_calloc(1, sizeof (__nis_object_dn_t));
		if (tmp_map->objectDN == NULL) {
			logmsg(MSG_NOMEM, LOG_ERR,
"%s: Cannot allocate memory for objectDN",
				myself);
			return (2);
		}
		tmp_map->objectDN->read.base = NULL;
		tmp_map->objectDN->write.base = NULL;
		tmp_map->objectDN->read.attrs = NULL;
		tmp_map->objectDN->write.attrs = NULL;
		tmp_map->objectDN->read.scope = LDAP_SCOPE_ONELEVEL;
		tmp_map->objectDN->write.scope = LDAP_SCOPE_UNKNOWN;
	}

	if (!make_fqdn(tmp_map->objectDN, Domain))
		return (-1);
	if (tmp_map->objectDN->write.base) {
		if (!make_full_dn(&tmp_map->objectDN->write.base, Domain))
			return (-1);
	}

	return (0);
}
示例#19
0
static int
merge_table_mapping(
	__nis_table_mapping_t *in,
	__nis_table_mapping_t *out)
{
	int i;
	int len;
	int orig_num_rules;
	int append;

	if (in == NULL)
		return (1);

	if (in->dbId == NULL)
		return (1);

	/*
	 * If 'in' is generic (non-expanded) and 'out' is domain-specific,
	 * then rules from 'in' should not be appended to those in 'out'.
	 */
	if (!strchr(in->dbId, COMMA_CHAR) && strchr(out->dbId, COMMA_CHAR))
		append = 0;
	else
		append = 1;


	if (!out->index.numIndexes && in->index.numIndexes > 0) {
		if (!dup_index(&in->index, &out->index))
			return (1);
	}

	/* add_column() increments numColumns, so we don't */
	if (!out->numColumns && in->numColumns > 0) {
		for (i = 0; i < in->numColumns; i++) {
			if (!add_column(out, in->column[i]))
				return (1);
		}
	}

	if (out->commentChar == DEFAULT_COMMENT_CHAR &&
		in->commentChar != DEFAULT_COMMENT_CHAR)
		out->commentChar = in->commentChar;

	if (out->usedns_flag == 0)
		out->usedns_flag = in->usedns_flag;

	if (out->securemap_flag == 0)
		out->securemap_flag = in->securemap_flag;

	if (out->separatorStr == DEFAULT_SEP_STRING &&
		in->separatorStr != DEFAULT_SEP_STRING) {
		out->separatorStr = s_strdup(in->separatorStr);
		if (!out->separatorStr)
			return (2);
	}

	if (!out->numSplits && !out->e && in->e) {
		out->numSplits = in->numSplits;
		out->e = (__nis_mapping_element_t *)
			s_calloc(1, (in->numSplits+1) *
			sizeof (__nis_mapping_element_t));
		if (!out->e)
			return (2);
		for (i = 0; i <= in->numSplits; i++) {
			if (!dup_mapping_element(&in->e[i], &out->e[i])) {
				for (; i > 0; i--) {
					free_mapping_element(&out->e[i - 1]);
				}
				out->e = NULL;
				return (1);
			}
		}
	}

	if (out->initTtlLo == (time_t)NO_VALUE_SET &&
		in->initTtlLo != (time_t)NO_VALUE_SET)
		out->initTtlLo = in->initTtlLo;

	if (out->initTtlHi == (time_t)NO_VALUE_SET &&
		in->initTtlHi != (time_t)NO_VALUE_SET)
		out->initTtlHi = in->initTtlHi;

	if (out->ttl == (time_t)NO_VALUE_SET &&
		in->ttl != (time_t)NO_VALUE_SET)
		out->ttl = in->ttl;

	if (!out->numRulesFromLDAP && in->numRulesFromLDAP) {
		out->ruleFromLDAP = dup_mapping_rules(in->ruleFromLDAP,
			in->numRulesFromLDAP);
		if (!out->ruleFromLDAP)
			return (1);
		out->numRulesFromLDAP = in->numRulesFromLDAP;
	} else if (append && out->numRulesFromLDAP && in->numRulesFromLDAP) {
		orig_num_rules = out->numRulesFromLDAP;
		for (i = 0; i < in->numRulesFromLDAP; i++) {
			if (append_mapping_rule(in->ruleFromLDAP[i], out, 0)) {
				for (i = out->numRulesFromLDAP;
					i > orig_num_rules; i--) {
					free_mapping_rule(out->ruleFromLDAP[i]);
					out->ruleFromLDAP[i] = NULL;
				}
				return (1);

			}
		}
	}

	if (!out->numRulesToLDAP && in->numRulesToLDAP) {
		out->ruleToLDAP = dup_mapping_rules(in->ruleToLDAP,
			in->numRulesToLDAP);
		if (!out->ruleToLDAP)
			return (1);
		out->numRulesToLDAP = in->numRulesToLDAP;
	} else if (append && out->numRulesToLDAP && in->numRulesToLDAP) {
		orig_num_rules = out->numRulesToLDAP;
		for (i = 0; i < in->numRulesToLDAP; i++) {
			if (append_mapping_rule(in->ruleToLDAP[i], out, 1)) {
				for (i = out->numRulesToLDAP;
					i > orig_num_rules; i--) {
					free_mapping_rule(out->ruleToLDAP[i]);
					out->ruleToLDAP[i] = NULL;
				}
				return (1);
			}
		}
	}
	if (!out->objectDN && in->objectDN) {
		out->objectDN = (__nis_object_dn_t *)
			s_calloc(1, sizeof (__nis_object_dn_t));
		if (!out->objectDN)
			return (2);
		if (copy_object_dn(in->objectDN, out->objectDN)) {
			free_object_dn(out->objectDN);
			out->objectDN = NULL;
			return (1);
		}
	}

	if (!out->objName && in->objName) {
		if (!strchr(in->objName, SPACE_CHAR)) {
		/* objName has no space- a single map dbIdMapping */
			out->objName = s_strndup(in->objName,
				strlen(in->objName));
			if (!out->objName)
				return (2);
		}
	}

	if (!out->objName && out->dbId) {
		out->objName = s_strndup(out->dbId, strlen(out->dbId));
		if (!out->objName)
			return (2);
	}

	if (out->seq_num == NO_VALUE_SET && in->seq_num >= 0)
		out->seq_num = in->seq_num;

	return (p_error == no_parse_error ? 0 : 1);
}
示例#20
0
static int
copy_object_dn(
	__nis_object_dn_t	*in,
	__nis_object_dn_t	*newdn)
{
	if (in == NULL) {
		p_error = parse_no_object_dn;
		return (1);
	}
	while (in != NULL) {
		if (in->read.base == NULL) {
			newdn->read.base = NULL;
		} else {
			newdn->read.base = s_strndup(
				in->read.base,
				strlen(in->read.base));
			if (newdn->read.base == NULL)
				return (2);
		}
		newdn->read.scope = in->read.scope;
		if (in->read.attrs) {
			newdn->read.attrs = s_strndup(
					in->read.attrs,
					strlen(in->read.attrs));
			if (newdn->read.attrs == NULL) {
				return (2);
			}
		} else {
			newdn->read.attrs = NULL;
		}
		newdn->read.element = in->read.element;
		if (in->write.base != NULL) {
			newdn->write.base = s_strndup(
				in->write.base,
				strlen(in->write.base));
			if (newdn->write.base == NULL)
				return (2);
		} else {
			newdn->write.base = NULL;
		}
		newdn->write.scope = in->write.scope;
		if (in->write.attrs != NULL) {
			newdn->write.attrs = s_strndup(
				in->write.attrs,
				strlen(in->write.attrs));
			if (newdn->write.attrs == NULL) {
				return (2);
			}
		} else {
			newdn->write.attrs = NULL;
		}
		newdn->write.element = in->write.element;
		if (in->dbIdName) {
			newdn->dbIdName = s_strndup(in->dbIdName,
						strlen(in->dbIdName));
			if (newdn->dbIdName == NULL)
				return (2);
		}

		if (in->delDisp)
			newdn->delDisp = in->delDisp;

		if (in->dbId && in->numDbIds > 0) {
			newdn->dbId = dup_mapping_rules(in->dbId,
					in->numDbIds);
			if (!newdn->dbId)
				return (1);
			newdn->numDbIds = in->numDbIds;
		}
		if (in->next != NULL) {
			newdn->next = (__nis_object_dn_t *)s_calloc(1,
					sizeof (__nis_object_dn_t));
			if (newdn->next == NULL)
				return (1);
			newdn = newdn->next;
			in = in->next;
		} else {
			return (0);
		}
	} /* End of while on in */

	return (0);
}
示例#21
0
/*
 * FUNCTION:	second_parser_pass
 *
 * Prepares the linked list of table_mappings for processing
 * by finish_parse(), adding, merging and deleting structures
 * as necessary. Also adds dummy objectDN info. for splitField's.
 *
 * RETURN VALUE: 0 on success, > 0 on failure.
 */
int
second_parser_pass(
	__nis_table_mapping_t   **table_mapping)
{
	__nis_table_mapping_t   *t, *t2;
	__nis_table_mapping_t   *t_new = NULL, *tg;
	__nis_table_mapping_t	*prev = NULL;
	__nis_object_dn_t   *objectDN;
	char	*objs, *dom;
	char	*objName = NULL;
	char	*lasts;
	char	*tobj, *alias, *dupalias, *tmp;
	char	*myself = "second_parser_pass";
	int	i = 0, len;
	int	remove_t = 0;
	int	add_t = 0;

	prev = NULL;
	for (t = *table_mapping; t != NULL; ) {
		/*
		 * Temporarily using this field to flag deletion.
		 * 0 : don't delete
		 * 1 : delete
		 * The mapping structure will be deleted in final_parser_pass
		 */
		t->isMaster = 0;

		if (!t->dbId) {
			p_error = parse_bad_map_error;
			logmsg(MSG_NOTIMECHECK, LOG_ERR,
			"%s: no dbId field", myself);
			return (1);
		}
		tg = NULL;
		dom = strchr(t->dbId, COMMA_CHAR);
		if (t->objName != NULL) {
			objName = strdup(t->objName);
			if (objName == NULL) {
				p_error = parse_no_mem_error;
				logmsg(MSG_NOMEM, LOG_ERR,
		"%s: Cannot allocate memory for objName", myself);
				return (1);
			}
			objs = (char *)strtok_r(objName, " ", &lasts);
			/* Get the generic mapping */
			if (dom != NULL) {
				tg = find_table_mapping(t->dbId, dom - t->dbId,
								*table_mapping);
			}
		} else {
			objs = NULL;
			if (dom == NULL) {
				t->objName = s_strndup(t->dbId,
						strlen(t->dbId));
				if (!t->objName) {
					logmsg(MSG_NOMEM, LOG_ERR,
"%s: Cannot allocate memory for t->objName",
						myself);
					objs = NULL;
					return (2);
				}
			} else {
				/* Force relationship for domain specific */

				/* Get the generic mapping */
				tg = find_table_mapping(t->dbId, dom - t->dbId,
								*table_mapping);
				if (tg == NULL || tg->objName == NULL) {
					/* If not found, use dbId for objName */
					t->objName = s_strndup(t->dbId,
							strlen(t->dbId));
					if (t->objName == NULL) {
						logmsg(MSG_NOMEM, LOG_ERR,
"%s: Cannot allocate memory for t->objName",
							myself);
						return (2);
					}
				} else {
					dom++;
					tobj = s_strndup(tg->objName,
							strlen(tg->objName));
					if (tobj == NULL) {
						logmsg(MSG_NOMEM, LOG_ERR,
"%s: Cannot allocate memory for t->objName",
							myself);
						return (2);
					}
					alias = (char *)strtok_r(tobj, " ",
									&lasts);

					/* Loop 'breaks' on errors */
					while (alias) {
						tmp = NULL;
						dupalias = s_strndup(alias,
								strlen(alias));
						if (!dupalias)
							break;
						if (getfullmapname(&dupalias,
								dom)) {
							i = 1;
							break;
						}
						if (t->objName == NULL)
							t->objName = dupalias;
						else {
							len = strlen(t->objName)
							+ strlen(dupalias) + 2;
							tmp = s_calloc(1, len);
							if (tmp == NULL)
								break;
							snprintf(tmp, len,
								"%s %s",
								t->objName,
								dupalias);
							free(dupalias);
							dupalias = NULL;
							free(t->objName);
							t->objName = tmp;
						}
						alias = (char *)strtok_r(NULL,
								" ", &lasts);
					}

					if (tobj)
						free(tobj);

					if (alias ||
						(objName = s_strdup(t->objName))
								== NULL) {
						if (i)
							logmsg(MSG_NOTIMECHECK,
							LOG_ERR,
"%s: getfullmapname failed for %s for domain \"%s\"",
							myself, dupalias, dom);
						else {
							p_error =
							parse_no_mem_error;
							logmsg(MSG_NOMEM,
							LOG_ERR,
"%s: Cannot allocate memory",
							myself);
						}
						if (dupalias)
							free(dupalias);
						if (t->objName)
							free(t->objName);
						return (2);

					}
					objs = (char *)strtok_r(objName, " ",
								&lasts);
				}
			}
		}

		if (tg != NULL) {
			if (merge_table_mapping(tg, t)) {
				logmsg(MSG_NOTIMECHECK, LOG_ERR,
"Error merging information from the %s to the %s mapping structure",
					tg->dbId, t->dbId);
				objs = NULL;
				if (objName)
					free(objName);
				return (1);
			}
		}

		/*
		 * If objName is "map1 map2" then do the second pass.
		 * If it is just "map1" however skip the expansion.
		 * Also skip it if t->objName is null.
		 */
		if (objs && strncasecmp(objs, t->objName,
			strlen(t->objName))) {
			t2 = find_table_mapping(objs, strlen(objs),
							*table_mapping);
			if (t2) {
				if (merge_table_mapping(t, t2)) {
					logmsg(MSG_NOTIMECHECK, LOG_ERR,
"Error merging information from the %s to the %s mapping structure",
						t->dbId, t2->dbId);
					objs = NULL;
					if (objName)
						free(objName);
					return (1);
				}
				t->isMaster = 1;
			} else {
				t_new = new_merged_mapping(objs, t);
				if (t_new) {
					t->isMaster = 1;
					if (prev != NULL)
						prev->next = t_new;
					else
						*table_mapping = t_new;
					prev = t_new;
					prev->next = t;
				} else {
					logmsg(MSG_NOTIMECHECK, LOG_ERR,
"Error creating a new mapping structure %s",
						objs);
					objs = NULL;
					if (objName)
						free(objName);
					return (1);
				}
			}
			while ((objs = (char *)strtok_r(NULL, " ", &lasts))
				!= NULL) {
				t2 = find_table_mapping(objs, strlen(objs),
								*table_mapping);
				if (t2) {
					if (merge_table_mapping(t, t2)) {
						logmsg(MSG_NOTIMECHECK, LOG_ERR,
"Error merging information from the %s to the %s mapping structure",
							t->dbId, t2->dbId);
						objs = NULL;
						if (objName)
							free(objName);
						return (1);
					}
					t->isMaster = 1;
				} else {
					/*
					 * create a new t_map with dbId = objs
					 * and copy t->* into new t_map
					 */
					t_new = new_merged_mapping(objs, t);
					if (t_new) {
						t->isMaster = 1;
						if (prev != NULL)
							prev->next = t_new;
						else
							*table_mapping = t_new;
						prev = t_new;
						prev->next = t;
					} else {
						logmsg(MSG_NOTIMECHECK, LOG_ERR,
"Error creating a new mapping structure %s",
							objs);
						objs = NULL;
						if (objName)
							free(objName);
						return (1);
					}
				}
			}
		} /* if objs!= NULL */

		prev = t;
		t = t->next;

		if (objName) {
			free(objName);
			objName = NULL;
			objs = NULL;
		}
	} /* for t = table_mapping loop */
	return (0);
}
示例#22
0
int s_image_png (char *file, s_image_t *img)
{
	int x;
	int y;
	FILE *fp;
	png_uint_32 width, height;
	char header[8];
	unsigned int *tmp;
	int bit_depth, color_type;
	png_infop info_ptr;
	png_structp png_ptr;
	int __attribute__((unused)) number_of_passes;
	png_bytep *row_pointers;
	
	fp = fopen(file, "rb");
	if (!fp) {
		debugf(DFAT, "Could not open file %s", file);
	}
	fread(header, 1, 8, fp);
	if (png_sig_cmp((png_bytep) header, 0, 8)) {
		debugf(DFAT, "File is not a PNG file %s", file);
	}
	
	png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
	if (!png_ptr) {
		debugf(DFAT, "png_create_read_struct failed");
	}

	info_ptr = png_create_info_struct(png_ptr);
	if (!info_ptr) {
		debugf(DFAT, "png_create_info_struct failed");
	}

	if (setjmp(png_jmpbuf(png_ptr))) {
		debugf(DFAT, "setjmp failed");
	}
	
	png_init_io(png_ptr, fp);
	png_set_sig_bytes(png_ptr, 8);
	png_read_info(png_ptr, info_ptr);
	png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
		NULL, NULL, NULL);
	number_of_passes = png_set_interlace_handling(png_ptr);
	png_read_update_info(png_ptr, info_ptr);
	
	if (setjmp(png_jmpbuf(png_ptr))) {
		debugf(DFAT, "Error during read_image");
	}

	row_pointers = (png_bytep *) s_malloc(sizeof(png_bytep) * height);
	for (y = 0; y < height; y++) {
		row_pointers[y] = (png_byte* ) s_malloc(png_get_rowbytes(png_ptr, info_ptr));
	}

        png_read_image(png_ptr, row_pointers);
	
	img->w = width;
	img->h = height;
	img->rgba = (unsigned int *) s_calloc(1, img->w * img->h * sizeof(unsigned int));
	tmp = img->rgba;

	if (color_type == PNG_COLOR_TYPE_RGBA) {
		for (y = 0; y < height; y++) {
			png_byte *row = row_pointers[y];
			for (x = 0; x < width; x++) {
				png_byte *ptr = &(row[x * 4]);

				*tmp |= (ptr[0] << 0x18);
				*tmp |= (ptr[1] << 0x10);
				*tmp |= (ptr[2] << 0x08);
				*tmp |= (~(ptr[3] << 0x00)) & 0xFF;

				ptr[0] = 0;
				ptr[1] = ptr[2];
				tmp++;
			}
		}
	} else if (color_type == PNG_COLOR_TYPE_RGB) {
		for (y = 0; y < height; y++) {
			png_byte *row = row_pointers[y];
			for (x = 0; x < width; x++) {
				png_byte *ptr = &(row[x * 3]);

				*tmp |= (ptr[0] << 0x18);
				*tmp |= (ptr[1] << 0x10);
				*tmp |= (ptr[2] << 0x08);
				*tmp |= 0x0;

				ptr[0] = 0;
				ptr[1] = ptr[2];
				tmp++;
			}
		}
	} else if (color_type == PNG_COLOR_TYPE_PALETTE) {
		int i;
		png_colorp palette;
		png_bytep trans_alpha;
		png_color_16p trans_color;
		int num_palette, num_trans;
		struct Palette {
			int r;
			int g;
			int b;
			int a;
		};
		struct Palette *pal;
		pal = (struct Palette *) s_malloc(256 * sizeof(struct Palette));
		memset(pal, 0, 256 * sizeof(struct Palette));
		
		for (i = 0; i < 256; i++) {
			pal[i].a = 255;
		}
		
		if (png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette)) {
			if (png_get_tRNS(png_ptr, info_ptr,
			    &trans_alpha, &num_trans, &trans_color)) {
				for (i = 0; i < num_palette; i++) {
					pal[i].r = palette[i].red;
					pal[i].g = palette[i].green;
					pal[i].b = palette[i].blue;
					if (i < num_trans) {
						pal[i].a = trans_alpha[i];
					}
				}
				for (y = 0; y < height; y++) {
					png_byte *row = row_pointers[y];
					for (x = 0; x < width; x++) {
						png_byte *ptr = &(row[x]);
						*tmp |= (pal[ptr[0]].r << 0x18);
						*tmp |= (pal[ptr[0]].g << 0x10);
						*tmp |= (pal[ptr[0]].b << 0x08);
						*tmp |= (~(pal[ptr[0]].a << 0x00)) & 0xff;
						tmp++;
					}
				}
			}
		}

		s_free(pal);
	} else {
		debugf(DFAT, "Unknown color_type : %d (%s)", color_type, file);
	}

	for (y = 0; y < height; y++) {
		s_free(row_pointers[y]);
	}
	s_free(row_pointers);
	
	png_destroy_info_struct(png_ptr, &info_ptr);
	png_destroy_read_struct(&png_ptr, NULL, NULL);
	
	fclose(fp);
	
	return 0;
}
示例#23
0
文件: lsyncd.c 项目: aking666/lsyncd
/*
| Executes a subprocess. Does not wait for it to return.
|
| Params on Lua stack:
|
|    1: Path to binary to call
|    2: List of string as arguments
|         or "<" in which case the next argument is a string
|         that will be piped on stdin.
|         The arguments will follow that one.
|
| Returns (Lua stack) the pid on success, 0 on failure.
*/
static int
l_exec( lua_State *L )
{
	// the binary to call
	const char *binary = luaL_checkstring(L, 1);

	// number of arguments
	int argc = lua_gettop( L ) - 1;

	// the pid spawned
	pid_t pid;

	// the arguments position in the lua arguments
	int li = 1;

	// the pipe to text
	char const * pipe_text = NULL;

	// the pipes length
	size_t pipe_len = 0;

	// the arguments
	char const ** argv;

	// pipe file descriptors
	int pipefd[ 2 ];

	int i;

	// expands tables
	// and removes nils
	for( i = 1; i <= lua_gettop( L ); i++ )
	{
		if( lua_isnil( L, i ) )
		{
			lua_remove( L, i );
			i--;
			argc--;
			continue;
		}

		if( lua_istable( L, i ) )
		{
			int tlen;
			int it;
			lua_checkstack( L, lua_gettop( L ) + lua_objlen( L, i ) + 1 );

			// moves table to top of stack
			lua_pushvalue( L, i );
			lua_remove( L, i );
			argc--;
			tlen = lua_objlen( L, -1 );

			for( it = 1; it <= tlen; it++ )
			{
				lua_pushinteger( L, it );
				lua_gettable( L, -2 );
				lua_insert( L, i );
				i++;
				argc++;
			}
			i--;
			lua_pop( L, 1 );
		}
	}

	// writes a log message (if needed).
	if( check_logcat( "Exec" ) <= settings.log_level )
	{
		lua_checkstack( L, lua_gettop( L ) + argc * 3 + 2 );
		lua_pushvalue( L, 1 );

		for( i = 1; i <= argc; i++ )
		{
			lua_pushstring( L, " [" );
			lua_pushvalue( L, i + 1 );
			lua_pushstring( L, "]" );
		}

		lua_concat( L, 3 * argc + 1 );

		// replaces midfile 0 chars by linefeed
		size_t len = 0;
		const char * cs = lua_tolstring( L, -1, &len );
		char * s = s_calloc( len + 1, sizeof( char ) ); 

		for( i = 0; i < len; i++ )
		{
			s[ i ] = cs[ i ] ? cs[ i ] : '\n';
		}

		logstring0(
			LOG_DEBUG, "Exec",
			s
		);

		free( s );

		lua_pop( L, 1 );
	}

	if( argc >= 2 && !strcmp( luaL_checkstring( L, 2 ), "<" ) )
	{
		// pipes something into stdin
		if( !lua_isstring( L, 3 ) )
		{
			logstring(
				"Error",
				"in spawn(), expected a string after pipe '<'"
			);

			exit( -1 );
		}

		pipe_text = lua_tolstring( L, 3, &pipe_len );

		if( strlen( pipe_text ) > 0 )
		{
			// creates the pipe
			if( pipe( pipefd ) == -1 )
			{
				logstring( "Error", "cannot create a pipe!" );

				exit( -1 );
			}

			// always closes the write end for child processes
			close_exec_fd( pipefd[ 1 ] );

			// sets the write end on non-blocking
			non_block_fd( pipefd[ 1 ] );
		}
		else
		{
			pipe_text = NULL;
		}
		argc -= 2;
		li += 2;
	}

	// prepares the arguments
	argv = s_calloc( argc + 2, sizeof( char * ) );

	argv[ 0 ] = binary;

	for( i = 1; i <= argc; i++ )
	{
		argv[i] = luaL_checkstring( L, i + li );
	}

	argv[ i ] = NULL;

	// the fork!
	pid = fork( );

	if( pid == 0 )
	{
		// replaces stdin for pipes
		if( pipe_text )
		{
			dup2( pipefd[ 0 ], STDIN_FILENO );
		}

		// if lsyncd runs as a daemon and has a logfile it will redirect
		// stdout/stderr of child processes to the logfile.
		if( is_daemon && settings.log_file )
		{
			if( !freopen( settings.log_file, "a", stdout ) )
			{
				printlogf(
					L, "Error",
					"cannot redirect stdout to '%s'.",
					settings.log_file
				);
			}

			if( !freopen( settings.log_file, "a", stderr ) )
			{
				printlogf(
					L, "Error",
					"cannot redirect stderr to '%s'.",
					settings.log_file
				);
			}
		}

		execv( binary, ( char ** ) argv );

		// in a sane world execv does not return!
		printlogf(
			L, "Error",
			"Failed executing [ %s ]!",
			binary
		);

		exit( -1 );
	}

	if( pipe_text )
	{
		int len;

		// first closes read-end of pipe, this is for child process only
		close( pipefd[ 0 ] );

		// starts filling the pipe
		len = write( pipefd[ 1 ], pipe_text, pipe_len );

		if( len < 0 )
		{
			logstring( "Normal", "immediatly broken pipe." );
			close( pipefd[ 1 ] );
		}
		else if( len == pipe_len )
		{
			// usual and best case, the pipe accepted all input -> close
			close( pipefd[ 1 ] );
			logstring( "Exec", "one-sweeped pipe" );
		}
		else
		{
			struct pipemsg *pm;
			logstring( "Exec", "adding pipe observance" );
			pm = s_calloc( 1, sizeof( struct pipemsg ) );
			pm->text = s_calloc( pipe_len + 1, sizeof( char ) );
			memcpy( pm->text, pipe_text, pipe_len + 1 );
			pm->tlen = pipe_len;
			pm->pos  = len;

			observe_fd(
				pipefd[ 1 ],
				NULL,
				pipe_writey,
				pipe_tidy,
				pm
			);
		}
	}

	free( argv );
	lua_pushnumber( L, pid );

	return 1;
}
示例#24
0
int s_image_xpm (char *file, s_image_t *img)
{
	int i;
	int j;
	int k;
	int l;
	int m;
	int x;
	int y;
	char *buf;
	char *buf_tmp;
	int colors = 0;
	int colors_pp = 0;
	char color_hex[10];
	unsigned int *rgba_tmp = NULL;
	struct xpm_rgb_s {
		char sign[10];
		unsigned char a;
		unsigned char r;
		unsigned char g;
		unsigned char b;
	} *rgb = NULL;
	char sign_str0[] = " .+@#$%&*=-;>,')!~{]^/(_:<[}|1234567890abcdefg"
                           "hijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`";
	char sign_str1[] = " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"
	                   "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
	FILE *fp;

	buf = (char *) s_malloc(sizeof(char) * BUFFSIZE + 1);
	buf_tmp = buf;

	if ((fp = fopen(file, "r")) == NULL) {
		debugf(DCLI | DFAT, "Coult not open file (%s) for reading");
	}

	while (!feof(fp)) {
		fgets(buf, BUFFSIZE, fp);
		if (strncmp(buf, "/*", 2) == 0) {
			continue;
		}
		if (buf[0] != '"') {
			continue;
		}
		sscanf(buf + 1, "%d %d %d %d", &(img->w), &(img->h), &colors, &colors_pp);
		rgb = (struct xpm_rgb_s *) s_calloc(1, colors * sizeof(struct xpm_rgb_s));
		img->rgba = (unsigned int *) s_calloc(1, img->w * img->h * sizeof(unsigned int));
		rgba_tmp = img->rgba;
		break;
	}

	if ((rgb == NULL) || (rgba_tmp == NULL)) {
		debugf(DCLI | DFAT, "Not enough memory");
	}

        for (i = 0; i < colors; i++) {
		fgets(buf, BUFFSIZE, fp);
		if (strncmp(buf, "/*", 2) == 0) {
			continue;
		}
		for (j = 0; j < colors_pp; j++) {
			rgb[i].sign[j] = buf[j + 1];
		}
		rgb[i].sign[j] = '\0';
		if ((buf_tmp = strstr(buf, "c #")) != NULL) {
			sscanf(buf_tmp, "c #%s", color_hex);
			rgb[i].a = 0;
			rgb[i].r = s_image_hex2int(color_hex);
			rgb[i].g = s_image_hex2int(color_hex + 2);
			rgb[i].b = s_image_hex2int(color_hex + 4);
		} else if ((buf_tmp = strstr(buf, "c ")) != NULL) {
			char rgbname[30];
			sscanf(buf_tmp, "c %s", rgbname);
			rgbname[strlen(rgbname) - 2] = 0;
			if (strcasecmp(rgbname, "none") == 0) {
				goto color_none;
			}
			for (k = 0; k < 234; k++) {
				if (strcasecmp(rgbname, rgbRecord[k].name) == 0) {
					rgb[i].a = 0;
					rgb[i].r = rgbRecord[k].r;
					rgb[i].g = rgbRecord[k].g;
					rgb[i].b = rgbRecord[k].b;
					break;
				}
			}
		} else {
color_none:		rgb[i].a = 255;
			rgb[i].r = 0;
			rgb[i].g = 0;
			rgb[i].b = 0;
		}
	}

	for (y = 0; y < img->h; y++) {
		fgets(buf, BUFFSIZE, fp);
		if (strncmp(buf, "/*", 2) == 0) {
			continue;
		}
		buf_tmp = buf + 1;
		for (x = 0; x < img->w; x++, buf_tmp += colors_pp) {
			find_sign(sign_str0);
			m = i;
			find_sign(sign_str1);
			i = (m < i) ? m : i;
			for (; i < colors; i++) {
				if (s_image_xpm_memcmp(buf_tmp, rgb[i].sign, colors_pp) == 0) {
					*rgba_tmp |= (rgb[i].r << 0x18);
					*rgba_tmp |= (rgb[i].g << 0x10);
					*rgba_tmp |= (rgb[i].b << 0x08);
					*rgba_tmp |= (rgb[i].a << 0x00);
					rgba_tmp++;
					break;
				}
			}
		}
	}

	s_free(rgb);
	s_free(buf);
	fclose(fp);
	
	return 0;
}
示例#25
0
文件: server.c 项目: jetlive/xynth
int s_video_gdi_server_init (s_server_conf_t *cfg)
{
	void *addr;

	s_video_helper_mode_info_t *gmode;
	s_video_gdi_data_t *priv;

	priv = (s_video_gdi_data_t *) s_calloc(1, sizeof(s_video_gdi_data_t));
	xynth_server->driver->driver_data = (void *) priv;

	if (s_video_helper_mode_find(cfg, &gmode)) {
		debugf(DSER, "Couldn't find mode: %s", cfg->general.mode);
		goto err0;
	}else {
		xynth_server->window->surface->width = gmode->xdim;
		xynth_server->window->surface->height = gmode->ydim;
		xynth_server->window->surface->bytesperpixel = gmode->bytesperpixel;
		xynth_server->window->surface->bitsperpixel = gmode->bytesperpixel * 8;
		xynth_server->window->surface->blueoffset = 0;
		xynth_server->window->surface->greenoffset = 0;
		xynth_server->window->surface->redoffset = 0;
		xynth_server->window->surface->bluelength = 0;
		xynth_server->window->surface->greenlength = 0;
		xynth_server->window->surface->redlength = 0;
		xynth_server->window->surface->colors = 0;

		switch (xynth_server->window->surface->bitsperpixel) {
			case 8:
				xynth_server->window->surface->colors = 256;
				xynth_server->window->surface->bitsperpixel = 8;
				xynth_server->window->surface->blueoffset = 0;
				xynth_server->window->surface->greenoffset = 3;
				xynth_server->window->surface->redoffset = 6;
				xynth_server->window->surface->bluelength = 3;
				xynth_server->window->surface->greenlength = 3;
				xynth_server->window->surface->redlength = 2;
				break;
			case 15:
				xynth_server->window->surface->colors = 32768;
				xynth_server->window->surface->bitsperpixel = 15;
				xynth_server->window->surface->blueoffset = 0;
				xynth_server->window->surface->greenoffset = 5;
				xynth_server->window->surface->redoffset = 10;
				xynth_server->window->surface->bluelength = 5;
				xynth_server->window->surface->greenlength = 5;
				xynth_server->window->surface->redlength = 5;
				break;
			case 16:
				xynth_server->window->surface->colors = 65536;
				xynth_server->window->surface->bitsperpixel = 16;
				xynth_server->window->surface->blueoffset = 0;
				xynth_server->window->surface->greenoffset = 5;
				xynth_server->window->surface->redoffset = 11;
				xynth_server->window->surface->bluelength = 5;
				xynth_server->window->surface->greenlength = 6;
				xynth_server->window->surface->redlength = 5;
				break;
			case 24:
			case 32:
				xynth_server->window->surface->colors = 256 * 65536;
				xynth_server->window->surface->bitsperpixel = xynth_server->window->surface->bytesperpixel * 8;
				xynth_server->window->surface->blueoffset = 0;
				xynth_server->window->surface->greenoffset = 8;
				xynth_server->window->surface->redoffset = 16;
				xynth_server->window->surface->bluelength = 8;
				xynth_server->window->surface->greenlength = 8;
				xynth_server->window->surface->redlength = 8;
				break;
		}
	}

	addr = (void *) s_malloc(sizeof(char) * xynth_server->window->surface->width * xynth_server->window->surface->height * xynth_server->window->surface->bytesperpixel);

	xynth_server->window->surface->linear_mem_base = (unsigned int) addr;
	xynth_server->window->surface->linear_mem_size = (unsigned int) (sizeof(char) * xynth_server->window->surface->width * xynth_server->window->surface->height * xynth_server->window->surface->bytesperpixel);

	xynth_server->window->surface->vbuf = (char *) addr;
	xynth_server->window->surface->linear_buf = (char *) addr;

	xynth_server->window->surface->need_expose = SURFACE_NEEDEXPOSE;

	/* We need seperate thread for events so event loop and the
	   window creation are in the same thread.

	   Applications with multiple threads can include a
	   message loop in each thread that creates a window.

	   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/
	   windowsuserinterface/windowing/messagesandmessagequeues/
	   aboutmessagesandmessagequeues.asp
	*/
       	priv->event_tid = s_thread_create(&s_video_gdi_create_window, (void *) NULL);

	return gmode->number;

err0:	PostQuitMessage(0);
	return -1;
}
示例#26
0
文件: handler.c 项目: d33tah/whitix
int s_handler_init (s_handler_t **handler)
{
	(*handler) = (s_handler_t *) s_calloc(1, sizeof(s_handler_t));
	return 0;
}
示例#27
0
文件: window.c 项目: d33tah/whitix
int s_window_init (s_window_t **window)
{
    s_window_t *w;

    w = (s_window_t *) s_calloc(1, sizeof(s_window_t));
    if (s_surface_init(w))       {
        goto err1;
    }
    if (s_pollfds_init(w))       {
        goto err2;
    }
    if (s_timers_init(w))        {
        goto err3;
    }
    if (s_socket_init(w))        {
        goto err4;
    }
    if (s_handlers_init(w))      {
        goto err4;
    }
    if (s_childs_init(w))        {
        goto err5;
    }
    if (s_eventq_init(w))        {
        goto err6;
    }
    if (s_event_init(&w->event)) {
        goto err7;
    }
    if (s_gettext_init(w))       {
        goto err8;
    }

    w->running = 1;
    w->id = -1;
    w->pri = -1;
    w->resizeable = 1;

    /* -1 means no limits on width or height. */
    w->min_w = w->min_h = w->max_w = w->max_h = -1;

    w->alwaysontop = 0;
    w->cursor = MOUSE_CURSOR_ARROW;
    w->mouse_entered = 0;
    *window = w;

    if (s_socket_request(w, SOC_DATA_DISPLAY)) {
        goto err9;
    }

    return 0;
err9:
    s_gettext_uninit(w);
err8:
    s_event_uninit(w->event);
err7:
    s_eventq_uninit(w);
err6:
    s_childs_uninit(w);
err5:
    s_handlers_uninit(w);
err4:
    s_timers_uninit(w);
err3:
    s_pollfds_uninit(w);
err2:
    s_free(w->surface);
err1:
    s_free(w);
    debugf(DCLI | DFAT, "Cannot connect to server");
    return -1;
}