mirror of
https://github.com/rkd77/elinks.git
synced 2024-12-04 14:46:47 -05:00
Merge textarea updates
This commit is contained in:
commit
a96c0250c3
@ -36,7 +36,7 @@ report_scripting_error(struct module *module, struct session *ses,
|
|||||||
|
|
||||||
if (!ses) {
|
if (!ses) {
|
||||||
if (list_empty(terminals)) {
|
if (list_empty(terminals)) {
|
||||||
usrerror("%s error] %s", module->name, msg);
|
usrerror("[%s error] %s", module->name, msg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,8 +4,13 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef _GNU_SOURCE
|
||||||
|
#define _GNU_SOURCE /* XXX: fseeko, ftello */
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@ -325,6 +330,46 @@ add_string_to_string(struct string *string, struct string *from)
|
|||||||
return add_bytes_to_string(string, from->source, from->length);
|
return add_bytes_to_string(string, from->source, from->length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct string *
|
||||||
|
add_file_to_string(struct string *string, unsigned char *filename)
|
||||||
|
{
|
||||||
|
FILE *file;
|
||||||
|
off_t filelen;
|
||||||
|
int newlength;
|
||||||
|
|
||||||
|
assertm(string && filename, "[add_file_to_string]");
|
||||||
|
if_assert_failed { return NULL; }
|
||||||
|
|
||||||
|
check_string_magic(string);
|
||||||
|
|
||||||
|
file = fopen(filename, "rb");
|
||||||
|
if (!file) return NULL;
|
||||||
|
|
||||||
|
if (fseeko(file, 0, SEEK_END)) goto err;
|
||||||
|
|
||||||
|
filelen = ftello(file);
|
||||||
|
if (filelen == -1) goto err;
|
||||||
|
|
||||||
|
if (fseeko(file, 0, SEEK_SET)) goto err;
|
||||||
|
|
||||||
|
newlength = string->length + filelen;
|
||||||
|
if (!realloc_string(string, newlength)) goto err;
|
||||||
|
|
||||||
|
string->length += fread(string->source + string->length, 1,
|
||||||
|
(size_t) filelen, file);
|
||||||
|
string->source[string->length] = 0;
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
if (string->length != newlength) goto err;
|
||||||
|
|
||||||
|
return string;
|
||||||
|
|
||||||
|
err:
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
struct string *
|
struct string *
|
||||||
string_concat(struct string *string, ...)
|
string_concat(struct string *string, ...)
|
||||||
{
|
{
|
||||||
|
@ -157,6 +157,7 @@ struct string *add_to_string(struct string *string,
|
|||||||
const unsigned char *source);
|
const unsigned char *source);
|
||||||
struct string *add_char_to_string(struct string *string, unsigned char character);
|
struct string *add_char_to_string(struct string *string, unsigned char character);
|
||||||
struct string *add_string_to_string(struct string *to, struct string *from);
|
struct string *add_string_to_string(struct string *to, struct string *from);
|
||||||
|
struct string *add_file_to_string(struct string *string, unsigned char *filename);
|
||||||
struct string *add_crlf_to_string(struct string *string);
|
struct string *add_crlf_to_string(struct string *string);
|
||||||
|
|
||||||
/* Adds each C string to @string until a terminating NULL is met. */
|
/* Adds each C string to @string until a terminating NULL is met. */
|
||||||
|
@ -311,37 +311,6 @@ save_textarea_file(unsigned char *value)
|
|||||||
return filename;
|
return filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned char *
|
|
||||||
load_textarea_file(unsigned char *filename, size_t maxlength)
|
|
||||||
{
|
|
||||||
unsigned char *value = NULL;
|
|
||||||
FILE *file = fopen(filename, "rb+");
|
|
||||||
off_t filelen = -1;
|
|
||||||
|
|
||||||
if (!file) return NULL;
|
|
||||||
|
|
||||||
if (!fseeko(file, 0, SEEK_END)) {
|
|
||||||
filelen = ftello(file);
|
|
||||||
if (filelen != -1 && fseeko(file, 0, SEEK_SET))
|
|
||||||
filelen = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (filelen >= 0 && filelen <= maxlength) {
|
|
||||||
value = mem_alloc((size_t) (filelen + 1));
|
|
||||||
if (value) {
|
|
||||||
size_t bread;
|
|
||||||
|
|
||||||
bread = fread(value, 1, (size_t) filelen, file);
|
|
||||||
value[bread] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fclose(file);
|
|
||||||
unlink(filename);
|
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
textarea_edit(int op, struct terminal *term_, struct form_state *fs_,
|
textarea_edit(int op, struct terminal *term_, struct form_state *fs_,
|
||||||
struct document_view *doc_view_, struct link *link_)
|
struct document_view *doc_view_, struct link *link_)
|
||||||
@ -403,16 +372,38 @@ textarea_edit(int op, struct terminal *term_, struct form_state *fs_,
|
|||||||
textarea_editor = 1;
|
textarea_editor = 1;
|
||||||
|
|
||||||
} else if (op == 1 && fs) {
|
} else if (op == 1 && fs) {
|
||||||
unsigned char *value = load_textarea_file(fn, fc_maxlength);
|
struct string file;
|
||||||
|
|
||||||
|
if (!init_string(&file)
|
||||||
|
|| !add_file_to_string(&file, fn)) {
|
||||||
|
textarea_editor = 0;
|
||||||
|
goto free_and_return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file.length > fc_maxlength) {
|
||||||
|
file.source[fc_maxlength] = '\0';
|
||||||
|
info_box(term, MSGBOX_FREE_TEXT, N_("Warning"),
|
||||||
|
ALIGN_CENTER,
|
||||||
|
msg_text(term,
|
||||||
|
N_("You have exceeded the textarea's"
|
||||||
|
" size limit: your input is %d"
|
||||||
|
" bytes, but the maximum is %u"
|
||||||
|
" bytes.\n\n"
|
||||||
|
"Your input has been truncated,"
|
||||||
|
" but you can still recover the"
|
||||||
|
" text that you entered from"
|
||||||
|
" this file: %s"), file.length,
|
||||||
|
fc_maxlength, fn));
|
||||||
|
} else {
|
||||||
|
unlink(fn);
|
||||||
|
}
|
||||||
|
|
||||||
if (value) {
|
|
||||||
mem_free(fs->value);
|
mem_free(fs->value);
|
||||||
fs->value = value;
|
fs->value = file.source;
|
||||||
fs->state = strlen(value);
|
fs->state = file.length;
|
||||||
|
|
||||||
if (doc_view && link)
|
if (doc_view && link)
|
||||||
draw_form_entry(term, doc_view, link);
|
draw_form_entry(term, doc_view, link);
|
||||||
}
|
|
||||||
|
|
||||||
textarea_editor = 0;
|
textarea_editor = 0;
|
||||||
goto free_and_return;
|
goto free_and_return;
|
||||||
|
Loading…
Reference in New Issue
Block a user