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(void);
38 * Asmname(s, c) - assemble name
41 int Asmname(unsigned char *s
, unsigned char *c
) {
43 * code destination (c[3])
48 if ((c
[0] = *s
) == '\0')
50 return(((c
[1] = s
[1]) == '\0') ? 1 : 2);
55 * Delstr(sx) - delete string
60 char buf
[MAXLINE
]; /* message buffer */
63 (void) sprintf(buf
, " bad Delstr(%d) index", sx
);
64 Error(FATAL
, LINE
, buf
, NULL
);
67 while (sx
< (Nstr
- 1)) {
68 Str
[sx
] = Str
[sx
+ 1];
76 * Endword() - end a word
87 * Findchar(nm, l, s, e) - find special character definition and
91 int Findchar(unsigned char *nm
, int l
, unsigned char *s
, int e
) {
95 * e = 0 = find, don't enter
96 * e = 1 = don't find, enter
98 int cmp
, hi
, low
, mid
;
102 c
[1] = (nm
[1] == ' ' || nm
[1] == '\t') ? '\0' : nm
[1];
107 mid
= (low
+ hi
) / 2;
108 if ((cmp
= strncmp((char *)c
, (char *)Schar
[mid
].nm
, 2)) < 0)
115 Free(&Schar
[mid
].str
);
122 Error(FATAL
, LINE
, " at character table limit", NULL
);
126 for (hi
= Nsch
- 1; hi
>= mid
; hi
--)
127 Schar
[hi
+1] = Schar
[hi
];
130 Schar
[mid
].nm
[0] = c
[0];
131 Schar
[mid
].nm
[1] = c
[1];
135 Schar
[mid
].str
= Newstr(s
);
142 * Findhy(s, l, e) - find and optionally enter hyphen
145 int Findhy(unsigned char *s
, int l
, int e
) {
147 * equivalent length l
148 * e = 0 = find, don't enter
149 * e = 1 = enter, don't find
153 for (i
= 0; i
< Nhy
; i
++) {
154 if (Font
[0] == Hychar
[i
].font
)
161 Error(FATAL
, LINE
, " at hyphen limit for font ",
163 Hychar
[i
].font
= Font
[0];
168 Error(WARN
, LINE
, " duplicate hyphen for font ", (char *)Font
);
169 Free(&Hychar
[i
].str
);
171 Hychar
[i
].str
= Newstr(s
);
178 * Findstr(nm, s, e) - find and optionally enter string in Str[]
181 unsigned char *Findstr(unsigned char *nm
, unsigned char *s
, int e
) {
182 /* 2 character string name nm
184 * e = 0 = find, don't enter
185 * e = 1 = enter, don't find
187 unsigned char c
[3]; /* character buffer */
188 int cmp
, hi
, low
, mid
; /* binary search controls */
189 int i
; /* temporary indexes */
190 unsigned char *s1
, *s2
; /* temporary string pointers */
193 c
[1] = (nm
[1] == ' ' || nm
[1] == '\t') ? '\0' : nm
[1];
199 mid
= (low
+ hi
) / 2;
200 if ((cmp
= strncmp((char *)c
, (char *)Str
[mid
].nm
, 2)) < 0)
207 return(Str
[mid
].str
);
213 return((unsigned char *)"");
215 Error(FATAL
, LINE
, " out of space for string ", (char *)c
);
219 for (hi
= Nstr
- 1; hi
>= mid
; hi
--)
224 Str
[mid
].nm
[0] = c
[0];
225 Str
[mid
].nm
[1] = c
[1];
230 return (Str
[mid
].str
= Newstr((unsigned char *)""));
231 i
= (*s
== '"') ? 1 : 0;
232 s1
= Str
[mid
].str
= Newstr(s
+ i
);
234 s2
= s1
+ strlen((char *)s1
);
235 if (s2
> s1
&& *(s2
-1) == '"')
243 * Setroman() - set Roman font
246 static void Setroman(void) {
249 if ((Wordx
+ Fstr
.rl
) >= MAXLINE
)
250 Error(WARN
, LINE
, " word too long", NULL
);
253 for (i
= 0; i
< Fstr
.rl
; i
++) {
254 Word
[Wordx
++] = Fstr
.r
[i
];
263 * Str2word(s, len) - copy len characters from string to Word[]
266 int Str2word(unsigned char *s
, int len
) {
269 for (; len
> 0; len
--, s
++) {
274 if ((Wordx
+ 5) >= MAXLINE
) {
276 Error(WARN
, LINE
, " word too long",
280 Word
[Wordx
++] = Trtbl
[(int)*s
];
281 Word
[Wordx
++] = '\b';
282 Word
[Wordx
++] = Trtbl
[(int)*s
];
283 Word
[Wordx
++] = '\b';
284 Word
[Wordx
++] = Trtbl
[(int)*s
];
287 if (Fontstat
!= Font
[0]) {
290 if ((Wordx
+ Fstr
.bl
) >= MAXLINE
)
293 for (i
= 0; i
< Fstr
.bl
; i
++) {
294 Word
[Wordx
++] = Fstr
.b
[i
];
299 if ((Wordx
+ 1) >= MAXLINE
)
301 Word
[Wordx
++] = Trtbl
[(int)*s
];
306 if ((Wordx
+ 3) >= MAXLINE
)
309 Word
[Wordx
++] = '\b';
310 Word
[Wordx
++] = Trtbl
[(int)*s
];
313 if (Fontstat
!= 'I') {
316 if ((Wordx
+ Fstr
.itl
) >= MAXLINE
)
319 for (i
= 0; i
< Fstr
.itl
; i
++) {
320 Word
[Wordx
++] = Fstr
.it
[i
];
325 if ((Wordx
+ 1) >= MAXLINE
)
327 Word
[Wordx
++] = Trtbl
[(int)*s
];
330 /* else fall through */
334 if ((Wordx
+ 1) >= MAXLINE
)
336 Word
[Wordx
++] = Trtbl
[(int)*s
];