-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
4056 lines (3654 loc) · 80 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* $Id: utils.c,v 2.61 1996/10/15 20:16:35 hzoli Exp $
*
* utils.c - miscellaneous utilities
*
* This file is part of zsh, the Z shell.
*
* Copyright (c) 1992-1996 Paul Falstad
* All rights reserved.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and to distribute modified versions of this software for any
* purpose, provided that the above copyright notice and the following
* two paragraphs appear in all copies of this software.
*
* In no event shall Paul Falstad or the Zsh Development Group be liable
* to any party for direct, indirect, special, incidental, or consequential
* damages arising out of the use of this software and its documentation,
* even if Paul Falstad and the Zsh Development Group have been advised of
* the possibility of such damage.
*
* Paul Falstad and the Zsh Development Group specifically disclaim any
* warranties, including, but not limited to, the implied warranties of
* merchantability and fitness for a particular purpose. The software
* provided hereunder is on an "as is" basis, and Paul Falstad and the
* Zsh Development Group have no obligation to provide maintenance,
* support, updates, enhancements, or modifications.
*
*/
/*
This special version of utils.c includes changes which are part of Event_Logger,
author Amacri - [email protected]
Please, check Event_Logger license and related terms and conditions.
*/
#include "zsh.h"
/* Print an error */
/**/
void
zwarnnam(char *cmd, char *fmt, char *str, int num)
{
int waserr;
waserr = errflag;
zerrnam(cmd, fmt, str, num);
errflag = waserr;
}
/**/
void
zerr(char *fmt, char *str, int num)
{
if (errflag || noerrs)
return;
// errflag = 1;
/*
* scriptname is set when sourcing scripts, so that we get the
* correct name instead of the generic name of whatever
* program/script is running.
*/
if (unset(SHOWERROR))
return;
nicezputs(isset(SHINSTDIN) ? "zsh" :
scriptname ? scriptname : argzero, stderr);
fputs(": ", stderr);
zerrnam(NULL, fmt, str, num);
}
/**/
void
zerrnam(char *cmd, char *fmt, char *str, int num)
{
if (unset(SHOWERROR))
return;
char *ErrorString;
if ((ErrorString = getsparam("ERRORSTRING")) && *ErrorString)
fprintf(stderr, "%s", ErrorString);
if (cmd) {
if (errflag || noerrs)
return;
errflag = 1;
if(unset(SHINSTDIN)) {
nicezputs(scriptname ? scriptname : argzero, stderr);
fputs(": ", stderr);
}
nicezputs(cmd, stderr);
fputs(": ", stderr);
}
while (*fmt)
if (*fmt == '%') {
fmt++;
switch (*fmt++) {
case 's':
nicezputs(str, stderr);
break;
case 'l': {
char sav;
num = metalen(str, num);
sav = str[num];
str[num] = '\0';
nicezputs(str, stderr);
str[num] = sav;
break;
}
case 'd':
fprintf(stderr, "%d", num);
break;
case '%':
putc('%', stderr);
break;
case 'c':
fputs(nicechar(num), stderr);
break;
case 'e':
/* print the corresponding message for this errno */
if (num == EINTR) {
fputs("interrupt\n", stderr);
errflag = 1;
return;
}
/* If the message is not about I/O problems, it looks better *
* if we uncapitalize the first letter of the message */
if (num == EIO)
fputs(strerror(num), stderr);
else {
char *errmsg = strerror(num);
fputc(tulower(errmsg[0]), stderr);
fputs(errmsg + 1, stderr);
}
break;
}
} else {
putc(*fmt == Meta ? *++fmt ^ 32 : *fmt, stderr);
fmt++;
}
if (unset(SHINSTDIN) && lineno)
fprintf(stderr, " [%ld]\n", lineno);
else
putc('\n', stderr);
fflush(stderr);
}
/* Output a single character, for the termcap routines. *
* This is used instead of putchar since it can be a macro. */
/**/
int
putraw(int c)
{
putc(c, stdout);
return 0;
}
/* Output a single character, for the termcap routines. */
/**/
int
putshout(int c)
{
putc(c, shout);
return 0;
}
/* Turn a character into a visible representation thereof. The visible *
* string is put together in a static buffer, and this function returns *
* a pointer to it. Printable characters stand for themselves, DEL is *
* represented as "^?", newline and tab are represented as "\n" and *
* "\t", and normal control characters are represented in "^C" form. *
* Characters with bit 7 set, if unprintable, are represented as "\M-" *
* followed by the visible representation of the character with bit 7 *
* stripped off. Tokens are interpreted, rather than being treated as *
* literal characters. */
/**/
char *
nicechar(int c)
{
static char buf[6];
char *s = buf;
c &= 0xff;
if (isprint(c))
goto done;
if (c & 0x80) {
*s++ = '\\';
*s++ = 'M';
*s++ = '-';
c &= 0x7f;
if(isprint(c))
goto done;
}
if (c == 0x7f) {
*s++ = '^';
c = '?';
} else if (c == '\n') {
*s++ = '\\';
c = 'n';
} else if (c == '\t') {
*s++ = '\\';
c = 't';
} else if (c < 0x20) {
*s++ = '^';
c += 0x40;
}
done:
*s++ = c;
*s = 0;
return buf;
}
#if 0
/* Output a string's visible representation. */
/**/
void
nicefputs(char *s, FILE *f)
{
for (; *s; s++)
fputs(nicechar(STOUC(*s)), f);
}
#endif
/* Return the length of the visible representation of a string. */
/**/
size_t
nicestrlen(char *s)
{
size_t l = 0;
for (; *s; s++)
l += strlen(nicechar(STOUC(*s)));
return l;
}
/* get a symlink-free pathname for s relative to PWD */
/**/
char *
findpwd(char *s)
{
char *t;
if (*s == '/')
return xsymlink(s);
s = tricat((pwd[1]) ? pwd : "", "/", s);
t = xsymlink(s);
zsfree(s);
return t;
}
/* Check whether a string contains the *
* name of the present directory. */
/**/
int
ispwd(char *s)
{
struct stat sbuf, tbuf;
if (stat(unmeta(s), &sbuf) == 0 && stat(".", &tbuf) == 0)
if (sbuf.st_dev == tbuf.st_dev && sbuf.st_ino == tbuf.st_ino)
return 1;
return 0;
}
static char xbuf[PATH_MAX*2];
/**/
char **
slashsplit(char *s)
{
char *t, **r, **q;
int t0;
if (!*s)
return (char **) zcalloc(sizeof(char **));
for (t = s, t0 = 0; *t; t++)
if (*t == '/')
t0++;
q = r = (char **) zalloc(sizeof(char **) * (t0 + 2));
while ((t = strchr(s, '/'))) {
*q++ = ztrduppfx(s, t - s);
while (*t == '/')
t++;
if (!*t) {
*q = NULL;
return r;
}
s = t;
}
*q++ = ztrdup(s);
*q = NULL;
return r;
}
/* expands symlinks and .. or . expressions */
/* if flag = 0, only expand .. and . expressions */
static int
xsymlinks(char *s, int flag)
{
char **pp, **opp;
char xbuf2[PATH_MAX*2], xbuf3[PATH_MAX*2];
int t0;
opp = pp = slashsplit(s);
for (; *pp; pp++) {
if (!strcmp(*pp, ".")) {
zsfree(*pp);
continue;
}
if (!strcmp(*pp, "..")) {
char *p;
zsfree(*pp);
if (!strcmp(xbuf, "/"))
continue;
p = xbuf + strlen(xbuf);
while (*--p != '/');
*p = '\0';
continue;
}
if (unset(CHASELINKS)) {
strcat(xbuf, "/");
strcat(xbuf, *pp);
zsfree(*pp);
continue;
}
sprintf(xbuf2, "%s/%s", xbuf, *pp);
t0 = readlink(unmeta(xbuf2), xbuf3, PATH_MAX);
if (t0 == -1 || !flag) {
strcat(xbuf, "/");
strcat(xbuf, *pp);
zsfree(*pp);
} else {
metafy(xbuf3, t0, META_NOALLOC);
if (*xbuf3 == '/') {
strcpy(xbuf, "");
if (xsymlinks(xbuf3 + 1, flag))
return 1;
} else if (xsymlinks(xbuf3, flag))
return 1;
zsfree(*pp);
}
}
free(opp);
return 0;
}
/* expand symlinks in s, and remove other weird things */
/**/
char *
xsymlink(char *s)
{
if (unset(CHASELINKS))
return ztrdup(s);
if (*s != '/')
return NULL;
*xbuf = '\0';
if (xsymlinks(s + 1, 1))
return ztrdup(s);
if (!*xbuf)
return ztrdup("/");
return ztrdup(xbuf);
}
/* print a directory */
/**/
void
fprintdir(char *s, FILE *f)
{
Nameddir d = finddir(s);
if (!d)
fputs(unmeta(s), f);
else {
putc('~', f);
fputs(unmeta(d->nam), f);
fputs(unmeta(s + strlen(d->dir)), f);
}
}
/* Returns the current username. It caches the username *
* and uid to try to avoid requerying the password files *
* or NIS/NIS+ database. */
/**/
char *
get_username(void)
{
struct passwd *pswd;
uid_t current_uid;
current_uid = getuid();
if (current_uid != cached_uid) {
cached_uid = current_uid;
zsfree(cached_username);
if ((pswd = getpwuid(current_uid)))
cached_username = ztrdup(pswd->pw_name);
else
cached_username = ztrdup("");
}
return cached_username;
}
/* static variables needed by finddir(). */
static char finddir_full[PATH_MAX];
static Nameddir finddir_last;
static int finddir_best;
/* ScanFunc used by finddir(). */
static void finddir_scan _((HashNode, int));
static void
finddir_scan(HashNode hn, int flags)
{
Nameddir nd = (Nameddir) hn;
if(nd->diff > finddir_best && !dircmp(nd->dir, finddir_full)) {
finddir_last=nd;
finddir_best=nd->diff;
}
}
/* See if a path has a named directory as its prefix. *
* If passed a NULL argument, it will invalidate any *
* cached information. */
/**/
Nameddir
finddir(char *s)
{
static struct nameddir homenode = { NULL, "", 0, NULL, 0 };
/* Invalidate directory cache if argument is NULL. This is called *
* whenever a node is added to or removed from the hash table, and *
* whenever the value of $HOME changes. (On startup, too.) */
if (!s) {
homenode.dir = home;
homenode.diff = strlen(home);
if(homenode.diff==1 || homenode.diff>=PATH_MAX)
homenode.diff = 0;
finddir_full[0] = 0;
return finddir_last = NULL;
}
if (!strcmp(s, finddir_full))
return finddir_last;
strcpy(finddir_full, s);
finddir_best=0;
finddir_last=NULL;
finddir_scan((HashNode)&homenode, 0);
scanhashtable(nameddirtab, 0, 0, 0, finddir_scan, 0);
return finddir_last;
}
/* add a named directory */
/**/
void
adduserdir(char *s, char *t, int flags, int always)
{
Nameddir nd;
/* We don't maintain a hash table in non-interactive shells. */
if (!interact)
return;
/* The ND_USERNAME flag means that this possible hash table *
* entry is derived from a passwd entry. Such entries are *
* subordinate to explicitly generated entries. */
if ((flags & ND_USERNAME) && nameddirtab->getnode2(nameddirtab, s))
return;
/* Normal parameter assignments generate calls to this function, *
* with always==0. Unless the AUTO_NAME_DIRS option is set, we *
* don't let such assignments actually create directory names. *
* Instead, a reference to the parameter as a directory name can *
* cause the actual creation of the hash table entry. */
if (!always && unset(AUTONAMEDIRS) &&
!nameddirtab->getnode2(nameddirtab, s))
return;
if (!t || *t != '/' || strlen(t) >= PATH_MAX) {
/* We can't use this value as a directory, so simply remove *
* the corresponding entry in the hash table, if any. */
HashNode hn = nameddirtab->removenode(nameddirtab, s);
if(hn)
nameddirtab->freenode(hn);
return;
}
/* add the name */
nd = (Nameddir) zcalloc(sizeof *nd);
nd->flags = flags;
nd->dir = ztrdup(t);
nameddirtab->addnode(nameddirtab, ztrdup(s), nd);
}
/* Get a named directory: this function can cause a directory name *
* to be added to the hash table, if it isn't there already. */
/**/
char *
getnameddir(char *name)
{
Param pm;
char *str;
struct passwd *pw;
Nameddir nd;
/* Check if it is already in the named directory table */
if ((nd = (Nameddir) nameddirtab->getnode(nameddirtab, name)))
return dupstring(nd->dir);
/* Check if there is a scalar parameter with this name whose value *
* begins with a `/'. If there is, add it to the hash table and *
* return the new value. */
if ((pm = (Param) paramtab->getnode(paramtab, name)) &&
(PM_TYPE(pm->flags) == PM_SCALAR) &&
(str = getsparam(name)) && *str == '/') {
adduserdir(name, str, 0, 1);
return str;
}
/* Retrieve an entry from the password table/database for this user. */
if ((pw = getpwnam(name))) {
char *dir = xsymlink(pw->pw_dir);
adduserdir(name, dir, ND_USERNAME, 1);
str = dupstring(dir);
zsfree(dir);
return str;
}
/* There are no more possible sources of directory names, so give up. */
return NULL;
}
/**/
int
dircmp(char *s, char *t)
{
if (s) {
for (; *s == *t; s++, t++)
if (!*s)
return 0;
if (!*s && *t == '/')
return 0;
}
return 1;
}
/* do pre-prompt stuff */
/**/
void
preprompt(void)
{
List list;
struct schedcmd *sch, *schl;
int period = getiparam("PERIOD");
int mailcheck = getiparam("MAILCHECK");
/* If NOTIFY is not set, then check for completed *
* jobs before we print the prompt. */
if (unset(NOTIFY))
scanjobs();
if (errflag)
return;
/* If a shell function named "precmd" exists, *
* then execute it. */
if ((list = getshfunc("precmd")))
doshfunc(list, NULL, 0, 1);
if (errflag)
return;
/* If 1) the parameter PERIOD exists, 2) the shell function *
* "periodic" exists, 3) it's been greater than PERIOD since we *
* executed "periodic", then execute it now. */
if (period && (time(NULL) > lastperiodic + period) &&
(list = getshfunc("periodic"))) {
doshfunc(list, NULL, 0, 1);
lastperiodic = time(NULL);
}
if (errflag)
return;
/* If WATCH is set, then check for the *
* specified login/logout events. */
if (watch) {
if ((int) difftime(time(NULL), lastwatch) > getiparam("LOGCHECK")) {
dowatch();
lastwatch = time(NULL);
}
}
if (errflag)
return;
/* Check mail */
if (mailcheck && (int) difftime(time(NULL), lastmailcheck) > mailcheck) {
char *mailfile;
if (mailpath && *mailpath && **mailpath)
checkmailpath(mailpath);
else if ((mailfile = getsparam("MAIL")) && *mailfile) {
char *x[2];
x[0] = mailfile;
x[1] = NULL;
checkmailpath(x);
}
lastmailcheck = time(NULL);
}
/* Check scheduled commands */
for (schl = (struct schedcmd *)&schedcmds, sch = schedcmds; sch;
sch = (schl = sch)->next) {
if (sch->time < time(NULL)) {
execstring(sch->cmd, 0, 0);
schl->next = sch->next;
zsfree(sch->cmd);
zfree(sch, sizeof(struct schedcmd));
sch = schl;
}
if (errflag)
return;
}
}
/**/
void
checkmailpath(char **s)
{
struct stat st;
char *v, *u, c;
while (*s) {
for (v = *s; *v && *v != '?'; v++);
c = *v;
*v = '\0';
if (c != '?')
u = NULL;
else
u = v + 1;
if (**s == 0) {
*v = c;
zerr("empty MAILPATH component: %s", *s, 0);
} else if (stat(unmeta(*s), &st) == -1) {
if (errno != ENOENT)
zerr("%e: %s", *s, errno);
} else if (S_ISDIR(st.st_mode)) {
LinkList l;
DIR *lock = opendir(unmeta(*s));
char buf[PATH_MAX * 2], **arr, **ap;
int ct = 1;
if (lock) {
char *fn;
HEAPALLOC {
pushheap();
l = newlinklist();
while ((fn = zreaddir(lock))) {
if (errflag)
break;
/* Ignore `.' and `..'. */
if (fn[0] == '.' &&
(fn[1] == '\0' ||
(fn[1] == '.' && fn[2] == '\0')))
continue;
if (u)
sprintf(buf, "%s/%s?%s", *s, fn, u);
else
sprintf(buf, "%s/%s", *s, fn);
addlinknode(l, dupstring(buf));
ct++;
}
closedir(lock);
ap = arr = (char **) alloc(ct * sizeof(char *));
while ((*ap++ = (char *)ugetnode(l)));
checkmailpath(arr);
popheap();
} LASTALLOC;
}
} else {
if (st.st_size && st.st_atime <= st.st_mtime &&
st.st_mtime > lastmailcheck)
if (!u) {
fprintf(stderr, "You have new mail.\n");
fflush(stderr);
} else {
char *usav = underscore;
underscore = *s;
HEAPALLOC {
u = dupstring(u);
if (! parsestr(u)) {
singsub(&u);
zputs(u, stderr);
fputc('\n', stderr);
fflush(stderr);
}
underscore = usav;
} LASTALLOC;
}
if (isset(MAILWARNING) && st.st_atime > st.st_mtime &&
st.st_atime > lastmailcheck && st.st_size) {
fprintf(stderr, "The mail in %s has been read.\n", unmeta(*s));
fflush(stderr);
}
}
*v = c;
s++;
}
}
/**/
void
freecompcond(void *a)
{
Compcond cc = (Compcond) a;
Compcond and, or, c;
int n;
for (c = cc; c; c = or) {
or = c->or;
for (; c; c = and) {
and = c->and;
if (c->type == CCT_POS ||
c->type == CCT_NUMWORDS) {
free(c->u.r.a);
free(c->u.r.b);
} else if (c->type == CCT_CURSUF ||
c->type == CCT_CURPRE) {
for (n = 0; n < c->n; n++)
if (c->u.s.s[n])
zsfree(c->u.s.s[n]);
free(c->u.s.s);
} else if (c->type == CCT_RANGESTR ||
c->type == CCT_RANGEPAT) {
for (n = 0; n < c->n; n++)
if (c->u.l.a[n])
zsfree(c->u.l.a[n]);
free(c->u.l.a);
for (n = 0; n < c->n; n++)
if (c->u.l.b[n])
zsfree(c->u.l.b[n]);
free(c->u.l.b);
} else {
for (n = 0; n < c->n; n++)
if (c->u.s.s[n])
zsfree(c->u.s.s[n]);
free(c->u.s.p);
free(c->u.s.s);
}
zfree(c, sizeof(struct compcond));
}
}
}
/**/
void
freestr(void *a)
{
zsfree(a);
}
/**/
void
gettyinfo(struct ttyinfo *ti)
{
if (SHTTY != -1) {
#ifdef HAVE_TERMIOS_H
# ifdef HAVE_TCGETATTR
if (tcgetattr(SHTTY, &ti->tio) == -1)
# else
if (ioctl(SHTTY, TCGETS, &ti->tio) == -1)
# endif
zerr("bad tcgets: %e", NULL, errno);
#else
# ifdef HAVE_TERMIO_H
ioctl(SHTTY, TCGETA, &ti->tio);
# else
ioctl(SHTTY, TIOCGETP, &ti->sgttyb);
ioctl(SHTTY, TIOCLGET, &ti->lmodes);
ioctl(SHTTY, TIOCGETC, &ti->tchars);
ioctl(SHTTY, TIOCGLTC, &ti->ltchars);
# endif
#endif
}
}
/**/
void
settyinfo(struct ttyinfo *ti)
{
if (SHTTY != -1) {
#ifdef HAVE_TERMIOS_H
# ifdef HAVE_TCGETATTR
# ifndef TCSADRAIN
# define TCSADRAIN 1 /* XXX Princeton's include files are screwed up */
# endif
tcsetattr(SHTTY, TCSADRAIN, &ti->tio);
/* if (tcsetattr(SHTTY, TCSADRAIN, &ti->tio) == -1) */
# else
ioctl(SHTTY, TCSETS, &ti->tio);
/* if (ioctl(SHTTY, TCSETS, &ti->tio) == -1) */
# endif
/* zerr("settyinfo: %e",NULL,errno)*/ ;
#else
# ifdef HAVE_TERMIO_H
ioctl(SHTTY, TCSETA, &ti->tio);
# else
ioctl(SHTTY, TIOCSETN, &ti->sgttyb);
ioctl(SHTTY, TIOCLSET, &ti->lmodes);
ioctl(SHTTY, TIOCSETC, &ti->tchars);
ioctl(SHTTY, TIOCSLTC, &ti->ltchars);
# endif
#endif
}
}
#ifdef TIOCGWINSZ
extern winchanged;
#endif
/* check the size of the window and adjust if necessary. *
* The value of from: *
* 0: called from update_job or setupvals *
* 1: called from the SIGWINCH handler *
* 2: the user have just changed LINES manually *
* 3: the user have just changed COLUMNS manually */
/**/
void
adjustwinsize(int from)
{
int oldcols = columns, oldrows = lines;
#ifdef TIOCGWINSZ
static int userlines, usercols;
if (SHTTY == -1)
return;
if (from == 2)
userlines = lines > 0;
if (from == 3)
usercols = columns > 0;
if (!ioctl(SHTTY, TIOCGWINSZ, (char *)&shttyinfo.winsize)) {
if (!userlines)
lines = shttyinfo.winsize.ws_row;
if (!usercols)
columns = shttyinfo.winsize.ws_col;
}
#endif /* TIOCGWINSZ */
if (lines <= 0)
lines = tclines > 0 ? tclines : 24;
if (columns <= 0)
columns = tccolumns > 0 ? tccolumns : 80;
if (lines > 2)
termflags &= ~TERM_SHORT;
else
termflags |= TERM_SHORT;
if (columns > 2)
termflags &= ~TERM_NARROW;
else
termflags |= TERM_NARROW;
#ifdef TIOCGWINSZ
if (from >= 2) {
shttyinfo.winsize.ws_row = lines;
shttyinfo.winsize.ws_col = columns;
ioctl(SHTTY, TIOCSWINSZ, (char *)&shttyinfo.winsize);
}
#endif
}
/* Move a fd to a place >= 10 and mark the new fd in fdtable. If the fd *
* is already >= 10, it is not moved. If it is invalid, -1 is returned. */
/**/
int
movefd(int fd)
{
if(fd != -1 && fd < 10) {
#ifdef F_DUPFD
int fe = fcntl(fd, F_DUPFD, 10);
#else
int fe = movefd(dup(fd));
#endif
zclose(fd);
fd = fe;
}
if(fd != -1) {
if (fd > max_zsh_fd) {
while (fd >= fdtable_size)
fdtable = zrealloc(fdtable, (fdtable_size *= 2));
max_zsh_fd = fd;
}
fdtable[fd] = 1;
}
return fd;
}
/* Move fd x to y. If x == -1, fd y is closed. */
/**/
void
redup(int x, int y)
{
if(x < 0)
zclose(y);
else if (x != y) {
while (y >= fdtable_size)
fdtable = zrealloc(fdtable, (fdtable_size *= 2));
dup2(x, y);
if ((fdtable[y] = fdtable[x]) && y > max_zsh_fd)
max_zsh_fd = y;
zclose(x);
}
}
/* Close the given fd, and clear it from fdtable. */
/**/
int
zclose(int fd)
{
if (fd >= 0) {
fdtable[fd] = 0;
while (max_zsh_fd > 0 && !fdtable[max_zsh_fd])
max_zsh_fd--;
if (fd == coprocin)
coprocin = -1;
if (fd == coprocout)
coprocout = -1;
}
return close(fd);
}
/* Get a file name relative to $TMPPREFIX which *
* is unique, for use as a temporary file. */
/**/
char *
gettempname(void)
{
char *s;
if (!(s = getsparam("TMPPREFIX")))
s = DEFAULT_TMPPREFIX;
return ((char *) mktemp(dyncat(unmeta(s), "XXXXXX")));
}
/* Check if a string contains a token */
/**/
int
has_token(const char *s)
{
while(*s)
if(itok(*s++))
return 1;
return 0;
}
/* Delete a character in a string */
/**/
void
chuck(char *str)
{
while ((str[0] = str[1]))
str++;
}
/**/
int