+ if (n == -1)
+ advise(NULL, "usage is alist [-ln].");
+
+ else
+ annolist(drft, attach, l, n);
+
+ break;
+
+ case ATTACHCMDSW:
+ /*
+ ** Attach files to current draft.
+ */
+
+ if (attach == NULL) {
+ advise(NULL, "can't attach because no header field name was given.");
+ break;
+ }
+
+ if (*(argp+1) == NULL) {
+ advise(NULL, "attach command requires file argument(s).");
+ break;
+ }
+
+ /*
+ ** 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.
+ */
+
+ if ((f = popen_in_dir(cwd, buf, "r")) != (FILE *)0) {
+ while (fgets(shell, sizeof (shell), f)
+ != NULL) {
+ *(strchr(shell, '\n')) = '\0';
+
+ if (*shell == '/')
+ annotate(drft, attach, shell,
+ 1, 0, -2, 1);
+ else {
+ sprintf(file, "%s/%s", cwd,
+ shell);
+ annotate(drft, attach, file,
+ 1, 0, -2, 1);
+ }
+ }
+
+ pclose(f);
+ } else {
+ advise("popen", "could not get file from shell");
+ }
+
+ break;
+
+ case DETACHCMDSW:
+ /*
+ ** Detach files from current draft.
+ */
+
+ if (attach == NULL) {
+ advise(NULL, "can't detach because no header field name was given.");
+ break;
+ }
+
+ /*
+ ** 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 != NULL;
+ arguments++) {
+ if (strcmp(*arguments, "-n") == 0) {
+ n = 1;
+ break;
+ }
+ }
+
+ /*
+ ** 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 != NULL;
+ arguments++) {
+ if (strcmp(*arguments, "-n") == 0)
+ continue;
+
+ if (**arguments != '\0') {
+ n = atoi(*arguments);
+ annotate(drft, attach, NULL,
+ 1, 0, n, 1);
+
+ for (argp = arguments + 1; *argp != NULL; argp++) {
+ if (atoi(*argp) > n) {
+ if (atoi(*argp) == 1)
+ *argp = "";
+ else
+ sprintf(*argp, "%d", atoi(*argp) - 1);
+ }
+ }
+ }
+ }
+ }
+
+ /*
+ ** 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)
+ != NULL) {
+ *(strchr(shell, '\n')) = '\0';
+ annotate(drft, attach, shell,
+ 1, 0, 0, 1);
+ }
+ pclose(f);
+ } else {
+ advise("popen", "could not get file from shell");
+ }
+
+ break;
+
+ default:
+ /* Unknown command */
+ advise(NULL, "say what?");
+ break;