Debian Codegraph @

names.c

raw symbols SCIP
1 /*** names.c ******************************************************************
2 **
3 ** This file is part of BibTool.
4 ** It is distributed under the GNU General Public License.
5 ** See the file COPYING for details.
6 **
7 ** (c) 1996-2020 Gerd Neugebauer
8 **
9 ** Net: [email protected]
10 **
11 ** This program is free software; you can redistribute it and/or modify
12 ** it under the terms of the GNU General Public License as published by
13 ** the Free Software Foundation; either version 2, or (at your option)
14 ** any later version.
15 **
16 ** This program is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ** GNU General Public License for more details.
20 **
21 ** You should have received a copy of the GNU General Public License
22 ** along with this program; if not, write to the Free Software
23 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 **
25 ******************************************************************************/
26
27 #include <bibtool/general.h>
28 #include <bibtool/names.h>
29 #include <bibtool/error.h>
30 #include <bibtool/symbols.h>
31 #include <bibtool/stack.h>
32 #include <bibtool/sbuffer.h>
33 #include <bibtool/s_parse.h>
34
35 /*****************************************************************************/
36 /* Internal Programs */
37 /*===========================================================================*/
38
39 #ifdef __STDC__
40 #define _ARG(A) A
41 #else
42 #define _ARG(A) ()
43 #endif
44 NameNode name_format _ARG((String s)); /* names.c */
45 String pp_list_of_names _ARG((String *wa,NameNode format,String trans,int max,String comma,String and,char *namesep,char *etal));/* names.c*/
46 #ifdef STANDALONE
47 char * pp_names _ARG((char *s,NameNode format,String trans,int max,char *namesep,char *etal));/* names.c*/
48 #endif
49 static NameNode new_name_node _ARG((int type,int strip,int trim,Symbol pre,Symbol mid,Symbol post));/* names.c*/
50 static bool is_jr _ARG((String s, bool eager)); /* names.c */
51 static bool is_lower_word _ARG((String s)); /* names.c */
52 static void initial _ARG((String s,String trans,int len,StringBuffer *sb));/* names.c*/
53 static void pp_one_name _ARG((StringBuffer *sb,String *w,NameNode format,String trans,int len,String comma,int commas));/* names.c*/
54
55 #ifdef BIBTEX_SYNTAX
56 static void set_type _ARG((char **sp,char **midp));/* names.c */
57 void set_name_format _ARG((NameNode *nodep,char *s));/* names.c */
58 #endif
59
60 /*****************************************************************************/
61 /* External Programs */
62 /*===========================================================================*/
63
64 /*---------------------------------------------------------------------------*/
65
66
67 /*-----------------------------------------------------------------------------
68 ** Function*: new_name_node()
69 ** Purpose: Allocate a name node structure and fill it with the values
70 ** given as arguments.
71 ** If no more memory is available then en error is raised and
72 ** the program is terminated.
73 ** Arguments:
74 ** type
75 ** strip
76 ** trim
77 ** pre
78 ** mid
79 ** post
80 ** Returns: The new instance of a node.
81 **___________________________________________________ */
82 static NameNode new_name_node(type, strip, trim, pre, mid, post)/* */
83 int type; /* */
84 int strip; /* */
85 int trim; /* */
86 Symbol pre; /* */
87 Symbol mid; /* */
88 Symbol post; /* */
89 { NameNode node; /* */
90 /* */
91 if ( (node = (NameNode)malloc(sizeof(SNameNode))) == NameNULL )/* */
92 { OUT_OF_MEMORY("NameNode"); } /* */
93 NameType(node) = type; /* */
94 NameStrip(node) = strip; /* */
95 NameTrim(node) = trim; /* */
96 NamePre(node) = pre; /* */
97 NameMid(node) = mid; /* */
98 NamePost(node) = post; /* */
99 NextName(node) = NameNULL; /* */
100 return node; /* */
101 } /*------------------------*/
102
103 #ifdef UNUSED
104
105 /*-----------------------------------------------------------------------------
106 ** Function*: free_name_node()
107 ** Purpose: Release the memory allocated for a node together with all
108 ** nodes reachable via a |NextName| chain.
109 ** It is not save to access a freed node.
110 ** Arguments:
111 ** node Node to free.
112 ** Returns: nothing
113 **___________________________________________________ */
114 static void free_name_node(node) /* */
115 NameNode node; /* */
116 { NameNode next; /* */
117 /* */
118 while ( node ) /* */
119 { next = NextName(node); /* */
120 free(node); /* */
121 node = next; /* */
122 } /* */
123 } /*------------------------*/
124 #endif
125
126 #ifdef DEBUG
127 /*-----------------------------------------------------------------------------
128 ** Function*: dump_name_node()
129 ** Purpose:
130 **
131 **
132 ** Arguments:
133 ** n
134 ** Returns: nothing
135 **___________________________________________________ */
136 static void dump_name_node(n) /* */
137 NameNode n; /* */
138 { /* */
139 fputc('%',err_file); /* */
140 switch ( NameType(n) & NameTranslationMask ) /* */
141 { case NameLower: fputc('-',err_file); break; /* */
142 case NameUpper: fputc('+',err_file); break; /* */
143 case NameExternal: fputc('*',err_file); break; /* */
144 } /* */
145 if ( NameStrip(n) >= 0 ) /* */
146 { fprintf(err_file,"%d",NameStrip(n)); } /* */
147 if ( NameTrim(n) >= 0 ) /* */
148 { fprintf(err_file,".%d",NameTrim(n)); } /* */
149 switch ( NameType(n) & NameTypeMask) /* */
150 { case NameFirst: fputc('f',err_file); break; /* */
151 case NameLast: fputc('l',err_file); break; /* */
152 case NameVon: fputc('v',err_file); break; /* */
153 case NameJr: fputc('j',err_file); break; /* */
154 case NameString: fputc('s',err_file); break; /* */
155 default: fputc('?',err_file); break; /* */
156 } /* */
157 fprintf(err_file, /* */
158 "[%s][%s][%s]\n", /* */
159 (NamePre(n)?(char*)NamePre(n):""), /* */
160 (NameMid(n)?(char*)NameMid(n):""), /* */
161 (NamePost(n)?(char*)NamePost(n):"")); /* */
162 } /*------------------------*/
163 #else
164 #define dump_name_node(N)
165 #endif
166
167
168 #ifdef BIBTEX_SYNTAX
169 /*-----------------------------------------------------------------------------
170 ** Function*: set_type()
171 ** Purpose:
172 **
173 **
174 ** Arguments:
175 ** sp
176 ** midp
177 ** Returns:
178 **___________________________________________________ */
179 static void set_type(sp,midp) /* */
180 char **sp; /* */
181 char **midp; /* */
182 { char c, *s, *mid; /* */
183 int type = 0; /* */
184 /* */
185 s = *sp; /* */
186 if ( *(s+1) == '{' ) /* */
187 { mid = ++s; /* */
188 while ( *s && *s != '}' ) s++; /* */
189 c = *s; *s = '\0'; /* */
190 *midp = symbol(++mid); /* */
191 *s = c; /* */
192 } /* */
193 *sp = s; /* */
194 } /*------------------------*/
195
196 #define SetType(T,C) type = T; \
197 c = *s; *s = '\0'; pre = symbol(pre); *s = c; \
198 if ( *(s+1) == C ) { strip = -1; s++; } \
199 set_type(&s,&mid); \
200 post = s+1
201
202 /*-----------------------------------------------------------------------------
203 ** Function: set_name_format()
204 ** Purpose:
205 **
206 **
207 ** Arguments:
208 ** nodep
209 ** s
210 ** Returns: nothing
211 **___________________________________________________ */
212 void set_name_format(nodep,s) /* */
213 NameNode *nodep; /* */
214 char *s; /* */
215 { int n, /* */
216 type, /* */
217 strip, /* */
218 trim = 0; /* */
219 String mid, /* */
220 pre, /* */
221 post; /* */
222 Uchar c; /* */
223 char buffer[256]; /* */
224 #define UseBuffer(S) strcpy(buffer,S); s = buffer
225 /* */
226 free_name_node(*nodep); /* */
227 *nodep = NameNULL; /* */
228 /* */
229 if (strcmp(s,"short")==0) { UseBuffer("{ll{-}}"); }/* */
230 else if (strcmp(s,"med") ==0) { UseBuffer("{f{}}{ll{-}}"); }/* */
231 /* */
232 while ( *s ) /* */
233 { /* */
234 if (*s && *s != '{') /* */
235 { mid = s; /* */
236 while ( *s && *s != '{' ) s++; /* */
237 c = *s; /* */
238 *s = '\0'; /* */
239 mid = symbol(mid); /* */
240 *s = c; /* */
241 *nodep = new_name_node(NameString, /* */
242 -1, /* */
243 -1, /* */
244 StringNULL, /* */
245 mid, /* */
246 StringNULL); /* */
247 nodep = &NextName(*nodep); /* */
248 /* */
249 if ( c == '\0' ) return; /* */
250 } /* */
251 /* */
252 pre = ++s; /* */
253 type = NoName; /* */
254 mid = sym_space; /* */
255 strip = 1; /* */
256 for ( n = 1 ; n > 0; ++s ) /* */
257 { switch (*s) /* */
258 { case '{': n++; break; /* */
259 case '}': n--; break; /* */
260 case 'f': SetType(NameFirst,'f'); break; /* */
261 case 'l': SetType(NameLast,'l'); break; /* */
262 case 'v': SetType(NameVon,'v'); break; /* */
263 case 'j': SetType(NameJr,'j'); break; /* */
264 case '\0': return; /* */
265 } /* */
266 } /* */
267 /* */
268 if ( type != NoName ) /* */
269 { c = *(s-1); /* */
270 *(s-1) = '\0'; /* */
271 post = symbol(post); /* */
272 *(s-1) = c; /* */
273 *nodep = new_name_node(type, strip, trim, pre, mid, post);/* */
274 dump_name_node(*nodep); /* */
275 nodep = &NextName(*nodep); /* */
276 } /* */
277 } /* */
278 } /*------------------------*/
279
280 #endif
281
282 /*-----------------------------------------------------------------------------
283 ** Function: name_format()
284 ** Purpose:
285 **
286 **
287 ** Arguments:
288 ** s
289 ** Returns:
290 **___________________________________________________ */
291 NameNode name_format(s) /* */
292 String s; /* */
293 { int type, /* */
294 strip, /* */
295 trim; /* */
296 Symbol pre, /* */
297 mid, /* */
298 post; /* */
299 Uchar c; /* */
300 String cp; /* */
301 NameNode node = NameNULL; /* */
302 NameNode *nnp; /* */
303 static char *msg = "Missing ']' in format string.";/* */
304 #define OptionalArg(S) \
305 if ( *cp == '[' ) \
306 { String sss; \
307 for ( sss = ++cp; *cp && *cp != ']'; cp++) ; \
308 if ( *cp ) \
309 { c = *cp; *cp = '\0'; S = symbol(sss); *(cp++) = c; } \
310 else { S = sym_empty; WARNING(msg); } \
311 } else { S = sym_empty; }
312 /* */
313 nnp = &node; /* */
314 cp = s; /* */
315 while (*cp) /* */
316 { /* */
317 if ( *cp == '%' ) /* */
318 { /* */
319 switch ( *++cp ) /* */
320 { case '+': type = NameUpper; cp++; break;/* */
321 case '-': type = NameLower; cp++; break;/* */
322 case '*': type = NameExternal; cp++; break;/* */
323 default: type = NameNone; /* */
324 } /* */
325 if ( is_digit(*cp) ) /* */
326 { strip = 0; /* */
327 while(is_digit(*cp)) /* */
328 { strip = strip*10 + (*cp++) - '0';} /* */
329 } else { strip = -1; } /* */
330 /* */
331 if ( *cp == '.' ) /* */
332 { trim = 0; /* */
333 cp++; /* */
334 while(is_digit(*cp)) /* */
335 { trim = trim*10 + (*cp++) - '0';} /* */
336 } else { trim = -1; } /* */
337 /* */
338 switch ( *cp ) /* */
339 { case 'f': type |= NameFirst; break; /* */
340 case 'l': type |= NameLast; break; /* */
341 case 'v': type |= NameVon; break; /* */
342 case 'j': type |= NameJr; break; /* */
343 default: type |= NoName; /* */
344 error(ERR_ERROR|ERR_POINT, /* */
345 (String)"Wrong type of format.", /* */
346 StringNULL, /* */
347 StringNULL, /* */
348 s, /* */
349 cp, 0, NO_SYMBOL); /* */
350 } /* */
351 cp++; /* */
352 OptionalArg(pre); /* */
353 OptionalArg(mid); /* */
354 OptionalArg(post); /* */
355 } /* */
356 else /* */
357 { String mmm; /* */
358 mmm = cp; /* */
359 while ( *cp && *cp != '%' ) { cp++; } /* */
360 c = *cp; /* */
361 *cp = '\0'; /* */
362 mid = symbol(mmm); /* */
363 *cp = c; /* */
364 type = NameString; /* */
365 pre = NULL; /* */
366 post = NULL; /* */
367 strip = -1; /* */
368 trim = -1; /* */
369 } /* */
370 if ( type != NoName ) /* */
371 { *nnp = new_name_node(type, /* */
372 strip, /* */
373 trim, /* */
374 pre, /* */
375 mid, /* */
376 post); /* */
377 nnp = &NextName(*nnp); /* */
378 } /* */
379 } /* */
380 return node; /* */
381 } /*------------------------*/
382
383 /*-----------------------------------------------------------------------------
384 ** Function: pp_list_of_names()
385 ** Purpose: Pretty-print a list of names.
386 **
387 **
388 ** Arguments:
389 ** wa Word array of name constituents
390 ** format the format of the name
391 ** trans the translation table
392 ** max the limit for the number of names to be formatted
393 ** comma ","
394 ** and name separator
395 ** namesep
396 ** etal
397 ** Returns: Pointer to static string which is reused upon the next
398 ** invocation of this function.
399 **___________________________________________________ */
400 String pp_list_of_names(wa,format,trans,max,comma,and,namesep,etal)/* */
401 String *wa; /* */
402 NameNode format; /* */
403 String trans; /* */
404 int max; /* */
405 String comma; /* */
406 String and; /* */
407 char *namesep; /* */
408 char *etal; /* */
409 { String *w; /* */
410 String word; /* */
411 int commas; /* */
412 bool first = true; /* */
413 static StringBuffer *sb = (StringBuffer*)0; /* */
414 /* */
415 if ( sb == (StringBuffer*)0 /* */
416 && (sb=sbopen()) == (StringBuffer*)0 ) /* */
417 { OUT_OF_MEMORY("name list"); } /* */
418 else /* */
419 { sbrewind(sb); } /* */
420 /* */
421 while (*wa) /* */
422 { /* */
423 if (*(wa+1) == NULL /* Look at the end */
424 && strcmp((char*)(*wa), "others") == 0) /* for `and others' */
425 { sbputs(etal, sb); /* */
426 break; /* */
427 } /* */
428 if (max >= 0 && --max < 0) /* */
429 { sbputs(etal, sb); /* */
430 break; /* */
431 } /* */
432 /* */
433 commas = 0; /* */
434 for (w = wa; *w && *w != and; w++) /* */
435 { if (*w == comma) commas++; /* */
436 DebugPrint1(*w); /* */
437 } /* */
438 word = *w; /* */
439 *w = NULL; /* */
440 if (first) { first = false; } /* */
441 else { sbputs(namesep, sb); } /* */
442 pp_one_name(sb, /* */
443 wa, /* */
444 format, /* */
445 trans, /* */
446 (int)(w-wa), /* */
447 comma, /* */
448 commas); /* */
449 *w = word; /* */
450 wa = (word != NULL ? ++w : w); /* */
451 } /* */
452 /* */
453 return (String)sbflush(sb); /* */
454 } /*------------------------*/
455
456 /*-----------------------------------------------------------------------------
457 ** Function: pp_one_name()
458 ** Type: void
459 ** Purpose:
460 **
461 ** Arguments:
462 ** sb
463 ** w
464 ** format
465 ** trans
466 ** len
467 ** comma
468 ** commas
469 ** Returns: nothing
470 **___________________________________________________ */
471 static void pp_one_name(sb, w, format, trans, len, comma, commas)/* */
472 StringBuffer *sb; /* */
473 String *w; /* */
474 NameNode format; /* */
475 String trans; /* */
476 int len; /* */
477 String comma; /* */
478 int commas; /* */
479 { NameNode nn; /* */
480 char t; /* */
481 char *type; /* */
482 String tr; /* */
483 int i, j; /* */
484 bool again; /* */
485 int first, last, von, jr; /* */
486 /* */
487 first = last = von = jr = 0; /* */
488 if (len < 1) return; /* */
489 /* */
490 if ( (type=(char*)malloc((size_t)len)) == NULL ) /* */
491 { OUT_OF_MEMORY("name type"); } /* */
492 /* */
493 #ifdef DEBUG
494 ErrPrint("+++ BibTool: Name parts: "); /* */
495 for (i = 0; i < len; i++) /* */
496 { ErrPrintF("\n\t/%s/ ",w[i]); } /* */
497 ErrC('\n'); /* */
498 #endif
499 /* */
500 if ( commas == 0 ) /*------------------------*/
501 { /* ff vv ll */
502 j = len - 1; /* ff vv ll jr */
503 if ( is_jr(w[j], true) ) /* */
504 { type[j--] = 'j'; jr++; } /* */
505 if ( j >= 0 ) /* */
506 { type[j] = 'l'; last++; } /* */
507 i = 0; /* */
508 while ( i < j && !is_lower_word(w[i]) ) /* */
509 { type[i++] = 'f'; first++; } /* */
510 while ( i < j && is_lower_word(w[i]) ) /* */
511 { type[i++] = 'v'; von++; } /* */
512 while ( i < j ) /* */
513 { type[i++] = 'l'; last++; } /* */
514 } /* */
515 else if ( commas == 1 /*------------------------*/
516 && len > 2 /* ff vv ll, jj */
517 && w[len-2] == comma /* */
518 && is_jr(w[len-1], false) ) /* */
519 { j = len - 1; /* */
520 type[j--] = 'j'; jr++; /* */
521 type[j--] = ','; /* */
522 if ( j >= 0 ) { type[j--] = 'l'; last++; } /* */
523 i = 0; /* */
524 while ( i < j && !is_lower_word(w[i]) ) /* */
525 { type[i++] = 'f'; first++; } /* */
526 while ( i < j && is_lower_word(w[i]) ) /* */
527 { type[i++] = 'v'; von++; } /* */
528 while ( i < j ) /* */
529 { type[i++] = 'l'; last++; } /* */
530 } /* */
531 else /* */
532 { i = 0; /*------------------------*/
533 if ( is_lower_word(w[i]) ) /* vv ll, ff */
534 { while ( i < len && /* */
535 w[i] != comma && /* */
536 is_lower_word(w[i]) ) /* */
537 { type[i++] = 'v'; von++; } /* */
538 } /* */
539 /*------------------------*/
540 while ( i < len && w[i] != comma ) /* ll, ff vv */
541 { type[i++] = 'l'; last++; } /* */
542 if (i < len) { type[i++] = ','; } /* */
543 /*------------------------*/
544 if ( commas == 2 ) /* ll, jj, ff vv */
545 { while ( i < len && w[i] != comma ) /* */
546 { type[i++] = 'j'; jr++; } /* */
547 if (i < len) { type[i++] = ','; } /* */
548 } /* */
549 while ( i < len && !is_lower_word(w[i]) ) /* */
550 { if (w[i] == comma) { /* */
551 type[i++] = ','; /* */
552 } else { /* */
553 type[i++] = 'f'; first++; /* */
554 } /* */
555 } /* */
556 while ( i < len && is_lower_word(w[i]) ) /* */
557 { type[i++] = 'v'; von++; } /* */
558 } /* */
559 /* */
560 #ifdef DEBUG
561 ErrPrint("+++ BibTool: Classified name parts: ");/* */
562 for ( i = 0; i < len; i++) /* */
563 { ErrPrintF2("\n\t%c/%s/ ",type[i], w[i]); } /* */
564 ErrC('\n'); /* */
565 #endif
566 /* */
567 for ( nn = format; /* */
568 nn != NameNULL; /* */
569 nn = NextName(nn) ) /* */
570 { int strip = NameStrip(nn); /* */
571 int trim = NameTrim(nn); /* */
572 dump_name_node(nn); /* only in debug mode */
573 switch( NameType(nn) & NameTypeMask ) /* */
574 { case NameFirst: t = 'f'; j = first; break; /* */
575 case NameLast: t = 'l'; j = last; break; /* */
576 case NameVon: t = 'v'; j = von; break; /* */
577 case NameJr: t = 'j'; j = jr; break; /* */
578 case NameString: sbputs((char*)NameMid(nn),sb);/* */
579 default: t = ' '; j = 0; /* */
580 } /* */
581 switch( NameType(nn) & NameTranslationMask ) /* */
582 { case NameLower: tr = trans_lower; break; /* */
583 case NameUpper: tr = trans_upper; break; /* */
584 case NameNone: tr = trans_id; break; /* */
585 default: tr = trans; /* */
586 } /* */
587 /* */
588 if (j > 0) /* */
589 { sbputs((char*)SymbolValue(NamePre(nn)), sb); /* */
590 again = false; /* */
591 for (i = 0; i < len; i++) /* */
592 { if (type[i] == t) /* */
593 { /* */
594 if (trim-- == 0) break; /* */
595 if (again) sbputs((char*)SymbolValue(NameMid(nn)), sb);/* */
596 else again = true; /* */
597 /* */
598 initial(w[i], tr, strip, sb); /* */
599 } /* */
600 } /* */
601 sbputs((char*)SymbolValue(NamePost(nn)), sb);/* */
602 } /* */
603 } /* */
604 /* */
605 free(type); /* */
606 } /*------------------------*/
607
608 /*-----------------------------------------------------------------------------
609 ** Function*: initial()
610 ** Purpose:
611 **
612 **
613 ** Arguments:
614 ** s
615 ** trans the translation to apply
616 ** len
617 ** sb the target string buffer
618 ** Returns: nothing
619 **___________________________________________________ */
620 static void initial(s,trans,len,sb) /* */
621 String s; /* */
622 String trans; /* */
623 int len; /* */
624 StringBuffer *sb; /* */
625 { /* */
626 if (len < 0) { sbputs((char*)s, sb); return; } /* */
627 /* */
628 while (len > 0) /* */
629 { if (*s=='\0') { return; } /* */
630 if ( is_alpha(*s) ) /* */
631 { sbputc(trans[*s], sb); /* */
632 len--; /* */
633 } /* */
634 s++; /* */
635 } /* */
636 } /*------------------------*/
637
638 /*-----------------------------------------------------------------------------
639 ** Function*: is_jr()
640 ** Purpose:
641 **
642 **
643 ** Arguments:
644 ** s string to consider
645 ** eager indicator whether single letters should be classified as
646 ** Roman numbers
647 ** Returns:
648 **___________________________________________________ */
649 static bool is_jr(s, eager) /* */
650 String s; /* */
651 bool eager; /* */
652 { /* */
653 switch ( ToLower(*s) ) /* */
654 { case 'j': /* */
655 s++; /* */
656 return ( case_eq(s,(String)"r") || /* */
657 case_eq(s,(String)"r.") || /* */
658 case_eq(s,(String)"unior") ); /* */
659 case 's': /* */
660 s++; /* */
661 return ( case_eq(s,(String)"en") || /* */
662 case_eq(s,(String)"en.") || /* */
663 case_eq(s,(String)"enior") ); /* */
664 case 'm': /* */
665 s++; /* */
666 return ( case_eq(s,(String)"d") ); /* */
667 case 'p': /* */
668 s++; /* */
669 return ( case_eq(s,(String)"hD") || /* */
670 case_eq(s,(String)"hD.") ); /* */
671 case 'i': /* */
672 s++; /* */
673 return ( (*s == '\0' && eager) || /* */
674 strcmp((char*)s,"I") == 0 || /* */
675 strcmp((char*)s,"II") == 0 || /* */
676 strcmp((char*)s,"V") == 0 || /* */
677 strcmp((char*)s,"X") == 0 ); /* */
678 case 'v': /* */
679 s++; /* */
680 return ( (*s == '\0' && eager) || /* */
681 strcmp((char*)s,"I") == 0 || /* */
682 strcmp((char*)s,"II") == 0 || /* */
683 strcmp((char*)s,"III") == 0 ); /* */
684 case 'x': /* */
685 s++; /* */
686 return ( (*s == '\0' && eager) || /* */
687 strcmp((char*)s,"I") == 0 || /* */
688 strcmp((char*)s,"II") == 0 || /* */
689 strcmp((char*)s,"III") == 0 || /* */
690 strcmp((char*)s,"IV") == 0 || /* */
691 strcmp((char*)s,"V") == 0 || /* */
692 strcmp((char*)s,"VI") == 0 || /* */
693 strcmp((char*)s,"VII") == 0 || /* */
694 strcmp((char*)s,"VIII") == 0 || /* */
695 strcmp((char*)s,"IX") == 0 || /* */
696 strcmp((char*)s,"X") == 0 || /* */
697 strcmp((char*)s,"XI") == 0 || /* */
698 strcmp((char*)s,"XII") == 0 || /* */
699 strcmp((char*)s,"XIII") == 0 || /* */
700 strcmp((char*)s,"XIV") == 0 || /* */
701 strcmp((char*)s,"XV") == 0 || /* */
702 strcmp((char*)s,"XVI") == 0 || /* */
703 strcmp((char*)s,"XVII") == 0 || /* */
704 strcmp((char*)s,"XVIII") == 0 || /* */
705 strcmp((char*)s,"XIX") == 0 || /* */
706 strcmp((char*)s,"XX") == 0 ); /* */
707 case 'e': /* */
708 return ( case_eq(++s,(String)"sc") || /* */
709 case_eq(s,(String)"sc.") ); /* */
710 } /* */
711 return false; /* */
712 } /*------------------------*/
713
714 /*-----------------------------------------------------------------------------
715 ** Function*: is_lower_word()
716 ** Purpose: Test if the word given as argument starts with a lower case
717 ** letter. Non letter characters are ignored.
718 ** \TeX\ control sequences are partially ignored:
719 ** Arguments:
720 ** s Word to test
721 ** Returns: |true| or |false|
722 **___________________________________________________ */
723 static bool is_lower_word(s) /* */
724 register String s; /* */
725 { /* */
726 while(*s) /* */
727 { if ( is_lower(*s) ) return true; /* */
728 if ( is_upper(*s) ) return false; /* */
729 if ( *s == '\\' ) { s++; if (*s) s++; } /* */
730 else s++; /* */
731 } /* */
732 return false; /* */
733 } /*------------------------*/
734
735 /*---------------------------------------------------------------------------*/
736
737 #ifdef STANDALONE
738
739 /*-----------------------------------------------------------------------------
740 ** Function*: pp_names()
741 ** Purpose: Take a string consisting of a list of names, parse it and
742 ** reformat it according to a format specification.
743 ** This function is modeled according to the specification
744 ** for |format.name$| in \BibTeX.
745 ** Arguments:
746 ** s string to be parsed and reformatted.
747 ** format the format
748 ** trans translation table
749 ** max
750 ** namesep string to be inserted between names.
751 ** etal string to be added instead of `and others'
752 ** Returns: a new symbol with the reformatted names.
753 **___________________________________________________ */
754 char * pp_names(s, format, trans, max, namesep, etal)/* */
755 char *s; /* */
756 NameNode format; /* */
757 String trans; /* */
758 int max; /* */
759 char *namesep; /* */
760 char *etal; /* */
761 { char *wp, /* */
762 *comma, /* */
763 *and; /* */
764 int brace, /* */
765 n, /* */
766 cc; /* */
767 char **words; /* */
768 /* */
769 if ( format == NameNULL ) return s; /* */
770 /* */
771 s = new_string(s); /* */
772 wp = s; /* */
773 brace = 0; /* */
774 n = 0; /* */
775 comma = ","; /* */
776 and = "&"; /* */
777 /* */
778 for ( ; *s; s++ ) /* */
779 { switch(*s) /* */
780 { case '{': brace++; break; /* */
781 case '}': brace--; break; /* */
782 case ',': /* */
783 if ( brace < 1 ) /* */
784 { *s = '\0'; /* */
785 if (*wp) { push_string(wp); n++; } /* */
786 push_string(comma); n++; /* */
787 wp = s+1; /* */
788 } /* */
789 break; /* */
790 case ' ': case '~': case '\t': case '\n': /* */
791 case '\r': /* */
792 if ( brace < 1 ) /* */
793 { *s = '\0'; /* */
794 if (*wp) /* */
795 { push_string(strcmp(wp,"and")==0?and:wp);/* */
796 n++; /* */
797 } /* */
798 wp = s+1; /* */
799 } /* */
800 break; /* */
801 } /* */
802 } /* */
803 if (*wp) { push_string(wp); n++; } /* */
804 push_string(NULL); /* */
805 n++; /* */
806 /* */
807 if ( (words=(char**)malloc(n*sizeof(char*))) == (char**)0 )/* Allocate */
808 { OUT_OF_MEMORY("Names"); } /* temp. array of words. */
809 /* */
810 while( --n >= 0 ) /* Transfer the stack into*/
811 { words[n] = pop_string(); /* the word array. */
812 } /* */
813 /* */
814 wp = symbol(pp_list_of_names(words, /* */
815 format, /* */
816 trans, /* */
817 max, /* */
818 comma, /* */
819 and, /* */
820 namesep, /* */
821 etal)); /* */
822 /* */
823 free(words); /* */
824 free(s); /* */
825 return wp; /* */
826 } /*------------------------*/
827
828 /*-----------------------------------------------------------------------------
829 ** Function*: main()
830 ** Purpose: Main routine
831 ** Arguments:
832 ** argc Number of arguments (+1)
833 ** argv Array of command line arguments (and program name)
834 ** Returns:
835 **___________________________________________________ */
836 int main(argc,argv) /* */
837 int argc; /* */
838 char *argv[]; /* */
839 { Uchar s[1024]; /* */
840 NameNode format = NameNULL; /* */
841 /* */
842 init_type(); /* */
843 /* */
844 fputs("Format: ",stdout); /* */
845 gets(s); /* */
846 /* */
847 format = name_format(s); /* */
848 /* */
849 while ( gets(s) ) /* */
850 { puts (pp_names(s, /* */
851 format, /* */
852 trans_lower, /* */
853 -1, /* */
854 " and ", /* */
855 " and others")); /* */
856 } /* */
857 } /*------------------------*/
858
859 bool rsc_quiet = true;
860 /*
861
862 gcc -g -DDEBUG -DSTANDALONE names.c stack.o symbols.o sbuffer.o type.o error.o -o names
863 ./names
864 */
865
866 #endif