# HG changeset patch # User Michiel Broek # Date 1398255909 -7200 # Node ID d74b26b2f2175d772f88b85caef52f437e005d15 # Parent 9db76e20e21efc626fd7b08a3cd9b62a136d793e Added library diff -r 9db76e20e21e -r d74b26b2f217 .hgignore --- a/.hgignore Tue Apr 22 23:28:17 2014 +0200 +++ b/.hgignore Wed Apr 23 14:25:09 2014 +0200 @@ -10,3 +10,4 @@ syntax: glob *.o */filelist +lib/*.a diff -r 9db76e20e21e -r d74b26b2f217 Makefile.global.in --- a/Makefile.global.in Tue Apr 22 23:28:17 2014 +0200 +++ b/Makefile.global.in Wed Apr 23 14:25:09 2014 +0200 @@ -19,7 +19,7 @@ TAR = @TAR@ RANLIB = @RANLIB@ -CFLAGS = @CFLAGS@ +CFLAGS = @CFLAGS@ -DETCDIR=@prefix@/etc -DVARDIR=@prefix@/var CXXFLAGS = @CXXFLAGS@ LIBS = @LIBS@ diff -r 9db76e20e21e -r d74b26b2f217 config.h.in --- a/config.h.in Tue Apr 22 23:28:17 2014 +0200 +++ b/config.h.in Wed Apr 23 14:25:09 2014 +0200 @@ -9,4 +9,3 @@ /* According to Sun we MUST define this in the source */ #define _REENTRANT 1 - diff -r 9db76e20e21e -r d74b26b2f217 configure --- a/configure Tue Apr 22 23:28:17 2014 +0200 +++ b/configure Wed Apr 23 14:25:09 2014 +0200 @@ -1874,7 +1874,7 @@ ac_config_headers="$ac_config_headers config.h" -SUBDIRS="thermometers" +SUBDIRS="lib thermometers" PACKAGE="mbsePi-apps" @@ -3354,7 +3354,6 @@ - ac_config_files="$ac_config_files Makefile.global" cat >confcache <<\_ACEOF diff -r 9db76e20e21e -r d74b26b2f217 configure.ac --- a/configure.ac Tue Apr 22 23:28:17 2014 +0200 +++ b/configure.ac Wed Apr 23 14:25:09 2014 +0200 @@ -2,7 +2,7 @@ AC_INIT(thermometers/main.c) AM_CONFIG_HEADER(config.h) -SUBDIRS="thermometers" +SUBDIRS="lib thermometers" AC_SUBST(SUBDIRS) dnl General settings @@ -61,7 +61,6 @@ AC_SUBST(CXXFLAGS) - AC_OUTPUT( Makefile.global ) diff -r 9db76e20e21e -r d74b26b2f217 lib/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/Makefile Wed Apr 23 14:25:09 2014 +0200 @@ -0,0 +1,49 @@ +# Makefile for the library files. + +include ../Makefile.global + +SRCS = xutil.c rdconfig.c +OBJS = xutil.o rdconfig.o +HDRS = mbselib.h +TARGET = libmbse.a + +############################################################################# + +.c.o: + ${CC} ${CFLAGS} ${INCLUDES} ${DEFINES} -c $< + +all: ${TARGET} + +libmbse.a: ${OBJS} + ar r $@ $? + ${RANLIB} $@ + +clean: + rm -f ${TARGET} *.o *.h~ *.c~ core filelist Makefile.bak + +depend: + @rm -f Makefile.bak; \ + mv Makefile Makefile.bak; \ + sed -e '/^# DO NOT DELETE/,$$d' Makefile.bak >Makefile; \ + ${ECHO} '# DO NOT DELETE THIS LINE - MAKE DEPEND RELIES ON IT' >>Makefile; \ + ${ECHO} '# Dependencies generated by make depend' >>Makefile; \ + for f in ${SRCS}; \ + do \ + ${ECHO} "Dependencies for $$f:\c"; \ + ${ECHO} "`basename $$f .c`.o:\c" >>Makefile; \ + for h in `sed -n -e \ + 's/^#[ ]*include[ ]*"\([^"]*\)".*/\1/p' $$f`; \ + do \ + ${ECHO} " $$h\c"; \ + ${ECHO} " $$h\c" >>Makefile; \ + done; \ + ${ECHO} " done."; \ + ${ECHO} "" >>Makefile; \ + done; \ + ${ECHO} '# End of generated dependencies' >>Makefile + +# DO NOT DELETE THIS LINE - MAKE DEPEND RELIES ON IT +# Dependencies generated by make depend +xutil.o: ../config.h mbselib.h +rdconfig.o: ../config.h mbselib.h +# End of generated dependencies diff -r 9db76e20e21e -r d74b26b2f217 lib/mbselib.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/mbselib.h Wed Apr 23 14:25:09 2014 +0200 @@ -0,0 +1,43 @@ +#ifndef _MBSELIB_H +#define _MBSELIB_H + +#include "../config.h" + +#include +#include +#include +#include +#include + +#define TRUE 1 +#define FALSE 0 + +/* rdconfig.c */ +#define S(x) (x)?(x):"(null)" + +typedef struct _key_list { + char *key; + int (*prc)(char **); + char **dest; +} key_list; + + +typedef struct _sys_config { + char *name; /* Configuration name */ + char *mosq_server; /* mosquitto server hostname */ + int mosq_port; /* mosquitto server port */ +} sys_config; + + +void killconfig(void); +int rdconfig(char *); +int wrconfig(char *); + + +/* xutil.c */ +char *xmalloc(size_t); +char *xstrcpy(char *); +char *xstrcat(char *, char *); + +#endif + diff -r 9db76e20e21e -r d74b26b2f217 lib/rdconfig.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/rdconfig.c Wed Apr 23 14:25:09 2014 +0200 @@ -0,0 +1,231 @@ +/***************************************************************************** + * Copyright (C) 2008-2014 + * + * Michiel Broek + * + * This file is part of the mbsePi-apps + * + * This 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, or (at your option) any + * later version. + * + * mbsePi-apps 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 EC-65K; see the file COPYING. If not, write to the Free + * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + *****************************************************************************/ + +#include "../config.h" +#include "mbselib.h" + +// #define DEBUG_CONFIG 1 + + +extern int DebugEmu; +extern char *Private_Path; + +static char *mypath; +static char *k, *v; +static int linecnt = 0; + +extern int MustSave; + + +sys_config Config; /* System configuration */ + + + +static int getstr(char **); +static int getint(char **); +//static int getbyt(char **); +//static int gethex(char **); + + + +/* + * System configuration table + */ +key_list keytab[] = { + {(char *)"name", getstr, &Config.name}, + {(char *)"mosq_server", getstr, &Config.mosq_server}, + {(char *)"mosq_port", getint, (char **)&Config.mosq_port}, + {NULL, NULL, NULL} +}; + + + +void killconfig(void) +{ + if (Config.name) + free(Config.name); + Config.name = NULL; + +} + + + +int wrconfig(char *machine) +{ + FILE *fp; + + /* + * Get config from the system path + */ + mypath = xstrcpy(Private_Path); + mypath = xstrcat(mypath, (char *)"/data/machines/"); + mypath = xstrcat(mypath, machine); + mypath = xstrcat(mypath, (char *)".conf"); + + if ((fp = fopen(mypath, "w")) == NULL) { +// Log_Msg("[rdconfig] could not rewrite %s", mypath); + return 1; + } + + fprintf(fp, "# Configuration file for EC65K %s\n", VERSION); + fprintf(fp, "# Machine: `%s'\n", machine); + fprintf(fp, "#\n"); +// fprintf(fp, "description %s\n", Config.description); +// fprintf(fp, "\n"); +// fprintf(fp, "# 65(C)02 CPU\n"); +// fprintf(fp, "#\n"); + + fprintf(fp, "# End of generated configuration\n"); + fclose(fp); + free(mypath); + mypath = NULL; + MustSave = FALSE; + + return 0; +} + + + +int rdconfig(char *machine) +{ + char buf[256], *p; + FILE *fp; + int i, rc = 0; + + killconfig(); + + /* + * Get config from the system path + */ + mypath = xstrcpy(Private_Path); + mypath = xstrcat(mypath, (char *)"/data/machines/"); + mypath = xstrcat(mypath, machine); + mypath = xstrcat(mypath, (char *)".conf"); + + if ((fp = fopen(mypath, "r")) == NULL) { +// Log_Msg("[rdconfig] could not open %s", mypath); + return 1; + } + + linecnt = 0; + while (fgets(buf, sizeof(buf) -1, fp)) { + linecnt++; + if (*(p = buf + strlen(buf) -1) != '\n') { +// Log_Msg("[rdconfig] %s(%d): \"%s\" - line too long", mypath, linecnt, buf); + rc = 1; + break; + } + *p-- = '\0'; + while ((p >= buf) && isspace(*p)) + *p-- = '\0'; + k = buf; + while (*k && isspace(*k)) + k++; + p = k; + while (*p && !isspace(*p)) + p++; + *p++='\0'; + v = p; + while (*v && isspace(*v)) + v++; + + if ((*k == '\0') || (*k == '#')) { + continue; + } + + for (i = 0; keytab[i].key; i++) + if (strcasecmp(k,keytab[i].key) == 0) + break; + + if (keytab[i].key == NULL) { +// Log_Msg("[rdconfig] %s(%d): %s %s - unknown keyword", mypath, linecnt, S(k), S(v)); + rc = 1; + break; + } else if ((keytab[i].prc(keytab[i].dest))) { + rc = 1; + break; + } + + } + fclose(fp); + + free(mypath); + mypath = NULL; + MustSave = FALSE; + + return rc; +} + + + +static int getstr(char **dest) +{ +// Log_Msg("[rdconfig] getstr: %s(%d): %s %s", mypath, linecnt, S(k), S(v)); + *dest = xstrcpy(v); + return 0; +} + + + +static int getint(char **dest) +{ +// Log_Msg("[rdconfig] getint: %s(%d): %s %s", mypath, linecnt, k, v); +// if (strspn(v,"0123456789") != strlen(v)) +// Log_Msg("[rdconfig] %s(%d): %s %s - bad numeric", mypath, linecnt, S(k), S(v)); +// else + *((int*)dest)=atoi(v); + return 0; +} + + +/* +static int getbyt(char **dest) +{ + Log_Msg("[rdconfig] getbyt: %s(%d): %s %s", mypath, linecnt, k, v); + if (strspn(v,"0123456789") != strlen(v)) + Log_Msg("[rdconfig] %s(%d): %s %s - bad numeric", mypath, linecnt, S(k), S(v)); + else + *((Uint8*)dest)=atoi(v); + return 0; +} +*/ + + +/* +static int gethex(char **dest) +{ + unsigned int val = 0; + int rc; + + Log_Msg("[rdconfig] gethex: %s(%d): %s %s", mypath, linecnt, k, v); + rc = sscanf(v, "%08x", &val); + if (rc != 1) { + Log_Msg("[rdconfig] %s(%d): %s %s - bad hex value", mypath, linecnt, S(k), S(v)); + return 1; + } + *((int*)dest) = val; + + return 0; +} +*/ + + diff -r 9db76e20e21e -r d74b26b2f217 lib/xutil.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/xutil.c Wed Apr 23 14:25:09 2014 +0200 @@ -0,0 +1,73 @@ +/***************************************************************************** + * Copyright (C) 2008-2014 + * + * Michiel Broek + * + * This file is part of the mbsePi-apps + * + * This 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, or (at your option) any + * later version. + * + * mbsePi-apps 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 EC-65K; see the file COPYING. If not, write to the Free + * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + *****************************************************************************/ + +#include "../config.h" +#include "mbselib.h" + + +char *xmalloc(size_t size) +{ + char *tmp; + + tmp = malloc(size); + if (!tmp) + abort(); + + return tmp; +} + + + +char *xstrcpy(char *src) +{ + char *tmp; + + if (src == NULL) + return(NULL); + tmp = xmalloc(strlen(src)+1); + strcpy(tmp, src); + return tmp; +} + + + +char *xstrcat(char *src, char *add) +{ + char *tmp; + size_t size = 0; + + if ((add == NULL) || (strlen(add) == 0)) + return src; + if (src) + size = strlen(src); + size += strlen(add); + tmp = xmalloc(size + 1); + *tmp = '\0'; + if (src) { + strcpy(tmp, src); + free(src); + } + strcat(tmp, add); + return tmp; +} + + diff -r 9db76e20e21e -r d74b26b2f217 thermometers/main.c --- a/thermometers/main.c Tue Apr 22 23:28:17 2014 +0200 +++ b/thermometers/main.c Wed Apr 23 14:25:09 2014 +0200 @@ -113,22 +113,13 @@ fprintf(stdout, (char *)"my_publish_callback mid=%d\n", mid); last_mid_sent = mid; -// if(mode == MSGMODE_STDIN_LINE){ -// if(mid == last_mid){ -// mosquitto_disconnect(mosq); -// disconnect_sent = true; -// } -// }else if(disconnect_sent == false){ -// mosquitto_disconnect(mosq); -// disconnect_sent = true; -// } } void my_log_callback(struct mosquitto *mosq, void *obj, int level, const char *str) { - printf("%s\n", str); + printf("log: %s\n", str); }