]>
diplodocus.org Git - nmh/blob - sbr/fmt_scan.c
3 * fmt_scan.c -- format string interpretation
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
13 #include <h/addrsbr.h>
14 #include <h/fmt_scan.h>
16 #include <h/fmt_compile.h>
18 #ifdef TIME_WITH_SYS_TIME
19 # include <sys/time.h>
22 # ifdef TM_IN_SYS_TIME
23 # include <sys/time.h>
28 #ifdef MULTIBYTE_SUPPORT
35 extern char *formataddr (); /* hook for custom address formatting */
38 struct msgs
*fmt_current_folder
; /* current folder (set by main program) */
41 extern int fmt_norm
; /* defined in sbr/fmt_def.c = AD_NAME */
42 struct mailname fmt_mnull
;
47 static int match (char *, char *);
48 static char *get_x400_friendly (char *, char *, int);
49 static int get_x400_comp (char *, char *, char *, int);
53 * test if string "sub" appears anywhere in
54 * string "str" (case insensitive).
58 match (char *str
, char *sub
)
65 c1
= (isalpha(c1
) && isupper(c1
)) ? tolower(c1
) : c1
;
66 while ((c2
= *str
++) && c1
!= ((isalpha(c2
) && isupper(c2
)) ? tolower(c2
) : c2
))
70 s1
= sub
+ 1; s2
= str
;
71 while ((c1
= *s1
++) && ((isalpha(c1
) && isupper(c1
)) ? tolower(c1
) : c1
) == ((isalpha(c2
=*s2
++) && isupper(c2
)) ? tolower(c2
) : c2
))
78 while ((c2
= *str
++) && (c1
| 040) != (c2
| 040))
82 s1
= sub
+ 1; s2
= str
;
83 while ((c1
= *s1
++) && (c1
| 040) == (*s2
++ | 040))
93 * copy a number to the destination subject to a maximum width
96 cpnumber(char **dest
, int num
, unsigned int wid
, char fill
, size_t n
) {
109 *--sp
= (i
% 10) + '0';
111 } while (i
> 0 && sp
> cp
);
114 else if ((num
) < 0 && sp
> cp
)
124 * copy string from str to dest padding with the fill character to a size
125 * of wid characters. if wid is negative, the string is right aligned
126 * no more than n bytes are copied
129 cptrimmed(char **dest
, char *str
, unsigned int wid
, char fill
, size_t n
) {
130 int remaining
; /* remaining output width available */
132 int end
; /* number of input bytes remaining in str */
133 #ifdef MULTIBYTE_SUPPORT
134 int char_len
; /* bytes in current character */
137 char *sp
; /* current position in source string */
138 char *cp
= *dest
; /* current position in destination string */
139 char *ep
= cp
+ n
; /* end of destination buffer */
144 if ((remaining
= (wid
)) < 0) {
145 remaining
= -remaining
;
149 mbtowc(NULL
, NULL
, 0); /* reset shift state */
151 while (*sp
&& remaining
> 0 && end
> 0) {
152 #ifdef MULTIBYTE_SUPPORT
153 char_len
= mbtowc(&wide_char
, sp
, end
);
154 if (char_len
<= 0 || (cp
+ char_len
> ep
))
159 if (iswcntrl(wide_char
) || iswspace(wide_char
)) {
163 if (iscntrl(*sp
) || isspace(*sp
)) {
176 #ifdef MULTIBYTE_SUPPORT
177 w
= wcwidth(wide_char
);
178 if (w
>= 0 && remaining
>= w
) {
179 strncpy(cp
, sp
, char_len
);
192 if (cp
+ remaining
> ep
)
196 /* copy string to the right */
197 while (--cp
>= *dest
)
198 *(cp
+ remaining
) = *cp
;
199 /* add padding at the beginning */
201 for (c
=remaining
; c
>0; c
--)
206 /* pad remaining space */
207 while (remaining
-- > 0 && cp
< ep
)
214 cpstripped (char **start
, char *end
, char *str
)
222 /* skip any initial control characters or spaces */
223 while ((c
= (unsigned char) *s
) &&
225 (iscntrl(c
) || isspace(c
)))
231 /* compact repeated control characters and spaces into a single space */
232 while((c
= (unsigned char) *s
++) && *start
< end
)
233 if (!iscntrl(c
) && !isspace(c
))
236 while ((c
= (unsigned char) *s
) &&
238 (iscntrl(c
) || isspace(c
)))
247 static char *lmonth
[] = { "January", "February","March", "April",
248 "May", "June", "July", "August",
249 "September","October", "November","December" };
252 get_x400_friendly (char *mbox
, char *buffer
, int buffer_len
)
254 char given
[BUFSIZ
], surname
[BUFSIZ
];
263 if (get_x400_comp (mbox
, "/PN=", buffer
, buffer_len
)) {
264 for (mbox
= buffer
; (mbox
= strchr(mbox
, '.')); )
270 if (!get_x400_comp (mbox
, "/S=", surname
, sizeof(surname
)))
273 if (get_x400_comp (mbox
, "/G=", given
, sizeof(given
)))
274 snprintf (buffer
, buffer_len
, "%s %s", given
, surname
);
276 snprintf (buffer
, buffer_len
, "%s", surname
);
282 get_x400_comp (char *mbox
, char *key
, char *buffer
, int buffer_len
)
287 if ((idx
= stringdex (key
, mbox
)) < 0
288 || !(cp
= strchr(mbox
+= idx
+ strlen (key
), '/')))
291 snprintf (buffer
, buffer_len
, "%*.*s", (int)(cp
- mbox
), (int)(cp
- mbox
), mbox
);
296 fmt_scan (struct format
*format
, char *scanl
, int width
, int *dat
)
301 unsigned char *str
= NULL
;
302 char buffer
[BUFSIZ
], buffer2
[BUFSIZ
];
312 ep
= scanl
+ width
- 1;
314 for (fmt
= format
; fmt
->f_type
!= FT_DONE
; fmt
++)
315 switch (fmt
->f_type
) {
318 fmt
->f_comp
->c_flags
&= ~CF_PARSED
;
325 switch (fmt
->f_type
) {
328 cpstripped (&cp
, ep
, fmt
->f_comp
->c_text
);
331 cptrimmed (&cp
, fmt
->f_comp
->c_text
, fmt
->f_width
, fmt
->f_fill
, ep
- cp
);
336 while( (c
= *sp
++) && cp
< ep
)
345 ljust
++; /* XXX should do something with this */
347 while( (c
= *sp
++) && --i
>= 0 && cp
< ep
)
349 while( --i
>= 0 && cp
< ep
)
354 cpstripped (&cp
, ep
, str
);
357 cptrimmed (&cp
, str
, fmt
->f_width
, fmt
->f_fill
, ep
- cp
);
360 adios (NULL
, "internal error (FT_STRFW)");
363 n
= snprintf(cp
, ep
- cp
+ 1, "%d", value
);
372 cpnumber (&cp
, value
, fmt
->f_width
, fmt
->f_fill
, ep
- cp
);
383 if (!(value
= (str
&& *str
))) {
390 if (!(value
= (str
== NULL
|| *str
== 0))) {
397 if (value
!= fmt
->f_value
) {
404 if (value
== fmt
->f_value
) {
411 if (value
<= fmt
->f_value
) {
418 if (!(value
= (str
&& match (str
, fmt
->f_text
)))) {
426 value
= match (str
, fmt
->f_text
);
432 if (!(value
= (str
&& uprf (str
, fmt
->f_text
)))) {
439 value
= uprf (str
, fmt
->f_text
);
443 value
= (str
!= NULL
&& *str
!= 0);
447 value
= (str
== NULL
|| *str
== 0);
451 value
= (fmt
->f_value
== value
);
455 value
= (fmt
->f_value
!= value
);
459 value
= (fmt
->f_value
> value
);
470 str
= fmt
->f_comp
->c_text
;
476 if (!(str
= getenv (fmt
->f_text
)))
480 if (!(str
= context_find (fmt
->f_text
)))
484 case FT_LS_DECODECOMP
:
485 if (decode_rfc2047(fmt
->f_comp
->c_text
, buffer2
, sizeof(buffer2
)))
488 str
= fmt
->f_comp
->c_text
;
492 if (str
&& decode_rfc2047(str
, buffer2
, sizeof(buffer2
)))
500 strncpy(buffer
, str
, sizeof(buffer
));
501 buffer
[sizeof(buffer
)-1] = '\0';
503 while (isspace(*str
))
506 if ((i
= fmt
->f_width
) < 0) {
511 if (!ljust
&& i
> 0 && strlen(str
) > i
)
514 xp
+= strlen(str
) - 1;
515 while (xp
> str
&& isspace(*xp
))
517 if (ljust
&& i
> 0 && strlen(str
) > i
)
518 str
+= strlen(str
) - i
;
523 value
= (fmt
->f_comp
->c_flags
& CF_TRUE
) != 0;
526 value
= (comp
= fmt
->f_comp
)->c_text
? atoi(comp
->c_text
) : 0;
529 value
= fmt
->f_value
;
532 value
= dat
[fmt
->f_value
];
540 case FT_LV_CHAR_LEFT
:
541 value
= width
- (cp
- scanl
);
544 value
+= fmt
->f_value
;
547 value
= fmt
->f_value
- value
;
551 value
= value
/ fmt
->f_value
;
557 value
= value
% fmt
->f_value
;
566 value
= fmt
->f_comp
->c_tws
->tw_sec
;
569 value
= fmt
->f_comp
->c_tws
->tw_min
;
572 value
= fmt
->f_comp
->c_tws
->tw_hour
;
575 value
= fmt
->f_comp
->c_tws
->tw_mday
;
578 value
= fmt
->f_comp
->c_tws
->tw_mon
+ 1;
581 str
= tw_moty
[fmt
->f_comp
->c_tws
->tw_mon
];
584 str
= lmonth
[fmt
->f_comp
->c_tws
->tw_mon
];
587 str
= dtwszone (fmt
->f_comp
->c_tws
);
590 value
= fmt
->f_comp
->c_tws
->tw_year
;
593 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
595 value
= tws
->tw_wday
;
598 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
600 str
= tw_dotw
[tws
->tw_wday
];
603 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
605 str
= tw_ldotw
[tws
->tw_wday
];
608 value
= fmt
->f_comp
->c_tws
->tw_yday
;
611 value
= fmt
->f_comp
->c_tws
->tw_zone
;
614 if ((value
= fmt
->f_comp
->c_tws
->tw_clock
) == 0)
615 value
= dmktime(fmt
->f_comp
->c_tws
);
618 if ((value
= fmt
->f_comp
->c_tws
->tw_clock
) == 0)
619 value
= dmktime(fmt
->f_comp
->c_tws
);
620 value
= time((time_t *) 0) - value
;
623 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
625 switch (fmt
->f_comp
->c_tws
->tw_flags
& TW_SDAY
) {
634 if ((fmt
->f_comp
->c_tws
->tw_flags
& TW_SZONE
) == TW_SZEXP
)
640 value
= fmt
->f_comp
->c_tws
->tw_flags
& TW_DST
;
643 str
= dasctime (fmt
->f_comp
->c_tws
, TW_ZONE
);
646 str
= dasctime (fmt
->f_comp
->c_tws
, TW_NULL
);
650 str
= fmt
->f_comp
->c_mn
->m_pers
;
653 str
= fmt
->f_comp
->c_mn
->m_mbox
;
656 str
= fmt
->f_comp
->c_mn
->m_host
;
659 str
= fmt
->f_comp
->c_mn
->m_path
;
662 str
= fmt
->f_comp
->c_mn
->m_gname
;
665 str
= fmt
->f_comp
->c_mn
->m_note
;
668 str
= adrformat( fmt
->f_comp
->c_mn
);
671 value
= fmt
->f_comp
->c_mn
->m_type
;
674 value
= fmt
->f_comp
->c_mn
->m_ingrp
;
677 value
= fmt
->f_comp
->c_mn
->m_nohost
;
681 if ((mn
= fmt
->f_comp
->c_mn
) == &fmt_mnull
) {
682 str
= fmt
->f_comp
->c_text
;
685 if (fmt
->f_type
== FT_LS_ADDR
)
687 if ((str
= mn
->m_pers
) == NULL
) {
688 if ((str
= mn
->m_note
)) {
689 strncpy (buffer
, str
, sizeof(buffer
));
690 buffer
[sizeof(buffer
)-1] = '\0';
694 sp
= str
+ strlen(str
) - 1;
703 } else if (!(str
= get_x400_friendly (mn
->m_mbox
,
704 buffer
, sizeof(buffer
)))) {
706 switch (mn
->m_type
) {
711 snprintf (buffer
, sizeof(buffer
), "%s!%s",
712 mn
->m_host
, mn
->m_mbox
);
717 snprintf (buffer
, sizeof(buffer
), "%s@%s",
718 mn
->m_mbox
, mn
->m_host
);
730 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
734 strncpy(buffer
, str
, sizeof(buffer
));
735 /* strncpy doesn't NUL-terminate if it fills the buffer */
736 buffer
[sizeof(buffer
)-1] = '\0';
739 /* we will parse from buffer to buffer2 */
740 n
= 0; /* n is the input position in str */
741 m
= 0; /* m is the ouput position in buffer2 */
743 while ( str
[n
] != '\0') {
748 buffer2
[m
++] = str
[n
++];
754 buffer2
[m
++] = str
[n
++];
765 if ((t
= comp
->c_tws
->tw_clock
) == 0)
766 t
= dmktime(comp
->c_tws
);
767 tws
= dlocaltime(&t
);
773 if ((t
= comp
->c_tws
->tw_clock
) == 0)
774 t
= dmktime(comp
->c_tws
);
781 if (comp
->c_flags
& CF_PARSED
)
783 if ((sp
= comp
->c_text
) && (tws
= dparsetime(sp
))) {
785 comp
->c_flags
&= ~CF_TRUE
;
786 } else if ((comp
->c_flags
& CF_DATEFAB
) == 0) {
787 memset ((char *) comp
->c_tws
, 0, sizeof *comp
->c_tws
);
788 comp
->c_flags
= CF_TRUE
;
790 comp
->c_flags
|= CF_PARSED
;
794 /* hook for custom address list formatting (see replsbr.c) */
795 str
= formataddr (savestr
, str
);
799 /* output the str register as an address component,
800 * splitting it into multiple lines if necessary. The
801 * value reg. contains the max line length. The lit.
802 * field may contain a string to prepend to the result
808 int indent
, wid
, len
;
814 indent
= strlen (sp
);
816 while( (c
= *sp
++) && cp
< ep
)
819 /* try to break at a comma; failing that, break at a
822 lastb
= 0; sp
= lp
+ wid
;
823 while (sp
> lp
&& (c
= *--sp
) != ',') {
824 if (! lastb
&& isspace(c
))
828 if (! (sp
= lastb
)) {
830 while (*sp
&& *sp
!= ',' && !isspace(*sp
))
837 while (cp
< ep
&& lp
<= sp
)
844 for (i
=indent
; cp
< ep
&& i
> 0; i
--)
848 cpstripped (&cp
, ep
, lp
);
854 if (comp
->c_flags
& CF_PARSED
)
856 if (comp
->c_mn
!= &fmt_mnull
)
858 if ((sp
= comp
->c_text
) && (sp
= getname(sp
)) &&
859 (mn
= getm (sp
, NULL
, 0, fmt_norm
, NULL
))) {
863 comp
->c_flags
|= CF_PARSED
;
865 while (getname("")) /* XXX */
867 comp
->c_mn
= &fmt_mnull
;
873 * if there's no component, we say true. Otherwise we
874 * say "true" only if we can parse the address and it
875 * matches one of our addresses.
878 if (comp
->c_mn
!= &fmt_mnull
)
880 if ((sp
= comp
->c_text
) && (sp
= getname(sp
)) &&
881 (mn
= getm (sp
, NULL
, 0, AD_NAME
, NULL
))) {
884 comp
->c_flags
|= CF_TRUE
;
886 comp
->c_flags
&= ~CF_TRUE
;
887 while ((sp
= getname(sp
)))
888 if ((comp
->c_flags
& CF_TRUE
) == 0 &&
889 (mn
= getm (sp
, NULL
, 0, AD_NAME
, NULL
)))
891 comp
->c_flags
|= CF_TRUE
;
893 while (getname("")) /* XXX */
895 if (comp
->c_text
== 0)
896 comp
->c_flags
|= CF_TRUE
;
898 comp
->c_flags
&= ~CF_TRUE
;
899 comp
->c_mn
= &fmt_mnull
;
905 /* If we're working on a folder (as opposed to a file), add the
906 * current msg to sequence given in literal field. Don't
907 * disturb string or value registers.
909 if (fmt_current_folder
)
910 seq_addmsg(fmt_current_folder
, fmt
->f_text
, dat
[0], -1);
921 return ((struct format
*)0);
925 while (fmt
->f_type
!= FT_DONE
)
930 return (fmt
->f_value
? ++fmt
: (struct format
*) 0);