diff options
Diffstat (limited to 'usr.bin/mg')
-rw-r--r-- | usr.bin/mg/Makefile | 8 | ||||
-rw-r--r-- | usr.bin/mg/kbd.c | 11 | ||||
-rw-r--r-- | usr.bin/mg/log.c | 127 | ||||
-rw-r--r-- | usr.bin/mg/log.h | 11 | ||||
-rw-r--r-- | usr.bin/mg/main.c | 11 |
5 files changed, 165 insertions, 3 deletions
diff --git a/usr.bin/mg/Makefile b/usr.bin/mg/Makefile index 94ce340c452..daa9eae0498 100644 --- a/usr.bin/mg/Makefile +++ b/usr.bin/mg/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.33 2016/09/16 17:17:40 tedu Exp $ +# $OpenBSD: Makefile,v 1.34 2019/06/10 06:52:44 lum Exp $ PROG= mg @@ -9,6 +9,7 @@ DPADD+= ${LIBCURSES} ${LIBUTIL} # # REGEX -- create regular expression functions. # STARTUPFILE -- look for and handle initialization file. +# MGLOG -- debug mg internals to a log file. # CFLAGS+=-Wall -DREGEX @@ -23,6 +24,11 @@ SRCS= autoexec.c basic.c bell.c buffer.c cinfo.c dir.c display.c \ # SRCS+= cmode.c cscope.c dired.c grep.c tags.c +# +# -DMGLOG source file. +# +#SRCS+= log.c + afterinstall: ${INSTALL} -d -o root -g wheel ${DESTDIR}${DOCDIR}/mg ${INSTALL} ${INSTALL_COPY} -o ${DOCOWN} -g ${DOCGRP} -m ${DOCMODE} \ diff --git a/usr.bin/mg/kbd.c b/usr.bin/mg/kbd.c index 1d7a1a2a39c..0e733e15712 100644 --- a/usr.bin/mg/kbd.c +++ b/usr.bin/mg/kbd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kbd.c,v 1.30 2015/09/26 21:51:58 jasper Exp $ */ +/* $OpenBSD: kbd.c,v 1.31 2019/06/10 06:52:44 lum Exp $ */ /* This file is in the public domain. */ @@ -15,6 +15,10 @@ #include "key.h" #include "macro.h" +#ifdef MGLOG +#include "log.h" +#endif + #define METABIT 0x80 #define PROMPTL 80 @@ -152,6 +156,11 @@ doin(void) getkey(TRUE)), &curmap)) == NULL) /* nothing */; +#ifdef MGLOG + if (!mglog(funct)) + ewprintf("Problem with logging"); +#endif + if (macrodef && macrocount < MAXMACRO) macro[macrocount++].m_funct = funct; diff --git a/usr.bin/mg/log.c b/usr.bin/mg/log.c new file mode 100644 index 00000000000..4a3fcc4fd76 --- /dev/null +++ b/usr.bin/mg/log.c @@ -0,0 +1,127 @@ +/* $OpenBSD: log.c,v 1.1 2019/06/10 06:52:44 lum Exp $ */ + +/* + * This file is in the public domain. + * + * Author: Mark Lumsden <mark@showcomplex.com> + * + */ + +/* + * Record a history of an mg session for temporal debugging. + * Sometimes pressing a key will set the scene for a bug only visible + * dozens of keystrokes later. gdb has its limitations in this scenario. + */ + +#include <sys/queue.h> +#include <sys/stat.h> +#include <ctype.h> +#include <fcntl.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "def.h" +#include "log.h" +#include "funmap.h" + +char mglogpath[20]; + +int +mglog(PF funct) +{ + struct line *lp; + struct stat sb; + char *curline; + FILE *fd; + int i; + + i = 0; + + if(stat(mglogpath, &sb)) + return (FALSE); + + fd = fopen(mglogpath, "a"); + if (fprintf(fd, "%s\n", function_name(funct)) == -1) { + fclose(fd); + return (FALSE); + } + lp = bfirstlp(curbp); + + for(;;) { + i++; + curline = " "; + if (i == curwp->w_dotline) + curline = ">"; + if (fprintf(fd, "%s%p b^%p f.%p %d %d\t|%s\n", curline, + lp, lp->l_bp, lp->l_fp, + lp->l_size, lp->l_used, lp->l_text) == -1) { + fclose(fd); + return (FALSE); + } + lp = lforw(lp); + if (lp == curbp->b_headp) { + if (fprintf(fd, " %p b^%p f.%p [bhead]\n(EOB)\n", + lp, lp->l_bp, lp->l_fp) == -1) { + fclose(fd); + return (FALSE); + } + if (fprintf(fd, "lines:raw%d buf%d wdot%d\n\n", + i, curbp->b_lines, curwp->w_dotline)) { + fclose(fd); + return (FALSE); + } + break; + } + } + fclose(fd); + + return (TRUE); +} + + +/* + * Make sure logging to log file can happen. + */ +int +mgloginit(void) +{ + struct stat sb; + mode_t dir_mode, f_mode, oumask; + char *mglogdir, *mglogfile; + int fd; + + mglogdir = "./log/"; + mglogfile = "mg.log"; + + oumask = umask(0); + f_mode = 0777& ~oumask; + dir_mode = f_mode | S_IWUSR | S_IXUSR; + + if(stat(mglogdir, &sb)) { + if (mkdir(mglogdir, dir_mode) != 0) + return (FALSE); + if (chmod(mglogdir, f_mode) < 0) + return (FALSE); + } + if (strlcpy(mglogpath, mglogdir, sizeof(mglogpath)) > + sizeof(mglogpath)) + return (FALSE); + if (strlcat(mglogpath, mglogfile, sizeof(mglogpath)) > + sizeof(mglogpath)) + return (FALSE); + + if(stat(mglogpath, &sb)) + fd = open(mglogpath, O_RDWR | O_CREAT | O_TRUNC, 0644); + else + fd = open(mglogpath, O_RDWR | O_TRUNC, 0644); + + if (fd == -1) + return (FALSE); + + close(fd); + + return (TRUE); +} diff --git a/usr.bin/mg/log.h b/usr.bin/mg/log.h new file mode 100644 index 00000000000..814b19b5531 --- /dev/null +++ b/usr.bin/mg/log.h @@ -0,0 +1,11 @@ +/* $OpenBSD: log.h,v 1.1 2019/06/10 06:52:44 lum Exp $ */ + +/* This file is in the public domain. */ + +/* + * Specifically for mg logging functionality. + * + */ + +int mglog(PF); +int mgloginit(void); diff --git a/usr.bin/mg/main.c b/usr.bin/mg/main.c index b4cf96d426b..cbf5da60bb5 100644 --- a/usr.bin/mg/main.c +++ b/usr.bin/mg/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.85 2018/12/13 14:59:16 lum Exp $ */ +/* $OpenBSD: main.c,v 1.86 2019/06/10 06:52:44 lum Exp $ */ /* This file is in the public domain. */ @@ -21,6 +21,10 @@ #include "funmap.h" #include "macro.h" +#ifdef MGLOG +#include "log.h" +#endif + int thisflag; /* flags, this command */ int lastflag; /* flags, last command */ int curgoal; /* goal column */ @@ -87,6 +91,11 @@ main(int argc, char **argv) maps_init(); /* Keymaps and modes. */ funmap_init(); /* Functions. */ +#ifdef MGLOG + if (!mgloginit()) + errx(1, "Unable to create logging environment."); +#endif + /* * This is where we initialize standalone extensions that should * be loaded dynamically sometime in the future. |