summaryrefslogtreecommitdiff
path: root/src/config.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.c')
-rw-r--r--src/config.c222
1 files changed, 222 insertions, 0 deletions
diff --git a/src/config.c b/src/config.c
new file mode 100644
index 0000000..6d05005
--- /dev/null
+++ b/src/config.c
@@ -0,0 +1,222 @@
+/*
+
+ atarisio - A UNIX backend for an Atari SIO2PC lead.
+
+ Copyright (C) 2004 Ian Cowburn (ianc@noddybox.demon.co.uk)
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+ -------------------------------------------------------------------------
+
+ Config file
+
+*/
+static const char ident[]="$Id$";
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include "config.h"
+#include "token.h"
+
+static const char ident_h[]=ATARIOSIO_CONFIG_H;
+
+#ifndef TRUE
+#define TRUE 1
+#endif
+
+#ifndef FALSE
+#define FALSE 0
+#endif
+
+
+/* ---------------------------------------- CONFIG
+*/
+static char *serial_path=NULL;
+static char *prompt=NULL;
+
+static const struct
+{
+ const char *name;
+ void *var;
+ int is_int;
+} config[]= {
+ {"device", &serial_path, FALSE},
+ {"prompt", &prompt, FALSE},
+ {NULL, NULL, FALSE}
+ };
+
+
+/* ---------------------------------------- PRIVATE FUNCTIONS
+*/
+static void Parse(FILE *fp)
+{
+ char buff[1024];
+
+ while(fgets(buff,sizeof buff,fp))
+ {
+ size_t l;
+
+ l=strlen(buff);
+
+ if (buff[l-1]=='\n')
+ buff[--l]=0;
+
+ if (l>0 && buff[0]!='#')
+ TokenRun(buff);
+ }
+}
+
+
+static void Set(int argc, char *argv[])
+{
+ int f;
+
+ for(f=0;config[f].name;f++)
+ {
+ if (strcmp(config[f].name,argv[1])==0)
+ {
+ if (config[f].is_int)
+ {
+ int *i;
+
+ i=config[f].var;
+ *i=atoi(argv[2]);
+ }
+ else
+ {
+ char **p;
+
+ p=config[f].var;
+
+ if (*p)
+ free(*p);
+
+ *p=StrCopy(argv[2]);
+ }
+ }
+ }
+}
+
+
+static void Show(int argc, char *argv[])
+{
+ int f;
+
+ if (argc==2)
+ {
+ for(f=0;config[f].name;f++)
+ {
+ if (strcmp(config[f].name,argv[1])==0)
+ {
+ if (config[f].is_int)
+ {
+ int *i;
+
+ i=config[f].var;
+ printf("%s = %d\n",argv[1],*i);
+ }
+ else
+ {
+ char **p;
+
+ p=config[f].var;
+ printf("%s = %s\n",argv[1],*p ? *p:"undefined");
+ }
+ }
+ }
+ }
+ else
+ {
+ for(f=0;config[f].name;f++)
+ {
+ printf("%s = ",config[f].name);
+
+ if (config[f].is_int)
+ {
+ int *i;
+
+ i=config[f].var;
+ printf("%d\n",*i);
+ }
+ else
+ {
+ char **p;
+
+ p=config[f].var;
+ printf("%s\n",*p ? *p:"undefined");
+ }
+ }
+ }
+}
+
+
+/* ---------------------------------------- EXPORTED INTERFACES
+*/
+int ConfigRead(void)
+{
+ static Command cmd[2]=
+ {
+ {"set",3,3,"set variable value","Set a variable",Set},
+ {"show",1,2,"show [variable]","Show variables",Show}
+ };
+
+ FILE *fp;
+ char path[FILENAME_MAX]={0};
+
+ serial_path=StrCopy("/dev/cuaa0");
+ prompt=StrCopy("SIO% ");
+
+ if (getenv("HOME"))
+ strcpy(path,getenv("HOME"));
+
+ strcat(path,"/.siorc");
+
+ if (!(fp=fopen(path,"r")))
+ return FALSE;
+
+ TokenRegister(2,cmd);
+
+ Parse(fp);
+ fclose(fp);
+
+ return TRUE;
+}
+
+
+int IConfig(IConfigVar v)
+{
+ static const int *vars[]=
+ {
+ NULL
+ };
+
+ return *vars[v];
+}
+
+
+const char *SConfig(SConfigVar v)
+{
+ static char **vars[]=
+ {
+ &serial_path,
+ &prompt
+ };
+
+ return (const char *)*vars[v];
+}
+
+
+/* END OF FILE */