2014-03-18 17:35:10 +00:00
|
|
|
#include <stdlib.h>
|
2014-04-09 22:09:35 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <assert.h>
|
2014-03-28 19:33:20 +00:00
|
|
|
#include <bemenu.h>
|
2014-03-18 17:35:10 +00:00
|
|
|
|
2014-04-09 22:09:35 +00:00
|
|
|
static ptrdiff_t getLine(char **outLine, size_t *outAllocated, FILE *stream)
|
|
|
|
{
|
|
|
|
size_t len = 0, allocated;
|
|
|
|
char *s, *buffer;
|
|
|
|
|
2014-04-09 22:41:32 +00:00
|
|
|
assert(outLine);
|
|
|
|
assert(outAllocated);
|
2014-04-09 22:09:35 +00:00
|
|
|
|
2014-04-09 22:41:32 +00:00
|
|
|
if (!stream || feof(stream) || ferror(stream))
|
2014-04-09 22:09:35 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
allocated = *outAllocated;
|
|
|
|
buffer = *outLine;
|
|
|
|
|
2014-04-09 22:41:32 +00:00
|
|
|
if (!buffer || allocated == 0) {
|
2014-04-09 22:09:35 +00:00
|
|
|
if (!(buffer = calloc(1, (allocated = 1024) + 1)))
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (s = buffer;;) {
|
2014-04-09 22:41:32 +00:00
|
|
|
if (!fgets(s, allocated - (s - buffer), stream)) {
|
2014-04-09 22:29:01 +00:00
|
|
|
*outAllocated = allocated;
|
|
|
|
*outLine = buffer;
|
2014-04-09 22:09:35 +00:00
|
|
|
return -1;
|
2014-04-09 22:29:01 +00:00
|
|
|
}
|
2014-04-09 22:09:35 +00:00
|
|
|
|
|
|
|
len = strlen(s);
|
|
|
|
if (feof(stream))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (len > 0 && s[len - 1] == '\n')
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (len + 1 >= allocated - (s - buffer)) {
|
|
|
|
void *tmp = realloc(buffer, 2 * allocated);
|
|
|
|
if (!tmp)
|
|
|
|
break;
|
|
|
|
|
|
|
|
buffer = tmp;
|
|
|
|
s = buffer + allocated - 1;
|
|
|
|
memset(s, 0, allocated - (s - buffer));
|
|
|
|
allocated *= 2;
|
|
|
|
} else {
|
|
|
|
s += len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*outAllocated = allocated;
|
|
|
|
*outLine = buffer;
|
|
|
|
|
|
|
|
if (s[len - 1] == '\n')
|
|
|
|
s[len - 1] = 0;
|
|
|
|
|
|
|
|
return s - buffer + len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void readItemsToMenuFromStdin(bmMenu *menu)
|
|
|
|
{
|
2014-04-10 17:02:27 +00:00
|
|
|
assert(menu);
|
|
|
|
|
2014-04-09 22:09:35 +00:00
|
|
|
ptrdiff_t len;
|
2014-04-09 22:29:01 +00:00
|
|
|
size_t size = 0;
|
2014-04-09 22:09:35 +00:00
|
|
|
char *line = NULL;
|
|
|
|
|
|
|
|
while ((len = getLine(&line, &size, stdin)) != -1) {
|
|
|
|
bmItem *item = bmItemNew((len > 0 ? line : NULL));
|
|
|
|
if (!item)
|
|
|
|
break;
|
|
|
|
|
|
|
|
bmMenuAddItem(menu, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line)
|
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
|
2014-03-18 17:35:10 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
(void)argc, (void)argv;
|
|
|
|
|
2014-04-09 22:09:35 +00:00
|
|
|
bmMenu *menu = bmMenuNew(BM_DRAW_MODE_CURSES);
|
2014-03-28 19:33:20 +00:00
|
|
|
if (!menu)
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2014-04-09 22:09:35 +00:00
|
|
|
bmMenuSetTitle(menu, "bemenu");
|
|
|
|
readItemsToMenuFromStdin(menu);
|
|
|
|
|
|
|
|
bmKey key;
|
|
|
|
unsigned int unicode;
|
|
|
|
int status = 0;
|
|
|
|
do {
|
|
|
|
bmMenuRender(menu);
|
|
|
|
key = bmMenuGetKey(menu, &unicode);
|
|
|
|
} while ((status = bmMenuRunWithKey(menu, key, unicode)) == BM_RUN_RESULT_RUNNING);
|
|
|
|
|
|
|
|
if (status == BM_RUN_RESULT_SELECTED) {
|
2014-04-10 17:04:06 +00:00
|
|
|
unsigned int i, count;
|
|
|
|
bmItem **items = bmMenuGetSelectedItems(menu, &count);
|
2014-04-12 11:15:37 +00:00
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
const char *text = bmItemGetText(items[i]);
|
|
|
|
printf("%s\n", (text ? text : ""));
|
|
|
|
}
|
2014-04-09 22:09:35 +00:00
|
|
|
}
|
2014-03-28 19:33:20 +00:00
|
|
|
|
|
|
|
bmMenuFree(menu);
|
2014-04-09 22:09:35 +00:00
|
|
|
return (status == BM_RUN_RESULT_SELECTED ? EXIT_SUCCESS : EXIT_FAILURE);
|
2014-03-18 17:35:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim: set ts=8 sw=4 tw=0 :*/
|