3 * msh.c -- The nmh shell
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 * Keep more status information in maildrop map
17 #include <h/signals.h>
18 #include <h/dropsbr.h>
19 #include <h/fmt_scan.h>
20 #include <h/scansbr.h>
33 #define QUOTE '\\' /* sigh */
35 static struct swit switches[] = {
37 { "idstart number", -7 }, /* interface from bbc */
39 { "idstop number", -6 }, /* .. */
41 { "idquit number", -6 }, /* .. */
43 { "idname BBoard", -6 }, /* .. */
45 { "prompt string", 0 },
53 { "vmhwrite fd", -8 },
57 { "popwrite fd", -8 },
69 static int mbx_style = MMDF_FORMAT;
74 char*fmsh = NULL; /* folder instead of file */
75 int modified; /* command modified folder */
76 struct msgs *mp; /* used a lot */
78 struct Msg *Msgs = NULL; /* Msgs[0] not used */
79 static FILE *fp; /* input file */
80 static FILE *yp = NULL; /* temporary file */
81 static int mode; /* mode of file */
82 static int numfds = 0; /* number of files cached */
83 static int maxfds = 0; /* number of files cached to be cached */
84 static time_t mtime = (time_t) 0; /* mtime of file */
89 #define ALARM ((unsigned int) 10)
90 #define ttyN(c) ttyNaux ((c), NULL)
94 static int vmhpid = OK;
99 static int vmhtty = NOTOK;
106 static int topcur = 0;
108 static int numwins = 0;
109 static int windows[NWIN + 1];
111 static jmp_buf peerenv;
116 static int pfd = NOTOK; /* fd parent is reading from */
117 static int ppid = 0; /* pid of parent */
122 int interactive; /* running from a /dev/tty */
123 int redirected; /* re-directing output */
124 FILE *sp = NULL; /* original stdout */
126 char *cmd_name; /* command being run */
127 char myfilter[BUFSIZ]; /* path to mhl.forward */
129 static char *myprompt = "(%s) ";/* prompting string */
134 static int gap; /* gap in BBoard-ID:s */
135 static char *myname = NULL; /* BBoard name */
136 char *BBoard_ID = "BBoard-ID"; /* BBoard-ID constant */
141 SIGNAL_HANDLER istat; /* original SIGINT */
142 static SIGNAL_HANDLER pstat; /* current SIGPIPE */
143 SIGNAL_HANDLER qstat; /* original SIGQUIT */
146 SIGNAL_HANDLER tstat; /* original SIGTSTP */
149 int interrupted; /* SIGINT detected */
150 int broken_pipe; /* SIGPIPE detected */
151 int told_to_quit; /* SIGQUIT detected */
156 void fsetup (char *);
160 void display_info (int);
163 void seq_setcur (struct msgs *, int);
164 void padios (char *, char *, ...);
165 void padvise (char *, char *, ...);
171 static void msh (int);
172 static int read_map (char *, long);
173 static int read_file (long, int);
175 static void m_gMsgs (int);
176 static int check_folder (int);
177 static void scanrange (int, int);
178 static void scanstring (char *);
179 static void write_ids (void);
180 static void quit (void);
181 static int getargs (char *, struct swit *, struct Cmd *);
182 static int getcmds (struct swit *, struct Cmd *, int);
183 static int parse (char *, struct Cmd *);
184 static int init_io (struct Cmd *, int);
185 static int initaux_io (struct Cmd *);
186 static void fin_io (struct Cmd *, int);
187 static void finaux_io (struct Cmd *);
188 static void m_init (void);
189 static void intrser (int);
190 static void pipeser (int);
191 static void quitser (int);
192 static void alrmser (int);
193 static int pINI (void);
194 static int pQRY (char *, int);
195 static int pQRY1 (int);
196 static int pQRY2 (void);
197 static int pCMD (char *, struct swit *, struct Cmd *);
198 static int pFIN (void);
199 static int peerwait (void);
200 static int ttyNaux (struct Cmd *, char *);
201 static int ttyR (struct Cmd *);
202 static int winN (struct Cmd *, int, int);
203 static int winR (struct Cmd *);
204 static int winX (int);
208 main (int argc, char **argv)
210 int id = 0, scansw = 0, vmh1 = 0, vmh2 = 0;
211 char *cp, *file = NULL, *folder = NULL;
212 char **argp, **arguments, buf[BUFSIZ];
215 setlocale(LC_ALL, "");
217 invo_name = r1bindex (argv[0], '/');
219 /* read user profile/context */
222 mts_init (invo_name);
223 arguments = getarguments (invo_name, argc,argv, 1);
226 while ((cp = *argp++)) {
228 switch (smatch (++cp, switches)) {
230 ambigsw (cp, switches);
233 adios (NULL, "-%s unknown", cp);
236 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
237 print_help (buf, switches, 1);
240 print_version(invo_name);
244 if (!(cp = *argp++) || *cp == '-')
245 adios (NULL, "missing argument to %s", argp[-2]);
246 if ((id = atoi (cp)) < 1)
247 adios (NULL, "bad argument %s %s", argp[-2], cp);
250 if (!(cp = *argp++) || *cp == '-')
251 adios (NULL, "missing argument to %s", argp[-2]);
252 if ((pfd = atoi (cp)) <= 1)
253 adios (NULL, "bad argument %s %s", argp[-2], cp);
256 if (!(cp = *argp++) || *cp == '-')
257 adios (NULL, "missing argument to %s", argp[-2]);
258 if ((ppid = atoi (cp)) <= 1)
259 adios (NULL, "bad argument %s %s", argp[-2], cp);
262 if (!(myname = *argp++) || *myname == '-')
263 adios (NULL, "missing argument to %s", argp[-2]);
274 if (!(myprompt = *argp++) || *myprompt == '-')
275 adios (NULL, "missing argument to %s", argp[-2]);
279 if (!(cp = *argp++) || *cp == '-')
280 adios (NULL, "missing argument to %s", argp[-2]);
281 if ((vmh1 = atoi (cp)) < 1)
282 adios (NULL, "bad argument %s %s", argp[-2], cp);
285 if (!(cp = *argp++) || *cp == '-')
286 adios (NULL, "missing argument to %s", argp[-2]);
287 if ((vmh2 = atoi (cp)) < 1)
288 adios (NULL, "bad argument %s %s", argp[-2], cp);
292 if (!(cp = *argp++) || *cp == '-')
293 adios (NULL, "missing argument to %s", argp[-2]);
296 if (!(cp = *argp++) || *cp == '-')
297 adios (NULL, "missing argument to %s", argp[-2]);
307 if (*cp == '+' || *cp == '@') {
309 adios (NULL, "only one folder at a time!");
311 folder = pluspath (cp);
315 adios (NULL, "only one file at a time!");
320 if (!file && !folder)
323 adios (NULL, "use a file or a folder, not both");
324 strncpy (myfilter, etcpath (mhlforward), sizeof(myfilter));
327 ioctl (pfd, FIOCLEX, NULL);
330 istat = SIGNAL2 (SIGINT, intrser);
331 qstat = SIGNAL2 (SIGQUIT, quitser);
333 sc_width (); /* MAGIC... */
335 if ((vmh = vmh1 && vmh2)) {
338 SIGNAL (SIGINT, SIG_IGN);
339 SIGNAL (SIGQUIT, SIG_IGN);
341 tstat = SIGNAL (SIGTSTP, SIG_IGN);
350 display_info (id > 0 ? scansw : 0);
352 msh (id > 0 ? scansw : 0);
361 static struct swit mshcmds[] = {
426 register char *cp, **ap;
427 char prompt[BUFSIZ], *vec[MAXARGS];
429 register struct Cmd *cmdp;
430 static int once_only = ADVCMD;
432 snprintf (prompt, sizeof(prompt), myprompt, invo_name);
441 if ((i = getcmds (mshcmds, cmdp, scansw)) == EOF) {
446 check_folder (scansw);
447 if ((i = getargs (prompt, mshcmds, cmdp)) == EOF) {
452 cmd_name = mshcmds[i].sw;
460 if (once_only == ADVCMD)
461 once_only = i = SHOWCMD;
463 i = mp->curmsg != mp->hghmsg ? NEXTCMD : EXITCMD;
464 cmd_name = mshcmds[i].sw;
470 case FORWCMD: /* sigh */
480 if ((cp = context_find (cmd_name))) {
482 ap = brkstring (cp, " ", "\n");
483 ap = copyip (ap, vec, MAXARGS);
494 copyip (cmdp->args + 1, ap, MAXARGS);
498 if (!vmh && init_io (cmdp, vmh) == NOTOK) {
504 redirected = vmh || cmdp->direction != STDIO;
515 if (!vmh || ttyN (cmdp) != NOTOK)
516 forkcmd (vec, cmd_name);
520 if (!vmh || ttyN (cmdp) != NOTOK)
525 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
531 || (filehak (vec) == OK ? ttyN (cmdp)
532 : winN (cmdp, DISPLAY, 1)) != NOTOK)
537 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
542 if (!vmh || ttyN (cmdp) != NOTOK)
547 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
553 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
558 if (!vmh || ttyN (cmdp) != NOTOK)
565 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
571 || (packhak (vec) == OK ? ttyN (cmdp)
572 : winN (cmdp, DISPLAY, 1)) != NOTOK)
577 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
582 if (!vmh || ttyN (cmdp) != NOTOK)
587 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
592 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
597 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
602 padios (NULL, "no dispatch for %s", cmd_name);
624 fsetup (char *folder)
630 maildir = m_maildir (folder);
631 if (chdir (maildir) == NOTOK)
632 padios (maildir, "unable to change directory to");
634 /* read folder and create message structure */
635 if (!(mp = folder_read (folder)))
636 padios (NULL, "unable to read folder %s", folder);
638 /* check for empty folder */
640 padios (NULL, "no messages in %s", folder);
643 mtime = stat (mp->foldpath, &st) != NOTOK ? st.st_mtime : 0;
645 m_gMsgs (mp->hghmsg);
647 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++) {
648 Msgs[msgnum].m_bboard_id = 0;
649 Msgs[msgnum].m_top = NOTOK;
650 Msgs[msgnum].m_start = Msgs[msgnum].m_stop = 0L;
651 Msgs[msgnum].m_scanl = NULL;
656 fmsh = getcpy (folder);
658 maxfds = OPEN_MAX / 2;
660 if ((maxfds -= 2) < 1)
670 if ((fp = fopen (file, "r")) == NULL)
671 padios (file, "unable to read");
673 ioctl (fileno (fp), FIOCLEX, NULL);
675 if (fstat (fileno (fp), &st) != NOTOK) {
676 mode = (int) (st.st_mode & 0777), mtime = st.st_mtime;
677 msgp = read_map (file, (long) st.st_size);
680 mode = m_gmprot (), mtime = 0;
684 if ((msgp = read_file (msgp ? Msgs[msgp].m_stop : 0L, msgp + 1)) < 1)
685 padios (NULL, "no messages in %s", myname ? myname : file);
687 if (!(mp = (struct msgs *) calloc ((size_t) 1, sizeof(*mp))))
688 padios (NULL, "unable to allocate folder storage");
690 if (!(mp->msgstats = calloc ((size_t) msgp + 3, sizeof(*(mp->msgstats)))))
691 padios (NULL, "unable to allocate message status storage");
697 mp->foldpath = getcpy (myname ? myname : file);
698 clear_folder_flags (mp);
701 if (st.st_uid != getuid () || access (file, W_OK) == NOTOK)
705 mp->hghoff = mp->hghmsg + 1;
707 for (i = mp->lowmsg; i <= mp->hghmsg; i++) {
708 clear_msg_flags (mp, i);
713 mp->msgattrs[0] = getcpy ("unseen");
714 mp->msgattrs[1] = NULL;
716 m_unknown (fp); /* the MAGIC invocation */
725 read_map (char *file, long size)
727 register int i, msgp;
728 register struct drop *dp, *mp;
731 if ((i = map_read (file, size, &rp, 1)) == 0)
737 for (dp = rp + 1; i-- > 0; msgp++, dp++) {
738 mp = &Msgs[msgp].m_drop;
740 mp->d_size = dp->d_size;
741 mp->d_start = dp->d_start;
742 mp->d_stop = dp->d_stop;
743 Msgs[msgp].m_scanl = NULL;
752 read_file (long pos, int msgp)
755 register struct drop *dp, *mp;
758 if ((i = mbx_read (fp, pos, &rp, 1)) <= 0)
761 m_gMsgs ((msgp - 1) + i);
763 for (dp = rp; i-- > 0; msgp++, dp++) {
764 mp = &Msgs[msgp].m_drop;
766 mp->d_size = dp->d_size;
767 mp->d_start = dp->d_start;
768 mp->d_stop = dp->d_stop;
769 Msgs[msgp].m_scanl = NULL;
783 nMsgs = n + MAXFOLDER / 2;
784 Msgs = (struct Msg *) calloc ((size_t) (nMsgs + 2), sizeof *Msgs);
786 padios (NULL, "unable to allocate Msgs structure");
793 nmsgs = nMsgs + n + MAXFOLDER / 2;
794 Msgs = (struct Msg *) mh_xrealloc ((char *) Msgs, (size_t) (nmsgs + 2) * sizeof *Msgs);
795 memset((char *) (Msgs + nMsgs + 2), 0, (size_t) ((nmsgs - nMsgs) * sizeof *Msgs));
802 msh_ready (int msgnum, int full)
815 if ((fd = Msgs[msgnum].m_top) == NOTOK) {
816 if (numfds >= maxfds)
817 for (msgp = mp->lowmsg; msgp <= mp->hghmsg; msgp++)
818 if (Msgs[msgp].m_top != NOTOK) {
819 close (Msgs[msgp].m_top);
820 Msgs[msgp].m_top = NOTOK;
825 if ((fd = open (cp = m_name (msgnum), O_RDONLY)) == NOTOK)
826 padios (cp, "unable to open message");
827 Msgs[msgnum].m_top = fd;
831 if ((fd = dup (fd)) == NOTOK)
832 padios ("cached message", "unable to dup");
833 if ((yp = fdopen (fd, "r")) == NULL)
834 padios (NULL, "unable to fdopen cached message");
835 fseek (yp, 0L, SEEK_SET);
839 m_eomsbr ((int (*)()) 0); /* XXX */
840 fseek (fp, Msgs[msgnum].m_start, SEEK_SET);
846 check_folder (int scansw)
848 int seqnum, i, low, hgh, msgp;
852 if (stat (mp->foldpath, &st) == NOTOK)
853 padios (mp->foldpath, "unable to stat");
854 if (mtime == st.st_mtime)
858 low = mp->hghmsg + 1;
859 folder_free (mp); /* free folder/message structure */
861 if (!(mp = folder_read (fmsh)))
862 padios (NULL, "unable to re-read folder %s", fmsh);
866 for (msgp = mp->lowmsg; msgp <= mp->hghmsg; msgp++) {
867 if (Msgs[msgp].m_top != NOTOK) {
868 close (Msgs[msgp].m_top);
869 Msgs[msgp].m_top = NOTOK;
872 if (Msgs[msgp].m_scanl) {
873 free (Msgs[msgp].m_scanl);
874 Msgs[msgp].m_scanl = NULL;
880 if (modified || low > hgh)
884 if (fstat (fileno (fp), &st) == NOTOK)
885 padios (mp->foldpath, "unable to fstat");
886 if (mtime == st.st_mtime)
888 mode = (int) (st.st_mode & 0777);
891 if ((msgp = read_file (Msgs[mp->hghmsg].m_stop, mp->hghmsg + 1)) < 1)
892 padios (NULL, "no messages in %s", mp->foldpath); /* XXX */
893 if (msgp >= MAXFOLDER)
894 padios (NULL, "more than %d messages in %s", MAXFOLDER,
896 if (msgp <= mp->hghmsg)
899 if (!(mp = folder_realloc (mp, mp->lowoff, msgp)))
900 padios (NULL, "unable to allocate folder storage");
902 low = mp->hghmsg + 1, hgh = msgp;
903 seqnum = scansw ? seq_getnum (mp, "unseen") : -1;
904 for (i = mp->hghmsg + 1; i <= msgp; i++) {
907 add_sequence(mp, seqnum, i);
917 advise (NULL, "new messages have arrived!\007");
919 scanrange (low, hgh);
926 scanrange (int low, int hgh)
930 snprintf (buffer, sizeof(buffer), "%d-%d", low, hgh);
936 scanstring (char *arg)
938 char *cp, **ap, *vec[MAXARGS];
941 * This should be replace with a call to getarguments()
943 if ((cp = context_find (cmd_name = "scan"))) {
945 ap = brkstring (cp, " ", "\n");
946 ap = copyip (ap, vec, MAXARGS);
962 register int cur, seqnum, i=0, msgnum;
965 seq_setcur (mp, mp->lowmsg);
966 if (id <= 0 || (seqnum = seq_getnum (mp, "unseen")) == -1)
969 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
970 add_sequence(mp, seqnum, msgnum);
975 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
976 if (does_exist(mp, msgnum)) /* FIX */
977 if ((i = readid (msgnum)) > 0 && i < id) {
979 clear_sequence(mp, seqnum, msgnum);
982 for (i = mp->lowmsg; i < msgnum; i++)
983 clear_sequence(mp, seqnum, i);
985 if (cur > mp->hghmsg)
988 seq_setcur (mp, cur);
991 if ((gap = 1 < id && id < (i = readid (mp->lowmsg)) ? id : 0) && !vmh)
992 advise (NULL, "gap in ID:s, last seen %d, lowest present %d\n",
1001 char *bp, buf[BUFSIZ], name[NAMESZ];
1004 if (Msgs[msgnum].m_bboard_id)
1005 return Msgs[msgnum].m_bboard_id;
1007 zp = msh_ready (msgnum, 0);
1009 switch (state = m_getfld (state, name, buf, sizeof(buf), zp)) {
1013 if (!mh_strcasecmp (name, BBoard_ID)) {
1015 while (state == FLDPLUS) {
1016 state = m_getfld (state, name, buf, sizeof(buf), zp);
1022 return (Msgs[msgnum].m_bboard_id = i);
1026 while (state == FLDPLUS)
1027 state = m_getfld (state, name, buf, sizeof(buf), zp);
1028 if (state != FLDEOF)
1038 display_info (int scansw)
1042 interactive = isatty (fileno (stdout));
1044 if ((sd = dup (fileno (stdout))) == NOTOK)
1045 padios ("standard output", "unable to dup");
1047 ioctl (sd, FIOCLEX, NULL);
1048 #endif /* FIOCLEX */
1049 if ((sp = fdopen (sd, "w")) == NULL)
1050 padios ("standard output", "unable to fdopen");
1053 m_putenv ("mhfolder", mp->foldpath);
1058 printf ("Reading ");
1059 if (SOprintf ("%s", myname))
1060 printf ("%s", myname);
1061 printf (", currently at message %d of %d\n",
1062 mp->curmsg, mp->hghmsg);
1065 printf ("Reading ");
1067 printf ("+%s", fmsh);
1069 printf ("%s", mp->foldpath);
1070 printf (", currently at message %d of %d\n",
1071 mp->curmsg, mp->hghmsg);
1074 if (((seqnum = seq_getnum (mp, "unseen")) != -1)
1076 && in_sequence(mp, seqnum, mp->hghmsg))
1077 scanstring ("unseen");
1084 int i = 0, seqnum, msgnum;
1090 if ((seqnum = seq_getnum (mp, "unseen")) != -1)
1091 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
1092 if (!in_sequence(mp, seqnum, msgnum)) {
1093 if (Msgs[msgnum].m_bboard_id == 0)
1095 if ((i = Msgs[msgnum].m_bboard_id) > 0)
1099 snprintf (buffer, sizeof(buffer), "%d %d\n", i, Msgs[mp->hghmsg].m_bboard_id);
1100 write (pfd, buffer, sizeof(buffer));
1110 char *cp, tmpfil[BUFSIZ];
1111 char map1[BUFSIZ], map2[BUFSIZ];
1115 if (!(mp->msgflags & MODIFIED) || is_readonly(mp) || fmsh) {
1117 rc2peer (RC_FIN, 0, NULL);
1122 ttyNaux (NULLCMD, "FAST");
1124 if ((dp = lkfopen (mp->foldpath, "r")) == NULL) {
1125 advise (mp->foldpath, "unable to lock");
1132 if (fstat (fileno (dp), &st) == NOTOK) {
1133 advise (mp->foldpath, "unable to stat");
1136 if (mtime != st.st_mtime) {
1137 advise (NULL, "new messages have arrived, no update");
1140 mode = (int) (st.st_mode & 0777);
1142 if (mp->nummsg == 0) {
1143 cp = concat ("Zero file \"", mp->foldpath, "\"? ", NULL);
1144 if (getanswer (cp)) {
1145 if ((i = creat (mp->foldpath, mode)) != NOTOK)
1148 advise (mp->foldpath, "error zero'ing");
1149 unlink (map_name (mp->foldpath));/* XXX */
1154 cp = concat ("Update file \"", mp->foldpath, "\"? ", NULL);
1155 if (!getanswer (cp))
1157 strncpy (tmpfil, m_backup (mp->foldpath), sizeof(tmpfil));
1158 if ((md = mbx_open (tmpfil, mbx_style, st.st_uid, st.st_gid, mode)) == NOTOK) {
1159 advise (tmpfil, "unable to open");
1163 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
1164 if (does_exist(mp, msgnum) && pack (tmpfil, md, msgnum) == NOTOK) {
1165 mbx_close (tmpfil, md);
1167 unlink (map_name (tmpfil));
1170 mbx_close (tmpfil, md);
1172 if (rename (tmpfil, mp->foldpath) == NOTOK)
1173 admonish (mp->foldpath, "unable to rename %s to", tmpfil);
1175 strncpy (map1, map_name (tmpfil), sizeof(map1));
1176 strncpy (map2, map_name (mp->foldpath), sizeof(map2));
1178 if (rename (map1, map2) == NOTOK) {
1179 admonish (map2, "unable to rename %s to", map1);
1188 lkfclose (dp, mp->foldpath);
1197 getargs (char *prompt, struct swit *sw, struct Cmd *cmdp)
1201 static char buffer[BUFSIZ];
1207 printf ("%s", prompt);
1210 for (cp = buffer; (i = getchar ()) != '\n';) {
1211 if (interrupted && !told_to_quit) {
1216 if (told_to_quit || i == EOF) {
1219 kill (ppid, SIGEMT);
1221 kill (ppid, SIGTERM);
1225 if (cp < &buffer[sizeof buffer - 2])
1232 if (buffer[0] == '?') {
1233 printf ("commands:\n");
1234 print_sw (ALL, sw, "", stdout);
1235 printf ("type CTRL-D or use ``quit'' to leave %s\n",
1240 if (parse (buffer, cmdp) == NOTOK)
1243 switch (i = smatch (cmdp->args[0], sw)) {
1245 ambigsw (cmdp->args[0], sw);
1248 printf ("say what: ``%s'' -- type ? (or help) for help\n",
1259 getcmds (struct swit *sw, struct Cmd *cmdp, int scansw)
1262 struct record rcs, *rc;
1268 switch (peer2rc (rc)) {
1270 pQRY (rc->rc_data, scansw);
1274 if ((i = pCMD (rc->rc_data, sw, cmdp)) != NOTOK)
1281 kill (ppid, SIGEMT);
1283 kill (ppid, SIGTERM);
1288 padios (NULL, "%s", rc->rc_data);
1291 fmt2peer (RC_ERR, "pLOOP protocol screw-up");
1298 parse (char *buffer, struct Cmd *cmdp)
1301 unsigned char c, *cp;
1305 pp = cmdp->args[argp++] = cmdp->line;
1306 cmdp->redirect = NULL;
1307 cmdp->direction = STDIO;
1308 cmdp->stream = NULL;
1310 for (cp = buffer; (c = *cp); cp++) {
1316 fmt2peer (RC_EOF, "null command");
1320 while ((c = *cp++)) {
1327 cmdp->args[argp++] = pp;
1334 switch (c = *cp++) {
1336 padvise (NULL, "unmatched \"");
1341 if ((c = *cp++) == 0)
1352 if ((c = *cp++) == 0) {
1354 padvise (NULL, "the newline character can not be quoted");
1364 if (pp == cmdp->line) {
1365 padvise (NULL, "invalid null command");
1368 if (*cmdp->args[argp - 1] == 0)
1370 cmdp->direction = c == '>' ? CRTIO : PIPIO;
1371 if (cmdp->direction == CRTIO && (c = *cp) == '>') {
1372 cmdp->direction = APPIO;
1375 cmdp->redirect = pp + 1;/* sigh */
1376 for (; (c = *cp); cp++)
1380 padvise (NULL, cmdp->direction != PIPIO
1381 ? "missing name for redirect"
1382 : "invalid null command");
1385 strcpy (cmdp->redirect, cp);
1386 if (cmdp->direction != PIPIO) {
1388 if (isspace (*cp)) {
1389 padvise (NULL, "bad name for redirect");
1392 if (expand (cmdp->redirect) == NOTOK)
1401 cmdp->args[argp] = NULL;
1408 expand (char *redirect)
1414 if (*redirect != '~')
1417 if ((cp = strchr(pp = redirect + 1, '/')))
1422 if ((pw = getpwnam (pp)))
1425 padvise (NULL, "unknown user: %s", pp);
1429 snprintf (path, sizeof(path), "%s/%s", pp, cp ? cp : "");
1430 strcpy (redirect, path);
1436 init_io (struct Cmd *cmdp, int vio)
1443 result = initaux_io (cmdp);
1451 initaux_io (struct Cmd *cmdp)
1455 switch (cmdp->direction) {
1461 mode = cmdp->direction == CRTIO ? "write" : "append";
1462 if ((cmdp->stream = fopen (cmdp->redirect, mode)) == NULL) {
1463 padvise (cmdp->redirect, "unable to %s ", mode);
1464 cmdp->direction = STDIO;
1470 if ((cmdp->stream = popen (cmdp->redirect, "w")) == NULL) {
1471 padvise (cmdp->redirect, "unable to pipe");
1472 cmdp->direction = STDIO;
1475 SIGNAL (SIGPIPE, pipeser);
1480 padios (NULL, "unknown redirection for command");
1484 if (dup2 (fileno (cmdp->stream), fileno (stdout)) == NOTOK)
1485 padios ("standard output", "unable to dup2");
1493 fin_io (struct Cmd *cmdp, int vio)
1505 finaux_io (struct Cmd *cmdp)
1507 switch (cmdp->direction) {
1514 close (fileno (stdout));
1515 if (ferror (stdout))
1516 padvise (NULL, "problems writing %s", cmdp->redirect);
1517 fclose (cmdp->stream);
1522 close (fileno (stdout));
1523 pclose (cmdp->stream);
1524 SIGNAL (SIGPIPE, SIG_DFL);
1528 padios (NULL, "unknown redirection for command");
1531 if (dup2 (fileno (sp), fileno (stdout)) == NOTOK)
1532 padios ("standard output", "unable to dup2");
1535 cmdp->direction = STDIO;
1544 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
1545 unset_selected (mp, msgnum);
1546 mp->lowsel = mp->hghsel = mp->numsel = 0;
1554 folder_free (mp); /* free folder/message structure */
1560 seq_setcur (struct msgs *mp, int msgnum)
1562 if (mp->curmsg == msgnum)
1565 if (mp->curmsg && Msgs[mp->curmsg].m_scanl) {
1566 free (Msgs[mp->curmsg].m_scanl);
1567 Msgs[mp->curmsg].m_scanl = NULL;
1569 if (Msgs[msgnum].m_scanl) {
1570 free (Msgs[msgnum].m_scanl);
1571 Msgs[msgnum].m_scanl = NULL;
1574 mp->curmsg = msgnum;
1592 if (broken_pipe++ == 0)
1593 fprintf (stderr, "broken pipe\n");
1612 longjmp (peerenv, DONE);
1621 struct record rcs, *rc;
1626 switch (peer2rc (rc)) {
1629 while (isspace (*bp))
1631 if (sscanf (bp, "%d", &vrsn) != 1) {
1633 fmt2peer (RC_ERR, "bad init \"%s\"", rc->rc_data);
1636 if (vrsn != RC_VRSN) {
1637 fmt2peer (RC_ERR, "version %d unsupported", vrsn);
1641 while (*bp && !isspace (*bp))
1643 while (isspace (*bp))
1645 if (sscanf (bp, "%d", &numwins) != 1 || numwins <= 0)
1650 for (i = 1; i <= numwins; i++) {
1651 while (*bp && !isspace (*bp))
1653 while (isspace (*bp))
1655 if (sscanf (bp, "%d", &windows[i]) != 1 || windows[i] <= 0)
1658 rc2peer (RC_ACK, 0, NULL);
1662 padios (NULL, "%s", rc->rc_data);
1665 fmt2peer (RC_ERR, "pINI protocol screw-up");
1666 done (1); /* NOTREACHED */
1669 return 1; /* dead code to satisfy the compiler */
1674 pQRY (char *str, int scansw)
1677 if (pQRY1 (scansw) == NOTOK || pQRY2 () == NOTOK)
1680 rc2peer (RC_EOF, 0, NULL);
1689 static int lastlow = 0,
1694 oldhgh = mp->hghmsg;
1695 if (check_folder (scansw) && oldhgh < mp->hghmsg) {
1696 switch (winX (STATUS)) {
1701 printf ("new messages have arrived!");
1704 _exit (0); /* NOTREACHED */
1707 lastlow = lastcur = lasthgh = lastnum = 0;
1711 switch (winX (DISPLAY)) {
1716 scanrange (oldhgh + 1, mp->hghmsg);
1719 _exit (0); /* NOTREACHED */
1728 switch (winX (STATUS)) {
1733 printf ("%s: gap in ID:s, last seen %d, lowest present %d\n",
1734 myname ? myname : fmsh ? fmsh : mp->foldpath, gap - 1,
1735 readid (mp->lowmsg));
1738 _exit (0); /* NOTREACHED */
1745 if (mp->lowmsg != lastlow
1746 || mp->curmsg != lastcur
1747 || mp->hghmsg != lasthgh
1748 || mp->nummsg != lastnum)
1749 switch (winX (STATUS)) {
1757 _exit (0); /* NOTREACHED */
1760 lastlow = mp->lowmsg;
1761 lastcur = mp->curmsg;
1762 lasthgh = mp->hghmsg;
1763 lastnum = mp->nummsg;
1774 int i, j, k, msgnum, n;
1780 if (mp->nummsg == 0 && mp->nummsg != num)
1781 switch (winX (SCAN)) {
1789 _exit (0); /* NOTREACHED */
1798 j = (k = windows[SCAN]) / 2;
1799 for (msgnum = mp->curmsg; msgnum <= mp->hghmsg; msgnum++)
1800 if (does_exist (mp, msgnum))
1804 k = i >= k ? 1 : k - i;
1811 for (msgnum = mp->curmsg; msgnum >= mp->lowmsg; msgnum--)
1812 if (does_exist (mp, msgnum)) {
1819 for (msgnum = mp->curmsg + 1; msgnum <= mp->hghmsg; msgnum++)
1820 if (does_exist (mp, msgnum)) {
1824 if (n++ >= windows[SCAN])
1830 && does_exist (mp, lo)
1831 && does_exist (mp, hi)
1833 || (lo == mp->curmsg && lo == mp->lowmsg))
1835 || (hi == mp->curmsg && hi == mp->hghmsg))
1836 && hi - lo == j - i)
1839 if (mp->curmsg != cur || modified)
1840 switch (winN (NULLCMD, SCAN, 0)) {
1848 scanrange (lo = i, hi = j);
1859 pCMD (char *str, struct swit *sw, struct Cmd *cmdp)
1864 switch (winX (DISPLAY)) {
1869 printf ("commands:\n");
1870 print_sw (ALL, sw, "", stdout);
1871 printf ("type ``quit'' to leave %s\n", invo_name);
1874 _exit (0); /* NOTREACHED */
1877 rc2peer (RC_EOF, 0, NULL);
1881 if (parse (str, cmdp) == NOTOK)
1884 switch (i = smatch (cmdp->args[0], sw)) {
1886 switch (winX (DISPLAY)) {
1891 ambigsw (cmdp->args[0], sw);
1894 _exit (0); /* NOTREACHED */
1897 rc2peer (RC_EOF, 0, NULL);
1903 "say what: ``%s'' -- type ? (or help) for help",
1918 switch (setjmp (peerenv)) {
1920 SIGNAL (SIGALRM, alrmser);
1923 status = peerwait ();
1937 struct record rcs, *rc;
1942 switch (peer2rc (rc)) {
1945 rc2peer (RC_FIN, 0, NULL);
1949 advise (NULL, "%s", rc->rc_data);
1953 fmt2peer (RC_FIN, "pLOOP protocol screw-up");
1960 ttyNaux (struct Cmd *cmdp, char *s)
1962 struct record rcs, *rc;
1967 if (cmdp && init_io (cmdp, vmh) == NOTOK)
1970 /* XXX: fseek() too tricky for our own good */
1972 fseek (fp, 0L, SEEK_SET);
1975 switch (rc2rc (RC_TTY, s ? strlen (s) : 0, s, rc)) {
1977 vmhtty = OK; /* fall */
1982 padios (NULL, "%s", rc->rc_data);/* NOTREACHED */
1985 fmt2peer (RC_ERR, "pTTY protocol screw-up");
1986 done (1); /* NOTREACHED */
1990 SIGNAL (SIGTSTP, tstat);
1997 ttyR (struct Cmd *cmdp)
1999 struct record rcs, *rc;
2004 SIGNAL (SIGTSTP, SIG_IGN);
2016 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2018 rc2peer (RC_EOF, 0, NULL);
2022 padios (NULL, "%s", rc->rc_data);/* NOTREACHED */
2025 fmt2peer (RC_ERR, "pTTY protocol screw-up");
2026 done (1); /* NOTREACHED */
2029 return 1; /* dead code to satisfy compiler */
2034 winN (struct Cmd *cmdp, int n, int eof)
2037 char buffer[BUFSIZ];
2038 struct record rcs, *rc;
2041 if (vmhpid == NOTOK)
2046 /* XXX: fseek() too tricky for our own good */
2048 fseek (fp, 0L, SEEK_SET);
2052 snprintf (buffer, sizeof(buffer), "%d", n);
2053 switch (str2rc (RC_WIN, buffer, rc)) {
2061 padios (NULL, "%s", rc->rc_data);
2064 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2068 if (pipe (pd) == NOTOK) {
2069 err2peer (RC_ERR, "pipe", "unable to");
2073 switch (vmhpid = fork()) {
2075 err2peer (RC_ERR, "fork", "unable to");
2082 SIGNAL (SIGPIPE, SIG_IGN);
2083 while ((i = read (pd[0], buffer, sizeof buffer)) > 0)
2084 switch (rc2rc (RC_DATA, i, buffer, rc)) {
2092 advise (NULL, "%s", rc->rc_data);
2096 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2100 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2103 rc2peer (RC_EOF, 0, NULL);
2108 advise (NULL, "%s", rc->rc_data);
2113 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2118 err2peer (RC_ERR, "pipe", "error reading from");
2120 _exit (i != NOTOK ? i : 1);
2123 if ((vmhfd0 = dup (fileno (stdin))) == NOTOK)
2124 padios ("standard input", "unable to dup");
2125 if ((vmhfd1 = dup (fileno (stdout))) == NOTOK)
2126 padios ("standard output", "unable to dup");
2127 if ((vmhfd2 = dup (fileno (stderr))) == NOTOK)
2128 padios ("diagnostic output", "unable to dup");
2131 if ((i = open ("/dev/null", O_RDONLY)) != NOTOK && i != fileno (stdin)) {
2132 dup2 (i, fileno (stdin));
2137 if (dup2 (pd[1], fileno (stdout)) == NOTOK)
2138 padios ("standard output", "unable to dup2");
2142 if (dup2 (pd[1], fileno (stderr)) == NOTOK)
2143 padios ("diagnostic output", "unable to dup2");
2146 if (cmdp && init_io (cmdp, 0) == NOTOK)
2148 pstat = SIGNAL (SIGPIPE, pipeser);
2160 winR (struct Cmd *cmdp)
2170 if (dup2 (vmhfd0, fileno (stdin)) == NOTOK)
2171 padios ("standard input", "unable to dup2");
2176 if (dup2 (vmhfd1, fileno (stdout)) == NOTOK)
2177 padios ("standard output", "unable to dup2");
2182 if (dup2 (vmhfd2, fileno (stderr)) == NOTOK)
2183 padios ("diagnostic output", "unable to dup2");
2187 SIGNAL (SIGPIPE, pstat);
2189 if ((status = pidwait (vmhpid, OK)) == 2)
2193 return (status == 0 ? OK : NOTOK);
2201 char buffer[BUFSIZ];
2202 struct record rcs, *rc;
2207 /* XXX: fseek() too tricky for our own good */
2209 fseek (fp, 0L, SEEK_SET);
2211 snprintf (buffer, sizeof(buffer), "%d", n);
2212 switch (str2rc (RC_WIN, buffer, rc)) {
2220 padios (NULL, "%s", rc->rc_data);
2223 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2227 if (pipe (pd) == NOTOK) {
2228 err2peer (RC_ERR, "pipe", "unable to");
2232 switch (pid = fork ()) {
2234 err2peer (RC_ERR, "fork", "unable to");
2240 close (fileno (stdin));
2241 if ((i = open ("/dev/null", O_RDONLY)) != NOTOK && i != fileno (stdin)) {
2242 dup2 (i, fileno (stdin));
2245 dup2 (pd[1], fileno (stdout));
2246 dup2 (pd[1], fileno (stderr));
2254 while ((i = read (pd[0], buffer, sizeof buffer)) > 0)
2255 switch (rc2rc (RC_DATA, i, buffer, rc)) {
2265 padios (NULL, "%s", rc->rc_data);
2268 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2272 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2277 padios (NULL, "%s", rc->rc_data);
2280 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2284 err2peer (RC_ERR, "pipe", "error reading from");
2288 return (i != NOTOK ? pid : NOTOK);
2294 padios (char *what, char *fmt, ...)
2300 verr2peer (RC_FIN, what, fmt, ap);
2303 advertise (what, NULL, fmt, ap);
2312 padvise (char *what, char *fmt, ...)
2318 verr2peer (RC_ERR, what, fmt, ap);
2320 advertise (what, NULL, fmt, ap);