From 8efbb1f90525d91c0f0bac38a678dd8d5f81d723 Mon Sep 17 00:00:00 2001
From: Thomas Blume <Thomas.Blume@suse.com>
Date: Thu, 24 Nov 2016 14:27:18 +0100
Subject: [PATCH] implements simple session management

added new option "-session=<sessionname>"

port of: w3m-0.4.1-session-mgmt.dif
---
 fm.h      |  1 +
 history.c | 22 ++++++++++++++++++++--
 main.c    | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 84 insertions(+), 3 deletions(-)

diff --git a/fm.h b/fm.h
index 2227ec4..c016693 100644
--- a/fm.h
+++ b/fm.h
@@ -959,6 +959,7 @@ global int emacs_like_lineedit init(FALSE);
 global int vi_prec_num init(FALSE);
 global int label_topline init(FALSE);
 global int nextpage_topline init(FALSE);
+global char *Session init(NULL);
 global char *displayTitleTerm init(NULL);
 global int displayLink init(FALSE);
 global int displayLinkNumber init(FALSE);
diff --git a/history.c b/history.c
index f2a00b4..471059e 100644
--- a/history.c
+++ b/history.c
@@ -1,5 +1,6 @@
 /* $Id: history.c,v 1.11 2003/09/26 17:59:51 ukai Exp $ */
 #include "fm.h"
+#include <errno.h>
 
 #ifdef USE_HISTORY
 Buffer *
@@ -36,11 +37,21 @@ loadHistory(Hist *hist)
 {
     FILE *f;
     Str line;
+#define FNAMELEN 255
+    char fname[FNAMELEN+1] = HISTORY_FILE;
+
 
     if (hist == NULL)
 	return;
-    if ((f = fopen(rcFile(HISTORY_FILE), "rt")) == NULL)
+    if (Session) {
+        strncat(fname, ".", FNAMELEN -6 - strlen(fname));
+        strncat(fname, Session, FNAMELEN -6 - strlen(fname));
+    }
+    if ((f = fopen(rcFile(fname), "rt")) == NULL) {
+	if (errno != ENOENT)
+	    perror("error reading history file");
 	return;
+    }
 
     while (!feof(f)) {
 	line = Strfgets(f);
@@ -61,6 +72,8 @@ saveHistory(Hist *hist, size_t size)
     HistItem *item;
     char *tmpf;
     int rename_ret;
+#define FNAMELEN 255
+    char fname[FNAMELEN+1] = HISTORY_FILE;
 
     if (hist == NULL || hist->list == NULL)
 	return;
@@ -80,7 +93,12 @@ saveHistory(Hist *hist, size_t size)
 	disp_err_message("Can't save history", FALSE);
 	return;
     }
-    rename_ret = rename(tmpf, rcFile(HISTORY_FILE));
+
+    if (Session) {
+       strncat(fname, ".", FNAMELEN -6 - strlen(fname));
+       strncat(fname, Session, FNAMELEN -6 - strlen(fname));
+    }
+    rename_ret = rename(tmpf, rcFile(fname));
     if (rename_ret != 0) {
 	disp_err_message("Can't save history", FALSE);
 	return;
diff --git a/main.c b/main.c
index 85b0003..fdc5429 100644
--- a/main.c
+++ b/main.c
@@ -7,6 +7,7 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <errno.h>
 #include <fcntl.h>
 #if defined(HAVE_WAITPID) || defined(HAVE_WAIT3)
 #include <sys/wait.h>
@@ -242,6 +243,7 @@ fusage(FILE * f, int err)
     fprintf(f, "    -header string   insert string as a header\n");
     fprintf(f, "    +<num>           goto <num> line\n");
     fprintf(f, "    -num             show line number\n");
+    fprintf(f, "    -session=<id>    use session <id>\n");
     fprintf(f, "    -no-proxy        don't use proxy\n");
 #ifdef INET6
     fprintf(f, "    -4               IPv4 only (-o dns_order=4)\n");
@@ -283,6 +285,8 @@ static char *getCodePage(void);
 #endif
 #endif
 
+char *loadBufferInfo(void);
+
 static GC_warn_proc orig_GC_warn_proc = NULL;
 #define GC_WARN_KEEP_MAX (20)
 
@@ -750,6 +754,8 @@ main(int argc, char **argv, char **envp)
 		squeezeBlankLine = TRUE;
 	    else if (!strcmp("-X", argv[i]))
 		Do_not_use_ti_te = TRUE;
+	    else if (!strncmp("-session=", argv[i], 9))
+		Session = argv[i] + 9;
 	    else if (!strcmp("-title", argv[i]))
 		displayTitleTerm = getenv("TERM");
 	    else if (!strncmp("-title=", argv[i], 7))
@@ -800,6 +806,22 @@ main(int argc, char **argv, char **envp)
 	i++;
     }
 
+    /* if last session has been saved, get last URL */
+    {
+	char * str;	/* we blantantly skip the release of this memory --
+			   this seems to be the way to do things in w3m anyway
+			   ...*/
+	if (Session && (str = loadBufferInfo()) != NULL ) {
+	    /* The URL from last session overrides the URL(s) from the command
+	     * line */
+	    load_argv[0] = str;
+	    load_argc = 1;
+	}
+    }
+#ifdef USE_HISTORY
+    loadHistory(URLHist);
+#endif                         /* not USE_HISTORY */
+
 #ifdef	__WATT32__
     if (w3m_debug)
 	dbug_init();
@@ -1478,14 +1500,54 @@ tmpClearBuffer(Buffer *buf)
 static Str currentURL(void);
 
 #ifdef USE_BUFINFO
+char *
+loadBufferInfo()
+{
+    FILE *fp;
+    Str line;
+    char *str;
+#define FNAMELEN 255
+    char fname[FNAMELEN+1] = "bufinfo";
+
+    if (Session) {
+        strncat(fname, ".", FNAMELEN -6 - strlen(fname));
+        strncat(fname, Session, FNAMELEN -6 - strlen(fname));
+    }
+    if ((fp = fopen(rcFile(fname), "r")) == NULL) {
+	if (errno != ENOENT)
+	    perror("error reading bufinfo file");
+	return NULL;
+    }
+    line = Strfgets(fp);
+    Strchop(line);
+    Strremovefirstspaces(line);
+    Strremovetrailingspaces(line);
+    fclose(fp);
+    if (line->length == 0) {
+	str=NULL;
+    } else {
+	str=allocStr(line->ptr, -1);
+    }
+    Strclear(line);
+    Strfree(line);
+    return str;
+}
+
 void
 saveBufferInfo()
 {
     FILE *fp;
+#define FNAMELEN 255
+    char fname[FNAMELEN+1] = "bufinfo";
 
     if (w3m_dump)
 	return;
-    if ((fp = fopen(rcFile("bufinfo"), "w")) == NULL) {
+    if (Session) {
+        strncat(fname, ".", FNAMELEN -6 - strlen(fname));
+        strncat(fname, Session, FNAMELEN -6 - strlen(fname));
+    }
+    if ((fp = fopen(rcFile(fname), "w")) == NULL) {
+	perror("error writing bufinfo file");
 	return;
     }
     fprintf(fp, "%s\n", currentURL()->ptr);
-- 
2.6.6

