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 librararies 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(__uxp__)
158 #define SYSTEM_MAILRC "/etc/mail/mailx.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.
1075 DtMail::MailRc::readline(FILE *ibuf, char *linebuf)
1083 for (cp = linebuf; c != '\n' && c != EOF; c = getc(ibuf)) {
1087 "Mail: ignoring NULL characters in mail\n");
1092 if (cp - linebuf < LINESIZE-2)
1096 if (c == EOF && cp == linebuf)
1098 return(cp - linebuf + 1);
1103 * Pop the current input back to the previous level.
1104 * Update the "sourcing" flag as appropriate.
1107 void DtMail::MailRc::unstack()
1110 fprintf(stderr,"\"Source\" stack over-pop.\n");
1116 fprintf(stderr,"Unmatched \"if\"\n");
1117 cond = sstack[ssp].s_cond;
1118 input = sstack[ssp--].s_file;
1123 * Find the correct command in the command table corresponding
1124 * to the passed command "word"
1127 void *DtMail::MailRc::lex(char word[])
1129 register struct cmd *cp;
1131 for (cp = &cmdtab[0]; cp->c_name != NOSTR; cp++)
1132 if (DtMail::MailRc::isprefix(word, cp->c_name))
1138 * Determine if as1 is a valid prefix of as2.
1139 * Return true if yep.
1142 int DtMail::MailRc::isprefix(char *as1, char *as2)
1144 register char *s1, *s2;
1148 while (*s1++ == *s2)
1151 return(*--s1 == '\0');
1155 * Scan out the list of string arguments, shell style
1159 int DtMail::MailRc::getrawlist(char line[], char **argv, int argc)
1161 register char **ap, *cp, *cp2;
1162 char linebuf[LINESIZE], quotec;
1163 register char **last;
1167 last = argv + argc - 1;
1168 while (*cp != '\0') {
1169 while (*cp && strchr(" \t", *cp))
1173 while (*cp != '\0') {
1175 if (*cp == quotec) {
1181 if (*cp && strchr(" \t", *cp))
1183 if (*cp && strchr("'\"", *cp))
1194 "Too many elements in the list; excess discarded\n");
1197 *ap++ = strdup((char*)linebuf);
1203 void DtMail::MailRc::freerawlist(char **argv)
1213 * Get the value of a variable and return it.
1214 * Look in the environment if its not available locally.
1217 char *DtMail::MailRc::mt_value(char name[])
1219 register struct var *vp;
1221 // extern char *getenv();
1223 if ((vp = lookup(name, (struct var **)this->variables)) == (struct var *)NULL)
1231 * Locate a variable and return its variable
1234 struct var *DtMail::MailRc::lookup(char *name, struct var **hasharray)
1236 register struct var *vp;
1240 for (vp = hasharray[h]; vp != (struct var *)NULL; vp = vp->v_link)
1241 if (strcmp(vp->v_name, name) == 0)
1243 return ((struct var *)NULL);
1248 * Put add users to a group.
1251 int DtMail::MailRc::group(char **argv, DtMail::MailRc *)
1259 * Insert names from the command list into the group.
1260 * Who cares if there are duplicates? They get tossed
1264 /* compute the size of the buffer */
1266 for (ap = argv+1; *ap != NOSTR; ap++) {
1267 size += strlen(*ap) +1;
1269 buf = (char *)malloc(size);
1271 for (ap = argv+1; *ap != NOSTR; ap++) {
1278 MailRc::add_alias((char *)argv[0], (char *)buf);
1284 DtMail::MailRc::wgroup(const char * verbatim, char ** argv, FILE * outf)
1291 for (ap = argv+1; *ap != NOSTR; ap++) {
1292 size += strlen(*ap) +1;
1294 buf = (char *)malloc(size);
1296 for (ap = argv+1; *ap != NOSTR; ap++) {
1303 char * cur = (char *)hm_test((struct hash **)glob.g_alias, argv[0]);
1304 if (!cur || (!clearAliases && hm_ismarked((struct hash **)glob.g_alias,
1306 // Its been removed or written. Dont write it to the file.
1311 if (strcmp(cur, buf) == 0) {
1312 // It hasnt changed. Write the original to preserve spacing.
1314 fwrite(verbatim, 1, strlen(verbatim), outf);
1317 // It has changed. Create a new alias.
1319 fwrite("alias ", 1, 6, outf);
1320 fwrite(argv[0], 1, strlen(argv[0]), outf);
1321 fwrite(" ", 1, 1, outf);
1322 fwrite(cur, 1, strlen(cur), outf);
1323 fwrite("\n", 1, 1, outf);
1326 hm_mark((struct hash **)glob.g_alias, argv[0]);
1330 DtMail::MailRc::ngroup(char * key, void * data, void * client_data)
1332 char * value = (char *)data;
1333 FILE * outf = (FILE *)client_data;
1335 fwrite("alias ", 1, 6, outf);
1336 fwrite(key, 1, strlen(key), outf);
1337 fwrite(" ", 1, 1, outf);
1338 fwrite(value, 1, strlen(value), outf);
1339 fwrite("\n", 1, 1, outf);
1343 DtMail::MailRc::nalias(char * key, void * data, void * client_data)
1345 DtVirtArray<char *> *value_list = (DtVirtArray<char *> *)client_data;
1346 char *new_alias = NULL;
1347 char *white_space = NULL;
1349 int i, num_spaces = 0;
1350 int key_len = strlen(key);
1351 // figure out whitespace for formatting
1352 // assume 13 for normal sized alias name
1354 if(key_len < 13) // need to add spaces
1356 num_spaces = 13 - key_len;
1358 white_space = (char *)malloc(num_spaces + 1);
1359 white_space[0] = '\0';
1361 for(i = 0; i < num_spaces; i++)
1362 white_space[i] = ' ';
1364 white_space[num_spaces] = '\0';
1366 // strcat(white_space, " ");
1368 /* make an alias string */
1369 m_size = key_len + strlen((char *)white_space)
1370 + strlen((char *)data) + strlen(" = ") + 1;
1371 new_alias = (char *)malloc(m_size);
1373 sprintf(new_alias, "%s%s = %s",key, white_space, data);
1378 /* make an alias string */
1379 m_size = key_len + strlen((char *)data) + strlen(" = ") + 1;
1380 new_alias = (char *)malloc(m_size);
1382 sprintf(new_alias, "%s = %s",key, data);
1386 value_list->append(new_alias);
1391 DtMail::MailRc::nignorelist(char * key, void * data, void * client_data)
1394 DtVirtArray<char *> *value_list = (DtVirtArray<char *> *)client_data;
1395 char *new_ignore = NULL;
1397 new_ignore = (char *)malloc(strlen((char *)key) + 2);
1399 sprintf(new_ignore, "%s", key);
1401 value_list->append(new_ignore);
1407 * Set or display a variable value. Syntax is similar to that
1411 int DtMail::MailRc::set(char **arglist, DtMail::MailRc *)
1413 register char *cp, *cp2;
1414 char varbuf[LINESIZE], **ap;
1418 for (ap = arglist; *ap != NOSTR; ap++) {
1421 while (*cp != '=' && *cp != '\0')
1428 if (equal(varbuf, "")) {
1429 fprintf(stderr,"Non-null variable name required\n");
1433 MailRc::mt_assign(varbuf, cp);
1439 DtMail::MailRc::wset(const char * verbatim,
1443 char varbuf[LINESIZE];
1444 char *cp, *cp2, **ap;
1446 for (ap = arglist; *ap != NOSTR; ap++) {
1449 while (*cp != '=' && *cp != '\0')
1456 if (equal(varbuf, "")) {
1460 // Lookup the current value, and see if we should rewrite this
1463 struct var * vp = lookup(varbuf, (struct var **)variables);
1464 if (vp == NULL || vp->v_written) {
1465 // If the original input line was set novar then just write
1466 // it out again. We can not easily track duplicates here.
1468 if (varbuf[0] == 'n' && varbuf[1] == 'o') {
1469 fwrite(verbatim, 1, strlen(verbatim), outf);
1472 // This variable has been unassigned or previously written.
1473 // Remove it from the file, by not writing it.
1478 // Compare the values. If equal, then write the line verbatim to
1479 // preserve the users original spacing and quoting.
1481 if (strcmp(cp, vp->v_value) == 0) {
1482 fwrite(verbatim, 1, strlen(verbatim), outf);
1486 // Write the variable, with a new value.
1488 fwrite("set ", 1, 4, outf);
1489 fwrite(varbuf, 1, strlen(varbuf), outf);
1490 if (strlen(vp->v_value) > 0) {
1491 fwrite("='", 1, 2, outf);
1492 fwrite(vp->v_value, 1, strlen(vp->v_value), outf);
1493 fwrite("'", 1, 1, outf);
1495 fwrite("\n", 1, 1, outf);
1502 * Unset a bunch of variable values.
1505 int DtMail::MailRc::unset(char **arglist, DtMail::MailRc *)
1509 for (ap = arglist; *ap != NOSTR; ap++)
1510 (void) MailRc::mt_deassign(*ap);
1515 DtMail::MailRc::wunset(const char * verbatim,
1520 if(verbatim != NULL && outf != NULL)
1521 fwrite(verbatim, 1, strlen(verbatim), outf);
1525 * Hash the passed string and return an index into
1526 * the variable or group hash table.
1528 int DtMail::MailRc::hash(char *name)
1530 register unsigned h;
1533 for (cp = name, h = 0; *cp; h = (h << 2) + *cp++)
1535 return (h % HSHSIZE);
1538 /* manage the alias list */
1539 void DtMail::MailRc::add_alias(char *name, char *value)
1544 /* aliases to the same name get appended to the list */
1545 old = (char*)MailRc::hm_test((struct hash **)glob.g_alias, name);
1550 size = strlen(value) + strlen(old) + 2;
1551 new_ptr = (char *)malloc(size);
1552 sprintf(new_ptr, "%s %s", value, old);
1554 /* delete any old bindings for the name */
1555 MailRc::hm_delete((struct hash**)glob.g_alias, name);
1557 /* add the new binding */
1558 MailRc::hm_add((struct hash**)glob.g_alias, name, new_ptr, size);
1560 /* free up the temporary space */
1563 /* add the new binding */
1564 MailRc::hm_add((struct hash**)glob.g_alias, name, value, strlen(value) +1);
1569 * Assign a value to a mail variable.
1571 void DtMail::MailRc::mt_assign(char *name,char * val)
1575 (void) mt_deassign(name+1);
1576 else if (name[0]=='n' && name[1]=='o')
1577 (void) mt_deassign(name+2);
1578 else if ((val[0] == 'n' || val[0] == 'N') &&
1579 (val[1] == 'o' || val[1] == 'O') && val[2] == '\0') {
1580 (void) mt_deassign(name);
1581 } else mt_puthash(name, vcopy(val), MailRc::variables);
1584 int DtMail::MailRc::mt_deassign(char *s)
1586 register struct var *vp, *vp2;
1589 if ((vp2 = lookup(s, MailRc::variables)) == (struct var *)NULL) {
1593 if (vp2 == MailRc::variables[h]) {
1594 MailRc::variables[h] = MailRc::variables[h]->v_link;
1596 vfree(vp2->v_value);
1600 for (vp = MailRc::variables[h]; vp->v_link != vp2; vp = vp->v_link)
1602 vp->v_link = vp2->v_link;
1604 vfree(vp2->v_value);
1610 * associate val with name in hasharray
1612 void DtMail::MailRc::mt_puthash(char *name, char *val, struct var **hasharray)
1614 register struct var *vp;
1617 vp = lookup(name, hasharray);
1618 if (vp == (struct var *)NULL) {
1620 vp = (struct var *) (calloc(sizeof *vp, 1));
1621 vp->v_name = vcopy(name);
1622 vp->v_link = hasharray[h];
1631 DtMail::MailRc::mt_scan(FILE * outf)
1633 for (int slot = 0; slot < HSHSIZE; slot++) {
1634 for (var * vp = MailRc::variables[slot]; vp != (struct var *)NULL; vp = vp->v_link) {
1635 if (!vp->v_written) {
1636 fwrite("set ", 1, 4, outf);
1637 fwrite(vp->v_name, 1, strlen(vp->v_name), outf);
1638 if (strlen(vp->v_value)) {
1639 fwrite("='", 1, 2, outf);
1640 fwrite(vp->v_value, 1, strlen(vp->v_value), outf);
1641 fwrite("'", 1, 1, outf);
1643 fwrite("\n", 1, 1, outf);
1651 * Copy a variable value into permanent space.
1652 * Do not bother to alloc space for "".
1654 char *DtMail::MailRc::vcopy(char *str)
1657 if (strcmp(str, "") == 0)
1659 return (strdup(str));
1663 * Free up a variable string. We do not bother to allocate
1664 * strings whose value is "" since they are expected to be frequent.
1665 * Thus, we cannot free same!
1667 void DtMail::MailRc::vfree(char *cp)
1670 if (strcmp(cp, "") != 0)
1674 void *DtMail::MailRc::hm_alloc(void)
1676 struct hash **table;
1678 table = (struct hash**)malloc(sizeof (struct hash) * HASHSIZE);
1679 memset(table, '\0', sizeof (struct hash) * HASHSIZE);
1682 void DtMail::MailRc::hm_add(struct hash **table,
1688 register struct hash *h;
1693 index = hash_index(key);
1694 h = (struct hash *)malloc(sizeof (struct hash));
1695 h->h_next = table[index];
1698 h->h_key = strdup(key);
1699 if (size && value != NULL) {
1700 h->h_value = malloc(size);
1701 memcpy(h->h_value, value, size);
1703 h->h_value = nullfield;
1708 void DtMail::MailRc::hm_delete(struct hash **table, char *key)
1711 register struct hash *h;
1712 register struct hash *old;
1717 index = hash_index(key);
1721 if (strcasecmp(h->h_key, key) == 0) {
1722 /* found the match */
1724 table[index] = h->h_next;
1726 old->h_next = h->h_next;
1738 void *DtMail::MailRc::hm_test(struct hash **table, char *key)
1740 register struct hash *h;
1745 h = table[hash_index(key)];
1748 if (strcasecmp(h->h_key, key) == 0) {
1750 return (h->h_value);
1759 void DtMail::MailRc::hm_mark(struct hash **table, char *key)
1761 register struct hash *h;
1766 h = table[hash_index(key)];
1769 if (strcasecmp(h->h_key, key) == 0) {
1778 int DtMail::MailRc::hm_ismarked(struct hash **table, char *key)
1780 register struct hash *h;
1782 if (!table) return 0;
1783 h = table[hash_index(key)];
1786 if (strcasecmp(h->h_key, key) == 0) return(h->h_written);
1794 DtMail::MailRc::hm_scan(struct hash **table, hm_callback callback, void * client_data)
1796 for (int slot = 0; slot < HASHSIZE; slot++) {
1797 for (struct hash * h = table[slot]; h; h = h->h_next) {
1798 if (!h->h_written) {
1799 callback(h->h_key, h->h_value, client_data);
1806 int DtMail::MailRc::hash_index(char *key)
1808 register unsigned h;
1822 return (h % HASHSIZE);
1825 void DtMail::MailRc::free_hash(struct hash *h)
1828 if (h->h_value != nullfield) {
1835 * Add the given header fields to the ignored list.
1836 * If no arguments, print the current list of ignored fields.
1838 int DtMail::MailRc::igfield(char *list[], DtMail::MailRc *)
1842 for (ap = list; *ap != 0; ap++) {
1843 // DP(("igfield: adding %s\n", *ap));
1844 MailRc::add_ignore(*ap);
1850 DtMail::MailRc::wigfield(const char * verbatim, char ** list, FILE * outf)
1853 Boolean ignore_written = FALSE;
1855 // We need to see that every name in this line still exists. If not,
1856 // then we will make a second pass, rewriting the line with only the
1860 for (ap = list; *ap && good; ap++) {
1861 if (!hm_test((struct hash **)glob.g_ignore, *ap)) {
1868 fwrite(verbatim, 1, strlen(verbatim), outf);
1869 for (ap = list; *ap; ap++) {
1870 hm_mark((struct hash **)glob.g_ignore, *ap);
1875 // Create a new ignore line, leaving out the ignores that have
1876 // been removed. Also, dont write any ignores that have been
1877 // previously written.
1879 for (ap = list; *ap; ap++) {
1880 if (hm_test((struct hash **)glob.g_ignore, *ap) &&
1881 !hm_ismarked((struct hash **)glob.g_ignore, *ap)) {
1882 // Only write 'ignore' if there is something in the list
1883 if (!ignore_written) {
1884 fwrite("ignore ", 1, 7, outf);
1885 ignore_written = TRUE;
1887 fwrite(*ap, 1, strlen(*ap), outf);
1888 fwrite(" ", 1, 1, outf);
1889 hm_mark((struct hash **)glob.g_ignore, *ap);
1893 fwrite("\n", 1, 1, outf);
1897 DtMail::MailRc::nigfield(char * key, void *, void * client_data)
1899 DtVirtArray<char *> *value_list = (DtVirtArray<char *> *)client_data;
1901 value_list->append(key);
1904 /* manage the retain/ignore list */
1905 void DtMail::MailRc::add_ignore(char *name)
1907 if(! MailRc::hm_test((struct hash **)glob.g_ignore, name)) {
1908 /* name is not already there... */
1909 MailRc::hm_add((struct hash **)glob.g_ignore, name, NULL, 0);
1915 * Set the list of alternate names.
1917 int DtMail::MailRc::alternates(char **namelist, DtMail::MailRc *)
1919 while (*namelist != NULL)
1920 MailRc::add_alternates(*namelist++);
1925 DtMail::MailRc::walternates(const char * verbatim, char ** list, FILE * outf)
1927 // This is like ignores. We need to make sure all of the alternates
1928 // on this command are still in the database.
1932 for (ap = list; *ap && good; ap++) {
1933 if (!hm_test((struct hash **)glob.g_alternates, *ap)) {
1940 fwrite(verbatim, 1, strlen(verbatim), outf);
1941 for (ap = list; *ap; ap++) {
1942 hm_mark((struct hash **)glob.g_alternates, *ap);
1947 // Write out the alternates that still exist and have not been
1948 // previously written.
1950 Boolean written = FALSE;
1951 for (ap = list; *ap; ap++) {
1952 if (hm_test((struct hash **)glob.g_alternates, *ap) &&
1953 !hm_ismarked((struct hash **)glob.g_alternates, *ap)) {
1955 fwrite("alternates ", 1, 11, outf);
1958 fwrite(*ap, 1, strlen(*ap), outf);
1959 fwrite(" ", 1, 1, outf);
1960 hm_mark((struct hash **)glob.g_alternates, *ap);
1966 DtMail::MailRc::nalternates(char * key, void *, void * client_data)
1968 DtVirtArray<char *> *value_list = (DtVirtArray<char *> *)client_data;
1970 value_list->append(key);
1973 /* manage the alternate name list */
1974 void DtMail::MailRc::add_alternates(char *name)
1976 if(! MailRc::hm_test((struct hash**)glob.g_alternates, name)) {
1977 /* name is not already there... */
1978 MailRc::hm_add((struct hash **)glob.g_alternates, name, NULL, 0);
1983 * Determine the current folder directory name.
1986 DtMail::MailRc::getfolderdir(char *name)
1990 if ((folder = mt_value("folder")) == NOSTR)
1993 strcpy(name, folder);
1995 sprintf(name, "%s/%s", mt_value("HOME"), folder);
2000 * Take a file name, possibly with shell meta characters
2001 * in it and expand it by using "sh -c echo filename"
2002 * Return the file name as a dynamic string.
2006 DtMail::MailRc::expand(char *name)
2008 char *xname = new char[LINESIZE];
2009 char *cmdbuf = new char[LINESIZE];
2011 register int pid, l;
2012 register char *cp, *Shell;
2016 if (name[0] == '+' && getfolderdir(cmdbuf) >= 0) {
2017 sprintf(xname, "%s/%s", cmdbuf, name + 1);
2018 str = expand(xname);
2023 if (!strpbrk(name, "~{[*?$`'\"\\")) {
2024 return(strdup(name));
2026 if (pipe(pivec) < 0) {
2030 return(strdup(name));
2032 sprintf(cmdbuf, "echo %s", name);
2033 if ((pid = DTMAIL_FORK()) == 0) {
2034 Shell = mt_value("SHELL");
2035 if (Shell == NOSTR || *Shell=='\0')
2042 execlp(Shell, Shell, "-c", cmdbuf, (char *)0);
2054 l = read(pivec[0], xname, LINESIZE);
2056 while (wait(&s) != pid);
2059 if (s != 0 && s != SIGPIPE) {
2060 fprintf(stderr, "Echo failed\n");
2068 fprintf(stderr, "%s: No match\n", name);
2071 if (l == LINESIZE) {
2072 fprintf(stderr, "Buffer overflow expanding %s\n", name);
2076 for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
2079 if (strchr(xname, ' ') && stat(xname, &sbuf) < 0) {
2080 fprintf(stderr, "%s: Ambiguous\n", name);
2086 return(strdup(xname));
2096 DtMail::MailRc::source(char **arglist, DtMail::MailRc *self)
2098 char *fname = arglist[0];
2102 /* if any of the three if test failed, return 0,
2103 * since we have not updated the input and stack pointer yet
2105 if ((cp = self->expand(fname)) == NOSTR)
2108 if ((fi = fopen(cp, "r")) == NULL) {
2114 if (self->ssp >= NOFILE - 2) {
2118 self->sstack[++(self->ssp)].s_file = self->input;
2119 self->sstack[self->ssp].s_cond = self->cond;
2128 DtMail::MailRc::wsource(const char * verbatim, char ** arglist, FILE * outf)
2131 if(verbatim != NULL && outf != NULL)
2132 fwrite(verbatim, 1, strlen(verbatim), outf);
2136 DtMail::MailRc::ifcmd(char **arglist, DtMail::MailRc *self)
2140 if (self->cond != CANY) {
2141 fprintf(stderr,"Illegal nested \"if\"\n");
2160 fprintf(stderr,"Unrecognized if-keyword: \"%s\"\n",
2168 DtMail::MailRc::wifcmd(const char * verbatim, char ** arglist, FILE * outf)
2171 if(verbatim != NULL && outf != NULL)
2172 fwrite(verbatim, 1, strlen(verbatim), outf);
2177 DtMail::MailRc::elsecmd(char **, DtMail::MailRc *self)
2180 switch (self->cond) {
2182 fprintf(stderr, "\"Else\" without matching \"if\"\n");
2194 self->cond = CNOTTY;
2202 fprintf(stderr,"invalid condition encountered\n");
2210 DtMail::MailRc::welsecmd(const char * verbatim, char ** arglist, FILE * outf)
2213 if(verbatim != NULL && outf != NULL)
2214 fwrite(verbatim, 1, strlen(verbatim), outf);
2219 DtMail::MailRc::endifcmd(char **, DtMail::MailRc *self)
2222 if (self->cond == CANY) {
2223 fprintf(stderr,"\"Endif\" without matching \"if\"\n");
2231 DtMail::MailRc::wendifcmd(const char * verbatim, char ** arglist, FILE * outf)
2234 if(verbatim != NULL && outf != NULL)
2235 fwrite(verbatim, 1, strlen(verbatim), outf);
2239 DtMail::MailRc::clearaliases(char **, DtMail::MailRc *)
2241 DtVirtArray<char *> *value_list = NULL;
2244 value_list = new DtVirtArray<char *>(10);
2246 hm_scan((struct hash **)glob.g_alias, nalias, value_list);
2248 while (value_list->length()) {
2249 char *buf, *val = (*value_list)[0];
2250 if ((buf = strchr(val, ' ')) != NULL)
2252 if (hm_test((struct hash **)glob.g_alias, val))
2253 hm_delete((struct hash **)glob.g_alias, val);
2254 value_list->remove(0);
2261 DtMail::MailRc::wclearaliases(const char *, char **, FILE *)