| 1 |
/* |
| 2 |
* $Id: replay.c,v 1.9 2024/12/05 00:37:39 tom Exp $ |
| 3 |
*/ |
| 4 |
#include <vttest.h> |
| 5 |
|
| 6 |
static FILE *rp; |
| 7 |
static int paused; |
| 8 |
static int marker; |
| 9 |
static char *buffer; |
| 10 |
static size_t length; |
| 11 |
static unsigned lineno; |
| 12 |
|
| 13 |
void |
| 14 |
setup_replay(const char *pathname) |
| 15 |
{ |
| 16 |
close_replay(); |
| 17 |
rp = fopen(pathname, "rb"); |
| 18 |
lineno = 0; |
| 19 |
} |
| 20 |
|
| 21 |
void |
| 22 |
close_replay(void) |
| 23 |
{ |
| 24 |
if (rp != NULL) { |
| 25 |
fclose(rp); |
| 26 |
rp = NULL; |
| 27 |
paused = 0; |
| 28 |
marker = 0; |
| 29 |
free(buffer); |
| 30 |
buffer = NULL; |
| 31 |
length = 0; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
int |
| 36 |
is_replaying(void) |
| 37 |
{ |
| 38 |
return (rp != NULL && !paused); |
| 39 |
} |
| 40 |
|
| 41 |
/* Read the "next" line, skipping data, notes, etc., to return a "Read" line */ |
| 42 |
static char * |
| 43 |
next_line(void) |
| 44 |
{ |
| 45 |
char *result = NULL; |
| 46 |
if (rp != NULL) { |
| 47 |
char *buf; |
| 48 |
char *tmp; |
| 49 |
size_t now = 0; |
| 50 |
|
| 51 |
if (buffer == NULL) |
| 52 |
buffer = malloc(length = BUF_SIZE); |
| 53 |
buf = buffer; |
| 54 |
|
| 55 |
while ((tmp = fgets(buf, BUF_SIZE, rp)) != NULL) { |
| 56 |
size_t len = strlen(buf); |
| 57 |
if (len != 0) { |
| 58 |
if (buf[len - 1] == '\n') |
| 59 |
break; |
| 60 |
if ((now + BUF_SIZE) >= length) { |
| 61 |
buffer = realloc(buffer, length += BUF_SIZE); |
| 62 |
} |
| 63 |
now += len; |
| 64 |
buf = buffer + now; |
| 65 |
} else { |
| 66 |
if (buf != buffer) /* just in case the file had no final \n */ |
| 67 |
tmp = NULL; |
| 68 |
break; |
| 69 |
} |
| 70 |
} |
| 71 |
if (tmp == NULL) |
| 72 |
close_replay(); |
| 73 |
if (buffer != NULL) |
| 74 |
++lineno; |
| 75 |
if (LOG_ENABLED && debug_level > 1) { |
| 76 |
fprintf(log_fp, "[%04u]: %s", lineno, |
| 77 |
(tmp && buffer) ? buffer : "<EOF>\n"); |
| 78 |
} |
| 79 |
result = buffer; |
| 80 |
} |
| 81 |
return result; |
| 82 |
} |
| 83 |
|
| 84 |
static void |
| 85 |
skip_to_tag(const char *tag) |
| 86 |
{ |
| 87 |
size_t need = strlen(tag); |
| 88 |
char *check; |
| 89 |
int skip = 0; |
| 90 |
int jump = !strcmp(tag, WAIT_STR); |
| 91 |
while ((check = next_line()) != NULL) { |
| 92 |
if (LOG_ENABLED && debug_level > 0 && strncmp(check, SKIP_STR, need)) |
| 93 |
fprintf(log_fp, SKIP_STR "%s", check); |
| 94 |
if (!jump && !strncmp(check, WAIT_STR, need)) |
| 95 |
skip = 1; |
| 96 |
if (!skip && !strncmp(check, tag, need)) |
| 97 |
break; |
| 98 |
if (!jump && !strncmp(check, DONE_STR, need)) |
| 99 |
skip = 0; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/* |
| 104 |
* Reverse the changes made by put_string/put_char |
| 105 |
*/ |
| 106 |
static char * |
| 107 |
get_string(char *source) |
| 108 |
{ |
| 109 |
size_t len = strlen(source); |
| 110 |
char *temp = malloc(len + 1); |
| 111 |
|
| 112 |
if (len != 0 && source[len - 1] == '\n') |
| 113 |
source[--len] = '\0'; |
| 114 |
|
| 115 |
if (temp != NULL) { |
| 116 |
char *s, *t; |
| 117 |
for (s = source, t = temp; *s != '\0'; ++s) { |
| 118 |
if (*s == '<' && isdigit(s[1])) { |
| 119 |
long value = strtol(++s, &s, 10); |
| 120 |
if (s == NULL) |
| 121 |
break; |
| 122 |
*t++ = (char) (value & 0xff); |
| 123 |
} else if (*s != ' ') { |
| 124 |
*t++ = *s; |
| 125 |
} |
| 126 |
} |
| 127 |
*t = '\0'; |
| 128 |
strcpy(source, temp); |
| 129 |
free(temp); |
| 130 |
} |
| 131 |
return source; |
| 132 |
} |
| 133 |
|
| 134 |
/* |
| 135 |
* Returns a single character, not replayable. |
| 136 |
*/ |
| 137 |
int |
| 138 |
get_char(void) |
| 139 |
{ |
| 140 |
int result; |
| 141 |
pause_replay(); |
| 142 |
result = inchar(); |
| 143 |
resume_replay(); |
| 144 |
return result; |
| 145 |
} |
| 146 |
|
| 147 |
#define READ_LEN (sizeof(READ_STR) - 1) |
| 148 |
|
| 149 |
/* |
| 150 |
* Returns the string from the next "Read", trimming the newline. |
| 151 |
*/ |
| 152 |
char * |
| 153 |
replay_string(void) |
| 154 |
{ |
| 155 |
char *result = NULL; |
| 156 |
skip_to_tag(READ_STR); |
| 157 |
if (buffer != NULL && !strncmp(buffer, READ_STR, READ_LEN)) { |
| 158 |
result = get_string(buffer + READ_LEN); |
| 159 |
} |
| 160 |
return result; |
| 161 |
} |
| 162 |
|
| 163 |
/* |
| 164 |
* Suspend command-replay when waiting for responses from the terminal, as well |
| 165 |
* as unpredictable user-initiated mouse and key events. The command-file can |
| 166 |
* hold several records; we only care about the "Done" line which happens when |
| 167 |
* replay is resumed. |
| 168 |
*/ |
| 169 |
void |
| 170 |
pause_replay(void) |
| 171 |
{ |
| 172 |
if (is_replaying()) |
| 173 |
skip_to_tag(WAIT_STR); |
| 174 |
paused++; |
| 175 |
if (LOG_ENABLED) { |
| 176 |
if (paused == 1) { |
| 177 |
fprintf(log_fp, WAIT_STR "%d\n", ++marker); |
| 178 |
fflush(log_fp); |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
/* |
| 184 |
* Pausing replay caused us to stop reading the command-file. Skip any unread |
| 185 |
* lines until the "Done" line. |
| 186 |
*/ |
| 187 |
void |
| 188 |
resume_replay(void) |
| 189 |
{ |
| 190 |
if (rp != NULL && paused == 1) |
| 191 |
skip_to_tag(DONE_STR); |
| 192 |
if (paused > 0) { |
| 193 |
--paused; |
| 194 |
} |
| 195 |
if (LOG_ENABLED) { |
| 196 |
if (paused == 0) { |
| 197 |
fprintf(log_fp, DONE_STR "%d\n", marker); |
| 198 |
fflush(log_fp); |
| 199 |
} |
| 200 |
} |
| 201 |
} |