2 * string.c - string support functions for cawf(1)
6 * Copyright (c) 1991 Purdue University Research Foundation,
7 * West Lafayette, Indiana 47907. All rights reserved.
9 * Written by Victor A. Abell <abe@mace.cc.purdue.edu>, Purdue
10 * University Computing Center. Not derived from licensed software;
11 * derived from awf(1) by Henry Spencer of the University of Toronto.
13 * Permission is granted to anyone to use this software for any
14 * purpose on any computer system, and to alter it and redistribute
15 * it freely, subject to the following restrictions:
17 * 1. The author is not responsible for any consequences of use of
18 * this software, even if they arise from flaws in it.
20 * 2. The origin of this software must not be misrepresented, either
21 * by explicit claim or by omission. Credits must appear in the
24 * 3. Altered versions must be plainly marked as such, and must not
25 * be misrepresented as being the original software. Credits must
26 * appear in the documentation.
28 * 4. This notice may not be removed or altered.
34 static void Setroman();
38 * Asmname(s, c) - assemble name
42 unsigned char *s
; /* pointer to name */
43 unsigned char *c
; /* code destination (c[3]) */
47 while (*s
&& *s
== ' ')
49 if ((c
[0] = *s
) == '\0')
51 return(((c
[1] = s
[1]) == '\0') ? 1 : 2);
56 * Delstr(sx) - delete string
61 int sx
; /* string index */
63 char buf
[MAXLINE
]; /* message buffer */
66 (void) sprintf(buf
, " bad Delstr(%d) index", sx
);
67 Error(FATAL
, LINE
, buf
, NULL
);
70 while (sx
< (Nstr
- 1)) {
71 Str
[sx
] = Str
[sx
+ 1];
79 * Endword() - end a word
92 * Findchar(nm, l, s, e) - find special character definition and
97 unsigned char *nm
; /* character name */
98 int l
; /* effective length */
99 unsigned char *s
; /* value string */
100 int e
; /* 0 = find, don't enter
101 * 1 = don't find, enter */
103 int cmp
, hi
, low
, mid
;
107 c
[1] = (nm
[1] == ' ' || nm
[1] == '\t') ? '\0' : nm
[1];
112 mid
= (low
+ hi
) / 2;
113 if ((cmp
= strncmp((char *)c
, (char *)Schar
[mid
].nm
, 2)) < 0)
120 Free(&Schar
[mid
].str
);
127 Error(FATAL
, LINE
, " at character table limit", NULL
);
131 for (hi
= Nsch
- 1; hi
>= mid
; hi
--)
132 Schar
[hi
+1] = Schar
[hi
];
135 Schar
[mid
].nm
[0] = c
[0];
136 Schar
[mid
].nm
[1] = c
[1];
140 Schar
[mid
].str
= Newstr(s
);
147 * Findhy(s, l, e) - find and optionally enter hyphen
151 unsigned char *s
; /* value string */
152 int l
; /* equivalent length */
153 int e
; /* 0 = find, don't enter
154 * 1 = enter, don't find */
158 for (i
= 0; i
< Nhy
; i
++) {
159 if (Font
[0] == Hychar
[i
].font
)
166 Error(FATAL
, LINE
, " at hyphen limit for font ",
168 Hychar
[i
].font
= Font
[0];
173 Error(WARN
, LINE
, " duplicate hyphen for font ", (char *)Font
);
174 Free(&Hychar
[i
].str
);
176 Hychar
[i
].str
= Newstr(s
);
183 * Findstr(nm, s, e) - find and optionally enter string in Str[]
188 unsigned char *nm
; /* 2 character string name */
189 unsigned char *s
; /* string value */
190 int e
; /* 0 = find, don't enter
191 * 1 = enter, don't find */
193 unsigned char c
[3]; /* character buffer */
194 int cmp
, hi
, low
, mid
; /* binary search controls */
195 int i
; /* temporary indexes */
196 unsigned char *s1
, *s2
; /* temporary string pointers */
199 c
[1] = (nm
[1] == ' ' || nm
[1] == '\t') ? '\0' : nm
[1];
205 mid
= (low
+ hi
) / 2;
206 if ((cmp
= strncmp((char *)c
, (char *)Str
[mid
].nm
, 2)) < 0)
213 return(Str
[mid
].str
);
219 return((unsigned char *)"");
221 Error(FATAL
, LINE
, " out of space for string ", (char *)c
);
225 for (hi
= Nstr
- 1; hi
>= mid
; hi
--)
230 Str
[mid
].nm
[0] = c
[0];
231 Str
[mid
].nm
[1] = c
[1];
236 return (Str
[mid
].str
= Newstr((unsigned char *)""));
237 i
= (*s
== '"') ? 1 : 0;
238 s1
= Str
[mid
].str
= Newstr(s
+ i
);
240 s2
= s1
+ strlen((char *)s1
);
241 if (s2
> s1
&& *(s2
-1) == '"')
249 * Setroman() - set Roman font
257 if ((Wordx
+ Fstr
.rl
) >= MAXLINE
)
258 Error(WARN
, LINE
, " word too long", NULL
);
261 for (i
= 0; i
< Fstr
.rl
; i
++) {
262 Word
[Wordx
++] = Fstr
.r
[i
];
271 * Str2word(s, len) - copy len characters from string to Word[]
280 for (; len
> 0; len
--, s
++) {
285 if ((Wordx
+ 5) >= MAXLINE
) {
287 Error(WARN
, LINE
, " word too long",
291 Word
[Wordx
++] = Trtbl
[(int)*s
];
292 Word
[Wordx
++] = '\b';
293 Word
[Wordx
++] = Trtbl
[(int)*s
];
294 Word
[Wordx
++] = '\b';
295 Word
[Wordx
++] = Trtbl
[(int)*s
];
298 if (Fontstat
!= Font
[0]) {
301 if ((Wordx
+ Fstr
.bl
) >= MAXLINE
)
304 for (i
= 0; i
< Fstr
.bl
; i
++) {
305 Word
[Wordx
++] = Fstr
.b
[i
];
310 if ((Wordx
+ 1) >= MAXLINE
)
312 Word
[Wordx
++] = Trtbl
[(int)*s
];
317 if ((Wordx
+ 3) >= MAXLINE
)
320 Word
[Wordx
++] = '\b';
321 Word
[Wordx
++] = Trtbl
[(int)*s
];
324 if (Fontstat
!= 'I') {
327 if ((Wordx
+ Fstr
.itl
) >= MAXLINE
)
330 for (i
= 0; i
< Fstr
.itl
; i
++) {
331 Word
[Wordx
++] = Fstr
.it
[i
];
336 if ((Wordx
+ 1) >= MAXLINE
)
338 Word
[Wordx
++] = Trtbl
[(int)*s
];
341 /* else fall through */
345 if ((Wordx
+ 1) >= MAXLINE
)
347 Word
[Wordx
++] = Trtbl
[(int)*s
];