/* ################################################################## */ static int find_next_kv(char *buf, int len, char **key, int *key_len, char **value, int *value_len, char **nbuf, int *nlen) { char *p = buf, *p1, *p2, *pe, *pn = 0; int find; p = ignore_chs(p, len, ";\t \r\n", 4, 1); if (!p) { return -1; } *nbuf = 0; *key = p; pe = memchr(p, '=', len - (p - buf)); if (!pe) { return -1; } *pe = 0; p1 = find_delim(p, pe - p, "\t \r\n", 4); if (p1) { *p1 = 0; } *key_len = strlen(p); p = pe + 1; p = ignore_chs(p, len - (p - buf), "\t \r\n", 4, 1); if (!p) { return -1; } find = 0; if (*p == '"') { p++; find = 1; } *value = p; if (find) { p2 = find_delim(p, len - (p - buf), "\"\r\n", 3); } else { p2 = find_delim(p, len - (p - buf), "\t ;\r\n", 5); } if (p2) { *value_len = p2 - p; pn = p2 + 1; } else { *value_len = strlen(p); } if (pn) { *nbuf = pn; *nlen = len - (pn - buf); } return 0; }
/* Documented in io.h */ int lsi_io_read(sckhld sh, struct readctx *rctx, tokarr *tok, uint64_t to_us) { uint64_t tend = to_us ? lsi_b_tstamp_us() + to_us : 0; uint64_t tnow, trem = 0; V("Wanna read with%s timeout (%"PRIu64"). %zu bytes in buffer: '%.*s'", tend?"":" no", to_us, rctx->eptr - rctx->wptr, (int)(rctx->eptr - rctx->wptr), rctx->wptr); while (rctx->wptr < rctx->eptr && ISDELIM(*rctx->wptr)) rctx->wptr++; /* skip leading line delimiters */ if (rctx->wptr == rctx->eptr) { /* empty buffer, use the opportunity.. */ rctx->wptr = rctx->eptr = rctx->workbuf; V("Opportunistical buffer reset"); } size_t linelen; char *delim; char *linestart; do { while (!(delim = find_delim(rctx))) { if (tend) { tnow = lsi_b_tstamp_us(); trem = tnow >= tend ? 1 : tend - tnow; } int r = read_more(sh, rctx, trem); if (r <= 0) return r; } linestart = rctx->wptr; linelen = delim - linestart; rctx->wptr++; V("Delim found, linelen %zu", linelen); } while (linelen == 0); rctx->wptr += linelen; *delim = '\0'; I("Read: '%s'", linestart); return lsi_ut_tokenize(linestart, tok) ? 1 : -1; }
static int find_value(char *buf, int len, char **value, int *value_len, char **nbuf, int *nlen) { char *p = buf, *p1; p = ignore_chs(p, len, "\t \"", 3, 1); if (!p) { return -1; } *nbuf = 0; *value = p; p1 = find_delim(p, len - (p - buf), ";\t \"\r\n", 6); if (p1) { *value_len = p1 - p; *nbuf = p1 + 1; *nlen = len - (p1 + 1 - buf); } else { *value_len = len - (p - buf); } return 0; }
int main(int argc, char **argv) { unsigned int major = 0, mid = 0, minor = 0; char vbuf[4096] = { 0 }; char *version="0.0.0", *ncommits="0", *sha="0"; FILE *po; int buflen; int outval; // Open output file char * filename = argv[1]; if (!filename) { fprintf(stderr, "Bad output file name\n"); return -5; } FILE * f; errno_t err = fopen_s(&f, filename, "w+"); if (err || !f) { fprintf(stderr, "Could not oupen output file name (errno=%d)\n", err); return -6; } fprintf(stderr, "Starting CreateVersion\n"); if (argc != 2) { fprintf(stderr, "Usage: %s <Outputfile>\n", argv[0]); outval = -1; goto FINAL; } /** Get the output */ po = popen("git describe --long --match v*", "r"); if (!po) { perror("git describe"); outval = -2; goto FINAL; } if (!fgets(vbuf, sizeof(vbuf), po)) { fprintf(stderr, "git describe closed stream\n"); outval = -3; goto FINAL; } pclose(po); buflen = (int)strlen(vbuf); vbuf[buflen - 2] = '\0'; buflen -= 1; // SHA1 (Remove the leading 'g') find_delim(vbuf, &sha, &buflen); sha++; // Commit # - Will be used as build number find_delim(vbuf, &ncommits, &buflen); // Version version = vbuf; // Break the version number into its components int nFields = sscanf_s(version, "v%u.%u.%u",&major, &mid, &minor) ; if (nFields != 3) { fprintf(stderr, "Version string corrupt\n"); outval = -4; goto FINAL; } FINAL: // Put data into file fprintf(f, "#pragma once\n"); fprintf(f, "#define VER_H_ %u\n", major); fprintf(f, "#define VER_M_ %u\n", mid); fprintf(f, "#define VER_L_ %u\n", minor); fprintf(f, "#define BUILD %s\n", ncommits); fprintf(f, "\n"); fprintf(f, "#define FILEVER\t\t\t\t\"%u, %u, %u, %s\"\n", major, mid, minor, ncommits); fprintf(f, "#define PRODVER_TAG\t\t\t\"%s\"\n", version); fprintf(f, "#define PRODVER_SHA1\t\t\"%s\"\n", sha); fprintf(f, "\n"); return 0; }
/* Parse an AMQP URL into its component parts. */ int amqp_parse_url(char *url, struct amqp_connection_info *parsed) { int res = -ERROR_BAD_AMQP_URL; char delim; char *start; char *host; char *port = NULL; /* check the prefix */ if (strncmp(url, "amqp://", 7)) goto out; host = start = url += 7; delim = find_delim(&url, 1); if (delim == ':') { /* The colon could be introducing the port or the password part of the userinfo. We don't know yet, so stash the preceding component. */ port = start = url; delim = find_delim(&url, 1); } if (delim == '@') { /* What might have been the host and port were in fact the username and password */ parsed->user = host; if (port) parsed->password = port; port = NULL; host = start = url; delim = find_delim(&url, 1); } if (delim == '[') { /* IPv6 address. The bracket should be the first character in the host. */ if (host != start || *host != 0) goto out; start = url; delim = find_delim(&url, 0); if (delim != ']') goto out; parsed->host = start; start = url; delim = find_delim(&url, 1); /* Closing bracket should be the last character in the host. */ if (*start != 0) goto out; } else { /* If we haven't seen the host yet, this is it. */ if (*host != 0) parsed->host = host; } if (delim == ':') { port = start = url; delim = find_delim(&url, 1); } if (port) { char *end; long portnum = strtol(port, &end, 10); if (port == end || *end != 0 || portnum < 0 || portnum > 65535) goto out; parsed->port = portnum; } if (delim == '/') { start = url; delim = find_delim(&url, 1); if (delim != 0) goto out; parsed->vhost = start; res = 0; } else if (delim == 0) { res = 0; } /* Any other delimiter is bad, and we will return ERROR_BAD_AMQP_URL. */ out: return res; }