mirror of
https://github.com/tio/tio.git
synced 2026-05-01 14:57:59 +02:00
Add lua expect(string)
Add simple expect functionality.
The expect(string) function will wait for input from the tty device and
only return when there is a string match. Regular expressions are
supported.
Example:
script = expect('password:'); send('my_password\n')
This commit is contained in:
parent
0afae5d3ee
commit
fc54df1f22
4 changed files with 133 additions and 7 deletions
25
src/misc.c
25
src/misc.c
|
|
@ -20,6 +20,7 @@
|
|||
*/
|
||||
|
||||
#include "config.h"
|
||||
#include <regex.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
|
@ -87,3 +88,27 @@ bool fs_dir_exists(const char *path)
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool regex_match(const char *string, const char *pattern)
|
||||
{
|
||||
regex_t regex;
|
||||
int status;
|
||||
|
||||
if (regcomp(®ex, pattern, REG_EXTENDED | REG_NOSUB) != 0)
|
||||
{
|
||||
// No match
|
||||
return false;
|
||||
}
|
||||
|
||||
status = regexec(®ex, string, (size_t) 0, NULL, 0);
|
||||
regfree(®ex);
|
||||
|
||||
if (status != 0)
|
||||
{
|
||||
// No match
|
||||
return false;
|
||||
}
|
||||
|
||||
// Match
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue