commit 12b6c9dfd009352e0bb7f8395abd3678e3bd6265
parent 96e9e044e779b301b0608aea0ee4449124a8d0bd
Author: Louis Burda <quent.burda@gmail.com>
Date: Sun, 13 Feb 2022 17:02:19 +0100
Added liblist
Diffstat:
10 files changed, 22 insertions(+), 217 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -7,3 +7,4 @@ todo
log*
gmon.out*
profile.txt
+compile_commands.json
diff --git a/.gitmodules b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "lib/clist"]
+ path = lib/clist
+ url = git@sinitax.com:sinitax/clist
diff --git a/Makefile b/Makefile
@@ -1,11 +1,18 @@
-CFLAGS = -O2 -I src -g $(shell pkg-config --cflags glib-2.0 dbus-1)
+CFLAGS = -I src -g $(shell pkg-config --cflags glib-2.0 dbus-1)
+CFLAGS += -I lib/clist/include
LDLIBS = -lcurses -lmpdclient $(shell pkg-config --libs glib-2.0 dbus-1)
DEPFLAGS = -MT $@ -MMD -MP -MF build/$*.d
+ifeq "$(PROF)" "YES"
+ CFLAGS += -pg
+endif
+
SRCS = $(wildcard src/*.c)
OBJS = $(SRCS:src/%.c=build/%.o)
DEPS = $(OBJS:%.o=%.d)
+LIBLIST_A = lib/clist/build/liblist.a
+
.PHONY: all tmus clean install uninstall
all: tmus
@@ -23,11 +30,11 @@ build/%.d: | build;
include $(DEPS)
-tmus: $(OBJS)
- $(CC) -o $@ $^ $(CFLAGS) $(LDLIBS)
+$(LIBLIST_A):
+ make -C lib/clist build/liblist.a
-tmus.prof: $(OBJS)
- $(CC) -o $@ $^ -pg $(CFLAGS) $(LDLIBS)
+tmus: $(OBJS) $(LIBLIST_A)
+ $(CC) -o tmus $^ $(CFLAGS) $(LDLIBS)
install:
install -m 755 tmus /usr/bin
diff --git a/compile_commands.json b/compile_commands.json
@@ -1,20 +0,0 @@
-[
- {
- "arguments": [
- "cc",
- "-c",
- "-I",
- "src",
- "-g",
- "-I/usr/include/glib-2.0",
- "-I/usr/lib/glib-2.0/include",
- "-I/usr/include/dbus-1.0",
- "-I/usr/lib/dbus-1.0/include",
- "-o",
- "build/main.o",
- "src/main.c"
- ],
- "directory": "/snxdata/lib/dev/tmus",
- "file": "src/main.c"
- }
-]
-\ No newline at end of file
diff --git a/lib/clist b/lib/clist
@@ -0,0 +1 @@
+Subproject commit e25f7bbf238b47675c922428abdb6c638e1a01e0
diff --git a/src/history.c b/src/history.c
@@ -41,7 +41,7 @@ history_free(struct history *history)
inputln_free(UPCAST(ln, struct inputln));
list_free(&history->list, (link_free_func) inputln_free,
- LINK_OFFSET(struct inputln));
+ LINK_OFFSET(struct inputln, link));
history->input = NULL;
history->sel = NULL;
diff --git a/src/list.c b/src/list.c
@@ -1,143 +0,0 @@
-#include "list.h"
-#include "util.h"
-
-void
-list_init(struct list *list)
-{
- list->head.prev = NULL;
- list->head.next = &list->tail;
- list->tail.prev = &list->head;
- list->tail.next = NULL;
-}
-
-void
-list_free(struct list *list, void (*free_item)(void *), int offset)
-{
- struct link *item;
-
- ASSERT(list != NULL);
-
- while (!list_empty(list)) {
- item = link_pop(list->head.next);
- free_item(((void *) item) - offset);
- }
-}
-
-int
-list_empty(struct list *list)
-{
- ASSERT(list != NULL);
-
- return list->head.next == &list->tail;
-}
-
-int
-list_len(struct list *list)
-{
- struct link *iter;
- int len;
-
- ASSERT(list != NULL);
-
- len = 0;
- for (LIST_ITER(list, iter))
- len += 1;
-
- return len;
-}
-
-struct link *
-list_at(struct list *list, int n)
-{
- ASSERT(list != NULL);
-
- return link_iter(list->head.next, n);
-}
-
-void
-list_push_front(struct list *list, struct link *link)
-{
- link_append(&list->head, link);
-}
-
-void
-list_push_back(struct list *list, struct link *link)
-{
- link_prepend(&list->tail, link);
-}
-
-struct link *
-list_pop_front(struct list *list)
-{
- ASSERT(list != NULL);
-
- if (list_empty(list))
- return NULL;
-
- return link_pop(list->head.next);
-}
-
-struct link *
-list_pop_back(struct list *list)
-{
- ASSERT(list != NULL);
-
- if (list_empty(list))
- return NULL;
-
- return link_pop(list->tail.prev);
-}
-
-void
-link_prepend(struct link *cur, struct link *link)
-{
- ASSERT(cur != NULL && link != NULL);
-
- link->prev = cur->prev;
- link->next = cur;
-
- if (link->prev)
- link->prev->next = link;
- if (link->next)
- link->next->prev = link;
-}
-
-void
-link_append(struct link *cur, struct link *link)
-{
- ASSERT(cur != NULL && link != NULL);
-
- link->prev = cur;
- link->next = cur->next;
-
- if (link->prev)
- link->prev->next = link;
- if (link->next)
- link->next->prev = link;
-}
-
-struct link *
-link_iter(struct link *link, int n)
-{
- int i;
-
- for (i = 0; i < n; i++) {
- if (!link) return NULL;
- link = link->next;
- }
-
- return link;
-}
-
-struct link *
-link_pop(struct link *link)
-{
- ASSERT(link != NULL);
-
- if (link->prev)
- link->prev->next = link->next;
- if (link->next)
- link->next->prev = link->prev;
-
- return link;
-}
diff --git a/src/list.h b/src/list.h
@@ -1,46 +0,0 @@
-#pragma once
-
-#include <stdlib.h>
-
-#define LINK_OFFSET(type) ((size_t) &((type *)0)->link)
-#define UPCAST(ptr, type) ({ \
- const typeof( ((type *)0)->link ) *__mptr = (ptr); \
- (type *)( (char *)__mptr - LINK_OFFSET(type) ); })
-
-#define LINK_EMPTY ((struct link) { 0 })
-
-#define LINK(p) (&(p)->link)
-
-#define LIST_INNER(list, link) \
- (((link) != &(list)->head) && ((link) != &(list)->tail))
-
-#define LIST_ITER(list, iter) (iter) = (list)->head.next; \
- (iter) != &(list)->tail; (iter) = (iter)->next
-
-typedef void (*link_free_func)(void *p);
-
-struct link {
- struct link *prev;
- struct link *next;
-};
-
-struct list {
- struct link head;
- struct link tail;
-};
-
-void list_init(struct list *list);
-void list_free(struct list *list, void (*free_item)(void *), int offset);
-int list_empty(struct list *list);
-int list_len(struct list *list);
-struct link *list_at(struct list *list, int n);
-void list_push_front(struct list *list, struct link *link);
-void list_push_back(struct list *list, struct link *link);
-struct link *list_pop_front(struct list *list);
-struct link *list_pop_back(struct list *list);
-
-void link_prepend(struct link *list, struct link *link);
-void link_append(struct link *list, struct link *link);
-struct link *link_iter(struct link *link, int n);
-struct link *link_pop(struct link *link);
-
diff --git a/src/ref.c b/src/ref.c
@@ -22,7 +22,7 @@ ref_free(void *ref)
void
refs_free(struct list *list)
{
- list_free(list, ref_free, LINK_OFFSET(struct ref));
+ list_free(list, ref_free, LINK_OFFSET(struct ref, link));
}
static struct link *
diff --git a/src/util.h b/src/util.h
@@ -10,6 +10,9 @@
#define PANIC(...) panic(__FILE__, __LINE__, "" __VA_ARGS__)
#define ASSERT(x) assert((x), __FILE__, __LINE__, #x)
+#define LINK(p) (&(p)->link)
+#define UPCAST(iter, type) LINK_UPCAST(iter, type, link)
+
int strnwidth(const char *s, int n);
void panic(const char *file, int line, const char *msg, ...);