2014-10-25 17:43:37 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2020-02-07 13:56:50 +00:00
|
|
|
#include "common/common.h"
|
2014-10-25 17:43:37 +00:00
|
|
|
|
|
|
|
static struct client client = {
|
|
|
|
.filter_mode = BM_FILTER_MODE_DMENU,
|
|
|
|
.title = "bemenu",
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
read_items_to_menu_from_stdin(struct bm_menu *menu)
|
|
|
|
{
|
|
|
|
assert(menu);
|
|
|
|
|
|
|
|
size_t step = 1024, allocated;
|
|
|
|
char *buffer;
|
|
|
|
|
|
|
|
if (!(buffer = malloc((allocated = step))))
|
|
|
|
return;
|
|
|
|
|
2020-02-11 08:19:57 +00:00
|
|
|
// TODO: do not read everything into memory
|
2014-10-25 17:43:37 +00:00
|
|
|
size_t read;
|
|
|
|
while ((read = fread(buffer + (allocated - step), 1, step, stdin)) == step) {
|
|
|
|
void *tmp;
|
|
|
|
if (!(tmp = realloc(buffer, (allocated += step)))) {
|
|
|
|
free(buffer);
|
2020-02-11 08:19:57 +00:00
|
|
|
fprintf(stderr, "Out of memory\n");
|
2014-10-25 17:43:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
buffer = tmp;
|
|
|
|
}
|
2020-02-11 08:19:57 +00:00
|
|
|
|
|
|
|
// make sure we can null terminate the input
|
|
|
|
const size_t end = allocated - step + read;
|
2020-02-11 08:23:33 +00:00
|
|
|
assert(end != ~(size_t)0);
|
2020-02-11 08:19:57 +00:00
|
|
|
if (end >= allocated && !(buffer = realloc(buffer, end + 1))) {
|
|
|
|
fprintf(stderr, "Out of memory\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-11 08:23:33 +00:00
|
|
|
buffer[end] = 0;
|
2014-10-25 17:43:37 +00:00
|
|
|
|
|
|
|
char *s = buffer;
|
2020-02-11 08:23:33 +00:00
|
|
|
while ((size_t)(s - buffer) < end && *s != 0) {
|
2020-02-11 08:19:57 +00:00
|
|
|
const size_t pos = strcspn(s, "\n");
|
2014-10-25 17:43:37 +00:00
|
|
|
s[pos] = 0;
|
|
|
|
|
|
|
|
struct bm_item *item;
|
|
|
|
if (!(item = bm_item_new(s)))
|
|
|
|
break;
|
|
|
|
|
|
|
|
bm_menu_add_item(menu, item);
|
|
|
|
s += pos + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
|
2017-02-14 19:51:02 +00:00
|
|
|
static void
|
2020-02-08 11:21:36 +00:00
|
|
|
item_cb(const struct client *client, struct bm_item *item)
|
2017-02-14 19:51:02 +00:00
|
|
|
{
|
2020-02-08 11:21:36 +00:00
|
|
|
(void)client;
|
|
|
|
const char *text = bm_item_get_text(item);
|
2017-02-14 19:51:02 +00:00
|
|
|
printf("%s\n", (text ? text : ""));
|
|
|
|
}
|
|
|
|
|
2014-10-25 17:43:37 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
if (!bm_init())
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
parse_args(&client, &argc, &argv);
|
|
|
|
|
|
|
|
struct bm_menu *menu;
|
|
|
|
if (!(menu = menu_with_options(&client)))
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
read_items_to_menu_from_stdin(menu);
|
2017-02-14 19:51:02 +00:00
|
|
|
const enum bm_run_result status = run_menu(&client, menu, item_cb);
|
2014-10-25 17:43:37 +00:00
|
|
|
bm_menu_free(menu);
|
|
|
|
return (status == BM_RUN_RESULT_SELECTED ? EXIT_SUCCESS : EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim: set ts=8 sw=4 tw=0 :*/
|