1 /* $NetBSD: api.c,v 1.3 2013/11/25 22:43:46 christos Exp $ */
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * George V. Neville-Neil. All rights reserved.
10 * See the LICENSE file for redistribution information.
16 static const char sccsid
[] = "Id: api.c,v 8.40 2002/06/08 19:30:33 skimo Exp (Berkeley) Date: 2002/06/08 19:30:33 ";
19 #include <sys/types.h>
20 #include <sys/queue.h>
23 #include <bitstring.h>
31 #include "../common/common.h"
32 #include "../ex/tag.h"
34 extern GS
*__global_list
; /* XXX */
38 * Return a pointer to the screen specified by the screen id
41 * PUBLIC: SCR *api_fscreen __P((int, char *));
44 api_fscreen(int id
, char *name
)
52 /* Search the displayed lists. */
53 TAILQ_FOREACH(wp
, &gp
->dq
, q
)
54 TAILQ_FOREACH(tsp
, &wp
->scrq
, q
)
58 } else if (!strcmp(name
, tsp
->frp
->name
))
61 /* Search the hidden list. */
62 TAILQ_FOREACH (tsp
, &gp
->hq
, q
)
66 } else if (!strcmp(name
, tsp
->frp
->name
))
75 * PUBLIC: int api_aline __P((SCR *, db_recno_t, char *, size_t));
78 api_aline(SCR
*sp
, db_recno_t lno
, char *line
, size_t len
)
83 CHAR2INT(sp
, line
, len
, wbp
, wblen
);
85 return (db_append(sp
, 1, lno
, wbp
, wblen
));
92 * PUBLIC: int api_extend __P((SCR *, db_recno_t));
95 api_extend(SCR
*sp
, db_recno_t lno
)
98 if (db_last(sp
, &lastlno
))
101 if (db_append(sp
, 1, lastlno
++, NULL
, 0))
110 * PUBLIC: int api_dline __P((SCR *, db_recno_t));
113 api_dline(SCR
*sp
, db_recno_t lno
)
115 if (db_delete(sp
, lno
))
117 /* change current line if deleted line is that one
118 * or one berfore that
120 if (sp
->lno
>= lno
&& sp
->lno
> 1)
129 * PUBLIC: int api_gline __P((SCR *, db_recno_t, CHAR_T **, size_t *));
132 api_gline(SCR
*sp
, db_recno_t lno
, CHAR_T
**linepp
, size_t *lenp
)
136 if (db_eget(sp
, lno
, linepp
, lenp
, &isempty
)) {
138 msgq(sp
, M_ERR
, "209|The file is empty");
148 * PUBLIC: int api_iline __P((SCR *, db_recno_t, CHAR_T *, size_t));
151 api_iline(SCR
*sp
, db_recno_t lno
, CHAR_T
*line
, size_t len
)
153 return (db_insert(sp
, lno
, line
, len
));
158 * Return the line number of the last line in the file.
160 * PUBLIC: int api_lline __P((SCR *, db_recno_t *));
163 api_lline(SCR
*sp
, db_recno_t
*lnop
)
165 return (db_last(sp
, lnop
));
172 * PUBLIC: int api_sline __P((SCR *, db_recno_t, CHAR_T *, size_t));
175 api_sline(SCR
*sp
, db_recno_t lno
, CHAR_T
*line
, size_t len
)
177 return (db_set(sp
, lno
, line
, len
));
184 * PUBLIC: int api_getmark __P((SCR *, int, MARK *));
187 api_getmark(SCR
*sp
, int markname
, MARK
*mp
)
189 return (mark_get(sp
, (ARG_CHAR_T
)markname
, mp
, M_ERR
));
196 * PUBLIC: int api_setmark __P((SCR *, int, MARK *));
199 api_setmark(SCR
*sp
, int markname
, MARK
*mp
)
201 return (mark_set(sp
, (ARG_CHAR_T
)markname
, mp
, 1));
206 * Return the first mark if next not set, otherwise return the
209 * PUBLIC: int api_nextmark __P((SCR *, int, char *));
212 api_nextmark(SCR
*sp
, int next
, char *namep
)
216 mp
= LIST_FIRST(&sp
->ep
->marks
);
218 for (; mp
!= NULL
; mp
= LIST_NEXT(mp
, q
))
219 if (mp
->name
== *namep
) {
220 mp
= LIST_NEXT(mp
, q
);
233 * PUBLIC: int api_getcursor __P((SCR *, MARK *));
236 api_getcursor(SCR
*sp
, MARK
*mp
)
247 * PUBLIC: int api_setcursor __P((SCR *, MARK *));
250 api_setcursor(SCR
*sp
, MARK
*mp
)
254 if (db_get(sp
, mp
->lno
, DBG_FATAL
, NULL
, &len
))
257 msgq(sp
, M_ERR
, "Cursor set to nonexistent column");
261 /* Set the cursor. */
269 * Print an error message.
271 * PUBLIC: void api_emessage __P((SCR *, char *));
274 api_emessage(SCR
*sp
, char *text
)
276 msgq(sp
, M_ERR
, "%s", text
);
281 * Print an informational message.
283 * PUBLIC: void api_imessage __P((SCR *, char *));
286 api_imessage(SCR
*sp
, char *text
)
288 msgq(sp
, M_INFO
, "%s", text
);
293 * Create a new screen and return its id
294 * or edit a new file in the current screen.
296 * PUBLIC: int api_edit __P((SCR *, char *, SCR **, int));
299 api_edit(SCR
*sp
, char *file
, SCR
**spp
, int newscreen
)
306 ex_cinit(sp
, &cmd
, C_EDIT
, 0, OOBLNO
, OOBLNO
, 0);
307 CHAR2INT(sp
, file
, strlen(file
) + 1, wp
, wlen
);
308 argv_exp0(sp
, &cmd
, wp
, wlen
- 1 /* terminating 0 */);
310 ex_cinit(sp
, &cmd
, C_EDIT
, 0, OOBLNO
, OOBLNO
, 0);
312 cmd
.flags
|= E_NEWSCREEN
; /* XXX */
313 if (cmd
.cmd
->fn(sp
, &cmd
))
323 * PUBLIC: int api_escreen __P((SCR *));
332 * If the interpreter exits anything other than the current
333 * screen, vi isn't going to update everything correctly.
335 ex_cinit(sp
, &cmd
, C_QUIT
, 0, OOBLNO
, OOBLNO
, 0);
336 return (cmd
.cmd
->fn(sp
, &cmd
));
341 * Switch to a new screen.
343 * PUBLIC: int api_swscreen __P((SCR *, SCR *));
346 api_swscreen(SCR
*sp
, SCR
*new)
350 * If the interpreter switches from anything other than the
351 * current screen, vi isn't going to update everything correctly.
354 F_SET(sp
, SC_SSWITCH
);
363 * PUBLIC: int api_map __P((SCR *, char *, char *, size_t));
366 api_map(SCR
*sp
, char *name
, char *map
, size_t len
)
372 ex_cinit(sp
, &cmd
, C_MAP
, 0, OOBLNO
, OOBLNO
, 0);
373 CHAR2INT(sp
, name
, strlen(name
) + 1, wp
, wlen
);
374 argv_exp0(sp
, &cmd
, wp
, wlen
- 1);
375 CHAR2INT(sp
, map
, len
, wp
, wlen
);
376 argv_exp0(sp
, &cmd
, wp
, wlen
);
377 return (cmd
.cmd
->fn(sp
, &cmd
));
384 * PUBLIC: int api_unmap __P((SCR *, char *));
387 api_unmap(SCR
*sp
, char *name
)
393 ex_cinit(sp
, &cmd
, C_UNMAP
, 0, OOBLNO
, OOBLNO
, 0);
394 CHAR2INT(sp
, name
, strlen(name
) + 1, wp
, wlen
);
395 argv_exp0(sp
, &cmd
, wp
, wlen
- 1);
396 return (cmd
.cmd
->fn(sp
, &cmd
));
401 * Return a option value as a string, in allocated memory.
402 * If the option is of type boolean, boolvalue is (un)set
403 * according to the value; otherwise boolvalue is -1.
405 * PUBLIC: int api_opts_get __P((SCR *, const CHAR_T *, char **, int *));
408 api_opts_get(SCR
*sp
, const CHAR_T
*name
, char **value
, int *boolvalue
)
413 if ((op
= opts_search(name
)) == NULL
) {
414 opts_nomatch(sp
, name
);
418 offset
= op
- optlist
;
419 if (boolvalue
!= NULL
)
424 MALLOC_RET(sp
, *value
, char *, STRLEN(op
->name
) + 2 + 1);
425 (void)sprintf(*value
,
426 "%s"WS
, O_ISSET(sp
, offset
) ? "" : "no", op
->name
);
427 if (boolvalue
!= NULL
)
428 *boolvalue
= O_ISSET(sp
, offset
);
431 MALLOC_RET(sp
, *value
, char *, 20);
432 (void)sprintf(*value
, "%lu", (u_long
)O_VAL(sp
, offset
));
435 if (O_STR(sp
, offset
) == NULL
) {
436 MALLOC_RET(sp
, *value
, char *, 2);
440 *value
, char *, strlen(O_STR(sp
, offset
)) + 1);
441 (void)sprintf(*value
, "%s", O_STR(sp
, offset
));
452 * PUBLIC: int api_opts_set __P((SCR *, const CHAR_T *, const char *, u_long, int));
455 api_opts_set(SCR
*sp
, const CHAR_T
*name
,
456 const char *str_value
, u_long num_value
, int bool_value
)
464 if ((op
= opts_search(name
)) == NULL
) {
465 opts_nomatch(sp
, name
);
472 GET_SPACE_RETW(sp
, bp
, blen
, 64);
473 a
.len
= SPRINTF(bp
, 64, L("%s"WS
), bool_value
? "" : "no", name
);
476 GET_SPACE_RETW(sp
, bp
, blen
, 64);
477 a
.len
= SPRINTF(bp
, 64, L(""WS
"=%lu"), name
, num_value
);
480 GET_SPACE_RETW(sp
, bp
, blen
, 1024);
481 a
.len
= SPRINTF(bp
, 1024, L(""WS
"=%s"), name
, str_value
);
493 rval
= opts_set(sp
, ap
, NULL
);
495 FREE_SPACEW(sp
, bp
, blen
);
502 * Execute a string as an ex command.
504 * PUBLIC: int api_run_str __P((SCR *, char *));
507 api_run_str(SCR
*sp
, char *cmd
)
512 CHAR2INT(sp
, cmd
, strlen(cmd
)+1, wp
, wlen
);
513 return (ex_run_str(sp
, NULL
, wp
, wlen
- 1, 0, 0));
517 * PUBLIC: TAGQ * api_tagq_new __P((SCR*, char*));
520 api_tagq_new(SCR
*sp
, char *tag
)
525 /* Allocate and initialize the tag queue structure. */
527 CALLOC_GOTO(sp
, tqp
, TAGQ
*, 1, sizeof(TAGQ
) + len
+ 1);
528 TAILQ_INIT(&tqp
->tagq
);
530 memcpy(tqp
->tag
, tag
, (tqp
->tlen
= len
) + 1);
539 * PUBLIC: void api_tagq_add __P((SCR*, TAGQ*, char*, char *, char *));
542 api_tagq_add(SCR
*sp
, TAGQ
*tqp
, char *filename
, char *search
, char *msg
)
547 size_t flen
= strlen(filename
);
548 size_t slen
= strlen(search
);
549 size_t mlen
= strlen(msg
);
551 CALLOC_GOTO(sp
, tp
, TAG
*, 1,
552 sizeof(TAG
) - 1 + flen
+ 1 +
553 (slen
+ 1 + mlen
+ 1) * sizeof(CHAR_T
));
554 tp
->fname
= (char *)tp
->buf
;
555 memcpy(tp
->fname
, filename
, flen
+ 1);
557 tp
->search
= (CHAR_T
*)((char *)tp
->fname
+ flen
+ 1);
558 CHAR2INT(sp
, search
, slen
+ 1, wp
, wlen
);
559 MEMCPYW(tp
->search
, wp
, wlen
);
561 tp
->msg
= tp
->search
+ slen
+ 1;
562 CHAR2INT(sp
, msg
, mlen
+ 1, wp
, wlen
);
563 MEMCPYW(tp
->msg
, wp
, wlen
);
565 TAILQ_INSERT_TAIL(&tqp
->tagq
, tp
, q
);
572 * PUBLIC: int api_tagq_push __P((SCR*, TAGQ**));
575 api_tagq_push(SCR
*sp
, TAGQ
**tqpp
)
583 /* Check to see if we found anything. */
584 if (TAILQ_EMPTY(&tqp
->tagq
)) {
589 tqp
->current
= TAILQ_FIRST(&tqp
->tagq
);
591 if (tagq_push(sp
, tqp
, 0, 0))
598 * PUBLIC: void api_tagq_free __P((SCR*, TAGQ*));
601 api_tagq_free(SCR
*sp
, TAGQ
*tqp
)