| 1 |
|
| 2 |
#include <stdlib.h> |
| 3 |
#include <stdio.h> |
| 4 |
#include <math.h> |
| 5 |
#include <time.h> |
| 6 |
#include <errno.h> |
| 7 |
|
| 8 |
#define BUILTIN_ENABLED 0x01 |
| 9 |
struct word_desc { char* word; int flags; }; |
| 10 |
struct word_list { struct word_list* next; struct word_desc* word; }; |
| 11 |
struct builtin { |
| 12 |
const char* name; |
| 13 |
int (*function)(struct word_list*); |
| 14 |
int flags; |
| 15 |
const char** long_doc; |
| 16 |
const char* short_doc; |
| 17 |
char* handle; |
| 18 |
}; |
| 19 |
|
| 20 |
static int msleep_builtin(struct word_list* list) { |
| 21 |
if (!list || !list->word) return 2; |
| 22 |
double value = atof(list->word->word) * 0.001; |
| 23 |
if (value < 0.0) return 2; |
| 24 |
if (value == 0.0) return 0; |
| 25 |
struct timespec tv; |
| 26 |
tv.tv_sec = floor(value); |
| 27 |
tv.tv_nsec = floor((value - floor(value)) * 1e9); |
| 28 |
while (nanosleep(&tv, &tv) == -1 && errno == EINTR); |
| 29 |
return 0; |
| 30 |
} |
| 31 |
static const char* msleep_doc[] = { "This is a builtin for ble.sh. Sleep for 'msec' milliseconds.", 0 }; |
| 32 |
struct builtin msleep_struct = { "ble/builtin/msleep", msleep_builtin, BUILTIN_ENABLED, msleep_doc, "ble/builtin/msleep msec", 0, }; |