-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtype-command.c
62 lines (46 loc) · 1.63 KB
/
type-command.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <strings.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
const char *TYPE_CMD_ENABLED = "TYPE_CMD_ENABLED";
const char *TYPE_CMD_TYPE = "TYPE_CMD_TYPE";
const char *PROGRAM_NAME = "type-command";
int main(void) {
struct termios oldtio,newtio;
int fd=0;
int i;
char *is_enabled;
char *command;
// read the bash variables
is_enabled = getenv(TYPE_CMD_ENABLED);
command = getenv(TYPE_CMD_TYPE);
// is the var defined at all
if ( ! is_enabled ) {
printf("%s: the variable %s is not set, set it to 'no' to surpress this message; set the %s for the command to type\n",
PROGRAM_NAME, TYPE_CMD_ENABLED, TYPE_CMD_TYPE);
printf("\n Example: export %s=yes; export %s=date\n", TYPE_CMD_ENABLED, TYPE_CMD_TYPE);
return 0;
// has the variable value different than 'yes'
} else if ( strcmp(is_enabled, "yes") ) {
return 0;
}
if ( ! command ) {
return 0;
}
tcgetattr(fd,&oldtio); // save current terminal settings
bzero(&newtio, sizeof(newtio)); // clear struct for new port settings
memcpy(&newtio, &oldtio, sizeof(oldtio) );
newtio.c_lflag &= ~ECHO; // do not echo the input chars
tcflush(fd, TCIFLUSH); // set the new terminal settings
tcsetattr(fd,TCSANOW,&newtio);
for (i = 0; i < strlen(command); i++)
ioctl(fd, TIOCSTI, &command[i]);
tcsetattr(fd,TCSANOW,&oldtio);
return 0;
}