]>
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.
11 #include <h/addrsbr.h>
12 #include <h/fmt_scan.h>
14 #include <h/fmt_compile.h>
16 #ifdef TIME_WITH_SYS_TIME
17 # include <sys/time.h>
20 # ifdef TM_IN_SYS_TIME
21 # include <sys/time.h>
26 #ifdef MULTIBYTE_SUPPORT
33 extern char *formataddr (); /* hook for custom address formatting */
36 struct msgs
*fmt_current_folder
; /* current folder (set by main program) */
39 extern int fmt_norm
; /* defined in sbr/fmt_def.c = AD_NAME */
40 struct mailname fmt_mnull
;
45 static int match (char *, char *);
46 static char *get_x400_friendly (char *, char *, int);
47 static int get_x400_comp (char *, char *, char *, int);
51 * test if string "sub" appears anywhere in
52 * string "str" (case insensitive).
56 match (char *str
, char *sub
)
63 c1
= (isalpha(c1
) && isupper(c1
)) ? tolower(c1
) : c1
;
64 while ((c2
= *str
++) && c1
!= ((isalpha(c2
) && isupper(c2
)) ? tolower(c2
) : c2
))
68 s1
= sub
+ 1; s2
= str
;
69 while ((c1
= *s1
++) && ((isalpha(c1
) && isupper(c1
)) ? tolower(c1
) : c1
) == ((isalpha(c2
=*s2
++) && isupper(c2
)) ? tolower(c2
) : c2
))
76 while ((c2
= *str
++) && (c1
| 040) != (c2
| 040))
80 s1
= sub
+ 1; s2
= str
;
81 while ((c1
= *s1
++) && (c1
| 040) == (*s2
++ | 040))
91 * copy a number to the destination subject to a maximum width
94 cpnumber(char **dest
, int num
, unsigned int wid
, char fill
, size_t n
) {
107 *--sp
= (i
% 10) + '0';
109 } while (i
> 0 && sp
> cp
);
112 else if ((num
) < 0 && sp
> cp
)
122 * copy string from str to dest padding with the fill character to a size
123 * of wid characters. if wid is negative, the string is right aligned
124 * no more than n bytes are copied
127 cptrimmed(char **dest
, char *str
, unsigned int wid
, char fill
, size_t n
) {
128 int remaining
; /* remaining output width available */
130 int end
; /* number of input bytes remaining in str */
131 #ifdef MULTIBYTE_SUPPORT
132 int char_len
; /* bytes in current character */
136 char *sp
; /* current position in source string */
137 char *cp
= *dest
; /* current position in destination string */
138 char *ep
= cp
+ n
; /* end of destination buffer */
143 if ((remaining
= (wid
)) < 0) {
144 remaining
= -remaining
;
148 mbtowc(NULL
, NULL
, 0); /* reset shift state */
150 while (*sp
&& remaining
> 0 && end
> 0) {
151 #ifdef MULTIBYTE_SUPPORT
152 char_len
= mbtowc(&wide_char
, sp
, end
);
153 if (char_len
<= 0 || (cp
+ char_len
> ep
))
158 if (iswcntrl(wide_char
) || iswspace(wide_char
)) {
162 if (iscntrl(*sp
) || isspace(*sp
)) {
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
);
359 adios (NULL
, "internal error (FT_STRFW)");
362 n
= snprintf(cp
, ep
- cp
+ 1, "%d", value
);
371 cpnumber (&cp
, value
, fmt
->f_width
, fmt
->f_fill
, ep
- cp
);
382 if (!(value
= (str
&& *str
))) {
389 if (!(value
= (str
== NULL
|| *str
== 0))) {
396 if (value
!= fmt
->f_value
) {
403 if (value
== fmt
->f_value
) {
410 if (value
<= fmt
->f_value
) {
417 if (!(value
= (str
&& match (str
, fmt
->f_text
)))) {
425 value
= match (str
, fmt
->f_text
);
431 if (!(value
= (str
&& uprf (str
, fmt
->f_text
)))) {
438 value
= uprf (str
, fmt
->f_text
);
442 value
= (str
!= NULL
&& *str
!= 0);
446 value
= (str
== NULL
|| *str
== 0);
450 value
= (fmt
->f_value
== value
);
454 value
= (fmt
->f_value
!= value
);
458 value
= (fmt
->f_value
> value
);
469 str
= fmt
->f_comp
->c_text
;
475 if (!(str
= getenv (fmt
->f_text
)))
479 if (!(str
= context_find (fmt
->f_text
)))
483 case FT_LS_DECODECOMP
:
484 if (decode_rfc2047(fmt
->f_comp
->c_text
, buffer2
, sizeof(buffer2
)))
487 str
= fmt
->f_comp
->c_text
;
491 if (str
&& decode_rfc2047(str
, buffer2
, sizeof(buffer2
)))
499 strncpy(buffer
, str
, sizeof(buffer
));
500 buffer
[sizeof(buffer
)-1] = '\0';
502 while (isspace(*str
))
505 if ((i
= fmt
->f_width
) < 0) {
510 if (!ljust
&& i
> 0 && strlen(str
) > i
)
513 xp
+= strlen(str
) - 1;
514 while (xp
> str
&& isspace(*xp
))
516 if (ljust
&& i
> 0 && strlen(str
) > i
)
517 str
+= strlen(str
) - i
;
522 value
= (fmt
->f_comp
->c_flags
& CF_TRUE
) != 0;
525 value
= (comp
= fmt
->f_comp
)->c_text
? atoi(comp
->c_text
) : 0;
528 value
= fmt
->f_value
;
531 value
= dat
[fmt
->f_value
];
539 case FT_LV_CHAR_LEFT
:
540 value
= width
- (cp
- scanl
);
543 value
+= fmt
->f_value
;
546 value
= fmt
->f_value
- value
;
550 value
= value
/ fmt
->f_value
;
556 value
= value
% fmt
->f_value
;
565 value
= fmt
->f_comp
->c_tws
->tw_sec
;
568 value
= fmt
->f_comp
->c_tws
->tw_min
;
571 value
= fmt
->f_comp
->c_tws
->tw_hour
;
574 value
= fmt
->f_comp
->c_tws
->tw_mday
;
577 value
= fmt
->f_comp
->c_tws
->tw_mon
+ 1;
580 str
= tw_moty
[fmt
->f_comp
->c_tws
->tw_mon
];
583 str
= lmonth
[fmt
->f_comp
->c_tws
->tw_mon
];
586 str
= dtwszone (fmt
->f_comp
->c_tws
);
589 value
= fmt
->f_comp
->c_tws
->tw_year
;
592 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
594 value
= tws
->tw_wday
;
597 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
599 str
= tw_dotw
[tws
->tw_wday
];
602 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
604 str
= tw_ldotw
[tws
->tw_wday
];
607 value
= fmt
->f_comp
->c_tws
->tw_yday
;
610 value
= fmt
->f_comp
->c_tws
->tw_zone
;
613 if ((value
= fmt
->f_comp
->c_tws
->tw_clock
) == 0)
614 value
= dmktime(fmt
->f_comp
->c_tws
);
617 if ((value
= fmt
->f_comp
->c_tws
->tw_clock
) == 0)
618 value
= dmktime(fmt
->f_comp
->c_tws
);
619 value
= time((time_t *) 0) - value
;
622 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
624 switch (fmt
->f_comp
->c_tws
->tw_flags
& TW_SDAY
) {
633 if ((fmt
->f_comp
->c_tws
->tw_flags
& TW_SZONE
) == TW_SZEXP
)
639 value
= fmt
->f_comp
->c_tws
->tw_flags
& TW_DST
;
642 str
= dasctime (fmt
->f_comp
->c_tws
, TW_ZONE
);
645 str
= dasctime (fmt
->f_comp
->c_tws
, TW_NULL
);
649 str
= fmt
->f_comp
->c_mn
->m_pers
;
652 str
= fmt
->f_comp
->c_mn
->m_mbox
;
655 str
= fmt
->f_comp
->c_mn
->m_host
;
658 str
= fmt
->f_comp
->c_mn
->m_path
;
661 str
= fmt
->f_comp
->c_mn
->m_gname
;
664 str
= fmt
->f_comp
->c_mn
->m_note
;
667 str
= adrformat( fmt
->f_comp
->c_mn
);
670 value
= fmt
->f_comp
->c_mn
->m_type
;
673 value
= fmt
->f_comp
->c_mn
->m_ingrp
;
676 value
= fmt
->f_comp
->c_mn
->m_nohost
;
680 if ((mn
= fmt
->f_comp
->c_mn
) == &fmt_mnull
) {
681 str
= fmt
->f_comp
->c_text
;
684 if (fmt
->f_type
== FT_LS_ADDR
)
686 if ((str
= mn
->m_pers
) == NULL
) {
687 if ((str
= mn
->m_note
)) {
688 strncpy (buffer
, str
, sizeof(buffer
));
689 buffer
[sizeof(buffer
)-1] = '\0';
693 sp
= str
+ strlen(str
) - 1;
702 } else if (!(str
= get_x400_friendly (mn
->m_mbox
,
703 buffer
, sizeof(buffer
)))) {
705 switch (mn
->m_type
) {
710 snprintf (buffer
, sizeof(buffer
), "%s!%s",
711 mn
->m_host
, mn
->m_mbox
);
716 snprintf (buffer
, sizeof(buffer
), "%s@%s",
717 mn
->m_mbox
, mn
->m_host
);
729 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
733 strncpy(buffer
, str
, sizeof(buffer
));
734 /* strncpy doesn't NUL-terminate if it fills the buffer */
735 buffer
[sizeof(buffer
)-1] = '\0';
738 /* we will parse from buffer to buffer2 */
739 n
= 0; /* n is the input position in str */
740 m
= 0; /* m is the ouput position in buffer2 */
742 while ( str
[n
] != '\0') {
747 buffer2
[m
++] = str
[n
++];
753 buffer2
[m
++] = str
[n
++];
764 if ((t
= comp
->c_tws
->tw_clock
) == 0)
765 t
= dmktime(comp
->c_tws
);
766 tws
= dlocaltime(&t
);
772 if ((t
= comp
->c_tws
->tw_clock
) == 0)
773 t
= dmktime(comp
->c_tws
);
780 if (comp
->c_flags
& CF_PARSED
)
782 if ((sp
= comp
->c_text
) && (tws
= dparsetime(sp
))) {
784 comp
->c_flags
&= ~CF_TRUE
;
785 } else if ((comp
->c_flags
& CF_DATEFAB
) == 0) {
786 memset ((char *) comp
->c_tws
, 0, sizeof *comp
->c_tws
);
787 comp
->c_flags
= CF_TRUE
;
789 comp
->c_flags
|= CF_PARSED
;
793 /* hook for custom address list formatting (see replsbr.c) */
794 str
= formataddr (savestr
, str
);
798 /* output the str register as an address component,
799 * splitting it into multiple lines if necessary. The
800 * value reg. contains the max line length. The lit.
801 * field may contain a string to prepend to the result
807 int indent
, wid
, len
;
813 indent
= strlen (sp
);
815 while( (c
= *sp
++) && cp
< ep
)
818 /* try to break at a comma; failing that, break at a
821 lastb
= 0; sp
= lp
+ wid
;
822 while (sp
> lp
&& (c
= *--sp
) != ',') {
823 if (! lastb
&& isspace(c
))
827 if (! (sp
= lastb
)) {
829 while (*sp
&& *sp
!= ',' && !isspace(*sp
))
836 while (cp
< ep
&& lp
<= sp
)
843 for (i
=indent
; cp
< ep
&& i
> 0; i
--)
847 cpstripped (&cp
, ep
, lp
);
853 if (comp
->c_flags
& CF_PARSED
)
855 if (comp
->c_mn
!= &fmt_mnull
)
857 if ((sp
= comp
->c_text
) && (sp
= getname(sp
)) &&
858 (mn
= getm (sp
, NULL
, 0, fmt_norm
, NULL
))) {
862 comp
->c_flags
|= CF_PARSED
;
864 while (getname("")) /* XXX */
866 comp
->c_mn
= &fmt_mnull
;
872 * if there's no component, we say true. Otherwise we
873 * say "true" only if we can parse the address and it
874 * matches one of our addresses.
877 if (comp
->c_mn
!= &fmt_mnull
)
879 if ((sp
= comp
->c_text
) && (sp
= getname(sp
)) &&
880 (mn
= getm (sp
, NULL
, 0, AD_NAME
, NULL
))) {
883 comp
->c_flags
|= CF_TRUE
;
885 comp
->c_flags
&= ~CF_TRUE
;
886 while ((sp
= getname(sp
)))
887 if ((comp
->c_flags
& CF_TRUE
) == 0 &&
888 (mn
= getm (sp
, NULL
, 0, AD_NAME
, NULL
)))
890 comp
->c_flags
|= CF_TRUE
;
892 while (getname("")) /* XXX */
894 if (comp
->c_text
== 0)
895 comp
->c_flags
|= CF_TRUE
;
897 comp
->c_flags
&= ~CF_TRUE
;
898 comp
->c_mn
= &fmt_mnull
;
904 /* If we're working on a folder (as opposed to a file), add the
905 * current msg to sequence given in literal field. Don't
906 * disturb string or value registers.
908 if (fmt_current_folder
)
909 seq_addmsg(fmt_current_folder
, fmt
->f_text
, dat
[0], -1);
920 return ((struct format
*)0);
924 while (fmt
->f_type
!= FT_DONE
)
929 return (fmt
->f_value
? ++fmt
: (struct format
*) 0);