commit 5b5bb82ec00cb7b7b02ac97550683e2f377e6f13
Author: sin <[email protected]>
Date:   Fri Nov 21 16:20:15 2014 +0000

    Factor out readrune and writerune

diff --git a/Makefile b/Makefile
index 100632d..14dc982 100644
--- a/Makefile
+++ b/Makefile
@@ -20,9 +20,11 @@ HDR =\
 
 LIBUTF = libutf.a
 LIBUTFSRC =\
+       libutf/readrune.c\
        libutf/rune.c\
        libutf/runetype.c\
-       libutf/utf.c
+       libutf/utf.c\
+       libutf/writerune.c
 
 LIBUTIL = libutil.a
 LIBUTILSRC =\
diff --git a/expand.c b/expand.c
index 23595bb..6c453fd 100644
--- a/expand.c
+++ b/expand.c
@@ -47,47 +47,6 @@ main(int argc, char *argv[])
        return 0;
 }
 
-int
-in(const char *file, FILE *fp, Rune *r)
-{
-       char buf[UTFmax];
-       int c, i;
-
-       c = fgetc(fp);
-       if (ferror(fp))
-               eprintf("%s: read error:", file);
-       if (feof(fp))
-               return 0;
-       if (c < Runeself) {
-               *r = (Rune)c;
-               return 1;
-       }
-       buf[0] = c;
-       for (i = 1; ;) {
-               c = fgetc(fp);
-               if (ferror(fp))
-                       eprintf("%s: read error:", file);
-               if (feof(fp))
-                       return 0;
-               buf[i++] = c;
-               if (fullrune(buf, i))
-                       return chartorune(r, buf);
-       }
-}
-
-static void
-out(Rune *r)
-{
-       char buf[UTFmax];
-       int len;
-
-       if ((len = runetochar(buf, r))) {
-               fwrite(buf, len, 1, stdout);
-               if (ferror(stdout))
-                       eprintf("stdout: write error:");
-       }
-}
-
 static int
 expand(const char *file, FILE *fp, int tabstop)
 {
@@ -96,7 +55,7 @@ expand(const char *file, FILE *fp, int tabstop)
        int bol = 1;
 
        for (;;) {
-               if (!in(file, fp, &r))
+               if (!readrune(file, fp, &r))
                        break;
 
                switch (r) {
@@ -115,18 +74,18 @@ expand(const char *file, FILE *fp, int tabstop)
                        if (col)
                                col--;
                        bol = 0;
-                       out(&r);
+                       writerune(&r);
                        break;
                case '\n':
                        col = 0;
                        bol = 1;
-                       out(&r);
+                       writerune(&r);
                        break;
                default:
                        col++;
                        if (r != ' ')
                                bol = 0;
-                       out(&r);
+                       writerune(&r);
                        break;
                }
        }
diff --git a/libutf/readrune.c b/libutf/readrune.c
new file mode 100644
index 0000000..407182b
--- /dev/null
+++ b/libutf/readrune.c
@@ -0,0 +1,46 @@
+/* See LICENSE file for copyright and license details. */
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "../utf.h"
+
+int
+readrune(const char *file, FILE *fp, Rune *r)
+{
+       char buf[UTFmax];
+       int c, i;
+
+       if ((c = fgetc(fp)) == EOF) {
+               if (ferror(fp)) {
+                       fprintf(stderr, "%s: read error: %s\n",
+                               file, strerror(errno));
+                       exit(1);
+               }
+               return 0;
+       }
+
+       if (c < Runeself) {
+               *r = (Rune)c;
+               return 1;
+       }
+
+       buf[0] = c;
+       for (i = 1; ;) {
+               if ((c = fgetc(fp)) == EOF) {
+                       if (ferror(fp)) {
+                               fprintf(stderr, "%s: read error: %s\n",
+                                       file, strerror(errno));
+                               exit(1);
+                       }
+                       return 0;
+               }
+               buf[i++] = c;
+               if (fullrune(buf, i)) {
+                       chartorune(r, buf);
+                       break;
+               }
+       }
+       return 1;
+}
diff --git a/libutf/writerune.c b/libutf/writerune.c
new file mode 100644
index 0000000..0615860
--- /dev/null
+++ b/libutf/writerune.c
@@ -0,0 +1,23 @@
+/* See LICENSE file for copyright and license details. */
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "../utf.h"
+
+void
+writerune(Rune *r)
+{
+       char buf[UTFmax];
+       int n;
+
+       if ((n = runetochar(buf, r)) > 0) {
+               fwrite(buf, n, 1, stdout);
+               if (ferror(stdout)) {
+                       fprintf(stderr, "stdout: write error: %s\n",
+                               strerror(errno));
+                       exit(1);
+               }
+       }
+}
diff --git a/utf.h b/utf.h
index fe039ff..44c5bba 100644
--- a/utf.h
+++ b/utf.h
@@ -18,8 +18,7 @@
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  * DEALINGS IN THE SOFTWARE.
  */
-
-#include <stddef.h>
+#include <stdio.h>
 
 typedef int Rune;
 
@@ -49,3 +48,6 @@ int isspacerune(Rune);
 int istitlerune(Rune);
 int isupperrune(Rune);
 int isdigitrune(Rune);
+
+int readrune(const char *, FILE *, Rune *);
+void writerune(Rune *);


Reply via email to