Extract RC_FOCUS implementation to clean up duplicated code.
[screen-lua.git] / src / process.c
blobd6cfda67d979b5867cf386cd518ddd1f0dc10cc2
1 /* Copyright (c) 2008, 2009
2 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
3 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
4 * Micah Cowan (micah@cowan.name)
5 * Sadrul Habib Chowdhury (sadrul@users.sourceforge.net)
6 * Copyright (c) 1993-2002, 2003, 2005, 2006, 2007
7 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
8 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
9 * Copyright (c) 1987 Oliver Laumann
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3, or (at your option)
14 * any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program (see the file COPYING); if not, see
23 * http://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 ****************************************************************
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <signal.h>
32 #include <fcntl.h>
33 #if !defined(sun) && !defined(B43) && !defined(ISC) && !defined(pyr) && !defined(_CX_UX)
34 # include <time.h>
35 #endif
36 #include <sys/time.h>
37 #ifndef sun
38 #include <sys/ioctl.h>
39 #endif
42 #include "config.h"
44 /* for solaris 2.1, Unixware (SVR4.2) and possibly others: */
45 #ifdef HAVE_STROPTS_H
46 # include <sys/stropts.h>
47 #endif
49 #include "screen.h"
50 #include "extern.h"
51 #include "logfile.h"
53 extern struct comm comms[];
54 extern char *rc_name;
55 extern char *RcFileName, *home;
56 extern char *BellString, *ActivityString, *ShellProg, *ShellArgs[];
57 extern char *hstatusstring, *captionstring, *timestring;
58 extern char *wliststr, *wlisttit;
59 extern int captionalways;
60 extern char *hardcopydir, *screenlogfile, *logtstamp_string;
61 extern int log_flush, logtstamp_on, logtstamp_after;
62 extern char *VisualBellString;
63 extern int VBellWait, MsgWait, MsgMinWait, SilenceWait;
64 extern char SockPath[], *SockName;
65 extern int TtyMode, auto_detach, use_altscreen;
66 extern int iflag, maxwin;
67 extern int focusminwidth, focusminheight;
68 extern int use_hardstatus, visual_bell;
69 #ifdef COLOR
70 extern int attr2color[][4];
71 extern int nattr2color;
72 #endif
73 extern int hardstatusemu;
74 extern char *printcmd;
75 extern int default_startup;
76 extern int defobuflimit;
77 extern int defnonblock;
78 extern int ZombieKey_destroy;
79 extern int ZombieKey_resurrect;
80 extern int ZombieKey_onerror;
81 #ifdef AUTO_NUKE
82 extern int defautonuke;
83 #endif
84 extern int separate_sids;
85 extern struct NewWindow nwin_default, nwin_undef;
86 #ifdef COPY_PASTE
87 extern int join_with_cr;
88 extern int compacthist;
89 extern int search_ic;
90 # ifdef FONT
91 extern int pastefont;
92 # endif
93 extern unsigned char mark_key_tab[];
94 extern char *BufferFile;
95 #endif
96 #ifdef POW_DETACH
97 extern char *BufferFile, *PowDetachString;
98 #endif
99 #ifdef MULTIUSER
100 extern struct acluser *EffectiveAclUser; /* acl.c */
101 #endif
102 extern struct term term[]; /* terminal capabilities */
103 #ifdef MAPKEYS
104 extern char *kmapdef[];
105 extern char *kmapadef[];
106 extern char *kmapmdef[];
107 #endif
108 extern struct mchar mchar_so, mchar_null;
109 extern int renditions[];
110 extern int VerboseCreate;
111 #ifdef UTF8
112 extern char *screenencodings;
113 #endif
114 #ifdef DW_CHARS
115 extern int cjkwidth;
116 #endif
118 static int CheckArgNum __P((int, char **));
119 static void ClearAction __P((struct action *));
120 static void SaveAction __P((struct action *, int, char **, int *));
121 static int NextWindow __P((void));
122 static int PreviousWindow __P((void));
123 static int MoreWindows __P((void));
124 static void LogToggle __P((int));
125 static void ShowInfo __P((void));
126 static void ShowDInfo __P((void));
127 static struct win *WindowByName __P((char *));
128 static int WindowByNumber __P((char *));
129 static int ParseOnOff __P((struct action *, int *));
130 static int ParseWinNum __P((struct action *, int *));
131 static int ParseBase __P((struct action *, char *, int *, int, char *));
132 static int ParseNum1000 __P((struct action *, int *));
133 static char **SaveArgs __P((char **));
134 static int IsNum __P((char *, int));
135 static void Colonfin __P((char *, int, char *));
136 static void InputSelect __P((void));
137 static void InputSetenv __P((char *));
138 static void InputAKA __P((void));
139 #ifdef MULTIUSER
140 static int InputSu __P((struct win *, struct acluser **, char *));
141 static void su_fin __P((char *, int, char *));
142 #endif
143 static void AKAfin __P((char *, int, char *));
144 #ifdef COPY_PASTE
145 static void copy_reg_fn __P((char *, int, char *));
146 static void ins_reg_fn __P((char *, int, char *));
147 #endif
148 static void process_fn __P((char *, int, char *));
149 #ifdef PASSWORD
150 static void pass1 __P((char *, int, char *));
151 static void pass2 __P((char *, int, char *));
152 #endif
153 #ifdef POW_DETACH
154 static void pow_detach_fn __P((char *, int, char *));
155 #endif
156 static void digraph_fn __P((char *, int, char *));
157 static int digraph_find __P((const char *buf));
158 static void confirm_fn __P((char *, int, char *));
159 static int IsOnDisplay __P((struct win *));
160 static void ResizeRegions __P((char *, int));
161 static void ResizeFin __P((char *, int, char *));
162 static struct action *FindKtab __P((char *, int));
163 static void SelectFin __P((char *, int, char *));
164 static void SelectLayoutFin __P((char *, int, char *));
166 /* Alias */
167 static void AddAlias __P((const char *name, const char *val , char **args, int *argl, int count));
168 static struct alias * FindAlias __P((const char *name));
169 static struct alias * FindAliasnr __P((int));
170 static void DelAlias __P((const char *name));
171 static int DoAlias __P((const char *, char **, int *));
173 extern struct layer *flayer;
174 extern struct display *display, *displays;
175 extern struct win *fore, *console_window, *windows;
176 extern struct acluser *users;
177 extern struct layout *layouts, *layout_attach, layout_last_marker;
178 extern struct layout *laytab[];
180 extern char screenterm[], HostName[], version[];
181 extern struct NewWindow nwin_undef, nwin_default;
182 extern struct LayFuncs WinLf, MarkLf;
184 extern int Z0width, Z1width;
185 extern int real_uid, real_gid;
187 #ifdef NETHACK
188 extern int nethackflag;
189 #endif
192 struct win *wtab[MAXWIN]; /* window table, should be dynamic */
194 #ifdef MULTIUSER
195 extern char *multi;
196 extern int maxusercount;
197 #endif
198 char NullStr[] = "";
200 struct plop plop_tab[MAX_PLOP_DEFS];
202 #ifndef PTYMODE
203 # define PTYMODE 0622
204 #endif
206 int TtyMode = PTYMODE;
207 int hardcopy_append = 0;
208 int all_norefresh = 0;
209 #ifdef ZMODEM
210 int zmodem_mode = 0;
211 char *zmodem_sendcmd;
212 char *zmodem_recvcmd;
213 static char *zmodes[4] = {"off", "auto", "catch", "pass"};
214 #endif
216 int idletimo;
217 struct action idleaction;
218 #ifdef BLANKER_PRG
219 char **blankerprg;
220 #endif
222 struct action ktab[256 + KMAP_KEYS]; /* command key translation table */
223 struct kclass {
224 struct kclass *next;
225 char *name;
226 struct action ktab[256 + KMAP_KEYS];
228 struct kclass *kclasses;
230 #ifdef MAPKEYS
231 struct action umtab[KMAP_KEYS+KMAP_AKEYS];
232 struct action dmtab[KMAP_KEYS+KMAP_AKEYS];
233 struct action mmtab[KMAP_KEYS+KMAP_AKEYS];
234 struct kmap_ext *kmap_exts;
235 int kmap_extn;
236 static int maptimeout = 300;
237 #endif
239 #ifndef MAX_DIGRAPH
240 #define MAX_DIGRAPH 512
241 #endif
243 struct digraph
245 unsigned char d[2];
246 int value;
250 * Command aliases.
252 struct alias {
253 int nr;
254 char *name; /* Name of the alias */
255 int cmdnr; /* Number of the command this is alias for */
256 char **args; /* The argument list for the command */
257 int *argl;
258 struct alias *next;
260 struct alias *g_aliases_list = NULL;
263 /* digraph table taken from old vim and rfc1345 */
264 static struct digraph digraphs[MAX_DIGRAPH + 1] = {
265 {' ', ' ', 160}, /*   */
266 {'N', 'S', 160}, /*   */
267 {'~', '!', 161}, /* ¡ */
268 {'!', '!', 161}, /* ¡ */
269 {'!', 'I', 161}, /* ¡ */
270 {'c', '|', 162}, /* ¢ */
271 {'c', 't', 162}, /* ¢ */
272 {'$', '$', 163}, /* £ */
273 {'P', 'd', 163}, /* £ */
274 {'o', 'x', 164}, /* ¤ */
275 {'C', 'u', 164}, /* ¤ */
276 {'C', 'u', 164}, /* ¤ */
277 {'E', 'u', 164}, /* ¤ */
278 {'Y', '-', 165}, /* ¥ */
279 {'Y', 'e', 165}, /* ¥ */
280 {'|', '|', 166}, /* ¦ */
281 {'B', 'B', 166}, /* ¦ */
282 {'p', 'a', 167}, /* § */
283 {'S', 'E', 167}, /* § */
284 {'"', '"', 168}, /* ¨ */
285 {'\'', ':', 168}, /* ¨ */
286 {'c', 'O', 169}, /* © */
287 {'C', 'o', 169}, /* © */
288 {'a', '-', 170}, /* ª */
289 {'<', '<', 171}, /* « */
290 {'-', ',', 172}, /* ¬ */
291 {'N', 'O', 172}, /* ¬ */
292 {'-', '-', 173}, /* ­ */
293 {'r', 'O', 174}, /* ® */
294 {'R', 'g', 174}, /* ® */
295 {'-', '=', 175}, /* ¯ */
296 {'\'', 'm', 175}, /* ¯ */
297 {'~', 'o', 176}, /* ° */
298 {'D', 'G', 176}, /* ° */
299 {'+', '-', 177}, /* ± */
300 {'2', '2', 178}, /* ² */
301 {'2', 'S', 178}, /* ² */
302 {'3', '3', 179}, /* ³ */
303 {'3', 'S', 179}, /* ³ */
304 {'\'', '\'', 180}, /* ´ */
305 {'j', 'u', 181}, /* µ */
306 {'M', 'y', 181}, /* µ */
307 {'p', 'p', 182}, /* ¶ */
308 {'P', 'I', 182}, /* ¶ */
309 {'~', '.', 183}, /* · */
310 {'.', 'M', 183}, /* · */
311 {',', ',', 184}, /* ¸ */
312 {'\'', ',', 184}, /* ¸ */
313 {'1', '1', 185}, /* ¹ */
314 {'1', 'S', 185}, /* ¹ */
315 {'o', '-', 186}, /* º */
316 {'>', '>', 187}, /* » */
317 {'1', '4', 188}, /* ¼ */
318 {'1', '2', 189}, /* ½ */
319 {'3', '4', 190}, /* ¾ */
320 {'~', '?', 191}, /* ¿ */
321 {'?', '?', 191}, /* ¿ */
322 {'?', 'I', 191}, /* ¿ */
323 {'A', '`', 192}, /* À */
324 {'A', '!', 192}, /* À */
325 {'A', '\'', 193}, /* Á */
326 {'A', '^', 194}, /* Â */
327 {'A', '>', 194}, /* Â */
328 {'A', '~', 195}, /* Ã */
329 {'A', '?', 195}, /* Ã */
330 {'A', '"', 196}, /* Ä */
331 {'A', ':', 196}, /* Ä */
332 {'A', '@', 197}, /* Å */
333 {'A', 'A', 197}, /* Å */
334 {'A', 'E', 198}, /* Æ */
335 {'C', ',', 199}, /* Ç */
336 {'E', '`', 200}, /* È */
337 {'E', '!', 200}, /* È */
338 {'E', '\'', 201}, /* É */
339 {'E', '^', 202}, /* Ê */
340 {'E', '>', 202}, /* Ê */
341 {'E', '"', 203}, /* Ë */
342 {'E', ':', 203}, /* Ë */
343 {'I', '`', 204}, /* Ì */
344 {'I', '!', 204}, /* Ì */
345 {'I', '\'', 205}, /* Í */
346 {'I', '^', 206}, /* Î */
347 {'I', '>', 206}, /* Î */
348 {'I', '"', 207}, /* Ï */
349 {'I', ':', 207}, /* Ï */
350 {'D', '-', 208}, /* Ð */
351 {'N', '~', 209}, /* Ñ */
352 {'N', '?', 209}, /* Ñ */
353 {'O', '`', 210}, /* Ò */
354 {'O', '!', 210}, /* Ò */
355 {'O', '\'', 211}, /* Ó */
356 {'O', '^', 212}, /* Ô */
357 {'O', '>', 212}, /* Ô */
358 {'O', '~', 213}, /* Õ */
359 {'O', '?', 213}, /* Õ */
360 {'O', '"', 214}, /* Ö */
361 {'O', ':', 214}, /* Ö */
362 {'/', '\\', 215}, /* × */
363 {'*', 'x', 215}, /* × */
364 {'O', '/', 216}, /* Ø */
365 {'U', '`', 217}, /* Ù */
366 {'U', '!', 217}, /* Ù */
367 {'U', '\'', 218}, /* Ú */
368 {'U', '^', 219}, /* Û */
369 {'U', '>', 219}, /* Û */
370 {'U', '"', 220}, /* Ü */
371 {'U', ':', 220}, /* Ü */
372 {'Y', '\'', 221}, /* Ý */
373 {'I', 'p', 222}, /* Þ */
374 {'T', 'H', 222}, /* Þ */
375 {'s', 's', 223}, /* ß */
376 {'s', '"', 223}, /* ß */
377 {'a', '`', 224}, /* à */
378 {'a', '!', 224}, /* à */
379 {'a', '\'', 225}, /* á */
380 {'a', '^', 226}, /* â */
381 {'a', '>', 226}, /* â */
382 {'a', '~', 227}, /* ã */
383 {'a', '?', 227}, /* ã */
384 {'a', '"', 228}, /* ä */
385 {'a', ':', 228}, /* ä */
386 {'a', 'a', 229}, /* å */
387 {'a', 'e', 230}, /* æ */
388 {'c', ',', 231}, /* ç */
389 {'e', '`', 232}, /* è */
390 {'e', '!', 232}, /* è */
391 {'e', '\'', 233}, /* é */
392 {'e', '^', 234}, /* ê */
393 {'e', '>', 234}, /* ê */
394 {'e', '"', 235}, /* ë */
395 {'e', ':', 235}, /* ë */
396 {'i', '`', 236}, /* ì */
397 {'i', '!', 236}, /* ì */
398 {'i', '\'', 237}, /* í */
399 {'i', '^', 238}, /* î */
400 {'i', '>', 238}, /* î */
401 {'i', '"', 239}, /* ï */
402 {'i', ':', 239}, /* ï */
403 {'d', '-', 240}, /* ð */
404 {'n', '~', 241}, /* ñ */
405 {'n', '?', 241}, /* ñ */
406 {'o', '`', 242}, /* ò */
407 {'o', '!', 242}, /* ò */
408 {'o', '\'', 243}, /* ó */
409 {'o', '^', 244}, /* ô */
410 {'o', '>', 244}, /* ô */
411 {'o', '~', 245}, /* õ */
412 {'o', '?', 245}, /* õ */
413 {'o', '"', 246}, /* ö */
414 {'o', ':', 246}, /* ö */
415 {':', '-', 247}, /* ÷ */
416 {'o', '/', 248}, /* ø */
417 {'u', '`', 249}, /* ù */
418 {'u', '!', 249}, /* ù */
419 {'u', '\'', 250}, /* ú */
420 {'u', '^', 251}, /* û */
421 {'u', '>', 251}, /* û */
422 {'u', '"', 252}, /* ü */
423 {'u', ':', 252}, /* ü */
424 {'y', '\'', 253}, /* ý */
425 {'i', 'p', 254}, /* þ */
426 {'t', 'h', 254}, /* þ */
427 {'y', '"', 255}, /* ÿ */
428 {'y', ':', 255}, /* ÿ */
429 {'"', '[', 196}, /* Ä */
430 {'"', '\\', 214}, /* Ö */
431 {'"', ']', 220}, /* Ü */
432 {'"', '{', 228}, /* ä */
433 {'"', '|', 246}, /* ö */
434 {'"', '}', 252}, /* ü */
435 {'"', '~', 223} /* ß */
438 #define RESIZE_FLAG_H 1
439 #define RESIZE_FLAG_V 2
440 #define RESIZE_FLAG_L 4
442 static char *resizeprompts[] = {
443 "resize # lines: ",
444 "resize -h # lines: ",
445 "resize -v # lines: ",
446 "resize -b # lines: ",
447 "resize -l # lines: ",
448 "resize -l -h # lines: ",
449 "resize -l -v # lines: ",
450 "resize -l -b # lines: ",
453 static int
454 parse_input_int(buf, len, val)
455 const char *buf;
456 int len;
457 int *val;
459 int x = 0, i;
460 if (len >= 1 && ((*buf == 'U' && buf[1] == '+') || (*buf == '0' && (buf[1] == 'x' || buf[1] == 'X'))))
462 x = 0;
463 for (i = 2; i < len; i++)
465 if (buf[i] >= '0' && buf[i] <= '9')
466 x = x * 16 | (buf[i] - '0');
467 else if (buf[i] >= 'a' && buf[i] <= 'f')
468 x = x * 16 | (buf[i] - ('a' - 10));
469 else if (buf[i] >= 'A' && buf[i] <= 'F')
470 x = x * 16 | (buf[i] - ('A' - 10));
471 else
472 return 0;
475 else if (buf[0] == '0')
477 x = 0;
478 for (i = 1; i < len; i++)
480 if (buf[i] < '0' || buf[i] > '7')
481 return 0;
482 x = x * 8 | (buf[i] - '0');
485 else
486 return 0;
487 *val = x;
488 return 1;
491 char *noargs[1];
493 void
494 InitKeytab()
496 register unsigned int i;
497 #ifdef MAPKEYS
498 char *argarr[2];
499 #endif
501 for (i = 0; i < sizeof(ktab)/sizeof(*ktab); i++)
503 ktab[i].nr = RC_ILLEGAL;
504 ktab[i].args = noargs;
505 ktab[i].argl = 0;
507 #ifdef MAPKEYS
508 for (i = 0; i < KMAP_KEYS+KMAP_AKEYS; i++)
510 umtab[i].nr = RC_ILLEGAL;
511 umtab[i].args = noargs;
512 umtab[i].argl = 0;
513 dmtab[i].nr = RC_ILLEGAL;
514 dmtab[i].args = noargs;
515 dmtab[i].argl = 0;
516 mmtab[i].nr = RC_ILLEGAL;
517 mmtab[i].args = noargs;
518 mmtab[i].argl = 0;
520 argarr[1] = 0;
521 for (i = 0; i < NKMAPDEF; i++)
523 if (i + KMAPDEFSTART < T_CAPS)
524 continue;
525 if (i + KMAPDEFSTART >= T_CAPS + KMAP_KEYS)
526 continue;
527 if (kmapdef[i] == 0)
528 continue;
529 argarr[0] = kmapdef[i];
530 SaveAction(dmtab + i + (KMAPDEFSTART - T_CAPS), RC_STUFF, argarr, 0);
532 for (i = 0; i < NKMAPADEF; i++)
534 if (i + KMAPADEFSTART < T_CURSOR)
535 continue;
536 if (i + KMAPADEFSTART >= T_CURSOR + KMAP_AKEYS)
537 continue;
538 if (kmapadef[i] == 0)
539 continue;
540 argarr[0] = kmapadef[i];
541 SaveAction(dmtab + i + (KMAPADEFSTART - T_CURSOR + KMAP_KEYS), RC_STUFF, argarr, 0);
543 for (i = 0; i < NKMAPMDEF; i++)
545 if (i + KMAPMDEFSTART < T_CAPS)
546 continue;
547 if (i + KMAPMDEFSTART >= T_CAPS + KMAP_KEYS)
548 continue;
549 if (kmapmdef[i] == 0)
550 continue;
551 argarr[0] = kmapmdef[i];
552 argarr[1] = 0;
553 SaveAction(mmtab + i + (KMAPMDEFSTART - T_CAPS), RC_STUFF, argarr, 0);
555 #endif
557 ktab['h'].nr = RC_HARDCOPY;
558 #ifdef BSDJOBS
559 ktab['z'].nr = ktab[Ctrl('z')].nr = RC_SUSPEND;
560 #endif
561 ktab['c'].nr = ktab[Ctrl('c')].nr = RC_SCREEN;
562 ktab[' '].nr = ktab[Ctrl(' ')].nr =
563 ktab['n'].nr = ktab[Ctrl('n')].nr = RC_NEXT;
564 ktab['N'].nr = RC_NUMBER;
565 ktab[Ctrl('h')].nr = ktab[0177].nr = ktab['p'].nr = ktab[Ctrl('p')].nr = RC_PREV;
566 ktab['k'].nr = ktab[Ctrl('k')].nr = RC_KILL;
567 ktab['l'].nr = ktab[Ctrl('l')].nr = RC_REDISPLAY;
568 ktab['w'].nr = ktab[Ctrl('w')].nr = RC_WINDOWS;
569 ktab['v'].nr = RC_VERSION;
570 ktab[Ctrl('v')].nr = RC_DIGRAPH;
571 ktab['q'].nr = ktab[Ctrl('q')].nr = RC_XON;
572 ktab['s'].nr = ktab[Ctrl('s')].nr = RC_XOFF;
573 ktab['t'].nr = ktab[Ctrl('t')].nr = RC_TIME;
574 ktab['i'].nr = ktab[Ctrl('i')].nr = RC_INFO;
575 ktab['m'].nr = ktab[Ctrl('m')].nr = RC_LASTMSG;
576 ktab['A'].nr = RC_TITLE;
577 #if defined(UTMPOK) && defined(LOGOUTOK)
578 ktab['L'].nr = RC_LOGIN;
579 #endif
580 ktab[','].nr = RC_LICENSE;
581 ktab['W'].nr = RC_WIDTH;
582 ktab['.'].nr = RC_DUMPTERMCAP;
583 ktab[Ctrl('\\')].nr = RC_QUIT;
584 #ifdef DETACH
585 ktab['d'].nr = ktab[Ctrl('d')].nr = RC_DETACH;
586 # ifdef POW_DETACH
587 ktab['D'].nr = RC_POW_DETACH;
588 # endif
589 #endif
590 ktab['r'].nr = ktab[Ctrl('r')].nr = RC_WRAP;
591 ktab['f'].nr = ktab[Ctrl('f')].nr = RC_FLOW;
592 ktab['C'].nr = RC_CLEAR;
593 ktab['Z'].nr = RC_RESET;
594 ktab['H'].nr = RC_LOG;
595 ktab['M'].nr = RC_MONITOR;
596 ktab['?'].nr = RC_HELP;
597 #ifdef MULTI
598 ktab['*'].nr = RC_DISPLAYS;
599 #endif
601 char *args[2];
602 args[0] = "-";
603 args[1] = NULL;
604 SaveAction(ktab + '-', RC_SELECT, args, 0);
606 for (i = 0; i < ((MAXWIN < 10) ? MAXWIN : 10); i++)
608 char *args[2], arg1[10];
609 args[0] = arg1;
610 args[1] = 0;
611 sprintf(arg1, "%d", i);
612 SaveAction(ktab + '0' + i, RC_SELECT, args, 0);
614 ktab['\''].nr = RC_SELECT; /* calling a window by name */
616 char *args[2];
617 args[0] = "-b";
618 args[1] = 0;
619 SaveAction(ktab + '"', RC_WINDOWLIST, args, 0);
621 ktab[Ctrl('G')].nr = RC_VBELL;
622 ktab[':'].nr = RC_COLON;
623 #ifdef COPY_PASTE
624 ktab['['].nr = ktab[Ctrl('[')].nr = RC_COPY;
626 char *args[2];
627 args[0] = ".";
628 args[1] = 0;
629 SaveAction(ktab + ']', RC_PASTE, args, 0);
630 SaveAction(ktab + Ctrl(']'), RC_PASTE, args, 0);
632 ktab['{'].nr = RC_HISTORY;
633 ktab['}'].nr = RC_HISTORY;
634 ktab['>'].nr = RC_WRITEBUF;
635 ktab['<'].nr = RC_READBUF;
636 ktab['='].nr = RC_REMOVEBUF;
637 #endif
638 #ifdef POW_DETACH
639 ktab['D'].nr = RC_POW_DETACH;
640 #endif
641 #ifdef LOCK
642 ktab['x'].nr = ktab[Ctrl('x')].nr = RC_LOCKSCREEN;
643 #endif
644 ktab['b'].nr = ktab[Ctrl('b')].nr = RC_BREAK;
645 ktab['B'].nr = RC_POW_BREAK;
646 ktab['_'].nr = RC_SILENCE;
647 ktab['S'].nr = RC_SPLIT;
648 ktab['Q'].nr = RC_ONLY;
649 ktab['X'].nr = RC_REMOVE;
650 ktab['F'].nr = RC_FIT;
651 ktab['\t'].nr = RC_FOCUS;
653 char *args[2];
654 args[0] = "up";
655 args[1] = 0;
656 SaveAction(ktab + T_BACKTAB - T_CAPS + 256, RC_FOCUS, args, 0);
659 char *args[2];
660 args[0] = "-v";
661 args[1] = 0;
662 SaveAction(ktab + '|', RC_SPLIT, args, 0);
664 /* These come last; they may want overwrite others: */
665 if (DefaultEsc >= 0)
667 ClearAction(&ktab[DefaultEsc]);
668 ktab[DefaultEsc].nr = RC_OTHER;
670 if (DefaultMetaEsc >= 0)
672 ClearAction(&ktab[DefaultMetaEsc]);
673 ktab[DefaultMetaEsc].nr = RC_META;
676 idleaction.nr = RC_BLANKER;
677 idleaction.args = noargs;
678 idleaction.argl = 0;
681 static struct action *
682 FindKtab(class, create)
683 char *class;
684 int create;
686 struct kclass *kp, **kpp;
687 int i;
689 if (class == 0)
690 return ktab;
691 for (kpp = &kclasses; (kp = *kpp) != 0; kpp = &kp->next)
692 if (!strcmp(kp->name, class))
693 break;
694 if (kp == 0)
696 if (!create)
697 return 0;
698 if (strlen(class) > 80)
700 Msg(0, "Command class name too long.");
701 return 0;
703 kp = malloc(sizeof(*kp));
704 if (kp == 0)
706 Msg(0, strnomem);
707 return 0;
709 kp->name = SaveStr(class);
710 for (i = 0; i < (int)(sizeof(kp->ktab)/sizeof(*kp->ktab)); i++)
712 kp->ktab[i].nr = RC_ILLEGAL;
713 kp->ktab[i].args = noargs;
715 kp->next = 0;
716 *kpp = kp;
718 return kp->ktab;
721 static void
722 ClearAction(act)
723 struct action *act;
725 char **p;
727 if (act->nr == RC_ILLEGAL)
728 return;
729 act->nr = RC_ILLEGAL;
730 if (act->args == noargs)
731 return;
732 for (p = act->args; *p; p++)
733 free(*p);
734 free((char *)act->args);
735 act->args = noargs;
736 act->argl = 0;
740 * ProcessInput: process input from display and feed it into
741 * the layer on canvas D_forecv.
744 #ifdef MAPKEYS
747 * This ProcessInput just does the keybindings and passes
748 * everything else on to ProcessInput2.
751 void
752 ProcessInput(ibuf, ilen)
753 char *ibuf;
754 int ilen;
756 int ch, slen;
757 unsigned char *s, *q;
758 int i, l;
759 char *p;
761 debug1("ProcessInput: %d bytes\n", ilen);
762 if (display == 0 || ilen == 0)
763 return;
764 if (D_seql)
765 evdeq(&D_mapev);
766 slen = ilen;
767 s = (unsigned char *)ibuf;
768 while (ilen-- > 0)
770 ch = *s++;
771 if (D_dontmap || !D_nseqs)
773 D_dontmap = 0;
774 continue;
776 for (;;)
778 debug3("cmp %c %c[%d]\n", ch, *D_seqp, D_seqp - D_kmaps);
779 if (*D_seqp != ch)
781 l = D_seqp[D_seqp[-D_seql-1] + 1];
782 if (l)
784 D_seqp += l * 2 + 4;
785 debug1("miss %d\n", D_seqp - D_kmaps);
786 continue;
788 debug("complete miss\n");
789 D_mapdefault = 0;
790 l = D_seql;
791 p = (char *)D_seqp - l;
792 D_seql = 0;
793 D_seqp = D_kmaps + 3;
794 if (l == 0)
795 break;
796 if ((q = D_seqh) != 0)
798 D_seqh = 0;
799 i = q[0] << 8 | q[1];
800 i &= ~KMAP_NOTIMEOUT;
801 debug1("Mapping former hit #%d - ", i);
802 debug2("%d(%s) - ", q[2], q + 3);
803 if (StuffKey(i))
804 ProcessInput2((char *)q + 3, q[2]);
805 if (display == 0)
806 return;
807 l -= q[2];
808 p += q[2];
810 else
811 D_dontmap = 1;
812 debug1("flush old %d\n", l);
813 ProcessInput(p, l);
814 if (display == 0)
815 return;
816 evdeq(&D_mapev);
817 continue;
819 if (D_seql++ == 0)
821 /* Finish old stuff */
822 slen -= ilen + 1;
823 debug1("finish old %d\n", slen);
824 if (slen)
825 ProcessInput2(ibuf, slen);
826 if (display == 0)
827 return;
828 D_seqh = 0;
830 ibuf = (char *)s;
831 slen = ilen;
832 D_seqp++;
833 l = D_seql;
834 debug2("length am %d, want %d\n", l, D_seqp[-l - 1]);
835 if (l == D_seqp[-l - 1])
837 if (D_seqp[l] != l)
839 q = D_seqp + 1 + l;
840 if (D_kmaps + D_nseqs > q && q[2] > l && !bcmp(D_seqp - l, q + 3, l))
842 debug1("have another mapping (%s), delay execution\n", q + 3);
843 D_seqh = D_seqp - 3 - l;
844 D_seqp = q + 3 + l;
845 break;
848 i = D_seqp[-l - 3] << 8 | D_seqp[-l - 2];
849 i &= ~KMAP_NOTIMEOUT;
850 debug1("Mapping #%d - ", i);
851 p = (char *)D_seqp - l;
852 debug2("%d(%s) - ", l, p);
853 D_seql = 0;
854 D_seqp = D_kmaps + 3;
855 D_seqh = 0;
856 if (StuffKey(i))
857 ProcessInput2(p, l);
858 if (display == 0)
859 return;
861 break;
864 if (D_seql)
866 debug("am in sequence -> check for timeout\n");
867 l = D_seql;
868 for (s = D_seqp; ; s += i * 2 + 4)
870 if (s[-l-3] & KMAP_NOTIMEOUT >> 8)
871 break;
872 if ((i = s[s[-l-1] + 1]) == 0)
874 SetTimeout(&D_mapev, maptimeout);
875 evenq(&D_mapev);
876 break;
880 ProcessInput2(ibuf, slen);
883 #else
884 # define ProcessInput2 ProcessInput
885 #endif
889 * Here only the screen escape commands are handled.
892 void
893 ProcessInput2(ibuf, ilen)
894 char *ibuf;
895 int ilen;
897 char *s;
898 int ch, slen;
899 struct action *ktabp;
901 debug1("ProcessInput2: %d bytes\n", ilen);
902 while (ilen && display)
904 debug1(" - ilen now %d bytes\n", ilen);
905 flayer = D_forecv->c_layer;
906 fore = D_fore;
907 slen = ilen;
908 s = ibuf;
909 if (!D_ESCseen)
911 while (ilen > 0)
913 if ((unsigned char)*s++ == D_user->u_Esc)
914 break;
915 ilen--;
917 slen -= ilen;
918 if (slen)
919 DoProcess(fore, &ibuf, &slen, 0);
920 if (--ilen == 0)
921 D_ESCseen = ktab;
923 if (ilen <= 0)
924 return;
925 ktabp = D_ESCseen ? D_ESCseen : ktab;
926 D_ESCseen = 0;
927 ch = (unsigned char)*s;
930 * As users have different esc characters, but a common ktab[],
931 * we fold back the users esc and meta-esc key to the Default keys
932 * that can be looked up in the ktab[]. grmbl. jw.
933 * XXX: make ktab[] a per user thing.
935 if (ch == D_user->u_Esc)
936 ch = DefaultEsc;
937 else if (ch == D_user->u_MetaEsc)
938 ch = DefaultMetaEsc;
940 if (ch >= 0)
941 DoAction(&ktabp[ch], ch);
942 ibuf = (char *)(s + 1);
943 ilen--;
947 void
948 DoProcess(p, bufp, lenp, pa)
949 struct win *p;
950 char **bufp;
951 int *lenp;
952 struct paster *pa;
954 int oldlen;
955 struct display *d = display;
957 #ifdef COPY_PASTE
958 /* XXX -> PasteStart */
959 if (pa && *lenp > 1 && p && p->w_slowpaste)
961 /* schedule slowpaste event */
962 SetTimeout(&p->w_paster.pa_slowev, p->w_slowpaste);
963 evenq(&p->w_paster.pa_slowev);
964 return;
966 #endif
967 while (flayer && *lenp)
969 #ifdef COPY_PASTE
970 if (!pa && p && p->w_paster.pa_pastelen && flayer == p->w_paster.pa_pastelayer)
972 debug("layer is busy - beep!\n");
973 WBell(p, visual_bell);
974 *bufp += *lenp;
975 *lenp = 0;
976 display = d;
977 return;
979 #endif
980 oldlen = *lenp;
981 LayProcess(bufp, lenp);
982 #ifdef COPY_PASTE
983 if (pa && !pa->pa_pastelayer)
984 break; /* flush rest of paste */
985 #endif
986 if (*lenp == oldlen)
988 if (pa)
990 display = d;
991 return;
993 /* We're full, let's beep */
994 debug("layer is full - beep!\n");
995 WBell(p, visual_bell);
996 break;
999 *bufp += *lenp;
1000 *lenp = 0;
1001 display = d;
1002 #ifdef COPY_PASTE
1003 if (pa && pa->pa_pastelen == 0)
1004 FreePaster(pa);
1005 #endif
1009 FindCommnr(str)
1010 char *str;
1012 int x, m, l = 0, r = RC_LAST;
1013 while (l <= r)
1015 m = (l + r) / 2;
1016 x = strcmp(str, comms[m].name);
1017 if (x > 0)
1018 l = m + 1;
1019 else if (x < 0)
1020 r = m - 1;
1021 else
1022 return m;
1024 return RC_ILLEGAL;
1027 static int
1028 CheckArgNum(nr, args)
1029 int nr;
1030 char **args;
1032 int i, n;
1033 static char *argss[] = {"no", "one", "two", "three", "four", "OOPS"};
1034 static char *orformat[] =
1036 "%s: %s: %s argument%s required",
1037 "%s: %s: %s or %s argument%s required",
1038 "%s: %s: %s, %s or %s argument%s required",
1039 "%s: %s: %s, %s, %s or %s argument%s required"
1042 n = comms[nr].flags & ARGS_MASK;
1043 for (i = 0; args[i]; i++)
1045 if (comms[nr].flags & ARGS_ORMORE)
1047 if (i < n)
1049 Msg(0, "%s: %s: at least %s argument%s required",
1050 rc_name, comms[nr].name, argss[n], n != 1 ? "s" : "");
1051 return -1;
1054 else if ((comms[nr].flags & ARGS_PLUS1) &&
1055 (comms[nr].flags & ARGS_PLUS2) &&
1056 (comms[nr].flags & ARGS_PLUS3))
1058 if (i != n && i != n + 1 && i != n + 2 && i != n + 3)
1060 Msg(0, orformat[3], rc_name, comms[nr].name, argss[n],
1061 argss[n + 1], argss[n + 2], argss[n + 3], "");
1062 return -1;
1065 else if ((comms[nr].flags & ARGS_PLUS1) &&
1066 (comms[nr].flags & ARGS_PLUS2))
1068 if (i != n && i != n + 1 && i != n + 2)
1070 Msg(0, orformat[2], rc_name, comms[nr].name, argss[n],
1071 argss[n + 1], argss[n + 2], "");
1072 return -1;
1075 else if ((comms[nr].flags & ARGS_PLUS1) &&
1076 (comms[nr].flags & ARGS_PLUS3))
1078 if (i != n && i != n + 1 && i != n + 3)
1080 Msg(0, orformat[2], rc_name, comms[nr].name, argss[n],
1081 argss[n + 1], argss[n + 3], "");
1082 return -1;
1085 else if ((comms[nr].flags & ARGS_PLUS2) &&
1086 (comms[nr].flags & ARGS_PLUS3))
1088 if (i != n && i != n + 2 && i != n + 3)
1090 Msg(0, orformat[2], rc_name, comms[nr].name, argss[n],
1091 argss[n + 2], argss[n + 3], "");
1092 return -1;
1095 else if (comms[nr].flags & ARGS_PLUS1)
1097 if (i != n && i != n + 1)
1099 Msg(0, orformat[1], rc_name, comms[nr].name, argss[n],
1100 argss[n + 1], n != 0 ? "s" : "");
1101 return -1;
1104 else if (comms[nr].flags & ARGS_PLUS2)
1106 if (i != n && i != n + 2)
1108 Msg(0, orformat[1], rc_name, comms[nr].name, argss[n],
1109 argss[n + 2], "s");
1110 return -1;
1113 else if (comms[nr].flags & ARGS_PLUS3)
1115 if (i != n && i != n + 3)
1117 Msg(0, orformat[1], rc_name, comms[nr].name, argss[n],
1118 argss[n + 3], "");
1119 return -1;
1122 else if (i != n)
1124 Msg(0, orformat[0], rc_name, comms[nr].name, argss[n], n != 1 ? "s" : "");
1125 return -1;
1127 return i;
1130 static void
1131 StuffFin(buf, len, data)
1132 char *buf;
1133 int len;
1134 char *data;
1136 if (!flayer)
1137 return;
1138 while(len)
1139 LayProcess(&buf, &len);
1142 void
1143 ChangeWinNum(old, new)
1144 int old;
1145 int new;
1147 struct win *pnew = wtab[new];
1148 struct win *pold = wtab[old];
1149 if (new < 0 || new >= maxwin)
1151 Msg(0, "Given window position is invalid.");
1152 return;
1154 wtab[new] = pold;
1155 pold->w_number = new;
1156 wtab[old] = pnew;
1157 if (pnew)
1158 pnew->w_number = old;
1159 #ifdef MULTIUSER
1160 /* exchange the acls for these windows. */
1161 AclWinSwap(old, new);
1162 #endif
1163 #ifdef UTMPOK
1164 /* exchange the utmp-slots for these windows */
1165 if ((pold->w_slot != (slot_t) -1) && (pold->w_slot != (slot_t) 0))
1167 RemoveUtmp(pold);
1168 SetUtmp(pold);
1170 if (pnew && (pnew->w_slot != (slot_t) -1) && (pnew->w_slot != (slot_t) 0))
1172 /* XXX: first display wins? */
1173 display = pold->w_layer.l_cvlist ? pold->w_layer.l_cvlist->c_display : 0;
1174 RemoveUtmp(pnew);
1175 SetUtmp(pnew);
1177 #endif
1179 WindowChanged(pold, 'n');
1180 WindowChanged((struct win *)0, 'w');
1181 WindowChanged((struct win *)0, 'W');
1182 WindowChanged((struct win *)0, 0);
1185 void
1186 FocusCanvas(struct canvas *cv)
1188 D_forecv = cv;
1190 if ((focusminwidth && (focusminwidth < 0 || D_forecv->c_xe - D_forecv->c_xs + 1 < focusminwidth)) ||
1191 (focusminheight && (focusminheight < 0 || D_forecv->c_ye - D_forecv->c_ys + 1 < focusminheight)))
1193 ResizeCanvas(&D_canvas);
1194 RecreateCanvasChain();
1195 RethinkDisplayViewports();
1196 ResizeLayersToCanvases(); /* redisplays */
1198 #ifdef SCRIPT
1200 struct win *oldfore = D_fore;
1201 fore = D_fore = Layer2Window(D_forecv->c_layer);
1202 if (oldfore != fore)
1204 if (oldfore)
1205 trigger_sevent(&oldfore->w_sev.onleave, display, oldfore);
1206 if (fore)
1207 trigger_sevent(&fore->w_sev.onfocus, display, oldfore);
1208 trigger_sevent(&globalevents.forechanged, display, display ? D_fore : fore);
1211 #else
1212 fore = D_fore = Layer2Window(D_forecv->c_layer);
1213 #endif
1214 if (D_other == fore)
1215 D_other = 0;
1216 flayer = D_forecv->c_layer;
1217 #ifdef RXVT_OSC
1218 if (D_xtermosc[2] || D_xtermosc[3])
1220 Activate(-1);
1221 break;
1223 #endif
1224 RefreshHStatus();
1225 #ifdef RXVT_OSC
1226 RefreshXtermOSC();
1227 #endif
1228 flayer = D_forecv->c_layer;
1229 CV_CALL(D_forecv, LayRestore();LaySetCursor());
1230 WindowChanged(0, 'F');
1233 /*ARGSUSED*/
1234 void
1235 DoAction(act, key)
1236 struct action *act;
1237 int key;
1239 int nr = act->nr;
1240 char **args = act->args;
1241 int *argl = act->argl;
1242 struct win *p;
1243 int argc, i, n, msgok;
1244 char *s;
1245 char ch;
1246 struct display *odisplay = display;
1247 struct acluser *user;
1249 user = display ? D_user : users;
1251 if (nr > RC_LAST)
1253 struct alias *alias = FindAliasnr(nr);
1254 if (alias)
1256 DoAlias(alias->name, act->args, act->argl);
1257 return;
1259 nr = RC_ILLEGAL;
1262 if (nr == RC_ILLEGAL)
1264 debug1("key '%c': No action\n", key);
1265 return;
1268 n = comms[nr].flags;
1269 if ((n & NEED_DISPLAY) && display == 0)
1271 Msg(0, "%s: %s: display required", rc_name, comms[nr].name);
1272 return;
1274 if ((n & NEED_FORE) && fore == 0)
1276 Msg(0, "%s: %s: window required", rc_name, comms[nr].name);
1277 return;
1279 if ((n & NEED_LAYER) && flayer == 0)
1281 Msg(0, "%s: %s: display or window required", rc_name, comms[nr].name);
1282 return;
1284 if ((argc = CheckArgNum(nr, args)) < 0)
1285 return;
1286 #ifdef MULTIUSER
1287 if (display)
1289 if (AclCheckPermCmd(D_user, ACL_EXEC, &comms[nr]))
1291 Msg(0, "%s: %s: permission denied (user %s)",
1292 rc_name, comms[nr].name, (EffectiveAclUser ? EffectiveAclUser : D_user)->u_name);
1293 return;
1296 #endif /* MULTIUSER */
1298 msgok = display && !*rc_name;
1299 switch(nr)
1301 case RC_SELECT:
1302 if (!*args)
1303 InputSelect();
1304 else if (args[0][0] == '-' && !args[0][1])
1306 SetForeWindow((struct win *)0);
1307 Activate(0);
1309 else if (args[0][0] == '.' && !args[0][1])
1311 if (!fore)
1312 Msg(0, "select . needs a window");
1313 else
1315 SetForeWindow(fore);
1316 Activate(0);
1319 else if (ParseWinNum(act, &n) == 0)
1320 SwitchWindow(n);
1321 break;
1322 #ifdef AUTO_NUKE
1323 case RC_DEFAUTONUKE:
1324 if (ParseOnOff(act, &defautonuke) == 0 && msgok)
1325 Msg(0, "Default autonuke turned %s", defautonuke ? "on" : "off");
1326 if (display && *rc_name)
1327 D_auto_nuke = defautonuke;
1328 break;
1329 case RC_AUTONUKE:
1330 if (ParseOnOff(act, &D_auto_nuke) == 0 && msgok)
1331 Msg(0, "Autonuke turned %s", D_auto_nuke ? "on" : "off");
1332 break;
1333 #endif
1334 case RC_DEFOBUFLIMIT:
1335 if (ParseNum(act, &defobuflimit) == 0 && msgok)
1336 Msg(0, "Default limit set to %d", defobuflimit);
1337 if (display && *rc_name)
1339 D_obufmax = defobuflimit;
1340 D_obuflenmax = D_obuflen - D_obufmax;
1342 break;
1343 case RC_OBUFLIMIT:
1344 if (*args == 0)
1345 Msg(0, "Limit is %d, current buffer size is %d", D_obufmax, D_obuflen);
1346 else if (ParseNum(act, &D_obufmax) == 0 && msgok)
1347 Msg(0, "Limit set to %d", D_obufmax);
1348 D_obuflenmax = D_obuflen - D_obufmax;
1349 break;
1350 case RC_DUMPTERMCAP:
1351 WriteFile(user, (char *)0, DUMP_TERMCAP);
1352 break;
1353 case RC_HARDCOPY:
1355 int mode = DUMP_HARDCOPY;
1357 if (argc > 1 && !strcmp(*args, "-h"))
1359 mode = DUMP_SCROLLBACK;
1360 args++;
1361 argc--;
1363 if (*args && args[1])
1365 Msg(0, "%s: hardcopy: too many arguments", rc_name);
1366 break;
1368 if (fore == 0 && *args == 0)
1369 Msg(0, "%s: hardcopy: window required", rc_name);
1370 else
1371 WriteFile(user, *args, mode);
1373 break;
1374 case RC_DEFLOG:
1375 (void)ParseOnOff(act, &nwin_default.Lflag);
1376 break;
1377 case RC_LOG:
1378 n = fore->w_log ? 1 : 0;
1379 ParseSwitch(act, &n);
1380 LogToggle(n);
1381 break;
1382 #ifdef BSDJOBS
1383 case RC_SUSPEND:
1384 Detach(D_STOP);
1385 break;
1386 #endif
1387 case RC_NEXT:
1388 if (MoreWindows())
1389 SwitchWindow(NextWindow());
1390 break;
1391 case RC_PREV:
1392 if (MoreWindows())
1393 SwitchWindow(PreviousWindow());
1394 break;
1395 case RC_KILL:
1397 char *name;
1399 if (key >= 0)
1401 #ifdef PSEUDOS
1402 Input(fore->w_pwin ? "Really kill this filter [y/n]" : "Really kill this window [y/n]", 1, INP_RAW, confirm_fn, NULL, RC_KILL);
1403 #else
1404 Input("Really kill this window [y/n]", 1, INP_RAW, confirm_fn, NULL, RC_KILL);
1405 #endif
1406 break;
1408 n = fore->w_number;
1409 #ifdef PSEUDOS
1410 if (fore->w_pwin)
1412 FreePseudowin(fore);
1413 Msg(0, "Filter removed.");
1414 break;
1416 #endif
1417 name = SaveStr(fore->w_title);
1418 KillWindow(fore);
1419 Msg(0, "Window %d (%s) killed.", n, name);
1420 if (name)
1421 free(name);
1422 break;
1424 case RC_QUIT:
1425 if (key >= 0)
1427 Input("Really quit and kill all your windows [y/n]", 1, INP_RAW, confirm_fn, NULL, RC_QUIT);
1428 break;
1430 Finit(0);
1431 /* NOTREACHED */
1432 #ifdef DETACH
1433 case RC_DETACH:
1434 if (*args && !strcmp(*args, "-h"))
1435 Hangup();
1436 else
1437 Detach(D_DETACH);
1438 break;
1439 # ifdef POW_DETACH
1440 case RC_POW_DETACH:
1441 if (key >= 0)
1443 static char buf[2];
1445 buf[0] = key;
1446 Input(buf, 1, INP_RAW, pow_detach_fn, NULL, 0);
1448 else
1449 Detach(D_POWER); /* detach and kill Attacher's parent */
1450 break;
1451 # endif
1452 #endif
1453 case RC_DEBUG:
1454 #ifdef DEBUG
1455 if (!*args)
1457 if (dfp)
1458 Msg(0, "debugging info is written to %s/", DEBUGDIR);
1459 else
1460 Msg(0, "debugging is currently off. Use 'debug on' to enable.");
1461 break;
1463 if (dfp)
1465 debug("debug: closing debug file.\n");
1466 fflush(dfp);
1467 fclose(dfp);
1468 dfp = NULL;
1470 if (strcmp("off", *args))
1471 opendebug(0, 1);
1472 # ifdef SIG_NODEBUG
1473 else if (display)
1474 kill(D_userpid, SIG_NODEBUG); /* a one shot item, but hey... */
1475 # endif /* SIG_NODEBUG */
1476 #else
1477 if (*args == 0 || strcmp("off", *args))
1478 Msg(0, "Sorry, screen was compiled without -DDEBUG option.");
1479 #endif
1480 break;
1481 #ifdef ZMODEM
1482 case RC_ZMODEM:
1483 if (*args && !strcmp(*args, "sendcmd"))
1485 if (args[1])
1487 free(zmodem_sendcmd);
1488 zmodem_sendcmd = SaveStr(args[1]);
1490 if (msgok)
1491 Msg(0, "zmodem sendcmd: %s", zmodem_sendcmd);
1492 break;
1494 if (*args && !strcmp(*args, "recvcmd"))
1496 if (args[1])
1498 free(zmodem_recvcmd);
1499 zmodem_recvcmd = SaveStr(args[1]);
1501 if (msgok)
1502 Msg(0, "zmodem recvcmd: %s", zmodem_recvcmd);
1503 break;
1505 if (*args)
1507 for (i = 0; i < 4; i++)
1508 if (!strcmp(zmodes[i], *args))
1509 break;
1510 if (i == 4 && !strcmp(*args, "on"))
1511 i = 1;
1512 if (i == 4)
1514 Msg(0, "usage: zmodem off|auto|catch|pass");
1515 break;
1517 zmodem_mode = i;
1519 if (msgok)
1520 Msg(0, "zmodem mode is %s", zmodes[zmodem_mode]);
1521 break;
1522 #endif
1523 case RC_ZOMBIE:
1525 if (!(s = *args))
1527 ZombieKey_destroy = 0;
1528 break;
1530 if (*argl == 0 || *argl > 2)
1532 Msg(0, "%s:zombie: one or two characters expected.", rc_name);
1533 break;
1535 if (args[1])
1537 if (!strcmp(args[1], "onerror"))
1539 ZombieKey_onerror = 1;
1540 } else {
1541 Msg(0, "usage: zombie [keys [onerror]]");
1542 break;
1544 } else
1545 ZombieKey_onerror = 0;
1546 ZombieKey_destroy = args[0][0];
1547 ZombieKey_resurrect = *argl == 2 ? args[0][1] : 0;
1549 break;
1550 case RC_WALL:
1551 #ifdef MULTIUSER
1552 s = D_user->u_name;
1553 #else
1554 s = D_usertty;
1555 #endif
1557 struct display *olddisplay = display;
1558 display = 0; /* no display will cause a broadcast */
1559 Msg(0, "%s: %s", s, *args);
1560 display = olddisplay;
1562 break;
1563 case RC_AT:
1564 /* where this AT command comes from: */
1565 #ifdef MULTIUSER
1566 s = SaveStr(D_user->u_name);
1567 /* DO NOT RETURN FROM HERE WITHOUT RESETTING THIS: */
1568 EffectiveAclUser = D_user;
1569 #else
1570 s = SaveStr(D_usertty);
1571 #endif
1572 n = strlen(args[0]);
1573 if (n) n--;
1575 * the windows/displays loops are quite dangerous here, take extra
1576 * care not to trigger landmines. Things may appear/disappear while
1577 * we are walking along.
1579 switch (args[0][n])
1581 case '*': /* user */
1583 struct display *nd;
1584 struct acluser *u;
1586 if (!n)
1587 u = D_user;
1588 else
1589 for (u = users; u; u = u->u_next)
1591 debug3("strncmp('%s', '%s', %d)\n", *args, u->u_name, n);
1592 if (!strncmp(*args, u->u_name, n))
1593 break;
1595 debug1("at all displays of user %s\n", u->u_name);
1596 for (display = displays; display; display = nd)
1598 nd = display->d_next;
1599 if (D_forecv == 0)
1600 continue;
1601 flayer = D_forecv->c_layer;
1602 fore = D_fore;
1603 if (D_user != u)
1604 continue;
1605 debug1("AT display %s\n", D_usertty);
1606 DoCommand(args + 1, argl + 1);
1607 if (display)
1608 Msg(0, "command from %s: %s %s",
1609 s, args[1], args[2] ? args[2] : "");
1610 display = NULL;
1611 flayer = 0;
1612 fore = NULL;
1614 break;
1616 case '%': /* display */
1618 struct display *nd;
1620 debug1("at display matching '%s'\n", args[0]);
1621 for (display = displays; display; display = nd)
1623 nd = display->d_next;
1624 if (D_forecv == 0)
1625 continue;
1626 fore = D_fore;
1627 flayer = D_forecv->c_layer;
1628 if (strncmp(args[0], D_usertty, n) &&
1629 (strncmp("/dev/", D_usertty, 5) ||
1630 strncmp(args[0], D_usertty + 5, n)) &&
1631 (strncmp("/dev/tty", D_usertty, 8) ||
1632 strncmp(args[0], D_usertty + 8, n)))
1633 continue;
1634 debug1("AT display %s\n", D_usertty);
1635 DoCommand(args + 1, argl + 1);
1636 if (display)
1637 Msg(0, "command from %s: %s %s",
1638 s, args[1], args[2] ? args[2] : "");
1639 display = NULL;
1640 fore = NULL;
1641 flayer = 0;
1643 break;
1645 case '#': /* window */
1646 n--;
1647 /* FALLTHROUGH */
1648 default:
1650 struct win *nw;
1651 int ch;
1653 n++;
1654 ch = args[0][n];
1655 args[0][n] = '\0';
1656 if (!*args[0] || (i = WindowByNumber(args[0])) < 0)
1658 args[0][n] = ch; /* must restore string in case of bind */
1659 /* try looping over titles */
1660 for (fore = windows; fore; fore = nw)
1662 nw = fore->w_next;
1663 if (strncmp(args[0], fore->w_title, n))
1664 continue;
1665 debug2("AT window %d(%s)\n", fore->w_number, fore->w_title);
1667 * consider this a bug or a feature:
1668 * while looping through windows, we have fore AND
1669 * display context. This will confuse users who try to
1670 * set up loops inside of loops, but often allows to do
1671 * what you mean, even when you adress your context wrong.
1673 i = 0;
1674 /* XXX: other displays? */
1675 if (fore->w_layer.l_cvlist)
1676 display = fore->w_layer.l_cvlist->c_display;
1677 flayer = fore->w_savelayer ? fore->w_savelayer : &fore->w_layer;
1678 DoCommand(args + 1, argl + 1); /* may destroy our display */
1679 if (fore && fore->w_layer.l_cvlist)
1681 display = fore->w_layer.l_cvlist->c_display;
1682 Msg(0, "command from %s: %s %s",
1683 s, args[1], args[2] ? args[2] : "");
1686 display = NULL;
1687 fore = NULL;
1688 if (i < 0)
1689 Msg(0, "%s: at '%s': no such window.\n", rc_name, args[0]);
1690 break;
1692 else if (i < MAXWIN && (fore = wtab[i]))
1694 args[0][n] = ch; /* must restore string in case of bind */
1695 debug2("AT window %d (%s)\n", fore->w_number, fore->w_title);
1696 if (fore->w_layer.l_cvlist)
1697 display = fore->w_layer.l_cvlist->c_display;
1698 flayer = fore->w_savelayer ? fore->w_savelayer : &fore->w_layer;
1699 DoCommand(args + 1, argl + 1);
1700 if (fore && fore->w_layer.l_cvlist)
1702 display = fore->w_layer.l_cvlist->c_display;
1703 Msg(0, "command from %s: %s %s",
1704 s, args[1], args[2] ? args[2] : "");
1706 display = NULL;
1707 fore = NULL;
1709 else
1710 Msg(0, "%s: at [identifier][%%|*|#] command [args]", rc_name);
1711 break;
1714 free(s);
1715 #ifdef MULTIUSER
1716 EffectiveAclUser = NULL;
1717 #endif
1718 break;
1720 #ifdef COPY_PASTE
1721 case RC_READREG:
1722 #ifdef ENCODINGS
1723 i = fore ? fore->w_encoding : display ? display->d_encoding : 0;
1724 if (args[0] && args[1] && !strcmp(args[0], "-e"))
1726 i = FindEncoding(args[1]);
1727 if (i == -1)
1729 Msg(0, "%s: readreg: unknown encoding", rc_name);
1730 break;
1732 args += 2;
1734 #endif
1736 * Without arguments we prompt for a destination register.
1737 * It will receive the copybuffer contents.
1738 * This is not done by RC_PASTE, as we prompt for source
1739 * (not dest) there.
1741 if ((s = *args) == NULL)
1743 Input("Copy to register:", 1, INP_RAW, copy_reg_fn, NULL, 0);
1744 break;
1746 if (*argl != 1)
1748 Msg(0, "%s: copyreg: character, ^x, or (octal) \\032 expected.", rc_name);
1749 break;
1751 ch = args[0][0];
1753 * With two arguments we *really* read register contents from file
1755 if (args[1])
1757 if (args[2])
1759 Msg(0, "%s: readreg: too many arguments", rc_name);
1760 break;
1762 if ((s = ReadFile(args[1], &n)))
1764 struct plop *pp = plop_tab + (int)(unsigned char)ch;
1766 if (pp->buf)
1767 free(pp->buf);
1768 pp->buf = s;
1769 pp->len = n;
1770 #ifdef ENCODINGS
1771 pp->enc = i;
1772 #endif
1775 else
1777 * with one argument we copy the copybuffer into a specified register
1778 * This could be done with RC_PASTE too, but is here to be consistent
1779 * with the zero argument call.
1781 copy_reg_fn(&ch, 0, NULL);
1782 break;
1783 #endif
1784 case RC_REGISTER:
1785 #ifdef ENCODINGS
1786 i = fore ? fore->w_encoding : display ? display->d_encoding : 0;
1787 if (args[0] && args[1] && !strcmp(args[0], "-e"))
1789 i = FindEncoding(args[1]);
1790 if (i == -1)
1792 Msg(0, "%s: register: unknown encoding", rc_name);
1793 break;
1795 args += 2;
1796 argc -= 2;
1798 #endif
1799 if (argc != 2)
1801 Msg(0, "%s: register: illegal number of arguments.", rc_name);
1802 break;
1804 if (*argl != 1)
1806 Msg(0, "%s: register: character, ^x, or (octal) \\032 expected.", rc_name);
1807 break;
1809 ch = args[0][0];
1810 #ifdef COPY_PASTE
1811 if (ch == '.')
1813 if (user->u_plop.buf != NULL)
1814 UserFreeCopyBuffer(user);
1815 if (args[1] && args[1][0])
1817 user->u_plop.buf = SaveStrn(args[1], argl[1]);
1818 user->u_plop.len = argl[1];
1819 #ifdef ENCODINGS
1820 user->u_plop.enc = i;
1821 #endif
1824 else
1825 #endif
1827 struct plop *plp = plop_tab + (int)(unsigned char)ch;
1829 if (plp->buf)
1830 free(plp->buf);
1831 plp->buf = SaveStrn(args[1], argl[1]);
1832 plp->len = argl[1];
1833 #ifdef ENCODINGS
1834 plp->enc = i;
1835 #endif
1837 break;
1838 case RC_PROCESS:
1839 if ((s = *args) == NULL)
1841 Input("Process register:", 1, INP_RAW, process_fn, NULL, 0);
1842 break;
1844 if (*argl != 1)
1846 Msg(0, "%s: process: character, ^x, or (octal) \\032 expected.", rc_name);
1847 break;
1849 ch = args[0][0];
1850 process_fn(&ch, 0, NULL);
1851 break;
1852 case RC_STUFF:
1853 s = *args;
1854 if (!args[0])
1856 Input("Stuff:", 100, INP_COOKED, StuffFin, NULL, 0);
1857 break;
1859 n = *argl;
1860 if (args[1])
1862 if (strcmp(s, "-k"))
1864 Msg(0, "%s: stuff: invalid option %s", rc_name, s);
1865 break;
1867 s = args[1];
1868 for (i = T_CAPS; i < T_OCAPS; i++)
1869 if (strcmp(term[i].tcname, s) == 0)
1870 break;
1871 if (i == T_OCAPS)
1873 Msg(0, "%s: stuff: unknown key '%s'", rc_name, s);
1874 break;
1876 #ifdef MAPKEYS
1877 if (StuffKey(i - T_CAPS) == 0)
1878 break;
1879 #endif
1880 s = display ? D_tcs[i].str : 0;
1881 if (s == 0)
1882 break;
1883 n = strlen(s);
1885 while(n)
1886 LayProcess(&s, &n);
1887 break;
1888 case RC_REDISPLAY:
1889 Activate(-1);
1890 break;
1891 case RC_WINDOWS:
1892 ShowWindows(-1);
1893 break;
1894 case RC_VERSION:
1895 Msg(0, "screen %s", version);
1896 break;
1897 case RC_TIME:
1898 if (*args)
1900 timestring = SaveStr(*args);
1901 break;
1903 Msg(0, "%s", MakeWinMsg(timestring, fore, '%'));
1904 break;
1905 case RC_INFO:
1906 ShowInfo();
1907 break;
1908 case RC_DINFO:
1909 ShowDInfo();
1910 break;
1911 case RC_COMMAND:
1913 struct action *ktabp = ktab;
1914 if (argc == 2 && !strcmp(*args, "-c"))
1916 if ((ktabp = FindKtab(args[1], 0)) == 0)
1918 Msg(0, "Unknown command class '%s'", args[1]);
1919 break;
1922 if (D_ESCseen != ktab || ktabp != ktab)
1924 D_ESCseen = ktabp;
1925 break;
1927 D_ESCseen = 0;
1929 /* FALLTHROUGH */
1930 case RC_OTHER:
1931 if (MoreWindows())
1932 SwitchWindow(display && D_other ? D_other->w_number : NextWindow());
1933 break;
1934 case RC_META:
1935 if (user->u_Esc == -1)
1936 break;
1937 ch = user->u_Esc;
1938 s = &ch;
1939 n = 1;
1940 LayProcess(&s, &n);
1941 break;
1942 case RC_XON:
1943 ch = Ctrl('q');
1944 s = &ch;
1945 n = 1;
1946 LayProcess(&s, &n);
1947 break;
1948 case RC_XOFF:
1949 ch = Ctrl('s');
1950 s = &ch;
1951 n = 1;
1952 LayProcess(&s, &n);
1953 break;
1954 case RC_DEFBREAKTYPE:
1955 case RC_BREAKTYPE:
1957 static char *types[] = { "TIOCSBRK", "TCSBRK", "tcsendbreak", NULL };
1958 extern int breaktype;
1960 if (*args)
1962 if (ParseNum(act, &n))
1963 for (n = 0; n < (int)(sizeof(types)/sizeof(*types)); n++)
1965 for (i = 0; i < 4; i++)
1967 ch = args[0][i];
1968 if (ch >= 'a' && ch <= 'z')
1969 ch -= 'a' - 'A';
1970 if (ch != types[n][i] && (ch + ('a' - 'A')) != types[n][i])
1971 break;
1973 if (i == 4)
1974 break;
1976 if (n < 0 || n >= (int)(sizeof(types)/sizeof(*types)))
1977 Msg(0, "%s invalid, chose one of %s, %s or %s", *args, types[0], types[1], types[2]);
1978 else
1980 breaktype = n;
1981 Msg(0, "breaktype set to (%d) %s", n, types[n]);
1984 else
1985 Msg(0, "breaktype is (%d) %s", breaktype, types[breaktype]);
1987 break;
1988 case RC_POW_BREAK:
1989 case RC_BREAK:
1990 n = 0;
1991 if (*args && ParseNum(act, &n))
1992 break;
1993 SendBreak(fore, n, nr == RC_POW_BREAK);
1994 break;
1995 #ifdef LOCK
1996 case RC_LOCKSCREEN:
1997 Detach(D_LOCK);
1998 break;
1999 #endif
2000 case RC_WIDTH:
2001 case RC_HEIGHT:
2003 int w, h;
2004 int what = 0;
2006 i = 1;
2007 if (*args && !strcmp(*args, "-w"))
2008 what = 1;
2009 else if (*args && !strcmp(*args, "-d"))
2010 what = 2;
2011 if (what)
2012 args++;
2013 if (what == 0 && flayer && !display)
2014 what = 1;
2015 if (what == 1)
2017 if (!flayer)
2019 Msg(0, "%s: %s: window required", rc_name, comms[nr].name);
2020 break;
2022 w = flayer->l_width;
2023 h = flayer->l_height;
2025 else
2027 if (!display)
2029 Msg(0, "%s: %s: display required", rc_name, comms[nr].name);
2030 break;
2032 w = D_width;
2033 h = D_height;
2035 if (*args && args[0][0] == '-')
2037 Msg(0, "%s: %s: unknown option %s", rc_name, comms[nr].name, *args);
2038 break;
2040 if (nr == RC_HEIGHT)
2042 if (!*args)
2044 #define H0height 42
2045 #define H1height 24
2046 if (h == H0height)
2047 h = H1height;
2048 else if (h == H1height)
2049 h = H0height;
2050 else if (h > (H0height + H1height) / 2)
2051 h = H0height;
2052 else
2053 h = H1height;
2055 else
2057 h = atoi(*args);
2058 if (args[1])
2059 w = atoi(args[1]);
2062 else
2064 if (!*args)
2066 if (w == Z0width)
2067 w = Z1width;
2068 else if (w == Z1width)
2069 w = Z0width;
2070 else if (w > (Z0width + Z1width) / 2)
2071 w = Z0width;
2072 else
2073 w = Z1width;
2075 else
2077 w = atoi(*args);
2078 if (args[1])
2079 h = atoi(args[1]);
2082 if (*args && args[1] && args[2])
2084 Msg(0, "%s: %s: too many arguments", rc_name, comms[nr].name);
2085 break;
2087 if (w <= 0)
2089 Msg(0, "Illegal width");
2090 break;
2092 if (h <= 0)
2094 Msg(0, "Illegal height");
2095 break;
2097 if (what == 1)
2099 if (flayer->l_width == w && flayer->l_height == h)
2100 break;
2101 ResizeLayer(flayer, w, h, (struct display *)0);
2102 break;
2104 if (D_width == w && D_height == h)
2105 break;
2106 if (what == 2)
2108 ChangeScreenSize(w, h, 1);
2110 else
2112 if (ResizeDisplay(w, h) == 0)
2114 Activate(D_fore ? D_fore->w_norefresh : 0);
2115 /* autofit */
2116 ResizeLayer(D_forecv->c_layer, D_forecv->c_xe - D_forecv->c_xs + 1, D_forecv->c_ye - D_forecv->c_ys + 1, 0);
2117 break;
2119 if (h == D_height)
2120 Msg(0, "Your termcap does not specify how to change the terminal's width to %d.", w);
2121 else if (w == D_width)
2122 Msg(0, "Your termcap does not specify how to change the terminal's height to %d.", h);
2123 else
2124 Msg(0, "Your termcap does not specify how to change the terminal's resolution to %dx%d.", w, h);
2127 break;
2128 case RC_TITLE:
2129 if (*args == 0)
2130 InputAKA();
2131 else
2132 ChangeAKA(fore, *args, strlen(*args));
2133 break;
2134 case RC_COLON:
2135 Input(":", 100, INP_EVERY, Colonfin, NULL, 0);
2136 if (*args && **args)
2138 s = *args;
2139 n = strlen(s);
2140 LayProcess(&s, &n);
2142 break;
2143 case RC_LASTMSG:
2144 if (D_status_lastmsg)
2145 Msg(0, "%s", D_status_lastmsg);
2146 break;
2147 case RC_SCREEN:
2148 DoScreen("key", args);
2149 break;
2150 case RC_WRAP:
2151 if (ParseSwitch(act, &fore->w_wrap) == 0 && msgok)
2152 Msg(0, "%cwrap", fore->w_wrap ? '+' : '-');
2153 break;
2154 case RC_FLOW:
2155 if (*args)
2157 if (args[0][0] == 'a')
2159 fore->w_flow = (fore->w_flow & FLOW_AUTO) ? FLOW_AUTOFLAG |FLOW_AUTO|FLOW_NOW : FLOW_AUTOFLAG;
2161 else
2163 if (ParseOnOff(act, &n))
2164 break;
2165 fore->w_flow = (fore->w_flow & FLOW_AUTO) | n;
2168 else
2170 if (fore->w_flow & FLOW_AUTOFLAG)
2171 fore->w_flow = (fore->w_flow & FLOW_AUTO) | FLOW_NOW;
2172 else if (fore->w_flow & FLOW_NOW)
2173 fore->w_flow &= ~FLOW_NOW;
2174 else
2175 fore->w_flow = fore->w_flow ? FLOW_AUTOFLAG|FLOW_AUTO|FLOW_NOW : FLOW_AUTOFLAG;
2177 SetFlow(fore->w_flow & FLOW_NOW);
2178 if (msgok)
2179 Msg(0, "%cflow%s", (fore->w_flow & FLOW_NOW) ? '+' : '-',
2180 (fore->w_flow & FLOW_AUTOFLAG) ? "(auto)" : "");
2181 break;
2182 #ifdef MULTIUSER
2183 case RC_DEFWRITELOCK:
2184 if (args[0][0] == 'a')
2185 nwin_default.wlock = WLOCK_AUTO;
2186 else
2188 if (ParseOnOff(act, &n))
2189 break;
2190 nwin_default.wlock = n ? WLOCK_ON : WLOCK_OFF;
2192 break;
2193 case RC_WRITELOCK:
2194 if (*args)
2196 if (args[0][0] == 'a')
2198 fore->w_wlock = WLOCK_AUTO;
2200 else
2202 if (ParseOnOff(act, &n))
2203 break;
2204 fore->w_wlock = n ? WLOCK_ON : WLOCK_OFF;
2207 * user may have permission to change the writelock setting,
2208 * but he may never aquire the lock himself without write permission
2210 if (!AclCheckPermWin(D_user, ACL_WRITE, fore))
2211 fore->w_wlockuser = D_user;
2213 Msg(0, "writelock %s", (fore->w_wlock == WLOCK_AUTO) ? "auto" :
2214 ((fore->w_wlock == WLOCK_OFF) ? "off" : "on"));
2215 break;
2216 #endif
2217 case RC_CLEAR:
2218 ResetAnsiState(fore);
2219 WriteString(fore, "\033[H\033[J", 6);
2220 break;
2221 case RC_RESET:
2222 ResetAnsiState(fore);
2223 #ifdef ZMODEM
2224 if (fore->w_zdisplay)
2225 zmodem_abort(fore, fore->w_zdisplay);
2226 #endif
2227 WriteString(fore, "\033c", 2);
2228 break;
2229 case RC_MONITOR:
2230 n = fore->w_monitor != MON_OFF;
2231 #ifdef MULTIUSER
2232 if (display)
2233 n = n && (ACLBYTE(fore->w_mon_notify, D_user->u_id) & ACLBIT(D_user->u_id));
2234 #endif
2235 if (ParseSwitch(act, &n))
2236 break;
2237 if (n)
2239 #ifdef MULTIUSER
2240 if (display) /* we tell only this user */
2241 ACLBYTE(fore->w_mon_notify, D_user->u_id) |= ACLBIT(D_user->u_id);
2242 else
2243 for (i = 0; i < maxusercount; i++)
2244 ACLBYTE(fore->w_mon_notify, i) |= ACLBIT(i);
2245 #endif
2246 if (fore->w_monitor == MON_OFF)
2247 fore->w_monitor = MON_ON;
2248 Msg(0, "Window %d (%s) is now being monitored for all activity.", fore->w_number, fore->w_title);
2250 else
2252 #ifdef MULTIUSER
2253 if (display) /* we remove only this user */
2254 ACLBYTE(fore->w_mon_notify, D_user->u_id)
2255 &= ~ACLBIT(D_user->u_id);
2256 else
2257 for (i = 0; i < maxusercount; i++)
2258 ACLBYTE(fore->w_mon_notify, i) &= ~ACLBIT(i);
2259 for (i = maxusercount - 1; i >= 0; i--)
2260 if (ACLBYTE(fore->w_mon_notify, i))
2261 break;
2262 if (i < 0)
2263 #endif
2264 fore->w_monitor = MON_OFF;
2265 Msg(0, "Window %d (%s) is no longer being monitored for activity.", fore->w_number, fore->w_title);
2267 break;
2268 #ifdef MULTI
2269 case RC_DISPLAYS:
2270 display_displays();
2271 break;
2272 #endif
2273 case RC_WINDOWLIST:
2274 if (!*args)
2275 display_wlist(0, WLIST_NUM, (struct win *)0);
2276 else if (!strcmp(*args, "string"))
2278 if (args[1])
2280 if (wliststr)
2281 free(wliststr);
2282 wliststr = SaveStr(args[1]);
2284 if (msgok)
2285 Msg(0, "windowlist string is '%s'", wliststr);
2287 else if (!strcmp(*args, "title"))
2289 if (args[1])
2291 if (wlisttit)
2292 free(wlisttit);
2293 wlisttit = SaveStr(args[1]);
2295 if (msgok)
2296 Msg(0, "windowlist title is '%s'", wlisttit);
2298 else
2300 int flag = 0;
2301 int blank = 0;
2302 for (i = 0; i < argc; i++)
2303 if (!args[i])
2304 continue;
2305 else if (!strcmp(args[i], "-m"))
2306 flag |= WLIST_MRU;
2307 else if (!strcmp(args[i], "-b"))
2308 blank = 1;
2309 else if (!strcmp(args[i], "-g"))
2310 flag |= WLIST_NESTED;
2311 else
2313 Msg(0, "usage: windowlist [-b] [-g] [-m] [string [string] | title [title]]");
2314 break;
2316 if (i == argc)
2317 display_wlist(blank, flag, (struct win *)0);
2319 break;
2320 case RC_HELP:
2321 if (argc == 2 && !strcmp(*args, "-c"))
2323 struct action *ktabp;
2324 if ((ktabp = FindKtab(args[1], 0)) == 0)
2326 Msg(0, "Unknown command class '%s'", args[1]);
2327 break;
2329 display_help(args[1], ktabp);
2331 else
2332 display_help((char *)0, ktab);
2333 break;
2334 case RC_LICENSE:
2335 display_copyright();
2336 break;
2337 #ifdef COPY_PASTE
2338 case RC_COPY:
2339 if (flayer->l_layfn != &WinLf)
2341 Msg(0, "Must be on a window layer");
2342 break;
2344 MarkRoutine();
2345 break;
2346 case RC_HISTORY:
2348 static char *pasteargs[] = {".", 0};
2349 static int pasteargl[] = {1};
2351 if (flayer->l_layfn != &WinLf)
2353 Msg(0, "Must be on a window layer");
2354 break;
2356 if (GetHistory() == 0)
2357 break;
2358 if (user->u_plop.buf == NULL)
2359 break;
2360 args = pasteargs;
2361 argl = pasteargl;
2363 /*FALLTHROUGH*/
2364 case RC_PASTE:
2366 char *ss, *dbuf, dch;
2367 int l = 0;
2368 # ifdef ENCODINGS
2369 int enc = -1;
2370 # endif
2373 * without args we prompt for one(!) register to be pasted in the window
2375 if ((s = *args) == NULL)
2377 Input("Paste from register:", 1, INP_RAW, ins_reg_fn, NULL, 0);
2378 break;
2380 if (args[1] == 0 && !fore) /* no window? */
2381 break;
2383 * with two arguments we paste into a destination register
2384 * (no window needed here).
2386 if (args[1] && argl[1] != 1)
2388 Msg(0, "%s: paste destination: character, ^x, or (octal) \\032 expected.",
2389 rc_name);
2390 break;
2392 # ifdef ENCODINGS
2393 else if (fore)
2394 enc = fore->w_encoding;
2395 # endif
2398 * measure length of needed buffer
2400 for (ss = s = *args; (ch = *ss); ss++)
2402 if (ch == '.')
2404 # ifdef ENCODINGS
2405 if (enc == -1)
2406 enc = user->u_plop.enc;
2407 if (enc != user->u_plop.enc)
2408 l += RecodeBuf((unsigned char *)user->u_plop.buf, user->u_plop.len, user->u_plop.enc, enc, (unsigned char *)0);
2409 else
2410 # endif
2411 l += user->u_plop.len;
2413 else
2415 # ifdef ENCODINGS
2416 if (enc == -1)
2417 enc = plop_tab[(int)(unsigned char)ch].enc;
2418 if (enc != plop_tab[(int)(unsigned char)ch].enc)
2419 l += RecodeBuf((unsigned char *)plop_tab[(int)(unsigned char)ch].buf, plop_tab[(int)(unsigned char)ch].len, plop_tab[(int)(unsigned char)ch].enc, enc, (unsigned char *)0);
2420 else
2421 # endif
2422 l += plop_tab[(int)(unsigned char)ch].len;
2425 if (l == 0)
2427 Msg(0, "empty buffer");
2428 break;
2431 * shortcut:
2432 * if there is only one source and the destination is a window, then
2433 * pass a pointer rather than duplicating the buffer.
2435 if (s[1] == 0 && args[1] == 0)
2436 # ifdef ENCODINGS
2437 if (enc == (*s == '.' ? user->u_plop.enc : plop_tab[(int)(unsigned char)*s].enc))
2438 # endif
2440 MakePaster(&fore->w_paster, *s == '.' ? user->u_plop.buf : plop_tab[(int)(unsigned char)*s].buf, l, 0);
2441 break;
2444 * if no shortcut, we construct a buffer
2446 if ((dbuf = (char *)malloc(l)) == 0)
2448 Msg(0, strnomem);
2449 break;
2451 l = 0;
2453 * concatenate all sources into our own buffer, copy buffer is
2454 * special and is skipped if no display exists.
2456 for (ss = s; (ch = *ss); ss++)
2458 struct plop *pp = (ch == '.' ? &user->u_plop : &plop_tab[(int)(unsigned char)ch]);
2459 #ifdef ENCODINGS
2460 if (pp->enc != enc)
2462 l += RecodeBuf((unsigned char *)pp->buf, pp->len, pp->enc, enc, (unsigned char *)dbuf + l);
2463 continue;
2465 #endif
2466 bcopy(pp->buf, dbuf + l, pp->len);
2467 l += pp->len;
2470 * when called with one argument we paste our buffer into the window
2472 if (args[1] == 0)
2474 MakePaster(&fore->w_paster, dbuf, l, 1);
2476 else
2479 * we have two arguments, the second is already in dch.
2480 * use this as destination rather than the window.
2482 dch = args[1][0];
2483 if (dch == '.')
2485 if (user->u_plop.buf != NULL)
2486 UserFreeCopyBuffer(user);
2487 user->u_plop.buf = dbuf;
2488 user->u_plop.len = l;
2489 #ifdef ENCODINGS
2490 user->u_plop.enc = enc;
2491 #endif
2493 else
2495 struct plop *pp = plop_tab + (int)(unsigned char)dch;
2496 if (pp->buf)
2497 free(pp->buf);
2498 pp->buf = dbuf;
2499 pp->len = l;
2500 #ifdef ENCODINGS
2501 pp->enc = enc;
2502 #endif
2505 break;
2507 case RC_WRITEBUF:
2508 if (!user->u_plop.buf)
2510 Msg(0, "empty buffer");
2511 break;
2513 #ifdef ENCODINGS
2515 struct plop oldplop;
2517 oldplop = user->u_plop;
2518 if (args[0] && args[1] && !strcmp(args[0], "-e"))
2520 int enc, l;
2521 char *newbuf;
2523 enc = FindEncoding(args[1]);
2524 if (enc == -1)
2526 Msg(0, "%s: writebuf: unknown encoding", rc_name);
2527 break;
2529 if (enc != oldplop.enc)
2531 l = RecodeBuf((unsigned char *)oldplop.buf, oldplop.len, oldplop.enc, enc, (unsigned char *)0);
2532 newbuf = malloc(l + 1);
2533 if (!newbuf)
2535 Msg(0, strnomem);
2536 break;
2538 user->u_plop.len = RecodeBuf((unsigned char *)oldplop.buf, oldplop.len, oldplop.enc, enc, (unsigned char *)newbuf);
2539 user->u_plop.buf = newbuf;
2540 user->u_plop.enc = enc;
2542 args += 2;
2544 #endif
2545 if (args[0] && args[1])
2546 Msg(0, "%s: writebuf: too many arguments", rc_name);
2547 else
2548 WriteFile(user, args[0], DUMP_EXCHANGE);
2549 #ifdef ENCODINGS
2550 if (user->u_plop.buf != oldplop.buf)
2551 free(user->u_plop.buf);
2552 user->u_plop = oldplop;
2554 #endif
2555 break;
2556 case RC_READBUF:
2557 #ifdef ENCODINGS
2558 i = fore ? fore->w_encoding : display ? display->d_encoding : 0;
2559 if (args[0] && args[1] && !strcmp(args[0], "-e"))
2561 i = FindEncoding(args[1]);
2562 if (i == -1)
2564 Msg(0, "%s: readbuf: unknown encoding", rc_name);
2565 break;
2567 args += 2;
2569 #endif
2570 if (args[0] && args[1])
2572 Msg(0, "%s: readbuf: too many arguments", rc_name);
2573 break;
2575 if ((s = ReadFile(args[0] ? args[0] : BufferFile, &n)))
2577 if (user->u_plop.buf)
2578 UserFreeCopyBuffer(user);
2579 user->u_plop.len = n;
2580 user->u_plop.buf = s;
2581 #ifdef ENCODINGS
2582 user->u_plop.enc = i;
2583 #endif
2585 break;
2586 case RC_REMOVEBUF:
2587 KillBuffers();
2588 break;
2589 case RC_IGNORECASE:
2590 (void)ParseSwitch(act, &search_ic);
2591 if (msgok)
2592 Msg(0, "Will %signore case in searches", search_ic ? "" : "not ");
2593 break;
2594 #endif /* COPY_PASTE */
2595 case RC_ESCAPE:
2596 if (*argl == 0)
2597 SetEscape(user, -1, -1);
2598 else if (*argl == 2)
2599 SetEscape(user, (int)(unsigned char)args[0][0], (int)(unsigned char)args[0][1]);
2600 else
2602 Msg(0, "%s: two characters required after escape.", rc_name);
2603 break;
2605 /* Change defescape if master user. This is because we only
2606 * have one ktab.
2608 if (display && user != users)
2609 break;
2610 /* FALLTHROUGH */
2611 case RC_DEFESCAPE:
2612 if (*argl == 0)
2613 SetEscape(NULL, -1, -1);
2614 else if (*argl == 2)
2615 SetEscape(NULL, (int)(unsigned char)args[0][0], (int)(unsigned char)args[0][1]);
2616 else
2618 Msg(0, "%s: two characters required after defescape.", rc_name);
2619 break;
2621 #ifdef MAPKEYS
2622 CheckEscape();
2623 #endif
2624 break;
2625 case RC_CHDIR:
2626 s = *args ? *args : home;
2627 if (chdir(s) == -1)
2628 Msg(errno, "%s", s);
2629 break;
2630 case RC_SHELL:
2631 case RC_DEFSHELL:
2632 if (ParseSaveStr(act, &ShellProg) == 0)
2633 ShellArgs[0] = ShellProg;
2634 break;
2635 case RC_HARDCOPYDIR:
2636 if (*args)
2637 (void)ParseSaveStr(act, &hardcopydir);
2638 if (msgok)
2639 Msg(0, "hardcopydir is %s\n", hardcopydir && *hardcopydir ? hardcopydir : "<cwd>");
2640 break;
2641 case RC_LOGFILE:
2642 if (*args)
2644 if (args[1] && !(strcmp(*args, "flush")))
2646 log_flush = atoi(args[1]);
2647 if (msgok)
2648 Msg(0, "log flush timeout set to %ds\n", log_flush);
2649 break;
2651 if (ParseSaveStr(act, &screenlogfile) || !msgok)
2652 break;
2654 Msg(0, "logfile is '%s'", screenlogfile);
2655 break;
2656 case RC_LOGTSTAMP:
2657 if (!*args || !strcmp(*args, "on") || !strcmp(*args, "off"))
2659 if (ParseSwitch(act, &logtstamp_on) == 0 && msgok)
2660 Msg(0, "timestamps turned %s", logtstamp_on ? "on" : "off");
2662 else if (!strcmp(*args, "string"))
2664 if (args[1])
2666 if (logtstamp_string)
2667 free(logtstamp_string);
2668 logtstamp_string = SaveStr(args[1]);
2670 if (msgok)
2671 Msg(0, "logfile timestamp is '%s'", logtstamp_string);
2673 else if (!strcmp(*args, "after"))
2675 if (args[1])
2677 logtstamp_after = atoi(args[1]);
2678 if (!msgok)
2679 break;
2681 Msg(0, "timestamp printed after %ds\n", logtstamp_after);
2683 else
2684 Msg(0, "usage: logtstamp [after [n]|string [str]|on|off]");
2685 break;
2686 case RC_SHELLTITLE:
2687 (void)ParseSaveStr(act, &nwin_default.aka);
2688 break;
2689 case RC_TERMCAP:
2690 case RC_TERMCAPINFO:
2691 case RC_TERMINFO:
2692 if (!rc_name || !*rc_name)
2693 Msg(0, "Sorry, too late now. Place that in your .screenrc file.");
2694 break;
2695 case RC_SLEEP:
2696 break; /* Already handled */
2697 case RC_ALIAS:
2698 if (argc == 1)
2699 DelAlias(args[0]);
2700 else
2701 AddAlias(args[0], args[1], args+1, argl+1, argc - 1);
2702 break;
2703 case RC_TERM:
2704 s = NULL;
2705 if (ParseSaveStr(act, &s))
2706 break;
2707 if (strlen(s) >= 20)
2709 Msg(0, "%s: term: argument too long ( < 20)", rc_name);
2710 free(s);
2711 break;
2713 strcpy(screenterm, s);
2714 free(s);
2715 debug1("screenterm set to %s\n", screenterm);
2716 MakeTermcap((display == 0));
2717 debug("new termcap made\n");
2718 break;
2719 case RC_ECHO:
2720 if (!msgok && (!rc_name || strcmp(rc_name, "-X")))
2721 break;
2723 * user typed ^A:echo... well, echo isn't FinishRc's job,
2724 * but as he wanted to test us, we show good will
2726 if (argc > 1 && !strcmp(*args, "-n"))
2728 args++;
2729 argc--;
2731 s = *args;
2732 if (argc > 1 && !strcmp(*args, "-p"))
2734 args++;
2735 argc--;
2736 s = *args;
2737 if (s)
2738 s = MakeWinMsg(s, fore, '%');
2740 if (s)
2741 Msg(0, "%s", s);
2742 else
2743 Msg(0, "%s: 'echo [-n] [-p] \"string\"' expected.", rc_name);
2744 break;
2745 case RC_BELL:
2746 case RC_BELL_MSG:
2747 if (*args == 0)
2749 char buf[256];
2750 AddXChars(buf, sizeof(buf), BellString);
2751 Msg(0, "bell_msg is '%s'", buf);
2752 break;
2754 (void)ParseSaveStr(act, &BellString);
2755 break;
2756 #ifdef COPY_PASTE
2757 case RC_BUFFERFILE:
2758 if (*args == 0)
2759 BufferFile = SaveStr(DEFAULT_BUFFERFILE);
2760 else if (ParseSaveStr(act, &BufferFile))
2761 break;
2762 if (msgok)
2763 Msg(0, "Bufferfile is now '%s'", BufferFile);
2764 break;
2765 #endif
2766 case RC_ACTIVITY:
2767 (void)ParseSaveStr(act, &ActivityString);
2768 break;
2769 #if defined(DETACH) && defined(POW_DETACH)
2770 case RC_POW_DETACH_MSG:
2771 if (*args == 0)
2773 char buf[256];
2774 AddXChars(buf, sizeof(buf), PowDetachString);
2775 Msg(0, "pow_detach_msg is '%s'", buf);
2776 break;
2778 (void)ParseSaveStr(act, &PowDetachString);
2779 break;
2780 #endif
2781 #if defined(UTMPOK) && defined(LOGOUTOK)
2782 case RC_LOGIN:
2783 n = fore->w_slot != (slot_t)-1;
2784 if (*args && !strcmp(*args, "always"))
2786 fore->w_lflag = 3;
2787 if (!displays && n)
2788 SlotToggle(n);
2789 break;
2791 if (*args && !strcmp(*args, "attached"))
2793 fore->w_lflag = 1;
2794 if (!displays && n)
2795 SlotToggle(0);
2796 break;
2798 if (ParseSwitch(act, &n) == 0)
2799 SlotToggle(n);
2800 break;
2801 case RC_DEFLOGIN:
2802 if (!strcmp(*args, "always"))
2803 nwin_default.lflag |= 2;
2804 else if (!strcmp(*args, "attached"))
2805 nwin_default.lflag &= ~2;
2806 else
2807 (void)ParseOnOff(act, &nwin_default.lflag);
2808 break;
2809 #endif
2810 case RC_DEFFLOW:
2811 if (args[0] && args[1] && args[1][0] == 'i')
2813 iflag = 1;
2814 for (display = displays; display; display = display->d_next)
2816 if (!D_flow)
2817 continue;
2818 #if defined(TERMIO) || defined(POSIX)
2819 D_NewMode.tio.c_cc[VINTR] = D_OldMode.tio.c_cc[VINTR];
2820 D_NewMode.tio.c_lflag |= ISIG;
2821 #else /* TERMIO || POSIX */
2822 D_NewMode.m_tchars.t_intrc = D_OldMode.m_tchars.t_intrc;
2823 #endif /* TERMIO || POSIX */
2824 SetTTY(D_userfd, &D_NewMode);
2827 if (args[0] && args[0][0] == 'a')
2828 nwin_default.flowflag = FLOW_AUTOFLAG;
2829 else
2830 (void)ParseOnOff(act, &nwin_default.flowflag);
2831 break;
2832 case RC_DEFWRAP:
2833 (void)ParseOnOff(act, &nwin_default.wrap);
2834 break;
2835 case RC_DEFC1:
2836 (void)ParseOnOff(act, &nwin_default.c1);
2837 break;
2838 #ifdef COLOR
2839 case RC_DEFBCE:
2840 (void)ParseOnOff(act, &nwin_default.bce);
2841 break;
2842 #endif
2843 case RC_DEFGR:
2844 (void)ParseOnOff(act, &nwin_default.gr);
2845 break;
2846 case RC_DEFMONITOR:
2847 if (ParseOnOff(act, &n) == 0)
2848 nwin_default.monitor = (n == 0) ? MON_OFF : MON_ON;
2849 break;
2850 case RC_DEFSILENCE:
2851 if (ParseOnOff(act, &n) == 0)
2852 nwin_default.silence = (n == 0) ? SILENCE_OFF : SILENCE_ON;
2853 break;
2854 case RC_VERBOSE:
2855 if (!*args)
2856 Msg(0, "W%s echo command when creating windows.",
2857 VerboseCreate ? "ill" : "on't");
2858 else if (ParseOnOff(act, &n) == 0)
2859 VerboseCreate = n;
2860 break;
2861 case RC_HARDSTATUS:
2862 if (display)
2864 Msg(0, "%s", ""); /* wait till mintime (keep gcc quiet) */
2865 RemoveStatus();
2867 if (args[0] && strcmp(args[0], "on") && strcmp(args[0], "off"))
2869 struct display *olddisplay = display;
2870 int old_use, new_use = -1;
2872 s = args[0];
2873 if (!strncmp(s, "always", 6))
2874 s += 6;
2875 if (!strcmp(s, "lastline"))
2876 new_use = HSTATUS_LASTLINE;
2877 else if (!strcmp(s, "ignore"))
2878 new_use = HSTATUS_IGNORE;
2879 else if (!strcmp(s, "message"))
2880 new_use = HSTATUS_MESSAGE;
2881 else if (!strcmp(args[0], "string"))
2883 if (!args[1])
2885 char buf[256];
2886 AddXChars(buf, sizeof(buf), hstatusstring);
2887 Msg(0, "hardstatus string is '%s'", buf);
2888 break;
2891 else
2893 Msg(0, "%s: usage: hardstatus [always]lastline|ignore|message|string [string]", rc_name);
2894 break;
2896 if (new_use != -1)
2898 hardstatusemu = new_use | (s == args[0] ? 0 : HSTATUS_ALWAYS);
2899 for (display = displays; display; display = display->d_next)
2901 RemoveStatus();
2902 new_use = hardstatusemu & ~HSTATUS_ALWAYS;
2903 if (D_HS && s == args[0])
2904 new_use = HSTATUS_HS;
2905 ShowHStatus((char *)0);
2906 old_use = D_has_hstatus;
2907 D_has_hstatus = new_use;
2908 if ((new_use == HSTATUS_LASTLINE && old_use != HSTATUS_LASTLINE) || (new_use != HSTATUS_LASTLINE && old_use == HSTATUS_LASTLINE))
2909 ChangeScreenSize(D_width, D_height, 1);
2910 RefreshHStatus();
2913 if (args[1])
2915 if (hstatusstring)
2916 free(hstatusstring);
2917 hstatusstring = SaveStr(args[1]);
2918 for (display = displays; display; display = display->d_next)
2919 RefreshHStatus();
2921 display = olddisplay;
2922 break;
2924 (void)ParseSwitch(act, &use_hardstatus);
2925 if (msgok)
2926 Msg(0, "messages displayed on %s", use_hardstatus ? "hardstatus line" : "window");
2927 break;
2928 case RC_CAPTION:
2929 if (strcmp(args[0], "always") == 0 || strcmp(args[0], "splitonly") == 0)
2931 struct display *olddisplay = display;
2933 captionalways = args[0][0] == 'a';
2934 for (display = displays; display; display = display->d_next)
2935 ChangeScreenSize(D_width, D_height, 1);
2936 display = olddisplay;
2938 else if (strcmp(args[0], "string") == 0)
2940 if (!args[1])
2942 char buf[256];
2943 AddXChars(buf, sizeof(buf), captionstring);
2944 Msg(0, "caption string is '%s'", buf);
2945 break;
2948 else
2950 Msg(0, "%s: usage: caption always|splitonly|string <string>", rc_name);
2951 break;
2953 if (!args[1])
2954 break;
2955 if (captionstring)
2956 free(captionstring);
2957 captionstring = SaveStr(args[1]);
2958 RedisplayDisplays(0);
2959 break;
2960 case RC_CONSOLE:
2961 n = (console_window != 0);
2962 if (ParseSwitch(act, &n))
2963 break;
2964 if (TtyGrabConsole(fore->w_ptyfd, n, rc_name))
2965 break;
2966 if (n == 0)
2967 Msg(0, "%s: releasing console %s", rc_name, HostName);
2968 else if (console_window)
2969 Msg(0, "%s: stealing console %s from window %d (%s)", rc_name,
2970 HostName, console_window->w_number, console_window->w_title);
2971 else
2972 Msg(0, "%s: grabbing console %s", rc_name, HostName);
2973 console_window = n ? fore : 0;
2974 break;
2975 case RC_ALLPARTIAL:
2976 if (ParseOnOff(act, &all_norefresh))
2977 break;
2978 if (!all_norefresh && fore)
2979 Activate(-1);
2980 if (msgok)
2981 Msg(0, all_norefresh ? "No refresh on window change!\n" :
2982 "Window specific refresh\n");
2983 break;
2984 case RC_PARTIAL:
2985 (void)ParseSwitch(act, &n);
2986 fore->w_norefresh = n;
2987 break;
2988 case RC_VBELL:
2989 if (ParseSwitch(act, &visual_bell) || !msgok)
2990 break;
2991 if (visual_bell == 0)
2992 Msg(0, "switched to audible bell.");
2993 else
2994 Msg(0, "switched to visual bell.");
2995 break;
2996 case RC_VBELLWAIT:
2997 if (ParseNum1000(act, &VBellWait) == 0 && msgok)
2998 Msg(0, "vbellwait set to %.10g seconds", VBellWait/1000.);
2999 break;
3000 case RC_MSGWAIT:
3001 if (ParseNum1000(act, &MsgWait) == 0 && msgok)
3002 Msg(0, "msgwait set to %.10g seconds", MsgWait/1000.);
3003 break;
3004 case RC_MSGMINWAIT:
3005 if (ParseNum1000(act, &MsgMinWait) == 0 && msgok)
3006 Msg(0, "msgminwait set to %.10g seconds", MsgMinWait/1000.);
3007 break;
3008 case RC_SILENCEWAIT:
3009 if (ParseNum(act, &SilenceWait))
3010 break;
3011 if (SilenceWait < 1)
3012 SilenceWait = 1;
3013 for (p = windows; p; p = p->w_next)
3014 p->w_silencewait = SilenceWait;
3015 if (msgok)
3016 Msg(0, "silencewait set to %d seconds", SilenceWait);
3017 break;
3018 case RC_NUMBER:
3019 if (*args == 0)
3020 Msg(0, "This is window %d (%s).\n", fore->w_number, fore->w_title);
3021 else
3023 int old = fore->w_number;
3024 int rel = 0, parse;
3025 if (args[0][0] == '+')
3026 rel = 1;
3027 else if (args[0][0] == '-')
3028 rel = -1;
3029 if (rel)
3030 ++act->args[0];
3031 parse = ParseNum(act, &n);
3032 if (rel)
3033 --act->args[0];
3034 if (parse)
3035 break;
3036 if (rel > 0)
3037 n += old;
3038 else if (rel < 0)
3039 n = old - n;
3040 ChangeWinNum(old, n);
3042 break;
3043 case RC_SILENCE:
3044 n = fore->w_silence != 0;
3045 i = fore->w_silencewait;
3046 if (args[0] && (args[0][0] == '-' || (args[0][0] >= '0' && args[0][0] <= '9')))
3048 if (ParseNum(act, &i))
3049 break;
3050 n = i > 0;
3052 else if (ParseSwitch(act, &n))
3053 break;
3054 if (n)
3056 #ifdef MULTIUSER
3057 if (display) /* we tell only this user */
3058 ACLBYTE(fore->w_lio_notify, D_user->u_id) |= ACLBIT(D_user->u_id);
3059 else
3060 for (n = 0; n < maxusercount; n++)
3061 ACLBYTE(fore->w_lio_notify, n) |= ACLBIT(n);
3062 #endif
3063 fore->w_silencewait = i;
3064 fore->w_silence = SILENCE_ON;
3065 SetTimeout(&fore->w_silenceev, fore->w_silencewait * 1000);
3066 evenq(&fore->w_silenceev);
3068 if (!msgok)
3069 break;
3070 Msg(0, "The window is now being monitored for %d sec. silence.", fore->w_silencewait);
3072 else
3074 #ifdef MULTIUSER
3075 if (display) /* we remove only this user */
3076 ACLBYTE(fore->w_lio_notify, D_user->u_id)
3077 &= ~ACLBIT(D_user->u_id);
3078 else
3079 for (n = 0; n < maxusercount; n++)
3080 ACLBYTE(fore->w_lio_notify, n) &= ~ACLBIT(n);
3081 for (i = maxusercount - 1; i >= 0; i--)
3082 if (ACLBYTE(fore->w_lio_notify, i))
3083 break;
3084 if (i < 0)
3085 #endif
3087 fore->w_silence = SILENCE_OFF;
3088 evdeq(&fore->w_silenceev);
3090 if (!msgok)
3091 break;
3092 Msg(0, "The window is no longer being monitored for silence.");
3094 break;
3095 #ifdef COPY_PASTE
3096 case RC_DEFSCROLLBACK:
3097 (void)ParseNum(act, &nwin_default.histheight);
3098 break;
3099 case RC_SCROLLBACK:
3100 if (flayer->l_layfn == &MarkLf)
3102 Msg(0, "Cannot resize scrollback buffer in copy/scrollback mode.");
3103 break;
3105 (void)ParseNum(act, &n);
3106 ChangeWindowSize(fore, fore->w_width, fore->w_height, n);
3107 if (msgok)
3108 Msg(0, "scrollback set to %d", fore->w_histheight);
3109 break;
3110 #endif
3111 case RC_SESSIONNAME:
3112 if (*args == 0)
3113 Msg(0, "This session is named '%s'\n", SockName);
3114 else
3116 char buf[MAXPATHLEN];
3118 s = 0;
3119 if (ParseSaveStr(act, &s))
3120 break;
3121 if (!*s || strlen(s) + (SockName - SockPath) > MAXPATHLEN - 13 || index(s, '/'))
3123 Msg(0, "%s: bad session name '%s'\n", rc_name, s);
3124 free(s);
3125 break;
3127 strncpy(buf, SockPath, SockName - SockPath);
3128 sprintf(buf + (SockName - SockPath), "%d.%s", (int)getpid(), s);
3129 free(s);
3130 if ((access(buf, F_OK) == 0) || (errno != ENOENT))
3132 Msg(0, "%s: inappropriate path: '%s'.", rc_name, buf);
3133 break;
3135 if (rename(SockPath, buf))
3137 Msg(errno, "%s: failed to rename(%s, %s)", rc_name, SockPath, buf);
3138 break;
3140 debug2("rename(%s, %s) done\n", SockPath, buf);
3141 strcpy(SockPath, buf);
3142 MakeNewEnv();
3143 WindowChanged((struct win *)0, 'S');
3145 break;
3146 case RC_SETENV:
3147 if (!args[0] || !args[1])
3149 debug1("RC_SETENV arguments missing: %s\n", args[0] ? args[0] : "");
3150 InputSetenv(args[0]);
3152 else
3154 xsetenv(args[0], args[1]);
3155 MakeNewEnv();
3157 break;
3158 case RC_UNSETENV:
3159 unsetenv(*args);
3160 MakeNewEnv();
3161 break;
3162 #ifdef COPY_PASTE
3163 case RC_DEFSLOWPASTE:
3164 (void)ParseNum(act, &nwin_default.slow);
3165 break;
3166 case RC_SLOWPASTE:
3167 if (*args == 0)
3168 Msg(0, fore->w_slowpaste ?
3169 "Slowpaste in window %d is %d milliseconds." :
3170 "Slowpaste in window %d is unset.",
3171 fore->w_number, fore->w_slowpaste);
3172 else if (ParseNum(act, &fore->w_slowpaste) == 0 && msgok)
3173 Msg(0, fore->w_slowpaste ?
3174 "Slowpaste in window %d set to %d milliseconds." :
3175 "Slowpaste in window %d now unset.",
3176 fore->w_number, fore->w_slowpaste);
3177 break;
3178 case RC_MARKKEYS:
3179 if (CompileKeys(*args, *argl, mark_key_tab))
3181 Msg(0, "%s: markkeys: syntax error.", rc_name);
3182 break;
3184 debug1("markkeys %s\n", *args);
3185 break;
3186 # ifdef FONT
3187 case RC_PASTEFONT:
3188 if (ParseSwitch(act, &pastefont) == 0 && msgok)
3189 Msg(0, "Will %spaste font settings", pastefont ? "" : "not ");
3190 break;
3191 # endif
3192 case RC_CRLF:
3193 (void)ParseSwitch(act, &join_with_cr);
3194 break;
3195 case RC_COMPACTHIST:
3196 if (ParseSwitch(act, &compacthist) == 0 && msgok)
3197 Msg(0, "%scompacting history lines", compacthist ? "" : "not ");
3198 break;
3199 #endif
3200 #ifdef NETHACK
3201 case RC_NETHACK:
3202 (void)ParseOnOff(act, &nethackflag);
3203 break;
3204 #endif
3205 case RC_HARDCOPY_APPEND:
3206 (void)ParseOnOff(act, &hardcopy_append);
3207 break;
3208 case RC_VBELL_MSG:
3209 if (*args == 0)
3211 char buf[256];
3212 AddXChars(buf, sizeof(buf), VisualBellString);
3213 Msg(0, "vbell_msg is '%s'", buf);
3214 break;
3216 (void)ParseSaveStr(act, &VisualBellString);
3217 debug1(" new vbellstr '%s'\n", VisualBellString);
3218 break;
3219 case RC_DEFMODE:
3220 if (ParseBase(act, *args, &n, 8, "octal"))
3221 break;
3222 if (n < 0 || n > 0777)
3224 Msg(0, "%s: mode: Invalid tty mode %o", rc_name, n);
3225 break;
3227 TtyMode = n;
3228 if (msgok)
3229 Msg(0, "Ttymode set to %03o", TtyMode);
3230 break;
3231 case RC_AUTODETACH:
3232 (void)ParseOnOff(act, &auto_detach);
3233 break;
3234 case RC_STARTUP_MESSAGE:
3235 (void)ParseOnOff(act, &default_startup);
3236 break;
3237 #ifdef PASSWORD
3238 case RC_PASSWORD:
3239 if (*args)
3241 n = (*user->u_password) ? 1 : 0;
3242 if (user->u_password != NullStr) free((char *)user->u_password);
3243 user->u_password = SaveStr(*args);
3244 if (!strcmp(user->u_password, "none"))
3246 if (n)
3247 Msg(0, "Password checking disabled");
3248 free(user->u_password);
3249 user->u_password = NullStr;
3252 else
3254 if (!fore)
3256 Msg(0, "%s: password: window required", rc_name);
3257 break;
3259 Input("New screen password:", 100, INP_NOECHO, pass1, display ? (char *)D_user : (char *)users, 0);
3261 break;
3262 #endif /* PASSWORD */
3263 case RC_BIND:
3265 struct action *ktabp = ktab;
3266 int kflag = 0;
3268 for (;;)
3270 if (argc > 2 && !strcmp(*args, "-c"))
3272 ktabp = FindKtab(args[1], 1);
3273 if (ktabp == 0)
3274 break;
3275 args += 2;
3276 argl += 2;
3277 argc -= 2;
3279 else if (argc > 1 && !strcmp(*args, "-k"))
3281 kflag = 1;
3282 args++;
3283 argl++;
3284 argc--;
3286 else
3287 break;
3289 #ifdef MAPKEYS
3290 if (kflag)
3292 for (n = 0; n < KMAP_KEYS; n++)
3293 if (strcmp(term[n + T_CAPS].tcname, *args) == 0)
3294 break;
3295 if (n == KMAP_KEYS)
3297 Msg(0, "%s: bind: unknown key '%s'", rc_name, *args);
3298 break;
3300 n += 256;
3302 else
3303 #endif
3304 if (*argl != 1)
3306 Msg(0, "%s: bind: character, ^x, or (octal) \\032 expected.", rc_name);
3307 break;
3309 else
3310 n = (unsigned char)args[0][0];
3312 if (args[1])
3314 if ((i = FindCommnr(args[1])) == RC_ILLEGAL)
3316 struct alias *alias = FindAlias(args[1]);
3317 if (!alias)
3319 Msg(0, "%s: bind: unknown command or alias '%s'", rc_name, args[1]);
3320 break;
3322 i = alias->nr;
3324 if (i <= RC_LAST && CheckArgNum(i, args + 2) < 0)
3325 break;
3326 ClearAction(&ktabp[n]);
3327 SaveAction(ktabp + n, i, args + 2, argl + 2);
3329 else
3330 ClearAction(&ktabp[n]);
3332 break;
3333 #ifdef MAPKEYS
3334 case RC_BINDKEY:
3336 struct action *newact;
3337 int newnr, fl = 0, kf = 0, af = 0, df = 0, mf = 0;
3338 struct display *odisp = display;
3339 int used = 0;
3340 struct kmap_ext *kme;
3342 for (; *args && **args == '-'; args++, argl++)
3344 if (strcmp(*args, "-t") == 0)
3345 fl = KMAP_NOTIMEOUT;
3346 else if (strcmp(*args, "-k") == 0)
3347 kf = 1;
3348 else if (strcmp(*args, "-a") == 0)
3349 af = 1;
3350 else if (strcmp(*args, "-d") == 0)
3351 df = 1;
3352 else if (strcmp(*args, "-m") == 0)
3353 mf = 1;
3354 else if (strcmp(*args, "--") == 0)
3356 args++;
3357 argl++;
3358 break;
3360 else
3362 Msg(0, "%s: bindkey: invalid option %s", rc_name, *args);
3363 return;
3366 if (df && mf)
3368 Msg(0, "%s: bindkey: -d does not work with -m", rc_name);
3369 break;
3371 if (*args == 0)
3373 if (mf)
3374 display_bindkey("Edit mode", mmtab);
3375 else if (df)
3376 display_bindkey("Default", dmtab);
3377 else
3378 display_bindkey("User", umtab);
3379 break;
3381 if (kf == 0)
3383 if (af)
3385 Msg(0, "%s: bindkey: -a only works with -k", rc_name);
3386 break;
3388 if (*argl == 0)
3390 Msg(0, "%s: bindkey: empty string makes no sense", rc_name);
3391 break;
3393 for (i = 0, kme = kmap_exts; i < kmap_extn; i++, kme++)
3394 if (kme->str == 0)
3396 if (args[1])
3397 break;
3399 else
3400 if (*argl == (kme->fl & ~KMAP_NOTIMEOUT) && bcmp(kme->str, *args, *argl) == 0)
3401 break;
3402 if (i == kmap_extn)
3404 if (!args[1])
3406 Msg(0, "%s: bindkey: keybinding not found", rc_name);
3407 break;
3409 kmap_extn += 8;
3410 kmap_exts = (struct kmap_ext *)xrealloc((char *)kmap_exts, kmap_extn * sizeof(*kmap_exts));
3411 kme = kmap_exts + i;
3412 bzero((char *)kme, 8 * sizeof(*kmap_exts));
3413 for (; i < kmap_extn; i++, kme++)
3415 kme->str = 0;
3416 kme->dm.nr = kme->mm.nr = kme->um.nr = RC_ILLEGAL;
3417 kme->dm.args = kme->mm.args = kme->um.args = noargs;
3419 i -= 8;
3420 kme -= 8;
3422 if (df == 0 && kme->dm.nr != RC_ILLEGAL)
3423 used = 1;
3424 if (mf == 0 && kme->mm.nr != RC_ILLEGAL)
3425 used = 1;
3426 if ((df || mf) && kme->um.nr != RC_ILLEGAL)
3427 used = 1;
3428 i += KMAP_KEYS + KMAP_AKEYS;
3429 newact = df ? &kme->dm : mf ? &kme->mm : &kme->um;
3431 else
3433 for (i = T_CAPS; i < T_OCAPS; i++)
3434 if (strcmp(term[i].tcname, *args) == 0)
3435 break;
3436 if (i == T_OCAPS)
3438 Msg(0, "%s: bindkey: unknown key '%s'", rc_name, *args);
3439 break;
3441 if (af && i >= T_CURSOR && i < T_OCAPS)
3442 i -= T_CURSOR - KMAP_KEYS;
3443 else
3444 i -= T_CAPS;
3445 newact = df ? &dmtab[i] : mf ? &mmtab[i] : &umtab[i];
3447 if (args[1])
3449 if ((newnr = FindCommnr(args[1])) == RC_ILLEGAL)
3451 struct alias *alias = FindAlias(args[1]);
3452 if (!alias)
3454 Msg(0, "%s: bindkey: unknown command or alias '%s'", rc_name, args[1]);
3455 break;
3457 newnr = alias->nr;
3459 if (newnr <= RC_LAST && CheckArgNum(newnr, args + 2) < 0)
3460 break;
3461 ClearAction(newact);
3462 SaveAction(newact, newnr, args + 2, argl + 2);
3463 if (kf == 0 && args[1])
3465 if (kme->str)
3466 free(kme->str);
3467 kme->str = SaveStrn(*args, *argl);
3468 kme->fl = fl | *argl;
3471 else
3472 ClearAction(newact);
3473 for (display = displays; display; display = display->d_next)
3474 remap(i, args[1] ? 1 : 0);
3475 if (kf == 0 && !args[1])
3477 if (!used && kme->str)
3479 free(kme->str);
3480 kme->str = 0;
3481 kme->fl = 0;
3484 display = odisp;
3486 break;
3487 case RC_MAPTIMEOUT:
3488 if (*args)
3490 if (ParseNum(act, &n))
3491 break;
3492 if (n < 0)
3494 Msg(0, "%s: maptimeout: illegal time %d", rc_name, n);
3495 break;
3497 maptimeout = n;
3499 if (*args == 0 || msgok)
3500 Msg(0, "maptimeout is %dms", maptimeout);
3501 break;
3502 case RC_MAPNOTNEXT:
3503 D_dontmap = 1;
3504 break;
3505 case RC_MAPDEFAULT:
3506 D_mapdefault = 1;
3507 break;
3508 #endif
3509 #ifdef MULTIUSER
3510 case RC_ACLCHG:
3511 case RC_ACLADD:
3512 case RC_ADDACL:
3513 case RC_CHACL:
3514 UsersAcl(NULL, argc, args);
3515 break;
3516 case RC_ACLDEL:
3517 if (UserDel(args[0], NULL))
3518 break;
3519 if (msgok)
3520 Msg(0, "%s removed from acl database", args[0]);
3521 break;
3522 case RC_ACLGRP:
3524 * modify a user to gain or lose rights granted to a group.
3525 * This group is actually a normal user whose rights were defined
3526 * with chacl in the usual way.
3528 if (args[1])
3530 if (strcmp(args[1], "none")) /* link a user to another user */
3532 if (AclLinkUser(args[0], args[1]))
3533 break;
3534 if (msgok)
3535 Msg(0, "User %s joined acl-group %s", args[0], args[1]);
3537 else /* remove all groups from user */
3539 struct acluser *u;
3540 struct aclusergroup *g;
3542 if (!(u = *FindUserPtr(args[0])))
3543 break;
3544 while ((g = u->u_group))
3546 u->u_group = g->next;
3547 free((char *)g);
3551 else /* show all groups of user */
3553 char buf[256], *p = buf;
3554 int ngroups = 0;
3555 struct acluser *u;
3556 struct aclusergroup *g;
3558 if (!(u = *FindUserPtr(args[0])))
3560 if (msgok)
3561 Msg(0, "User %s does not exist.", args[0]);
3562 break;
3564 g = u->u_group;
3565 while (g)
3567 ngroups++;
3568 sprintf(p, "%s ", g->u->u_name);
3569 p += strlen(p);
3570 if (p > buf+200)
3571 break;
3572 g = g->next;
3574 if (ngroups)
3575 *(--p) = '\0';
3576 Msg(0, "%s's group%s: %s.", args[0], (ngroups == 1) ? "" : "s",
3577 (ngroups == 0) ? "none" : buf);
3579 break;
3580 case RC_ACLUMASK:
3581 case RC_UMASK:
3582 while ((s = *args++))
3584 char *err = 0;
3586 if (AclUmask(display ? D_user : users, s, &err))
3587 Msg(0, "umask: %s\n", err);
3589 break;
3590 case RC_MULTIUSER:
3591 if (ParseOnOff(act, &n))
3592 break;
3593 multi = n ? "" : 0;
3594 chsock();
3595 if (msgok)
3596 Msg(0, "Multiuser mode %s", multi ? "enabled" : "disabled");
3597 break;
3598 #endif /* MULTIUSER */
3599 #ifdef PSEUDOS
3600 case RC_EXEC:
3601 winexec(args);
3602 break;
3603 #endif
3604 #ifdef MULTI
3605 case RC_NONBLOCK:
3606 i = D_nonblock >= 0;
3607 if (*args && ((args[0][0] >= '0' && args[0][0] <= '9') || args[0][0] == '.'))
3609 if (ParseNum1000(act, &i))
3610 break;
3612 else if (!ParseSwitch(act, &i))
3613 i = i == 0 ? -1 : 1000;
3614 else
3615 break;
3616 if (msgok && i == -1)
3617 Msg(0, "display set to blocking mode");
3618 else if (msgok && i == 0)
3619 Msg(0, "display set to nonblocking mode, no timeout");
3620 else if (msgok)
3621 Msg(0, "display set to nonblocking mode, %.10gs timeout", i/1000.);
3622 D_nonblock = i;
3623 if (D_nonblock <= 0)
3624 evdeq(&D_blockedev);
3625 break;
3626 case RC_DEFNONBLOCK:
3627 if (*args && ((args[0][0] >= '0' && args[0][0] <= '9') || args[0][0] == '.'))
3629 if (ParseNum1000(act, &defnonblock))
3630 break;