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>
41 #define QUOTE '\\' /* sigh */
43 static struct swit switches[] = {
45 { "idstart number", -7 }, /* interface from bbc */
47 { "idstop number", -6 }, /* .. */
49 { "idquit number", -6 }, /* .. */
51 { "idname BBoard", -6 }, /* .. */
53 { "prompt string", 0 },
61 { "vmhwrite fd", -8 },
65 { "popwrite fd", -8 },
77 static int mbx_style = MMDF_FORMAT;
82 char*fmsh = NULL; /* folder instead of file */
83 int modified; /* command modified folder */
84 struct msgs *mp; /* used a lot */
86 struct Msg *Msgs = NULL; /* Msgs[0] not used */
87 static FILE *fp; /* input file */
88 static FILE *yp = NULL; /* temporary file */
89 static int mode; /* mode of file */
90 static int numfds = 0; /* number of files cached */
91 static int maxfds = 0; /* number of files cached to be cached */
92 static time_t mtime = (time_t) 0; /* mtime of file */
97 #define ALARM ((unsigned int) 10)
98 #define ttyN(c) ttyNaux ((c), NULL)
102 static int vmhpid = OK;
107 static int vmhtty = NOTOK;
114 static int topcur = 0;
116 static int numwins = 0;
117 static int windows[NWIN + 1];
119 static jmp_buf peerenv;
122 int pmsh = 0; /* BPOP enabled */
123 extern char response[];
129 static int pfd = NOTOK; /* fd parent is reading from */
130 static int ppid = 0; /* pid of parent */
135 int interactive; /* running from a /dev/tty */
136 int redirected; /* re-directing output */
137 FILE *sp = NULL; /* original stdout */
139 char *cmd_name; /* command being run */
140 char myfilter[BUFSIZ]; /* path to mhl.forward */
142 static char *myprompt = "(%s) ";/* prompting string */
147 static int gap; /* gap in BBoard-ID:s */
148 static char *myname = NULL; /* BBoard name */
149 char *BBoard_ID = "BBoard-ID"; /* BBoard-ID constant */
154 SIGNAL_HANDLER istat; /* original SIGINT */
155 static SIGNAL_HANDLER pstat; /* current SIGPIPE */
156 SIGNAL_HANDLER qstat; /* original SIGQUIT */
159 SIGNAL_HANDLER tstat; /* original SIGTSTP */
162 int interrupted; /* SIGINT detected */
163 int broken_pipe; /* SIGPIPE detected */
164 int told_to_quit; /* SIGQUIT detected */
167 int should_intr; /* signal handler should interrupt call */
168 jmp_buf sigenv; /* the environment pointer */
174 int SOprintf (char *, ...); /* from termsbr.c */
175 int sc_width (void); /* from termsbr.c */
176 void fsetup (char *);
178 FILE *msh_ready (int, int);
181 void display_info (int);
184 void seq_setcur (struct msgs *, int);
185 void padios (char *, char *, ...);
186 void padvise (char *, char *, ...);
192 static void msh (int);
193 static int read_map (char *, long);
194 static int read_file (long, int);
198 static int pop_statmsg (char *);
200 static int read_pop (void);
201 static int pop_action (char *);
204 static void m_gMsgs (int);
205 FILE *msh_ready (int, int);
206 static int check_folder (int);
207 static void scanrange (int, int);
208 static void scanstring (char *);
209 static void write_ids (void);
210 static void quit (void);
211 static int getargs (char *, struct swit *, struct Cmd *);
212 static int getcmds (struct swit *, struct Cmd *, int);
213 static int parse (char *, struct Cmd *);
214 static int init_io (struct Cmd *, int);
215 static int initaux_io (struct Cmd *);
216 static void fin_io (struct Cmd *, int);
217 static void finaux_io (struct Cmd *);
218 static void m_init (void);
219 static RETSIGTYPE intrser (int);
220 static RETSIGTYPE pipeser (int);
221 static RETSIGTYPE quitser (int);
222 static RETSIGTYPE alrmser (int);
223 static int pINI (void);
224 static int pQRY (char *, int);
225 static int pQRY1 (int);
226 static int pQRY2 (void);
227 static int pCMD (char *, struct swit *, struct Cmd *);
228 static int pFIN (void);
229 static int peerwait (void);
230 static int ttyNaux (struct Cmd *, char *);
231 static int ttyR (struct Cmd *);
232 static int winN (struct Cmd *, int, int);
233 static int winR (struct Cmd *);
234 static int winX (int);
238 main (int argc, char **argv)
240 int id = 0, scansw = 0, vmh1 = 0, vmh2 = 0;
241 char *cp, *file = NULL, *folder = NULL;
242 char **argp, **arguments, buf[BUFSIZ];
244 int pmsh1 = 0, pmsh2 = 0;
248 setlocale(LC_ALL, "");
250 invo_name = r1bindex (argv[0], '/');
252 /* read user profile/context */
255 mts_init (invo_name);
256 arguments = getarguments (invo_name, argc,argv, 1);
259 while ((cp = *argp++)) {
261 switch (smatch (++cp, switches)) {
263 ambigsw (cp, switches);
266 adios (NULL, "-%s unknown", cp);
269 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
270 print_help (buf, switches, 1);
273 print_version(invo_name);
277 if (!(cp = *argp++) || *cp == '-')
278 adios (NULL, "missing argument to %s", argp[-2]);
279 if ((id = atoi (cp)) < 1)
280 adios (NULL, "bad argument %s %s", argp[-2], cp);
283 if (!(cp = *argp++) || *cp == '-')
284 adios (NULL, "missing argument to %s", argp[-2]);
285 if ((pfd = 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 ((ppid = atoi (cp)) <= 1)
292 adios (NULL, "bad argument %s %s", argp[-2], cp);
295 if (!(myname = *argp++) || *myname == '-')
296 adios (NULL, "missing argument to %s", argp[-2]);
307 if (!(myprompt = *argp++) || *myprompt == '-')
308 adios (NULL, "missing argument to %s", argp[-2]);
312 if (!(cp = *argp++) || *cp == '-')
313 adios (NULL, "missing argument to %s", argp[-2]);
314 if ((vmh1 = atoi (cp)) < 1)
315 adios (NULL, "bad argument %s %s", argp[-2], cp);
318 if (!(cp = *argp++) || *cp == '-')
319 adios (NULL, "missing argument to %s", argp[-2]);
320 if ((vmh2 = atoi (cp)) < 1)
321 adios (NULL, "bad argument %s %s", argp[-2], cp);
325 if (!(cp = *argp++) || *cp == '-')
326 adios (NULL, "missing argument to %s", argp[-2]);
328 if ((pmsh1 = atoi (cp)) < 1)
329 adios (NULL, "bad argument %s %s", argp[-2], cp);
333 if (!(cp = *argp++) || *cp == '-')
334 adios (NULL, "missing argument to %s", argp[-2]);
336 if ((pmsh2 = atoi (cp)) < 1)
337 adios (NULL, "bad argument %s %s", argp[-2], cp);
348 if (*cp == '+' || *cp == '@') {
350 adios (NULL, "only one folder at a time!");
352 folder = pluspath (cp);
356 adios (NULL, "only one file at a time!");
361 if (!file && !folder)
364 adios (NULL, "use a file or a folder, not both");
365 strncpy (myfilter, etcpath (mhlforward), sizeof(myfilter));
368 ioctl (pfd, FIOCLEX, NULL);
374 istat = SIGNAL2 (SIGINT, intrser);
375 qstat = SIGNAL2 (SIGQUIT, quitser);
377 sc_width (); /* MAGIC... */
379 if ((vmh = vmh1 && vmh2)) {
382 SIGNAL (SIGINT, SIG_IGN);
383 SIGNAL (SIGQUIT, SIG_IGN);
385 tstat = SIGNAL (SIGTSTP, SIG_IGN);
390 if (pmsh = pmsh1 && pmsh2) {
391 cp = getenv ("MHPOPDEBUG");
393 if (pop_set (pmsh1, pmsh2, cp && *cp, myname) == NOTOK)
395 if (pop_set (pmsh1, pmsh2, cp && *cp) == NOTOK)
397 padios (NULL, "%s", response);
399 file = folder, folder = NULL;
408 display_info (id > 0 ? scansw : 0);
410 msh (id > 0 ? scansw : 0);
419 static struct swit mshcmds[] = {
484 register char *cp, **ap;
485 char prompt[BUFSIZ], *vec[MAXARGS];
487 register struct Cmd *cmdp;
488 static int once_only = ADVCMD;
490 snprintf (prompt, sizeof(prompt), myprompt, invo_name);
499 if ((i = getcmds (mshcmds, cmdp, scansw)) == EOF) {
504 check_folder (scansw);
505 if ((i = getargs (prompt, mshcmds, cmdp)) == EOF) {
510 cmd_name = mshcmds[i].sw;
518 if (once_only == ADVCMD)
519 once_only = i = SHOWCMD;
521 i = mp->curmsg != mp->hghmsg ? NEXTCMD : EXITCMD;
522 cmd_name = mshcmds[i].sw;
528 case FORWCMD: /* sigh */
538 if ((cp = context_find (cmd_name))) {
540 ap = brkstring (cp, " ", "\n");
541 ap = copyip (ap, vec, MAXARGS);
552 copyip (cmdp->args + 1, ap, MAXARGS);
556 if (!vmh && init_io (cmdp, vmh) == NOTOK) {
562 redirected = vmh || cmdp->direction != STDIO;
573 if (!vmh || ttyN (cmdp) != NOTOK)
574 forkcmd (vec, cmd_name);
578 if (!vmh || ttyN (cmdp) != NOTOK)
583 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
589 || (filehak (vec) == OK ? ttyN (cmdp)
590 : winN (cmdp, DISPLAY, 1)) != NOTOK)
595 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
600 if (!vmh || ttyN (cmdp) != NOTOK)
605 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
611 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
616 if (!vmh || ttyN (cmdp) != NOTOK)
623 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
629 || (packhak (vec) == OK ? ttyN (cmdp)
630 : winN (cmdp, DISPLAY, 1)) != NOTOK)
635 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
640 if (!vmh || ttyN (cmdp) != NOTOK)
645 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
650 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
655 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
660 padios (NULL, "no dispatch for %s", cmd_name);
682 fsetup (char *folder)
688 maildir = m_maildir (folder);
689 if (chdir (maildir) == NOTOK)
690 padios (maildir, "unable to change directory to");
692 /* read folder and create message structure */
693 if (!(mp = folder_read (folder)))
694 padios (NULL, "unable to read folder %s", folder);
696 /* check for empty folder */
698 padios (NULL, "no messages in %s", folder);
701 mtime = stat (mp->foldpath, &st) != NOTOK ? st.st_mtime : 0;
703 m_gMsgs (mp->hghmsg);
705 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++) {
706 Msgs[msgnum].m_bboard_id = 0;
707 Msgs[msgnum].m_top = NOTOK;
708 Msgs[msgnum].m_start = Msgs[msgnum].m_stop = 0L;
709 Msgs[msgnum].m_scanl = NULL;
714 fmsh = getcpy (folder);
716 maxfds = OPEN_MAX / 2;
718 if ((maxfds -= 2) < 1)
734 char *tfile = m_mktemp2(NULL, invo_name, NULL, &fp);
735 if (tfile == NULL) padios("msh", "unable to create temporary file");
737 strncpy(tmpfil, tfile, sizeof(tmpfil));
741 if ((fp = fopen (file, "r")) == NULL)
742 padios (file, "unable to read");
744 ioctl (fileno (fp), FIOCLEX, NULL);
746 if (fstat (fileno (fp), &st) != NOTOK) {
747 mode = (int) (st.st_mode & 0777), mtime = st.st_mtime;
748 msgp = read_map (file, (long) st.st_size);
751 mode = m_gmprot (), mtime = 0;
755 if ((msgp = read_file (msgp ? Msgs[msgp].m_stop : 0L, msgp + 1)) < 1)
756 padios (NULL, "no messages in %s", myname ? myname : file);
758 if (!(mp = (struct msgs *) calloc ((size_t) 1, sizeof(*mp))))
759 padios (NULL, "unable to allocate folder storage");
761 if (!(mp->msgstats = calloc ((size_t) msgp + 3, sizeof(*(mp->msgstats)))))
762 padios (NULL, "unable to allocate message status storage");
768 mp->foldpath = getcpy (myname ? myname : file);
769 clear_folder_flags (mp);
777 if (st.st_uid != getuid () || access (file, W_OK) == NOTOK)
784 mp->hghoff = mp->hghmsg + 1;
789 for (i = mp->lowmsg; i <= mp->hghmsg; i++) {
791 clear_msg_flags (mp, i);
796 for (i = mp->lowmsg; i <= mp->hghmsg; i++) {
797 if (Msgs[i].m_top) /* set in read_pop() */
798 clear_msg_flags (mp, i);
806 for (i = mp->lowmsg; i <= mp->hghmsg; i++) {
807 clear_msg_flags (mp, i);
812 mp->msgattrs[0] = getcpy ("unseen");
813 mp->msgattrs[1] = NULL;
815 m_unknown (fp); /* the MAGIC invocation */
824 read_map (char *file, long size)
826 register int i, msgp;
827 register struct drop *dp, *mp;
835 if ((i = map_read (file, size, &rp, 1)) == 0)
841 for (dp = rp + 1; i-- > 0; msgp++, dp++) {
842 mp = &Msgs[msgp].m_drop;
844 mp->d_size = dp->d_size;
845 mp->d_start = dp->d_start;
846 mp->d_stop = dp->d_stop;
847 Msgs[msgp].m_scanl = NULL;
856 read_file (long pos, int msgp)
859 register struct drop *dp, *mp;
867 if ((i = mbx_read (fp, pos, &rp, 1)) <= 0)
870 m_gMsgs ((msgp - 1) + i);
872 for (dp = rp; i-- > 0; msgp++, dp++) {
873 mp = &Msgs[msgp].m_drop;
875 mp->d_size = dp->d_size;
876 mp->d_start = dp->d_start;
877 mp->d_stop = dp->d_stop;
878 Msgs[msgp].m_scanl = NULL;
888 static int pop_base = 0;
891 pop_statmsg (char *s)
895 n = (i = atoi (s)) - pop_base; /* s="nnn header-line..." */
896 Msgs[n].m_top = Msgs[n].m_bboard_id = i;
906 if (pop_stat (&nmsgs, &nbytes) == NOTOK)
907 padios (NULL, "%s", response);
911 #ifdef NNTP /* this makes read_pop() do some real work... */
912 pop_base = nbytes - 1; /* nmsgs=last-first+1, nbytes=first */
913 pop_exists (pop_statmsg);
922 fprintf (yp, "%s\n", s);
933 nMsgs = n + MAXFOLDER / 2;
934 Msgs = (struct Msg *) calloc ((size_t) (nMsgs + 2), sizeof *Msgs);
936 padios (NULL, "unable to allocate Msgs structure");
943 nmsgs = nMsgs + n + MAXFOLDER / 2;
944 Msgs = (struct Msg *) mh_xrealloc ((char *) Msgs, (size_t) (nmsgs + 2) * sizeof *Msgs);
945 memset((char *) (Msgs + nMsgs + 2), 0, (size_t) ((nmsgs - nMsgs) * sizeof *Msgs));
952 msh_ready (int msgnum, int full)
968 if ((fd = Msgs[msgnum].m_top) == NOTOK) {
969 if (numfds >= maxfds)
970 for (msgp = mp->lowmsg; msgp <= mp->hghmsg; msgp++)
971 if (Msgs[msgp].m_top != NOTOK) {
972 close (Msgs[msgp].m_top);
973 Msgs[msgp].m_top = NOTOK;
978 if ((fd = open (cp = m_name (msgnum), O_RDONLY)) == NOTOK)
979 padios (cp, "unable to open message");
980 Msgs[msgnum].m_top = fd;
984 if ((fd = dup (fd)) == NOTOK)
985 padios ("cached message", "unable to dup");
986 if ((yp = fdopen (fd, "r")) == NULL)
987 padios (NULL, "unable to fdopen cached message");
988 fseek (yp, 0L, SEEK_SET);
993 if (pmsh && is_virtual (mp, msgnum)) {
994 if (Msgs[msgnum].m_top == 0)
995 padios (NULL, "msh_ready (%d, %d) botch", msgnum, full);
997 char *tfile = m_mktemp2(NULL, invo_name, NULL, &yp);
998 if (tfile == NULL) padios("msh", "unable to create temporary file");
1000 strncpy(tmpfil, tfile, sizeof(tmpfil));
1002 if (pop_top (Msgs[msgnum].m_top, 4, pop_action) == NOTOK)
1003 padios (NULL, "%s", response);
1005 m_eomsbr ((int (*)()) 0); /* XXX */
1006 msg_style = MS_DEFAULT; /* .. */
1007 fseek (yp, 0L, SEEK_SET);
1011 fseek (fp, 0L, SEEK_END);
1012 fwrite (mmdlm1, 1, strlen (mmdlm1), fp);
1014 padios ("temporary file", "write error on");
1015 fseek (fp, 0L, SEEK_END);
1019 if (pop_retr (Msgs[msgnum].m_top, pop_action) == NOTOK)
1020 padios (NULL, "%s", response);
1023 fseek (fp, 0L, SEEK_END);
1025 fwrite (mmdlm2, 1, strlen (mmdlm2), fp);
1027 padios ("temporary file", "write error on");
1029 Msgs[msgnum].m_start = pos1;
1030 Msgs[msgnum].m_stop = pos2;
1032 unset_virtual (mp, msgnum);
1036 m_eomsbr ((int (*)()) 0); /* XXX */
1037 fseek (fp, Msgs[msgnum].m_start, SEEK_SET);
1043 check_folder (int scansw)
1045 int seqnum, i, low, hgh, msgp;
1054 if (stat (mp->foldpath, &st) == NOTOK)
1055 padios (mp->foldpath, "unable to stat");
1056 if (mtime == st.st_mtime)
1058 mtime = st.st_mtime;
1060 low = mp->hghmsg + 1;
1061 folder_free (mp); /* free folder/message structure */
1063 if (!(mp = folder_read (fmsh)))
1064 padios (NULL, "unable to re-read folder %s", fmsh);
1068 for (msgp = mp->lowmsg; msgp <= mp->hghmsg; msgp++) {
1069 if (Msgs[msgp].m_top != NOTOK) {
1070 close (Msgs[msgp].m_top);
1071 Msgs[msgp].m_top = NOTOK;
1074 if (Msgs[msgp].m_scanl) {
1075 free (Msgs[msgp].m_scanl);
1076 Msgs[msgp].m_scanl = NULL;
1082 if (modified || low > hgh)
1086 if (fstat (fileno (fp), &st) == NOTOK)
1087 padios (mp->foldpath, "unable to fstat");
1088 if (mtime == st.st_mtime)
1090 mode = (int) (st.st_mode & 0777);
1091 mtime = st.st_mtime;
1093 if ((msgp = read_file (Msgs[mp->hghmsg].m_stop, mp->hghmsg + 1)) < 1)
1094 padios (NULL, "no messages in %s", mp->foldpath); /* XXX */
1095 if (msgp >= MAXFOLDER)
1096 padios (NULL, "more than %d messages in %s", MAXFOLDER,
1098 if (msgp <= mp->hghmsg)
1101 if (!(mp = folder_realloc (mp, mp->lowoff, msgp)))
1102 padios (NULL, "unable to allocate folder storage");
1104 low = mp->hghmsg + 1, hgh = msgp;
1105 seqnum = scansw ? seq_getnum (mp, "unseen") : -1;
1106 for (i = mp->hghmsg + 1; i <= msgp; i++) {
1109 add_sequence(mp, seqnum, i);
1119 advise (NULL, "new messages have arrived!\007");
1121 scanrange (low, hgh);
1128 scanrange (int low, int hgh)
1130 char buffer[BUFSIZ];
1132 snprintf (buffer, sizeof(buffer), "%d-%d", low, hgh);
1133 scanstring (buffer);
1138 scanstring (char *arg)
1140 char *cp, **ap, *vec[MAXARGS];
1143 * This should be replace with a call to getarguments()
1145 if ((cp = context_find (cmd_name = "scan"))) {
1147 ap = brkstring (cp, " ", "\n");
1148 ap = copyip (ap, vec, MAXARGS);
1164 register int cur, seqnum, i=0, msgnum;
1166 if (mp->curmsg == 0)
1167 seq_setcur (mp, mp->lowmsg);
1168 if (id <= 0 || (seqnum = seq_getnum (mp, "unseen")) == -1)
1171 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
1172 add_sequence(mp, seqnum, msgnum);
1177 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
1178 if (does_exist(mp, msgnum)) /* FIX */
1179 if ((i = readid (msgnum)) > 0 && i < id) {
1181 clear_sequence(mp, seqnum, msgnum);
1184 for (i = mp->lowmsg; i < msgnum; i++)
1185 clear_sequence(mp, seqnum, i);
1187 if (cur > mp->hghmsg)
1190 seq_setcur (mp, cur);
1193 if ((gap = 1 < id && id < (i = readid (mp->lowmsg)) ? id : 0) && !vmh)
1194 advise (NULL, "gap in ID:s, last seen %d, lowest present %d\n",
1203 char *bp, buf[BUFSIZ], name[NAMESZ];
1206 int arg1, arg2, arg3;
1209 if (Msgs[msgnum].m_bboard_id)
1210 return Msgs[msgnum].m_bboard_id;
1213 if (Msgs[msgnum].m_top == 0)
1214 padios (NULL, "readid (%d) botch", msgnum);
1215 if (pop_list (Msgs[msgnum].m_top, (int *) 0, &arg1, &arg2, &arg3) == OK
1217 return (Msgs[msgnum].m_bboard_id = arg3);
1221 zp = msh_ready (msgnum, 0);
1223 switch (state = m_getfld (state, name, buf, sizeof(buf), zp)) {
1227 if (!mh_strcasecmp (name, BBoard_ID)) {
1229 while (state == FLDPLUS) {
1230 state = m_getfld (state, name, buf, sizeof(buf), zp);
1236 return (Msgs[msgnum].m_bboard_id = i);
1240 while (state == FLDPLUS)
1241 state = m_getfld (state, name, buf, sizeof(buf), zp);
1242 if (state != FLDEOF)
1252 display_info (int scansw)
1256 interactive = isatty (fileno (stdout));
1258 if ((sd = dup (fileno (stdout))) == NOTOK)
1259 padios ("standard output", "unable to dup");
1260 #ifndef BSD42 /* XXX */
1262 ioctl (sd, FIOCLEX, NULL);
1263 #endif /* FIOCLEX */
1264 #endif /* not BSD42 */
1265 if ((sp = fdopen (sd, "w")) == NULL)
1266 padios ("standard output", "unable to fdopen");
1269 m_putenv ("mhfolder", mp->foldpath);
1274 printf ("Reading ");
1275 if (SOprintf ("%s", myname))
1276 printf ("%s", myname);
1277 printf (", currently at message %d of %d\n",
1278 mp->curmsg, mp->hghmsg);
1281 printf ("Reading ");
1283 printf ("+%s", fmsh);
1285 printf ("%s", mp->foldpath);
1286 printf (", currently at message %d of %d\n",
1287 mp->curmsg, mp->hghmsg);
1290 if (((seqnum = seq_getnum (mp, "unseen")) != -1)
1292 && in_sequence(mp, seqnum, mp->hghmsg))
1293 scanstring ("unseen");
1300 int i = 0, seqnum, msgnum;
1306 if ((seqnum = seq_getnum (mp, "unseen")) != -1)
1307 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
1308 if (!in_sequence(mp, seqnum, msgnum)) {
1309 if (Msgs[msgnum].m_bboard_id == 0)
1311 if ((i = Msgs[msgnum].m_bboard_id) > 0)
1315 snprintf (buffer, sizeof(buffer), "%d %d\n", i, Msgs[mp->hghmsg].m_bboard_id);
1316 write (pfd, buffer, sizeof(buffer));
1326 char *cp, tmpfil[BUFSIZ];
1327 char map1[BUFSIZ], map2[BUFSIZ];
1331 if (!(mp->msgflags & MODIFIED) || is_readonly(mp) || fmsh) {
1333 rc2peer (RC_FIN, 0, NULL);
1338 ttyNaux (NULLCMD, "FAST");
1340 if ((dp = lkfopen (mp->foldpath, "r")) == NULL) {
1341 advise (mp->foldpath, "unable to lock");
1348 if (fstat (fileno (dp), &st) == NOTOK) {
1349 advise (mp->foldpath, "unable to stat");
1352 if (mtime != st.st_mtime) {
1353 advise (NULL, "new messages have arrived, no update");
1356 mode = (int) (st.st_mode & 0777);
1358 if (mp->nummsg == 0) {
1359 cp = concat ("Zero file \"", mp->foldpath, "\"? ", NULL);
1360 if (getanswer (cp)) {
1361 if ((i = creat (mp->foldpath, mode)) != NOTOK)
1364 advise (mp->foldpath, "error zero'ing");
1365 unlink (map_name (mp->foldpath));/* XXX */
1370 cp = concat ("Update file \"", mp->foldpath, "\"? ", NULL);
1371 if (!getanswer (cp))
1373 strncpy (tmpfil, m_backup (mp->foldpath), sizeof(tmpfil));
1374 if ((md = mbx_open (tmpfil, mbx_style, st.st_uid, st.st_gid, mode)) == NOTOK) {
1375 advise (tmpfil, "unable to open");
1379 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
1380 if (does_exist(mp, msgnum) && pack (tmpfil, md, msgnum) == NOTOK) {
1381 mbx_close (tmpfil, md);
1383 unlink (map_name (tmpfil));
1386 mbx_close (tmpfil, md);
1388 if (rename (tmpfil, mp->foldpath) == NOTOK)
1389 admonish (mp->foldpath, "unable to rename %s to", tmpfil);
1391 strncpy (map1, map_name (tmpfil), sizeof(map1));
1392 strncpy (map2, map_name (mp->foldpath), sizeof(map2));
1394 if (rename (map1, map2) == NOTOK) {
1395 admonish (map2, "unable to rename %s to", map1);
1404 lkfclose (dp, mp->foldpath);
1413 getargs (char *prompt, struct swit *sw, struct Cmd *cmdp)
1417 static char buffer[BUFSIZ];
1423 switch (setjmp (sigenv)) {
1430 if (interrupted && !told_to_quit) {
1436 kill (ppid, SIGEMT);
1438 kill (ppid, SIGTERM);
1444 printf ("%s", prompt);
1447 for (cp = buffer; (i = getchar ()) != '\n';) {
1449 if (interrupted && !told_to_quit) {
1454 if (told_to_quit || i == EOF) {
1457 kill (ppid, SIGEMT);
1459 kill (ppid, SIGTERM);
1465 longjmp (sigenv, DONE);
1467 if (cp < &buffer[sizeof buffer - 2])
1474 if (buffer[0] == '?') {
1475 printf ("commands:\n");
1476 print_sw (ALL, sw, "", stdout);
1477 printf ("type CTRL-D or use ``quit'' to leave %s\n",
1482 if (parse (buffer, cmdp) == NOTOK)
1485 switch (i = smatch (cmdp->args[0], sw)) {
1487 ambigsw (cmdp->args[0], sw);
1490 printf ("say what: ``%s'' -- type ? (or help) for help\n",
1504 getcmds (struct swit *sw, struct Cmd *cmdp, int scansw)
1507 struct record rcs, *rc;
1513 switch (peer2rc (rc)) {
1515 pQRY (rc->rc_data, scansw);
1519 if ((i = pCMD (rc->rc_data, sw, cmdp)) != NOTOK)
1526 kill (ppid, SIGEMT);
1528 kill (ppid, SIGTERM);
1533 padios (NULL, "%s", rc->rc_data);
1536 fmt2peer (RC_ERR, "pLOOP protocol screw-up");
1543 parse (char *buffer, struct Cmd *cmdp)
1546 unsigned char c, *cp;
1550 pp = cmdp->args[argp++] = cmdp->line;
1551 cmdp->redirect = NULL;
1552 cmdp->direction = STDIO;
1553 cmdp->stream = NULL;
1555 for (cp = buffer; (c = *cp); cp++) {
1561 fmt2peer (RC_EOF, "null command");
1565 while ((c = *cp++)) {
1572 cmdp->args[argp++] = pp;
1579 switch (c = *cp++) {
1581 padvise (NULL, "unmatched \"");
1586 if ((c = *cp++) == 0)
1597 if ((c = *cp++) == 0) {
1599 padvise (NULL, "the newline character can not be quoted");
1609 if (pp == cmdp->line) {
1610 padvise (NULL, "invalid null command");
1613 if (*cmdp->args[argp - 1] == 0)
1615 cmdp->direction = c == '>' ? CRTIO : PIPIO;
1616 if (cmdp->direction == CRTIO && (c = *cp) == '>') {
1617 cmdp->direction = APPIO;
1620 cmdp->redirect = pp + 1;/* sigh */
1621 for (; (c = *cp); cp++)
1625 padvise (NULL, cmdp->direction != PIPIO
1626 ? "missing name for redirect"
1627 : "invalid null command");
1630 strcpy (cmdp->redirect, cp);
1631 if (cmdp->direction != PIPIO) {
1633 if (isspace (*cp)) {
1634 padvise (NULL, "bad name for redirect");
1637 if (expand (cmdp->redirect) == NOTOK)
1646 cmdp->args[argp] = NULL;
1653 expand (char *redirect)
1659 if (*redirect != '~')
1662 if ((cp = strchr(pp = redirect + 1, '/')))
1667 if ((pw = getpwnam (pp)))
1670 padvise (NULL, "unknown user: %s", pp);
1674 snprintf (path, sizeof(path), "%s/%s", pp, cp ? cp : "");
1675 strcpy (redirect, path);
1681 init_io (struct Cmd *cmdp, int vio)
1688 result = initaux_io (cmdp);
1696 initaux_io (struct Cmd *cmdp)
1700 switch (cmdp->direction) {
1706 mode = cmdp->direction == CRTIO ? "write" : "append";
1707 if ((cmdp->stream = fopen (cmdp->redirect, mode)) == NULL) {
1708 padvise (cmdp->redirect, "unable to %s ", mode);
1709 cmdp->direction = STDIO;
1715 if ((cmdp->stream = popen (cmdp->redirect, "w")) == NULL) {
1716 padvise (cmdp->redirect, "unable to pipe");
1717 cmdp->direction = STDIO;
1720 SIGNAL (SIGPIPE, pipeser);
1725 padios (NULL, "unknown redirection for command");
1729 if (dup2 (fileno (cmdp->stream), fileno (stdout)) == NOTOK)
1730 padios ("standard output", "unable to dup2");
1738 fin_io (struct Cmd *cmdp, int vio)
1750 finaux_io (struct Cmd *cmdp)
1752 switch (cmdp->direction) {
1759 close (fileno (stdout));
1760 if (ferror (stdout))
1761 padvise (NULL, "problems writing %s", cmdp->redirect);
1762 fclose (cmdp->stream);
1767 close (fileno (stdout));
1768 pclose (cmdp->stream);
1769 SIGNAL (SIGPIPE, SIG_DFL);
1773 padios (NULL, "unknown redirection for command");
1776 if (dup2 (fileno (sp), fileno (stdout)) == NOTOK)
1777 padios ("standard output", "unable to dup2");
1780 cmdp->direction = STDIO;
1789 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
1790 unset_selected (mp, msgnum);
1791 mp->lowsel = mp->hghsel = mp->numsel = 0;
1799 folder_free (mp); /* free folder/message structure */
1811 seq_setcur (struct msgs *mp, int msgnum)
1813 if (mp->curmsg == msgnum)
1816 if (mp->curmsg && Msgs[mp->curmsg].m_scanl) {
1817 free (Msgs[mp->curmsg].m_scanl);
1818 Msgs[mp->curmsg].m_scanl = NULL;
1820 if (Msgs[msgnum].m_scanl) {
1821 free (Msgs[msgnum].m_scanl);
1822 Msgs[msgnum].m_scanl = NULL;
1825 mp->curmsg = msgnum;
1833 #ifndef RELIABLE_SIGNALS
1834 SIGNAL (SIGINT, intrser);
1842 longjmp (sigenv, NOTOK);
1850 #ifndef RELIABLE_SIGNALS
1851 SIGNAL (SIGPIPE, pipeser);
1854 if (broken_pipe++ == 0)
1855 fprintf (stderr, "broken pipe\n");
1861 longjmp (sigenv, NOTOK);
1869 #ifndef RELIABLE_SIGNALS
1870 SIGNAL (SIGQUIT, quitser);
1878 longjmp (sigenv, NOTOK);
1886 longjmp (peerenv, DONE);
1895 struct record rcs, *rc;
1900 switch (peer2rc (rc)) {
1903 while (isspace (*bp))
1905 if (sscanf (bp, "%d", &vrsn) != 1) {
1907 fmt2peer (RC_ERR, "bad init \"%s\"", rc->rc_data);
1910 if (vrsn != RC_VRSN) {
1911 fmt2peer (RC_ERR, "version %d unsupported", vrsn);
1915 while (*bp && !isspace (*bp))
1917 while (isspace (*bp))
1919 if (sscanf (bp, "%d", &numwins) != 1 || numwins <= 0)
1924 for (i = 1; i <= numwins; i++) {
1925 while (*bp && !isspace (*bp))
1927 while (isspace (*bp))
1929 if (sscanf (bp, "%d", &windows[i]) != 1 || windows[i] <= 0)
1932 rc2peer (RC_ACK, 0, NULL);
1936 padios (NULL, "%s", rc->rc_data);
1939 fmt2peer (RC_ERR, "pINI protocol screw-up");
1940 done (1); /* NOTREACHED */
1943 return 1; /* dead code to satisfy the compiler */
1948 pQRY (char *str, int scansw)
1950 if (pQRY1 (scansw) == NOTOK || pQRY2 () == NOTOK)
1953 rc2peer (RC_EOF, 0, NULL);
1962 static int lastlow = 0,
1967 oldhgh = mp->hghmsg;
1968 if (check_folder (scansw) && oldhgh < mp->hghmsg) {
1969 switch (winX (STATUS)) {
1974 printf ("new messages have arrived!");
1977 _exit (0); /* NOTREACHED */
1980 lastlow = lastcur = lasthgh = lastnum = 0;
1984 switch (winX (DISPLAY)) {
1989 scanrange (oldhgh + 1, mp->hghmsg);
1992 _exit (0); /* NOTREACHED */
2001 switch (winX (STATUS)) {
2006 printf ("%s: gap in ID:s, last seen %d, lowest present %d\n",
2007 myname ? myname : fmsh ? fmsh : mp->foldpath, gap - 1,
2008 readid (mp->lowmsg));
2011 _exit (0); /* NOTREACHED */
2018 if (mp->lowmsg != lastlow
2019 || mp->curmsg != lastcur
2020 || mp->hghmsg != lasthgh
2021 || mp->nummsg != lastnum)
2022 switch (winX (STATUS)) {
2030 _exit (0); /* NOTREACHED */
2033 lastlow = mp->lowmsg;
2034 lastcur = mp->curmsg;
2035 lasthgh = mp->hghmsg;
2036 lastnum = mp->nummsg;
2047 int i, j, k, msgnum, n;
2053 if (mp->nummsg == 0 && mp->nummsg != num)
2054 switch (winX (SCAN)) {
2062 _exit (0); /* NOTREACHED */
2071 j = (k = windows[SCAN]) / 2;
2072 for (msgnum = mp->curmsg; msgnum <= mp->hghmsg; msgnum++)
2073 if (does_exist (mp, msgnum))
2077 k = i >= k ? 1 : k - i;
2084 for (msgnum = mp->curmsg; msgnum >= mp->lowmsg; msgnum--)
2085 if (does_exist (mp, msgnum)) {
2092 for (msgnum = mp->curmsg + 1; msgnum <= mp->hghmsg; msgnum++)
2093 if (does_exist (mp, msgnum)) {
2097 if (n++ >= windows[SCAN])
2103 && does_exist (mp, lo)
2104 && does_exist (mp, hi)
2106 || (lo == mp->curmsg && lo == mp->lowmsg))
2108 || (hi == mp->curmsg && hi == mp->hghmsg))
2109 && hi - lo == j - i)
2112 if (mp->curmsg != cur || modified)
2113 switch (winN (NULLCMD, SCAN, 0)) {
2121 scanrange (lo = i, hi = j);
2132 pCMD (char *str, struct swit *sw, struct Cmd *cmdp)
2137 switch (winX (DISPLAY)) {
2142 printf ("commands:\n");
2143 print_sw (ALL, sw, "", stdout);
2144 printf ("type ``quit'' to leave %s\n", invo_name);
2147 _exit (0); /* NOTREACHED */
2150 rc2peer (RC_EOF, 0, NULL);
2154 if (parse (str, cmdp) == NOTOK)
2157 switch (i = smatch (cmdp->args[0], sw)) {
2159 switch (winX (DISPLAY)) {
2164 ambigsw (cmdp->args[0], sw);
2167 _exit (0); /* NOTREACHED */
2170 rc2peer (RC_EOF, 0, NULL);
2176 "say what: ``%s'' -- type ? (or help) for help",
2191 switch (setjmp (peerenv)) {
2193 SIGNAL (SIGALRM, alrmser);
2196 status = peerwait ();
2210 struct record rcs, *rc;
2215 switch (peer2rc (rc)) {
2218 rc2peer (RC_FIN, 0, NULL);
2222 advise (NULL, "%s", rc->rc_data);
2226 fmt2peer (RC_FIN, "pLOOP protocol screw-up");
2233 ttyNaux (struct Cmd *cmdp, char *s)
2235 struct record rcs, *rc;
2240 if (cmdp && init_io (cmdp, vmh) == NOTOK)
2243 /* XXX: fseek() too tricky for our own good */
2245 fseek (fp, 0L, SEEK_SET);
2248 switch (rc2rc (RC_TTY, s ? strlen (s) : 0, s, rc)) {
2250 vmhtty = OK; /* fall */
2255 padios (NULL, "%s", rc->rc_data);/* NOTREACHED */
2258 fmt2peer (RC_ERR, "pTTY protocol screw-up");
2259 done (1); /* NOTREACHED */
2263 SIGNAL (SIGTSTP, tstat);
2270 ttyR (struct Cmd *cmdp)
2272 struct record rcs, *rc;
2277 SIGNAL (SIGTSTP, SIG_IGN);
2289 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2291 rc2peer (RC_EOF, 0, NULL);
2295 padios (NULL, "%s", rc->rc_data);/* NOTREACHED */
2298 fmt2peer (RC_ERR, "pTTY protocol screw-up");
2299 done (1); /* NOTREACHED */
2302 return 1; /* dead code to satisfy compiler */
2307 winN (struct Cmd *cmdp, int n, int eof)
2310 char buffer[BUFSIZ];
2311 struct record rcs, *rc;
2314 if (vmhpid == NOTOK)
2319 /* XXX: fseek() too tricky for our own good */
2321 fseek (fp, 0L, SEEK_SET);
2325 snprintf (buffer, sizeof(buffer), "%d", n);
2326 switch (str2rc (RC_WIN, buffer, rc)) {
2334 padios (NULL, "%s", rc->rc_data);
2337 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2341 if (pipe (pd) == NOTOK) {
2342 err2peer (RC_ERR, "pipe", "unable to");
2346 switch (vmhpid = fork()) {
2348 err2peer (RC_ERR, "fork", "unable to");
2355 SIGNAL (SIGPIPE, SIG_IGN);
2356 while ((i = read (pd[0], buffer, sizeof buffer)) > 0)
2357 switch (rc2rc (RC_DATA, i, buffer, rc)) {
2365 advise (NULL, "%s", rc->rc_data);
2369 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2373 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2376 rc2peer (RC_EOF, 0, NULL);
2381 advise (NULL, "%s", rc->rc_data);
2386 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2391 err2peer (RC_ERR, "pipe", "error reading from");
2393 _exit (i != NOTOK ? i : 1);
2396 if ((vmhfd0 = dup (fileno (stdin))) == NOTOK)
2397 padios ("standard input", "unable to dup");
2398 if ((vmhfd1 = dup (fileno (stdout))) == NOTOK)
2399 padios ("standard output", "unable to dup");
2400 if ((vmhfd2 = dup (fileno (stderr))) == NOTOK)
2401 padios ("diagnostic output", "unable to dup");
2404 if ((i = open ("/dev/null", O_RDONLY)) != NOTOK && i != fileno (stdin)) {
2405 dup2 (i, fileno (stdin));
2410 if (dup2 (pd[1], fileno (stdout)) == NOTOK)
2411 padios ("standard output", "unable to dup2");
2415 if (dup2 (pd[1], fileno (stderr)) == NOTOK)
2416 padios ("diagnostic output", "unable to dup2");
2419 if (cmdp && init_io (cmdp, 0) == NOTOK)
2421 pstat = SIGNAL (SIGPIPE, pipeser);
2433 winR (struct Cmd *cmdp)
2443 if (dup2 (vmhfd0, fileno (stdin)) == NOTOK)
2444 padios ("standard input", "unable to dup2");
2449 if (dup2 (vmhfd1, fileno (stdout)) == NOTOK)
2450 padios ("standard output", "unable to dup2");
2455 if (dup2 (vmhfd2, fileno (stderr)) == NOTOK)
2456 padios ("diagnostic output", "unable to dup2");
2460 SIGNAL (SIGPIPE, pstat);
2462 if ((status = pidwait (vmhpid, OK)) == 2)
2466 return (status == 0 ? OK : NOTOK);
2474 char buffer[BUFSIZ];
2475 struct record rcs, *rc;
2480 /* XXX: fseek() too tricky for our own good */
2482 fseek (fp, 0L, SEEK_SET);
2484 snprintf (buffer, sizeof(buffer), "%d", n);
2485 switch (str2rc (RC_WIN, buffer, rc)) {
2493 padios (NULL, "%s", rc->rc_data);
2496 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2500 if (pipe (pd) == NOTOK) {
2501 err2peer (RC_ERR, "pipe", "unable to");
2505 switch (pid = fork ()) {
2507 err2peer (RC_ERR, "fork", "unable to");
2513 close (fileno (stdin));
2514 if ((i = open ("/dev/null", O_RDONLY)) != NOTOK && i != fileno (stdin)) {
2515 dup2 (i, fileno (stdin));
2518 dup2 (pd[1], fileno (stdout));
2519 dup2 (pd[1], fileno (stderr));
2527 while ((i = read (pd[0], buffer, sizeof buffer)) > 0)
2528 switch (rc2rc (RC_DATA, i, buffer, rc)) {
2538 padios (NULL, "%s", rc->rc_data);
2541 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2545 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2550 padios (NULL, "%s", rc->rc_data);
2553 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2557 err2peer (RC_ERR, "pipe", "error reading from");
2561 return (i != NOTOK ? pid : NOTOK);
2567 padios (char *what, char *fmt, ...)
2573 verr2peer (RC_FIN, what, fmt, ap);
2576 advertise (what, NULL, fmt, ap);
2585 padvise (char *what, char *fmt, ...)
2591 verr2peer (RC_ERR, what, fmt, ap);
2593 advertise (what, NULL, fmt, ap);