From 4f13fff6a49aa0fe5fee41c983bc3ca0f202c01a Mon Sep 17 00:00:00 2001 From: skimo Date: Sat, 24 Jun 2000 18:54:49 +0000 Subject: [PATCH] split off ipc vi --- cl/cl_main.c | 51 +-------------------- common/gs.c | 64 ++++++++++++++++++++++++++ dist/Makefile.in | 14 ++++-- ip/ip_main.c | 134 ++++++++++++++++++++++++++++++++++++++++--------------- ipc/ip.h | 6 ++- ipc/ip_run.c | 6 +-- 6 files changed, 183 insertions(+), 92 deletions(-) create mode 100644 common/gs.c diff --git a/cl/cl_main.c b/cl/cl_main.c index 75b82e79..4854e826 100644 --- a/cl/cl_main.c +++ b/cl/cl_main.c @@ -10,7 +10,7 @@ #include "config.h" #ifndef lint -static const char sccsid[] = "$Id: cl_main.c,v 10.46 1997/08/02 16:48:14 bostic Exp $ (Berkeley) $Date: 1997/08/02 16:48:14 $"; +static const char sccsid[] = "$Id: cl_main.c,v 10.47 2000/06/24 18:54:49 skimo Exp $ (Berkeley) $Date: 2000/06/24 18:54:49 $"; #endif /* not lint */ #include @@ -38,7 +38,6 @@ sigset_t __sigblockset; /* GLOBAL: Blocked signals. */ static void cl_func_std __P((GS *)); static void cl_end __P((CL_PRIVATE *)); static CL_PRIVATE *cl_init __P((GS *)); -static GS *gs_init __P((char *)); static void perr __P((char *, char *)); static int setsig __P((int, struct sigaction *, void (*)(int))); static void sig_end __P((GS *)); @@ -58,7 +57,7 @@ main(argc, argv) GS *gp; size_t rows, cols; int rval; - char *ip_arg, **p_av, **t_av, *ttype; + char **p_av, **t_av, *ttype; /* If loaded at 0 and jumping through a NULL pointer, stop. */ if (reenter++) @@ -72,7 +71,6 @@ main(argc, argv) * no way to portably call getopt twice, so arguments parsed here must * be removed from the argument list. */ - ip_arg = NULL; for (p_av = t_av = argv;;) { if (*t_av == NULL) { *p_av = NULL; @@ -82,29 +80,9 @@ main(argc, argv) while ((*p_av++ = *t_av++) != NULL); break; } - if (!memcmp(*t_av, "-I", sizeof("-I") - 1)) { - if (t_av[0][2] != '\0') { - ip_arg = t_av[0] + 2; - ++t_av; - --argc; - continue; - } - if (t_av[1] != NULL) { - ip_arg = t_av[1]; - t_av += 2; - argc -= 2; - continue; - } - } *p_av++ = *t_av++; } - /* - * If we're being called as an editor library, we're done here, we - * get loaded with the curses screen, we don't share much code. - */ - if (ip_arg != NULL) - exit (ip_main(argc, argv, gp, ip_arg)); /* Create and initialize the CL_PRIVATE structure. */ clp = cl_init(gp); @@ -179,31 +157,6 @@ main(argc, argv) } /* - * gs_init -- - * Create and partially initialize the GS structure. - */ -static GS * -gs_init(name) - char *name; -{ - GS *gp; - char *p; - - /* Figure out what our name is. */ - if ((p = strrchr(name, '/')) != NULL) - name = p + 1; - - /* Allocate the global structure. */ - CALLOC_NOMSG(NULL, gp, GS *, 1, sizeof(GS)); - if (gp == NULL) - perr(name, NULL); - - - gp->progname = name; - return (gp); -} - -/* * cl_init -- * Create and partially initialize the CL structure. */ diff --git a/common/gs.c b/common/gs.c new file mode 100644 index 00000000..f99e2b3c --- /dev/null +++ b/common/gs.c @@ -0,0 +1,64 @@ +/*- + * Copyright (c) 2000 + * Sven Verdoolaege. All rights reserved. + * + * See the LICENSE file for redistribution information. + */ + +#include "config.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "../common/common.h" + +static void perr __P((char *, char *)); + +/* + * gs_init -- + * Create and partially initialize the GS structure. + * PUBLIC: GS * gs_init __P((char*)); + */ +GS * +gs_init(name) + char *name; +{ + GS *gp; + char *p; + + /* Figure out what our name is. */ + if ((p = strrchr(name, '/')) != NULL) + name = p + 1; + + /* Allocate the global structure. */ + CALLOC_NOMSG(NULL, gp, GS *, 1, sizeof(GS)); + if (gp == NULL) + perr(name, NULL); + + gp->progname = name; + + return (gp); +} + +/* + * perr -- + * Print system error. + */ +static void +perr(name, msg) + char *name, *msg; +{ + (void)fprintf(stderr, "%s:", name); + if (msg != NULL) + (void)fprintf(stderr, "%s:", msg); + (void)fprintf(stderr, "%s\n", strerror(errno)); + exit(1); +} diff --git a/dist/Makefile.in b/dist/Makefile.in index 0bf3f0c7..0b223a9d 100644 --- a/dist/Makefile.in +++ b/dist/Makefile.in @@ -36,7 +36,7 @@ VIOBJS= api.o cut.o db.o delete.o ex.o ex_abbrev.o ex_append.o ex_args.o \ ex_put.o ex_quit.o ex_read.o ex_screen.o ex_script.o ex_set.o \ ex_shell.o ex_shift.o ex_source.o ex_stop.o ex_subst.o ex_tag.o \ ex_tcl.o ex_txt.o ex_undo.o ex_usage.o ex_util.o ex_version.o \ - ex_visual.o ex_write.o ex_yank.o ex_z.o exf.o getc.o key.o \ + ex_visual.o ex_write.o ex_yank.o ex_z.o exf.o getc.o gs.o key.o \ log.o main.o mark.o msg.o options.o options_f.o put.o recover.o \ screen.o search.o seq.o trace.o util.o v_at.o v_ch.o v_cmd.o \ v_delete.o v_event.o v_ex.o v_increment.o v_init.o v_itxt.o \ @@ -46,14 +46,18 @@ VIOBJS= api.o cut.o db.o delete.o ex.o ex_abbrev.o ex_append.o ex_args.o \ v_word.o v_xchar.o v_yank.o v_z.o v_zexit.o vi.o vs_line.o \ vs_msg.o vs_refresh.o vs_relative.o vs_smap.o vs_split.o -all: vi @vi_motif@ +all: vi vi-ipc @vi_motif@ -VIALL= $(CLOBJS) $(IPOBJS) $(VIOBJS) @cobjs@ @LIBOBJS@ +VIALL= $(CLOBJS) $(VIOBJS) @cobjs@ @LIBOBJS@ vi ex: $(VIALL) $(SHRPENV) $(CC) $(LDFLAGS) -o $@ $(VIALL) @LIBS@ -$(rm) -f ex $(ln) $@ ex +IPALL= $(IPOBJS) $(VIOBJS) @cobjs@ @LIBOBJS@ +vi-ipc: $(IPALL) + $(SHRPENV) $(CC) $(LDFLAGS) -o $@ $(IPALL) @LIBS@ + MALL= m_main.o m_cde.o trace.o libmvi.a vi_motif: $(MALL) $(SHRPENV) $(CC) $(LDFLAGS) -o $@ $(MALL) @MOTIFLIBS@ @@ -246,7 +250,7 @@ ip_term.o: $(srcdir)/ip/ip_term.c $(CC) $(CFLAGS) $? # VI is the binary that the client programs run. -VI=-DVI=\"$(bindir)/`echo vi | sed '$(transform)'`\" +VI=-DVI=\"$(bindir)/`echo vi-ipc | sed '$(transform)'`\" # Shared IP client sources. ip_run.o: $(srcdir)/ipc/ip_run.c @@ -317,6 +321,8 @@ delete.o: $(srcdir)/common/delete.c $(CC) $(CFLAGS) $? exf.o: $(srcdir)/common/exf.c $(CC) $(CFLAGS) $? +gs.o: $(srcdir)/common/gs.c + $(CC) $(CFLAGS) $? key.o: $(srcdir)/common/key.c $(CC) $(CFLAGS) $? log.o: $(srcdir)/common/log.c diff --git a/ip/ip_main.c b/ip/ip_main.c index 914b6de8..2d689f65 100644 --- a/ip/ip_main.c +++ b/ip/ip_main.c @@ -8,7 +8,7 @@ #include "config.h" #ifndef lint -static const char sccsid[] = "$Id: ip_main.c,v 8.10 1997/08/02 16:48:59 bostic Exp $ (Berkeley) $Date: 1997/08/02 16:48:59 $"; +static const char sccsid[] = "$Id: ip_main.c,v 8.11 2000/06/24 18:54:50 skimo Exp $ (Berkeley) $Date: 2000/06/24 18:54:50 $"; #endif /* not lint */ #include @@ -22,15 +22,20 @@ static const char sccsid[] = "$Id: ip_main.c,v 8.10 1997/08/02 16:48:59 bostic E #include #include +#include +#include + #include "../common/common.h" #include "../ipc/ip.h" #include "extern.h" int vi_ofd; /* GLOBAL: known to vi_send(). */ +GS *__global_list; /* GLOBAL: List of screens. */ static void ip_func_std __P((GS *)); -static IP_PRIVATE *ip_init __P((GS *, char *)); +static IP_PRIVATE *ip_init __P((GS *gp, int i_fd, int o_fd, int argc, char *argv[])); static void perr __P((char *, char *)); +static void run_editor __P((void * vp)); /* * ip_main -- @@ -39,20 +44,95 @@ static void perr __P((char *, char *)); * PUBLIC: int ip_main __P((int, char *[], GS *, char *)); */ int -ip_main(argc, argv, gp, ip_arg) +main(argc, argv) int argc; - char *argv[], *ip_arg; - GS *gp; + char *argv[]; { - EVENT ev; IP_PRIVATE *ipp; - IP_BUF ipb; int rval; + char *ip_arg; + char **p_av, **t_av; + GS *gp; + int i_fd, o_fd, main_ifd, main_ofd; + char *ep; + + /* Create and initialize the global structure. */ + __global_list = gp = gs_init(argv[0]); + + /* + * Strip out any arguments that vi isn't going to understand. There's + * no way to portably call getopt twice, so arguments parsed here must + * be removed from the argument list. + */ + ip_arg = NULL; + for (p_av = t_av = argv;;) { + if (*t_av == NULL) { + *p_av = NULL; + break; + } + if (!strcmp(*t_av, "--")) { + while ((*p_av++ = *t_av++) != NULL); + break; + } + if (!memcmp(*t_av, "-I", sizeof("-I") - 1)) { + if (t_av[0][2] != '\0') { + ip_arg = t_av[0] + 2; + ++t_av; + --argc; + continue; + } + else if (t_av[1] != NULL) { + ip_arg = t_av[1]; + t_av += 2; + argc -= 2; + continue; + } + } + *p_av++ = *t_av++; + } + + /* + * Crack ip_arg -- it's of the form #.#, where the first number is the + * file descriptor from the screen, the second is the file descriptor + * to the screen. + */ + if (!isdigit(ip_arg[0])) + goto usage; + i_fd = strtol(ip_arg, &ep, 10); + if (ep[0] != '.' || !isdigit(ep[1])) + goto usage; + o_fd = strtol(++ep, &ep, 10); + if (ep[0] != '\0') { +usage: ip_usage(); + return (NULL); + } /* Create and partially initialize the IP structure. */ - if ((ipp = ip_init(gp, ip_arg)) == NULL) + if ((ipp = ip_init(gp, i_fd, o_fd, argc, argv)) == NULL) return (1); + run_editor((void *)gp); + + /* Free the global and IP private areas. */ +#if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY) + free(gp); +#endif + exit (rval); +} + +static void +run_editor(void * vp) +{ + GS *gp; + IP_PRIVATE *ipp; + EVENT ev; + int rval; + IP_BUF ipb; + + gp = (GS *)vp; + + ipp = gp->ip_private; + /* Add the terminal type to the global structure. */ if ((OG_D_STR(gp, GO_TERM) = OG_STR(gp, GO_TERM) = strdup("ip_curses")) == NULL) @@ -64,18 +144,18 @@ ip_main(argc, argv, gp, ip_arg) */ for (;;) { if (ip_event(NULL, &ev, 0, 0)) - return (1); + return; if (ev.e_event == E_WRESIZE) break; if (ev.e_event == E_EOF || ev.e_event == E_ERR || ev.e_event == E_SIGHUP || ev.e_event == E_SIGTERM) - return (1); + return; if (ev.e_event == E_IPCOMMAND && ev.e_ipcom == VI_QUIT) - return (1); + return; } /* Run ex/vi. */ - rval = editor(gp, argc, argv); + rval = editor(gp, ipp->argc, ipp->argv); /* Clean up the screen. */ (void)ip_quit(gp); @@ -87,12 +167,9 @@ ip_main(argc, argv, gp, ip_arg) /* Give the screen a couple of seconds to deal with it. */ sleep(2); - /* Free the global and IP private areas. */ #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY) free(ipp); - free(gp); #endif - return (rval); } /* @@ -100,12 +177,9 @@ ip_main(argc, argv, gp, ip_arg) * Create and partially initialize the GS structure. */ static IP_PRIVATE * -ip_init(gp, ip_arg) - GS *gp; - char *ip_arg; +ip_init(GS *gp, int i_fd, int o_fd, int argc, char *argv[]) { IP_PRIVATE *ipp; - char *ep; /* Allocate the IP private structure. */ CALLOC_NOMSG(NULL, ipp, IP_PRIVATE *, 1, sizeof(IP_PRIVATE)); @@ -113,22 +187,12 @@ ip_init(gp, ip_arg) perr(gp->progname, NULL); gp->ip_private = ipp; - /* - * Crack ip_arg -- it's of the form #.#, where the first number is the - * file descriptor from the screen, the second is the file descriptor - * to the screen. - */ - if (!isdigit(ip_arg[0])) - goto usage; - ipp->i_fd = strtol(ip_arg, &ep, 10); - if (ep[0] != '.' || !isdigit(ep[1])) - goto usage; - vi_ofd = strtol(++ep, &ep, 10); - if (ep[0] != '\0') { -usage: ip_usage(); - return (NULL); - } - + ipp->i_fd = i_fd; + vi_ofd = ipp->o_fd = o_fd; + + ipp->argc = argc; + ipp->argv = argv; + /* Initialize the list of ip functions. */ ip_func_std(gp); diff --git a/ipc/ip.h b/ipc/ip.h index da6e6a4a..0ee06741 100644 --- a/ipc/ip.h +++ b/ipc/ip.h @@ -4,7 +4,7 @@ * * See the LICENSE file for redistribution information. * - * $Id: ip.h,v 8.20 2000/05/07 19:49:42 skimo Exp $ (Berkeley) $Date: 2000/05/07 19:49:42 $ + * $Id: ip.h,v 8.21 2000/06/24 18:54:50 skimo Exp $ (Berkeley) $Date: 2000/06/24 18:54:50 $ */ extern int vi_ifd; /* Input file descriptor. */ @@ -12,6 +12,10 @@ extern int vi_ofd; /* Output file descriptor. */ typedef struct _ip_private { int i_fd; /* Input file descriptor. */ + int o_fd; /* Output file descriptor. */ + + int argc; + char **argv; size_t row; /* Current row. */ size_t col; /* Current column. */ diff --git a/ipc/ip_run.c b/ipc/ip_run.c index 845a9d88..1950ed8c 100644 --- a/ipc/ip_run.c +++ b/ipc/ip_run.c @@ -10,7 +10,7 @@ #include "config.h" #ifndef lint -static const char sccsid[] = "$Id: ip_run.c,v 8.13 1997/08/03 15:31:34 bostic Exp $ (Berkeley) $Date: 1997/08/03 15:31:34 $"; +static const char sccsid[] = "$Id: ip_run.c,v 8.14 2000/06/24 18:54:50 skimo Exp $ (Berkeley) $Date: 2000/06/24 18:54:50 $"; #endif /* not lint */ #include @@ -141,8 +141,8 @@ vi_run(argc, argv, ip, op, pidp) * (debugging) nvi, run it, otherwise run the user's path, * if specified, else run the compiled in path. */ - if (!pflag && stat("vi", &sb) == 0) - execv("vi", argv); + if (!pflag && stat("vi-ipc", &sb) == 0) + execv("vi-ipc", argv); execv(execp, argv); (void)fprintf(stderr, "%s: %s %s\n", vi_progname, execp, strerror(errno)); -- 2.11.4.GIT