Esempio n. 1
0
File: cdbc.c Progetto: jpmens/cdbc
CDBC *cdbc_new(char *baseurl)
{
    CDBC *cd;

    if (!curl_global) {
        curl_global = 1;
        curl_global_init(CURL_GLOBAL_DEFAULT);
    }

    if ((cd = (CDBC *)calloc(1, sizeof (struct _cdbc))) == NULL)
        return (NULL);

    cd->dbname	= NULL;

    cd->baseurl	= strdup(baseurl);

    /*
     * Ensure base URL has no trailing slash; I'll be adding that
     * later on, and CouchDB doesn't like the double-slash.
     */

    if (cd->baseurl[strlen(cd->baseurl) - 1] == '/')
        cd->baseurl[strlen(cd->baseurl) - 1] = '\0';

    dsinit(&cd->buf);
    dsinit(&cd->url);

    if ((cd->con = curl_easy_init()) == NULL) {
        cdbc_free(cd);
        return (NULL);
    }

    cdbc_addheader(cd, "Content-type: application/json");
    cdbc_addheader(cd, "Y-JP: true");
    cdbc_addheader(cd, "Expect:");	// Turn it off

    Cso(CURLOPT_HTTPHEADER, cd->hlist);

    Cso(CURLOPT_FAILONERROR, 0);
    Cso(CURLOPT_FILE, stdout );
    Cso(CURLOPT_WRITEDATA, &cd->buf );		// `ptr' in collect()
    Cso(CURLOPT_WRITEFUNCTION, collect );

    Cso(CURLOPT_USERAGENT, "CDBC");
    Cso(CURLOPT_VERBOSE, FALSE);


    Cso(CURLOPT_ENCODING, "deflate");
    Cso(CURLOPT_ENCODING, "gzip");


    return (cd);
}
Esempio n. 2
0
File: cdbc.c Progetto: jpmens/cdbc
int cdbc_view_walk(CDBC *cd, int (*func)(CDBC *, json_t *obj, void *userdata), void *userdata, char *ddoc, char *view, char *arg, ...)
{
    va_list ap;
    char *s;
    str uri;
    int rc;

    if (!cd || !cd->dbname)
        return (CDBC_ERROR);

    dsinit(&uri);

    dscat(&uri, cd->dbname);
    dscat(&uri, "/_design/");
    dscat(&uri, ddoc);
    dscat(&uri, "/_view/");
    dscat(&uri, view);


    if (arg && *arg) {
        dsadd(&uri, '?');
        dscat(&uri, arg);

        va_start(ap, arg);
        while ((s = va_arg(ap, char *)) != NULL) {
            dsadd(&uri, '&');
            dscat(&uri, s);
        }
        va_end(ap);
    }
Esempio n. 3
0
int
initiostat(void)
{
	if ((num_devices = devstat_getnumdevs(NULL)) < 0)
		return(0);

	cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
	last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
	bzero(cur.dinfo, sizeof(struct devinfo));
	bzero(last.dinfo, sizeof(struct devinfo));

	/*
	 * This value for maxshowdevs (100) is bogus.  I'm not sure exactly
	 * how to calculate it, though.
	 */
	if (dsinit(100, &cur, &last, NULL) != 1)
		return(0);

	return(1);
}
Esempio n. 4
0
File: cdbc.c Progetto: jpmens/cdbc
int cdbc_get(CDBC *cd, char *docid)
{
    str uri;
    int rc;

    if (!cd)
        return CDBC_ERROR;

    if (!cd->dbname)
        return CDBC_NODBNAME;

    dsinit(&uri);

    dscat(&uri, cd->dbname);
    dsadd(&uri, '/');
    dscat(&uri, docid);

    rc = cdbc_request(cd, (char *)dsstring(&uri));
    dsfree(&uri);
    return (rc);

}
Esempio n. 5
0
int
initkre(void)
{
	char *intrnamebuf;
	size_t bytes;
	size_t b;
	size_t i;

	if (namelist[0].n_type == 0) {
		if (kvm_nlist(kd, namelist)) {
			nlisterr(namelist);
			return(0);
		}
		if (namelist[0].n_type == 0) {
			error("No namelist");
			return(0);
		}
	}

	if ((num_devices = getnumdevs()) < 0) {
		warnx("%s", devstat_errbuf);
		return(0);
	}

	cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
	last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
	run.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
	bzero(cur.dinfo, sizeof(struct devinfo));
	bzero(last.dinfo, sizeof(struct devinfo));
	bzero(run.dinfo, sizeof(struct devinfo));

	if (dsinit(MAXDRIVES, &cur, &last, &run) != 1)
		return(0);

	if (nintr == 0) {
		if (sysctlbyname("hw.intrnames", NULL, &bytes, NULL, 0) == 0) {
			intrnamebuf = malloc(bytes);
			sysctlbyname("hw.intrnames", intrnamebuf, &bytes,
					NULL, 0);
			for (i = 0; i < bytes; ++i) {
				if (intrnamebuf[i] == 0)
					++nintr;
			}
			intrname = malloc(nintr * sizeof(char *));
			intrloc = malloc(nintr * sizeof(*intrloc));
			nintr = 0;
			for (b = i = 0; i < bytes; ++i) {
				if (intrnamebuf[i] == 0) {
					intrname[nintr] = intrnamebuf + b;
					intrloc[nintr] = 0;
					b = i + 1;
					++nintr;
				}
			}
		}
		nextintsrow = INTSROW + 2;
		allocinfo(&s);
		allocinfo(&s1);
		allocinfo(&s2);
		allocinfo(&z);
	}
	getinfo(&s2);
	copyinfo(&s2, &s1);
	return(1);
}
Esempio n. 6
0
int
initkre(void)
{
	char *cp, *cp1, *cp2, *intrnamebuf, *nextcp;
	int i;
	size_t sz;

	if ((num_devices = devstat_getnumdevs(NULL)) < 0) {
		warnx("%s", devstat_errbuf);
		return(0);
	}

	cur.dinfo = calloc(1, sizeof(struct devinfo));
	last.dinfo = calloc(1, sizeof(struct devinfo));
	run.dinfo = calloc(1, sizeof(struct devinfo));

	if (dsinit(MAXDRIVES, &cur, &last, &run) != 1)
		return(0);

	if (nintr == 0) {
		if (sysctlbyname("hw.intrcnt", NULL, &sz, NULL, 0) == -1) {
			error("sysctl(hw.intrcnt...) failed: %s",
			      strerror(errno));
			return (0);
		}
		nintr = sz / sizeof(u_long);
		intrloc = calloc(nintr, sizeof (long));
		intrname = calloc(nintr, sizeof (char *));
		intrnamebuf = sysctl_dynread("hw.intrnames", NULL);
		if (intrnamebuf == NULL || intrname == NULL ||
		    intrloc == NULL) {
			error("Out of memory");
			if (intrnamebuf)
				free(intrnamebuf);
			if (intrname)
				free(intrname);
			if (intrloc)
				free(intrloc);
			nintr = 0;
			return(0);
		}
		for (cp = intrnamebuf, i = 0; i < nintr; i++) {
			nextcp = cp + strlen(cp) + 1;

			/* Discard trailing spaces. */
			for (cp1 = nextcp - 1; cp1 > cp && *(cp1 - 1) == ' '; )
				*--cp1 = '\0';

			/* Convert "irqN: name" to "name irqN". */
			if (strncmp(cp, "irq", 3) == 0) {
				cp1 = cp + 3;
				while (isdigit((u_char)*cp1))
					cp1++;
				if (cp1 != cp && *cp1 == ':' &&
				    *(cp1 + 1) == ' ') {
					sz = strlen(cp);
					*cp1 = '\0';
					cp1 = cp1 + 2;
					cp2 = strdup(cp);
					bcopy(cp1, cp, sz - (cp1 - cp) + 1);
					if (sz <= 10 + 4) {
						strcat(cp, " ");
						strcat(cp, cp2 + 3);
					}
					free(cp2);
				}
			}

			/*
			 * Convert "name irqN" to "name N" if the former is
			 * longer than the field width.
			 */
			if ((cp1 = strstr(cp, "irq")) != NULL &&
			    strlen(cp) > 10)
				bcopy(cp1 + 3, cp1, strlen(cp1 + 3) + 1);

			intrname[i] = cp;
			cp = nextcp;
		}
		nextintsrow = INTSROW + 2;
		allocinfo(&s);
		allocinfo(&s1);
		allocinfo(&s2);
		allocinfo(&z);
	}
	GETSYSCTL("vm.kmem_size", kmem_size);
	GETSYSCTL("vm.stats.vm.v_page_count", v_page_count);
	getinfo(&s2);
	copyinfo(&s2, &s1);
	return(1);
}