]>
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>
31 extern char *formataddr (); /* hook for custom address formatting */
34 struct msgs
*fmt_current_folder
; /* current folder (set by main program) */
37 extern int fmt_norm
; /* defined in sbr/fmt_def.c = AD_NAME */
38 struct mailname fmt_mnull
;
43 static int match (char *, char *);
44 static char *get_x400_friendly (char *, char *, int);
45 static int get_x400_comp (char *, char *, char *, int);
49 * test if string "sub" appears anywhere in
50 * string "str" (case insensitive).
54 match (char *str
, char *sub
)
61 c1
= (isalpha(c1
) && isupper(c1
)) ? tolower(c1
) : c1
;
62 while ((c2
= *str
++) && c1
!= ((isalpha(c2
) && isupper(c2
)) ? tolower(c2
) : c2
))
66 s1
= sub
+ 1; s2
= str
;
67 while ((c1
= *s1
++) && ((isalpha(c1
) && isupper(c1
)) ? tolower(c1
) : c1
) == ((isalpha(c2
=*s2
++) && isupper(c2
)) ? tolower(c2
) : c2
))
74 while ((c2
= *str
++) && (c1
| 040) != (c2
| 040))
78 s1
= sub
+ 1; s2
= str
;
79 while ((c1
= *s1
++) && (c1
| 040) == (*s2
++ | 040))
89 * macros to format data
92 #define PUTDF(cp, num, wid, fill)\
100 *--sp = (i % 10) + '0';\
102 } while (i > 0 && sp > cp);\
105 else if ((num) < 0 && sp > cp)\
112 #define PUTD(cp, num)\
114 if ((i = (num)) == 0)\
117 if ((i = (num)) < 0) {\
124 while (cp < ep && c > 1) {\
126 *cp++ = (i / c) + '0';\
133 #define PUTSF(cp, str, wid, fill) {\
135 if ((i = (wid)) < 0) {\
145 while( --i >= c && cp < ep)\
150 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
153 while ((c = (unsigned char) *sp++) && --i >= 0 && cp < ep)\
157 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
163 while( --i >= 0 && cp < ep)\
167 #define PUTS(cp, str) {\
169 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
171 while((c = (unsigned char) *sp++) && cp < ep)\
175 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
183 #define PUTSF(cp, str, wid, fill) {\
185 if ((i = (wid)) < 0) {\
195 while( --i >= c && cp < ep)\
200 while ((c = *sp) && c <= 32)\
203 while ((c = *sp++) && --i >= 0 && cp < ep)\
207 while ((c = *sp) && c <= 32)\
213 while( --i >= 0 && cp < ep)\
217 #define PUTS(cp, str) {\
219 while ((c = *sp) && c <= 32)\
221 while( (c = *sp++) && cp < ep)\
225 while ( (c = *sp) && c <= 32 )\
235 static char *lmonth
[] = { "January", "February","March", "April",
236 "May", "June", "July", "August",
237 "September","October", "November","December" };
240 get_x400_friendly (char *mbox
, char *buffer
, int buffer_len
)
242 char given
[BUFSIZ
], surname
[BUFSIZ
];
251 if (get_x400_comp (mbox
, "/PN=", buffer
, buffer_len
)) {
252 for (mbox
= buffer
; (mbox
= strchr(mbox
, '.')); )
258 if (!get_x400_comp (mbox
, "/S=", surname
, sizeof(surname
)))
261 if (get_x400_comp (mbox
, "/G=", given
, sizeof(given
)))
262 snprintf (buffer
, buffer_len
, "%s %s", given
, surname
);
264 snprintf (buffer
, buffer_len
, "%s", surname
);
270 get_x400_comp (char *mbox
, char *key
, char *buffer
, int buffer_len
)
275 if ((idx
= stringdex (key
, mbox
)) < 0
276 || !(cp
= strchr(mbox
+= idx
+ strlen (key
), '/')))
279 snprintf (buffer
, buffer_len
, "%*.*s", cp
- mbox
, cp
- mbox
, mbox
);
284 fmt_scan (struct format
*format
, char *scanl
, int width
, int *dat
)
287 char *savestr
, *str
= NULL
;
288 char buffer
[BUFSIZ
], buffer2
[BUFSIZ
];
298 ep
= scanl
+ width
- 1;
300 for (fmt
= format
; fmt
->f_type
!= FT_DONE
; fmt
++)
301 switch (fmt
->f_type
) {
304 fmt
->f_comp
->c_flags
&= ~CF_PARSED
;
311 switch (fmt
->f_type
) {
314 PUTS (cp
, fmt
->f_comp
->c_text
);
317 PUTSF (cp
, fmt
->f_comp
->c_text
, fmt
->f_width
, fmt
->f_fill
);
322 while( (c
= *sp
++) && cp
< ep
)
331 ljust
++; /* XXX should do something with this */
333 while( (c
= *sp
++) && --i
>= 0 && cp
< ep
)
335 while( --i
>= 0 && cp
< ep
)
343 PUTSF (cp
, str
, fmt
->f_width
, fmt
->f_fill
);
346 adios (NULL
, "internal error (FT_STRFW)");
352 PUTDF (cp
, value
, fmt
->f_width
, fmt
->f_fill
);
363 if (!(value
= (str
&& *str
))) {
370 if (!(value
= (str
== NULL
|| *str
== 0))) {
377 if (value
!= fmt
->f_value
) {
384 if (value
== fmt
->f_value
) {
391 if (value
<= fmt
->f_value
) {
398 if (!(value
= (str
&& match (str
, fmt
->f_text
)))) {
406 value
= match (str
, fmt
->f_text
);
412 if (!(value
= (str
&& uprf (str
, fmt
->f_text
)))) {
419 value
= uprf (str
, fmt
->f_text
);
423 value
= (str
!= NULL
&& *str
!= 0);
427 value
= (str
== NULL
|| *str
== 0);
431 value
= (fmt
->f_value
== value
);
435 value
= (fmt
->f_value
!= value
);
439 value
= (fmt
->f_value
> value
);
450 str
= fmt
->f_comp
->c_text
;
456 if (!(str
= getenv (fmt
->f_text
)))
460 if (!(str
= context_find (fmt
->f_text
)))
464 case FT_LS_DECODECOMP
:
465 if (decode_rfc2047(fmt
->f_comp
->c_text
, buffer2
))
468 str
= fmt
->f_comp
->c_text
;
472 if (str
&& decode_rfc2047(str
, buffer2
))
480 strncpy(buffer
, str
, sizeof(buffer
));
482 while (isspace(*str
))
485 if ((i
= fmt
->f_width
) < 0) {
490 if (!ljust
&& i
> 0 && strlen(str
) > i
)
493 xp
+= strlen(str
) - 1;
494 while (xp
> str
&& isspace(*xp
))
496 if (ljust
&& i
> 0 && strlen(str
) > i
)
497 str
+= strlen(str
) - i
;
502 value
= (fmt
->f_comp
->c_flags
& CF_TRUE
) != 0;
505 value
= (comp
= fmt
->f_comp
)->c_text
? atoi(comp
->c_text
) : 0;
508 value
= fmt
->f_value
;
511 value
= dat
[fmt
->f_value
];
516 case FT_LV_CHAR_LEFT
:
517 value
= width
- (cp
- scanl
);
520 value
+= fmt
->f_value
;
523 value
= fmt
->f_value
- value
;
527 value
= value
/ fmt
->f_value
;
533 value
= value
% fmt
->f_value
;
542 value
= fmt
->f_comp
->c_tws
->tw_sec
;
545 value
= fmt
->f_comp
->c_tws
->tw_min
;
548 value
= fmt
->f_comp
->c_tws
->tw_hour
;
551 value
= fmt
->f_comp
->c_tws
->tw_mday
;
554 value
= fmt
->f_comp
->c_tws
->tw_mon
+ 1;
557 str
= tw_moty
[fmt
->f_comp
->c_tws
->tw_mon
];
560 str
= lmonth
[fmt
->f_comp
->c_tws
->tw_mon
];
563 str
= dtwszone (fmt
->f_comp
->c_tws
);
566 value
= fmt
->f_comp
->c_tws
->tw_year
;
569 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
571 value
= tws
->tw_wday
;
574 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
576 str
= tw_dotw
[tws
->tw_wday
];
579 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
581 str
= tw_ldotw
[tws
->tw_wday
];
584 value
= fmt
->f_comp
->c_tws
->tw_yday
;
587 value
= fmt
->f_comp
->c_tws
->tw_zone
;
590 if ((value
= fmt
->f_comp
->c_tws
->tw_clock
) == 0)
591 value
= dmktime(fmt
->f_comp
->c_tws
);
594 if ((value
= fmt
->f_comp
->c_tws
->tw_clock
) == 0)
595 value
= dmktime(fmt
->f_comp
->c_tws
);
596 value
= time((time_t *) 0) - value
;
599 if (!(((tws
= fmt
->f_comp
->c_tws
)->tw_flags
) & (TW_SEXP
|TW_SIMP
)))
601 switch (fmt
->f_comp
->c_tws
->tw_flags
& TW_SDAY
) {
610 if ((fmt
->f_comp
->c_tws
->tw_flags
& TW_SZONE
) == TW_SZEXP
)
616 value
= fmt
->f_comp
->c_tws
->tw_flags
& TW_DST
;
619 str
= dasctime (fmt
->f_comp
->c_tws
, TW_ZONE
);
622 str
= dasctime (fmt
->f_comp
->c_tws
, TW_NULL
);
626 str
= fmt
->f_comp
->c_mn
->m_pers
;
629 str
= fmt
->f_comp
->c_mn
->m_mbox
;
632 str
= fmt
->f_comp
->c_mn
->m_host
;
635 str
= fmt
->f_comp
->c_mn
->m_path
;
638 str
= fmt
->f_comp
->c_mn
->m_gname
;
641 str
= fmt
->f_comp
->c_mn
->m_note
;
644 str
= adrformat( fmt
->f_comp
->c_mn
);
647 value
= fmt
->f_comp
->c_mn
->m_type
;
650 value
= fmt
->f_comp
->c_mn
->m_ingrp
;
653 value
= fmt
->f_comp
->c_mn
->m_nohost
;
657 if ((mn
= fmt
->f_comp
->c_mn
) == &fmt_mnull
) {
658 str
= fmt
->f_comp
->c_text
;
661 if (fmt
->f_type
== FT_LS_ADDR
)
663 if ((str
= mn
->m_pers
) == NULL
) {
664 if ((str
= mn
->m_note
)) {
665 strncpy (buffer
, str
, sizeof(buffer
));
669 sp
= str
+ strlen(str
) - 1;
678 } else if (!(str
= get_x400_friendly (mn
->m_mbox
,
679 buffer
, sizeof(buffer
)))) {
681 switch (mn
->m_type
) {
686 snprintf (buffer
, sizeof(buffer
), "%s!%s",
687 mn
->m_host
, mn
->m_mbox
);
692 snprintf (buffer
, sizeof(buffer
), "%s@%s",
693 mn
->m_mbox
, mn
->m_host
);
706 if ((t
= comp
->c_tws
->tw_clock
) == 0)
707 t
= dmktime(comp
->c_tws
);
708 tws
= dlocaltime(&t
);
714 if ((t
= comp
->c_tws
->tw_clock
) == 0)
715 t
= dmktime(comp
->c_tws
);
722 if (comp
->c_flags
& CF_PARSED
)
724 if ((sp
= comp
->c_text
) && (tws
= dparsetime(sp
))) {
726 comp
->c_flags
&= ~CF_TRUE
;
727 } else if ((comp
->c_flags
& CF_DATEFAB
) == 0) {
728 memset ((char *) comp
->c_tws
, 0, sizeof *comp
->c_tws
);
729 comp
->c_flags
= CF_TRUE
;
731 comp
->c_flags
|= CF_PARSED
;
735 /* hook for custom address list formatting (see replsbr.c) */
736 str
= formataddr (savestr
, str
);
740 /* output the str register as an address component,
741 * splitting it into multiple lines if necessary. The
742 * value reg. contains the max line length. The lit.
743 * field may contain a string to prepend to the result
748 int indent
, wid
, len
;
754 indent
= strlen (sp
);
756 while( (c
= *sp
++) && cp
< ep
)
759 /* try to break at a comma; failing that, break at a
762 lastb
= 0; sp
= lp
+ wid
;
763 while (sp
> lp
&& (c
= *--sp
) != ',') {
764 if (! lastb
&& isspace(c
))
768 if (! (sp
= lastb
)) {
770 while (*sp
&& *sp
!= ',' && !isspace(*sp
))
777 while (cp
< ep
&& lp
<= sp
)
784 for (i
=indent
; cp
< ep
&& i
> 0; i
--)
794 if (comp
->c_flags
& CF_PARSED
)
796 if (comp
->c_mn
!= &fmt_mnull
)
798 if ((sp
= comp
->c_text
) && (sp
= getname(sp
)) &&
799 (mn
= getm (sp
, NULL
, 0, fmt_norm
, NULL
))) {
803 comp
->c_flags
|= CF_PARSED
;
805 while (getname("")) /* XXX */
807 comp
->c_mn
= &fmt_mnull
;
813 * if there's no component, we say true. Otherwise we
814 * say "true" only if we can parse the address and it
815 * matches one of our addresses.
818 if (comp
->c_mn
!= &fmt_mnull
)
820 if ((sp
= comp
->c_text
) && (sp
= getname(sp
)) &&
821 (mn
= getm (sp
, NULL
, 0, AD_NAME
, NULL
))) {
824 comp
->c_flags
|= CF_TRUE
;
826 comp
->c_flags
&= ~CF_TRUE
;
827 while ((sp
= getname(sp
)))
828 if ((comp
->c_flags
& CF_TRUE
) == 0 &&
829 (mn
= getm (sp
, NULL
, 0, AD_NAME
, NULL
)))
831 comp
->c_flags
|= CF_TRUE
;
833 while (getname("")) /* XXX */
835 if (comp
->c_text
== 0)
836 comp
->c_flags
|= CF_TRUE
;
838 comp
->c_flags
&= ~CF_TRUE
;
839 comp
->c_mn
= &fmt_mnull
;
845 /* If we're working on a folder (as opposed to a file), add the
846 * current msg to sequence given in literal field. Don't
847 * disturb string or value registers.
849 if (fmt_current_folder
)
850 seq_addmsg(fmt_current_folder
, fmt
->f_text
, dat
[0], -1);
861 return ((struct format
*)0);
865 while (fmt
->f_type
!= FT_DONE
)
870 return (fmt
->f_value
? ++fmt
: (struct format
*) 0);