commit 6794f20
chld
·
2026-07-13 16:00:50 +0000 UTC
parent 506a6c3
read from a file
5 files changed,
+111,
-16
+1,
-0
1@@ -1,4 +1,5 @@
2 *.out
3 *.a
4 *.so
5+*.core
6 xl
A
build.sh
+22,
-0
1@@ -0,0 +1,22 @@
2+#!/bin/sh
3+SRC='xl.c rd.c'
4+LIBS=`pkgconf --libs x11`
5+INC=`pkgconf --cflags x11`
6+OUT=xl
7+PREFIX=${PREFIX:-/usr/pkg}
8+
9+case $1 in
10+ make)
11+ cc $SRC $INC $LIBS -o $OUT
12+ ;;
13+ install)
14+ [ ! -f xd ] && return
15+ cp $OUT $PREFIX/bin/$OUT
16+ ;;
17+ uninstall)
18+ rm $PREFIX/bin/$OUT
19+ ;;
20+ clean)
21+ rm $OUT
22+ ;;
23+esac
A
rd.c
+40,
-0
1@@ -0,0 +1,40 @@
2+#include<stdio.h>
3+#include<stdlib.h>
4+#include<unistd.h>
5+
6+char* fbuf(const char* file)
7+{
8+ FILE* f = fopen(file, "rb");
9+
10+ if (!f) return NULL;
11+
12+ fseek(f, 0, SEEK_END);
13+ long size = ftell(f);
14+
15+ if (size < 0)
16+ {
17+ fclose(f);
18+ return NULL;
19+ }
20+
21+ rewind(f);
22+
23+ char* buf = malloc((size_t)size+1);
24+ if (!buf)
25+ {
26+ fclose(f);
27+ return NULL;
28+ }
29+
30+ size_t readb = fread(buf, 1, (size_t)size, f);
31+ fclose(f);
32+
33+ if (readb != (size_t)size)
34+ {
35+ free(buf);
36+ return NULL;
37+ }
38+
39+ buf[size] = '\0';
40+ return buf;
41+}
A
rd.h
+2,
-0
1@@ -0,0 +1,2 @@
2+char* fbuf(const char* file)
3+ ;
M
xl.c
+46,
-16
1@@ -6,6 +6,8 @@
2 #include<stdlib.h>
3 #include<string.h>
4
5+#include"rd.h"
6+
7 /*
8 * a remake of the late `lnd`.
9 * since X is less secure,
10@@ -55,15 +57,33 @@ int main(int ac, char **av)
11 XFontStruct *font = XLoadQueryFont(d, "fixed");
12 XSetFont(d, gc, font->fid);
13
14- int direction, as, des;
15- XCharStruct ov;
16-
17- XTextExtents(font, txt, strlen(txt),
18- &direction,
19- &as,
20- &des,
21- &ov);
22 int32_t sel=0;
23+ int32_t lidx=0;
24+
25+ /* file reading */
26+ char path[128];
27+ snprintf(path, sizeof path, "%s/%s", getenv("HOME"), ".xlrc");
28+ char *prg=fbuf(path);
29+ if(!prg)
30+ {
31+ free(prg);
32+ goto q;
33+ }
34+
35+ /* init prg list */
36+
37+ char *prgs[64];
38+
39+ size_t p_i=0;
40+ char p_c[1024];
41+ strncpy(p_c, prg, sizeof p_c -1); p_c[sizeof p_c-1]='\0';
42+
43+ char *t = strtok(p_c, "\n");
44+ while (t != NULL && p_i < 64){
45+ prgs[p_i++]=t;
46+ t=strtok(NULL, "\n");
47+ }
48+ fprintf(stderr, "%d\n", (int)p_i);
49
50 while(1)
51 {
52@@ -76,7 +96,7 @@ int main(int ac, char **av)
53 if (ev.xexpose.count != 0)
54 break;
55
56- for(int j=0; j<5; ++j)
57+ for(int j=0; j<(int)p_i; ++j)
58 {
59 if(sel==j){
60 XSetForeground(d, gc, WhitePixel(d,s));
61@@ -84,13 +104,22 @@ int main(int ac, char **av)
62 XSetForeground(d, gc, BlackPixel(d,s));
63 }
64
65- XFillRectangle(d,w,gc,0,(hi*j)/5,wi,hi/5);
66+ XFillRectangle(d,w,gc,0,(hi*j)/(int)p_i,wi,hi/(int)p_i);
67 }
68 XFlush(d);
69 /* text drawing */
70 XSetForeground(d, gc, WhitePixel(d,s));
71- for(int i=0; i<5; ++i)
72+ for(int i=0; i<(int)p_i; ++i)
73 {
74+ int direction, as, des;
75+ XCharStruct ov;
76+
77+ XTextExtents(font, prgs[i], strlen(prgs[i]),
78+ &direction,
79+ &as,
80+ &des,
81+ &ov);
82+
83 if(sel==i){
84 XSetForeground(d, gc, BlackPixel(d,s));
85 }else{
86@@ -99,8 +128,9 @@ int main(int ac, char **av)
87
88 XDrawString(d,w,gc,
89 (wi-ov.width)/2,
90- (hi*i)/5+(hi/5+(as+des))/2,
91- txt,txt_len);
92+ (hi*i)/(int)p_i+(hi/(int)p_i+(as+des))/2,
93+ prgs[i],
94+ strlen(prgs[i]));
95 }
96 XSetForeground(d, gc, BlackPixel(d,s));
97 break;
98@@ -110,14 +140,14 @@ int main(int ac, char **av)
99 goto q;
100 break;
101 case ButtonPress:
102- fprintf(stderr, "%s %d\n", txt, sel);
103+ fprintf(stderr, "%s: last sel %d\n", prgs[sel], sel);
104 goto q;
105 break;
106 case ButtonRelease:
107 break;
108 case MotionNotify: {
109- int ssel=ev.xmotion.y/(hi/5);
110- if(ssel>=5) ssel=5-1;
111+ int ssel=ev.xmotion.y/(hi/(int)p_i);
112+ if(ssel>=(int)p_i) ssel=(int)p_i-1;
113
114 if(ssel!=sel)
115 {