[headless] multiline support for command files

pull/209/head
Timothy Stack 10 years ago
parent 269dfac33e
commit 0b232a7588

@ -2575,7 +2575,9 @@ string execute_sql(string sql, string &alt_msg)
return retval;
}
static void execute_file(string path)
string execute_from_file(const string &path, int line_number, char mode, const string &cmdline);
static void execute_file(string path, bool multiline = true)
{
FILE *file;
@ -2586,10 +2588,12 @@ static void execute_file(string path)
return;
}
int line_number = 0;
int line_number = 0, starting_line_number = 0;
char *line = NULL;
size_t line_max_size;
ssize_t line_size;
string cmdline;
char mode = '\0';
while ((line_size = getline(&line, &line_max_size, file)) != -1) {
line_number += 1;
@ -2601,41 +2605,71 @@ static void execute_file(string path)
continue;
}
string rc, alt_msg;
switch (line[0]) {
case ':':
case '/':
case ';':
case '|':
if (mode) {
execute_from_file(path, starting_line_number, mode, trim(cmdline));
}
if (line[line_size - 1] == '\n') {
line[line_size - 1] = '\0';
starting_line_number = line_number;
mode = line[0];
cmdline = string(&line[1]);
break;
default:
if (multiline) {
cmdline += line;
}
else {
execute_from_file(path, line_number, ':', line);
}
break;
}
switch (line[0]) {
}
if (mode) {
execute_from_file(path, starting_line_number, mode, trim(cmdline));
}
if (file != stdin) {
fclose(file);
}
}
string execute_from_file(const string &path, int line_number, char mode, const string &cmdline)
{
string retval, alt_msg;
switch (mode) {
case ':':
rc = execute_command(&line[1]);
retval = execute_command(cmdline);
break;
case '/':
case ';':
setup_logline_table();
rc = execute_sql(&line[1], alt_msg);
retval = execute_sql(cmdline, alt_msg);
break;
case '|':
execute_file(&line[1]);
execute_file(cmdline);
break;
default:
rc = execute_command(line);
retval = execute_command(cmdline);
break;
}
}
if (rescan_files()) {
rebuild_indexes(true);
}
if (rescan_files()) {
rebuild_indexes(true);
}
log_info("%s:%d:execute result -- %s",
log_info("%s:%d:execute result -- %s",
path.c_str(),
line_number,
rc.c_str());
}
retval.c_str());
if (file != stdin) {
fclose(file);
}
return retval;
}
void execute_init_commands(vector<pair<string, string> > &msgs)

@ -243,6 +243,7 @@ dist_noinst_DATA = \
logfile_vami.0 \
logfile_vdsm.0 \
logfile_with_a_really_long_name_to_test_a_bug_with_long_names.0 \
multiline.lnav \
mvwattrline_output.0 \
simple-db.sql \
view_colors_output.0 \

@ -0,0 +1,10 @@
#! /usr/bin/env lnav -f
;CREATE TABLE foobar (
mykey integer primary key,
name text
);
;INSERT INTO foobar VALUES (1, 'Jules');
;SELECT * FROM foobar;

@ -514,3 +514,13 @@ run_test ${lnav_test} -n \
check_error_output "bad zoom level is not rejected?" <<EOF
error: invalid zoom level -- bad
EOF
run_test ${lnav_test} -n \
-f ${test_dir}/multiline.lnav \
${test_dir}/logfile_access_log.0
check_output "multiline commands do not work?" <<EOF
mykey name
1 Jules
EOF

Loading…
Cancel
Save