]>
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
)) {
157 /* isnctrl(), etc., take an int argument. Cygwin's ctype.h
158 intentionally warns if they are passed a char. */
160 if (iscntrl(c
) || isspace(c
)) {
173 #ifdef MULTIBYTE_SUPPORT
174 w
= wcwidth(wide_char
);
175 if (w
>= 0 && remaining
>= w
) {
176 strncpy(cp
, sp
, char_len
);
189 if (cp
+ remaining
> ep
)
193 /* copy string to the right */
194 while (--cp
>= *dest
)
195 *(cp
+ remaining
) = *cp
;
196 /* add padding at the beginning */
198 for (c
=remaining
; c
>0; c
--)
203 /* pad remaining space */
204 while (remaining
-- > 0 && cp
< ep
)
211 cpstripped (char **dest
, char *end
, char *str
)
213 int prevCtrl
= 1; /* This is 1 so we strip out leading spaces */
215 #ifdef MULTIBYTE_SUPPORT
218 #endif /* MULTIBYTE_SUPPORT */
225 #ifdef MULTIBYTE_SUPPORT
226 mbtowc(NULL
, NULL
, 0); /* Reset shift state */
227 #endif /* MULTIBYTE_SUPPORT */
230 * Process each character at a time; if we have multibyte support
231 * then deal with that here.
234 while (*str
!= '\0' && len
> 0 && *dest
< end
) {
235 #ifdef MULTIBYTE_SUPPORT
236 char_len
= mbtowc(&wide_char
, str
, len
);
238 if (char_len
<= 0 || *dest
+ char_len
> end
)
243 if (iswcntrl(wide_char
) || iswspace(wide_char
)) {
245 #else /* MULTIBYTE_SUPPORT */
248 if (iscntrl(c
) || isspace(c
)) {
250 #endif /* MULTIBYTE_SUPPORT */
261 #ifdef MULTIBYTE_SUPPORT
262 memcpy(*dest
, str
, char_len
);
265 #else /* MULTIBYE_SUPPORT */
267 #endif /* MULTIBYTE_SUPPORT */
271 static char *lmonth
[] = { "January", "February","March", "April",
272 "May", "June", "July", "August",
273 "September","October", "November","December" };
276 get_x400_friendly (char *mbox
, char *buffer
, int buffer_len
)
278 char given
[BUFSIZ
], surname
[BUFSIZ
];
287 if (get_x400_comp (mbox
, "/PN=", buffer
, buffer_len
)) {
288 for (mbox
= buffer
; (mbox
= strchr(mbox
, '.')); )
294 if (!get_x400_comp (mbox
, "/S=", surname
, sizeof(surname
)))
297 if (get_x400_comp (mbox
, "/G=", given
, sizeof(given
)))
298 snprintf (buffer
, buffer_len
, "%s %s", given
, surname
);
300 snprintf (buffer
, buffer_len
, "%s", surname
);
306 get_x400_comp (char *mbox
, char *key
, char *buffer
, int buffer_len
)
311 if ((idx
= stringdex (key
, mbox
)) < 0
312 || !(cp
= strchr(mbox
+= idx
+ strlen (key
), '/')))
315 snprintf (buffer
, buffer_len
, "%*.*s", (int)(cp
- mbox
), (int)(cp
- mbox
), mbox
);
320 fmt_scan (struct format
*format
, char *scanl
, int width
, int *dat
)
324 char *savestr
= NULL
;
325 unsigned char *str
= NULL
;
326 char buffer
[BUFSIZ
], buffer2
[BUFSIZ
];
336 ep
= scanl
+ width
- 1;
338 for (fmt
= format
; fmt
->f_type
!= FT_DONE
; fmt
++)
339 switch (fmt
->f_type
) {
342 fmt
->f_comp
->c_flags
&= ~CF_PARSED
;
347 case FT_LS_DECODECOMP
:
349 * Trim these components of any newlines.
351 * But don't trim the "body" and "text" components.
356 if (! (comp
->c_flags
& CF_TRIMMED
) && comp
->c_text
) {
357 i
= strlen(comp
->c_text
);
358 if (comp
->c_text
[i
- 1] == '\n' &&
359 strcmp(comp
->c_name
, "body") != 0 &&
360 strcmp(comp
->c_name
, "text") != 0)
361 comp
->c_text
[i
- 1] = '\0';
362 comp
->c_flags
|= CF_TRIMMED
;
370 switch (fmt
->f_type
) {
373 cpstripped (&cp
, ep
, fmt
->f_comp
->c_text
);
376 cptrimmed (&cp
, fmt
->f_comp
->c_text
, fmt
->f_width
, fmt
->f_fill
, ep
- cp
);
381 while( (c
= *sp
++) && cp
< ep
)
390 ljust
++; /* XXX should do something with this */
392 while( (c
= *sp
++) && --i
>= 0 && cp
< ep
)
394 while( --i
>= 0 && cp
< ep
)
399 cpstripped (&cp
, ep
, str
);
402 cptrimmed (&cp
, str
, fmt
->f_width
, fmt
->f_fill
, ep
- cp
);
406 while ((c
= *sp
++) && cp
< ep
)
410 adios (NULL
, "internal error (FT_STRFW)");
413 n
= snprintf(cp
, ep
- cp
+ 1, "%d", value
);
422 cpnumber (&cp
, value
, fmt
->f_width
, fmt
->f_fill
, ep
- cp
);
433 if (!(value
= (str
&& *str
))) {
440 if (!(value
= (str
== NULL
|| *str
== 0))) {
447 if (value
!= fmt
->f_value
) {
454 if (value
== fmt
->f_value
) {
461 if (value
<= fmt
->f_value
) {
468 if (!(value
= (str
&& match (str
, fmt
->f_text
)))) {
476 value
= match (str
, fmt
->f_text
);
482 if (!(value
= (str
&& uprf (str
, fmt
->f_text
)))) {
489 value
= uprf (str
, fmt
->f_text
);
493 value
= (str
!= NULL
&& *str
!= 0);
497 value
= (str
== NULL
|| *str
== 0);
501 value
= (fmt
->f_value
== value
);
505 value
= (fmt
->f_value
!= value
);
509 value
= (fmt
->f_value
> value
);
520 str
= fmt
->f_comp
->c_text
;
526 if (!(str
= getenv (fmt
->f_text
)))
530 if (!(str
= context_find (fmt
->f_text
)))
534 case FT_LS_DECODECOMP
:
535 if (decode_rfc2047(fmt
->f_comp
->c_text
, buffer2
, sizeof(buffer2
)))
538 str
= fmt
->f_comp
->c_text
;
542 if (str
&& decode_rfc2047(str
, buffer2
, sizeof(buffer2
)))
550 strncpy(buffer
, str
, sizeof(buffer
));
551 buffer
[sizeof(buffer
)-1] = '\0';
553 while (isspace(*str
))
556 if ((i
= fmt
->f_width
) < 0) {
561 if (!ljust
&& i
> 0 && (int) strlen(str
) > i
)
564 xp
+= strlen(str
) - 1;
565 while (xp
> str
&& isspace(*xp
))
567 if (ljust
&& i
> 0 && (int) strlen(str
) > i
)
568 str
+= strlen(str
) - i
;
573 value
= (fmt
->f_comp
->c_flags
& CF_TRUE
) != 0;
576 value
= (comp
= fmt
->f_comp
)->c_text
? atoi(comp
->c_text
) : 0;
579 value
= fmt
->f_value
;
582 value
= dat
[fmt
->f_value
];
590 case FT_LV_CHAR_LEFT
:
591 value
= width
- (cp
- scanl
);
594 value
+= fmt
->f_value
;
597 value
= fmt
->f_value
- value
;
601 value
= value
/ fmt
->f_value
;
607 value
= value
% fmt
->f_value
;
616 value
= fmt
->f_comp
->c_tws
->tw_sec
;
619 value
= fmt
->f_comp
->c_tws
->tw_min
;
622 value
= fmt
->f_comp
->c_tws
->tw_hour
;
625 value
= fmt
->f_comp
->c_tws
->tw_mday
;
628 value
= fmt
->f_comp
->c_tws
->tw_mon
+ 1;
631 str
= tw_moty
[fmt
->f_comp
->c_tws
->tw_mon
];
634 str
= lmonth
[fmt
->f_comp
->c_tws
->tw_mon
];
637 str
= dtwszone (fmt
->f_comp
->c_tws
);
640 value
= fmt
->f_comp
->c_tws
->tw_year
;
643 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
645 value
= tws
->tw_wday
;
648 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
650 str
= tw_dotw
[tws
->tw_wday
];
653 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
655 str
= tw_ldotw
[tws
->tw_wday
];
658 value
= fmt
->f_comp
->c_tws
->tw_yday
;
661 value
= fmt
->f_comp
->c_tws
->tw_zone
;
664 if ((value
= fmt
->f_comp
->c_tws
->tw_clock
) == 0)
665 value
= dmktime(fmt
->f_comp
->c_tws
);
668 if ((value
= fmt
->f_comp
->c_tws
->tw_clock
) == 0)
669 value
= dmktime(fmt
->f_comp
->c_tws
);
670 value
= time((time_t *) 0) - value
;
673 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
675 switch (fmt
->f_comp
->c_tws
->tw_flags
& TW_SDAY
) {
684 if ((fmt
->f_comp
->c_tws
->tw_flags
& TW_SZONE
) == TW_SZEXP
)
690 value
= fmt
->f_comp
->c_tws
->tw_flags
& TW_DST
;
693 str
= dasctime (fmt
->f_comp
->c_tws
, TW_ZONE
);
696 str
= dasctime (fmt
->f_comp
->c_tws
, TW_NULL
);
700 str
= fmt
->f_comp
->c_mn
->m_pers
;
703 str
= fmt
->f_comp
->c_mn
->m_mbox
;
706 str
= fmt
->f_comp
->c_mn
->m_host
;
709 str
= fmt
->f_comp
->c_mn
->m_path
;
712 str
= fmt
->f_comp
->c_mn
->m_gname
;
715 str
= fmt
->f_comp
->c_mn
->m_note
;
718 str
= adrformat( fmt
->f_comp
->c_mn
);
721 value
= fmt
->f_comp
->c_mn
->m_type
;
724 value
= fmt
->f_comp
->c_mn
->m_ingrp
;
727 value
= fmt
->f_comp
->c_mn
->m_nohost
;
731 if ((mn
= fmt
->f_comp
->c_mn
) == &fmt_mnull
) {
732 str
= fmt
->f_comp
->c_text
;
735 if (fmt
->f_type
== FT_LS_ADDR
)
737 if ((str
= mn
->m_pers
) == NULL
) {
738 if ((str
= mn
->m_note
)) {
739 strncpy (buffer
, str
, sizeof(buffer
));
740 buffer
[sizeof(buffer
)-1] = '\0';
744 sp
= str
+ strlen(str
) - 1;
753 } else if (!(str
= get_x400_friendly (mn
->m_mbox
,
754 buffer
, sizeof(buffer
)))) {
756 switch (mn
->m_type
) {
761 snprintf (buffer
, sizeof(buffer
), "%s!%s",
762 mn
->m_host
, mn
->m_mbox
);
767 snprintf (buffer
, sizeof(buffer
), "%s@%s",
768 mn
->m_mbox
, mn
->m_host
);
780 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
784 strncpy(buffer
, str
, sizeof(buffer
));
785 /* strncpy doesn't NUL-terminate if it fills the buffer */
786 buffer
[sizeof(buffer
)-1] = '\0';
789 /* we will parse from buffer to buffer2 */
790 n
= 0; /* n is the input position in str */
791 m
= 0; /* m is the ouput position in buffer2 */
793 while ( str
[n
] != '\0') {
798 buffer2
[m
++] = str
[n
++];
804 buffer2
[m
++] = str
[n
++];
815 if ((t
= comp
->c_tws
->tw_clock
) == 0)
816 t
= dmktime(comp
->c_tws
);
817 tws
= dlocaltime(&t
);
823 if ((t
= comp
->c_tws
->tw_clock
) == 0)
824 t
= dmktime(comp
->c_tws
);
831 if (comp
->c_flags
& CF_PARSED
)
833 if ((sp
= comp
->c_text
) && (tws
= dparsetime(sp
))) {
835 comp
->c_flags
&= ~CF_TRUE
;
836 } else if ((comp
->c_flags
& CF_DATEFAB
) == 0) {
837 memset ((char *) comp
->c_tws
, 0, sizeof *comp
->c_tws
);
838 comp
->c_flags
= CF_TRUE
;
840 comp
->c_flags
|= CF_PARSED
;
844 /* hook for custom address list formatting (see replsbr.c) */
845 str
= formataddr (savestr
, str
);
849 /* The same as formataddr, but doesn't do duplicate suppression */
850 str
= concataddr (savestr
, str
);
854 /* output the str register as an address component,
855 * splitting it into multiple lines if necessary. The
856 * value reg. contains the max line length. The lit.
857 * field may contain a string to prepend to the result
863 int indent
, wid
, len
;
869 indent
= strlen (sp
);
872 adios(NULL
, "putaddr -- num register (%d) must be greater "
873 "than label width (%d)", value
, indent
);
875 while( (c
= *sp
++) && cp
< ep
)
878 /* try to break at a comma; failing that, break at a
881 lastb
= 0; sp
= lp
+ wid
;
882 while (sp
> lp
&& (c
= *--sp
) != ',') {
883 if (! lastb
&& isspace(c
))
887 if (! (sp
= lastb
)) {
889 while (*sp
&& *sp
!= ',' && !isspace(*sp
))
896 while (cp
< ep
&& lp
<= sp
)
903 for (i
=indent
; cp
< ep
&& i
> 0; i
--)
907 cpstripped (&cp
, ep
, lp
);
913 if (comp
->c_flags
& CF_PARSED
)
915 if (comp
->c_mn
!= &fmt_mnull
)
917 if ((sp
= comp
->c_text
) && (sp
= getname(sp
)) &&
918 (mn
= getm (sp
, NULL
, 0, fmt_norm
, NULL
))) {
922 comp
->c_flags
|= CF_PARSED
;
924 while (getname("")) /* XXX */
926 comp
->c_mn
= &fmt_mnull
;
932 * if there's no component, we say true. Otherwise we
933 * say "true" only if we can parse the address and it
934 * matches one of our addresses.
937 if (comp
->c_mn
!= &fmt_mnull
)
939 if ((sp
= comp
->c_text
) && (sp
= getname(sp
)) &&
940 (mn
= getm (sp
, NULL
, 0, AD_NAME
, NULL
))) {
943 comp
->c_flags
|= CF_TRUE
;
945 comp
->c_flags
&= ~CF_TRUE
;
946 while ((sp
= getname(sp
)))
947 if ((comp
->c_flags
& CF_TRUE
) == 0 &&
948 (mn
= getm (sp
, NULL
, 0, AD_NAME
, NULL
)))
950 comp
->c_flags
|= CF_TRUE
;
952 while (getname("")) /* XXX */
954 if (comp
->c_text
== 0)
955 comp
->c_flags
|= CF_TRUE
;
957 comp
->c_flags
&= ~CF_TRUE
;
958 comp
->c_mn
= &fmt_mnull
;
964 /* If we're working on a folder (as opposed to a file), add the
965 * current msg to sequence given in literal field. Don't
966 * disturb string or value registers.
968 if (fmt_current_folder
)
969 seq_addmsg(fmt_current_folder
, fmt
->f_text
, dat
[0], -1);
980 return ((struct format
*)0);
984 while (fmt
->f_type
!= FT_DONE
)
989 return (fmt
->f_value
? ++fmt
: (struct format
*) 0);