]>
diplodocus.org Git - nmh/blob - sbr/fmt_scan.c
9c1eb16aecbc8eba89d6a7e3c3dfc5bf9b2ffdf0
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
;
41 static int match (char *, char *);
42 static char *get_x400_friendly (char *, char *, int);
43 static int get_x400_comp (char *, char *, char *, int);
47 * test if string "sub" appears anywhere in
48 * string "str" (case insensitive).
52 match (char *str
, char *sub
)
59 c1
= (isalpha(c1
) && isupper(c1
)) ? tolower(c1
) : c1
;
60 while ((c2
= *str
++) && c1
!= ((isalpha(c2
) && isupper(c2
)) ? tolower(c2
) : c2
))
64 s1
= sub
+ 1; s2
= str
;
65 while ((c1
= *s1
++) && ((isalpha(c1
) && isupper(c1
)) ? tolower(c1
) : c1
) == ((isalpha(c2
=*s2
++) && isupper(c2
)) ? tolower(c2
) : c2
))
72 while ((c2
= *str
++) && (c1
| 040) != (c2
| 040))
76 s1
= sub
+ 1; s2
= str
;
77 while ((c1
= *s1
++) && (c1
| 040) == (*s2
++ | 040))
87 * copy a number to the destination subject to a maximum width
90 cpnumber(char **dest
, int num
, unsigned int wid
, char fill
, size_t n
) {
103 *--sp
= (i
% 10) + '0';
105 } while (i
> 0 && sp
> cp
);
108 else if ((num
) < 0 && sp
> cp
)
118 * copy string from str to dest padding with the fill character to a size
119 * of wid characters. if wid is negative, the string is right aligned
120 * no more than n bytes are copied
123 cptrimmed(char **dest
, char *str
, unsigned int wid
, char fill
, size_t n
) {
124 int remaining
; /* remaining output width available */
126 int end
; /* number of input bytes remaining in str */
127 #ifdef MULTIBYTE_SUPPORT
128 int char_len
; /* bytes in current character */
132 char *sp
; /* current position in source string */
133 char *cp
= *dest
; /* current position in destination string */
134 char *ep
= cp
+ n
; /* end of destination buffer */
139 if ((remaining
= (wid
)) < 0) {
140 remaining
= -remaining
;
144 mbtowc(NULL
, NULL
, 0); /* reset shift state */
146 while (*sp
&& remaining
> 0 && end
> 0) {
147 #ifdef MULTIBYTE_SUPPORT
148 char_len
= mbtowc(&wide_char
, sp
, end
);
149 if (char_len
<= 0 || (cp
+ char_len
> ep
))
154 if (iswcntrl(wide_char
) || iswspace(wide_char
)) {
158 /* isnctrl(), etc., take an int argument. Cygwin's ctype.h
159 intentionally warns if they are passed a char. */
161 if (iscntrl(c
) || isspace(c
)) {
174 #ifdef MULTIBYTE_SUPPORT
175 w
= wcwidth(wide_char
);
176 if (w
>= 0 && remaining
>= w
) {
177 strncpy(cp
, sp
, char_len
);
190 if (cp
+ remaining
> ep
)
194 /* copy string to the right */
195 while (--cp
>= *dest
)
196 *(cp
+ remaining
) = *cp
;
197 /* add padding at the beginning */
199 for (c
=remaining
; c
>0; c
--)
204 /* pad remaining space */
205 while (remaining
-- > 0 && cp
< ep
)
212 cpstripped (char **start
, char *end
, char *str
)
220 /* skip any initial control characters or spaces */
221 while ((c
= (unsigned char) *s
) &&
223 (iscntrl(c
) || isspace(c
)))
229 /* compact repeated control characters and spaces into a single space */
230 while((c
= (unsigned char) *s
++) && *start
< end
)
231 if (!iscntrl(c
) && !isspace(c
))
234 while ((c
= (unsigned char) *s
) &&
236 (iscntrl(c
) || isspace(c
)))
245 static char *lmonth
[] = { "January", "February","March", "April",
246 "May", "June", "July", "August",
247 "September","October", "November","December" };
250 get_x400_friendly (char *mbox
, char *buffer
, int buffer_len
)
252 char given
[BUFSIZ
], surname
[BUFSIZ
];
261 if (get_x400_comp (mbox
, "/PN=", buffer
, buffer_len
)) {
262 for (mbox
= buffer
; (mbox
= strchr(mbox
, '.')); )
268 if (!get_x400_comp (mbox
, "/S=", surname
, sizeof(surname
)))
271 if (get_x400_comp (mbox
, "/G=", given
, sizeof(given
)))
272 snprintf (buffer
, buffer_len
, "%s %s", given
, surname
);
274 snprintf (buffer
, buffer_len
, "%s", surname
);
280 get_x400_comp (char *mbox
, char *key
, char *buffer
, int buffer_len
)
285 if ((idx
= stringdex (key
, mbox
)) < 0
286 || !(cp
= strchr(mbox
+= idx
+ strlen (key
), '/')))
289 snprintf (buffer
, buffer_len
, "%*.*s", (int)(cp
- mbox
), (int)(cp
- mbox
), mbox
);
294 fmt_scan (struct format
*format
, char *scanl
, int width
, int *dat
)
298 char *savestr
= NULL
;
299 unsigned char *str
= NULL
;
300 char buffer
[BUFSIZ
], buffer2
[BUFSIZ
];
310 ep
= scanl
+ width
- 1;
312 for (fmt
= format
; fmt
->f_type
!= FT_DONE
; fmt
++)
313 switch (fmt
->f_type
) {
316 fmt
->f_comp
->c_flags
&= ~CF_PARSED
;
323 switch (fmt
->f_type
) {
326 cpstripped (&cp
, ep
, fmt
->f_comp
->c_text
);
329 cptrimmed (&cp
, fmt
->f_comp
->c_text
, fmt
->f_width
, fmt
->f_fill
, ep
- cp
);
334 while( (c
= *sp
++) && cp
< ep
)
343 ljust
++; /* XXX should do something with this */
345 while( (c
= *sp
++) && --i
>= 0 && cp
< ep
)
347 while( --i
>= 0 && cp
< ep
)
352 cpstripped (&cp
, ep
, str
);
355 cptrimmed (&cp
, str
, fmt
->f_width
, fmt
->f_fill
, ep
- cp
);
359 while ((c
= *sp
++) && cp
< ep
)
363 adios (NULL
, "internal error (FT_STRFW)");
366 n
= snprintf(cp
, ep
- cp
+ 1, "%d", value
);
375 cpnumber (&cp
, value
, fmt
->f_width
, fmt
->f_fill
, ep
- cp
);
386 if (!(value
= (str
&& *str
))) {
393 if (!(value
= (str
== NULL
|| *str
== 0))) {
400 if (value
!= fmt
->f_value
) {
407 if (value
== fmt
->f_value
) {
414 if (value
<= fmt
->f_value
) {
421 if (!(value
= (str
&& match (str
, fmt
->f_text
)))) {
429 value
= match (str
, fmt
->f_text
);
435 if (!(value
= (str
&& uprf (str
, fmt
->f_text
)))) {
442 value
= uprf (str
, fmt
->f_text
);
446 value
= (str
!= NULL
&& *str
!= 0);
450 value
= (str
== NULL
|| *str
== 0);
454 value
= (fmt
->f_value
== value
);
458 value
= (fmt
->f_value
!= value
);
462 value
= (fmt
->f_value
> value
);
473 str
= fmt
->f_comp
->c_text
;
479 if (!(str
= getenv (fmt
->f_text
)))
483 if (!(str
= context_find (fmt
->f_text
)))
487 case FT_LS_DECODECOMP
:
488 if (decode_rfc2047(fmt
->f_comp
->c_text
, buffer2
, sizeof(buffer2
)))
491 str
= fmt
->f_comp
->c_text
;
495 if (str
&& decode_rfc2047(str
, buffer2
, sizeof(buffer2
)))
503 strncpy(buffer
, str
, sizeof(buffer
));
504 buffer
[sizeof(buffer
)-1] = '\0';
506 while (isspace(*str
))
509 if ((i
= fmt
->f_width
) < 0) {
514 if (!ljust
&& i
> 0 && (int) strlen(str
) > i
)
517 xp
+= strlen(str
) - 1;
518 while (xp
> str
&& isspace(*xp
))
520 if (ljust
&& i
> 0 && (int) strlen(str
) > i
)
521 str
+= strlen(str
) - i
;
526 value
= (fmt
->f_comp
->c_flags
& CF_TRUE
) != 0;
529 value
= (comp
= fmt
->f_comp
)->c_text
? atoi(comp
->c_text
) : 0;
532 value
= fmt
->f_value
;
535 value
= dat
[fmt
->f_value
];
543 case FT_LV_CHAR_LEFT
:
544 value
= width
- (cp
- scanl
);
547 value
+= fmt
->f_value
;
550 value
= fmt
->f_value
- value
;
554 value
= value
/ fmt
->f_value
;
560 value
= value
% fmt
->f_value
;
569 value
= fmt
->f_comp
->c_tws
->tw_sec
;
572 value
= fmt
->f_comp
->c_tws
->tw_min
;
575 value
= fmt
->f_comp
->c_tws
->tw_hour
;
578 value
= fmt
->f_comp
->c_tws
->tw_mday
;
581 value
= fmt
->f_comp
->c_tws
->tw_mon
+ 1;
584 str
= tw_moty
[fmt
->f_comp
->c_tws
->tw_mon
];
587 str
= lmonth
[fmt
->f_comp
->c_tws
->tw_mon
];
590 str
= dtwszone (fmt
->f_comp
->c_tws
);
593 value
= fmt
->f_comp
->c_tws
->tw_year
;
596 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
598 value
= tws
->tw_wday
;
601 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
603 str
= tw_dotw
[tws
->tw_wday
];
606 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
608 str
= tw_ldotw
[tws
->tw_wday
];
611 value
= fmt
->f_comp
->c_tws
->tw_yday
;
614 value
= fmt
->f_comp
->c_tws
->tw_zone
;
617 if ((value
= fmt
->f_comp
->c_tws
->tw_clock
) == 0)
618 value
= dmktime(fmt
->f_comp
->c_tws
);
621 if ((value
= fmt
->f_comp
->c_tws
->tw_clock
) == 0)
622 value
= dmktime(fmt
->f_comp
->c_tws
);
623 value
= time((time_t *) 0) - value
;
626 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
628 switch (fmt
->f_comp
->c_tws
->tw_flags
& TW_SDAY
) {
637 if ((fmt
->f_comp
->c_tws
->tw_flags
& TW_SZONE
) == TW_SZEXP
)
643 value
= fmt
->f_comp
->c_tws
->tw_flags
& TW_DST
;
646 str
= dasctime (fmt
->f_comp
->c_tws
, TW_ZONE
);
649 str
= dasctime (fmt
->f_comp
->c_tws
, TW_NULL
);
653 str
= fmt
->f_comp
->c_mn
->m_pers
;
656 str
= fmt
->f_comp
->c_mn
->m_mbox
;
659 str
= fmt
->f_comp
->c_mn
->m_host
;
662 str
= fmt
->f_comp
->c_mn
->m_path
;
665 str
= fmt
->f_comp
->c_mn
->m_gname
;
668 str
= fmt
->f_comp
->c_mn
->m_note
;
671 str
= adrformat( fmt
->f_comp
->c_mn
);
674 value
= fmt
->f_comp
->c_mn
->m_type
;
677 value
= fmt
->f_comp
->c_mn
->m_ingrp
;
680 value
= fmt
->f_comp
->c_mn
->m_nohost
;
684 if ((mn
= fmt
->f_comp
->c_mn
) == &fmt_mnull
) {
685 str
= fmt
->f_comp
->c_text
;
688 if (fmt
->f_type
== FT_LS_ADDR
)
690 if ((str
= mn
->m_pers
) == NULL
) {
691 if ((str
= mn
->m_note
)) {
692 strncpy (buffer
, str
, sizeof(buffer
));
693 buffer
[sizeof(buffer
)-1] = '\0';
697 sp
= str
+ strlen(str
) - 1;
706 } else if (!(str
= get_x400_friendly (mn
->m_mbox
,
707 buffer
, sizeof(buffer
)))) {
709 switch (mn
->m_type
) {
714 snprintf (buffer
, sizeof(buffer
), "%s!%s",
715 mn
->m_host
, mn
->m_mbox
);
720 snprintf (buffer
, sizeof(buffer
), "%s@%s",
721 mn
->m_mbox
, mn
->m_host
);
733 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
737 strncpy(buffer
, str
, sizeof(buffer
));
738 /* strncpy doesn't NUL-terminate if it fills the buffer */
739 buffer
[sizeof(buffer
)-1] = '\0';
742 /* we will parse from buffer to buffer2 */
743 n
= 0; /* n is the input position in str */
744 m
= 0; /* m is the ouput position in buffer2 */
746 while ( str
[n
] != '\0') {
751 buffer2
[m
++] = str
[n
++];
757 buffer2
[m
++] = str
[n
++];
768 if ((t
= comp
->c_tws
->tw_clock
) == 0)
769 t
= dmktime(comp
->c_tws
);
770 tws
= dlocaltime(&t
);
776 if ((t
= comp
->c_tws
->tw_clock
) == 0)
777 t
= dmktime(comp
->c_tws
);
784 if (comp
->c_flags
& CF_PARSED
)
786 if ((sp
= comp
->c_text
) && (tws
= dparsetime(sp
))) {
788 comp
->c_flags
&= ~CF_TRUE
;
789 } else if ((comp
->c_flags
& CF_DATEFAB
) == 0) {
790 memset ((char *) comp
->c_tws
, 0, sizeof *comp
->c_tws
);
791 comp
->c_flags
= CF_TRUE
;
793 comp
->c_flags
|= CF_PARSED
;
797 /* hook for custom address list formatting (see replsbr.c) */
798 str
= formataddr (savestr
, str
);
802 /* The same as formataddr, but doesn't do duplicate suppression */
803 str
= concataddr (savestr
, str
);
807 /* output the str register as an address component,
808 * splitting it into multiple lines if necessary. The
809 * value reg. contains the max line length. The lit.
810 * field may contain a string to prepend to the result
816 int indent
, wid
, len
;
822 indent
= strlen (sp
);
824 while( (c
= *sp
++) && cp
< ep
)
827 /* try to break at a comma; failing that, break at a
830 lastb
= 0; sp
= lp
+ wid
;
831 while (sp
> lp
&& (c
= *--sp
) != ',') {
832 if (! lastb
&& isspace(c
))
836 if (! (sp
= lastb
)) {
838 while (*sp
&& *sp
!= ',' && !isspace(*sp
))
845 while (cp
< ep
&& lp
<= sp
)
852 for (i
=indent
; cp
< ep
&& i
> 0; i
--)
856 cpstripped (&cp
, ep
, lp
);
862 if (comp
->c_flags
& CF_PARSED
)
864 if (comp
->c_mn
!= &fmt_mnull
)
866 if ((sp
= comp
->c_text
) && (sp
= getname(sp
)) &&
867 (mn
= getm (sp
, NULL
, 0, fmt_norm
, NULL
))) {
871 comp
->c_flags
|= CF_PARSED
;
873 while (getname("")) /* XXX */
875 comp
->c_mn
= &fmt_mnull
;
881 * if there's no component, we say true. Otherwise we
882 * say "true" only if we can parse the address and it
883 * matches one of our addresses.
886 if (comp
->c_mn
!= &fmt_mnull
)
888 if ((sp
= comp
->c_text
) && (sp
= getname(sp
)) &&
889 (mn
= getm (sp
, NULL
, 0, AD_NAME
, NULL
))) {
892 comp
->c_flags
|= CF_TRUE
;
894 comp
->c_flags
&= ~CF_TRUE
;
895 while ((sp
= getname(sp
)))
896 if ((comp
->c_flags
& CF_TRUE
) == 0 &&
897 (mn
= getm (sp
, NULL
, 0, AD_NAME
, NULL
)))
899 comp
->c_flags
|= CF_TRUE
;
901 while (getname("")) /* XXX */
903 if (comp
->c_text
== 0)
904 comp
->c_flags
|= CF_TRUE
;
906 comp
->c_flags
&= ~CF_TRUE
;
907 comp
->c_mn
= &fmt_mnull
;
913 /* If we're working on a folder (as opposed to a file), add the
914 * current msg to sequence given in literal field. Don't
915 * disturb string or value registers.
917 if (fmt_current_folder
)
918 seq_addmsg(fmt_current_folder
, fmt
->f_text
, dat
[0], -1);
929 return ((struct format
*)0);
933 while (fmt
->f_type
!= FT_DONE
)
938 return (fmt
->f_value
? ++fmt
: (struct format
*) 0);