2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * George V. Neville-Neil. All rights reserved.
9 * See the LICENSE file for redistribution information.
15 static const char sccsid
[] = "$Id: api.c,v 8.33 2000/09/03 18:58:25 skimo Exp $ (Berkeley) $Date: 2000/09/03 18:58:25 $";
18 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <bitstring.h>
30 #include "../common/common.h"
31 #include "../ex/tag.h"
33 extern GS
*__global_list
; /* XXX */
37 * Return a pointer to the screen specified by the screen id
40 * PUBLIC: SCR *api_fscreen __P((int, char *));
53 /* Search the displayed lists. */
54 for (wp
= gp
->dq
.cqh_first
;
55 wp
!= (void *)&gp
->dq
; wp
= wp
->q
.cqe_next
)
56 for (tsp
= wp
->scrq
.cqh_first
;
57 tsp
!= (void *)&wp
->scrq
; tsp
= tsp
->q
.cqe_next
)
61 } else if (!strcmp(name
, tsp
->frp
->name
))
64 /* Search the hidden list. */
65 for (tsp
= gp
->hq
.cqh_first
;
66 tsp
!= (void *)&gp
->hq
; tsp
= tsp
->q
.cqe_next
)
70 } else if (!strcmp(name
, tsp
->frp
->name
))
79 * PUBLIC: int api_aline __P((SCR *, db_recno_t, char *, size_t));
82 api_aline(sp
, lno
, line
, len
)
91 CHAR2INT(sp
, line
, len
, wbp
, wblen
);
93 return (db_append(sp
, 1, lno
, wbp
, wblen
));
100 * PUBLIC: int api_extend __P((SCR *, db_recno_t));
108 if (db_last(sp
, &lastlno
))
111 if (db_append(sp
, 1, lastlno
++, NULL
, 0))
120 * PUBLIC: int api_dline __P((SCR *, db_recno_t));
127 if (db_delete(sp
, lno
))
129 /* change current line if deleted line is that one
130 * or one berfore that
132 if (sp
->lno
>= lno
&& sp
->lno
> 1)
141 * PUBLIC: int api_gline __P((SCR *, db_recno_t, CHAR_T **, size_t *));
144 api_gline(sp
, lno
, linepp
, lenp
)
152 if (db_eget(sp
, lno
, linepp
, lenp
, &isempty
)) {
154 msgq(sp
, M_ERR
, "209|The file is empty");
164 * PUBLIC: int api_iline __P((SCR *, db_recno_t, char *, size_t));
167 api_iline(sp
, lno
, line
, len
)
176 CHAR2INT(sp
, line
, len
, wbp
, wblen
);
178 return (db_insert(sp
, lno
, wbp
, wblen
));
183 * Return the line number of the last line in the file.
185 * PUBLIC: int api_lline __P((SCR *, db_recno_t *));
192 return (db_last(sp
, lnop
));
199 * PUBLIC: int api_sline __P((SCR *, db_recno_t, char *, size_t));
202 api_sline(sp
, lno
, line
, len
)
211 CHAR2INT(sp
, line
, len
, wbp
, wblen
);
213 return (db_set(sp
, lno
, wbp
, wblen
));
220 * PUBLIC: int api_getmark __P((SCR *, int, MARK *));
223 api_getmark(sp
, markname
, mp
)
228 return (mark_get(sp
, (ARG_CHAR_T
)markname
, mp
, M_ERR
));
235 * PUBLIC: int api_setmark __P((SCR *, int, MARK *));
238 api_setmark(sp
, markname
, mp
)
243 return (mark_set(sp
, (ARG_CHAR_T
)markname
, mp
, 1));
248 * Return the first mark if next not set, otherwise return the
251 * PUBLIC: int api_nextmark __P((SCR *, int, char *));
254 api_nextmark(sp
, next
, namep
)
261 mp
= sp
->ep
->marks
.lh_first
;
263 for (; mp
!= NULL
; mp
= mp
->q
.le_next
)
264 if (mp
->name
== *namep
) {
278 * PUBLIC: int api_getcursor __P((SCR *, MARK *));
281 api_getcursor(sp
, mp
)
294 * PUBLIC: int api_setcursor __P((SCR *, MARK *));
297 api_setcursor(sp
, mp
)
303 if (db_get(sp
, mp
->lno
, DBG_FATAL
, NULL
, &len
))
305 if (mp
->cno
< 0 || mp
->cno
> len
) {
306 msgq(sp
, M_ERR
, "Cursor set to nonexistent column");
310 /* Set the cursor. */
318 * Print an error message.
320 * PUBLIC: void api_emessage __P((SCR *, char *));
323 api_emessage(sp
, text
)
327 msgq(sp
, M_ERR
, "%s", text
);
332 * Print an informational message.
334 * PUBLIC: void api_imessage __P((SCR *, char *));
337 api_imessage(sp
, text
)
341 msgq(sp
, M_INFO
, "%s", text
);
346 * Create a new screen and return its id
347 * or edit a new file in the current screen.
349 * PUBLIC: int api_edit __P((SCR *, char *, SCR **, int));
352 api_edit(sp
, file
, spp
, newscreen
)
363 ex_cinit(sp
, &cmd
, C_EDIT
, 0, OOBLNO
, OOBLNO
, 0);
364 CHAR2INT(sp
, file
, strlen(file
) + 1, wp
, wlen
);
365 argv_exp0(sp
, &cmd
, wp
, wlen
- 1 /* terminating 0 */);
367 ex_cinit(sp
, &cmd
, C_EDIT
, 0, OOBLNO
, OOBLNO
, 0);
369 cmd
.flags
|= E_NEWSCREEN
; /* XXX */
370 if (cmd
.cmd
->fn(sp
, &cmd
))
380 * PUBLIC: int api_escreen __P((SCR *));
390 * If the interpreter exits anything other than the current
391 * screen, vi isn't going to update everything correctly.
393 ex_cinit(sp
, &cmd
, C_QUIT
, 0, OOBLNO
, OOBLNO
, 0);
394 return (cmd
.cmd
->fn(sp
, &cmd
));
399 * Switch to a new screen.
401 * PUBLIC: int api_swscreen __P((SCR *, SCR *));
404 api_swscreen(sp
, new)
409 * If the interpreter switches from anything other than the
410 * current screen, vi isn't going to update everything correctly.
413 F_SET(sp
, SC_SSWITCH
);
422 * PUBLIC: int api_map __P((SCR *, char *, char *, size_t));
425 api_map(sp
, name
, map
, len
)
434 ex_cinit(sp
, &cmd
, C_MAP
, 0, OOBLNO
, OOBLNO
, 0);
435 CHAR2INT(sp
, name
, strlen(name
) + 1, wp
, wlen
);
436 argv_exp0(sp
, &cmd
, wp
, wlen
- 1);
437 CHAR2INT(sp
, map
, len
, wp
, wlen
);
438 argv_exp0(sp
, &cmd
, wp
, wlen
);
439 return (cmd
.cmd
->fn(sp
, &cmd
));
446 * PUBLIC: int api_unmap __P((SCR *, char *));
457 ex_cinit(sp
, &cmd
, C_UNMAP
, 0, OOBLNO
, OOBLNO
, 0);
458 CHAR2INT(sp
, name
, strlen(name
) + 1, wp
, wlen
);
459 argv_exp0(sp
, &cmd
, wp
, wlen
- 1);
460 return (cmd
.cmd
->fn(sp
, &cmd
));
465 * Return a option value as a string, in allocated memory.
466 * If the option is of type boolean, boolvalue is (un)set
467 * according to the value; otherwise boolvalue is -1.
469 * PUBLIC: int api_opts_get __P((SCR *, char *, char **, int *));
472 api_opts_get(sp
, name
, value
, boolvalue
)
480 if ((op
= opts_search(name
)) == NULL
) {
481 opts_nomatch(sp
, name
);
485 offset
= op
- optlist
;
486 if (boolvalue
!= NULL
)
491 MALLOC_RET(sp
, *value
, char *, strlen(op
->name
) + 2 + 1);
492 (void)sprintf(*value
,
493 "%s%s", O_ISSET(sp
, offset
) ? "" : "no", op
->name
);
494 if (boolvalue
!= NULL
)
495 *boolvalue
= O_ISSET(sp
, offset
);
498 MALLOC_RET(sp
, *value
, char *, 20);
499 (void)sprintf(*value
, "%lu", (u_long
)O_VAL(sp
, offset
));
502 if (O_STR(sp
, offset
) == NULL
) {
503 MALLOC_RET(sp
, *value
, char *, 2);
507 *value
, char *, strlen(O_STR(sp
, offset
)) + 1);
508 (void)sprintf(*value
, "%s", O_STR(sp
, offset
));
519 * PUBLIC: int api_opts_set __P((SCR *, char *, char *, u_long, int));
522 api_opts_set(sp
, name
, str_value
, num_value
, bool_value
)
524 char *name
, *str_value
;
536 if ((op
= opts_search(name
)) == NULL
) {
537 opts_nomatch(sp
, name
);
544 GET_SPACE_RET(sp
, bp
, blen
, 64);
545 a
.len
= snprintf(bp
, 64, "%s%s", bool_value
? "" : "no", name
);
548 GET_SPACE_RET(sp
, bp
, blen
, 64);
549 a
.len
= snprintf(bp
, 64, "%s=%lu", name
, num_value
);
552 GET_SPACE_RET(sp
, bp
, blen
, 1024);
553 a
.len
= snprintf(bp
, 1024, "%s=%s", name
, str_value
);
557 CHAR2INT(sp
, bp
, a
.len
, wbp
, wblen
);
559 wbp
= v_wstrdup(sp
, wbp
, wblen
);
566 rval
= opts_set(sp
, ap
, NULL
);
569 FREE_SPACE(sp
, bp
, blen
);
576 * Execute a string as an ex command.
578 * PUBLIC: int api_run_str __P((SCR *, char *));
588 CHAR2INT(sp
, cmd
, strlen(cmd
)+1, wp
, wlen
);
589 return (ex_run_str(sp
, NULL
, wp
, wlen
- 1, 0, 0));
593 * PUBLIC: TAGQ * api_tagq_new __P((SCR*, char*));
596 api_tagq_new(sp
, tag
)
603 /* Allocate and initialize the tag queue structure. */
605 CALLOC_GOTO(sp
, tqp
, TAGQ
*, 1, sizeof(TAGQ
) + len
+ 1);
606 CIRCLEQ_INIT(&tqp
->tagq
);
608 memcpy(tqp
->tag
, tag
, (tqp
->tlen
= len
) + 1);
617 * PUBLIC: void api_tagq_add __P((SCR*, TAGQ*, char*, char *, char *));
620 api_tagq_add(sp
, tqp
, filename
, search
, msg
)
623 char *filename
, *search
, *msg
;
628 size_t flen
= strlen(filename
);
629 size_t slen
= strlen(search
);
630 size_t mlen
= strlen(msg
);
632 CALLOC_GOTO(sp
, tp
, TAG
*, 1,
633 sizeof(TAG
) - 1 + flen
+ 1 + slen
+ 1 + mlen
+ 1);
634 tp
->fname
= (char *)tp
->buf
;
635 memcpy(tp
->fname
, filename
, flen
+ 1);
637 tp
->search
= (CHAR_T
*)((char *)tp
->fname
+ flen
+ 1);
638 CHAR2INT(sp
, search
, slen
+ 1, wp
, wlen
);
639 MEMCPYW(tp
->search
, wp
, wlen
);
641 tp
->msg
= tp
->search
+ slen
+ 1;
642 CHAR2INT(sp
, msg
, mlen
+ 1, wp
, wlen
);
643 MEMCPYW(tp
->msg
, wp
, wlen
);
645 CIRCLEQ_INSERT_TAIL(&tqp
->tagq
, tp
, q
);
652 * PUBLIC: int api_tagq_push __P((SCR*, TAGQ**));
655 api_tagq_push(sp
, tqpp
)
669 /* Check to see if we found anything. */
670 if (tqp
->tagq
.cqh_first
== (void *)&tqp
->tagq
) {
675 tqp
->current
= tqp
->tagq
.cqh_first
;
677 if (tagq_push(sp
, tqp
, 0, 0))
684 * PUBLIC: void api_tagq_free __P((SCR*, TAGQ*));
687 api_tagq_free(sp
, tqp
)