]> skyeroc.xyz Git - dgamelaunch/commitdiff
userprefs - read and write prefs
authorTangles <andyrthomson@gmail.com>
Tue, 21 Jan 2020 04:25:50 +0000 (15:25 +1100)
committerTangles <andyrthomson@gmail.com>
Tue, 21 Jan 2020 04:25:50 +0000 (15:25 +1100)
config.l
dgamelaunch.c
dgamelaunch.h
dgl-common.c

index db3ff1cef575a1d29906b80540189d0f0570f65a..81a188cb4fec0a44b80e5a54d1b5cd550c6f9711 100644 (file)
--- a/config.l
+++ b/config.l
@@ -119,6 +119,9 @@ setenv              { yylval.i = DGLCMD_SETENV; return TYPE_DGLCMD2; }
 exec           { yylval.i = DGLCMD_EXEC;   return TYPE_DGLCMD2; }
 chpasswd       { yylval.i = DGLCMD_CHPASSWD;   return TYPE_DGLCMD0; }
 chmail         { yylval.i = DGLCMD_CHMAIL;     return TYPE_DGLCMD0; }
+setprefpath    { yylval.i = DGLCMD_SETPREFPATH; return TYPE_DGLCMD1; }
+readprefs      { yylval.i = DGLCMD_READPREFS;  return TYPE_DGLCMD0; }
+writeprefs     { yylval.i = DGLCMD_WRITEPREFS; return TYPE_DGLCMD0; }
 watch_menu     { yylval.i = DGLCMD_WATCH_MENU; return TYPE_DGLCMD0; }
 ask_login      { yylval.i = DGLCMD_LOGIN;      return TYPE_DGLCMD0; }
 ask_register   { yylval.i = DGLCMD_REGISTER;   return TYPE_DGLCMD0; }
index 751929abbb0992b1976d2a832c77a5c4621326f1..9c381c80edb5b37a9d832553980574be6499392b 100644 (file)
@@ -104,6 +104,13 @@ int  showplayers = 0;
 int  initplayer = 0;
 void (*g_chain_winch)(int);
 
+struct userpref {
+   char *name;
+   char *value;
+   struct userpref *npref;
+};
+struct userpref *userprefs = NULL;
+
 #ifndef USE_SQLITE3
 int f_num = 0;
 struct dg_user **users = NULL;
@@ -168,7 +175,7 @@ mysetenv (const char* name, const char* value, int overwrite)
 {
   int retval;
   char *buf = NULL;
-  
+
   if (getenv(name) == NULL || overwrite)
   {
     size_t len = strlen(name) + 1 + strlen(value) + 1; /* NAME=VALUE\0 */
@@ -178,8 +185,8 @@ mysetenv (const char* name, const char* value, int overwrite)
   }
   else
     retval = -1;
-  
-  return retval;  
+
+  return retval;
 }
 #else /* use native setenv */
 # define mysetenv setenv
@@ -1752,7 +1759,7 @@ domailuser (char *username)
 
   fprintf (user_spool, "%s:%s\n", me->username, message);
 
-  /* 
+  /*
    * Don't unlock the file ourselves, this way it will be done automatically
    * after all data has been written. (Using file locking with stdio is icky.)
    */
@@ -1928,7 +1935,7 @@ loginprompt (int from_ttyplay)
          setproctitle("%s", me->username);
       dgl_exec_cmdqueue(globalconfig.cmdqueue[DGLTIME_LOGIN], 0, me);
     }
-  else 
+  else
   {
     me = NULL;
     if (from_ttyplay == 1)
@@ -1937,7 +1944,7 @@ loginprompt (int from_ttyplay)
       refresh();
       sleep(2);
     }
-  } 
+  }
 }
 
 /* ************************************************************* */
@@ -2071,7 +2078,7 @@ newuser ()
         error = 0;
       else
         error = 1;
+
       if (*buf == '\0')
       {
         free (me->username);
@@ -2110,6 +2117,92 @@ passwordgood (char *cpw)
 
 /* ************************************************************* */
 
+void
+freeprefs()
+{
+    struct userpref *cpref;
+    for (cpref = userprefs; cpref; ) {
+        cpref = cpref->npref;
+        free(userprefs->name);
+        free(userprefs->value);
+        free(userprefs);
+        userprefs = cpref;
+    }
+}
+
+int
+readprefs ()
+{
+    FILE *upf;
+    struct userpref **cpref = &userprefs;
+    int prefcount = 0;
+    char buf[256];
+    char *p;
+    char *nameptr;
+    char *valptr;
+
+    freeprefs();
+    upf = fopen(userpref_path, "r");
+    if (!upf) return -1;
+    while (fgets(buf, 256, upf)) {
+        /* first token is 'name' - must start with letter */
+        /* skip over any leading non-letter garbage */
+        for (p = buf; (*p < 'a' || *p > 'z') && (*p < 'A' || *p > 'Z'); p++)
+            if (p >= buf + 256) {
+                fclose(upf);
+                return prefcount;
+            }
+        nameptr = p;
+        /* rest of name can be alphanum */
+        for ( ; (*p >= 'a' && *p <= 'z')
+             || (*p >= 'A' && *p <= 'Z')
+             || (*p >= '0' && *p <= '9'); p++)
+            if (p >= buf + 256) {
+                fclose(upf);
+                return prefcount;
+            }
+        /* first non-alphanum is delimiter (it should be a space) */
+        *p++ = 0;
+        /* skip extra whitespace */
+        for (; *p == ' ' || *p == '\t'; p++)
+            if (p >= buf + 256) {
+                fclose(upf);
+                return prefcount;
+            }
+        valptr = p;
+        /* can probably acccept anything that's not a newline here */
+        for (; *p; p++)
+            if (p >= buf + 256 || *p == '\n') {
+                *p = 0;
+                break;
+            }
+        *cpref = (struct userpref *)malloc(sizeof(struct userpref));
+        (*cpref)->name = strdup(nameptr);
+        (*cpref)->value = strdup(valptr);
+        cpref = &(*cpref)->npref;
+        prefcount++;
+    }
+    fclose(upf);
+    return prefcount;
+}
+
+int
+writeprefs ()
+{
+    struct userpref *cpref;
+    int prefcount = 0;
+    FILE *upf = fopen (userpref_path, "w");
+    if (!upf) return -1;
+    for (cpref = userprefs; cpref; cpref = cpref->npref) {
+        fprintf (upf, "%s %s\n", cpref->name, cpref->value);
+        prefcount++;
+    }
+    fclose(upf);
+    return prefcount;
+}
+
+/* ************************************************************* */
+
 int
 readfile (int nolock)
 {
@@ -2738,7 +2831,7 @@ main (int argc, char** argv)
   for (i = 0; i < argc; i++)
     saved_argv[i] = strdup(argv[i]);
   saved_argv[i] = '\0';
-  
+
   compat_init_setproctitle(argc, argv);
   argv = saved_argv;
 #endif
index c7ba13452c29bef51f7780e9160a802fb8c42a6d..a45cd85d392169db252b66065e13734d4cb25295 100644 (file)
@@ -67,6 +67,9 @@ typedef enum
     DGLCMD_UNLINK,     /* unlink foo */
     DGLCMD_EXEC,       /* exec foo bar */
     DGLCMD_SETENV,     /* setenv foo bar */
+    DGLCMD_SETPREFPATH,        /* setprefpath foo */
+    DGLCMD_READPREFS,  /* readprefs */
+    DGLCMD_WRITEPREFS, /* writeprefs */
     DGLCMD_WATCH_MENU,  /* watch_menu */
     DGLCMD_LOGIN,       /* ask_login */
     DGLCMD_REGISTER,   /* ask_register */
@@ -285,6 +288,7 @@ extern int dgl_local_COLS;
 extern int dgl_local_LINES;
 
 extern char last_ttyrec[512];
+extern char *userpref_path;
 
 /* dgamelaunch.c */
 extern void create_config(void);
@@ -351,6 +355,8 @@ extern void write_canned_rcfile(int game, char *target);
 extern void writefile(int requirenew);
 extern void graceful_exit(int status);
 extern int purge_stale_locks(int game);
+extern int readprefs(void);
+extern int writeprefs(void);
 /*extern int menuloop(void);*/
 extern void ttyrec_getpty(void);
 #ifndef HAVE_SETENV
index e02c00b3e4d67eda4d95ef9ed6563c361306d621..f81bddafe5b9522859a2065d6c5f9daa31851779 100644 (file)
@@ -58,6 +58,7 @@ int curses_resize = 0;
 
 int selected_game = 0;
 int return_from_submenu = 0;
+char *userpref_path = NULL;
 
 mode_t default_fmode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
 
@@ -353,6 +354,18 @@ dgl_exec_cmdqueue_w(struct dg_cmdpart *queue, int game, struct dg_user *me, char
        case DGLCMD_CHMAIL:
            if (loggedin) change_email();
            break;
+        case DGLCMD_SETPREFPATH:
+            if (loggedin && p1) {
+                free(userpref_path);
+                userpref_path = strdup(dgl_format_str(NULL, me, p1, playername));
+            }
+            break;
+        case DGLCMD_READPREFS:
+            if (loggedin && userpref_path) readprefs();
+           break;
+        case DGLCMD_WRITEPREFS:
+            if (loggedin && userpref_path) writeprefs();
+           break;
        case DGLCMD_WATCH_MENU:
            inprogressmenu(-1);
            break;