- if (argc != 2) {
- fprintf(stderr, "usage: view file\n");
- return EXIT_FAILURE;
- } else {
- fname = argv[1];
- if ((fp = fopen(fname, "r")) == (FILE *)NULL) {
- perror(fname);
- return EXIT_FAILURE;
+/*
+ * Allocate a string into an array of chtype's. If UTF-8 mode is
+ * active, translate the string accordingly.
+ */
+static NCURSES_CH_T *
+ch_dup(char *src)
+{
+ unsigned len = strlen(src);
+ NCURSES_CH_T *dst = typeMalloc(NCURSES_CH_T, len + 1);
+ unsigned j, k;
+#if USE_WIDEC_SUPPORT
+ wchar_t wstr[CCHARW_MAX + 1];
+ wchar_t wch;
+ int l = 0;
+ mbstate_t state;
+ size_t rc;
+ int width;
+#endif
+
+#if USE_WIDEC_SUPPORT
+ memset(&state, 0, sizeof(state));
+#endif
+ for (j = k = 0; j < len; j++) {
+#if USE_WIDEC_SUPPORT
+ rc = mbrtowc(&wch, src + j, len - j, &state);
+ if (rc == (size_t) -1 || rc == (size_t) -2)
+ break;
+ j += rc - 1;
+ if ((width = wcwidth(wch)) < 0)
+ break;
+ if ((width > 0 && l > 0) || l == CCHARW_MAX) {
+ wstr[l] = L'\0';
+ l = 0;
+ if (setcchar(dst + k, wstr, 0, 0, NULL) != OK)
+ break;
+ ++k;