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.34 2001/03/14 21:48:14 skimo Exp $ (Berkeley) $Date: 2001/03/14 21:48:14 $";
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_T *, size_t));
167 api_iline(sp
, lno
, line
, len
)
173 return (db_insert(sp
, lno
, line
, len
));
178 * Return the line number of the last line in the file.
180 * PUBLIC: int api_lline __P((SCR *, db_recno_t *));
187 return (db_last(sp
, lnop
));
194 * PUBLIC: int api_sline __P((SCR *, db_recno_t, CHAR_T *, size_t));
197 api_sline(sp
, lno
, line
, len
)
203 return (db_set(sp
, lno
, line
, len
));
210 * PUBLIC: int api_getmark __P((SCR *, int, MARK *));
213 api_getmark(sp
, markname
, mp
)
218 return (mark_get(sp
, (ARG_CHAR_T
)markname
, mp
, M_ERR
));
225 * PUBLIC: int api_setmark __P((SCR *, int, MARK *));
228 api_setmark(sp
, markname
, mp
)
233 return (mark_set(sp
, (ARG_CHAR_T
)markname
, mp
, 1));
238 * Return the first mark if next not set, otherwise return the
241 * PUBLIC: int api_nextmark __P((SCR *, int, char *));
244 api_nextmark(sp
, next
, namep
)
251 mp
= sp
->ep
->marks
.lh_first
;
253 for (; mp
!= NULL
; mp
= mp
->q
.le_next
)
254 if (mp
->name
== *namep
) {
268 * PUBLIC: int api_getcursor __P((SCR *, MARK *));
271 api_getcursor(sp
, mp
)
284 * PUBLIC: int api_setcursor __P((SCR *, MARK *));
287 api_setcursor(sp
, mp
)
293 if (db_get(sp
, mp
->lno
, DBG_FATAL
, NULL
, &len
))
295 if (mp
->cno
< 0 || mp
->cno
> len
) {
296 msgq(sp
, M_ERR
, "Cursor set to nonexistent column");
300 /* Set the cursor. */
308 * Print an error message.
310 * PUBLIC: void api_emessage __P((SCR *, char *));
313 api_emessage(sp
, text
)
317 msgq(sp
, M_ERR
, "%s", text
);
322 * Print an informational message.
324 * PUBLIC: void api_imessage __P((SCR *, char *));
327 api_imessage(sp
, text
)
331 msgq(sp
, M_INFO
, "%s", text
);
336 * Create a new screen and return its id
337 * or edit a new file in the current screen.
339 * PUBLIC: int api_edit __P((SCR *, char *, SCR **, int));
342 api_edit(sp
, file
, spp
, newscreen
)
353 ex_cinit(sp
, &cmd
, C_EDIT
, 0, OOBLNO
, OOBLNO
, 0);
354 CHAR2INT(sp
, file
, strlen(file
) + 1, wp
, wlen
);
355 argv_exp0(sp
, &cmd
, wp
, wlen
- 1 /* terminating 0 */);
357 ex_cinit(sp
, &cmd
, C_EDIT
, 0, OOBLNO
, OOBLNO
, 0);
359 cmd
.flags
|= E_NEWSCREEN
; /* XXX */
360 if (cmd
.cmd
->fn(sp
, &cmd
))
370 * PUBLIC: int api_escreen __P((SCR *));
380 * If the interpreter exits anything other than the current
381 * screen, vi isn't going to update everything correctly.
383 ex_cinit(sp
, &cmd
, C_QUIT
, 0, OOBLNO
, OOBLNO
, 0);
384 return (cmd
.cmd
->fn(sp
, &cmd
));
389 * Switch to a new screen.
391 * PUBLIC: int api_swscreen __P((SCR *, SCR *));
394 api_swscreen(sp
, new)
399 * If the interpreter switches from anything other than the
400 * current screen, vi isn't going to update everything correctly.
403 F_SET(sp
, SC_SSWITCH
);
412 * PUBLIC: int api_map __P((SCR *, char *, char *, size_t));
415 api_map(sp
, name
, map
, len
)
424 ex_cinit(sp
, &cmd
, C_MAP
, 0, OOBLNO
, OOBLNO
, 0);
425 CHAR2INT(sp
, name
, strlen(name
) + 1, wp
, wlen
);
426 argv_exp0(sp
, &cmd
, wp
, wlen
- 1);
427 CHAR2INT(sp
, map
, len
, wp
, wlen
);
428 argv_exp0(sp
, &cmd
, wp
, wlen
);
429 return (cmd
.cmd
->fn(sp
, &cmd
));
436 * PUBLIC: int api_unmap __P((SCR *, char *));
447 ex_cinit(sp
, &cmd
, C_UNMAP
, 0, OOBLNO
, OOBLNO
, 0);
448 CHAR2INT(sp
, name
, strlen(name
) + 1, wp
, wlen
);
449 argv_exp0(sp
, &cmd
, wp
, wlen
- 1);
450 return (cmd
.cmd
->fn(sp
, &cmd
));
455 * Return a option value as a string, in allocated memory.
456 * If the option is of type boolean, boolvalue is (un)set
457 * according to the value; otherwise boolvalue is -1.
459 * PUBLIC: int api_opts_get __P((SCR *, char *, char **, int *));
462 api_opts_get(sp
, name
, value
, boolvalue
)
470 if ((op
= opts_search(name
)) == NULL
) {
471 opts_nomatch(sp
, name
);
475 offset
= op
- optlist
;
476 if (boolvalue
!= NULL
)
481 MALLOC_RET(sp
, *value
, char *, strlen(op
->name
) + 2 + 1);
482 (void)sprintf(*value
,
483 "%s%s", O_ISSET(sp
, offset
) ? "" : "no", op
->name
);
484 if (boolvalue
!= NULL
)
485 *boolvalue
= O_ISSET(sp
, offset
);
488 MALLOC_RET(sp
, *value
, char *, 20);
489 (void)sprintf(*value
, "%lu", (u_long
)O_VAL(sp
, offset
));
492 if (O_STR(sp
, offset
) == NULL
) {
493 MALLOC_RET(sp
, *value
, char *, 2);
497 *value
, char *, strlen(O_STR(sp
, offset
)) + 1);
498 (void)sprintf(*value
, "%s", O_STR(sp
, offset
));
509 * PUBLIC: int api_opts_set __P((SCR *, char *, char *, u_long, int));
512 api_opts_set(sp
, name
, str_value
, num_value
, bool_value
)
514 char *name
, *str_value
;
526 if ((op
= opts_search(name
)) == NULL
) {
527 opts_nomatch(sp
, name
);
534 GET_SPACE_RET(sp
, bp
, blen
, 64);
535 a
.len
= snprintf(bp
, 64, "%s%s", bool_value
? "" : "no", name
);
538 GET_SPACE_RET(sp
, bp
, blen
, 64);
539 a
.len
= snprintf(bp
, 64, "%s=%lu", name
, num_value
);
542 GET_SPACE_RET(sp
, bp
, blen
, 1024);
543 a
.len
= snprintf(bp
, 1024, "%s=%s", name
, str_value
);
547 CHAR2INT(sp
, bp
, a
.len
, wbp
, wblen
);
549 wbp
= v_wstrdup(sp
, wbp
, wblen
);
556 rval
= opts_set(sp
, ap
, NULL
);
559 FREE_SPACE(sp
, bp
, blen
);
566 * Execute a string as an ex command.
568 * PUBLIC: int api_run_str __P((SCR *, char *));
578 CHAR2INT(sp
, cmd
, strlen(cmd
)+1, wp
, wlen
);
579 return (ex_run_str(sp
, NULL
, wp
, wlen
- 1, 0, 0));
583 * PUBLIC: TAGQ * api_tagq_new __P((SCR*, char*));
586 api_tagq_new(sp
, tag
)
593 /* Allocate and initialize the tag queue structure. */
595 CALLOC_GOTO(sp
, tqp
, TAGQ
*, 1, sizeof(TAGQ
) + len
+ 1);
596 CIRCLEQ_INIT(&tqp
->tagq
);
598 memcpy(tqp
->tag
, tag
, (tqp
->tlen
= len
) + 1);
607 * PUBLIC: void api_tagq_add __P((SCR*, TAGQ*, char*, char *, char *));
610 api_tagq_add(sp
, tqp
, filename
, search
, msg
)
613 char *filename
, *search
, *msg
;
618 size_t flen
= strlen(filename
);
619 size_t slen
= strlen(search
);
620 size_t mlen
= strlen(msg
);
622 CALLOC_GOTO(sp
, tp
, TAG
*, 1,
623 sizeof(TAG
) - 1 + flen
+ 1 + slen
+ 1 + mlen
+ 1);
624 tp
->fname
= (char *)tp
->buf
;
625 memcpy(tp
->fname
, filename
, flen
+ 1);
627 tp
->search
= (CHAR_T
*)((char *)tp
->fname
+ flen
+ 1);
628 CHAR2INT(sp
, search
, slen
+ 1, wp
, wlen
);
629 MEMCPYW(tp
->search
, wp
, wlen
);
631 tp
->msg
= tp
->search
+ slen
+ 1;
632 CHAR2INT(sp
, msg
, mlen
+ 1, wp
, wlen
);
633 MEMCPYW(tp
->msg
, wp
, wlen
);
635 CIRCLEQ_INSERT_TAIL(&tqp
->tagq
, tp
, q
);
642 * PUBLIC: int api_tagq_push __P((SCR*, TAGQ**));
645 api_tagq_push(sp
, tqpp
)
659 /* Check to see if we found anything. */
660 if (tqp
->tagq
.cqh_first
== (void *)&tqp
->tagq
) {
665 tqp
->current
= tqp
->tagq
.cqh_first
;
667 if (tagq_push(sp
, tqp
, 0, 0))
674 * PUBLIC: void api_tagq_free __P((SCR*, TAGQ*));
677 api_tagq_free(sp
, tqp
)