--- /dev/null
+# $Id$
+#
+# $Log$
+# Revision 1.1.1.1 1999/05/26 13:06:26 short
+# First alpha release.
+#
+
+dnl Process this file with autoconf to produce a configure script.
+AC_INIT(mdsms.c)
+AM_INIT_AUTOMAKE(mdsms, 1.0)
+AM_CONFIG_HEADER(config.h)
+AM_MAINTAINER_MODE
+
+dnl Configuration switches.
+
+AC_MSG_CHECKING([whether debugging is requested])
+AC_ARG_ENABLE(debug,
+[ --enable-debug enable debugging output to stderr],
+ enable_debug=$enableval, enable_debug=no)
+AC_MSG_RESULT($enable_debug)
+
+AC_MSG_CHECKING([whether included getopt is requested])
+AC_ARG_WITH(included-getopt,
+[ --with-included-getopt use included getopt(3)],
+ with_getopt=$withval, with_getopt=no)
+AC_MSG_RESULT($with_getopt)
+
+AC_MSG_CHECKING([for user-specified lock directory])
+AC_ARG_WITH(lock-directory,
+[ --with-lock-directory override default search for modem lock directory],
+ with_lock_directory=$withval, with_lock_directory=)
+AC_MSG_RESULT($with_lock_directory)
+
+dnl Checks for programs.
+AC_PROG_CC
+if test "$USE_MAINTAINER_MODE" = yes;then
+ if test "$GCC" = yes;then
+ CFLAGS="-Wall -ansi -pedantic -ggdb"
+ LDFLAGS="$LDFLAGS -lefence"
+ fi
+else
+ LDFLAGS="-s"
+ AC_DEFINE(NDEBUG)
+ fi
+
+AC_PROG_CPP
+
+dnl Checks for pathnames.
+
+AC_MSG_CHECKING([for modem lock directory])
+dirx=""
+if test "$with_lock_directory" = no;then
+ AC_MSG_RESULT([user disabled])
+else
+ if test yes = "$with_lock_directory" \
+ -o -z "$with_lock_directory";then
+ for dira in /usr /var ;do
+ for dirb in /spool "" ;do
+ for dirc in /uucp /locks /lock;do
+ dir="${dira}${dirb}${dirc}"
+ if test -d "$dir";then dirx="$dir";fi
+ done;done;done
+ if test -z "$dirx";then
+ AC_MSG_RESULT([none found, disabled])
+ else
+ AC_MSG_RESULT([$dirx])
+ fi
+ else
+ dirx="$with_lock_directory"
+ if test -d "$dirx";then AC_MSG_RESULT([$dirx])
+ else AC_MSG_RESULT([warning - directory doesn't exist: $dirx])
+ fi
+ fi
+fi
+if test -z "$dirx";then AC_DEFINE(DEF_LOCKFILE,[""])
+else AC_DEFINE_UNQUOTED(DEF_LOCKFILE,"${dirx}/LCK..%s")
+fi
+
+dnl Checks for libraries.
+
+dnl Checks for header files.
+AC_HEADER_STDC
+
+dnl Checks for typedefs, structures, and compiler characteristics.
+
+AC_C_CONST
+AC_C_INLINE
+
+dnl Checks for library functions.
+
+if test "$with_getopt" = no;then
+ AC_CHECK_FUNCS(getopt_long)
+ fi
+
+if test "$enable_debug" = yes;then
+ AC_DEFINE(DEBUG)
+ fi
+
+AC_MSG_CHECKING(whether to use included getopt)
+if test "$with_getopt" = yes -o "$ac_cv_func_getopt_long" != yes ;then
+ LIBOBJS="$LIBOBJS getopt1.o getopt.o"
+ AC_MSG_RESULT(yes)
+else AC_MSG_RESULT(no)
+fi
+
+AC_CHECK_FUNC(MAX, AC_DEFINE(HAVE_MAX) ,
+ AC_CHECK_FUNC(max, AC_DEFINE(MAX, max) AC_DEFINE(HAVE_MAX))
+ )
+
+AC_CHECK_FUNC(MIN, AC_DEFINE(HAVE_MIN) ,
+ AC_CHECK_FUNC(min, AC_DEFINE(MIN, min) AC_DEFINE(HAVE_MIN))
+ )
+
+AC_MSG_CHECKING([for LINE_MAX])
+AC_TRY_COMPILE([#include <limits.h>], [LINE_MAX;],
+ AC_DEFINE(HAVE_LINE_MAX) AC_MSG_RESULT(yes),
+ AC_DEFINE(LINE_MAX, 4096) AC_MSG_RESULT(no))
+
+AC_MSG_CHECKING([for CBAUD])
+AC_TRY_COMPILE([#include <termios.h>], [CBAUD;],
+ AC_DEFINE(HAVE_CBAUD) AC_MSG_RESULT(yes),
+ AC_DEFINE(CBAUD, 0) AC_MSG_RESULT(no))
+
+AC_MSG_CHECKING([for CBAUDEX])
+AC_TRY_COMPILE([#include <termios.h>], [CBAUDEX;],
+ AC_DEFINE(HAVE_CBAUDEX) AC_MSG_RESULT(yes),
+ AC_DEFINE(CBAUDEX, 0) AC_MSG_RESULT(no))
+
+AC_MSG_CHECKING([for unused attribute])
+uns=true
+for un in __unused__ unused;do
+ if $uns;then
+ unx="__attribute__ (($un))"
+ AC_TRY_COMPILE([#include <stddef.h>],[char untest $unx;],
+ AC_DEFINE_UNQUOTED(ATTR_UNUSED, $unx)
+ AC_MSG_RESULT($un)
+ uns=false)
+ fi
+ done
+if $uns;then AC_DEFINE(ATTR_UNUSED,) AC_MSG_RESULT(no);fi
+
+AC_CHECK_FUNC(snprintf, AC_DEFINE(HAVE_SNPRINTF))
+AC_CHECK_FUNC(vsnprintf, AC_DEFINE(HAVE_VSNPRINTF))
+
+AC_TYPE_SIGNAL
+AC_TYPE_PID_T
+
+AC_REPLACE_FUNCS(strdup)
+dnl AC_MSG_CHECKING([for strdup])
+dnl AC_TRY_COMPILE([#include <string.h>], [strdup("");],
+dnl AC_DEFINE(HAVE_STRDUP) AC_MSG_RESULT(yes),
+dnl LIBOBJS="$LIBOBJS strdup.o" AC_MSG_RESULT(no))
+
+AC_MSG_CHECKING([for working %m in printf])
+AC_TRY_RUN([int main() { char s[100];
+ sprintf(s,"%m");
+ return(!(strcmp(s,"m") && strcmp(s,"%m")));
+ }],AC_DEFINE(PRINTF_WORKS_PM) AC_MSG_RESULT(yes),AC_MSG_RESULT(no),AC_MSG_RESULT(avoiding))
+
+# Final output.
+
+AC_SUBST(LIBOBJS)
+
+AC_OUTPUT(Makefile)
--- /dev/null
+.TH MDSMS 1 "17 May 1999" ~ VERSION ~ "SendSMS Manual"
+
+.\~ $Id$
+.\~
+.\~ $Log$
+.\~ Revision 1.1.1.1 1999/05/26 13:06:26 short
+.\~ First alpha release.
+.\~
+
+.SH NAME
+mdsms \- send SMSes through MobilDock
+
+.SH SYNOPSIS
+.B mdsms
+.RB [ "-c <cfgfile>" ]
+.RB [ "-d <device>" ]
+.RB [ "-l <lockfile>" ]
+.RB [ "-s <smsc #>" ]
+.RB [ "-t <msec>" ]
+.RB [ "-T <msec>" ]
+.RB [ "\-vhV" ]
+.RB [ "<dest. phone>" ]
+.RB [ "<msg text>" ]
+
+.SH DESCRIPTION
+Program sends one SMS message through MobilDock device.
+
+.SH OPTIONS
+.TP
+.BR -c | --config\ < cfgfile >
+Process recursively this file and read all options from it. See the section
+.B CONFIGURATION
+for more information.
+.TP
+.BR -d | --device\ < device >
+Specify serial device to communicate with MobilDock. If only bare name is
+specified, "\fB/dev/\fP" is prepended automatically. This device name is used
+subsequently with
+.BR -l | --lockfile
+option, see below.
+.TP
+.BR -l | --lockfile\ < lockfile >
+Prior to accessing serial device specified above the lockfile should be
+acquired for correct concurrent processes behaviour. Although this name
+can be given as direct filename more common method is to use pattern
+with embedded "\fB%s\fP" where this mark is replaced by actual basename
+(last component of pathname with all preceding directory names stripped)
+of serial device used.
+Default name for lockfile is ~\fB DEF_LOCKFILE \fP~.
+.TP
+.BR -s | --smsc\ < smsc\ # >
+Specify custom SMS center number. If not specified (or overriden as empty
+string by \fB-s ""\fP)
+.B mdsms
+asks by "\fBAT+CSCA?\fP"
+Siemens A1 for its default SMS center. Situation with undeterminable SMS center
+is unrecoverable and casues immediate fail. It is a common practice to use
+plus sign ("\fB+\fP") to indicate international number type.
+Known SMS centers as of May 1999:
+.RS
+.TP
+.BR "CZ Paegas " ( "230 01" "): " +420603052000
+.TP
+.BR "CZ EuroTel " ( "230 02" "): " +420602909909
+.RE
+.TP
+.BR -t | --chartime\ < msec >
+Although the fixed used baudrate of
+.B 19200
+is pretty low, MobilDock/Siemens A1 couple
+.RB "aren't"
+able to accept steady
+stream of data at this speed. Even the used XON/XOFF handshaking is just not
+enough. The only possible workaround is to slowdown the communication by
+waiting a bit after character sent to give relax time to these devices.
+When full rate communication was used, occasional longer SMS data corruption
+was observed. Argument is given in milliseconds and its default value is
+.BR DEF_CHARTIME ms.
+.TP
+.BR -T | --cmdtime\ < msec >
+This delay is given before sending any
+.B AT-style
+command to MobilDock/Siemens A1.
+Its primary purpose is to let any previous entered commands to finish and
+to clear any input before actually sending our own command. Also all Siemens
+devices are known that they strongly dislike fast edge-to-edge communication
+and to satisfy these requirements this delay was considered as the best
+approach. The default value is
+.BR DEF_CMDTIME ms.
+.TP
+.BR -v | --verbose
+Increase verbosity level by one. Currently the maximum defined level is
+.BR 3 ,
+the default value is
+.BR 0 .
+.TP
+.BR -h | --help
+Give short parameters description to
+.I stderr
+(standard error output stream).
+.TP
+.BR -v | --version
+Print the version number and exit.
+.TP
+.RB < "dest. phone" >
+This mandatory parameter specifies the telephone number of the recipient
+of SMS message. International prefix character plus
+.RB ( + )
+is supported the national mode without plus
+.RB ( + )
+prefix is supported and the meaning is specific to the GSM operator currently
+being roamed in (NOT the native
+.RB "'home'"
+operator of the SIM card!).
+This number can be made default in system configuration files, see below
+for section
+.BR CONFIGURATION .
+.TP
+.RB < "msg text" >
+Here you write the exact body of the message. This parameter should be
+specified only as one component, although if more found they are concatenated
+with separating space (" "). But this practice is discouraged as
+your shell will probably remove any multiple spaces found and also other
+metacharacters may be incorrectly interpreted. To prevent any escaping
+mess, you may prefer to omit this parameter and the the message text is
+then read from
+.I stdin
+(standard input stream).
+
+.SH CONFIGURATION
+.B mdsms
+reads ~\fB CONFIG_MAIN \fP~ followed by ~\fB$HOME CONFIG_HOME \fP~
+configuration files. The content of these files (and also any file read by
+.BR -c | --config
+option)
+has the usual
+.BR getopt (3)
+syntax with dashes. Newlines are taken as whitespace, both double ("\fB~\fP")
+and single ("\fB'\fP")
+quoting is supported. Embedded quote characters can be escaped by backslash
+("\fB\\\fP").
+Arguments are processes in order as specified in configuration files and
+finally the command-line of the program itself, overriding any previous
+values. Although the order of
+.BR -c | --config
+options on one line is preserved the first file is read AFTER all of the
+current options are processed. Recursive use of
+.B -c
+is permitted and the files are read in LIFO (Last-In First-Out, AKA stack)
+order. You should use "\fB-v -v\fP"
+to see details of option processing with more complex configuration setups.
+
+.SH OPERATION
+Upon startup
+.B mdsms
+locks the port (see option
+.BR -l | --lockfile
+for details) and opens the serial device with hardcoded parameters of
+19200 baud, software handshaking (XON/XOFF style), 8 bits, no parity.
+Then issues the following commands:
+.TP
+.BR AT <CTRL-Z>
+.B AT
+is used to fool MobilDock and pass the following
+.B <CTRL-Z>
+(ascii decimal code 26) character to Siemens A1 and break it from eventual
+.B AT+CMGS
+mode in which it may errorneously remain from previous sessions.
+.TP
+.B AT
+Test the responsiveness of Siemens A1.
+.TP
+\fBAT+CSCA="\fPsmc # from user\fB"\fP
+This command is omitted if
+.B "smsc #"
+is not specified by user (or specified/overriden as empty string \fB""\fP
+.TP
+\fBAT+CSCA?\fP
+Query the currently set SMC center number to include it later to the
+header of SMS PDU format where it is required. If \fBAT+CSCA="\fP...\fB"\fP
+was issued before, this number should match it but no sanity checks are
+currently do so.
+.TP
+\fBAT+CMGF=0\fP
+Set the default SMS message format to PDU type. Currently Siemens A1
+.RB "doesn't"
+support any other SMS format anyway.
+.TP
+\fBAT+CMGS=\fP# chars
+This command physically sends the message and the resulting "\fB+CMGS:\fP" output
+is catched and returned as
+.B MR
+(message reference) number to the user.
+.TP
+\fBAT\fP
+Check that Siemens A1 survived the sending of the message.
+
+.SH SEE ALSO
+.TP
+.B GSM 03.40
+ETSI documentation for SMS messages in GSM networks
+.TP
+.B "Developers'" Guide: SMS with the A1
+Tech note on PDU SMS format etc:
+.B http:/ /www.siemens.se/telefoner/ovrigtgsm/fragorsvar/a1_sms.pdf
+.TP
+.B Technical Description of the Siemens A1
+Siemens A1 command description
+.B http:/ /www.siemens.se/telefoner/ovrigtgsm/fragorsvar/a1_manual.pdf
+
+.SH FILES
+.TP
+\fB CONFIG_MAIN \fP
+Main configuration file
+.TP
+\fB$HOME CONFIG_HOME \fP
+User personalized local configuration file
+
+.SH AUTHOR
+.B mdsms
+was written by Jan Kratochvil who should be responsible for all the bugs
+included. Please see the file "\fBAUTHORS\fP"
+shipped with the original distribution archive for more details.
--- /dev/null
+#include "config.h"
+#ifndef lint
+static char rcsid[] ATTR_UNUSED = "$Id$";
+#endif
+
+/*
+ * $Log$
+ * Revision 1.1.1.1 1999/05/26 13:06:26 short
+ * First alpha release.
+ *
+ */
+
+#include "setup.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <limits.h>
+#include <ctype.h>
+#include <string.h>
+#include <termios.h>
+#include <unistd.h>
+#include <assert.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <signal.h>
+
+#ifdef HAVE_GETOPT_LONG
+#include <getopt.h>
+#else
+#include "getopt.h"
+#endif
+
+#if 0
+char *strdup(const char *s);
+int kill(pid_t pid,int sig);
+int snprintf(char *str,size_t n,const char *format,...);
+int vsnprintf(char *str,size_t n,const char *format,va_list ap);
+void usleep(unsigned long usec);
+#endif
+
+#define NELEM(x) (sizeof((x))/sizeof(*(x)))
+
+#ifndef DEBUG
+#define dbg(cmd)
+#else
+#define dbg(cmd) cmd
+#endif
+/* ANSI C does not allow macro with variable arguments */
+#define dO stderr
+#define dB(a) dbg(fprintf a)
+
+#define d1(n1) dB((dO,n1 ))
+#define d2(n1,n2) dB((dO,n1,n2 ))
+#define d3(n1,n2,n3) dB((dO,n1,n2,n3 ))
+#define d4(n1,n2,n3,n4) dB((dO,n1,n2,n3,n4))
+
+static const char version[]="This is MobilDock SMS sender (" PACKAGE " " VERSION ")\n";
+
+static int verbose
+#ifdef DEBUG
+ =0xFFFF
+#endif
+ ;
+static char *pname;
+static int dis_cleanup=0,devfd=-1;
+
+static char *phone,*body,*device,*lockfile,*smsc,*chartime,*cmdtime;
+static int readbody;
+static long chartimen=DEF_CHARTIME,cmdtimen=DEF_CMDTIME;
+static size_t bodylen;
+
+static char *devicename; /* path stripped */
+static char lockreal[512],locked;
+
+static struct termios restios;
+static char restios_yes;
+
+static void error(const char *fmt,...)
+{
+va_list ap;
+char fatal=*fmt;
+#ifndef PRINTF_WORKS_PM
+char pm,*nfmt;
+size_t fmtl;
+#endif
+
+ if (fatal=='!' || fatal=='.' || fatal=='\n') fmt++;
+ else fatal=0;
+
+#ifndef PRINTF_WORKS_PM
+ if (!(nfmt=strdup(fmt))) return;
+ fmtl=strlen(fmt);
+ if ((pm=(fmtl>=2 && !strcmp(fmt+fmtl-2,"%m")))) nfmt[fmtl-2]='\0';
+#endif
+
+ fprintf(stderr,"%s: ",pname);
+ va_start(ap,fmt);
+ vfprintf(stderr,
+#ifdef PRINTF_WORKS_PM
+ fmt
+#else
+ nfmt
+#endif
+ ,ap);
+ va_end(ap);
+
+#ifndef PRINTF_WORKS_PM
+ if (pm) {
+ fputs(strerror(errno),stderr);
+ free(nfmt);
+ }
+#endif
+
+ if (fatal!='\n') fputc((fatal=='.'?'.':'!'),stderr);
+ fputc('\n',stderr);
+ if (fatal=='!') exit(EXIT_FAILURE);
+}
+
+static void chk(void *p)
+{
+ if (p) return;
+ error("!Virtual memory exhausted");
+}
+
+static void cleanup(void)
+{
+ d1("cleanup()\n");
+ if (dis_cleanup) return;
+ if (restios_yes) {
+ if (tcsetattr(devfd,TCSANOW,&restios))
+ error("Error restoring termios for device: %m");
+ restios_yes=0;
+ }
+ if (locked && *lockreal) {
+ d2("Removing lockfile \"%s\"\n",lockreal);
+ if (unlink(lockreal))
+ error("Error removing my device lockfile \"%s\": %m",lockreal);
+ locked=0;
+ }
+ dis_cleanup=1;
+ exit(EXIT_FAILURE);
+}
+
+static void usage(void)
+{
+ fprintf(stderr,"\
+\n\
+%s\
+\n\
+Usage: " PACKAGE " [-c|--config <cfgfile>] [-d|--device <device>]\n\
+ [-l|--lockfile <lock>] [-s|--smsc <smsc #>]\n\
+ [-t|--chartime <msec>] [-T|--cmdtime <msec>]\n\
+ [-f|--file] [-v|--verbose] [-h|--help] [-V|--version]\n\
+ <dest. phone> <msg text|msg filename>\n\
+\n\
+ -c, --config\tRead this additional config file\n\
+\t\t(def. \"" CONFIG_MAIN "\" and \"$HOME" CONFIG_HOME "\")\n\
+ -d, --device\tMobilDock on this serial device (def. \"" DEF_DEVICE "\")\n\
+ -l, --lockfile\tLock serial port by this file, \"%%s\" is basename of device\n\
+\t\t(def. \"%s\")\n\
+ -s, --smsc\tUse this SMS Center number (def. query from Siemens A1)\n\
+ -t, --chartime\tMilliseconds between each char on baud 19200 (def. %dms)\n\
+ -T, --cmdtime\tMilliseconds before each whole AT command (def. %dms)\n\
+ -f, --file\tRead contents of message from file instead\n\
+ -v, --verbose\tIncrease verbosity level, more \"-v\"s give more messages\n\
+ -h, --help\tPrint a summary of the options\n\
+ -V, --version\tPrint the version number\n\
+\n",version,DEF_LOCKFILE,DEF_CHARTIME,DEF_CMDTIME);
+ exit(EXIT_FAILURE);
+}
+
+static const struct option longopts[]={
+{"config" ,1,0,'c'},
+{"device" ,1,0,'d'},
+{"lockfile",1,0,'l'},
+{"smsc" ,1,0,'s'},
+{"chartime",1,0,'t'},
+{"cmdtime" ,1,0,'T'},
+{"file" ,0,0,'f'},
+{"verbose" ,0,0,'v'},
+{"help" ,0,0,'h'},
+{"version" ,0,0,'V'}};
+
+static void processargs(int argp,char **args,const char *from);
+
+static char *cfgstack[MAXCFGLOOP];
+static unsigned cfgstacki=0;
+
+static void chkfclose(FILE *f,const char *fname)
+{
+ if (fclose(f))
+ error("Error closing \"%s\": %m",fname);
+}
+
+static void readfile(const char *fname,char quiet)
+{
+FILE *f;
+size_t got;
+char *args[MAXCFGARGS],*d,*s,blank,quote;
+unsigned argp;
+char *buf;
+long size;
+static unsigned tot=0;
+
+ if (tot++>=MAXCFGNUM) {
+ if (tot==MAXCFGNUM+1) error("Too many config files to read, max is %d, break-out",MAXCFGNUM);
+ return;
+ }
+ if (!(f=fopen(fname,"rt"))) {
+ if (!quiet) error("Can't open config file \"%s\" for r/o: %m",fname);
+ return;
+ }
+ if (verbose>=2) error(".Reading config file \"%s\"",fname);
+ if (fseek(f,0,SEEK_END))
+ error("Error seeking to end of \"s\": %m",fname);
+ if ((size=ftell(f))<0)
+ size=0,error("Error measuring \"%s\": %m",fname);
+ if (size>MAXCONFIG)
+ error("File \"%s\" is too long, read only %ld bytes",fname,MAXCONFIG);
+ chk(buf=malloc((size?size:MAXCONFIG)+1));
+ rewind(f);
+ got=fread(buf,1,(size?size:MAXCONFIG),f);
+ if (size && got!=size)
+ error("File \"%s\" read error, got only %u bytes of %ld",fname,got,size);
+ chkfclose(f,fname);
+ buf[got]='\0';
+ args[0]=pname;
+ for (argp=1,d=s=buf,blank=1,quote=0;s<buf+got;s++) {
+char c=*s;
+
+ if (!quote && isspace(c)) {
+ if (!blank) {
+ *d='\0';
+ blank=1;
+ if (verbose>=2) error("\nConfig \"%s\": arg#%d: %s",fname,argp-1,args[argp-1]);
+ }
+ continue;
+ }
+ if (blank) {
+ if (argp>=NELEM(args)-1) {
+ error("Too many arguments in \"%s\", from offset %d ignored",fname,s-buf);
+ break;
+ }
+ args[argp++]=s;
+ d=s;
+ blank=0;
+ }
+ if (c=='\\') { *d++=*++s; continue; }
+ if (c=='"' || c=='\'') {
+ if (!quote ) { quote=c; continue; }
+ if ( quote==c) { quote=0; continue; }
+ /* FALLTHRU */
+ }
+ *d++=c;
+ }
+ args[argp]=NULL;
+ processargs(argp,args,fname);
+ free(buf);
+}
+
+static struct {
+ const char c;
+ char **const var;
+ unsigned stamp;
+ } optset[]={
+ { 'd',&device },
+ { 'l',&lockfile },
+ { 's',&smsc },
+ { 't',&chartime },
+ { 'T',&cmdtime },
+ };
+
+static void processargs(int argp,char **args,const char *from)
+{
+int optc;
+static unsigned seq=0;
+int i;
+
+ seq++;
+ optarg=NULL; optind=0; /* FIXME: Possible portability problem. */
+ while ((optc=getopt_long(argp,args,"c:d:l:s:t:T:fvhV",longopts,NULL))!=EOF) switch (optc) {
+ case 'c':
+ if (cfgstacki>=NELEM(cfgstack)) {
+ error("Looping (%d) during attempt to read config file \"%s\", break-out",NELEM(cfgstack),optind);
+ break;
+ }
+ chk(cfgstack[cfgstacki++]=strdup(optarg));
+ break;
+ case 'd': case 'l': case 's': case 't': case 'T':
+ for (i=0;i<NELEM(optset);i++)
+ if (optset[i].c==optc) {
+ if (optset[i].stamp && optset[i].stamp!=seq) {
+ assert(!!*optset[i].var);
+ break;
+ }
+ free(*optset[i].var);
+ chk(*optset[i].var=strdup(optarg));
+ optset[i].stamp=seq;
+ break;
+ }
+ assert(i<NELEM(optset));
+ break;
+ case 'f':
+ readbody++;
+ break;
+ case 'v':
+ verbose++;
+ break;
+ case 'V':
+ fprintf(stderr,version);
+ exit(EXIT_FAILURE);
+ default:
+ if (optc!='h')
+ error("\nLast getopt(3) error occured during parsing option %d from \"%s\"! Follows help:",optind-1,from);
+ usage();
+ break;
+ }
+ if (!phone && optind<argp)
+ chk(phone=strdup(args[optind++]));
+ if (!body && optind<argp) {
+char *d;
+int i;
+
+ for (i=optind,bodylen=argp-optind;i<argp;i++)
+ bodylen+=strlen(args[i]);
+ chk(body=malloc(bodylen));
+ for (d=body,i=optind;i<argp;i++) {
+size_t l=strlen(args[i]);
+ memcpy(d,args[i],l);
+ d+=l;
+ *d++=' ';
+ assert(d<=body+bodylen);
+ }
+ assert(d==body+bodylen);
+ d[-1]='\0';
+ bodylen--;
+ }
+ while (cfgstacki) {
+char *s=cfgstack[--cfgstacki];
+
+ assert(cfgstacki>=0);
+ readfile(s,0);
+ free(s);
+ assert(cfgstacki>=0 && cfgstacki<NELEM(cfgstack));
+ }
+}
+
+static const struct {
+ char **var;
+ const char *name;
+ } nullcheck[]={
+ {&phone,"destination phone number"},
+#if 0
+ {&body ,"body text"},
+ {&device,"device for communication"},
+#endif
+ };
+static char **emptycheck[]={&smsc,&body};
+
+static void lockclose(int fd)
+{
+ if (close(fd))
+ error("Error closing lockfile \"%s\"",lockreal);
+}
+
+static inline void lockdevice(void)
+{
+int fd=-1;
+char buf[64];
+ssize_t got;
+int delay=0;
+char empty=0;
+pid_t pid;
+
+ for (;;) {
+ if (fd!=-1) lockclose(fd);
+recheck:
+ if (delay) sleep(delay);
+ delay=DEVLOCK_PERIOD;
+ if (verbose>=3) error(".Checking the lockfile \"%s\"..",lockreal);
+ if ((fd=open(lockreal,O_RDONLY))==-1) break;
+ if ((got=read(fd,buf,sizeof(buf)-1))<=0) {
+isempty:
+ if (empty>=DEVLOCK_MAXEMPTY) {
+ error(".Lockfile \"%s\" is still not valid, removing it",lockreal);
+ goto remove;
+ }
+ empty++;
+ continue;
+ }
+ assert(got<sizeof(buf));
+ buf[got]='\0';
+ if (sscanf(buf,"%d",&pid)!=1) goto isempty;
+ empty=0;
+ errno=0;
+ if (kill(pid,0) && errno!=ESRCH && errno!=EPERM)
+ error("Error during checking consciousness of PID %d: %m",pid);
+ if (errno!=ESRCH) continue;
+ error(".Lockfile \"%s\" is stale (PID %d), removing it",lockreal,pid);
+remove:
+ lockclose(fd);
+ if (unlink(lockreal))
+ error("Error removing foreign lockfile \"%s\": %m",lockreal);
+ break;
+ }
+ errno=0;
+ if ((fd=open(lockreal,O_WRONLY|O_CREAT|O_EXCL,0644))==-1) {
+ if (errno==EEXIST) goto recheck;
+ error("!Error creating lockfile \"%s\": %m",lockreal);
+ }
+ locked=1;
+ got=VARPRINTF(buf,"%010d\n",getpid()); assert(got==11);
+ if (write(fd,buf,got)!=got)
+ error("!Error writing data to lockfile \"%s\": %m",lockreal);
+ lockclose(fd);
+}
+
+static void sigalarm(int signo)
+{
+ if (verbose>=1) error("Timed out");
+}
+
+static void blocking(char yes)
+{
+static char state=-1;
+ if (state==yes) return;
+ if (fcntl(devfd,F_SETFL,(yes?0:O_NONBLOCK)))
+ error("!fcntl() on device for %sblocking mode: %m",(yes?"":"non-"));
+ state=yes;
+}
+
+static const char *record;
+static char *catchdata;
+static size_t catchdatal,catchdatasiz;
+
+static void catched(const char *end)
+{
+size_t len;
+void *p;
+
+ if (!record) return;
+ assert(end>=record);
+ if ((p=memchr(record,'\n',end-record))) end=p;
+ if ((len=end-record)) {
+ if (catchdatal+len>catchdatasiz)
+ chk(catchdata=realloc(catchdata,
+ (catchdatasiz=MAX(LINE_MAX,(catchdatal+len)*2))));
+ memcpy(catchdata+catchdatal,record,len);
+ catchdatal+=len;
+ }
+ record=(p?NULL:end);
+ assert(catchdatal<=catchdatasiz);
+}
+
+static char *devcmd(const char *term,const char *catch,const char *send,...)
+{
+size_t l,bufl,terml,catchl,fragl,offs;
+char buf[LINE_MAX];
+ssize_t got;
+RETSIGTYPE (*origsig)(int);
+char *hit,*s;
+va_list ap;
+char errout;
+
+ if (!term) term="\nOK\n";
+ if ((errout=(*send=='!'))) send++;
+ if (0) {
+err:
+ if (errout) return(NULL);
+ if (verbose>=2) error(".Retrying last device command..");
+ }
+ catchdatal=0;
+ va_start(ap,send);
+ l=VARVPRINTF(buf,send,ap); bufl=l+1;
+ va_end(ap);
+ if (bufl>=sizeof(buf)-1) error("!Command too big (%d>%d)",bufl,sizeof(buf)-1);
+ if (verbose>=2) error(".devcmd(send=\"%s\",term=\"%s\",catch=\"%s\")",buf,term,catch);
+ buf[l]='\r'; buf[l+1]='\n';
+ origsig=signal(SIGALRM,(RETSIGTYPE (*)(int))sigalarm);
+ for (offs=0,got=0;offs<bufl;offs++) {
+ alarm(MAXSENDTIME);
+ usleep((offs?chartimen:cmdtimen)*1000);
+ if (!offs && tcflush(devfd,TCIOFLUSH))
+ error("Error flushing I/O queue of device: %m");
+ if (write(devfd,buf+offs,1)!=1) break;
+ got++;
+ if (tcdrain(devfd))
+ error("Error forcing output of char %d of cmd \"%s\": %m",offs,buf);
+ }
+ alarm(0);
+ signal(SIGALRM,origsig);
+ if (got!=bufl) {
+ error("Wrote only %d of %d bytes of command: %m",got,bufl);
+ goto err;
+ }
+
+ if (!(terml=strlen(term))) {
+ assert(!catch);
+ return(NULL);
+ }
+ if (catch) {
+ catchl=strlen(catch);
+ fragl=MAX(terml,catchl);
+ }
+ else fragl=terml;
+ fragl=MAX(fragl,MAX(strlen(ERROR_SUBSTR1),strlen(ERROR_SUBSTR2)));
+ bufl=0;
+ record=NULL;
+ for (;;) {
+ blocking(0);
+ errno=0;
+ got=read(devfd,buf+bufl,sizeof(buf)-1-bufl);
+ if (got==-1 && errno==EAGAIN) {
+ blocking(1);
+ errno=0;
+ got=read(devfd,buf+bufl,1);
+ }
+ if (got<=0) {
+ error("Could read device data (ret=%d): %m",got);
+ goto err;
+ }
+ s=buf+bufl;
+ bufl+=got;
+ /* FIXME: '\0' conversion */
+ while (buf+bufl>s && (s=memchr(s,'\r',buf+bufl-s))) *s='\n';
+ catched(buf+bufl); assert(!record || record==buf+bufl);
+ assert(bufl<sizeof(buf));
+ if (bufl>=fragl) {
+ buf[bufl]='\0';
+ assert(strlen(buf)==bufl);
+ /* d3(">%s|%s<\n",buf,term); */
+ if (strstr(buf,ERROR_SUBSTR1) || strstr(buf,ERROR_SUBSTR2)) {
+ error("Found ERROR response on command \"%s\"",send);
+ goto err;
+ }
+ if (catch && bufl>=catchl && (hit=strstr(buf,catch))) {
+ record=hit+catchl;
+ catched(buf+bufl); assert(!record || record==buf+bufl);
+ }
+ if ( bufl>= terml && (hit=strstr(buf,term))) break;
+ memmove(buf,buf+bufl-(fragl-1),fragl-1);
+ bufl=fragl-1;
+ if (record) record=buf+bufl;
+ }
+ }
+ if (!catchdatal) {
+ if (!catch) return(NULL);
+ error("Data requested on command \"%s\" but no found after term \"%s\"",send,term);
+ goto err;
+ }
+ assert(!!catch);
+ record=buf;
+ buf[0]='\0';
+ catched(buf+1);
+ if (verbose>=2) error(".Returning data \"%s\" for cmd \"%s\"",catchdata,send);
+ return(catchdata);
+}
+
+static int prepaddr(unsigned char *d,const char *addr)
+{
+int tot=0;
+char flip=0,plus;
+unsigned char n;
+
+ if ((plus=(*addr=='+'))) addr++;
+ *++d=(plus?ADDR_INT:ADDR_NAT);
+ while (*addr) {
+ if (*addr<'0' || *addr>'9')
+ error("!Error during conversion of number at: %s",addr);
+ tot++;
+ n=(*addr++)-'0';
+ if ((flip=!flip)) *++d=0xF0|n;
+ else *d=(*d&0x0F)|(n<<4U);
+ }
+ return(tot);
+}
+
+static char *finalsmsc;
+#define SMSCBINSIZE (1+1+(MAXNUMLEN+1)/2)
+static char pdusmsc[SMSCBINSIZE*2+1];
+
+static inline char tohex(unsigned char x)
+{
+ x&=0x0F;
+ if (x<10) return(x +'0');
+ return(x-10+'A');
+}
+
+static void textconv(char *d,unsigned char *s,size_t len)
+{
+ while (len--) {
+ *d++=tohex(*s>>4U);
+ *d++=tohex(*s );
+ s++;
+ }
+ *d='\0';
+}
+
+static inline void smscset(void)
+{
+char *s,*t,*e,*serr;
+long l;
+unsigned char bin[2+(MAXNUMLEN+1)/2];
+
+ if (smsc) devcmd(NULL,NULL,"\r\nAT+CSCA=\"%s\"",smsc);
+ s=devcmd(NULL,"\n+CSCA:","\r\nAT+CSCA?");
+ while (isspace(*s)) s++;
+ if (!*s || !strcmp(s,"EMPTY"))
+ error("!No SMS set in A1 found");
+ if (verbose>=1) error("\nFound default SMSC in A1: %s",s);
+ if (*s!='"') error("!No left-quote found in: %s",s);
+ if (!(t=strrchr(s+1,'"'))) error("!No right-quote found in: %s",s);
+ e=t++;
+ while (isspace(*t)) t++;
+ if (*t++!=',') error("!No comma found after quotes in: %s",s);
+ while (isspace(*t)) t++;
+ l=strtol(t,&serr,10);
+ if ((l!=ADDR_NAT && l!=ADDR_INT) || (serr && *serr))
+ error("!Type parse error in: %s",s);
+ if (l==ADDR_NAT || s[1]=='+') s++;
+ else *s='+';
+ *e='\0';
+ chk(finalsmsc=strdup(s));
+ if (verbose>=2) error("\nDecoded SMSC address: %s",finalsmsc);
+ bin[0]=1+(prepaddr(bin,finalsmsc)+1)/2;
+ textconv(pdusmsc,bin,bin[0]+1);
+}
+
+static char *pdudata;
+
+static inline unsigned char charconv(char c,size_t offs)
+{
+ if ((c>='A' && c<='Z') || (c>='a' && c<='z') || (c>='0' && c<='9')) return(c);
+ switch (c) {
+ case '@': return(0);
+ case '$': return(2);
+ case '"': return(96);
+ case ' ':
+ return(c);
+ case 0: assert(0);
+ }
+ error("Can't convert character '%c' (0x%02X) at offs %d (0-based), substituted '?'",
+ c,(unsigned char)c,offs);
+ return('?');
+}
+
+static inline void genpdu(void)
+{
+static unsigned char pdu[64+MAXNUMLEN/2+(MAXBODYLEN*7)/8];
+unsigned char *d=pdu;
+int i;
+char inb=0,outb=0,xb;
+unsigned char inreg;
+size_t offs=0;
+
+ *d++=PDU_TYPE;
+ *d++=PDU_MR;
+ i=prepaddr(d,phone);
+ *d=i; d+=1+1+(i+1)/2;
+ *d++=PDU_PID;
+ *d++=PDU_DCS;
+ *d++=PDU_VP;
+ if (bodylen>MAXBODYLEN) {
+ error("Body too large (%d>%d), cut",bodylen,MAXBODYLEN);
+ bodylen=MAXBODYLEN;
+ }
+ *d=bodylen;
+ assert(d<pdu+sizeof(pdu));
+ while (bodylen || inb) {
+ if (!inb) {
+ assert(bodylen>0); assert(!!*body);
+ inreg=charconv(*body++,offs++);
+ bodylen--;
+ inb=7;
+ }
+ if (!outb) {
+ *++d=0x00;
+ outb=8;
+ }
+ xb=MIN(inb,outb);
+ d4("inb=%d,outb=%d,xb=%d\n",inb,outb,xb);
+ *d|=((inreg>>(unsigned)(7-inb))&((1<<xb)-1))<<(unsigned)(8-outb);
+ inb-=xb; outb-=xb;
+ }
+ d++;
+ assert(d<pdu+sizeof(pdu));
+ pdudata=malloc(2*(d-pdu)+1);
+ textconv(pdudata,pdu,d-pdu);
+}
+
+static struct {
+ char **sp;
+ long *ip;
+ const char *const msg;
+ } numarg[]={
+ { &chartime,&chartimen,"chartime" },
+ { &cmdtime ,&cmdtimen ,"cmdtime" },
+ };
+
+int main(int argc,char **argv)
+{
+char *s,*finame;
+FILE *fin;
+int i;
+unsigned fatal=0;
+struct termios tios;
+
+ pname=*argv;
+ atexit(cleanup);
+ signal(SIGTERM,(RETSIGTYPE (*)(int))cleanup);
+ signal(SIGQUIT,(RETSIGTYPE (*)(int))cleanup);
+ signal(SIGINT ,(RETSIGTYPE (*)(int))cleanup);
+ signal(SIGHUP ,(RETSIGTYPE (*)(int))cleanup);
+ processargs(argc,argv,"<command-line>");
+ if ((s=getenv("HOME"))) {
+size_t l=strlen(s);
+char *buf=malloc(l+50);
+
+ memcpy(buf,s,l);
+ strcpy(buf+l,CONFIG_HOME);
+ readfile(buf,1);
+ free(buf);
+ }
+ readfile(CONFIG_MAIN,1);
+
+ for (i=0;i<NELEM(nullcheck);i++)
+ if (!*nullcheck[i].var) {
+ error("Missing parameter \"%s\"",nullcheck[i].name);
+ fatal++;
+ }
+ if (fatal) error("!Previous error%s considered unrecoverable",(fatal==1?"":"s"));
+ for (i=0;i<NELEM(emptycheck);i++)
+ if (*emptycheck[i] && !**emptycheck[i]) {
+ free(*emptycheck[i]);
+ *emptycheck[i]=NULL;
+ }
+ if (!lockfile) lockfile=DEF_LOCKFILE;
+ if (!device) device=DEF_DEVICE;
+ if (body && readbody) {
+ finame=body;
+ body=NULL;
+ }
+ else fin=stdin;
+ if (!body) {
+ readbody=0;
+ if (verbose>=1)
+ error("\nPlease enter the SMS text body, end with EOF (ctrl-D):");
+ chk(body=malloc(BODYLOAD));
+ bodylen=fread(body,1,BODYLOAD,fin);
+ if (bodylen==-1)
+ error("!Error reading stream \"%s\": %m",(finame?finame:"<stdin>"));
+ }
+ if (fin!=stdin) {
+ chkfclose(fin,finame);
+ free(finame);
+ }
+
+ for (i=0;i<NELEM(numarg);i++) {
+char *serr;
+ if (!*numarg[i].sp) continue;
+ *numarg[i].ip=strtol(*numarg[i].sp,&serr,0);
+ if (*numarg[i].ip<0 || *numarg[i].ip>=LONG_MAX || !serr || *serr)
+ error("!Number parse error for parameter \"%s\" of \"%s\" at: %s",
+ numarg[i].msg,*numarg[i].sp,serr);
+ }
+
+ if (!strchr(device,'/')) {
+size_t l=strlen(device);
+ chk(s=malloc(5+l+1));
+ strcpy(s,"/dev/");
+ strcpy(s+5,device);
+ free(device);
+ device=s;
+ }
+ devicename=strrchr(device,'/')+1; assert(!!(devicename-1));
+ for (i=0,s=lockfile;*s;s++) {
+ if (*s!='%') continue;
+ s++;
+ if (*s=='%') continue;
+ if (*s=='s') {
+ if (i) error("!Only one \"%%s\" permitted in lockfile format-string");
+ i=1; continue;
+ }
+ error("!Invalid format-character '%c' in lockfile format-string, only \"%%s\" allowed",*s);
+ }
+ genpdu();
+
+ if (lockfile && *lockfile && VARPRINTF(lockreal,lockfile,devicename)>0) {
+ if (verbose>=1) error(".Locking device \"%s\" by \"%s\"..",device,lockreal);
+ lockdevice();
+ }
+ if (verbose>=1) error(".Opening device \"%s\"..",device);
+ if ((devfd=open(device,O_RDWR|O_NDELAY))<0)
+ error("!Cannot open device \"%s\" for rw-access: %m",device);
+
+ if (tcgetattr(devfd,&restios))
+ error("Unable to get termios settings: %m");
+ else {
+ restios.c_cflag=(restios.c_cflag&~(CBAUD|CBAUDEX))|B0|HUPCL;
+ restios_yes=1;
+ }
+ tios.c_iflag=IGNBRK|IGNPAR|IXON|IXOFF;
+ tios.c_oflag=0;
+ tios.c_cflag=CS8|CREAD|CLOCAL|B19200|HUPCL;
+ tios.c_lflag=IEXTEN|NOFLSH;
+ memset(tios.c_cc,_POSIX_VDISABLE,sizeof(tios.c_cc));
+ tios.c_cc[VTIME]=0;
+ tios.c_cc[VMIN ]=1;
+ if (tcflush(devfd,TCIOFLUSH))
+ error("Error flushing termios (TCIOFLUSH) on device: %m");
+ if (tcsetattr(devfd,TCSANOW,&tios))
+ error("!Unable to set initial termios device settings");
+
+ do {
+ devcmd("",NULL,"\r\nAT\x1A");
+ devcmd(NULL,NULL,"\r\nAT");
+ smscset();
+ devcmd(NULL,NULL,"\r\nAT+CMGF=0");
+ devcmd("\n> ",NULL,"\r\nAT+CMGS=%d",(strlen(pdusmsc)+strlen(pdudata))/2);
+ s=devcmd(NULL,"\n+CMGS:","!%s%s\x1A",pdusmsc,pdudata);
+ } while (!s);
+ while (isspace(*s)) s++;
+ if (verbose>=1) error("\nMessage successfuly sent with MR: %s",s);
+ devcmd(NULL,NULL,"\r\nAT");
+
+ return(EXIT_SUCCESS);
+}