X-Git-Url: http://git.marmaro.de/?a=blobdiff_plain;ds=sidebyside;f=uip%2Fwhatnowsbr.c;fp=uip%2Fwhatnowsbr.c;h=0dfc3d72194e4eaa8a70eee9f9cfa6ba9f505f13;hb=ced6090a330d3d83d0bce709f756aa3d7d65fea4;hp=f8b7223b1d8d21a3c4b0dfa4925d11b4c9333e4b;hpb=337338b404931f06f0db2119c9e145e8ca5a9860;p=mmh diff --git a/uip/whatnowsbr.c b/uip/whatnowsbr.c index f8b7223..0dfc3d7 100644 --- a/uip/whatnowsbr.c +++ b/uip/whatnowsbr.c @@ -1,41 +1,41 @@ /* - * whatnowsbr.c -- the WhatNow shell - * - * This code is Copyright (c) 2002, by the authors of nmh. See the - * COPYRIGHT file in the root directory of the nmh distribution for - * complete copyright information. - * - * Several options have been added to ease the inclusion of attachments - * using the header field name mechanism added to anno and send. The - * -attach option is used to specify the header field name for attachments. - * - * Several commands have been added at the whatnow prompt: - * - * cd [ directory ] This option works just like the shell's - * cd command and lets the user change the - * directory from which attachments are - * taken so that long path names are not - * needed with every file. - * - * ls [ ls-options ] This option works just like the normal - * ls command and exists to allow the user - * to verify file names in the directory. - * - * pwd This option works just like the normal - * pwd command and exists to allow the user - * to verify the directory. - * - * attach files This option attaches the named files to - * the draft. - * - * alist [-ln] This option lists the attachments on the - * draft. -l gets long listings, -n gets - * numbered listings. - * - * detach files This option removes attachments from the - * detach -n numbers draft. This can be done by file name or - * by attachment number. - */ +** whatnowsbr.c -- the WhatNow shell +** +** This code is Copyright (c) 2002, by the authors of nmh. See the +** COPYRIGHT file in the root directory of the nmh distribution for +** complete copyright information. +** +** Several options have been added to ease the inclusion of attachments +** using the header field name mechanism added to anno and send. The +** -attach option is used to specify the header field name for attachments. +** +** Several commands have been added at the whatnow prompt: +** +** cd [ directory ] This option works just like the shell's +** cd command and lets the user change the +** directory from which attachments are +** taken so that long path names are not +** needed with every file. +** +** ls [ ls-options ] This option works just like the normal +** ls command and exists to allow the user +** to verify file names in the directory. +** +** pwd This option works just like the normal +** pwd command and exists to allow the user +** to verify the directory. +** +** attach files This option attaches the named files to +** the draft. +** +** alist [-ln] This option lists the attachments on the +** draft. -l gets long listings, -n gets +** numbered listings. +** +** detach files This option removes attachments from the +** detach -n numbers draft. This can be done by file name or +** by attachment number. +*/ #include #include @@ -60,8 +60,8 @@ static struct swit whatnowswitches[] = { }; /* - * Options at the "whatnow" prompt - */ +** Options at the "whatnow" prompt +*/ static struct swit aleqs[] = { #define EDITSW 0 { "edit [ ]", 0 }, @@ -99,8 +99,8 @@ static struct swit aleqs[] = { static char *myprompt = "\nWhat now? "; /* - * static prototypes - */ +** static prototypes +*/ static int editfile (char **, char **, char *, int, struct msgs *, char *, char *, int); static int sendfile (char **, char *, int); @@ -145,8 +145,8 @@ WhatNow (int argc, char **argv) argp = arguments; /* - * Get the initial current working directory. - */ + ** Get the initial current working directory. + */ if (getcwd(cwd, sizeof (cwd)) == (char *)0) { adios("getcwd", "could not get working directory"); @@ -162,7 +162,9 @@ WhatNow (int argc, char **argv) adios (NULL, "-%s unknown", cp); case HELPSW: - snprintf (buf, sizeof(buf), "%s [switches] [file]", invo_name); + snprintf (buf, sizeof(buf), + "%s [switches] [file]", + invo_name); print_help (buf, whatnowswitches, 1); done (1); case VERSIONSW: @@ -171,7 +173,8 @@ WhatNow (int argc, char **argv) case EDITRSW: if (!(ed = *argp++) || *ed == '-') - adios (NULL, "missing argument to %s", argp[-2]); + adios (NULL, "missing argument to %s", + argp[-2]); nedit = 0; continue; case NEDITSW: @@ -180,14 +183,16 @@ WhatNow (int argc, char **argv) case PRMPTSW: if (!(myprompt = *argp++) || *myprompt == '-') - adios (NULL, "missing argument to %s", argp[-2]); + adios (NULL, "missing argument to %s", + argp[-2]); continue; case ATTACHSW: if (attach != (char *)0) adios(NULL, "only one attachment header field name at a time!"); if (!(attach = *argp++) || *attach == '-') - adios (NULL, "missing argument to %s", argp[-2]); + adios (NULL, "missing argument to %s", + argp[-2]); continue; } } @@ -197,7 +202,7 @@ WhatNow (int argc, char **argv) drft = cp; } - if ((drft == NULL && (drft = getenv ("mhdraft")) == NULL) || *drft == 0) + if ((drft == NULL && (drft = getenv("mhdraft")) == NULL) || *drft == 0) drft = getcpy (m_draft("cur")); msgnam = (cp = getenv ("mhaltmsg")) && *cp ? getcpy (cp) : NULL; @@ -211,7 +216,8 @@ WhatNow (int argc, char **argv) } /* start editing the draft, unless -noedit was given */ - if (!nedit && editfile (&ed, NULL, drft, use, NULL, msgnam, NULL, 1) < 0) + if (!nedit && editfile(&ed, NULL, drft, use, NULL, msgnam, NULL, 1) + < 0) done (1); snprintf (prompt, sizeof(prompt), myprompt, invo_name); @@ -222,7 +228,10 @@ WhatNow (int argc, char **argv) } switch (smatch (*argp, aleqs)) { case DISPSW: - /* display the message being replied to, or distributed */ + /* + ** display the message being replied to, + ** or distributed + */ if (msgnam) showfile (++argp, msgnam); else @@ -238,7 +247,8 @@ WhatNow (int argc, char **argv) /* Call an editor on the draft file */ if (*++argp) ed = *argp++; - if (editfile (&ed, argp, drft, NOUSE, NULL, msgnam, NULL, 1) == NOTOK) + if (editfile (&ed, argp, drft, NOUSE, NULL, + msgnam, NULL, 1) == NOTOK) done (1); break; @@ -254,7 +264,7 @@ WhatNow (int argc, char **argv) removefile (drft); } else { if (stat (drft, &st) != NOTOK) - advise (NULL, "draft left on %s", drft); + advise(NULL, "draft left on %s", drft); } done (1); @@ -282,13 +292,13 @@ WhatNow (int argc, char **argv) case CDCMDSW: /* - * Change the working directory for attachments - * - * Run the directory through the user's shell - * so that we can take advantage of any syntax - * that the user is accustomed to. Read back - * the absolute path. - */ + ** Change the working directory for attachments + ** + ** Run the directory through the user's shell + ** so that we can take advantage of any syntax + ** that the user is accustomed to. Read back + ** the absolute path. + */ if (*(argp+1) == (char *)0) { (void)sprintf(buf, "$SHELL -c \"cd;pwd\""); @@ -315,24 +325,24 @@ WhatNow (int argc, char **argv) case LSCMDSW: /* - * List files in the current attachment working - * directory - * - * Use the user's shell so that we can take - * advantage of any syntax that the user is - * accustomed to. - */ + ** List files in the current attachment working + ** directory + ** + ** Use the user's shell so that we can take + ** advantage of any syntax that the user is + ** accustomed to. + */ writelscmd(buf, sizeof(buf), argp); (void)system_in_dir(cwd, buf); break; case ALISTCMDSW: /* - * List attachments on current draft. Options are: - * - * -l long listing (full path names) - * -n numbers listing - */ + ** List attachments on current draft. Options are: + ** + ** -l long listing (full path names) + ** -n numbers listing + */ if (attach == (char *)0) { advise((char *)0, "can't list because no header field name was given."); @@ -349,7 +359,8 @@ WhatNow (int argc, char **argv) else if (strcmp(*argp, "-n") == 0) n = 1; - else if (strcmp(*argp, "-ln") == 0 || strcmp(*argp, "-nl") == 0) { + else if (strcmp(*argp, "-ln") == 0 || + strcmp(*argp, "-nl") == 0) { l = "/"; n = 1; } else { @@ -368,8 +379,8 @@ WhatNow (int argc, char **argv) case ATTACHCMDSW: /* - * Attach files to current draft. - */ + ** Attach files to current draft. + */ if (attach == (char *)0) { advise((char *)0, "can't attach because no header field name was given."); @@ -382,32 +393,36 @@ WhatNow (int argc, char **argv) } /* - * Build a command line that causes the user's - * shell to list the file name arguments. - * This handles and wildcard expansion, tilde - * expansion, etc. - */ + ** Build a command line that causes the user's + ** shell to list the file name arguments. + ** This handles and wildcard expansion, tilde + ** expansion, etc. + */ writelscmd(buf, sizeof(buf), argp); /* - * Read back the response from the shell, - * which contains a number of lines with one - * file name per line. Remove off the newline. - * Determine whether we have an absolute or - * relative path name. Prepend the current - * working directory to relative path names. - * Add the attachment annotation to the draft. - */ + ** Read back the response from the shell, + ** which contains a number of lines with one + ** file name per line. Remove off the newline. + ** Determine whether we have an absolute or + ** relative path name. Prepend the current + ** working directory to relative path names. + ** Add the attachment annotation to the draft. + */ if ((f = popen_in_dir(cwd, buf, "r")) != (FILE *)0) { - while (fgets(shell, sizeof (shell), f) != (char *)0) { + while (fgets(shell, sizeof (shell), f) + != (char *)0) { *(strchr(shell, '\n')) = '\0'; if (*shell == '/') - (void)annotate(drft, attach, shell, 1, 0, -2, 1); + annotate(drft, attach, shell, + 1, 0, -2, 1); else { - (void)sprintf(file, "%s/%s", cwd, shell); - (void)annotate(drft, attach, file, 1, 0, -2, 1); + sprintf(file, "%s/%s", cwd, + shell); + annotate(drft, attach, file, + 1, 0, -2, 1); } } @@ -420,8 +435,8 @@ WhatNow (int argc, char **argv) case DETACHCMDSW: /* - * Detach files from current draft. - */ + ** Detach files from current draft. + */ if (attach == (char *)0) { advise((char *)0, "can't detach because no header field name was given."); @@ -429,12 +444,14 @@ WhatNow (int argc, char **argv) } /* - * Scan the arguments for a -n. Mixed file - * names and numbers aren't allowed, so this - * catches a -n anywhere in the argument list. - */ - - for (n = 0, arguments = argp + 1; *arguments != (char *)0; arguments++) { + ** Scan the arguments for a -n. Mixed file + ** names and numbers aren't allowed, so this + ** catches a -n anywhere in the argument list. + */ + + for (n = 0, arguments = argp + 1; + *arguments != (char *)0; + arguments++) { if (strcmp(*arguments, "-n") == 0) { n = 1; break; @@ -442,21 +459,24 @@ WhatNow (int argc, char **argv) } /* - * A -n was found so interpret the arguments as - * attachment numbers. Decrement any remaining - * argument number that is greater than the one - * just processed after processing each one so - * that the numbering stays correct. - */ + ** A -n was found so interpret the arguments as + ** attachment numbers. Decrement any remaining + ** argument number that is greater than the one + ** just processed after processing each one so + ** that the numbering stays correct. + */ if (n == 1) { - for (arguments = argp + 1; *arguments != (char *)0; arguments++) { + for (arguments = argp + 1; + *arguments != (char *)0; + arguments++) { if (strcmp(*arguments, "-n") == 0) continue; if (**arguments != '\0') { n = atoi(*arguments); - (void)annotate(drft, attach, (char *)0, 1, 0, n, 1); + annotate(drft, attach, NULL, + 1, 0, n, 1); for (argp = arguments + 1; *argp != (char *)0; argp++) { if (atoi(*argp) > n) { @@ -471,22 +491,24 @@ WhatNow (int argc, char **argv) } /* - * The arguments are interpreted as file names. - * Run them through the user's shell for wildcard - * expansion and other goodies. Do this from - * the current working directory if the argument - * is not an absolute path name (does not begin - * with a /). - * - * We feed all the file names to the shell at - * once, otherwise you can't provide a file name - * with a space in it. - */ + ** The arguments are interpreted as file names. + ** Run them through the user's shell for wildcard + ** expansion and other goodies. Do this from + ** the current working directory if the argument + ** is not an absolute path name (does not begin + ** with a /). + ** + ** We feed all the file names to the shell at + ** once, otherwise you can't provide a file name + ** with a space in it. + */ writelscmd(buf, sizeof(buf), argp); if ((f = popen_in_dir(cwd, buf, "r")) != (FILE *)0) { - while (fgets(shell, sizeof (shell), f) != (char *)0) { + while (fgets(shell, sizeof (shell), f) + != NULL) { *(strchr(shell, '\n')) = '\0'; - (void)annotate(drft, attach, shell, 1, 0, 0, 1); + annotate(drft, attach, shell, + 1, 0, 0, 1); } pclose(f); } else { @@ -507,27 +529,30 @@ WhatNow (int argc, char **argv) /* - * Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; - * trailcmd". - */ +** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ; +** trailcmd". +*/ static void writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp) { char *cp; - /* Note that we do not quote -- the argp from the user - * is assumed to be quoted as they desire. (We can't treat - * it as pure literal as that would prevent them using ~, - * wildcards, etc.) The buffer produced by this function - * should be given to popen_in_dir() or system_in_dir() so - * that the current working directory is set correctly. - */ + /* + ** Note that we do not quote -- the argp from the user + ** is assumed to be quoted as they desire. (We can't treat + ** it as pure literal as that would prevent them using ~, + ** wildcards, etc.) The buffer produced by this function + ** should be given to popen_in_dir() or system_in_dir() so + ** that the current working directory is set correctly. + */ int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd); - /* NB that some snprintf() return -1 on overflow rather than the - * new C99 mandated 'number of chars that would have been written' - */ - /* length checks here and inside the loop allow for the - * trailing ';', trailcmd, '"' and NUL - */ + /* + ** NB that some snprintf() return -1 on overflow rather than the + ** new C99 mandated 'number of chars that would have been written' + */ + /* + ** length checks here and inside the loop allow for the + ** trailing ';', trailcmd, '"' and NUL + */ int trailln = strlen(trailcmd) + 3; if (ln < 0 || ln + trailln > bufsz) adios((char *)0, "arguments too long"); @@ -553,18 +578,19 @@ writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp) } /* - * Build a command line that causes the user's shell to list the file name - * arguments. This handles and wildcard expansion, tilde expansion, etc. - */ +** Build a command line that causes the user's shell to list the file name +** arguments. This handles and wildcard expansion, tilde expansion, etc. +*/ static void writelscmd(char *buf, int bufsz, char **argp) { writesomecmd(buf, bufsz, "ls", "", argp); } -/* Like system(), but run the command in directory dir. - * This assumes the program is single-threaded! - */ +/* +** Like system(), but run the command in directory dir. +** This assumes the program is single-threaded! +*/ static int system_in_dir(const char *dir, const char *cmd) { @@ -598,8 +624,8 @@ popen_in_dir(const char *dir, const char *cmd, const char *type) /* - * EDIT - */ +** EDIT +*/ static int reedit = 0; /* have we been here before? */ static char *edsave = NULL; /* the editor we used previously */ @@ -643,11 +669,13 @@ editfile (char **ed, char **arg, char *file, int use, struct msgs *mp, if (mp == NULL || *altmsg == '/' || cwd == NULL) strncpy (altpath, altmsg, sizeof(altpath)); else - snprintf (altpath, sizeof(altpath), "%s/%s", mp->foldpath, altmsg); + snprintf (altpath, sizeof(altpath), "%s/%s", + mp->foldpath, altmsg); if (cwd == NULL) strncpy (linkpath, altmsglink, sizeof(linkpath)); else - snprintf (linkpath, sizeof(linkpath), "%s/%s", cwd, altmsglink); + snprintf (linkpath, sizeof(linkpath), "%s/%s", + cwd, altmsglink); } if (altmsg) { @@ -656,7 +684,8 @@ editfile (char **ed, char **arg, char *file, int use, struct msgs *mp, if (link (altpath, linkpath) == NOTOK) { #if 0 /* I don't think permission on symlinks matters /JLR */ - oumask = umask(0044); /* PJS: else symlinks are world 'r' */ + /* PJS: else symlinks are world 'r' */ + oumask = umask(0044); #endif symlink (altpath, linkpath); #if 0 @@ -742,14 +771,13 @@ editfile (char **ed, char **arg, char *file, int use, struct msgs *mp, || link (linkpath, altpath) == NOTOK))) advise (linkpath, "unable to update %s from", altmsg); #else /* HAVE_LSTAT */ - if (altmsg - && mp - && !is_readonly(mp) + if (altmsg && mp && !is_readonly(mp) && stat (linkpath, &st) != NOTOK && st.st_nlink == 1 && (unlink (altpath) == NOTOK - || link (linkpath, altpath) == NOTOK)) - advise (linkpath, "unable to update %s from", altmsg); + || link (linkpath, altpath) == NOTOK)) + advise (linkpath, "unable to update %s from", + altmsg); #endif /* HAVE_LSTAT */ } @@ -795,8 +823,8 @@ copyf (char *ifile, char *ofile) /* - * SEND - */ +** SEND +*/ static int sendfile (char **arg, char *file, int pushsw) @@ -806,24 +834,21 @@ sendfile (char **arg, char *file, int pushsw) char *cp, *sp, *vec[MAXARGS]; /* Translate MIME composition file, if necessary */ - if ((cp = context_find ("automimeproc")) - && (!strcmp (cp, "1")) - && !getenv ("NOMHNPROC") - && check_draft (file) + if ((cp = context_find ("automimeproc")) && (!strcmp (cp, "1")) + && !getenv ("NOMHNPROC") && check_draft (file) && (buildfile (NULL, file) == NOTOK)) return 0; /* For backwards compatibility */ - if ((cp = context_find ("automhnproc")) - && !getenv ("NOMHNPROC") - && check_draft (file) - && (i = editfile (&cp, NULL, file, NOUSE, NULL, NULL, NULL, 0))) + if ((cp = context_find ("automhnproc")) && !getenv ("NOMHNPROC") + && check_draft (file) && (i = editfile (&cp, NULL, + file, NOUSE, NULL, NULL, NULL, 0))) return 0; /* - * If the sendproc is the nmh command `send', then we call - * those routines directly rather than exec'ing the command. - */ + ** If the sendproc is the nmh command `send', then we call + ** those routines directly rather than exec'ing the command. + */ if (strcmp (sp = r1bindex (sendproc, '/'), "send") == 0) { cp = invo_name; sendit (invo_name = sp, arg, file, pushsw); @@ -864,8 +889,8 @@ sendfile (char **arg, char *file, int pushsw) /* - * Translate MIME composition file (call buildmimeproc) - */ +** Translate MIME composition file (call buildmimeproc) +*/ static int buildfile (char **argp, char *file) @@ -884,9 +909,9 @@ buildfile (char **argp, char *file) args = (char **) mh_xmalloc((i + 2) * sizeof(char *)); /* - * For backward compatibility, we need to add -build - * if we are using mhn as buildmimeproc - */ + ** For backward compatibility, we need to add -build + ** if we are using mhn as buildmimeproc + */ i = 0; if (strcmp (r1bindex (ed, '/'), "mhn") == 0) args[i++] = "-build"; @@ -904,8 +929,8 @@ buildfile (char **argp, char *file) /* - * Check if draft is a mhbuild composition file - */ +** Check if draft is a mhbuild composition file +*/ static int check_draft (char *msgnam) @@ -922,16 +947,17 @@ check_draft (char *msgnam) case FLDPLUS: case FLDEOF: /* - * If draft already contains any of the - * Content-XXX fields, then assume it already - * been converted. - */ + ** If draft already contains any of the + ** Content-XXX fields, then assume it already + ** been converted. + */ if (uprf (name, XXX_FIELD_PRF)) { fclose (fp); return 0; } while (state == FLDPLUS) - state = m_getfld (state, name, buf, sizeof(buf), fp); + state = m_getfld (state, name, buf, + sizeof(buf), fp); break; case BODY: @@ -944,7 +970,8 @@ check_draft (char *msgnam) return 1; } - state = m_getfld (state, name, buf, sizeof(buf), fp); + state = m_getfld (state, name, buf, + sizeof(buf), fp); } while (state == BODY); /* and fall... */ @@ -1050,8 +1077,7 @@ sendit (char *sp, char **arg, char *file, int pushed) char **arguments, *vec[MAXARGS]; struct stat st; char *attach = (char *)0; /* attachment header field name */ - int attachformat = 0; /* mhbuild format specifier for - attachments */ + int attachformat = 0; /* mhbuild format specifier for attachments */ #ifndef lint int distsw = 0; @@ -1061,22 +1087,22 @@ sendit (char *sp, char **arg, char *file, int pushed) #endif /* - * Make sure these are defined. In particular, we need - * vec[1] to be NULL, in case "arg" is NULL below. It - * doesn't matter what is the value of vec[0], but we - * set it to NULL, to help catch "off-by-one" errors. - */ + ** Make sure these are defined. In particular, we need + ** vec[1] to be NULL, in case "arg" is NULL below. It + ** doesn't matter what is the value of vec[0], but we + ** set it to NULL, to help catch "off-by-one" errors. + */ vec[0] = NULL; vec[1] = NULL; /* - * Temporarily copy arg to vec, since the brkstring() call in - * getarguments() will wipe it out before it is merged in. - * Also, we skip the first element of vec, since getarguments() - * skips it. Then we count the number of arguments - * copied. The value of "n" will be one greater than - * this in order to simulate the standard argc/argv. - */ + ** Temporarily copy arg to vec, since the brkstring() call in + ** getarguments() will wipe it out before it is merged in. + ** Also, we skip the first element of vec, since getarguments() + ** skips it. Then we count the number of arguments + ** copied. The value of "n" will be one greater than + ** this in order to simulate the standard argc/argv. + */ if (arg) { char **bp; @@ -1087,9 +1113,9 @@ sendit (char *sp, char **arg, char *file, int pushed) } /* - * Merge any arguments from command line (now in vec) - * and arguments from profile. - */ + ** Merge any arguments from command line (now in vec) + ** and arguments from profile. + */ arguments = getarguments (sp, n, vec, 1); argp = arguments; @@ -1117,7 +1143,8 @@ sendit (char *sp, char **arg, char *file, int pushed) return; case SHELPSW: - snprintf (buf, sizeof(buf), "%s [switches]", sp); + snprintf (buf, sizeof(buf), + "%s [switches]", sp); print_help (buf, sendswitches, 1); return; case SVERSIONSW: @@ -1189,7 +1216,8 @@ sendit (char *sp, char **arg, char *file, int pushed) continue; case SNDATTACHSW: - if (!(attach = *argp++) || *attach == '-') { + if (!(attach = *argp++) || + *attach == '-') { advise (NULL, "missing argument to %s", argp[-2]); return; } @@ -1200,10 +1228,8 @@ sendit (char *sp, char **arg, char *file, int pushed) adios (NULL, "missing argument to %s", argp[-1]); else { attachformat = atoi (*argp); - if (attachformat < 0 || - attachformat > ATTACHFORMATS - 1) { - advise (NULL, "unsupported attachformat %d", - attachformat); + if (attachformat < 0 || attachformat > ATTACHFORMATS - 1) { + advise (NULL, "unsupported attachformat %d", attachformat); continue; } } @@ -1280,8 +1306,8 @@ sendit (char *sp, char **arg, char *file, int pushed) /* - * Remove the draft file - */ +** Remove the draft file +*/ static int removefile (char *drft)