3 * msh.c -- The nmh shell
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 * Keep more status information in maildrop map
19 #include <h/signals.h>
20 #include <h/dropsbr.h>
21 #include <h/fmt_scan.h>
22 #include <h/scansbr.h>
42 #define QUOTE '\\' /* sigh */
44 static struct swit switches[] = {
46 { "idstart number", -7 }, /* interface from bbc */
48 { "idstop number", -6 }, /* .. */
50 { "idquit number", -6 }, /* .. */
52 { "idname BBoard", -6 }, /* .. */
54 { "prompt string", 0 },
62 { "vmhwrite fd", -8 },
66 { "popwrite fd", -8 },
78 static int mbx_style = MMDF_FORMAT;
83 char*fmsh = NULL; /* folder instead of file */
84 int modified; /* command modified folder */
85 struct msgs *mp; /* used a lot */
87 struct Msg *Msgs = NULL; /* Msgs[0] not used */
88 static FILE *fp; /* input file */
89 static FILE *yp = NULL; /* temporary file */
90 static int mode; /* mode of file */
91 static int numfds = 0; /* number of files cached */
92 static int maxfds = 0; /* number of files cached to be cached */
93 static time_t mtime = (time_t) 0; /* mtime of file */
98 #define ALARM ((unsigned int) 10)
99 #define ttyN(c) ttyNaux ((c), NULL)
103 static int vmhpid = OK;
108 static int vmhtty = NOTOK;
115 static int topcur = 0;
117 static int numwins = 0;
118 static int windows[NWIN + 1];
120 static jmp_buf peerenv;
123 int pmsh = 0; /* BPOP enabled */
124 extern char response[];
130 static int pfd = NOTOK; /* fd parent is reading from */
131 static int ppid = 0; /* pid of parent */
136 int interactive; /* running from a /dev/tty */
137 int redirected; /* re-directing output */
138 FILE *sp = NULL; /* original stdout */
140 char *cmd_name; /* command being run */
141 char myfilter[BUFSIZ]; /* path to mhl.forward */
143 static char *myprompt = "(%s) ";/* prompting string */
148 static int gap; /* gap in BBoard-ID:s */
149 static char *myname = NULL; /* BBoard name */
150 char *BBoard_ID = "BBoard-ID"; /* BBoard-ID constant */
155 SIGNAL_HANDLER istat; /* original SIGINT */
156 static SIGNAL_HANDLER pstat; /* current SIGPIPE */
157 SIGNAL_HANDLER qstat; /* original SIGQUIT */
160 SIGNAL_HANDLER tstat; /* original SIGTSTP */
163 int interrupted; /* SIGINT detected */
164 int broken_pipe; /* SIGPIPE detected */
165 int told_to_quit; /* SIGQUIT detected */
168 int should_intr; /* signal handler should interrupt call */
169 jmp_buf sigenv; /* the environment pointer */
175 int SOprintf (char *, ...); /* from termsbr.c */
176 int sc_width (void); /* from termsbr.c */
177 void fsetup (char *);
179 FILE *msh_ready (int, int);
182 void display_info (int);
185 void seq_setcur (struct msgs *, int);
186 void padios (char *, char *, ...);
187 void padvise (char *, char *, ...);
193 static void msh (int);
194 static int read_map (char *, long);
195 static int read_file (long, int);
199 static int pop_statmsg (char *);
201 static int read_pop (void);
202 static int pop_action (char *);
205 static void m_gMsgs (int);
206 FILE *msh_ready (int, int);
207 static int check_folder (int);
208 static void scanrange (int, int);
209 static void scanstring (char *);
210 static void write_ids (void);
211 static void quit (void);
212 static int getargs (char *, struct swit *, struct Cmd *);
213 static int getcmds (struct swit *, struct Cmd *, int);
214 static int parse (char *, struct Cmd *);
215 static int init_io (struct Cmd *, int);
216 static int initaux_io (struct Cmd *);
217 static void fin_io (struct Cmd *, int);
218 static void finaux_io (struct Cmd *);
219 static void m_init (void);
220 static RETSIGTYPE intrser (int);
221 static RETSIGTYPE pipeser (int);
222 static RETSIGTYPE quitser (int);
223 static RETSIGTYPE alrmser (int);
224 static int pINI (void);
225 static int pQRY (char *, int);
226 static int pQRY1 (int);
227 static int pQRY2 (void);
228 static int pCMD (char *, struct swit *, struct Cmd *);
229 static int pFIN (void);
230 static int peerwait (void);
231 static int ttyNaux (struct Cmd *, char *);
232 static int ttyR (struct Cmd *);
233 static int winN (struct Cmd *, int, int);
234 static int winR (struct Cmd *);
235 static int winX (int);
239 main (int argc, char **argv)
241 int id = 0, scansw = 0, vmh1 = 0, vmh2 = 0;
242 char *cp, *file = NULL, *folder = NULL;
243 char **argp, **arguments, buf[BUFSIZ];
245 int pmsh1 = 0, pmsh2 = 0;
249 setlocale(LC_ALL, "");
251 invo_name = r1bindex (argv[0], '/');
253 /* read user profile/context */
256 mts_init (invo_name);
257 arguments = getarguments (invo_name, argc,argv, 1);
260 while ((cp = *argp++)) {
262 switch (smatch (++cp, switches)) {
264 ambigsw (cp, switches);
267 adios (NULL, "-%s unknown", cp);
270 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
271 print_help (buf, switches, 1);
274 print_version(invo_name);
278 if (!(cp = *argp++) || *cp == '-')
279 adios (NULL, "missing argument to %s", argp[-2]);
280 if ((id = atoi (cp)) < 1)
281 adios (NULL, "bad argument %s %s", argp[-2], cp);
284 if (!(cp = *argp++) || *cp == '-')
285 adios (NULL, "missing argument to %s", argp[-2]);
286 if ((pfd = atoi (cp)) <= 1)
287 adios (NULL, "bad argument %s %s", argp[-2], cp);
290 if (!(cp = *argp++) || *cp == '-')
291 adios (NULL, "missing argument to %s", argp[-2]);
292 if ((ppid = atoi (cp)) <= 1)
293 adios (NULL, "bad argument %s %s", argp[-2], cp);
296 if (!(myname = *argp++) || *myname == '-')
297 adios (NULL, "missing argument to %s", argp[-2]);
308 if (!(myprompt = *argp++) || *myprompt == '-')
309 adios (NULL, "missing argument to %s", argp[-2]);
313 if (!(cp = *argp++) || *cp == '-')
314 adios (NULL, "missing argument to %s", argp[-2]);
315 if ((vmh1 = atoi (cp)) < 1)
316 adios (NULL, "bad argument %s %s", argp[-2], cp);
319 if (!(cp = *argp++) || *cp == '-')
320 adios (NULL, "missing argument to %s", argp[-2]);
321 if ((vmh2 = atoi (cp)) < 1)
322 adios (NULL, "bad argument %s %s", argp[-2], cp);
326 if (!(cp = *argp++) || *cp == '-')
327 adios (NULL, "missing argument to %s", argp[-2]);
329 if ((pmsh1 = atoi (cp)) < 1)
330 adios (NULL, "bad argument %s %s", argp[-2], cp);
334 if (!(cp = *argp++) || *cp == '-')
335 adios (NULL, "missing argument to %s", argp[-2]);
337 if ((pmsh2 = atoi (cp)) < 1)
338 adios (NULL, "bad argument %s %s", argp[-2], cp);
349 if (*cp == '+' || *cp == '@') {
351 adios (NULL, "only one folder at a time!");
353 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
357 adios (NULL, "only one file at a time!");
362 if (!file && !folder)
365 adios (NULL, "use a file or a folder, not both");
366 strncpy (myfilter, etcpath (mhlforward), sizeof(myfilter));
369 ioctl (pfd, FIOCLEX, NULL);
375 istat = SIGNAL2 (SIGINT, intrser);
376 qstat = SIGNAL2 (SIGQUIT, quitser);
378 sc_width (); /* MAGIC... */
380 if ((vmh = vmh1 && vmh2)) {
383 SIGNAL (SIGINT, SIG_IGN);
384 SIGNAL (SIGQUIT, SIG_IGN);
386 tstat = SIGNAL (SIGTSTP, SIG_IGN);
391 if (pmsh = pmsh1 && pmsh2) {
392 cp = getenv ("MHPOPDEBUG");
394 if (pop_set (pmsh1, pmsh2, cp && *cp, myname) == NOTOK)
396 if (pop_set (pmsh1, pmsh2, cp && *cp) == NOTOK)
398 padios (NULL, "%s", response);
400 file = folder, folder = NULL;
409 display_info (id > 0 ? scansw : 0);
411 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 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
735 if ((fp = fopen (tmpfil, "w+")) == NULL)
736 padios (tmpfil, "unable to create");
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 *) realloc ((char *) Msgs, (size_t) (nmsgs + 2) * sizeof *Msgs);
946 padios (NULL, "unable to reallocate Msgs structure");
947 memset((char *) (Msgs + nMsgs + 2), 0, (size_t) ((nmsgs - nMsgs) * sizeof *Msgs));
954 msh_ready (int msgnum, int full)
970 if ((fd = Msgs[msgnum].m_top) == NOTOK) {
971 if (numfds >= maxfds)
972 for (msgp = mp->lowmsg; msgp <= mp->hghmsg; msgp++)
973 if (Msgs[msgp].m_top != NOTOK) {
974 close (Msgs[msgp].m_top);
975 Msgs[msgp].m_top = NOTOK;
980 if ((fd = open (cp = m_name (msgnum), O_RDONLY)) == NOTOK)
981 padios (cp, "unable to open message");
982 Msgs[msgnum].m_top = fd;
986 if ((fd = dup (fd)) == NOTOK)
987 padios ("cached message", "unable to dup");
988 if ((yp = fdopen (fd, "r")) == NULL)
989 padios (NULL, "unable to fdopen cached message");
990 fseek (yp, 0L, SEEK_SET);
995 if (pmsh && is_virtual (mp, msgnum)) {
996 if (Msgs[msgnum].m_top == 0)
997 padios (NULL, "msh_ready (%d, %d) botch", msgnum, full);
999 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
1000 if ((yp = fopen (tmpfil, "w+")) == NULL)
1001 padios (tmpfil, "unable to create");
1004 if (pop_top (Msgs[msgnum].m_top, 4, pop_action) == NOTOK)
1005 padios (NULL, "%s", response);
1007 m_eomsbr ((int (*)()) 0); /* XXX */
1008 msg_style = MS_DEFAULT; /* .. */
1009 fseek (yp, 0L, SEEK_SET);
1013 fseek (fp, 0L, SEEK_END);
1014 fwrite (mmdlm1, 1, strlen (mmdlm1), fp);
1016 padios ("temporary file", "write error on");
1017 fseek (fp, 0L, SEEK_END);
1021 if (pop_retr (Msgs[msgnum].m_top, pop_action) == NOTOK)
1022 padios (NULL, "%s", response);
1025 fseek (fp, 0L, SEEK_END);
1027 fwrite (mmdlm2, 1, strlen (mmdlm2), fp);
1029 padios ("temporary file", "write error on");
1031 Msgs[msgnum].m_start = pos1;
1032 Msgs[msgnum].m_stop = pos2;
1034 unset_virtual (mp, msgnum);
1038 m_eomsbr ((int (*)()) 0); /* XXX */
1039 fseek (fp, Msgs[msgnum].m_start, SEEK_SET);
1045 check_folder (int scansw)
1047 int seqnum, i, low, hgh, msgp;
1056 if (stat (mp->foldpath, &st) == NOTOK)
1057 padios (mp->foldpath, "unable to stat");
1058 if (mtime == st.st_mtime)
1060 mtime = st.st_mtime;
1062 low = mp->hghmsg + 1;
1063 folder_free (mp); /* free folder/message structure */
1065 if (!(mp = folder_read (fmsh)))
1066 padios (NULL, "unable to re-read folder %s", fmsh);
1070 for (msgp = mp->lowmsg; msgp <= mp->hghmsg; msgp++) {
1071 if (Msgs[msgp].m_top != NOTOK) {
1072 close (Msgs[msgp].m_top);
1073 Msgs[msgp].m_top = NOTOK;
1076 if (Msgs[msgp].m_scanl) {
1077 free (Msgs[msgp].m_scanl);
1078 Msgs[msgp].m_scanl = NULL;
1084 if (modified || low > hgh)
1088 if (fstat (fileno (fp), &st) == NOTOK)
1089 padios (mp->foldpath, "unable to fstat");
1090 if (mtime == st.st_mtime)
1092 mode = (int) (st.st_mode & 0777);
1093 mtime = st.st_mtime;
1095 if ((msgp = read_file (Msgs[mp->hghmsg].m_stop, mp->hghmsg + 1)) < 1)
1096 padios (NULL, "no messages in %s", mp->foldpath); /* XXX */
1097 if (msgp >= MAXFOLDER)
1098 padios (NULL, "more than %d messages in %s", MAXFOLDER,
1100 if (msgp <= mp->hghmsg)
1103 if (!(mp = folder_realloc (mp, mp->lowoff, msgp)))
1104 padios (NULL, "unable to allocate folder storage");
1106 low = mp->hghmsg + 1, hgh = msgp;
1107 seqnum = scansw ? seq_getnum (mp, "unseen") : -1;
1108 for (i = mp->hghmsg + 1; i <= msgp; i++) {
1111 add_sequence(mp, seqnum, i);
1121 advise (NULL, "new messages have arrived!\007");
1123 scanrange (low, hgh);
1130 scanrange (int low, int hgh)
1132 char buffer[BUFSIZ];
1134 snprintf (buffer, sizeof(buffer), "%d-%d", low, hgh);
1135 scanstring (buffer);
1140 scanstring (char *arg)
1142 char *cp, **ap, *vec[MAXARGS];
1145 * This should be replace with a call to getarguments()
1147 if ((cp = context_find (cmd_name = "scan"))) {
1149 ap = brkstring (cp, " ", "\n");
1150 ap = copyip (ap, vec, MAXARGS);
1166 register int cur, seqnum, i, msgnum;
1168 if (mp->curmsg == 0)
1169 seq_setcur (mp, mp->lowmsg);
1170 if (id <= 0 || (seqnum = seq_getnum (mp, "unseen")) == -1)
1173 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
1174 add_sequence(mp, seqnum, msgnum);
1179 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
1180 if (does_exist(mp, msgnum)) /* FIX */
1181 if ((i = readid (msgnum)) > 0 && i < id) {
1183 clear_sequence(mp, seqnum, msgnum);
1186 for (i = mp->lowmsg; i < msgnum; i++)
1187 clear_sequence(mp, seqnum, i);
1189 if (cur > mp->hghmsg)
1192 seq_setcur (mp, cur);
1195 if ((gap = 1 < id && id < (i = readid (mp->lowmsg)) ? id : 0) && !vmh)
1196 advise (NULL, "gap in ID:s, last seen %d, lowest present %d\n",
1205 char *bp, buf[BUFSIZ], name[NAMESZ];
1208 int arg1, arg2, arg3;
1211 if (Msgs[msgnum].m_bboard_id)
1212 return Msgs[msgnum].m_bboard_id;
1215 if (Msgs[msgnum].m_top == 0)
1216 padios (NULL, "readid (%d) botch", msgnum);
1217 if (pop_list (Msgs[msgnum].m_top, (int *) 0, &arg1, &arg2, &arg3) == OK
1219 return (Msgs[msgnum].m_bboard_id = arg3);
1223 zp = msh_ready (msgnum, 0);
1225 switch (state = m_getfld (state, name, buf, sizeof(buf), zp)) {
1229 if (!strcasecmp (name, BBoard_ID)) {
1231 while (state == FLDPLUS) {
1232 state = m_getfld (state, name, buf, sizeof(buf), zp);
1238 return (Msgs[msgnum].m_bboard_id = i);
1242 while (state == FLDPLUS)
1243 state = m_getfld (state, name, buf, sizeof(buf), zp);
1244 if (state != FLDEOF)
1254 display_info (int scansw)
1258 interactive = isatty (fileno (stdout));
1260 if ((sd = dup (fileno (stdout))) == NOTOK)
1261 padios ("standard output", "unable to dup");
1262 #ifndef BSD42 /* XXX */
1264 ioctl (sd, FIOCLEX, NULL);
1265 #endif /* FIOCLEX */
1266 #endif /* not BSD42 */
1267 if ((sp = fdopen (sd, "w")) == NULL)
1268 padios ("standard output", "unable to fdopen");
1271 m_putenv ("mhfolder", mp->foldpath);
1276 printf ("Reading ");
1277 if (SOprintf ("%s", myname))
1278 printf ("%s", myname);
1279 printf (", currently at message %d of %d\n",
1280 mp->curmsg, mp->hghmsg);
1283 printf ("Reading ");
1285 printf ("+%s", fmsh);
1287 printf ("%s", mp->foldpath);
1288 printf (", currently at message %d of %d\n",
1289 mp->curmsg, mp->hghmsg);
1292 if (((seqnum = seq_getnum (mp, "unseen")) != -1)
1294 && in_sequence(mp, seqnum, mp->hghmsg))
1295 scanstring ("unseen");
1302 int i = 0, seqnum, msgnum;
1308 if ((seqnum = seq_getnum (mp, "unseen")) != -1)
1309 for (msgnum = mp->hghmsg; msgnum >= mp->lowmsg; msgnum--)
1310 if (!in_sequence(mp, seqnum, msgnum)) {
1311 if (Msgs[msgnum].m_bboard_id == 0)
1313 if ((i = Msgs[msgnum].m_bboard_id) > 0)
1317 snprintf (buffer, sizeof(buffer), "%d %d\n", i, Msgs[mp->hghmsg].m_bboard_id);
1318 write (pfd, buffer, sizeof(buffer));
1328 char *cp, tmpfil[BUFSIZ];
1329 char map1[BUFSIZ], map2[BUFSIZ];
1333 if (!(mp->msgflags & MODIFIED) || is_readonly(mp) || fmsh) {
1335 rc2peer (RC_FIN, 0, NULL);
1340 ttyNaux (NULLCMD, "FAST");
1342 if ((dp = lkfopen (mp->foldpath, "r")) == NULL) {
1343 advise (mp->foldpath, "unable to lock");
1350 if (fstat (fileno (dp), &st) == NOTOK) {
1351 advise (mp->foldpath, "unable to stat");
1354 if (mtime != st.st_mtime) {
1355 advise (NULL, "new messages have arrived, no update");
1358 mode = (int) (st.st_mode & 0777);
1360 if (mp->nummsg == 0) {
1361 cp = concat ("Zero file \"", mp->foldpath, "\"? ", NULL);
1362 if (getanswer (cp)) {
1363 if ((i = creat (mp->foldpath, mode)) != NOTOK)
1366 advise (mp->foldpath, "error zero'ing");
1367 unlink (map_name (mp->foldpath));/* XXX */
1372 cp = concat ("Update file \"", mp->foldpath, "\"? ", NULL);
1373 if (!getanswer (cp))
1375 strncpy (tmpfil, m_backup (mp->foldpath), sizeof(tmpfil));
1376 if ((md = mbx_open (tmpfil, mbx_style, st.st_uid, st.st_gid, mode)) == NOTOK) {
1377 advise (tmpfil, "unable to open");
1381 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
1382 if (does_exist(mp, msgnum) && pack (tmpfil, md, msgnum) == NOTOK) {
1383 mbx_close (tmpfil, md);
1385 unlink (map_name (tmpfil));
1388 mbx_close (tmpfil, md);
1390 if (rename (tmpfil, mp->foldpath) == NOTOK)
1391 admonish (mp->foldpath, "unable to rename %s to", tmpfil);
1393 strncpy (map1, map_name (tmpfil), sizeof(map1));
1394 strncpy (map2, map_name (mp->foldpath), sizeof(map2));
1396 if (rename (map1, map2) == NOTOK) {
1397 admonish (map2, "unable to rename %s to", map1);
1406 lkfclose (dp, mp->foldpath);
1415 getargs (char *prompt, struct swit *sw, struct Cmd *cmdp)
1419 static char buffer[BUFSIZ];
1425 switch (setjmp (sigenv)) {
1432 if (interrupted && !told_to_quit) {
1438 kill (ppid, SIGEMT);
1440 kill (ppid, SIGTERM);
1446 printf ("%s", prompt);
1449 for (cp = buffer; (i = getchar ()) != '\n';) {
1451 if (interrupted && !told_to_quit) {
1456 if (told_to_quit || i == EOF) {
1459 kill (ppid, SIGEMT);
1461 kill (ppid, SIGTERM);
1467 longjmp (sigenv, DONE);
1469 if (cp < &buffer[sizeof buffer - 2])
1476 if (buffer[0] == '?') {
1477 printf ("commands:\n");
1478 print_sw (ALL, sw, "");
1479 printf ("type CTRL-D or use ``quit'' to leave %s\n",
1484 if (parse (buffer, cmdp) == NOTOK)
1487 switch (i = smatch (cmdp->args[0], sw)) {
1489 ambigsw (cmdp->args[0], sw);
1492 printf ("say what: ``%s'' -- type ? (or help) for help\n",
1506 getcmds (struct swit *sw, struct Cmd *cmdp, int scansw)
1509 struct record rcs, *rc;
1515 switch (peer2rc (rc)) {
1517 pQRY (rc->rc_data, scansw);
1521 if ((i = pCMD (rc->rc_data, sw, cmdp)) != NOTOK)
1528 kill (ppid, SIGEMT);
1530 kill (ppid, SIGTERM);
1535 padios (NULL, "%s", rc->rc_data);
1538 fmt2peer (RC_ERR, "pLOOP protocol screw-up");
1545 parse (char *buffer, struct Cmd *cmdp)
1551 pp = cmdp->args[argp++] = cmdp->line;
1552 cmdp->redirect = NULL;
1553 cmdp->direction = STDIO;
1554 cmdp->stream = NULL;
1556 for (cp = buffer; (c = *cp); cp++) {
1562 fmt2peer (RC_EOF, "null command");
1566 while ((c = *cp++)) {
1573 cmdp->args[argp++] = pp;
1580 switch (c = *cp++) {
1582 padvise (NULL, "unmatched \"");
1587 if ((c = *cp++) == 0)
1598 if ((c = *cp++) == 0) {
1600 padvise (NULL, "the newline character can not be quoted");
1610 if (pp == cmdp->line) {
1611 padvise (NULL, "invalid null command");
1614 if (*cmdp->args[argp - 1] == 0)
1616 cmdp->direction = c == '>' ? CRTIO : PIPIO;
1617 if (cmdp->direction == CRTIO && (c = *cp) == '>') {
1618 cmdp->direction = APPIO;
1621 cmdp->redirect = pp + 1;/* sigh */
1622 for (; (c = *cp); cp++)
1626 padvise (NULL, cmdp->direction != PIPIO
1627 ? "missing name for redirect"
1628 : "invalid null command");
1631 strcpy (cmdp->redirect, cp);
1632 if (cmdp->direction != PIPIO) {
1634 if (isspace (*cp)) {
1635 padvise (NULL, "bad name for redirect");
1638 if (expand (cmdp->redirect) == NOTOK)
1647 cmdp->args[argp] = NULL;
1654 expand (char *redirect)
1660 if (*redirect != '~')
1663 if ((cp = strchr(pp = redirect + 1, '/')))
1668 if ((pw = getpwnam (pp)))
1671 padvise (NULL, "unknown user: %s", pp);
1675 snprintf (path, sizeof(path), "%s/%s", pp, cp ? cp : "");
1676 strcpy (redirect, path);
1682 init_io (struct Cmd *cmdp, int vio)
1689 result = initaux_io (cmdp);
1697 initaux_io (struct Cmd *cmdp)
1701 switch (cmdp->direction) {
1707 mode = cmdp->direction == CRTIO ? "write" : "append";
1708 if ((cmdp->stream = fopen (cmdp->redirect, mode)) == NULL) {
1709 padvise (cmdp->redirect, "unable to %s ", mode);
1710 cmdp->direction = STDIO;
1716 if ((cmdp->stream = popen (cmdp->redirect, "w")) == NULL) {
1717 padvise (cmdp->redirect, "unable to pipe");
1718 cmdp->direction = STDIO;
1721 SIGNAL (SIGPIPE, pipeser);
1726 padios (NULL, "unknown redirection for command");
1730 if (dup2 (fileno (cmdp->stream), fileno (stdout)) == NOTOK)
1731 padios ("standard output", "unable to dup2");
1739 fin_io (struct Cmd *cmdp, int vio)
1751 finaux_io (struct Cmd *cmdp)
1753 switch (cmdp->direction) {
1760 close (fileno (stdout));
1761 if (ferror (stdout))
1762 padvise (NULL, "problems writing %s", cmdp->redirect);
1763 fclose (cmdp->stream);
1768 close (fileno (stdout));
1769 pclose (cmdp->stream);
1770 SIGNAL (SIGPIPE, SIG_DFL);
1774 padios (NULL, "unknown redirection for command");
1777 if (dup2 (fileno (sp), fileno (stdout)) == NOTOK)
1778 padios ("standard output", "unable to dup2");
1781 cmdp->direction = STDIO;
1790 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
1791 unset_selected (mp, msgnum);
1792 mp->lowsel = mp->hghsel = mp->numsel = 0;
1800 folder_free (mp); /* free folder/message structure */
1812 seq_setcur (struct msgs *mp, int msgnum)
1814 if (mp->curmsg == msgnum)
1817 if (mp->curmsg && Msgs[mp->curmsg].m_scanl) {
1818 free (Msgs[mp->curmsg].m_scanl);
1819 Msgs[mp->curmsg].m_scanl = NULL;
1821 if (Msgs[msgnum].m_scanl) {
1822 free (Msgs[msgnum].m_scanl);
1823 Msgs[msgnum].m_scanl = NULL;
1826 mp->curmsg = msgnum;
1834 #ifndef RELIABLE_SIGNALS
1835 SIGNAL (SIGINT, intrser);
1843 longjmp (sigenv, NOTOK);
1851 #ifndef RELIABLE_SIGNALS
1852 SIGNAL (SIGPIPE, pipeser);
1855 if (broken_pipe++ == 0)
1856 fprintf (stderr, "broken pipe\n");
1862 longjmp (sigenv, NOTOK);
1870 #ifndef RELIABLE_SIGNALS
1871 SIGNAL (SIGQUIT, quitser);
1879 longjmp (sigenv, NOTOK);
1887 longjmp (peerenv, DONE);
1896 struct record rcs, *rc;
1901 switch (peer2rc (rc)) {
1904 while (isspace (*bp))
1906 if (sscanf (bp, "%d", &vrsn) != 1) {
1908 fmt2peer (RC_ERR, "bad init \"%s\"", rc->rc_data);
1911 if (vrsn != RC_VRSN) {
1912 fmt2peer (RC_ERR, "version %d unsupported", vrsn);
1916 while (*bp && !isspace (*bp))
1918 while (isspace (*bp))
1920 if (sscanf (bp, "%d", &numwins) != 1 || numwins <= 0)
1925 for (i = 1; i <= numwins; i++) {
1926 while (*bp && !isspace (*bp))
1928 while (isspace (*bp))
1930 if (sscanf (bp, "%d", &windows[i]) != 1 || windows[i] <= 0)
1933 rc2peer (RC_ACK, 0, NULL);
1937 padios (NULL, "%s", rc->rc_data);
1940 fmt2peer (RC_ERR, "pINI protocol screw-up");
1941 done (1); /* NOTREACHED */
1944 return 1; /* dead code to satisfy the compiler */
1949 pQRY (char *str, int scansw)
1951 if (pQRY1 (scansw) == NOTOK || pQRY2 () == NOTOK)
1954 rc2peer (RC_EOF, 0, NULL);
1963 static int lastlow = 0,
1968 oldhgh = mp->hghmsg;
1969 if (check_folder (scansw) && oldhgh < mp->hghmsg) {
1970 switch (winX (STATUS)) {
1975 printf ("new messages have arrived!");
1978 _exit (0); /* NOTREACHED */
1981 lastlow = lastcur = lasthgh = lastnum = 0;
1985 switch (winX (DISPLAY)) {
1990 scanrange (oldhgh + 1, mp->hghmsg);
1993 _exit (0); /* NOTREACHED */
2002 switch (winX (STATUS)) {
2007 printf ("%s: gap in ID:s, last seen %d, lowest present %d\n",
2008 myname ? myname : fmsh ? fmsh : mp->foldpath, gap - 1,
2009 readid (mp->lowmsg));
2012 _exit (0); /* NOTREACHED */
2019 if (mp->lowmsg != lastlow
2020 || mp->curmsg != lastcur
2021 || mp->hghmsg != lasthgh
2022 || mp->nummsg != lastnum)
2023 switch (winX (STATUS)) {
2031 _exit (0); /* NOTREACHED */
2034 lastlow = mp->lowmsg;
2035 lastcur = mp->curmsg;
2036 lasthgh = mp->hghmsg;
2037 lastnum = mp->nummsg;
2048 int i, j, k, msgnum, n;
2054 if (mp->nummsg == 0 && mp->nummsg != num)
2055 switch (winX (SCAN)) {
2063 _exit (0); /* NOTREACHED */
2072 j = (k = windows[SCAN]) / 2;
2073 for (msgnum = mp->curmsg; msgnum <= mp->hghmsg; msgnum++)
2074 if (does_exist (mp, msgnum))
2078 k = i >= k ? 1 : k - i;
2085 for (msgnum = mp->curmsg; msgnum >= mp->lowmsg; msgnum--)
2086 if (does_exist (mp, msgnum)) {
2093 for (msgnum = mp->curmsg + 1; msgnum <= mp->hghmsg; msgnum++)
2094 if (does_exist (mp, msgnum)) {
2098 if (n++ >= windows[SCAN])
2104 && does_exist (mp, lo)
2105 && does_exist (mp, hi)
2107 || (lo == mp->curmsg && lo == mp->lowmsg))
2109 || (hi == mp->curmsg && hi == mp->hghmsg))
2110 && hi - lo == j - i)
2113 if (mp->curmsg != cur || modified)
2114 switch (winN (NULLCMD, SCAN, 0)) {
2122 scanrange (lo = i, hi = j);
2133 pCMD (char *str, struct swit *sw, struct Cmd *cmdp)
2138 switch (winX (DISPLAY)) {
2143 printf ("commands:\n");
2144 print_sw (ALL, sw, "");
2145 printf ("type ``quit'' to leave %s\n", invo_name);
2148 _exit (0); /* NOTREACHED */
2151 rc2peer (RC_EOF, 0, NULL);
2155 if (parse (str, cmdp) == NOTOK)
2158 switch (i = smatch (cmdp->args[0], sw)) {
2160 switch (winX (DISPLAY)) {
2165 ambigsw (cmdp->args[0], sw);
2168 _exit (0); /* NOTREACHED */
2171 rc2peer (RC_EOF, 0, NULL);
2177 "say what: ``%s'' -- type ? (or help) for help",
2192 switch (setjmp (peerenv)) {
2194 SIGNAL (SIGALRM, alrmser);
2197 status = peerwait ();
2211 struct record rcs, *rc;
2216 switch (peer2rc (rc)) {
2219 rc2peer (RC_FIN, 0, NULL);
2223 advise (NULL, "%s", rc->rc_data);
2227 fmt2peer (RC_FIN, "pLOOP protocol screw-up");
2234 ttyNaux (struct Cmd *cmdp, char *s)
2236 struct record rcs, *rc;
2241 if (cmdp && init_io (cmdp, vmh) == NOTOK)
2244 /* XXX: fseek() too tricky for our own good */
2246 fseek (fp, 0L, SEEK_SET);
2249 switch (rc2rc (RC_TTY, s ? strlen (s) : 0, s, rc)) {
2251 vmhtty = OK; /* fall */
2256 padios (NULL, "%s", rc->rc_data);/* NOTREACHED */
2259 fmt2peer (RC_ERR, "pTTY protocol screw-up");
2260 done (1); /* NOTREACHED */
2264 SIGNAL (SIGTSTP, tstat);
2271 ttyR (struct Cmd *cmdp)
2273 struct record rcs, *rc;
2278 SIGNAL (SIGTSTP, SIG_IGN);
2290 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2292 rc2peer (RC_EOF, 0, NULL);
2296 padios (NULL, "%s", rc->rc_data);/* NOTREACHED */
2299 fmt2peer (RC_ERR, "pTTY protocol screw-up");
2300 done (1); /* NOTREACHED */
2303 return 1; /* dead code to satisfy compiler */
2308 winN (struct Cmd *cmdp, int n, int eof)
2311 char buffer[BUFSIZ];
2312 struct record rcs, *rc;
2315 if (vmhpid == NOTOK)
2320 /* XXX: fseek() too tricky for our own good */
2322 fseek (fp, 0L, SEEK_SET);
2326 snprintf (buffer, sizeof(buffer), "%d", n);
2327 switch (str2rc (RC_WIN, buffer, rc)) {
2335 padios (NULL, "%s", rc->rc_data);
2338 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2342 if (pipe (pd) == NOTOK) {
2343 err2peer (RC_ERR, "pipe", "unable to");
2347 switch (vmhpid = fork()) {
2349 err2peer (RC_ERR, "fork", "unable to");
2356 SIGNAL (SIGPIPE, SIG_IGN);
2357 while ((i = read (pd[0], buffer, sizeof buffer)) > 0)
2358 switch (rc2rc (RC_DATA, i, buffer, rc)) {
2366 advise (NULL, "%s", rc->rc_data);
2370 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2374 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2377 rc2peer (RC_EOF, 0, NULL);
2382 advise (NULL, "%s", rc->rc_data);
2387 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2392 err2peer (RC_ERR, "pipe", "error reading from");
2394 _exit (i != NOTOK ? i : 1);
2397 if ((vmhfd0 = dup (fileno (stdin))) == NOTOK)
2398 padios ("standard input", "unable to dup");
2399 if ((vmhfd1 = dup (fileno (stdout))) == NOTOK)
2400 padios ("standard output", "unable to dup");
2401 if ((vmhfd2 = dup (fileno (stderr))) == NOTOK)
2402 padios ("diagnostic output", "unable to dup");
2405 if ((i = open ("/dev/null", O_RDONLY)) != NOTOK && i != fileno (stdin)) {
2406 dup2 (i, fileno (stdin));
2411 if (dup2 (pd[1], fileno (stdout)) == NOTOK)
2412 padios ("standard output", "unable to dup2");
2416 if (dup2 (pd[1], fileno (stderr)) == NOTOK)
2417 padios ("diagnostic output", "unable to dup2");
2420 if (cmdp && init_io (cmdp, 0) == NOTOK)
2422 pstat = SIGNAL (SIGPIPE, pipeser);
2434 winR (struct Cmd *cmdp)
2444 if (dup2 (vmhfd0, fileno (stdin)) == NOTOK)
2445 padios ("standard input", "unable to dup2");
2450 if (dup2 (vmhfd1, fileno (stdout)) == NOTOK)
2451 padios ("standard output", "unable to dup2");
2456 if (dup2 (vmhfd2, fileno (stderr)) == NOTOK)
2457 padios ("diagnostic output", "unable to dup2");
2461 SIGNAL (SIGPIPE, pstat);
2463 if ((status = pidwait (vmhpid, OK)) == 2)
2467 return (status == 0 ? OK : NOTOK);
2475 char buffer[BUFSIZ];
2476 struct record rcs, *rc;
2481 /* XXX: fseek() too tricky for our own good */
2483 fseek (fp, 0L, SEEK_SET);
2485 snprintf (buffer, sizeof(buffer), "%d", n);
2486 switch (str2rc (RC_WIN, buffer, rc)) {
2494 padios (NULL, "%s", rc->rc_data);
2497 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2501 if (pipe (pd) == NOTOK) {
2502 err2peer (RC_ERR, "pipe", "unable to");
2506 switch (pid = fork ()) {
2508 err2peer (RC_ERR, "fork", "unable to");
2514 close (fileno (stdin));
2515 if ((i = open ("/dev/null", O_RDONLY)) != NOTOK && i != fileno (stdin)) {
2516 dup2 (i, fileno (stdin));
2519 dup2 (pd[1], fileno (stdout));
2520 dup2 (pd[1], fileno (stderr));
2528 while ((i = read (pd[0], buffer, sizeof buffer)) > 0)
2529 switch (rc2rc (RC_DATA, i, buffer, rc)) {
2539 padios (NULL, "%s", rc->rc_data);
2542 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2546 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2551 padios (NULL, "%s", rc->rc_data);
2554 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2558 err2peer (RC_ERR, "pipe", "error reading from");
2562 return (i != NOTOK ? pid : NOTOK);
2568 padios (char *what, char *fmt, ...)
2574 verr2peer (RC_FIN, what, fmt, ap);
2577 advertise (what, NULL, fmt, ap);
2586 padvise (char *what, char *fmt, ...)
2592 verr2peer (RC_ERR, what, fmt, ap);
2594 advertise (what, NULL, fmt, ap);