Device reinitialized before any command retry ("retryall" label moved)
[mdsms.git] / mdsms.c
diff --git a/mdsms.c b/mdsms.c
index 700cdc9..bf307b5 100644 (file)
--- a/mdsms.c
+++ b/mdsms.c
@@ -625,7 +625,9 @@ int i;
                        readbody++;
                        break;
                case 'v':
+#ifndef DEBUG  /* prevent suppositious overflow */
                        verbose++;
+#endif
                        break;
                case 'V':
                        fprintf(stderr,version);
@@ -785,7 +787,7 @@ static void lockclose(int fd)
                error(_("Error closing lockfile \"%s\""),lockreal);
 }
 
-static inline int lockdevice(int attempt)
+static int lockdevice(int attempt)
 {
 int fd=-1;
 char buf[64];
@@ -2091,34 +2093,38 @@ time_t start,end;
                if ((end-=start)>LOCKREPORT)
                        logmsg(_("Device lock succeeded after %ld seconds"),(long)end);
                }
+
+retryopen:
+
        if (verbose>=1) error(_(".Opening device \"%s\".."),device);
        if ((devfd=open(device,O_RDWR|O_NDELAY))<0)
                error(_("^!Cannot open device \"%s\" for r/w access"),device);
        
-       if (tcgetattr(devfd,&restios))
-               error(_("^Unable to get termios settings"));
-       else {
-               restios.c_cflag=(restios.c_cflag&~(CBAUD|CBAUDEX))|B0|HUPCL;
-               restios_yes=1;
-               }
-       tios.c_iflag=IGNBRK|IGNPAR|(handshake_rtscts ? 0 : IXON|IXOFF);
-       tios.c_oflag=0;
-       tios.c_cflag=CS8|CREAD|CLOCAL|HUPCL|portbaud|(handshake_rtscts ? CRTSCTS : 0);
-       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;
-           cfsetispeed(&tios,portbaud);
-       if (cfsetospeed(&tios,portbaud)|cfsetispeed(&tios,portbaud))
-               error(_("^Error setting termios baudrate on device"));
-       if (tcflush(devfd,TCIOFLUSH))
-               error(_("^Error flushing termios (TCIOFLUSH) on device"));
-       if (tcsetattr(devfd,TCSANOW,&tios))
-               error(_("^!Unable to set initial termios device settings"));
+retryall:
 
-       setalarm();
+               if (tcgetattr(devfd,&restios))
+                       error(_("^Unable to get termios settings"));
+               else {
+                       restios.c_cflag=(restios.c_cflag&~(CBAUD|CBAUDEX))|B0|HUPCL;
+                       restios_yes=1;
+                       }
+               tios.c_iflag=IGNBRK|IGNPAR|(handshake_rtscts ? 0 : IXON|IXOFF);
+               tios.c_oflag=0;
+               tios.c_cflag=CS8|CREAD|CLOCAL|HUPCL|portbaud|(handshake_rtscts ? CRTSCTS : 0);
+               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;
+                               cfsetispeed(&tios,portbaud);
+               if (cfsetospeed(&tios,portbaud)|cfsetispeed(&tios,portbaud))
+                       error(_("^Error setting termios baudrate on device"));
+               if (tcflush(devfd,TCIOFLUSH))
+                       error(_("^Error flushing termios (TCIOFLUSH) on device"));
+               if (tcsetattr(devfd,TCSANOW,&tios))
+                       error(_("^!Unable to set initial termios device settings"));
+
+               setalarm();
 
-retryall:
                devcmd("",NULL,"\r\nAT\033\032"); /* ESCAPE, CTRL-Z */
                devcmd(NULL,NULL,"\r\nAT");
                smscset();
@@ -2197,8 +2203,11 @@ continue_receive:
                                        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 retryall;
+                                               goto retryopen;
                                                }
                                        d1("Lock-device succeeded\n");
                                        do {