2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
26 * $TOG: DtMailRc.C /main/9 1998/07/23 18:02:08 mgreess $
28 * RESTRICTED CONFIDENTIAL INFORMATION:
30 * The information in this document is subject to special
31 * restrictions in a confidential disclosure agreement between
32 * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
33 * document outside HP, IBM, Sun, USL, SCO, or Univel without
34 * Sun's specific written approval. This document and all copies
35 * and derivative works thereof must be returned or destroyed at
38 * Copyright 1993, 1995, 1995 Sun Microsystems, Inc. All rights reserved.
44 * Format of the command description table.
45 * The actual table is declared and initialized
51 #include <sys/param.h>
55 #include <DtMail/DtMail.hh>
56 #include <DtMail/DtMailError.hh>
57 #include <DtMail/Threads.hh>
58 #include <DtMail/IO.hh>
59 #include <EUSCompat.h>
60 #include "str_utils.h"
63 char *c_name; /* Name of command */
64 int (*c_func)(char **, DtMail::MailRc *); /* Implementor of the command */
65 void (*c_write)(const char * verbatim, char ** args, FILE * outf);
66 short c_argtype; /* Type of arglist (see below) */
67 short c_msgflag; /* Required flags of messages */
68 short c_msgmask; /* Relevant flags of messages */
71 struct var *DtMail::MailRc::variables[HSHSIZE]; /* Pointer to active var list */
73 typedef DtMail::MailRc::globals DtMailRc_globals;
74 DtMailRc_globals DtMail::MailRc::glob;
76 struct DtMail::MailRc::globals DtMail::MailRc::glob;
78 char *DtMail::MailRc::nullfield;
79 Boolean DtMail::MailRc::clearAliases;
82 #define DTMAIL_FORK vfork
84 #define DTMAIL_FORK fork
87 #define MAXIMUM_PATH_LENGTH 2048
89 /* can't initialize unions */
91 #define c_minargs c_msgflag /* Minimum argcount for RAWLIST */
92 #define c_maxargs c_msgmask /* Max argcount for RAWLIST */
96 // defines needed by mail-parse
97 #define LINESIZE 10240 /* max readable line width */
98 #define MAXARGC 1024 /* Maximum list of raw strings */
99 #define equal(a, b) (strcmp(a,b)==0)/* A nice function to string compare */
100 #define NONE ((struct cmd *) 0) /* The nil pointer to command tab */
102 #define CANY 0 /* Execute in send or receive mode */
103 #define CRCV 1 /* Execute in receive mode only */
104 #define CSEND 2 /* Execute in send mode only */
105 #define CTTY 3 /* Execute if attached to a tty only */
106 #define CNOTTY 4 /* Execute if not attached to a tty */
108 #define STRLIST 1 /* A pure string */
109 #define RAWLIST 2 /* Shell string list */
110 #define F 01000 /* Is a conditional command */
111 #define NOLIST 3 /* Just plain 0 */
112 #define CANY 0 /* Execute in send or receive mode */
113 #define NOSTR ((char *) 0) /* Null string pointer */
114 // #define NOFILE_DTMAIL 20 /* this define is here for */
115 // /* compatibility purposes only */
116 // /* and will be removed in a */
117 // /* later release */
123 struct var *v_link; /* Forward link to next variable */
124 char *v_name; /* The variable's name */
125 char *v_value; /* And it's current value */
126 int v_written; /* It has been written on this pass. */
136 struct cmd cmdtab[] = {
137 { "unset", DtMail::MailRc::unset, DtMail::MailRc::wunset, RAWLIST, 1, 1000, },
138 { "alias", DtMail::MailRc::group, DtMail::MailRc::wgroup, RAWLIST, 2, 1000, },
139 { "group", DtMail::MailRc::group, DtMail::MailRc::wgroup, RAWLIST, 2, 1000, },
140 { "set", DtMail::MailRc::set, DtMail::MailRc::wset, RAWLIST, 1, 1000,},
141 { "ignore", DtMail::MailRc::igfield, DtMail::MailRc::wigfield, RAWLIST, 1, 1000, },
142 { "source", DtMail::MailRc::source, DtMail::MailRc::wsource, RAWLIST, 1, 1000, },
143 { "if", DtMail::MailRc::ifcmd, DtMail::MailRc::wifcmd, F|RAWLIST, 1, 1, },
144 { "else", DtMail::MailRc::elsecmd, DtMail::MailRc::welsecmd, F|RAWLIST, 0, 0, },
145 { "endif", DtMail::MailRc::endifcmd, DtMail::MailRc::wendifcmd, F|RAWLIST, 0, 0, },
146 { "alternates", DtMail::MailRc::alternates, DtMail::MailRc::walternates, RAWLIST, 1, 1000, },
147 { "clearaliases", DtMail::MailRc::clearaliases, DtMail::MailRc::wclearaliases, RAWLIST, 0, 0, },
152 #define SYSTEM_MAILRC "/etc/mail/mail.rc"
153 #elif defined(_AIX) || defined(__osf__) || defined(linux)
154 #define SYSTEM_MAILRC "/usr/share/lib/Mail.rc"
155 #elif defined(USL) || defined(__hpux)
156 #define SYSTEM_MAILRC "/usr/share/lib/mailx.rc"
157 #elif defined(CSRG_BASED)
158 #define SYSTEM_MAILRC "/etc/mail.rc"
162 DtMail::MailRc::MailRc(DtMailEnv &env, DtMail::Session *session)
170 _parseError = DTME_NoError;
176 char * mail_rc = getenv("MAILRC");
178 _mailrc_name = strdup(mail_rc);
182 GetPasswordEntry(pw);
184 _mailrc_name = (char *)malloc(MAXIMUM_PATH_LENGTH);
186 strcpy(_mailrc_name, pw.pw_dir);
187 strcat(_mailrc_name, "/.mailrc");
196 if ((rval = this->load(_mailrc_name, line)) != 0) {
197 char *msg = new char[MAXIMUM_PATH_LENGTH + LINESIZE];
199 sprintf(msg, "error while reading \'%s\' in %s\n", line,
202 else if (rval == -2) {
203 sprintf(msg, "%s\n", line);
205 else if (rval == -3) {
206 sprintf(msg, "error: no endif for conditional if statement while reading %s\n", _mailrc_name);
207 env.setError(DTME_ResourceParsingNoEndif, NULL);
208 _parseError = DTME_ResourceParsingNoEndif;
211 env.logError(DTM_FALSE, "%s", msg);
218 if ((rval = this->load(SYSTEM_MAILRC, line)) != 0) {
219 char *msg = new char[MAXIMUM_PATH_LENGTH + LINESIZE];
222 sprintf(msg, "error while reading \'%s\' in %s\n",
223 line, SYSTEM_MAILRC);
224 env.logError(DTM_FALSE, "%s", msg);
225 } else if (rval == -2) {
226 /* Ignore error if the system logfile does not exist. */
233 DtMail::MailRc::~MailRc(void)
238 // return true if in list
239 DtMailBoolean DtMail::MailRc::ignore(DtMailEnv &env, const char *name)
242 char *ignore_list = (char *)MailRc::hm_test((struct hash **)glob.g_ignore, (char *)name);
246 if(ignore_list != NULL) // we have a valid ignore list
252 env.setError(DTME_NoObjectValue, NULL);
259 DtMail::MailRc::addIgnore(DtMailEnv & error, const char * name)
262 add_ignore((char *)name);
266 DtMail::MailRc::removeIgnore(DtMailEnv & error, const char * name)
269 if(MailRc::hm_test((struct hash **)glob.g_ignore, (char *)name)) {
270 MailRc::hm_delete((struct hash **)glob.g_ignore, (char *)name);
274 const char *DtMail::MailRc::getAlias(DtMailEnv &env, const char * name)
277 char *return_value = (char *)hm_test((struct hash **)glob.g_alias, (char *)name);
281 if(return_value == NULL)
283 env.setError(DTME_NoObjectValue, NULL);
294 DtMail::MailRc::setAlias(DtMailEnv & error,
300 if (hm_test((struct hash **)glob.g_alias, (char *)name)) {
301 hm_delete((struct hash **)glob.g_alias, (char *)name);
304 add_alias((char *)name, (char *)value);
308 DtMail::MailRc::removeAlias(DtMailEnv & error,
313 if (hm_test((struct hash **)glob.g_alias, (char *)name)) {
314 hm_delete((struct hash **)glob.g_alias, (char *)name);
319 // Adapted from uuencode and uudecode written by Ian Lance Taylor.
321 #define ENCRYPT(c) ((c) ? (((c)&077) + ' ' + 2) : ('`' + 2))
322 #define DECRYPT(c) ((((c) - ' ') & 077) - 2)
325 DtMail::MailRc::encryptedLength(int length)
327 // One for the length, one for the '\0', expansion factor of 4/3.
328 int encrypted_length = (2 + (4 * ((length+2)/3)));
329 return encrypted_length;
333 DtMail::MailRc::encryptValue(char *to, char *from, int buflen)
339 memset(to, 0, buflen);
341 length = strlen(from);
342 to[i++] = ENCRYPT(length);
344 for (p=from; length>0; length-=3, p+=3)
350 ch = ((*p << 4) & 060) | ((p[1] >> 4) & 017);
354 ch = ((p[1] << 2) & 074) | ((p[2] >> 6) & 03);
365 DtMail::MailRc::decryptValue(char *to, char *from, int buflen)
371 memset(to, 0, buflen);
374 length = DECRYPT(*p);
375 if (length<=0) return 1;
377 for (++p; length>0; p+=4, length-=3)
381 ch = DECRYPT(p[0]) << 2 | DECRYPT(p[1]) >> 4;
383 ch = DECRYPT(p[1]) << 4 | DECRYPT(p[2]) >> 2;
385 ch = DECRYPT(p[2]) << 6 | DECRYPT(p[3]);
392 ch = DECRYPT(p[0]) << 2 | DECRYPT(p[1]) >> 4;
397 ch = DECRYPT(p[1]) << 4 | DECRYPT(p[2]) >> 2;
405 // if set with value return no error in env and set value
406 // if set with no value return no error in env and return empty string
407 // if not set, return error in env and leave value alone
409 void DtMail::MailRc::getValue(DtMailEnv &env,
412 DtMailBoolean decrypt)
414 char *get_result = mt_value((char *)var);
419 if (get_result != NULL)
423 int length = encryptedLength(strlen(get_result));
424 *value = (char*) malloc(length);
425 if (decryptValue((char*) *value, get_result, length))
426 strcpy((char*) *value, get_result);
429 *value = strdup((char *)get_result);
432 env.setError(DTME_NoObjectValue, NULL);
436 DtMail::MailRc::setValue(DtMailEnv & error,
439 DtMailBoolean encrypt)
443 if (encrypt && strlen(value))
445 int length = encryptedLength(strlen(value));
446 char *encrypted = (char*) malloc(length);
447 encryptValue(encrypted, (char*) value, length);
448 mt_assign((char*) var, (char*) encrypted);
451 mt_assign((char*) var, (char*) value);
455 DtMail::MailRc::removeValue(DtMailEnv & error,
460 mt_deassign((char *)var);
463 // return alternates list
464 const char * DtMail::MailRc::getAlternates(DtMailEnv &env)
467 register struct hash *h;
468 struct hash **table = (struct hash **)glob.g_alternates;
473 // we don't free this memory...
474 alternate_list = NULL;
478 env.setError(DTME_NoObjectValue, NULL);
487 len = strlen((const char*)h->h_key);
489 if(alternate_list == NULL)
491 alternate_list = (char *)malloc(len + 1); // plus terminator
492 strcpy(alternate_list, (const char*)h->h_key);
496 len += strlen(alternate_list);
497 alternate_list = (char *)realloc(alternate_list,
498 len + 2); // plus terminator
500 strcat(alternate_list, " "); // add space
501 strcat(alternate_list, (const char *)h->h_key);
510 return (alternate_list);
515 DtMail::MailRc::setAlternate(DtMailEnv & error, const char * alt)
519 add_alternates((char *)alt);
523 DtMail::MailRc::removeAlternate(DtMailEnv & error, const char * name)
527 if(MailRc::hm_test((struct hash**)glob.g_alternates, (char *)name)) {
528 MailRc::hm_delete((struct hash **)glob.g_alternates, (char *)name);
533 DtMail::MailRc::update(DtMailEnv & error)
537 // Generate a temporary file.
538 char *tmp_mailrc = new char[MAXIMUM_PATH_LENGTH];
539 strcpy(tmp_mailrc, _mailrc_name);
540 strcat(tmp_mailrc, ".tmp");
542 FILE * outf = fopen(tmp_mailrc, "w+");
544 error.setError(DTME_ObjectCreationFailed);
545 delete [] tmp_mailrc;
549 // Now open the mailrc for input.
550 FILE * inf = fopen(_mailrc_name, "r");
552 // Now we will read the input file, and copy it to the output,
553 // based on type and changes to the mailrc.
555 updateByLine(inf, outf);
560 // Now we need to make a final scan. This will cause new values
561 // to be written and the written flag to be reset for the next
566 hm_scan((struct hash **)glob.g_alias, ngroup, outf);
568 // Alternates and groups we will add to a list, and then
569 // put them out in one batch.
571 DtVirtArray<char *> value_list(32);
573 hm_scan((struct hash **)glob.g_ignore, nigfield, &value_list);
575 if (value_list.length()) {
576 fwrite("ignore ", 1, 7, outf);
577 while (value_list.length()) {
578 char * val = value_list[0];
579 fwrite(val, 1, strlen(val), outf);
580 fwrite(" ", 1, 1, outf);
581 value_list.remove(0);
583 fwrite("\n", 1, 1, outf);
586 hm_scan((struct hash **)glob.g_alternates, nalternates, &value_list);
588 if (value_list.length()) {
589 fwrite("alternates ", 1, 11, outf);
590 while (value_list.length()) {
591 char * val = value_list[0];
592 fwrite(val, 1, strlen(val), outf);
593 fwrite(" ", 1, 1, outf);
594 value_list.remove(0);
596 fwrite("\n", 1, 1, outf);
600 if (rename(tmp_mailrc, _mailrc_name)) {
601 error.setError(DTME_ObjectAccessFailed);
602 delete [] tmp_mailrc;
605 delete [] tmp_mailrc;
608 void DtMail::MailRc::getAliasList(hm_callback stuffing_func, void *client_data)
610 hm_scan((struct hash **)glob.g_alias, stuffing_func, client_data);
614 DtVirtArray<char *> *DtMail::MailRc::getAliasList()
617 DtVirtArray<char *> *value_list = NULL;
619 value_list = new DtVirtArray<char *>(10);
621 hm_scan((struct hash **)glob.g_alias, nalias, value_list);
626 DtVirtArray<char *> *DtMail::MailRc::getIgnoreList()
629 DtVirtArray<char *> *value_list = NULL;
631 value_list = new DtVirtArray<char *>(10);
633 hm_scan((struct hash **)glob.g_ignore, nignorelist, value_list);
639 DtMail::MailRc::updateByLine(FILE * inf, FILE * outf)
641 // We are going to keep two line buffers, unlike the first
642 // pass parser. One is the line, with continuations, verbatim.
643 // If nothing has changed, then we put this into the output
644 // unmodified. The other is the line with continuations stripped
645 // so we can finish parsing and analyzing the line.
647 char *verbatim = new char[LINESIZE];
648 char *for_parser = new char[LINESIZE];
649 char *linebuf = new char[LINESIZE];
657 if (readline(inf, linebuf) <= 0) {
662 int len = strlen(linebuf);
663 if (len == 0 || linebuf[len - 1] != '\\') {
667 strcat(verbatim, linebuf);
668 strcat(verbatim, "\n");
670 linebuf[len - 1] = ' ';
671 strcat(for_parser, linebuf);
677 strcat(verbatim, linebuf);
678 strcat(verbatim, "\n");
679 strcat(for_parser, linebuf);
680 outputLine(verbatim, for_parser, outf);
684 delete [] for_parser;
689 DtMail::MailRc::outputLine(const char * verbatim,
690 const char * for_parser,
693 char *arglist[MAXARGC];
694 const char * start = for_parser;
695 while(*start && isspace(*start)) {
699 // If we have a comment, write it out and move on.
702 fwrite(verbatim, 1, strlen(verbatim), outf);
708 // Pull off the command word.
711 while (*start && !strchr(" \t0123456789$^.:/-+*'\"", *start))
715 if (equal(word, "")) {
716 fwrite(verbatim, 1, strlen(verbatim), outf);
720 struct cmd * com = (struct cmd *)lex(word);
722 // We dont know the command, so just copy the line.
724 fwrite(verbatim, 1, strlen(verbatim), outf);
728 // We will simply rewrite conditionals.
729 if ((com->c_argtype & F)) {
730 fwrite(verbatim, 1, strlen(verbatim), outf);
735 switch (com->c_argtype & ~(F)) {
738 * Just the straight string, with
739 * leading blanks removed.
741 while (strchr(" \t", *start)) {
744 com->c_write(verbatim, (char **)&start, outf);
749 * A vector of strings, in shell style.
751 if ((c = getrawlist((char *)start, arglist,
752 sizeof arglist / sizeof *arglist)) < 0)
754 if (c < com->c_minargs) {
755 fprintf(stderr,"%s requires at least %d arg(s)\n",
756 com->c_name, com->c_minargs);
759 if (c > com->c_maxargs) {
760 fprintf(stderr,"%s takes no more than %d arg(s)\n",
761 com->c_name, com->c_maxargs);
764 com->c_write(verbatim, arglist, outf);
765 freerawlist(arglist);
770 * Just the constant zero, for exiting,
773 com->c_write(verbatim, NULL, outf);
777 fprintf(stderr,"Unknown argtype %#x", com->c_argtype);
778 fprintf(stderr, "NEED TO FIX THIS FOR DTMAIL!\n");
784 // init the global hash structure
785 void DtMail::MailRc::init_globals()
787 glob.g_myname = NULL;
789 glob.g_ignore = this->hm_alloc();
790 glob.g_retain = this->hm_alloc();
791 glob.g_alias = this->hm_alloc();
792 glob.g_alternates = this->hm_alloc();
795 nullfield = new char[3];
797 strcpy(nullfield, "");
799 alternate_list = NULL;
803 // load a "user definintion" file
805 DtMail::MailRc::load(char *name, char* line)
807 register FILE *in, *oldin;
810 if ((in = fopen(name, "r")) == NULL) {
811 sprintf(line, "can not open file %s\n", name);
818 if ((ret = commands(line)) != 0) {
829 * Interpret user commands one by one. If standard input is not a tty,
834 DtMail::MailRc::commands(char* iline)
837 char *linebuf = new char[LINESIZE];
838 char *line = new char[LINESIZE];
842 * Read a line of commands from the current input
843 * and handle end of file specially.
849 if (readline(input, line) <= 0) {
856 // Conditional if statement with no corresponding endif
867 if ((n = strlen(line)) == 0)
873 if (n > LINESIZE - strlen(linebuf) - 1)
875 strcat(linebuf, line);
877 n = LINESIZE - strlen(linebuf) - 1;
878 if (strlen(line) > n) {
880 "Line plus continuation line too long:\n\t%s\n\nplus\n\t%s\n",
886 // Conditional if statement with no corresponding endif
897 strncat(linebuf, line, n);
898 if (execute(linebuf)) {
899 strncpy(iline, linebuf, LINESIZE);
900 iline[LINESIZE-1] = '\0';
912 * Execute a single command. If the command executed
913 * is "quit," then return non-zero so that the caller
914 * will know to return back to main, if he cares.
915 * Contxt is non-zero if called while composing mail.
918 int DtMail::MailRc::execute(char linebuf[])
921 char *arglist[MAXARGC];
930 * Strip the white space away from the beginning
931 * of the command, then scan out a word, which
932 * consists of anything except digits and white space.
934 * Handle ! escapes differently to get the correct
935 * lexical conventions.
939 while (*cp && strchr(" \t", *cp))
942 * Throw away comment lines here.
948 /* the special newcmd header -- "#-" */
951 /* strip whitespace again */
952 while (*++cp && strchr(" \t", *cp));
955 while (*cp && !strchr(" \t0123456789$^.:/-+*'\"", *cp))
960 * Look up the command; if not found, complain.
961 * We ignore blank lines to eliminate confusion.
967 com = (struct cmd *)lex(word);
970 /* this command is OK not to be found; that way
971 * we can extend the .mailrc file with new
972 * commands and not kill old mail and mailtool
977 fprintf(stderr,"Unknown command: \"%s\"\n", word);
984 * See if we should execute the command -- if a conditional
985 * we always execute it, otherwise, check the state of cond.
988 if ((com->c_argtype & F) == 0) {
989 if (cond == CSEND || cond == CTTY )
995 if (cond == CRCV && !rcvmode || cond == CSEND && rcvmode ||
996 cond == CTTY && !intty || cond == CNOTTY && intty)
1004 * Process the arguments to the command, depending
1005 * on the type he expects. Default to an error.
1006 * If we are sourcing an interactive command, it's
1011 switch (com->c_argtype & ~(F)) {
1014 * Just the straight string, with
1015 * leading blanks removed.
1017 while (strchr(" \t", *cp))
1019 e = (*com->c_func)(&cp, this);
1024 * A vector of strings, in shell style.
1026 if ((c = getrawlist(cp, arglist,
1027 sizeof arglist / sizeof *arglist)) < 0)
1029 if (c < com->c_minargs) {
1030 fprintf(stderr,"%s requires at least %d arg(s)\n",
1031 com->c_name, com->c_minargs);
1034 if (c > com->c_maxargs) {
1035 fprintf(stderr,"%s takes no more than %d arg(s)\n",
1036 com->c_name, com->c_maxargs);
1039 e = (*com->c_func)(arglist, this);
1040 freerawlist(arglist);
1045 * Just the constant zero, for exiting,
1048 e = (*com->c_func)(0, this);
1052 fprintf(stderr,"Unknown argtype %#x", com->c_argtype);
1053 fprintf(stderr, "NEED TO FIX THIS FOR DTMAIL!\n");
1058 * Exit the current source file on
1065 /* ZZZ:katin: should we return an error here? */
1070 * Read up a line from the specified input into the line
1071 * buffer. Return the number of characters read. Do not
1072 * include the newline at the end.
1076 DtMail::MailRc::readline(FILE *ibuf, char *linebuf)
1084 for (cp = linebuf; c != '\n' && c != EOF; c = getc(ibuf)) {
1088 "Mail: ignoring NULL characters in mail\n");
1093 if (cp - linebuf < LINESIZE-2)
1097 if (c == EOF && cp == linebuf)
1099 return(cp - linebuf + 1);
1104 * Pop the current input back to the previous level.
1105 * Update the "sourcing" flag as appropriate.
1108 void DtMail::MailRc::unstack()
1111 fprintf(stderr,"\"Source\" stack over-pop.\n");
1117 fprintf(stderr,"Unmatched \"if\"\n");
1118 cond = sstack[ssp].s_cond;
1119 input = sstack[ssp--].s_file;
1124 * Find the correct command in the command table corresponding
1125 * to the passed command "word"
1128 void *DtMail::MailRc::lex(char word[])
1130 register struct cmd *cp;
1132 for (cp = &cmdtab[0]; cp->c_name != NOSTR; cp++)
1133 if (DtMail::MailRc::isprefix(word, cp->c_name))
1139 * Determine if as1 is a valid prefix of as2.
1140 * Return true if yep.
1143 int DtMail::MailRc::isprefix(char *as1, char *as2)
1145 register char *s1, *s2;
1149 while (*s1++ == *s2)
1152 return(*--s1 == '\0');
1156 * Scan out the list of string arguments, shell style
1160 int DtMail::MailRc::getrawlist(char line[], char **argv, int argc)
1162 register char **ap, *cp, *cp2;
1163 char linebuf[LINESIZE], quotec;
1164 register char **last;
1168 last = argv + argc - 1;
1169 while (*cp != '\0') {
1170 while (*cp && strchr(" \t", *cp))
1174 while (*cp != '\0') {
1176 if (*cp == quotec) {
1182 if (*cp && strchr(" \t", *cp))
1184 if (*cp && strchr("'\"", *cp))
1195 "Too many elements in the list; excess discarded\n");
1198 *ap++ = strdup((char*)linebuf);
1204 void DtMail::MailRc::freerawlist(char **argv)
1214 * Get the value of a variable and return it.
1215 * Look in the environment if its not available locally.
1218 char *DtMail::MailRc::mt_value(char name[])
1220 register struct var *vp;
1222 // extern char *getenv();
1224 if ((vp = lookup(name, (struct var **)this->variables)) == (struct var *)NULL)
1232 * Locate a variable and return its variable
1235 struct var *DtMail::MailRc::lookup(char *name, struct var **hasharray)
1237 register struct var *vp;
1241 for (vp = hasharray[h]; vp != (struct var *)NULL; vp = vp->v_link)
1242 if (strcmp(vp->v_name, name) == 0)
1244 return ((struct var *)NULL);
1249 * Put add users to a group.
1252 int DtMail::MailRc::group(char **argv, DtMail::MailRc *)
1260 * Insert names from the command list into the group.
1261 * Who cares if there are duplicates? They get tossed
1265 /* compute the size of the buffer */
1267 for (ap = argv+1; *ap != NOSTR; ap++) {
1268 size += strlen(*ap) +1;
1270 buf = (char *)malloc(size);
1272 for (ap = argv+1; *ap != NOSTR; ap++) {
1279 MailRc::add_alias((char *)argv[0], (char *)buf);
1285 DtMail::MailRc::wgroup(const char * verbatim, char ** argv, FILE * outf)
1292 for (ap = argv+1; *ap != NOSTR; ap++) {
1293 size += strlen(*ap) +1;
1295 buf = (char *)malloc(size);
1297 for (ap = argv+1; *ap != NOSTR; ap++) {
1304 char * cur = (char *)hm_test((struct hash **)glob.g_alias, argv[0]);
1305 if (!cur || (!clearAliases && hm_ismarked((struct hash **)glob.g_alias,
1307 // Its been removed or written. Dont write it to the file.
1312 if (strcmp(cur, buf) == 0) {
1313 // It hasnt changed. Write the original to preserve spacing.
1315 fwrite(verbatim, 1, strlen(verbatim), outf);
1318 // It has changed. Create a new alias.
1320 fwrite("alias ", 1, 6, outf);
1321 fwrite(argv[0], 1, strlen(argv[0]), outf);
1322 fwrite(" ", 1, 1, outf);
1323 fwrite(cur, 1, strlen(cur), outf);
1324 fwrite("\n", 1, 1, outf);
1327 hm_mark((struct hash **)glob.g_alias, argv[0]);
1331 DtMail::MailRc::ngroup(char * key, void * data, void * client_data)
1333 char * value = (char *)data;
1334 FILE * outf = (FILE *)client_data;
1336 fwrite("alias ", 1, 6, outf);
1337 fwrite(key, 1, strlen(key), outf);
1338 fwrite(" ", 1, 1, outf);
1339 fwrite(value, 1, strlen(value), outf);
1340 fwrite("\n", 1, 1, outf);
1344 DtMail::MailRc::nalias(char * key, void * data, void * client_data)
1346 DtVirtArray<char *> *value_list = (DtVirtArray<char *> *)client_data;
1347 char *new_alias = NULL;
1348 char *white_space = NULL;
1350 int i, num_spaces = 0;
1351 int key_len = strlen(key);
1352 // figure out whitespace for formatting
1353 // assume 13 for normal sized alias name
1355 if(key_len < 13) // need to add spaces
1357 num_spaces = 13 - key_len;
1359 white_space = (char *)malloc(num_spaces + 1);
1360 white_space[0] = '\0';
1362 for(i = 0; i < num_spaces; i++)
1363 white_space[i] = ' ';
1365 white_space[num_spaces] = '\0';
1367 // strcat(white_space, " ");
1369 /* make an alias string */
1370 m_size = key_len + strlen((char *)white_space)
1371 + strlen((char *)data) + strlen(" = ") + 1;
1372 new_alias = (char *)malloc(m_size);
1374 sprintf(new_alias, "%s%s = %s",key, white_space, (char *) data);
1379 /* make an alias string */
1380 m_size = key_len + strlen((char *)data) + strlen(" = ") + 1;
1381 new_alias = (char *)malloc(m_size);
1383 sprintf(new_alias, "%s = %s",key, (char *) data);
1387 value_list->append(new_alias);
1392 DtMail::MailRc::nignorelist(char * key, void * data, void * client_data)
1395 DtVirtArray<char *> *value_list = (DtVirtArray<char *> *)client_data;
1396 char *new_ignore = NULL;
1398 new_ignore = (char *)malloc(strlen((char *)key) + 2);
1400 sprintf(new_ignore, "%s", key);
1402 value_list->append(new_ignore);
1408 * Set or display a variable value. Syntax is similar to that
1412 int DtMail::MailRc::set(char **arglist, DtMail::MailRc *)
1414 register char *cp, *cp2;
1415 char varbuf[LINESIZE], **ap;
1419 for (ap = arglist; *ap != NOSTR; ap++) {
1422 while (*cp != '=' && *cp != '\0')
1429 if (equal(varbuf, "")) {
1430 fprintf(stderr,"Non-null variable name required\n");
1434 MailRc::mt_assign(varbuf, cp);
1440 DtMail::MailRc::wset(const char * verbatim,
1444 char varbuf[LINESIZE];
1445 char *cp, *cp2, **ap;
1447 for (ap = arglist; *ap != NOSTR; ap++) {
1450 while (*cp != '=' && *cp != '\0')
1457 if (equal(varbuf, "")) {
1461 // Lookup the current value, and see if we should rewrite this
1464 struct var * vp = lookup(varbuf, (struct var **)variables);
1465 if (vp == NULL || vp->v_written) {
1466 // If the original input line was set novar then just write
1467 // it out again. We can not easily track duplicates here.
1469 if (varbuf[0] == 'n' && varbuf[1] == 'o') {
1470 fwrite(verbatim, 1, strlen(verbatim), outf);
1473 // This variable has been unassigned or previously written.
1474 // Remove it from the file, by not writing it.
1479 // Compare the values. If equal, then write the line verbatim to
1480 // preserve the users original spacing and quoting.
1482 if (strcmp(cp, vp->v_value) == 0) {
1483 fwrite(verbatim, 1, strlen(verbatim), outf);
1487 // Write the variable, with a new value.
1489 fwrite("set ", 1, 4, outf);
1490 fwrite(varbuf, 1, strlen(varbuf), outf);
1491 if (strlen(vp->v_value) > 0) {
1492 fwrite("='", 1, 2, outf);
1493 fwrite(vp->v_value, 1, strlen(vp->v_value), outf);
1494 fwrite("'", 1, 1, outf);
1496 fwrite("\n", 1, 1, outf);
1503 * Unset a bunch of variable values.
1506 int DtMail::MailRc::unset(char **arglist, DtMail::MailRc *)
1510 for (ap = arglist; *ap != NOSTR; ap++)
1511 (void) MailRc::mt_deassign(*ap);
1516 DtMail::MailRc::wunset(const char * verbatim,
1521 if(verbatim != NULL && outf != NULL)
1522 fwrite(verbatim, 1, strlen(verbatim), outf);
1526 * Hash the passed string and return an index into
1527 * the variable or group hash table.
1529 int DtMail::MailRc::hash(char *name)
1531 register unsigned h;
1534 for (cp = name, h = 0; *cp; h = (h << 2) + *cp++)
1536 return (h % HSHSIZE);
1539 /* manage the alias list */
1540 void DtMail::MailRc::add_alias(char *name, char *value)
1545 /* aliases to the same name get appended to the list */
1546 old = (char*)MailRc::hm_test((struct hash **)glob.g_alias, name);
1551 size = strlen(value) + strlen(old) + 2;
1552 new_ptr = (char *)malloc(size);
1553 sprintf(new_ptr, "%s %s", value, old);
1555 /* delete any old bindings for the name */
1556 MailRc::hm_delete((struct hash**)glob.g_alias, name);
1558 /* add the new binding */
1559 MailRc::hm_add((struct hash**)glob.g_alias, name, new_ptr, size);
1561 /* free up the temporary space */
1564 /* add the new binding */
1565 MailRc::hm_add((struct hash**)glob.g_alias, name, value, strlen(value) +1);
1570 * Assign a value to a mail variable.
1572 void DtMail::MailRc::mt_assign(char *name,char * val)
1576 (void) mt_deassign(name+1);
1577 else if (name[0]=='n' && name[1]=='o')
1578 (void) mt_deassign(name+2);
1579 else if ((val[0] == 'n' || val[0] == 'N') &&
1580 (val[1] == 'o' || val[1] == 'O') && val[2] == '\0') {
1581 (void) mt_deassign(name);
1582 } else mt_puthash(name, vcopy(val), MailRc::variables);
1585 int DtMail::MailRc::mt_deassign(char *s)
1587 register struct var *vp, *vp2;
1590 if ((vp2 = lookup(s, MailRc::variables)) == (struct var *)NULL) {
1594 if (vp2 == MailRc::variables[h]) {
1595 MailRc::variables[h] = MailRc::variables[h]->v_link;
1597 vfree(vp2->v_value);
1601 for (vp = MailRc::variables[h]; vp->v_link != vp2; vp = vp->v_link)
1603 vp->v_link = vp2->v_link;
1605 vfree(vp2->v_value);
1611 * associate val with name in hasharray
1613 void DtMail::MailRc::mt_puthash(char *name, char *val, struct var **hasharray)
1615 register struct var *vp;
1618 vp = lookup(name, hasharray);
1619 if (vp == (struct var *)NULL) {
1621 vp = (struct var *) (calloc(sizeof *vp, 1));
1622 vp->v_name = vcopy(name);
1623 vp->v_link = hasharray[h];
1632 DtMail::MailRc::mt_scan(FILE * outf)
1634 for (int slot = 0; slot < HSHSIZE; slot++) {
1635 for (var * vp = MailRc::variables[slot]; vp != (struct var *)NULL; vp = vp->v_link) {
1636 if (!vp->v_written) {
1637 fwrite("set ", 1, 4, outf);
1638 fwrite(vp->v_name, 1, strlen(vp->v_name), outf);
1639 if (strlen(vp->v_value)) {
1640 fwrite("='", 1, 2, outf);
1641 fwrite(vp->v_value, 1, strlen(vp->v_value), outf);
1642 fwrite("'", 1, 1, outf);
1644 fwrite("\n", 1, 1, outf);
1652 * Copy a variable value into permanent space.
1653 * Do not bother to alloc space for "".
1655 char *DtMail::MailRc::vcopy(char *str)
1658 if (strcmp(str, "") == 0)
1660 return (strdup(str));
1664 * Free up a variable string. We do not bother to allocate
1665 * strings whose value is "" since they are expected to be frequent.
1666 * Thus, we cannot free same!
1668 void DtMail::MailRc::vfree(char *cp)
1671 if (strcmp(cp, "") != 0)
1675 void *DtMail::MailRc::hm_alloc(void)
1677 struct hash **table;
1679 table = (struct hash**)malloc(sizeof (struct hash) * HASHSIZE);
1680 memset(table, '\0', sizeof (struct hash) * HASHSIZE);
1683 void DtMail::MailRc::hm_add(struct hash **table,
1689 register struct hash *h;
1694 index = hash_index(key);
1695 h = (struct hash *)malloc(sizeof (struct hash));
1696 h->h_next = table[index];
1699 h->h_key = strdup(key);
1700 if (size && value != NULL) {
1701 h->h_value = malloc(size);
1702 memcpy(h->h_value, value, size);
1704 h->h_value = nullfield;
1709 void DtMail::MailRc::hm_delete(struct hash **table, char *key)
1712 register struct hash *h;
1713 register struct hash *old;
1718 index = hash_index(key);
1722 if (strcasecmp(h->h_key, key) == 0) {
1723 /* found the match */
1725 table[index] = h->h_next;
1727 old->h_next = h->h_next;
1739 void *DtMail::MailRc::hm_test(struct hash **table, char *key)
1741 register struct hash *h;
1746 h = table[hash_index(key)];
1749 if (strcasecmp(h->h_key, key) == 0) {
1751 return (h->h_value);
1760 void DtMail::MailRc::hm_mark(struct hash **table, char *key)
1762 register struct hash *h;
1767 h = table[hash_index(key)];
1770 if (strcasecmp(h->h_key, key) == 0) {
1779 int DtMail::MailRc::hm_ismarked(struct hash **table, char *key)
1781 register struct hash *h;
1783 if (!table) return 0;
1784 h = table[hash_index(key)];
1787 if (strcasecmp(h->h_key, key) == 0) return(h->h_written);
1795 DtMail::MailRc::hm_scan(struct hash **table, hm_callback callback, void * client_data)
1797 for (int slot = 0; slot < HASHSIZE; slot++) {
1798 for (struct hash * h = table[slot]; h; h = h->h_next) {
1799 if (!h->h_written) {
1800 callback(h->h_key, h->h_value, client_data);
1807 int DtMail::MailRc::hash_index(char *key)
1809 register unsigned h;
1823 return (h % HASHSIZE);
1826 void DtMail::MailRc::free_hash(struct hash *h)
1829 if (h->h_value != nullfield) {
1836 * Add the given header fields to the ignored list.
1837 * If no arguments, print the current list of ignored fields.
1839 int DtMail::MailRc::igfield(char *list[], DtMail::MailRc *)
1843 for (ap = list; *ap != 0; ap++) {
1844 // DP(("igfield: adding %s\n", *ap));
1845 MailRc::add_ignore(*ap);
1851 DtMail::MailRc::wigfield(const char * verbatim, char ** list, FILE * outf)
1854 Boolean ignore_written = FALSE;
1856 // We need to see that every name in this line still exists. If not,
1857 // then we will make a second pass, rewriting the line with only the
1861 for (ap = list; *ap && good; ap++) {
1862 if (!hm_test((struct hash **)glob.g_ignore, *ap)) {
1869 fwrite(verbatim, 1, strlen(verbatim), outf);
1870 for (ap = list; *ap; ap++) {
1871 hm_mark((struct hash **)glob.g_ignore, *ap);
1876 // Create a new ignore line, leaving out the ignores that have
1877 // been removed. Also, dont write any ignores that have been
1878 // previously written.
1880 for (ap = list; *ap; ap++) {
1881 if (hm_test((struct hash **)glob.g_ignore, *ap) &&
1882 !hm_ismarked((struct hash **)glob.g_ignore, *ap)) {
1883 // Only write 'ignore' if there is something in the list
1884 if (!ignore_written) {
1885 fwrite("ignore ", 1, 7, outf);
1886 ignore_written = TRUE;
1888 fwrite(*ap, 1, strlen(*ap), outf);
1889 fwrite(" ", 1, 1, outf);
1890 hm_mark((struct hash **)glob.g_ignore, *ap);
1894 fwrite("\n", 1, 1, outf);
1898 DtMail::MailRc::nigfield(char * key, void *, void * client_data)
1900 DtVirtArray<char *> *value_list = (DtVirtArray<char *> *)client_data;
1902 value_list->append(key);
1905 /* manage the retain/ignore list */
1906 void DtMail::MailRc::add_ignore(char *name)
1908 if(! MailRc::hm_test((struct hash **)glob.g_ignore, name)) {
1909 /* name is not already there... */
1910 MailRc::hm_add((struct hash **)glob.g_ignore, name, NULL, 0);
1916 * Set the list of alternate names.
1918 int DtMail::MailRc::alternates(char **namelist, DtMail::MailRc *)
1920 while (*namelist != NULL)
1921 MailRc::add_alternates(*namelist++);
1926 DtMail::MailRc::walternates(const char * verbatim, char ** list, FILE * outf)
1928 // This is like ignores. We need to make sure all of the alternates
1929 // on this command are still in the database.
1933 for (ap = list; *ap && good; ap++) {
1934 if (!hm_test((struct hash **)glob.g_alternates, *ap)) {
1941 fwrite(verbatim, 1, strlen(verbatim), outf);
1942 for (ap = list; *ap; ap++) {
1943 hm_mark((struct hash **)glob.g_alternates, *ap);
1948 // Write out the alternates that still exist and have not been
1949 // previously written.
1951 Boolean written = FALSE;
1952 for (ap = list; *ap; ap++) {
1953 if (hm_test((struct hash **)glob.g_alternates, *ap) &&
1954 !hm_ismarked((struct hash **)glob.g_alternates, *ap)) {
1956 fwrite("alternates ", 1, 11, outf);
1959 fwrite(*ap, 1, strlen(*ap), outf);
1960 fwrite(" ", 1, 1, outf);
1961 hm_mark((struct hash **)glob.g_alternates, *ap);
1967 DtMail::MailRc::nalternates(char * key, void *, void * client_data)
1969 DtVirtArray<char *> *value_list = (DtVirtArray<char *> *)client_data;
1971 value_list->append(key);
1974 /* manage the alternate name list */
1975 void DtMail::MailRc::add_alternates(char *name)
1977 if(! MailRc::hm_test((struct hash**)glob.g_alternates, name)) {
1978 /* name is not already there... */
1979 MailRc::hm_add((struct hash **)glob.g_alternates, name, NULL, 0);
1984 * Determine the current folder directory name.
1987 DtMail::MailRc::getfolderdir(char *name, size_t buffsize)
1991 if ((folder = mt_value("folder")) == NOSTR)
1994 snprintf(name, buffsize, "%s", folder);
1996 snprintf(name, buffsize, "%s/%s", mt_value("HOME"), folder);
2001 * Take a file name, possibly with shell meta characters
2002 * in it and expand it by using "sh -c echo filename"
2003 * Return the file name as a dynamic string.
2007 DtMail::MailRc::expand(char *name)
2009 char *xname = new char[LINESIZE];
2010 char *cmdbuf = new char[LINESIZE];
2012 register int pid, l;
2013 register char *cp, *Shell;
2016 char *retchr = NULL;
2018 if (name[0] == '+' && getfolderdir(cmdbuf, LINESIZE) >= 0) {
2019 sprintf(xname, "%s/%s", cmdbuf, name + 1);
2020 str = expand(xname);
2025 if (!strpbrk(name, "~{[*?$`'\"\\")) {
2026 return(strdup(name));
2028 if (pipe(pivec) < 0) {
2032 return(strdup(name));
2034 sprintf(cmdbuf, "echo %s", name);
2035 if ((pid = DTMAIL_FORK()) == 0) {
2036 Shell = mt_value("SHELL");
2037 if (Shell == NOSTR || *Shell=='\0')
2044 execlp(Shell, Shell, "-c", cmdbuf, (char *)0);
2056 l = read(pivec[0], xname, LINESIZE);
2058 while (wait(&s) != pid);
2061 if (s != 0 && s != SIGPIPE) {
2062 fprintf(stderr, "Echo failed\n");
2070 fprintf(stderr, "%s: No match\n", name);
2073 if (l == LINESIZE) {
2074 fprintf(stderr, "Buffer overflow expanding %s\n", name);
2078 for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
2081 if (strchr(xname, ' ') && stat(xname, &sbuf) < 0) {
2082 fprintf(stderr, "%s: Ambiguous\n", name);
2086 retchr = strdup(xname);
2099 DtMail::MailRc::source(char **arglist, DtMail::MailRc *self)
2101 char *fname = arglist[0];
2105 /* if any of the three if test failed, return 0,
2106 * since we have not updated the input and stack pointer yet
2108 if ((cp = self->expand(fname)) == NOSTR)
2111 if ((fi = fopen(cp, "r")) == NULL) {
2117 if (self->ssp >= NOFILE - 2) {
2121 self->sstack[++(self->ssp)].s_file = self->input;
2122 self->sstack[self->ssp].s_cond = self->cond;
2131 DtMail::MailRc::wsource(const char * verbatim, char ** arglist, FILE * outf)
2134 if(verbatim != NULL && outf != NULL)
2135 fwrite(verbatim, 1, strlen(verbatim), outf);
2139 DtMail::MailRc::ifcmd(char **arglist, DtMail::MailRc *self)
2143 if (self->cond != CANY) {
2144 fprintf(stderr,"Illegal nested \"if\"\n");
2163 fprintf(stderr,"Unrecognized if-keyword: \"%s\"\n",
2171 DtMail::MailRc::wifcmd(const char * verbatim, char ** arglist, FILE * outf)
2174 if(verbatim != NULL && outf != NULL)
2175 fwrite(verbatim, 1, strlen(verbatim), outf);
2180 DtMail::MailRc::elsecmd(char **, DtMail::MailRc *self)
2183 switch (self->cond) {
2185 fprintf(stderr, "\"Else\" without matching \"if\"\n");
2197 self->cond = CNOTTY;
2205 fprintf(stderr,"invalid condition encountered\n");
2213 DtMail::MailRc::welsecmd(const char * verbatim, char ** arglist, FILE * outf)
2216 if(verbatim != NULL && outf != NULL)
2217 fwrite(verbatim, 1, strlen(verbatim), outf);
2222 DtMail::MailRc::endifcmd(char **, DtMail::MailRc *self)
2225 if (self->cond == CANY) {
2226 fprintf(stderr,"\"Endif\" without matching \"if\"\n");
2234 DtMail::MailRc::wendifcmd(const char * verbatim, char ** arglist, FILE * outf)
2237 if(verbatim != NULL && outf != NULL)
2238 fwrite(verbatim, 1, strlen(verbatim), outf);
2242 DtMail::MailRc::clearaliases(char **, DtMail::MailRc *)
2244 DtVirtArray<char *> *value_list = NULL;
2247 value_list = new DtVirtArray<char *>(10);
2249 hm_scan((struct hash **)glob.g_alias, nalias, value_list);
2251 while (value_list->length()) {
2252 char *buf, *val = (*value_list)[0];
2253 if ((buf = strchr(val, ' ')) != NULL)
2255 if (hm_test((struct hash **)glob.g_alias, val))
2256 hm_delete((struct hash **)glob.g_alias, val);
2257 value_list->remove(0);
2264 DtMail::MailRc::wclearaliases(const char *, char **, FILE *)