DB 3.1 update
[nvi.git] / common / msg.c
blobe9ab6b8885bf0751631d57f4dcde82f8eba1a8f8
1 /*-
2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1991, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: msg.c,v 10.51 2000/04/21 21:26:19 skimo Exp $ (Berkeley) $Date: 2000/04/21 21:26:19 $";
14 #endif /* not lint */
16 #include <sys/param.h>
17 #include <sys/types.h> /* XXX: param.h may not have included types.h */
18 #include <sys/queue.h>
19 #include <sys/stat.h>
20 #include <sys/time.h>
22 #include <bitstring.h>
23 #include <ctype.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <limits.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
32 #ifdef __STDC__
33 #include <stdarg.h>
34 #else
35 #include <varargs.h>
36 #endif
38 #include "common.h"
39 #include "../vi/vi.h"
42 * msgq --
43 * Display a message.
45 * PUBLIC: void msgq __P((SCR *, mtype_t, const char *, ...));
47 void
48 #ifdef __STDC__
49 msgq(SCR *sp, mtype_t mt, const char *fmt, ...)
50 #else
51 msgq(sp, mt, fmt, va_alist)
52 SCR *sp;
53 mtype_t mt;
54 const char *fmt;
55 va_dcl
56 #endif
58 #ifndef NL_ARGMAX
59 #define __NL_ARGMAX 20 /* Set to 9 by System V. */
60 struct {
61 const char *str; /* String pointer. */
62 size_t arg; /* Argument number. */
63 size_t prefix; /* Prefix string length. */
64 size_t skip; /* Skipped string length. */
65 size_t suffix; /* Suffix string length. */
66 } str[__NL_ARGMAX];
67 #endif
68 static int reenter; /* STATIC: Re-entrancy check. */
69 CHAR_T ch;
70 GS *gp;
71 size_t blen, cnt1, cnt2, len, mlen, nlen, soff;
72 const char *p, *t, *u;
73 char *bp, *mp, *rbp, *s_rbp;
74 va_list ap;
77 * !!!
78 * It's possible to enter msg when there's no screen to hold the
79 * message. If sp is NULL, ignore the special cases and put the
80 * message out to stderr.
82 if (sp == NULL) {
83 gp = NULL;
84 if (mt == M_BERR)
85 mt = M_ERR;
86 else if (mt == M_VINFO)
87 mt = M_INFO;
88 } else {
89 gp = sp->gp;
90 switch (mt) {
91 case M_BERR:
92 if (F_ISSET(sp, SC_VI) && !O_ISSET(sp, O_VERBOSE)) {
93 F_SET(gp, G_BELLSCHED);
94 return;
96 mt = M_ERR;
97 break;
98 case M_VINFO:
99 if (!O_ISSET(sp, O_VERBOSE))
100 return;
101 mt = M_INFO;
102 /* FALLTHROUGH */
103 case M_INFO:
104 if (F_ISSET(sp, SC_EX_SILENT))
105 return;
106 break;
107 case M_ERR:
108 case M_SYSERR:
109 case M_DBERR:
110 break;
111 default:
112 abort();
117 * It's possible to reenter msg when it allocates space. We're
118 * probably dead anyway, but there's no reason to drop core.
120 * XXX
121 * Yes, there's a race, but it should only be two instructions.
123 if (reenter++)
124 return;
126 /* Get space for the message. */
127 nlen = 1024;
128 if (0) {
129 retry: FREE_SPACE(sp, bp, blen);
130 nlen *= 2;
132 bp = NULL;
133 blen = 0;
134 GET_SPACE_GOTO(sp, bp, blen, nlen);
137 * Error prefix.
139 * mp: pointer to the current next character to be written
140 * mlen: length of the already written characters
141 * blen: total length of the buffer
143 #define REM (blen - mlen)
144 mp = bp;
145 mlen = 0;
146 if (mt == M_SYSERR || mt == M_DBERR) {
147 p = msg_cat(sp, "020|Error: ", &len);
148 if (REM < len)
149 goto retry;
150 memcpy(mp, p, len);
151 mp += len;
152 mlen += len;
156 * If we're running an ex command that the user didn't enter, display
157 * the file name and line number prefix.
159 if ((mt == M_ERR || mt == M_SYSERR) &&
160 sp != NULL && gp != NULL && gp->if_name != NULL) {
161 for (p = gp->if_name; *p != '\0'; ++p) {
162 len = snprintf(mp, REM, "%s", KEY_NAME(sp, *p));
163 mp += len;
164 if ((mlen += len) > blen)
165 goto retry;
167 len = snprintf(mp, REM, ", %d: ", gp->if_lno);
168 mp += len;
169 if ((mlen += len) > blen)
170 goto retry;
173 /* If nothing to format, we're done. */
174 if (fmt == NULL)
175 goto nofmt;
176 fmt = msg_cat(sp, fmt, NULL);
178 #ifndef NL_ARGMAX
180 * Nvi should run on machines that don't support the numbered argument
181 * specifications (%[digit]*$). We do this by reformatting the string
182 * so that we can hand it to vsprintf(3) and it will use the arguments
183 * in the right order. When vsprintf returns, we put the string back
184 * into the right order. It's undefined, according to SVID III, to mix
185 * numbered argument specifications with the standard style arguments,
186 * so this should be safe.
188 * In addition, we also need a character that is known to not occur in
189 * any vi message, for separating the parts of the string. As callers
190 * of msgq are responsible for making sure that all the non-printable
191 * characters are formatted for printing before calling msgq, we use a
192 * random non-printable character selected at terminal initialization
193 * time. This code isn't fast by any means, but as messages should be
194 * relatively short and normally have only a few arguments, it won't be
195 * too bad. Regardless, nobody has come up with any other solution.
197 * The result of this loop is an array of pointers into the message
198 * string, with associated lengths and argument numbers. The array
199 * is in the "correct" order, and the arg field contains the argument
200 * order.
202 for (p = fmt, soff = 0; soff < __NL_ARGMAX;) {
203 for (t = p; *p != '\0' && *p != '%'; ++p);
204 if (*p == '\0')
205 break;
206 ++p;
207 if (!isdigit(*p)) {
208 if (*p == '%')
209 ++p;
210 continue;
212 for (u = p; *++p != '\0' && isdigit(*p););
213 if (*p != '$')
214 continue;
216 /* Up to, and including the % character. */
217 str[soff].str = t;
218 str[soff].prefix = u - t;
220 /* Up to, and including the $ character. */
221 str[soff].arg = atoi(u);
222 str[soff].skip = (p - u) + 1;
223 if (str[soff].arg >= __NL_ARGMAX)
224 goto ret;
226 /* Up to, and including the conversion character. */
227 for (u = p; (ch = *++p) != '\0';)
228 if (isalpha(ch) &&
229 strchr("diouxXfeEgGcspn", ch) != NULL)
230 break;
231 str[soff].suffix = p - u;
232 if (ch != '\0')
233 ++p;
234 ++soff;
237 /* If no magic strings, we're done. */
238 if (soff == 0)
239 goto format;
241 /* Get space for the reordered strings. */
242 if ((rbp = malloc(nlen)) == NULL)
243 goto ret;
244 s_rbp = rbp;
247 * Reorder the strings into the message string based on argument
248 * order.
250 * !!!
251 * We ignore arguments that are out of order, i.e. if we don't find
252 * an argument, we continue. Assume (almost certainly incorrectly)
253 * that whoever created the string knew what they were doing.
255 * !!!
256 * Brute force "sort", but since we don't expect more than one or two
257 * arguments in a string, the setup cost of a fast sort will be more
258 * expensive than the loop.
260 for (cnt1 = 1; cnt1 <= soff; ++cnt1)
261 for (cnt2 = 0; cnt2 < soff; ++cnt2)
262 if (cnt1 == str[cnt2].arg) {
263 memmove(s_rbp, str[cnt2].str, str[cnt2].prefix);
264 memmove(s_rbp + str[cnt2].prefix,
265 str[cnt2].str + str[cnt2].prefix +
266 str[cnt2].skip, str[cnt2].suffix);
267 s_rbp += str[cnt2].prefix + str[cnt2].suffix;
268 *s_rbp++ =
269 gp == NULL ? DEFAULT_NOPRINT : gp->noprint;
270 break;
272 *s_rbp = '\0';
273 fmt = rbp;
274 #endif
276 format: /* Format the arguments into the string. */
277 #ifdef __STDC__
278 va_start(ap, fmt);
279 #else
280 va_start(ap);
281 #endif
282 len = vsnprintf(mp, REM, fmt, ap);
283 va_end(ap);
284 if (len >= nlen)
285 goto retry;
287 #ifndef NL_ARGMAX
288 if (soff == 0)
289 goto nofmt;
292 * Go through the resulting string, and, for each separator character
293 * separated string, enter its new starting position and length in the
294 * array.
296 for (p = t = mp, cnt1 = 1,
297 ch = gp == NULL ? DEFAULT_NOPRINT : gp->noprint; *p != '\0'; ++p)
298 if (*p == ch) {
299 for (cnt2 = 0; cnt2 < soff; ++cnt2)
300 if (str[cnt2].arg == cnt1)
301 break;
302 str[cnt2].str = t;
303 str[cnt2].prefix = p - t;
304 t = p + 1;
305 ++cnt1;
309 * Reorder the strings once again, putting them back into the
310 * message buffer.
312 * !!!
313 * Note, the length of the message gets decremented once for
314 * each substring, when we discard the separator character.
316 for (s_rbp = rbp, cnt1 = 0; cnt1 < soff; ++cnt1) {
317 memmove(rbp, str[cnt1].str, str[cnt1].prefix);
318 rbp += str[cnt1].prefix;
319 --len;
321 memmove(mp, s_rbp, rbp - s_rbp);
323 /* Free the reordered string memory. */
324 free(s_rbp);
325 #endif
327 nofmt: mp += len;
328 if ((mlen += len) > blen)
329 goto retry;
330 if (mt == M_SYSERR) {
331 len = snprintf(mp, REM, ": %s", strerror(errno));
332 mp += len;
333 if ((mlen += len) > blen)
334 goto retry;
335 mt = M_ERR;
337 if (mt == M_DBERR) {
338 len = snprintf(mp, REM, ": %s", db_strerror(sp->db_error));
339 mp += len;
340 if ((mlen += len) > blen)
341 goto retry;
342 mt = M_ERR;
345 /* Add trailing newline. */
346 if ((mlen += 1) > blen)
347 goto retry;
348 *mp = '\n';
350 if (sp != NULL)
351 (void)ex_fflush(sp);
352 if (gp != NULL)
353 gp->scr_msg(sp, mt, bp, mlen);
354 else
355 (void)fprintf(stderr, "%.*s", (int)mlen, bp);
357 /* Cleanup. */
358 ret: FREE_SPACE(sp, bp, blen);
359 alloc_err:
360 reenter = 0;
364 * msgq_str --
365 * Display a message with an embedded string.
367 * PUBLIC: void msgq_str __P((SCR *, mtype_t, char *, char *));
369 void
370 msgq_str(sp, mtype, str, fmt)
371 SCR *sp;
372 mtype_t mtype;
373 char *str, *fmt;
375 int nf, sv_errno;
376 char *p;
378 if (str == NULL) {
379 msgq(sp, mtype, fmt);
380 return;
383 sv_errno = errno;
384 p = msg_print(sp, str, &nf);
385 errno = sv_errno;
386 msgq(sp, mtype, fmt, p);
387 if (nf)
388 FREE_SPACE(sp, p, 0);
392 * mod_rpt --
393 * Report on the lines that changed.
395 * !!!
396 * Historic vi documentation (USD:15-8) claimed that "The editor will also
397 * always tell you when a change you make affects text which you cannot see."
398 * This wasn't true -- edit a large file and do "100d|1". We don't implement
399 * this semantic since it requires tracking each line that changes during a
400 * command instead of just keeping count.
402 * Line counts weren't right in historic vi, either. For example, given the
403 * file:
404 * abc
405 * def
406 * the command 2d}, from the 'b' would report that two lines were deleted,
407 * not one.
409 * PUBLIC: void mod_rpt __P((SCR *));
411 void
412 mod_rpt(sp)
413 SCR *sp;
415 static char * const action[] = {
416 "293|added",
417 "294|changed",
418 "295|deleted",
419 "296|joined",
420 "297|moved",
421 "298|shifted",
422 "299|yanked",
424 static char * const lines[] = {
425 "300|line",
426 "301|lines",
428 db_recno_t total;
429 u_long rptval;
430 int first, cnt;
431 size_t blen, len, tlen;
432 const char *t;
433 char * const *ap;
434 char *bp, *p;
436 /* Change reports are turned off in batch mode. */
437 if (F_ISSET(sp, SC_EX_SILENT))
438 return;
440 /* Reset changing line number. */
441 sp->rptlchange = OOBLNO;
444 * Don't build a message if not enough changed.
446 * !!!
447 * And now, a vi clone test. Historically, vi reported if the number
448 * of changed lines was > than the value, not >=, unless it was a yank
449 * command, which used >=. No lie. Furthermore, an action was never
450 * reported for a single line action. This is consistent for actions
451 * other than yank, but yank didn't report single line actions even if
452 * the report edit option was set to 1. In addition, setting report to
453 * 0 in the 4BSD historic vi was equivalent to setting it to 1, for an
454 * unknown reason (this bug was fixed in System III/V at some point).
455 * I got complaints, so nvi conforms to System III/V historic practice
456 * except that we report a yank of 1 line if report is set to 1.
458 #define ARSIZE(a) sizeof(a) / sizeof (*a)
459 #define MAXNUM 25
460 rptval = O_VAL(sp, O_REPORT);
461 for (cnt = 0, total = 0; cnt < ARSIZE(action); ++cnt)
462 total += sp->rptlines[cnt];
463 if (total == 0)
464 return;
465 if (total <= rptval && sp->rptlines[L_YANKED] < rptval) {
466 for (cnt = 0; cnt < ARSIZE(action); ++cnt)
467 sp->rptlines[cnt] = 0;
468 return;
471 /* Build and display the message. */
472 GET_SPACE_GOTO(sp, bp, blen, sizeof(action) * MAXNUM + 1);
473 for (p = bp, first = 1, tlen = 0,
474 ap = action, cnt = 0; cnt < ARSIZE(action); ++ap, ++cnt)
475 if (sp->rptlines[cnt] != 0) {
476 if (first)
477 first = 0;
478 else {
479 *p++ = ';';
480 *p++ = ' ';
481 tlen += 2;
483 len = snprintf(p, MAXNUM, "%lu ", sp->rptlines[cnt]);
484 p += len;
485 tlen += len;
486 t = msg_cat(sp,
487 lines[sp->rptlines[cnt] == 1 ? 0 : 1], &len);
488 memcpy(p, t, len);
489 p += len;
490 tlen += len;
491 *p++ = ' ';
492 ++tlen;
493 t = msg_cat(sp, *ap, &len);
494 memcpy(p, t, len);
495 p += len;
496 tlen += len;
497 sp->rptlines[cnt] = 0;
500 /* Add trailing newline. */
501 *p = '\n';
502 ++tlen;
504 (void)ex_fflush(sp);
505 sp->gp->scr_msg(sp, M_INFO, bp, tlen);
507 FREE_SPACE(sp, bp, blen);
508 alloc_err:
509 return;
511 #undef ARSIZE
512 #undef MAXNUM
516 * msgq_status --
517 * Report on the file's status.
519 * PUBLIC: void msgq_status __P((SCR *, db_recno_t, u_int));
521 void
522 msgq_status(sp, lno, flags)
523 SCR *sp;
524 db_recno_t lno;
525 u_int flags;
527 db_recno_t last;
528 size_t blen, len;
529 int cnt, needsep;
530 const char *t;
531 char **ap, *bp, *np, *p, *s;
533 /* Get sufficient memory. */
534 len = strlen(sp->frp->name);
535 GET_SPACE_GOTO(sp, bp, blen, len * MAX_CHARACTER_COLUMNS + 128);
536 p = bp;
538 /* Copy in the filename. */
539 for (p = bp, t = sp->frp->name; *t != '\0'; ++t) {
540 len = KEY_LEN(sp, *t);
541 memcpy(p, KEY_NAME(sp, *t), len);
542 p += len;
544 np = p;
545 *p++ = ':';
546 *p++ = ' ';
548 /* Copy in the argument count. */
549 if (F_ISSET(sp, SC_STATUS_CNT) && sp->argv != NULL) {
550 for (cnt = 0, ap = sp->argv; *ap != NULL; ++ap, ++cnt);
551 if (cnt > 1) {
552 (void)sprintf(p,
553 msg_cat(sp, "317|%d files to edit", NULL), cnt);
554 p += strlen(p);
555 *p++ = ':';
556 *p++ = ' ';
558 F_CLR(sp, SC_STATUS_CNT);
562 * See nvi/exf.c:file_init() for a description of how and when the
563 * read-only bit is set.
565 * !!!
566 * The historic display for "name changed" was "[Not edited]".
568 needsep = 0;
569 if (F_ISSET(sp->frp, FR_NEWFILE)) {
570 F_CLR(sp->frp, FR_NEWFILE);
571 t = msg_cat(sp, "021|new file", &len);
572 memcpy(p, t, len);
573 p += len;
574 needsep = 1;
575 } else {
576 if (F_ISSET(sp->frp, FR_NAMECHANGE)) {
577 t = msg_cat(sp, "022|name changed", &len);
578 memcpy(p, t, len);
579 p += len;
580 needsep = 1;
582 if (needsep) {
583 *p++ = ',';
584 *p++ = ' ';
586 if (F_ISSET(sp->ep, F_MODIFIED))
587 t = msg_cat(sp, "023|modified", &len);
588 else
589 t = msg_cat(sp, "024|unmodified", &len);
590 memcpy(p, t, len);
591 p += len;
592 needsep = 1;
594 if (F_ISSET(sp->frp, FR_UNLOCKED)) {
595 if (needsep) {
596 *p++ = ',';
597 *p++ = ' ';
599 t = msg_cat(sp, "025|UNLOCKED", &len);
600 memcpy(p, t, len);
601 p += len;
602 needsep = 1;
604 if (O_ISSET(sp, O_READONLY)) {
605 if (needsep) {
606 *p++ = ',';
607 *p++ = ' ';
609 t = msg_cat(sp, "026|readonly", &len);
610 memcpy(p, t, len);
611 p += len;
612 needsep = 1;
614 if (needsep) {
615 *p++ = ':';
616 *p++ = ' ';
618 if (LF_ISSET(MSTAT_SHOWLAST)) {
619 if (db_last(sp, &last))
620 return;
621 if (last == 0) {
622 t = msg_cat(sp, "028|empty file", &len);
623 memcpy(p, t, len);
624 p += len;
625 } else {
626 t = msg_cat(sp, "027|line %lu of %lu [%ld%%]", &len);
627 (void)sprintf(p, t, lno, last, (lno * 100) / last);
628 p += strlen(p);
630 } else {
631 t = msg_cat(sp, "029|line %lu", &len);
632 (void)sprintf(p, t, lno);
633 p += strlen(p);
635 #ifdef DEBUG
636 (void)sprintf(p, " (pid %lu)", (u_long)getpid());
637 p += strlen(p);
638 #endif
639 *p++ = '\n';
640 len = p - bp;
643 * There's a nasty problem with long path names. Cscope and tags files
644 * can result in long paths and vi will request a continuation key from
645 * the user as soon as it starts the screen. Unfortunately, the user
646 * has already typed ahead, and chaos results. If we assume that the
647 * characters in the filenames and informational messages only take a
648 * single screen column each, we can trim the filename.
650 * XXX
651 * Status lines get put up at fairly awkward times. For example, when
652 * you do a filter read (e.g., :read ! echo foo) in the top screen of a
653 * split screen, we have to repaint the status lines for all the screens
654 * below the top screen. We don't want users having to enter continue
655 * characters for those screens. Make it really hard to screw this up.
657 s = bp;
658 if (LF_ISSET(MSTAT_TRUNCATE) && len > sp->cols) {
659 for (; s < np && (*s != '/' || (p - s) > sp->cols - 3); ++s);
660 if (s == np) {
661 s = p - (sp->cols - 5);
662 *--s = ' ';
664 *--s = '.';
665 *--s = '.';
666 *--s = '.';
667 len = p - s;
670 /* Flush any waiting ex messages. */
671 (void)ex_fflush(sp);
673 sp->gp->scr_msg(sp, M_INFO, s, len);
675 FREE_SPACE(sp, bp, blen);
676 alloc_err:
677 return;
681 * msg_open --
682 * Open the message catalogs.
684 * PUBLIC: int msg_open __P((SCR *, char *));
687 msg_open(sp, file)
688 SCR *sp;
689 char *file;
692 * !!!
693 * Assume that the first file opened is the system default, and that
694 * all subsequent ones user defined. Only display error messages
695 * if we can't open the user defined ones -- it's useful to know if
696 * the system one wasn't there, but if nvi is being shipped with an
697 * installed system, the file will be there, if it's not, then the
698 * message will be repeated every time nvi is started up.
700 static int first = 1;
701 DB *db;
702 DBT data, key;
703 db_recno_t msgno;
704 char *p, *t, buf[MAXPATHLEN];
706 if ((p = strrchr(file, '/')) != NULL && p[1] == '\0' &&
707 (((t = getenv("LC_MESSAGES")) != NULL && t[0] != '\0') ||
708 ((t = getenv("LANG")) != NULL && t[0] != '\0'))) {
709 (void)snprintf(buf, sizeof(buf), "%s%s", file, t);
710 p = buf;
711 } else
712 p = file;
713 if ((sp->db_error = db_create(&db, sp->gp->env, 0)) != 0 ||
714 (sp->db_error = db->set_re_source(db, p)) != 0 ||
715 (sp->db_error = db->open(db, NULL, NULL, DB_RECNO, 0, 0)) != 0) {
716 if (first) {
717 first = 0;
718 return (1);
720 msgq_str(sp, M_DBERR, p, "%s");
721 return (1);
725 * Test record 1 for the magic string. The msgq call is here so
726 * the message catalog build finds it.
728 #define VMC "VI_MESSAGE_CATALOG"
729 memset(&key, 0, sizeof(key));
730 key.data = &msgno;
731 key.size = sizeof(db_recno_t);
732 memset(&data, 0, sizeof(data));
733 msgno = 1;
734 if ((sp->db_error = db->get(db, NULL, &key, &data, 0)) != 0 ||
735 data.size != sizeof(VMC) - 1 ||
736 memcmp(data.data, VMC, sizeof(VMC) - 1)) {
737 (void)db->close(db, DB_NOSYNC);
738 if (first) {
739 first = 0;
740 return (1);
742 msgq_str(sp, M_DBERR, p,
743 "030|The file %s is not a message catalog");
744 return (1);
746 first = 0;
748 if (sp->gp->msg != NULL)
749 (void)sp->gp->msg->close(sp->gp->msg, DB_NOSYNC);
750 sp->gp->msg = db;
751 return (0);
755 * msg_close --
756 * Close the message catalogs.
758 * PUBLIC: void msg_close __P((GS *));
760 void
761 msg_close(gp)
762 GS *gp;
764 if (gp->msg != NULL)
765 (void)gp->msg->close(gp->msg, 0);
769 * msg_cont --
770 * Return common continuation messages.
772 * PUBLIC: const char *msg_cmsg __P((SCR *, cmsg_t, size_t *));
774 const char *
775 msg_cmsg(sp, which, lenp)
776 SCR *sp;
777 cmsg_t which;
778 size_t *lenp;
780 switch (which) {
781 case CMSG_CONF:
782 return (msg_cat(sp, "268|confirm? [ynq]", lenp));
783 case CMSG_CONT:
784 return (msg_cat(sp, "269|Press any key to continue: ", lenp));
785 case CMSG_CONT_EX:
786 return (msg_cat(sp,
787 "270|Press any key to continue [: to enter more ex commands]: ",
788 lenp));
789 case CMSG_CONT_R:
790 return (msg_cat(sp, "161|Press Enter to continue: ", lenp));
791 case CMSG_CONT_S:
792 return (msg_cat(sp, "275| cont?", lenp));
793 case CMSG_CONT_Q:
794 return (msg_cat(sp,
795 "271|Press any key to continue [q to quit]: ", lenp));
796 default:
797 abort();
799 /* NOTREACHED */
803 * msg_cat --
804 * Return a single message from the catalog, plus its length.
806 * !!!
807 * Only a single catalog message can be accessed at a time, if multiple
808 * ones are needed, they must be copied into local memory.
810 * PUBLIC: const char *msg_cat __P((SCR *, const char *, size_t *));
812 const char *
813 msg_cat(sp, str, lenp)
814 SCR *sp;
815 const char *str;
816 size_t *lenp;
818 GS *gp;
819 DBT data, key;
820 db_recno_t msgno;
823 * If it's not a catalog message, i.e. has doesn't have a leading
824 * number and '|' symbol, we're done.
826 if (isdigit(str[0]) &&
827 isdigit(str[1]) && isdigit(str[2]) && str[3] == '|') {
828 memset(&key, 0, sizeof(key));
829 key.data = &msgno;
830 key.size = sizeof(db_recno_t);
831 memset(&data, 0, sizeof(data));
832 msgno = atoi(str);
835 * XXX
836 * Really sleazy hack -- we put an extra character on the
837 * end of the format string, and then we change it to be
838 * the nul termination of the string. There ought to be
839 * a better way. Once we can allocate multiple temporary
840 * memory buffers, maybe we can use one of them instead.
842 gp = sp == NULL ? NULL : sp->gp;
843 if (gp != NULL && gp->msg != NULL &&
844 gp->msg->get(gp->msg, NULL, &key, &data, 0) == 0 &&
845 data.size != 0) {
846 if (lenp != NULL)
847 *lenp = data.size - 1;
848 ((char *)data.data)[data.size - 1] = '\0';
849 return (data.data);
851 str = &str[4];
853 if (lenp != NULL)
854 *lenp = strlen(str);
855 return (str);
859 * msg_print --
860 * Return a printable version of a string, in allocated memory.
862 * PUBLIC: char *msg_print __P((SCR *, const char *, int *));
864 char *
865 msg_print(sp, s, needfree)
866 SCR *sp;
867 const char *s;
868 int *needfree;
870 size_t blen, nlen;
871 const char *cp;
872 char *bp, *ep, *p, *t;
874 *needfree = 0;
876 for (cp = s; *cp != '\0'; ++cp)
877 if (!isprint(*cp))
878 break;
879 if (*cp == '\0')
880 return ((char *)s); /* SAFE: needfree set to 0. */
882 nlen = 0;
883 if (0) {
884 retry: if (sp == NULL)
885 free(bp);
886 else
887 FREE_SPACE(sp, bp, blen);
888 needfree = 0;
890 nlen += 256;
891 if (sp == NULL) {
892 if ((bp = malloc(nlen)) == NULL)
893 goto alloc_err;
894 } else
895 GET_SPACE_GOTO(sp, bp, blen, nlen);
896 if (0) {
897 alloc_err: return ("");
899 *needfree = 1;
901 for (p = bp, ep = (bp + blen) - 1, cp = s; *cp != '\0' && p < ep; ++cp)
902 for (t = KEY_NAME(sp, *cp); *t != '\0' && p < ep; *p++ = *t++);
903 if (p == ep)
904 goto retry;
905 *p = '\0';
906 return (bp);