Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/gwsw/less
Browse files Browse the repository at this point in the history
  • Loading branch information
gwsw committed Nov 30, 2023
2 parents a1a6f61 + 9d4c138 commit ca2826b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 210 deletions.
26 changes: 6 additions & 20 deletions lesstest/lesstest.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
extern TermInfo terminfo;

int verbose = 0;
int explore = 0;
int less_quit = 0;
int details = 0;
char* details_file = NULL;
Expand All @@ -18,14 +17,13 @@ static char* keyfile = NULL;
static int usage(void) {
fprintf(stderr, "usage: lesstest -o file.lt [-w#] [-h#] [-eEdv] [-D detail-file] [-S lt_screen-opts] [--] less.exe [flags] textfile\n");
fprintf(stderr, " or: lesstest -t file.lt less.exe\n");
fprintf(stderr, " or: lesstest -x file.lt [-D detail-file]\n");
return 0;
}

static int setup(int argc, char* const* argv) {
char* logfile = NULL;
int ch;
while ((ch = getopt(argc, argv, "dD:eEk:o:s:S:t:vx:")) != -1) {
while ((ch = getopt(argc, argv, "dD:eEk:o:s:S:t:v")) != -1) {
switch (ch) {
case 'd':
details = 1;
Expand Down Expand Up @@ -57,10 +55,6 @@ static int setup(int argc, char* const* argv) {
case 'v':
verbose = 1;
break;
case 'x':
explore = 1;
testfile = optarg;
break;
default:
return usage();
}
Expand All @@ -77,19 +71,11 @@ int main(int argc, char* const* argv, char* const* envp) {
return RUN_ERR;
int ok = 0;
if (testfile != NULL) { // run existing test
if (explore) {
if (optind != argc) {
usage();
} else {
ok = explore_testfile(testfile);
}
} else {
if (optind+1 != argc) {
usage();
} else {
ok = run_testfile(testfile, argv[optind]);
}
}
if (optind+1 != argc) {
usage();
} else {
ok = run_testfile(testfile, argv[optind]);
}
} else { // gen; create new test
if (optind+2 > argc) {
usage();
Expand Down
190 changes: 0 additions & 190 deletions lesstest/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,193 +247,3 @@ int run_testfile(const char* ltfile, const char* less) {
fclose(testfd);
return (fails == 0);
}

static void free_states(TestState* states, int num_states) {
int s;
for (s = 0; s < num_states; ++s) {
free(states[s].screen);
}
free(states);
}

static int read_states(FILE* testfd, TestState** p_states) {
TestState* states = NULL;
int num_states = 0;
int max_states = 0;
int quit = 0;
while (!quit) {
char line[10000];
int line_len = read_zline(testfd, line, sizeof(line));
if (line_len < 0)
break;
if (line_len < 1)
continue;
switch (line[0]) {
case '=':
if (num_states >= max_states) {
TestState* new_states;
max_states = (max_states+1) * 2;
new_states = malloc(max_states * sizeof(TestState));
if (states != NULL) {
memcpy(new_states, states, num_states * sizeof(TestState));
free(states);
}
states = new_states;
}
states[num_states].screen_len = line_len-1;
states[num_states].screen = (byte*) malloc(states[num_states].screen_len);
states[num_states].ch = 0;
memcpy(states[num_states].screen, line+1, states[num_states].screen_len);
break;
case '+':
states[num_states].ch = (wchar) strtol(line+1, NULL, 16);
++num_states;
break;
case 'Q':
quit = 1;
break;
case '\n':
case '!':
break;
default:
fprintf(stderr, "unrecognized char at start of \"%s\"\n", line);
free_states(states, num_states);
return -1;
}
}
*p_states = states;
return num_states;
}

int explore_testfile(const char* ltfile) {
TestDetails* td = NULL;
if (details_file != NULL) {
FILE* df = fopen(details_file, "r");
if (df == NULL) {
fprintf(stderr, "cannot open %s\n", details_file);
return 0;
}
td = read_test_details(df);
fclose(df);
if (td == NULL)
return 0;
}
FILE* testfd = fopen(ltfile, "r");
if (testfd == NULL) {
fprintf(stderr, "cannot open %s\n", ltfile);
free_test_details(td);
return 0;
}
int ok = 0;
TestSetup* setup = read_test_setup(testfd, NULL);
if (setup != NULL) {
TestState* states;
int num_states = read_states(testfd, &states);
const char* w = get_envp(setup->env.env_list, "COLUMNS");
const char* h = get_envp(setup->env.env_list, "LINES");
int screen_width;
int screen_height;
int ttyin = 0; // stdin
if (w == NULL || (screen_width = atoi(w)) <= 0 ||
h == NULL || (screen_height = atoi(h)) <= 0) {
fprintf(stderr, "no screen geometry found in %s\n", ltfile);
} else {
int curr_state = 0;
int num = -1;
int disp_td = 0;
setup_term();
raw_mode(ttyin, 1);
printf("%s%s", terminfo.init_term, terminfo.enter_keypad);
while (!less_quit) {
printf("%s%s%s[%d/%d]%s", terminfo.clear_screen,
tgoto(terminfo.cursor_move, 1, screen_height+1),
terminfo.enter_bold, curr_state+1, num_states, terminfo.exit_bold);
if (td != NULL && curr_state == td->cmd_num)
printf(" %s", disp_td ? "FAILED(bad)" : "FAILED(good)");
if (states[curr_state].ch) {
wchar ch = states[curr_state].ch;
printf(" Next key: ");
switch (ch) {
case ESC: printf("ESC"); break;
case '\b': printf("\\b"); break;
case '\n': printf("\\n"); break;
case '\r': printf("\\r"); break;
case '\t': printf("\\t"); break;
default:
if (ch > ' ' && ch < 0x7f)
printf("%c", (char) ch);
else
printf("0x%lx", (long) ch);
break;
}
}
if (num >= 0) printf(" Number:%d", num);
printf("%s", tgoto(terminfo.cursor_move, 0, 0));
if (disp_td) {
display_screen(td->img_actual, td->len_actual, screen_width, screen_height);
} else {
display_screen(states[curr_state].screen, states[curr_state].screen_len, screen_width, screen_height);
}
wchar ch = read_wchar(ttyin);
if (ch == 'q') break;
if (ch >= '0' && ch <= '9') {
if (num < 0) num = 0;
num = (10 * num) + (ch - '0');
continue;
}
switch (ch) {
case 'l':
if (num <= 0) num = 1;
curr_state += num;
if (curr_state >= num_states) curr_state = num_states-1;
disp_td = 0;
break;
case 'h':
if (num <= 0) num = 1;
curr_state -= num;
if (curr_state < 0) curr_state = 0;
disp_td = 0;
break;
case 'g': case 'G':
if (num < 0 && ch == 'G') num = num_states;
if (num <= 0) num = 1;
curr_state = num-1;
if (curr_state >= num_states) curr_state = num_states-1;
if (curr_state < 0) curr_state = 0;
disp_td = 0;
break;
case 'L': case 'H':
if (td != NULL)
curr_state = td->cmd_num;
break;
case 'j': case 'k':
if (td != NULL && td->cmd_num == curr_state)
disp_td = !disp_td;
break;
case '?':
printf("%s%s", tgoto(terminfo.cursor_move, 0, screen_height+1), terminfo.clear_eos);
printf("Commands in -x mode:\n");
printf(" [N]l Go to (N-th) next state.\n");
printf(" [N]h Go to (N-th) previous state.\n");
printf(" [N]g Go to first (or N-th) state.\n");
printf(" [N]G Go to last (or N-th) state.\n");
printf(" L Go to failed state.\n");
printf(" j Toggle between good and failed screens.\n");
printf("Press any key to continue.\n");
(void) read_wchar(ttyin);
break;
default:
break;
}
num = -1;
}
printf("%s%s%s", terminfo.clear_screen, terminfo.exit_keypad, terminfo.deinit_term);
raw_mode(ttyin, 0);
free_test_setup(setup);
}
free_states(states, num_states);
}
fclose(testfd);
free_test_details(td);
return ok;
}

0 comments on commit ca2826b

Please sign in to comment.