📄 conswdir.c
字号:
/* * Process in-band messages about window title changes. * The messages are of the form: * * \033];xxx\007 * * where xxx is the new directory. This format was chosen * because it changes the label on xterm windows. */#include <u.h>#include <libc.h>char *prog = "/bin/rwd";voidusage(void){ fprint(2, "usage: conswdir [/bin/rwd]\n"); exits("usage");}voidsetpath(char *s){ switch(rfork(RFPROC|RFFDG|RFNOWAIT)){ case 0: execl(prog, prog, s, nil); _exits(nil); }}enum{ None, Esc, Brack, Semi, Bell,};intprocess(char *buf, int n, int *pn){ char *p; char path[4096]; int start, state; start = 0; state = None; for(p=buf; p<buf+n; p++){ switch(state){ case None: if(*p == '\033'){ start = p-buf; state++; } break; case Esc: if(*p == ']') state++; else state = None; break; case Brack: if(*p == ';') state++; else state = None; break; case Semi: if(*p == '\007') state++; else if((uchar)*p < 040) state = None; break; } if(state == Bell){ memmove(path, buf+start+3, p - (buf+start+3)); path[p-(buf+start+3)] = 0; p++; memmove(buf+start, p, n-(p-buf)); n -= p-(buf+start); p = buf+start; p--; start = 0; state = None; setpath(path); } } /* give up if we go too long without seeing the close */ *pn = n; if(state == None || p-(buf+start) >= 2048) return (p - buf); else return start;}static voidcatchint(void*, char *msg){ if(strstr(msg, "interrupt")) noted(NCONT); else if(strstr(msg, "kill")) noted(NDFLT); else noted(NCONT);}voidmain(int argc, char **argv){ char buf[4096]; int n, m; notify(catchint); ARGBEGIN{ default: usage(); }ARGEND if(argc > 1) usage(); if(argc == 1) prog = argv[0]; n = 0; for(;;){ m = read(0, buf+n, sizeof buf-n); if(m < 0){ rerrstr(buf, sizeof buf); if(strstr(buf, "interrupt")) continue; exits(nil); } n += m; m = process(buf, n, &n); if(m > 0){ write(1, buf, m); memmove(buf, buf+m, n-m); n -= m; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -