[headless] multiline support for command files

This commit is contained in:
Timothy Stack 2015-04-07 22:13:44 -07:00
parent 269dfac33e
commit 0b232a7588
4 changed files with 83 additions and 28 deletions

View File

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

View File

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

10
test/multiline.lnav Normal file
View File

@ -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;

View File

@ -514,3 +514,13 @@ run_test ${lnav_test} -n \
check_error_output "bad zoom level is not rejected?" <<EOF check_error_output "bad zoom level is not rejected?" <<EOF
error: invalid zoom level -- bad error: invalid zoom level -- bad
EOF 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