A bit refactoring.
authormarkus schnalke <meillo@marmaro.de>
Tue, 20 Mar 2012 20:39:17 +0000 (21:39 +0100)
committermarkus schnalke <meillo@marmaro.de>
Tue, 20 Mar 2012 20:39:17 +0000 (21:39 +0100)
uip/anno.c
uip/annosbr.c

index e97afbf..36ff4b5 100644 (file)
@@ -162,27 +162,25 @@ main(int argc, char **argv)
                                        number = 1;
 
                                else {
-                                               if (strcmp(*argp, "all") == 0)
-                                                       number = -1;
-
-                                               else if (!(number = atoi(*argp)))
+                                       if (strcmp(*argp, "all") == 0)
+                                               number = -1;
+                                       else if (!(number = atoi(*argp)))
                                                adios(NULL, "missing argument to %s", argp[-2]);
-
                                        argp++;
                                }
 
                                delete = number;
                                continue;
 
-                       case APPENDSW:  /* append annotations instead of default prepend */
+                       case APPENDSW:
                                append = 1;
                                continue;
 
-                       case PRESERVESW:  /* preserve access and modification times on annotated message */
+                       case PRESERVESW:
                                preserve = 1;
                                continue;
 
-                       case NOPRESERVESW:  /* don't preserve access and modification times on annotated message (default) */
+                       case NOPRESERVESW:
                                preserve = 0;
                                continue;
                        }
@@ -226,15 +224,16 @@ main(int argc, char **argv)
                        if (list)
                                annolist(m_name(msgnum), comp, text, number);
                        else
-                               annotate(m_name(msgnum), comp, text, datesw, delete, append, preserve);
+                               annotate(m_name(msgnum), comp, text, datesw,
+                                               delete, append, preserve);
                }
        }
 
-       context_replace(curfolder, folder);  /* update current folder  */
-       seq_setcur(mp, mp->lowsel);  /* update current message */
-       seq_save(mp);  /* synchronize message sequences */
-       folder_free(mp);  /* free folder/message structure */
-       context_save();  /* save the context file */
+       context_replace(curfolder, folder);
+       seq_setcur(mp, mp->lowsel);
+       seq_save(mp);
+       folder_free(mp);
+       context_save();
        done(0);
        return 1;
 }
index d1e39b0..170d76b 100644 (file)
@@ -33,7 +33,6 @@ annotate(char *file, char *comp, char *text, int datesw,
                switch (errno) {
                case ENOENT:
                        break;
-
                default:
                        admonish(file, "unable to lock and open");
                        break;
@@ -51,47 +50,40 @@ annotate(char *file, char *comp, char *text, int datesw,
 
        i = annosbr(fd, file, comp, text, datesw, delete, append);
 
-       if (preserve && utime(file, &b) == -1)
+       if (preserve && utime(file, &b) == -1) {
                advise("can't set access and modification times for %s", file);
-
+       }
        lkclose(fd, file);
        return i;
 }
 
 /*
 **  Produce a listing of all header fields (annotations) whose field
-**  name matches comp.  Number the listing if number is set.  Treate the
+**  name matches comp.  Number the listing if number is set.  Treat the
 **  field bodies as path names and just output the last component unless
 **  text is non-NULL.  We don't care what text is set to.
 */
-
 void
 annolist(char *file, char *comp, char *text, int number)
 {
-       int c;  /* current character */
+       int c;
        int count;  /* header field (annotation) counter */
-       char *cp;  /* miscellaneous character pointer */
-       char *field;  /* buffer for header field */
-       int field_size;  /* size of field buffer */
-       FILE *fp;  /* file pointer made from locked file descriptor */
-       int length;  /* length of field name */
+       char *cp;
+       char *field;
+       int field_size;
+       FILE *fp;
+       int length;
        int n;  /* number of bytes written */
-       char *sp;  /* another miscellaneous character pointer */
+       char *sp;
 
-       if ((fp = fopen(file, "r")) == (FILE *)0)
+       if ((fp = fopen(file, "r")) == NULL) {
                adios(file, "unable to open");
+       }
 
-       /*
-       **  Allocate a buffer to hold the header components as they're read in.
-       **  This buffer might need to be quite large, so we grow it as needed.
-       */
+       /* We'll grow this buffer as needed. */
        field = (char *)mh_xmalloc(field_size = 256);
 
-       /*
-       **  Get the length of the field name since we use it often.
-       */
-       length = strlen(comp);
-
+       length = strlen(comp); /* Convenience copy. */
        count = 0;
 
        do {
@@ -102,40 +94,37 @@ annolist(char *file, char *comp, char *text, int number)
                ** on both the field name and the field body which might be
                ** a long path name.
                */
-
                for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
                        if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
                                ungetc(c, fp);
                                c = '\n';
                                break;
                        }
-
                        if (++n >= field_size - 1) {
-                               field = (char *) mh_xrealloc((void *)field, field_size += 256);
-
+                               field = (char *)mh_xrealloc(field,
+                                               field_size += 256);
                                cp = field + n - 1;
                        }
                }
-
-               /* NUL-terminate the field */
                *cp = '\0';
 
-               if (strncasecmp(field, comp, length) == 0 && field[length] == ':') {
-                       for (cp = field + length + 1; *cp == ' ' || *cp == '\t'; cp++)
-                               ;
-
-                       if (number)
+               if (strncasecmp(field, comp, length)==0 &&
+                               field[length] == ':') {
+                       for (cp = field + length + 1;
+                                       *cp == ' ' || *cp == '\t'; cp++) {
+                               continue;
+                       }
+                       if (number) {
                                printf("%d\t", ++count);
-
-                       if (text == NULL && (sp = strrchr(cp, '/')) != NULL)
+                       }
+                       if (!text && (sp = strrchr(cp, '/'))) {
                                cp = sp + 1;
-
+                       }
                        printf("%s\n", cp);
                }
 
-       } while (*field != '\0' && *field != '-');
+       } while (*field && *field != '-');
 
-       /* Clean up. */
        free(field);
        fclose(fp);
 
@@ -151,17 +140,18 @@ annosbr(int fd, char *file, char *comp, char *text, int datesw, int delete,
        char tmpfil[BUFSIZ];
        struct stat st;
        FILE *tmp;
-       int c;  /* current character */
+       int c;
        int count;  /* header field (annotation) counter */
-       char *field = NULL;  /* buffer for header field */
-       int field_size = 0;  /* size of field buffer */
-       FILE *fp = NULL;  /* file pointer made from locked file descriptor */
-       int length;  /* length of field name */
+       char *field = NULL;
+       int field_size = 0;
+       FILE *fp = NULL;
+       int length;
        int n;  /* number of bytes written */
 
        mode = fstat(fd, &st) != NOTOK ? (st.st_mode & 0777) : m_gmprot();
 
-       strncpy(tmpfil, m_mktemp2(file, "annotate", NULL, &tmp), sizeof(tmpfil));
+       strncpy(tmpfil, m_mktemp2(file, "annotate", NULL, &tmp),
+                       sizeof(tmpfil));
        chmod(tmpfil, mode);
 
        /*
@@ -172,17 +162,16 @@ annosbr(int fd, char *file, char *comp, char *text, int datesw, int delete,
        ** a buffer to hold the header components as they're read in.
        ** This buffer is grown as needed later.
        */
-
        if (delete >= -1 || append != 0) {
-               if ((fp = fdopen(fd, "r")) == (FILE *)0)
+               if ((fp = fdopen(fd, "r")) == NULL) {
                        adios(NULL, "unable to fdopen file.");
-
+               }
                field = (char *)mh_xmalloc(field_size = 256);
        }
 
        /*
-       ** We're trying to delete a header field (annotation )if the
-       ** delete flag is not -2 or less.  A  value greater than zero
+       ** We're trying to delete a header field (annotation) if the
+       ** delete flag is greater -2.  A value greater than zero
        ** means that we're deleting the nth header field that matches
        ** the field (component) name.  A value of zero means that
        ** we're deleting the first field in which both the field name
@@ -196,16 +185,9 @@ annosbr(int fd, char *file, char *comp, char *text, int datesw, int delete,
        ** to make it more general.  A value of -1 means to delete all
        ** matching fields.
        */
-
        if (delete >= -1) {
-               /* Get the length of the field name since we use it often. */
-               length = strlen(comp);
-
-               /*
-               **  Initialize the field counter.  This is only used if
-               **  we're deleting by number.
-               */
-               count = 0;
+               length = strlen(comp);  /* convenience copy */
+               count = 0; /* Only used if we're deleting by number. */
 
                /*
                **  Copy lines from the input file to the temporary file
@@ -215,7 +197,6 @@ annosbr(int fd, char *file, char *comp, char *text, int datesw, int delete,
                **  with a - terminate the headers so that we can handle
                **  both drafts and RFC-2822 format messages.
                */
-
                do {
                        /*
                        ** Get a line from the input file, growing the
@@ -225,22 +206,20 @@ annosbr(int fd, char *file, char *comp, char *text, int datesw, int delete,
                        ** field name and the field body which might be
                        ** a long path name.
                        */
-
-                       for (n = 0, cp = field; (c = getc(fp)) != EOF; *cp++ = c) {
-                               if (c == '\n' && (c = getc(fp)) != ' ' && c != '\t') {
+                       for (n=0, cp=field; (c=getc(fp)) != EOF; *cp++ = c) {
+                               if (c == '\n' && (c = getc(fp)) != ' ' &&
+                                               c != '\t') {
                                        ungetc(c, fp);
                                        c = '\n';
                                        break;
                                }
 
                                if (++n >= field_size - 1) {
-                                       field = (char *) mh_xrealloc((void *)field, field_size *= 2);
-
+                                       field = (char *) mh_xrealloc(field,
+                                                       field_size *= 2);
                                        cp = field + n - 1;
                                }
                        }
-
-                       /* NUL-terminate the field */
                        *cp = '\0';
 
                        /*
@@ -275,50 +254,45 @@ annosbr(int fd, char *file, char *comp, char *text, int datesw, int delete,
                        **     going to delete all fields with a
                        **     matching field name.
                        */
-
-                       if (strncasecmp(field, comp, length) == 0 && field[length] == ':') {
-                               if (delete == 0) {
-                                       if (text == NULL)
+                       if (strncasecmp(field, comp, length)==0 &&
+                                       field[length] == ':') {
+                               if (!delete) {
+                                       if (!text) {
                                                break;
-
-                                       for (cp = field + length + 1; *cp == ' ' || *cp == '\t'; cp++)
-                                               ;
-
-                                       if (*text == '/') {
-                                               if (strcmp(cp, text) == 0)
-                                                               break;
                                        }
-                                       else {
-                                               if ((sp = strrchr(cp, '/')) != NULL)
+                                       for (cp=field+length+1;
+                                                       *cp==' ' || *cp=='\t';
+                                                       cp++) {
+                                               continue;
+                                       }
+                                       if (*text == '/' &&
+                                                       strcmp(cp, text)==0) {
+                                               break;
+                                       } else {
+                                               if ((sp = strrchr(cp, '/'))) {
                                                        cp = sp + 1;
-
-                                               if (strcmp(cp, text) == 0)
+                                               }
+                                               if (strcmp(cp, text)==0) {
                                                        break;
+                                               }
                                        }
-                               }
-
-                               else if (delete == -1)
-                                               continue;
-
-                               else if (++count == delete)
+                               } else if (delete == -1) {
+                                       continue;
+                               } else if (++count == delete) {
                                        break;
+                               }
                        }
 
                        /*
                        ** This line wasn't a match so copy it to the
                        ** temporary file.
                        */
-
-                       if ((n = fputs(field, tmp)) == EOF || (c == '\n' && fputc('\n', tmp) == EOF))
+                       if ((n = fputs(field, tmp)) == EOF ||
+                                       (c=='\n' && fputc('\n', tmp)==EOF)) {
                                adios(NULL, "unable to write temporary file.");
-
-               } while (*field != '\0' && *field != '-');
-
-               /*
-               **  Get rid of the field buffer because we're done with it.
-               */
-
-               free((void *)field);
+                       }
+               } while (*field && *field != '-');
+               free(field);
 
        } else {
                /*
@@ -327,49 +301,50 @@ annosbr(int fd, char *file, char *comp, char *text, int datesw, int delete,
                **  prepending.  A special check for no headers is needed
                **  if appending.
                */
-
                if (append) {
                        /*
                        ** Copy lines from the input file to the temporary
                        ** file until we reach the end of the headers.
                        */
-
-                       if ((c = getc(fp)) == '\n')
+                       if ((c = getc(fp)) == '\n') {
                                rewind(fp);
-
-                       else {
+                       } else {
                                putc(c, tmp);
-
                                while ((c = getc(fp)) != EOF) {
                                        putc(c, tmp);
-
                                        if (c == '\n') {
                                                ungetc(c = getc(fp), fp);
-
-                                               if (c == '\n' || c == '-')
+                                               if (c == '\n' || c == '-') {
                                                        break;
+                                               }
                                        }
                                }
                        }
                }
 
-               if (datesw)
+               if (datesw) {
                        fprintf(tmp, "%s: %s\n", comp, dtimenow(0));
+               }
                if ((cp = text)) {
                        do {
-                               while (*cp == ' ' || *cp == '\t')
+                               while (*cp == ' ' || *cp == '\t') {
                                        cp++;
+                               }
                                sp = cp;
-                               while (*cp && *cp++ != '\n')
+                               while (*cp && *cp++ != '\n') {
                                        continue;
-                               if (cp - sp)
-                                       fprintf(tmp, "%s: %*.*s", comp, (int)(cp - sp), (int)(cp - sp), sp);
+                               }
+                               if (cp - sp) {
+                                       fprintf(tmp, "%s: %*.*s", comp,
+                                               (int)(cp - sp),
+                                               (int)(cp - sp), sp);
+                               }
                        } while (*cp);
-                       if (cp[-1] != '\n' && cp != text)
+                       if (cp[-1] != '\n' && cp != text) {
                                putc('\n', tmp);
+                       }
                }
        }
-
        fflush(tmp);
 
        /*
@@ -378,29 +353,27 @@ annosbr(int fd, char *file, char *comp, char *text, int datesw, int delete,
        ** because the stock data copying routine uses the descriptor,
        ** not the pointer.
        */
-
        if (append || delete >= -1) {
-               if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1)
+               if (lseek(fd, (off_t)ftell(fp), SEEK_SET) == (off_t)-1) {
                        adios(NULL, "can't seek.");
+               }
        }
 
        cpydata(fd, fileno(tmp), file, tmpfil);
        fclose(tmp);
 
-       if ((tmpfd = open(tmpfil, O_RDONLY)) == NOTOK)
+       if ((tmpfd = open(tmpfil, O_RDONLY)) == NOTOK) {
                adios(tmpfil, "unable to open for re-reading");
-
+       }
        lseek(fd, (off_t) 0, SEEK_SET);
 
        /*
-       **  We're making the file shorter if we're deleting a
-       **  header field so the file has to be truncated or it
-       **  will contain garbage.
+       **  We're making the file shorter if we're deleting a header field
+       **  so the file has to be truncated or it will contain garbage.
        */
-
-       if (delete >= -1 && ftruncate(fd, 0) == -1)
+       if (delete >= -1 && ftruncate(fd, 0) == -1) {
                adios(tmpfil, "unable to truncate.");
-
+       }
        cpydata(tmpfd, fd, tmpfil, file);
        close(tmpfd);
        unlink(tmpfil);
@@ -410,9 +383,8 @@ annosbr(int fd, char *file, char *comp, char *text, int datesw, int delete,
        ** we're doing piles of files.  Note that this will make the close() in
        ** lkclose() fail, but that failure is ignored so it's not a problem.
        */
-
-       if (delete >= -1)
+       if (delete >= -1) {
                fclose(fp);
-
+       }
        return 0;
 }