Files
lidm/src/efield.c
javalsai 0b2d0bdf03 fixes: improve login environment handling (#114)
* chore(organization): allow nested header and c files

* feat: add shell login "middleware" & etc:
  * lidm now calls `bash` (or other shells, depends on PACKAGE cfg) in
  login mode as a session wrapper to source most env (can be disabled)
  * this fixes a lot of env problems with all `/etc/profile` and more

Extra:
* implemented a musl compatible version of `execvpe` and now lidm should
  search for PATH everywhere it needs to
* `search_path` now also checks if the found binary is properly
  executable
* lidm now uses `confstr` for a decent PATH default if none is found
* logs are unbuffered for cases where debug logs appear empty (exit
  without handlers moment)

* chore: one-time evaluate certain makefile vars

---------

Co-authored-by: grialion <48643945+grialion@users.noreply.github.com>
2026-02-07 17:12:03 +01:00

81 lines
1.9 KiB
C

#include <string.h>
#include "efield.h"
#include "ui.h"
#include "util/utf8.h"
struct editable_field efield_new(char* content) {
struct editable_field efield;
if (content != NULL) {
efield.pos = strlen(content);
memcpy(efield.content, content, strlen(content) + 1);
} else {
efield_trim(&efield, 0);
}
return efield;
}
void efield_trim(struct editable_field* self, uint8_t pos) {
self->pos = pos;
self->content[pos + 1] = '\0';
}
#define BACKSPACE_CODE 127
void efield_update(struct editable_field* self, char* update) {
uint8_t insert_len = strlen(update);
if (insert_len == 0) return;
if (self->pos > strlen(self->content))
self->pos = strlen(self->content); // WTF tho
if (insert_len == 1) {
// backspace
if (*update == BACKSPACE_CODE) {
if (self->pos == 0) return;
char* curr = &self->content[self->pos];
char* prev = (char*)utf8back(curr);
memmove(prev, curr, strlen(self->content) - self->pos + 1);
self->pos -= curr - prev;
return;
}
// TODO: Del
}
// append
if (strlen(update) + self->pos + 1 >= 255) {
print_err("field too long");
}
// move the after pos, including nullbyte
memmove(&self->content[self->pos + insert_len], &self->content[self->pos],
strlen(self->content) - self->pos + 1);
memcpy(&self->content[self->pos], update, insert_len);
self->pos += insert_len;
}
// returns bool depending if it was able to "use" the seek
bool efield_seek(struct editable_field* self, char seek) {
if (*self->content == '\0') return false;
if (seek == 0) return false;
uint8_t count = seek < 0 ? -seek : seek;
char* ptr = &self->content[self->pos];
char* start = ptr;
while (count-- > 0) {
if (seek < 0) {
if (ptr == self->content) break;
ptr = (char*)utf8back(ptr);
} else {
if (*ptr == '\0') break;
ptr = (char*)utf8seek(ptr);
}
}
self->pos = (uint8_t)(ptr - self->content);
return ptr != start;
}