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.39 2001/06/25 15:19:08 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:08 $";
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 *));
43 api_fscreen(int id
, char *name
)
51 /* Search the displayed lists. */
52 for (wp
= gp
->dq
.cqh_first
;
53 wp
!= (void *)&gp
->dq
; wp
= wp
->q
.cqe_next
)
54 for (tsp
= wp
->scrq
.cqh_first
;
55 tsp
!= (void *)&wp
->scrq
; tsp
= tsp
->q
.cqe_next
)
59 } else if (!strcmp(name
, tsp
->frp
->name
))
62 /* Search the hidden list. */
63 for (tsp
= gp
->hq
.cqh_first
;
64 tsp
!= (void *)&gp
->hq
; tsp
= tsp
->q
.cqe_next
)
68 } else if (!strcmp(name
, tsp
->frp
->name
))
77 * PUBLIC: int api_aline __P((SCR *, db_recno_t, char *, size_t));
80 api_aline(SCR
*sp
, db_recno_t lno
, char *line
, size_t len
)
85 CHAR2INT(sp
, line
, len
, wbp
, wblen
);
87 return (db_append(sp
, 1, lno
, wbp
, wblen
));
94 * PUBLIC: int api_extend __P((SCR *, db_recno_t));
97 api_extend(SCR
*sp
, db_recno_t lno
)
100 if (db_last(sp
, &lastlno
))
103 if (db_append(sp
, 1, lastlno
++, NULL
, 0))
112 * PUBLIC: int api_dline __P((SCR *, db_recno_t));
115 api_dline(SCR
*sp
, db_recno_t lno
)
117 if (db_delete(sp
, lno
))
119 /* change current line if deleted line is that one
120 * or one berfore that
122 if (sp
->lno
>= lno
&& sp
->lno
> 1)
131 * PUBLIC: int api_gline __P((SCR *, db_recno_t, CHAR_T **, size_t *));
134 api_gline(SCR
*sp
, db_recno_t lno
, CHAR_T
**linepp
, size_t *lenp
)
138 if (db_eget(sp
, lno
, linepp
, lenp
, &isempty
)) {
140 msgq(sp
, M_ERR
, "209|The file is empty");
150 * PUBLIC: int api_iline __P((SCR *, db_recno_t, CHAR_T *, size_t));
153 api_iline(SCR
*sp
, db_recno_t lno
, CHAR_T
*line
, size_t len
)
155 return (db_insert(sp
, lno
, line
, len
));
160 * Return the line number of the last line in the file.
162 * PUBLIC: int api_lline __P((SCR *, db_recno_t *));
165 api_lline(SCR
*sp
, db_recno_t
*lnop
)
167 return (db_last(sp
, lnop
));
174 * PUBLIC: int api_sline __P((SCR *, db_recno_t, CHAR_T *, size_t));
177 api_sline(SCR
*sp
, db_recno_t lno
, CHAR_T
*line
, size_t len
)
179 return (db_set(sp
, lno
, line
, len
));
186 * PUBLIC: int api_getmark __P((SCR *, int, MARK *));
189 api_getmark(SCR
*sp
, int markname
, MARK
*mp
)
191 return (mark_get(sp
, (ARG_CHAR_T
)markname
, mp
, M_ERR
));
198 * PUBLIC: int api_setmark __P((SCR *, int, MARK *));
201 api_setmark(SCR
*sp
, int markname
, MARK
*mp
)
203 return (mark_set(sp
, (ARG_CHAR_T
)markname
, mp
, 1));
208 * Return the first mark if next not set, otherwise return the
211 * PUBLIC: int api_nextmark __P((SCR *, int, char *));
214 api_nextmark(SCR
*sp
, int next
, char *namep
)
218 mp
= sp
->ep
->marks
.lh_first
;
220 for (; mp
!= NULL
; mp
= mp
->q
.le_next
)
221 if (mp
->name
== *namep
) {
235 * PUBLIC: int api_getcursor __P((SCR *, MARK *));
238 api_getcursor(SCR
*sp
, MARK
*mp
)
249 * PUBLIC: int api_setcursor __P((SCR *, MARK *));
252 api_setcursor(SCR
*sp
, MARK
*mp
)
256 if (db_get(sp
, mp
->lno
, DBG_FATAL
, NULL
, &len
))
258 if (mp
->cno
< 0 || mp
->cno
> len
) {
259 msgq(sp
, M_ERR
, "Cursor set to nonexistent column");
263 /* Set the cursor. */
271 * Print an error message.
273 * PUBLIC: void api_emessage __P((SCR *, char *));
276 api_emessage(SCR
*sp
, char *text
)
278 msgq(sp
, M_ERR
, "%s", text
);
283 * Print an informational message.
285 * PUBLIC: void api_imessage __P((SCR *, char *));
288 api_imessage(SCR
*sp
, char *text
)
290 msgq(sp
, M_INFO
, "%s", text
);
295 * Create a new screen and return its id
296 * or edit a new file in the current screen.
298 * PUBLIC: int api_edit __P((SCR *, char *, SCR **, int));
301 api_edit(SCR
*sp
, char *file
, SCR
**spp
, int newscreen
)
308 ex_cinit(sp
, &cmd
, C_EDIT
, 0, OOBLNO
, OOBLNO
, 0);
309 CHAR2INT(sp
, file
, strlen(file
) + 1, wp
, wlen
);
310 argv_exp0(sp
, &cmd
, wp
, wlen
- 1 /* terminating 0 */);
312 ex_cinit(sp
, &cmd
, C_EDIT
, 0, OOBLNO
, OOBLNO
, 0);
314 cmd
.flags
|= E_NEWSCREEN
; /* XXX */
315 if (cmd
.cmd
->fn(sp
, &cmd
))
325 * PUBLIC: int api_escreen __P((SCR *));
334 * If the interpreter exits anything other than the current
335 * screen, vi isn't going to update everything correctly.
337 ex_cinit(sp
, &cmd
, C_QUIT
, 0, OOBLNO
, OOBLNO
, 0);
338 return (cmd
.cmd
->fn(sp
, &cmd
));
343 * Switch to a new screen.
345 * PUBLIC: int api_swscreen __P((SCR *, SCR *));
348 api_swscreen(SCR
*sp
, SCR
*new)
352 * If the interpreter switches from anything other than the
353 * current screen, vi isn't going to update everything correctly.
356 F_SET(sp
, SC_SSWITCH
);
365 * PUBLIC: int api_map __P((SCR *, char *, char *, size_t));
368 api_map(SCR
*sp
, char *name
, char *map
, size_t len
)
374 ex_cinit(sp
, &cmd
, C_MAP
, 0, OOBLNO
, OOBLNO
, 0);
375 CHAR2INT(sp
, name
, strlen(name
) + 1, wp
, wlen
);
376 argv_exp0(sp
, &cmd
, wp
, wlen
- 1);
377 CHAR2INT(sp
, map
, len
, wp
, wlen
);
378 argv_exp0(sp
, &cmd
, wp
, wlen
);
379 return (cmd
.cmd
->fn(sp
, &cmd
));
386 * PUBLIC: int api_unmap __P((SCR *, char *));
389 api_unmap(SCR
*sp
, char *name
)
395 ex_cinit(sp
, &cmd
, C_UNMAP
, 0, OOBLNO
, OOBLNO
, 0);
396 CHAR2INT(sp
, name
, strlen(name
) + 1, wp
, wlen
);
397 argv_exp0(sp
, &cmd
, wp
, wlen
- 1);
398 return (cmd
.cmd
->fn(sp
, &cmd
));
403 * Return a option value as a string, in allocated memory.
404 * If the option is of type boolean, boolvalue is (un)set
405 * according to the value; otherwise boolvalue is -1.
407 * PUBLIC: int api_opts_get __P((SCR *, CHAR_T *, char **, int *));
410 api_opts_get(SCR
*sp
, CHAR_T
*name
, char **value
, int *boolvalue
)
415 if ((op
= opts_search(name
)) == NULL
) {
416 opts_nomatch(sp
, name
);
420 offset
= op
- optlist
;
421 if (boolvalue
!= NULL
)
426 MALLOC_RET(sp
, *value
, char *, STRLEN(op
->name
) + 2 + 1);
427 (void)sprintf(*value
,
428 "%s"WS
, O_ISSET(sp
, offset
) ? "" : "no", op
->name
);
429 if (boolvalue
!= NULL
)
430 *boolvalue
= O_ISSET(sp
, offset
);
433 MALLOC_RET(sp
, *value
, char *, 20);
434 (void)sprintf(*value
, "%lu", (u_long
)O_VAL(sp
, offset
));
437 if (O_STR(sp
, offset
) == NULL
) {
438 MALLOC_RET(sp
, *value
, char *, 2);
442 *value
, char *, strlen(O_STR(sp
, offset
)) + 1);
443 (void)sprintf(*value
, "%s", O_STR(sp
, offset
));
454 * PUBLIC: int api_opts_set __P((SCR *, CHAR_T *, char *, u_long, int));
457 api_opts_set(SCR
*sp
, CHAR_T
*name
,
458 char *str_value
, u_long num_value
, int bool_value
)
466 if ((op
= opts_search(name
)) == NULL
) {
467 opts_nomatch(sp
, name
);
474 GET_SPACE_RETW(sp
, bp
, blen
, 64);
475 a
.len
= SPRINTF(bp
, 64, L("%s"WS
), bool_value
? "" : "no", name
);
478 GET_SPACE_RETW(sp
, bp
, blen
, 64);
479 a
.len
= SPRINTF(bp
, 64, L(""WS
"=%lu"), name
, num_value
);
482 GET_SPACE_RETW(sp
, bp
, blen
, 1024);
483 a
.len
= SPRINTF(bp
, 1024, L(""WS
"=%s"), name
, str_value
);
492 rval
= opts_set(sp
, ap
, NULL
);
494 FREE_SPACEW(sp
, bp
, blen
);
501 * Execute a string as an ex command.
503 * PUBLIC: int api_run_str __P((SCR *, char *));
506 api_run_str(SCR
*sp
, char *cmd
)
511 CHAR2INT(sp
, cmd
, strlen(cmd
)+1, wp
, wlen
);
512 return (ex_run_str(sp
, NULL
, wp
, wlen
- 1, 0, 0));
516 * PUBLIC: TAGQ * api_tagq_new __P((SCR*, char*));
519 api_tagq_new(SCR
*sp
, char *tag
)
524 /* Allocate and initialize the tag queue structure. */
526 CALLOC_GOTO(sp
, tqp
, TAGQ
*, 1, sizeof(TAGQ
) + len
+ 1);
527 CIRCLEQ_INIT(&tqp
->tagq
);
529 memcpy(tqp
->tag
, tag
, (tqp
->tlen
= len
) + 1);
538 * PUBLIC: void api_tagq_add __P((SCR*, TAGQ*, char*, char *, char *));
541 api_tagq_add(SCR
*sp
, TAGQ
*tqp
, char *filename
, char *search
, char *msg
)
546 size_t flen
= strlen(filename
);
547 size_t slen
= strlen(search
);
548 size_t mlen
= strlen(msg
);
550 CALLOC_GOTO(sp
, tp
, TAG
*, 1,
551 sizeof(TAG
) - 1 + flen
+ 1 +
552 (slen
+ 1 + mlen
+ 1) * sizeof(CHAR_T
));
553 tp
->fname
= (char *)tp
->buf
;
554 memcpy(tp
->fname
, filename
, flen
+ 1);
556 tp
->search
= (CHAR_T
*)((char *)tp
->fname
+ flen
+ 1);
557 CHAR2INT(sp
, search
, slen
+ 1, wp
, wlen
);
558 MEMCPYW(tp
->search
, wp
, wlen
);
560 tp
->msg
= tp
->search
+ slen
+ 1;
561 CHAR2INT(sp
, msg
, mlen
+ 1, wp
, wlen
);
562 MEMCPYW(tp
->msg
, wp
, wlen
);
564 CIRCLEQ_INSERT_TAIL(&tqp
->tagq
, tp
, q
);
571 * PUBLIC: int api_tagq_push __P((SCR*, TAGQ**));
574 api_tagq_push(SCR
*sp
, TAGQ
**tqpp
)
586 /* Check to see if we found anything. */
587 if (tqp
->tagq
.cqh_first
== (void *)&tqp
->tagq
) {
592 tqp
->current
= tqp
->tagq
.cqh_first
;
594 if (tagq_push(sp
, tqp
, 0, 0))
601 * PUBLIC: void api_tagq_free __P((SCR*, TAGQ*));
604 api_tagq_free(SCR
*sp
, TAGQ
*tqp
)