/* * jsonwr_log10num_value * Write a number that has been converted into log base 10 * because it may be too small to represent. */ void jsonwr_log10num_value(JSONWR_T* jsonwr, double value, int prec) { double m, e; m = 0; e = 0; if (value > -HUGE_VAL && value < HUGE_VAL) { // normal value e = floor(value); m = pow(10.0, value - e); // check that rounding up won't cause a 9.9999 to go to a 10 if (m + (.5 * pow(10,-prec)) >= 10) { m = 1; e += 1; } str_clear(jsonwr->value_buf); str_appendf(jsonwr->value_buf, "\"%.*fe%+04.0f\"", prec, m, e); write_value(jsonwr); } else if (value >= HUGE_VAL) { // infinity str_clear(jsonwr->value_buf); str_appendf(jsonwr->value_buf, "\"inf\""); write_value(jsonwr); } else { // negative infinity str_clear(jsonwr->value_buf); // note that m and e are both 0, but it is important that we pass them // and not constants because if I pass a 0 then it is of int type which // takes up less space on the stack then the format string is expecting str_appendf(jsonwr->value_buf, "\"%.*fe%+04.0f\"", prec, m, e); write_value(jsonwr); } }
/* * jsonwr_log10num_value * Write a number that has been converted into log base 10 * because it may be too small to represent. */ void jsonwr_log10num_value(JSONWR_T* jsonwr, double value, int prec) { double m, e; m = 0; e = 0; if (value > -HUGE_VAL && value < HUGE_VAL) { // normal value e = floor(value); m = exp10(value - e); // check that rounding up won't cause a 9.9999 to go to a 10 if (m + (.5 * pow(10,-prec)) >= 10) { m = 1; e += 1; } str_clear(jsonwr->value_buf); str_appendf(jsonwr->value_buf, "\"%.*fe%+04.0f\"", prec, m, e); write_value(jsonwr); } else if (value >= HUGE_VAL) { // infinity str_clear(jsonwr->value_buf); str_appendf(jsonwr->value_buf, "\"inf\""); write_value(jsonwr); } else { // negative infinity str_clear(jsonwr->value_buf); str_appendf(jsonwr->value_buf, "\"%.*fe%+04.0f\"", prec, 0, 0); write_value(jsonwr); } }
static void monitor_satlist(WINDOW *win, int y, int x) /* display as much as we can of a satlist in a specified window */ { int ymax, xmax; char scr[128]; int i; assert(win != NULL); (void)wmove(win, y, x); (void)wclrtoeol(win); scr[0] = '\0'; for (i = 0; i < MAXCHANNELS; i++) { if (session.gpsdata.skyview[i].used) str_appendf(scr, sizeof(scr), "%d ", session.gpsdata.skyview[i].PRN); } getmaxyx(win, ymax, xmax); assert(ymax != 0); /* suppress compiler warning */ (void)mvwaddnstr(win, y, x, scr, xmax - 2 - x); if (strlen(scr) >= (size_t) (xmax - 2)) { (void)mvwaddch(win, y, xmax - 2 - x, (chtype) '.'); (void)mvwaddch(win, y, xmax - 3 - x, (chtype) '.'); (void)mvwaddch(win, y, xmax - 4 - x, (chtype) '.'); } monitor_fixframe(win); }
static void sd_description_callback(const char *key, void *value, void *userdata) { String *str = (String *)userdata; str_append(str, key); str_appendLiteral(str, " = 0x"); str_appendf(str, "%x", value); str_appendLiteral(str, "\n"); }
int gps_sock_stream(struct gps_data_t *gpsdata, unsigned int flags, void *d) /* ask gpsd to stream reports at you, hiding the command details */ { char buf[GPS_JSON_COMMAND_MAX]; if ((flags & (WATCH_JSON | WATCH_NMEA | WATCH_RAW)) == 0) { flags |= WATCH_JSON; } if ((flags & WATCH_DISABLE) != 0) { (void)strlcpy(buf, "?WATCH={\"enable\":false,", sizeof(buf)); if (flags & WATCH_JSON) (void)strlcat(buf, "\"json\":false,", sizeof(buf)); if (flags & WATCH_NMEA) (void)strlcat(buf, "\"nmea\":false,", sizeof(buf)); if (flags & WATCH_RAW) (void)strlcat(buf, "\"raw\":1,", sizeof(buf)); if (flags & WATCH_RARE) (void)strlcat(buf, "\"raw\":0,", sizeof(buf)); if (flags & WATCH_SCALED) (void)strlcat(buf, "\"scaled\":false,", sizeof(buf)); if (flags & WATCH_TIMING) (void)strlcat(buf, "\"timing\":false,", sizeof(buf)); if (flags & WATCH_SPLIT24) (void)strlcat(buf, "\"split24\":false,", sizeof(buf)); if (flags & WATCH_PPS) (void)strlcat(buf, "\"pps\":false,", sizeof(buf)); str_rstrip_char(buf, ','); (void)strlcat(buf, "};", sizeof(buf)); libgps_debug_trace((DEBUG_CALLS, "gps_stream() disable command: %s\n", buf)); return gps_send(gpsdata, buf); } else { /* if ((flags & WATCH_ENABLE) != 0) */ (void)strlcpy(buf, "?WATCH={\"enable\":true,", sizeof(buf)); if (flags & WATCH_JSON) (void)strlcat(buf, "\"json\":true,", sizeof(buf)); if (flags & WATCH_NMEA) (void)strlcat(buf, "\"nmea\":true,", sizeof(buf)); if (flags & WATCH_RARE) (void)strlcat(buf, "\"raw\":1,", sizeof(buf)); if (flags & WATCH_RAW) (void)strlcat(buf, "\"raw\":2,", sizeof(buf)); if (flags & WATCH_SCALED) (void)strlcat(buf, "\"scaled\":true,", sizeof(buf)); if (flags & WATCH_TIMING) (void)strlcat(buf, "\"timing\":true,", sizeof(buf)); if (flags & WATCH_SPLIT24) (void)strlcat(buf, "\"split24\":true,", sizeof(buf)); if (flags & WATCH_PPS) (void)strlcat(buf, "\"pps\":true,", sizeof(buf)); if (flags & WATCH_DEVICE) str_appendf(buf, sizeof(buf), "\"device\":\"%s\",", (char *)d); str_rstrip_char(buf, ','); (void)strlcat(buf, "};", sizeof(buf)); libgps_debug_trace((DEBUG_CALLS, "gps_stream() enable command: %s\n", buf)); return gps_send(gpsdata, buf); } }
void cap_start(const int promisc) { struct str *ifs = str_make(); assert(STAILQ_EMPTY(&cap_ifs)); if (STAILQ_EMPTY(&cli_ifnames)) errx(1, "no interfaces specified"); /* For each ifname */ while (!STAILQ_EMPTY(&cli_ifnames)) { struct strnode *ifname, *filter = NULL; struct cap_iface *iface = xmalloc(sizeof(*iface)); ifname = STAILQ_FIRST(&cli_ifnames); STAILQ_REMOVE_HEAD(&cli_ifnames, entries); if (!STAILQ_EMPTY(&cli_filters)) { filter = STAILQ_FIRST(&cli_filters); STAILQ_REMOVE_HEAD(&cli_filters, entries); } iface->name = ifname->str; iface->filter = (filter == NULL) ? NULL : filter->str; iface->pcap = NULL; iface->fd = -1; iface->linkhdr = NULL; localip_init(&iface->local_ips); STAILQ_INSERT_TAIL(&cap_ifs, iface, entries); cap_start_one(iface, promisc); free(ifname); if (filter) free(filter); if (str_len(ifs) == 0) str_append(ifs, iface->name); else str_appendf(ifs, ", %s", iface->name); } verbosef("all capture interfaces prepared"); /* Deallocate extra filters, if any. */ while (!STAILQ_EMPTY(&cli_filters)) { struct strnode *filter = STAILQ_FIRST(&cli_filters); verbosef("ignoring extraneous filter '%s'", filter->str); STAILQ_REMOVE_HEAD(&cli_filters, entries); free(filter); } str_appendn(ifs, "", 1); /* NUL terminate */ { size_t _; str_extract(ifs, &_, &title_interfaces); } }
int str_append_icon(struct str *str, char *icon) { char *home = getenv("HOME"); if (!home) { fprintf(stderr, "HOME is not set\n"); return -1; } return str_appendf(str, "\x1b]9;%s/.dotfiles/wm/icons/%s.xbm\a", home, icon); }
/************************************************************************** * Generate logos for a motif * Warning, this may modify the path and motif arguments. **************************************************************************/ static void generate_motif_logos(OPTIONS_T *options, STR_T *path, MOTIF_T *motif) { int path_len; char name[MAX_MOTIF_ID_LENGTH + 1]; copy_and_sanatise_name(name, get_motif_id(motif), MAX_MOTIF_ID_LENGTH); name[MAX_MOTIF_ID_LENGTH] = '\0'; path_len = str_len(path); str_appendf(path, "logo%s", name); CL_create1(motif, FALSE, FALSE, "MEME (no SSC)", str_internal(path), options->eps, options->png); if (options->rc) { str_truncate(path, path_len); str_appendf(path, "logo_rc%s", name); reverse_complement_motif(motif); CL_create1(motif, FALSE, FALSE, "MEME (no SSC)", str_internal(path), options->eps, options->png); } str_truncate(path, path_len); }
void html_open(struct str *buf, const char *title, const unsigned int path_depth, const int want_graph_js) { const char *root; assert(path_depth < (sizeof(relpaths)/sizeof(*relpaths))); root = relpaths[path_depth]; str_appendf(buf, "<!DOCTYPE html>\n" "<html>\n" "<head>\n" "<title>%s (darkstat %s)</title>\n" "<meta name=\"generator\" content=\"" PACKAGE_STRING "\">\n" "<meta name=\"robots\" content=\"noindex, noarchive\">\n" "<link rel=\"stylesheet\" href=\"%s/style.css\" type=\"text/css\">\n", title, title_interfaces, root); if (want_graph_js) str_appendf(buf, "<script src=\"%s/graph.js\" type=\"text/javascript\"></script>\n" , root); str_appendf(buf, "</head>\n" "<body>\n" "<div class=\"menu\">\n" "<ul class=\"menu\">" /* no whitespace (newlines) in list */ "<li class=\"label\">" PACKAGE_STRING "</li>" "<li><a href=\"%s/\">graphs</a></li>" "<li><a href=\"%s/hosts/\">hosts</a></li>" "<li><a href=\"" PACKAGE_URL "\">homepage</a></li>" "</ul>\n" "</div>\n" "<div class=\"content\">\n" "<h2 class=\"pageheader\">%s</h2>\n" , root, root, title); }
int str_append_escaped(struct str *str, char *buf, size_t len) { int ret; size_t i; for (i = 0; i < len; i++) { switch (buf[i]) { case '\0': ret = str_append(str, "\\0"); break; case '\a': ret = str_append(str, "\\a"); break; case '\b': ret = str_append(str, "\\b"); break; case '\t': ret = str_append(str, "\\t"); break; case '\n': ret = str_append(str, "\\n"); break; case '\v': ret = str_append(str, "\\v"); break; case '\f': ret = str_append(str, "\\f"); break; case '\r': ret = str_append(str, "\\r"); break; case '\\': ret = str_append(str, "\\\\"); break; default: if (isprint(buf[i])) ret = str_append_buf(str, &buf[i], 1); else ret = str_appendf(str, "\\x%x", buf[i]); break; } if (ret) return -1; } return 0; }
static int power_append(void *data, struct str *str, bool wordy) { struct power_section *section = data; const double high_thresh = 55.0; const double low_thresh = 20.0; int ret; if (section->ac_online) ret = str_append_icon(str, "ac"); else if (section->battery_capacity >= high_thresh) ret = str_append_icon(str, "bat_full"); else if (section->battery_capacity >= low_thresh) ret = str_append_icon(str, "bat_low"); else ret = str_append_icon(str, "bat_empty"); if (ret) return -1; if (str_appendf(str, " %.0f%%", section->battery_capacity)) return -1; return str_separator(str); }
/* zodiac_spew - Takes a message type, an array of data words, and a length * for the array, and prepends a 5 word header (including checksum). * The data words are expected to be checksummed. */ static ssize_t zodiac_spew(struct gps_device_t *session, unsigned short type, unsigned short *dat, int dlen) { struct header h; int i; char buf[BUFSIZ]; h.sync = 0x81ff; h.id = (unsigned short)type; h.ndata = (unsigned short)(dlen - 1); h.flags = 0; h.csum = zodiac_checksum((unsigned short *)&h, 4); if (!BAD_SOCKET(session->gpsdata.gps_fd)) { size_t hlen, datlen; hlen = sizeof(h); datlen = sizeof(unsigned short) * dlen; if (end_write(session->gpsdata.gps_fd, &h, hlen) != (ssize_t) hlen || end_write(session->gpsdata.gps_fd, dat, datlen) != (ssize_t) datlen) { gpsd_log(&session->context->errout, LOG_RAW, "Reconfigure write failed\n"); return -1; } } (void)snprintf(buf, sizeof(buf), "%04x %04x %04x %04x %04x", h.sync, h.id, h.ndata, h.flags, h.csum); for (i = 0; i < dlen; i++) str_appendf(buf, sizeof(buf), " %04x", dat[i]); gpsd_log(&session->context->errout, LOG_RAW, "Sent Zodiac packet: %s\n", buf); return 0; }
/* * jsonwr_dbl_value * Write a number. */ void jsonwr_dbl_value(JSONWR_T* jsonwr, double value) { str_clear(jsonwr->value_buf); str_appendf(jsonwr->value_buf, "%g", value); write_value(jsonwr); }
/* * jsonwr_lng_value * Write a number */ void jsonwr_lng_value(JSONWR_T* jsonwr, long value) { str_clear(jsonwr->value_buf); str_appendf(jsonwr->value_buf, "%ld", value); write_value(jsonwr); }
/* * converts a string into a JSON allowed string and stores it * in the storage string builder. The string may be UTF-8 or ASCII. */ static void convert_string(STR_T* storage, const char* string) { const char *c; unsigned char byte; uint32_t codepoint; int bytes; assert(storage != NULL); str_clear(storage); str_append(storage, "\"", 1); for (c = string; *c != '\0'; c += bytes) { byte = *c; // check if the high bit is set if (byte & 0x80) {// UTF-8 multibyte int i; // determine the number of bytes in the multibyte if ((byte & 0xE0) == 0xC0) { bytes = 2; codepoint = ((uint32_t)(byte & 31)) << 6; } else if ((byte & 0xF0) == 0xE0) { bytes = 3; codepoint = ((uint32_t)(byte & 15)) << 12; } else if ((byte & 0xF8) == 0xF0) { bytes = 4; codepoint = ((uint32_t)(byte & 7)) << 18; } else if ((byte & 0xFC) == 0xF8) { bytes = 5; codepoint = ((uint32_t)(byte & 3)) << 24; } else if ((byte & 0xFE) == 0xFC) { bytes = 6; codepoint = ((uint32_t)(byte & 1)) << 30; } else { // bad byte! if ((byte & 0xC0) == 0x80) { die("The bit pattern 10xxxxxx is illegal for the first byte of a UTF-8 multibyte."); } else if (byte == 0xFE) { die("The byte 0xFE is illegal for UTF-8."); } else { die("The byte 0xFF is illegal for UTF-8."); } // stop compiler complaints bytes = 1; codepoint = 0; } // read the rest of the bytes for (i = 1; i < bytes; i++) { byte = c[i]; if ((byte & 0xC0) != 0x80) die("Expected the bit pattern 10xxxxxx for " "a following byte of a UTF-8 multibyte."); codepoint = codepoint | (((uint32_t)(byte & 0x3F)) << (6 * (bytes - i - 1))); } // check for overlong representations by seeing if we could have represented // the number with one less byte if (codepoint < (1 << (bytes == 2 ? 7 : (6 * (bytes - 2) + (8 - bytes))))) { die("The UTF-8 multibyte uses too many bytes for the codepoint it represents."); } } else { // ASCII byte bytes = 1; codepoint = byte; } switch(codepoint) { case 8: // backspace str_append(storage, "\\b", 2); break; case 9: // tab str_append(storage, "\\t", 2); break; case 10: // line-feed (newline) str_append(storage, "\\n", 2); break; case 12: // form-feed str_append(storage, "\\f", 2); break; case 13: // carriage return str_append(storage, "\\r", 2); break; case 34: // double quote str_append(storage, "\\\"", 2); break; case 47: // slash str_append(storage, "\\/", 2); break; case 92: // backslash str_append(storage, "\\\\", 2); break; default: // check if a control character // or if line seperator (U+2028) or if paragraph separator (U+2029) // the latter two are valid JSON but not valid Javascript as Javascript // can't have unescaped newline characters in a string. if (codepoint <= 0x1F || (codepoint >= 0x7F && codepoint <= 0x9F) || codepoint == 0x2028 || codepoint == 0x2029) { str_appendf(storage, "\\u%04x", codepoint); } else { str_append(storage, c, bytes); } } } str_append(storage, "\"", 1); }