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 */
72 struct DtMail::MailRc::globals DtMail::MailRc::glob;
73 char *DtMail::MailRc::nullfield;
74 Boolean DtMail::MailRc::clearAliases;
77 #define DTMAIL_FORK vfork
79 #define DTMAIL_FORK fork
82 #define MAXIMUM_PATH_LENGTH 2048
84 /* can't initialize unions */
86 #define c_minargs c_msgflag /* Minimum argcount for RAWLIST */
87 #define c_maxargs c_msgmask /* Max argcount for RAWLIST */
91 // defines needed by mail-parse
92 #define LINESIZE 10240 /* max readable line width */
93 #define MAXARGC 1024 /* Maximum list of raw strings */
94 #define equal(a, b) (strcmp(a,b)==0)/* A nice function to string compare */
95 #define NONE ((struct cmd *) 0) /* The nil pointer to command tab */
97 #define CANY 0 /* Execute in send or receive mode */
98 #define CRCV 1 /* Execute in receive mode only */
99 #define CSEND 2 /* Execute in send mode only */
100 #define CTTY 3 /* Execute if attached to a tty only */
101 #define CNOTTY 4 /* Execute if not attached to a tty */
103 #define STRLIST 1 /* A pure string */
104 #define RAWLIST 2 /* Shell string list */
105 #define F 01000 /* Is a conditional command */
106 #define NOLIST 3 /* Just plain 0 */
107 #define CANY 0 /* Execute in send or receive mode */
108 #define NOSTR ((char *) 0) /* Null string pointer */
109 // #define NOFILE_DTMAIL 20 /* this define is here for */
110 // /* compatibility purposes only */
111 // /* and will be removed in a */
112 // /* later release */
118 struct var *v_link; /* Forward link to next variable */
119 char *v_name; /* The variable's name */
120 char *v_value; /* And it's current value */
121 int v_written; /* It has been written on this pass. */
131 struct cmd cmdtab[] = {
132 { "unset", DtMail::MailRc::unset, DtMail::MailRc::wunset, RAWLIST, 1, 1000, },
133 { "alias", DtMail::MailRc::group, DtMail::MailRc::wgroup, RAWLIST, 2, 1000, },
134 { "group", DtMail::MailRc::group, DtMail::MailRc::wgroup, RAWLIST, 2, 1000, },
135 { "set", DtMail::MailRc::set, DtMail::MailRc::wset, RAWLIST, 1, 1000,},
136 { "ignore", DtMail::MailRc::igfield, DtMail::MailRc::wigfield, RAWLIST, 1, 1000, },
137 { "source", DtMail::MailRc::source, DtMail::MailRc::wsource, RAWLIST, 1, 1000, },
138 { "if", DtMail::MailRc::ifcmd, DtMail::MailRc::wifcmd, F|RAWLIST, 1, 1, },
139 { "else", DtMail::MailRc::elsecmd, DtMail::MailRc::welsecmd, F|RAWLIST, 0, 0, },
140 { "endif", DtMail::MailRc::endifcmd, DtMail::MailRc::wendifcmd, F|RAWLIST, 0, 0, },
141 { "alternates", DtMail::MailRc::alternates, DtMail::MailRc::walternates, RAWLIST, 1, 1000, },
142 { "clearaliases", DtMail::MailRc::clearaliases, DtMail::MailRc::wclearaliases, RAWLIST, 0, 0, },
147 #define SYSTEM_MAILRC "/etc/mail/mail.rc"
148 #elif defined(_AIX) || defined(__linux__)
149 #define SYSTEM_MAILRC "/usr/share/lib/Mail.rc"
150 #elif defined(__hpux)
151 #define SYSTEM_MAILRC "/usr/share/lib/mailx.rc"
152 #elif defined(CSRG_BASED)
153 #define SYSTEM_MAILRC "/etc/mail.rc"
157 DtMail::MailRc::MailRc(DtMailEnv &env, DtMail::Session *session)
165 _parseError = DTME_NoError;
171 char * mail_rc = getenv("MAILRC");
173 _mailrc_name = strdup(mail_rc);
177 GetPasswordEntry(pw);
179 _mailrc_name = (char *)malloc(MAXIMUM_PATH_LENGTH);
181 strcpy(_mailrc_name, pw.pw_dir);
182 strcat(_mailrc_name, "/.mailrc");
191 if ((rval = this->load(_mailrc_name, line)) != 0) {
192 char *msg = new char[MAXIMUM_PATH_LENGTH + LINESIZE];
194 sprintf(msg, "error while reading \'%s\' in %s\n", line,
197 else if (rval == -2) {
198 sprintf(msg, "%s\n", line);
200 else if (rval == -3) {
201 sprintf(msg, "error: no endif for conditional if statement while reading %s\n", _mailrc_name);
202 env.setError(DTME_ResourceParsingNoEndif, NULL);
203 _parseError = DTME_ResourceParsingNoEndif;
206 env.logError(DTM_FALSE, "%s", msg);
213 if ((rval = this->load(SYSTEM_MAILRC, line)) != 0) {
214 char *msg = new char[MAXIMUM_PATH_LENGTH + LINESIZE];
217 sprintf(msg, "error while reading \'%s\' in %s\n",
218 line, SYSTEM_MAILRC);
219 env.logError(DTM_FALSE, "%s", msg);
220 } else if (rval == -2) {
221 /* Ignore error if the system logfile does not exist. */
228 DtMail::MailRc::~MailRc(void)
233 // return true if in list
234 DtMailBoolean DtMail::MailRc::ignore(DtMailEnv &env, const char *name)
237 char *ignore_list = (char *)MailRc::hm_test((struct hash **)glob.g_ignore, (char *)name);
241 if(ignore_list != NULL) // we have a valid ignore list
247 env.setError(DTME_NoObjectValue, NULL);
254 DtMail::MailRc::addIgnore(DtMailEnv & error, const char * name)
257 add_ignore((char *)name);
261 DtMail::MailRc::removeIgnore(DtMailEnv & error, const char * name)
264 if(MailRc::hm_test((struct hash **)glob.g_ignore, (char *)name)) {
265 MailRc::hm_delete((struct hash **)glob.g_ignore, (char *)name);
269 const char *DtMail::MailRc::getAlias(DtMailEnv &env, const char * name)
272 char *return_value = (char *)hm_test((struct hash **)glob.g_alias, (char *)name);
276 if(return_value == NULL)
278 env.setError(DTME_NoObjectValue, NULL);
289 DtMail::MailRc::setAlias(DtMailEnv & error,
295 if (hm_test((struct hash **)glob.g_alias, (char *)name)) {
296 hm_delete((struct hash **)glob.g_alias, (char *)name);
299 add_alias((char *)name, (char *)value);
303 DtMail::MailRc::removeAlias(DtMailEnv & error,
308 if (hm_test((struct hash **)glob.g_alias, (char *)name)) {
309 hm_delete((struct hash **)glob.g_alias, (char *)name);
314 // Adapted from uuencode and uudecode written by Ian Lance Taylor.
316 #define ENCRYPT(c) ((c) ? (((c)&077) + ' ' + 2) : ('`' + 2))
317 #define DECRYPT(c) ((((c) - ' ') & 077) - 2)
320 DtMail::MailRc::encryptedLength(int length)
322 // One for the length, one for the '\0', expansion factor of 4/3.
323 int encrypted_length = (2 + (4 * ((length+2)/3)));
324 return encrypted_length;
328 DtMail::MailRc::encryptValue(char *to, char *from, int buflen)
334 memset(to, 0, buflen);
336 length = strlen(from);
337 to[i++] = ENCRYPT(length);
339 for (p=from; length>0; length-=3, p+=3)
345 ch = ((*p << 4) & 060) | ((p[1] >> 4) & 017);
349 ch = ((p[1] << 2) & 074) | ((p[2] >> 6) & 03);
360 DtMail::MailRc::decryptValue(char *to, char *from, int buflen)
366 memset(to, 0, buflen);
369 length = DECRYPT(*p);
370 if (length<=0) return 1;
372 for (++p; length>0; p+=4, length-=3)
376 ch = DECRYPT(p[0]) << 2 | DECRYPT(p[1]) >> 4;
378 ch = DECRYPT(p[1]) << 4 | DECRYPT(p[2]) >> 2;
380 ch = DECRYPT(p[2]) << 6 | DECRYPT(p[3]);
387 ch = DECRYPT(p[0]) << 2 | DECRYPT(p[1]) >> 4;
392 ch = DECRYPT(p[1]) << 4 | DECRYPT(p[2]) >> 2;
400 // if set with value return no error in env and set value
401 // if set with no value return no error in env and return empty string
402 // if not set, return error in env and leave value alone
404 void DtMail::MailRc::getValue(DtMailEnv &env,
407 DtMailBoolean decrypt)
409 char *get_result = mt_value((char *)var);
414 if (get_result != NULL)
418 int length = encryptedLength(strlen(get_result));
419 *value = (char*) malloc(length);
420 if (decryptValue((char*) *value, get_result, length))
421 strcpy((char*) *value, get_result);
424 *value = strdup((char *)get_result);
427 env.setError(DTME_NoObjectValue, NULL);
431 DtMail::MailRc::setValue(DtMailEnv & error,
434 DtMailBoolean encrypt)
438 if (encrypt && strlen(value))
440 int length = encryptedLength(strlen(value));
441 char *encrypted = (char*) malloc(length);
442 encryptValue(encrypted, (char*) value, length);
443 mt_assign((char*) var, (char*) encrypted);
446 mt_assign((char*) var, (char*) value);
450 DtMail::MailRc::removeValue(DtMailEnv & error,
455 mt_deassign((char *)var);
458 // return alternates list
459 const char * DtMail::MailRc::getAlternates(DtMailEnv &env)
462 register struct hash *h;
463 struct hash **table = (struct hash **)glob.g_alternates;
468 // we don't free this memory...
469 alternate_list = NULL;
473 env.setError(DTME_NoObjectValue, NULL);
482 len = strlen((const char*)h->h_key);
484 if(alternate_list == NULL)
486 alternate_list = (char *)malloc(len + 1); // plus terminator
487 strcpy(alternate_list, (const char*)h->h_key);
491 len += strlen(alternate_list);
492 alternate_list = (char *)realloc(alternate_list,
493 len + 2); // plus terminator
495 strcat(alternate_list, " "); // add space
496 strcat(alternate_list, (const char *)h->h_key);
505 return (alternate_list);
510 DtMail::MailRc::setAlternate(DtMailEnv & error, const char * alt)
514 add_alternates((char *)alt);
518 DtMail::MailRc::removeAlternate(DtMailEnv & error, const char * name)
522 if(MailRc::hm_test((struct hash**)glob.g_alternates, (char *)name)) {
523 MailRc::hm_delete((struct hash **)glob.g_alternates, (char *)name);
528 DtMail::MailRc::update(DtMailEnv & error)
532 // Generate a temporary file.
533 char *tmp_mailrc = new char[MAXIMUM_PATH_LENGTH];
534 strcpy(tmp_mailrc, _mailrc_name);
535 strcat(tmp_mailrc, ".tmp");
537 FILE * outf = fopen(tmp_mailrc, "w+");
539 error.setError(DTME_ObjectCreationFailed);
540 delete [] tmp_mailrc;
544 // Now open the mailrc for input.
545 FILE * inf = fopen(_mailrc_name, "r");
547 // Now we will read the input file, and copy it to the output,
548 // based on type and changes to the mailrc.
550 updateByLine(inf, outf);
555 // Now we need to make a final scan. This will cause new values
556 // to be written and the written flag to be reset for the next
561 hm_scan((struct hash **)glob.g_alias, ngroup, outf);
563 // Alternates and groups we will add to a list, and then
564 // put them out in one batch.
566 DtVirtArray<char *> value_list(32);
568 hm_scan((struct hash **)glob.g_ignore, nigfield, &value_list);
570 if (value_list.length()) {
571 fwrite("ignore ", 1, 7, outf);
572 while (value_list.length()) {
573 char * val = value_list[0];
574 fwrite(val, 1, strlen(val), outf);
575 fwrite(" ", 1, 1, outf);
576 value_list.remove(0);
578 fwrite("\n", 1, 1, outf);
581 hm_scan((struct hash **)glob.g_alternates, nalternates, &value_list);
583 if (value_list.length()) {
584 fwrite("alternates ", 1, 11, outf);
585 while (value_list.length()) {
586 char * val = value_list[0];
587 fwrite(val, 1, strlen(val), outf);
588 fwrite(" ", 1, 1, outf);
589 value_list.remove(0);
591 fwrite("\n", 1, 1, outf);
595 if (rename(tmp_mailrc, _mailrc_name)) {
596 error.setError(DTME_ObjectAccessFailed);
597 delete [] tmp_mailrc;
600 delete [] tmp_mailrc;
603 void DtMail::MailRc::getAliasList(hm_callback stuffing_func, void *client_data)
605 hm_scan((struct hash **)glob.g_alias, stuffing_func, client_data);
609 DtVirtArray<char *> *DtMail::MailRc::getAliasList()
612 DtVirtArray<char *> *value_list = NULL;
614 value_list = new DtVirtArray<char *>(10);
616 hm_scan((struct hash **)glob.g_alias, nalias, value_list);
621 DtVirtArray<char *> *DtMail::MailRc::getIgnoreList()
624 DtVirtArray<char *> *value_list = NULL;
626 value_list = new DtVirtArray<char *>(10);
628 hm_scan((struct hash **)glob.g_ignore, nignorelist, value_list);
634 DtMail::MailRc::updateByLine(FILE * inf, FILE * outf)
636 // We are going to keep two line buffers, unlike the first
637 // pass parser. One is the line, with continuations, verbatim.
638 // If nothing has changed, then we put this into the output
639 // unmodified. The other is the line with continuations stripped
640 // so we can finish parsing and analyzing the line.
642 char *verbatim = new char[LINESIZE];
643 char *for_parser = new char[LINESIZE];
644 char *linebuf = new char[LINESIZE];
652 if (readline(inf, linebuf) <= 0) {
657 int len = strlen(linebuf);
658 if (len == 0 || linebuf[len - 1] != '\\') {
662 strcat(verbatim, linebuf);
663 strcat(verbatim, "\n");
665 linebuf[len - 1] = ' ';
666 strcat(for_parser, linebuf);
672 strcat(verbatim, linebuf);
673 strcat(verbatim, "\n");
674 strcat(for_parser, linebuf);
675 outputLine(verbatim, for_parser, outf);
679 delete [] for_parser;
684 DtMail::MailRc::outputLine(const char * verbatim,
685 const char * for_parser,
688 char *arglist[MAXARGC];
689 const char * start = for_parser;
690 while(*start && isspace(*start)) {
694 // If we have a comment, write it out and move on.
697 fwrite(verbatim, 1, strlen(verbatim), outf);
703 // Pull off the command word.
706 while (*start && !strchr(" \t0123456789$^.:/-+*'\"", *start))
710 if (equal(word, "")) {
711 fwrite(verbatim, 1, strlen(verbatim), outf);
715 struct cmd * com = (struct cmd *)lex(word);
717 // We don't know the command, so just copy the line.
719 fwrite(verbatim, 1, strlen(verbatim), outf);
723 // We will simply rewrite conditionals.
724 if ((com->c_argtype & F)) {
725 fwrite(verbatim, 1, strlen(verbatim), outf);
730 switch (com->c_argtype & ~(F)) {
733 * Just the straight string, with
734 * leading blanks removed.
736 while (strchr(" \t", *start)) {
739 com->c_write(verbatim, (char **)&start, outf);
744 * A vector of strings, in shell style.
746 if ((c = getrawlist((char *)start, arglist,
747 sizeof arglist / sizeof *arglist)) < 0)
749 if (c < com->c_minargs) {
750 fprintf(stderr,"%s requires at least %d arg(s)\n",
751 com->c_name, com->c_minargs);
754 if (c > com->c_maxargs) {
755 fprintf(stderr,"%s takes no more than %d arg(s)\n",
756 com->c_name, com->c_maxargs);
759 com->c_write(verbatim, arglist, outf);
760 freerawlist(arglist);
765 * Just the constant zero, for exiting,
768 com->c_write(verbatim, NULL, outf);
772 fprintf(stderr,"Unknown argtype %#x", com->c_argtype);
773 fprintf(stderr, "NEED TO FIX THIS FOR DTMAIL!\n");
779 // init the global hash structure
780 void DtMail::MailRc::init_globals()
782 glob.g_myname = NULL;
784 glob.g_ignore = this->hm_alloc();
785 glob.g_retain = this->hm_alloc();
786 glob.g_alias = this->hm_alloc();
787 glob.g_alternates = this->hm_alloc();
790 nullfield = new char[3];
792 strcpy(nullfield, "");
794 alternate_list = NULL;
798 // load a "user definintion" file
800 DtMail::MailRc::load(char *name, char* line)
802 register FILE *in, *oldin;
805 if ((in = fopen(name, "r")) == NULL) {
806 sprintf(line, "can not open file %s\n", name);
813 if ((ret = commands(line)) != 0) {
824 * Interpret user commands one by one. If standard input is not a tty,
829 DtMail::MailRc::commands(char* iline)
832 char *linebuf = new char[LINESIZE];
833 char *line = new char[LINESIZE];
837 * Read a line of commands from the current input
838 * and handle end of file specially.
844 if (readline(input, line) <= 0) {
851 // Conditional if statement with no corresponding endif
862 if ((n = strlen(line)) == 0)
868 if (n > LINESIZE - strlen(linebuf) - 1)
870 strcat(linebuf, line);
872 n = LINESIZE - strlen(linebuf) - 1;
873 if (strlen(line) > n) {
875 "Line plus continuation line too long:\n\t%s\n\nplus\n\t%s\n",
881 // Conditional if statement with no corresponding endif
892 strncat(linebuf, line, n);
893 if (execute(linebuf)) {
894 strncpy(iline, linebuf, LINESIZE);
895 iline[LINESIZE-1] = '\0';
907 * Execute a single command. If the command executed
908 * is "quit," then return non-zero so that the caller
909 * will know to return back to main, if he cares.
910 * Contxt is non-zero if called while composing mail.
913 int DtMail::MailRc::execute(char linebuf[])
916 char *arglist[MAXARGC];
925 * Strip the white space away from the beginning
926 * of the command, then scan out a word, which
927 * consists of anything except digits and white space.
929 * Handle ! escapes differently to get the correct
930 * lexical conventions.
934 while (*cp && strchr(" \t", *cp))
937 * Throw away comment lines here.
943 /* the special newcmd header -- "#-" */
946 /* strip whitespace again */
947 while (*++cp && strchr(" \t", *cp));
950 while (*cp && !strchr(" \t0123456789$^.:/-+*'\"", *cp))
955 * Look up the command; if not found, complain.
956 * We ignore blank lines to eliminate confusion.
962 com = (struct cmd *)lex(word);
965 /* this command is OK not to be found; that way
966 * we can extend the .mailrc file with new
967 * commands and not kill old mail and mailtool
972 fprintf(stderr,"Unknown command: \"%s\"\n", word);
979 * See if we should execute the command -- if a conditional
980 * we always execute it, otherwise, check the state of cond.
983 if ((com->c_argtype & F) == 0) {
984 if (cond == CSEND || cond == CTTY )
990 if (cond == CRCV && !rcvmode || cond == CSEND && rcvmode ||
991 cond == CTTY && !intty || cond == CNOTTY && intty)
999 * Process the arguments to the command, depending
1000 * on the type he expects. Default to an error.
1001 * If we are sourcing an interactive command, it's
1006 switch (com->c_argtype & ~(F)) {
1009 * Just the straight string, with
1010 * leading blanks removed.
1012 while (strchr(" \t", *cp))
1014 e = (*com->c_func)(&cp, this);
1019 * A vector of strings, in shell style.
1021 if ((c = getrawlist(cp, arglist,
1022 sizeof arglist / sizeof *arglist)) < 0)
1024 if (c < com->c_minargs) {
1025 fprintf(stderr,"%s requires at least %d arg(s)\n",
1026 com->c_name, com->c_minargs);
1029 if (c > com->c_maxargs) {
1030 fprintf(stderr,"%s takes no more than %d arg(s)\n",
1031 com->c_name, com->c_maxargs);
1034 e = (*com->c_func)(arglist, this);
1035 freerawlist(arglist);
1040 * Just the constant zero, for exiting,
1043 e = (*com->c_func)(0, this);
1047 fprintf(stderr,"Unknown argtype %#x", com->c_argtype);
1048 fprintf(stderr, "NEED TO FIX THIS FOR DTMAIL!\n");
1053 * Exit the current source file on
1060 /* ZZZ:katin: should we return an error here? */
1065 * Read up a line from the specified input into the line
1066 * buffer. Return the number of characters read. Do not
1067 * include the newline at the end.
1071 DtMail::MailRc::readline(FILE *ibuf, char *linebuf)
1079 for (cp = linebuf; c != '\n' && c != EOF; c = getc(ibuf)) {
1083 "Mail: ignoring NULL characters in mail\n");
1088 if (cp - linebuf < LINESIZE-2)
1092 if (c == EOF && cp == linebuf)
1094 return(cp - linebuf + 1);
1099 * Pop the current input back to the previous level.
1100 * Update the "sourcing" flag as appropriate.
1103 void DtMail::MailRc::unstack()
1106 fprintf(stderr,"\"Source\" stack over-pop.\n");
1112 fprintf(stderr,"Unmatched \"if\"\n");
1113 cond = sstack[ssp].s_cond;
1114 input = sstack[ssp--].s_file;
1119 * Find the correct command in the command table corresponding
1120 * to the passed command "word"
1123 void *DtMail::MailRc::lex(char word[])
1125 register struct cmd *cp;
1127 for (cp = &cmdtab[0]; cp->c_name != NOSTR; cp++)
1128 if (DtMail::MailRc::isprefix(word, cp->c_name))
1134 * Determine if as1 is a valid prefix of as2.
1135 * Return true if yep.
1138 int DtMail::MailRc::isprefix(char *as1, char *as2)
1140 register char *s1, *s2;
1144 while (*s1++ == *s2)
1147 return(*--s1 == '\0');
1151 * Scan out the list of string arguments, shell style
1155 int DtMail::MailRc::getrawlist(char line[], char **argv, int argc)
1157 register char **ap, *cp, *cp2;
1158 char linebuf[LINESIZE], quotec;
1159 register char **last;
1163 last = argv + argc - 1;
1164 while (*cp != '\0') {
1165 while (*cp && strchr(" \t", *cp))
1169 while (*cp != '\0') {
1171 if (*cp == quotec) {
1177 if (*cp && strchr(" \t", *cp))
1179 if (*cp && strchr("'\"", *cp))
1190 "Too many elements in the list; excess discarded\n");
1193 *ap++ = strdup((char*)linebuf);
1199 void DtMail::MailRc::freerawlist(char **argv)
1209 * Get the value of a variable and return it.
1210 * Look in the environment if its not available locally.
1213 char *DtMail::MailRc::mt_value(char name[])
1215 register struct var *vp;
1217 // extern char *getenv();
1219 if ((vp = lookup(name, (struct var **)this->variables)) == (struct var *)NULL)
1227 * Locate a variable and return its variable
1230 struct var *DtMail::MailRc::lookup(char *name, struct var **hasharray)
1232 register struct var *vp;
1236 for (vp = hasharray[h]; vp != (struct var *)NULL; vp = vp->v_link)
1237 if (strcmp(vp->v_name, name) == 0)
1239 return ((struct var *)NULL);
1244 * Put add users to a group.
1247 int DtMail::MailRc::group(char **argv, DtMail::MailRc *)
1255 * Insert names from the command list into the group.
1256 * Who cares if there are duplicates? They get tossed
1260 /* compute the size of the buffer */
1262 for (ap = argv+1; *ap != NOSTR; ap++) {
1263 size += strlen(*ap) +1;
1265 buf = (char *)malloc(size);
1267 for (ap = argv+1; *ap != NOSTR; ap++) {
1274 MailRc::add_alias((char *)argv[0], (char *)buf);
1280 DtMail::MailRc::wgroup(const char * verbatim, char ** argv, FILE * outf)
1287 for (ap = argv+1; *ap != NOSTR; ap++) {
1288 size += strlen(*ap) +1;
1290 buf = (char *)malloc(size);
1292 for (ap = argv+1; *ap != NOSTR; ap++) {
1299 char * cur = (char *)hm_test((struct hash **)glob.g_alias, argv[0]);
1300 if (!cur || (!clearAliases && hm_ismarked((struct hash **)glob.g_alias,
1302 // Its been removed or written. Don't write it to the file.
1307 if (strcmp(cur, buf) == 0) {
1308 // It hasnt changed. Write the original to preserve spacing.
1310 fwrite(verbatim, 1, strlen(verbatim), outf);
1313 // It has changed. Create a new alias.
1315 fwrite("alias ", 1, 6, outf);
1316 fwrite(argv[0], 1, strlen(argv[0]), outf);
1317 fwrite(" ", 1, 1, outf);
1318 fwrite(cur, 1, strlen(cur), outf);
1319 fwrite("\n", 1, 1, outf);
1322 hm_mark((struct hash **)glob.g_alias, argv[0]);
1326 DtMail::MailRc::ngroup(char * key, void * data, void * client_data)
1328 char * value = (char *)data;
1329 FILE * outf = (FILE *)client_data;
1331 fwrite("alias ", 1, 6, outf);
1332 fwrite(key, 1, strlen(key), outf);
1333 fwrite(" ", 1, 1, outf);
1334 fwrite(value, 1, strlen(value), outf);
1335 fwrite("\n", 1, 1, outf);
1339 DtMail::MailRc::nalias(char * key, void * data, void * client_data)
1341 DtVirtArray<char *> *value_list = (DtVirtArray<char *> *)client_data;
1342 char *new_alias = NULL;
1343 char *white_space = NULL;
1345 int i, num_spaces = 0;
1346 int key_len = strlen(key);
1347 // figure out whitespace for formatting
1348 // assume 13 for normal sized alias name
1350 if(key_len < 13) // need to add spaces
1352 num_spaces = 13 - key_len;
1354 white_space = (char *)malloc(num_spaces + 1);
1355 white_space[0] = '\0';
1357 for(i = 0; i < num_spaces; i++)
1358 white_space[i] = ' ';
1360 white_space[num_spaces] = '\0';
1362 // strcat(white_space, " ");
1364 /* make an alias string */
1365 m_size = key_len + strlen((char *)white_space)
1366 + strlen((char *)data) + strlen(" = ") + 1;
1367 new_alias = (char *)malloc(m_size);
1369 sprintf(new_alias, "%s%s = %s",key, white_space, (char *) data);
1374 /* make an alias string */
1375 m_size = key_len + strlen((char *)data) + strlen(" = ") + 1;
1376 new_alias = (char *)malloc(m_size);
1378 sprintf(new_alias, "%s = %s",key, (char *) data);
1382 value_list->append(new_alias);
1387 DtMail::MailRc::nignorelist(char * key, void * data, void * client_data)
1390 DtVirtArray<char *> *value_list = (DtVirtArray<char *> *)client_data;
1391 char *new_ignore = NULL;
1393 new_ignore = (char *)malloc(strlen((char *)key) + 2);
1395 sprintf(new_ignore, "%s", key);
1397 value_list->append(new_ignore);
1403 * Set or display a variable value. Syntax is similar to that
1407 int DtMail::MailRc::set(char **arglist, DtMail::MailRc *)
1409 register char *cp, *cp2;
1410 char varbuf[LINESIZE], **ap;
1414 for (ap = arglist; *ap != NOSTR; ap++) {
1417 while (*cp != '=' && *cp != '\0')
1424 if (equal(varbuf, "")) {
1425 fprintf(stderr,"Non-null variable name required\n");
1429 MailRc::mt_assign(varbuf, cp);
1435 DtMail::MailRc::wset(const char * verbatim,
1439 char varbuf[LINESIZE];
1440 char *cp, *cp2, **ap;
1442 for (ap = arglist; *ap != NOSTR; ap++) {
1445 while (*cp != '=' && *cp != '\0')
1452 if (equal(varbuf, "")) {
1456 // Lookup the current value, and see if we should rewrite this
1459 struct var * vp = lookup(varbuf, (struct var **)variables);
1460 if (vp == NULL || vp->v_written) {
1461 // If the original input line was set novar then just write
1462 // it out again. We can not easily track duplicates here.
1464 if (varbuf[0] == 'n' && varbuf[1] == 'o') {
1465 fwrite(verbatim, 1, strlen(verbatim), outf);
1468 // This variable has been unassigned or previously written.
1469 // Remove it from the file, by not writing it.
1474 // Compare the values. If equal, then write the line verbatim to
1475 // preserve the users original spacing and quoting.
1477 if (strcmp(cp, vp->v_value) == 0) {
1478 fwrite(verbatim, 1, strlen(verbatim), outf);
1482 // Write the variable, with a new value.
1484 fwrite("set ", 1, 4, outf);
1485 fwrite(varbuf, 1, strlen(varbuf), outf);
1486 if (strlen(vp->v_value) > 0) {
1487 fwrite("='", 1, 2, outf);
1488 fwrite(vp->v_value, 1, strlen(vp->v_value), outf);
1489 fwrite("'", 1, 1, outf);
1491 fwrite("\n", 1, 1, outf);
1498 * Unset a bunch of variable values.
1501 int DtMail::MailRc::unset(char **arglist, DtMail::MailRc *)
1505 for (ap = arglist; *ap != NOSTR; ap++)
1506 (void) MailRc::mt_deassign(*ap);
1511 DtMail::MailRc::wunset(const char * verbatim,
1516 if(verbatim != NULL && outf != NULL)
1517 fwrite(verbatim, 1, strlen(verbatim), outf);
1521 * Hash the passed string and return an index into
1522 * the variable or group hash table.
1524 int DtMail::MailRc::hash(char *name)
1526 register unsigned h;
1529 for (cp = name, h = 0; *cp; h = (h << 2) + *cp++)
1531 return (h % HSHSIZE);
1534 /* manage the alias list */
1535 void DtMail::MailRc::add_alias(char *name, char *value)
1540 /* aliases to the same name get appended to the list */
1541 old = (char*)MailRc::hm_test((struct hash **)glob.g_alias, name);
1546 size = strlen(value) + strlen(old) + 2;
1547 new_ptr = (char *)malloc(size);
1548 sprintf(new_ptr, "%s %s", value, old);
1550 /* delete any old bindings for the name */
1551 MailRc::hm_delete((struct hash**)glob.g_alias, name);
1553 /* add the new binding */
1554 MailRc::hm_add((struct hash**)glob.g_alias, name, new_ptr, size);
1556 /* free up the temporary space */
1559 /* add the new binding */
1560 MailRc::hm_add((struct hash**)glob.g_alias, name, value, strlen(value) +1);
1565 * Assign a value to a mail variable.
1567 void DtMail::MailRc::mt_assign(char *name,char * val)
1571 (void) mt_deassign(name+1);
1572 else if (name[0]=='n' && name[1]=='o')
1573 (void) mt_deassign(name+2);
1574 else if ((val[0] == 'n' || val[0] == 'N') &&
1575 (val[1] == 'o' || val[1] == 'O') && val[2] == '\0') {
1576 (void) mt_deassign(name);
1577 } else mt_puthash(name, vcopy(val), MailRc::variables);
1580 int DtMail::MailRc::mt_deassign(char *s)
1582 register struct var *vp, *vp2;
1585 if ((vp2 = lookup(s, MailRc::variables)) == (struct var *)NULL) {
1589 if (vp2 == MailRc::variables[h]) {
1590 MailRc::variables[h] = MailRc::variables[h]->v_link;
1592 vfree(vp2->v_value);
1596 for (vp = MailRc::variables[h]; vp->v_link != vp2; vp = vp->v_link)
1598 vp->v_link = vp2->v_link;
1600 vfree(vp2->v_value);
1606 * associate val with name in hasharray
1608 void DtMail::MailRc::mt_puthash(char *name, char *val, struct var **hasharray)
1610 register struct var *vp;
1613 vp = lookup(name, hasharray);
1614 if (vp == (struct var *)NULL) {
1616 vp = (struct var *) (calloc(sizeof *vp, 1));
1617 vp->v_name = vcopy(name);
1618 vp->v_link = hasharray[h];
1627 DtMail::MailRc::mt_scan(FILE * outf)
1629 for (int slot = 0; slot < HSHSIZE; slot++) {
1630 for (var * vp = MailRc::variables[slot]; vp != (struct var *)NULL; vp = vp->v_link) {
1631 if (!vp->v_written) {
1632 fwrite("set ", 1, 4, outf);
1633 fwrite(vp->v_name, 1, strlen(vp->v_name), outf);
1634 if (strlen(vp->v_value)) {
1635 fwrite("='", 1, 2, outf);
1636 fwrite(vp->v_value, 1, strlen(vp->v_value), outf);
1637 fwrite("'", 1, 1, outf);
1639 fwrite("\n", 1, 1, outf);
1647 * Copy a variable value into permanent space.
1648 * Do not bother to alloc space for "".
1650 char *DtMail::MailRc::vcopy(char *str)
1653 if (strcmp(str, "") == 0)
1655 return (strdup(str));
1659 * Free up a variable string. We do not bother to allocate
1660 * strings whose value is "" since they are expected to be frequent.
1661 * Thus, we cannot free same!
1663 void DtMail::MailRc::vfree(char *cp)
1666 if (strcmp(cp, "") != 0)
1670 void *DtMail::MailRc::hm_alloc(void)
1672 struct hash **table;
1674 table = (struct hash**)malloc(sizeof (struct hash) * HASHSIZE);
1675 memset(table, '\0', sizeof (struct hash) * HASHSIZE);
1678 void DtMail::MailRc::hm_add(struct hash **table,
1684 register struct hash *h;
1689 index = hash_index(key);
1690 h = (struct hash *)malloc(sizeof (struct hash));
1691 h->h_next = table[index];
1694 h->h_key = strdup(key);
1695 if (size && value != NULL) {
1696 h->h_value = malloc(size);
1697 memcpy(h->h_value, value, size);
1699 h->h_value = nullfield;
1704 void DtMail::MailRc::hm_delete(struct hash **table, char *key)
1707 register struct hash *h;
1708 register struct hash *old;
1713 index = hash_index(key);
1717 if (strcasecmp(h->h_key, key) == 0) {
1718 /* found the match */
1720 table[index] = h->h_next;
1722 old->h_next = h->h_next;
1734 void *DtMail::MailRc::hm_test(struct hash **table, char *key)
1736 register struct hash *h;
1741 h = table[hash_index(key)];
1744 if (strcasecmp(h->h_key, key) == 0) {
1746 return (h->h_value);
1755 void DtMail::MailRc::hm_mark(struct hash **table, char *key)
1757 register struct hash *h;
1762 h = table[hash_index(key)];
1765 if (strcasecmp(h->h_key, key) == 0) {
1774 int DtMail::MailRc::hm_ismarked(struct hash **table, char *key)
1776 register struct hash *h;
1778 if (!table) return 0;
1779 h = table[hash_index(key)];
1782 if (strcasecmp(h->h_key, key) == 0) return(h->h_written);
1790 DtMail::MailRc::hm_scan(struct hash **table, hm_callback callback, void * client_data)
1792 for (int slot = 0; slot < HASHSIZE; slot++) {
1793 for (struct hash * h = table[slot]; h; h = h->h_next) {
1794 if (!h->h_written) {
1795 callback(h->h_key, h->h_value, client_data);
1802 int DtMail::MailRc::hash_index(char *key)
1804 register unsigned h;
1818 return (h % HASHSIZE);
1821 void DtMail::MailRc::free_hash(struct hash *h)
1824 if (h->h_value != nullfield) {
1831 * Add the given header fields to the ignored list.
1832 * If no arguments, print the current list of ignored fields.
1834 int DtMail::MailRc::igfield(char *list[], DtMail::MailRc *)
1838 for (ap = list; *ap != 0; ap++) {
1839 // DP(("igfield: adding %s\n", *ap));
1840 MailRc::add_ignore(*ap);
1846 DtMail::MailRc::wigfield(const char * verbatim, char ** list, FILE * outf)
1849 Boolean ignore_written = FALSE;
1851 // We need to see that every name in this line still exists. If not,
1852 // then we will make a second pass, rewriting the line with only the
1856 for (ap = list; *ap && good; ap++) {
1857 if (!hm_test((struct hash **)glob.g_ignore, *ap)) {
1864 fwrite(verbatim, 1, strlen(verbatim), outf);
1865 for (ap = list; *ap; ap++) {
1866 hm_mark((struct hash **)glob.g_ignore, *ap);
1871 // Create a new ignore line, leaving out the ignores that have
1872 // been removed. Also, don't write any ignores that have been
1873 // previously written.
1875 for (ap = list; *ap; ap++) {
1876 if (hm_test((struct hash **)glob.g_ignore, *ap) &&
1877 !hm_ismarked((struct hash **)glob.g_ignore, *ap)) {
1878 // Only write 'ignore' if there is something in the list
1879 if (!ignore_written) {
1880 fwrite("ignore ", 1, 7, outf);
1881 ignore_written = TRUE;
1883 fwrite(*ap, 1, strlen(*ap), outf);
1884 fwrite(" ", 1, 1, outf);
1885 hm_mark((struct hash **)glob.g_ignore, *ap);
1889 fwrite("\n", 1, 1, outf);
1893 DtMail::MailRc::nigfield(char * key, void *, void * client_data)
1895 DtVirtArray<char *> *value_list = (DtVirtArray<char *> *)client_data;
1897 value_list->append(key);
1900 /* manage the retain/ignore list */
1901 void DtMail::MailRc::add_ignore(char *name)
1903 if(! MailRc::hm_test((struct hash **)glob.g_ignore, name)) {
1904 /* name is not already there... */
1905 MailRc::hm_add((struct hash **)glob.g_ignore, name, NULL, 0);
1911 * Set the list of alternate names.
1913 int DtMail::MailRc::alternates(char **namelist, DtMail::MailRc *)
1915 while (*namelist != NULL)
1916 MailRc::add_alternates(*namelist++);
1921 DtMail::MailRc::walternates(const char * verbatim, char ** list, FILE * outf)
1923 // This is like ignores. We need to make sure all of the alternates
1924 // on this command are still in the database.
1928 for (ap = list; *ap && good; ap++) {
1929 if (!hm_test((struct hash **)glob.g_alternates, *ap)) {
1936 fwrite(verbatim, 1, strlen(verbatim), outf);
1937 for (ap = list; *ap; ap++) {
1938 hm_mark((struct hash **)glob.g_alternates, *ap);
1943 // Write out the alternates that still exist and have not been
1944 // previously written.
1946 Boolean written = FALSE;
1947 for (ap = list; *ap; ap++) {
1948 if (hm_test((struct hash **)glob.g_alternates, *ap) &&
1949 !hm_ismarked((struct hash **)glob.g_alternates, *ap)) {
1951 fwrite("alternates ", 1, 11, outf);
1954 fwrite(*ap, 1, strlen(*ap), outf);
1955 fwrite(" ", 1, 1, outf);
1956 hm_mark((struct hash **)glob.g_alternates, *ap);
1962 DtMail::MailRc::nalternates(char * key, void *, void * client_data)
1964 DtVirtArray<char *> *value_list = (DtVirtArray<char *> *)client_data;
1966 value_list->append(key);
1969 /* manage the alternate name list */
1970 void DtMail::MailRc::add_alternates(char *name)
1972 if(! MailRc::hm_test((struct hash**)glob.g_alternates, name)) {
1973 /* name is not already there... */
1974 MailRc::hm_add((struct hash **)glob.g_alternates, name, NULL, 0);
1979 * Determine the current folder directory name.
1982 DtMail::MailRc::getfolderdir(char *name, size_t buffsize)
1986 if ((folder = mt_value("folder")) == NOSTR)
1989 snprintf(name, buffsize, "%s", folder);
1991 snprintf(name, buffsize, "%s/%s", mt_value("HOME"), folder);
1996 * Take a file name, possibly with shell meta characters
1997 * in it and expand it by using "sh -c echo filename"
1998 * Return the file name as a dynamic string.
2002 DtMail::MailRc::expand(char *name)
2004 char *xname = new char[LINESIZE];
2005 char *cmdbuf = new char[LINESIZE];
2007 register int pid, l;
2008 register char *cp, *Shell;
2011 char *retchr = NULL;
2013 if (name[0] == '+' && getfolderdir(cmdbuf, LINESIZE) >= 0) {
2014 sprintf(xname, "%s/%s", cmdbuf, name + 1);
2015 str = expand(xname);
2020 if (!strpbrk(name, "~{[*?$`'\"\\")) {
2021 return(strdup(name));
2023 if (pipe(pivec) < 0) {
2027 return(strdup(name));
2029 sprintf(cmdbuf, "echo %s", name);
2030 if ((pid = DTMAIL_FORK()) == 0) {
2031 Shell = mt_value("SHELL");
2032 if (Shell == NOSTR || *Shell=='\0')
2039 execlp(Shell, Shell, "-c", cmdbuf, (char *)0);
2051 l = read(pivec[0], xname, LINESIZE);
2053 while (wait(&s) != pid);
2056 if (s != 0 && s != SIGPIPE) {
2057 fprintf(stderr, "Echo failed\n");
2065 fprintf(stderr, "%s: No match\n", name);
2068 if (l == LINESIZE) {
2069 fprintf(stderr, "Buffer overflow expanding %s\n", name);
2073 for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
2076 if (strchr(xname, ' ') && stat(xname, &sbuf) < 0) {
2077 fprintf(stderr, "%s: Ambiguous\n", name);
2081 retchr = strdup(xname);
2094 DtMail::MailRc::source(char **arglist, DtMail::MailRc *self)
2096 char *fname = arglist[0];
2100 /* if any of the three if test failed, return 0,
2101 * since we have not updated the input and stack pointer yet
2103 if ((cp = self->expand(fname)) == NOSTR)
2106 if ((fi = fopen(cp, "r")) == NULL) {
2112 if (self->ssp >= NOFILE - 2) {
2116 self->sstack[++(self->ssp)].s_file = self->input;
2117 self->sstack[self->ssp].s_cond = self->cond;
2126 DtMail::MailRc::wsource(const char * verbatim, char ** arglist, FILE * outf)
2129 if(verbatim != NULL && outf != NULL)
2130 fwrite(verbatim, 1, strlen(verbatim), outf);
2134 DtMail::MailRc::ifcmd(char **arglist, DtMail::MailRc *self)
2138 if (self->cond != CANY) {
2139 fprintf(stderr,"Illegal nested \"if\"\n");
2158 fprintf(stderr,"Unrecognized if-keyword: \"%s\"\n",
2166 DtMail::MailRc::wifcmd(const char * verbatim, char ** arglist, FILE * outf)
2169 if(verbatim != NULL && outf != NULL)
2170 fwrite(verbatim, 1, strlen(verbatim), outf);
2175 DtMail::MailRc::elsecmd(char **, DtMail::MailRc *self)
2178 switch (self->cond) {
2180 fprintf(stderr, "\"Else\" without matching \"if\"\n");
2192 self->cond = CNOTTY;
2200 fprintf(stderr,"invalid condition encountered\n");
2208 DtMail::MailRc::welsecmd(const char * verbatim, char ** arglist, FILE * outf)
2211 if(verbatim != NULL && outf != NULL)
2212 fwrite(verbatim, 1, strlen(verbatim), outf);
2217 DtMail::MailRc::endifcmd(char **, DtMail::MailRc *self)
2220 if (self->cond == CANY) {
2221 fprintf(stderr,"\"Endif\" without matching \"if\"\n");
2229 DtMail::MailRc::wendifcmd(const char * verbatim, char ** arglist, FILE * outf)
2232 if(verbatim != NULL && outf != NULL)
2233 fwrite(verbatim, 1, strlen(verbatim), outf);
2237 DtMail::MailRc::clearaliases(char **, DtMail::MailRc *)
2239 DtVirtArray<char *> *value_list = NULL;
2242 value_list = new DtVirtArray<char *>(10);
2244 hm_scan((struct hash **)glob.g_alias, nalias, value_list);
2246 while (value_list->length()) {
2247 char *buf, *val = (*value_list)[0];
2248 if ((buf = strchr(val, ' ')) != NULL)
2250 if (hm_test((struct hash **)glob.g_alias, val))
2251 hm_delete((struct hash **)glob.g_alias, val);
2252 value_list->remove(0);
2259 DtMail::MailRc::wclearaliases(const char *, char **, FILE *)