Printformats cleanup, ugly "%m" handling completely removed.
authorshort <>
Tue, 7 Sep 1999 15:32:09 +0000 (15:32 +0000)
committershort <>
Tue, 7 Sep 1999 15:32:09 +0000 (15:32 +0000)
mdsms.c

diff --git a/mdsms.c b/mdsms.c
index d96bd95..1243586 100644 (file)
--- a/mdsms.c
+++ b/mdsms.c
@@ -65,11 +65,8 @@ static struct termios restios,tios;
 static char restios_yes;
 static FILE *logf;
 
-static void vlogmsg(
-#ifndef PRINTF_WORKS_PM
-               char outerr,
-#endif
-               const char *fmt,va_list ap)
+static void vlogmsg(char outerr, const char *fmt,va_list ap) ATTR_PRINTFORMAT(2,0);
+static void vlogmsg(char outerr, const char *fmt,va_list ap)
 {
 time_t stamp;
 char *ctm,*s;
@@ -86,68 +83,37 @@ char host[LINE_MAX];
        if ((s=strchr(ctm,'\n'))) *s='\0';
        fprintf(logf,"%s %s %s[%d]: ",ctm,host,pname,mypid);
        vfprintf(logf,fmt,ap);
-#ifndef PRINTF_WORKS_PM
        if (outerr) fputs(strerror(errno),logf);
-#endif
        fputc('\n',logf);
        fflush(logf);
 }
 
+static void logmsg(const char *fmt,...) ATTR_PRINTFORMAT(1,2);
 static void logmsg(const char *fmt,...)
 {
 va_list ap;
        va_start(ap,fmt);
-       vlogmsg(
-#ifndef PRINTF_WORKS_PM
-               0,
-#endif
-               fmt,ap);
+       vlogmsg(0,fmt,ap);
        va_end(ap);
 }
 
+static void error(const char *fmt,...) ATTR_PRINTFORMAT(1,2);
 static void error(const char *fmt,...)
 {
 va_list ap;
-char fatal=*fmt;
-#ifndef PRINTF_WORKS_PM
-char pm,*nfmt;
-size_t fmtl;
-#endif
+char fatal,pm;
 
+       if ((pm=(*fmt=='^'))) fmt++;
+       fatal=*fmt;
        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);
-       if (fatal=='!') vlogmsg(
-#ifdef PRINTF_WORKS_PM
-               fmt
-#else
-               pm,nfmt
-#endif
-               ,ap);
+       vfprintf(stderr,fmt,ap);
+       if (fatal=='!') vlogmsg(pm,fmt,ap);
        va_end(ap);
-
-#ifndef PRINTF_WORKS_PM
-       if (pm) {
-               fputs(strerror(errno),stderr);
-               free(nfmt);
-               }
-#endif
-
+       if (pm) { fputs(": ",stderr); fputs(strerror(errno),stderr); }
        if (fatal!='\n') fputc((fatal=='.'?'.':'!'),stderr);
        fputc('\n',stderr);
        if (fatal=='!') exit(EXIT_FAILURE);
@@ -165,13 +131,13 @@ static void cleanup(void)
        if (dis_cleanup) return;
        if (restios_yes) {
                if (tcsetattr(devfd,TCSANOW,&restios))
-                       error("Error restoring termios for device: %m");
+                       error("^Error restoring termios for device");
                restios_yes=0;
                }
        if (locked && *lockreal) {
                d2("Removing lockfile \"%s\"\n",lockreal);
                if (unlink(lockreal))
-                       error("Error removing my device lockfile \"%s\": %m",lockreal);
+                       error("^Error removing my device lockfile \"%s\"",lockreal);
                locked=0;
                }
        dis_cleanup=1;
@@ -236,7 +202,7 @@ static unsigned cfgstacki=0;
 static void chkfclose(FILE *f,const char *fname)
 {
        if (fclose(f))
-               error("Error closing \"%s\": %m",fname);
+               error("^Error closing \"%s\"",fname);
 }
 
 static void readfile(const char *fname,char quiet)
@@ -254,16 +220,16 @@ static unsigned tot=0;
                return;
                }
        if (!(f=fopen(fname,"rt"))) {
-               if (!quiet) error("Can't open config file \"%s\" for r/o: %m",fname);
+               if (!quiet) error("^Can't open config file \"%s\" for r/o",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);
+               error("^Error seeking to end of \"%s\"",fname);
        if ((size=ftell(f))<0)
-               size=0,error("Error measuring \"%s\": %m",fname);
+               size=0,error("^Error measuring \"%s\"",fname);
        if (size>MAXCONFIG) 
-               error("File \"%s\" is too long, read only %ld bytes",fname,MAXCONFIG);
+               error("File \"%s\" is too long, read only %d bytes",fname,MAXCONFIG);
        chk(buf=malloc((size?size:MAXCONFIG)+1));
        rewind(f);
        got=fread(buf,1,(size?size:MAXCONFIG),f);
@@ -332,7 +298,7 @@ int i;
        while ((optc=getopt_long(argp,args,"c:d:L:l:s:m:r: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);
+                               error("Looping (%d) during attempt to read config file \"%s\", break-out",NELEM(cfgstack),from);
                                break;
                                }
                        chk(cfgstack[cfgstacki++]=strdup(optarg));
@@ -467,24 +433,24 @@ isempty:
                empty=0;
                errno=0;
                if (kill(pid,0) && errno!=ESRCH && errno!=EPERM)
-                       error("Error during checking consciousness of PID %d: %m",pid);
+                       error("^Error during checking consciousness of PID %d",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);
+                       error("^Error removing foreign lockfile \"%s\"",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);
+               error("^!Error creating lockfile \"%s\"",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);
+               error("^!Error writing data to lockfile \"%s\"",lockreal);
        lockclose(fd);
 }
 
@@ -511,7 +477,7 @@ 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-"));
+               error("^!fcntl() on device for %sblocking mode",(yes?"":"non-"));
        state=yes;
 }
 
@@ -541,10 +507,11 @@ void *p;
 static int retrycnt=0;
 static void retrying(void)
 {
-       if (++retrycnt>maxretryn) error("!Maximum command retry count (%d) exceeded",maxretryn);
-       if (verbose>=2) error(".Retrying phase, %d out of %d..",retrycnt,maxretryn);
+       if (++retrycnt>maxretryn) error("!Maximum command retry count (%ld) exceeded",maxretryn);
+       if (verbose>=2) error(".Retrying phase, %d out of %ld..",retrycnt,maxretryn);
 }
 
+static char *devcmd(const char *term,const char *catch,const char *send,...) ATTR_PRINTFORMAT(3,4);
 static char *devcmd(const char *term,const char *catch,const char *send,...)
 {
 size_t l,bufl,terml,catchl,fragl,offs;
@@ -573,15 +540,15 @@ err:
                alarm(MAXSENDTIME);
                usleep((offs?chartimen:cmdtimen)*1000);
                if (!offs && tcflush(devfd,TCIOFLUSH))
-                       error("Error flushing I/O queue of device: %m");
+                       error("^Error flushing I/O queue of device");
                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);
+                       error("^Error forcing output of char %d of cmd \"%s\"",offs,buf);
                }
        alarm(0);
        if (got!=bufl) {
-               error("Wrote only %d of %d bytes of command: %m",got,bufl);
+               error("^Wrote only %d of %d bytes of command",got,bufl);
                goto err;
                }
 
@@ -609,8 +576,8 @@ err:
                        got=read(devfd,buf+bufl,1);
                        }
                if (got<=0) {
-                       if (wasalarm) error("Maximum response timeout (%ds) exceeded",readtimen);
-                       else error("Couldn't read device data (ret=%d): %m",got);
+                       if (wasalarm) error("Maximum response timeout (%lds) exceeded",readtimen);
+                       else error("^Couldn't read device data (ret=%d)",got);
                        goto err;
                        }
                s=buf+bufl;
@@ -721,7 +688,7 @@ int sizex,sizey,bit;
 #define WORD(n) (((unsigned char)buf[(n)])|(((unsigned char)buf[(n)+1])<<8))
 
        if (!(f=fopen(logoname,"rb")))
-               error("!Cannot open logo file \"%s\" for r/o: %m",logoname);
+               error("^!Cannot open logo file \"%s\" for r/o",logoname);
        got=fread(buf,1,sizeof(buf),f);
             if (got>=20 && !memcmp(buf,"NOL",4)) {
                VARPRINTF2(gsmnetf,"%03.3u%02.2u",WORD(6),WORD(8));
@@ -733,9 +700,9 @@ int sizex,sizey,bit;
        else if (got>=16 && !memcmp(buf,"NGG",4)) {
                r=6;
                if (verbose>=1) error(".Reading NGG file \"%s\", word@4=%d..",
-                       logoname,gsmnetf,WORD(4));
+                       logoname,WORD(4));
                }
-       else error("!Unknown file format of logo file \"%s\"");
+       else error("!Unknown file format of logo file \"%s\"",logoname);
        if (!strcasecmp(gsmnet,WORD_NET)) {
                if (!*gsmnetf) error("!NOL network code detection requested but NOL file not loaded, please specify network code");
                gsmnet=gsmnetf;
@@ -784,6 +751,8 @@ int sizex,sizey,bit;
        assert(chars==-1); assert(bits==0); assert(w==got); assert(w<=140);
        textconv(hexdata,bin,w);
        if (verbose>=2) error("\nWill send hexdata: %s",hexdata);
+
+#undef WORD
 }
 
 static struct {
@@ -870,7 +839,7 @@ size_t l=strlen(device);
        
        if (*logname) {
                if (!(logf=fopen(logname,"a")))
-                       error("!Error opening log \"%s\" for append: %m",logname);
+                       error("^!Error opening log \"%s\" for append",logname);
                logmsg("Starting up: " PACKAGE " " VERSION);
                }
        switch (baudn) {
@@ -881,9 +850,9 @@ size_t l=strlen(device);
                case 38400: portbaud=B38400; break;
                case 57600: portbaud=B57600; break;
                default:
-                       error("!Specified baudrate %d is not supported",baudn);
+                       error("!Specified baudrate %ld is not supported",baudn);
                }
-       if (verbose>=2) error(".Will use baudrate %d with hexval 0x%X",baudn,portbaud);
+       if (verbose>=2) error(".Will use baudrate %ld with hexval 0x%X",baudn,portbaud);
                
        if (lockfile && *lockfile && VARPRINTF(lockreal,lockfile,devicename)>0) {
 time_t start,end;
@@ -892,14 +861,14 @@ time_t start,end;
                lockdevice();
                time(&end);
                if ((end-=start)>LOCKREPORT)
-                       logmsg("Device lock succeeded after %d seconds",end);
+                       logmsg("Device lock succeeded after %ld seconds",(long)end);
                }
        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);
+               error("^!Cannot open device \"%s\" for rw-access",device);
        
        if (tcgetattr(devfd,&restios))
-               error("Unable to get termios settings: %m");
+               error("^Unable to get termios settings");
        else {
                restios.c_cflag=(restios.c_cflag&~(CBAUD|CBAUDEX))|B0|HUPCL;
                restios_yes=1;
@@ -913,11 +882,11 @@ time_t start,end;
        tios.c_cc[VMIN ]=1;
            cfsetispeed(&tios,portbaud);
        if (cfsetospeed(&tios,portbaud)|cfsetispeed(&tios,portbaud))
-               error("Error setting termios baudrate on device: %m");
+               error("^Error setting termios baudrate on device");
        if (tcflush(devfd,TCIOFLUSH))
-               error("Error flushing termios (TCIOFLUSH) on device: %m");
+               error("^Error flushing termios (TCIOFLUSH) on device");
        if (tcsetattr(devfd,TCSANOW,&tios))
-               error("!Unable to set initial termios device settings: %m");
+               error("^!Unable to set initial termios device settings");
 
        setalarm();
        do {