]>
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
28 extern char *formataddr (); /* hook for custom address formatting */
29 extern char *concataddr (); /* address formatting but allowing duplicates */
32 struct msgs
*fmt_current_folder
; /* current folder (set by main program) */
35 extern int fmt_norm
; /* defined in sbr/fmt_def.c = AD_NAME */
36 struct mailname fmt_mnull
= { NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, 0, 0, 0,
42 static int match (char *, char *);
43 static char *get_x400_friendly (char *, char *, int);
44 static int get_x400_comp (char *, char *, char *, int);
48 * test if string "sub" appears anywhere in
49 * string "str" (case insensitive).
53 match (char *str
, char *sub
)
60 c1
= (isalpha(c1
) && isupper(c1
)) ? tolower(c1
) : c1
;
61 while ((c2
= *str
++) && c1
!= ((isalpha(c2
) && isupper(c2
)) ? tolower(c2
) : c2
))
65 s1
= sub
+ 1; s2
= str
;
66 while ((c1
= *s1
++) && ((isalpha(c1
) && isupper(c1
)) ? tolower(c1
) : c1
) == ((isalpha(c2
=*s2
++) && isupper(c2
)) ? tolower(c2
) : c2
))
73 while ((c2
= *str
++) && (c1
| 040) != (c2
| 040))
77 s1
= sub
+ 1; s2
= str
;
78 while ((c1
= *s1
++) && (c1
| 040) == (*s2
++ | 040))
88 * copy a number to the destination subject to a maximum width
91 cpnumber(char **dest
, int num
, unsigned int wid
, char fill
, size_t n
) {
104 *--sp
= (i
% 10) + '0';
106 } while (i
> 0 && sp
> cp
);
109 else if ((num
) < 0 && sp
> cp
)
119 * copy string from str to dest padding with the fill character to a size
120 * of wid characters. if wid is negative, the string is right aligned
121 * no more than n bytes are copied
124 cptrimmed(char **dest
, char *str
, unsigned int wid
, char fill
, size_t n
) {
125 int remaining
; /* remaining output width available */
127 int end
; /* number of input bytes remaining in str */
128 #ifdef MULTIBYTE_SUPPORT
129 int char_len
; /* bytes in current character */
133 char *sp
; /* current position in source string */
134 char *cp
= *dest
; /* current position in destination string */
135 char *ep
= cp
+ n
; /* end of destination buffer */
140 if ((remaining
= (wid
)) < 0) {
141 remaining
= -remaining
;
145 mbtowc(NULL
, NULL
, 0); /* reset shift state */
147 while (*sp
&& remaining
> 0 && end
> 0) {
148 #ifdef MULTIBYTE_SUPPORT
149 char_len
= mbtowc(&wide_char
, sp
, end
);
150 if (char_len
<= 0 || (cp
+ char_len
> ep
))
155 if (iswcntrl(wide_char
) || iswspace(wide_char
)) {
159 /* isnctrl(), etc., take an int argument. Cygwin's ctype.h
160 intentionally warns if they are passed a char. */
162 if (iscntrl(c
) || isspace(c
)) {
175 #ifdef MULTIBYTE_SUPPORT
176 w
= wcwidth(wide_char
);
177 if (w
>= 0 && remaining
>= w
) {
178 strncpy(cp
, sp
, char_len
);
191 if (cp
+ remaining
> ep
)
195 /* copy string to the right */
196 while (--cp
>= *dest
)
197 *(cp
+ remaining
) = *cp
;
198 /* add padding at the beginning */
200 for (c
=remaining
; c
>0; c
--)
205 /* pad remaining space */
206 while (remaining
-- > 0 && cp
< ep
)
213 cpstripped (char **start
, char *end
, char *str
)
221 /* skip any initial control characters or spaces */
222 while ((c
= (unsigned char) *s
) &&
224 (iscntrl(c
) || isspace(c
)))
230 /* compact repeated control characters and spaces into a single space */
231 while((c
= (unsigned char) *s
++) && *start
< end
)
232 if (!iscntrl(c
) && !isspace(c
))
235 while ((c
= (unsigned char) *s
) &&
237 (iscntrl(c
) || isspace(c
)))
246 static char *lmonth
[] = { "January", "February","March", "April",
247 "May", "June", "July", "August",
248 "September","October", "November","December" };
251 get_x400_friendly (char *mbox
, char *buffer
, int buffer_len
)
253 char given
[BUFSIZ
], surname
[BUFSIZ
];
262 if (get_x400_comp (mbox
, "/PN=", buffer
, buffer_len
)) {
263 for (mbox
= buffer
; (mbox
= strchr(mbox
, '.')); )
269 if (!get_x400_comp (mbox
, "/S=", surname
, sizeof(surname
)))
272 if (get_x400_comp (mbox
, "/G=", given
, sizeof(given
)))
273 snprintf (buffer
, buffer_len
, "%s %s", given
, surname
);
275 snprintf (buffer
, buffer_len
, "%s", surname
);
281 get_x400_comp (char *mbox
, char *key
, char *buffer
, int buffer_len
)
286 if ((idx
= stringdex (key
, mbox
)) < 0
287 || !(cp
= strchr(mbox
+= idx
+ strlen (key
), '/')))
290 snprintf (buffer
, buffer_len
, "%*.*s", (int)(cp
- mbox
), (int)(cp
- mbox
), mbox
);
295 fmt_scan (struct format
*format
, char *scanl
, int width
, int *dat
)
299 char *savestr
= NULL
;
300 unsigned char *str
= NULL
;
301 char buffer
[BUFSIZ
], buffer2
[BUFSIZ
];
311 ep
= scanl
+ width
- 1;
313 for (fmt
= format
; fmt
->f_type
!= FT_DONE
; fmt
++)
314 switch (fmt
->f_type
) {
317 fmt
->f_comp
->c_flags
&= ~CF_PARSED
;
324 switch (fmt
->f_type
) {
327 cpstripped (&cp
, ep
, fmt
->f_comp
->c_text
);
330 cptrimmed (&cp
, fmt
->f_comp
->c_text
, fmt
->f_width
, fmt
->f_fill
, ep
- cp
);
335 while( (c
= *sp
++) && cp
< ep
)
344 ljust
++; /* XXX should do something with this */
346 while( (c
= *sp
++) && --i
>= 0 && cp
< ep
)
348 while( --i
>= 0 && cp
< ep
)
353 cpstripped (&cp
, ep
, str
);
356 cptrimmed (&cp
, str
, fmt
->f_width
, fmt
->f_fill
, ep
- cp
);
360 while ((c
= *sp
++) && cp
< ep
)
364 adios (NULL
, "internal error (FT_STRFW)");
367 n
= snprintf(cp
, ep
- cp
+ 1, "%d", value
);
376 cpnumber (&cp
, value
, fmt
->f_width
, fmt
->f_fill
, ep
- cp
);
387 if (!(value
= (str
&& *str
))) {
394 if (!(value
= (str
== NULL
|| *str
== 0))) {
401 if (value
!= fmt
->f_value
) {
408 if (value
== fmt
->f_value
) {
415 if (value
<= fmt
->f_value
) {
422 if (!(value
= (str
&& match (str
, fmt
->f_text
)))) {
430 value
= match (str
, fmt
->f_text
);
436 if (!(value
= (str
&& uprf (str
, fmt
->f_text
)))) {
443 value
= uprf (str
, fmt
->f_text
);
447 value
= (str
!= NULL
&& *str
!= 0);
451 value
= (str
== NULL
|| *str
== 0);
455 value
= (fmt
->f_value
== value
);
459 value
= (fmt
->f_value
!= value
);
463 value
= (fmt
->f_value
> value
);
474 str
= fmt
->f_comp
->c_text
;
480 if (!(str
= getenv (fmt
->f_text
)))
484 if (!(str
= context_find (fmt
->f_text
)))
488 case FT_LS_DECODECOMP
:
489 if (decode_rfc2047(fmt
->f_comp
->c_text
, buffer2
, sizeof(buffer2
)))
492 str
= fmt
->f_comp
->c_text
;
496 if (str
&& decode_rfc2047(str
, buffer2
, sizeof(buffer2
)))
504 strncpy(buffer
, str
, sizeof(buffer
));
505 buffer
[sizeof(buffer
)-1] = '\0';
507 while (isspace(*str
))
510 if ((i
= fmt
->f_width
) < 0) {
515 if (!ljust
&& i
> 0 && (int) strlen(str
) > i
)
518 xp
+= strlen(str
) - 1;
519 while (xp
> str
&& isspace(*xp
))
521 if (ljust
&& i
> 0 && (int) strlen(str
) > i
)
522 str
+= strlen(str
) - i
;
527 value
= (fmt
->f_comp
->c_flags
& CF_TRUE
) != 0;
530 value
= (comp
= fmt
->f_comp
)->c_text
? atoi(comp
->c_text
) : 0;
533 value
= fmt
->f_value
;
536 value
= dat
[fmt
->f_value
];
544 case FT_LV_CHAR_LEFT
:
545 value
= width
- (cp
- scanl
);
548 value
+= fmt
->f_value
;
551 value
= fmt
->f_value
- value
;
555 value
= value
/ fmt
->f_value
;
561 value
= value
% fmt
->f_value
;
570 value
= fmt
->f_comp
->c_tws
->tw_sec
;
573 value
= fmt
->f_comp
->c_tws
->tw_min
;
576 value
= fmt
->f_comp
->c_tws
->tw_hour
;
579 value
= fmt
->f_comp
->c_tws
->tw_mday
;
582 value
= fmt
->f_comp
->c_tws
->tw_mon
+ 1;
585 str
= tw_moty
[fmt
->f_comp
->c_tws
->tw_mon
];
588 str
= lmonth
[fmt
->f_comp
->c_tws
->tw_mon
];
591 str
= dtwszone (fmt
->f_comp
->c_tws
);
594 value
= fmt
->f_comp
->c_tws
->tw_year
;
597 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
599 value
= tws
->tw_wday
;
602 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
604 str
= tw_dotw
[tws
->tw_wday
];
607 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
609 str
= tw_ldotw
[tws
->tw_wday
];
612 value
= fmt
->f_comp
->c_tws
->tw_yday
;
615 value
= fmt
->f_comp
->c_tws
->tw_zone
;
618 if ((value
= fmt
->f_comp
->c_tws
->tw_clock
) == 0)
619 value
= dmktime(fmt
->f_comp
->c_tws
);
622 if ((value
= fmt
->f_comp
->c_tws
->tw_clock
) == 0)
623 value
= dmktime(fmt
->f_comp
->c_tws
);
624 value
= time((time_t *) 0) - value
;
627 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
629 switch (fmt
->f_comp
->c_tws
->tw_flags
& TW_SDAY
) {
638 if ((fmt
->f_comp
->c_tws
->tw_flags
& TW_SZONE
) == TW_SZEXP
)
644 value
= fmt
->f_comp
->c_tws
->tw_flags
& TW_DST
;
647 str
= dasctime (fmt
->f_comp
->c_tws
, TW_ZONE
);
650 str
= dasctime (fmt
->f_comp
->c_tws
, TW_NULL
);
654 str
= fmt
->f_comp
->c_mn
->m_pers
;
657 str
= fmt
->f_comp
->c_mn
->m_mbox
;
660 str
= fmt
->f_comp
->c_mn
->m_host
;
663 str
= fmt
->f_comp
->c_mn
->m_path
;
666 str
= fmt
->f_comp
->c_mn
->m_gname
;
669 str
= fmt
->f_comp
->c_mn
->m_note
;
672 str
= adrformat( fmt
->f_comp
->c_mn
);
675 value
= fmt
->f_comp
->c_mn
->m_type
;
678 value
= fmt
->f_comp
->c_mn
->m_ingrp
;
681 value
= fmt
->f_comp
->c_mn
->m_nohost
;
685 if ((mn
= fmt
->f_comp
->c_mn
) == &fmt_mnull
) {
686 str
= fmt
->f_comp
->c_text
;
689 if (fmt
->f_type
== FT_LS_ADDR
)
691 if ((str
= mn
->m_pers
) == NULL
) {
692 if ((str
= mn
->m_note
)) {
693 strncpy (buffer
, str
, sizeof(buffer
));
694 buffer
[sizeof(buffer
)-1] = '\0';
698 sp
= str
+ strlen(str
) - 1;
707 } else if (!(str
= get_x400_friendly (mn
->m_mbox
,
708 buffer
, sizeof(buffer
)))) {
710 switch (mn
->m_type
) {
715 snprintf (buffer
, sizeof(buffer
), "%s!%s",
716 mn
->m_host
, mn
->m_mbox
);
721 snprintf (buffer
, sizeof(buffer
), "%s@%s",
722 mn
->m_mbox
, mn
->m_host
);
734 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
738 strncpy(buffer
, str
, sizeof(buffer
));
739 /* strncpy doesn't NUL-terminate if it fills the buffer */
740 buffer
[sizeof(buffer
)-1] = '\0';
743 /* we will parse from buffer to buffer2 */
744 n
= 0; /* n is the input position in str */
745 m
= 0; /* m is the ouput position in buffer2 */
747 while ( str
[n
] != '\0') {
752 buffer2
[m
++] = str
[n
++];
758 buffer2
[m
++] = str
[n
++];
769 if ((t
= comp
->c_tws
->tw_clock
) == 0)
770 t
= dmktime(comp
->c_tws
);
771 tws
= dlocaltime(&t
);
777 if ((t
= comp
->c_tws
->tw_clock
) == 0)
778 t
= dmktime(comp
->c_tws
);
785 if (comp
->c_flags
& CF_PARSED
)
787 if ((sp
= comp
->c_text
) && (tws
= dparsetime(sp
))) {
789 comp
->c_flags
&= ~CF_TRUE
;
790 } else if ((comp
->c_flags
& CF_DATEFAB
) == 0) {
791 memset ((char *) comp
->c_tws
, 0, sizeof *comp
->c_tws
);
792 comp
->c_flags
= CF_TRUE
;
794 comp
->c_flags
|= CF_PARSED
;
798 /* hook for custom address list formatting (see replsbr.c) */
799 str
= formataddr (savestr
, str
);
803 /* The same as formataddr, but doesn't do duplicate suppression */
804 str
= concataddr (savestr
, str
);
808 /* output the str register as an address component,
809 * splitting it into multiple lines if necessary. The
810 * value reg. contains the max line length. The lit.
811 * field may contain a string to prepend to the result
817 int indent
, wid
, len
;
823 indent
= strlen (sp
);
826 adios(NULL
, "putaddr -- num register (%d) must be greater "
827 "than label width (%d)", value
, indent
);
829 while( (c
= *sp
++) && cp
< ep
)
832 /* try to break at a comma; failing that, break at a
835 lastb
= 0; sp
= lp
+ wid
;
836 while (sp
> lp
&& (c
= *--sp
) != ',') {
837 if (! lastb
&& isspace(c
))
841 if (! (sp
= lastb
)) {
843 while (*sp
&& *sp
!= ',' && !isspace(*sp
))
850 while (cp
< ep
&& lp
<= sp
)
857 for (i
=indent
; cp
< ep
&& i
> 0; i
--)
861 cpstripped (&cp
, ep
, lp
);
867 if (comp
->c_flags
& CF_PARSED
)
869 if (comp
->c_mn
!= &fmt_mnull
)
871 if ((sp
= comp
->c_text
) && (sp
= getname(sp
)) &&
872 (mn
= getm (sp
, NULL
, 0, fmt_norm
, NULL
))) {
876 comp
->c_flags
|= CF_PARSED
;
878 while (getname("")) /* XXX */
880 comp
->c_mn
= &fmt_mnull
;
886 * if there's no component, we say true. Otherwise we
887 * say "true" only if we can parse the address and it
888 * matches one of our addresses.
891 if (comp
->c_mn
!= &fmt_mnull
)
893 if ((sp
= comp
->c_text
) && (sp
= getname(sp
)) &&
894 (mn
= getm (sp
, NULL
, 0, AD_NAME
, NULL
))) {
897 comp
->c_flags
|= CF_TRUE
;
899 comp
->c_flags
&= ~CF_TRUE
;
900 while ((sp
= getname(sp
)))
901 if ((comp
->c_flags
& CF_TRUE
) == 0 &&
902 (mn
= getm (sp
, NULL
, 0, AD_NAME
, NULL
)))
904 comp
->c_flags
|= CF_TRUE
;
906 while (getname("")) /* XXX */
908 if (comp
->c_text
== 0)
909 comp
->c_flags
|= CF_TRUE
;
911 comp
->c_flags
&= ~CF_TRUE
;
912 comp
->c_mn
= &fmt_mnull
;
918 /* If we're working on a folder (as opposed to a file), add the
919 * current msg to sequence given in literal field. Don't
920 * disturb string or value registers.
922 if (fmt_current_folder
)
923 seq_addmsg(fmt_current_folder
, fmt
->f_text
, dat
[0], -1);
934 return ((struct format
*)0);
938 while (fmt
->f_type
!= FT_DONE
)
943 return (fmt
->f_value
? ++fmt
: (struct format
*) 0);