Add 'remove' subcommand for 'layout'
[screen-lua.git] / src / process.c
blobf1a9353a850d6928e31bca2d5067060114b7b801
1 /* Copyright (c) 1993-2002
2 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
3 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
4 * Copyright (c) 1987 Oliver Laumann
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program (see the file COPYING); if not, write to the
18 * Free Software Foundation, Inc.,
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 ****************************************************************
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <signal.h>
27 #include <fcntl.h>
28 #if !defined(sun) && !defined(B43) && !defined(ISC) && !defined(pyr) && !defined(_CX_UX)
29 # include <time.h>
30 #endif
31 #include <sys/time.h>
32 #ifndef sun
33 #include <sys/ioctl.h>
34 #endif
37 #include "config.h"
39 /* for solaris 2.1, Unixware (SVR4.2) and possibly others: */
40 #ifdef HAVE_STROPTS_H
41 # include <sys/stropts.h>
42 #endif
44 #include "screen.h"
45 #include "extern.h"
46 #include "logfile.h"
48 extern struct comm comms[];
49 extern char *rc_name;
50 extern char *RcFileName, *home;
51 extern char *BellString, *ActivityString, *ShellProg, *ShellArgs[];
52 extern char *hstatusstring, *captionstring, *timestring;
53 extern char *wliststr, *wlisttit;
54 extern int captionalways;
55 extern char *hardcopydir, *screenlogfile, *logtstamp_string;
56 extern int log_flush, logtstamp_on, logtstamp_after;
57 extern char *VisualBellString;
58 extern int VBellWait, MsgWait, MsgMinWait, SilenceWait;
59 extern char SockPath[], *SockName;
60 extern int TtyMode, auto_detach, use_altscreen;
61 extern int iflag, maxwin;
62 extern int focusminwidth, focusminheight;
63 extern int use_hardstatus, visual_bell;
64 #ifdef COLOR
65 extern int attr2color[][4];
66 extern int nattr2color;
67 #endif
68 extern int hardstatusemu;
69 extern char *printcmd;
70 extern int default_startup;
71 extern int defobuflimit;
72 extern int defnonblock;
73 extern int ZombieKey_destroy;
74 extern int ZombieKey_resurrect;
75 extern int ZombieKey_onerror;
76 #ifdef AUTO_NUKE
77 extern int defautonuke;
78 #endif
79 extern int separate_sids;
80 extern struct NewWindow nwin_default, nwin_undef;
81 #ifdef COPY_PASTE
82 extern int join_with_cr;
83 extern int compacthist;
84 extern int search_ic;
85 # ifdef FONT
86 extern int pastefont;
87 # endif
88 extern unsigned char mark_key_tab[];
89 extern char *BufferFile;
90 #endif
91 #ifdef POW_DETACH
92 extern char *BufferFile, *PowDetachString;
93 #endif
94 #ifdef MULTIUSER
95 extern struct acluser *EffectiveAclUser; /* acl.c */
96 #endif
97 extern struct term term[]; /* terminal capabilities */
98 #ifdef MAPKEYS
99 extern char *kmapdef[];
100 extern char *kmapadef[];
101 extern char *kmapmdef[];
102 #endif
103 extern struct mchar mchar_so, mchar_null;
104 extern int VerboseCreate;
105 #ifdef UTF8
106 extern char *screenencodings;
107 #endif
108 #ifdef DW_CHARS
109 extern int cjkwidth;
110 #endif
112 static int CheckArgNum __P((int, char **));
113 static void ClearAction __P((struct action *));
114 static void SaveAction __P((struct action *, int, char **, int *));
115 static int NextWindow __P((void));
116 static int PreviousWindow __P((void));
117 static int MoreWindows __P((void));
118 static void LogToggle __P((int));
119 static void ShowInfo __P((void));
120 static void ShowDInfo __P((void));
121 static struct win *WindowByName __P((char *));
122 static int WindowByNumber __P((char *));
123 static int ParseOnOff __P((struct action *, int *));
124 static int ParseWinNum __P((struct action *, int *));
125 static int ParseBase __P((struct action *, char *, int *, int, char *));
126 static int ParseNum1000 __P((struct action *, int *));
127 static char **SaveArgs __P((char **));
128 static int IsNum __P((char *, int));
129 static void Colonfin __P((char *, int, char *));
130 static void InputSelect __P((void));
131 static void InputSetenv __P((char *));
132 static void InputAKA __P((void));
133 #ifdef MULTIUSER
134 static int InputSu __P((struct win *, struct acluser **, char *));
135 static void su_fin __P((char *, int, char *));
136 #endif
137 static void AKAfin __P((char *, int, char *));
138 #ifdef COPY_PASTE
139 static void copy_reg_fn __P((char *, int, char *));
140 static void ins_reg_fn __P((char *, int, char *));
141 #endif
142 static void process_fn __P((char *, int, char *));
143 #ifdef PASSWORD
144 static void pass1 __P((char *, int, char *));
145 static void pass2 __P((char *, int, char *));
146 #endif
147 #ifdef POW_DETACH
148 static void pow_detach_fn __P((char *, int, char *));
149 #endif
150 static void digraph_fn __P((char *, int, char *));
151 static void confirm_fn __P((char *, int, char *));
152 static int IsOnDisplay __P((struct win *));
153 static void ResizeRegions __P((char *, int));
154 static void ResizeFin __P((char *, int, char *));
155 static struct action *FindKtab __P((char *, int));
156 static void SelectFin __P((char *, int, char *));
157 static void SelectLayoutFin __P((char *, int, char *));
160 extern struct layer *flayer;
161 extern struct display *display, *displays;
162 extern struct win *fore, *console_window, *windows;
163 extern struct acluser *users;
164 extern struct layout *layouts, *layout_attach, layout_last_marker;
165 extern struct layout *laytab[];
167 extern char screenterm[], HostName[], version[];
168 extern struct NewWindow nwin_undef, nwin_default;
169 extern struct LayFuncs WinLf, MarkLf;
171 extern int Z0width, Z1width;
172 extern int real_uid, real_gid;
174 #ifdef NETHACK
175 extern int nethackflag;
176 #endif
179 struct win *wtab[MAXWIN]; /* window table, should be dynamic */
181 #ifdef MULTIUSER
182 extern char *multi;
183 extern int maxusercount;
184 #endif
185 char NullStr[] = "";
187 struct plop plop_tab[MAX_PLOP_DEFS];
189 #ifndef PTYMODE
190 # define PTYMODE 0622
191 #endif
193 int TtyMode = PTYMODE;
194 int hardcopy_append = 0;
195 int all_norefresh = 0;
196 #ifdef ZMODEM
197 int zmodem_mode = 0;
198 char *zmodem_sendcmd;
199 char *zmodem_recvcmd;
200 static char *zmodes[4] = {"off", "auto", "catch", "pass"};
201 #endif
203 int idletimo;
204 struct action idleaction;
205 #ifdef BLANKER_PRG
206 char **blankerprg;
207 #endif
209 struct action ktab[256 + KMAP_KEYS]; /* command key translation table */
210 struct kclass {
211 struct kclass *next;
212 char *name;
213 struct action ktab[256 + KMAP_KEYS];
215 struct kclass *kclasses;
217 #ifdef MAPKEYS
218 struct action umtab[KMAP_KEYS+KMAP_AKEYS];
219 struct action dmtab[KMAP_KEYS+KMAP_AKEYS];
220 struct action mmtab[KMAP_KEYS+KMAP_AKEYS];
221 struct kmap_ext *kmap_exts;
222 int kmap_extn;
223 static int maptimeout = 300;
224 #endif
227 /* digraph table taken from old vim and rfc1345 */
228 static const unsigned char digraphs[][3] = {
229 {' ', ' ', 160}, /*   */
230 {'N', 'S', 160}, /*   */
231 {'~', '!', 161}, /* ¡ */
232 {'!', '!', 161}, /* ¡ */
233 {'!', 'I', 161}, /* ¡ */
234 {'c', '|', 162}, /* ¢ */
235 {'c', 't', 162}, /* ¢ */
236 {'$', '$', 163}, /* £ */
237 {'P', 'd', 163}, /* £ */
238 {'o', 'x', 164}, /* ¤ */
239 {'C', 'u', 164}, /* ¤ */
240 {'C', 'u', 164}, /* ¤ */
241 {'E', 'u', 164}, /* ¤ */
242 {'Y', '-', 165}, /* ¥ */
243 {'Y', 'e', 165}, /* ¥ */
244 {'|', '|', 166}, /* ¦ */
245 {'B', 'B', 166}, /* ¦ */
246 {'p', 'a', 167}, /* § */
247 {'S', 'E', 167}, /* § */
248 {'"', '"', 168}, /* ¨ */
249 {'\'', ':', 168}, /* ¨ */
250 {'c', 'O', 169}, /* © */
251 {'C', 'o', 169}, /* © */
252 {'a', '-', 170}, /* ª */
253 {'<', '<', 171}, /* « */
254 {'-', ',', 172}, /* ¬ */
255 {'N', 'O', 172}, /* ¬ */
256 {'-', '-', 173}, /* ­ */
257 {'r', 'O', 174}, /* ® */
258 {'R', 'g', 174}, /* ® */
259 {'-', '=', 175}, /* ¯ */
260 {'\'', 'm', 175}, /* ¯ */
261 {'~', 'o', 176}, /* ° */
262 {'D', 'G', 176}, /* ° */
263 {'+', '-', 177}, /* ± */
264 {'2', '2', 178}, /* ² */
265 {'2', 'S', 178}, /* ² */
266 {'3', '3', 179}, /* ³ */
267 {'3', 'S', 179}, /* ³ */
268 {'\'', '\'', 180}, /* ´ */
269 {'j', 'u', 181}, /* µ */
270 {'M', 'y', 181}, /* µ */
271 {'p', 'p', 182}, /* ¶ */
272 {'P', 'I', 182}, /* ¶ */
273 {'~', '.', 183}, /* · */
274 {'.', 'M', 183}, /* · */
275 {',', ',', 184}, /* ¸ */
276 {'\'', ',', 184}, /* ¸ */
277 {'1', '1', 185}, /* ¹ */
278 {'1', 'S', 185}, /* ¹ */
279 {'o', '-', 186}, /* º */
280 {'>', '>', 187}, /* » */
281 {'1', '4', 188}, /* ¼ */
282 {'1', '2', 189}, /* ½ */
283 {'3', '4', 190}, /* ¾ */
284 {'~', '?', 191}, /* ¿ */
285 {'?', '?', 191}, /* ¿ */
286 {'?', 'I', 191}, /* ¿ */
287 {'A', '`', 192}, /* À */
288 {'A', '!', 192}, /* À */
289 {'A', '\'', 193}, /* Á */
290 {'A', '^', 194}, /* Â */
291 {'A', '>', 194}, /* Â */
292 {'A', '~', 195}, /* Ã */
293 {'A', '?', 195}, /* Ã */
294 {'A', '"', 196}, /* Ä */
295 {'A', ':', 196}, /* Ä */
296 {'A', '@', 197}, /* Å */
297 {'A', 'A', 197}, /* Å */
298 {'A', 'E', 198}, /* Æ */
299 {'C', ',', 199}, /* Ç */
300 {'E', '`', 200}, /* È */
301 {'E', '!', 200}, /* È */
302 {'E', '\'', 201}, /* É */
303 {'E', '^', 202}, /* Ê */
304 {'E', '>', 202}, /* Ê */
305 {'E', '"', 203}, /* Ë */
306 {'E', ':', 203}, /* Ë */
307 {'I', '`', 204}, /* Ì */
308 {'I', '!', 204}, /* Ì */
309 {'I', '\'', 205}, /* Í */
310 {'I', '^', 206}, /* Î */
311 {'I', '>', 206}, /* Î */
312 {'I', '"', 207}, /* Ï */
313 {'I', ':', 207}, /* Ï */
314 {'D', '-', 208}, /* Ð */
315 {'N', '~', 209}, /* Ñ */
316 {'N', '?', 209}, /* Ñ */
317 {'O', '`', 210}, /* Ò */
318 {'O', '!', 210}, /* Ò */
319 {'O', '\'', 211}, /* Ó */
320 {'O', '^', 212}, /* Ô */
321 {'O', '>', 212}, /* Ô */
322 {'O', '~', 213}, /* Õ */
323 {'O', '?', 213}, /* Õ */
324 {'O', '"', 214}, /* Ö */
325 {'O', ':', 214}, /* Ö */
326 {'/', '\\', 215}, /* × */
327 {'*', 'x', 215}, /* × */
328 {'O', '/', 216}, /* Ø */
329 {'U', '`', 217}, /* Ù */
330 {'U', '!', 217}, /* Ù */
331 {'U', '\'', 218}, /* Ú */
332 {'U', '^', 219}, /* Û */
333 {'U', '>', 219}, /* Û */
334 {'U', '"', 220}, /* Ü */
335 {'U', ':', 220}, /* Ü */
336 {'Y', '\'', 221}, /* Ý */
337 {'I', 'p', 222}, /* Þ */
338 {'T', 'H', 222}, /* Þ */
339 {'s', 's', 223}, /* ß */
340 {'s', '"', 223}, /* ß */
341 {'a', '`', 224}, /* à */
342 {'a', '!', 224}, /* à */
343 {'a', '\'', 225}, /* á */
344 {'a', '^', 226}, /* â */
345 {'a', '>', 226}, /* â */
346 {'a', '~', 227}, /* ã */
347 {'a', '?', 227}, /* ã */
348 {'a', '"', 228}, /* ä */
349 {'a', ':', 228}, /* ä */
350 {'a', 'a', 229}, /* å */
351 {'a', 'e', 230}, /* æ */
352 {'c', ',', 231}, /* ç */
353 {'e', '`', 232}, /* è */
354 {'e', '!', 232}, /* è */
355 {'e', '\'', 233}, /* é */
356 {'e', '^', 234}, /* ê */
357 {'e', '>', 234}, /* ê */
358 {'e', '"', 235}, /* ë */
359 {'e', ':', 235}, /* ë */
360 {'i', '`', 236}, /* ì */
361 {'i', '!', 236}, /* ì */
362 {'i', '\'', 237}, /* í */
363 {'i', '^', 238}, /* î */
364 {'i', '>', 238}, /* î */
365 {'i', '"', 239}, /* ï */
366 {'i', ':', 239}, /* ï */
367 {'d', '-', 240}, /* ð */
368 {'n', '~', 241}, /* ñ */
369 {'n', '?', 241}, /* ñ */
370 {'o', '`', 242}, /* ò */
371 {'o', '!', 242}, /* ò */
372 {'o', '\'', 243}, /* ó */
373 {'o', '^', 244}, /* ô */
374 {'o', '>', 244}, /* ô */
375 {'o', '~', 245}, /* õ */
376 {'o', '?', 245}, /* õ */
377 {'o', '"', 246}, /* ö */
378 {'o', ':', 246}, /* ö */
379 {':', '-', 247}, /* ÷ */
380 {'o', '/', 248}, /* ø */
381 {'u', '`', 249}, /* ù */
382 {'u', '!', 249}, /* ù */
383 {'u', '\'', 250}, /* ú */
384 {'u', '^', 251}, /* û */
385 {'u', '>', 251}, /* û */
386 {'u', '"', 252}, /* ü */
387 {'u', ':', 252}, /* ü */
388 {'y', '\'', 253}, /* ý */
389 {'i', 'p', 254}, /* þ */
390 {'t', 'h', 254}, /* þ */
391 {'y', '"', 255}, /* ÿ */
392 {'y', ':', 255}, /* ÿ */
393 {'"', '[', 196}, /* Ä */
394 {'"', '\\', 214}, /* Ö */
395 {'"', ']', 220}, /* Ü */
396 {'"', '{', 228}, /* ä */
397 {'"', '|', 246}, /* ö */
398 {'"', '}', 252}, /* ü */
399 {'"', '~', 223} /* ß */
402 #define RESIZE_FLAG_H 1
403 #define RESIZE_FLAG_V 2
404 #define RESIZE_FLAG_L 4
406 static char *resizeprompts[] = {
407 "resize # lines: ",
408 "resize -h # lines: ",
409 "resize -v # lines: ",
410 "resize -b # lines: ",
411 "resize -l # lines: ",
412 "resize -l -h # lines: ",
413 "resize -l -v # lines: ",
414 "resize -l -b # lines: ",
417 char *noargs[1];
419 void
420 InitKeytab()
422 register unsigned int i;
423 #ifdef MAPKEYS
424 char *argarr[2];
425 #endif
427 for (i = 0; i < sizeof(ktab)/sizeof(*ktab); i++)
429 ktab[i].nr = RC_ILLEGAL;
430 ktab[i].args = noargs;
431 ktab[i].argl = 0;
433 #ifdef MAPKEYS
434 for (i = 0; i < KMAP_KEYS+KMAP_AKEYS; i++)
436 umtab[i].nr = RC_ILLEGAL;
437 umtab[i].args = noargs;
438 umtab[i].argl = 0;
439 dmtab[i].nr = RC_ILLEGAL;
440 dmtab[i].args = noargs;
441 dmtab[i].argl = 0;
442 mmtab[i].nr = RC_ILLEGAL;
443 mmtab[i].args = noargs;
444 mmtab[i].argl = 0;
446 argarr[1] = 0;
447 for (i = 0; i < NKMAPDEF; i++)
449 if (i + KMAPDEFSTART < T_CAPS)
450 continue;
451 if (i + KMAPDEFSTART >= T_CAPS + KMAP_KEYS)
452 continue;
453 if (kmapdef[i] == 0)
454 continue;
455 argarr[0] = kmapdef[i];
456 SaveAction(dmtab + i + (KMAPDEFSTART - T_CAPS), RC_STUFF, argarr, 0);
458 for (i = 0; i < NKMAPADEF; i++)
460 if (i + KMAPADEFSTART < T_CURSOR)
461 continue;
462 if (i + KMAPADEFSTART >= T_CURSOR + KMAP_AKEYS)
463 continue;
464 if (kmapadef[i] == 0)
465 continue;
466 argarr[0] = kmapadef[i];
467 SaveAction(dmtab + i + (KMAPADEFSTART - T_CURSOR + KMAP_KEYS), RC_STUFF, argarr, 0);
469 for (i = 0; i < NKMAPMDEF; i++)
471 if (i + KMAPMDEFSTART < T_CAPS)
472 continue;
473 if (i + KMAPMDEFSTART >= T_CAPS + KMAP_KEYS)
474 continue;
475 if (kmapmdef[i] == 0)
476 continue;
477 argarr[0] = kmapmdef[i];
478 argarr[1] = 0;
479 SaveAction(mmtab + i + (KMAPMDEFSTART - T_CAPS), RC_STUFF, argarr, 0);
481 #endif
483 ktab['h'].nr = RC_HARDCOPY;
484 #ifdef BSDJOBS
485 ktab['z'].nr = ktab[Ctrl('z')].nr = RC_SUSPEND;
486 #endif
487 ktab['c'].nr = ktab[Ctrl('c')].nr = RC_SCREEN;
488 ktab[' '].nr = ktab[Ctrl(' ')].nr =
489 ktab['n'].nr = ktab[Ctrl('n')].nr = RC_NEXT;
490 ktab['N'].nr = RC_NUMBER;
491 ktab[Ctrl('h')].nr = ktab[0177].nr = ktab['p'].nr = ktab[Ctrl('p')].nr = RC_PREV;
492 ktab['k'].nr = ktab[Ctrl('k')].nr = RC_KILL;
493 ktab['l'].nr = ktab[Ctrl('l')].nr = RC_REDISPLAY;
494 ktab['w'].nr = ktab[Ctrl('w')].nr = RC_WINDOWS;
495 ktab['v'].nr = RC_VERSION;
496 ktab[Ctrl('v')].nr = RC_DIGRAPH;
497 ktab['q'].nr = ktab[Ctrl('q')].nr = RC_XON;
498 ktab['s'].nr = ktab[Ctrl('s')].nr = RC_XOFF;
499 ktab['t'].nr = ktab[Ctrl('t')].nr = RC_TIME;
500 ktab['i'].nr = ktab[Ctrl('i')].nr = RC_INFO;
501 ktab['m'].nr = ktab[Ctrl('m')].nr = RC_LASTMSG;
502 ktab['A'].nr = RC_TITLE;
503 #if defined(UTMPOK) && defined(LOGOUTOK)
504 ktab['L'].nr = RC_LOGIN;
505 #endif
506 ktab[','].nr = RC_LICENSE;
507 ktab['W'].nr = RC_WIDTH;
508 ktab['.'].nr = RC_DUMPTERMCAP;
509 ktab[Ctrl('\\')].nr = RC_QUIT;
510 #ifdef DETACH
511 ktab['d'].nr = ktab[Ctrl('d')].nr = RC_DETACH;
512 # ifdef POW_DETACH
513 ktab['D'].nr = RC_POW_DETACH;
514 # endif
515 #endif
516 ktab['r'].nr = ktab[Ctrl('r')].nr = RC_WRAP;
517 ktab['f'].nr = ktab[Ctrl('f')].nr = RC_FLOW;
518 ktab['C'].nr = RC_CLEAR;
519 ktab['Z'].nr = RC_RESET;
520 ktab['H'].nr = RC_LOG;
521 ktab['M'].nr = RC_MONITOR;
522 ktab['?'].nr = RC_HELP;
523 #ifdef MULTI
524 ktab['*'].nr = RC_DISPLAYS;
525 #endif
527 char *args[2];
528 args[0] = "-";
529 args[1] = NULL;
530 SaveAction(ktab + '-', RC_SELECT, args, 0);
532 for (i = 0; i < ((MAXWIN < 10) ? MAXWIN : 10); i++)
534 char *args[2], arg1[10];
535 args[0] = arg1;
536 args[1] = 0;
537 sprintf(arg1, "%d", i);
538 SaveAction(ktab + '0' + i, RC_SELECT, args, 0);
540 ktab['\''].nr = RC_SELECT; /* calling a window by name */
542 char *args[2];
543 args[0] = "-b";
544 args[1] = 0;
545 SaveAction(ktab + '"', RC_WINDOWLIST, args, 0);
547 ktab[Ctrl('G')].nr = RC_VBELL;
548 ktab[':'].nr = RC_COLON;
549 #ifdef COPY_PASTE
550 ktab['['].nr = ktab[Ctrl('[')].nr = RC_COPY;
552 char *args[2];
553 args[0] = ".";
554 args[1] = 0;
555 SaveAction(ktab + ']', RC_PASTE, args, 0);
556 SaveAction(ktab + Ctrl(']'), RC_PASTE, args, 0);
558 ktab['{'].nr = RC_HISTORY;
559 ktab['}'].nr = RC_HISTORY;
560 ktab['>'].nr = RC_WRITEBUF;
561 ktab['<'].nr = RC_READBUF;
562 ktab['='].nr = RC_REMOVEBUF;
563 #endif
564 #ifdef POW_DETACH
565 ktab['D'].nr = RC_POW_DETACH;
566 #endif
567 #ifdef LOCK
568 ktab['x'].nr = ktab[Ctrl('x')].nr = RC_LOCKSCREEN;
569 #endif
570 ktab['b'].nr = ktab[Ctrl('b')].nr = RC_BREAK;
571 ktab['B'].nr = RC_POW_BREAK;
572 ktab['_'].nr = RC_SILENCE;
573 ktab['S'].nr = RC_SPLIT;
574 ktab['Q'].nr = RC_ONLY;
575 ktab['X'].nr = RC_REMOVE;
576 ktab['F'].nr = RC_FIT;
577 ktab['\t'].nr = RC_FOCUS;
579 char *args[2];
580 args[0] = "up";
581 args[1] = 0;
582 SaveAction(ktab + T_BACKTAB - T_CAPS + 256, RC_FOCUS, args, 0);
585 char *args[2];
586 args[0] = "-v";
587 args[1] = 0;
588 SaveAction(ktab + '|', RC_SPLIT, args, 0);
590 /* These come last; they may want overwrite others: */
591 if (DefaultEsc >= 0)
593 ClearAction(&ktab[DefaultEsc]);
594 ktab[DefaultEsc].nr = RC_OTHER;
596 if (DefaultMetaEsc >= 0)
598 ClearAction(&ktab[DefaultMetaEsc]);
599 ktab[DefaultMetaEsc].nr = RC_META;
602 idleaction.nr = RC_BLANKER;
603 idleaction.args = noargs;
604 idleaction.argl = 0;
607 static struct action *
608 FindKtab(class, create)
609 char *class;
610 int create;
612 struct kclass *kp, **kpp;
613 int i;
615 if (class == 0)
616 return ktab;
617 for (kpp = &kclasses; (kp = *kpp) != 0; kpp = &kp->next)
618 if (!strcmp(kp->name, class))
619 break;
620 if (kp == 0)
622 if (!create)
623 return 0;
624 if (strlen(class) > 80)
626 Msg(0, "Command class name too long.");
627 return 0;
629 kp = malloc(sizeof(*kp));
630 if (kp == 0)
632 Msg(0, strnomem);
633 return 0;
635 kp->name = SaveStr(class);
636 for (i = 0; i < (int)(sizeof(kp->ktab)/sizeof(*kp->ktab)); i++)
638 kp->ktab[i].nr = RC_ILLEGAL;
639 kp->ktab[i].args = noargs;
641 kp->next = 0;
642 *kpp = kp;
644 return kp->ktab;
647 static void
648 ClearAction(act)
649 struct action *act;
651 char **p;
653 if (act->nr == RC_ILLEGAL)
654 return;
655 act->nr = RC_ILLEGAL;
656 if (act->args == noargs)
657 return;
658 for (p = act->args; *p; p++)
659 free(*p);
660 free((char *)act->args);
661 act->args = noargs;
662 act->argl = 0;
666 * ProcessInput: process input from display and feed it into
667 * the layer on canvas D_forecv.
670 #ifdef MAPKEYS
673 * This ProcessInput just does the keybindings and passes
674 * everything else on to ProcessInput2.
677 void
678 ProcessInput(ibuf, ilen)
679 char *ibuf;
680 int ilen;
682 int ch, slen;
683 unsigned char *s, *q;
684 int i, l;
685 char *p;
687 debug1("ProcessInput: %d bytes\n", ilen);
688 if (display == 0 || ilen == 0)
689 return;
690 if (D_seql)
691 evdeq(&D_mapev);
692 slen = ilen;
693 s = (unsigned char *)ibuf;
694 while (ilen-- > 0)
696 ch = *s++;
697 if (D_dontmap || !D_nseqs)
699 D_dontmap = 0;
700 continue;
702 for (;;)
704 debug3("cmp %c %c[%d]\n", ch, *D_seqp, D_seqp - D_kmaps);
705 if (*D_seqp != ch)
707 l = D_seqp[D_seqp[-D_seql-1] + 1];
708 if (l)
710 D_seqp += l * 2 + 4;
711 debug1("miss %d\n", D_seqp - D_kmaps);
712 continue;
714 debug("complete miss\n");
715 D_mapdefault = 0;
716 l = D_seql;
717 p = (char *)D_seqp - l;
718 D_seql = 0;
719 D_seqp = D_kmaps + 3;
720 if (l == 0)
721 break;
722 if ((q = D_seqh) != 0)
724 D_seqh = 0;
725 i = q[0] << 8 | q[1];
726 i &= ~KMAP_NOTIMEOUT;
727 debug1("Mapping former hit #%d - ", i);
728 debug2("%d(%s) - ", q[2], q + 3);
729 if (StuffKey(i))
730 ProcessInput2((char *)q + 3, q[2]);
731 if (display == 0)
732 return;
733 l -= q[2];
734 p += q[2];
736 else
737 D_dontmap = 1;
738 debug1("flush old %d\n", l);
739 ProcessInput(p, l);
740 if (display == 0)
741 return;
742 evdeq(&D_mapev);
743 continue;
745 if (D_seql++ == 0)
747 /* Finish old stuff */
748 slen -= ilen + 1;
749 debug1("finish old %d\n", slen);
750 if (slen)
751 ProcessInput2(ibuf, slen);
752 if (display == 0)
753 return;
754 D_seqh = 0;
756 ibuf = (char *)s;
757 slen = ilen;
758 D_seqp++;
759 l = D_seql;
760 debug2("length am %d, want %d\n", l, D_seqp[-l - 1]);
761 if (l == D_seqp[-l - 1])
763 if (D_seqp[l] != l)
765 q = D_seqp + 1 + l;
766 if (D_kmaps + D_nseqs > q && q[2] > l && !bcmp(D_seqp - l, q + 3, l))
768 debug1("have another mapping (%s), delay execution\n", q + 3);
769 D_seqh = D_seqp - 3 - l;
770 D_seqp = q + 3 + l;
771 break;
774 i = D_seqp[-l - 3] << 8 | D_seqp[-l - 2];
775 i &= ~KMAP_NOTIMEOUT;
776 debug1("Mapping #%d - ", i);
777 p = (char *)D_seqp - l;
778 debug2("%d(%s) - ", l, p);
779 D_seql = 0;
780 D_seqp = D_kmaps + 3;
781 D_seqh = 0;
782 if (StuffKey(i))
783 ProcessInput2(p, l);
784 if (display == 0)
785 return;
787 break;
790 if (D_seql)
792 debug("am in sequence -> check for timeout\n");
793 l = D_seql;
794 for (s = D_seqp; ; s += i * 2 + 4)
796 if (s[-l-3] & KMAP_NOTIMEOUT >> 8)
797 break;
798 if ((i = s[s[-l-1] + 1]) == 0)
800 SetTimeout(&D_mapev, maptimeout);
801 evenq(&D_mapev);
802 break;
806 ProcessInput2(ibuf, slen);
809 #else
810 # define ProcessInput2 ProcessInput
811 #endif
815 * Here only the screen escape commands are handled.
818 void
819 ProcessInput2(ibuf, ilen)
820 char *ibuf;
821 int ilen;
823 char *s;
824 int ch, slen;
825 struct action *ktabp;
827 debug1("ProcessInput2: %d bytes\n", ilen);
828 while (ilen && display)
830 debug1(" - ilen now %d bytes\n", ilen);
831 flayer = D_forecv->c_layer;
832 fore = D_fore;
833 slen = ilen;
834 s = ibuf;
835 if (!D_ESCseen)
837 while (ilen > 0)
839 if ((unsigned char)*s++ == D_user->u_Esc)
840 break;
841 ilen--;
843 slen -= ilen;
844 if (slen)
845 DoProcess(fore, &ibuf, &slen, 0);
846 if (--ilen == 0)
847 D_ESCseen = ktab;
849 if (ilen <= 0)
850 return;
851 ktabp = D_ESCseen ? D_ESCseen : ktab;
852 D_ESCseen = 0;
853 ch = (unsigned char)*s;
856 * As users have different esc characters, but a common ktab[],
857 * we fold back the users esc and meta-esc key to the Default keys
858 * that can be looked up in the ktab[]. grmbl. jw.
859 * XXX: make ktab[] a per user thing.
861 if (ch == D_user->u_Esc)
862 ch = DefaultEsc;
863 else if (ch == D_user->u_MetaEsc)
864 ch = DefaultMetaEsc;
866 if (ch >= 0)
867 DoAction(&ktabp[ch], ch);
868 ibuf = (char *)(s + 1);
869 ilen--;
873 void
874 DoProcess(p, bufp, lenp, pa)
875 struct win *p;
876 char **bufp;
877 int *lenp;
878 struct paster *pa;
880 int oldlen;
881 struct display *d = display;
883 #ifdef COPY_PASTE
884 /* XXX -> PasteStart */
885 if (pa && *lenp > 1 && p && p->w_slowpaste)
887 /* schedule slowpaste event */
888 SetTimeout(&p->w_paster.pa_slowev, p->w_slowpaste);
889 evenq(&p->w_paster.pa_slowev);
890 return;
892 #endif
893 while (flayer && *lenp)
895 #ifdef COPY_PASTE
896 if (!pa && p && p->w_paster.pa_pastelen && flayer == p->w_paster.pa_pastelayer)
898 debug("layer is busy - beep!\n");
899 WBell(p, visual_bell);
900 *bufp += *lenp;
901 *lenp = 0;
902 display = d;
903 return;
905 #endif
906 oldlen = *lenp;
907 LayProcess(bufp, lenp);
908 #ifdef COPY_PASTE
909 if (pa && !pa->pa_pastelayer)
910 break; /* flush rest of paste */
911 #endif
912 if (*lenp == oldlen)
914 if (pa)
916 display = d;
917 return;
919 /* We're full, let's beep */
920 debug("layer is full - beep!\n");
921 WBell(p, visual_bell);
922 break;
925 *bufp += *lenp;
926 *lenp = 0;
927 display = d;
928 #ifdef COPY_PASTE
929 if (pa && pa->pa_pastelen == 0)
930 FreePaster(pa);
931 #endif
935 FindCommnr(str)
936 char *str;
938 int x, m, l = 0, r = RC_LAST;
939 while (l <= r)
941 m = (l + r) / 2;
942 x = strcmp(str, comms[m].name);
943 if (x > 0)
944 l = m + 1;
945 else if (x < 0)
946 r = m - 1;
947 else
948 return m;
950 return RC_ILLEGAL;
953 static int
954 CheckArgNum(nr, args)
955 int nr;
956 char **args;
958 int i, n;
959 static char *argss[] = {"no", "one", "two", "three", "four", "OOPS"};
960 static char *orformat[] =
962 "%s: %s: %s argument%s required",
963 "%s: %s: %s or %s argument%s required",
964 "%s: %s: %s, %s or %s argument%s required",
965 "%s: %s: %s, %s, %s or %s argument%s required"
968 n = comms[nr].flags & ARGS_MASK;
969 for (i = 0; args[i]; i++)
971 if (comms[nr].flags & ARGS_ORMORE)
973 if (i < n)
975 Msg(0, "%s: %s: at least %s argument%s required",
976 rc_name, comms[nr].name, argss[n], n != 1 ? "s" : "");
977 return -1;
980 else if ((comms[nr].flags & ARGS_PLUS1) &&
981 (comms[nr].flags & ARGS_PLUS2) &&
982 (comms[nr].flags & ARGS_PLUS3))
984 if (i != n && i != n + 1 && i != n + 2 && i != n + 3)
986 Msg(0, orformat[3], rc_name, comms[nr].name, argss[n],
987 argss[n + 1], argss[n + 2], argss[n + 3], "");
988 return -1;
991 else if ((comms[nr].flags & ARGS_PLUS1) &&
992 (comms[nr].flags & ARGS_PLUS2))
994 if (i != n && i != n + 1 && i != n + 2)
996 Msg(0, orformat[2], rc_name, comms[nr].name, argss[n],
997 argss[n + 1], argss[n + 2], "");
998 return -1;
1001 else if ((comms[nr].flags & ARGS_PLUS1) &&
1002 (comms[nr].flags & ARGS_PLUS3))
1004 if (i != n && i != n + 1 && i != n + 3)
1006 Msg(0, orformat[2], rc_name, comms[nr].name, argss[n],
1007 argss[n + 1], argss[n + 3], "");
1008 return -1;
1011 else if ((comms[nr].flags & ARGS_PLUS2) &&
1012 (comms[nr].flags & ARGS_PLUS3))
1014 if (i != n && i != n + 2 && i != n + 3)
1016 Msg(0, orformat[2], rc_name, comms[nr].name, argss[n],
1017 argss[n + 2], argss[n + 3], "");
1018 return -1;
1021 else if (comms[nr].flags & ARGS_PLUS1)
1023 if (i != n && i != n + 1)
1025 Msg(0, orformat[1], rc_name, comms[nr].name, argss[n],
1026 argss[n + 1], n != 0 ? "s" : "");
1027 return -1;
1030 else if (comms[nr].flags & ARGS_PLUS2)
1032 if (i != n && i != n + 2)
1034 Msg(0, orformat[1], rc_name, comms[nr].name, argss[n],
1035 argss[n + 2], "s");
1036 return -1;
1039 else if (comms[nr].flags & ARGS_PLUS3)
1041 if (i != n && i != n + 3)
1043 Msg(0, orformat[1], rc_name, comms[nr].name, argss[n],
1044 argss[n + 3], "");
1045 return -1;
1048 else if (i != n)
1050 Msg(0, orformat[0], rc_name, comms[nr].name, argss[n], n != 1 ? "s" : "");
1051 return -1;
1053 return i;
1056 static void
1057 StuffFin(buf, len, data)
1058 char *buf;
1059 int len;
1060 char *data;
1062 if (!flayer)
1063 return;
1064 while(len)
1065 LayProcess(&buf, &len);
1068 /*ARGSUSED*/
1069 void
1070 DoAction(act, key)
1071 struct action *act;
1072 int key;
1074 int nr = act->nr;
1075 char **args = act->args;
1076 int *argl = act->argl;
1077 struct win *p;
1078 int argc, i, n, msgok;
1079 char *s;
1080 char ch;
1081 struct display *odisplay = display;
1082 struct acluser *user;
1084 user = display ? D_user : users;
1085 if (nr == RC_ILLEGAL)
1087 debug1("key '%c': No action\n", key);
1088 return;
1090 n = comms[nr].flags;
1091 if ((n & NEED_DISPLAY) && display == 0)
1093 Msg(0, "%s: %s: display required", rc_name, comms[nr].name);
1094 return;
1096 if ((n & NEED_FORE) && fore == 0)
1098 Msg(0, "%s: %s: window required", rc_name, comms[nr].name);
1099 return;
1101 if ((n & NEED_LAYER) && flayer == 0)
1103 Msg(0, "%s: %s: display or window required", rc_name, comms[nr].name);
1104 return;
1106 if ((argc = CheckArgNum(nr, args)) < 0)
1107 return;
1108 #ifdef MULTIUSER
1109 if (display)
1111 if (AclCheckPermCmd(D_user, ACL_EXEC, &comms[nr]))
1113 Msg(0, "%s: %s: permission denied (user %s)",
1114 rc_name, comms[nr].name, (EffectiveAclUser ? EffectiveAclUser : D_user)->u_name);
1115 return;
1118 #endif /* MULTIUSER */
1120 msgok = display && !*rc_name;
1121 switch(nr)
1123 case RC_SELECT:
1124 if (!*args)
1125 InputSelect();
1126 else if (args[0][0] == '-' && !args[0][1])
1128 SetForeWindow((struct win *)0);
1129 Activate(0);
1131 else if (args[0][0] == '.' && !args[0][1])
1133 if (!fore)
1134 Msg(0, "select . needs a window");
1135 else
1137 SetForeWindow(fore);
1138 Activate(0);
1141 else if (ParseWinNum(act, &n) == 0)
1142 SwitchWindow(n);
1143 break;
1144 #ifdef AUTO_NUKE
1145 case RC_DEFAUTONUKE:
1146 if (ParseOnOff(act, &defautonuke) == 0 && msgok)
1147 Msg(0, "Default autonuke turned %s", defautonuke ? "on" : "off");
1148 if (display && *rc_name)
1149 D_auto_nuke = defautonuke;
1150 break;
1151 case RC_AUTONUKE:
1152 if (ParseOnOff(act, &D_auto_nuke) == 0 && msgok)
1153 Msg(0, "Autonuke turned %s", D_auto_nuke ? "on" : "off");
1154 break;
1155 #endif
1156 case RC_DEFOBUFLIMIT:
1157 if (ParseNum(act, &defobuflimit) == 0 && msgok)
1158 Msg(0, "Default limit set to %d", defobuflimit);
1159 if (display && *rc_name)
1161 D_obufmax = defobuflimit;
1162 D_obuflenmax = D_obuflen - D_obufmax;
1164 break;
1165 case RC_OBUFLIMIT:
1166 if (*args == 0)
1167 Msg(0, "Limit is %d, current buffer size is %d", D_obufmax, D_obuflen);
1168 else if (ParseNum(act, &D_obufmax) == 0 && msgok)
1169 Msg(0, "Limit set to %d", D_obufmax);
1170 D_obuflenmax = D_obuflen - D_obufmax;
1171 break;
1172 case RC_DUMPTERMCAP:
1173 WriteFile(user, (char *)0, DUMP_TERMCAP);
1174 break;
1175 case RC_HARDCOPY:
1177 int mode = DUMP_HARDCOPY;
1179 if (argc > 1 && !strcmp(*args, "-h"))
1181 mode = DUMP_SCROLLBACK;
1182 args++;
1183 argc--;
1185 if (*args && args[1])
1187 Msg(0, "%s: hardcopy: too many arguments", rc_name);
1188 break;
1190 if (fore == 0 && *args == 0)
1191 Msg(0, "%s: hardcopy: window required", rc_name);
1192 else
1193 WriteFile(user, *args, mode);
1195 break;
1196 case RC_DEFLOG:
1197 (void)ParseOnOff(act, &nwin_default.Lflag);
1198 break;
1199 case RC_LOG:
1200 n = fore->w_log ? 1 : 0;
1201 ParseSwitch(act, &n);
1202 LogToggle(n);
1203 break;
1204 #ifdef BSDJOBS
1205 case RC_SUSPEND:
1206 Detach(D_STOP);
1207 break;
1208 #endif
1209 case RC_NEXT:
1210 if (MoreWindows())
1211 SwitchWindow(NextWindow());
1212 break;
1213 case RC_PREV:
1214 if (MoreWindows())
1215 SwitchWindow(PreviousWindow());
1216 break;
1217 case RC_KILL:
1219 char *name;
1221 if (key >= 0)
1223 #ifdef PSEUDOS
1224 Input(fore->w_pwin ? "Really kill this filter [y/n]" : "Really kill this window [y/n]", 1, INP_RAW, confirm_fn, NULL, RC_KILL);
1225 #else
1226 Input("Really kill this window [y/n]", 1, INP_RAW, confirm_fn, NULL, RC_KILL);
1227 #endif
1228 break;
1230 n = fore->w_number;
1231 #ifdef PSEUDOS
1232 if (fore->w_pwin)
1234 FreePseudowin(fore);
1235 Msg(0, "Filter removed.");
1236 break;
1238 #endif
1239 name = SaveStr(fore->w_title);
1240 KillWindow(fore);
1241 Msg(0, "Window %d (%s) killed.", n, name);
1242 if (name)
1243 free(name);
1244 break;
1246 case RC_QUIT:
1247 if (key >= 0)
1249 Input("Really quit and kill all your windows [y/n]", 1, INP_RAW, confirm_fn, NULL, RC_QUIT);
1250 break;
1252 Finit(0);
1253 /* NOTREACHED */
1254 #ifdef DETACH
1255 case RC_DETACH:
1256 if (*args && !strcmp(*args, "-h"))
1257 Hangup();
1258 else
1259 Detach(D_DETACH);
1260 break;
1261 # ifdef POW_DETACH
1262 case RC_POW_DETACH:
1263 if (key >= 0)
1265 static char buf[2];
1267 buf[0] = key;
1268 Input(buf, 1, INP_RAW, pow_detach_fn, NULL, 0);
1270 else
1271 Detach(D_POWER); /* detach and kill Attacher's parent */
1272 break;
1273 # endif
1274 #endif
1275 case RC_DEBUG:
1276 #ifdef DEBUG
1277 if (!*args)
1279 if (dfp)
1280 Msg(0, "debugging info is written to %s/", DEBUGDIR);
1281 else
1282 Msg(0, "debugging is currently off. Use 'debug on' to enable.");
1283 break;
1285 if (dfp)
1287 debug("debug: closing debug file.\n");
1288 fflush(dfp);
1289 fclose(dfp);
1290 dfp = NULL;
1292 if (strcmp("off", *args))
1293 opendebug(0, 1);
1294 # ifdef SIG_NODEBUG
1295 else if (display)
1296 kill(D_userpid, SIG_NODEBUG); /* a one shot item, but hey... */
1297 # endif /* SIG_NODEBUG */
1298 #else
1299 if (*args == 0 || strcmp("off", *args))
1300 Msg(0, "Sorry, screen was compiled without -DDEBUG option.");
1301 #endif
1302 break;
1303 #ifdef ZMODEM
1304 case RC_ZMODEM:
1305 if (*args && !strcmp(*args, "sendcmd"))
1307 if (args[1])
1309 free(zmodem_sendcmd);
1310 zmodem_sendcmd = SaveStr(args[1]);
1312 if (msgok)
1313 Msg(0, "zmodem sendcmd: %s", zmodem_sendcmd);
1314 break;
1316 if (*args && !strcmp(*args, "recvcmd"))
1318 if (args[1])
1320 free(zmodem_recvcmd);
1321 zmodem_recvcmd = SaveStr(args[1]);
1323 if (msgok)
1324 Msg(0, "zmodem recvcmd: %s", zmodem_recvcmd);
1325 break;
1327 if (*args)
1329 for (i = 0; i < 4; i++)
1330 if (!strcmp(zmodes[i], *args))
1331 break;
1332 if (i == 4 && !strcmp(*args, "on"))
1333 i = 1;
1334 if (i == 4)
1336 Msg(0, "usage: zmodem off|auto|catch|pass");
1337 break;
1339 zmodem_mode = i;
1341 if (msgok)
1342 Msg(0, "zmodem mode is %s", zmodes[zmodem_mode]);
1343 break;
1344 #endif
1345 case RC_ZOMBIE:
1347 if (!(s = *args))
1349 ZombieKey_destroy = 0;
1350 break;
1352 if (*argl == 0 || *argl > 2)
1354 Msg(0, "%s:zombie: one or two characters expected.", rc_name);
1355 break;
1357 if (args[1])
1359 if (!strcmp(args[1], "onerror"))
1361 ZombieKey_onerror = 1;
1362 } else {
1363 Msg(0, "usage: zombie [keys [onerror]]");
1364 break;
1366 } else
1367 ZombieKey_onerror = 0;
1368 ZombieKey_destroy = args[0][0];
1369 ZombieKey_resurrect = *argl == 2 ? args[0][1] : 0;
1371 break;
1372 case RC_WALL:
1373 #ifdef MULTIUSER
1374 s = D_user->u_name;
1375 #else
1376 s = D_usertty;
1377 #endif
1379 struct display *olddisplay = display;
1380 display = 0; /* no display will cause a broadcast */
1381 Msg(0, "%s: %s", s, *args);
1382 display = olddisplay;
1384 break;
1385 case RC_AT:
1386 /* where this AT command comes from: */
1387 #ifdef MULTIUSER
1388 s = SaveStr(D_user->u_name);
1389 /* DO NOT RETURN FROM HERE WITHOUT RESETTING THIS: */
1390 EffectiveAclUser = D_user;
1391 #else
1392 s = SaveStr(D_usertty);
1393 #endif
1394 n = strlen(args[0]);
1395 if (n) n--;
1397 * the windows/displays loops are quite dangerous here, take extra
1398 * care not to trigger landmines. Things may appear/disappear while
1399 * we are walking along.
1401 switch (args[0][n])
1403 case '*': /* user */
1405 struct display *nd;
1406 struct acluser *u;
1408 if (!n)
1409 u = D_user;
1410 else
1411 for (u = users; u; u = u->u_next)
1413 debug3("strncmp('%s', '%s', %d)\n", *args, u->u_name, n);
1414 if (!strncmp(*args, u->u_name, n))
1415 break;
1417 debug1("at all displays of user %s\n", u->u_name);
1418 for (display = displays; display; display = nd)
1420 nd = display->d_next;
1421 if (D_forecv == 0)
1422 continue;
1423 flayer = D_forecv->c_layer;
1424 fore = D_fore;
1425 if (D_user != u)
1426 continue;
1427 debug1("AT display %s\n", D_usertty);
1428 DoCommand(args + 1, argl + 1);
1429 if (display)
1430 Msg(0, "command from %s: %s %s",
1431 s, args[1], args[2] ? args[2] : "");
1432 display = NULL;
1433 flayer = 0;
1434 fore = NULL;
1436 break;
1438 case '%': /* display */
1440 struct display *nd;
1442 debug1("at display matching '%s'\n", args[0]);
1443 for (display = displays; display; display = nd)
1445 nd = display->d_next;
1446 if (D_forecv == 0)
1447 continue;
1448 fore = D_fore;
1449 flayer = D_forecv->c_layer;
1450 if (strncmp(args[0], D_usertty, n) &&
1451 (strncmp("/dev/", D_usertty, 5) ||
1452 strncmp(args[0], D_usertty + 5, n)) &&
1453 (strncmp("/dev/tty", D_usertty, 8) ||
1454 strncmp(args[0], D_usertty + 8, n)))
1455 continue;
1456 debug1("AT display %s\n", D_usertty);
1457 DoCommand(args + 1, argl + 1);
1458 if (display)
1459 Msg(0, "command from %s: %s %s",
1460 s, args[1], args[2] ? args[2] : "");
1461 display = NULL;
1462 fore = NULL;
1463 flayer = 0;
1465 break;
1467 case '#': /* window */
1468 n--;
1469 /* FALLTHROUGH */
1470 default:
1472 struct win *nw;
1473 int ch;
1475 n++;
1476 ch = args[0][n];
1477 args[0][n] = '\0';
1478 if (!*args[0] || (i = WindowByNumber(args[0])) < 0)
1480 args[0][n] = ch; /* must restore string in case of bind */
1481 /* try looping over titles */
1482 for (fore = windows; fore; fore = nw)
1484 nw = fore->w_next;
1485 if (strncmp(args[0], fore->w_title, n))
1486 continue;
1487 debug2("AT window %d(%s)\n", fore->w_number, fore->w_title);
1489 * consider this a bug or a feature:
1490 * while looping through windows, we have fore AND
1491 * display context. This will confuse users who try to
1492 * set up loops inside of loops, but often allows to do
1493 * what you mean, even when you adress your context wrong.
1495 i = 0;
1496 /* XXX: other displays? */
1497 if (fore->w_layer.l_cvlist)
1498 display = fore->w_layer.l_cvlist->c_display;
1499 flayer = fore->w_savelayer ? fore->w_savelayer : &fore->w_layer;
1500 DoCommand(args + 1, argl + 1); /* may destroy our display */
1501 if (fore && fore->w_layer.l_cvlist)
1503 display = fore->w_layer.l_cvlist->c_display;
1504 Msg(0, "command from %s: %s %s",
1505 s, args[1], args[2] ? args[2] : "");
1508 display = NULL;
1509 fore = NULL;
1510 if (i < 0)
1511 Msg(0, "%s: at '%s': no such window.\n", rc_name, args[0]);
1512 break;
1514 else if (i < MAXWIN && (fore = wtab[i]))
1516 args[0][n] = ch; /* must restore string in case of bind */
1517 debug2("AT window %d (%s)\n", fore->w_number, fore->w_title);
1518 if (fore->w_layer.l_cvlist)
1519 display = fore->w_layer.l_cvlist->c_display;
1520 flayer = fore->w_savelayer ? fore->w_savelayer : &fore->w_layer;
1521 DoCommand(args + 1, argl + 1);
1522 if (fore && fore->w_layer.l_cvlist)
1524 display = fore->w_layer.l_cvlist->c_display;
1525 Msg(0, "command from %s: %s %s",
1526 s, args[1], args[2] ? args[2] : "");
1528 display = NULL;
1529 fore = NULL;
1531 else
1532 Msg(0, "%s: at [identifier][%%|*|#] command [args]", rc_name);
1533 break;
1536 free(s);
1537 #ifdef MULTIUSER
1538 EffectiveAclUser = NULL;
1539 #endif
1540 break;
1542 #ifdef COPY_PASTE
1543 case RC_READREG:
1544 #ifdef ENCODINGS
1545 i = fore ? fore->w_encoding : display ? display->d_encoding : 0;
1546 if (args[0] && args[1] && !strcmp(args[0], "-e"))
1548 i = FindEncoding(args[1]);
1549 if (i == -1)
1551 Msg(0, "%s: readreg: unknown encoding", rc_name);
1552 break;
1554 args += 2;
1556 #endif
1558 * Without arguments we prompt for a destination register.
1559 * It will receive the copybuffer contents.
1560 * This is not done by RC_PASTE, as we prompt for source
1561 * (not dest) there.
1563 if ((s = *args) == NULL)
1565 Input("Copy to register:", 1, INP_RAW, copy_reg_fn, NULL, 0);
1566 break;
1568 if (*argl != 1)
1570 Msg(0, "%s: copyreg: character, ^x, or (octal) \\032 expected.", rc_name);
1571 break;
1573 ch = args[0][0];
1575 * With two arguments we *really* read register contents from file
1577 if (args[1])
1579 if (args[2])
1581 Msg(0, "%s: readreg: too many arguments", rc_name);
1582 break;
1584 if ((s = ReadFile(args[1], &n)))
1586 struct plop *pp = plop_tab + (int)(unsigned char)ch;
1588 if (pp->buf)
1589 free(pp->buf);
1590 pp->buf = s;
1591 pp->len = n;
1592 #ifdef ENCODINGS
1593 pp->enc = i;
1594 #endif
1597 else
1599 * with one argument we copy the copybuffer into a specified register
1600 * This could be done with RC_PASTE too, but is here to be consistent
1601 * with the zero argument call.
1603 copy_reg_fn(&ch, 0, NULL);
1604 break;
1605 #endif
1606 case RC_REGISTER:
1607 #ifdef ENCODINGS
1608 i = fore ? fore->w_encoding : display ? display->d_encoding : 0;
1609 if (args[0] && args[1] && !strcmp(args[0], "-e"))
1611 i = FindEncoding(args[1]);
1612 if (i == -1)
1614 Msg(0, "%s: register: unknown encoding", rc_name);
1615 break;
1617 args += 2;
1618 argc -= 2;
1620 #endif
1621 if (argc != 2)
1623 Msg(0, "%s: register: illegal number of arguments.", rc_name);
1624 break;
1626 if (*argl != 1)
1628 Msg(0, "%s: register: character, ^x, or (octal) \\032 expected.", rc_name);
1629 break;
1631 ch = args[0][0];
1632 #ifdef COPY_PASTE
1633 if (ch == '.')
1635 if (user->u_plop.buf != NULL)
1636 UserFreeCopyBuffer(user);
1637 if (args[1] && args[1][0])
1639 user->u_plop.buf = SaveStrn(args[1], argl[1]);
1640 user->u_plop.len = argl[1];
1641 #ifdef ENCODINGS
1642 user->u_plop.enc = i;
1643 #endif
1646 #endif
1647 else
1649 struct plop *plp = plop_tab + (int)(unsigned char)ch;
1651 if (plp->buf)
1652 free(plp->buf);
1653 plp->buf = SaveStrn(args[1], argl[1]);
1654 plp->len = argl[1];
1655 #ifdef ENCODINGS
1656 plp->enc = i;
1657 #endif
1659 break;
1660 case RC_PROCESS:
1661 if ((s = *args) == NULL)
1663 Input("Process register:", 1, INP_RAW, process_fn, NULL, 0);
1664 break;
1666 if (*argl != 1)
1668 Msg(0, "%s: process: character, ^x, or (octal) \\032 expected.", rc_name);
1669 break;
1671 ch = args[0][0];
1672 process_fn(&ch, 0, NULL);
1673 break;
1674 case RC_STUFF:
1675 s = *args;
1676 if (!args[0])
1678 Input("Stuff:", 100, INP_COOKED, StuffFin, NULL, 0);
1679 break;
1681 n = *argl;
1682 if (args[1])
1684 if (strcmp(s, "-k"))
1686 Msg(0, "%s: stuff: invalid option %s", rc_name, s);
1687 break;
1689 s = args[1];
1690 for (i = T_CAPS; i < T_OCAPS; i++)
1691 if (strcmp(term[i].tcname, s) == 0)
1692 break;
1693 if (i == T_OCAPS)
1695 Msg(0, "%s: stuff: unknown key '%s'", rc_name, s);
1696 break;
1698 #ifdef MAPKEYS
1699 if (StuffKey(i - T_CAPS) == 0)
1700 break;
1701 #endif
1702 s = display ? D_tcs[i].str : 0;
1703 if (s == 0)
1704 break;
1705 n = strlen(s);
1707 while(n)
1708 LayProcess(&s, &n);
1709 break;
1710 case RC_REDISPLAY:
1711 Activate(-1);
1712 break;
1713 case RC_WINDOWS:
1714 ShowWindows(-1);
1715 break;
1716 case RC_VERSION:
1717 Msg(0, "screen %s", version);
1718 break;
1719 case RC_TIME:
1720 if (*args)
1722 timestring = SaveStr(*args);
1723 break;
1725 Msg(0, "%s", MakeWinMsg(timestring, fore, '%'));
1726 break;
1727 case RC_INFO:
1728 ShowInfo();
1729 break;
1730 case RC_DINFO:
1731 ShowDInfo();
1732 break;
1733 case RC_COMMAND:
1735 struct action *ktabp = ktab;
1736 if (argc == 2 && !strcmp(*args, "-c"))
1738 if ((ktabp = FindKtab(args[1], 0)) == 0)
1740 Msg(0, "Unknown command class '%s'", args[1]);
1741 break;
1744 if (D_ESCseen != ktab || ktabp != ktab)
1746 D_ESCseen = ktabp;
1747 break;
1749 D_ESCseen = 0;
1751 /* FALLTHROUGH */
1752 case RC_OTHER:
1753 if (MoreWindows())
1754 SwitchWindow(display && D_other ? D_other->w_number : NextWindow());
1755 break;
1756 case RC_META:
1757 if (user->u_Esc == -1)
1758 break;
1759 ch = user->u_Esc;
1760 s = &ch;
1761 n = 1;
1762 LayProcess(&s, &n);
1763 break;
1764 case RC_XON:
1765 ch = Ctrl('q');
1766 s = &ch;
1767 n = 1;
1768 LayProcess(&s, &n);
1769 break;
1770 case RC_XOFF:
1771 ch = Ctrl('s');
1772 s = &ch;
1773 n = 1;
1774 LayProcess(&s, &n);
1775 break;
1776 case RC_DEFBREAKTYPE:
1777 case RC_BREAKTYPE:
1779 static char *types[] = { "TIOCSBRK", "TCSBRK", "tcsendbreak", NULL };
1780 extern int breaktype;
1782 if (*args)
1784 if (ParseNum(act, &n))
1785 for (n = 0; n < (int)(sizeof(types)/sizeof(*types)); n++)
1787 for (i = 0; i < 4; i++)
1789 ch = args[0][i];
1790 if (ch >= 'a' && ch <= 'z')
1791 ch -= 'a' - 'A';
1792 if (ch != types[n][i] && (ch + ('a' - 'A')) != types[n][i])
1793 break;
1795 if (i == 4)
1796 break;
1798 if (n < 0 || n >= (int)(sizeof(types)/sizeof(*types)))
1799 Msg(0, "%s invalid, chose one of %s, %s or %s", *args, types[0], types[1], types[2]);
1800 else
1802 breaktype = n;
1803 Msg(0, "breaktype set to (%d) %s", n, types[n]);
1806 else
1807 Msg(0, "breaktype is (%d) %s", breaktype, types[breaktype]);
1809 break;
1810 case RC_POW_BREAK:
1811 case RC_BREAK:
1812 n = 0;
1813 if (*args && ParseNum(act, &n))
1814 break;
1815 SendBreak(fore, n, nr == RC_POW_BREAK);
1816 break;
1817 #ifdef LOCK
1818 case RC_LOCKSCREEN:
1819 Detach(D_LOCK);
1820 break;
1821 #endif
1822 case RC_WIDTH:
1823 case RC_HEIGHT:
1825 int w, h;
1826 int what = 0;
1828 i = 1;
1829 if (*args && !strcmp(*args, "-w"))
1830 what = 1;
1831 else if (*args && !strcmp(*args, "-d"))
1832 what = 2;
1833 if (what)
1834 args++;
1835 if (what == 0 && flayer && !display)
1836 what = 1;
1837 if (what == 1)
1839 if (!flayer)
1841 Msg(0, "%s: %s: window required", rc_name, comms[nr].name);
1842 break;
1844 w = flayer->l_width;
1845 h = flayer->l_height;
1847 else
1849 if (!display)
1851 Msg(0, "%s: %s: display required", rc_name, comms[nr].name);
1852 break;
1854 w = D_width;
1855 h = D_height;
1857 if (*args && args[0][0] == '-')
1859 Msg(0, "%s: %s: unknown option %s", rc_name, comms[nr].name, *args);
1860 break;
1862 if (nr == RC_HEIGHT)
1864 if (!*args)
1866 #define H0height 42
1867 #define H1height 24
1868 if (h == H0height)
1869 h = H1height;
1870 else if (h == H1height)
1871 h = H0height;
1872 else if (h > (H0height + H1height) / 2)
1873 h = H0height;
1874 else
1875 h = H1height;
1877 else
1879 h = atoi(*args);
1880 if (args[1])
1881 w = atoi(args[1]);
1884 else
1886 if (!*args)
1888 if (w == Z0width)
1889 w = Z1width;
1890 else if (w == Z1width)
1891 w = Z0width;
1892 else if (w > (Z0width + Z1width) / 2)
1893 w = Z0width;
1894 else
1895 w = Z1width;
1897 else
1899 w = atoi(*args);
1900 if (args[1])
1901 h = atoi(args[1]);
1904 if (*args && args[1] && args[2])
1906 Msg(0, "%s: %s: too many arguments", rc_name, comms[nr].name);
1907 break;
1909 if (w <= 0)
1911 Msg(0, "Illegal width");
1912 break;
1914 if (h <= 0)
1916 Msg(0, "Illegal height");
1917 break;
1919 if (what == 1)
1921 if (flayer->l_width == w && flayer->l_height == h)
1922 break;
1923 ResizeLayer(flayer, w, h, (struct display *)0);
1924 break;
1926 if (D_width == w && D_height == h)
1927 break;
1928 if (what == 2)
1930 ChangeScreenSize(w, h, 1);
1932 else
1934 if (ResizeDisplay(w, h) == 0)
1936 Activate(D_fore ? D_fore->w_norefresh : 0);
1937 /* autofit */
1938 ResizeLayer(D_forecv->c_layer, D_forecv->c_xe - D_forecv->c_xs + 1, D_forecv->c_ye - D_forecv->c_ys + 1, 0);
1939 break;
1941 if (h == D_height)
1942 Msg(0, "Your termcap does not specify how to change the terminal's width to %d.", w);
1943 else if (w == D_width)
1944 Msg(0, "Your termcap does not specify how to change the terminal's height to %d.", h);
1945 else
1946 Msg(0, "Your termcap does not specify how to change the terminal's resolution to %dx%d.", w, h);
1949 break;
1950 case RC_TITLE:
1951 if (*args == 0)
1952 InputAKA();
1953 else
1954 ChangeAKA(fore, *args, strlen(*args));
1955 break;
1956 case RC_COLON:
1957 Input(":", 100, INP_EVERY, Colonfin, NULL, 0);
1958 if (*args && **args)
1960 s = *args;
1961 n = strlen(s);
1962 LayProcess(&s, &n);
1964 break;
1965 case RC_LASTMSG:
1966 if (D_status_lastmsg)
1967 Msg(0, "%s", D_status_lastmsg);
1968 break;
1969 case RC_SCREEN:
1970 DoScreen("key", args);
1971 break;
1972 case RC_WRAP:
1973 if (ParseSwitch(act, &fore->w_wrap) == 0 && msgok)
1974 Msg(0, "%cwrap", fore->w_wrap ? '+' : '-');
1975 break;
1976 case RC_FLOW:
1977 if (*args)
1979 if (args[0][0] == 'a')
1981 fore->w_flow = (fore->w_flow & FLOW_AUTO) ? FLOW_AUTOFLAG |FLOW_AUTO|FLOW_NOW : FLOW_AUTOFLAG;
1983 else
1985 if (ParseOnOff(act, &n))
1986 break;
1987 fore->w_flow = (fore->w_flow & FLOW_AUTO) | n;
1990 else
1992 if (fore->w_flow & FLOW_AUTOFLAG)
1993 fore->w_flow = (fore->w_flow & FLOW_AUTO) | FLOW_NOW;
1994 else if (fore->w_flow & FLOW_NOW)
1995 fore->w_flow &= ~FLOW_NOW;
1996 else
1997 fore->w_flow = fore->w_flow ? FLOW_AUTOFLAG|FLOW_AUTO|FLOW_NOW : FLOW_AUTOFLAG;
1999 SetFlow(fore->w_flow & FLOW_NOW);
2000 if (msgok)
2001 Msg(0, "%cflow%s", (fore->w_flow & FLOW_NOW) ? '+' : '-',
2002 (fore->w_flow & FLOW_AUTOFLAG) ? "(auto)" : "");
2003 break;
2004 #ifdef MULTIUSER
2005 case RC_DEFWRITELOCK:
2006 if (args[0][0] == 'a')
2007 nwin_default.wlock = WLOCK_AUTO;
2008 else
2010 if (ParseOnOff(act, &n))
2011 break;
2012 nwin_default.wlock = n ? WLOCK_ON : WLOCK_OFF;
2014 break;
2015 case RC_WRITELOCK:
2016 if (*args)
2018 if (args[0][0] == 'a')
2020 fore->w_wlock = WLOCK_AUTO;
2022 else
2024 if (ParseOnOff(act, &n))
2025 break;
2026 fore->w_wlock = n ? WLOCK_ON : WLOCK_OFF;
2029 * user may have permission to change the writelock setting,
2030 * but he may never aquire the lock himself without write permission
2032 if (!AclCheckPermWin(D_user, ACL_WRITE, fore))
2033 fore->w_wlockuser = D_user;
2035 Msg(0, "writelock %s", (fore->w_wlock == WLOCK_AUTO) ? "auto" :
2036 ((fore->w_wlock == WLOCK_OFF) ? "off" : "on"));
2037 break;
2038 #endif
2039 case RC_CLEAR:
2040 ResetAnsiState(fore);
2041 WriteString(fore, "\033[H\033[J", 6);
2042 break;
2043 case RC_RESET:
2044 ResetAnsiState(fore);
2045 #ifdef ZMODEM
2046 if (fore->w_zdisplay)
2047 zmodem_abort(fore, fore->w_zdisplay);
2048 #endif
2049 WriteString(fore, "\033c", 2);
2050 break;
2051 case RC_MONITOR:
2052 n = fore->w_monitor != MON_OFF;
2053 #ifdef MULTIUSER
2054 if (display)
2055 n = n && (ACLBYTE(fore->w_mon_notify, D_user->u_id) & ACLBIT(D_user->u_id));
2056 #endif
2057 if (ParseSwitch(act, &n))
2058 break;
2059 if (n)
2061 #ifdef MULTIUSER
2062 if (display) /* we tell only this user */
2063 ACLBYTE(fore->w_mon_notify, D_user->u_id) |= ACLBIT(D_user->u_id);
2064 else
2065 for (i = 0; i < maxusercount; i++)
2066 ACLBYTE(fore->w_mon_notify, i) |= ACLBIT(i);
2067 #endif
2068 if (fore->w_monitor == MON_OFF)
2069 fore->w_monitor = MON_ON;
2070 Msg(0, "Window %d (%s) is now being monitored for all activity.", fore->w_number, fore->w_title);
2072 else
2074 #ifdef MULTIUSER
2075 if (display) /* we remove only this user */
2076 ACLBYTE(fore->w_mon_notify, D_user->u_id)
2077 &= ~ACLBIT(D_user->u_id);
2078 else
2079 for (i = 0; i < maxusercount; i++)
2080 ACLBYTE(fore->w_mon_notify, i) &= ~ACLBIT(i);
2081 for (i = maxusercount - 1; i >= 0; i--)
2082 if (ACLBYTE(fore->w_mon_notify, i))
2083 break;
2084 if (i < 0)
2085 #endif
2086 fore->w_monitor = MON_OFF;
2087 Msg(0, "Window %d (%s) is no longer being monitored for activity.", fore->w_number, fore->w_title);
2089 break;
2090 #ifdef MULTI
2091 case RC_DISPLAYS:
2092 display_displays();
2093 break;
2094 #endif
2095 case RC_WINDOWLIST:
2096 if (!*args)
2097 display_wlist(0, WLIST_NUM, (struct win *)0);
2098 else if (!strcmp(*args, "string"))
2100 if (args[1])
2102 if (wliststr)
2103 free(wliststr);
2104 wliststr = SaveStr(args[1]);
2106 if (msgok)
2107 Msg(0, "windowlist string is '%s'", wliststr);
2109 else if (!strcmp(*args, "title"))
2111 if (args[1])
2113 if (wlisttit)
2114 free(wlisttit);
2115 wlisttit = SaveStr(args[1]);
2117 if (msgok)
2118 Msg(0, "windowlist title is '%s'", wlisttit);
2120 else
2122 int flag = 0;
2123 int blank = 0;
2124 for (i = 0; i < argc; i++)
2125 if (!args[i])
2126 continue;
2127 else if (!strcmp(args[i], "-m"))
2128 flag |= WLIST_MRU;
2129 else if (!strcmp(args[i], "-b"))
2130 blank = 1;
2131 else if (!strcmp(args[i], "-g"))
2132 flag |= WLIST_NESTED;
2133 else
2135 Msg(0, "usage: windowlist [-b] [-g] [-m] [string [string] | title [title]]");
2136 break;
2138 if (i == argc)
2139 display_wlist(blank, flag, (struct win *)0);
2141 break;
2142 case RC_HELP:
2143 if (argc == 2 && !strcmp(*args, "-c"))
2145 struct action *ktabp;
2146 if ((ktabp = FindKtab(args[1], 0)) == 0)
2148 Msg(0, "Unknown command class '%s'", args[1]);
2149 break;
2151 display_help(args[1], ktabp);
2153 else
2154 display_help((char *)0, ktab);
2155 break;
2156 case RC_LICENSE:
2157 display_copyright();
2158 break;
2159 #ifdef COPY_PASTE
2160 case RC_COPY:
2161 if (flayer->l_layfn != &WinLf)
2163 Msg(0, "Must be on a window layer");
2164 break;
2166 MarkRoutine();
2167 break;
2168 case RC_HISTORY:
2170 static char *pasteargs[] = {".", 0};
2171 static int pasteargl[] = {1};
2173 if (flayer->l_layfn != &WinLf)
2175 Msg(0, "Must be on a window layer");
2176 break;
2178 if (GetHistory() == 0)
2179 break;
2180 if (user->u_plop.buf == NULL)
2181 break;
2182 args = pasteargs;
2183 argl = pasteargl;
2185 /*FALLTHROUGH*/
2186 case RC_PASTE:
2188 char *ss, *dbuf, dch;
2189 int l = 0;
2190 # ifdef ENCODINGS
2191 int enc = -1;
2192 # endif
2195 * without args we prompt for one(!) register to be pasted in the window
2197 if ((s = *args) == NULL)
2199 Input("Paste from register:", 1, INP_RAW, ins_reg_fn, NULL, 0);
2200 break;
2202 if (args[1] == 0 && !fore) /* no window? */
2203 break;
2205 * with two arguments we paste into a destination register
2206 * (no window needed here).
2208 if (args[1] && argl[1] != 1)
2210 Msg(0, "%s: paste destination: character, ^x, or (octal) \\032 expected.",
2211 rc_name);
2212 break;
2214 # ifdef ENCODINGS
2215 else if (fore)
2216 enc = fore->w_encoding;
2217 # endif
2220 * measure length of needed buffer
2222 for (ss = s = *args; (ch = *ss); ss++)
2224 if (ch == '.')
2226 # ifdef ENCODINGS
2227 if (enc == -1)
2228 enc = user->u_plop.enc;
2229 if (enc != user->u_plop.enc)
2230 l += RecodeBuf((unsigned char *)user->u_plop.buf, user->u_plop.len, user->u_plop.enc, enc, (unsigned char *)0);
2231 else
2232 # endif
2233 l += user->u_plop.len;
2235 else
2237 # ifdef ENCODINGS
2238 if (enc == -1)
2239 enc = plop_tab[(int)(unsigned char)ch].enc;
2240 if (enc != plop_tab[(int)(unsigned char)ch].enc)
2241 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);
2242 else
2243 # endif
2244 l += plop_tab[(int)(unsigned char)ch].len;
2247 if (l == 0)
2249 Msg(0, "empty buffer");
2250 break;
2253 * shortcut:
2254 * if there is only one source and the destination is a window, then
2255 * pass a pointer rather than duplicating the buffer.
2257 if (s[1] == 0 && args[1] == 0)
2258 # ifdef ENCODINGS
2259 if (enc == (*s == '.' ? user->u_plop.enc : plop_tab[(int)(unsigned char)*s].enc))
2260 # endif
2262 MakePaster(&fore->w_paster, *s == '.' ? user->u_plop.buf : plop_tab[(int)(unsigned char)*s].buf, l, 0);
2263 break;
2266 * if no shortcut, we construct a buffer
2268 if ((dbuf = (char *)malloc(l)) == 0)
2270 Msg(0, strnomem);
2271 break;
2273 l = 0;
2275 * concatenate all sources into our own buffer, copy buffer is
2276 * special and is skipped if no display exists.
2278 for (ss = s; (ch = *ss); ss++)
2280 struct plop *pp = (ch == '.' ? &user->u_plop : &plop_tab[(int)(unsigned char)ch]);
2281 #ifdef ENCODINGS
2282 if (pp->enc != enc)
2284 l += RecodeBuf((unsigned char *)pp->buf, pp->len, pp->enc, enc, (unsigned char *)dbuf + l);
2285 continue;
2287 #endif
2288 bcopy(pp->buf, dbuf + l, pp->len);
2289 l += pp->len;
2292 * when called with one argument we paste our buffer into the window
2294 if (args[1] == 0)
2296 MakePaster(&fore->w_paster, dbuf, l, 1);
2298 else
2301 * we have two arguments, the second is already in dch.
2302 * use this as destination rather than the window.
2304 dch = args[1][0];
2305 if (dch == '.')
2307 if (user->u_plop.buf != NULL)
2308 UserFreeCopyBuffer(user);
2309 user->u_plop.buf = dbuf;
2310 user->u_plop.len = l;
2311 #ifdef ENCODINGS
2312 user->u_plop.enc = enc;
2313 #endif
2315 else
2317 struct plop *pp = plop_tab + (int)(unsigned char)dch;
2318 if (pp->buf)
2319 free(pp->buf);
2320 pp->buf = dbuf;
2321 pp->len = l;
2322 #ifdef ENCODINGS
2323 pp->enc = enc;
2324 #endif
2327 break;
2329 case RC_WRITEBUF:
2330 if (!user->u_plop.buf)
2332 Msg(0, "empty buffer");
2333 break;
2335 #ifdef ENCODINGS
2337 struct plop oldplop;
2339 oldplop = user->u_plop;
2340 if (args[0] && args[1] && !strcmp(args[0], "-e"))
2342 int enc, l;
2343 char *newbuf;
2345 enc = FindEncoding(args[1]);
2346 if (enc == -1)
2348 Msg(0, "%s: writebuf: unknown encoding", rc_name);
2349 break;
2351 if (enc != oldplop.enc)
2353 l = RecodeBuf((unsigned char *)oldplop.buf, oldplop.len, oldplop.enc, enc, (unsigned char *)0);
2354 newbuf = malloc(l + 1);
2355 if (!newbuf)
2357 Msg(0, strnomem);
2358 break;
2360 user->u_plop.len = RecodeBuf((unsigned char *)oldplop.buf, oldplop.len, oldplop.enc, enc, (unsigned char *)newbuf);
2361 user->u_plop.buf = newbuf;
2362 user->u_plop.enc = enc;
2364 args += 2;
2366 #endif
2367 if (args[0] && args[1])
2368 Msg(0, "%s: writebuf: too many arguments", rc_name);
2369 else
2370 WriteFile(user, args[0], DUMP_EXCHANGE);
2371 #ifdef ENCODINGS
2372 if (user->u_plop.buf != oldplop.buf)
2373 free(user->u_plop.buf);
2374 user->u_plop = oldplop;
2376 #endif
2377 break;
2378 case RC_READBUF:
2379 #ifdef ENCODINGS
2380 i = fore ? fore->w_encoding : display ? display->d_encoding : 0;
2381 if (args[0] && args[1] && !strcmp(args[0], "-e"))
2383 i = FindEncoding(args[1]);
2384 if (i == -1)
2386 Msg(0, "%s: readbuf: unknown encoding", rc_name);
2387 break;
2389 args += 2;
2391 #endif
2392 if (args[0] && args[1])
2394 Msg(0, "%s: readbuf: too many arguments", rc_name);
2395 break;
2397 if ((s = ReadFile(args[0] ? args[0] : BufferFile, &n)))
2399 if (user->u_plop.buf)
2400 UserFreeCopyBuffer(user);
2401 user->u_plop.len = n;
2402 user->u_plop.buf = s;
2403 #ifdef ENCODINGS
2404 user->u_plop.enc = i;
2405 #endif
2407 break;
2408 case RC_REMOVEBUF:
2409 KillBuffers();
2410 break;
2411 case RC_IGNORECASE:
2412 (void)ParseSwitch(act, &search_ic);
2413 if (msgok)
2414 Msg(0, "Will %signore case in searches", search_ic ? "" : "not ");
2415 break;
2416 #endif /* COPY_PASTE */
2417 case RC_ESCAPE:
2418 if (*argl == 0)
2419 SetEscape(user, -1, -1);
2420 else if (*argl == 2)
2421 SetEscape(user, (int)(unsigned char)args[0][0], (int)(unsigned char)args[0][1]);
2422 else
2424 Msg(0, "%s: two characters required after escape.", rc_name);
2425 break;
2427 /* Change defescape if master user. This is because we only
2428 * have one ktab.
2430 if (display && user != users)
2431 break;
2432 /* FALLTHROUGH */
2433 case RC_DEFESCAPE:
2434 if (*argl == 0)
2435 SetEscape(NULL, -1, -1);
2436 else if (*argl == 2)
2437 SetEscape(NULL, (int)(unsigned char)args[0][0], (int)(unsigned char)args[0][1]);
2438 else
2440 Msg(0, "%s: two characters required after defescape.", rc_name);
2441 break;
2443 #ifdef MAPKEYS
2444 CheckEscape();
2445 #endif
2446 break;
2447 case RC_CHDIR:
2448 s = *args ? *args : home;
2449 if (chdir(s) == -1)
2450 Msg(errno, "%s", s);
2451 break;
2452 case RC_SHELL:
2453 case RC_DEFSHELL:
2454 if (ParseSaveStr(act, &ShellProg) == 0)
2455 ShellArgs[0] = ShellProg;
2456 break;
2457 case RC_HARDCOPYDIR:
2458 if (*args)
2459 (void)ParseSaveStr(act, &hardcopydir);
2460 if (msgok)
2461 Msg(0, "hardcopydir is %s\n", hardcopydir && *hardcopydir ? hardcopydir : "<cwd>");
2462 break;
2463 case RC_LOGFILE:
2464 if (*args)
2466 if (args[1] && !(strcmp(*args, "flush")))
2468 log_flush = atoi(args[1]);
2469 if (msgok)
2470 Msg(0, "log flush timeout set to %ds\n", log_flush);
2471 break;
2473 if (ParseSaveStr(act, &screenlogfile) || !msgok)
2474 break;
2476 Msg(0, "logfile is '%s'", screenlogfile);
2477 break;
2478 case RC_LOGTSTAMP:
2479 if (!*args || !strcmp(*args, "on") || !strcmp(*args, "off"))
2481 if (ParseSwitch(act, &logtstamp_on) == 0 && msgok)
2482 Msg(0, "timestamps turned %s", logtstamp_on ? "on" : "off");
2484 else if (!strcmp(*args, "string"))
2486 if (args[1])
2488 if (logtstamp_string)
2489 free(logtstamp_string);
2490 logtstamp_string = SaveStr(args[1]);
2492 if (msgok)
2493 Msg(0, "logfile timestamp is '%s'", logtstamp_string);
2495 else if (!strcmp(*args, "after"))
2497 if (args[1])
2499 logtstamp_after = atoi(args[1]);
2500 if (!msgok)
2501 break;
2503 Msg(0, "timestamp printed after %ds\n", logtstamp_after);
2505 else
2506 Msg(0, "usage: logtstamp [after [n]|string [str]|on|off]");
2507 break;
2508 case RC_SHELLTITLE:
2509 (void)ParseSaveStr(act, &nwin_default.aka);
2510 break;
2511 case RC_TERMCAP:
2512 case RC_TERMCAPINFO:
2513 case RC_TERMINFO:
2514 if (!rc_name || !*rc_name)
2515 Msg(0, "Sorry, too late now. Place that in your .screenrc file.");
2516 break;
2517 case RC_SLEEP:
2518 break; /* Already handled */
2519 case RC_TERM:
2520 s = NULL;
2521 if (ParseSaveStr(act, &s))
2522 break;
2523 if (strlen(s) >= 20)
2525 Msg(0, "%s: term: argument too long ( < 20)", rc_name);
2526 free(s);
2527 break;
2529 strcpy(screenterm, s);
2530 free(s);
2531 debug1("screenterm set to %s\n", screenterm);
2532 MakeTermcap((display == 0));
2533 debug("new termcap made\n");
2534 break;
2535 case RC_ECHO:
2536 if (!msgok && (!rc_name || strcmp(rc_name, "-X")))
2537 break;
2539 * user typed ^A:echo... well, echo isn't FinishRc's job,
2540 * but as he wanted to test us, we show good will
2542 if (argc > 1 && !strcmp(*args, "-n"))
2544 args++;
2545 argc--;
2547 s = *args;
2548 if (argc > 1 && !strcmp(*args, "-p"))
2550 args++;
2551 argc--;
2552 s = *args;
2553 if (s)
2554 s = MakeWinMsg(s, fore, '%');
2556 if (s)
2557 Msg(0, "%s", s);
2558 else
2559 Msg(0, "%s: 'echo [-n] [-p] \"string\"' expected.", rc_name);
2560 break;
2561 case RC_BELL:
2562 case RC_BELL_MSG:
2563 if (*args == 0)
2565 char buf[256];
2566 AddXChars(buf, sizeof(buf), BellString);
2567 Msg(0, "bell_msg is '%s'", buf);
2568 break;
2570 (void)ParseSaveStr(act, &BellString);
2571 break;
2572 #ifdef COPY_PASTE
2573 case RC_BUFFERFILE:
2574 if (*args == 0)
2575 BufferFile = SaveStr(DEFAULT_BUFFERFILE);
2576 else if (ParseSaveStr(act, &BufferFile))
2577 break;
2578 if (msgok)
2579 Msg(0, "Bufferfile is now '%s'", BufferFile);
2580 break;
2581 #endif
2582 case RC_ACTIVITY:
2583 (void)ParseSaveStr(act, &ActivityString);
2584 break;
2585 #if defined(DETACH) && defined(POW_DETACH)
2586 case RC_POW_DETACH_MSG:
2587 if (*args == 0)
2589 char buf[256];
2590 AddXChars(buf, sizeof(buf), PowDetachString);
2591 Msg(0, "pow_detach_msg is '%s'", buf);
2592 break;
2594 (void)ParseSaveStr(act, &PowDetachString);
2595 break;
2596 #endif
2597 #if defined(UTMPOK) && defined(LOGOUTOK)
2598 case RC_LOGIN:
2599 n = fore->w_slot != (slot_t)-1;
2600 if (*args && !strcmp(*args, "always"))
2602 fore->w_lflag = 3;
2603 if (!displays && n)
2604 SlotToggle(n);
2605 break;
2607 if (*args && !strcmp(*args, "attached"))
2609 fore->w_lflag = 1;
2610 if (!displays && n)
2611 SlotToggle(0);
2612 break;
2614 if (ParseSwitch(act, &n) == 0)
2615 SlotToggle(n);
2616 break;
2617 case RC_DEFLOGIN:
2618 if (!strcmp(*args, "always"))
2619 nwin_default.lflag |= 2;
2620 else if (!strcmp(*args, "attached"))
2621 nwin_default.lflag &= ~2;
2622 else
2623 (void)ParseOnOff(act, &nwin_default.lflag);
2624 break;
2625 #endif
2626 case RC_DEFFLOW:
2627 if (args[0] && args[1] && args[1][0] == 'i')
2629 iflag = 1;
2630 for (display = displays; display; display = display->d_next)
2632 if (!D_flow)
2633 continue;
2634 #if defined(TERMIO) || defined(POSIX)
2635 D_NewMode.tio.c_cc[VINTR] = D_OldMode.tio.c_cc[VINTR];
2636 D_NewMode.tio.c_lflag |= ISIG;
2637 #else /* TERMIO || POSIX */
2638 D_NewMode.m_tchars.t_intrc = D_OldMode.m_tchars.t_intrc;
2639 #endif /* TERMIO || POSIX */
2640 SetTTY(D_userfd, &D_NewMode);
2643 if (args[0] && args[0][0] == 'a')
2644 nwin_default.flowflag = FLOW_AUTOFLAG;
2645 else
2646 (void)ParseOnOff(act, &nwin_default.flowflag);
2647 break;
2648 case RC_DEFWRAP:
2649 (void)ParseOnOff(act, &nwin_default.wrap);
2650 break;
2651 case RC_DEFC1:
2652 (void)ParseOnOff(act, &nwin_default.c1);
2653 break;
2654 #ifdef COLOR
2655 case RC_DEFBCE:
2656 (void)ParseOnOff(act, &nwin_default.bce);
2657 break;
2658 #endif
2659 case RC_DEFGR:
2660 (void)ParseOnOff(act, &nwin_default.gr);
2661 break;
2662 case RC_DEFMONITOR:
2663 if (ParseOnOff(act, &n) == 0)
2664 nwin_default.monitor = (n == 0) ? MON_OFF : MON_ON;
2665 break;
2666 case RC_DEFSILENCE:
2667 if (ParseOnOff(act, &n) == 0)
2668 nwin_default.silence = (n == 0) ? SILENCE_OFF : SILENCE_ON;
2669 break;
2670 case RC_VERBOSE:
2671 if (!*args)
2672 Msg(0, "W%s echo command when creating windows.",
2673 VerboseCreate ? "ill" : "on't");
2674 else if (ParseOnOff(act, &n) == 0)
2675 VerboseCreate = n;
2676 break;
2677 case RC_HARDSTATUS:
2678 if (display)
2680 Msg(0, "%s", ""); /* wait till mintime (keep gcc quiet) */
2681 RemoveStatus();
2683 if (args[0] && strcmp(args[0], "on") && strcmp(args[0], "off"))
2685 struct display *olddisplay = display;
2686 int old_use, new_use = -1;
2688 s = args[0];
2689 if (!strncmp(s, "always", 6))
2690 s += 6;
2691 if (!strcmp(s, "lastline"))
2692 new_use = HSTATUS_LASTLINE;
2693 else if (!strcmp(s, "ignore"))
2694 new_use = HSTATUS_IGNORE;
2695 else if (!strcmp(s, "message"))
2696 new_use = HSTATUS_MESSAGE;
2697 else if (!strcmp(args[0], "string"))
2699 if (!args[1])
2701 char buf[256];
2702 AddXChars(buf, sizeof(buf), hstatusstring);
2703 Msg(0, "hardstatus string is '%s'", buf);
2704 break;
2707 else
2709 Msg(0, "%s: usage: hardstatus [always]lastline|ignore|message|string [string]", rc_name);
2710 break;
2712 if (new_use != -1)
2714 hardstatusemu = new_use | (s == args[0] ? 0 : HSTATUS_ALWAYS);
2715 for (display = displays; display; display = display->d_next)
2717 RemoveStatus();
2718 new_use = hardstatusemu & ~HSTATUS_ALWAYS;
2719 if (D_HS && s == args[0])
2720 new_use = HSTATUS_HS;
2721 ShowHStatus((char *)0);
2722 old_use = D_has_hstatus;
2723 D_has_hstatus = new_use;
2724 if ((new_use == HSTATUS_LASTLINE && old_use != HSTATUS_LASTLINE) || (new_use != HSTATUS_LASTLINE && old_use == HSTATUS_LASTLINE))
2725 ChangeScreenSize(D_width, D_height, 1);
2726 RefreshHStatus();
2729 if (args[1])
2731 if (hstatusstring)
2732 free(hstatusstring);
2733 hstatusstring = SaveStr(args[1]);
2734 for (display = displays; display; display = display->d_next)
2735 RefreshHStatus();
2737 display = olddisplay;
2738 break;
2740 (void)ParseSwitch(act, &use_hardstatus);
2741 if (msgok)
2742 Msg(0, "messages displayed on %s", use_hardstatus ? "hardstatus line" : "window");
2743 break;
2744 case RC_CAPTION:
2745 if (strcmp(args[0], "always") == 0 || strcmp(args[0], "splitonly") == 0)
2747 struct display *olddisplay = display;
2749 captionalways = args[0][0] == 'a';
2750 for (display = displays; display; display = display->d_next)
2751 ChangeScreenSize(D_width, D_height, 1);
2752 display = olddisplay;
2754 else if (strcmp(args[0], "string") == 0)
2756 if (!args[1])
2758 char buf[256];
2759 AddXChars(buf, sizeof(buf), captionstring);
2760 Msg(0, "caption string is '%s'", buf);
2761 break;
2764 else
2766 Msg(0, "%s: usage: caption always|splitonly|string <string>", rc_name);
2767 break;
2769 if (!args[1])
2770 break;
2771 if (captionstring)
2772 free(captionstring);
2773 captionstring = SaveStr(args[1]);
2774 RedisplayDisplays(0);
2775 break;
2776 case RC_CONSOLE:
2777 n = (console_window != 0);
2778 if (ParseSwitch(act, &n))
2779 break;
2780 if (TtyGrabConsole(fore->w_ptyfd, n, rc_name))
2781 break;
2782 if (n == 0)
2783 Msg(0, "%s: releasing console %s", rc_name, HostName);
2784 else if (console_window)
2785 Msg(0, "%s: stealing console %s from window %d (%s)", rc_name,
2786 HostName, console_window->w_number, console_window->w_title);
2787 else
2788 Msg(0, "%s: grabbing console %s", rc_name, HostName);
2789 console_window = n ? fore : 0;
2790 break;
2791 case RC_ALLPARTIAL:
2792 if (ParseOnOff(act, &all_norefresh))
2793 break;
2794 if (!all_norefresh && fore)
2795 Activate(-1);
2796 if (msgok)
2797 Msg(0, all_norefresh ? "No refresh on window change!\n" :
2798 "Window specific refresh\n");
2799 break;
2800 case RC_PARTIAL:
2801 (void)ParseSwitch(act, &n);
2802 fore->w_norefresh = n;
2803 break;
2804 case RC_VBELL:
2805 if (ParseSwitch(act, &visual_bell) || !msgok)
2806 break;
2807 if (visual_bell == 0)
2808 Msg(0, "switched to audible bell.");
2809 else
2810 Msg(0, "switched to visual bell.");
2811 break;
2812 case RC_VBELLWAIT:
2813 if (ParseNum1000(act, &VBellWait) == 0 && msgok)
2814 Msg(0, "vbellwait set to %.10g seconds", VBellWait/1000.);
2815 break;
2816 case RC_MSGWAIT:
2817 if (ParseNum1000(act, &MsgWait) == 0 && msgok)
2818 Msg(0, "msgwait set to %.10g seconds", MsgWait/1000.);
2819 break;
2820 case RC_MSGMINWAIT:
2821 if (ParseNum1000(act, &MsgMinWait) == 0 && msgok)
2822 Msg(0, "msgminwait set to %.10g seconds", MsgMinWait/1000.);
2823 break;
2824 case RC_SILENCEWAIT:
2825 if (ParseNum(act, &SilenceWait))
2826 break;
2827 if (SilenceWait < 1)
2828 SilenceWait = 1;
2829 for (p = windows; p; p = p->w_next)
2830 p->w_silencewait = SilenceWait;
2831 if (msgok)
2832 Msg(0, "silencewait set to %d seconds", SilenceWait);
2833 break;
2834 case RC_NUMBER:
2835 if (*args == 0)
2836 Msg(0, "This is window %d (%s).\n", fore->w_number, fore->w_title);
2837 else
2839 int old = fore->w_number;
2840 int rel = 0, parse;
2841 if (args[0][0] == '+')
2842 rel = 1;
2843 else if (args[0][0] == '-')
2844 rel = -1;
2845 if (rel)
2846 ++act->args[0];
2847 parse = ParseNum(act, &n);
2848 if (rel)
2849 --act->args[0];
2850 if (parse)
2851 break;
2852 if (rel > 0)
2853 n += old;
2854 else if (rel < 0)
2855 n = old - n;
2856 if (n < 0 || n >= maxwin)
2858 Msg(0, "Given window position is invalid.");
2859 return;
2861 p = wtab[n];
2862 wtab[n] = fore;
2863 fore->w_number = n;
2864 wtab[old] = p;
2865 if (p)
2866 p->w_number = old;
2867 #ifdef MULTIUSER
2868 /* exchange the acls for these windows. */
2869 AclWinSwap(old, n);
2870 #endif
2871 #ifdef UTMPOK
2872 /* exchange the utmp-slots for these windows */
2873 if ((fore->w_slot != (slot_t) -1) && (fore->w_slot != (slot_t) 0))
2875 RemoveUtmp(fore);
2876 SetUtmp(fore);
2878 if (p && (p->w_slot != (slot_t) -1) && (p->w_slot != (slot_t) 0))
2880 /* XXX: first display wins? */
2881 display = fore->w_layer.l_cvlist ? fore->w_layer.l_cvlist->c_display : 0;
2882 RemoveUtmp(p);
2883 SetUtmp(p);
2885 #endif
2887 WindowChanged(fore, 'n');
2888 WindowChanged((struct win *)0, 'w');
2889 WindowChanged((struct win *)0, 'W');
2890 WindowChanged((struct win *)0, 0);
2892 break;
2893 case RC_SILENCE:
2894 n = fore->w_silence != 0;
2895 i = fore->w_silencewait;
2896 if (args[0] && (args[0][0] == '-' || (args[0][0] >= '0' && args[0][0] <= '9')))
2898 if (ParseNum(act, &i))
2899 break;
2900 n = i > 0;
2902 else if (ParseSwitch(act, &n))
2903 break;
2904 if (n)
2906 #ifdef MULTIUSER
2907 if (display) /* we tell only this user */
2908 ACLBYTE(fore->w_lio_notify, D_user->u_id) |= ACLBIT(D_user->u_id);
2909 else
2910 for (n = 0; n < maxusercount; n++)
2911 ACLBYTE(fore->w_lio_notify, n) |= ACLBIT(n);
2912 #endif
2913 fore->w_silencewait = i;
2914 fore->w_silence = SILENCE_ON;
2915 SetTimeout(&fore->w_silenceev, fore->w_silencewait * 1000);
2916 evenq(&fore->w_silenceev);
2918 if (!msgok)
2919 break;
2920 Msg(0, "The window is now being monitored for %d sec. silence.", fore->w_silencewait);
2922 else
2924 #ifdef MULTIUSER
2925 if (display) /* we remove only this user */
2926 ACLBYTE(fore->w_lio_notify, D_user->u_id)
2927 &= ~ACLBIT(D_user->u_id);
2928 else
2929 for (n = 0; n < maxusercount; n++)
2930 ACLBYTE(fore->w_lio_notify, n) &= ~ACLBIT(n);
2931 for (i = maxusercount - 1; i >= 0; i--)
2932 if (ACLBYTE(fore->w_lio_notify, i))
2933 break;
2934 if (i < 0)
2935 #endif
2937 fore->w_silence = SILENCE_OFF;
2938 evdeq(&fore->w_silenceev);
2940 if (!msgok)
2941 break;
2942 Msg(0, "The window is no longer being monitored for silence.");
2944 break;
2945 #ifdef COPY_PASTE
2946 case RC_DEFSCROLLBACK:
2947 (void)ParseNum(act, &nwin_default.histheight);
2948 break;
2949 case RC_SCROLLBACK:
2950 if (flayer->l_layfn == &MarkLf)
2952 Msg(0, "Cannot resize scrollback buffer in copy/scrollback mode.");
2953 break;
2955 (void)ParseNum(act, &n);
2956 ChangeWindowSize(fore, fore->w_width, fore->w_height, n);
2957 if (msgok)
2958 Msg(0, "scrollback set to %d", fore->w_histheight);
2959 break;
2960 #endif
2961 case RC_SESSIONNAME:
2962 if (*args == 0)
2963 Msg(0, "This session is named '%s'\n", SockName);
2964 else
2966 char buf[MAXPATHLEN];
2968 s = 0;
2969 if (ParseSaveStr(act, &s))
2970 break;
2971 if (!*s || strlen(s) + (SockName - SockPath) > MAXPATHLEN - 13 || index(s, '/'))
2973 Msg(0, "%s: bad session name '%s'\n", rc_name, s);
2974 free(s);
2975 break;
2977 strncpy(buf, SockPath, SockName - SockPath);
2978 sprintf(buf + (SockName - SockPath), "%d.%s", (int)getpid(), s);
2979 free(s);
2980 if ((access(buf, F_OK) == 0) || (errno != ENOENT))
2982 Msg(0, "%s: inappropriate path: '%s'.", rc_name, buf);
2983 break;
2985 if (rename(SockPath, buf))
2987 Msg(errno, "%s: failed to rename(%s, %s)", rc_name, SockPath, buf);
2988 break;
2990 debug2("rename(%s, %s) done\n", SockPath, buf);
2991 strcpy(SockPath, buf);
2992 MakeNewEnv();
2993 WindowChanged((struct win *)0, 'S');
2995 break;
2996 case RC_SETENV:
2997 if (!args[0] || !args[1])
2999 debug1("RC_SETENV arguments missing: %s\n", args[0] ? args[0] : "");
3000 InputSetenv(args[0]);
3002 else
3004 xsetenv(args[0], args[1]);
3005 MakeNewEnv();
3007 break;
3008 case RC_UNSETENV:
3009 unsetenv(*args);
3010 MakeNewEnv();
3011 break;
3012 #ifdef COPY_PASTE
3013 case RC_DEFSLOWPASTE:
3014 (void)ParseNum(act, &nwin_default.slow);
3015 break;
3016 case RC_SLOWPASTE:
3017 if (*args == 0)
3018 Msg(0, fore->w_slowpaste ?
3019 "Slowpaste in window %d is %d milliseconds." :
3020 "Slowpaste in window %d is unset.",
3021 fore->w_number, fore->w_slowpaste);
3022 else if (ParseNum(act, &fore->w_slowpaste) == 0 && msgok)
3023 Msg(0, fore->w_slowpaste ?
3024 "Slowpaste in window %d set to %d milliseconds." :
3025 "Slowpaste in window %d now unset.",
3026 fore->w_number, fore->w_slowpaste);
3027 break;
3028 case RC_MARKKEYS:
3029 if (CompileKeys(*args, *argl, mark_key_tab))
3031 Msg(0, "%s: markkeys: syntax error.", rc_name);
3032 break;
3034 debug1("markkeys %s\n", *args);
3035 break;
3036 # ifdef FONT
3037 case RC_PASTEFONT:
3038 if (ParseSwitch(act, &pastefont) == 0 && msgok)
3039 Msg(0, "Will %spaste font settings", pastefont ? "" : "not ");
3040 break;
3041 # endif
3042 case RC_CRLF:
3043 (void)ParseSwitch(act, &join_with_cr);
3044 break;
3045 case RC_COMPACTHIST:
3046 if (ParseSwitch(act, &compacthist) == 0 && msgok)
3047 Msg(0, "%scompacting history lines", compacthist ? "" : "not ");
3048 break;
3049 #endif
3050 #ifdef NETHACK
3051 case RC_NETHACK:
3052 (void)ParseOnOff(act, &nethackflag);
3053 break;
3054 #endif
3055 case RC_HARDCOPY_APPEND:
3056 (void)ParseOnOff(act, &hardcopy_append);
3057 break;
3058 case RC_VBELL_MSG:
3059 if (*args == 0)
3061 char buf[256];
3062 AddXChars(buf, sizeof(buf), VisualBellString);
3063 Msg(0, "vbell_msg is '%s'", buf);
3064 break;
3066 (void)ParseSaveStr(act, &VisualBellString);
3067 debug1(" new vbellstr '%s'\n", VisualBellString);
3068 break;
3069 case RC_DEFMODE:
3070 if (ParseBase(act, *args, &n, 8, "octal"))
3071 break;
3072 if (n < 0 || n > 0777)
3074 Msg(0, "%s: mode: Invalid tty mode %o", rc_name, n);
3075 break;
3077 TtyMode = n;
3078 if (msgok)
3079 Msg(0, "Ttymode set to %03o", TtyMode);
3080 break;
3081 case RC_AUTODETACH:
3082 (void)ParseOnOff(act, &auto_detach);
3083 break;
3084 case RC_STARTUP_MESSAGE:
3085 (void)ParseOnOff(act, &default_startup);
3086 break;
3087 #ifdef PASSWORD
3088 case RC_PASSWORD:
3089 if (*args)
3091 n = (*user->u_password) ? 1 : 0;
3092 if (user->u_password != NullStr) free((char *)user->u_password);
3093 user->u_password = SaveStr(*args);
3094 if (!strcmp(user->u_password, "none"))
3096 if (n)
3097 Msg(0, "Password checking disabled");
3098 free(user->u_password);
3099 user->u_password = NullStr;
3102 else
3104 if (!fore)
3106 Msg(0, "%s: password: window required", rc_name);
3107 break;
3109 Input("New screen password:", 100, INP_NOECHO, pass1, display ? (char *)D_user : (char *)users, 0);
3111 break;
3112 #endif /* PASSWORD */
3113 case RC_BIND:
3115 struct action *ktabp = ktab;
3116 int kflag = 0;
3118 for (;;)
3120 if (argc > 2 && !strcmp(*args, "-c"))
3122 ktabp = FindKtab(args[1], 1);
3123 if (ktabp == 0)
3124 break;
3125 args += 2;
3126 argl += 2;
3127 argc -= 2;
3129 else if (argc > 1 && !strcmp(*args, "-k"))
3131 kflag = 1;
3132 args++;
3133 argl++;
3134 argc--;
3136 else
3137 break;
3139 #ifdef MAPKEYS
3140 if (kflag)
3142 for (n = 0; n < KMAP_KEYS; n++)
3143 if (strcmp(term[n + T_CAPS].tcname, *args) == 0)
3144 break;
3145 if (n == KMAP_KEYS)
3147 Msg(0, "%s: bind: unknown key '%s'", rc_name, *args);
3148 break;
3150 n += 256;
3152 else
3153 #endif
3154 if (*argl != 1)
3156 Msg(0, "%s: bind: character, ^x, or (octal) \\032 expected.", rc_name);
3157 break;
3159 else
3160 n = (unsigned char)args[0][0];
3162 if (args[1])
3164 if ((i = FindCommnr(args[1])) == RC_ILLEGAL)
3166 Msg(0, "%s: bind: unknown command '%s'", rc_name, args[1]);
3167 break;
3169 if (CheckArgNum(i, args + 2) < 0)
3170 break;
3171 ClearAction(&ktabp[n]);
3172 SaveAction(ktabp + n, i, args + 2, argl + 2);
3174 else
3175 ClearAction(&ktabp[n]);
3177 break;
3178 #ifdef MAPKEYS
3179 case RC_BINDKEY:
3181 struct action *newact;
3182 int newnr, fl = 0, kf = 0, af = 0, df = 0, mf = 0;
3183 struct display *odisp = display;
3184 int used = 0;
3185 struct kmap_ext *kme;
3187 for (; *args && **args == '-'; args++, argl++)
3189 if (strcmp(*args, "-t") == 0)
3190 fl = KMAP_NOTIMEOUT;
3191 else if (strcmp(*args, "-k") == 0)
3192 kf = 1;
3193 else if (strcmp(*args, "-a") == 0)
3194 af = 1;
3195 else if (strcmp(*args, "-d") == 0)
3196 df = 1;
3197 else if (strcmp(*args, "-m") == 0)
3198 mf = 1;
3199 else if (strcmp(*args, "--") == 0)
3201 args++;
3202 argl++;
3203 break;
3205 else
3207 Msg(0, "%s: bindkey: invalid option %s", rc_name, *args);
3208 return;
3211 if (df && mf)
3213 Msg(0, "%s: bindkey: -d does not work with -m", rc_name);
3214 break;
3216 if (*args == 0)
3218 if (mf)
3219 display_bindkey("Edit mode", mmtab);
3220 else if (df)
3221 display_bindkey("Default", dmtab);
3222 else
3223 display_bindkey("User", umtab);
3224 break;
3226 if (kf == 0)
3228 if (af)
3230 Msg(0, "%s: bindkey: -a only works with -k", rc_name);
3231 break;
3233 if (*argl == 0)
3235 Msg(0, "%s: bindkey: empty string makes no sense", rc_name);
3236 break;
3238 for (i = 0, kme = kmap_exts; i < kmap_extn; i++, kme++)
3239 if (kme->str == 0)
3241 if (args[1])
3242 break;
3244 else
3245 if (*argl == (kme->fl & ~KMAP_NOTIMEOUT) && bcmp(kme->str, *args, *argl) == 0)
3246 break;
3247 if (i == kmap_extn)
3249 if (!args[1])
3251 Msg(0, "%s: bindkey: keybinding not found", rc_name);
3252 break;
3254 kmap_extn += 8;
3255 kmap_exts = (struct kmap_ext *)xrealloc((char *)kmap_exts, kmap_extn * sizeof(*kmap_exts));
3256 kme = kmap_exts + i;
3257 bzero((char *)kme, 8 * sizeof(*kmap_exts));
3258 for (; i < kmap_extn; i++, kme++)
3260 kme->str = 0;
3261 kme->dm.nr = kme->mm.nr = kme->um.nr = RC_ILLEGAL;
3262 kme->dm.args = kme->mm.args = kme->um.args = noargs;
3264 i -= 8;
3265 kme -= 8;
3267 if (df == 0 && kme->dm.nr != RC_ILLEGAL)
3268 used = 1;
3269 if (mf == 0 && kme->mm.nr != RC_ILLEGAL)
3270 used = 1;
3271 if ((df || mf) && kme->um.nr != RC_ILLEGAL)
3272 used = 1;
3273 i += KMAP_KEYS + KMAP_AKEYS;
3274 newact = df ? &kme->dm : mf ? &kme->mm : &kme->um;
3276 else
3278 for (i = T_CAPS; i < T_OCAPS; i++)
3279 if (strcmp(term[i].tcname, *args) == 0)
3280 break;
3281 if (i == T_OCAPS)
3283 Msg(0, "%s: bindkey: unknown key '%s'", rc_name, *args);
3284 break;
3286 if (af && i >= T_CURSOR && i < T_OCAPS)
3287 i -= T_CURSOR - KMAP_KEYS;
3288 else
3289 i -= T_CAPS;
3290 newact = df ? &dmtab[i] : mf ? &mmtab[i] : &umtab[i];
3292 if (args[1])
3294 if ((newnr = FindCommnr(args[1])) == RC_ILLEGAL)
3296 Msg(0, "%s: bindkey: unknown command '%s'", rc_name, args[1]);
3297 break;
3299 if (CheckArgNum(newnr, args + 2) < 0)
3300 break;
3301 ClearAction(newact);
3302 SaveAction(newact, newnr, args + 2, argl + 2);
3303 if (kf == 0 && args[1])
3305 if (kme->str)
3306 free(kme->str);
3307 kme->str = SaveStrn(*args, *argl);
3308 kme->fl = fl | *argl;
3311 else
3312 ClearAction(newact);
3313 for (display = displays; display; display = display->d_next)
3314 remap(i, args[1] ? 1 : 0);
3315 if (kf == 0 && !args[1])
3317 if (!used && kme->str)
3319 free(kme->str);
3320 kme->str = 0;
3321 kme->fl = 0;
3324 display = odisp;
3326 break;
3327 case RC_MAPTIMEOUT:
3328 if (*args)
3330 if (ParseNum(act, &n))
3331 break;
3332 if (n < 0)
3334 Msg(0, "%s: maptimeout: illegal time %d", rc_name, n);
3335 break;
3337 maptimeout = n;
3339 if (*args == 0 || msgok)
3340 Msg(0, "maptimeout is %dms", maptimeout);
3341 break;
3342 case RC_MAPNOTNEXT:
3343 D_dontmap = 1;
3344 break;
3345 case RC_MAPDEFAULT:
3346 D_mapdefault = 1;
3347 break;
3348 #endif
3349 #ifdef MULTIUSER
3350 case RC_ACLCHG:
3351 case RC_ACLADD:
3352 case RC_ADDACL:
3353 case RC_CHACL:
3354 UsersAcl(NULL, argc, args);
3355 break;
3356 case RC_ACLDEL:
3357 if (UserDel(args[0], NULL))
3358 break;
3359 if (msgok)
3360 Msg(0, "%s removed from acl database", args[0]);
3361 break;
3362 case RC_ACLGRP:
3364 * modify a user to gain or lose rights granted to a group.
3365 * This group is actually a normal user whose rights were defined
3366 * with chacl in the usual way.
3368 if (args[1])
3370 if (strcmp(args[1], "none")) /* link a user to another user */
3372 if (AclLinkUser(args[0], args[1]))
3373 break;
3374 if (msgok)
3375 Msg(0, "User %s joined acl-group %s", args[0], args[1]);
3377 else /* remove all groups from user */
3379 struct acluser *u;
3380 struct aclusergroup *g;
3382 if (!(u = *FindUserPtr(args[0])))
3383 break;
3384 while ((g = u->u_group))
3386 u->u_group = g->next;
3387 free((char *)g);
3391 else /* show all groups of user */
3393 char buf[256], *p = buf;
3394 int ngroups = 0;
3395 struct acluser *u;
3396 struct aclusergroup *g;
3398 if (!(u = *FindUserPtr(args[0])))
3400 if (msgok)
3401 Msg(0, "User %s does not exist.", args[0]);
3402 break;
3404 g = u->u_group;
3405 while (g)
3407 ngroups++;
3408 sprintf(p, "%s ", g->u->u_name);
3409 p += strlen(p);
3410 if (p > buf+200)
3411 break;
3412 g = g->next;
3414 if (ngroups)
3415 *(--p) = '\0';
3416 Msg(0, "%s's group%s: %s.", args[0], (ngroups == 1) ? "" : "s",
3417 (ngroups == 0) ? "none" : buf);
3419 break;
3420 case RC_ACLUMASK:
3421 case RC_UMASK:
3422 while ((s = *args++))
3424 char *err = 0;
3426 if (AclUmask(display ? D_user : users, s, &err))
3427 Msg(0, "umask: %s\n", err);
3429 break;
3430 case RC_MULTIUSER:
3431 if (ParseOnOff(act, &n))
3432 break;
3433 multi = n ? "" : 0;
3434 chsock();
3435 if (msgok)
3436 Msg(0, "Multiuser mode %s", multi ? "enabled" : "disabled");
3437 break;
3438 #endif /* MULTIUSER */
3439 #ifdef PSEUDOS
3440 case RC_EXEC:
3441 winexec(args);
3442 break;
3443 #endif
3444 #ifdef MULTI
3445 case RC_NONBLOCK:
3446 i = D_nonblock >= 0;
3447 if (*args && ((args[0][0] >= '0' && args[0][0] <= '9') || args[0][0] == '.'))
3449 if (ParseNum1000(act, &i))
3450 break;
3452 else if (!ParseSwitch(act, &i))
3453 i = i == 0 ? -1 : 1000;
3454 else
3455 break;
3456 if (msgok && i == -1)
3457 Msg(0, "display set to blocking mode");
3458 else if (msgok && i == 0)
3459 Msg(0, "display set to nonblocking mode, no timeout");
3460 else if (msgok)
3461 Msg(0, "display set to nonblocking mode, %.10gs timeout", i/1000.);
3462 D_nonblock = i;
3463 if (D_nonblock <= 0)
3464 evdeq(&D_blockedev);
3465 break;
3466 case RC_DEFNONBLOCK:
3467 if (*args && ((args[0][0] >= '0' && args[0][0] <= '9') || args[0][0] == '.'))
3469 if (ParseNum1000(act, &defnonblock))
3470 break;
3472 else if (!ParseOnOff(act, &defnonblock))
3473 defnonblock = defnonblock == 0 ? -1 : 1000;
3474 else
3475 break;
3476 if (display && *rc_name)
3478 D_nonblock = defnonblock;
3479 if (D_nonblock <= 0)
3480 evdeq(&D_blockedev);
3482 break;
3483 #endif
3484 case RC_GR:
3485 #ifdef ENCODINGS
3486 if (fore->w_gr == 2)
3487 fore->w_gr = 0;
3488 #endif
3489 if (ParseSwitch(act, &fore->w_gr) == 0 && msgok)
3490 Msg(0, "Will %suse GR", fore->w_gr ? "" : "not ");
3491 #ifdef ENCODINGS
3492 if (fore->w_gr == 0 && fore->w_FontE)
3493 fore->w_gr = 2;
3494 #endif
3495 break;
3496 case RC_C1:
3497 if (ParseSwitch(act, &fore->w_c1) == 0 && msgok)
3498 Msg(0, "Will %suse C1", fore->w_c1 ? "" : "not ");
3499 break;
3500 #ifdef COLOR
3501 case RC_BCE:
3502 if (ParseSwitch(act, &fore->w_bce) == 0 && msgok)
3503 Msg(0, "Will %serase with background color", fore->w_bce ? "" : "not ");
3504 break;
3505 #endif
3506 #ifdef ENCODINGS
3507 case RC_KANJI:
3508 case RC_ENCODING:
3509 #ifdef UTF8
3510 if (*args && !strcmp(args[0], "-d"))
3512 if (!args[1])
3513 Msg(0, "encodings directory is %s", screenencodings ? screenencodings : "<unset>");
3514 else
3516 free(screenencodings);
3517 screenencodings = SaveStr(args[1]);
3519 break;
3521 if (*args && !strcmp(args[0], "-l"))
3523 if (!args[1])
3524 Msg(0, "encoding: -l: argument required");
3525 else if (LoadFontTranslation(-1, args[1]))
3526 Msg(0, "encoding: could not load utf8 encoding file");
3527 else if (msgok)
3528 Msg(0, "encoding: utf8 encoding file loaded");
3529 break;
3531 #else
3532 if (*args && (!strcmp(args[0], "-l") || !strcmp(args[0], "-d")))
3534 if (msgok)
3535 Msg(0, "encoding: screen is not compiled for UTF-8.");
3536 break;
3538 #endif
3539 for (i = 0; i < 2; i++)
3541 if (args[i] == 0)
3542 break;
3543 if (!strcmp(args[i], "."))
3544 continue;
3545 n = FindEncoding(args[i]);
3546 if (n == -1)
3548 Msg(0, "encoding: unknown encoding '%s'", args[i]);
3549 break;
3551 if (i == 0 && fore)
3553 WinSwitchEncoding(fore, n);
3554 ResetCharsets(fore);
3556 else if (i && display)
3557 D_encoding = n;
3559 break;
3560 case RC_DEFKANJI:
3561 case RC_DEFENCODING:
3562 n = FindEncoding(*args);
3563 if (n == -1)
3565 Msg(0, "defencoding: unknown encoding '%s'", *args);
3566 break;
3568 nwin_default.encoding = n;
3569 break;
3570 #endif
3572 #ifdef UTF8
3573 case RC_DEFUTF8:
3574 n = nwin_default.encoding == UTF8;
3575 if (ParseSwitch(act, &n) == 0)
3577 nwin_default.encoding = n ? UTF8 : 0;
3578 if (msgok)
3579 Msg(0, "Will %suse UTF-8 encoding for new windows", n ? "" : "not ");
3581 break;
3582 case RC_UTF8:
3583 for (i = 0; i < 2; i++)
3585 if (i && args[i] == 0)
3586 break;
3587 if (args[i] == 0)
3588 n = fore->w_encoding != UTF8;
3589 else if (strcmp(args[i], "off") == 0)
3590 n = 0;
3591 else if (strcmp(args[i], "on") == 0)
3592 n = 1;
3593 else
3595 Msg(0, "utf8: illegal argument (%s)", args[i]);
3596 break;
3598 if (i == 0)
3600 WinSwitchEncoding(fore, n ? UTF8 : 0);
3601 if (msgok)
3602 Msg(0, "Will %suse UTF-8 encoding", n ? "" : "not ");
3604 else if (display)
3605 D_encoding = n ? UTF8 : 0;
3606 if (args[i] == 0)
3607 break;
3609 break;
3610 #endif
3612 case RC_PRINTCMD:
3613 if (*args)
3615 if (printcmd)
3616 free(printcmd);
3617 printcmd = 0;
3618 if (**args)
3619 printcmd = SaveStr(*args);
3621 if (*args == 0 || msgok)
3623 if (printcmd)
3624 Msg(0, "using '%s' as print command", printcmd);
3625 else
3626 Msg(0, "using termcap entries for printing");
3627 break;
3629 break;
3631 case RC_DIGRAPH:
3632 Input("Enter digraph: ", 10, INP_EVERY, digraph_fn, NULL, 0);
3633 if (*args && **args)
3635 s = *args;
3636 n = strlen(s);
3637 LayProcess(&s, &n);
3639 break;
3641 case RC_DEFHSTATUS:
3642 if (*args == 0)
3644 char buf[256];
3645 *buf = 0;
3646 if (nwin_default.hstatus)
3647 AddXChars(buf, sizeof(buf), nwin_default.hstatus);
3648 Msg(0, "default hstatus is '%s'", buf);
3649 break;
3651 (void)ParseSaveStr(act, &nwin_default.hstatus);
3652 if (*nwin_default.hstatus == 0)
3654 free(nwin_default.hstatus);
3655 nwin_default.hstatus = 0;
3657 break;
3658 case RC_HSTATUS:
3659 (void)ParseSaveStr(act, &fore->w_hstatus);
3660 if (*fore->w_hstatus == 0)
3662 free(fore->w_hstatus);
3663 fore->w_hstatus = 0;
3665 WindowChanged(fore, 'h');
3666 break;
3668 #ifdef FONT
3669 case RC_DEFCHARSET:
3670 case RC_CHARSET:
3671 if (*args == 0)
3673 char buf[256];
3674 *buf = 0;
3675 if (nwin_default.charset)
3676 AddXChars(buf, sizeof(buf), nwin_default.charset);
3677 Msg(0, "default charset is '%s'", buf);
3678 break;
3680 n = strlen(*args);
3681 if (n == 0 || n > 6)
3683 Msg(0, "%s: %s: string has illegal size.", rc_name, comms[nr].name);
3684 break;
3686 if (n > 4 && (
3687 ((args[0][4] < '0' || args[0][4] > '3') && args[0][4] != '.') ||
3688 ((args[0][5] < '0' || args[0][5] > '3') && args[0][5] && args[0][5] != '.')))
3690 Msg(0, "%s: %s: illegal mapping number.", rc_name, comms[nr].name);
3691 break;
3693 if (nr == RC_CHARSET)
3695 SetCharsets(fore, *args);
3696 break;
3698 if (nwin_default.charset)
3699 free(nwin_default.charset);
3700 nwin_default.charset = SaveStr(*args);
3701 break;
3702 #endif
3703 #ifdef COLOR
3704 case RC_ATTRCOLOR:
3705 s = args[0];
3706 if (*s >= '0' && *s <= '9')
3707 i = *s - '0';
3708 else
3709 for (i = 0; i < 8; i++)
3710 if (*s == "dubrsBiI"[i])
3711 break;
3712 s++;
3713 nr = 0;
3714 if (*s && s[1] && !s[2])
3716 if (*s == 'd' && s[1] == 'd')
3717 nr = 3;
3718 else if (*s == '.' && s[1] == 'd')
3719 nr = 2;
3720 else if (*s == 'd' && s[1] == '.')
3721 nr = 1;
3722 else if (*s != '.' || s[1] != '.')
3723 s--;
3724 s += 2;
3726 if (*s || i < 0 || i >= 8)
3728 Msg(0, "%s: attrcolor: unknown attribute '%s'.", rc_name, args[0]);
3729 break;
3731 n = 0;
3732 if (args[1])
3733 n = ParseAttrColor(args[1], args[2], 1);
3734 if (n == -1)
3735 break;
3736 attr2color[i][nr] = n;
3737 n = 0;
3738 for (i = 0; i < 8; i++)
3739 if (attr2color[i][0] || attr2color[i][1] || attr2color[i][2] || attr2color[i][3])
3740 n |= 1 << i;
3741 nattr2color = n;
3742 break;
3743 #endif
3744 case RC_SORENDITION:
3745 i = 0;
3746 if (*args)
3748 i = ParseAttrColor(*args, args[1], 1);
3749 if (i == -1)
3750 break;
3751 ApplyAttrColor(i, &mchar_so);
3752 debug2("--> %x %x\n", mchar_so.attr, mchar_so.color);
3754 if (msgok)
3755 #ifdef COLOR
3756 Msg(0, "Standout attributes 0x%02x color 0x%02x", (unsigned char)mchar_so.attr, 0x99 ^ (unsigned char)mchar_so.color);
3757 #else
3758 Msg(0, "Standout attributes 0x%02x ", (unsigned char)mchar_so.attr);
3759 #endif
3760 break;
3762 case RC_SOURCE:
3763 do_source(*args);
3764 break;
3766 #ifdef MULTIUSER
3767 case RC_SU:
3768 s = NULL;
3769 if (!*args)
3771 Msg(0, "%s:%s screen login", HostName, SockPath);
3772 InputSu(D_fore, &D_user, NULL);
3774 else if (!args[1])
3775 InputSu(D_fore, &D_user, args[0]);
3776 else if (!args[2])
3777 s = DoSu(&D_user, args[0], args[1], "\377");
3778 else
3779 s = DoSu(&D_user, args[0], args[1], args[2]);
3780 if (s)
3781 Msg(0, "%s", s);
3782 break;
3783 #endif /* MULTIUSER */
3784 case RC_SPLIT:
3785 s = args[0];
3786 if (s && !strcmp(s, "-v"))
3787 AddCanvas(SLICE_HORI);
3788 else
3789 AddCanvas(SLICE_VERT);
3790 Activate(-1);
3791 break;
3792 case RC_REMOVE:
3793 RemCanvas();
3794 Activate(-1);
3795 break;
3796 case RC_ONLY:
3797 OneCanvas();
3798 Activate(-1);
3799 break;
3800 case RC_FIT:
3801 D_forecv->c_xoff = D_forecv->c_xs;
3802 D_forecv->c_yoff = D_forecv->c_ys;
3803 RethinkViewportOffsets(D_forecv);
3804 ResizeLayer(D_forecv->c_layer, D_forecv->c_xe - D_forecv->c_xs + 1, D_forecv->c_ye - D_forecv->c_ys + 1, 0);
3805 flayer = D_forecv->c_layer;
3806 LaySetCursor();
3807 break;
3808 case RC_FOCUS:
3809 if (!*args || !strcmp(*args, "down"))
3810 D_forecv = D_forecv->c_next ? D_forecv->c_next : D_cvlist;
3811 else if (!strcmp(*args, "up"))
3813 struct canvas *cv;
3814 for (cv = D_cvlist; cv->c_next && cv->c_next != D_forecv; cv = cv->c_next)
3816 D_forecv = cv;
3818 else if (!strcmp(*args, "top"))
3819 D_forecv = D_cvlist;
3820 else if (!strcmp(*args, "bottom"))
3822 struct canvas *cv;
3823 for (cv = D_cvlist; cv->c_next; cv = cv->c_next)
3825 D_forecv = cv;
3827 else
3829 Msg(0, "%s: usage: focus [up|down|top|bottom]", rc_name);
3830 break;
3832 if ((focusminwidth && (focusminwidth < 0 || D_forecv->c_xe - D_forecv->c_xs + 1 < focusminwidth)) ||
3833 (focusminheight && (focusminheight < 0 || D_forecv->c_ye - D_forecv->c_ys + 1 < focusminheight)))
3835 ResizeCanvas(&D_canvas);
3836 RecreateCanvasChain();
3837 RethinkDisplayViewports();
3838 ResizeLayersToCanvases(); /* redisplays */
3840 fore = D_fore = Layer2Window(D_forecv->c_layer);
3841 flayer = D_forecv->c_layer;
3842 #ifdef RXVT_OSC
3843 if (D_xtermosc[2] || D_xtermosc[3])
3845 Activate(-1);
3846 break;
3848 #endif
3849 RefreshHStatus();
3850 #ifdef RXVT_OSC
3851 RefreshXtermOSC();
3852 #endif
3853 flayer = D_forecv->c_layer;
3854 CV_CALL(D_forecv, LayRestore();LaySetCursor());
3855 WindowChanged(0, 'F');
3856 break;
3857 case RC_RESIZE:
3858 i = 0;
3859 if (D_forecv->c_slorient == SLICE_UNKN)
3861 Msg(0, "resize: need more than one region");
3862 break;
3864 for (; *args; args++)
3866 if (!strcmp(*args, "-h"))
3867 i |= RESIZE_FLAG_H;
3868 else if (!strcmp(*args, "-v"))
3869 i |= RESIZE_FLAG_V;
3870 else if (!strcmp(*args, "-b"))
3871 i |= RESIZE_FLAG_H | RESIZE_FLAG_V;
3872 else if (!strcmp(*args, "-p"))
3873 i |= D_forecv->c_slorient == SLICE_VERT ? RESIZE_FLAG_H : RESIZE_FLAG_V;
3874 else if (!strcmp(*args, "-l"))
3875 i |= RESIZE_FLAG_L;
3876 else
3877 break;
3879 if (*args && args[1])
3881 Msg(0, "%s: usage: resize [-h] [-v] [-l] [num]\n", rc_name);
3882 break;
3884 if (*args)
3885 ResizeRegions(*args, i);
3886 else
3887 Input(resizeprompts[i], 20, INP_EVERY, ResizeFin, (char*)0, i);
3888 break;
3889 case RC_SETSID:
3890 (void)ParseSwitch(act, &separate_sids);
3891 break;
3892 case RC_EVAL:
3893 args = SaveArgs(args);
3894 for (i = 0; args[i]; i++)
3896 if (args[i][0])
3897 Colonfin(args[i], strlen(args[i]), (char *)0);
3898 free(args[i]);
3900 free(args);
3901 break;
3902 case RC_ALTSCREEN:
3903 (void)ParseSwitch(act, &use_altscreen);
3904 if (msgok)
3905 Msg(0, "Will %sdo alternate screen switching", use_altscreen ? "" : "not ");
3906 break;
3907 case RC_MAXWIN:
3908 if (ParseNum(act, &n))
3909 break;
3910 if (n < 1)
3911 Msg(0, "illegal maxwin number specified");
3912 else if (n > maxwin)
3913 Msg(0, "may only decrease maxwin number");
3914 else
3915 maxwin = n;
3916 break;
3917 case RC_BACKTICK:
3918 if (ParseBase(act, *args, &n, 10, "decimal"))
3919 break;
3920 if (!args[1])
3921 setbacktick(n, 0, 0, (char **)0);
3922 else
3924 int lifespan, tick;
3925 if (argc < 4)
3927 Msg(0, "%s: usage: backtick num [lifespan tick cmd args...]", rc_name);
3928 break;
3930 if (ParseBase(act, args[1], &lifespan, 10, "decimal"))
3931 break;
3932 if (ParseBase(act, args[2], &tick, 10, "decimal"))
3933 break;
3934 setbacktick(n, lifespan, tick, SaveArgs(args + 3));
3936 WindowChanged(0, '`');
3937 break;
3938 case RC_BLANKER:
3939 #ifdef BLANKER_PRG
3940 if (blankerprg)
3942 RunBlanker(blankerprg);
3943 break;
3945 #endif
3946 ClearAll();
3947 CursorVisibility(-1);
3948 D_blocked = 4;
3949 break;
3950 #ifdef BLANKER_PRG
3951 case RC_BLANKERPRG:
3952 if (blankerprg)
3954 char **pp;
3955 for (pp = blankerprg; *pp; pp++)
3956 free(*pp);
3957 free(blankerprg);
3958 blankerprg = 0;
3960 if (args[0][0])
3961 blankerprg = SaveArgs(args);
3962 break;
3963 #endif
3964 case RC_IDLE:
3965 if (*args)
3967 struct display *olddisplay = display;
3968 if (!strcmp(*args, "off"))
3969 idletimo = 0;
3970 else if (args[0][0])
3971 idletimo = atoi(*args) * 1000;
3972 if (argc > 1)
3974 if ((i = FindCommnr(args[1])) == RC_ILLEGAL)
3976 Msg(0, "%s: idle: unknown command '%s'", rc_name, args[1]);
3977 break;
3979 if (CheckArgNum(i, args + 2) < 0)
3980 break;
3981 ClearAction(&idleaction);
3982 SaveAction(&idleaction, i, args + 2, argl + 2);
3984 for (display = displays; display; display = display->d_next)
3985 ResetIdle();
3986 display = olddisplay;
3988 if (msgok)
3990 if (idletimo)
3991 Msg(0, "idle timeout %ds, %s", idletimo / 1000, comms[idleaction.nr].name);
3992 else
3993 Msg(0, "idle off");
3995 break;
3996 case RC_FOCUSMINSIZE:
3997 for (i = 0; i < 2 && args[i]; i++)
3999 if (!strcmp(args[i], "max") || !strcmp(args[i], "_"))
4000 n = -1;
4001 else
4002 n = atoi(args[i]);
4003 if (i == 0)
4004 focusminwidth = n;
4005 else
4006 focusminheight = n;
4008 if (msgok)
4010 char b[2][20];
4011 for (i = 0; i < 2; i++)
4013 n = i == 0 ? focusminwidth : focusminheight;
4014 if (n == -1)
4015 strcpy(b[i], "max");
4016 else
4017 sprintf(b[i], "%d", n);
4019 Msg(0, "focus min size is %s %s\n", b[0], b[1]);
4021 break;
4022 case RC_GROUP:
4023 if (*args)
4025 fore->w_group = 0;
4026 if (args[0][0])
4028 fore->w_group = WindowByName(*args);
4029 if (fore->w_group && fore->w_group->w_type != W_TYPE_GROUP)
4030 fore->w_group = 0;
4032 WindowChanged((struct win *)0, 'w');
4033 WindowChanged((struct win *)0, 'W');
4034 WindowChanged((struct win *)0, 0);
4036 if (msgok)
4038 if (fore->w_group)
4039 Msg(0, "window group is %d (%s)\n", fore->w_group->w_number, fore->w_group->w_title);
4040 else
4041 Msg(0, "window belongs to no group");
4043 break;
4044 case RC_LAYOUT:
4045 if (!strcmp(args[0], "title"))
4047 if (!D_layout)
4049 Msg(0, "not on a layout");
4050 break;
4052 if (!args[1])
4054 Msg(0, "current layout is %d (%s)", D_layout->lay_number, D_layout->lay_title);
4055 break;
4057 free(D_layout->lay_title);
4058 D_layout->lay_title= SaveStr(args[1]);
4060 else if (!strcmp(args[0], "number"))
4062 int old;
4063 struct layout *lay;
4064 if (!args[1])
4066 Msg(0, "This is layout %d (%s).\n", D_layout->lay_number, D_layout->lay_title);
4067 break;
4069 old = D_layout->lay_number;
4070 n = atoi(args[1]);
4071 if (n < 0 || n >= MAXLAY)
4072 break;
4073 lay = laytab[n];
4074 laytab[n] = D_layout;
4075 D_layout->lay_number = n;
4076 laytab[old] = lay;
4077 if (lay)
4078 lay->lay_number = old;
4079 break;
4081 else if (!strcmp(args[0], "autosave"))
4083 if (!D_layout)
4085 Msg(0, "not on a layout");
4086 break;
4088 if (args[1])
4090 if (!strcmp(args[1], "on"))
4091 D_layout->lay_autosave = 1;
4092 else if (!strcmp(args[1], "off"))
4093 D_layout->lay_autosave = 0;
4094 else
4096 Msg(0, "invalid argument. Give 'on' or 'off");
4097 break;
4100 if (msgok)
4101 Msg(0, "autosave is %s", D_layout->lay_autosave ? "on" : "off");
4103 else if (!strcmp(args[0], "new"))
4105 char *t = args[1];
4106 n = 0;
4107 if (t)
4109 while (*t >= '0' && *t <= '9')
4110 t++;
4111 if (t != args[1] && (!*t || *t == ':'))
4113 n = atoi(args[1]);
4114 if (*t)
4115 t++;
4117 else
4118 t = args[1];
4120 if (!t || !*t)
4121 t = "layout";
4122 NewLayout(t, n);
4123 Activate(-1);
4125 else if (!strcmp(args[0], "save"))
4127 if (!args[1])
4129 Msg(0, "usage: layout save <name>");
4130 break;
4132 SaveLayout(args[1], &D_canvas);
4134 else if (!strcmp(args[0], "select"))
4136 if (!args[1])
4138 Input("Switch to layout: ", 20, INP_COOKED, SelectLayoutFin, NULL, 0);
4139 break;
4141 SelectLayoutFin(args[1], strlen(args[1]), (char *)0);
4143 else if (!strcmp(args[0], "next"))
4145 struct layout *lay = D_layout;
4146 if (lay)
4147 lay = lay->lay_next ? lay->lay_next : layouts;
4148 else
4149 lay = layouts;
4150 if (!lay)
4152 Msg(0, "no layout defined");
4153 break;
4155 if (lay == D_layout)
4156 break;
4157 LoadLayout(lay, &D_canvas);
4158 Activate(-1);
4160 else if (!strcmp(args[0], "prev"))
4162 struct layout *lay = D_layout;
4163 if (lay)
4165 for (lay = layouts; lay->lay_next && lay->lay_next != D_layout; lay = lay->lay_next)
4168 else
4169 lay = layouts;
4170 if (!lay)
4172 Msg(0, "no layout defined");
4173 break;
4175 if (lay == D_layout)
4176 break;
4177 LoadLayout(lay, &D_canvas);
4178 Activate(-1);
4180 else if (!strcmp(args[0], "attach"))
4182 if (!args[1])
4184 if (!layout_attach)
4185 Msg(0, "no attach layout set");
4186 else if (layout_attach == &layout_last_marker)
4187 Msg(0, "will attach to last layout");
4188 else
4189 Msg(0, "will attach to layout %d (%s)", layout_attach->lay_number, layout_attach->lay_title);
4190 break;
4192 if (!strcmp(args[1], ":last"))
4193 layout_attach = &layout_last_marker;
4194 else if (!args[1][0])
4195 layout_attach = 0;
4196 else
4198 struct layout *lay;
4199 lay = FindLayout(args[1]);
4200 if (!lay)
4202 Msg(0, "unknown layout '%s'", args[1]);
4203 break;
4205 layout_attach = lay;
4208 else if (!strcmp(args[0], "show"))
4210 ShowLayouts(-1);
4212 else if (!strcmp(args[0], "remove"))
4214 struct layout *lay = display ? D_layout : layouts;
4215 if (args[1])
4217 lay = layouts ? FindLayout(args[1]) : (struct layout *)0;
4218 if (!lay)
4220 Msg(0, "unknown layout '%s'", args[1]);
4221 break;
4224 if (lay)
4225 RemoveLayout(lay);
4227 else
4228 Msg(0, "unknown layout subcommand");
4229 break;
4230 #ifdef DW_CHARS
4231 case RC_CJKWIDTH:
4232 if(ParseSwitch(act, &cjkwidth) == 0)
4234 if(msgok)
4235 Msg(0, "Treat ambiguous width characters as %s width", cjkwidth ? "full" : "half");
4237 break;
4238 #endif
4239 default:
4240 #ifdef HAVE_BRAILLE
4241 /* key == -2: input from braille keybord, msgok always 0 */
4242 DoBrailleAction(act, key == -2 ? 0 : msgok);
4243 #endif
4244 break;
4246 if (display != odisplay)
4248 for (display = displays; display; display = display->d_next)
4249 if (display == odisplay)
4250 break;
4254 void
4255 DoCommand(argv, argl)
4256 char **argv;
4257 int *argl;
4259 struct action act;
4261 if ((act.nr = FindCommnr(*argv)) == RC_ILLEGAL)
4263 Msg(0, "%s: unknown command '%s'", rc_name, *argv);
4264 return;
4266 act.args = argv + 1;
4267 act.argl = argl + 1;
4268 DoAction(&act, -1);
4271 static void
4272 SaveAction(act, nr, args, argl)
4273 struct action *act;
4274 int nr;
4275 char **args;
4276 int *argl;
4278 register int argc = 0;
4279 char **pp;
4280 int *lp;
4282 if (args)
4283 while (args[argc])
4284 argc++;
4285 if (argc == 0)
4287 act->nr = nr;
4288 act->args = noargs;
4289 act->argl = 0;
4290 return;
4292 if ((pp = (char **)malloc((unsigned)(argc + 1) * sizeof(char **))) == 0)
4293 Panic(0, strnomem);
4294 if ((lp = (int *)malloc((unsigned)(argc) * sizeof(int *))) == 0)
4295 Panic(0, strnomem);
4296 act->nr = nr;
4297 act->args = pp;
4298 act->argl = lp;
4299 while (argc--)
4301 *lp = argl ? *argl++ : (int)strlen(*args);
4302 *pp++ = SaveStrn(*args++, *lp++);
4304 *pp = 0;
4307 static char **
4308 SaveArgs(args)
4309 char **args;
4311 register char **ap, **pp;
4312 register int argc = 0;
4314 while (args[argc])
4315 argc++;
4316 if ((pp = ap = (char **)malloc((unsigned)(argc + 1) * sizeof(char **))) == 0)
4317 Panic(0, strnomem);
4318 while (argc--)
4319 *pp++ = SaveStr(*args++);
4320 *pp = 0;
4321 return ap;
4326 * buf is split into argument vector args.
4327 * leading whitespace is removed.
4328 * @!| abbreviations are expanded.
4329 * the end of buffer is recognized by '\0' or an un-escaped '#'.
4330 * " and ' are interpreted.
4332 * argc is returned.
4334 int
4335 Parse(buf, bufl, args, argl)
4336 char *buf, **args;
4337 int bufl, *argl;
4339 register char *p = buf, **ap = args, *pp;
4340 register int delim, argc;
4341 int *lp = argl;
4343 debug2("Parse %d %s\n", bufl, buf);
4344 argc = 0;
4345 pp = buf;
4346 delim = 0;
4347 for (;;)
4349 while (*p && (*p == ' ' || *p == '\t'))
4350 ++p;
4351 #ifdef PSEUDOS
4352 if (argc == 0 && *p == '!')
4354 *ap++ = "exec";
4355 *lp++ = 4;
4356 p++;
4357 argc++;
4358 continue;
4360 #endif
4361 if (*p == '\0' || *p == '#' || *p == '\n')
4363 *p = '\0';
4364 for (delim = 0; delim < argc; delim++)
4365 debug1("-- %s\n", args[delim]);
4366 args[argc] = 0;
4367 return argc;
4369 if (++argc >= MAXARGS)
4371 Msg(0, "%s: too many tokens.", rc_name);
4372 return 0;
4374 *ap++ = pp;
4376 debug1("- new arg %s\n", p);
4377 while (*p)
4379 if (*p == delim)
4380 delim = 0;
4381 else if (delim != '\'' && *p == '\\' && (p[1] == '\'' || p[1] == '"' || p[1] == '\\' || p[1] == '$' || p[1] == '#' || p[1] == '^' || (p[1] >= '0' && p[1] <= '7')))
4383 p++;
4384 if (*p >= '0' && *p <= '7')
4386 *pp = *p - '0';
4387 if (p[1] >= '0' && p[1] <= '7')
4389 p++;
4390 *pp = (*pp << 3) | (*p - '0');
4391 if (p[1] >= '0' && p[1] <= '7')
4393 p++;
4394 *pp = (*pp << 3) | (*p - '0');
4397 pp++;
4399 else
4400 *pp++ = *p;
4402 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] == '_'))
4405 char *ps, *pe, op, *v, xbuf[11];
4406 int vl;
4408 ps = ++p;
4409 debug1("- var %s\n", ps);
4410 p++;
4411 while (*p)
4413 if (*ps == '{' && *p == '}')
4414 break;
4415 if (*ps == ':' && *p == ':')
4416 break;
4417 if (*ps != '{' && *ps != ':' && (*p < 'a' || *p > 'z') && (*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9') && *p != '_')
4418 break;
4419 p++;
4421 pe = p;
4422 if (*ps == '{' || *ps == ':')
4424 if (!*p)
4426 Msg(0, "%s: bad variable name.", rc_name);
4427 return 0;
4429 p++;
4431 op = *pe;
4432 *pe = 0;
4433 debug1("- var is '%s'\n", ps);
4434 if (*ps == ':')
4435 v = gettermcapstring(ps + 1);
4436 else
4438 if (*ps == '{')
4439 ps++;
4440 v = xbuf;
4441 if (!strcmp(ps, "TERM"))
4442 v = display ? D_termname : "unknown";
4443 else if (!strcmp(ps, "COLUMNS"))
4444 sprintf(xbuf, "%d", display ? D_width : -1);
4445 else if (!strcmp(ps, "LINES"))
4446 sprintf(xbuf, "%d", display ? D_height : -1);
4447 else
4448 v = getenv(ps);
4450 *pe = op;
4451 vl = v ? strlen(v) : 0;
4452 if (vl)
4454 debug1("- sub is '%s'\n", v);
4455 if (p - pp < vl)
4457 int right = buf + bufl - (p + strlen(p) + 1);
4458 if (right > 0)
4460 bcopy(p, p + right, strlen(p) + 1);
4461 p += right;
4464 if (p - pp < vl)
4466 Msg(0, "%s: no space left for variable expansion.", rc_name);
4467 return 0;
4469 bcopy(v, pp, vl);
4470 pp += vl;
4472 continue;
4474 else if (delim != '\'' && *p == '^' && p[1])
4476 p++;
4477 *pp++ = *p == '?' ? '\177' : *p & 0x1f;
4479 else if (delim == 0 && (*p == '\'' || *p == '"'))
4480 delim = *p;
4481 else if (delim == 0 && (*p == ' ' || *p == '\t' || *p == '\n'))
4482 break;
4483 else
4484 *pp++ = *p;
4485 p++;
4487 if (delim)
4489 Msg(0, "%s: Missing %c quote.", rc_name, delim);
4490 return 0;
4492 if (*p)
4493 p++;
4494 *pp = 0;
4495 debug2("- arg done, '%s' rest %s\n", ap[-1], p);
4496 *lp++ = pp - ap[-1];
4497 pp++;
4501 void
4502 SetEscape(u, e, me)
4503 struct acluser *u;
4504 int e, me;
4506 if (u)
4508 u->u_Esc = e;
4509 u->u_MetaEsc = me;
4511 else
4513 if (users)
4515 if (DefaultEsc >= 0)
4516 ClearAction(&ktab[DefaultEsc]);
4517 if (DefaultMetaEsc >= 0)
4518 ClearAction(&ktab[DefaultMetaEsc]);
4520 DefaultEsc = e;
4521 DefaultMetaEsc = me;
4522 if (users)
4524 if (DefaultEsc >= 0)
4526 ClearAction(&ktab[DefaultEsc]);
4527 ktab[DefaultEsc].nr = RC_OTHER;
4529 if (DefaultMetaEsc >= 0)
4531 ClearAction(&ktab[DefaultMetaEsc]);
4532 ktab[DefaultMetaEsc].nr = RC_META;
4539 ParseSwitch(act, var)
4540 struct action *act;
4541 int *var;
4543 if (*act->args == 0)
4545 *var ^= 1;
4546 return 0;
4548 return ParseOnOff(act, var);
4551 static int
4552 ParseOnOff(act, var)
4553 struct action *act;
4554 int *var;
4556 register int num = -1;
4557 char **args = act->args;
4559 if (args[1] == 0)
4561 if (strcmp(args[0], "on") == 0)
4562 num = 1;
4563 else if (strcmp(args[0], "off") == 0)
4564 num = 0;
4566 if (num < 0)
4568 Msg(0, "%s: %s: invalid argument. Give 'on' or 'off'", rc_name, comms[act->nr].name);
4569 return -1;
4571 *var = num;
4572 return 0;
4576 ParseSaveStr(act, var)
4577 struct action *act;
4578 char **var;
4580 char **args = act->args;
4581 if (*args == 0 || args[1])
4583 Msg(0, "%s: %s: one argument required.", rc_name, comms[act->nr].name);
4584 return -1;
4586 if (*var)
4587 free(*var);
4588 *var = SaveStr(*args);
4589 return 0;
4593 ParseNum(act, var)
4594 struct action *act;
4595 int *var;
4597 int i;
4598 char *p, **args = act->args;
4600 p = *args;
4601 if (p == 0 || *p == 0 || args[1])
4603 Msg(0, "%s: %s: invalid argument. Give one argument.",
4604 rc_name, comms[act->nr].name);
4605 return -1;
4607 i = 0;
4608 while (*p)
4610 if (*p >= '0' && *p <= '9')
4611 i = 10 * i + (*p - '0');
4612 else
4614 Msg(0, "%s: %s: invalid argument. Give numeric argument.",
4615 rc_name, comms[act->nr].name);
4616 return -1;
4618 p++;
4620 debug1("ParseNum got %d\n", i);
4621 *var = i;
4622 return 0;
4625 static int
4626 ParseNum1000(act, var)
4627 struct action *act;
4628 int *var;
4630 int i;
4631 char *p, **args = act->args;
4632 int dig = 0;
4634 p = *args;
4635 if (p == 0 || *p == 0 || args[1])
4637 Msg(0, "%s: %s: invalid argument. Give one argument.",
4638 rc_name, comms[act->nr].name);
4639 return -1;
4641 i = 0;
4642 while (*p)
4644 if (*p >= '0' && *p <= '9')
4646 if (dig < 4)
4647 i = 10 * i + (*p - '0');
4648 else if (dig == 4 && *p >= '5')
4649 i++;
4650 if (dig)
4651 dig++;
4653 else if (*p == '.' && !dig)
4654 dig++;
4655 else
4657 Msg(0, "%s: %s: invalid argument. Give floating point argument.",
4658 rc_name, comms[act->nr].name);
4659 return -1;
4661 p++;
4663 if (dig == 0)
4664 i *= 1000;
4665 else
4666 while (dig++ < 4)
4667 i *= 10;
4668 if (i < 0)
4669 i = (int)((unsigned int)~0 >> 1);
4670 debug1("ParseNum1000 got %d\n", i);
4671 *var = i;
4672 return 0;
4675 static struct win *
4676 WindowByName(s)
4677 char *s;
4679 struct win *p;
4681 for (p = windows; p; p = p->w_next)
4682 if (!strcmp(p->w_title, s))
4683 return p;
4684 for (p = windows; p; p = p->w_next)
4685 if (!strncmp(p->w_title, s, strlen(s)))
4686 return p;
4687 return 0;
4690 static int
4691 WindowByNumber(str)
4692 char *str;
4694 int i;
4695 char *s;
4697 for (i = 0, s = str; *s; s++)
4699 if (*s < '0' || *s > '9')
4700 break;
4701 i = i * 10 + (*s - '0');
4703 return *s ? -1 : i;
4707 * Get window number from Name or Number string.
4708 * Numbers are tried first, then names, a prefix match suffices.
4709 * Be careful when assigning numeric strings as WindowTitles.
4712 WindowByNoN(str)
4713 char *str;
4715 int i;
4716 struct win *p;
4718 if ((i = WindowByNumber(str)) < 0 || i >= MAXWIN)
4720 if ((p = WindowByName(str)))
4721 return p->w_number;
4722 return -1;
4724 return i;
4727 static int
4728 ParseWinNum(act, var)
4729 struct action *act;
4730 int *var;
4732 char **args = act->args;
4733 int i = 0;
4735 if (*args == 0 || args[1])
4737 Msg(0, "%s: %s: one argument required.", rc_name, comms[act->nr].name);
4738 return -1;
4741 i = WindowByNoN(*args);
4742 if (i < 0)
4744 Msg(0, "%s: %s: invalid argument. Give window number or name.",
4745 rc_name, comms[act->nr].name);
4746 return -1;
4748 debug1("ParseWinNum got %d\n", i);
4749 *var = i;
4750 return 0;
4753 static int
4754 ParseBase(act, p, var, base, bname)
4755 struct action *act;
4756 char *p;
4757 int *var;
4758 int base;
4759 char *bname;
4761 int i = 0;
4762 int c;
4764 if (*p == 0)
4766 Msg(0, "%s: %s: empty argument.", rc_name, comms[act->nr].name);
4767 return -1;
4769 while ((c = *p++))
4771 if (c >= 'a' && c <= 'z')
4772 c -= 'a' - 'A';
4773 if (c >= 'A' && c <= 'Z')
4774 c -= 'A' - ('0' + 10);
4775 c -= '0';
4776 if (c < 0 || c >= base)
4778 Msg(0, "%s: %s: argument is not %s.", rc_name, comms[act->nr].name, bname);
4779 return -1;
4781 i = base * i + c;
4783 debug1("ParseBase got %d\n", i);
4784 *var = i;
4785 return 0;
4788 static int
4789 IsNum(s, base)
4790 register char *s;
4791 register int base;
4793 for (base += '0'; *s; ++s)
4794 if (*s < '0' || *s > base)
4795 return 0;
4796 return 1;
4800 IsNumColon(s, base, p, psize)
4801 int base, psize;
4802 char *s, *p;
4804 char *q;
4805 if ((q = rindex(s, ':')) != 0)
4807 strncpy(p, q + 1, psize - 1);
4808 p[psize - 1] = '\0';
4809 *q = '\0';
4811 else
4812 *p = '\0';
4813 return IsNum(s, base);
4816 void
4817 SwitchWindow(n)
4818 int n;
4820 struct win *p;
4822 debug1("SwitchWindow %d\n", n);
4823 if (n < 0 || n >= MAXWIN)
4825 ShowWindows(-1);
4826 return;
4828 if ((p = wtab[n]) == 0)
4830 ShowWindows(n);
4831 return;
4833 if (display == 0)
4835 fore = p;
4836 return;
4838 if (p == D_fore)
4840 Msg(0, "This IS window %d (%s).", n, p->w_title);
4841 return;
4843 #ifdef MULTIUSER
4844 if (AclCheckPermWin(D_user, ACL_READ, p))
4846 Msg(0, "Access to window %d denied.", p->w_number);
4847 return;
4849 #endif
4850 SetForeWindow(p);
4851 Activate(fore->w_norefresh);
4855 void
4856 SetCanvasWindow(cv, wi)
4857 struct canvas *cv;
4858 struct win *wi;
4860 struct win *p = 0, **pp;
4861 struct layer *l;
4862 struct canvas *cvp, **cvpp;
4864 l = cv->c_layer;
4865 display = cv->c_display;
4867 if (l)
4869 /* remove old layer */
4870 for (cvpp = &l->l_cvlist; (cvp = *cvpp); cvpp = &cvp->c_lnext)
4871 if (cvp == cv)
4872 break;
4873 ASSERT(cvp);
4874 *cvpp = cvp->c_lnext;
4876 p = Layer2Window(l);
4877 l = cv->c_layer;
4878 cv->c_layer = 0;
4880 if (p && cv == D_forecv)
4882 #ifdef MULTIUSER
4883 ReleaseAutoWritelock(display, p);
4884 #endif
4885 if (p->w_silence)
4887 SetTimeout(&p->w_silenceev, p->w_silencewait * 1000);
4888 evenq(&p->w_silenceev);
4890 D_other = fore;
4891 D_fore = 0;
4893 if (l->l_cvlist == 0 && (p == 0 || l != p->w_savelayer))
4894 KillLayerChain(l);
4897 /* find right layer to display on canvas */
4898 if (wi && wi->w_type != W_TYPE_GROUP)
4900 l = &wi->w_layer;
4901 if (wi->w_savelayer && (wi->w_blocked || wi->w_savelayer->l_cvlist == 0))
4902 l = wi->w_savelayer;
4904 else
4906 l = &cv->c_blank;
4907 if (wi)
4908 l->l_data = (char *)wi;
4909 else
4910 l->l_data = 0;
4913 /* add our canvas to the layer's canvaslist */
4914 ASSERT(l->l_cvlist != cv);
4915 cv->c_lnext = l->l_cvlist;
4916 l->l_cvlist = cv;
4917 cv->c_layer = l;
4918 cv->c_xoff = cv->c_xs;
4919 cv->c_yoff = cv->c_ys;
4920 RethinkViewportOffsets(cv);
4922 if (flayer == 0)
4923 flayer = l;
4925 if (wi && wi->w_type == W_TYPE_GROUP)
4927 /* auto-start windowlist on groups */
4928 struct display *d = display;
4929 struct layer *oldflayer = flayer;
4930 flayer = l;
4931 display_wlist(0, 0, wi);
4932 flayer = oldflayer;
4933 display = d;
4936 if (wi && D_other == wi)
4937 D_other = wi->w_next; /* Might be 0, but that's OK. */
4938 if (cv == D_forecv)
4940 D_fore = wi;
4941 fore = D_fore; /* XXX ? */
4942 if (wi)
4944 #ifdef MULTIUSER
4945 ObtainAutoWritelock(display, wi);
4946 #endif
4948 * Place the window at the head of the most-recently-used list
4950 if (windows != wi)
4952 for (pp = &windows; (p = *pp); pp = &p->w_next)
4953 if (p == wi)
4954 break;
4955 ASSERT(p);
4956 *pp = p->w_next;
4957 p->w_next = windows;
4958 windows = p;
4959 WListLinkChanged();
4967 * SetForeWindow changes the window in the input focus of the display.
4968 * Puts window wi in canvas display->d_forecv.
4970 void
4971 SetForeWindow(wi)
4972 struct win *wi;
4974 struct win *p;
4975 if (display == 0)
4977 fore = wi;
4978 return;
4980 p = Layer2Window(D_forecv->c_layer);
4981 SetCanvasWindow(D_forecv, wi);
4982 if (p)
4983 WindowChanged(p, 'u');
4984 if (wi)
4985 WindowChanged(wi, 'u');
4986 flayer = D_forecv->c_layer;
4987 /* Activate called afterwards, so no RefreshHStatus needed */
4991 /*****************************************************************/
4994 * Activate - make fore window active
4995 * norefresh = -1 forces a refresh, disregard all_norefresh then.
4997 void
4998 Activate(norefresh)
4999 int norefresh;
5001 debug1("Activate(%d)\n", norefresh);
5002 if (display == 0)
5003 return;
5004 if (D_status)
5006 Msg(0, "%s", ""); /* wait till mintime (keep gcc quiet) */
5007 RemoveStatus();
5010 if (MayResizeLayer(D_forecv->c_layer))
5011 ResizeLayer(D_forecv->c_layer, D_forecv->c_xe - D_forecv->c_xs + 1, D_forecv->c_ye - D_forecv->c_ys + 1, display);
5013 fore = D_fore;
5014 if (fore)
5016 /* XXX ? */
5017 if (fore->w_monitor != MON_OFF)
5018 fore->w_monitor = MON_ON;
5019 fore->w_bell = BELL_ON;
5020 WindowChanged(fore, 'f');
5022 #if 0
5023 if (ResizeDisplay(fore->w_width, fore->w_height))
5025 debug2("Cannot resize from (%d,%d)", D_width, D_height);
5026 debug2(" to (%d,%d) -> resize window\n", fore->w_width, fore->w_height);
5027 DoResize(D_width, D_height);
5029 #endif
5031 Redisplay(norefresh + all_norefresh);
5035 static int
5036 NextWindow()
5038 register struct win **pp;
5039 int n = fore ? fore->w_number : MAXWIN;
5040 struct win *group = fore ? fore->w_group : 0;
5042 for (pp = fore ? wtab + n + 1 : wtab; pp != wtab + n; pp++)
5044 if (pp == wtab + MAXWIN)
5045 pp = wtab;
5046 if (*pp)
5048 if (!fore || group == (*pp)->w_group)
5049 break;
5052 if (pp == wtab + n)
5053 return -1;
5054 return pp - wtab;
5057 static int
5058 PreviousWindow()
5060 register struct win **pp;
5061 int n = fore ? fore->w_number : -1;
5062 struct win *group = fore ? fore->w_group : 0;
5064 for (pp = wtab + n - 1; pp != wtab + n; pp--)
5066 if (pp == wtab - 1)
5067 pp = wtab + MAXWIN - 1;
5068 if (*pp)
5070 if (!fore || group == (*pp)->w_group)
5071 break;
5074 if (pp == wtab + n)
5075 return -1;
5076 return pp - wtab;
5079 static int
5080 MoreWindows()
5082 char *m = "No other window.";
5083 if (windows && (fore == 0 || windows->w_next))
5084 return 1;
5085 if (fore == 0)
5087 Msg(0, "No window available");
5088 return 0;
5090 Msg(0, m, fore->w_number); /* other arg for nethack */
5091 return 0;
5094 static void
5095 UpdateLayoutCanvas(cv, wi)
5096 struct canvas *cv;
5097 struct win *wi;
5099 for (; cv; cv = cv->c_slnext)
5101 if (cv->c_layer && Layer2Window(cv->c_layer) == wi)
5103 /* A simplistic version of SetCanvasWindow(cv, 0) */
5104 struct layer *l = cv->c_layer;
5105 cv->c_layer = 0;
5106 if (l->l_cvlist == 0 && (wi == 0 || l != wi->w_savelayer))
5107 KillLayerChain(l);
5108 l = &cv->c_blank;
5109 l->l_data = 0;
5110 if (l->l_cvlist != cv)
5112 cv->c_lnext = l->l_cvlist;
5113 l->l_cvlist = cv;
5115 cv->c_layer = l;
5116 /* Do not end here. Multiple canvases can have the same window */
5119 if (cv->c_slperp)
5120 UpdateLayoutCanvas(cv->c_slperp, wi);
5124 void
5125 KillWindow(wi)
5126 struct win *wi;
5128 struct win **pp, *p;
5129 struct canvas *cv;
5130 int gotone;
5131 struct layout *lay;
5134 * Remove window from linked list.
5136 for (pp = &windows; (p = *pp); pp = &p->w_next)
5137 if (p == wi)
5138 break;
5139 ASSERT(p);
5140 *pp = p->w_next;
5141 wi->w_inlen = 0;
5142 wtab[wi->w_number] = 0;
5144 if (windows == 0)
5146 FreeWindow(wi);
5147 Finit(0);
5151 * switch to different window on all canvases
5153 for (display = displays; display; display = display->d_next)
5155 gotone = 0;
5156 for (cv = D_cvlist; cv; cv = cv->c_next)
5158 if (Layer2Window(cv->c_layer) != wi)
5159 continue;
5160 /* switch to other window */
5161 SetCanvasWindow(cv, FindNiceWindow(D_other, 0));
5162 gotone = 1;
5164 if (gotone)
5166 #ifdef ZMODEM
5167 if (wi->w_zdisplay == display)
5169 D_blocked = 0;
5170 D_readev.condpos = D_readev.condneg = 0;
5172 #endif
5173 Activate(-1);
5177 /* do the same for the layouts */
5178 for (lay = layouts; lay; lay = lay->lay_next)
5179 UpdateLayoutCanvas(&lay->lay_canvas, wi);
5181 FreeWindow(wi);
5182 WindowChanged((struct win *)0, 'w');
5183 WindowChanged((struct win *)0, 'W');
5184 WindowChanged((struct win *)0, 0);
5187 static void
5188 LogToggle(on)
5189 int on;
5191 char buf[1024];
5193 if ((fore->w_log != 0) == on)
5195 if (display && !*rc_name)
5196 Msg(0, "You are %s logging.", on ? "already" : "not");
5197 return;
5199 if (fore->w_log != 0)
5201 Msg(0, "Logfile \"%s\" closed.", fore->w_log->name);
5202 logfclose(fore->w_log);
5203 fore->w_log = 0;
5204 WindowChanged(fore, 'f');
5205 return;
5207 if (DoStartLog(fore, buf, sizeof(buf)))
5209 Msg(errno, "Error opening logfile \"%s\"", buf);
5210 return;
5212 if (ftell(fore->w_log->fp) == 0)
5213 Msg(0, "Creating logfile \"%s\".", fore->w_log->name);
5214 else
5215 Msg(0, "Appending to logfile \"%s\".", fore->w_log->name);
5216 WindowChanged(fore, 'f');
5219 char *
5220 AddWindows(buf, len, flags, where)
5221 char *buf;
5222 int len;
5223 int flags;
5224 int where;
5226 register char *s, *ss;
5227 register struct win **pp, *p;
5228 register char *cmd;
5229 int l;
5231 s = ss = buf;
5232 for (pp = ((flags & 4) && where >= 0) ? wtab + where + 1: wtab; pp < wtab + MAXWIN; pp++)
5234 if (pp - wtab == where && ss == buf)
5235 ss = s;
5236 if ((p = *pp) == 0)
5237 continue;
5238 if ((flags & 1) && display && p == D_fore)
5239 continue;
5240 if (D_fore && D_fore->w_group != p->w_group)
5241 continue;
5243 cmd = p->w_title;
5244 l = strlen(cmd);
5245 if (l > 20)
5246 l = 20;
5247 if (s - buf + l > len - 24)
5248 break;
5249 if (s > buf || (flags & 4))
5251 *s++ = ' ';
5252 *s++ = ' ';
5254 sprintf(s, "%d", p->w_number);
5255 if (p->w_number == where)
5256 ss = s;
5257 s += strlen(s);
5258 if (display && p == D_fore)
5259 *s++ = '*';
5260 if (!(flags & 2))
5262 if (display && p == D_other)
5263 *s++ = '-';
5264 s = AddWindowFlags(s, len, p);
5266 *s++ = ' ';
5267 strncpy(s, cmd, l);
5268 s += l;
5270 *s = 0;
5271 return ss;
5274 char *
5275 AddWindowFlags(buf, len, p)
5276 char *buf;
5277 int len;
5278 struct win *p;
5280 char *s = buf;
5281 if (p == 0 || len < 12)
5283 *s = 0;
5284 return s;
5286 #if 0
5287 if (display && p == D_fore)
5288 *s++ = '*';
5289 if (display && p == D_other)
5290 *s++ = '-';
5291 #endif
5292 if (p->w_layer.l_cvlist && p->w_layer.l_cvlist->c_lnext)
5293 *s++ = '&';
5294 if (p->w_monitor == MON_DONE
5295 #ifdef MULTIUSER
5296 && (ACLBYTE(p->w_mon_notify, D_user->u_id) & ACLBIT(D_user->u_id))
5297 #endif
5299 *s++ = '@';
5300 if (p->w_bell == BELL_DONE)
5301 *s++ = '!';
5302 #ifdef UTMPOK
5303 if (p->w_slot != (slot_t) 0 && p->w_slot != (slot_t) -1)
5304 *s++ = '$';
5305 #endif
5306 if (p->w_log != 0)
5308 strcpy(s, "(L)");
5309 s += 3;
5311 if (p->w_ptyfd < 0 && p->w_type != W_TYPE_GROUP)
5312 *s++ = 'Z';
5313 *s = 0;
5314 return s;
5317 char *
5318 AddOtherUsers(buf, len, p)
5319 char *buf;
5320 int len;
5321 struct win *p;
5323 struct display *d, *olddisplay = display;
5324 struct canvas *cv;
5325 char *s;
5326 int l;
5328 s = buf;
5329 for (display = displays; display; display = display->d_next)
5331 if (olddisplay && D_user == olddisplay->d_user)
5332 continue;
5333 for (cv = D_cvlist; cv; cv = cv->c_next)
5334 if (Layer2Window(cv->c_layer) == p)
5335 break;
5336 if (!cv)
5337 continue;
5338 for (d = displays; d && d != display; d = d->d_next)
5339 if (D_user == d->d_user)
5340 break;
5341 if (d && d != display)
5342 continue;
5343 if (len > 1 && s != buf)
5345 *s++ = ',';
5346 len--;
5348 l = strlen(D_user->u_name);
5349 if (l + 1 > len)
5350 break;
5351 strcpy(s, D_user->u_name);
5352 s += l;
5353 len -= l;
5355 *s = 0;
5356 display = olddisplay;
5357 return s;
5360 void
5361 ShowWindows(where)
5362 int where;
5364 char buf[1024];
5365 char *s, *ss;
5367 if (!display)
5368 return;
5369 if (where == -1 && D_fore)
5370 where = D_fore->w_number;
5371 ss = AddWindows(buf, sizeof(buf), 0, where);
5372 s = buf + strlen(buf);
5373 if (ss - buf > D_width / 2)
5375 ss -= D_width / 2;
5376 if (s - ss < D_width)
5378 ss = s - D_width;
5379 if (ss < buf)
5380 ss = buf;
5383 else
5384 ss = buf;
5385 Msg(0, "%s", ss);
5388 static void
5389 ShowInfo()
5391 char buf[512], *p;
5392 register struct win *wp = fore;
5393 register int i;
5395 if (wp == 0)
5397 Msg(0, "(%d,%d)/(%d,%d) no window", D_x + 1, D_y + 1, D_width, D_height);
5398 return;
5400 p = buf;
5401 if (buf < (p += GetAnsiStatus(wp, p)))
5402 *p++ = ' ';
5403 sprintf(p, "(%d,%d)/(%d,%d)",
5404 wp->w_x + 1, wp->w_y + 1, wp->w_width, wp->w_height);
5405 #ifdef COPY_PASTE
5406 sprintf(p += strlen(p), "+%d", wp->w_histheight);
5407 #endif
5408 sprintf(p += strlen(p), " %c%sflow",
5409 (wp->w_flow & FLOW_NOW) ? '+' : '-',
5410 (wp->w_flow & FLOW_AUTOFLAG) ? "" :
5411 ((wp->w_flow & FLOW_AUTO) ? "(+)" : "(-)"));
5412 if (!wp->w_wrap) sprintf(p += strlen(p), " -wrap");
5413 if (wp->w_insert) sprintf(p += strlen(p), " ins");
5414 if (wp->w_origin) sprintf(p += strlen(p), " org");
5415 if (wp->w_keypad) sprintf(p += strlen(p), " app");
5416 if (wp->w_log) sprintf(p += strlen(p), " log");
5417 if (wp->w_monitor != MON_OFF
5418 #ifdef MULTIUSER
5419 && (ACLBYTE(wp->w_mon_notify, D_user->u_id) & ACLBIT(D_user->u_id))
5420 #endif
5422 sprintf(p += strlen(p), " mon");
5423 if (wp->w_mouse) sprintf(p += strlen(p), " mouse");
5424 #ifdef COLOR
5425 if (wp->w_bce) sprintf(p += strlen(p), " bce");
5426 #endif
5427 if (!wp->w_c1) sprintf(p += strlen(p), " -c1");
5428 if (wp->w_norefresh) sprintf(p += strlen(p), " nored");
5430 p += strlen(p);
5431 #ifdef FONT
5432 # ifdef ENCODINGS
5433 if (wp->w_encoding && (display == 0 || D_encoding != wp->w_encoding || EncodingDefFont(wp->w_encoding) <= 0))
5435 *p++ = ' ';
5436 strcpy(p, EncodingName(wp->w_encoding));
5437 p += strlen(p);
5439 # ifdef UTF8
5440 if (wp->w_encoding != UTF8)
5441 # endif
5442 # endif
5443 if (D_CC0 || (D_CS0 && *D_CS0))
5445 if (wp->w_gr == 2)
5447 sprintf(p, " G%c", wp->w_Charset + '0');
5448 if (wp->w_FontE >= ' ')
5449 p[3] = wp->w_FontE;
5450 else
5452 p[3] = '^';
5453 p[4] = wp->w_FontE ^ 0x40;
5454 p++;
5456 p[4] = '[';
5457 p++;
5459 else if (wp->w_gr)
5460 sprintf(p++, " G%c%c[", wp->w_Charset + '0', wp->w_CharsetR + '0');
5461 else
5462 sprintf(p, " G%c[", wp->w_Charset + '0');
5463 p += 4;
5464 for (i = 0; i < 4; i++)
5466 if (wp->w_charsets[i] == ASCII)
5467 *p++ = 'B';
5468 else if (wp->w_charsets[i] >= ' ')
5469 *p++ = wp->w_charsets[i];
5470 else
5472 *p++ = '^';
5473 *p++ = wp->w_charsets[i] ^ 0x40;
5476 *p++ = ']';
5477 *p = 0;
5479 #endif
5481 if (wp->w_type == W_TYPE_PLAIN)
5483 /* add info about modem control lines */
5484 *p++ = ' ';
5485 TtyGetModemStatus(wp->w_ptyfd, p);
5487 #ifdef BUILTIN_TELNET
5488 else if (wp->w_type == W_TYPE_TELNET)
5490 *p++ = ' ';
5491 TelStatus(wp, p, sizeof(buf) - 1 - (p - buf));
5493 #endif
5494 Msg(0, "%s %d(%s)", buf, wp->w_number, wp->w_title);
5497 static void
5498 ShowDInfo()
5500 char buf[512], *p;
5501 if (display == 0)
5502 return;
5503 p = buf;
5504 sprintf(p, "(%d,%d)", D_width, D_height),
5505 p += strlen(p);
5506 #ifdef ENCODINGS
5507 if (D_encoding)
5509 *p++ = ' ';
5510 strcpy(p, EncodingName(D_encoding));
5511 p += strlen(p);
5513 #endif
5514 if (D_CXT)
5516 strcpy(p, " xterm");
5517 p += strlen(p);
5519 #ifdef COLOR
5520 if (D_hascolor)
5522 strcpy(p, " color");
5523 p += strlen(p);
5525 #endif
5526 #ifdef FONT
5527 if (D_CG0)
5529 strcpy(p, " iso2022");
5530 p += strlen(p);
5532 else if (D_CS0 && *D_CS0)
5534 strcpy(p, " altchar");
5535 p += strlen(p);
5537 #endif
5538 Msg(0, "%s", buf);
5541 static void
5542 AKAfin(buf, len, data)
5543 char *buf;
5544 int len;
5545 char *data; /* dummy */
5547 ASSERT(display);
5548 if (len && fore)
5549 ChangeAKA(fore, buf, strlen(buf));
5552 static void
5553 InputAKA()
5555 char *s, *ss;
5556 int n;
5557 Input("Set window's title to: ", sizeof(fore->w_akabuf) - 1, INP_COOKED, AKAfin, NULL, 0);
5558 s = fore->w_title;
5559 if (!s)
5560 return;
5561 for (; *s; s++)
5563 if ((*(unsigned char *)s & 0x7f) < 0x20 || *s == 0x7f)
5564 continue;
5565 ss = s;
5566 n = 1;
5567 LayProcess(&ss, &n);
5571 static void
5572 Colonfin(buf, len, data)
5573 char *buf;
5574 int len;
5575 char *data; /* dummy */
5577 char mbuf[256];
5579 RemoveStatus();
5580 if (buf[len] == '\t')
5582 int m, x;
5583 int l = 0, r = RC_LAST;
5584 int showmessage = 0;
5585 char *s = buf;
5587 while (*s && s - buf < len)
5588 if (*s++ == ' ')
5589 return;
5591 /* Showing a message when there's no hardstatus or caption cancels the input */
5592 if (display &&
5593 (captionalways || D_has_hstatus == HSTATUS_LASTLINE || (D_canvas.c_slperp && D_canvas.c_slperp->c_slnext)))
5594 showmessage = 1;
5596 while (l <= r)
5598 m = (l + r) / 2;
5599 x = strncmp(buf, comms[m].name, len);
5600 if (x > 0)
5601 l = m + 1;
5602 else if (x < 0)
5603 r = m - 1;
5604 else
5606 s = mbuf;
5607 for (l = m - 1; l >= 0 && strncmp(buf, comms[l].name, len) == 0; l--)
5609 for (m = ++l; m <= r && strncmp(buf, comms[m].name, len) == 0 && s - mbuf < sizeof(mbuf); m++)
5610 s += snprintf(s, sizeof(mbuf) - (s - mbuf), " %s", comms[m].name);
5611 if (l < m - 1)
5613 if (showmessage)
5614 Msg(0, "Possible commands:%s", mbuf);
5616 else
5618 s = mbuf;
5619 len = snprintf(mbuf, sizeof(mbuf), "%s \t", comms[l].name + len);
5620 if (len > 0 && len < sizeof(mbuf))
5621 LayProcess(&s, &len);
5623 break;
5626 if (l > r && showmessage)
5627 Msg(0, "No commands matching '%*s'", len, buf);
5628 return;
5631 if (!len || buf[len])
5632 return;
5634 len = strlen(buf) + 1;
5635 if (len > (int)sizeof(mbuf))
5636 RcLine(buf, len);
5637 else
5639 bcopy(buf, mbuf, len);
5640 RcLine(mbuf, sizeof mbuf);
5644 static void
5645 SelectFin(buf, len, data)
5646 char *buf;
5647 int len;
5648 char *data; /* dummy */
5650 int n;
5652 if (!len || !display)
5653 return;
5654 if (len == 1 && *buf == '-')
5656 SetForeWindow((struct win *)0);
5657 Activate(0);
5658 return;
5660 if ((n = WindowByNoN(buf)) < 0)
5661 return;
5662 SwitchWindow(n);
5665 static void
5666 SelectLayoutFin(buf, len, data)
5667 char *buf;
5668 int len;
5669 char *data; /* dummy */
5671 struct layout *lay;
5673 if (!len || !display)
5674 return;
5675 if (len == 1 && *buf == '-')
5677 LoadLayout((struct layout *)0, (struct canvas *)0);
5678 Activate(0);
5679 return;
5681 lay = FindLayout(buf);
5682 if (!lay)
5683 Msg(0, "No such layout\n");
5684 else if (lay == D_layout)
5685 Msg(0, "This IS layout %d (%s).\n", lay->lay_number, lay->lay_title);
5686 else
5688 LoadLayout(lay, &D_canvas);
5689 Activate(0);
5694 static void
5695 InputSelect()
5697 Input("Switch to window: ", 20, INP_COOKED, SelectFin, NULL, 0);
5700 static char setenv_var[31];
5703 static void
5704 SetenvFin1(buf, len, data)
5705 char *buf;
5706 int len;
5707 char *data; /* dummy */
5709 if (!len || !display)
5710 return;
5711 InputSetenv(buf);
5714 static void
5715 SetenvFin2(buf, len, data)
5716 char *buf;
5717 int len;
5718 char *data; /* dummy */
5720 if (!len || !display)
5721 return;
5722 debug2("SetenvFin2: setenv '%s' '%s'\n", setenv_var, buf);
5723 xsetenv(setenv_var, buf);
5724 MakeNewEnv();
5727 static void
5728 InputSetenv(arg)
5729 char *arg;
5731 static char setenv_buf[50 + sizeof(setenv_var)]; /* need to be static here, cannot be freed */
5733 if (arg)
5735 strncpy(setenv_var, arg, sizeof(setenv_var) - 1);
5736 sprintf(setenv_buf, "Enter value for %s: ", setenv_var);
5737 Input(setenv_buf, 30, INP_COOKED, SetenvFin2, NULL, 0);
5739 else
5740 Input("Setenv: Enter variable name: ", 30, INP_COOKED, SetenvFin1, NULL, 0);
5744 * the following options are understood by this parser:
5745 * -f, -f0, -f1, -fy, -fa
5746 * -t title, -T terminal-type, -h height-of-scrollback,
5747 * -ln, -l0, -ly, -l1, -l
5748 * -a, -M, -L
5750 void
5751 DoScreen(fn, av)
5752 char *fn, **av;
5754 struct NewWindow nwin;
5755 register int num;
5756 char buf[20];
5758 nwin = nwin_undef;
5759 while (av && *av && av[0][0] == '-')
5761 if (av[0][1] == '-')
5763 av++;
5764 break;
5766 switch (av[0][1])
5768 case 'f':
5769 switch (av[0][2])
5771 case 'n':
5772 case '0':
5773 nwin.flowflag = FLOW_NOW * 0;
5774 break;
5775 case 'y':
5776 case '1':
5777 case '\0':
5778 nwin.flowflag = FLOW_NOW * 1;
5779 break;
5780 case 'a':
5781 nwin.flowflag = FLOW_AUTOFLAG;
5782 break;
5783 default:
5784 break;
5786 break;
5787 case 't': /* no more -k */
5788 if (av[0][2])
5789 nwin.aka = &av[0][2];
5790 else if (*++av)
5791 nwin.aka = *av;
5792 else
5793 --av;
5794 break;
5795 case 'T':
5796 if (av[0][2])
5797 nwin.term = &av[0][2];
5798 else if (*++av)
5799 nwin.term = *av;
5800 else
5801 --av;
5802 break;
5803 case 'h':
5804 if (av[0][2])
5805 nwin.histheight = atoi(av[0] + 2);
5806 else if (*++av)
5807 nwin.histheight = atoi(*av);
5808 else
5809 --av;
5810 break;
5811 #ifdef LOGOUTOK
5812 case 'l':
5813 switch (av[0][2])
5815 case 'n':
5816 case '0':
5817 nwin.lflag = 0;
5818 break;
5819 case 'y':
5820 case '1':
5821 case '\0':
5822 nwin.lflag = 1;
5823 break;
5824 case 'a':
5825 nwin.lflag = 3;
5826 break;
5827 default:
5828 break;
5830 break;
5831 #endif
5832 case 'a':
5833 nwin.aflag = 1;
5834 break;
5835 case 'M':
5836 nwin.monitor = MON_ON;
5837 break;
5838 case 'L':
5839 nwin.Lflag = 1;
5840 break;
5841 default:
5842 Msg(0, "%s: screen: invalid option -%c.", fn, av[0][1]);
5843 break;
5845 ++av;
5847 num = 0;
5848 if (av && *av && IsNumColon(*av, 10, buf, sizeof(buf)))
5850 if (*buf != '\0')
5851 nwin.aka = buf;
5852 num = atoi(*av);
5853 if (num < 0 || num > MAXWIN - 1)
5855 Msg(0, "%s: illegal screen number %d.", fn, num);
5856 num = 0;
5858 nwin.StartAt = num;
5859 ++av;
5861 if (av && *av)
5863 nwin.args = av;
5864 if (!nwin.aka)
5865 nwin.aka = Filename(*av);
5867 MakeWindow(&nwin);
5870 #ifdef COPY_PASTE
5872 * CompileKeys must be called before Markroutine is first used.
5873 * to initialise the keys with defaults, call CompileKeys(NULL, mark_key_tab);
5875 * s is an ascii string in a termcap-like syntax. It looks like
5876 * "j=u:k=d:l=r:h=l: =.:" and so on...
5877 * this example rebinds the cursormovement to the keys u (up), d (down),
5878 * l (left), r (right). placing a mark will now be done with ".".
5881 CompileKeys(s, sl, array)
5882 char *s;
5883 int sl;
5884 unsigned char *array;
5886 int i;
5887 unsigned char key, value;
5889 if (sl == 0)
5891 for (i = 0; i < 256; i++)
5892 array[i] = i;
5893 return 0;
5895 debug1("CompileKeys: '%s'\n", s);
5896 while (sl)
5898 key = *(unsigned char *)s++;
5899 if (*s != '=' || sl < 3)
5900 return -1;
5901 sl--;
5904 s++;
5905 sl -= 2;
5906 value = *(unsigned char *)s++;
5907 array[value] = key;
5909 while (*s == '=' && sl >= 2);
5910 if (sl == 0)
5911 break;
5912 if (*s++ != ':')
5913 return -1;
5914 sl--;
5916 return 0;
5918 #endif /* COPY_PASTE */
5921 * Asynchronous input functions
5924 #if defined(DETACH) && defined(POW_DETACH)
5925 static void
5926 pow_detach_fn(buf, len, data)
5927 char *buf;
5928 int len;
5929 char *data; /* dummy */
5931 debug("pow_detach_fn called\n");
5932 if (len)
5934 *buf = 0;
5935 return;
5937 if (ktab[(int)(unsigned char)*buf].nr != RC_POW_DETACH)
5939 if (display)
5940 write(D_userfd, "\007", 1);
5941 Msg(0, "Detach aborted.");
5943 else
5944 Detach(D_POWER);
5946 #endif /* POW_DETACH */
5948 #ifdef COPY_PASTE
5949 static void
5950 copy_reg_fn(buf, len, data)
5951 char *buf;
5952 int len;
5953 char *data; /* dummy */
5955 struct plop *pp = plop_tab + (int)(unsigned char)*buf;
5957 if (len)
5959 *buf = 0;
5960 return;
5962 if (pp->buf)
5963 free(pp->buf);
5964 pp->buf = 0;
5965 pp->len = 0;
5966 if (D_user->u_plop.len)
5968 if ((pp->buf = (char *)malloc(D_user->u_plop.len)) == NULL)
5970 Msg(0, strnomem);
5971 return;
5973 bcopy(D_user->u_plop.buf, pp->buf, D_user->u_plop.len);
5975 pp->len = D_user->u_plop.len;
5976 #ifdef ENCODINGS
5977 pp->enc = D_user->u_plop.enc;
5978 #endif
5979 Msg(0, "Copied %d characters into register %c", D_user->u_plop.len, *buf);
5982 static void
5983 ins_reg_fn(buf, len, data)
5984 char *buf;
5985 int len;
5986 char *data; /* dummy */
5988 struct plop *pp = plop_tab + (int)(unsigned char)*buf;
5990 if (len)
5992 *buf = 0;
5993 return;
5995 if (!fore)
5996 return; /* Input() should not call us w/o fore, but you never know... */
5997 if (*buf == '.')
5998 Msg(0, "ins_reg_fn: Warning: pasting real register '.'!");
5999 if (pp->buf)
6001 MakePaster(&fore->w_paster, pp->buf, pp->len, 0);
6002 return;
6004 Msg(0, "Empty register.");
6006 #endif /* COPY_PASTE */
6008 static void
6009 process_fn(buf, len, data)
6010 char *buf;
6011 int len;
6012 char *data; /* dummy */
6014 struct plop *pp = plop_tab + (int)(unsigned char)*buf;
6016 if (len)
6018 *buf = 0;
6019 return;
6021 if (pp->buf)
6023 ProcessInput(pp->buf, pp->len);
6024 return;
6026 Msg(0, "Empty register.");
6029 static void
6030 confirm_fn(buf, len, data)
6031 char *buf;
6032 int len;
6033 char *data;
6035 struct action act;
6037 if (len || (*buf != 'y' && *buf != 'Y'))
6039 *buf = 0;
6040 return;
6042 act.nr = *(int *)data;
6043 act.args = noargs;
6044 act.argl = 0;
6045 DoAction(&act, -1);
6048 #ifdef MULTIUSER
6049 struct inputsu
6051 struct acluser **up;
6052 char name[24];
6053 char pw1[130]; /* FreeBSD crypts to 128 bytes */
6054 char pw2[130];
6057 static void
6058 su_fin(buf, len, data)
6059 char *buf;
6060 int len;
6061 char *data;
6063 struct inputsu *i = (struct inputsu *)data;
6064 char *p;
6065 int l;
6067 if (!*i->name)
6068 { p = i->name; l = sizeof(i->name) - 1; }
6069 else if (!*i->pw1)
6070 { strcpy(p = i->pw1, "\377"); l = sizeof(i->pw1) - 1; }
6071 else
6072 { strcpy(p = i->pw2, "\377"); l = sizeof(i->pw2) - 1; }
6073 if (buf && len)
6074 strncpy(p, buf, 1 + (l < len) ? l : len);
6075 if (!*i->name)
6076 Input("Screen User: ", sizeof(i->name) - 1, INP_COOKED, su_fin, (char *)i, 0);
6077 else if (!*i->pw1)
6078 Input("User's UNIX Password: ", sizeof(i->pw1)-1, INP_COOKED|INP_NOECHO, su_fin, (char *)i, 0);
6079 else if (!*i->pw2)
6080 Input("User's Screen Password: ", sizeof(i->pw2)-1, INP_COOKED|INP_NOECHO, su_fin, (char *)i, 0);
6081 else
6083 if ((p = DoSu(i->up, i->name, i->pw2, i->pw1)))
6084 Msg(0, "%s", p);
6085 free((char *)i);
6089 static int
6090 InputSu(w, up, name)
6091 struct win *w;
6092 struct acluser **up;
6093 char *name;
6095 struct inputsu *i;
6097 if (!(i = (struct inputsu *)calloc(1, sizeof(struct inputsu))))
6098 return -1;
6100 i->up = up;
6101 if (name && *name)
6102 su_fin(name, (int)strlen(name), (char *)i); /* can also initialise stuff */
6103 else
6104 su_fin((char *)0, 0, (char *)i);
6105 return 0;
6107 #endif /* MULTIUSER */
6109 #ifdef PASSWORD
6111 static void
6112 pass1(buf, len, data)
6113 char *buf;
6114 int len;
6115 char *data;
6117 struct acluser *u = (struct acluser *)data;
6119 if (!*buf)
6120 return;
6121 ASSERT(u);
6122 if (u->u_password != NullStr)
6123 free((char *)u->u_password);
6124 u->u_password = SaveStr(buf);
6125 bzero(buf, strlen(buf));
6126 Input("Retype new password:", 100, INP_NOECHO, pass2, data, 0);
6129 static void
6130 pass2(buf, len, data)
6131 char *buf;
6132 int len;
6133 char *data;
6135 int st;
6136 char salt[3];
6137 struct acluser *u = (struct acluser *)data;
6139 ASSERT(u);
6140 if (!buf || strcmp(u->u_password, buf))
6142 Msg(0, "[ Passwords don't match - checking turned off ]");
6143 if (u->u_password != NullStr)
6145 bzero(u->u_password, strlen(u->u_password));
6146 free((char *)u->u_password);
6148 u->u_password = NullStr;
6150 else if (u->u_password[0] == '\0')
6152 Msg(0, "[ No password - no secure ]");
6153 if (buf)
6154 bzero(buf, strlen(buf));
6157 if (u->u_password != NullStr)
6159 for (st = 0; st < 2; st++)
6160 salt[st] = 'A' + (int)((time(0) >> 6 * st) % 26);
6161 salt[2] = 0;
6162 buf = crypt(u->u_password, salt);
6163 bzero(u->u_password, strlen(u->u_password));
6164 free((char *)u->u_password);
6165 u->u_password = SaveStr(buf);
6166 bzero(buf, strlen(buf));
6167 #ifdef COPY_PASTE
6168 if (u->u_plop.buf)
6169 UserFreeCopyBuffer(u);
6170 u->u_plop.len = strlen(u->u_password);
6171 # ifdef ENCODINGS
6172 u->u_plop.enc = 0;
6173 #endif
6174 if (!(u->u_plop.buf = SaveStr(u->u_password)))
6176 Msg(0, strnomem);
6177 D_user->u_plop.len = 0;
6179 else
6180 Msg(0, "[ Password moved into copybuffer ]");
6181 #else /* COPY_PASTE */
6182 Msg(0, "[ Crypted password is \"%s\" ]", u->u_password);
6183 #endif /* COPY_PASTE */
6186 #endif /* PASSWORD */
6188 static void
6189 digraph_fn(buf, len, data)
6190 char *buf;
6191 int len;
6192 char *data; /* dummy */
6194 int ch, i, x;
6196 ch = buf[len];
6197 if (ch)
6199 buf[len + 1] = ch; /* so we can restore it later */
6200 if (ch < ' ' || ch == '\177')
6201 return;
6202 if (len >= 1 && ((*buf == 'U' && buf[1] == '+') || (*buf == '0' && (buf[1] == 'x' || buf[1] == 'X'))))
6204 if (len == 1)
6205 return;
6206 if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f') && (ch < 'A' || ch > 'F'))
6208 buf[len] = '\034'; /* ^] is ignored by Input() */
6209 return;
6211 if (len == (*buf == 'U' ? 5 : 3))
6212 buf[len] = '\n';
6213 return;
6215 if (len && *buf == '0')
6217 if (ch < '0' || ch > '7')
6219 buf[len] = '\034'; /* ^] is ignored by Input() */
6220 return;
6222 if (len == 3)
6223 buf[len] = '\n';
6224 return;
6226 if (len == 1)
6227 buf[len] = '\n';
6228 return;
6230 if (len < 1)
6231 return;
6232 if (buf[len + 1])
6234 buf[len] = buf[len + 1]; /* stored above */
6235 len++;
6237 if (len < 2)
6238 return;
6239 if (len >= 1 && ((*buf == 'U' && buf[1] == '+') || (*buf == '0' && (buf[1] == 'x' || buf[1] == 'X'))))
6241 x = 0;
6242 for (i = 2; i < len; i++)
6244 if (buf[i] >= '0' && buf[i] <= '9')
6245 x = x * 16 | (buf[i] - '0');
6246 else if (buf[i] >= 'a' && buf[i] <= 'f')
6247 x = x * 16 | (buf[i] - ('a' - 10));
6248 else if (buf[i] >= 'A' && buf[i] <= 'F')
6249 x = x * 16 | (buf[i] - ('A' - 10));
6250 else
6251 break;
6254 else if (buf[0] == '0')
6256 x = 0;
6257 for (i = 1; i < len; i++)
6259 if (buf[i] < '0' || buf[i] > '7')
6260 break;
6261 x = x * 8 | (buf[i] - '0');
6264 else
6266 for (i = 0; i < (int)(sizeof(digraphs)/sizeof(*digraphs)); i++)
6267 if ((digraphs[i][0] == (unsigned char)buf[0] && digraphs[i][1] == (unsigned char)buf[1]) ||
6268 (digraphs[i][0] == (unsigned char)buf[1] && digraphs[i][1] == (unsigned char)buf[0]))
6269 break;
6270 if (i == (int)(sizeof(digraphs)/sizeof(*digraphs)))
6272 Msg(0, "Unknown digraph");
6273 return;
6275 x = digraphs[i][2];
6277 i = 1;
6278 *buf = x;
6279 #ifdef UTF8
6280 if (flayer->l_encoding == UTF8)
6281 i = ToUtf8(buf, x); /* buf is big enough for all UTF-8 codes */
6282 #endif
6283 while(i)
6284 LayProcess(&buf, &i);
6287 #ifdef MAPKEYS
6289 StuffKey(i)
6290 int i;
6292 struct action *act;
6293 int discard = 0;
6295 debug1("StuffKey #%d", i);
6296 #ifdef DEBUG
6297 if (i < KMAP_KEYS)
6298 debug1(" - %s", term[i + T_CAPS].tcname);
6299 #endif
6301 if (i < KMAP_KEYS && D_ESCseen)
6303 struct action *act = &D_ESCseen[i + 256];
6304 if (act->nr != RC_ILLEGAL)
6306 D_ESCseen = 0;
6307 DoAction(act, i + 256);
6308 return 0;
6310 discard = 1;
6313 if (i >= T_CURSOR - T_CAPS && i < T_KEYPAD - T_CAPS && D_cursorkeys)
6314 i += T_OCAPS - T_CURSOR;
6315 else if (i >= T_KEYPAD - T_CAPS && i < T_OCAPS - T_CAPS && D_keypad)
6316 i += T_OCAPS - T_CURSOR;
6317 debug1(" - action %d\n", i);
6318 flayer = D_forecv->c_layer;
6319 fore = D_fore;
6320 act = 0;
6321 #ifdef COPY_PASTE
6322 if (InMark() || InInput() || InWList())
6323 act = i < KMAP_KEYS+KMAP_AKEYS ? &mmtab[i] : &kmap_exts[i - (KMAP_KEYS+KMAP_AKEYS)].mm;
6324 #endif
6325 if ((!act || act->nr == RC_ILLEGAL) && !D_mapdefault)
6326 act = i < KMAP_KEYS+KMAP_AKEYS ? &umtab[i] : &kmap_exts[i - (KMAP_KEYS+KMAP_AKEYS)].um;
6327 if (!act || act->nr == RC_ILLEGAL)
6328 act = i < KMAP_KEYS+KMAP_AKEYS ? &dmtab[i] : &kmap_exts[i - (KMAP_KEYS+KMAP_AKEYS)].dm;
6330 if (discard && (!act || act->nr != RC_COMMAND))
6332 D_ESCseen = 0;
6333 return 0;
6335 D_mapdefault = 0;
6337 if (act == 0 || act->nr == RC_ILLEGAL)
6338 return -1;
6339 DoAction(act, 0);
6340 return 0;
6342 #endif
6345 static int
6346 IsOnDisplay(wi)
6347 struct win *wi;
6349 struct canvas *cv;
6350 ASSERT(display);
6351 for (cv = D_cvlist; cv; cv = cv->c_next)
6352 if (Layer2Window(cv->c_layer) == wi)
6353 return 1;
6354 return 0;
6357 struct win *
6358 FindNiceWindow(wi, presel)
6359 struct win *wi;
6360 char *presel;
6362 int i;
6364 debug2("FindNiceWindow %d %s\n", wi ? wi->w_number : -1 , presel ? presel : "NULL");
6365 if (presel)
6367 i = WindowByNoN(presel);
6368 if (i >= 0)
6369 wi = wtab[i];
6371 if (!display)
6372 return wi;
6373 #ifdef MULTIUSER
6374 if (wi && AclCheckPermWin(D_user, ACL_READ, wi))
6375 wi = 0;
6376 #endif
6377 if (!wi || (IsOnDisplay(wi) && !presel))
6379 /* try to get another window */
6380 wi = 0;
6381 #ifdef MULTIUSER
6382 for (wi = windows; wi; wi = wi->w_next)
6383 if (!wi->w_layer.l_cvlist && !AclCheckPermWin(D_user, ACL_WRITE, wi))
6384 break;
6385 if (!wi)
6386 for (wi = windows; wi; wi = wi->w_next)
6387 if (wi->w_layer.l_cvlist && !IsOnDisplay(wi) && !AclCheckPermWin(D_user, ACL_WRITE, wi))
6388 break;
6389 if (!wi)
6390 for (wi = windows; wi; wi = wi->w_next)
6391 if (!wi->w_layer.l_cvlist && !AclCheckPermWin(D_user, ACL_READ, wi))
6392 break;
6393 if (!wi)
6394 for (wi = windows; wi; wi = wi->w_next)
6395 if (wi->w_layer.l_cvlist && !IsOnDisplay(wi) && !AclCheckPermWin(D_user, ACL_READ, wi))
6396 break;
6397 #endif
6398 if (!wi)
6399 for (wi = windows; wi; wi = wi->w_next)
6400 if (!wi->w_layer.l_cvlist)
6401 break;
6402 if (!wi)
6403 for (wi = windows; wi; wi = wi->w_next)
6404 if (wi->w_layer.l_cvlist && !IsOnDisplay(wi))
6405 break;
6407 #ifdef MULTIUSER
6408 if (wi && AclCheckPermWin(D_user, ACL_READ, wi))
6409 wi = 0;
6410 #endif
6411 return wi;
6414 #if 0
6416 /* sorted list of all commands */
6417 static struct comm **commtab;
6418 static int ncommtab;
6420 void
6421 AddComms(cos, hand)
6422 struct comm *cos;
6423 void (*hand) __P((struct comm *, char **, int));
6425 int n, i, j, r;
6426 for (n = 0; cos[n].name; n++)
6428 if (n == 0)
6429 return;
6430 if (commtab)
6431 commtab = (struct commt *)realloc(commtab, sizeof(*commtab) * (ncommtab + n));
6432 else
6433 commtab = (struct commt *)malloc(sizeof(*commtab) * (ncommtab + n));
6434 if (!commtab)
6435 Panic(0, strnomem);
6436 for (i = 0; i < n; i++)
6438 for (j = 0; j < ncommtab; j++)
6440 r = strcmp(cos[i].name, commtab[j]->name);
6441 if (r == 0)
6442 Panic(0, "Duplicate command: %s\n", cos[i].name);
6443 if (r < 0)
6444 break;
6446 for (r = ncommtab; r > j; r--)
6447 commtab[r] = commtab[r - 1];
6448 commtab[j] = cos + i;
6449 cos[i].handler = hand;
6450 bzero(cos[i].userbits, sizeof(cos[i].userbits));
6451 ncommtab++;
6455 struct comm *
6456 FindComm(str)
6457 char *str;
6459 int x, m, l = 0, r = ncommtab - 1;
6460 while (l <= r)
6462 m = (l + r) / 2;
6463 x = strcmp(str, commtab[m]->name);
6464 if (x > 0)
6465 l = m + 1;
6466 else if (x < 0)
6467 r = m - 1;
6468 else
6469 return commtab[m];
6471 return 0;
6474 #endif
6476 static int
6477 CalcSlicePercent(cv, percent)
6478 struct canvas *cv;
6479 int percent;
6481 int w, wsum, up;
6482 if (!cv || !cv->c_slback)
6483 return percent;
6484 up = CalcSlicePercent(cv->c_slback->c_slback, percent);
6485 w = cv->c_slweight;
6486 for (cv = cv->c_slback->c_slperp, wsum = 0; cv; cv = cv->c_slnext)
6487 wsum += cv->c_slweight;
6488 if (wsum == 0)
6489 return 0;
6490 return (up * w) / wsum;
6493 static int
6494 ChangeCanvasSize(fcv, abs, diff, gflag, percent)
6495 struct canvas *fcv; /* make this canvas bigger */
6496 int abs; /* mode: 0:rel 1:abs 2:max */
6497 int diff; /* change this much */
6498 int gflag; /* go up if neccessary */
6499 int percent;
6501 struct canvas *cv;
6502 int done, have, m, dir;
6504 debug3("ChangeCanvasSize abs %d diff %d percent=%d\n", abs, diff, percent);
6505 if (abs == 0 && diff == 0)
6506 return 0;
6507 if (abs == 2)
6509 if (diff == 0)
6510 fcv->c_slweight = 0;
6511 else
6513 for (cv = fcv->c_slback->c_slperp; cv; cv = cv->c_slnext)
6514 cv->c_slweight = 0;
6515 fcv->c_slweight = 1;
6516 cv = fcv->c_slback->c_slback;
6517 if (gflag && cv && cv->c_slback)
6518 ChangeCanvasSize(cv, abs, diff, gflag, percent);
6520 return diff;
6522 if (abs)
6524 if (diff < 0)
6525 diff = 0;
6526 if (percent && diff > percent)
6527 diff = percent;
6529 if (percent)
6531 int wsum, up;
6532 for (cv = fcv->c_slback->c_slperp, wsum = 0; cv; cv = cv->c_slnext)
6533 wsum += cv->c_slweight;
6534 if (wsum)
6536 up = gflag ? CalcSlicePercent(fcv->c_slback->c_slback, percent) : percent;
6537 debug3("up=%d, wsum=%d percent=%d\n", up, wsum, percent);
6538 if (wsum < 1000)
6540 int scale = wsum < 10 ? 1000 : 100;
6541 for (cv = fcv->c_slback->c_slperp; cv; cv = cv->c_slnext)
6542 cv->c_slweight *= scale;
6543 wsum *= scale;
6544 debug1("scaled wsum to %d\n", wsum);
6546 for (cv = fcv->c_slback->c_slperp; cv; cv = cv->c_slnext)
6548 if (cv->c_slweight)
6550 cv->c_slweight = (cv->c_slweight * up) / percent;
6551 if (cv->c_slweight == 0)
6552 cv->c_slweight = 1;
6554 debug1(" - weight %d\n", cv->c_slweight);
6556 diff = (diff * wsum) / percent;
6557 percent = wsum;
6560 else
6562 if (abs && diff == (fcv->c_slorient == SLICE_VERT ? fcv->c_ye - fcv->c_ys + 2 : fcv->c_xe - fcv->c_xs + 2))
6563 return 0;
6564 /* fix weights to real size (can't be helped, sorry) */
6565 for (cv = fcv->c_slback->c_slperp; cv; cv = cv->c_slnext)
6567 cv->c_slweight = cv->c_slorient == SLICE_VERT ? cv->c_ye - cv->c_ys + 2 : cv->c_xe - cv->c_xs + 2;
6568 debug1(" - weight %d\n", cv->c_slweight);
6571 if (abs)
6572 diff = diff - fcv->c_slweight;
6573 debug1("diff = %d\n", diff);
6574 if (diff == 0)
6575 return 0;
6576 if (diff < 0)
6578 cv = fcv->c_slnext ? fcv->c_slnext : fcv->c_slprev;
6579 fcv->c_slweight += diff;
6580 cv->c_slweight -= diff;
6581 return diff;
6583 done = 0;
6584 dir = 1;
6585 for (cv = fcv->c_slnext; diff > 0; cv = dir > 0 ? cv->c_slnext : cv->c_slprev)
6587 if (!cv)
6589 debug1("reached end, dir is %d\n", dir);
6590 if (dir == -1)
6591 break;
6592 dir = -1;
6593 cv = fcv;
6594 continue;
6596 if (percent)
6597 m = 1;
6598 else
6599 m = cv->c_slperp ? CountCanvasPerp(cv) * 2 : 2;
6600 debug2("min is %d, have %d\n", m, cv->c_slweight);
6601 if (cv->c_slweight > m)
6603 have = cv->c_slweight - m;
6604 if (have > diff)
6605 have = diff;
6606 debug1("subtract %d\n", have);
6607 cv->c_slweight -= have;
6608 done += have;
6609 diff -= have;
6612 if (diff && gflag)
6614 /* need more room! */
6615 cv = fcv->c_slback->c_slback;
6616 if (cv && cv->c_slback)
6617 done += ChangeCanvasSize(fcv->c_slback->c_slback, 0, diff, gflag, percent);
6619 fcv->c_slweight += done;
6620 debug1("ChangeCanvasSize returns %d\n", done);
6621 return done;
6624 static void
6625 ResizeRegions(arg, flags)
6626 char *arg;
6627 int flags;
6629 struct canvas *cv;
6630 int diff, l;
6631 int gflag = 0, abs = 0, percent = 0;
6632 int orient = 0;
6634 ASSERT(display);
6635 if (!*arg)
6636 return;
6637 if (D_forecv->c_slorient == SLICE_UNKN)
6639 Msg(0, "resize: need more than one region");
6640 return;
6642 gflag = flags & RESIZE_FLAG_L ? 0 : 1;
6643 orient |= flags & RESIZE_FLAG_H ? SLICE_HORI : 0;
6644 orient |= flags & RESIZE_FLAG_V ? SLICE_VERT : 0;
6645 if (orient == 0)
6646 orient = D_forecv->c_slorient;
6647 l = strlen(arg);
6648 if (*arg == '=')
6650 /* make all regions the same height */
6651 struct canvas *cv = gflag ? &D_canvas : D_forecv->c_slback;
6652 if (cv->c_slperp->c_slorient & orient)
6653 EqualizeCanvas(cv->c_slperp, gflag);
6654 /* can't use cv->c_slorient directly as it can be D_canvas */
6655 if ((cv->c_slperp->c_slorient ^ (SLICE_HORI ^ SLICE_VERT)) & orient)
6657 if (cv->c_slback)
6659 cv = cv->c_slback;
6660 EqualizeCanvas(cv->c_slperp, gflag);
6662 else
6663 EqualizeCanvas(cv, gflag);
6665 ResizeCanvas(cv);
6666 RecreateCanvasChain();
6667 RethinkDisplayViewports();
6668 ResizeLayersToCanvases();
6669 return;
6671 if (!strcmp(arg, "min") || !strcmp(arg, "0"))
6673 abs = 2;
6674 diff = 0;
6676 else if (!strcmp(arg, "max") || !strcmp(arg, "_"))
6678 abs = 2;
6679 diff = 1;
6681 else
6683 if (l > 0 && arg[l - 1] == '%')
6684 percent = 1000;
6685 if (*arg == '+')
6686 diff = atoi(arg + 1);
6687 else if (*arg == '-')
6688 diff = -atoi(arg + 1);
6689 else
6691 diff = atoi(arg); /* +1 because of caption line */
6692 if (diff < 0)
6693 diff = 0;
6694 abs = diff == 0 ? 2 : 1;
6697 if (!abs && !diff)
6698 return;
6699 if (percent)
6700 diff = diff * percent / 100;
6701 cv = D_forecv;
6702 if (cv->c_slorient & orient)
6703 ChangeCanvasSize(cv, abs, diff, gflag, percent);
6704 if (cv->c_slback->c_slorient & orient)
6705 ChangeCanvasSize(cv->c_slback, abs, diff, gflag, percent);
6707 ResizeCanvas(&D_canvas);
6708 RecreateCanvasChain();
6709 RethinkDisplayViewports();
6710 ResizeLayersToCanvases();
6711 return;
6713 #if 0
6715 if (siz + diff < 1)
6716 diff = 1 - siz;
6717 if (siz + diff > dsize - (nreg - 1) * 2 - 1)
6718 diff = dsize - (nreg - 1) * 2 - 1 - siz;
6719 if (diff == 0 || siz + diff < 1)
6720 return;
6722 if (diff < 0)
6724 if (D_forecv->c_next)
6726 D_forecv->c_ye += diff;
6727 D_forecv->c_next->c_ys += diff;
6728 D_forecv->c_next->c_yoff += diff;
6730 else
6732 for (cv = D_cvlist; cv; cv = cv->c_next)
6733 if (cv->c_next == D_forecv)
6734 break;
6735 ASSERT(cv);
6736 cv->c_ye -= diff;
6737 D_forecv->c_ys -= diff;
6738 D_forecv->c_yoff -= diff;
6741 else
6743 int s, i = 0, found = 0, di = diff, d2;
6744 s = dsize - (nreg - 1) * 2 - 1 - siz;
6745 for (cv = D_cvlist; cv; i = cv->c_ye + 2, cv = cv->c_next)
6747 if (cv == D_forecv)
6749 cv->c_ye = i + (cv->c_ye - cv->c_ys) + diff;
6750 cv->c_yoff -= cv->c_ys - i;
6751 cv->c_ys = i;
6752 found = 1;
6753 continue;
6755 s -= cv->c_ye - cv->c_ys;
6756 if (!found)
6758 if (s >= di)
6759 continue;
6760 d2 = di - s;
6762 else
6763 d2 = di > cv->c_ye - cv->c_ys ? cv->c_ye - cv->c_ys : di;
6764 di -= d2;
6765 cv->c_ye = i + (cv->c_ye - cv->c_ys) - d2;
6766 cv->c_yoff -= cv->c_ys - i;
6767 cv->c_ys = i;
6770 RethinkDisplayViewports();
6771 ResizeLayersToCanvases();
6772 #endif
6775 static void
6776 ResizeFin(buf, len, data)
6777 char *buf;
6778 int len;
6779 char *data;
6781 int ch;
6782 int flags = *(int *)data;
6783 ch = ((unsigned char *)buf)[len];
6784 if (ch == 0)
6786 ResizeRegions(buf, flags);
6787 return;
6789 if (ch == 'h')
6790 flags ^= RESIZE_FLAG_H;
6791 else if (ch == 'v')
6792 flags ^= RESIZE_FLAG_V;
6793 else if (ch == 'b')
6794 flags |= RESIZE_FLAG_H|RESIZE_FLAG_V;
6795 else if (ch == 'p')
6796 flags ^= D_forecv->c_slorient == SLICE_VERT ? RESIZE_FLAG_H : RESIZE_FLAG_V;
6797 else if (ch == 'l')
6798 flags ^= RESIZE_FLAG_L;
6799 else
6800 return;
6801 inp_setprompt(resizeprompts[flags], NULL);
6802 *(int *)data = flags;
6803 buf[len] = '\034';
6806 #ifdef RXVT_OSC
6807 void
6808 RefreshXtermOSC()
6810 int i;
6811 struct win *p;
6813 p = Layer2Window(D_forecv->c_layer);
6814 for (i = 3; i >=0; i--)
6815 SetXtermOSC(i, p ? p->w_xtermosc[i] : 0);
6817 #endif
6820 ParseAttrColor(s1, s2, msgok)
6821 char *s1, *s2;
6822 int msgok;
6824 int i, n;
6825 char *s, *ss;
6826 int r = 0;
6828 s = s1;
6829 while (*s == ' ')
6830 s++;
6831 ss = s;
6832 while (*ss && *ss != ' ')
6833 ss++;
6834 while (*ss == ' ')
6835 ss++;
6836 if (*s && (s2 || *ss || !((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') || *s == '.')))
6838 int mode = 0, n = 0;
6839 if (*s == '+')
6841 mode = 1;
6842 s++;
6844 else if (*s == '-')
6846 mode = -1;
6847 s++;
6849 else if (*s == '!')
6851 mode = 2;
6852 s++;
6854 else if (*s == '=')
6855 s++;
6856 if (*s >= '0' && *s <= '9')
6858 n = *s++ - '0';
6859 if (*s >= '0' && *s <= '9')
6860 n = n * 16 + (*s++ - '0');
6861 else if (*s >= 'a' && *s <= 'f')
6862 n = n * 16 + (*s++ - ('a' - 10));
6863 else if (*s >= 'A' && *s <= 'F')
6864 n = n * 16 + (*s++ - ('A' - 10));
6865 else if (*s && *s != ' ')
6867 if (msgok)
6868 Msg(0, "Illegal attribute hexchar '%c'", *s);
6869 return -1;
6872 else
6874 while (*s && *s != ' ')
6876 if (*s == 'd')
6877 n |= A_DI;
6878 else if (*s == 'u')
6879 n |= A_US;
6880 else if (*s == 'b')
6881 n |= A_BD;
6882 else if (*s == 'r')
6883 n |= A_RV;
6884 else if (*s == 's')
6885 n |= A_SO;
6886 else if (*s == 'B')
6887 n |= A_BL;
6888 else
6890 if (msgok)
6891 Msg(0, "Illegal attribute specifier '%c'", *s);
6892 return -1;
6894 s++;
6897 if (*s && *s != ' ')
6899 if (msgok)
6900 Msg(0, "junk after attribute description: '%c'", *s);
6901 return -1;
6903 if (mode == -1)
6904 r = n << 8 | n;
6905 else if (mode == 1)
6906 r = n << 8;
6907 else if (mode == 2)
6908 r = n;
6909 else if (mode == 0)
6910 r = 0xffff ^ n;
6912 while (*s && *s == ' ')
6913 s++;
6915 if (s2)
6917 if (*s)
6919 if (msgok)
6920 Msg(0, "junk after description: '%c'", *s);
6921 return -1;
6923 s = s2;
6924 while (*s && *s == ' ')
6925 s++;
6928 #ifdef COLOR
6929 if (*s)
6931 static char costr[] = "krgybmcw d i.01234567 9 f FKRGYBMCW I ";
6932 int numco = 0, j;
6934 n = 0;
6935 if (*s == '.')
6937 numco++;
6938 n = 0x0f;
6939 s++;
6941 for (j = 0; j < 2 && *s && *s != ' '; j++)
6943 for (i = 0; costr[i]; i++)
6944 if (*s == costr[i])
6945 break;
6946 if (!costr[i])
6948 if (msgok)
6949 Msg(0, "illegal color descriptor: '%c'", *s);
6950 return -1;
6952 numco++;
6953 n = n << 4 | (i & 15);
6954 #ifdef COLORS16
6955 if (i >= 48)
6956 n = (n & 0x20ff) | 0x200;
6957 #endif
6958 s++;
6960 if ((n & 0xf00) == 0xf00)
6961 n ^= 0xf00; /* clear superflous bits */
6962 #ifdef COLORS16
6963 if (n & 0x2000)
6964 n ^= 0x2400; /* shift bit into right position */
6965 #endif
6966 if (numco == 1)
6967 n |= 0xf0; /* don't change bg color */
6968 if (numco != 2 && n != 0xff)
6969 n |= 0x100; /* special invert mode */
6970 if (*s && *s != ' ')
6972 if (msgok)
6973 Msg(0, "junk after color description: '%c'", *s);
6974 return -1;
6976 n ^= 0xff;
6977 r |= n << 16;
6979 #endif
6981 while (*s && *s == ' ')
6982 s++;
6983 if (*s)
6985 if (msgok)
6986 Msg(0, "junk after description: '%c'", *s);
6987 return -1;
6989 debug1("ParseAttrColor %06x\n", r);
6990 return r;
6994 * Color coding:
6995 * 0-7 normal colors
6996 * 9 default color
6997 * e just set intensity
6998 * f don't change anything
6999 * Intensity is encoded into bits 17(fg) and 18(bg).
7001 void
7002 ApplyAttrColor(i, mc)
7003 int i;
7004 struct mchar *mc;
7006 debug1("ApplyAttrColor %06x\n", i);
7007 mc->attr |= i >> 8 & 255;
7008 mc->attr ^= i & 255;
7009 #ifdef COLOR
7010 i = (i >> 16) ^ 0xff;
7011 if ((i & 0x100) != 0)
7013 i &= 0xeff;
7014 if (mc->attr & (A_SO|A_RV))
7015 # ifdef COLORS16
7016 i = ((i & 0x0f) << 4) | ((i & 0xf0) >> 4) | ((i & 0x200) << 1) | ((i & 0x400) >> 1);
7017 # else
7018 i = ((i & 0x0f) << 4) | ((i & 0xf0) >> 4);
7019 # endif
7021 # ifdef COLORS16
7022 if ((i & 0x0f) != 0x0f)
7023 mc->attr = (mc->attr & 0xbf) | ((i >> 3) & 0x40);
7024 if ((i & 0xf0) != 0xf0)
7025 mc->attr = (mc->attr & 0x7f) | ((i >> 3) & 0x80);
7026 # endif
7027 mc->color = 0x99 ^ mc->color;
7028 if ((i & 0x0e) == 0x0e)
7029 i = (i & 0xf0) | (mc->color & 0x0f);
7030 if ((i & 0xe0) == 0xe0)
7031 i = (i & 0x0f) | (mc->color & 0xf0);
7032 mc->color = 0x99 ^ i;
7033 debug2("ApplyAttrColor - %02x %02x\n", mc->attr, i);
7034 #endif