Allow creating zet in editor

main
cel 5 years ago
parent 9ea941359c
commit e0c1489956
Signed by: cel
GPG Key ID: C28D95BB012367EA

@ -94,7 +94,7 @@ static int count_lines_in(const char *str) {
static int count_lines(int fd) { static int count_lines(int fd) {
int count = 1; int count = 1;
do { if (fd >= 0) do {
unsigned char buf[4096]; unsigned char buf[4096];
size_t len = sizeof(buf)-1; size_t len = sizeof(buf)-1;
int rc = read_some(fd, buf, &len); int rc = read_some(fd, buf, &len);
@ -189,7 +189,7 @@ static int write_topbar(int fd, enum page page, const char *id, const char *titl
static int passthrough_note_html(int fd, int note_fd) { static int passthrough_note_html(int fd, int note_fd) {
int rc; int rc;
do { if (note_fd >= 0) do {
unsigned char buf[4096]; unsigned char buf[4096];
size_t len = sizeof(buf)-1; size_t len = sizeof(buf)-1;
rc = read_some(note_fd, buf, &len); rc = read_some(note_fd, buf, &len);
@ -219,6 +219,7 @@ static int dpi_respond_err(int fd, const char *fmt, ...) {
} }
static char *zet_get_title_fd(int fd, char *buf, size_t len) { static char *zet_get_title_fd(int fd, char *buf, size_t len) {
if (fd < 0) return NULL;
int rc = read_some(fd, (unsigned char *)buf, &len); int rc = read_some(fd, (unsigned char *)buf, &len);
if (rc < 0) return NULL; if (rc < 0) return NULL;
buf[len] = '\0'; buf[len] = '\0';
@ -745,7 +746,7 @@ md_rndr_autolink(struct buf *ob, const struct buf *link, enum mkd_autolink type,
} }
static int dpi_serve_zet(int fd, char *path) { static int dpi_serve_zet(int fd, char *path) {
int rc; int rc = 0;
char *hash, *query; char *hash, *query;
parse_req_uri(path, &hash, &query); parse_req_uri(path, &hash, &query);
@ -761,21 +762,20 @@ static int dpi_serve_zet(int fd, char *path) {
if (sz < 0 || (size_t)sz >= sizeof(path_buf)) return dpi_respond_err(fd, "read_file"); if (sz < 0 || (size_t)sz >= sizeof(path_buf)) return dpi_respond_err(fd, "read_file");
int note_fd = open(path_buf, O_RDONLY); int note_fd = open(path_buf, O_RDONLY);
if (note_fd < 0 && errno == ENOENT) return dpi_serve_not_found(fd); if (note_fd < 0 && errno != ENOENT) return dpi_respond_err(fd, "open");
if (note_fd < 0) return dpi_respond_err(fd, "open");
int rows = count_lines(note_fd); int rows = count_lines(note_fd);
if (rows < 0) return dpi_respond_err(fd, "count_lines"); if (rows < 0) return dpi_respond_err(fd, "count_lines");
static const int max_rows = 57, min_rows = 8; static const int max_rows = 57, min_rows = 8;
if (rows > max_rows) rows = max_rows; if (rows > max_rows) rows = max_rows;
else if (rows < min_rows) rows = min_rows; else if (rows < min_rows) rows = min_rows;
rc = lseek(note_fd, 0, SEEK_SET); if (note_fd >= 0) rc = lseek(note_fd, 0, SEEK_SET);
if (rc < 0) return dpi_respond_err(fd, "lseek"); if (rc < 0) return dpi_respond_err(fd, "lseek");
char buf[128]; char buf[128];
char *title = zet_get_title_fd(note_fd, buf, sizeof(buf)); char *title = zet_get_title_fd(note_fd, buf, sizeof(buf));
if (title == NULL) title = id; if (title == NULL) title = id;
rc = lseek(note_fd, 0, SEEK_SET); if (note_fd >= 0) rc = lseek(note_fd, 0, SEEK_SET);
if (rc < 0) return dpi_respond_err(fd, "lseek"); if (rc < 0) return dpi_respond_err(fd, "lseek");
rc = dpi_send_header(fd, "text/html"); rc = dpi_send_header(fd, "text/html");
@ -803,13 +803,13 @@ static int dpi_serve_zet(int fd, char *path) {
"<td>" "<td>"
); );
if (rc < 0) { warn("dpi_send_header"); close(fd); return 0; } if (rc < 0) { warn("dpi_send_header"); close(fd); return 0; }
rc = lseek(note_fd, 0, SEEK_SET); if (note_fd >= 0) rc = lseek(note_fd, 0, SEEK_SET);
if (rc < 0) { if (rc < 0) {
rc = dprintf(fd, "\n<strong>Error</strong>: %s", strerror(errno)); rc = dprintf(fd, "\n<strong>Error</strong>: %s", strerror(errno));
if (rc < 0) warnx("dprintf"); if (rc < 0) warnx("dprintf");
close(fd); close(fd);
} }
char *text = read_full(note_fd); char *text = note_fd >= 0 ? read_full(note_fd) : NULL;
if (text == NULL) { if (text == NULL) {
rc = dprintf(fd, "\n<strong>Error</strong>: %s", strerror(errno)); rc = dprintf(fd, "\n<strong>Error</strong>: %s", strerror(errno));
if (rc < 0) warnx("dprintf"); if (rc < 0) warnx("dprintf");

Loading…
Cancel
Save