Get rid of compile-time warnings.
[screen-lua.git] / src / process.c
blob128b0e25ebb8d09462913475feafba992a786c6b
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 "config.h"
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <signal.h>
34 #include <fcntl.h>
35 #if !defined(sun) && !defined(B43) && !defined(ISC) && !defined(pyr) && !defined(_CX_UX)
36 # include <time.h>
37 #endif
38 #include <sys/time.h>
39 #ifndef sun
40 #include <sys/ioctl.h>
41 #endif
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"
52 #include "layout.h"
53 #include "viewport.h"
55 extern struct comm comms[];
56 extern char *rc_name;
57 extern char *RcFileName, *home;
58 extern char *BellString, *ActivityString, *ShellProg, *ShellArgs[];
59 extern char *hstatusstring, *captionstring, *timestring;
60 extern char *wliststr, *wlisttit;
61 extern int captionalways;
62 extern char *hardcopydir, *screenlogfile, *logtstamp_string;
63 extern int log_flush, logtstamp_on, logtstamp_after;
64 extern char *VisualBellString;
65 extern int VBellWait, MsgWait, MsgMinWait, SilenceWait;
66 extern char SockPath[], *SockName;
67 extern int TtyMode, auto_detach, use_altscreen;
68 extern int iflag, maxwin;
69 extern int focusminwidth, focusminheight;
70 extern int use_hardstatus, visual_bell;
71 #ifdef COLOR
72 extern int attr2color[][4];
73 extern int nattr2color;
74 #endif
75 extern int hardstatusemu;
76 extern char *printcmd;
77 extern int default_startup;
78 extern int defobuflimit;
79 extern int defnonblock;
80 extern int defmousetrack;
81 extern int ZombieKey_destroy;
82 extern int ZombieKey_resurrect;
83 extern int ZombieKey_onerror;
84 #ifdef AUTO_NUKE
85 extern int defautonuke;
86 #endif
87 extern int separate_sids;
88 extern struct NewWindow nwin_default, nwin_undef;
89 #ifdef COPY_PASTE
90 extern int join_with_cr;
91 extern int compacthist;
92 extern int search_ic;
93 # ifdef FONT
94 extern int pastefont;
95 # endif
96 extern unsigned char mark_key_tab[];
97 extern char *BufferFile;
98 #endif
99 #ifdef POW_DETACH
100 extern char *BufferFile, *PowDetachString;
101 #endif
102 #ifdef MULTIUSER
103 extern struct acluser *EffectiveAclUser; /* acl.c */
104 #endif
105 extern struct term term[]; /* terminal capabilities */
106 #ifdef MAPKEYS
107 extern char *kmapdef[];
108 extern char *kmapadef[];
109 extern char *kmapmdef[];
110 #endif
111 extern struct mchar mchar_so, mchar_null;
112 extern int renditions[];
113 extern int VerboseCreate;
114 #ifdef UTF8
115 extern char *screenencodings;
116 #endif
117 #ifdef DW_CHARS
118 extern int cjkwidth;
119 #endif
121 static int CheckArgNum __P((int, char **));
122 static void ClearAction __P((struct action *));
123 static void SaveAction __P((struct action *, int, char **, int *));
124 static int NextWindow __P((void));
125 static int PreviousWindow __P((void));
126 static int MoreWindows __P((void));
127 static void LogToggle __P((int));
128 static void ShowInfo __P((void));
129 static void ShowDInfo __P((void));
130 static struct win *WindowByName __P((char *));
131 static int WindowByNumber __P((char *));
132 static int ParseOnOff __P((struct action *, int *));
133 static int ParseWinNum __P((struct action *, int *));
134 static int ParseBase __P((struct action *, char *, int *, int, char *));
135 static int ParseNum1000 __P((struct action *, int *));
136 static char **SaveArgs __P((char **));
137 static int IsNum __P((char *, int));
138 static void Colonfin __P((char *, int, char *));
139 static void InputSelect __P((void));
140 static void InputSetenv __P((char *));
141 static void InputAKA __P((void));
142 #ifdef MULTIUSER
143 static int InputSu __P((struct win *, struct acluser **, char *));
144 static void su_fin __P((char *, int, char *));
145 #endif
146 static void AKAfin __P((char *, int, char *));
147 #ifdef COPY_PASTE
148 static void copy_reg_fn __P((char *, int, char *));
149 static void ins_reg_fn __P((char *, int, char *));
150 #endif
151 static void process_fn __P((char *, int, char *));
152 #ifdef PASSWORD
153 static void pass1 __P((char *, int, char *));
154 static void pass2 __P((char *, int, char *));
155 #endif
156 #ifdef POW_DETACH
157 static void pow_detach_fn __P((char *, int, char *));
158 #endif
159 static void digraph_fn __P((char *, int, char *));
160 static int digraph_find __P((const char *buf));
161 static void confirm_fn __P((char *, int, char *));
162 static int IsOnDisplay __P((struct win *));
163 static void ResizeRegions __P((char *, int));
164 static void ResizeFin __P((char *, int, char *));
165 static struct action *FindKtab __P((char *, int));
166 static void SelectFin __P((char *, int, char *));
167 static void SelectLayoutFin __P((char *, int, char *));
170 extern struct layer *flayer;
171 extern struct display *display, *displays;
172 extern struct win *fore, *console_window, *windows;
173 extern struct acluser *users;
174 extern struct layout *layouts, *layout_attach, layout_last_marker;
175 extern struct layout *laytab[];
177 extern char screenterm[], HostName[], version[];
178 extern struct NewWindow nwin_undef, nwin_default;
179 extern struct LayFuncs WinLf, MarkLf;
181 extern int Z0width, Z1width;
182 extern int real_uid, real_gid;
184 #ifdef NETHACK
185 extern int nethackflag;
186 #endif
189 extern struct win **wtab;
191 #ifdef MULTIUSER
192 extern char *multi;
193 extern int maxusercount;
194 #endif
195 char NullStr[] = "";
197 struct plop plop_tab[MAX_PLOP_DEFS];
199 #ifndef PTYMODE
200 # define PTYMODE 0622
201 #endif
203 int TtyMode = PTYMODE;
204 int hardcopy_append = 0;
205 int all_norefresh = 0;
206 #ifdef ZMODEM
207 int zmodem_mode = 0;
208 char *zmodem_sendcmd;
209 char *zmodem_recvcmd;
210 static char *zmodes[4] = {"off", "auto", "catch", "pass"};
211 #endif
213 int idletimo;
214 struct action idleaction;
215 #ifdef BLANKER_PRG
216 char **blankerprg;
217 #endif
219 struct action ktab[256 + KMAP_KEYS]; /* command key translation table */
220 struct kclass {
221 struct kclass *next;
222 char *name;
223 struct action ktab[256 + KMAP_KEYS];
225 struct kclass *kclasses;
227 #ifdef MAPKEYS
228 struct action umtab[KMAP_KEYS+KMAP_AKEYS];
229 struct action dmtab[KMAP_KEYS+KMAP_AKEYS];
230 struct action mmtab[KMAP_KEYS+KMAP_AKEYS];
231 struct kmap_ext *kmap_exts;
232 int kmap_extn;
233 static int maptimeout = 300;
234 #endif
236 #ifndef MAX_DIGRAPH
237 #define MAX_DIGRAPH 512
238 #endif
240 struct digraph
242 unsigned char d[2];
243 int value;
246 /* digraph table taken from old vim and rfc1345 */
247 static struct digraph digraphs[MAX_DIGRAPH + 1] = {
248 {' ', ' ', 160}, /*   */
249 {'N', 'S', 160}, /*   */
250 {'~', '!', 161}, /* ¡ */
251 {'!', '!', 161}, /* ¡ */
252 {'!', 'I', 161}, /* ¡ */
253 {'c', '|', 162}, /* ¢ */
254 {'c', 't', 162}, /* ¢ */
255 {'$', '$', 163}, /* £ */
256 {'P', 'd', 163}, /* £ */
257 {'o', 'x', 164}, /* ¤ */
258 {'C', 'u', 164}, /* ¤ */
259 {'C', 'u', 164}, /* ¤ */
260 {'E', 'u', 164}, /* ¤ */
261 {'Y', '-', 165}, /* ¥ */
262 {'Y', 'e', 165}, /* ¥ */
263 {'|', '|', 166}, /* ¦ */
264 {'B', 'B', 166}, /* ¦ */
265 {'p', 'a', 167}, /* § */
266 {'S', 'E', 167}, /* § */
267 {'"', '"', 168}, /* ¨ */
268 {'\'', ':', 168}, /* ¨ */
269 {'c', 'O', 169}, /* © */
270 {'C', 'o', 169}, /* © */
271 {'a', '-', 170}, /* ª */
272 {'<', '<', 171}, /* « */
273 {'-', ',', 172}, /* ¬ */
274 {'N', 'O', 172}, /* ¬ */
275 {'-', '-', 173}, /* ­ */
276 {'r', 'O', 174}, /* ® */
277 {'R', 'g', 174}, /* ® */
278 {'-', '=', 175}, /* ¯ */
279 {'\'', 'm', 175}, /* ¯ */
280 {'~', 'o', 176}, /* ° */
281 {'D', 'G', 176}, /* ° */
282 {'+', '-', 177}, /* ± */
283 {'2', '2', 178}, /* ² */
284 {'2', 'S', 178}, /* ² */
285 {'3', '3', 179}, /* ³ */
286 {'3', 'S', 179}, /* ³ */
287 {'\'', '\'', 180}, /* ´ */
288 {'j', 'u', 181}, /* µ */
289 {'M', 'y', 181}, /* µ */
290 {'p', 'p', 182}, /* ¶ */
291 {'P', 'I', 182}, /* ¶ */
292 {'~', '.', 183}, /* · */
293 {'.', 'M', 183}, /* · */
294 {',', ',', 184}, /* ¸ */
295 {'\'', ',', 184}, /* ¸ */
296 {'1', '1', 185}, /* ¹ */
297 {'1', 'S', 185}, /* ¹ */
298 {'o', '-', 186}, /* º */
299 {'>', '>', 187}, /* » */
300 {'1', '4', 188}, /* ¼ */
301 {'1', '2', 189}, /* ½ */
302 {'3', '4', 190}, /* ¾ */
303 {'~', '?', 191}, /* ¿ */
304 {'?', '?', 191}, /* ¿ */
305 {'?', 'I', 191}, /* ¿ */
306 {'A', '`', 192}, /* À */
307 {'A', '!', 192}, /* À */
308 {'A', '\'', 193}, /* Á */
309 {'A', '^', 194}, /* Â */
310 {'A', '>', 194}, /* Â */
311 {'A', '~', 195}, /* Ã */
312 {'A', '?', 195}, /* Ã */
313 {'A', '"', 196}, /* Ä */
314 {'A', ':', 196}, /* Ä */
315 {'A', '@', 197}, /* Å */
316 {'A', 'A', 197}, /* Å */
317 {'A', 'E', 198}, /* Æ */
318 {'C', ',', 199}, /* Ç */
319 {'E', '`', 200}, /* È */
320 {'E', '!', 200}, /* È */
321 {'E', '\'', 201}, /* É */
322 {'E', '^', 202}, /* Ê */
323 {'E', '>', 202}, /* Ê */
324 {'E', '"', 203}, /* Ë */
325 {'E', ':', 203}, /* Ë */
326 {'I', '`', 204}, /* Ì */
327 {'I', '!', 204}, /* Ì */
328 {'I', '\'', 205}, /* Í */
329 {'I', '^', 206}, /* Î */
330 {'I', '>', 206}, /* Î */
331 {'I', '"', 207}, /* Ï */
332 {'I', ':', 207}, /* Ï */
333 {'D', '-', 208}, /* Ð */
334 {'N', '~', 209}, /* Ñ */
335 {'N', '?', 209}, /* Ñ */
336 {'O', '`', 210}, /* Ò */
337 {'O', '!', 210}, /* Ò */
338 {'O', '\'', 211}, /* Ó */
339 {'O', '^', 212}, /* Ô */
340 {'O', '>', 212}, /* Ô */
341 {'O', '~', 213}, /* Õ */
342 {'O', '?', 213}, /* Õ */
343 {'O', '"', 214}, /* Ö */
344 {'O', ':', 214}, /* Ö */
345 {'/', '\\', 215}, /* × */
346 {'*', 'x', 215}, /* × */
347 {'O', '/', 216}, /* Ø */
348 {'U', '`', 217}, /* Ù */
349 {'U', '!', 217}, /* Ù */
350 {'U', '\'', 218}, /* Ú */
351 {'U', '^', 219}, /* Û */
352 {'U', '>', 219}, /* Û */
353 {'U', '"', 220}, /* Ü */
354 {'U', ':', 220}, /* Ü */
355 {'Y', '\'', 221}, /* Ý */
356 {'I', 'p', 222}, /* Þ */
357 {'T', 'H', 222}, /* Þ */
358 {'s', 's', 223}, /* ß */
359 {'s', '"', 223}, /* ß */
360 {'a', '`', 224}, /* à */
361 {'a', '!', 224}, /* à */
362 {'a', '\'', 225}, /* á */
363 {'a', '^', 226}, /* â */
364 {'a', '>', 226}, /* â */
365 {'a', '~', 227}, /* ã */
366 {'a', '?', 227}, /* ã */
367 {'a', '"', 228}, /* ä */
368 {'a', ':', 228}, /* ä */
369 {'a', 'a', 229}, /* å */
370 {'a', 'e', 230}, /* æ */
371 {'c', ',', 231}, /* ç */
372 {'e', '`', 232}, /* è */
373 {'e', '!', 232}, /* è */
374 {'e', '\'', 233}, /* é */
375 {'e', '^', 234}, /* ê */
376 {'e', '>', 234}, /* ê */
377 {'e', '"', 235}, /* ë */
378 {'e', ':', 235}, /* ë */
379 {'i', '`', 236}, /* ì */
380 {'i', '!', 236}, /* ì */
381 {'i', '\'', 237}, /* í */
382 {'i', '^', 238}, /* î */
383 {'i', '>', 238}, /* î */
384 {'i', '"', 239}, /* ï */
385 {'i', ':', 239}, /* ï */
386 {'d', '-', 240}, /* ð */
387 {'n', '~', 241}, /* ñ */
388 {'n', '?', 241}, /* ñ */
389 {'o', '`', 242}, /* ò */
390 {'o', '!', 242}, /* ò */
391 {'o', '\'', 243}, /* ó */
392 {'o', '^', 244}, /* ô */
393 {'o', '>', 244}, /* ô */
394 {'o', '~', 245}, /* õ */
395 {'o', '?', 245}, /* õ */
396 {'o', '"', 246}, /* ö */
397 {'o', ':', 246}, /* ö */
398 {':', '-', 247}, /* ÷ */
399 {'o', '/', 248}, /* ø */
400 {'u', '`', 249}, /* ù */
401 {'u', '!', 249}, /* ù */
402 {'u', '\'', 250}, /* ú */
403 {'u', '^', 251}, /* û */
404 {'u', '>', 251}, /* û */
405 {'u', '"', 252}, /* ü */
406 {'u', ':', 252}, /* ü */
407 {'y', '\'', 253}, /* ý */
408 {'i', 'p', 254}, /* þ */
409 {'t', 'h', 254}, /* þ */
410 {'y', '"', 255}, /* ÿ */
411 {'y', ':', 255}, /* ÿ */
412 {'"', '[', 196}, /* Ä */
413 {'"', '\\', 214}, /* Ö */
414 {'"', ']', 220}, /* Ü */
415 {'"', '{', 228}, /* ä */
416 {'"', '|', 246}, /* ö */
417 {'"', '}', 252}, /* ü */
418 {'"', '~', 223} /* ß */
421 #define RESIZE_FLAG_H 1
422 #define RESIZE_FLAG_V 2
423 #define RESIZE_FLAG_L 4
425 static char *resizeprompts[] = {
426 "resize # lines: ",
427 "resize -h # lines: ",
428 "resize -v # lines: ",
429 "resize -b # lines: ",
430 "resize -l # lines: ",
431 "resize -l -h # lines: ",
432 "resize -l -v # lines: ",
433 "resize -l -b # lines: ",
436 static int
437 parse_input_int(buf, len, val)
438 const char *buf;
439 int len;
440 int *val;
442 int x = 0, i;
443 if (len >= 1 && ((*buf == 'U' && buf[1] == '+') || (*buf == '0' && (buf[1] == 'x' || buf[1] == 'X'))))
445 x = 0;
446 for (i = 2; i < len; i++)
448 if (buf[i] >= '0' && buf[i] <= '9')
449 x = x * 16 | (buf[i] - '0');
450 else if (buf[i] >= 'a' && buf[i] <= 'f')
451 x = x * 16 | (buf[i] - ('a' - 10));
452 else if (buf[i] >= 'A' && buf[i] <= 'F')
453 x = x * 16 | (buf[i] - ('A' - 10));
454 else
455 return 0;
458 else if (buf[0] == '0')
460 x = 0;
461 for (i = 1; i < len; i++)
463 if (buf[i] < '0' || buf[i] > '7')
464 return 0;
465 x = x * 8 | (buf[i] - '0');
468 else
469 return 0;
470 *val = x;
471 return 1;
474 char *noargs[1];
476 void
477 InitKeytab()
479 register unsigned int i;
480 #ifdef MAPKEYS
481 char *argarr[2];
482 #endif
484 for (i = 0; i < sizeof(ktab)/sizeof(*ktab); i++)
486 ktab[i].nr = RC_ILLEGAL;
487 ktab[i].args = noargs;
488 ktab[i].argl = 0;
490 #ifdef MAPKEYS
491 for (i = 0; i < KMAP_KEYS+KMAP_AKEYS; i++)
493 umtab[i].nr = RC_ILLEGAL;
494 umtab[i].args = noargs;
495 umtab[i].argl = 0;
496 dmtab[i].nr = RC_ILLEGAL;
497 dmtab[i].args = noargs;
498 dmtab[i].argl = 0;
499 mmtab[i].nr = RC_ILLEGAL;
500 mmtab[i].args = noargs;
501 mmtab[i].argl = 0;
503 argarr[1] = 0;
504 for (i = 0; i < NKMAPDEF; i++)
506 if (i + KMAPDEFSTART < T_CAPS)
507 continue;
508 if (i + KMAPDEFSTART >= T_CAPS + KMAP_KEYS)
509 continue;
510 if (kmapdef[i] == 0)
511 continue;
512 argarr[0] = kmapdef[i];
513 SaveAction(dmtab + i + (KMAPDEFSTART - T_CAPS), RC_STUFF, argarr, 0);
515 for (i = 0; i < NKMAPADEF; i++)
517 if (i + KMAPADEFSTART < T_CURSOR)
518 continue;
519 if (i + KMAPADEFSTART >= T_CURSOR + KMAP_AKEYS)
520 continue;
521 if (kmapadef[i] == 0)
522 continue;
523 argarr[0] = kmapadef[i];
524 SaveAction(dmtab + i + (KMAPADEFSTART - T_CURSOR + KMAP_KEYS), RC_STUFF, argarr, 0);
526 for (i = 0; i < NKMAPMDEF; i++)
528 if (i + KMAPMDEFSTART < T_CAPS)
529 continue;
530 if (i + KMAPMDEFSTART >= T_CAPS + KMAP_KEYS)
531 continue;
532 if (kmapmdef[i] == 0)
533 continue;
534 argarr[0] = kmapmdef[i];
535 argarr[1] = 0;
536 SaveAction(mmtab + i + (KMAPMDEFSTART - T_CAPS), RC_STUFF, argarr, 0);
538 #endif
540 ktab['h'].nr = RC_HARDCOPY;
541 #ifdef BSDJOBS
542 ktab['z'].nr = ktab[Ctrl('z')].nr = RC_SUSPEND;
543 #endif
544 ktab['c'].nr = ktab[Ctrl('c')].nr = RC_SCREEN;
545 ktab[' '].nr = ktab[Ctrl(' ')].nr =
546 ktab['n'].nr = ktab[Ctrl('n')].nr = RC_NEXT;
547 ktab['N'].nr = RC_NUMBER;
548 ktab[Ctrl('h')].nr = ktab[0177].nr = ktab['p'].nr = ktab[Ctrl('p')].nr = RC_PREV;
549 ktab['k'].nr = ktab[Ctrl('k')].nr = RC_KILL;
550 ktab['l'].nr = ktab[Ctrl('l')].nr = RC_REDISPLAY;
551 ktab['w'].nr = ktab[Ctrl('w')].nr = RC_WINDOWS;
552 ktab['v'].nr = RC_VERSION;
553 ktab[Ctrl('v')].nr = RC_DIGRAPH;
554 ktab['q'].nr = ktab[Ctrl('q')].nr = RC_XON;
555 ktab['s'].nr = ktab[Ctrl('s')].nr = RC_XOFF;
556 ktab['t'].nr = ktab[Ctrl('t')].nr = RC_TIME;
557 ktab['i'].nr = ktab[Ctrl('i')].nr = RC_INFO;
558 ktab['m'].nr = ktab[Ctrl('m')].nr = RC_LASTMSG;
559 ktab['A'].nr = RC_TITLE;
560 #if defined(UTMPOK) && defined(LOGOUTOK)
561 ktab['L'].nr = RC_LOGIN;
562 #endif
563 ktab[','].nr = RC_LICENSE;
564 ktab['W'].nr = RC_WIDTH;
565 ktab['.'].nr = RC_DUMPTERMCAP;
566 ktab[Ctrl('\\')].nr = RC_QUIT;
567 #ifdef DETACH
568 ktab['d'].nr = ktab[Ctrl('d')].nr = RC_DETACH;
569 # ifdef POW_DETACH
570 ktab['D'].nr = RC_POW_DETACH;
571 # endif
572 #endif
573 ktab['r'].nr = ktab[Ctrl('r')].nr = RC_WRAP;
574 ktab['f'].nr = ktab[Ctrl('f')].nr = RC_FLOW;
575 ktab['C'].nr = RC_CLEAR;
576 ktab['Z'].nr = RC_RESET;
577 ktab['H'].nr = RC_LOG;
578 ktab['M'].nr = RC_MONITOR;
579 ktab['?'].nr = RC_HELP;
580 #ifdef MULTI
581 ktab['*'].nr = RC_DISPLAYS;
582 #endif
584 char *args[2];
585 args[0] = "-";
586 args[1] = NULL;
587 SaveAction(ktab + '-', RC_SELECT, args, 0);
589 for (i = 0; i < ((maxwin && maxwin < 10) ? maxwin : 10); i++)
591 char *args[2], arg1[10];
592 args[0] = arg1;
593 args[1] = 0;
594 sprintf(arg1, "%d", i);
595 SaveAction(ktab + '0' + i, RC_SELECT, args, 0);
597 ktab['\''].nr = RC_SELECT; /* calling a window by name */
599 char *args[2];
600 args[0] = "-b";
601 args[1] = 0;
602 SaveAction(ktab + '"', RC_WINDOWLIST, args, 0);
604 ktab[Ctrl('G')].nr = RC_VBELL;
605 ktab[':'].nr = RC_COLON;
606 #ifdef COPY_PASTE
607 ktab['['].nr = ktab[Ctrl('[')].nr = RC_COPY;
609 char *args[2];
610 args[0] = ".";
611 args[1] = 0;
612 SaveAction(ktab + ']', RC_PASTE, args, 0);
613 SaveAction(ktab + Ctrl(']'), RC_PASTE, args, 0);
615 ktab['{'].nr = RC_HISTORY;
616 ktab['}'].nr = RC_HISTORY;
617 ktab['>'].nr = RC_WRITEBUF;
618 ktab['<'].nr = RC_READBUF;
619 ktab['='].nr = RC_REMOVEBUF;
620 #endif
621 #ifdef POW_DETACH
622 ktab['D'].nr = RC_POW_DETACH;
623 #endif
624 #ifdef LOCK
625 ktab['x'].nr = ktab[Ctrl('x')].nr = RC_LOCKSCREEN;
626 #endif
627 ktab['b'].nr = ktab[Ctrl('b')].nr = RC_BREAK;
628 ktab['B'].nr = RC_POW_BREAK;
629 ktab['_'].nr = RC_SILENCE;
630 ktab['S'].nr = RC_SPLIT;
631 ktab['Q'].nr = RC_ONLY;
632 ktab['X'].nr = RC_REMOVE;
633 ktab['F'].nr = RC_FIT;
634 ktab['\t'].nr = RC_FOCUS;
636 char *args[2];
637 args[0] = "prev";
638 args[1] = 0;
639 SaveAction(ktab + T_BACKTAB - T_CAPS + 256, RC_FOCUS, args, 0);
642 char *args[2];
643 args[0] = "-v";
644 args[1] = 0;
645 SaveAction(ktab + '|', RC_SPLIT, args, 0);
647 /* These come last; they may want overwrite others: */
648 if (DefaultEsc >= 0)
650 ClearAction(&ktab[DefaultEsc]);
651 ktab[DefaultEsc].nr = RC_OTHER;
653 if (DefaultMetaEsc >= 0)
655 ClearAction(&ktab[DefaultMetaEsc]);
656 ktab[DefaultMetaEsc].nr = RC_META;
659 idleaction.nr = RC_BLANKER;
660 idleaction.args = noargs;
661 idleaction.argl = 0;
664 static struct action *
665 FindKtab(class, create)
666 char *class;
667 int create;
669 struct kclass *kp, **kpp;
670 int i;
672 if (class == 0)
673 return ktab;
674 for (kpp = &kclasses; (kp = *kpp) != 0; kpp = &kp->next)
675 if (!strcmp(kp->name, class))
676 break;
677 if (kp == 0)
679 if (!create)
680 return 0;
681 if (strlen(class) > 80)
683 Msg(0, "Command class name too long.");
684 return 0;
686 kp = malloc(sizeof(*kp));
687 if (kp == 0)
689 Msg(0, strnomem);
690 return 0;
692 kp->name = SaveStr(class);
693 for (i = 0; i < (int)(sizeof(kp->ktab)/sizeof(*kp->ktab)); i++)
695 kp->ktab[i].nr = RC_ILLEGAL;
696 kp->ktab[i].args = noargs;
697 kp->ktab[i].argl = 0;
698 kp->ktab[i].quiet = 0;
700 kp->next = 0;
701 *kpp = kp;
703 return kp->ktab;
706 static void
707 ClearAction(act)
708 struct action *act;
710 char **p;
712 if (act->nr == RC_ILLEGAL)
713 return;
714 act->nr = RC_ILLEGAL;
715 if (act->args == noargs)
716 return;
717 for (p = act->args; *p; p++)
718 free(*p);
719 free((char *)act->args);
720 act->args = noargs;
721 act->argl = 0;
725 * ProcessInput: process input from display and feed it into
726 * the layer on canvas D_forecv.
729 #ifdef MAPKEYS
732 * This ProcessInput just does the keybindings and passes
733 * everything else on to ProcessInput2.
736 void
737 ProcessInput(ibuf, ilen)
738 char *ibuf;
739 int ilen;
741 int ch, slen;
742 unsigned char *s, *q;
743 int i, l;
744 char *p;
746 debug1("ProcessInput: %d bytes\n", ilen);
747 if (display == 0 || ilen == 0)
748 return;
749 if (D_seql)
750 evdeq(&D_mapev);
751 slen = ilen;
752 s = (unsigned char *)ibuf;
753 while (ilen-- > 0)
755 ch = *s++;
756 if (D_dontmap || !D_nseqs)
758 D_dontmap = 0;
759 continue;
761 for (;;)
763 debug3("cmp %c %c[%d]\n", ch, *D_seqp, D_seqp - D_kmaps);
764 if (*D_seqp != ch)
766 l = D_seqp[D_seqp[-D_seql-1] + 1];
767 if (l)
769 D_seqp += l * 2 + 4;
770 debug1("miss %d\n", D_seqp - D_kmaps);
771 continue;
773 debug("complete miss\n");
774 D_mapdefault = 0;
775 l = D_seql;
776 p = (char *)D_seqp - l;
777 D_seql = 0;
778 D_seqp = D_kmaps + 3;
779 if (l == 0)
780 break;
781 if ((q = D_seqh) != 0)
783 D_seqh = 0;
784 i = q[0] << 8 | q[1];
785 i &= ~KMAP_NOTIMEOUT;
786 debug1("Mapping former hit #%d - ", i);
787 debug2("%d(%s) - ", q[2], q + 3);
788 if (StuffKey(i))
789 ProcessInput2((char *)q + 3, q[2]);
790 if (display == 0)
791 return;
792 l -= q[2];
793 p += q[2];
795 else
796 D_dontmap = 1;
797 debug1("flush old %d\n", l);
798 ProcessInput(p, l);
799 if (display == 0)
800 return;
801 evdeq(&D_mapev);
802 continue;
804 if (D_seql++ == 0)
806 /* Finish old stuff */
807 slen -= ilen + 1;
808 debug1("finish old %d\n", slen);
809 if (slen)
810 ProcessInput2(ibuf, slen);
811 if (display == 0)
812 return;
813 D_seqh = 0;
815 ibuf = (char *)s;
816 slen = ilen;
817 D_seqp++;
818 l = D_seql;
819 debug2("length am %d, want %d\n", l, D_seqp[-l - 1]);
820 if (l == D_seqp[-l - 1])
822 if (D_seqp[l] != l)
824 q = D_seqp + 1 + l;
825 if (D_kmaps + D_nseqs > q && q[2] > l && !bcmp(D_seqp - l, q + 3, l))
827 debug1("have another mapping (%s), delay execution\n", q + 3);
828 D_seqh = D_seqp - 3 - l;
829 D_seqp = q + 3 + l;
830 break;
833 i = D_seqp[-l - 3] << 8 | D_seqp[-l - 2];
834 i &= ~KMAP_NOTIMEOUT;
835 debug1("Mapping #%d - ", i);
836 p = (char *)D_seqp - l;
837 debug2("%d(%s) - ", l, p);
838 D_seql = 0;
839 D_seqp = D_kmaps + 3;
840 D_seqh = 0;
841 if (StuffKey(i))
842 ProcessInput2(p, l);
843 if (display == 0)
844 return;
846 break;
849 if (D_seql)
851 debug("am in sequence -> check for timeout\n");
852 l = D_seql;
853 for (s = D_seqp; ; s += i * 2 + 4)
855 if (s[-l-3] & KMAP_NOTIMEOUT >> 8)
856 break;
857 if ((i = s[s[-l-1] + 1]) == 0)
859 SetTimeout(&D_mapev, maptimeout);
860 evenq(&D_mapev);
861 break;
865 ProcessInput2(ibuf, slen);
868 #else
869 # define ProcessInput2 ProcessInput
870 #endif
874 * Here only the screen escape commands are handled.
877 void
878 ProcessInput2(ibuf, ilen)
879 char *ibuf;
880 int ilen;
882 char *s;
883 int ch, slen;
884 struct action *ktabp;
886 debug1("ProcessInput2: %d bytes\n", ilen);
887 while (ilen && display)
889 debug1(" - ilen now %d bytes\n", ilen);
890 flayer = D_forecv->c_layer;
891 fore = D_fore;
892 slen = ilen;
893 s = ibuf;
894 if (!D_ESCseen)
896 while (ilen > 0)
898 if ((unsigned char)*s++ == D_user->u_Esc)
899 break;
900 ilen--;
902 slen -= ilen;
903 if (slen)
904 DoProcess(fore, &ibuf, &slen, 0);
905 if (--ilen == 0)
906 D_ESCseen = ktab;
908 if (ilen <= 0)
909 return;
910 ktabp = D_ESCseen ? D_ESCseen : ktab;
911 D_ESCseen = 0;
912 ch = (unsigned char)*s;
915 * As users have different esc characters, but a common ktab[],
916 * we fold back the users esc and meta-esc key to the Default keys
917 * that can be looked up in the ktab[]. grmbl. jw.
918 * XXX: make ktab[] a per user thing.
920 if (ch == D_user->u_Esc)
921 ch = DefaultEsc;
922 else if (ch == D_user->u_MetaEsc)
923 ch = DefaultMetaEsc;
925 if (ch >= 0)
926 DoAction(&ktabp[ch], ch);
927 ibuf = (char *)(s + 1);
928 ilen--;
932 void
933 DoProcess(p, bufp, lenp, pa)
934 struct win *p;
935 char **bufp;
936 int *lenp;
937 struct paster *pa;
939 int oldlen;
940 struct display *d = display;
942 #ifdef COPY_PASTE
943 /* XXX -> PasteStart */
944 if (pa && *lenp > 1 && p && p->w_slowpaste)
946 /* schedule slowpaste event */
947 SetTimeout(&p->w_paster.pa_slowev, p->w_slowpaste);
948 evenq(&p->w_paster.pa_slowev);
949 return;
951 #endif
952 while (flayer && *lenp)
954 #ifdef COPY_PASTE
955 if (!pa && p && p->w_paster.pa_pastelen && flayer == p->w_paster.pa_pastelayer)
957 debug("layer is busy - beep!\n");
958 WBell(p, visual_bell);
959 *bufp += *lenp;
960 *lenp = 0;
961 display = d;
962 return;
964 #endif
965 oldlen = *lenp;
966 LayProcess(bufp, lenp);
967 #ifdef COPY_PASTE
968 if (pa && !pa->pa_pastelayer)
969 break; /* flush rest of paste */
970 #endif
971 if (*lenp == oldlen)
973 if (pa)
975 display = d;
976 return;
978 /* We're full, let's beep */
979 debug("layer is full - beep!\n");
980 WBell(p, visual_bell);
981 break;
984 *bufp += *lenp;
985 *lenp = 0;
986 display = d;
987 #ifdef COPY_PASTE
988 if (pa && pa->pa_pastelen == 0)
989 FreePaster(pa);
990 #endif
994 FindCommnr(str)
995 const char *str;
997 int x, m, l = 0, r = RC_LAST;
998 while (l <= r)
1000 m = (l + r) / 2;
1001 x = strcmp(str, comms[m].name);
1002 if (x > 0)
1003 l = m + 1;
1004 else if (x < 0)
1005 r = m - 1;
1006 else
1007 return m;
1009 return RC_ILLEGAL;
1012 static int
1013 CheckArgNum(nr, args)
1014 int nr;
1015 char **args;
1017 int i, n;
1018 static char *argss[] = {"no", "one", "two", "three", "four", "OOPS"};
1019 static char *orformat[] =
1021 "%s: %s: %s argument%s required",
1022 "%s: %s: %s or %s argument%s required",
1023 "%s: %s: %s, %s or %s argument%s required",
1024 "%s: %s: %s, %s, %s or %s argument%s required"
1027 n = comms[nr].flags & ARGS_MASK;
1028 for (i = 0; args[i]; i++)
1030 if (comms[nr].flags & ARGS_ORMORE)
1032 if (i < n)
1034 Msg(0, "%s: %s: at least %s argument%s required",
1035 rc_name, comms[nr].name, argss[n], n != 1 ? "s" : "");
1036 return -1;
1039 else if ((comms[nr].flags & ARGS_PLUS1) &&
1040 (comms[nr].flags & ARGS_PLUS2) &&
1041 (comms[nr].flags & ARGS_PLUS3))
1043 if (i != n && i != n + 1 && i != n + 2 && i != n + 3)
1045 Msg(0, orformat[3], rc_name, comms[nr].name, argss[n],
1046 argss[n + 1], argss[n + 2], argss[n + 3], "");
1047 return -1;
1050 else if ((comms[nr].flags & ARGS_PLUS1) &&
1051 (comms[nr].flags & ARGS_PLUS2))
1053 if (i != n && i != n + 1 && i != n + 2)
1055 Msg(0, orformat[2], rc_name, comms[nr].name, argss[n],
1056 argss[n + 1], argss[n + 2], "");
1057 return -1;
1060 else if ((comms[nr].flags & ARGS_PLUS1) &&
1061 (comms[nr].flags & ARGS_PLUS3))
1063 if (i != n && i != n + 1 && i != n + 3)
1065 Msg(0, orformat[2], rc_name, comms[nr].name, argss[n],
1066 argss[n + 1], argss[n + 3], "");
1067 return -1;
1070 else if ((comms[nr].flags & ARGS_PLUS2) &&
1071 (comms[nr].flags & ARGS_PLUS3))
1073 if (i != n && i != n + 2 && i != n + 3)
1075 Msg(0, orformat[2], rc_name, comms[nr].name, argss[n],
1076 argss[n + 2], argss[n + 3], "");
1077 return -1;
1080 else if (comms[nr].flags & ARGS_PLUS1)
1082 if (i != n && i != n + 1)
1084 Msg(0, orformat[1], rc_name, comms[nr].name, argss[n],
1085 argss[n + 1], n != 0 ? "s" : "");
1086 return -1;
1089 else if (comms[nr].flags & ARGS_PLUS2)
1091 if (i != n && i != n + 2)
1093 Msg(0, orformat[1], rc_name, comms[nr].name, argss[n],
1094 argss[n + 2], "s");
1095 return -1;
1098 else if (comms[nr].flags & ARGS_PLUS3)
1100 if (i != n && i != n + 3)
1102 Msg(0, orformat[1], rc_name, comms[nr].name, argss[n],
1103 argss[n + 3], "");
1104 return -1;
1107 else if (i != n)
1109 Msg(0, orformat[0], rc_name, comms[nr].name, argss[n], n != 1 ? "s" : "");
1110 return -1;
1112 return i;
1115 static void
1116 StuffFin(buf, len, data)
1117 char *buf;
1118 int len;
1119 char *data;
1121 if (!flayer)
1122 return;
1123 while(len)
1124 LayProcess(&buf, &len);
1127 /*ARGSUSED*/
1128 void
1129 DoAction(act, key)
1130 struct action *act;
1131 int key;
1133 int nr = act->nr;
1134 char **args = act->args;
1135 int *argl = act->argl;
1136 struct win *p;
1137 int argc, i, n, msgok;
1138 char *s;
1139 char ch;
1140 struct display *odisplay = display;
1141 struct acluser *user;
1143 user = display ? D_user : users;
1144 if (nr == RC_ILLEGAL)
1146 debug1("key '%c': No action\n", key);
1147 return;
1149 n = comms[nr].flags;
1150 if ((n & NEED_DISPLAY) && display == 0)
1152 Msg(0, "%s: %s: display required", rc_name, comms[nr].name);
1153 return;
1155 if ((n & NEED_FORE) && fore == 0)
1157 Msg(0, "%s: %s: window required", rc_name, comms[nr].name);
1158 return;
1160 if ((n & NEED_LAYER) && flayer == 0)
1162 Msg(0, "%s: %s: display or window required", rc_name, comms[nr].name);
1163 return;
1165 if ((argc = CheckArgNum(nr, args)) < 0)
1166 return;
1167 #ifdef MULTIUSER
1168 if (display)
1170 if (AclCheckPermCmd(D_user, ACL_EXEC, &comms[nr]))
1172 Msg(0, "%s: %s: permission denied (user %s)",
1173 rc_name, comms[nr].name, (EffectiveAclUser ? EffectiveAclUser : D_user)->u_name);
1174 return;
1177 #endif /* MULTIUSER */
1179 msgok = display && !*rc_name;
1180 switch(nr)
1182 case RC_SELECT:
1183 if (!*args)
1184 InputSelect();
1185 else if (args[0][0] == '-' && !args[0][1])
1187 SetForeWindow((struct win *)0);
1188 Activate(0);
1190 else if (args[0][0] == '.' && !args[0][1])
1192 if (!fore)
1193 Msg(0, "select . needs a window");
1194 else
1196 SetForeWindow(fore);
1197 Activate(0);
1200 else if (ParseWinNum(act, &n) == 0)
1201 SwitchWindow(n);
1202 break;
1203 #ifdef AUTO_NUKE
1204 case RC_DEFAUTONUKE:
1205 if (ParseOnOff(act, &defautonuke) == 0 && msgok)
1206 Msg(0, "Default autonuke turned %s", defautonuke ? "on" : "off");
1207 if (display && *rc_name)
1208 D_auto_nuke = defautonuke;
1209 break;
1210 case RC_AUTONUKE:
1211 if (ParseOnOff(act, &D_auto_nuke) == 0 && msgok)
1212 Msg(0, "Autonuke turned %s", D_auto_nuke ? "on" : "off");
1213 break;
1214 #endif
1215 case RC_DEFOBUFLIMIT:
1216 if (ParseNum(act, &defobuflimit) == 0 && msgok)
1217 Msg(0, "Default limit set to %d", defobuflimit);
1218 if (display && *rc_name)
1220 D_obufmax = defobuflimit;
1221 D_obuflenmax = D_obuflen - D_obufmax;
1223 break;
1224 case RC_OBUFLIMIT:
1225 if (*args == 0)
1226 Msg(0, "Limit is %d, current buffer size is %d", D_obufmax, D_obuflen);
1227 else if (ParseNum(act, &D_obufmax) == 0 && msgok)
1228 Msg(0, "Limit set to %d", D_obufmax);
1229 D_obuflenmax = D_obuflen - D_obufmax;
1230 break;
1231 case RC_DUMPTERMCAP:
1232 WriteFile(user, (char *)0, DUMP_TERMCAP);
1233 break;
1234 case RC_HARDCOPY:
1236 int mode = DUMP_HARDCOPY;
1238 if (argc > 1 && !strcmp(*args, "-h"))
1240 mode = DUMP_SCROLLBACK;
1241 args++;
1242 argc--;
1244 if (*args && args[1])
1246 Msg(0, "%s: hardcopy: too many arguments", rc_name);
1247 break;
1249 if (fore == 0 && *args == 0)
1250 Msg(0, "%s: hardcopy: window required", rc_name);
1251 else
1252 WriteFile(user, *args, mode);
1254 break;
1255 case RC_DEFLOG:
1256 (void)ParseOnOff(act, &nwin_default.Lflag);
1257 break;
1258 case RC_LOG:
1259 n = fore->w_log ? 1 : 0;
1260 ParseSwitch(act, &n);
1261 LogToggle(n);
1262 break;
1263 #ifdef BSDJOBS
1264 case RC_SUSPEND:
1265 Detach(D_STOP);
1266 break;
1267 #endif
1268 case RC_NEXT:
1269 if (MoreWindows())
1270 SwitchWindow(NextWindow());
1271 break;
1272 case RC_PREV:
1273 if (MoreWindows())
1274 SwitchWindow(PreviousWindow());
1275 break;
1276 case RC_KILL:
1278 char *name;
1280 if (key >= 0)
1282 #ifdef PSEUDOS
1283 Input(fore->w_pwin ? "Really kill this filter [y/n]" : "Really kill this window [y/n]", 1, INP_RAW, confirm_fn, NULL, RC_KILL);
1284 #else
1285 Input("Really kill this window [y/n]", 1, INP_RAW, confirm_fn, NULL, RC_KILL);
1286 #endif
1287 break;
1289 n = fore->w_number;
1290 #ifdef PSEUDOS
1291 if (fore->w_pwin)
1293 FreePseudowin(fore);
1294 Msg(0, "Filter removed.");
1295 break;
1297 #endif
1298 name = SaveStr(fore->w_title);
1299 KillWindow(fore);
1300 Msg(0, "Window %d (%s) killed.", n, name);
1301 if (name)
1302 free(name);
1303 break;
1305 case RC_QUIT:
1306 if (key >= 0)
1308 Input("Really quit and kill all your windows [y/n]", 1, INP_RAW, confirm_fn, NULL, RC_QUIT);
1309 break;
1311 Finit(0);
1312 /* NOTREACHED */
1313 #ifdef DETACH
1314 case RC_DETACH:
1315 if (*args && !strcmp(*args, "-h"))
1316 Hangup();
1317 else
1318 Detach(D_DETACH);
1319 break;
1320 # ifdef POW_DETACH
1321 case RC_POW_DETACH:
1322 if (key >= 0)
1324 static char buf[2];
1326 buf[0] = key;
1327 Input(buf, 1, INP_RAW, pow_detach_fn, NULL, 0);
1329 else
1330 Detach(D_POWER); /* detach and kill Attacher's parent */
1331 break;
1332 # endif
1333 #endif
1334 case RC_DEBUG:
1335 #ifdef DEBUG
1336 if (!*args)
1338 if (dfp)
1339 Msg(0, "debugging info is written to %s/", DEBUGDIR);
1340 else
1341 Msg(0, "debugging is currently off. Use 'debug on' to enable.");
1342 break;
1344 if (dfp)
1346 debug("debug: closing debug file.\n");
1347 fflush(dfp);
1348 fclose(dfp);
1349 dfp = NULL;
1351 if (strcmp("off", *args))
1352 opendebug(0, 1);
1353 # ifdef SIG_NODEBUG
1354 else if (display)
1355 kill(D_userpid, SIG_NODEBUG); /* a one shot item, but hey... */
1356 # endif /* SIG_NODEBUG */
1357 #else
1358 if (*args == 0 || strcmp("off", *args))
1359 Msg(0, "Sorry, screen was compiled without -DDEBUG option.");
1360 #endif
1361 break;
1362 #ifdef ZMODEM
1363 case RC_ZMODEM:
1364 if (*args && !strcmp(*args, "sendcmd"))
1366 if (args[1])
1368 free(zmodem_sendcmd);
1369 zmodem_sendcmd = SaveStr(args[1]);
1371 if (msgok)
1372 Msg(0, "zmodem sendcmd: %s", zmodem_sendcmd);
1373 break;
1375 if (*args && !strcmp(*args, "recvcmd"))
1377 if (args[1])
1379 free(zmodem_recvcmd);
1380 zmodem_recvcmd = SaveStr(args[1]);
1382 if (msgok)
1383 Msg(0, "zmodem recvcmd: %s", zmodem_recvcmd);
1384 break;
1386 if (*args)
1388 for (i = 0; i < 4; i++)
1389 if (!strcmp(zmodes[i], *args))
1390 break;
1391 if (i == 4 && !strcmp(*args, "on"))
1392 i = 1;
1393 if (i == 4)
1395 Msg(0, "usage: zmodem off|auto|catch|pass");
1396 break;
1398 zmodem_mode = i;
1400 if (msgok)
1401 Msg(0, "zmodem mode is %s", zmodes[zmodem_mode]);
1402 break;
1403 #endif
1404 case RC_UNBINDALL:
1406 register unsigned int i;
1408 for (i = 0; i < sizeof(ktab)/sizeof(*ktab); i++)
1409 ClearAction(&ktab[i]);
1410 Msg(0, "Unbound all keys." );
1411 break;
1413 case RC_ZOMBIE:
1415 if (!(s = *args))
1417 ZombieKey_destroy = 0;
1418 break;
1420 if (*argl == 0 || *argl > 2)
1422 Msg(0, "%s:zombie: one or two characters expected.", rc_name);
1423 break;
1425 if (args[1])
1427 if (!strcmp(args[1], "onerror"))
1429 ZombieKey_onerror = 1;
1430 } else {
1431 Msg(0, "usage: zombie [keys [onerror]]");
1432 break;
1434 } else
1435 ZombieKey_onerror = 0;
1436 ZombieKey_destroy = args[0][0];
1437 ZombieKey_resurrect = *argl == 2 ? args[0][1] : 0;
1439 break;
1440 case RC_WALL:
1441 #ifdef MULTIUSER
1442 s = D_user->u_name;
1443 #else
1444 s = D_usertty;
1445 #endif
1447 struct display *olddisplay = display;
1448 display = 0; /* no display will cause a broadcast */
1449 Msg(0, "%s: %s", s, *args);
1450 display = olddisplay;
1452 break;
1453 case RC_AT:
1454 /* where this AT command comes from: */
1455 if (!user)
1456 break;
1457 #ifdef MULTIUSER
1458 s = SaveStr(user->u_name);
1459 /* DO NOT RETURN FROM HERE WITHOUT RESETTING THIS: */
1460 EffectiveAclUser = user;
1461 #else
1462 s = SaveStr(display ? D_usertty : user->u_name);
1463 #endif
1464 n = strlen(args[0]);
1465 if (n) n--;
1467 * the windows/displays loops are quite dangerous here, take extra
1468 * care not to trigger landmines. Things may appear/disappear while
1469 * we are walking along.
1471 switch (args[0][n])
1473 case '*': /* user */
1475 struct display *nd;
1476 struct acluser *u;
1478 if (!n)
1479 u = user;
1480 else
1482 for (u = users; u; u = u->u_next)
1484 debug3("strncmp('%s', '%s', %d)\n", *args, u->u_name, n);
1485 if (!strncmp(*args, u->u_name, n))
1486 break;
1488 if (!u)
1490 args[0][n] = '\0';
1491 Msg(0, "Did not find any user matching '%s'", args[0]);
1492 break;
1495 debug1("at all displays of user %s\n", u->u_name);
1496 for (display = displays; display; display = nd)
1498 nd = display->d_next;
1499 if (D_forecv == 0)
1500 continue;
1501 flayer = D_forecv->c_layer;
1502 fore = D_fore;
1503 if (D_user != u)
1504 continue;
1505 debug1("AT display %s\n", D_usertty);
1506 DoCommand(args + 1, argl + 1);
1507 if (display)
1508 Msg(0, "command from %s: %s %s",
1509 s, args[1], args[2] ? args[2] : "");
1510 display = NULL;
1511 flayer = 0;
1512 fore = NULL;
1514 break;
1516 case '%': /* display */
1518 struct display *nd;
1520 debug1("at display matching '%s'\n", args[0]);
1521 for (display = displays; display; display = nd)
1523 nd = display->d_next;
1524 if (D_forecv == 0)
1525 continue;
1526 fore = D_fore;
1527 flayer = D_forecv->c_layer;
1528 if (strncmp(args[0], D_usertty, n) &&
1529 (strncmp("/dev/", D_usertty, 5) ||
1530 strncmp(args[0], D_usertty + 5, n)) &&
1531 (strncmp("/dev/tty", D_usertty, 8) ||
1532 strncmp(args[0], D_usertty + 8, n)))
1533 continue;
1534 debug1("AT display %s\n", D_usertty);
1535 DoCommand(args + 1, argl + 1);
1536 if (display)
1537 Msg(0, "command from %s: %s %s",
1538 s, args[1], args[2] ? args[2] : "");
1539 display = NULL;
1540 fore = NULL;
1541 flayer = 0;
1543 break;
1545 case '#': /* window */
1546 n--;
1547 /* FALLTHROUGH */
1548 default:
1550 struct win *nw;
1551 int ch;
1553 n++;
1554 ch = args[0][n];
1555 args[0][n] = '\0';
1556 if (!*args[0] || (i = WindowByNumber(args[0])) < 0)
1558 args[0][n] = ch; /* must restore string in case of bind */
1559 /* try looping over titles */
1560 for (fore = windows; fore; fore = nw)
1562 nw = fore->w_next;
1563 if (strncmp(args[0], fore->w_title, n))
1564 continue;
1565 debug2("AT window %d(%s)\n", fore->w_number, fore->w_title);
1567 * consider this a bug or a feature:
1568 * while looping through windows, we have fore AND
1569 * display context. This will confuse users who try to
1570 * set up loops inside of loops, but often allows to do
1571 * what you mean, even when you adress your context wrong.
1573 i = 0;
1574 /* XXX: other displays? */
1575 if (fore->w_layer.l_cvlist)
1576 display = fore->w_layer.l_cvlist->c_display;
1577 flayer = fore->w_savelayer ? fore->w_savelayer : &fore->w_layer;
1578 DoCommand(args + 1, argl + 1); /* may destroy our display */
1579 if (fore && fore->w_layer.l_cvlist)
1581 display = fore->w_layer.l_cvlist->c_display;
1582 Msg(0, "command from %s: %s %s",
1583 s, args[1], args[2] ? args[2] : "");
1586 display = NULL;
1587 fore = NULL;
1588 if (i < 0)
1589 Msg(0, "%s: at '%s': no such window.\n", rc_name, args[0]);
1590 break;
1592 else if (i < maxwin && (fore = wtab[i]))
1594 args[0][n] = ch; /* must restore string in case of bind */
1595 debug2("AT window %d (%s)\n", fore->w_number, fore->w_title);
1596 if (fore->w_layer.l_cvlist)
1597 display = fore->w_layer.l_cvlist->c_display;
1598 flayer = fore->w_savelayer ? fore->w_savelayer : &fore->w_layer;
1599 DoCommand(args + 1, argl + 1);
1600 if (fore && fore->w_layer.l_cvlist)
1602 display = fore->w_layer.l_cvlist->c_display;
1603 Msg(0, "command from %s: %s %s",
1604 s, args[1], args[2] ? args[2] : "");
1606 display = NULL;
1607 fore = NULL;
1609 else
1610 Msg(0, "%s: at [identifier][%%|*|#] command [args]", rc_name);
1611 break;
1614 free(s);
1615 #ifdef MULTIUSER
1616 EffectiveAclUser = NULL;
1617 #endif
1618 break;
1620 #ifdef COPY_PASTE
1621 case RC_READREG:
1622 #ifdef ENCODINGS
1623 i = fore ? fore->w_encoding : display ? display->d_encoding : 0;
1624 if (args[0] && args[1] && !strcmp(args[0], "-e"))
1626 i = FindEncoding(args[1]);
1627 if (i == -1)
1629 Msg(0, "%s: readreg: unknown encoding", rc_name);
1630 break;
1632 args += 2;
1634 #endif
1636 * Without arguments we prompt for a destination register.
1637 * It will receive the copybuffer contents.
1638 * This is not done by RC_PASTE, as we prompt for source
1639 * (not dest) there.
1641 if ((s = *args) == NULL)
1643 Input("Copy to register:", 1, INP_RAW, copy_reg_fn, NULL, 0);
1644 break;
1646 if (*argl != 1)
1648 Msg(0, "%s: copyreg: character, ^x, or (octal) \\032 expected.", rc_name);
1649 break;
1651 ch = args[0][0];
1653 * With two arguments we *really* read register contents from file
1655 if (args[1])
1657 if (args[2])
1659 Msg(0, "%s: readreg: too many arguments", rc_name);
1660 break;
1662 if ((s = ReadFile(args[1], &n)))
1664 struct plop *pp = plop_tab + (int)(unsigned char)ch;
1666 if (pp->buf)
1667 free(pp->buf);
1668 pp->buf = s;
1669 pp->len = n;
1670 #ifdef ENCODINGS
1671 pp->enc = i;
1672 #endif
1675 else
1677 * with one argument we copy the copybuffer into a specified register
1678 * This could be done with RC_PASTE too, but is here to be consistent
1679 * with the zero argument call.
1681 copy_reg_fn(&ch, 0, NULL);
1682 break;
1683 #endif
1684 case RC_REGISTER:
1685 #ifdef ENCODINGS
1686 i = fore ? fore->w_encoding : display ? display->d_encoding : 0;
1687 if (args[0] && args[1] && !strcmp(args[0], "-e"))
1689 i = FindEncoding(args[1]);
1690 if (i == -1)
1692 Msg(0, "%s: register: unknown encoding", rc_name);
1693 break;
1695 args += 2;
1696 argc -= 2;
1698 #endif
1699 if (argc != 2)
1701 Msg(0, "%s: register: illegal number of arguments.", rc_name);
1702 break;
1704 if (*argl != 1)
1706 Msg(0, "%s: register: character, ^x, or (octal) \\032 expected.", rc_name);
1707 break;
1709 ch = args[0][0];
1710 #ifdef COPY_PASTE
1711 if (ch == '.')
1713 if (user->u_plop.buf != NULL)
1714 UserFreeCopyBuffer(user);
1715 if (args[1] && args[1][0])
1717 user->u_plop.buf = SaveStrn(args[1], argl[1]);
1718 user->u_plop.len = argl[1];
1719 #ifdef ENCODINGS
1720 user->u_plop.enc = i;
1721 #endif
1724 else
1725 #endif
1727 struct plop *plp = plop_tab + (int)(unsigned char)ch;
1729 if (plp->buf)
1730 free(plp->buf);
1731 plp->buf = SaveStrn(args[1], argl[1]);
1732 plp->len = argl[1];
1733 #ifdef ENCODINGS
1734 plp->enc = i;
1735 #endif
1737 break;
1738 case RC_PROCESS:
1739 if ((s = *args) == NULL)
1741 Input("Process register:", 1, INP_RAW, process_fn, NULL, 0);
1742 break;
1744 if (*argl != 1)
1746 Msg(0, "%s: process: character, ^x, or (octal) \\032 expected.", rc_name);
1747 break;
1749 ch = args[0][0];
1750 process_fn(&ch, 0, NULL);
1751 break;
1752 case RC_STUFF:
1753 s = *args;
1754 if (!args[0])
1756 Input("Stuff:", 100, INP_COOKED, StuffFin, NULL, 0);
1757 break;
1759 n = *argl;
1760 if (args[1])
1762 if (strcmp(s, "-k"))
1764 Msg(0, "%s: stuff: invalid option %s", rc_name, s);
1765 break;
1767 s = args[1];
1768 for (i = T_CAPS; i < T_OCAPS; i++)
1769 if (strcmp(term[i].tcname, s) == 0)
1770 break;
1771 if (i == T_OCAPS)
1773 Msg(0, "%s: stuff: unknown key '%s'", rc_name, s);
1774 break;
1776 #ifdef MAPKEYS
1777 if (StuffKey(i - T_CAPS) == 0)
1778 break;
1779 #endif
1780 s = display ? D_tcs[i].str : 0;
1781 if (s == 0)
1782 break;
1783 n = strlen(s);
1785 while(n)
1786 LayProcess(&s, &n);
1787 break;
1788 case RC_REDISPLAY:
1789 Activate(-1);
1790 break;
1791 case RC_WINDOWS:
1792 ShowWindows(-1);
1793 break;
1794 case RC_VERSION:
1795 Msg(0, "screen %s", version);
1796 break;
1797 case RC_TIME:
1798 if (*args)
1800 timestring = SaveStr(*args);
1801 break;
1803 Msg(0, "%s", MakeWinMsg(timestring, fore, '%'));
1804 break;
1805 case RC_INFO:
1806 ShowInfo();
1807 break;
1808 case RC_DINFO:
1809 ShowDInfo();
1810 break;
1811 case RC_COMMAND:
1813 struct action *ktabp = ktab;
1814 if (argc == 2 && !strcmp(*args, "-c"))
1816 if ((ktabp = FindKtab(args[1], 0)) == 0)
1818 Msg(0, "Unknown command class '%s'", args[1]);
1819 break;
1822 if (D_ESCseen != ktab || ktabp != ktab)
1824 D_ESCseen = ktabp;
1825 break;
1827 D_ESCseen = 0;
1829 /* FALLTHROUGH */
1830 case RC_OTHER:
1831 if (MoreWindows())
1832 SwitchWindow(display && D_other ? D_other->w_number : NextWindow());
1833 break;
1834 case RC_META:
1835 if (user->u_Esc == -1)
1836 break;
1837 ch = user->u_Esc;
1838 s = &ch;
1839 n = 1;
1840 LayProcess(&s, &n);
1841 break;
1842 case RC_XON:
1843 ch = Ctrl('q');
1844 s = &ch;
1845 n = 1;
1846 LayProcess(&s, &n);
1847 break;
1848 case RC_XOFF:
1849 ch = Ctrl('s');
1850 s = &ch;
1851 n = 1;
1852 LayProcess(&s, &n);
1853 break;
1854 case RC_DEFBREAKTYPE:
1855 case RC_BREAKTYPE:
1857 static char *types[] = { "TIOCSBRK", "TCSBRK", "tcsendbreak", NULL };
1858 extern int breaktype;
1860 if (*args)
1862 if (ParseNum(act, &n))
1863 for (n = 0; n < (int)(sizeof(types)/sizeof(*types)); n++)
1865 for (i = 0; i < 4; i++)
1867 ch = args[0][i];
1868 if (ch >= 'a' && ch <= 'z')
1869 ch -= 'a' - 'A';
1870 if (ch != types[n][i] && (ch + ('a' - 'A')) != types[n][i])
1871 break;
1873 if (i == 4)
1874 break;
1876 if (n < 0 || n >= (int)(sizeof(types)/sizeof(*types)))
1877 Msg(0, "%s invalid, chose one of %s, %s or %s", *args, types[0], types[1], types[2]);
1878 else
1880 breaktype = n;
1881 Msg(0, "breaktype set to (%d) %s", n, types[n]);
1884 else
1885 Msg(0, "breaktype is (%d) %s", breaktype, types[breaktype]);
1887 break;
1888 case RC_POW_BREAK:
1889 case RC_BREAK:
1890 n = 0;
1891 if (*args && ParseNum(act, &n))
1892 break;
1893 SendBreak(fore, n, nr == RC_POW_BREAK);
1894 break;
1895 #ifdef LOCK
1896 case RC_LOCKSCREEN:
1897 Detach(D_LOCK);
1898 break;
1899 #endif
1900 case RC_WIDTH:
1901 case RC_HEIGHT:
1903 int w, h;
1904 int what = 0;
1906 i = 1;
1907 if (*args && !strcmp(*args, "-w"))
1908 what = 1;
1909 else if (*args && !strcmp(*args, "-d"))
1910 what = 2;
1911 if (what)
1912 args++;
1913 if (what == 0 && flayer && !display)
1914 what = 1;
1915 if (what == 1)
1917 if (!flayer)
1919 Msg(0, "%s: %s: window required", rc_name, comms[nr].name);
1920 break;
1922 w = flayer->l_width;
1923 h = flayer->l_height;
1925 else
1927 if (!display)
1929 Msg(0, "%s: %s: display required", rc_name, comms[nr].name);
1930 break;
1932 w = D_width;
1933 h = D_height;
1935 if (*args && args[0][0] == '-')
1937 Msg(0, "%s: %s: unknown option %s", rc_name, comms[nr].name, *args);
1938 break;
1940 if (nr == RC_HEIGHT)
1942 if (!*args)
1944 #define H0height 42
1945 #define H1height 24
1946 if (h == H0height)
1947 h = H1height;
1948 else if (h == H1height)
1949 h = H0height;
1950 else if (h > (H0height + H1height) / 2)
1951 h = H0height;
1952 else
1953 h = H1height;
1955 else
1957 h = atoi(*args);
1958 if (args[1])
1959 w = atoi(args[1]);
1962 else
1964 if (!*args)
1966 if (w == Z0width)
1967 w = Z1width;
1968 else if (w == Z1width)
1969 w = Z0width;
1970 else if (w > (Z0width + Z1width) / 2)
1971 w = Z0width;
1972 else
1973 w = Z1width;
1975 else
1977 w = atoi(*args);
1978 if (args[1])
1979 h = atoi(args[1]);
1982 if (*args && args[1] && args[2])
1984 Msg(0, "%s: %s: too many arguments", rc_name, comms[nr].name);
1985 break;
1987 if (w <= 0)
1989 Msg(0, "Illegal width");
1990 break;
1992 if (h <= 0)
1994 Msg(0, "Illegal height");
1995 break;
1997 if (what == 1)
1999 if (flayer->l_width == w && flayer->l_height == h)
2000 break;
2001 ResizeLayer(flayer, w, h, (struct display *)0);
2002 break;
2004 if (D_width == w && D_height == h)
2005 break;
2006 if (what == 2)
2008 ChangeScreenSize(w, h, 1);
2010 else
2012 if (ResizeDisplay(w, h) == 0)
2014 Activate(D_fore ? D_fore->w_norefresh : 0);
2015 /* autofit */
2016 ResizeLayer(D_forecv->c_layer, D_forecv->c_xe - D_forecv->c_xs + 1, D_forecv->c_ye - D_forecv->c_ys + 1, 0);
2017 break;
2019 if (h == D_height)
2020 Msg(0, "Your termcap does not specify how to change the terminal's width to %d.", w);
2021 else if (w == D_width)
2022 Msg(0, "Your termcap does not specify how to change the terminal's height to %d.", h);
2023 else
2024 Msg(0, "Your termcap does not specify how to change the terminal's resolution to %dx%d.", w, h);
2027 break;
2028 case RC_TITLE:
2029 if (*args == 0)
2030 InputAKA();
2031 else
2032 ChangeAKA(fore, *args, strlen(*args));
2033 break;
2034 case RC_COLON:
2035 Input(":", 100, INP_EVERY, Colonfin, NULL, 0);
2036 if (*args && **args)
2038 s = *args;
2039 n = strlen(s);
2040 LayProcess(&s, &n);
2042 break;
2043 case RC_LASTMSG:
2044 if (D_status_lastmsg)
2045 Msg(0, "%s", D_status_lastmsg);
2046 break;
2047 case RC_SCREEN:
2048 DoScreen("key", args);
2049 break;
2050 case RC_WRAP:
2051 if (ParseSwitch(act, &fore->w_wrap) == 0 && msgok)
2052 Msg(0, "%cwrap", fore->w_wrap ? '+' : '-');
2053 break;
2054 case RC_FLOW:
2055 if (*args)
2057 if (args[0][0] == 'a')
2059 fore->w_flow = (fore->w_flow & FLOW_AUTO) ? FLOW_AUTOFLAG |FLOW_AUTO|FLOW_NOW : FLOW_AUTOFLAG;
2061 else
2063 if (ParseOnOff(act, &n))
2064 break;
2065 fore->w_flow = (fore->w_flow & FLOW_AUTO) | n;
2068 else
2070 if (fore->w_flow & FLOW_AUTOFLAG)
2071 fore->w_flow = (fore->w_flow & FLOW_AUTO) | FLOW_NOW;
2072 else if (fore->w_flow & FLOW_NOW)
2073 fore->w_flow &= ~FLOW_NOW;
2074 else
2075 fore->w_flow = fore->w_flow ? FLOW_AUTOFLAG|FLOW_AUTO|FLOW_NOW : FLOW_AUTOFLAG;
2077 SetFlow(fore->w_flow & FLOW_NOW);
2078 if (msgok)
2079 Msg(0, "%cflow%s", (fore->w_flow & FLOW_NOW) ? '+' : '-',
2080 (fore->w_flow & FLOW_AUTOFLAG) ? "(auto)" : "");
2081 break;
2082 #ifdef MULTIUSER
2083 case RC_DEFWRITELOCK:
2084 if (args[0][0] == 'a')
2085 nwin_default.wlock = WLOCK_AUTO;
2086 else
2088 if (ParseOnOff(act, &n))
2089 break;
2090 nwin_default.wlock = n ? WLOCK_ON : WLOCK_OFF;
2092 break;
2093 case RC_WRITELOCK:
2094 if (*args)
2096 if (args[0][0] == 'a')
2098 fore->w_wlock = WLOCK_AUTO;
2100 else
2102 if (ParseOnOff(act, &n))
2103 break;
2104 fore->w_wlock = n ? WLOCK_ON : WLOCK_OFF;
2107 * user may have permission to change the writelock setting,
2108 * but he may never aquire the lock himself without write permission
2110 if (!AclCheckPermWin(D_user, ACL_WRITE, fore))
2111 fore->w_wlockuser = D_user;
2113 Msg(0, "writelock %s", (fore->w_wlock == WLOCK_AUTO) ? "auto" :
2114 ((fore->w_wlock == WLOCK_OFF) ? "off" : "on"));
2115 break;
2116 #endif
2117 case RC_CLEAR:
2118 ResetAnsiState(fore);
2119 WriteString(fore, "\033[H\033[J", 6);
2120 break;
2121 case RC_RESET:
2122 ResetAnsiState(fore);
2123 #ifdef ZMODEM
2124 if (fore->w_zdisplay)
2125 zmodem_abort(fore, fore->w_zdisplay);
2126 #endif
2127 WriteString(fore, "\033c", 2);
2128 break;
2129 case RC_MONITOR:
2130 n = fore->w_monitor != MON_OFF;
2131 #ifdef MULTIUSER
2132 if (display)
2133 n = n && (ACLBYTE(fore->w_mon_notify, D_user->u_id) & ACLBIT(D_user->u_id));
2134 #endif
2135 if (ParseSwitch(act, &n))
2136 break;
2137 if (n)
2139 #ifdef MULTIUSER
2140 if (display) /* we tell only this user */
2141 ACLBYTE(fore->w_mon_notify, D_user->u_id) |= ACLBIT(D_user->u_id);
2142 else
2143 for (i = 0; i < maxusercount; i++)
2144 ACLBYTE(fore->w_mon_notify, i) |= ACLBIT(i);
2145 #endif
2146 if (fore->w_monitor == MON_OFF)
2147 fore->w_monitor = MON_ON;
2148 Msg(0, "Window %d (%s) is now being monitored for all activity.", fore->w_number, fore->w_title);
2150 else
2152 #ifdef MULTIUSER
2153 if (display) /* we remove only this user */
2154 ACLBYTE(fore->w_mon_notify, D_user->u_id)
2155 &= ~ACLBIT(D_user->u_id);
2156 else
2157 for (i = 0; i < maxusercount; i++)
2158 ACLBYTE(fore->w_mon_notify, i) &= ~ACLBIT(i);
2159 for (i = maxusercount - 1; i >= 0; i--)
2160 if (ACLBYTE(fore->w_mon_notify, i))
2161 break;
2162 if (i < 0)
2163 #endif
2164 fore->w_monitor = MON_OFF;
2165 Msg(0, "Window %d (%s) is no longer being monitored for activity.", fore->w_number, fore->w_title);
2167 break;
2168 #ifdef MULTI
2169 case RC_DISPLAYS:
2170 display_displays();
2171 break;
2172 #endif
2173 case RC_WINDOWLIST:
2174 if (!*args)
2175 display_wlist(0, WLIST_NUM, (struct win *)0);
2176 else if (!strcmp(*args, "string"))
2178 if (args[1])
2180 if (wliststr)
2181 free(wliststr);
2182 wliststr = SaveStr(args[1]);
2184 if (msgok)
2185 Msg(0, "windowlist string is '%s'", wliststr);
2187 else if (!strcmp(*args, "title"))
2189 if (args[1])
2191 if (wlisttit)
2192 free(wlisttit);
2193 wlisttit = SaveStr(args[1]);
2195 if (msgok)
2196 Msg(0, "windowlist title is '%s'", wlisttit);
2198 else
2200 int flag = 0;
2201 int blank = 0;
2202 for (i = 0; i < argc; i++)
2203 if (!args[i])
2204 continue;
2205 else if (!strcmp(args[i], "-m"))
2206 flag |= WLIST_MRU;
2207 else if (!strcmp(args[i], "-b"))
2208 blank = 1;
2209 else if (!strcmp(args[i], "-g"))
2210 flag |= WLIST_NESTED;
2211 else
2213 Msg(0, "usage: windowlist [-b] [-g] [-m] [string [string] | title [title]]");
2214 break;
2216 if (i == argc)
2217 display_wlist(blank, flag, (struct win *)0);
2219 break;
2220 case RC_HELP:
2221 if (argc == 2 && !strcmp(*args, "-c"))
2223 struct action *ktabp;
2224 if ((ktabp = FindKtab(args[1], 0)) == 0)
2226 Msg(0, "Unknown command class '%s'", args[1]);
2227 break;
2229 display_help(args[1], ktabp);
2231 else
2232 display_help((char *)0, ktab);
2233 break;
2234 case RC_LICENSE:
2235 display_copyright();
2236 break;
2237 #ifdef COPY_PASTE
2238 case RC_COPY:
2239 if (flayer->l_layfn != &WinLf)
2241 Msg(0, "Must be on a window layer");
2242 break;
2244 MarkRoutine();
2245 WindowChanged(fore, 'P');
2246 break;
2247 case RC_HISTORY:
2249 static char *pasteargs[] = {".", 0};
2250 static int pasteargl[] = {1};
2252 if (flayer->l_layfn != &WinLf)
2254 Msg(0, "Must be on a window layer");
2255 break;
2257 if (GetHistory() == 0)
2258 break;
2259 if (user->u_plop.buf == NULL)
2260 break;
2261 args = pasteargs;
2262 argl = pasteargl;
2264 /*FALLTHROUGH*/
2265 case RC_PASTE:
2267 char *ss, *dbuf, dch;
2268 int l = 0;
2269 # ifdef ENCODINGS
2270 int enc = -1;
2271 # endif
2274 * without args we prompt for one(!) register to be pasted in the window
2276 if ((s = *args) == NULL)
2278 Input("Paste from register:", 1, INP_RAW, ins_reg_fn, NULL, 0);
2279 break;
2281 if (args[1] == 0 && !fore) /* no window? */
2282 break;
2284 * with two arguments we paste into a destination register
2285 * (no window needed here).
2287 if (args[1] && argl[1] != 1)
2289 Msg(0, "%s: paste destination: character, ^x, or (octal) \\032 expected.",
2290 rc_name);
2291 break;
2293 # ifdef ENCODINGS
2294 else if (fore)
2295 enc = fore->w_encoding;
2296 # endif
2299 * measure length of needed buffer
2301 for (ss = s = *args; (ch = *ss); ss++)
2303 if (ch == '.')
2305 # ifdef ENCODINGS
2306 if (enc == -1)
2307 enc = user->u_plop.enc;
2308 if (enc != user->u_plop.enc)
2309 l += RecodeBuf((unsigned char *)user->u_plop.buf, user->u_plop.len, user->u_plop.enc, enc, (unsigned char *)0);
2310 else
2311 # endif
2312 l += user->u_plop.len;
2314 else
2316 # ifdef ENCODINGS
2317 if (enc == -1)
2318 enc = plop_tab[(int)(unsigned char)ch].enc;
2319 if (enc != plop_tab[(int)(unsigned char)ch].enc)
2320 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);
2321 else
2322 # endif
2323 l += plop_tab[(int)(unsigned char)ch].len;
2326 if (l == 0)
2328 Msg(0, "empty buffer");
2329 break;
2332 * shortcut:
2333 * if there is only one source and the destination is a window, then
2334 * pass a pointer rather than duplicating the buffer.
2336 if (s[1] == 0 && args[1] == 0)
2337 # ifdef ENCODINGS
2338 if (enc == (*s == '.' ? user->u_plop.enc : plop_tab[(int)(unsigned char)*s].enc))
2339 # endif
2341 MakePaster(&fore->w_paster, *s == '.' ? user->u_plop.buf : plop_tab[(int)(unsigned char)*s].buf, l, 0);
2342 break;
2345 * if no shortcut, we construct a buffer
2347 if ((dbuf = (char *)malloc(l)) == 0)
2349 Msg(0, strnomem);
2350 break;
2352 l = 0;
2354 * concatenate all sources into our own buffer, copy buffer is
2355 * special and is skipped if no display exists.
2357 for (ss = s; (ch = *ss); ss++)
2359 struct plop *pp = (ch == '.' ? &user->u_plop : &plop_tab[(int)(unsigned char)ch]);
2360 #ifdef ENCODINGS
2361 if (pp->enc != enc)
2363 l += RecodeBuf((unsigned char *)pp->buf, pp->len, pp->enc, enc, (unsigned char *)dbuf + l);
2364 continue;
2366 #endif
2367 bcopy(pp->buf, dbuf + l, pp->len);
2368 l += pp->len;
2371 * when called with one argument we paste our buffer into the window
2373 if (args[1] == 0)
2375 MakePaster(&fore->w_paster, dbuf, l, 1);
2377 else
2380 * we have two arguments, the second is already in dch.
2381 * use this as destination rather than the window.
2383 dch = args[1][0];
2384 if (dch == '.')
2386 if (user->u_plop.buf != NULL)
2387 UserFreeCopyBuffer(user);
2388 user->u_plop.buf = dbuf;
2389 user->u_plop.len = l;
2390 #ifdef ENCODINGS
2391 user->u_plop.enc = enc;
2392 #endif
2394 else
2396 struct plop *pp = plop_tab + (int)(unsigned char)dch;
2397 if (pp->buf)
2398 free(pp->buf);
2399 pp->buf = dbuf;
2400 pp->len = l;
2401 #ifdef ENCODINGS
2402 pp->enc = enc;
2403 #endif
2406 break;
2408 case RC_WRITEBUF:
2409 if (!user->u_plop.buf)
2411 Msg(0, "empty buffer");
2412 break;
2414 #ifdef ENCODINGS
2416 struct plop oldplop;
2418 oldplop = user->u_plop;
2419 if (args[0] && args[1] && !strcmp(args[0], "-e"))
2421 int enc, l;
2422 char *newbuf;
2424 enc = FindEncoding(args[1]);
2425 if (enc == -1)
2427 Msg(0, "%s: writebuf: unknown encoding", rc_name);
2428 break;
2430 if (enc != oldplop.enc)
2432 l = RecodeBuf((unsigned char *)oldplop.buf, oldplop.len, oldplop.enc, enc, (unsigned char *)0);
2433 newbuf = malloc(l + 1);
2434 if (!newbuf)
2436 Msg(0, strnomem);
2437 break;
2439 user->u_plop.len = RecodeBuf((unsigned char *)oldplop.buf, oldplop.len, oldplop.enc, enc, (unsigned char *)newbuf);
2440 user->u_plop.buf = newbuf;
2441 user->u_plop.enc = enc;
2443 args += 2;
2445 #endif
2446 if (args[0] && args[1])
2447 Msg(0, "%s: writebuf: too many arguments", rc_name);
2448 else
2449 WriteFile(user, args[0], DUMP_EXCHANGE);
2450 #ifdef ENCODINGS
2451 if (user->u_plop.buf != oldplop.buf)
2452 free(user->u_plop.buf);
2453 user->u_plop = oldplop;
2455 #endif
2456 break;
2457 case RC_READBUF:
2458 #ifdef ENCODINGS
2459 i = fore ? fore->w_encoding : display ? display->d_encoding : 0;
2460 if (args[0] && args[1] && !strcmp(args[0], "-e"))
2462 i = FindEncoding(args[1]);
2463 if (i == -1)
2465 Msg(0, "%s: readbuf: unknown encoding", rc_name);
2466 break;
2468 args += 2;
2470 #endif
2471 if (args[0] && args[1])
2473 Msg(0, "%s: readbuf: too many arguments", rc_name);
2474 break;
2476 if ((s = ReadFile(args[0] ? args[0] : BufferFile, &n)))
2478 if (user->u_plop.buf)
2479 UserFreeCopyBuffer(user);
2480 user->u_plop.len = n;
2481 user->u_plop.buf = s;
2482 #ifdef ENCODINGS
2483 user->u_plop.enc = i;
2484 #endif
2486 break;
2487 case RC_REMOVEBUF:
2488 KillBuffers();
2489 break;
2490 case RC_IGNORECASE:
2491 (void)ParseSwitch(act, &search_ic);
2492 if (msgok)
2493 Msg(0, "Will %signore case in searches", search_ic ? "" : "not ");
2494 break;
2495 #endif /* COPY_PASTE */
2496 case RC_ESCAPE:
2497 if (*argl == 0)
2498 SetEscape(user, -1, -1);
2499 else if (*argl == 2)
2500 SetEscape(user, (int)(unsigned char)args[0][0], (int)(unsigned char)args[0][1]);
2501 else
2503 Msg(0, "%s: two characters required after escape.", rc_name);
2504 break;
2506 /* Change defescape if master user. This is because we only
2507 * have one ktab.
2509 if (display && user != users)
2510 break;
2511 /* FALLTHROUGH */
2512 case RC_DEFESCAPE:
2513 if (*argl == 0)
2514 SetEscape(NULL, -1, -1);
2515 else if (*argl == 2)
2516 SetEscape(NULL, (int)(unsigned char)args[0][0], (int)(unsigned char)args[0][1]);
2517 else
2519 Msg(0, "%s: two characters required after defescape.", rc_name);
2520 break;
2522 #ifdef MAPKEYS
2523 CheckEscape();
2524 #endif
2525 break;
2526 case RC_CHDIR:
2527 s = *args ? *args : home;
2528 if (chdir(s) == -1)
2529 Msg(errno, "%s", s);
2530 break;
2531 case RC_SHELL:
2532 case RC_DEFSHELL:
2533 if (ParseSaveStr(act, &ShellProg) == 0)
2534 ShellArgs[0] = ShellProg;
2535 break;
2536 case RC_HARDCOPYDIR:
2537 if (*args)
2538 (void)ParseSaveStr(act, &hardcopydir);
2539 if (msgok)
2540 Msg(0, "hardcopydir is %s\n", hardcopydir && *hardcopydir ? hardcopydir : "<cwd>");
2541 break;
2542 case RC_LOGFILE:
2543 if (*args)
2545 if (args[1] && !(strcmp(*args, "flush")))
2547 log_flush = atoi(args[1]);
2548 if (msgok)
2549 Msg(0, "log flush timeout set to %ds\n", log_flush);
2550 break;
2552 if (ParseSaveStr(act, &screenlogfile) || !msgok)
2553 break;
2555 Msg(0, "logfile is '%s'", screenlogfile);
2556 break;
2557 case RC_LOGTSTAMP:
2558 if (!*args || !strcmp(*args, "on") || !strcmp(*args, "off"))
2560 if (ParseSwitch(act, &logtstamp_on) == 0 && msgok)
2561 Msg(0, "timestamps turned %s", logtstamp_on ? "on" : "off");
2563 else if (!strcmp(*args, "string"))
2565 if (args[1])
2567 if (logtstamp_string)
2568 free(logtstamp_string);
2569 logtstamp_string = SaveStr(args[1]);
2571 if (msgok)
2572 Msg(0, "logfile timestamp is '%s'", logtstamp_string);
2574 else if (!strcmp(*args, "after"))
2576 if (args[1])
2578 logtstamp_after = atoi(args[1]);
2579 if (!msgok)
2580 break;
2582 Msg(0, "timestamp printed after %ds\n", logtstamp_after);
2584 else
2585 Msg(0, "usage: logtstamp [after [n]|string [str]|on|off]");
2586 break;
2587 case RC_SHELLTITLE:
2588 (void)ParseSaveStr(act, &nwin_default.aka);
2589 break;
2590 case RC_TERMCAP:
2591 case RC_TERMCAPINFO:
2592 case RC_TERMINFO:
2593 if (!rc_name || !*rc_name)
2594 Msg(0, "Sorry, too late now. Place that in your .screenrc file.");
2595 break;
2596 case RC_SLEEP:
2597 break; /* Already handled */
2598 case RC_TERM:
2599 s = NULL;
2600 if (ParseSaveStr(act, &s))
2601 break;
2602 if (strlen(s) >= 20)
2604 Msg(0, "%s: term: argument too long ( < 20)", rc_name);
2605 free(s);
2606 break;
2608 strcpy(screenterm, s);
2609 free(s);
2610 debug1("screenterm set to %s\n", screenterm);
2611 MakeTermcap((display == 0));
2612 debug("new termcap made\n");
2613 break;
2614 case RC_ECHO:
2615 if (!msgok && (!rc_name || strcmp(rc_name, "-X")))
2616 break;
2618 * user typed ^A:echo... well, echo isn't FinishRc's job,
2619 * but as he wanted to test us, we show good will
2621 if (argc > 1 && !strcmp(*args, "-n"))
2623 args++;
2624 argc--;
2626 s = *args;
2627 if (argc > 1 && !strcmp(*args, "-p"))
2629 args++;
2630 argc--;
2631 s = *args;
2632 if (s)
2633 s = MakeWinMsg(s, fore, '%');
2635 if (s)
2636 Msg(0, "%s", s);
2637 else
2638 Msg(0, "%s: 'echo [-n] [-p] \"string\"' expected.", rc_name);
2639 break;
2640 case RC_BELL:
2641 case RC_BELL_MSG:
2642 if (*args == 0)
2644 char buf[256];
2645 AddXChars(buf, sizeof(buf), BellString);
2646 Msg(0, "bell_msg is '%s'", buf);
2647 break;
2649 (void)ParseSaveStr(act, &BellString);
2650 break;
2651 #ifdef COPY_PASTE
2652 case RC_BUFFERFILE:
2653 if (*args == 0)
2654 BufferFile = SaveStr(DEFAULT_BUFFERFILE);
2655 else if (ParseSaveStr(act, &BufferFile))
2656 break;
2657 if (msgok)
2658 Msg(0, "Bufferfile is now '%s'", BufferFile);
2659 break;
2660 #endif
2661 case RC_ACTIVITY:
2662 (void)ParseSaveStr(act, &ActivityString);
2663 break;
2664 #if defined(DETACH) && defined(POW_DETACH)
2665 case RC_POW_DETACH_MSG:
2666 if (*args == 0)
2668 char buf[256];
2669 AddXChars(buf, sizeof(buf), PowDetachString);
2670 Msg(0, "pow_detach_msg is '%s'", buf);
2671 break;
2673 (void)ParseSaveStr(act, &PowDetachString);
2674 break;
2675 #endif
2676 #if defined(UTMPOK) && defined(LOGOUTOK)
2677 case RC_LOGIN:
2678 n = fore->w_slot != (slot_t)-1;
2679 if (*args && !strcmp(*args, "always"))
2681 fore->w_lflag = 3;
2682 if (!displays && n)
2683 SlotToggle(n);
2684 break;
2686 if (*args && !strcmp(*args, "attached"))
2688 fore->w_lflag = 1;
2689 if (!displays && n)
2690 SlotToggle(0);
2691 break;
2693 if (ParseSwitch(act, &n) == 0)
2694 SlotToggle(n);
2695 break;
2696 case RC_DEFLOGIN:
2697 if (!strcmp(*args, "always"))
2698 nwin_default.lflag |= 2;
2699 else if (!strcmp(*args, "attached"))
2700 nwin_default.lflag &= ~2;
2701 else
2702 (void)ParseOnOff(act, &nwin_default.lflag);
2703 break;
2704 #endif
2705 case RC_DEFFLOW:
2706 if (args[0] && args[1] && args[1][0] == 'i')
2708 iflag = 1;
2709 for (display = displays; display; display = display->d_next)
2711 if (!D_flow)
2712 continue;
2713 #if defined(TERMIO) || defined(POSIX)
2714 D_NewMode.tio.c_cc[VINTR] = D_OldMode.tio.c_cc[VINTR];
2715 D_NewMode.tio.c_lflag |= ISIG;
2716 #else /* TERMIO || POSIX */
2717 D_NewMode.m_tchars.t_intrc = D_OldMode.m_tchars.t_intrc;
2718 #endif /* TERMIO || POSIX */
2719 SetTTY(D_userfd, &D_NewMode);
2722 if (args[0] && args[0][0] == 'a')
2723 nwin_default.flowflag = FLOW_AUTOFLAG;
2724 else
2725 (void)ParseOnOff(act, &nwin_default.flowflag);
2726 break;
2727 case RC_DEFWRAP:
2728 (void)ParseOnOff(act, &nwin_default.wrap);
2729 break;
2730 case RC_DEFC1:
2731 (void)ParseOnOff(act, &nwin_default.c1);
2732 break;
2733 #ifdef COLOR
2734 case RC_DEFBCE:
2735 (void)ParseOnOff(act, &nwin_default.bce);
2736 break;
2737 #endif
2738 case RC_DEFGR:
2739 (void)ParseOnOff(act, &nwin_default.gr);
2740 break;
2741 case RC_DEFMONITOR:
2742 if (ParseOnOff(act, &n) == 0)
2743 nwin_default.monitor = (n == 0) ? MON_OFF : MON_ON;
2744 break;
2745 case RC_DEFMOUSETRACK:
2746 if (ParseOnOff(act, &n) == 0)
2747 defmousetrack = (n == 0) ? 0 : 1000;
2748 break;
2749 case RC_MOUSETRACK:
2750 if (!args[0])
2752 Msg(0, "Mouse tracking for this display is turned %s", D_mousetrack ? "on" : "off");
2754 else if (ParseOnOff(act, &n) == 0)
2756 D_mousetrack = n == 0 ? 0 : 1000;
2757 if (D_fore)
2758 MouseMode(D_fore->w_mouse);
2760 break;
2761 case RC_DEFSILENCE:
2762 if (ParseOnOff(act, &n) == 0)
2763 nwin_default.silence = (n == 0) ? SILENCE_OFF : SILENCE_ON;
2764 break;
2765 case RC_VERBOSE:
2766 if (!*args)
2767 Msg(0, "W%s echo command when creating windows.",
2768 VerboseCreate ? "ill" : "on't");
2769 else if (ParseOnOff(act, &n) == 0)
2770 VerboseCreate = n;
2771 break;
2772 case RC_HARDSTATUS:
2773 if (display)
2775 Msg(0, "%s", ""); /* wait till mintime (keep gcc quiet) */
2776 RemoveStatus();
2778 if (args[0] && strcmp(args[0], "on") && strcmp(args[0], "off"))
2780 struct display *olddisplay = display;
2781 int old_use, new_use = -1;
2783 s = args[0];
2784 if (!strncmp(s, "always", 6))
2785 s += 6;
2786 if (!strcmp(s, "lastline"))
2787 new_use = HSTATUS_LASTLINE;
2788 else if (!strcmp(s, "ignore"))
2789 new_use = HSTATUS_IGNORE;
2790 else if (!strcmp(s, "message"))
2791 new_use = HSTATUS_MESSAGE;
2792 else if (!strcmp(args[0], "string"))
2794 if (!args[1])
2796 char buf[256];
2797 AddXChars(buf, sizeof(buf), hstatusstring);
2798 Msg(0, "hardstatus string is '%s'", buf);
2799 break;
2802 else
2804 Msg(0, "%s: usage: hardstatus [always]lastline|ignore|message|string [string]", rc_name);
2805 break;
2807 if (new_use != -1)
2809 hardstatusemu = new_use | (s == args[0] ? 0 : HSTATUS_ALWAYS);
2810 for (display = displays; display; display = display->d_next)
2812 RemoveStatus();
2813 new_use = hardstatusemu & ~HSTATUS_ALWAYS;
2814 if (D_HS && s == args[0])
2815 new_use = HSTATUS_HS;
2816 ShowHStatus((char *)0);
2817 old_use = D_has_hstatus;
2818 D_has_hstatus = new_use;
2819 if ((new_use == HSTATUS_LASTLINE && old_use != HSTATUS_LASTLINE) || (new_use != HSTATUS_LASTLINE && old_use == HSTATUS_LASTLINE))
2820 ChangeScreenSize(D_width, D_height, 1);
2821 RefreshHStatus();
2824 if (args[1])
2826 if (hstatusstring)
2827 free(hstatusstring);
2828 hstatusstring = SaveStr(args[1]);
2829 for (display = displays; display; display = display->d_next)
2830 RefreshHStatus();
2832 display = olddisplay;
2833 break;
2835 (void)ParseSwitch(act, &use_hardstatus);
2836 if (msgok)
2837 Msg(0, "messages displayed on %s", use_hardstatus ? "hardstatus line" : "window");
2838 break;
2839 case RC_CAPTION:
2840 if (strcmp(args[0], "always") == 0 || strcmp(args[0], "splitonly") == 0)
2842 struct display *olddisplay = display;
2844 captionalways = args[0][0] == 'a';
2845 for (display = displays; display; display = display->d_next)
2846 ChangeScreenSize(D_width, D_height, 1);
2847 display = olddisplay;
2849 else if (strcmp(args[0], "string") == 0)
2851 if (!args[1])
2853 char buf[256];
2854 AddXChars(buf, sizeof(buf), captionstring);
2855 Msg(0, "caption string is '%s'", buf);
2856 break;
2859 else
2861 Msg(0, "%s: usage: caption always|splitonly|string <string>", rc_name);
2862 break;
2864 if (!args[1])
2865 break;
2866 if (captionstring)
2867 free(captionstring);
2868 captionstring = SaveStr(args[1]);
2869 RedisplayDisplays(0);
2870 break;
2871 case RC_CONSOLE:
2872 n = (console_window != 0);
2873 if (ParseSwitch(act, &n))
2874 break;
2875 if (TtyGrabConsole(fore->w_ptyfd, n, rc_name))
2876 break;
2877 if (n == 0)
2878 Msg(0, "%s: releasing console %s", rc_name, HostName);
2879 else if (console_window)
2880 Msg(0, "%s: stealing console %s from window %d (%s)", rc_name,
2881 HostName, console_window->w_number, console_window->w_title);
2882 else
2883 Msg(0, "%s: grabbing console %s", rc_name, HostName);
2884 console_window = n ? fore : 0;
2885 break;
2886 case RC_ALLPARTIAL:
2887 if (ParseOnOff(act, &all_norefresh))
2888 break;
2889 if (!all_norefresh && fore)
2890 Activate(-1);
2891 if (msgok)
2892 Msg(0, all_norefresh ? "No refresh on window change!\n" :
2893 "Window specific refresh\n");
2894 break;
2895 case RC_PARTIAL:
2896 (void)ParseSwitch(act, &n);
2897 fore->w_norefresh = n;
2898 break;
2899 case RC_VBELL:
2900 if (ParseSwitch(act, &visual_bell) || !msgok)
2901 break;
2902 if (visual_bell == 0)
2903 Msg(0, "switched to audible bell.");
2904 else
2905 Msg(0, "switched to visual bell.");
2906 break;
2907 case RC_VBELLWAIT:
2908 if (ParseNum1000(act, &VBellWait) == 0 && msgok)
2909 Msg(0, "vbellwait set to %.10g seconds", VBellWait/1000.);
2910 break;
2911 case RC_MSGWAIT:
2912 if (ParseNum1000(act, &MsgWait) == 0 && msgok)
2913 Msg(0, "msgwait set to %.10g seconds", MsgWait/1000.);
2914 break;
2915 case RC_MSGMINWAIT:
2916 if (ParseNum1000(act, &MsgMinWait) == 0 && msgok)
2917 Msg(0, "msgminwait set to %.10g seconds", MsgMinWait/1000.);
2918 break;
2919 case RC_SILENCEWAIT:
2920 if (ParseNum(act, &SilenceWait))
2921 break;
2922 if (SilenceWait < 1)
2923 SilenceWait = 1;
2924 for (p = windows; p; p = p->w_next)
2925 p->w_silencewait = SilenceWait;
2926 if (msgok)
2927 Msg(0, "silencewait set to %d seconds", SilenceWait);
2928 break;
2929 case RC_NUMBER:
2930 if (*args == 0)
2931 Msg(0, "This is window %d (%s).\n", fore->w_number, fore->w_title);
2932 else
2934 int old = fore->w_number;
2935 int rel = 0, parse;
2936 if (args[0][0] == '+')
2937 rel = 1;
2938 else if (args[0][0] == '-')
2939 rel = -1;
2940 if (rel)
2941 ++act->args[0];
2942 parse = ParseNum(act, &n);
2943 if (rel)
2944 --act->args[0];
2945 if (parse)
2946 break;
2947 if (rel > 0)
2948 n += old;
2949 else if (rel < 0)
2950 n = old - n;
2951 if (n < 0 || n >= maxwin)
2953 Msg(0, "Given window position is invalid.");
2954 return;
2956 p = wtab[n];
2957 wtab[n] = fore;
2958 fore->w_number = n;
2959 wtab[old] = p;
2960 if (p)
2961 p->w_number = old;
2962 #ifdef MULTIUSER
2963 /* exchange the acls for these windows. */
2964 AclWinSwap(old, n);
2965 #endif
2966 #ifdef UTMPOK
2967 /* exchange the utmp-slots for these windows */
2968 if ((fore->w_slot != (slot_t) -1) && (fore->w_slot != (slot_t) 0))
2970 RemoveUtmp(fore);
2971 SetUtmp(fore);
2973 if (p && (p->w_slot != (slot_t) -1) && (p->w_slot != (slot_t) 0))
2975 /* XXX: first display wins? */
2976 display = fore->w_layer.l_cvlist ? fore->w_layer.l_cvlist->c_display : 0;
2977 RemoveUtmp(p);
2978 SetUtmp(p);
2980 #endif
2982 WindowChanged(fore, 'n');
2983 WindowChanged((struct win *)0, 'w');
2984 WindowChanged((struct win *)0, 'W');
2985 WindowChanged((struct win *)0, 0);
2987 break;
2988 case RC_SILENCE:
2989 n = fore->w_silence != 0;
2990 i = fore->w_silencewait;
2991 if (args[0] && (args[0][0] == '-' || (args[0][0] >= '0' && args[0][0] <= '9')))
2993 if (ParseNum(act, &i))
2994 break;
2995 n = i > 0;
2997 else if (ParseSwitch(act, &n))
2998 break;
2999 if (n)
3001 #ifdef MULTIUSER
3002 if (display) /* we tell only this user */
3003 ACLBYTE(fore->w_lio_notify, D_user->u_id) |= ACLBIT(D_user->u_id);
3004 else
3005 for (n = 0; n < maxusercount; n++)
3006 ACLBYTE(fore->w_lio_notify, n) |= ACLBIT(n);
3007 #endif
3008 fore->w_silencewait = i;
3009 fore->w_silence = SILENCE_ON;
3010 SetTimeout(&fore->w_silenceev, fore->w_silencewait * 1000);
3011 evenq(&fore->w_silenceev);
3013 if (!msgok)
3014 break;
3015 Msg(0, "The window is now being monitored for %d sec. silence.", fore->w_silencewait);
3017 else
3019 #ifdef MULTIUSER
3020 if (display) /* we remove only this user */
3021 ACLBYTE(fore->w_lio_notify, D_user->u_id)
3022 &= ~ACLBIT(D_user->u_id);
3023 else
3024 for (n = 0; n < maxusercount; n++)
3025 ACLBYTE(fore->w_lio_notify, n) &= ~ACLBIT(n);
3026 for (i = maxusercount - 1; i >= 0; i--)
3027 if (ACLBYTE(fore->w_lio_notify, i))
3028 break;
3029 if (i < 0)
3030 #endif
3032 fore->w_silence = SILENCE_OFF;
3033 evdeq(&fore->w_silenceev);
3035 if (!msgok)
3036 break;
3037 Msg(0, "The window is no longer being monitored for silence.");
3039 break;
3040 #ifdef COPY_PASTE
3041 case RC_DEFSCROLLBACK:
3042 (void)ParseNum(act, &nwin_default.histheight);
3043 break;
3044 case RC_SCROLLBACK:
3045 if (flayer->l_layfn == &MarkLf)
3047 Msg(0, "Cannot resize scrollback buffer in copy/scrollback mode.");
3048 break;
3050 (void)ParseNum(act, &n);
3051 ChangeWindowSize(fore, fore->w_width, fore->w_height, n);
3052 if (msgok)
3053 Msg(0, "scrollback set to %d", fore->w_histheight);
3054 break;
3055 #endif
3056 case RC_SESSIONNAME:
3057 if (*args == 0)
3058 Msg(0, "This session is named '%s'\n", SockName);
3059 else
3061 char buf[MAXPATHLEN];
3063 s = 0;
3064 if (ParseSaveStr(act, &s))
3065 break;
3066 if (!*s || strlen(s) + (SockName - SockPath) > MAXPATHLEN - 13 || index(s, '/'))
3068 Msg(0, "%s: bad session name '%s'\n", rc_name, s);
3069 free(s);
3070 break;
3072 strncpy(buf, SockPath, SockName - SockPath);
3073 sprintf(buf + (SockName - SockPath), "%d.%s", (int)getpid(), s);
3074 free(s);
3075 if ((access(buf, F_OK) == 0) || (errno != ENOENT))
3077 Msg(0, "%s: inappropriate path: '%s'.", rc_name, buf);
3078 break;
3080 if (rename(SockPath, buf))
3082 Msg(errno, "%s: failed to rename(%s, %s)", rc_name, SockPath, buf);
3083 break;
3085 debug2("rename(%s, %s) done\n", SockPath, buf);
3086 strcpy(SockPath, buf);
3087 MakeNewEnv();
3088 WindowChanged((struct win *)0, 'S');
3090 break;
3091 case RC_SETENV:
3092 if (!args[0] || !args[1])
3094 debug1("RC_SETENV arguments missing: %s\n", args[0] ? args[0] : "");
3095 InputSetenv(args[0]);
3097 else
3099 xsetenv(args[0], args[1]);
3100 MakeNewEnv();
3102 break;
3103 case RC_UNSETENV:
3104 unsetenv(*args);
3105 MakeNewEnv();
3106 break;
3107 #ifdef COPY_PASTE
3108 case RC_DEFSLOWPASTE:
3109 (void)ParseNum(act, &nwin_default.slow);
3110 break;
3111 case RC_SLOWPASTE:
3112 if (*args == 0)
3113 Msg(0, fore->w_slowpaste ?
3114 "Slowpaste in window %d is %d milliseconds." :
3115 "Slowpaste in window %d is unset.",
3116 fore->w_number, fore->w_slowpaste);
3117 else if (ParseNum(act, &fore->w_slowpaste) == 0 && msgok)
3118 Msg(0, fore->w_slowpaste ?
3119 "Slowpaste in window %d set to %d milliseconds." :
3120 "Slowpaste in window %d now unset.",
3121 fore->w_number, fore->w_slowpaste);
3122 break;
3123 case RC_MARKKEYS:
3124 if (CompileKeys(*args, *argl, mark_key_tab))
3126 Msg(0, "%s: markkeys: syntax error.", rc_name);
3127 break;
3129 debug1("markkeys %s\n", *args);
3130 break;
3131 # ifdef FONT
3132 case RC_PASTEFONT:
3133 if (ParseSwitch(act, &pastefont) == 0 && msgok)
3134 Msg(0, "Will %spaste font settings", pastefont ? "" : "not ");
3135 break;
3136 # endif
3137 case RC_CRLF:
3138 (void)ParseSwitch(act, &join_with_cr);
3139 break;
3140 case RC_COMPACTHIST:
3141 if (ParseSwitch(act, &compacthist) == 0 && msgok)
3142 Msg(0, "%scompacting history lines", compacthist ? "" : "not ");
3143 break;
3144 #endif
3145 #ifdef NETHACK
3146 case RC_NETHACK:
3147 (void)ParseOnOff(act, &nethackflag);
3148 break;
3149 #endif
3150 case RC_HARDCOPY_APPEND:
3151 (void)ParseOnOff(act, &hardcopy_append);
3152 break;
3153 case RC_VBELL_MSG:
3154 if (*args == 0)
3156 char buf[256];
3157 AddXChars(buf, sizeof(buf), VisualBellString);
3158 Msg(0, "vbell_msg is '%s'", buf);
3159 break;
3161 (void)ParseSaveStr(act, &VisualBellString);
3162 debug1(" new vbellstr '%s'\n", VisualBellString);
3163 break;
3164 case RC_DEFMODE:
3165 if (ParseBase(act, *args, &n, 8, "octal"))
3166 break;
3167 if (n < 0 || n > 0777)
3169 Msg(0, "%s: mode: Invalid tty mode %o", rc_name, n);
3170 break;
3172 TtyMode = n;
3173 if (msgok)
3174 Msg(0, "Ttymode set to %03o", TtyMode);
3175 break;
3176 case RC_AUTODETACH:
3177 (void)ParseOnOff(act, &auto_detach);
3178 break;
3179 case RC_STARTUP_MESSAGE:
3180 (void)ParseOnOff(act, &default_startup);
3181 break;
3182 #ifdef PASSWORD
3183 case RC_PASSWORD:
3184 if (*args)
3186 n = (*user->u_password) ? 1 : 0;
3187 if (user->u_password != NullStr) free((char *)user->u_password);
3188 user->u_password = SaveStr(*args);
3189 if (!strcmp(user->u_password, "none"))
3191 if (n)
3192 Msg(0, "Password checking disabled");
3193 free(user->u_password);
3194 user->u_password = NullStr;
3197 else
3199 if (!fore)
3201 Msg(0, "%s: password: window required", rc_name);
3202 break;
3204 Input("New screen password:", 100, INP_NOECHO, pass1, display ? (char *)D_user : (char *)users, 0);
3206 break;
3207 #endif /* PASSWORD */
3208 case RC_BIND:
3210 struct action *ktabp = ktab;
3211 int kflag = 0;
3213 for (;;)
3215 if (argc > 2 && !strcmp(*args, "-c"))
3217 ktabp = FindKtab(args[1], 1);
3218 if (ktabp == 0)
3219 break;
3220 args += 2;
3221 argl += 2;
3222 argc -= 2;
3224 else if (argc > 1 && !strcmp(*args, "-k"))
3226 kflag = 1;
3227 args++;
3228 argl++;
3229 argc--;
3231 else
3232 break;
3234 #ifdef MAPKEYS
3235 if (kflag)
3237 for (n = 0; n < KMAP_KEYS; n++)
3238 if (strcmp(term[n + T_CAPS].tcname, *args) == 0)
3239 break;
3240 if (n == KMAP_KEYS)
3242 Msg(0, "%s: bind: unknown key '%s'", rc_name, *args);
3243 break;
3245 n += 256;
3247 else
3248 #endif
3249 if (*argl != 1)
3251 Msg(0, "%s: bind: character, ^x, or (octal) \\032 expected.", rc_name);
3252 break;
3254 else
3255 n = (unsigned char)args[0][0];
3257 if (args[1])
3259 if ((i = FindCommnr(args[1])) == RC_ILLEGAL)
3261 Msg(0, "%s: bind: unknown command '%s'", rc_name, args[1]);
3262 break;
3264 if (CheckArgNum(i, args + 2) < 0)
3265 break;
3266 ClearAction(&ktabp[n]);
3267 SaveAction(ktabp + n, i, args + 2, argl + 2);
3269 else
3270 ClearAction(&ktabp[n]);
3272 break;
3273 #ifdef MAPKEYS
3274 case RC_BINDKEY:
3276 struct action *newact;
3277 int newnr, fl = 0, kf = 0, af = 0, df = 0, mf = 0;
3278 struct display *odisp = display;
3279 int used = 0;
3280 struct kmap_ext *kme;
3282 for (; *args && **args == '-'; args++, argl++)
3284 if (strcmp(*args, "-t") == 0)
3285 fl = KMAP_NOTIMEOUT;
3286 else if (strcmp(*args, "-k") == 0)
3287 kf = 1;
3288 else if (strcmp(*args, "-a") == 0)
3289 af = 1;
3290 else if (strcmp(*args, "-d") == 0)
3291 df = 1;
3292 else if (strcmp(*args, "-m") == 0)
3293 mf = 1;
3294 else if (strcmp(*args, "--") == 0)
3296 args++;
3297 argl++;
3298 break;
3300 else
3302 Msg(0, "%s: bindkey: invalid option %s", rc_name, *args);
3303 return;
3306 if (df && mf)
3308 Msg(0, "%s: bindkey: -d does not work with -m", rc_name);
3309 break;
3311 if (*args == 0)
3313 if (mf)
3314 display_bindkey("Edit mode", mmtab);
3315 else if (df)
3316 display_bindkey("Default", dmtab);
3317 else
3318 display_bindkey("User", umtab);
3319 break;
3321 if (kf == 0)
3323 if (af)
3325 Msg(0, "%s: bindkey: -a only works with -k", rc_name);
3326 break;
3328 if (*argl == 0)
3330 Msg(0, "%s: bindkey: empty string makes no sense", rc_name);
3331 break;
3333 for (i = 0, kme = kmap_exts; i < kmap_extn; i++, kme++)
3334 if (kme->str == 0)
3336 if (args[1])
3337 break;
3339 else
3340 if (*argl == (kme->fl & ~KMAP_NOTIMEOUT) && bcmp(kme->str, *args, *argl) == 0)
3341 break;
3342 if (i == kmap_extn)
3344 if (!args[1])
3346 Msg(0, "%s: bindkey: keybinding not found", rc_name);
3347 break;
3349 kmap_extn += 8;
3350 kmap_exts = (struct kmap_ext *)xrealloc((char *)kmap_exts, kmap_extn * sizeof(*kmap_exts));
3351 kme = kmap_exts + i;
3352 bzero((char *)kme, 8 * sizeof(*kmap_exts));
3353 for (; i < kmap_extn; i++, kme++)
3355 kme->str = 0;
3356 kme->dm.nr = kme->mm.nr = kme->um.nr = RC_ILLEGAL;
3357 kme->dm.args = kme->mm.args = kme->um.args = noargs;
3358 kme->dm.argl = kme->mm.argl = kme->um.argl = 0;
3360 i -= 8;
3361 kme -= 8;
3363 if (df == 0 && kme->dm.nr != RC_ILLEGAL)
3364 used = 1;
3365 if (mf == 0 && kme->mm.nr != RC_ILLEGAL)
3366 used = 1;
3367 if ((df || mf) && kme->um.nr != RC_ILLEGAL)
3368 used = 1;
3369 i += KMAP_KEYS + KMAP_AKEYS;
3370 newact = df ? &kme->dm : mf ? &kme->mm : &kme->um;
3372 else
3374 for (i = T_CAPS; i < T_OCAPS; i++)
3375 if (strcmp(term[i].tcname, *args) == 0)
3376 break;
3377 if (i == T_OCAPS)
3379 Msg(0, "%s: bindkey: unknown key '%s'", rc_name, *args);
3380 break;
3382 if (af && i >= T_CURSOR && i < T_OCAPS)
3383 i -= T_CURSOR - KMAP_KEYS;
3384 else
3385 i -= T_CAPS;
3386 newact = df ? &dmtab[i] : mf ? &mmtab[i] : &umtab[i];
3388 if (args[1])
3390 if ((newnr = FindCommnr(args[1])) == RC_ILLEGAL)
3392 Msg(0, "%s: bindkey: unknown command '%s'", rc_name, args[1]);
3393 break;
3395 if (CheckArgNum(newnr, args + 2) < 0)
3396 break;
3397 ClearAction(newact);
3398 SaveAction(newact, newnr, args + 2, argl + 2);
3399 if (kf == 0 && args[1])
3401 if (kme->str)
3402 free(kme->str);
3403 kme->str = SaveStrn(*args, *argl);
3404 kme->fl = fl | *argl;
3407 else
3408 ClearAction(newact);
3409 for (display = displays; display; display = display->d_next)
3410 remap(i, args[1] ? 1 : 0);
3411 if (kf == 0 && !args[1])
3413 if (!used && kme->str)
3415 free(kme->str);
3416 kme->str = 0;
3417 kme->fl = 0;
3420 display = odisp;
3422 break;
3423 case RC_MAPTIMEOUT:
3424 if (*args)
3426 if (ParseNum(act, &n))
3427 break;
3428 if (n < 0)
3430 Msg(0, "%s: maptimeout: illegal time %d", rc_name, n);
3431 break;
3433 maptimeout = n;
3435 if (*args == 0 || msgok)
3436 Msg(0, "maptimeout is %dms", maptimeout);
3437 break;
3438 case RC_MAPNOTNEXT:
3439 D_dontmap = 1;
3440 break;
3441 case RC_MAPDEFAULT:
3442 D_mapdefault = 1;
3443 break;
3444 #endif
3445 #ifdef MULTIUSER
3446 case RC_ACLCHG:
3447 case RC_ACLADD:
3448 case RC_ADDACL:
3449 case RC_CHACL:
3450 UsersAcl(NULL, argc, args);
3451 break;
3452 case RC_ACLDEL:
3453 if (UserDel(args[0], NULL))
3454 break;
3455 if (msgok)
3456 Msg(0, "%s removed from acl database", args[0]);
3457 break;
3458 case RC_ACLGRP:
3460 * modify a user to gain or lose rights granted to a group.
3461 * This group is actually a normal user whose rights were defined
3462 * with chacl in the usual way.
3464 if (args[1])
3466 if (strcmp(args[1], "none")) /* link a user to another user */
3468 if (AclLinkUser(args[0], args[1]))
3469 break;
3470 if (msgok)
3471 Msg(0, "User %s joined acl-group %s", args[0], args[1]);
3473 else /* remove all groups from user */
3475 struct acluser *u;
3476 struct aclusergroup *g;
3478 if (!(u = *FindUserPtr(args[0])))
3479 break;
3480 while ((g = u->u_group))
3482 u->u_group = g->next;
3483 free((char *)g);
3487 else /* show all groups of user */
3489 char buf[256], *p = buf;
3490 int ngroups = 0;
3491 struct acluser *u;
3492 struct aclusergroup *g;
3494 if (!(u = *FindUserPtr(args[0])))
3496 if (msgok)
3497 Msg(0, "User %s does not exist.", args[0]);
3498 break;
3500 g = u->u_group;
3501 while (g)
3503 ngroups++;
3504 sprintf(p, "%s ", g->u->u_name);
3505 p += strlen(p);
3506 if (p > buf+200)
3507 break;
3508 g = g->next;
3510 if (ngroups)
3511 *(--p) = '\0';
3512 Msg(0, "%s's group%s: %s.", args[0], (ngroups == 1) ? "" : "s",
3513 (ngroups == 0) ? "none" : buf);
3515 break;
3516 case RC_ACLUMASK:
3517 case RC_UMASK:
3518 while ((s = *args++))
3520 char *err = 0;
3522 if (AclUmask(display ? D_user : users, s, &err))
3523 Msg(0, "umask: %s\n", err);
3525 break;
3526 case RC_MULTIUSER:
3527 if (ParseOnOff(act, &n))
3528 break;
3529 multi = n ? "" : 0;
3530 chsock();
3531 if (msgok)
3532 Msg(0, "Multiuser mode %s", multi ? "enabled" : "disabled");
3533 break;
3534 #endif /* MULTIUSER */
3535 #ifdef PSEUDOS
3536 case RC_EXEC:
3537 winexec(args);
3538 break;
3539 #endif
3540 #ifdef MULTI
3541 case RC_NONBLOCK:
3542 i = D_nonblock >= 0;
3543 if (*args && ((args[0][0] >= '0' && args[0][0] <= '9') || args[0][0] == '.'))
3545 if (ParseNum1000(act, &i))
3546 break;
3548 else if (!ParseSwitch(act, &i))
3549 i = i == 0 ? -1 : 1000;
3550 else
3551 break;
3552 if (msgok && i == -1)
3553 Msg(0, "display set to blocking mode");
3554 else if (msgok && i == 0)
3555 Msg(0, "display set to nonblocking mode, no timeout");
3556 else if (msgok)
3557 Msg(0, "display set to nonblocking mode, %.10gs timeout", i/1000.);
3558 D_nonblock = i;
3559 if (D_nonblock <= 0)
3560 evdeq(&D_blockedev);
3561 break;
3562 case RC_DEFNONBLOCK:
3563 if (*args && ((args[0][0] >= '0' && args[0][0] <= '9') || args[0][0] == '.'))
3565 if (ParseNum1000(act, &defnonblock))
3566 break;
3568 else if (!ParseOnOff(act, &defnonblock))
3569 defnonblock = defnonblock == 0 ? -1 : 1000;
3570 else
3571 break;
3572 if (display && *rc_name)
3574 D_nonblock = defnonblock;
3575 if (D_nonblock <= 0)
3576 evdeq(&D_blockedev);
3578 break;
3579 #endif
3580 case RC_GR:
3581 #ifdef ENCODINGS
3582 if (fore->w_gr == 2)
3583 fore->w_gr = 0;
3584 #endif
3585 if (ParseSwitch(act, &fore->w_gr) == 0 && msgok)
3586 Msg(0, "Will %suse GR", fore->w_gr ? "" : "not ");
3587 #ifdef ENCODINGS
3588 if (fore->w_gr == 0 && fore->w_FontE)
3589 fore->w_gr = 2;
3590 #endif
3591 break;
3592 case RC_C1:
3593 if (ParseSwitch(act, &fore->w_c1) == 0 && msgok)
3594 Msg(0, "Will %suse C1", fore->w_c1 ? "" : "not ");
3595 break;
3596 #ifdef COLOR
3597 case RC_BCE:
3598 if (ParseSwitch(act, &fore->w_bce) == 0 && msgok)
3599 Msg(0, "Will %serase with background color", fore->w_bce ? "" : "not ");
3600 break;
3601 #endif
3602 #ifdef ENCODINGS
3603 case RC_KANJI:
3604 case RC_ENCODING:
3605 #ifdef UTF8
3606 if (*args && !strcmp(args[0], "-d"))
3608 if (!args[1])
3609 Msg(0, "encodings directory is %s", screenencodings ? screenencodings : "<unset>");
3610 else
3612 free(screenencodings);
3613 screenencodings = SaveStr(args[1]);
3615 break;
3617 if (*args && !strcmp(args[0], "-l"))
3619 if (!args[1])
3620 Msg(0, "encoding: -l: argument required");
3621 else if (LoadFontTranslation(-1, args[1]))
3622 Msg(0, "encoding: could not load utf8 encoding file");
3623 else if (msgok)
3624 Msg(0, "encoding: utf8 encoding file loaded");
3625 break;
3627 #else
3628 if (*args && (!strcmp(args[0], "-l") || !strcmp(args[0], "-d")))
3630 if (msgok)
3631 Msg(0, "encoding: screen is not compiled for UTF-8.");
3632 break;
3634 #endif
3635 for (i = 0; i < 2; i++)
3637 if (args[i] == 0)
3638 break;
3639 if (!strcmp(args[i], "."))
3640 continue;
3641 n = FindEncoding(args[i]);
3642 if (n == -1)
3644 Msg(0, "encoding: unknown encoding '%s'", args[i]);
3645 break;
3647 if (i == 0 && fore)
3649 WinSwitchEncoding(fore, n);
3650 ResetCharsets(fore);
3652 else if (i && display)
3653 D_encoding = n;
3655 break;
3656 case RC_DEFKANJI:
3657 case RC_DEFENCODING:
3658 n = FindEncoding(*args);
3659 if (n == -1)
3661 Msg(0, "defencoding: unknown encoding '%s'", *args);
3662 break;
3664 nwin_default.encoding = n;
3665 break;
3666 #endif
3668 #ifdef UTF8
3669 case RC_DEFUTF8:
3670 n = nwin_default.encoding == UTF8;
3671 if (ParseSwitch(act, &n) == 0)
3673 nwin_default.encoding = n ? UTF8 : 0;
3674 if (msgok)
3675 Msg(0, "Will %suse UTF-8 encoding for new windows", n ? "" : "not ");
3677 break;
3678 case RC_UTF8:
3679 for (i = 0; i < 2; i++)
3681 if (i && args[i] == 0)
3682 break;
3683 if (args[i] == 0)
3684 n = fore->w_encoding != UTF8;
3685 else if (strcmp(args[i], "off") == 0)
3686 n = 0;
3687 else if (strcmp(args[i], "on") == 0)
3688 n = 1;
3689 else
3691 Msg(0, "utf8: illegal argument (%s)", args[i]);
3692 break;
3694 if (i == 0)
3696 WinSwitchEncoding(fore, n ? UTF8 : 0);
3697 if (msgok)
3698 Msg(0, "Will %suse UTF-8 encoding", n ? "" : "not ");
3700 else if (display)
3701 D_encoding = n ? UTF8 : 0;
3702 if (args[i] == 0)
3703 break;
3705 break;
3706 #endif
3708 case RC_PRINTCMD:
3709 if (*args)
3711 if (printcmd)
3712 free(printcmd);
3713 printcmd = 0;
3714 if (**args)
3715 printcmd = SaveStr(*args);
3717 if (*args == 0 || msgok)
3719 if (printcmd)
3720 Msg(0, "using '%s' as print command", printcmd);
3721 else
3722 Msg(0, "using termcap entries for printing");
3723 break;
3725 break;
3727 case RC_DIGRAPH:
3728 if (argl && argl[0] > 0 && argl[1] > 0)
3730 if (argl[0] != 2)
3732 Msg(0, "Two characters expected to define a digraph");
3733 break;
3735 i = digraph_find(args[0]);
3736 digraphs[i].d[0] = args[0][0];
3737 digraphs[i].d[1] = args[0][1];
3738 if (!parse_input_int(args[1], argl[1], &digraphs[i].value))
3740 if (!(digraphs[i].value = atoi(args[1])))
3742 if (!args[1][1])
3743 digraphs[i].value = (int)args[1][0];
3744 #ifdef UTF8
3745 else
3747 int t;
3748 unsigned char *s = args[1];
3749 digraphs[i].value = 0;
3750 while (*s)
3752 t = FromUtf8(*s++, &digraphs[i].value);
3753 if (t == -1)
3754 continue;
3755 if (t == -2)
3756 digraphs[i].value = 0;
3757 else
3758 digraphs[i].value = t;
3759 break;
3762 #endif
3765 break;
3767 Input("Enter digraph: ", 10, INP_EVERY, digraph_fn, NULL, 0);
3768 if (*args && **args)
3770 s = *args;
3771 n = strlen(s);
3772 LayProcess(&s, &n);
3774 break;
3776 case RC_DEFHSTATUS:
3777 if (*args == 0)
3779 char buf[256];
3780 *buf = 0;
3781 if (nwin_default.hstatus)
3782 AddXChars(buf, sizeof(buf), nwin_default.hstatus);
3783 Msg(0, "default hstatus is '%s'", buf);
3784 break;
3786 (void)ParseSaveStr(act, &nwin_default.hstatus);
3787 if (*nwin_default.hstatus == 0)
3789 free(nwin_default.hstatus);
3790 nwin_default.hstatus = 0;
3792 break;
3793 case RC_HSTATUS:
3794 (void)ParseSaveStr(act, &fore->w_hstatus);
3795 if (*fore->w_hstatus == 0)
3797 free(fore->w_hstatus);
3798 fore->w_hstatus = 0;
3800 WindowChanged(fore, 'h');
3801 break;
3803 #ifdef FONT
3804 case RC_DEFCHARSET:
3805 case RC_CHARSET:
3806 if (*args == 0)
3808 char buf[256];
3809 *buf = 0;
3810 if (nwin_default.charset)
3811 AddXChars(buf, sizeof(buf), nwin_default.charset);
3812 Msg(0, "default charset is '%s'", buf);
3813 break;
3815 n = strlen(*args);
3816 if (n == 0 || n > 6)
3818 Msg(0, "%s: %s: string has illegal size.", rc_name, comms[nr].name);
3819 break;
3821 if (n > 4 && (
3822 ((args[0][4] < '0' || args[0][4] > '3') && args[0][4] != '.') ||
3823 ((args[0][5] < '0' || args[0][5] > '3') && args[0][5] && args[0][5] != '.')))
3825 Msg(0, "%s: %s: illegal mapping number.", rc_name, comms[nr].name);
3826 break;
3828 if (nr == RC_CHARSET)
3830 SetCharsets(fore, *args);
3831 break;
3833 if (nwin_default.charset)
3834 free(nwin_default.charset);
3835 nwin_default.charset = SaveStr(*args);
3836 break;
3837 #endif
3838 #ifdef COLOR
3839 case RC_ATTRCOLOR:
3840 s = args[0];
3841 if (*s >= '0' && *s <= '9')
3842 i = *s - '0';
3843 else
3844 for (i = 0; i < 8; i++)
3845 if (*s == "dubrsBiI"[i])
3846 break;
3847 s++;
3848 nr = 0;
3849 if (*s && s[1] && !s[2])
3851 if (*s == 'd' && s[1] == 'd')
3852 nr = 3;
3853 else if (*s == '.' && s[1] == 'd')
3854 nr = 2;
3855 else if (*s == 'd' && s[1] == '.')
3856 nr = 1;
3857 else if (*s != '.' || s[1] != '.')
3858 s--;
3859 s += 2;
3861 if (*s || i < 0 || i >= 8)
3863 Msg(0, "%s: attrcolor: unknown attribute '%s'.", rc_name, args[0]);
3864 break;
3866 n = 0;
3867 if (args[1])
3868 n = ParseAttrColor(args[1], args[2], 1);
3869 if (n == -1)
3870 break;
3871 attr2color[i][nr] = n;
3872 n = 0;
3873 for (i = 0; i < 8; i++)
3874 if (attr2color[i][0] || attr2color[i][1] || attr2color[i][2] || attr2color[i][3])
3875 n |= 1 << i;
3876 nattr2color = n;
3877 break;
3878 #endif
3879 case RC_RENDITION:
3880 i = -1;
3881 if (strcmp(args[0], "bell") == 0)
3883 i = REND_BELL;
3885 else if (strcmp(args[0], "monitor") == 0)
3887 i = REND_MONITOR;
3889 else if (strcmp(args[0], "so") != 0)
3891 Msg(0, "Invalid option '%s' for rendition", args[0]);
3892 break;
3895 ++args;
3896 ++argl;
3898 if (i != -1)
3900 renditions[i] = ParseAttrColor(args[0], args[1], 1);
3901 WindowChanged((struct win *)0, 'w');
3902 WindowChanged((struct win *)0, 'W');
3903 WindowChanged((struct win *)0, 0);
3904 break;
3907 /* We are here, means we want to set the sorendition. */
3908 /* FALLTHROUGH*/
3909 case RC_SORENDITION:
3910 i = 0;
3911 if (*args)
3913 i = ParseAttrColor(*args, args[1], 1);
3914 if (i == -1)
3915 break;
3916 ApplyAttrColor(i, &mchar_so);
3917 WindowChanged((struct win *)0, 0);
3918 debug2("--> %x %x\n", mchar_so.attr, mchar_so.color);
3920 if (msgok)
3921 #ifdef COLOR
3922 Msg(0, "Standout attributes 0x%02x color 0x%02x", (unsigned char)mchar_so.attr, 0x99 ^ (unsigned char)mchar_so.color);
3923 #else
3924 Msg(0, "Standout attributes 0x%02x ", (unsigned char)mchar_so.attr);
3925 #endif
3926 break;
3928 case RC_SOURCE:
3929 do_source(*args);
3930 break;
3932 #ifdef MULTIUSER
3933 case RC_SU:
3934 s = NULL;
3935 if (!*args)
3937 Msg(0, "%s:%s screen login", HostName, SockPath);
3938 InputSu(D_fore, &D_user, NULL);
3940 else if (!args[1])
3941 InputSu(D_fore, &D_user, args[0]);
3942 else if (!args[2])
3943 s = DoSu(&D_user, args[0], args[1], "\377");
3944 else
3945 s = DoSu(&D_user, args[0], args[1], args[2]);
3946 if (s)
3947 Msg(0, "%s", s);
3948 break;
3949 #endif /* MULTIUSER */
3950 case RC_SPLIT:
3951 s = args[0];
3952 if (s && !strcmp(s, "-v"))
3953 AddCanvas(SLICE_HORI);
3954 else
3955 AddCanvas(SLICE_VERT);
3956 Activate(-1);
3957 break;
3958 case RC_REMOVE:
3959 RemCanvas();
3960 Activate(-1);
3961 break;
3962 case RC_ONLY:
3963 OneCanvas();
3964 Activate(-1);
3965 break;
3966 case RC_FIT:
3967 D_forecv->c_xoff = D_forecv->c_xs;
3968 D_forecv->c_yoff = D_forecv->c_ys;
3969 RethinkViewportOffsets(D_forecv);
3970 ResizeLayer(D_forecv->c_layer, D_forecv->c_xe - D_forecv->c_xs + 1, D_forecv->c_ye - D_forecv->c_ys + 1, 0);
3971 flayer = D_forecv->c_layer;
3972 LaySetCursor();
3973 break;
3974 case RC_FOCUS:
3976 struct canvas *cv = 0;
3977 if (!*args || !strcmp(*args, "next"))
3978 cv = D_forecv->c_next ? D_forecv->c_next : D_cvlist;
3979 else if (!strcmp(*args, "prev"))
3981 for (cv = D_cvlist; cv->c_next && cv->c_next != D_forecv; cv = cv->c_next)
3984 else if (!strcmp(*args, "top"))
3985 cv = D_cvlist;
3986 else if (!strcmp(*args, "bottom"))
3988 for (cv = D_cvlist; cv->c_next; cv = cv->c_next)
3991 else if (!strcmp(*args, "up"))
3992 cv = FindCanvas(D_forecv->c_xs, D_forecv->c_ys - 1);
3993 else if (!strcmp(*args, "down"))
3994 cv = FindCanvas(D_forecv->c_xs, D_forecv->c_ye + 2);
3995 else if (!strcmp(*args, "left"))
3996 cv = FindCanvas(D_forecv->c_xs - 1, D_forecv->c_ys);
3997 else if (!strcmp(*args, "right"))
3998 cv = FindCanvas(D_forecv->c_xe + 1, D_forecv->c_ys);
3999 else
4001 Msg(0, "%s: usage: focus [next|prev|up|down|left|right|top|bottom]", rc_name);
4002 break;
4004 SetForeCanvas(display, cv);
4006 break;
4007 case RC_RESIZE:
4008 i = 0;
4009 if (D_forecv->c_slorient == SLICE_UNKN)
4011 Msg(0, "resize: need more than one region");
4012 break;
4014 for (; *args; args++)
4016 if (!strcmp(*args, "-h"))
4017 i |= RESIZE_FLAG_H;
4018 else if (!strcmp(*args, "-v"))
4019 i |= RESIZE_FLAG_V;
4020 else if (!strcmp(*args, "-b"))
4021 i |= RESIZE_FLAG_H | RESIZE_FLAG_V;
4022 else if (!strcmp(*args, "-p"))
4023 i |= D_forecv->c_slorient == SLICE_VERT ? RESIZE_FLAG_H : RESIZE_FLAG_V;
4024 else if (!strcmp(*args, "-l"))
4025 i |= RESIZE_FLAG_L;
4026 else
4027 break;
4029 if (*args && args[1])
4031 Msg(0, "%s: usage: resize [-h] [-v] [-l] [num]\n", rc_name);
4032 break;
4034 if (*args)
4035 ResizeRegions(*args, i);
4036 else
4037 Input(resizeprompts[i], 20, INP_EVERY, ResizeFin, (char*)0, i);
4038 break;
4039 case RC_SETSID:
4040 (void)ParseSwitch(act, &separate_sids);
4041 break;
4042 case RC_EVAL:
4043 args = SaveArgs(args);
4044 for (i = 0; args[i]; i++)
4046 if (args[i][0])
4047 Colonfin(args[i], strlen(args[i]), (char *)0);
4048 free(args[i]);
4050 free(args);
4051 break;
4052 case RC_ALTSCREEN:
4053 (void)ParseSwitch(act, &use_altscreen);
4054 if (msgok)
4055 Msg(0, "Will %sdo alternate screen switching", use_altscreen ? "" : "not ");
4056 break;
4057 case RC_MAXWIN:
4058 if (!args[0])
4060 Msg(0, "maximum windows allowed: %d", maxwin);
4061 break;
4063 if (ParseNum(act, &n))
4064 break;
4065 if (n < 1)
4066 Msg(0, "illegal maxwin number specified");
4067 else if (n > 2048)
4068 Msg(0, "maximum 2048 windows allowed");
4069 else if (n > maxwin && windows)
4070 Msg(0, "may increase maxwin only when there's no window");
4071 else
4073 if (!windows)
4074 wtab = realloc(wtab, n * sizeof(struct win));
4075 maxwin = n;
4077 break;
4078 case RC_BACKTICK:
4079 if (ParseBase(act, *args, &n, 10, "decimal"))
4080 break;
4081 if (!args[1])
4082 setbacktick(n, 0, 0, (char **)0);
4083 else
4085 int lifespan, tick;
4086 if (argc < 4)
4088 Msg(0, "%s: usage: backtick num [lifespan tick cmd args...]", rc_name);
4089 break;
4091 if (ParseBase(act, args[1], &lifespan, 10, "decimal"))
4092 break;
4093 if (ParseBase(act, args[2], &tick, 10, "decimal"))
4094 break;
4095 setbacktick(n, lifespan, tick, SaveArgs(args + 3));
4097 WindowChanged(0, '`');
4098 break;
4099 case RC_BLANKER:
4100 #ifdef BLANKER_PRG
4101 if (blankerprg)
4103 RunBlanker(blankerprg);
4104 break;
4106 #endif
4107 ClearAll();
4108 CursorVisibility(-1);
4109 D_blocked = 4;
4110 break;
4111 #ifdef BLANKER_PRG
4112 case RC_BLANKERPRG:
4113 if (!args[0])
4115 if (blankerprg)
4117 char path[MAXPATHLEN];
4118 char *p = path, **pp;
4119 for (pp = blankerprg; *pp; pp++)
4120 p += snprintf(p, sizeof(path) - (p - path) - 1, "%s ", *pp);
4121 *(p - 1) = '\0';
4122 Msg(0, "blankerprg: %s", path);
4124 else
4125 Msg(0, "No blankerprg set.");
4126 break;
4128 if (blankerprg)
4130 char **pp;
4131 for (pp = blankerprg; *pp; pp++)
4132 free(*pp);
4133 free(blankerprg);
4134 blankerprg = 0;
4136 if (args[0][0])
4137 blankerprg = SaveArgs(args);
4138 break;
4139 #endif
4140 case RC_IDLE:
4141 if (*args)
4143 struct display *olddisplay = display;
4144 if (!strcmp(*args, "off"))
4145 idletimo = 0;
4146 else if (args[0][0])
4147 idletimo = atoi(*args) * 1000;
4148 if (argc > 1)
4150 if ((i = FindCommnr(args[1])) == RC_ILLEGAL)
4152 Msg(0, "%s: idle: unknown command '%s'", rc_name, args[1]);
4153 break;
4155 if (CheckArgNum(i, args + 2) < 0)
4156 break;
4157 ClearAction(&idleaction);
4158 SaveAction(&idleaction, i, args + 2, argl + 2);
4160 for (display = displays; display; display = display->d_next)
4161 ResetIdle();
4162 display = olddisplay;
4164 if (msgok)
4166 if (idletimo)
4167 Msg(0, "idle timeout %ds, %s", idletimo / 1000, comms[idleaction.nr].name);
4168 else
4169 Msg(0, "idle off");
4171 break;
4172 case RC_FOCUSMINSIZE:
4173 for (i = 0; i < 2 && args[i]; i++)
4175 if (!strcmp(args[i], "max") || !strcmp(args[i], "_"))
4176 n = -1;
4177 else
4178 n = atoi(args[i]);
4179 if (i == 0)
4180 focusminwidth = n;
4181 else
4182 focusminheight = n;
4184 if (msgok)
4186 char b[2][20];
4187 for (i = 0; i < 2; i++)
4189 n = i == 0 ? focusminwidth : focusminheight;
4190 if (n == -1)
4191 strcpy(b[i], "max");
4192 else
4193 sprintf(b[i], "%d", n);
4195 Msg(0, "focus min size is %s %s\n", b[0], b[1]);
4197 break;
4198 case RC_GROUP:
4199 if (*args)
4201 fore->w_group = 0;
4202 if (args[0][0])
4204 fore->w_group = WindowByName(*args);
4205 if (fore->w_group && fore->w_group->w_type != W_TYPE_GROUP)
4206 fore->w_group = 0;
4208 WindowChanged((struct win *)0, 'w');
4209 WindowChanged((struct win *)0, 'W');
4210 WindowChanged((struct win *)0, 0);
4212 if (msgok)
4214 if (fore->w_group)
4215 Msg(0, "window group is %d (%s)\n", fore->w_group->w_number, fore->w_group->w_title);
4216 else
4217 Msg(0, "window belongs to no group");
4219 break;
4220 case RC_LAYOUT:
4221 if (!strcmp(args[0], "title"))
4223 if (!D_layout)
4225 Msg(0, "not on a layout");
4226 break;
4228 if (!args[1])
4230 Msg(0, "current layout is %d (%s)", D_layout->lay_number, D_layout->lay_title);
4231 break;
4233 free(D_layout->lay_title);
4234 D_layout->lay_title= SaveStr(args[1]);
4236 else if (!strcmp(args[0], "number"))
4238 int old;
4239 struct layout *lay;
4240 if (!D_layout)
4242 Msg(0, "not on a layout");
4243 break;
4245 if (!args[1])
4247 Msg(0, "This is layout %d (%s).\n", D_layout->lay_number, D_layout->lay_title);
4248 break;
4250 old = D_layout->lay_number;
4251 n = atoi(args[1]);
4252 if (n < 0 || n >= MAXLAY)
4253 break;
4254 lay = laytab[n];
4255 laytab[n] = D_layout;
4256 D_layout->lay_number = n;
4257 laytab[old] = lay;
4258 if (lay)
4259 lay->lay_number = old;
4260 break;
4262 else if (!strcmp(args[0], "autosave"))
4264 if (!D_layout)
4266 Msg(0, "not on a layout");
4267 break;
4269 if (args[1])
4271 if (!strcmp(args[1], "on"))
4272 D_layout->lay_autosave = 1;
4273 else if (!strcmp(args[1], "off"))
4274 D_layout->lay_autosave = 0;
4275 else
4277 Msg(0, "invalid argument. Give 'on' or 'off");
4278 break;
4281 if (msgok)
4282 Msg(0, "autosave is %s", D_layout->lay_autosave ? "on" : "off");
4284 else if (!strcmp(args[0], "new"))
4286 char *t = args[1];
4287 n = 0;
4288 if (t)
4290 while (*t >= '0' && *t <= '9')
4291 t++;
4292 if (t != args[1] && (!*t || *t == ':'))
4294 n = atoi(args[1]);
4295 if (*t)
4296 t++;
4298 else
4299 t = args[1];
4301 if (!t || !*t)
4302 t = "layout";
4303 NewLayout(t, n);
4304 Activate(-1);
4306 else if (!strcmp(args[0], "save"))
4308 if (!args[1])
4310 Msg(0, "usage: layout save <name>");
4311 break;
4313 SaveLayout(args[1], &D_canvas);
4315 else if (!strcmp(args[0], "select"))
4317 if (!args[1])
4319 Input("Switch to layout: ", 20, INP_COOKED, SelectLayoutFin, NULL, 0);
4320 break;
4322 SelectLayoutFin(args[1], strlen(args[1]), (char *)0);
4324 else if (!strcmp(args[0], "next"))
4326 struct layout *lay = D_layout;
4327 if (lay)
4328 lay = lay->lay_next ? lay->lay_next : layouts;
4329 else
4330 lay = layouts;
4331 if (!lay)
4333 Msg(0, "no layout defined");
4334 break;
4336 if (lay == D_layout)
4337 break;
4338 LoadLayout(lay, &D_canvas);
4339 Activate(-1);
4341 else if (!strcmp(args[0], "prev"))
4343 struct layout *lay = D_layout;
4344 if (lay)
4346 for (lay = layouts; lay->lay_next && lay->lay_next != D_layout; lay = lay->lay_next)
4349 else
4350 lay = layouts;
4351 if (!lay)
4353 Msg(0, "no layout defined");
4354 break;
4356 if (lay == D_layout)
4357 break;
4358 LoadLayout(lay, &D_canvas);
4359 Activate(-1);
4361 else if (!strcmp(args[0], "attach"))
4363 if (!args[1])
4365 if (!layout_attach)
4366 Msg(0, "no attach layout set");
4367 else if (layout_attach == &layout_last_marker)
4368 Msg(0, "will attach to last layout");
4369 else
4370 Msg(0, "will attach to layout %d (%s)", layout_attach->lay_number, layout_attach->lay_title);
4371 break;
4373 if (!strcmp(args[1], ":last"))
4374 layout_attach = &layout_last_marker;
4375 else if (!args[1][0])
4376 layout_attach = 0;
4377 else
4379 struct layout *lay;
4380 lay = FindLayout(args[1]);
4381 if (!lay)
4383 Msg(0, "unknown layout '%s'", args[1]);
4384 break;
4386 layout_attach = lay;
4389 else if (!strcmp(args[0], "show"))
4391 ShowLayouts(-1);
4393 else if (!strcmp(args[0], "remove"))
4395 struct layout *lay = display ? D_layout : layouts;
4396 if (args[1])
4398 lay = layouts ? FindLayout(args[1]) : (struct layout *)0;
4399 if (!lay)
4401 Msg(0, "unknown layout '%s'", args[1]);
4402 break;
4405 if (lay)
4406 RemoveLayout(lay);
4408 else if (!strcmp(args[0], "dump"))
4410 if (!display)
4411 Msg(0, "Must have a display for 'layout dump'.");
4412 else if (!LayoutDumpCanvas(&D_canvas, args[1] ? args[1] : "layout-dump"))
4413 Msg(errno, "Error dumping layout.");
4414 else
4415 Msg(0, "Layout dumped to \"%s\"", args[1] ? args[1] : "layout-dump");
4417 else
4418 Msg(0, "unknown layout subcommand");
4419 break;
4420 #ifdef DW_CHARS
4421 case RC_CJKWIDTH:
4422 if(ParseSwitch(act, &cjkwidth) == 0)
4424 if(msgok)
4425 Msg(0, "Treat ambiguous width characters as %s width", cjkwidth ? "full" : "half");
4427 break;
4428 #endif
4429 default:
4430 #ifdef HAVE_BRAILLE
4431 /* key == -2: input from braille keybord, msgok always 0 */
4432 DoBrailleAction(act, key == -2 ? 0 : msgok);
4433 #endif
4434 break;
4436 if (display != odisplay)
4438 for (display = displays; display; display = display->d_next)
4439 if (display == odisplay)
4440 break;
4444 void
4445 DoCommand(argv, argl)
4446 char **argv;
4447 int *argl;
4449 struct action act;
4450 const char *cmd = *argv;
4452 act.quiet = 0;
4453 if (*cmd == '@') /* Suppress error */
4455 act.quiet |= 0x01;
4456 cmd++;
4458 if (*cmd == '-') /* Suppress normal message */
4460 act.quiet |= 0x02;
4461 cmd++;
4464 if ((act.nr = FindCommnr(cmd)) == RC_ILLEGAL)
4466 Msg(0, "%s: unknown command '%s'", rc_name, cmd);
4467 return;
4469 act.args = argv + 1;
4470 act.argl = argl + 1;
4471 DoAction(&act, -1);
4474 static void
4475 SaveAction(act, nr, args, argl)
4476 struct action *act;
4477 int nr;
4478 char **args;
4479 int *argl;
4481 register int argc = 0;
4482 char **pp;
4483 int *lp;
4485 if (args)
4486 while (args[argc])
4487 argc++;
4488 if (argc == 0)
4490 act->nr = nr;
4491 act->args = noargs;
4492 act->argl = 0;
4493 return;
4495 if ((pp = (char **)malloc((unsigned)(argc + 1) * sizeof(char **))) == 0)
4496 Panic(0, strnomem);
4497 if ((lp = (int *)malloc((unsigned)(argc) * sizeof(int *))) == 0)
4498 Panic(0, strnomem);
4499 act->nr = nr;
4500 act->args = pp;
4501 act->argl = lp;
4502 while (argc--)
4504 *lp = argl ? *argl++ : (int)strlen(*args);
4505 *pp++ = SaveStrn(*args++, *lp++);
4507 *pp = 0;
4510 static char **
4511 SaveArgs(args)
4512 char **args;
4514 register char **ap, **pp;
4515 register int argc = 0;
4517 while (args[argc])
4518 argc++;
4519 if ((pp = ap = (char **)malloc((unsigned)(argc + 1) * sizeof(char **))) == 0)
4520 Panic(0, strnomem);
4521 while (argc--)
4522 *pp++ = SaveStr(*args++);
4523 *pp = 0;
4524 return ap;
4529 * buf is split into argument vector args.
4530 * leading whitespace is removed.
4531 * @!| abbreviations are expanded.
4532 * the end of buffer is recognized by '\0' or an un-escaped '#'.
4533 * " and ' are interpreted.
4535 * argc is returned.
4537 int
4538 Parse(buf, bufl, args, argl)
4539 char *buf, **args;
4540 int bufl, *argl;
4542 register char *p = buf, **ap = args, *pp;
4543 register int delim, argc;
4544 int *lp = argl;
4546 debug2("Parse %d %s\n", bufl, buf);
4547 argc = 0;
4548 pp = buf;
4549 delim = 0;
4550 for (;;)
4552 *lp = 0;
4553 while (*p && (*p == ' ' || *p == '\t'))
4554 ++p;
4555 #ifdef PSEUDOS
4556 if (argc == 0 && *p == '!')
4558 *ap++ = "exec";
4559 *lp++ = 4;
4560 p++;
4561 argc++;
4562 continue;
4564 #endif
4565 if (*p == '\0' || *p == '#' || *p == '\n')
4567 *p = '\0';
4568 for (delim = 0; delim < argc; delim++)
4569 debug1("-- %s\n", args[delim]);
4570 args[argc] = 0;
4571 return argc;
4573 if (++argc >= MAXARGS)
4575 Msg(0, "%s: too many tokens.", rc_name);
4576 return 0;
4578 *ap++ = pp;
4580 debug1("- new arg %s\n", p);
4581 while (*p)
4583 if (*p == delim)
4584 delim = 0;
4585 else if (delim != '\'' && *p == '\\' && (p[1] == 'n' || p[1] == 'r' || p[1] == 't' || p[1] == '\'' || p[1] == '"' || p[1] == '\\' || p[1] == '$' || p[1] == '#' || p[1] == '^' || (p[1] >= '0' && p[1] <= '7')))
4587 p++;
4588 if (*p >= '0' && *p <= '7')
4590 *pp = *p - '0';
4591 if (p[1] >= '0' && p[1] <= '7')
4593 p++;
4594 *pp = (*pp << 3) | (*p - '0');
4595 if (p[1] >= '0' && p[1] <= '7')
4597 p++;
4598 *pp = (*pp << 3) | (*p - '0');
4601 pp++;
4603 else
4605 switch (*p)
4607 case 'n': *pp = '\n'; break;
4608 case 'r': *pp = '\r'; break;
4609 case 't': *pp = '\t'; break;
4610 default: *pp = *p; break;
4612 pp++;
4615 else if (delim != '\'' && *p == '$' && (p[1] == '{' || p[1] == ':' || (p[1] >= 'a' && p[1] <= 'z') || (p[1] >= 'A' && p[1] <= 'Z') || (p[1] >= '0' && p[1] <= '9') || p[1] == '_'))
4618 char *ps, *pe, op, *v, xbuf[11];
4619 int vl;
4621 ps = ++p;
4622 debug1("- var %s\n", ps);
4623 p++;
4624 while (*p)
4626 if (*ps == '{' && *p == '}')
4627 break;
4628 if (*ps == ':' && *p == ':')
4629 break;
4630 if (*ps != '{' && *ps != ':' && (*p < 'a' || *p > 'z') && (*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9') && *p != '_')
4631 break;
4632 p++;
4634 pe = p;
4635 if (*ps == '{' || *ps == ':')
4637 if (!*p)
4639 Msg(0, "%s: bad variable name.", rc_name);
4640 return 0;
4642 p++;
4644 op = *pe;
4645 *pe = 0;
4646 debug1("- var is '%s'\n", ps);
4647 if (*ps == ':')
4648 v = gettermcapstring(ps + 1);
4649 else
4651 if (*ps == '{')
4652 ps++;
4653 v = xbuf;
4654 if (!strcmp(ps, "TERM"))
4655 v = display ? D_termname : "unknown";
4656 else if (!strcmp(ps, "COLUMNS"))
4657 sprintf(xbuf, "%d", display ? D_width : -1);
4658 else if (!strcmp(ps, "LINES"))
4659 sprintf(xbuf, "%d", display ? D_height : -1);
4660 else if (!strcmp(ps, "PID"))
4661 sprintf(xbuf, "%d", getpid());
4662 else if (!strcmp(ps, "STY"))
4663 v = SockName;
4664 else
4665 v = getenv(ps);
4667 *pe = op;
4668 vl = v ? strlen(v) : 0;
4669 if (vl)
4671 debug1("- sub is '%s'\n", v);
4672 if (p - pp < vl)
4674 int right = buf + bufl - (p + strlen(p) + 1);
4675 if (right > 0)
4677 bcopy(p, p + right, strlen(p) + 1);
4678 p += right;
4681 if (p - pp < vl)
4683 Msg(0, "%s: no space left for variable expansion.", rc_name);
4684 return 0;
4686 bcopy(v, pp, vl);
4687 pp += vl;
4689 continue;
4691 else if (delim != '\'' && *p == '^' && p[1])
4693 p++;
4694 *pp++ = *p == '?' ? '\177' : *p & 0x1f;
4696 else if (delim == 0 && (*p == '\'' || *p == '"'))
4697 delim = *p;
4698 else if (delim == 0 && (*p == ' ' || *p == '\t' || *p == '\n'))
4699 break;
4700 else
4701 *pp++ = *p;
4702 p++;
4704 if (delim)
4706 Msg(0, "%s: Missing %c quote.", rc_name, delim);
4707 return 0;
4709 if (*p)
4710 p++;
4711 *pp = 0;
4712 debug2("- arg done, '%s' rest %s\n", ap[-1], p);
4713 *lp++ = pp - ap[-1];
4714 pp++;
4718 void
4719 SetEscape(u, e, me)
4720 struct acluser *u;
4721 int e, me;
4723 if (u)
4725 u->u_Esc = e;
4726 u->u_MetaEsc = me;
4728 else
4730 if (users)
4732 if (DefaultEsc >= 0)
4733 ClearAction(&ktab[DefaultEsc]);
4734 if (DefaultMetaEsc >= 0)
4735 ClearAction(&ktab[DefaultMetaEsc]);
4737 DefaultEsc = e;
4738 DefaultMetaEsc = me;
4739 if (users)
4741 if (DefaultEsc >= 0)
4743 ClearAction(&ktab[DefaultEsc]);
4744 ktab[DefaultEsc].nr = RC_OTHER;
4746 if (DefaultMetaEsc >= 0)
4748 ClearAction(&ktab[DefaultMetaEsc]);
4749 ktab[DefaultMetaEsc].nr = RC_META;
4756 ParseSwitch(act, var)
4757 struct action *act;
4758 int *var;
4760 if (*act->args == 0)
4762 *var ^= 1;
4763 return 0;
4765 return ParseOnOff(act, var);
4768 static int
4769 ParseOnOff(act, var)
4770 struct action *act;
4771 int *var;
4773 register int num = -1;
4774 char **args = act->args;
4776 if (args[1] == 0)
4778 if (strcmp(args[0], "on") == 0)
4779 num = 1;
4780 else if (strcmp(args[0], "off") == 0)
4781 num = 0;
4783 if (num < 0)
4785 Msg(0, "%s: %s: invalid argument. Give 'on' or 'off'", rc_name, comms[act->nr].name);
4786 return -1;
4788 *var = num;
4789 return 0;
4793 ParseSaveStr(act, var)
4794 struct action *act;
4795 char **var;
4797 char **args = act->args;
4798 if (*args == 0 || args[1])
4800 Msg(0, "%s: %s: one argument required.", rc_name, comms[act->nr].name);
4801 return -1;
4803 if (*var)
4804 free(*var);
4805 *var = SaveStr(*args);
4806 return 0;
4810 ParseNum(act, var)
4811 struct action *act;
4812 int *var;
4814 int i;
4815 char *p, **args = act->args;
4817 p = *args;
4818 if (p == 0 || *p == 0 || args[1])
4820 Msg(0, "%s: %s: invalid argument. Give one argument.",
4821 rc_name, comms[act->nr].name);
4822 return -1;
4824 i = 0;
4825 while (*p)
4827 if (*p >= '0' && *p <= '9')
4828 i = 10 * i + (*p - '0');
4829 else
4831 Msg(0, "%s: %s: invalid argument. Give numeric argument.",
4832 rc_name, comms[act->nr].name);
4833 return -1;
4835 p++;
4837 debug1("ParseNum got %d\n", i);
4838 *var = i;
4839 return 0;
4842 static int
4843 ParseNum1000(act, var)
4844 struct action *act;
4845 int *var;
4847 int i;
4848 char *p, **args = act->args;
4849 int dig = 0;
4851 p = *args;
4852 if (p == 0 || *p == 0 || args[1])
4854 Msg(0, "%s: %s: invalid argument. Give one argument.",
4855 rc_name, comms[act->nr].name);
4856 return -1;
4858 i = 0;
4859 while (*p)
4861 if (*p >= '0' && *p <= '9')
4863 if (dig < 4)
4864 i = 10 * i + (*p - '0');
4865 else if (dig == 4 && *p >= '5')
4866 i++;
4867 if (dig)
4868 dig++;
4870 else if (*p == '.' && !dig)
4871 dig++;
4872 else
4874 Msg(0, "%s: %s: invalid argument. Give floating point argument.",
4875 rc_name, comms[act->nr].name);
4876 return -1;
4878 p++;
4880 if (dig == 0)
4881 i *= 1000;
4882 else
4883 while (dig++ < 4)
4884 i *= 10;
4885 if (i < 0)
4886 i = (int)((unsigned int)~0 >> 1);
4887 debug1("ParseNum1000 got %d\n", i);
4888 *var = i;
4889 return 0;
4892 static struct win *
4893 WindowByName(s)
4894 char *s;
4896 struct win *p;
4898 for (p = windows; p; p = p->w_next)
4899 if (!strcmp(p->w_title, s))
4900 return p;
4901 for (p = windows; p; p = p->w_next)
4902 if (!strncmp(p->w_title, s, strlen(s)))
4903 return p;
4904 return 0;
4907 static int
4908 WindowByNumber(str)
4909 char *str;
4911 int i;
4912 char *s;
4914 for (i = 0, s = str; *s; s++)
4916 if (*s < '0' || *s > '9')
4917 break;
4918 i = i * 10 + (*s - '0');
4920 return *s ? -1 : i;
4924 * Get window number from Name or Number string.
4925 * Numbers are tried first, then names, a prefix match suffices.
4926 * Be careful when assigning numeric strings as WindowTitles.
4929 WindowByNoN(str)
4930 char *str;
4932 int i;
4933 struct win *p;
4935 if ((i = WindowByNumber(str)) < 0 || i >= maxwin)
4937 if ((p = WindowByName(str)))
4938 return p->w_number;
4939 return -1;
4941 return i;
4944 static int
4945 ParseWinNum(act, var)
4946 struct action *act;
4947 int *var;
4949 char **args = act->args;
4950 int i = 0;
4952 if (*args == 0 || args[1])
4954 Msg(0, "%s: %s: one argument required.", rc_name, comms[act->nr].name);
4955 return -1;
4958 i = WindowByNoN(*args);
4959 if (i < 0)
4961 Msg(0, "%s: %s: invalid argument. Give window number or name.",
4962 rc_name, comms[act->nr].name);
4963 return -1;
4965 debug1("ParseWinNum got %d\n", i);
4966 *var = i;
4967 return 0;
4970 static int
4971 ParseBase(act, p, var, base, bname)
4972 struct action *act;
4973 char *p;
4974 int *var;
4975 int base;
4976 char *bname;
4978 int i = 0;
4979 int c;
4981 if (*p == 0)
4983 Msg(0, "%s: %s: empty argument.", rc_name, comms[act->nr].name);
4984 return -1;
4986 while ((c = *p++))
4988 if (c >= 'a' && c <= 'z')
4989 c -= 'a' - 'A';
4990 if (c >= 'A' && c <= 'Z')
4991 c -= 'A' - ('0' + 10);
4992 c -= '0';
4993 if (c < 0 || c >= base)
4995 Msg(0, "%s: %s: argument is not %s.", rc_name, comms[act->nr].name, bname);
4996 return -1;
4998 i = base * i + c;
5000 debug1("ParseBase got %d\n", i);
5001 *var = i;
5002 return 0;
5005 static int
5006 IsNum(s, base)
5007 register char *s;
5008 register int base;
5010 for (base += '0'; *s; ++s)
5011 if (*s < '0' || *s > base)
5012 return 0;
5013 return 1;
5017 IsNumColon(s, base, p, psize)
5018 int base, psize;
5019 char *s, *p;
5021 char *q;
5022 if ((q = rindex(s, ':')) != 0)
5024 strncpy(p, q + 1, psize - 1);
5025 p[psize - 1] = '\0';
5026 *q = '\0';
5028 else
5029 *p = '\0';
5030 return IsNum(s, base);
5033 void
5034 SwitchWindow(n)
5035 int n;
5037 struct win *p;
5039 debug1("SwitchWindow %d\n", n);
5040 if (n < 0 || n >= maxwin)
5042 ShowWindows(-1);
5043 return;
5045 if ((p = wtab[n]) == 0)
5047 ShowWindows(n);
5048 return;
5050 if (display == 0)
5052 fore = p;
5053 return;
5055 if (p == D_fore)
5057 Msg(0, "This IS window %d (%s).", n, p->w_title);
5058 return;
5060 #ifdef MULTIUSER
5061 if (AclCheckPermWin(D_user, ACL_READ, p))
5063 Msg(0, "Access to window %d denied.", p->w_number);
5064 return;
5066 #endif
5067 SetForeWindow(p);
5068 Activate(fore->w_norefresh);
5072 * SetForeWindow changes the window in the input focus of the display.
5073 * Puts window wi in canvas display->d_forecv.
5075 void
5076 SetForeWindow(wi)
5077 struct win *wi;
5079 struct win *p;
5080 if (display == 0)
5082 fore = wi;
5083 return;
5085 p = Layer2Window(D_forecv->c_layer);
5086 SetCanvasWindow(D_forecv, wi);
5087 if (p)
5088 WindowChanged(p, 'u');
5089 if (wi)
5090 WindowChanged(wi, 'u');
5091 flayer = D_forecv->c_layer;
5092 /* Activate called afterwards, so no RefreshHStatus needed */
5096 /*****************************************************************/
5099 * Activate - make fore window active
5100 * norefresh = -1 forces a refresh, disregard all_norefresh then.
5102 void
5103 Activate(norefresh)
5104 int norefresh;
5106 debug1("Activate(%d)\n", norefresh);
5107 if (display == 0)
5108 return;
5109 if (D_status)
5111 Msg(0, "%s", ""); /* wait till mintime (keep gcc quiet) */
5112 RemoveStatus();
5115 if (MayResizeLayer(D_forecv->c_layer))
5116 ResizeLayer(D_forecv->c_layer, D_forecv->c_xe - D_forecv->c_xs + 1, D_forecv->c_ye - D_forecv->c_ys + 1, display);
5118 fore = D_fore;
5119 if (fore)
5121 /* XXX ? */
5122 if (fore->w_monitor != MON_OFF)
5123 fore->w_monitor = MON_ON;
5124 fore->w_bell = BELL_ON;
5125 WindowChanged(fore, 'f');
5127 #if 0
5128 if (ResizeDisplay(fore->w_width, fore->w_height))
5130 debug2("Cannot resize from (%d,%d)", D_width, D_height);
5131 debug2(" to (%d,%d) -> resize window\n", fore->w_width, fore->w_height);
5132 DoResize(D_width, D_height);
5134 #endif
5136 Redisplay(norefresh + all_norefresh);
5140 static int
5141 NextWindow()
5143 register struct win **pp;
5144 int n = fore ? fore->w_number : maxwin;
5145 struct win *group = fore ? fore->w_group : 0;
5147 for (pp = fore ? wtab + n + 1 : wtab; pp != wtab + n; pp++)
5149 if (pp == wtab + maxwin)
5150 pp = wtab;
5151 if (*pp)
5153 if (!fore || group == (*pp)->w_group)
5154 break;
5157 if (pp == wtab + n)
5158 return -1;
5159 return pp - wtab;
5162 static int
5163 PreviousWindow()
5165 register struct win **pp;
5166 int n = fore ? fore->w_number : -1;
5167 struct win *group = fore ? fore->w_group : 0;
5169 for (pp = wtab + n - 1; pp != wtab + n; pp--)
5171 if (pp == wtab - 1)
5172 pp = wtab + maxwin - 1;
5173 if (*pp)
5175 if (!fore || group == (*pp)->w_group)
5176 break;
5179 if (pp == wtab + n)
5180 return -1;
5181 return pp - wtab;
5184 static int
5185 MoreWindows()
5187 char *m = "No other window.";
5188 if (windows && (fore == 0 || windows->w_next))
5189 return 1;
5190 if (fore == 0)
5192 Msg(0, "No window available");
5193 return 0;
5195 Msg(0, m, fore->w_number); /* other arg for nethack */
5196 return 0;
5199 void
5200 KillWindow(wi)
5201 struct win *wi;
5203 struct win **pp, *p;
5204 struct canvas *cv;
5205 int gotone;
5206 struct layout *lay;
5209 * Remove window from linked list.
5211 for (pp = &windows; (p = *pp); pp = &p->w_next)
5212 if (p == wi)
5213 break;
5214 ASSERT(p);
5215 *pp = p->w_next;
5216 wi->w_inlen = 0;
5217 wtab[wi->w_number] = 0;
5219 if (windows == 0)
5221 FreeWindow(wi);
5222 Finit(0);
5226 * switch to different window on all canvases
5228 for (display = displays; display; display = display->d_next)
5230 gotone = 0;
5231 for (cv = D_cvlist; cv; cv = cv->c_next)
5233 if (Layer2Window(cv->c_layer) != wi)
5234 continue;
5235 /* switch to other window */
5236 SetCanvasWindow(cv, FindNiceWindow(D_other, 0));
5237 gotone = 1;
5239 if (gotone)
5241 #ifdef ZMODEM
5242 if (wi->w_zdisplay == display)
5244 D_blocked = 0;
5245 D_readev.condpos = D_readev.condneg = 0;
5247 #endif
5248 Activate(-1);
5252 /* do the same for the layouts */
5253 for (lay = layouts; lay; lay = lay->lay_next)
5254 UpdateLayoutCanvas(&lay->lay_canvas, wi);
5256 FreeWindow(wi);
5257 WindowChanged((struct win *)0, 'w');
5258 WindowChanged((struct win *)0, 'W');
5259 WindowChanged((struct win *)0, 0);
5262 static void
5263 LogToggle(on)
5264 int on;
5266 char buf[1024];
5268 if ((fore->w_log != 0) == on)
5270 if (display && !*rc_name)
5271 Msg(0, "You are %s logging.", on ? "already" : "not");
5272 return;
5274 if (fore->w_log != 0)
5276 Msg(0, "Logfile \"%s\" closed.", fore->w_log->name);
5277 logfclose(fore->w_log);
5278 fore->w_log = 0;
5279 WindowChanged(fore, 'f');
5280 return;
5282 if (DoStartLog(fore, buf, sizeof(buf)))
5284 Msg(errno, "Error opening logfile \"%s\"", buf);
5285 return;
5287 if (ftell(fore->w_log->fp) == 0)
5288 Msg(0, "Creating logfile \"%s\".", fore->w_log->name);
5289 else
5290 Msg(0, "Appending to logfile \"%s\".", fore->w_log->name);
5291 WindowChanged(fore, 'f');
5294 char *
5295 AddWindows(buf, len, flags, where)
5296 char *buf;
5297 int len;
5298 int flags;
5299 int where;
5301 register char *s, *ss;
5302 register struct win **pp, *p;
5303 register char *cmd;
5304 int l;
5306 s = ss = buf;
5307 if ((flags & 8) && where < 0)
5309 *s = 0;
5310 return ss;
5312 for (pp = ((flags & 4) && where >= 0) ? wtab + where + 1: wtab; pp < wtab + maxwin; pp++)
5314 int rend = -1;
5315 if (pp - wtab == where && ss == buf)
5316 ss = s;
5317 if ((p = *pp) == 0)
5318 continue;
5319 if ((flags & 1) && display && p == D_fore)
5320 continue;
5321 if (D_fore && D_fore->w_group != p->w_group)
5322 continue;
5324 cmd = p->w_title;
5325 l = strlen(cmd);
5326 if (l > 20)
5327 l = 20;
5328 if (s - buf + l > len - 24)
5329 break;
5330 if (s > buf || (flags & 4))
5332 *s++ = ' ';
5333 *s++ = ' ';
5335 if (p->w_number == where)
5337 ss = s;
5338 if (flags & 8)
5339 break;
5341 if (!(flags & 4) || where < 0 || ((flags & 4) && where < p->w_number))
5343 if (p->w_monitor == MON_DONE && renditions[REND_MONITOR] != -1)
5344 rend = renditions[REND_MONITOR];
5345 else if ((p->w_bell == BELL_DONE || p->w_bell == BELL_FOUND) && renditions[REND_BELL] != -1)
5346 rend = renditions[REND_BELL];
5348 if (rend != -1)
5349 AddWinMsgRend(s, rend);
5350 sprintf(s, "%d", p->w_number);
5351 s += strlen(s);
5352 if (display && p == D_fore)
5353 *s++ = '*';
5354 if (!(flags & 2))
5356 if (display && p == D_other)
5357 *s++ = '-';
5358 s = AddWindowFlags(s, len, p);
5360 *s++ = ' ';
5361 strncpy(s, cmd, l);
5362 s += l;
5363 if (rend != -1)
5364 AddWinMsgRend(s, -1);
5366 *s = 0;
5367 return ss;
5370 char *
5371 AddWindowFlags(buf, len, p)
5372 char *buf;
5373 int len;
5374 struct win *p;
5376 char *s = buf;
5377 if (p == 0 || len < 12)
5379 *s = 0;
5380 return s;
5382 #if 0
5383 if (display && p == D_fore)
5384 *s++ = '*';
5385 if (display && p == D_other)
5386 *s++ = '-';
5387 #endif
5388 if (p->w_layer.l_cvlist && p->w_layer.l_cvlist->c_lnext)
5389 *s++ = '&';
5390 if (p->w_monitor == MON_DONE
5391 #ifdef MULTIUSER
5392 && (ACLBYTE(p->w_mon_notify, D_user->u_id) & ACLBIT(D_user->u_id))
5393 #endif
5395 *s++ = '@';
5396 if (p->w_bell == BELL_DONE)
5397 *s++ = '!';
5398 #ifdef UTMPOK
5399 if (p->w_slot != (slot_t) 0 && p->w_slot != (slot_t) -1)
5400 *s++ = '$';
5401 #endif
5402 if (p->w_log != 0)
5404 strcpy(s, "(L)");
5405 s += 3;
5407 if (p->w_ptyfd < 0 && p->w_type != W_TYPE_GROUP)
5408 *s++ = 'Z';
5409 *s = 0;
5410 return s;
5413 char *
5414 AddOtherUsers(buf, len, p)
5415 char *buf;
5416 int len;
5417 struct win *p;
5419 struct display *d, *olddisplay = display;
5420 struct canvas *cv;
5421 char *s;
5422 int l;
5424 s = buf;
5425 for (display = displays; display; display = display->d_next)
5427 if (olddisplay && D_user == olddisplay->d_user)
5428 continue;
5429 for (cv = D_cvlist; cv; cv = cv->c_next)
5430 if (Layer2Window(cv->c_layer) == p)
5431 break;
5432 if (!cv)
5433 continue;
5434 for (d = displays; d && d != display; d = d->d_next)
5435 if (D_user == d->d_user)
5436 break;
5437 if (d && d != display)
5438 continue;
5439 if (len > 1 && s != buf)
5441 *s++ = ',';
5442 len--;
5444 l = strlen(D_user->u_name);
5445 if (l + 1 > len)
5446 break;
5447 strcpy(s, D_user->u_name);
5448 s += l;
5449 len -= l;
5451 *s = 0;
5452 display = olddisplay;
5453 return s;
5456 void
5457 ShowWindows(where)
5458 int where;
5460 char buf[1024];
5461 char *s, *ss;
5463 if (!display)
5464 return;
5465 if (where == -1 && D_fore)
5466 where = D_fore->w_number;
5467 ss = AddWindows(buf, sizeof(buf), 0, where);
5468 s = buf + strlen(buf);
5469 if (ss - buf > D_width / 2)
5471 ss -= D_width / 2;
5472 if (s - ss < D_width)
5474 ss = s - D_width;
5475 if (ss < buf)
5476 ss = buf;
5479 else
5480 ss = buf;
5481 Msg(0, "%s", ss);
5484 static void
5485 ShowInfo()
5487 char buf[512], *p;
5488 register struct win *wp = fore;
5489 register int i;
5491 if (wp == 0)
5493 Msg(0, "(%d,%d)/(%d,%d) no window", D_x + 1, D_y + 1, D_width, D_height);
5494 return;
5496 p = buf;
5497 if (buf < (p += GetAnsiStatus(wp, p)))
5498 *p++ = ' ';
5499 sprintf(p, "(%d,%d)/(%d,%d)",
5500 wp->w_x + 1, wp->w_y + 1, wp->w_width, wp->w_height);
5501 #ifdef COPY_PASTE
5502 sprintf(p += strlen(p), "+%d", wp->w_histheight);
5503 #endif
5504 sprintf(p += strlen(p), " %c%sflow",
5505 (wp->w_flow & FLOW_NOW) ? '+' : '-',
5506 (wp->w_flow & FLOW_AUTOFLAG) ? "" :
5507 ((wp->w_flow & FLOW_AUTO) ? "(+)" : "(-)"));
5508 if (!wp->w_wrap) sprintf(p += strlen(p), " -wrap");
5509 if (wp->w_insert) sprintf(p += strlen(p), " ins");
5510 if (wp->w_origin) sprintf(p += strlen(p), " org");
5511 if (wp->w_keypad) sprintf(p += strlen(p), " app");
5512 if (wp->w_log) sprintf(p += strlen(p), " log");
5513 if (wp->w_monitor != MON_OFF
5514 #ifdef MULTIUSER
5515 && (ACLBYTE(wp->w_mon_notify, D_user->u_id) & ACLBIT(D_user->u_id))
5516 #endif
5518 sprintf(p += strlen(p), " mon");
5519 if (wp->w_mouse) sprintf(p += strlen(p), " mouse");
5520 #ifdef COLOR
5521 if (wp->w_bce) sprintf(p += strlen(p), " bce");
5522 #endif
5523 if (!wp->w_c1) sprintf(p += strlen(p), " -c1");
5524 if (wp->w_norefresh) sprintf(p += strlen(p), " nored");
5526 p += strlen(p);
5527 #ifdef FONT
5528 # ifdef ENCODINGS
5529 if (wp->w_encoding && (display == 0 || D_encoding != wp->w_encoding || EncodingDefFont(wp->w_encoding) <= 0))
5531 *p++ = ' ';
5532 strcpy(p, EncodingName(wp->w_encoding));
5533 p += strlen(p);
5535 # ifdef UTF8
5536 if (wp->w_encoding != UTF8)
5537 # endif
5538 # endif
5539 if (D_CC0 || (D_CS0 && *D_CS0))
5541 if (wp->w_gr == 2)
5543 sprintf(p, " G%c", wp->w_Charset + '0');
5544 if (wp->w_FontE >= ' ')
5545 p[3] = wp->w_FontE;
5546 else
5548 p[3] = '^';
5549 p[4] = wp->w_FontE ^ 0x40;
5550 p++;
5552 p[4] = '[';
5553 p++;
5555 else if (wp->w_gr)
5556 sprintf(p++, " G%c%c[", wp->w_Charset + '0', wp->w_CharsetR + '0');
5557 else
5558 sprintf(p, " G%c[", wp->w_Charset + '0');
5559 p += 4;
5560 for (i = 0; i < 4; i++)
5562 if (wp->w_charsets[i] == ASCII)
5563 *p++ = 'B';
5564 else if (wp->w_charsets[i] >= ' ')
5565 *p++ = wp->w_charsets[i];
5566 else
5568 *p++ = '^';
5569 *p++ = wp->w_charsets[i] ^ 0x40;
5572 *p++ = ']';
5573 *p = 0;
5575 #endif
5577 if (wp->w_type == W_TYPE_PLAIN)
5579 /* add info about modem control lines */
5580 *p++ = ' ';
5581 TtyGetModemStatus(wp->w_ptyfd, p);
5583 #ifdef BUILTIN_TELNET
5584 else if (wp->w_type == W_TYPE_TELNET)
5586 *p++ = ' ';
5587 TelStatus(wp, p, sizeof(buf) - 1 - (p - buf));
5589 #endif
5590 Msg(0, "%s %d(%s)", buf, wp->w_number, wp->w_title);
5593 static void
5594 ShowDInfo()
5596 char buf[512], *p;
5597 if (display == 0)
5598 return;
5599 p = buf;
5600 sprintf(p, "(%d,%d)", D_width, D_height),
5601 p += strlen(p);
5602 #ifdef ENCODINGS
5603 if (D_encoding)
5605 *p++ = ' ';
5606 strcpy(p, EncodingName(D_encoding));
5607 p += strlen(p);
5609 #endif
5610 if (D_CXT)
5612 strcpy(p, " xterm");
5613 p += strlen(p);
5615 #ifdef COLOR
5616 if (D_hascolor)
5618 strcpy(p, " color");
5619 p += strlen(p);
5621 #endif
5622 #ifdef FONT
5623 if (D_CG0)
5625 strcpy(p, " iso2022");
5626 p += strlen(p);
5628 else if (D_CS0 && *D_CS0)
5630 strcpy(p, " altchar");
5631 p += strlen(p);
5633 #endif
5634 Msg(0, "%s", buf);
5637 static void
5638 AKAfin(buf, len, data)
5639 char *buf;
5640 int len;
5641 char *data; /* dummy */
5643 ASSERT(display);
5644 if (len && fore)
5645 ChangeAKA(fore, buf, strlen(buf));
5648 static void
5649 InputAKA()
5651 char *s, *ss;
5652 int n;
5653 Input("Set window's title to: ", sizeof(fore->w_akabuf) - 1, INP_COOKED, AKAfin, NULL, 0);
5654 s = fore->w_title;
5655 if (!s)
5656 return;
5657 for (; *s; s++)
5659 if ((*(unsigned char *)s & 0x7f) < 0x20 || *s == 0x7f)
5660 continue;
5661 ss = s;
5662 n = 1;
5663 LayProcess(&ss, &n);
5667 static void
5668 Colonfin(buf, len, data)
5669 char *buf;
5670 int len;
5671 char *data; /* dummy */
5673 char mbuf[256];
5675 RemoveStatus();
5676 if (buf[len] == '\t')
5678 int m, x;
5679 int l = 0, r = RC_LAST;
5680 int showmessage = 0;
5681 char *s = buf;
5683 while (*s && s - buf < len)
5684 if (*s++ == ' ')
5685 return;
5687 /* Showing a message when there's no hardstatus or caption cancels the input */
5688 if (display &&
5689 (captionalways || D_has_hstatus == HSTATUS_LASTLINE || (D_canvas.c_slperp && D_canvas.c_slperp->c_slnext)))
5690 showmessage = 1;
5692 while (l <= r)
5694 m = (l + r) / 2;
5695 x = strncmp(buf, comms[m].name, len);
5696 if (x > 0)
5697 l = m + 1;
5698 else if (x < 0)
5699 r = m - 1;
5700 else
5702 s = mbuf;
5703 for (l = m - 1; l >= 0 && strncmp(buf, comms[l].name, len) == 0; l--)
5705 for (m = ++l; m <= r && strncmp(buf, comms[m].name, len) == 0 && s - mbuf < sizeof(mbuf); m++)
5706 s += snprintf(s, sizeof(mbuf) - (s - mbuf), " %s", comms[m].name);
5707 if (l < m - 1)
5709 if (showmessage)
5710 Msg(0, "Possible commands:%s", mbuf);
5712 else
5714 s = mbuf;
5715 len = snprintf(mbuf, sizeof(mbuf), "%s \t", comms[l].name + len);
5716 if (len > 0 && len < sizeof(mbuf))
5717 LayProcess(&s, &len);
5719 break;
5722 if (l > r && showmessage)
5723 Msg(0, "No commands matching '%*s'", len, buf);
5724 return;
5727 if (!len || buf[len])
5728 return;
5730 len = strlen(buf) + 1;
5731 if (len > (int)sizeof(mbuf))
5732 RcLine(buf, len);
5733 else
5735 bcopy(buf, mbuf, len);
5736 RcLine(mbuf, sizeof mbuf);
5740 static void
5741 SelectFin(buf, len, data)
5742 char *buf;
5743 int len;
5744 char *data; /* dummy */
5746 int n;
5748 if (!len || !display)
5749 return;
5750 if (len == 1 && *buf == '-')
5752 SetForeWindow((struct win *)0);
5753 Activate(0);
5754 return;
5756 if ((n = WindowByNoN(buf)) < 0)
5757 return;
5758 SwitchWindow(n);
5761 static void
5762 SelectLayoutFin(buf, len, data)
5763 char *buf;
5764 int len;
5765 char *data; /* dummy */
5767 struct layout *lay;
5769 if (!len || !display)
5770 return;
5771 if (len == 1 && *buf == '-')
5773 LoadLayout((struct layout *)0, (struct canvas *)0);
5774 Activate(0);
5775 return;
5777 lay = FindLayout(buf);
5778 if (!lay)
5779 Msg(0, "No such layout\n");
5780 else if (lay == D_layout)
5781 Msg(0, "This IS layout %d (%s).\n", lay->lay_number, lay->lay_title);
5782 else
5784 LoadLayout(lay, &D_canvas);
5785 Activate(0);
5790 static void
5791 InputSelect()
5793 Input("Switch to window: ", 20, INP_COOKED, SelectFin, NULL, 0);
5796 static char setenv_var[31];
5799 static void
5800 SetenvFin1(buf, len, data)
5801 char *buf;
5802 int len;
5803 char *data; /* dummy */
5805 if (!len || !display)
5806 return;
5807 InputSetenv(buf);
5810 static void
5811 SetenvFin2(buf, len, data)
5812 char *buf;
5813 int len;
5814 char *data; /* dummy */
5816 if (!len || !display)
5817 return;
5818 debug2("SetenvFin2: setenv '%s' '%s'\n", setenv_var, buf);
5819 xsetenv(setenv_var, buf);
5820 MakeNewEnv();
5823 static void
5824 InputSetenv(arg)
5825 char *arg;
5827 static char setenv_buf[50 + sizeof(setenv_var)]; /* need to be static here, cannot be freed */
5829 if (arg)
5831 strncpy(setenv_var, arg, sizeof(setenv_var) - 1);
5832 sprintf(setenv_buf, "Enter value for %s: ", setenv_var);
5833 Input(setenv_buf, 30, INP_COOKED, SetenvFin2, NULL, 0);
5835 else
5836 Input("Setenv: Enter variable name: ", 30, INP_COOKED, SetenvFin1, NULL, 0);
5840 * the following options are understood by this parser:
5841 * -f, -f0, -f1, -fy, -fa
5842 * -t title, -T terminal-type, -h height-of-scrollback,
5843 * -ln, -l0, -ly, -l1, -l
5844 * -a, -M, -L
5846 void
5847 DoScreen(fn, av)
5848 char *fn, **av;
5850 struct NewWindow nwin;
5851 register int num;
5852 char buf[20];
5854 nwin = nwin_undef;
5855 while (av && *av && av[0][0] == '-')
5857 if (av[0][1] == '-')
5859 av++;
5860 break;
5862 switch (av[0][1])
5864 case 'f':
5865 switch (av[0][2])
5867 case 'n':
5868 case '0':
5869 nwin.flowflag = FLOW_NOW * 0;
5870 break;
5871 case 'y':
5872 case '1':
5873 case '\0':
5874 nwin.flowflag = FLOW_NOW * 1;
5875 break;
5876 case 'a':
5877 nwin.flowflag = FLOW_AUTOFLAG;
5878 break;
5879 default:
5880 break;
5882 break;
5883 case 't': /* no more -k */
5884 if (av[0][2])
5885 nwin.aka = &av[0][2];
5886 else if (*++av)
5887 nwin.aka = *av;
5888 else
5889 --av;
5890 break;
5891 case 'T':
5892 if (av[0][2])
5893 nwin.term = &av[0][2];
5894 else if (*++av)
5895 nwin.term = *av;
5896 else
5897 --av;
5898 break;
5899 case 'h':
5900 if (av[0][2])
5901 nwin.histheight = atoi(av[0] + 2);
5902 else if (*++av)
5903 nwin.histheight = atoi(*av);
5904 else
5905 --av;
5906 break;
5907 #ifdef LOGOUTOK
5908 case 'l':
5909 switch (av[0][2])
5911 case 'n':
5912 case '0':
5913 nwin.lflag = 0;
5914 break;
5915 case 'y':
5916 case '1':
5917 case '\0':
5918 nwin.lflag = 1;
5919 break;
5920 case 'a':
5921 nwin.lflag = 3;
5922 break;
5923 default:
5924 break;
5926 break;
5927 #endif
5928 case 'a':
5929 nwin.aflag = 1;
5930 break;
5931 case 'M':
5932 nwin.monitor = MON_ON;
5933 break;
5934 case 'L':
5935 nwin.Lflag = 1;
5936 break;
5937 default:
5938 Msg(0, "%s: screen: invalid option -%c.", fn, av[0][1]);
5939 break;
5941 ++av;
5943 num = 0;
5944 if (av && *av && IsNumColon(*av, 10, buf, sizeof(buf)))
5946 if (*buf != '\0')
5947 nwin.aka = buf;
5948 num = atoi(*av);
5949 if (num < 0 || num > maxwin - 1)
5951 Msg(0, "%s: illegal screen number %d.", fn, num);
5952 num = 0;
5954 nwin.StartAt = num;
5955 ++av;
5957 if (av && *av)
5959 nwin.args = av;
5960 if (!nwin.aka)
5961 nwin.aka = Filename(*av);
5963 MakeWindow(&nwin);
5966 #ifdef COPY_PASTE
5968 * CompileKeys must be called before Markroutine is first used.
5969 * to initialise the keys with defaults, call CompileKeys(NULL, mark_key_tab);
5971 * s is an ascii string in a termcap-like syntax. It looks like
5972 * "j=u:k=d:l=r:h=l: =.:" and so on...
5973 * this example rebinds the cursormovement to the keys u (up), d (down),
5974 * l (left), r (right). placing a mark will now be done with ".".
5977 CompileKeys(s, sl, array)
5978 char *s;
5979 int sl;
5980 unsigned char *array;
5982 int i;
5983 unsigned char key, value;
5985 if (sl == 0)
5987 for (i = 0; i < 256; i++)
5988 array[i] = i;
5989 return 0;
5991 debug1("CompileKeys: '%s'\n", s);
5992 while (sl)
5994 key = *(unsigned char *)s++;
5995 if (*s != '=' || sl < 3)
5996 return -1;
5997 sl--;
6000 s++;
6001 sl -= 2;
6002 value = *(unsigned char *)s++;
6003 array[value] = key;
6005 while (*s == '=' && sl >= 2);
6006 if (sl == 0)
6007 break;
6008 if (*s++ != ':')
6009 return -1;
6010 sl--;
6012 return 0;
6014 #endif /* COPY_PASTE */
6017 * Asynchronous input functions
6020 #if defined(DETACH) && defined(POW_DETACH)
6021 static void
6022 pow_detach_fn(buf, len, data)
6023 char *buf;
6024 int len;
6025 char *data; /* dummy */
6027 debug("pow_detach_fn called\n");
6028 if (len)
6030 *buf = 0;
6031 return;
6033 if (ktab[(int)(unsigned char)*buf].nr != RC_POW_DETACH)
6035 if (display)
6036 write(D_userfd, "\007", 1);
6037 Msg(0, "Detach aborted.");
6039 else
6040 Detach(D_POWER);
6042 #endif /* POW_DETACH */
6044 #ifdef COPY_PASTE
6045 static void
6046 copy_reg_fn(buf, len, data)
6047 char *buf;
6048 int len;
6049 char *data; /* dummy */
6051 struct plop *pp = plop_tab + (int)(unsigned char)*buf;
6053 if (len)
6055 *buf = 0;
6056 return;
6058 if (pp->buf)
6059 free(pp->buf);
6060 pp->buf = 0;
6061 pp->len = 0;
6062 if (D_user->u_plop.len)
6064 if ((pp->buf = (char *)malloc(D_user->u_plop.len)) == NULL)
6066 Msg(0, strnomem);
6067 return;
6069 bcopy(D_user->u_plop.buf, pp->buf, D_user->u_plop.len);
6071 pp->len = D_user->u_plop.len;
6072 #ifdef ENCODINGS
6073 pp->enc = D_user->u_plop.enc;
6074 #endif
6075 Msg(0, "Copied %d characters into register %c", D_user->u_plop.len, *buf);
6078 static void
6079 ins_reg_fn(buf, len, data)
6080 char *buf;
6081 int len;
6082 char *data; /* dummy */
6084 struct plop *pp = plop_tab + (int)(unsigned char)*buf;
6086 if (len)
6088 *buf = 0;
6089 return;
6091 if (!fore)
6092 return; /* Input() should not call us w/o fore, but you never know... */
6093 if (*buf == '.')
6094 Msg(0, "ins_reg_fn: Warning: pasting real register '.'!");
6095 if (pp->buf)
6097 MakePaster(&fore->w_paster, pp->buf, pp->len, 0);
6098 return;
6100 Msg(0, "Empty register.");
6102 #endif /* COPY_PASTE */
6104 static void
6105 process_fn(buf, len, data)
6106 char *buf;
6107 int len;
6108 char *data; /* dummy */
6110 struct plop *pp = plop_tab + (int)(unsigned char)*buf;
6112 if (len)
6114 *buf = 0;
6115 return;
6117 if (pp->buf)
6119 ProcessInput(pp->buf, pp->len);
6120 return;
6122 Msg(0, "Empty register.");
6125 static void
6126 confirm_fn(buf, len, data)
6127 char *buf;
6128 int len;
6129 char *data;
6131 struct action act;
6133 if (len || (*buf != 'y' && *buf != 'Y'))
6135 *buf = 0;
6136 return;
6138 act.nr = *(int *)data;
6139 act.args = noargs;
6140 act.argl = 0;
6141 act.quiet = 0;
6142 DoAction(&act, -1);
6145 #ifdef MULTIUSER
6146 struct inputsu
6148 struct acluser **up;
6149 char name[24];
6150 char pw1[130]; /* FreeBSD crypts to 128 bytes */
6151 char pw2[130];
6154 static void
6155 su_fin(buf, len, data)
6156 char *buf;
6157 int len;
6158 char *data;
6160 struct inputsu *i = (struct inputsu *)data;
6161 char *p;
6162 int l;
6164 if (!*i->name)
6165 { p = i->name; l = sizeof(i->name) - 1; }
6166 else if (!*i->pw1)
6167 { strcpy(p = i->pw1, "\377"); l = sizeof(i->pw1) - 1; }
6168 else
6169 { strcpy(p = i->pw2, "\377"); l = sizeof(i->pw2) - 1; }
6170 if (buf && len)
6171 strncpy(p, buf, 1 + (l < len) ? l : len);
6172 if (!*i->name)
6173 Input("Screen User: ", sizeof(i->name) - 1, INP_COOKED, su_fin, (char *)i, 0);
6174 else if (!*i->pw1)
6175 Input("User's UNIX Password: ", sizeof(i->pw1)-1, INP_COOKED|INP_NOECHO, su_fin, (char *)i, 0);
6176 else if (!*i->pw2)
6177 Input("User's Screen Password: ", sizeof(i->pw2)-1, INP_COOKED|INP_NOECHO, su_fin, (char *)i, 0);
6178 else
6180 if ((p = DoSu(i->up, i->name, i->pw2, i->pw1)))
6181 Msg(0, "%s", p);
6182 free((char *)i);
6186 static int
6187 InputSu(w, up, name)
6188 struct win *w;
6189 struct acluser **up;
6190 char *name;
6192 struct inputsu *i;
6194 if (!(i = (struct inputsu *)calloc(1, sizeof(struct inputsu))))
6195 return -1;
6197 i->up = up;
6198 if (name && *name)
6199 su_fin(name, (int)strlen(name), (char *)i); /* can also initialise stuff */
6200 else
6201 su_fin((char *)0, 0, (char *)i);
6202 return 0;
6204 #endif /* MULTIUSER */
6206 #ifdef PASSWORD
6208 static void
6209 pass1(buf, len, data)
6210 char *buf;
6211 int len;
6212 char *data;
6214 struct acluser *u = (struct acluser *)data;
6216 if (!*buf)
6217 return;
6218 ASSERT(u);
6219 if (u->u_password != NullStr)
6220 free((char *)u->u_password);
6221 u->u_password = SaveStr(buf);
6222 bzero(buf, strlen(buf));
6223 Input("Retype new password:", 100, INP_NOECHO, pass2, data, 0);
6226 static void
6227 pass2(buf, len, data)
6228 char *buf;
6229 int len;
6230 char *data;
6232 int st;
6233 char salt[3];
6234 struct acluser *u = (struct acluser *)data;
6236 ASSERT(u);
6237 if (!buf || strcmp(u->u_password, buf))
6239 Msg(0, "[ Passwords don't match - checking turned off ]");
6240 if (u->u_password != NullStr)
6242 bzero(u->u_password, strlen(u->u_password));
6243 free((char *)u->u_password);
6245 u->u_password = NullStr;
6247 else if (u->u_password[0] == '\0')
6249 Msg(0, "[ No password - no secure ]");
6250 if (buf)
6251 bzero(buf, strlen(buf));
6254 if (u->u_password != NullStr)
6256 for (st = 0; st < 2; st++)
6257 salt[st] = 'A' + (int)((time(0) >> 6 * st) % 26);
6258 salt[2] = 0;
6259 buf = crypt(u->u_password, salt);
6260 bzero(u->u_password, strlen(u->u_password));
6261 free((char *)u->u_password);
6262 u->u_password = SaveStr(buf);
6263 bzero(buf, strlen(buf));
6264 #ifdef COPY_PASTE
6265 if (u->u_plop.buf)
6266 UserFreeCopyBuffer(u);
6267 u->u_plop.len = strlen(u->u_password);
6268 # ifdef ENCODINGS
6269 u->u_plop.enc = 0;
6270 #endif
6271 if (!(u->u_plop.buf = SaveStr(u->u_password)))
6273 Msg(0, strnomem);
6274 D_user->u_plop.len = 0;
6276 else
6277 Msg(0, "[ Password moved into copybuffer ]");
6278 #else /* COPY_PASTE */
6279 Msg(0, "[ Crypted password is \"%s\" ]", u->u_password);
6280 #endif /* COPY_PASTE */
6283 #endif /* PASSWORD */
6285 static int
6286 digraph_find(buf)
6287 const char *buf;
6289 int i;
6290 for (i = 0; i < MAX_DIGRAPH && digraphs[i].d[0]; i++)
6291 if ((digraphs[i].d[0] == (unsigned char)buf[0] && digraphs[i].d[1] == (unsigned char)buf[1]) ||
6292 (digraphs[i].d[0] == (unsigned char)buf[1] && digraphs[i].d[1] == (unsigned char)buf[0]))
6293 break;
6294 return i;
6297 static void
6298 digraph_fn(buf, len, data)
6299 char *buf;
6300 int len;
6301 char *data; /* dummy */
6303 int ch, i, x;
6305 ch = buf[len];
6306 if (ch)
6308 buf[len + 1] = ch; /* so we can restore it later */
6309 if (ch < ' ' || ch == '\177')
6310 return;
6311 if (len >= 1 && ((*buf == 'U' && buf[1] == '+') || (*buf == '0' && (buf[1] == 'x' || buf[1] == 'X'))))
6313 if (len == 1)
6314 return;
6315 if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f') && (ch < 'A' || ch > 'F'))
6317 buf[len] = '\034'; /* ^] is ignored by Input() */
6318 return;
6320 if (len == (*buf == 'U' ? 5 : 3))
6321 buf[len] = '\n';
6322 return;
6324 if (len && *buf == '0')
6326 if (ch < '0' || ch > '7')
6328 buf[len] = '\034'; /* ^] is ignored by Input() */
6329 return;
6331 if (len == 3)
6332 buf[len] = '\n';
6333 return;
6335 if (len == 1)
6336 buf[len] = '\n';
6337 return;
6339 if (len < 1)
6340 return;
6341 if (buf[len + 1])
6343 buf[len] = buf[len + 1]; /* stored above */
6344 len++;
6346 if (len < 2)
6347 return;
6348 if (!parse_input_int(buf, len, &x))
6350 i = digraph_find(buf);
6351 if ((x = digraphs[i].value) <= 0)
6353 Msg(0, "Unknown digraph");
6354 return;
6357 i = 1;
6358 *buf = x;
6359 #ifdef UTF8
6360 if (flayer->l_encoding == UTF8)
6361 i = ToUtf8(buf, x); /* buf is big enough for all UTF-8 codes */
6362 #endif
6363 while(i)
6364 LayProcess(&buf, &i);
6367 #ifdef MAPKEYS
6369 StuffKey(i)
6370 int i;
6372 struct action *act;
6373 int discard = 0;
6375 debug1("StuffKey #%d", i);
6376 #ifdef DEBUG
6377 if (i < KMAP_KEYS)
6378 debug1(" - %s", term[i + T_CAPS].tcname);
6379 #endif
6381 if (i < KMAP_KEYS && D_ESCseen)
6383 struct action *act = &D_ESCseen[i + 256];
6384 if (act->nr != RC_ILLEGAL)
6386 D_ESCseen = 0;
6387 DoAction(act, i + 256);
6388 return 0;
6390 discard = 1;
6393 if (i >= T_CURSOR - T_CAPS && i < T_KEYPAD - T_CAPS && D_cursorkeys)
6394 i += T_OCAPS - T_CURSOR;
6395 else if (i >= T_KEYPAD - T_CAPS && i < T_OCAPS - T_CAPS && D_keypad)
6396 i += T_OCAPS - T_CURSOR;
6397 debug1(" - action %d\n", i);
6398 flayer = D_forecv->c_layer;
6399 fore = D_fore;
6400 act = 0;
6401 #ifdef COPY_PASTE
6402 if (flayer && flayer->l_mode == 1)
6403 act = i < KMAP_KEYS+KMAP_AKEYS ? &mmtab[i] : &kmap_exts[i - (KMAP_KEYS+KMAP_AKEYS)].mm;
6404 #endif
6405 if ((!act || act->nr == RC_ILLEGAL) && !D_mapdefault)
6406 act = i < KMAP_KEYS+KMAP_AKEYS ? &umtab[i] : &kmap_exts[i - (KMAP_KEYS+KMAP_AKEYS)].um;
6407 if (!act || act->nr == RC_ILLEGAL)
6408 act = i < KMAP_KEYS+KMAP_AKEYS ? &dmtab[i] : &kmap_exts[i - (KMAP_KEYS+KMAP_AKEYS)].dm;
6410 if (discard && (!act || act->nr != RC_COMMAND))
6412 D_ESCseen = 0;
6413 return 0;
6415 D_mapdefault = 0;
6417 if (act == 0 || act->nr == RC_ILLEGAL)
6418 return -1;
6419 DoAction(act, 0);
6420 return 0;
6422 #endif
6425 static int
6426 IsOnDisplay(wi)
6427 struct win *wi;
6429 struct canvas *cv;
6430 ASSERT(display);
6431 for (cv = D_cvlist; cv; cv = cv->c_next)
6432 if (Layer2Window(cv->c_layer) == wi)
6433 return 1;
6434 return 0;
6437 struct win *
6438 FindNiceWindow(wi, presel)
6439 struct win *wi;
6440 char *presel;
6442 int i;
6444 debug2("FindNiceWindow %d %s\n", wi ? wi->w_number : -1 , presel ? presel : "NULL");
6445 if (presel)
6447 i = WindowByNoN(presel);
6448 if (i >= 0)
6449 wi = wtab[i];
6451 if (!display)
6452 return wi;
6453 #ifdef MULTIUSER
6454 if (wi && AclCheckPermWin(D_user, ACL_READ, wi))
6455 wi = 0;
6456 #endif
6457 if (!wi || (IsOnDisplay(wi) && !presel))
6459 /* try to get another window */
6460 wi = 0;
6461 #ifdef MULTIUSER
6462 for (wi = windows; wi; wi = wi->w_next)
6463 if (!wi->w_layer.l_cvlist && !AclCheckPermWin(D_user, ACL_WRITE, wi))
6464 break;
6465 if (!wi)
6466 for (wi = windows; wi; wi = wi->w_next)
6467 if (wi->w_layer.l_cvlist && !IsOnDisplay(wi) && !AclCheckPermWin(D_user, ACL_WRITE, wi))
6468 break;
6469 if (!wi)
6470 for (wi = windows; wi; wi = wi->w_next)
6471 if (!wi->w_layer.l_cvlist && !AclCheckPermWin(D_user, ACL_READ, wi))
6472 break;
6473 if (!wi)
6474 for (wi = windows; wi; wi = wi->w_next)
6475 if (wi->w_layer.l_cvlist && !IsOnDisplay(wi) && !AclCheckPermWin(D_user, ACL_READ, wi))
6476 break;
6477 #endif
6478 if (!wi)
6479 for (wi = windows; wi; wi = wi->w_next)
6480 if (!wi->w_layer.l_cvlist)
6481 break;
6482 if (!wi)
6483 for (wi = windows; wi; wi = wi->w_next)
6484 if (wi->w_layer.l_cvlist && !IsOnDisplay(wi))
6485 break;
6487 #ifdef MULTIUSER
6488 if (wi && AclCheckPermWin(D_user, ACL_READ, wi))
6489 wi = 0;
6490 #endif
6491 return wi;
6494 #if 0
6496 /* sorted list of all commands */
6497 static struct comm **commtab;
6498 static int ncommtab;
6500 void
6501 AddComms(cos, hand)
6502 struct comm *cos;
6503 void (*hand) __P((struct comm *, char **, int));
6505 int n, i, j, r;
6506 for (n = 0; cos[n].name; n++)
6508 if (n == 0)
6509 return;
6510 if (commtab)
6511 commtab = (struct commt *)realloc(commtab, sizeof(*commtab) * (ncommtab + n));
6512 else
6513 commtab = (struct commt *)malloc(sizeof(*commtab) * (ncommtab + n));
6514 if (!commtab)
6515 Panic(0, strnomem);
6516 for (i = 0; i < n; i++)
6518 for (j = 0; j < ncommtab; j++)
6520 r = strcmp(cos[i].name, commtab[j]->name);
6521 if (r == 0)
6522 Panic(0, "Duplicate command: %s\n", cos[i].name);
6523 if (r < 0)
6524 break;
6526 for (r = ncommtab; r > j; r--)
6527 commtab[r] = commtab[r - 1];
6528 commtab[j] = cos + i;
6529 cos[i].handler = hand;
6530 bzero(cos[i].userbits, sizeof(cos[i].userbits));
6531 ncommtab++;
6535 struct comm *
6536 FindComm(str)
6537 char *str;
6539 int x, m, l = 0, r = ncommtab - 1;
6540 while (l <= r)
6542 m = (l + r) / 2;
6543 x = strcmp(str, commtab[m]->name);
6544 if (x > 0)
6545 l = m + 1;
6546 else if (x < 0)
6547 r = m - 1;
6548 else
6549 return commtab[m];
6551 return 0;
6554 #endif
6556 static int
6557 CalcSlicePercent(cv, percent)
6558 struct canvas *cv;
6559 int percent;
6561 int w, wsum, up;
6562 if (!cv || !cv->c_slback)
6563 return percent;
6564 up = CalcSlicePercent(cv->c_slback->c_slback, percent);
6565 w = cv->c_slweight;
6566 for (cv = cv->c_slback->c_slperp, wsum = 0; cv; cv = cv->c_slnext)
6567 wsum += cv->c_slweight;
6568 if (wsum == 0)
6569 return 0;
6570 return (up * w) / wsum;
6573 static int
6574 ChangeCanvasSize(fcv, abs, diff, gflag, percent)
6575 struct canvas *fcv; /* make this canvas bigger */
6576 int abs; /* mode: 0:rel 1:abs 2:max */
6577 int diff; /* change this much */
6578 int gflag; /* go up if neccessary */
6579 int percent;
6581 struct canvas *cv;
6582 int done, have, m, dir;
6584 debug3("ChangeCanvasSize abs %d diff %d percent=%d\n", abs, diff, percent);
6585 if (abs == 0 && diff == 0)
6586 return 0;
6587 if (abs == 2)
6589 if (diff == 0)
6590 fcv->c_slweight = 0;
6591 else
6593 for (cv = fcv->c_slback->c_slperp; cv; cv = cv->c_slnext)
6594 cv->c_slweight = 0;
6595 fcv->c_slweight = 1;
6596 cv = fcv->c_slback->c_slback;
6597 if (gflag && cv && cv->c_slback)
6598 ChangeCanvasSize(cv, abs, diff, gflag, percent);
6600 return diff;
6602 if (abs)
6604 if (diff < 0)
6605 diff = 0;
6606 if (percent && diff > percent)
6607 diff = percent;
6609 if (percent)
6611 int wsum, up;
6612 for (cv = fcv->c_slback->c_slperp, wsum = 0; cv; cv = cv->c_slnext)
6613 wsum += cv->c_slweight;
6614 if (wsum)
6616 up = gflag ? CalcSlicePercent(fcv->c_slback->c_slback, percent) : percent;
6617 debug3("up=%d, wsum=%d percent=%d\n", up, wsum, percent);
6618 if (wsum < 1000)
6620 int scale = wsum < 10 ? 1000 : 100;
6621 for (cv = fcv->c_slback->c_slperp; cv; cv = cv->c_slnext)
6622 cv->c_slweight *= scale;
6623 wsum *= scale;
6624 debug1("scaled wsum to %d\n", wsum);
6626 for (cv = fcv->c_slback->c_slperp; cv; cv = cv->c_slnext)
6628 if (cv->c_slweight)
6630 cv->c_slweight = (cv->c_slweight * up) / percent;
6631 if (cv->c_slweight == 0)
6632 cv->c_slweight = 1;
6634 debug1(" - weight %d\n", cv->c_slweight);
6636 diff = (diff * wsum) / percent;
6637 percent = wsum;
6640 else
6642 if (abs && diff == (fcv->c_slorient == SLICE_VERT ? fcv->c_ye - fcv->c_ys + 2 : fcv->c_xe - fcv->c_xs + 2))
6643 return 0;
6644 /* fix weights to real size (can't be helped, sorry) */
6645 for (cv = fcv->c_slback->c_slperp; cv; cv = cv->c_slnext)
6647 cv->c_slweight = cv->c_slorient == SLICE_VERT ? cv->c_ye - cv->c_ys + 2 : cv->c_xe - cv->c_xs + 2;
6648 debug1(" - weight %d\n", cv->c_slweight);
6651 if (abs)
6652 diff = diff - fcv->c_slweight;
6653 debug1("diff = %d\n", diff);
6654 if (diff == 0)
6655 return 0;
6656 if (diff < 0)
6658 cv = fcv->c_slnext ? fcv->c_slnext : fcv->c_slprev;
6659 fcv->c_slweight += diff;
6660 cv->c_slweight -= diff;
6661 return diff;
6663 done = 0;
6664 dir = 1;
6665 for (cv = fcv->c_slnext; diff > 0; cv = dir > 0 ? cv->c_slnext : cv->c_slprev)
6667 if (!cv)
6669 debug1("reached end, dir is %d\n", dir);
6670 if (dir == -1)
6671 break;
6672 dir = -1;
6673 cv = fcv;
6674 continue;
6676 if (percent)
6677 m = 1;
6678 else
6679 m = cv->c_slperp ? CountCanvasPerp(cv) * 2 : 2;
6680 debug2("min is %d, have %d\n", m, cv->c_slweight);
6681 if (cv->c_slweight > m)
6683 have = cv->c_slweight - m;
6684 if (have > diff)
6685 have = diff;
6686 debug1("subtract %d\n", have);
6687 cv->c_slweight -= have;
6688 done += have;
6689 diff -= have;
6692 if (diff && gflag)
6694 /* need more room! */
6695 cv = fcv->c_slback->c_slback;
6696 if (cv && cv->c_slback)
6697 done += ChangeCanvasSize(fcv->c_slback->c_slback, 0, diff, gflag, percent);
6699 fcv->c_slweight += done;
6700 debug1("ChangeCanvasSize returns %d\n", done);
6701 return done;
6704 static void
6705 ResizeRegions(arg, flags)
6706 char *arg;
6707 int flags;
6709 struct canvas *cv;
6710 int diff, l;
6711 int gflag = 0, abs = 0, percent = 0;
6712 int orient = 0;
6714 ASSERT(display);
6715 if (!*arg)
6716 return;
6717 if (D_forecv->c_slorient == SLICE_UNKN)
6719 Msg(0, "resize: need more than one region");
6720 return;
6722 gflag = flags & RESIZE_FLAG_L ? 0 : 1;
6723 orient |= flags & RESIZE_FLAG_H ? SLICE_HORI : 0;
6724 orient |= flags & RESIZE_FLAG_V ? SLICE_VERT : 0;
6725 if (orient == 0)
6726 orient = D_forecv->c_slorient;
6727 l = strlen(arg);
6728 if (*arg == '=')
6730 /* make all regions the same height */
6731 struct canvas *cv = gflag ? &D_canvas : D_forecv->c_slback;
6732 if (cv->c_slperp->c_slorient & orient)
6733 EqualizeCanvas(cv->c_slperp, gflag);
6734 /* can't use cv->c_slorient directly as it can be D_canvas */
6735 if ((cv->c_slperp->c_slorient ^ (SLICE_HORI ^ SLICE_VERT)) & orient)
6737 if (cv->c_slback)
6739 cv = cv->c_slback;
6740 EqualizeCanvas(cv->c_slperp, gflag);
6742 else
6743 EqualizeCanvas(cv, gflag);
6745 ResizeCanvas(cv);
6746 RecreateCanvasChain();
6747 RethinkDisplayViewports();
6748 ResizeLayersToCanvases();
6749 return;
6751 if (!strcmp(arg, "min") || !strcmp(arg, "0"))
6753 abs = 2;
6754 diff = 0;
6756 else if (!strcmp(arg, "max") || !strcmp(arg, "_"))
6758 abs = 2;
6759 diff = 1;
6761 else
6763 if (l > 0 && arg[l - 1] == '%')
6764 percent = 1000;
6765 if (*arg == '+')
6766 diff = atoi(arg + 1);
6767 else if (*arg == '-')
6768 diff = -atoi(arg + 1);
6769 else
6771 diff = atoi(arg); /* +1 because of caption line */
6772 if (diff < 0)
6773 diff = 0;
6774 abs = diff == 0 ? 2 : 1;
6777 if (!abs && !diff)
6778 return;
6779 if (percent)
6780 diff = diff * percent / 100;
6781 cv = D_forecv;
6782 if (cv->c_slorient & orient)
6783 ChangeCanvasSize(cv, abs, diff, gflag, percent);
6784 if (cv->c_slback->c_slorient & orient)
6785 ChangeCanvasSize(cv->c_slback, abs, diff, gflag, percent);
6787 ResizeCanvas(&D_canvas);
6788 RecreateCanvasChain();
6789 RethinkDisplayViewports();
6790 ResizeLayersToCanvases();
6791 return;
6793 #if 0
6795 if (siz + diff < 1)
6796 diff = 1 - siz;
6797 if (siz + diff > dsize - (nreg - 1) * 2 - 1)
6798 diff = dsize - (nreg - 1) * 2 - 1 - siz;
6799 if (diff == 0 || siz + diff < 1)
6800 return;
6802 if (diff < 0)
6804 if (D_forecv->c_next)
6806 D_forecv->c_ye += diff;
6807 D_forecv->c_next->c_ys += diff;
6808 D_forecv->c_next->c_yoff += diff;
6810 else
6812 for (cv = D_cvlist; cv; cv = cv->c_next)
6813 if (cv->c_next == D_forecv)
6814 break;
6815 ASSERT(cv);
6816 cv->c_ye -= diff;
6817 D_forecv->c_ys -= diff;
6818 D_forecv->c_yoff -= diff;
6821 else
6823 int s, i = 0, found = 0, di = diff, d2;
6824 s = dsize - (nreg - 1) * 2 - 1 - siz;
6825 for (cv = D_cvlist; cv; i = cv->c_ye + 2, cv = cv->c_next)
6827 if (cv == D_forecv)
6829 cv->c_ye = i + (cv->c_ye - cv->c_ys) + diff;
6830 cv->c_yoff -= cv->c_ys - i;
6831 cv->c_ys = i;
6832 found = 1;
6833 continue;
6835 s -= cv->c_ye - cv->c_ys;
6836 if (!found)
6838 if (s >= di)
6839 continue;
6840 d2 = di - s;
6842 else
6843 d2 = di > cv->c_ye - cv->c_ys ? cv->c_ye - cv->c_ys : di;
6844 di -= d2;
6845 cv->c_ye = i + (cv->c_ye - cv->c_ys) - d2;
6846 cv->c_yoff -= cv->c_ys - i;
6847 cv->c_ys = i;
6850 RethinkDisplayViewports();
6851 ResizeLayersToCanvases();
6852 #endif
6855 static void
6856 ResizeFin(buf, len, data)
6857 char *buf;
6858 int len;
6859 char *data;
6861 int ch;
6862 int flags = *(int *)data;
6863 ch = ((unsigned char *)buf)[len];
6864 if (ch == 0)
6866 ResizeRegions(buf, flags);
6867 return;
6869 if (ch == 'h')
6870 flags ^= RESIZE_FLAG_H;
6871 else if (ch == 'v')
6872 flags ^= RESIZE_FLAG_V;
6873 else if (ch == 'b')
6874 flags |= RESIZE_FLAG_H|RESIZE_FLAG_V;
6875 else if (ch == 'p')
6876 flags ^= D_forecv->c_slorient == SLICE_VERT ? RESIZE_FLAG_H : RESIZE_FLAG_V;
6877 else if (ch == 'l')
6878 flags ^= RESIZE_FLAG_L;
6879 else
6880 return;
6881 inp_setprompt(resizeprompts[flags], NULL);
6882 *(int *)data = flags;
6883 buf[len] = '\034';
6886 void
6887 SetForeCanvas(d, cv)
6888 struct display *d;
6889 struct canvas *cv;
6891 struct display *odisplay = display;
6892 if (d->d_forecv == cv)
6893 return;
6895 display = d;
6896 D_forecv = cv;
6897 if ((focusminwidth && (focusminwidth < 0 || D_forecv->c_xe - D_forecv->c_xs + 1 < focusminwidth)) ||
6898 (focusminheight && (focusminheight < 0 || D_forecv->c_ye - D_forecv->c_ys + 1 < focusminheight)))
6900 ResizeCanvas(&D_canvas);
6901 RecreateCanvasChain();
6902 RethinkDisplayViewports();
6903 ResizeLayersToCanvases(); /* redisplays */
6905 fore = D_fore = Layer2Window(D_forecv->c_layer);
6906 if (D_other == fore)
6907 D_other = 0;
6908 flayer = D_forecv->c_layer;
6909 #ifdef RXVT_OSC
6910 if (D_xtermosc[2] || D_xtermosc[3])
6912 Activate(-1);
6914 else
6915 #endif
6917 RefreshHStatus();
6918 #ifdef RXVT_OSC
6919 RefreshXtermOSC();
6920 #endif
6921 flayer = D_forecv->c_layer;
6922 CV_CALL(D_forecv, LayRestore();LaySetCursor());
6923 WindowChanged(0, 'F');
6926 display = odisplay;
6929 #ifdef RXVT_OSC
6930 void
6931 RefreshXtermOSC()
6933 int i;
6934 struct win *p;
6936 p = Layer2Window(D_forecv->c_layer);
6937 for (i = 3; i >=0; i--)
6938 SetXtermOSC(i, p ? p->w_xtermosc[i] : 0);
6940 #endif
6943 ParseAttrColor(s1, s2, msgok)
6944 char *s1, *s2;
6945 int msgok;
6947 int i, n;
6948 char *s, *ss;
6949 int r = 0;
6951 s = s1;
6952 while (*s == ' ')
6953 s++;
6954 ss = s;
6955 while (*ss && *ss != ' ')
6956 ss++;
6957 while (*ss == ' ')
6958 ss++;
6959 if (*s && (s2 || *ss || !((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') || *s == '.')))
6961 int mode = 0, n = 0;
6962 if (*s == '+')
6964 mode = 1;
6965 s++;
6967 else if (*s == '-')
6969 mode = -1;
6970 s++;
6972 else if (*s == '!')
6974 mode = 2;
6975 s++;
6977 else if (*s == '=')
6978 s++;
6979 if (*s >= '0' && *s <= '9')
6981 n = *s++ - '0';
6982 if (*s >= '0' && *s <= '9')
6983 n = n * 16 + (*s++ - '0');
6984 else if (*s >= 'a' && *s <= 'f')
6985 n = n * 16 + (*s++ - ('a' - 10));
6986 else if (*s >= 'A' && *s <= 'F')
6987 n = n * 16 + (*s++ - ('A' - 10));
6988 else if (*s && *s != ' ')
6990 if (msgok)
6991 Msg(0, "Illegal attribute hexchar '%c'", *s);
6992 return -1;
6995 else
6997 while (*s && *s != ' ')
6999 if (*s == 'd')
7000 n |= A_DI;
7001 else if (*s == 'u')
7002 n |= A_US;
7003 else if (*s == 'b')
7004 n |= A_BD;
7005 else if (*s == 'r')
7006 n |= A_RV;
7007 else if (*s == 's')
7008 n |= A_SO;
7009 else if (*s == 'B')
7010 n |= A_BL;
7011 else
7013 if (msgok)
7014 Msg(0, "Illegal attribute specifier '%c'", *s);
7015 return -1;
7017 s++;
7020 if (*s && *s != ' ')
7022 if (msgok)
7023 Msg(0, "junk after attribute description: '%c'", *s);
7024 return -1;
7026 if (mode == -1)
7027 r = n << 8 | n;
7028 else if (mode == 1)
7029 r = n << 8;
7030 else if (mode == 2)
7031 r = n;
7032 else if (mode == 0)
7033 r = 0xffff ^ n;
7035 while (*s && *s == ' ')
7036 s++;
7038 if (s2)
7040 if (*s)
7042 if (msgok)
7043 Msg(0, "junk after description: '%c'", *s);
7044 return -1;
7046 s = s2;
7047 while (*s && *s == ' ')
7048 s++;
7051 #ifdef COLOR
7052 if (*s)
7054 static char costr[] = "krgybmcw d i.01234567 9 f FKRGYBMCW I ";
7055 int numco = 0, j;
7057 n = 0;
7058 if (*s == '.')
7060 numco++;
7061 n = 0x0f;
7062 s++;
7064 for (j = 0; j < 2 && *s && *s != ' '; j++)
7066 for (i = 0; costr[i]; i++)
7067 if (*s == costr[i])
7068 break;
7069 if (!costr[i])
7071 if (msgok)
7072 Msg(0, "illegal color descriptor: '%c'", *s);
7073 return -1;
7075 numco++;
7076 n = n << 4 | (i & 15);
7077 #ifdef COLORS16
7078 if (i >= 48)
7079 n = (n & 0x20ff) | 0x200;
7080 #endif
7081 s++;
7083 if ((n & 0xf00) == 0xf00)
7084 n ^= 0xf00; /* clear superflous bits */
7085 #ifdef COLORS16
7086 if (n & 0x2000)
7087 n ^= 0x2400; /* shift bit into right position */
7088 #endif
7089 if (numco == 1)
7090 n |= 0xf0; /* don't change bg color */
7091 if (numco != 2 && n != 0xff)
7092 n |= 0x100; /* special invert mode */
7093 if (*s && *s != ' ')
7095 if (msgok)
7096 Msg(0, "junk after color description: '%c'", *s);
7097 return -1;
7099 n ^= 0xff;
7100 r |= n << 16;
7102 #endif
7104 while (*s && *s == ' ')
7105 s++;
7106 if (*s)
7108 if (msgok)
7109 Msg(0, "junk after description: '%c'", *s);
7110 return -1;
7112 debug1("ParseAttrColor %06x\n", r);
7113 return r;
7117 * Color coding:
7118 * 0-7 normal colors
7119 * 9 default color
7120 * e just set intensity
7121 * f don't change anything
7122 * Intensity is encoded into bits 17(fg) and 18(bg).
7124 void
7125 ApplyAttrColor(i, mc)
7126 int i;
7127 struct mchar *mc;
7129 debug1("ApplyAttrColor %06x\n", i);
7130 mc->attr |= i >> 8 & 255;
7131 mc->attr ^= i & 255;
7132 #ifdef COLOR
7133 i = (i >> 16) ^ 0xff;
7134 if ((i & 0x100) != 0)
7136 i &= 0xeff;
7137 if (mc->attr & (A_SO|A_RV))
7138 # ifdef COLORS16
7139 i = ((i & 0x0f) << 4) | ((i & 0xf0) >> 4) | ((i & 0x200) << 1) | ((i & 0x400) >> 1);
7140 # else
7141 i = ((i & 0x0f) << 4) | ((i & 0xf0) >> 4);
7142 # endif
7144 # ifdef COLORS16
7145 if ((i & 0x0f) != 0x0f)
7146 mc->attr = (mc->attr & 0xbf) | ((i >> 3) & 0x40);
7147 if ((i & 0xf0) != 0xf0)
7148 mc->attr = (mc->attr & 0x7f) | ((i >> 3) & 0x80);
7149 # endif
7150 mc->color = 0x99 ^ mc->color;
7151 if ((i & 0x0e) == 0x0e)
7152 i = (i & 0xf0) | (mc->color & 0x0f);
7153 if ((i & 0xe0) == 0xe0)
7154 i = (i & 0x0f) | (mc->color & 0xf0);
7155 mc->color = 0x99 ^ i;
7156 debug2("ApplyAttrColor - %02x %02x\n", mc->attr, i);
7157 #endif