+static int
+op_length(const char *src, int offset)
+{
+ int result = 0;
+ int ch;
+ if (offset > 0 && src[offset - 1] == '\\') {
+ result = 0;
+ } else {
+ result++; /* for '%' mark */
+ ch = src[offset + result];
+ if (TcOutput()) {
+ if (ch == '>') {
+ result += 3;
+ } else if (ch == '+') {
+ result += 2;
+ } else {
+ result++;
+ }
+ } else if (ch == '\'') {
+ result += 3;
+ } else if (ch == L_CURL[0]) {
+ int n = result;
+ while ((ch = src[offset + n]) != '\0') {
+ if (ch == R_CURL[0]) {
+ result = ++n;
+ break;
+ }
+ n++;
+ }
+ } else if (strchr("pPg", ch) != 0) {
+ result += 2;
+ } else {
+ result++; /* ordinary operator */
+ }
+ }
+ return result;
+}
+
+/*
+ * When wrapping too-long strings, avoid splitting a backslash sequence, or
+ * a terminfo '%' operator. That will leave things a little ragged, but avoids
+ * a stray backslash at the end of the line, as well as making the result a
+ * little more readable.
+ */
+static int
+find_split(const char *src, int step, int size)
+{
+ int result = size;
+ int n;
+ if (size > 0) {
+ /* check if that would split a backslash-sequence */
+ int mark = size;
+ for (n = size - 1; n > 0; --n) {
+ int ch = UChar(src[step + n]);
+ if (ch == '\\') {
+ if (n > 0 && src[step + n - 1] == ch)
+ --n;
+ mark = n;
+ break;
+ } else if (!isalnum(ch)) {
+ break;
+ }
+ }
+ if (mark < size) {
+ result = mark;
+ } else {
+ /* check if that would split a backslash-sequence */
+ for (n = size - 1; n > 0; --n) {
+ int ch = UChar(src[step + n]);
+ if (ch == '%') {
+ int need = op_length(src, step + n);
+ if ((n + need) > size)
+ mark = n;
+ break;
+ }
+ }
+ if (mark < size) {
+ result = mark;
+ }
+ }
+ }
+ return result;
+}
+
+/*
+ * If we are going to wrap lines, we cannot leave literal spaces because that
+ * would be ambiguous if we split on that space.
+ */
+static char *
+fill_spaces(const char *src)
+{
+ const char *fill = "\\s";
+ size_t need = strlen(src);
+ size_t size = strlen(fill);
+ char *result = 0;
+ int pass;
+ int s, d;
+ for (pass = 0; pass < 2; ++pass) {
+ for (s = d = 0; src[s] != '\0'; ++s) {
+ if (src[s] == ' ') {
+ if (pass) {
+ strcpy(&result[d], fill);
+ d += (int) size;
+ } else {
+ need += size;
+ }
+ } else {
+ if (pass) {
+ result[d++] = src[s];
+ } else {
+ ++d;
+ }
+ }
+ }
+ if (pass) {
+ result[d] = '\0';
+ } else {
+ result = malloc(need + 1);
+ if (result == 0)
+ failed("fill_spaces");
+ }
+ }
+ return result;
+}
+