]>
diplodocus.org Git - nmh/blob - sbr/fmt_scan.c
3 * fmt_scan.c -- format string interpretation
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
9 * This is the engine that processes the format instructions created by
10 * fmt_compile (found in fmt_compile.c).
14 #include <h/addrsbr.h>
15 #include <h/fmt_scan.h>
17 #include <h/fmt_compile.h>
19 #ifdef HAVE_SYS_TIME_H
20 # include <sys/time.h>
23 #ifdef MULTIBYTE_SUPPORT
29 struct msgs
*fmt_current_folder
; /* current folder (set by main program) */
32 extern int fmt_norm
; /* defined in sbr/fmt_def.c = AD_NAME */
33 struct mailname fmt_mnull
= { NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, 0, 0, 0,
39 static int match (char *, char *);
40 static char *get_x400_friendly (char *, char *, int);
41 static int get_x400_comp (char *, char *, char *, int);
45 * test if string "sub" appears anywhere in
46 * string "str" (case insensitive).
50 match (char *str
, char *sub
)
57 c1
= (isalpha(c1
) && isupper(c1
)) ? tolower(c1
) : c1
;
58 while ((c2
= *str
++) && c1
!= ((isalpha(c2
) && isupper(c2
)) ? tolower(c2
) : c2
))
62 s1
= sub
+ 1; s2
= str
;
63 while ((c1
= *s1
++) && ((isalpha(c1
) && isupper(c1
)) ? tolower(c1
) : c1
) == ((isalpha(c2
=*s2
++) && isupper(c2
)) ? tolower(c2
) : c2
))
70 while ((c2
= *str
++) && (c1
| 040) != (c2
| 040))
74 s1
= sub
+ 1; s2
= str
;
75 while ((c1
= *s1
++) && (c1
| 040) == (*s2
++ | 040))
85 * copy a number to the destination subject to a maximum width
88 cpnumber(char **dest
, int num
, unsigned int wid
, char fill
, size_t n
) {
101 *--sp
= (i
% 10) + '0';
103 } while (i
> 0 && sp
> cp
);
106 else if ((num
) < 0 && sp
> cp
)
116 * copy string from str to dest padding with the fill character to a size
117 * of wid characters. if wid is negative, the string is right aligned
118 * no more than n bytes are copied
121 cptrimmed(char **dest
, char *str
, unsigned int wid
, char fill
, size_t n
) {
122 int remaining
; /* remaining output width available */
124 int end
; /* number of input bytes remaining in str */
125 #ifdef MULTIBYTE_SUPPORT
126 int char_len
; /* bytes in current character */
130 char *sp
; /* current position in source string */
131 char *cp
= *dest
; /* current position in destination string */
132 char *ep
= cp
+ n
; /* end of destination buffer */
137 if ((remaining
= (wid
)) < 0) {
138 remaining
= -remaining
;
142 mbtowc(NULL
, NULL
, 0); /* reset shift state */
144 while (*sp
&& remaining
> 0 && end
> 0) {
145 #ifdef MULTIBYTE_SUPPORT
146 char_len
= mbtowc(&wide_char
, sp
, end
);
147 if (char_len
<= 0 || (cp
+ char_len
> ep
))
152 if (iswcntrl(wide_char
) || iswspace(wide_char
)) {
156 /* isnctrl(), etc., take an int argument. Cygwin's ctype.h
157 intentionally warns if they are passed a char. */
159 if (iscntrl(c
) || isspace(c
)) {
172 #ifdef MULTIBYTE_SUPPORT
173 w
= wcwidth(wide_char
);
174 if (w
>= 0 && remaining
>= w
) {
175 strncpy(cp
, sp
, char_len
);
188 if (cp
+ remaining
> ep
)
192 /* copy string to the right */
193 while (--cp
>= *dest
)
194 *(cp
+ remaining
) = *cp
;
195 /* add padding at the beginning */
197 for (c
=remaining
; c
>0; c
--)
202 /* pad remaining space */
203 while (remaining
-- > 0 && cp
< ep
)
210 cpstripped (char **start
, char *end
, char *str
)
218 /* skip any initial control characters or spaces */
219 while ((c
= (unsigned char) *s
) &&
221 (iscntrl(c
) || isspace(c
)))
227 /* compact repeated control characters and spaces into a single space */
228 while((c
= (unsigned char) *s
++) && *start
< end
)
229 if (!iscntrl(c
) && !isspace(c
))
232 while ((c
= (unsigned char) *s
) &&
234 (iscntrl(c
) || isspace(c
)))
243 static char *lmonth
[] = { "January", "February","March", "April",
244 "May", "June", "July", "August",
245 "September","October", "November","December" };
248 get_x400_friendly (char *mbox
, char *buffer
, int buffer_len
)
250 char given
[BUFSIZ
], surname
[BUFSIZ
];
259 if (get_x400_comp (mbox
, "/PN=", buffer
, buffer_len
)) {
260 for (mbox
= buffer
; (mbox
= strchr(mbox
, '.')); )
266 if (!get_x400_comp (mbox
, "/S=", surname
, sizeof(surname
)))
269 if (get_x400_comp (mbox
, "/G=", given
, sizeof(given
)))
270 snprintf (buffer
, buffer_len
, "%s %s", given
, surname
);
272 snprintf (buffer
, buffer_len
, "%s", surname
);
278 get_x400_comp (char *mbox
, char *key
, char *buffer
, int buffer_len
)
283 if ((idx
= stringdex (key
, mbox
)) < 0
284 || !(cp
= strchr(mbox
+= idx
+ strlen (key
), '/')))
287 snprintf (buffer
, buffer_len
, "%*.*s", (int)(cp
- mbox
), (int)(cp
- mbox
), mbox
);
292 fmt_scan (struct format
*format
, char *scanl
, int width
, int *dat
)
296 char *savestr
= NULL
;
297 unsigned char *str
= NULL
;
298 char buffer
[BUFSIZ
], buffer2
[BUFSIZ
];
308 ep
= scanl
+ width
- 1;
310 for (fmt
= format
; fmt
->f_type
!= FT_DONE
; fmt
++)
311 switch (fmt
->f_type
) {
314 fmt
->f_comp
->c_flags
&= ~CF_PARSED
;
319 case FT_LS_DECODECOMP
:
321 * Trim these components of any newlines.
323 * But don't trim the "body" and "text" components.
328 if (! (comp
->c_flags
& CF_TRIMMED
) && comp
->c_text
) {
329 i
= strlen(comp
->c_text
);
330 if (comp
->c_text
[i
- 1] == '\n' &&
331 strcmp(comp
->c_name
, "body") != 0 &&
332 strcmp(comp
->c_name
, "text") != 0)
333 comp
->c_text
[i
- 1] = '\0';
334 comp
->c_flags
|= CF_TRIMMED
;
342 switch (fmt
->f_type
) {
345 cpstripped (&cp
, ep
, fmt
->f_comp
->c_text
);
348 cptrimmed (&cp
, fmt
->f_comp
->c_text
, fmt
->f_width
, fmt
->f_fill
, ep
- cp
);
353 while( (c
= *sp
++) && cp
< ep
)
362 ljust
++; /* XXX should do something with this */
364 while( (c
= *sp
++) && --i
>= 0 && cp
< ep
)
366 while( --i
>= 0 && cp
< ep
)
371 cpstripped (&cp
, ep
, str
);
374 cptrimmed (&cp
, str
, fmt
->f_width
, fmt
->f_fill
, ep
- cp
);
378 while ((c
= *sp
++) && cp
< ep
)
382 adios (NULL
, "internal error (FT_STRFW)");
385 n
= snprintf(cp
, ep
- cp
+ 1, "%d", value
);
394 cpnumber (&cp
, value
, fmt
->f_width
, fmt
->f_fill
, ep
- cp
);
405 if (!(value
= (str
&& *str
))) {
412 if (!(value
= (str
== NULL
|| *str
== 0))) {
419 if (value
!= fmt
->f_value
) {
426 if (value
== fmt
->f_value
) {
433 if (value
<= fmt
->f_value
) {
440 if (!(value
= (str
&& match (str
, fmt
->f_text
)))) {
448 value
= match (str
, fmt
->f_text
);
454 if (!(value
= (str
&& uprf (str
, fmt
->f_text
)))) {
461 value
= uprf (str
, fmt
->f_text
);
465 value
= (str
!= NULL
&& *str
!= 0);
469 value
= (str
== NULL
|| *str
== 0);
473 value
= (fmt
->f_value
== value
);
477 value
= (fmt
->f_value
!= value
);
481 value
= (fmt
->f_value
> value
);
492 str
= fmt
->f_comp
->c_text
;
498 if (!(str
= getenv (fmt
->f_text
)))
502 if (!(str
= context_find (fmt
->f_text
)))
506 case FT_LS_DECODECOMP
:
507 if (decode_rfc2047(fmt
->f_comp
->c_text
, buffer2
, sizeof(buffer2
)))
510 str
= fmt
->f_comp
->c_text
;
514 if (str
&& decode_rfc2047(str
, buffer2
, sizeof(buffer2
)))
522 strncpy(buffer
, str
, sizeof(buffer
));
523 buffer
[sizeof(buffer
)-1] = '\0';
525 while (isspace(*str
))
528 if ((i
= fmt
->f_width
) < 0) {
533 if (!ljust
&& i
> 0 && (int) strlen(str
) > i
)
536 xp
+= strlen(str
) - 1;
537 while (xp
> str
&& isspace(*xp
))
539 if (ljust
&& i
> 0 && (int) strlen(str
) > i
)
540 str
+= strlen(str
) - i
;
545 value
= (fmt
->f_comp
->c_flags
& CF_TRUE
) != 0;
548 value
= (comp
= fmt
->f_comp
)->c_text
? atoi(comp
->c_text
) : 0;
551 value
= fmt
->f_value
;
554 value
= dat
[fmt
->f_value
];
562 case FT_LV_CHAR_LEFT
:
563 value
= width
- (cp
- scanl
);
566 value
+= fmt
->f_value
;
569 value
= fmt
->f_value
- value
;
573 value
= value
/ fmt
->f_value
;
579 value
= value
% fmt
->f_value
;
588 value
= fmt
->f_comp
->c_tws
->tw_sec
;
591 value
= fmt
->f_comp
->c_tws
->tw_min
;
594 value
= fmt
->f_comp
->c_tws
->tw_hour
;
597 value
= fmt
->f_comp
->c_tws
->tw_mday
;
600 value
= fmt
->f_comp
->c_tws
->tw_mon
+ 1;
603 str
= tw_moty
[fmt
->f_comp
->c_tws
->tw_mon
];
606 str
= lmonth
[fmt
->f_comp
->c_tws
->tw_mon
];
609 str
= dtwszone (fmt
->f_comp
->c_tws
);
612 value
= fmt
->f_comp
->c_tws
->tw_year
;
615 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
617 value
= tws
->tw_wday
;
620 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
622 str
= tw_dotw
[tws
->tw_wday
];
625 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
627 str
= tw_ldotw
[tws
->tw_wday
];
630 value
= fmt
->f_comp
->c_tws
->tw_yday
;
633 value
= fmt
->f_comp
->c_tws
->tw_zone
;
636 if ((value
= fmt
->f_comp
->c_tws
->tw_clock
) == 0)
637 value
= dmktime(fmt
->f_comp
->c_tws
);
640 if ((value
= fmt
->f_comp
->c_tws
->tw_clock
) == 0)
641 value
= dmktime(fmt
->f_comp
->c_tws
);
642 value
= time((time_t *) 0) - value
;
645 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
647 switch (fmt
->f_comp
->c_tws
->tw_flags
& TW_SDAY
) {
656 if ((fmt
->f_comp
->c_tws
->tw_flags
& TW_SZONE
) == TW_SZEXP
)
662 value
= fmt
->f_comp
->c_tws
->tw_flags
& TW_DST
;
665 str
= dasctime (fmt
->f_comp
->c_tws
, TW_ZONE
);
668 str
= dasctime (fmt
->f_comp
->c_tws
, TW_NULL
);
672 str
= fmt
->f_comp
->c_mn
->m_pers
;
675 str
= fmt
->f_comp
->c_mn
->m_mbox
;
678 str
= fmt
->f_comp
->c_mn
->m_host
;
681 str
= fmt
->f_comp
->c_mn
->m_path
;
684 str
= fmt
->f_comp
->c_mn
->m_gname
;
687 str
= fmt
->f_comp
->c_mn
->m_note
;
690 str
= adrformat( fmt
->f_comp
->c_mn
);
693 value
= fmt
->f_comp
->c_mn
->m_type
;
696 value
= fmt
->f_comp
->c_mn
->m_ingrp
;
699 value
= fmt
->f_comp
->c_mn
->m_nohost
;
703 if ((mn
= fmt
->f_comp
->c_mn
) == &fmt_mnull
) {
704 str
= fmt
->f_comp
->c_text
;
707 if (fmt
->f_type
== FT_LS_ADDR
)
709 if ((str
= mn
->m_pers
) == NULL
) {
710 if ((str
= mn
->m_note
)) {
711 strncpy (buffer
, str
, sizeof(buffer
));
712 buffer
[sizeof(buffer
)-1] = '\0';
716 sp
= str
+ strlen(str
) - 1;
725 } else if (!(str
= get_x400_friendly (mn
->m_mbox
,
726 buffer
, sizeof(buffer
)))) {
728 switch (mn
->m_type
) {
733 snprintf (buffer
, sizeof(buffer
), "%s!%s",
734 mn
->m_host
, mn
->m_mbox
);
739 snprintf (buffer
, sizeof(buffer
), "%s@%s",
740 mn
->m_mbox
, mn
->m_host
);
752 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
756 strncpy(buffer
, str
, sizeof(buffer
));
757 /* strncpy doesn't NUL-terminate if it fills the buffer */
758 buffer
[sizeof(buffer
)-1] = '\0';
761 /* we will parse from buffer to buffer2 */
762 n
= 0; /* n is the input position in str */
763 m
= 0; /* m is the ouput position in buffer2 */
765 while ( str
[n
] != '\0') {
770 buffer2
[m
++] = str
[n
++];
776 buffer2
[m
++] = str
[n
++];
787 if ((t
= comp
->c_tws
->tw_clock
) == 0)
788 t
= dmktime(comp
->c_tws
);
789 tws
= dlocaltime(&t
);
795 if ((t
= comp
->c_tws
->tw_clock
) == 0)
796 t
= dmktime(comp
->c_tws
);
803 if (comp
->c_flags
& CF_PARSED
)
805 if ((sp
= comp
->c_text
) && (tws
= dparsetime(sp
))) {
807 comp
->c_flags
&= ~CF_TRUE
;
808 } else if ((comp
->c_flags
& CF_DATEFAB
) == 0) {
809 memset ((char *) comp
->c_tws
, 0, sizeof *comp
->c_tws
);
810 comp
->c_flags
= CF_TRUE
;
812 comp
->c_flags
|= CF_PARSED
;
816 /* hook for custom address list formatting (see replsbr.c) */
817 str
= formataddr (savestr
, str
);
821 /* The same as formataddr, but doesn't do duplicate suppression */
822 str
= concataddr (savestr
, str
);
826 /* output the str register as an address component,
827 * splitting it into multiple lines if necessary. The
828 * value reg. contains the max line length. The lit.
829 * field may contain a string to prepend to the result
835 int indent
, wid
, len
;
841 indent
= strlen (sp
);
844 adios(NULL
, "putaddr -- num register (%d) must be greater "
845 "than label width (%d)", value
, indent
);
847 while( (c
= *sp
++) && cp
< ep
)
850 /* try to break at a comma; failing that, break at a
853 lastb
= 0; sp
= lp
+ wid
;
854 while (sp
> lp
&& (c
= *--sp
) != ',') {
855 if (! lastb
&& isspace(c
))
859 if (! (sp
= lastb
)) {
861 while (*sp
&& *sp
!= ',' && !isspace(*sp
))
868 while (cp
< ep
&& lp
<= sp
)
875 for (i
=indent
; cp
< ep
&& i
> 0; i
--)
879 cpstripped (&cp
, ep
, lp
);
885 if (comp
->c_flags
& CF_PARSED
)
887 if (comp
->c_mn
!= &fmt_mnull
)
889 if ((sp
= comp
->c_text
) && (sp
= getname(sp
)) &&
890 (mn
= getm (sp
, NULL
, 0, fmt_norm
, NULL
))) {
894 comp
->c_flags
|= CF_PARSED
;
896 while (getname("")) /* XXX */
898 comp
->c_mn
= &fmt_mnull
;
904 * if there's no component, we say true. Otherwise we
905 * say "true" only if we can parse the address and it
906 * matches one of our addresses.
909 if (comp
->c_mn
!= &fmt_mnull
)
911 if ((sp
= comp
->c_text
) && (sp
= getname(sp
)) &&
912 (mn
= getm (sp
, NULL
, 0, AD_NAME
, NULL
))) {
915 comp
->c_flags
|= CF_TRUE
;
917 comp
->c_flags
&= ~CF_TRUE
;
918 while ((sp
= getname(sp
)))
919 if ((comp
->c_flags
& CF_TRUE
) == 0 &&
920 (mn
= getm (sp
, NULL
, 0, AD_NAME
, NULL
)))
922 comp
->c_flags
|= CF_TRUE
;
924 while (getname("")) /* XXX */
926 if (comp
->c_text
== 0)
927 comp
->c_flags
|= CF_TRUE
;
929 comp
->c_flags
&= ~CF_TRUE
;
930 comp
->c_mn
= &fmt_mnull
;
936 /* If we're working on a folder (as opposed to a file), add the
937 * current msg to sequence given in literal field. Don't
938 * disturb string or value registers.
940 if (fmt_current_folder
)
941 seq_addmsg(fmt_current_folder
, fmt
->f_text
, dat
[0], -1);
952 return ((struct format
*)0);
956 while (fmt
->f_type
!= FT_DONE
)
961 return (fmt
->f_value
? ++fmt
: (struct format
*) 0);