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>
28 #include <h/m_setjmp.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 int SOprintf (char *, ...); /* from termsbr.c */
157 int sc_width (void); /* from termsbr.c */
158 void fsetup (char *);
160 FILE *msh_ready (int, int);
163 void display_info (int);
166 void seq_setcur (struct msgs *, int);
167 void padios (char *, char *, ...);
168 void padvise (char *, char *, ...);
174 static void msh (int);
175 static int read_map (char *, long);
176 static int read_file (long, int);
178 static void m_gMsgs (int);
179 FILE *msh_ready (int, int);
180 static int check_folder (int);
181 static void scanrange (int, int);
182 static void scanstring (char *);
183 static void write_ids (void);
184 static void quit (void);
185 static int getargs (char *, struct swit *, struct Cmd *);
186 static int getcmds (struct swit *, struct Cmd *, int);
187 static int parse (char *, struct Cmd *);
188 static int init_io (struct Cmd *, int);
189 static int initaux_io (struct Cmd *);
190 static void fin_io (struct Cmd *, int);
191 static void finaux_io (struct Cmd *);
192 static void m_init (void);
193 static void intrser (int);
194 static void pipeser (int);
195 static void quitser (int);
196 static void alrmser (int);
197 static int pINI (void);
198 static int pQRY (char *, int);
199 static int pQRY1 (int);
200 static int pQRY2 (void);
201 static int pCMD (char *, struct swit *, struct Cmd *);
202 static int pFIN (void);
203 static int peerwait (void);
204 static int ttyNaux (struct Cmd *, char *);
205 static int ttyR (struct Cmd *);
206 static int winN (struct Cmd *, int, int);
207 static int winR (struct Cmd *);
208 static int winX (int);
212 main (int argc, char **argv)
214 int id = 0, scansw = 0, vmh1 = 0, vmh2 = 0;
215 char *cp, *file = NULL, *folder = NULL;
216 char **argp, **arguments, buf[BUFSIZ];
219 setlocale(LC_ALL, "");
221 invo_name = r1bindex (argv[0], '/');
223 /* read user profile/context */
226 mts_init (invo_name);
227 arguments = getarguments (invo_name, argc,argv, 1);
230 while ((cp = *argp++)) {
232 switch (smatch (++cp, switches)) {
234 ambigsw (cp, switches);
237 adios (NULL, "-%s unknown", cp);
240 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
241 print_help (buf, switches, 1);
244 print_version(invo_name);
248 if (!(cp = *argp++) || *cp == '-')
249 adios (NULL, "missing argument to %s", argp[-2]);
250 if ((id = atoi (cp)) < 1)
251 adios (NULL, "bad argument %s %s", argp[-2], cp);
254 if (!(cp = *argp++) || *cp == '-')
255 adios (NULL, "missing argument to %s", argp[-2]);
256 if ((pfd = atoi (cp)) <= 1)
257 adios (NULL, "bad argument %s %s", argp[-2], cp);
260 if (!(cp = *argp++) || *cp == '-')
261 adios (NULL, "missing argument to %s", argp[-2]);
262 if ((ppid = atoi (cp)) <= 1)
263 adios (NULL, "bad argument %s %s", argp[-2], cp);
266 if (!(myname = *argp++) || *myname == '-')
267 adios (NULL, "missing argument to %s", argp[-2]);
278 if (!(myprompt = *argp++) || *myprompt == '-')
279 adios (NULL, "missing argument to %s", argp[-2]);
283 if (!(cp = *argp++) || *cp == '-')
284 adios (NULL, "missing argument to %s", argp[-2]);
285 if ((vmh1 = atoi (cp)) < 1)
286 adios (NULL, "bad argument %s %s", argp[-2], cp);
289 if (!(cp = *argp++) || *cp == '-')
290 adios (NULL, "missing argument to %s", argp[-2]);
291 if ((vmh2 = atoi (cp)) < 1)
292 adios (NULL, "bad argument %s %s", argp[-2], cp);
296 if (!(cp = *argp++) || *cp == '-')
297 adios (NULL, "missing argument to %s", argp[-2]);
300 if (!(cp = *argp++) || *cp == '-')
301 adios (NULL, "missing argument to %s", argp[-2]);
311 if (*cp == '+' || *cp == '@') {
313 adios (NULL, "only one folder at a time!");
315 folder = pluspath (cp);
319 adios (NULL, "only one file at a time!");
324 if (!file && !folder)
327 adios (NULL, "use a file or a folder, not both");
328 strncpy (myfilter, etcpath (mhlforward), sizeof(myfilter));
331 ioctl (pfd, FIOCLEX, NULL);
334 istat = SIGNAL2 (SIGINT, intrser);
335 qstat = SIGNAL2 (SIGQUIT, quitser);
337 sc_width (); /* MAGIC... */
339 if ((vmh = vmh1 && vmh2)) {
342 SIGNAL (SIGINT, SIG_IGN);
343 SIGNAL (SIGQUIT, SIG_IGN);
345 tstat = SIGNAL (SIGTSTP, SIG_IGN);
354 display_info (id > 0 ? scansw : 0);
356 msh (id > 0 ? scansw : 0);
365 static struct swit mshcmds[] = {
430 register char *cp, **ap;
431 char prompt[BUFSIZ], *vec[MAXARGS];
433 register struct Cmd *cmdp;
434 static int once_only = ADVCMD;
436 snprintf (prompt, sizeof(prompt), myprompt, invo_name);
445 if ((i = getcmds (mshcmds, cmdp, scansw)) == EOF) {
450 check_folder (scansw);
451 if ((i = getargs (prompt, mshcmds, cmdp)) == EOF) {
456 cmd_name = mshcmds[i].sw;
464 if (once_only == ADVCMD)
465 once_only = i = SHOWCMD;
467 i = mp->curmsg != mp->hghmsg ? NEXTCMD : EXITCMD;
468 cmd_name = mshcmds[i].sw;
474 case FORWCMD: /* sigh */
484 if ((cp = context_find (cmd_name))) {
486 ap = brkstring (cp, " ", "\n");
487 ap = copyip (ap, vec, MAXARGS);
498 copyip (cmdp->args + 1, ap, MAXARGS);
502 if (!vmh && init_io (cmdp, vmh) == NOTOK) {
508 redirected = vmh || cmdp->direction != STDIO;
519 if (!vmh || ttyN (cmdp) != NOTOK)
520 forkcmd (vec, cmd_name);
524 if (!vmh || ttyN (cmdp) != NOTOK)
529 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
535 || (filehak (vec) == OK ? ttyN (cmdp)
536 : winN (cmdp, DISPLAY, 1)) != NOTOK)
541 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
546 if (!vmh || ttyN (cmdp) != NOTOK)
551 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
557 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
562 if (!vmh || ttyN (cmdp) != NOTOK)
569 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
575 || (packhak (vec) == OK ? ttyN (cmdp)
576 : winN (cmdp, DISPLAY, 1)) != NOTOK)
581 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
586 if (!vmh || ttyN (cmdp) != NOTOK)
591 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
596 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
601 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
606 padios (NULL, "no dispatch for %s", cmd_name);
628 fsetup (char *folder)
634 maildir = m_maildir (folder);
635 if (chdir (maildir) == NOTOK)
636 padios (maildir, "unable to change directory to");
638 /* read folder and create message structure */
639 if (!(mp = folder_read (folder)))
640 padios (NULL, "unable to read folder %s", folder);
642 /* check for empty folder */
644 padios (NULL, "no messages in %s", folder);
647 mtime = stat (mp->foldpath, &st) != NOTOK ? st.st_mtime : 0;
649 m_gMsgs (mp->hghmsg);
651 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++) {
652 Msgs[msgnum].m_bboard_id = 0;
653 Msgs[msgnum].m_top = NOTOK;
654 Msgs[msgnum].m_start = Msgs[msgnum].m_stop = 0L;
655 Msgs[msgnum].m_scanl = NULL;
660 fmsh = getcpy (folder);
662 maxfds = OPEN_MAX / 2;
664 if ((maxfds -= 2) < 1)
674 if ((fp = fopen (file, "r")) == NULL)
675 padios (file, "unable to read");
677 ioctl (fileno (fp), FIOCLEX, NULL);
679 if (fstat (fileno (fp), &st) != NOTOK) {
680 mode = (int) (st.st_mode & 0777), mtime = st.st_mtime;
681 msgp = read_map (file, (long) st.st_size);
684 mode = m_gmprot (), mtime = 0;
688 if ((msgp = read_file (msgp ? Msgs[msgp].m_stop : 0L, msgp + 1)) < 1)
689 padios (NULL, "no messages in %s", myname ? myname : file);
691 if (!(mp = (struct msgs *) calloc ((size_t) 1, sizeof(*mp))))
692 padios (NULL, "unable to allocate folder storage");
694 if (!(mp->msgstats = calloc ((size_t) msgp + 3, sizeof(*(mp->msgstats)))))
695 padios (NULL, "unable to allocate message status storage");
701 mp->foldpath = getcpy (myname ? myname : file);
702 clear_folder_flags (mp);
705 if (st.st_uid != getuid () || access (file, W_OK) == NOTOK)
709 mp->hghoff = mp->hghmsg + 1;
711 for (i = mp->lowmsg; i <= mp->hghmsg; i++) {
712 clear_msg_flags (mp, i);
717 mp->msgattrs[0] = getcpy ("unseen");
718 mp->msgattrs[1] = NULL;
720 m_unknown (fp); /* the MAGIC invocation */
729 read_map (char *file, long size)
731 register int i, msgp;
732 register struct drop *dp, *mp;
735 if ((i = map_read (file, size, &rp, 1)) == 0)
741 for (dp = rp + 1; i-- > 0; msgp++, dp++) {
742 mp = &Msgs[msgp].m_drop;
744 mp->d_size = dp->d_size;
745 mp->d_start = dp->d_start;
746 mp->d_stop = dp->d_stop;
747 Msgs[msgp].m_scanl = NULL;
756 read_file (long pos, int msgp)
759 register struct drop *dp, *mp;
762 if ((i = mbx_read (fp, pos, &rp, 1)) <= 0)
765 m_gMsgs ((msgp - 1) + i);
767 for (dp = rp; i-- > 0; msgp++, dp++) {
768 mp = &Msgs[msgp].m_drop;
770 mp->d_size = dp->d_size;
771 mp->d_start = dp->d_start;
772 mp->d_stop = dp->d_stop;
773 Msgs[msgp].m_scanl = NULL;
787 nMsgs = n + MAXFOLDER / 2;
788 Msgs = (struct Msg *) calloc ((size_t) (nMsgs + 2), sizeof *Msgs);
790 padios (NULL, "unable to allocate Msgs structure");
797 nmsgs = nMsgs + n + MAXFOLDER / 2;
798 Msgs = (struct Msg *) mh_xrealloc ((char *) Msgs, (size_t) (nmsgs + 2) * sizeof *Msgs);
799 memset((char *) (Msgs + nMsgs + 2), 0, (size_t) ((nmsgs - nMsgs) * sizeof *Msgs));
806 msh_ready (int msgnum, int full)
819 if ((fd = Msgs[msgnum].m_top) == NOTOK) {
820 if (numfds >= maxfds)
821 for (msgp = mp->lowmsg; msgp <= mp->hghmsg; msgp++)
822 if (Msgs[msgp].m_top != NOTOK) {
823 close (Msgs[msgp].m_top);
824 Msgs[msgp].m_top = NOTOK;
829 if ((fd = open (cp = m_name (msgnum), O_RDONLY)) == NOTOK)
830 padios (cp, "unable to open message");
831 Msgs[msgnum].m_top = fd;
835 if ((fd = dup (fd)) == NOTOK)
836 padios ("cached message", "unable to dup");
837 if ((yp = fdopen (fd, "r")) == NULL)
838 padios (NULL, "unable to fdopen cached message");
839 fseek (yp, 0L, SEEK_SET);
843 m_eomsbr ((int (*)()) 0); /* XXX */
844 fseek (fp, Msgs[msgnum].m_start, SEEK_SET);
850 check_folder (int scansw)
852 int seqnum, i, low, hgh, msgp;
856 if (stat (mp->foldpath, &st) == NOTOK)
857 padios (mp->foldpath, "unable to stat");
858 if (mtime == st.st_mtime)
862 low = mp->hghmsg + 1;
863 folder_free (mp); /* free folder/message structure */
865 if (!(mp = folder_read (fmsh)))
866 padios (NULL, "unable to re-read folder %s", fmsh);
870 for (msgp = mp->lowmsg; msgp <= mp->hghmsg; msgp++) {
871 if (Msgs[msgp].m_top != NOTOK) {
872 close (Msgs[msgp].m_top);
873 Msgs[msgp].m_top = NOTOK;
876 if (Msgs[msgp].m_scanl) {
877 free (Msgs[msgp].m_scanl);
878 Msgs[msgp].m_scanl = NULL;
884 if (modified || low > hgh)
888 if (fstat (fileno (fp), &st) == NOTOK)
889 padios (mp->foldpath, "unable to fstat");
890 if (mtime == st.st_mtime)
892 mode = (int) (st.st_mode & 0777);
895 if ((msgp = read_file (Msgs[mp->hghmsg].m_stop, mp->hghmsg + 1)) < 1)
896 padios (NULL, "no messages in %s", mp->foldpath); /* XXX */
897 if (msgp >= MAXFOLDER)
898 padios (NULL, "more than %d messages in %s", MAXFOLDER,
900 if (msgp <= mp->hghmsg)
903 if (!(mp = folder_realloc (mp, mp->lowoff, msgp)))
904 padios (NULL, "unable to allocate folder storage");
906 low = mp->hghmsg + 1, hgh = msgp;
907 seqnum = scansw ? seq_getnum (mp, "unseen") : -1;
908 for (i = mp->hghmsg + 1; i <= msgp; i++) {
911 add_sequence(mp, seqnum, i);
921 advise (NULL, "new messages have arrived!\007");
923 scanrange (low, hgh);
930 scanrange (int low, int hgh)
934 snprintf (buffer, sizeof(buffer), "%d-%d", low, hgh);
940 scanstring (char *arg)
942 char *cp, **ap, *vec[MAXARGS];
945 * This should be replace with a call to getarguments()
947 if ((cp = context_find (cmd_name = "scan"))) {
949 ap = brkstring (cp, " ", "\n");
950 ap = copyip (ap, vec, MAXARGS);
966 register int cur, seqnum, i=0, msgnum;
969 seq_setcur (mp, mp->lowmsg);
970 if (id <= 0 || (seqnum = seq_getnum (mp, "unseen")) == -1)
973 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
974 add_sequence(mp, seqnum, msgnum);
979 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
980 if (does_exist(mp, msgnum)) /* FIX */
981 if ((i = readid (msgnum)) > 0 && i < id) {
983 clear_sequence(mp, seqnum, msgnum);
986 for (i = mp->lowmsg; i < msgnum; i++)
987 clear_sequence(mp, seqnum, i);
989 if (cur > mp->hghmsg)
992 seq_setcur (mp, cur);
995 if ((gap = 1 < id && id < (i = readid (mp->lowmsg)) ? id : 0) && !vmh)
996 advise (NULL, "gap in ID:s, last seen %d, lowest present %d\n",
1005 char *bp, buf[BUFSIZ], name[NAMESZ];
1008 if (Msgs[msgnum].m_bboard_id)
1009 return Msgs[msgnum].m_bboard_id;
1011 zp = msh_ready (msgnum, 0);
1013 switch (state = m_getfld (state, name, buf, sizeof(buf), zp)) {
1017 if (!mh_strcasecmp (name, BBoard_ID)) {
1019 while (state == FLDPLUS) {
1020 state = m_getfld (state, name, buf, sizeof(buf), zp);
1026 return (Msgs[msgnum].m_bboard_id = i);
1030 while (state == FLDPLUS)
1031 state = m_getfld (state, name, buf, sizeof(buf), zp);
1032 if (state != FLDEOF)
1042 display_info (int scansw)
1046 interactive = isatty (fileno (stdout));
1048 if ((sd = dup (fileno (stdout))) == NOTOK)
1049 padios ("standard output", "unable to dup");
1051 ioctl (sd, FIOCLEX, NULL);
1052 #endif /* FIOCLEX */
1053 if ((sp = fdopen (sd, "w")) == NULL)
1054 padios ("standard output", "unable to fdopen");
1057 m_putenv ("mhfolder", mp->foldpath);
1062 printf ("Reading ");
1063 if (SOprintf ("%s", myname))
1064 printf ("%s", myname);
1065 printf (", currently at message %d of %d\n",
1066 mp->curmsg, mp->hghmsg);
1069 printf ("Reading ");
1071 printf ("+%s", fmsh);
1073 printf ("%s", mp->foldpath);
1074 printf (", currently at message %d of %d\n",
1075 mp->curmsg, mp->hghmsg);
1078 if (((seqnum = seq_getnum (mp, "unseen")) != -1)
1080 && in_sequence(mp, seqnum, mp->hghmsg))
1081 scanstring ("unseen");
1088 int i = 0, seqnum, msgnum;
1094 if ((seqnum = seq_getnum (mp, "unseen")) != -1)
1095 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
1096 if (!in_sequence(mp, seqnum, msgnum)) {
1097 if (Msgs[msgnum].m_bboard_id == 0)
1099 if ((i = Msgs[msgnum].m_bboard_id) > 0)
1103 snprintf (buffer, sizeof(buffer), "%d %d\n", i, Msgs[mp->hghmsg].m_bboard_id);
1104 write (pfd, buffer, sizeof(buffer));
1114 char *cp, tmpfil[BUFSIZ];
1115 char map1[BUFSIZ], map2[BUFSIZ];
1119 if (!(mp->msgflags & MODIFIED) || is_readonly(mp) || fmsh) {
1121 rc2peer (RC_FIN, 0, NULL);
1126 ttyNaux (NULLCMD, "FAST");
1128 if ((dp = lkfopen (mp->foldpath, "r")) == NULL) {
1129 advise (mp->foldpath, "unable to lock");
1136 if (fstat (fileno (dp), &st) == NOTOK) {
1137 advise (mp->foldpath, "unable to stat");
1140 if (mtime != st.st_mtime) {
1141 advise (NULL, "new messages have arrived, no update");
1144 mode = (int) (st.st_mode & 0777);
1146 if (mp->nummsg == 0) {
1147 cp = concat ("Zero file \"", mp->foldpath, "\"? ", NULL);
1148 if (getanswer (cp)) {
1149 if ((i = creat (mp->foldpath, mode)) != NOTOK)
1152 advise (mp->foldpath, "error zero'ing");
1153 unlink (map_name (mp->foldpath));/* XXX */
1158 cp = concat ("Update file \"", mp->foldpath, "\"? ", NULL);
1159 if (!getanswer (cp))
1161 strncpy (tmpfil, m_backup (mp->foldpath), sizeof(tmpfil));
1162 if ((md = mbx_open (tmpfil, mbx_style, st.st_uid, st.st_gid, mode)) == NOTOK) {
1163 advise (tmpfil, "unable to open");
1167 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
1168 if (does_exist(mp, msgnum) && pack (tmpfil, md, msgnum) == NOTOK) {
1169 mbx_close (tmpfil, md);
1171 unlink (map_name (tmpfil));
1174 mbx_close (tmpfil, md);
1176 if (rename (tmpfil, mp->foldpath) == NOTOK)
1177 admonish (mp->foldpath, "unable to rename %s to", tmpfil);
1179 strncpy (map1, map_name (tmpfil), sizeof(map1));
1180 strncpy (map2, map_name (mp->foldpath), sizeof(map2));
1182 if (rename (map1, map2) == NOTOK) {
1183 admonish (map2, "unable to rename %s to", map1);
1192 lkfclose (dp, mp->foldpath);
1201 getargs (char *prompt, struct swit *sw, struct Cmd *cmdp)
1205 static char buffer[BUFSIZ];
1211 printf ("%s", prompt);
1214 for (cp = buffer; (i = getchar ()) != '\n';) {
1215 if (interrupted && !told_to_quit) {
1220 if (told_to_quit || i == EOF) {
1223 kill (ppid, SIGEMT);
1225 kill (ppid, SIGTERM);
1229 if (cp < &buffer[sizeof buffer - 2])
1236 if (buffer[0] == '?') {
1237 printf ("commands:\n");
1238 print_sw (ALL, sw, "", stdout);
1239 printf ("type CTRL-D or use ``quit'' to leave %s\n",
1244 if (parse (buffer, cmdp) == NOTOK)
1247 switch (i = smatch (cmdp->args[0], sw)) {
1249 ambigsw (cmdp->args[0], sw);
1252 printf ("say what: ``%s'' -- type ? (or help) for help\n",
1263 getcmds (struct swit *sw, struct Cmd *cmdp, int scansw)
1266 struct record rcs, *rc;
1272 switch (peer2rc (rc)) {
1274 pQRY (rc->rc_data, scansw);
1278 if ((i = pCMD (rc->rc_data, sw, cmdp)) != NOTOK)
1285 kill (ppid, SIGEMT);
1287 kill (ppid, SIGTERM);
1292 padios (NULL, "%s", rc->rc_data);
1295 fmt2peer (RC_ERR, "pLOOP protocol screw-up");
1302 parse (char *buffer, struct Cmd *cmdp)
1305 unsigned char c, *cp;
1309 pp = cmdp->args[argp++] = cmdp->line;
1310 cmdp->redirect = NULL;
1311 cmdp->direction = STDIO;
1312 cmdp->stream = NULL;
1314 for (cp = buffer; (c = *cp); cp++) {
1320 fmt2peer (RC_EOF, "null command");
1324 while ((c = *cp++)) {
1331 cmdp->args[argp++] = pp;
1338 switch (c = *cp++) {
1340 padvise (NULL, "unmatched \"");
1345 if ((c = *cp++) == 0)
1356 if ((c = *cp++) == 0) {
1358 padvise (NULL, "the newline character can not be quoted");
1368 if (pp == cmdp->line) {
1369 padvise (NULL, "invalid null command");
1372 if (*cmdp->args[argp - 1] == 0)
1374 cmdp->direction = c == '>' ? CRTIO : PIPIO;
1375 if (cmdp->direction == CRTIO && (c = *cp) == '>') {
1376 cmdp->direction = APPIO;
1379 cmdp->redirect = pp + 1;/* sigh */
1380 for (; (c = *cp); cp++)
1384 padvise (NULL, cmdp->direction != PIPIO
1385 ? "missing name for redirect"
1386 : "invalid null command");
1389 strcpy (cmdp->redirect, cp);
1390 if (cmdp->direction != PIPIO) {
1392 if (isspace (*cp)) {
1393 padvise (NULL, "bad name for redirect");
1396 if (expand (cmdp->redirect) == NOTOK)
1405 cmdp->args[argp] = NULL;
1412 expand (char *redirect)
1418 if (*redirect != '~')
1421 if ((cp = strchr(pp = redirect + 1, '/')))
1426 if ((pw = getpwnam (pp)))
1429 padvise (NULL, "unknown user: %s", pp);
1433 snprintf (path, sizeof(path), "%s/%s", pp, cp ? cp : "");
1434 strcpy (redirect, path);
1440 init_io (struct Cmd *cmdp, int vio)
1447 result = initaux_io (cmdp);
1455 initaux_io (struct Cmd *cmdp)
1459 switch (cmdp->direction) {
1465 mode = cmdp->direction == CRTIO ? "write" : "append";
1466 if ((cmdp->stream = fopen (cmdp->redirect, mode)) == NULL) {
1467 padvise (cmdp->redirect, "unable to %s ", mode);
1468 cmdp->direction = STDIO;
1474 if ((cmdp->stream = popen (cmdp->redirect, "w")) == NULL) {
1475 padvise (cmdp->redirect, "unable to pipe");
1476 cmdp->direction = STDIO;
1479 SIGNAL (SIGPIPE, pipeser);
1484 padios (NULL, "unknown redirection for command");
1488 if (dup2 (fileno (cmdp->stream), fileno (stdout)) == NOTOK)
1489 padios ("standard output", "unable to dup2");
1497 fin_io (struct Cmd *cmdp, int vio)
1509 finaux_io (struct Cmd *cmdp)
1511 switch (cmdp->direction) {
1518 close (fileno (stdout));
1519 if (ferror (stdout))
1520 padvise (NULL, "problems writing %s", cmdp->redirect);
1521 fclose (cmdp->stream);
1526 close (fileno (stdout));
1527 pclose (cmdp->stream);
1528 SIGNAL (SIGPIPE, SIG_DFL);
1532 padios (NULL, "unknown redirection for command");
1535 if (dup2 (fileno (sp), fileno (stdout)) == NOTOK)
1536 padios ("standard output", "unable to dup2");
1539 cmdp->direction = STDIO;
1548 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
1549 unset_selected (mp, msgnum);
1550 mp->lowsel = mp->hghsel = mp->numsel = 0;
1558 folder_free (mp); /* free folder/message structure */
1564 seq_setcur (struct msgs *mp, int msgnum)
1566 if (mp->curmsg == msgnum)
1569 if (mp->curmsg && Msgs[mp->curmsg].m_scanl) {
1570 free (Msgs[mp->curmsg].m_scanl);
1571 Msgs[mp->curmsg].m_scanl = NULL;
1573 if (Msgs[msgnum].m_scanl) {
1574 free (Msgs[msgnum].m_scanl);
1575 Msgs[msgnum].m_scanl = NULL;
1578 mp->curmsg = msgnum;
1596 if (broken_pipe++ == 0)
1597 fprintf (stderr, "broken pipe\n");
1616 longjmp (peerenv, DONE);
1625 struct record rcs, *rc;
1630 switch (peer2rc (rc)) {
1633 while (isspace (*bp))
1635 if (sscanf (bp, "%d", &vrsn) != 1) {
1637 fmt2peer (RC_ERR, "bad init \"%s\"", rc->rc_data);
1640 if (vrsn != RC_VRSN) {
1641 fmt2peer (RC_ERR, "version %d unsupported", vrsn);
1645 while (*bp && !isspace (*bp))
1647 while (isspace (*bp))
1649 if (sscanf (bp, "%d", &numwins) != 1 || numwins <= 0)
1654 for (i = 1; i <= numwins; i++) {
1655 while (*bp && !isspace (*bp))
1657 while (isspace (*bp))
1659 if (sscanf (bp, "%d", &windows[i]) != 1 || windows[i] <= 0)
1662 rc2peer (RC_ACK, 0, NULL);
1666 padios (NULL, "%s", rc->rc_data);
1669 fmt2peer (RC_ERR, "pINI protocol screw-up");
1670 done (1); /* NOTREACHED */
1673 return 1; /* dead code to satisfy the compiler */
1678 pQRY (char *str, int scansw)
1681 if (pQRY1 (scansw) == NOTOK || pQRY2 () == NOTOK)
1684 rc2peer (RC_EOF, 0, NULL);
1693 static int lastlow = 0,
1698 oldhgh = mp->hghmsg;
1699 if (check_folder (scansw) && oldhgh < mp->hghmsg) {
1700 switch (winX (STATUS)) {
1705 printf ("new messages have arrived!");
1708 _exit (0); /* NOTREACHED */
1711 lastlow = lastcur = lasthgh = lastnum = 0;
1715 switch (winX (DISPLAY)) {
1720 scanrange (oldhgh + 1, mp->hghmsg);
1723 _exit (0); /* NOTREACHED */
1732 switch (winX (STATUS)) {
1737 printf ("%s: gap in ID:s, last seen %d, lowest present %d\n",
1738 myname ? myname : fmsh ? fmsh : mp->foldpath, gap - 1,
1739 readid (mp->lowmsg));
1742 _exit (0); /* NOTREACHED */
1749 if (mp->lowmsg != lastlow
1750 || mp->curmsg != lastcur
1751 || mp->hghmsg != lasthgh
1752 || mp->nummsg != lastnum)
1753 switch (winX (STATUS)) {
1761 _exit (0); /* NOTREACHED */
1764 lastlow = mp->lowmsg;
1765 lastcur = mp->curmsg;
1766 lasthgh = mp->hghmsg;
1767 lastnum = mp->nummsg;
1778 int i, j, k, msgnum, n;
1784 if (mp->nummsg == 0 && mp->nummsg != num)
1785 switch (winX (SCAN)) {
1793 _exit (0); /* NOTREACHED */
1802 j = (k = windows[SCAN]) / 2;
1803 for (msgnum = mp->curmsg; msgnum <= mp->hghmsg; msgnum++)
1804 if (does_exist (mp, msgnum))
1808 k = i >= k ? 1 : k - i;
1815 for (msgnum = mp->curmsg; msgnum >= mp->lowmsg; msgnum--)
1816 if (does_exist (mp, msgnum)) {
1823 for (msgnum = mp->curmsg + 1; msgnum <= mp->hghmsg; msgnum++)
1824 if (does_exist (mp, msgnum)) {
1828 if (n++ >= windows[SCAN])
1834 && does_exist (mp, lo)
1835 && does_exist (mp, hi)
1837 || (lo == mp->curmsg && lo == mp->lowmsg))
1839 || (hi == mp->curmsg && hi == mp->hghmsg))
1840 && hi - lo == j - i)
1843 if (mp->curmsg != cur || modified)
1844 switch (winN (NULLCMD, SCAN, 0)) {
1852 scanrange (lo = i, hi = j);
1863 pCMD (char *str, struct swit *sw, struct Cmd *cmdp)
1868 switch (winX (DISPLAY)) {
1873 printf ("commands:\n");
1874 print_sw (ALL, sw, "", stdout);
1875 printf ("type ``quit'' to leave %s\n", invo_name);
1878 _exit (0); /* NOTREACHED */
1881 rc2peer (RC_EOF, 0, NULL);
1885 if (parse (str, cmdp) == NOTOK)
1888 switch (i = smatch (cmdp->args[0], sw)) {
1890 switch (winX (DISPLAY)) {
1895 ambigsw (cmdp->args[0], sw);
1898 _exit (0); /* NOTREACHED */
1901 rc2peer (RC_EOF, 0, NULL);
1907 "say what: ``%s'' -- type ? (or help) for help",
1922 switch (m_setjmp (peerenv)) {
1924 SIGNAL (SIGALRM, alrmser);
1927 status = peerwait ();
1941 struct record rcs, *rc;
1946 switch (peer2rc (rc)) {
1949 rc2peer (RC_FIN, 0, NULL);
1953 advise (NULL, "%s", rc->rc_data);
1957 fmt2peer (RC_FIN, "pLOOP protocol screw-up");
1964 ttyNaux (struct Cmd *cmdp, char *s)
1966 struct record rcs, *rc;
1971 if (cmdp && init_io (cmdp, vmh) == NOTOK)
1974 /* XXX: fseek() too tricky for our own good */
1976 fseek (fp, 0L, SEEK_SET);
1979 switch (rc2rc (RC_TTY, s ? strlen (s) : 0, s, rc)) {
1981 vmhtty = OK; /* fall */
1986 padios (NULL, "%s", rc->rc_data);/* NOTREACHED */
1989 fmt2peer (RC_ERR, "pTTY protocol screw-up");
1990 done (1); /* NOTREACHED */
1994 SIGNAL (SIGTSTP, tstat);
2001 ttyR (struct Cmd *cmdp)
2003 struct record rcs, *rc;
2008 SIGNAL (SIGTSTP, SIG_IGN);
2020 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2022 rc2peer (RC_EOF, 0, NULL);
2026 padios (NULL, "%s", rc->rc_data);/* NOTREACHED */
2029 fmt2peer (RC_ERR, "pTTY protocol screw-up");
2030 done (1); /* NOTREACHED */
2033 return 1; /* dead code to satisfy compiler */
2038 winN (struct Cmd *cmdp, int n, int eof)
2041 char buffer[BUFSIZ];
2042 struct record rcs, *rc;
2045 if (vmhpid == NOTOK)
2050 /* XXX: fseek() too tricky for our own good */
2052 fseek (fp, 0L, SEEK_SET);
2056 snprintf (buffer, sizeof(buffer), "%d", n);
2057 switch (str2rc (RC_WIN, buffer, rc)) {
2065 padios (NULL, "%s", rc->rc_data);
2068 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2072 if (pipe (pd) == NOTOK) {
2073 err2peer (RC_ERR, "pipe", "unable to");
2077 switch (vmhpid = fork()) {
2079 err2peer (RC_ERR, "fork", "unable to");
2086 SIGNAL (SIGPIPE, SIG_IGN);
2087 while ((i = read (pd[0], buffer, sizeof buffer)) > 0)
2088 switch (rc2rc (RC_DATA, i, buffer, rc)) {
2096 advise (NULL, "%s", rc->rc_data);
2100 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2104 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2107 rc2peer (RC_EOF, 0, NULL);
2112 advise (NULL, "%s", rc->rc_data);
2117 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2122 err2peer (RC_ERR, "pipe", "error reading from");
2124 _exit (i != NOTOK ? i : 1);
2127 if ((vmhfd0 = dup (fileno (stdin))) == NOTOK)
2128 padios ("standard input", "unable to dup");
2129 if ((vmhfd1 = dup (fileno (stdout))) == NOTOK)
2130 padios ("standard output", "unable to dup");
2131 if ((vmhfd2 = dup (fileno (stderr))) == NOTOK)
2132 padios ("diagnostic output", "unable to dup");
2135 if ((i = open ("/dev/null", O_RDONLY)) != NOTOK && i != fileno (stdin)) {
2136 dup2 (i, fileno (stdin));
2141 if (dup2 (pd[1], fileno (stdout)) == NOTOK)
2142 padios ("standard output", "unable to dup2");
2146 if (dup2 (pd[1], fileno (stderr)) == NOTOK)
2147 padios ("diagnostic output", "unable to dup2");
2150 if (cmdp && init_io (cmdp, 0) == NOTOK)
2152 pstat = SIGNAL (SIGPIPE, pipeser);
2164 winR (struct Cmd *cmdp)
2174 if (dup2 (vmhfd0, fileno (stdin)) == NOTOK)
2175 padios ("standard input", "unable to dup2");
2180 if (dup2 (vmhfd1, fileno (stdout)) == NOTOK)
2181 padios ("standard output", "unable to dup2");
2186 if (dup2 (vmhfd2, fileno (stderr)) == NOTOK)
2187 padios ("diagnostic output", "unable to dup2");
2191 SIGNAL (SIGPIPE, pstat);
2193 if ((status = pidwait (vmhpid, OK)) == 2)
2197 return (status == 0 ? OK : NOTOK);
2205 char buffer[BUFSIZ];
2206 struct record rcs, *rc;
2211 /* XXX: fseek() too tricky for our own good */
2213 fseek (fp, 0L, SEEK_SET);
2215 snprintf (buffer, sizeof(buffer), "%d", n);
2216 switch (str2rc (RC_WIN, buffer, rc)) {
2224 padios (NULL, "%s", rc->rc_data);
2227 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2231 if (pipe (pd) == NOTOK) {
2232 err2peer (RC_ERR, "pipe", "unable to");
2236 switch (pid = fork ()) {
2238 err2peer (RC_ERR, "fork", "unable to");
2244 close (fileno (stdin));
2245 if ((i = open ("/dev/null", O_RDONLY)) != NOTOK && i != fileno (stdin)) {
2246 dup2 (i, fileno (stdin));
2249 dup2 (pd[1], fileno (stdout));
2250 dup2 (pd[1], fileno (stderr));
2258 while ((i = read (pd[0], buffer, sizeof buffer)) > 0)
2259 switch (rc2rc (RC_DATA, i, buffer, rc)) {
2269 padios (NULL, "%s", rc->rc_data);
2272 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2276 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2281 padios (NULL, "%s", rc->rc_data);
2284 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2288 err2peer (RC_ERR, "pipe", "error reading from");
2292 return (i != NOTOK ? pid : NOTOK);
2298 padios (char *what, char *fmt, ...)
2304 verr2peer (RC_FIN, what, fmt, ap);
2307 advertise (what, NULL, fmt, ap);
2316 padvise (char *what, char *fmt, ...)
2322 verr2peer (RC_ERR, what, fmt, ap);
2324 advertise (what, NULL, fmt, ap);