Exemplo n.º 1
0
Arquivo: app.c Projeto: stoni/luarest
luarest_status invoke_application(application* apps, UT_string* url, luarest_method m, luarest_response* res_code, 
	luarest_content_type* con_type, UT_string* res_buf)
{
	application* app = NULL;
	service *service;
	char* pch = NULL;
	char* tmp = utstring_body(url);
	UT_string* app_name;
	UT_string* key;

	utstring_new(app_name);
	pch = strchr(++tmp, '/');
	if (pch == NULL) {
		return(LUAREST_ERROR);
	}
	utstring_bincpy(app_name, tmp, pch-tmp);
	HASH_FIND(hh, apps, utstring_body(app_name), utstring_len(app_name), app);
	utstring_free(app_name);
	if (app == NULL) {
		return(LUAREST_ERROR);
	}
	utstring_new(key);
	utstring_printf(key, "M%d#P%s", m, pch);
	HASH_FIND(hh, app->s, utstring_body(key), utstring_len(key), service);
	if (service == NULL) {
		return(LUAREST_ERROR);
	}
	invoke_lua(app->lua_state, service->callback_ref, res_code, con_type, res_buf);
	return(LUAREST_SUCCESS);
}
Exemplo n.º 2
0
void append_to_client_buf(UT_string *f) {
  assert(utarray_len(cfg.outbufs) > 0);
  UT_string **s=NULL;
  size_t l,least,c;
  char *b;
  int i=0,lx;

  b = utstring_body(f);
  l = utstring_len(f);

  switch(cfg.mode) {
    case fan:          // send to ALL clients
      while ( (s=(UT_string**)utarray_next(cfg.outbufs,s))) {
        utstring_bincpy(*s,b,l);
      }
      break;
    case round_robin:  // send to ONE client 
      while ( (s=(UT_string**)utarray_next(cfg.outbufs,s))) {
        c = utstring_len(*s);
        if ((i==0) || (c < least)) {least=c; lx=i;}
        i++;
      }
      s = (UT_string**)utarray_eltptr(cfg.outbufs,lx);
      utstring_bincpy(*s,b,l);
      break;
  }
}
Exemplo n.º 3
0
Arquivo: net.c Projeto: bauman/pmtr
/* report to all configured destinations */
void report_status(pmtr_t *cfg) {
  int rc;
  time_t now = time(NULL);

  /* construct msg */
  utstring_clear(cfg->s);
  utstring_printf(cfg->s, "report %s\n", cfg->report_id);
  job_t *j = NULL;
  while ( (j=(job_t*)utarray_next(cfg->jobs,j))) {
    if (j->respawn == 0) continue; /* don't advertise one-time jobs */
    utstring_printf(cfg->s, "%s %c %u %d %s\n", j->name, j->disabled?'d':'e',
                    (unsigned)(now - j->start_ts), (int)j->pid,
                    *((char**)utarray_front(&j->cmdv)));
  }

  /* send to all dests */
  int *fd=NULL;
  while ( (fd=(int*)utarray_next(cfg->report,fd))) {
    rc = write(*fd,utstring_body(cfg->s),utstring_len(cfg->s));
    if (rc < 0 && errno != ECONNREFUSED) 
      syslog(LOG_INFO,"write error: %s", strerror(errno));
    if (rc >= 0 && rc < utstring_len(cfg->s)) {
      syslog(LOG_INFO,"incomplete write %d/%d", rc, utstring_len(cfg->s));
    }
  }
}
Exemplo n.º 4
0
/* used to stop reading the spool when internal buffers are 90% full */
int have_capacity() {
  size_t max = utarray_len(cfg.outbufs) * cfg.mb_per_client * (1024*1024);
  size_t used=0;
  UT_string **s=NULL;
  while ( (s=(UT_string**)utarray_next(cfg.outbufs,s))) used += utstring_len(*s);
  double pct_full = max ? (used*100.0/max) : 100;
  return (pct_full > 90) ? 0 : 1;
}
Exemplo n.º 5
0
static int JsonParser_internalData(struct ParserInternal *pi)
{
    if (pi->quote_begin) {
        pi->error = JSON_ERR_QUOTE;
        return JSON_NOK;
    }

    if (utstring_len(pi->key) > 0 || utstring_len(pi->value) > 0) {

        if (pi->elemData) {
            pi->elemData(pi->parser, utstring_body(pi->key), utstring_body(pi->value) );
        }
    }

    JsonParser_internalReset(pi);
    return 0;
}
Exemplo n.º 6
0
void mark_writable() {
  /* mark writability-interest for any clients with pending output */
  int *fd=NULL, *i=NULL;
  UT_string **s=NULL;
  while ( (fd=(int*)utarray_next(cfg.clients,fd))) {
    s=(UT_string**)utarray_next(cfg.outbufs,s); assert(s);
    i=(int*)utarray_next(cfg.outidxs,i);        assert(i);
    if (utstring_len(*s) > *i) mod_epoll(EPOLLIN|EPOLLOUT, *fd);
  }
}
Exemplo n.º 7
0
int set_to_binary(void *set, UT_string *bin) {
  uint32_t l, u, a,b,c,d, abcd;
  uint16_t s;
  uint8_t g;
  double h;
  utstring_clear(bin);
  l=0; utstring_bincpy(bin,&l,sizeof(l)); // placeholder for size prefix
  int rc=-1,i=0,*t;
  kv_t *kv, kvdef;
  char **k=NULL,**def;
  while( (k=(char**)utarray_next(output_keys,k))) {
    kv = kv_get(set,*k);
    t = (int*)utarray_eltptr(output_types,i); assert(t);
    def = (char**)utarray_eltptr(output_defaults,i); assert(def);
    if (kv==NULL) { /* no such key */
      kv=&kvdef;
      if (*def) {kv->val=*def; kv->vlen=strlen(*def);} /* default */
      else if (*t == str) {kv->val=NULL; kv->vlen=0;}  /* zero len string */
      else {
        fprintf(stderr,"required key %s not present in spool frame\n", *k);
        goto done;
      }
    }
    switch(*t) {
      case d64: h=atof(kv->val); utstring_bincpy(bin,&h,sizeof(h)); break;
      case i8:  g=atoi(kv->val); utstring_bincpy(bin,&g,sizeof(g)); break;
      case i16: s=atoi(kv->val); utstring_bincpy(bin,&s,sizeof(s)); break;
      case i32: u=atoi(kv->val); utstring_bincpy(bin,&u,sizeof(u)); break;
      case str: 
        l=kv->vlen; utstring_bincpy(bin,&l,sizeof(l)); /* length prefix */
        utstring_bincpy(bin,kv->val,kv->vlen);         /* string itself */
        break;
      case ipv4: 
        if ((sscanf(kv->val,"%u.%u.%u.%u",&a,&b,&c,&d) != 4) ||
           (a > 255 || b > 255 || c > 255 || d > 255)) {
          fprintf(stderr,"invalid IP for key %s: %s\n",*k,kv->val);
          goto done;
        }
        abcd = (a << 24) | (b << 16) | (c << 8) | d;
        abcd = htonl(abcd);
        utstring_bincpy(bin,&abcd,sizeof(abcd));
        break;
      default: assert(0); break;
    }
    i++;
  }
  uint32_t len = utstring_len(bin); len -= sizeof(len); // length does not include itself
  char *length_prefix = utstring_body(bin);
  memcpy(length_prefix, &len, sizeof(len));

  rc = 0;

 done:
  return rc;
}
Exemplo n.º 8
0
int o_sendstream_len(orientdb *o, orientdb_con *c) {
	OM_DEFINE_OBJECT(o_con,och);
	int len;
	
	OM_MUTEX_LOCK(o_handler, ohandler);
	och = OM_DECAPSULATE(o_con, c->och);
	len = utstring_len(och->send_stream);
	OM_MUTEX_UNLOCK(o_handler, ohandler);
	
	return len;
}
Exemplo n.º 9
0
int rates_cmd(void *cp, cp_arg_t *arg, void *data) {
  utstring_renew(CF.s);
  unsigned i,hits;
  char *when;
  for(i=0; i<CF.frames_1h->num_buckets; i++) {
    hits = *(unsigned*)(bkt(CF.frames_1h,i)->data);
    when = asctime(localtime(&(bkt(CF.frames_1h,i)->start)));
    utstring_printf(CF.s, " %u frames: %s", hits, when);
  }
  cp_add_reply(cp,utstring_body(CF.s),utstring_len(CF.s));
}
Exemplo n.º 10
0
static PyObject* binaryplist_encode(PyObject *self, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = {"obj", "unique", "debug", "convert_nulls",
                             "max_recursion", "object_hook", "as_ascii", NULL};
    PyObject *newobj = NULL;
    PyObject *oinput = NULL;
    PyObject *ounique = NULL;
    PyObject *odebug = NULL;
    PyObject *orecursion = NULL;
    binaryplist_encoder encoder;
    
    memset(&encoder, 0, sizeof(binaryplist_encoder));
    encoder.convert_nulls = Py_False;
    encoder.as_ascii = Py_False;
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OOOOOO", kwlist, &oinput, &ounique,
        &odebug, &(encoder.convert_nulls), &orecursion, &(encoder.object_hook),
        &(encoder.as_ascii))) {   
        return NULL;
    }
    if (encoder.object_hook && !PyCallable_Check(encoder.object_hook)) {
        PyErr_SetString(PLIST_Error, "object_hook is not callable");
        return NULL;
    }
    encoder.ref_table = PyDict_New();
    encoder.objects = PyList_New(0);
    utstring_new(encoder.output);
    if (!ounique || (ounique && PyObject_IsTrue(ounique))) {
        /* default to True */
        encoder.dounique = 1;
        encoder.uniques = PyDict_New();
    }
    if (odebug && PyObject_IsTrue(odebug)) {
        encoder.debug = 1;
    }
    if (orecursion && PyInt_Check(orecursion)) {
        encoder.max_recursion = PyInt_AsLong(orecursion);
    } else {
        encoder.max_recursion = 1024*16;
    }

    if (encoder_encode_object(&encoder, oinput) == BINARYPLIST_OK
        && encoder_write(&encoder) == BINARYPLIST_OK) {
        newobj = PyString_FromStringAndSize(utstring_body(encoder.output),
            utstring_len(encoder.output));
    }

    Py_DECREF(encoder.ref_table);
    Py_DECREF(encoder.objects);
    Py_XDECREF(encoder.uniques);
    utstring_free(encoder.output);

    return newobj;
}
Exemplo n.º 11
0
int main() {
    UT_string *s,*t;
    char V_TestStr[] = "There are two needle\0s in this \0haystack with needle\0s.";
    char V_NeedleStr[] = "needle\0s";
    long *V_KMP_Table;
    long V_FindPos;
    size_t V_StartPos;
    size_t V_FindCnt;


    utstring_new(s);
    utstring_new(t);

    utstring_bincpy(s, V_TestStr, sizeof(V_TestStr)-1);
    printf("\"%s\" len=%u\n", utstring_body(s), utstring_len(s));
    utstring_bincpy(t, V_NeedleStr, sizeof(V_NeedleStr)-1);
    printf("\"%s\" len=%u\n", utstring_body(t), utstring_len(t));

    V_KMP_Table = (long *)malloc(sizeof(long) * (utstring_len(t) + 1));
    if (V_KMP_Table != NULL)
    {
        _utstring_BuildTable(utstring_body(t), utstring_len(t), V_KMP_Table);

        V_FindCnt = 0;
        V_FindPos = 0;
        V_StartPos = 0;
        do
        {
            V_FindPos = _utstring_find(utstring_body(s) + V_StartPos, 
                                       utstring_len(s) - V_StartPos, 
                                       utstring_body(t), 
                                       utstring_len(t), 
                                       V_KMP_Table);
            if (V_FindPos >= 0)
            {
                V_FindPos += V_StartPos;
                V_FindCnt++;
                V_StartPos = V_FindPos + 1;
            }
            printf("utstring_find()=%ld\n", V_FindPos);
        } while (V_FindPos >= 0);
        printf("FindCnt=%u\n", V_FindCnt);

        free(V_KMP_Table);
    }
    else
    {
        printf("malloc() failed...\n");
    }

    utstring_free(s);
    utstring_free(t);

    return 0;
}
Exemplo n.º 12
0
// periodically we shift the output buffers down
// to reclaim the already written output regions
void shift_buffers() {
  int *fd=NULL, *i=NULL;
  UT_string **s=NULL;
  size_t len;

  while ( (fd=(int*)utarray_next(cfg.clients,fd))) {
    s=(UT_string**)utarray_next(cfg.outbufs,s); assert(s);
    i=(int*)utarray_next(cfg.outidxs,i);        assert(i);
    len = utstring_len(*s);
    if (*i == 0) continue; // nothing to shift 

    assert(*i > 0);
    memmove((*s)->d, (*s)->d + *i, len-*i);
    (*s)->i -= *i;
    *i = 0;
  }
}
Exemplo n.º 13
0
struct pkg_repo_it *
pkg_repo_binary_query(struct pkg_repo *repo, const char *pattern, match_t match)
{
	sqlite3 *sqlite = PRIV_GET(repo);
	sqlite3_stmt	*stmt = NULL;
	UT_string	*sql = NULL;
	const char	*comp = NULL;
	int		 ret;
	char		 basesql[BUFSIZ] = ""
		"SELECT id, origin, name, name as uniqueid, version, comment, "
		"prefix, desc, arch, maintainer, www, "
		"licenselogic, flatsize, pkgsize, "
		"cksum, manifestdigest, path AS repopath, '%s' AS dbname "
		"FROM packages AS p";

	if (match != MATCH_ALL && (pattern == NULL || pattern[0] == '\0'))
		return (NULL);

	utstring_new(sql);
	comp = pkgdb_get_pattern_query(pattern, match);
	if (comp && comp[0])
		strlcat(basesql, comp, sizeof(basesql));

	utstring_printf(sql, basesql, repo->name);

	utstring_printf(sql, "%s", " ORDER BY name;");

	pkg_debug(4, "Pkgdb: running '%s' query for %s", utstring_body(sql),
	     pattern == NULL ? "all": pattern);
	ret = sqlite3_prepare_v2(sqlite, utstring_body(sql), utstring_len(sql), &stmt,
	    NULL);
	if (ret != SQLITE_OK) {
		ERROR_SQLITE(sqlite, utstring_body(sql));
		utstring_free(sql);
		return (NULL);
	}

	utstring_free(sql);

	if (match != MATCH_ALL && match != MATCH_CONDITION)
		sqlite3_bind_text(stmt, 1, pattern, -1, SQLITE_TRANSIENT);

	return (pkg_repo_binary_it_new(repo, stmt, PKGDB_IT_FLAG_ONCE));
}
Exemplo n.º 14
0
Arquivo: app.c Projeto: stoni/luarest
static luarest_status verify_application(application** apps, const char* appName, UT_string* path)
{
	int ret;
	lua_State* ls = luaL_newstate();
	application* app;
	
	luaL_openlibs(ls);
	luaopen_luarestlibs(ls);
        
    ret = luaL_loadfile(ls, utstring_body(path));
    if (ret != 0) {
		printf("Couldn't load file: %s\n", lua_tostring(ls, -1));
		lua_close(ls);
		return(LUAREST_ERROR);
    }
	ret = lua_pcall(ls, 0, 0, 0);
    if (ret != 0) {
        printf("Couldn't execute LUA Script %s\n", lua_tostring(ls, -1));
		lua_close(ls);
		return(LUAREST_ERROR);
    }
	lua_getglobal(ls, "luarest_init");
    if (lua_isfunction(ls, -1) == 0) {
		printf("Couln'd find 'luarest_init' table in LUA script!\n");
		lua_close(ls);
		return(LUAREST_ERROR);
	}
	app = (application*)lua_newuserdata(ls, sizeof(application));
	luaL_getmetatable(ls, LUA_USERDATA_APPLICATION);
	lua_setmetatable(ls, -2);
	app->s = NULL;
	utstring_new(app->name);
	utstring_printf(app->name, appName);
	if (lua_pcall(ls, 1, 0, 0) != 0) {
		printf("Error calling luarest_init: %s\n!", lua_tostring(ls, -1));
		lua_close(ls);
		return(LUAREST_ERROR);
	}
	app->lua_state = ls;
	HASH_ADD_KEYPTR(hh, *apps, utstring_body(app->name), utstring_len(app->name), app);
	return(LUAREST_SUCCESS);
}
Exemplo n.º 15
0
/* PLAYING WITH STRINGS */
int o_putstring(orientdb *o, orientdb_con *c, UT_string *str) {
	OM_DEFINE_OBJECT(o_con,och);
	int i,len;
	char *buf;
	
	len = utstring_len(str);
	
	debug_note(o, ORIENT_DEBUG, "put a string\n");
	// send an integer to orientdb with the length of the string that we are ready to send
	i = o_putint(o, c, len);
	if (i < 0) return -1;
	buf = utstring_body(str);
	
	OM_MUTEX_LOCK(o_handler, ohandler);
	och = OM_DECAPSULATE(o_con, c->och);
	utstring_concat(och->send_stream, str);
	OM_MUTEX_UNLOCK(o_handler, ohandler);
	
	debug_note(o, ORIENT_DEBUG, "STRING: put (4)+%i bytes of data to the wire: %s\n", len, buf);
	return i+len;
}
Exemplo n.º 16
0
Arquivo: app.c Projeto: stoni/luarest
/**
 * LUA syntax: application.register(method, url, callback)
 *
 * Return: boolean true on success
 *
 */
static int l_register(lua_State* state)
{
	service* s;
	application* a = (application*)luaL_checkudata(state, 1, LUA_USERDATA_APPLICATION);
	int method = luaL_checkint(state, 2);
	const char* url = luaL_checkstring(state, 3);
	int ref = luaL_ref(state, LUA_REGISTRYINDEX);

	s = (service*)malloc(sizeof(service));
	utstring_new(s->key);
	utstring_printf(s->key, "M%d#P%s", method, url);
	switch (method)
	{
		case 1:
			s->method = HTTP_METHOD_GET;
			break;
		case 2:
			s->method = HTTP_METHOD_POST;
			break;
		case 3:
			s->method = HTTP_METHOD_PUT;
			break;
		case 4:
			s->method = HTTP_METHOD_DELETE;
			break;
		case 5:
			s->method = HTTP_METHOD_OPTION;
			break;
		case 6:
			s->method = HTTP_METHOD_HEAD;
			break;
	}
	utstring_new(s->path);
	utstring_printf(s->path, url);
	s->callback_ref = ref;
	HASH_ADD_KEYPTR(hh, a->s, utstring_body(s->key), utstring_len(s->key), s);

	return(1);
}
Exemplo n.º 17
0
int main() {
    UT_string *s,*t;
    char V_TestStr[] = "There are two needle\0s in this \0haystack with needle\0s.";
    char V_NeedleStr[] = "needle\0s";
    long *V_KMP_Table;
    long V_FindPos;
    size_t V_StartPos;
    size_t V_FindCnt;


    utstring_new(s);
    utstring_new(t);

    utstring_bincpy(s, V_TestStr, sizeof(V_TestStr)-1);
    printf("\"%s\" len=%u\n", utstring_body(s), utstring_len(s));
    utstring_bincpy(t, V_NeedleStr, sizeof(V_NeedleStr)-1);
    printf("\"%s\" len=%u\n", utstring_body(t), utstring_len(t));

    V_KMP_Table = (long *)malloc(sizeof(long) * (utstring_len(t) + 1));
    if (V_KMP_Table != NULL)
    {
        _utstring_BuildTableR(utstring_body(t), utstring_len(t), V_KMP_Table);

        V_FindCnt = 0;
        V_FindPos = 0;
        V_StartPos = utstring_len(s) - 1;
        do
        {
            V_FindPos = _utstring_findR(utstring_body(s), 
                                        V_StartPos + 1, 
                                        utstring_body(t), 
                                        utstring_len(t), 
                                        V_KMP_Table);
            if (V_FindPos >= 0)
            {
                V_FindCnt++;
                V_StartPos = V_FindPos - 1;
            }
            printf("utstring_find()=%ld\n", V_FindPos);
        } while (V_FindPos >= 0);
        printf("FindCnt=%u\n", V_FindCnt);

        free(V_KMP_Table);
    }
    else
    {
        printf("malloc() failed...\n");
    }

    utstring_free(t);
    utstring_clear(s);
    utstring_printf(s,"ABC ABCDAB ABCDABCDABDE");
    int o;

    o=utstring_find(  s, -9, "ABC", 3 ) ; printf("expect 15 %d\n",o);
    o=utstring_find(  s,  3, "ABC", 3 ) ; printf("expect  4 %d\n",o);
    o=utstring_find(  s, 16, "ABC", 3 ) ; printf("expect -1 %d\n",o);
    o=utstring_findR( s, -9, "ABC", 3 ) ; printf("expect 11 %d\n",o);
    o=utstring_findR( s, 12, "ABC", 3 ) ; printf("expect  4 %d\n",o);
    o=utstring_findR( s, 13, "ABC", 3 ) ; printf("expect 11 %d\n",o);
    o=utstring_findR( s,  2, "ABC", 3 ) ; printf("expect  0 %d\n",o);
    


    utstring_free(s);

    return 0;
}
Exemplo n.º 18
0
unsigned long int fq_stream_trimmer(UT_string *fq_fn, int pipe_fd, UT_string *out_prefix, int no_pre, int len_pre, unsigned long int *comp_cnt, unsigned long int *org, char split, int fmt_fasta) {
    
    UT_string *new_head_data;
    utstring_new(new_head_data);
    UT_string *head_data;
    utstring_new(head_data);
    UT_string *seq_data;
    utstring_new(seq_data);
    UT_string *extra_data;
    utstring_new(extra_data);
    UT_string *qual_data;
    utstring_new(qual_data);
    
    unsigned long int cnt = 0;
    
    char *start = NULL; 
    char *end = NULL;
    char *suffix = NULL;
    
    FILE *fq_file = NULL;

    FILE *pipe_in = fdopen(pipe_fd, "w");
    
    if (!(utstring_len(fq_fn))) {
        fclose(pipe_in);
        return(0);
    }
    
    // Try to open the fastq file
    if (!(fq_file = gzopen(utstring_body(fq_fn), "r"))) {
        utstring_printf(fq_fn, ".gz");
        if (!(fq_file = gzopen(utstring_body(fq_fn), "r"))) {
            fclose(pipe_in);
            return(0);
        }
    } 
    
    int x = 0;
    
    char head_char = '@';
    
    if (fmt_fasta) {
        head_char = '>';
    }
    
    while (ss_gzget_utstring(fq_file, head_data)) {
        
        ss_gzget_utstring(fq_file, seq_data);
        ss_gzget_utstring(fq_file, extra_data);
        ss_gzget_utstring(fq_file, qual_data);
        
        if (!split || ((suffix = strchr(utstring_body(head_data), '/')) && (suffix[1] == split))) {

            (*org)++;
            
            if ((x = trimmer(utstring_body(qual_data))) >= min_len) {  // Keep at least some of read
                
                // Reject read if complexity is too low
                if ((entropy_cutoff < 0.0) || (entropy_calc(utstring_body(seq_data), x) >= entropy_cutoff)) {
                    
                    // Truncate sequence
                    
                    ss_trim_utstring(seq_data, x);
                    ss_strcat_utstring(seq_data, "\n");
                    
                    if (!fmt_fasta) {
                        ss_trim_utstring(qual_data, x);
                        ss_strcat_utstring(qual_data, "\n");
                    }
                    
                    // Fixup the read name
                    utstring_clear(new_head_data);
                    
                    end = strchr(utstring_body(head_data), ':');
                    
                    if (no_pre) {
                        if ((start = strchr(utstring_body(head_data), '|'))) {
                            start++;
                        } else {
                            if (colorspace_flag) {
                                start = utstring_body(head_data) + 4;
                            } else {
                                start = utstring_body(head_data) + 1;
                            }
                        }
                        *end = '\0';
                    } else {
                        start = utstring_body(out_prefix);
                    }
                    
                    end++;
                    
                    if (colorspace_flag) {
                        if (len_pre) {
                            utstring_printf(new_head_data, "%c%.2s+%u|%s:%s",head_char,utstring_body(head_data)+1,x,start,end);
                        } else {
                            utstring_printf(new_head_data, "%c%.2s+%s:%s",head_char,utstring_body(head_data)+1,start,end);
                        }
                    } else {
                        if (len_pre) {
                            utstring_printf(new_head_data, "%c%u|%s:%s",head_char,x,start,end);
                        } else {
                            utstring_printf(new_head_data, "%c%s:%s",head_char,start,end);
                        }
                    }
                    
                    fputs(utstring_body(new_head_data), pipe_in);
                    fputs(utstring_body(seq_data), pipe_in);
                    
                    if (!fmt_fasta) {
                        fputs(utstring_body(extra_data), pipe_in);
                        fputs(utstring_body(qual_data), pipe_in);
                    }
                
                    cnt++;
                    
                } else {
                    // rejected by entropy filter
                    // Send along placeholder read to be discarded, keeping read1 and read2 in sync
                    // Empty fastq header is a read to be rejected by consumer threads
                    (*comp_cnt)++;
                    fputs("\n", pipe_in);
                }
            } else {
                // rejected by minimum length cutoff
                // Send along placeholder read to be discarded, keeping read1 and read2 in sync
                // Empty fastq header is a read to be rejected by consumer threads
                fputs("\n", pipe_in);
            }
        }
    }
    
    fclose(pipe_in);
    
    gzclose(fq_file);
    
    utstring_free(new_head_data);
    utstring_free(head_data);
    utstring_free(seq_data);
    utstring_free(extra_data);
    utstring_free(qual_data);

    return(cnt);
}
Exemplo n.º 19
0
void handle_index( struct evhttp_request* req, void* dummy )
{
	assert(req);
	
	//	printf( "HEADERS: %s\n", req->input_headers );


/* 	struct evkeyval *item = NULL; */
/* 	puts( "HEADERS" ); */
/* 	TAILQ_FOREACH( item, req->input_headers, next ) */
/* 		printf( "HEADER: %s = %s\n", item->key, item->value ); */
	
/* 	const char* accept = evhttp_find_header( req->input_headers, "Accept" ); */

/*   printf( "ACCEPT: %s\n", accept ? accept : "<not found" ); */

	//add_std_hdrs( req );

	switch(req->type )
		{
		case EVHTTP_REQ_GET:
			{			 
				char server_str[512];
				snprintf( server_str, 512, "%s-%s (%s-%s)", 
									_av.backend_name, _av.backend_version,
									_package, _version );
				
				evhttp_add_header(req->output_headers, 
													"Server", server_str ); // todo: insert version number  
				evhttp_add_header(req->output_headers,"Content-Type", "text/HTML; charset=UTF-8");  
				//evhttp_add_header(req->output_headers, "Access-Control-Allow-Origin", "*"); 
				//evhttp_add_header(req->output_headers, "Access-Control-Allow-Methods", "POST, GET, OPTIONS");  

				struct evbuffer* eb = evbuffer_new();
				assert(eb);

				UT_string* page = uts_new();
				assert(page);

				utstring_printf( page, 
												 "<h1>%s-%s</h1>"
												 "<h2>Objects</h2>",
												 _av.backend_name, // implemented by the simulator
												 _av.backend_version );
				
				utstring_printf(page, "<table>\n"
												"<tr><th>name<th>interface<th>prototype</tr>\n" );
				html_tree( page, "", NULL );
				utstring_printf(page, "\n</table>\n" );
								
				utstring_printf( page, 
												 "<hr> Served by %s-%s <hr> %s",												
												 _package,
												 _version,
												 HTML_BOILERPLATE_FOOTER);

				evbuffer_add( eb, utstring_body(page), utstring_len(page) );							
				evhttp_send_reply( req, HTTP_OK, "Success", eb);			 
				evbuffer_free( eb );
				free(page);
			} break;
		case EVHTTP_REQ_HEAD:						
		 reply_success( req, HTTP_OK, "Success", NULL );			
		 break;		 
		case EVHTTP_REQ_POST:
			reply_error( req, HTTP_NOTMODIFIED, "POST index not implemented" );			 
			break;
		default:
			reply_error( req, HTTP_NOTMODIFIED, "unknown HTTP request type in handle index" );			 
		}
}
Exemplo n.º 20
0
int stats_cmd(void *cp, cp_arg_t *arg, void *data) {
  utstring_renew(CF.s);
  utstring_printf(CF.s, "frames_read:      %u\n", CF.frames_read);
  utstring_printf(CF.s, "frames_processed: %u\n", CF.frames_processed);
  cp_add_reply(cp,utstring_body(CF.s),utstring_len(CF.s));
}
Exemplo n.º 21
0
cxInt cxStringLength(cxString string)
{
    CX_RETURN(string == NULL, 0);
    return utstring_len(&string->strptr);
}
Exemplo n.º 22
0
static int
pkg_create_from_dir(struct pkg *pkg, const char *root,
    struct packing *pkg_archive)
{
	char		 fpath[MAXPATHLEN];
	struct pkg_file	*file = NULL;
	struct pkg_dir	*dir = NULL;
	int		 ret;
	struct stat	 st;
	int64_t		 flatsize = 0;
	int64_t		 nfiles;
	const char	*relocation;
	hardlinks_t	*hardlinks;

	if (pkg_is_valid(pkg) != EPKG_OK) {
		pkg_emit_error("the package is not valid");
		return (EPKG_FATAL);
	}

	relocation = pkg_kv_get(&pkg->annotations, "relocated");
	if (relocation == NULL)
		relocation = "";
	if (pkg_rootdir != NULL)
		relocation = pkg_rootdir;

	/*
	 * Get / compute size / checksum if not provided in the manifest
	 */

	nfiles = kh_count(pkg->filehash);
	counter_init("file sizes/checksums", nfiles);

	hardlinks = kh_init_hardlinks();
	while (pkg_files(pkg, &file) == EPKG_OK) {

		snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "",
		    relocation, file->path);

		if (lstat(fpath, &st) == -1) {
			pkg_emit_error("file '%s' is missing", fpath);
			kh_destroy_hardlinks(hardlinks);
			return (EPKG_FATAL);
		}

		if (file->size == 0)
			file->size = (int64_t)st.st_size;

		if (st.st_nlink == 1 || !check_for_hardlink(hardlinks, &st)) {
			flatsize += file->size;
		}

		file->sum = pkg_checksum_generate_file(fpath,
		    PKG_HASH_TYPE_SHA256_HEX);
		if (file->sum == NULL) {
			kh_destroy_hardlinks(hardlinks);
			return (EPKG_FATAL);
		}

		counter_count();
	}
	kh_destroy_hardlinks(hardlinks);

	counter_end();

	pkg->flatsize = flatsize;

	if (pkg->type == PKG_OLD_FILE) {
		pkg_emit_error("Cannot create an old format package");
		return (EPKG_FATAL);
	}
	/*
	 * Register shared libraries used by the package if
	 * SHLIBS enabled in conf.  Deletes shlib info if not.
	 */
	UT_string *b;
	utstring_new(b);

	pkg_emit_manifest_buf(pkg, b, PKG_MANIFEST_EMIT_COMPACT, NULL);
	packing_append_buffer(pkg_archive, utstring_body(b), "+COMPACT_MANIFEST", utstring_len(b));
	utstring_clear(b);
	pkg_emit_manifest_buf(pkg, b, 0, NULL);
	packing_append_buffer(pkg_archive, utstring_body(b), "+MANIFEST", utstring_len(b));
	utstring_free(b);

	counter_init("packing files", nfiles);

	while (pkg_files(pkg, &file) == EPKG_OK) {

		snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "",
		    relocation, file->path);

		ret = packing_append_file_attr(pkg_archive, fpath, file->path,
		    file->uname, file->gname, file->perm, file->fflags);
		if (developer_mode && ret != EPKG_OK)
			return (ret);
		counter_count();
	}

	counter_end();

	nfiles = kh_count(pkg->dirhash);
	counter_init("packing directories", nfiles);

	while (pkg_dirs(pkg, &dir) == EPKG_OK) {
		snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "",
		    relocation, dir->path);

		ret = packing_append_file_attr(pkg_archive, fpath, dir->path,
		    dir->uname, dir->gname, dir->perm, dir->fflags);
		if (developer_mode && ret != EPKG_OK)
			return (ret);
		counter_count();
	}

	counter_end();

	return (EPKG_OK);
}
Exemplo n.º 23
0
/* flush as much pending output to the client as it can handle. */
void feed_client(int ready_fd, int events) {
  int *fd=NULL, rc, pos, rv, *p;
  char *buf, tmp[100];
  size_t len;
  UT_string **s=NULL;

  /* find the fd in our list */
  while ( (fd=(int*)utarray_next(cfg.clients,fd))) {
    s=(UT_string**)utarray_next(cfg.outbufs,s); assert(s);
    pos = utarray_eltidx(cfg.clients, fd);
    if (ready_fd == *fd) break;
  }
  assert(fd);

  if (cfg.verbose > 1) {
    fprintf(stderr, "pollout:%c pollin: %c\n", (events & EPOLLOUT)?'1':'0',
                                               (events & EPOLLIN) ?'1':'0');
  }

  /* before we write to the client, drain any input or closure */
  rv = recv(*fd, tmp, sizeof(tmp), MSG_DONTWAIT);
  if (rv == 0) {
    fprintf(stderr,"client closed (eof)\n");
    close(*fd); /* deletes epoll instances on *fd too */
    discard_client_buffers(pos);
    return;
  }

  if ((events & EPOLLOUT) == 0) return;

  /* send the pending buffer to the client */
  p = (int*)utarray_eltptr(cfg.outidxs,pos);
  buf = utstring_body(*s) + *p;
  len = utstring_len(*s)  - *p;
  rc = send(*fd, buf, len, MSG_DONTWAIT);
  if (cfg.verbose) fprintf(stderr,"sent %d/%d bytes\n", rc, (int)len);

  /* test for client closure or error. */
  if (rc < 0) {
    if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) return;
    fprintf(stderr,"client closed (%s)\n", strerror(errno));
    close(*fd); /* deletes all epoll instances on *fd too */
    discard_client_buffers(pos);
    return;
  }

  /* advance output index in the output buffer; we wrote rc bytes */
  if (rc < len) {
    *p += rc;
    cfg.obpp += rc;
  } else {
    *p = 0;
    utstring_clear(*s);     // buffer emptied
    mod_epoll(EPOLLIN,*fd); // remove EPOLLOUT 
  }

#if 1
  shift_buffers();
  int kc;
  while (have_capacity()) {
    kc = kv_spool_read(cfg.sp,cfg.set,0);
    if (kc <  0) goto done; // error
    if (kc == 0) break;     // no data
    cfg.ompp++;
    if (set_to_binary(cfg.set, cfg.s)) goto done;
    append_to_client_buf(cfg.s);
  }
  mark_writable();
 done:
  return;
#endif
}
Exemplo n.º 24
0
char *mtex2MML_vertical_pipe_extract(char *string)
{
  char *dupe = string_dup(string);
  UT_string *columnlines, *border;
  char *previous_column = "", *attr_columnlines, *attr_border;
  unsigned int i = 0;

  utstring_new(columnlines);
  utstring_new(border);

  /* the first character (if it exists) determines the frame border */
  if (strncmp(dupe, "s", 1) == 0) {
    utstring_printf(columnlines, "%s", "frame=\"solid\" columnlines=\"");
    mtex2MML_remove_first_char(dupe);
  } else if (strncmp(dupe, "d", 1) == 0) {
    utstring_printf(columnlines, "%s", "frame=\"dashed\" columnlines=\"");
    mtex2MML_remove_first_char(dupe);
  } else {
    utstring_printf(columnlines, "%s", "columnlines=\"");
  }

  char *token = strtok(dupe, " ");

  while (token != NULL) {
    if (strncmp(token, "s", 1) == 0) {
      previous_column = "s";
      utstring_printf(border, "%s ", "solid");
    } else if (strncmp(token, "d", 1) == 0) {
      previous_column = "d";
      utstring_printf(border, "%s ", "dashed");
    } else {
      /* we must skip the first blank col only if there is no previous border
         should a border be considered, eg. "cc", not "c|c" */
      if (i >= 1) {
        if (strncmp(previous_column, "s", 1) != 0 && strncmp(previous_column, "d", 1) != 0) {
          utstring_printf(border, "%s ", "none");
        }
        previous_column = "0";
      }
    }

    i++;
    token = strtok(NULL, " ");
  }

  attr_border = utstring_body(border);
  if (strlen(attr_border) > 0) {
    mtex2MML_remove_last_char(attr_border); /* remove the final space */
  }
  utstring_printf(columnlines, "%s", attr_border);

  /* an empty string here angers Lasem, so let's remember to add 'none' */
  if (utstring_len(border) == 0) {
    utstring_printf(columnlines, "%s", "none");
  }

  attr_columnlines = string_dup(utstring_body(columnlines));
  free(dupe);
  utstring_free(border);
  utstring_free(columnlines);

  return attr_columnlines;
}