Debian Codegraph @

util.c

raw
1 /**************************************************************************
2 * This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is *
3 * provided by Jonathan and Jovehacks without charge and without *
4 * warranty. You may copy, modify, and/or distribute JOVE, provided that *
5 * this notice is included in all the source files and documentation. *
6 **************************************************************************/
7
8 #include "jove.h"
9 #include "jctype.h"
10 #include "disp.h"
11 #include "fp.h"
12 #include "ask.h"
13 #include "chars.h"
14 #include "fmt.h"
15 #include "insert.h"
16 #include "macros.h"
17 #include "marks.h"
18 #include "move.h"
19 #include "rec.h"
20
21 #ifdef MAC
22 # include "mac.h"
23 #endif
24
25 jbool
26 blnkp(char *buf)
27 {
28 char c;
29
30 do {
31 c = *buf++;
32 } while (jiswhite(c));
33 return c == '\0'; /* It's NUL if we got to the end of the Line */
34 }
35
36 jbool
37 within_indent(void)
38 {
39 int i;
40
41 i = curchar;
42 for (;;) {
43 if (--i < 0)
44 return YES;
45
46 if (!jiswhite(linebuf[i]))
47 return NO;
48 }
49 }
50
51 void
52 DotTo(LinePtr line, int col)
53 {
54 Bufpos bp;
55
56 bp.p_line = line;
57 bp.p_char = col;
58 SetDot(&bp);
59 }
60
61 /* If bp->p_line is != current line, then save current line. Then set dot
62 * to bp->p_line, and if they weren't equal get that line into linebuf.
63 */
64 void
65 SetDot(const Bufpos *bp)
66 {
67 jbool notequal;
68
69 if (bp == NULL)
70 return;
71
72 notequal = bp->p_line != curline;
73 if (notequal)
74 lsave();
75 if (bp->p_line != NULL)
76 curline = bp->p_line;
77 if (notequal)
78 getDOT();
79 curchar = bp->p_char;
80 if (curchar > length(curline))
81 curchar = length(curline);
82 }
83
84 void
85 ToLast(void)
86 {
87 SetLine(curbuf->b_last);
88 Eol();
89 }
90
91 int MarkThresh = 22; /* VAR: moves greater than MarkThresh will SetMark (avg screen size) */
92
93 private long line_diff;
94
95 long
96 inorder(LinePtr nextp, int char1, LinePtr endp, int char2)
97 {
98 LinePtr prevp = nextp;
99
100 line_diff = 0;
101 if (nextp == endp)
102 return char1 < char2;
103
104 while (nextp && prevp) {
105 nextp = nextp->l_next;
106 prevp = prevp->l_prev;
107 line_diff += 1;
108 if (nextp == endp)
109 return YES;
110
111 if (prevp == endp)
112 return NO;
113 }
114 while (nextp!=NULL) {
115 nextp = nextp->l_next;
116 line_diff += 1;
117 if (nextp == endp)
118 return YES;
119 }
120 while (prevp!=NULL) {
121 prevp = prevp->l_prev;
122 line_diff += 1;
123 if (prevp == endp)
124 return NO;
125 }
126 /* the lines are not ordered */
127 return -1;
128 }
129
130 /* Number of lines (forward OR back) from nextp to endp.
131 * Note: if they are not related, returns 0.
132 */
133 long
134 LineDist(LinePtr nextp, LinePtr endp)
135 {
136 return inorder(nextp, 0, endp, 0) == -1? 0 : line_diff;
137 }
138
139 /* Number of lines forward from "from" to "to"; -1 if not found.
140 * Note: if "to" is (LinePtr)NULL, returns number of lines to end + 1
141 */
142 long
143 LinesTo(LinePtr from, LinePtr to)
144 {
145 long n = 0;
146
147 for (;;) {
148 if (from == to)
149 return n;
150
151 if (from == NULL)
152 return -1;
153
154 n += 1;
155 from = from->l_next;
156 }
157 }
158
159 void
160 PushPntp(LinePtr line)
161 {
162 if (LineDist(curline, line) >= MarkThresh)
163 set_mark();
164 }
165
166 void
167 ToFirst(void)
168 {
169 SetLine(curbuf->b_first);
170 }
171
172 int
173 length(LinePtr line)
174 {
175 return strlen(lcontents(line));
176 }
177
178 void
179 to_word(int dir)
180 {
181 if (dir == FORWARD) {
182 for(;;) {
183 ZXchar c = ZXC(linebuf[curchar]);
184
185 if (c == '\0') {
186 if (curline->l_next == NULL)
187 break; /* failure: out of buffer */
188
189 SetLine(curline->l_next);
190 } else if (jisword(c)) {
191 break; /* success */
192 } else {
193 curchar += 1;
194 }
195 }
196 } else {
197 for (;;) {
198 if (bolp()) {
199 if (curline->l_prev == NULL)
200 break; /* failure: out of buffer */
201
202 SetLine(curline->l_prev);
203 Eol();
204 } else if (jisword(linebuf[curchar - 1])) {
205 break; /* success */
206 } else {
207 curchar -= 1;
208 }
209 }
210 }
211 }
212
213 /* Are there any modified buffers? Allp means include B_PROCESS
214 * buffers in the check.
215 */
216 jbool
217 ModBufs(jbool allp)
218 {
219 Buffer *b;
220
221 for (b = world; b != NULL; b = b->b_next)
222 if (b->b_type != B_SCRATCH
223 && (b->b_type == B_FILE || allp)
224 && IsModified(b))
225 return YES;
226 return NO;
227 }
228
229 const char *
230 filename(const Buffer *b)
231 {
232 return b->b_fname ? pr_name(b->b_fname, YES) : "[No file]";
233 }
234
235 int
236 jmin(int a, int b)
237 {
238 return (a < b) ? a : b;
239 }
240
241 int
242 jmax(int a, int b)
243 {
244 return (a > b) ? a : b;
245 }
246
247 char *
248 lcontents(LinePtr line)
249 {
250 return line == curline? linebuf : lbptr(line);
251 }
252
253 char *
254 ltobuf(LinePtr line, char *buf)
255 {
256 if (line == curline) {
257 if (buf != linebuf)
258 strcpy(buf, linebuf);
259 Jr_Len = strlen(linebuf);
260 } else {
261 jgetline(line->l_dline, buf);
262 }
263 return buf;
264 }
265
266 void
267 DOTsave(Bufpos *buf)
268 {
269 buf->p_line = curline;
270 buf->p_char = curchar;
271 }
272
273 /* Return YES iff we had to rearrange the order. */
274
275 jbool
276 fixorder(LinePtr *line1, int *char1, LinePtr *line2, int *char2)
277 {
278 LinePtr tline;
279 int tchar;
280
281 if (inorder(*line1, *char1, *line2, *char2))
282 return NO;
283
284 tline = *line1;
285 tchar = *char1;
286 *line1 = *line2;
287 *char1 = *char2;
288 *line2 = tline;
289 *char2 = tchar;
290
291 return YES;
292 }
293
294 jbool
295 inlist(LinePtr first, LinePtr what)
296 {
297 return LinesTo(first, what) != -1;
298 }
299
300 /* Make curbuf (un)modified and tell the redisplay code to update the modeline
301 * if it will need to be changed.
302 */
303 void
304 modify(void)
305 {
306 if (!curbuf->b_modified) {
307 UpdModLine = YES;
308 curbuf->b_modified = YES;
309 }
310 DOLsave = YES;
311 #ifdef RECOVER
312 if (curbuf->b_type != B_SCRATCH)
313 ModCount += 1;
314 #endif
315 }
316
317 void
318 unmodify(void)
319 {
320 if (curbuf->b_modified) {
321 UpdModLine = YES;
322 curbuf->b_modified = NO;
323 #ifdef RECOVER
324 /*
325 * When a buffer transitions to unmodified
326 * (most likely because it was saved, or if
327 * the user invoked NotModified), then it
328 * seems right to force a SyncRec (on the next
329 * getch) so that any subsequent crash/recover
330 * does not suggest recovery of this buffer.
331 */
332 if (curbuf->b_type != B_SCRATCH)
333 ModCount = SyncFreq;
334 #endif
335 }
336 }
337
338 /* Set or clear the divergence flag for `buf'.
339 * A buffer that contains a file is considered to have diverged
340 * if the file in the filesystem appears to have changed since the
341 * last time the buffer was loaded from or saved to that file.
342 * If the flag has changed, tell the redisplay code to update the
343 * modeline.
344 */
345 void
346 diverge(Buffer *buf, jbool d)
347 {
348 if (buf->b_diverged != d) {
349 UpdModLine = YES;
350 buf->b_diverged = d;
351 }
352 }
353
354 int
355 numcomp(const char *s1, const char *s2)
356 {
357 int count = 0;
358
359 while (*s1 != '\0' && *s1++ == *s2++)
360 count += 1;
361 return count;
362 }
363
364 #ifdef FILENAME_CASEINSENSITIVE
365 int
366 numcompcase(const char *s1, const char *s2)
367 {
368 int count = 0;
369
370 while (*s1 != '\0' && CharDowncase(*s1++) == CharDowncase(*s2++))
371 count += 1;
372 return count;
373 }
374 #endif
375
376 char *
377 copystr(const char *str)
378 {
379 return str == NULL? (char *) NULL :
380 strcpy(emalloc(strlen(str) + 1), str);
381 }
382
383 #ifndef byte_copy
384 void
385 byte_copy(const void *from, void *to, size_t count)
386 {
387 const char *p = from;
388 char *q = to;
389
390 if (count != 0) {
391 do *q++ = *p++; while (--count != 0);
392 }
393 }
394 #endif
395
396 void
397 len_error(int flag)
398 {
399 static const char mesg[] = "[line too long]";
400
401 if (flag == JMP_COMPLAIN) {
402 complain(mesg);
403 /* NOTREACHED */
404 } else {
405 error(mesg);
406 /* NOTREACHED */
407 }
408 }
409
410 /* Insert num copies of character c at offset atchar in buffer buf of size max */
411
412 void
413 ins_c(DAPchar c, char *buf, int atchar, int num, int max)
414 {
415 /* hint to reader: all copying and filling is done right to left */
416 char *from, *to;
417 int taillen;
418
419 if (num <= 0)
420 return;
421
422 from = &buf[atchar];
423 taillen = *from == '\0'? 1 : strlen(from) + 1; /* include NUL */
424 if (atchar + taillen + num > max) {
425 len_error(JMP_COMPLAIN);
426 /* NOTREACHED */
427 }
428 from += taillen;
429 to = from + num;
430 do {
431 *--to = *--from;
432 } while (--taillen != 0);
433 while (to != from)
434 *--to = c;
435 }
436
437 jbool
438 TwoBlank(void)
439 {
440 LinePtr next = curline->l_next;
441
442 return (next != NULL
443 && *(lcontents(next)) == '\0'
444 && next->l_next != NULL
445 && *(lcontents(next->l_next)) == '\0');
446 }
447
448 void
449 linecopy(char *onto, int atchar, char *from)
450 {
451 char *endp = &onto[LBSIZE];
452
453 onto += atchar;
454
455 do {
456 if (onto >= endp) {
457 len_error(JMP_ERROR);
458 /* NOTREACHED */
459 }
460 } while ((*onto++ = *from++) != '\0');
461 }
462
463 char *
464 IOerr(const char *err, const char *file)
465 {
466 return sprint("Couldn't %s \"%s\".", err, file);
467 }
468
469 #ifdef UNIX
470 void
471 dopipe(int p[2])
472 {
473 if (pipe(p) == -1) {
474 complain("[Pipe failed: %s]", strerror(errno));
475 /* NOTREACHED */
476 }
477 }
478
479 void
480 pipeclose(int p[2])
481 {
482 (void) close(p[0]);
483 (void) close(p[1]);
484 }
485 #endif /* UNIX */
486
487 /* NOSTRICT */
488
489 void *
490 emalloc(size_t size)
491 {
492 void *ptr;
493
494 if ((ptr = malloc(size)) == NULL) {
495 /* Try garbage collecting lines */
496 GCchunks();
497 if ((ptr = malloc(size)) == NULL) {
498 /* Uh ... Oh screw it! */
499 error("[Out of memory]");
500 /* NOTREACHED */
501 }
502 }
503 return ptr;
504 }
505
506 void *
507 erealloc(void *ptr, size_t size)
508 {
509 if (ptr == NULL) {
510 ptr = emalloc(size);
511 } else if ((ptr = realloc(ptr, size)) == NULL) {
512 /* no second chance for realloc! */
513 error("[out of memory]");
514 /* NOTREACHED */
515 }
516 return ptr;
517 }
518
519 /* Return the basename of pathname F.
520 *
521 * - System V release 4 includes a function named "basename" in libgen.
522 * It is incompatible with ours:
523 * + it strips trailing "/" characters (does this matter?)
524 * + although not clearly documented, this stripping modifies the argument!
525 * + it handles the NULL pointer and the null string as "."
526 *
527 * - LINUX also provides a basename
528 * + at least one version of Slackware puts the prototype in <unistd.h>
529 * so it cannot be ignored.
530 * + This LINUX prototype declares the parameter to be of const char *
531 * type. This is incompatible with ours and with SVR4's.
532 * + The fact that the argument is a pointer to const implies that
533 * the source string cannot be modified. Therefore trailing "/"
534 * characters are not stripped from the source.
535 * + Either stripping isn't done to the result OR the result must be
536 * placed in a distinct chunk of memory. How is this memory managed?
537 *
538 * To avoid conflict, we have renamed ours to "jbasename".
539 */
540
541 const char *
542 jbasename(const char *f)
543 {
544 const char *cp;
545
546 #ifdef MSFILESYSTEM
547 if (f[0] != '\0' && f[1] == ':')
548 f += 2;
549 if ((cp = strrchr(f, '\\')) != NULL)
550 f = cp + 1;
551 #endif /* MSFILESYSTEM */
552 if ((cp = strrchr(f, '/')) != NULL)
553 f = cp + 1;
554 return f;
555 }
556
557 void
558 push_env(jmp_buf savejmp)
559 {
560 byte_copy(mainjmp, savejmp, sizeof(jmp_buf));
561 }
562
563 void
564 pop_env(jmp_buf savejmp)
565 {
566 byte_copy(savejmp, mainjmp, sizeof(jmp_buf));
567 }
568
569 /* get the time buf, designated by *timep, from FROM to TO. */
570 char *
571 get_time(time_t *timep, char *buf, int from, int to)
572 {
573 time_t now;
574 char *cp;
575
576 if (timep != NULL)
577 now = *timep;
578 else
579 (void) time(&now);
580 cp = ctime(&now) + from;
581 if (to == -1)
582 cp[strlen(cp) - 1] = '\0'; /* Get rid of \n */
583 else
584 cp[to - from] = '\0';
585 if (buf) {
586 strcpy(buf, cp);
587 return buf;
588 } else {
589 return cp;
590 }
591 }
592
593 /* Are s1 and s2 equal, at least for the first n chars, ignoring case? */
594
595 jbool
596 caseeqn(const char *s1, const char *s2, size_t n)
597 {
598 if (s1==NULL || s2==NULL)
599 return NO;
600
601 for (;;) {
602 if (n == 0)
603 return YES;
604 n--;
605 if (!cind_eq(*s1, *s2++))
606 return NO;
607
608 if (*s1++ == '\0')
609 return YES;
610 }
611 }
612
613 /* copy a string into buffer; truncate silently if too large; NUL-pad.
614 * Note: buffer must be 1 larger than n to fit NUL!
615 * Duplicated in recover.c: needed by scandir.c
616 */
617 void
618 null_ncpy(char *to, const char *from, size_t n)
619 {
620 (void) strncpy(to, from, n);
621 to[n] = '\0';
622 }
623
624 /* Copy a string into a buffer; truncate silently if string is too large */
625 void
626 truncstrsub(char *buf, const char *str, size_t bufsz)
627 {
628 size_t strsz = strlen(str);
629 if (bufsz == 0) {
630 complain("internal error in truncstrsub"); /* cannot even fit NUL */
631 /* NOTREACHED */
632 } else {
633 size_t sz = strsz < bufsz ? strsz : bufsz - 1;
634 byte_move(str, buf, sz);
635 buf[sz] = '\0';
636 }
637 }
638
639 /* Copy a string into a buffer; complain if string is too large */
640 void
641 jamstrsub(char *buf, const char *str, size_t bufsz)
642 {
643 size_t strsz = strlen(str);
644 if (strsz < bufsz) {
645 byte_move(str, buf, strsz);
646 buf[strsz] = '\0';
647 } else {
648 complain("string too long");
649 /* NOTREACHED */
650 }
651 }
652
653 /* Concatenate a string onto a buffer; complain if buffer not large enough */
654 void
655 jamstrcat(char *buf, const char *str, size_t bufsz)
656 {
657 size_t bstrsz = strlen(buf);
658 if (bstrsz >= bufsz) {
659 complain("base string too long!");
660 /* NOTREACHED */
661 }
662 jamstrsub(buf + bstrsz, str, bufsz - bstrsz);
663 }
664
665 jbool
666 sindex(const char *pattern, const char *string)
667 {
668 size_t len = strlen(pattern);
669
670 if (len == 0)
671 return YES;
672
673 while (*string != '\0') {
674 if (*pattern == *string && strncmp(pattern, string, len) == 0)
675 return YES;
676
677 string += 1;
678 }
679 return NO;
680 }
681
682 /* Free, then allocate a block.
683 * Like erealloc, except that the previous contents of the block are lost.
684 */
685
686 void *
687 freealloc(void *obj, size_t size)
688 {
689 if (obj != NULL)
690 obj = realloc(obj, size);
691 if (obj == NULL)
692 obj = emalloc(size);
693 return obj;
694 }
695
696 /* order file names (parameter for qsort) */
697 int
698 fnamecomp(const void *a, const void *b)
699 {
700 return strcmp(*(const char **)a, *(const char **)b);
701 }
702
703 /* decode a pair of characters representing \x or ^x */
704 ZXchar
705 DecodePair(ZXchar first, ZXchar second)
706 {
707 if (second == EOF || second == '\n') {
708 complain("unexpected end of file after %p", first);
709 /* NOTREACHED */
710 }
711 if (first == '^') {
712 if (second == '?') {
713 second = DEL;
714 } else {
715 ZXchar us = CharUpcase(second);
716
717 if (us < '@' || '_' < us) {
718 complain("unknown control character %p", second);
719 /* NOTREACHED */
720 }
721 second = CTL(us);
722 }
723 }
724 return second;
725 }
726
727 #if defined(IPROCS) || defined(SUBSHELL)
728 /* Convenience routine, initializes envp properly if it is null,
729 * hides a bit of the abstraction and some duplicated code
730 */
731 const char **
732 jenvdata(Env *envp)
733 {
734 if (envp->e_data == NULL) {
735 envp->e_data = (const char **) environ; /* avoid gcc warning */
736 envp->e_malloced = NO;
737 envp->e_headroom = 0;
738 }
739 return envp->e_data;
740 }
741
742 /* Put a definition into the environment.
743 * Same as putenv(3) in SVID 3, POSIX, and BSD 4.3.
744 */
745 void
746 jputenv(Env *envp, const char *def)
747 {
748 const char **p, **e;
749 const char *eq;
750
751 if ((eq = strchr(def, '=')) == NULL)
752 return;
753 for (p = e = jenvdata(envp); ; p++) {
754 if (*p == NULL) {
755 if (envp->e_headroom == 0) {
756 # define JENV_INCR 5
757 size_t sz = ((p-e) + 1) * sizeof(char *);
758 const char **ne = (const char **)
759 malloc(sz + JENV_INCR*sizeof(char *));
760
761 if (ne == NULL)
762 break; /* malloc failed: give up -- doesn't matter much */
763
764 byte_copy(envp->e_data, ne, sz);
765 p = ne + (p-e);
766 if (envp->e_malloced)
767 free(envp->e_data);
768 envp->e_data = e = ne;
769 envp->e_malloced = YES;
770 envp->e_headroom = JENV_INCR;
771 # undef JENV_INCR
772 }
773 envp->e_headroom -= 1;
774 *p++ = copystr(def);
775 *p = NULL;
776 break;
777 }
778 if (strncmp(*p, def, (size_t) (eq - def + 1)) == 0) {
779 *p = copystr(def);
780 break;
781 }
782 }
783 }
784
785 /* Remove any definitions of name from the environment.
786 * Same as 4.3BSD's unsetenv(3).
787 */
788 void
789 junsetenv(Env *envp, const char *name)
790 {
791 const char **p, **q;
792 size_t l = strlen(name);
793
794 for (p = q = jenvdata(envp);;) {
795 const char *e = *p++;
796
797 *q = e;
798
799 if (e == NULL)
800 break;
801
802 if (strncmp(e, name, l) == 0 && e[l] == '=') {
803 /* unset this one by not advancing q */
804 envp->e_headroom += 1;
805 } else {
806 q += 1;
807 }
808 }
809 }
810 #endif /* defined(IPROCS) */