10 #include "bash/config.h"
11 #include "bash/shell.h"
12 #include "bash/builtins.h"
13 #include "bash/builtins/common.h"
14 #include "bash/builtins/bashgetopt.h"
18 int xtitle_builtin(WORD_LIST *list)
20 int main(int argc, char *argv[])
28 reset_internal_getopt();
33 i = internal_getopt(list, "q");
35 int i = getopt(argc, argv, "q");
47 fprintf(stderr, "usage: xtitle [-q] [string]\n");
54 if (!query && loptend == 0) {
56 if (!query && optind == argc) {
58 fprintf(stderr, "xtitle: no string to set\n");
63 char *t = getenv("TERM");
64 if (!t || strncmp(t, "xterm", 5))
71 fd = open("/dev/tty", O_RDWR);
73 fprintf(stderr, "xtitle: couldn't open terminal: %s", strerror(errno));
81 WORD_LIST *l = loptend;
83 write(fd, "\33]0;", 4);
85 write(fd, l->word->word, strlen(l->word->word));
94 write(fd, "\33]0;", 4);
95 for (i = optind; i < argc; i++) {
96 write(fd, argv[i], strlen(argv[i]));
109 n.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
110 |INLCR|IGNCR|ICRNL|IXON);
111 n.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
112 n.c_cflag &= ~(CSIZE|PARENB);
114 tcsetattr(fd, TCSAFLUSH, &n);
115 write(fd, "\33[21t", 5);
117 while (state != -1) {
118 if (read(fd, &hack, 1) < 1)
122 if (hack == '\33') state = 1;
125 if (hack == ']') state = 2; else state = 0;
128 if (hack == 'l') state = 3; else state = 0;
131 if (hack == '\33') state = 4; else putchar(hack);
134 if (hack == '\\') { state = -1; putchar('\n'); }
135 else { putchar('\33'); putchar(hack); state = 3; }
140 tcsetattr(fd, TCSAFLUSH, &o);
152 static char *xtitle_doc[] = {
153 "Either set or read the title of the current xterm window. With the",
154 "-q option, writes the current xterm title to standard output. Without",
155 "the -q option, sets the xterm title to be the arguments given,",
156 "separated by space characters. [By Mark Wooding, mdw@nsict.org]",
160 struct builtin xtitle_struct = {
165 "xtitle [-q] [arguments]",