dynamicoptions.c (2236B)
1 static void 2 refreshoptions(void) 3 { 4 int dynlen = strlen(dynamic); 5 char* cmd= malloc(dynlen + strlen(text) + 2); 6 if (cmd == NULL) 7 die("malloc:"); 8 sprintf(cmd, "%s %s", dynamic, text); 9 FILE *stream = popen(cmd, "r"); 10 if (!stream) 11 die("popen(%s):", cmd); 12 readstream(stream); 13 int pc = pclose(stream); 14 if (pc == -1) 15 die("pclose:"); 16 free(cmd); 17 curr = sel = items; 18 } 19 20 static void 21 readstream(FILE* stream) 22 { 23 char buf[sizeof text], *p; 24 size_t i, imax = 0, size = 0; 25 unsigned int tmpmax = 0; 26 27 /* read each line from stdin and add it to the item list */ 28 for (i = 0; fgets(buf, sizeof buf, stream); i++) { 29 if (i + 1 >= size / sizeof *items) 30 if (!(items = realloc(items, (size += BUFSIZ)))) 31 die("cannot realloc %u bytes:", size); 32 if ((p = strchr(buf, '\n'))) 33 *p = '\0'; 34 if (!(items[i].text = strdup(buf))) 35 die("cannot strdup %u bytes:", strlen(buf) + 1); 36 #if SEPARATOR_PATCH 37 if (separator && (p = sepchr(items[i].text, separator)) != NULL) { 38 *p = '\0'; 39 items[i].text_output = ++p; 40 } else { 41 items[i].text_output = items[i].text; 42 } 43 if (separator_reverse) { 44 p = items[i].text; 45 items[i].text = items[i].text_output; 46 items[i].text_output = p; 47 } 48 #elif TSV_PATCH 49 if ((p = strchr(buf, '\t'))) 50 *p = '\0'; 51 if (!(items[i].stext = strdup(buf))) 52 die("cannot strdup %u bytes:", strlen(buf) + 1); 53 #endif // TSV_PATCH 54 #if MULTI_SELECTION_PATCH 55 items[i].id = i; 56 #else 57 items[i].out = 0; 58 #endif // MULTI_SELECTION_PATCH 59 #if HIGHPRIORITY_PATCH 60 items[i].hp = arrayhas(hpitems, hplength, items[i].text); 61 #endif // HIGHPRIORITY_PATCH 62 #if PANGO_PATCH 63 drw_font_getexts(drw->font, buf, strlen(buf), &tmpmax, NULL, True); 64 #else 65 drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL); 66 #endif // PANGO_PATCH 67 if (tmpmax > inputw) { 68 inputw = tmpmax; 69 imax = i; 70 } 71 } 72 73 /* If the command did not give any output at all, then do not clear the existing items */ 74 if (!i) 75 return; 76 77 if (items) 78 items[i].text = NULL; 79 #if PANGO_PATCH 80 inputw = items ? TEXTWM(items[imax].text) : 0; 81 #else 82 inputw = items ? TEXTW(items[imax].text) : 0; 83 #endif // PANGO_PATCH 84 if (!dynamic || !*dynamic) 85 lines = MIN(lines, i); 86 else { 87 text[0] = '\0'; 88 cursor = 0; 89 } 90 }