Rename 'cvs2cl.pl' to the more common 'cvs2cl'.
[mdsms.git] / mdsms.c
diff --git a/mdsms.c b/mdsms.c
index c86f978..757124d 100644 (file)
--- a/mdsms.c
+++ b/mdsms.c
@@ -63,6 +63,9 @@ static char rcsid[] ATTR_UNUSED = "$Id$";
 #ifdef HAVE_LOCALE_H
 #include <locale.h>
 #endif
+#ifdef HAVE_STDDEF_H
+#include <stddef.h>
+#endif
 
 #ifdef HAVE_GETOPT_LONG
 #include <getopt.h>
@@ -107,9 +110,9 @@ static int verbose
 static char *pname;
 static int dis_cleanup=0,devfd=-1;
 
-static char *phone,*device,*logname,*lockfile,*smsmode,*pdusmscmode,*smsc,*maxretry,*readtime,*chartime,*cmdtime,*baud,*restore;
+static char *phone,*device,*logname,*lockfile,*smsmode,*pdusmscmode,*smsc,*maxretry,*readtime,*chartime,*cmdtime,*waittime,*baud,*restore;
 static int readbody;
-static long maxretryn=DEF_MAXRETRY,readtimen=-1,chartimen=DEF_CHARTIME,cmdtimen=DEF_CMDTIME,baudn=DEF_BAUD;
+static long maxretryn=DEF_MAXRETRY,readtimen=-1,chartimen=DEF_CHARTIME,cmdtimen=DEF_CMDTIME,waittimen=DEF_WAITTIME,baudn=DEF_BAUD;
 #ifdef HAVE_CRTSCTS
 static int handshake_rtscts;
 static unsigned handshake_stamp;
@@ -274,7 +277,7 @@ Usage: %s [-c|--config <cfgfile>] [-d|--device <device>]\n\
              [-M|--smsmode <mode>] [-P|--pdusmscmode <mode>]\n\
              [-s|--smsc <smsc #>] [-m|--maxretry <#>]\n\
              [-r|--readtime <sec>] [-t|--chartime <msec>] [-T|--cmdtime <msec>]\n\
-             [-v|--verbose] [-h|--help] [-V|--version]\n\
+             [-w|--waittime <sec>] [-v|--verbose] [-h|--help] [-V|--version]\n\
              {--send | --send-mobildock | --receive | --logo-send}\n\
   --send / --send-mobildock:\n\
              [-f|--file] <dest. phone> <msg text|msg filename>\n\
@@ -305,6 +308,7 @@ Usage: %s [-c|--config <cfgfile>] [-d|--device <device>]\n\
 \t\t multiplied %dx for long cmds)\n\
  -t, --chartime\tMilliseconds between each char (def. %dms)\n\
  -T, --cmdtime\tMilliseconds before each whole AT command (def. %dms)\n\
+ -w, --waittime\tSeconds to prevent timeout of 9110 FaxModem (def. %ds)\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\
@@ -332,7 +336,7 @@ You may need to use the following line to catch all of this help text:\n\
 #else
                _("\n\t\t(Not supported on this platform!)"),
 #endif
-DEF_MAXRETRY,DEF_READTIME,DEF_READTIME_MOBILDOCK,EXT_READTIME,DEF_CHARTIME,DEF_CMDTIME,
+DEF_MAXRETRY,DEF_READTIME,DEF_READTIME_MOBILDOCK,EXT_READTIME,DEF_CHARTIME,DEF_CMDTIME,DEF_WAITTIME,
 WORD_NET,WORD_GROUP);
        exit(EXIT_FAILURE);
 }
@@ -366,6 +370,7 @@ static const struct option longopts[]={
 {"readtime"    ,1,0,'r'},
 {"chartime"    ,1,0,'t'},
 {"cmdtime"     ,1,0,'T'},
+{"waittime"    ,1,0,'w'},
 {"file"        ,0,0,'f'},
 {"verbose"     ,0,0,'v'},
 {"help"        ,0,0,'h'},
@@ -577,6 +582,7 @@ static struct {
                { 'r',&readtime     },
                { 't',&chartime     },
                { 'T',&cmdtime      },
+               { 'w',&waittime     },
        };
 
 static void processargs(int argp,char **args,const char *from)
@@ -587,7 +593,7 @@ int i;
 
        seq++;
        optarg=NULL; optind=0; /* FIXME: Possible portability problem. */
-       while ((optc=getopt_long(argp,args,"c:d:L:l:b:xCM:P:s:m:r:t:T:fvhV",longopts,NULL))!=EOF) switch (optc) {
+       while ((optc=getopt_long(argp,args,"c:d:L:l:b:xCM:P:s:m:r:t:T:w: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),from);
@@ -595,7 +601,7 @@ int i;
                                }
                        chk(cfgstack[cfgstacki++]=strdup(optarg));
                        break;
-               case 'd': case 'L': case 'b': case 'l': case 'M': case 'P': case 's': case 'm': case 'r': case 't': case 'T':
+               case 'd': case 'L': case 'b': case 'l': case 'M': case 'P': case 's': case 'm': case 'r': case 't': case 'T': case 'w':
                        for (i=0;i<NELEM(optset);i++)
                                if (optset[i].c==optc) {
                                        if (optset[i].stamp && optset[i].stamp!=seq) {
@@ -662,7 +668,11 @@ static const struct nullcheck {
        enum modenum reqd;
        const char *name;
        } nullcheck[]={
-               {&phone,MODE_BIT(MODE_SEND)|MODE_BIT(MODE_SEND_MOBILDOCK)|MODE_BIT(MODE_LOGO_SEND),
+               {&phone,MODE_BIT(MODE_SEND)
+                      |MODE_BIT(MODE_SEND_MOBILDOCK)
+                      |MODE_BIT(MODE_LOGO_SEND)
+                      |MODE_BIT(MODE_RING_SEND)
+                      |MODE_BIT(MODE_PICTURE_SEND),
                        N_("destination phone number")},
                {&   logoname,MODE_BIT(   MODE_LOGO_SEND),N_(   "logo filename")},
                {&   ringname,MODE_BIT(   MODE_RING_SEND),N_(   "ring filename")},
@@ -1566,7 +1576,7 @@ char *finame;
                }
 }
 
-static int datawait(char immed)
+static int datawait(int timeout)
 {
 int i;
 #ifdef HAVE_POLL
@@ -1577,14 +1587,14 @@ fd_set rfds,xfds;
 
        assert(devfd>=0);
 retry:
-       if (!immed && verbose>=2)
+       if (timeout && verbose>=2)
                error(_(".Waiting for device incoming data.."));
 #ifdef HAVE_POLL
        ufd.fd=devfd;
        ufd.events=POLLIN;
        ufd.revents=0;
        errno=0;
-       i=poll(&ufd,1,(immed?0:-1));
+       i=poll(&ufd,1,timeout*1000);
 #else /* HAVE_POLL */
 #ifdef HAVE_FD_SETSIZE
        if (devfd>=FD_SETSIZE)
@@ -1596,7 +1606,7 @@ retry:
        errno=0;
        i=select(devfd+1,&rfds,NULL,&xfds,NULL);
 #endif /* HAVE_POLL */
-       if (immed && i==0) return(0);
+       if (i==0) return(0);
        if (i==-1 && errno==EINTR)
                goto retry; /* silent retry, for example SIGCHLD could occur */
        if (i!=1)
@@ -2009,6 +2019,7 @@ static struct {
                { &readtime,&readtimen,"readtime" },
                { &chartime,&chartimen,"chartime" },
                { &cmdtime ,&cmdtimen ,"cmdtime"  },
+               { &waittime,&waittimen,"waittime" },
                { &baud    ,&baudn    ,"baud"     },
        };
 
@@ -2288,50 +2299,55 @@ struct hexdata *hd;
                                        parts++;
                                        }
                                } break;
-                       case MODE_RECEIVE:
+                       case MODE_RECEIVE: {
+int gotdatawait;
+
                                restore="\r\nAT+CNMI=,0";
                                devcmd(NULL,NULL,"\r\nAT+CNMI=,2");
                                devcmd(NULL,NULL,"\r\nAT+CSDH=0");
-                               for (;;) {
 continue_receive:
-                                       unlockdevice(0);
-                                       /* Never bail-out when we got up to this point */
-                                       if (maxretryn!=-1 && verbose>=1)
-                                               error(_(".Initialization successful, infinite retry count set"));
-                                       maxretryn=-1;
+                               unlockdevice(0);
+                               /* Never bail-out when we got up to this point */
+                               if (maxretryn!=-1 && verbose>=1)
+                                       error(_(".Initialization successful, infinite retry count set"));
+                               maxretryn=-1;
 #if RECEIVE_TEST
-               receive_headerparse(" \"+420602123456\",,\"99/10/25,03:21:03-00\"");
-               receive_accept("TESTBODY");
-               exit(EXIT_SUCCESS);
+       receive_headerparse(" \"+420602123456\",,\"99/10/25,03:21:03-00\"");
+       receive_accept("TESTBODY");
+       exit(EXIT_SUCCESS);
 #endif
-                                       datawait(0);
-                                       if (!lockdevice(1)) {
-                                               if (verbose>=1)
-                                                       error(_(".Dialout detected, waiting for lock.."));
-                                               if (verbose>=1) error(_(".Closing device \"%s\".."),device);
-                                               if (close(devfd))
-                                                       error(_("Error closing device \"%s\""),device);
-                                               lockdevice(0);
-                                               goto retryopen;
-                                               }
-                                       d1("Lock-device succeeded\n");
-                                       do {
-                                               d1("Reading a message for us...\n");
-                                               if (!(s=devcmd("\n","+CMT:"," ")))
-                                                       goto retryall;
-                                               if (s==&devcmd_empty_return) /* only newlines found */
-                                                       goto continue_receive;
-                                               if (cmgf && !(i=receive_headerparse(s)))
-                                                       error(_("Receive-header parsing failed on: %s"),s);
-                                               if (!(s=devcmd("\n","@"," ")))
+                               gotdatawait=datawait(waittimen);
+                               if (!lockdevice(1)) {
+                                       if (verbose>=1)
+                                               error(_(".Dialout detected, waiting for lock.."));
+                                       if (verbose>=1) error(_(".Closing device \"%s\".."),device);
+                                       if (close(devfd))
+                                               error(_("Error closing device \"%s\""),device);
+                                       lockdevice(0);
+                                       goto retryopen;
+                                       }
+                               d1("Lock-device succeeded\n");
+                               do {
+                                       if (!(s=devcmd("\n","+CMT:"," ")))
+                                               goto retryall;
+                                       if (s==&devcmd_empty_return) { /* only newlines found */
+                                               if (gotdatawait==0) /* timeout, rather reinitialize the modem */
                                                        goto retryall;
-                                               if (cmgf) {
-                                                       if (i) receive_text(s);
-                                                       }
-                                               else receive_pdu(s);
-                                               } while (datawait(1));
-                                       } /* return to 'continue receive' point */
-                               break;
+                                               goto continue_receive;
+                                               }
+                                       d1("Reading a message for us...\n");
+                                       if (cmgf && !(i=receive_headerparse(s)))
+                                               error(_("Receive-header parsing failed on: %s"),s);
+                                       if (!(s=devcmd("\n","@"," ")))
+                                               goto retryall;
+                                       if (cmgf) {
+                                               if (i) receive_text(s);
+                                               }
+                                       else receive_pdu(s);
+                                       } while (datawait(0));
+                               goto retryall;
+                               }
+
                        default: assert(0);
                        }
                if (!s) { retrying(); goto retryall; }