2023-05-14 00:21:16 +02:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
|
2023-05-06 14:29:45 +02:00
|
|
|
void readstdin(void) {
|
2023-07-29 04:40:02 +02:00
|
|
|
char buf[sizeof tx.text], *p;
|
|
|
|
size_t i, itemsiz = 0;
|
|
|
|
unsigned int tmpmax = 0;
|
2023-03-24 03:14:24 +01:00
|
|
|
|
2023-07-29 04:40:02 +02:00
|
|
|
if (passwd) {
|
2023-06-23 03:38:21 +02:00
|
|
|
sp.inputw = lines = 0;
|
2023-05-08 23:00:45 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-03-24 03:14:24 +01:00
|
|
|
|
2023-07-29 04:40:02 +02:00
|
|
|
if (listfile) {
|
2023-05-08 17:33:55 +02:00
|
|
|
readfile();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-29 04:40:02 +02:00
|
|
|
int o = 0;
|
2023-03-24 03:14:24 +01:00
|
|
|
|
2023-07-29 04:40:02 +02:00
|
|
|
// read each line from stdin and add it to the item list
|
|
|
|
for (i = 0; fgets(buf, sizeof buf, stdin); i++) {
|
|
|
|
if (i + 1 >= itemsiz) {
|
|
|
|
itemsiz += 256;
|
|
|
|
if (!(items = realloc(items, itemsiz * sizeof(*items))))
|
|
|
|
die("spmenu: cannot realloc %zu bytes:", itemsiz * sizeof(*items));
|
|
|
|
}
|
|
|
|
if ((p = strchr(buf, '\n')))
|
|
|
|
*p = '\0';
|
|
|
|
if (!(items[i].text = strdup(buf)))
|
|
|
|
die("spmenu: cannot strdup %u bytes:", strlen(buf) + 1);
|
2023-07-19 01:06:38 +02:00
|
|
|
items[i].hp = arrayhas(hpitems, hplength, items[i].text);
|
2023-07-29 04:40:02 +02:00
|
|
|
draw_font_getexts(draw->font, buf, strlen(buf), &tmpmax, NULL, True);
|
|
|
|
if (tmpmax > sp.inputw) {
|
|
|
|
sp.inputw = tmpmax;
|
|
|
|
}
|
|
|
|
|
|
|
|
items[i].index = i;
|
2023-05-06 23:29:41 +02:00
|
|
|
|
2023-05-07 02:11:16 +02:00
|
|
|
if (parsemarkup(i)) {
|
2023-07-29 04:40:02 +02:00
|
|
|
o = 1;
|
2023-03-24 03:14:24 +01:00
|
|
|
}
|
2023-05-07 22:30:57 +02:00
|
|
|
|
2023-08-07 06:37:38 +02:00
|
|
|
#if !IMAGE
|
2023-07-29 04:40:02 +02:00
|
|
|
if (o) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2023-03-24 03:14:24 +01:00
|
|
|
|
2023-08-07 06:37:38 +02:00
|
|
|
#if IMAGE
|
2023-07-29 04:40:02 +02:00
|
|
|
if (!o) img.longestedge = img.imagegaps = 0;
|
2023-05-08 23:00:45 +02:00
|
|
|
#endif
|
2023-05-07 02:11:16 +02:00
|
|
|
|
2023-07-29 04:40:02 +02:00
|
|
|
// clean
|
|
|
|
if (items) {
|
|
|
|
items[i].text = NULL;
|
2023-08-07 06:37:38 +02:00
|
|
|
#if IMAGE
|
2023-07-29 04:40:02 +02:00
|
|
|
items[i].image = NULL;
|
2023-07-19 01:06:38 +02:00
|
|
|
#endif
|
2023-07-29 04:40:02 +02:00
|
|
|
}
|
2023-03-24 03:14:24 +01:00
|
|
|
|
2023-07-29 04:40:02 +02:00
|
|
|
lines = MIN(lines, i);
|
2023-03-24 03:14:24 +01:00
|
|
|
}
|
2023-05-07 01:41:30 +02:00
|
|
|
|
|
|
|
void readfile(void) {
|
|
|
|
if (passwd){
|
2023-06-23 03:38:21 +02:00
|
|
|
sp.inputw = lines = 0;
|
2023-05-08 23:00:45 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-05-07 01:41:30 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
size_t len;
|
2023-05-08 18:16:50 +02:00
|
|
|
static size_t c = 0;
|
2023-05-07 01:41:30 +02:00
|
|
|
char *l;
|
|
|
|
|
2023-05-08 22:44:27 +02:00
|
|
|
FILE *lf = fopen(listfile, "r");
|
2023-05-07 01:41:30 +02:00
|
|
|
|
2023-05-08 22:44:27 +02:00
|
|
|
if (!lf) return;
|
2023-05-07 01:41:30 +02:00
|
|
|
|
|
|
|
items = NULL;
|
|
|
|
listsize = 0;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
l = NULL;
|
|
|
|
len = 0;
|
|
|
|
|
2023-05-08 22:44:27 +02:00
|
|
|
if (-1 == getline(&l, &len, lf)) {
|
|
|
|
if (ferror(lf)) die("spmenu: failed to read file\n");
|
2023-05-07 01:41:30 +02:00
|
|
|
free(l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-05-08 18:16:50 +02:00
|
|
|
if (c == listsize) {
|
|
|
|
c += 64 * sizeof(char*);
|
|
|
|
list = realloc(list, c);
|
2023-05-07 01:41:30 +02:00
|
|
|
if (!list) die("spmenu: failed to realloc memory");
|
|
|
|
}
|
|
|
|
|
|
|
|
strtok(l, "\n");
|
|
|
|
list[listsize] = l;
|
|
|
|
listsize++;
|
|
|
|
}
|
|
|
|
|
2023-05-08 22:44:27 +02:00
|
|
|
if (fclose(lf)) {
|
2023-05-07 01:41:30 +02:00
|
|
|
die("spmenu: failed to close file %s\n", listfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!list_items) {
|
|
|
|
list_items = items;
|
|
|
|
items = calloc(listsize + 1, sizeof(struct item));
|
|
|
|
if (!items) die("spmenu: cannot alloc memory\n");
|
|
|
|
|
|
|
|
int i = 0;
|
2023-05-07 02:11:16 +02:00
|
|
|
int o = 0;
|
2023-05-07 01:41:30 +02:00
|
|
|
|
|
|
|
for (i = 0; i < listsize; i++) {
|
|
|
|
items[i].text = list[i];
|
2023-05-07 02:11:16 +02:00
|
|
|
|
2023-05-07 22:30:57 +02:00
|
|
|
if (parsemarkup(i)) {
|
2023-05-07 02:11:16 +02:00
|
|
|
o = 1;
|
2023-05-07 22:30:57 +02:00
|
|
|
}
|
|
|
|
|
2023-08-07 06:37:38 +02:00
|
|
|
#if !IMAGE
|
2023-05-07 22:30:57 +02:00
|
|
|
if (o) {
|
|
|
|
;
|
|
|
|
}
|
2023-05-08 23:00:45 +02:00
|
|
|
#endif
|
2023-05-07 01:41:30 +02:00
|
|
|
}
|
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
lines = columns == 1 ? i : MIN(i, lines); // i = number of items
|
2023-05-07 15:35:41 +02:00
|
|
|
|
2023-08-07 06:37:38 +02:00
|
|
|
#if IMAGE
|
2023-06-24 04:20:51 +02:00
|
|
|
if (!o) img.longestedge = img.imagegaps = 0;
|
2023-05-08 23:00:45 +02:00
|
|
|
#endif
|
2023-05-07 02:11:16 +02:00
|
|
|
|
2023-07-09 23:56:34 +02:00
|
|
|
if (i == sp.listcount) {
|
|
|
|
sp.listchanged = 0;
|
|
|
|
sp.listcount = i;
|
2023-05-07 01:41:30 +02:00
|
|
|
} else {
|
2023-07-09 23:56:34 +02:00
|
|
|
sp.listcount = i;
|
|
|
|
sp.listchanged = 1;
|
2023-05-07 18:07:02 +02:00
|
|
|
|
2023-06-02 18:37:51 +02:00
|
|
|
// prevents state->buffer from being NULL
|
|
|
|
if (!protocol) {
|
|
|
|
resizeclient();
|
|
|
|
}
|
2023-05-07 01:41:30 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
free(items);
|
|
|
|
items = list_items;
|
|
|
|
list_items = NULL;
|
|
|
|
}
|
2023-05-07 02:11:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int parsemarkup(int index) {
|
2023-08-07 06:37:38 +02:00
|
|
|
#if IMAGE
|
2023-05-08 23:00:45 +02:00
|
|
|
int w, h;
|
|
|
|
char *limg = NULL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// parse image markup
|
2023-08-07 06:37:38 +02:00
|
|
|
#if IMAGE
|
2023-05-10 21:51:23 +02:00
|
|
|
if (!strncmp("IMG:", items[index].text, strlen("IMG:")) || !strncmp("img://", items[index].text, strlen("img://"))) {
|
2023-05-08 23:00:45 +02:00
|
|
|
if(!(items[index].image = malloc(strlen(items[index].text)+1)))
|
|
|
|
fprintf(stderr, "spmenu: cannot malloc %lu bytes\n", strlen(items[index].text));
|
2023-05-10 21:51:23 +02:00
|
|
|
if (sscanf(items[index].text, "IMG:%[^\t]", items[index].image)) {
|
2023-05-08 23:00:45 +02:00
|
|
|
items[index].text += strlen("IMG:")+strlen(items[index].image)+1;
|
2023-05-10 21:51:23 +02:00
|
|
|
} else if (sscanf(items[index].text, "img://%[^\t]", items[index].image)) {
|
|
|
|
items[index].text += strlen("img://")+strlen(items[index].image)+1;
|
2023-05-07 02:11:16 +02:00
|
|
|
} else {
|
2023-05-08 23:00:45 +02:00
|
|
|
free(items[index].image);
|
2023-05-07 02:11:16 +02:00
|
|
|
items[index].image = NULL;
|
|
|
|
}
|
2023-05-08 23:00:45 +02:00
|
|
|
} else {
|
|
|
|
items[index].image = NULL;
|
|
|
|
}
|
2023-05-07 01:41:30 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
// load image cache (or generate)
|
2023-06-23 03:38:21 +02:00
|
|
|
if (generatecache && img.longestedge <= maxcache && items[index].image && strcmp(items[index].image, limg ? limg : "")) {
|
2023-05-08 23:00:45 +02:00
|
|
|
loadimagecache(items[index].image, &w, &h);
|
|
|
|
}
|
2023-05-07 02:11:16 +02:00
|
|
|
|
2023-05-08 23:00:45 +02:00
|
|
|
if(items[index].image) {
|
|
|
|
limg = items[index].image;
|
|
|
|
}
|
2023-07-11 17:01:57 +02:00
|
|
|
|
|
|
|
if (limg) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2023-05-08 23:00:45 +02:00
|
|
|
#else // remove the data, just for convenience
|
|
|
|
char *data;
|
|
|
|
|
2023-05-10 21:51:23 +02:00
|
|
|
if (!strncmp("IMG:", items[index].text, strlen("IMG:")) || !strncmp("img://", items[index].text, strlen("img://"))) {
|
2023-05-08 23:00:45 +02:00
|
|
|
if(!(data = malloc(strlen(items[index].text)+1)))
|
|
|
|
fprintf(stderr, "spmenu: cannot malloc %lu bytes\n", strlen(items[index].text));
|
|
|
|
if(sscanf(items[index].text, "IMG:%[^\t]", data)) {
|
|
|
|
items[index].text += strlen("IMG:")+strlen(data)+1;
|
2023-05-10 21:51:23 +02:00
|
|
|
} else if(sscanf(items[index].text, "img://%[^\t]", data)) {
|
|
|
|
items[index].text += strlen("img://")+strlen(data)+1;
|
2023-05-07 02:11:16 +02:00
|
|
|
}
|
2023-05-08 23:00:45 +02:00
|
|
|
}
|
|
|
|
#endif
|
2023-05-07 01:41:30 +02:00
|
|
|
}
|