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>
43 #define QUOTE '\\' /* sigh */
45 static struct swit switches[] = {
47 { "idstart number", -7 }, /* interface from bbc */
49 { "idstop number", -6 }, /* .. */
51 { "idquit number", -6 }, /* .. */
53 { "idname BBoard", -6 }, /* .. */
55 { "prompt string", 0 },
63 { "vmhwrite fd", -8 },
67 { "popwrite fd", -8 },
79 static int mbx_style = MMDF_FORMAT;
84 char*fmsh = NULL; /* folder instead of file */
85 int modified; /* command modified folder */
86 struct msgs *mp; /* used a lot */
88 struct Msg *Msgs = NULL; /* Msgs[0] not used */
89 static FILE *fp; /* input file */
90 static FILE *yp = NULL; /* temporary file */
91 static int mode; /* mode of file */
92 static int numfds = 0; /* number of files cached */
93 static int maxfds = 0; /* number of files cached to be cached */
94 static time_t mtime = (time_t) 0; /* mtime of file */
99 #define ALARM ((unsigned int) 10)
100 #define ttyN(c) ttyNaux ((c), NULL)
104 static int vmhpid = OK;
109 static int vmhtty = NOTOK;
116 static int topcur = 0;
118 static int numwins = 0;
119 static int windows[NWIN + 1];
121 static jmp_buf peerenv;
124 int pmsh = 0; /* BPOP enabled */
125 extern char response[];
131 static int pfd = NOTOK; /* fd parent is reading from */
132 static int ppid = 0; /* pid of parent */
137 int interactive; /* running from a /dev/tty */
138 int redirected; /* re-directing output */
139 FILE *sp = NULL; /* original stdout */
141 char *cmd_name; /* command being run */
142 char myfilter[BUFSIZ]; /* path to mhl.forward */
144 static char *myprompt = "(%s) ";/* prompting string */
149 static int gap; /* gap in BBoard-ID:s */
150 static char *myname = NULL; /* BBoard name */
151 char *BBoard_ID = "BBoard-ID"; /* BBoard-ID constant */
156 SIGNAL_HANDLER istat; /* original SIGINT */
157 static SIGNAL_HANDLER pstat; /* current SIGPIPE */
158 SIGNAL_HANDLER qstat; /* original SIGQUIT */
161 SIGNAL_HANDLER tstat; /* original SIGTSTP */
164 int interrupted; /* SIGINT detected */
165 int broken_pipe; /* SIGPIPE detected */
166 int told_to_quit; /* SIGQUIT detected */
169 int should_intr; /* signal handler should interrupt call */
170 jmp_buf sigenv; /* the environment pointer */
176 int SOprintf (char *, ...); /* from termsbr.c */
177 int sc_width (void); /* from termsbr.c */
178 void fsetup (char *);
180 FILE *msh_ready (int, int);
183 void display_info (int);
186 void seq_setcur (struct msgs *, int);
187 void padios (char *, char *, ...);
188 void padvise (char *, char *, ...);
194 static void msh (int);
195 static int read_map (char *, long);
196 static int read_file (long, int);
200 static int pop_statmsg (char *);
202 static int read_pop (void);
203 static int pop_action (char *);
206 static void m_gMsgs (int);
207 FILE *msh_ready (int, int);
208 static int check_folder (int);
209 static void scanrange (int, int);
210 static void scanstring (char *);
211 static void write_ids (void);
212 static void quit (void);
213 static int getargs (char *, struct swit *, struct Cmd *);
214 static int getcmds (struct swit *, struct Cmd *, int);
215 static int parse (char *, struct Cmd *);
216 static int init_io (struct Cmd *, int);
217 static int initaux_io (struct Cmd *);
218 static void fin_io (struct Cmd *, int);
219 static void finaux_io (struct Cmd *);
220 static void m_init (void);
221 static RETSIGTYPE intrser (int);
222 static RETSIGTYPE pipeser (int);
223 static RETSIGTYPE quitser (int);
224 static RETSIGTYPE alrmser (int);
225 static int pINI (void);
226 static int pQRY (char *, int);
227 static int pQRY1 (int);
228 static int pQRY2 (void);
229 static int pCMD (char *, struct swit *, struct Cmd *);
230 static int pFIN (void);
231 static int peerwait (void);
232 static int ttyNaux (struct Cmd *, char *);
233 static int ttyR (struct Cmd *);
234 static int winN (struct Cmd *, int, int);
235 static int winR (struct Cmd *);
236 static int winX (int);
240 main (int argc, char **argv)
242 int id = 0, scansw = 0, vmh1 = 0, vmh2 = 0;
243 char *cp, *file = NULL, *folder = NULL;
244 char **argp, **arguments, buf[BUFSIZ];
246 int pmsh1 = 0, pmsh2 = 0;
250 setlocale(LC_ALL, "");
252 invo_name = r1bindex (argv[0], '/');
254 /* read user profile/context */
257 mts_init (invo_name);
258 arguments = getarguments (invo_name, argc,argv, 1);
261 while ((cp = *argp++)) {
263 switch (smatch (++cp, switches)) {
265 ambigsw (cp, switches);
268 adios (NULL, "-%s unknown", cp);
271 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
272 print_help (buf, switches, 1);
275 print_version(invo_name);
279 if (!(cp = *argp++) || *cp == '-')
280 adios (NULL, "missing argument to %s", argp[-2]);
281 if ((id = atoi (cp)) < 1)
282 adios (NULL, "bad argument %s %s", argp[-2], cp);
285 if (!(cp = *argp++) || *cp == '-')
286 adios (NULL, "missing argument to %s", argp[-2]);
287 if ((pfd = atoi (cp)) <= 1)
288 adios (NULL, "bad argument %s %s", argp[-2], cp);
291 if (!(cp = *argp++) || *cp == '-')
292 adios (NULL, "missing argument to %s", argp[-2]);
293 if ((ppid = atoi (cp)) <= 1)
294 adios (NULL, "bad argument %s %s", argp[-2], cp);
297 if (!(myname = *argp++) || *myname == '-')
298 adios (NULL, "missing argument to %s", argp[-2]);
309 if (!(myprompt = *argp++) || *myprompt == '-')
310 adios (NULL, "missing argument to %s", argp[-2]);
314 if (!(cp = *argp++) || *cp == '-')
315 adios (NULL, "missing argument to %s", argp[-2]);
316 if ((vmh1 = atoi (cp)) < 1)
317 adios (NULL, "bad argument %s %s", argp[-2], cp);
320 if (!(cp = *argp++) || *cp == '-')
321 adios (NULL, "missing argument to %s", argp[-2]);
322 if ((vmh2 = atoi (cp)) < 1)
323 adios (NULL, "bad argument %s %s", argp[-2], cp);
327 if (!(cp = *argp++) || *cp == '-')
328 adios (NULL, "missing argument to %s", argp[-2]);
330 if ((pmsh1 = atoi (cp)) < 1)
331 adios (NULL, "bad argument %s %s", argp[-2], cp);
335 if (!(cp = *argp++) || *cp == '-')
336 adios (NULL, "missing argument to %s", argp[-2]);
338 if ((pmsh2 = atoi (cp)) < 1)
339 adios (NULL, "bad argument %s %s", argp[-2], cp);
350 if (*cp == '+' || *cp == '@') {
352 adios (NULL, "only one folder at a time!");
354 folder = pluspath (cp);
358 adios (NULL, "only one file at a time!");
363 if (!file && !folder)
366 adios (NULL, "use a file or a folder, not both");
367 strncpy (myfilter, etcpath (mhlforward), sizeof(myfilter));
370 ioctl (pfd, FIOCLEX, NULL);
376 istat = SIGNAL2 (SIGINT, intrser);
377 qstat = SIGNAL2 (SIGQUIT, quitser);
379 sc_width (); /* MAGIC... */
381 if ((vmh = vmh1 && vmh2)) {
384 SIGNAL (SIGINT, SIG_IGN);
385 SIGNAL (SIGQUIT, SIG_IGN);
387 tstat = SIGNAL (SIGTSTP, SIG_IGN);
392 if (pmsh = pmsh1 && pmsh2) {
393 cp = getenv ("MHPOPDEBUG");
395 if (pop_set (pmsh1, pmsh2, cp && *cp, myname) == NOTOK)
397 if (pop_set (pmsh1, pmsh2, cp && *cp) == NOTOK)
399 padios (NULL, "%s", response);
401 file = folder, folder = NULL;
410 display_info (id > 0 ? scansw : 0);
412 msh (id > 0 ? scansw : 0);
421 static struct swit mshcmds[] = {
486 register char *cp, **ap;
487 char prompt[BUFSIZ], *vec[MAXARGS];
489 register struct Cmd *cmdp;
490 static int once_only = ADVCMD;
492 snprintf (prompt, sizeof(prompt), myprompt, invo_name);
501 if ((i = getcmds (mshcmds, cmdp, scansw)) == EOF) {
506 check_folder (scansw);
507 if ((i = getargs (prompt, mshcmds, cmdp)) == EOF) {
512 cmd_name = mshcmds[i].sw;
520 if (once_only == ADVCMD)
521 once_only = i = SHOWCMD;
523 i = mp->curmsg != mp->hghmsg ? NEXTCMD : EXITCMD;
524 cmd_name = mshcmds[i].sw;
530 case FORWCMD: /* sigh */
540 if ((cp = context_find (cmd_name))) {
542 ap = brkstring (cp, " ", "\n");
543 ap = copyip (ap, vec, MAXARGS);
554 copyip (cmdp->args + 1, ap, MAXARGS);
558 if (!vmh && init_io (cmdp, vmh) == NOTOK) {
564 redirected = vmh || cmdp->direction != STDIO;
575 if (!vmh || ttyN (cmdp) != NOTOK)
576 forkcmd (vec, cmd_name);
580 if (!vmh || ttyN (cmdp) != NOTOK)
585 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
591 || (filehak (vec) == OK ? ttyN (cmdp)
592 : winN (cmdp, DISPLAY, 1)) != NOTOK)
597 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
602 if (!vmh || ttyN (cmdp) != NOTOK)
607 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
613 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
618 if (!vmh || ttyN (cmdp) != NOTOK)
625 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
631 || (packhak (vec) == OK ? ttyN (cmdp)
632 : winN (cmdp, DISPLAY, 1)) != NOTOK)
637 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
642 if (!vmh || ttyN (cmdp) != NOTOK)
647 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
652 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
657 if (!vmh || winN (cmdp, DISPLAY, 1) != NOTOK)
662 padios (NULL, "no dispatch for %s", cmd_name);
684 fsetup (char *folder)
690 maildir = m_maildir (folder);
691 if (chdir (maildir) == NOTOK)
692 padios (maildir, "unable to change directory to");
694 /* read folder and create message structure */
695 if (!(mp = folder_read (folder)))
696 padios (NULL, "unable to read folder %s", folder);
698 /* check for empty folder */
700 padios (NULL, "no messages in %s", folder);
703 mtime = stat (mp->foldpath, &st) != NOTOK ? st.st_mtime : 0;
705 m_gMsgs (mp->hghmsg);
707 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++) {
708 Msgs[msgnum].m_bboard_id = 0;
709 Msgs[msgnum].m_top = NOTOK;
710 Msgs[msgnum].m_start = Msgs[msgnum].m_stop = 0L;
711 Msgs[msgnum].m_scanl = NULL;
716 fmsh = getcpy (folder);
718 maxfds = OPEN_MAX / 2;
720 if ((maxfds -= 2) < 1)
736 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
737 if ((fp = fopen (tmpfil, "w+")) == NULL)
738 padios (tmpfil, "unable to create");
743 if ((fp = fopen (file, "r")) == NULL)
744 padios (file, "unable to read");
746 ioctl (fileno (fp), FIOCLEX, NULL);
748 if (fstat (fileno (fp), &st) != NOTOK) {
749 mode = (int) (st.st_mode & 0777), mtime = st.st_mtime;
750 msgp = read_map (file, (long) st.st_size);
753 mode = m_gmprot (), mtime = 0;
757 if ((msgp = read_file (msgp ? Msgs[msgp].m_stop : 0L, msgp + 1)) < 1)
758 padios (NULL, "no messages in %s", myname ? myname : file);
760 if (!(mp = (struct msgs *) calloc ((size_t) 1, sizeof(*mp))))
761 padios (NULL, "unable to allocate folder storage");
763 if (!(mp->msgstats = calloc ((size_t) msgp + 3, sizeof(*(mp->msgstats)))))
764 padios (NULL, "unable to allocate message status storage");
770 mp->foldpath = getcpy (myname ? myname : file);
771 clear_folder_flags (mp);
779 if (st.st_uid != getuid () || access (file, W_OK) == NOTOK)
786 mp->hghoff = mp->hghmsg + 1;
791 for (i = mp->lowmsg; i <= mp->hghmsg; i++) {
793 clear_msg_flags (mp, i);
798 for (i = mp->lowmsg; i <= mp->hghmsg; i++) {
799 if (Msgs[i].m_top) /* set in read_pop() */
800 clear_msg_flags (mp, i);
808 for (i = mp->lowmsg; i <= mp->hghmsg; i++) {
809 clear_msg_flags (mp, i);
814 mp->msgattrs[0] = getcpy ("unseen");
815 mp->msgattrs[1] = NULL;
817 m_unknown (fp); /* the MAGIC invocation */
826 read_map (char *file, long size)
828 register int i, msgp;
829 register struct drop *dp, *mp;
837 if ((i = map_read (file, size, &rp, 1)) == 0)
843 for (dp = rp + 1; i-- > 0; msgp++, dp++) {
844 mp = &Msgs[msgp].m_drop;
846 mp->d_size = dp->d_size;
847 mp->d_start = dp->d_start;
848 mp->d_stop = dp->d_stop;
849 Msgs[msgp].m_scanl = NULL;
858 read_file (long pos, int msgp)
861 register struct drop *dp, *mp;
869 if ((i = mbx_read (fp, pos, &rp, 1)) <= 0)
872 m_gMsgs ((msgp - 1) + i);
874 for (dp = rp; i-- > 0; msgp++, dp++) {
875 mp = &Msgs[msgp].m_drop;
877 mp->d_size = dp->d_size;
878 mp->d_start = dp->d_start;
879 mp->d_stop = dp->d_stop;
880 Msgs[msgp].m_scanl = NULL;
890 static int pop_base = 0;
893 pop_statmsg (char *s)
897 n = (i = atoi (s)) - pop_base; /* s="nnn header-line..." */
898 Msgs[n].m_top = Msgs[n].m_bboard_id = i;
908 if (pop_stat (&nmsgs, &nbytes) == NOTOK)
909 padios (NULL, "%s", response);
913 #ifdef NNTP /* this makes read_pop() do some real work... */
914 pop_base = nbytes - 1; /* nmsgs=last-first+1, nbytes=first */
915 pop_exists (pop_statmsg);
924 fprintf (yp, "%s\n", s);
935 nMsgs = n + MAXFOLDER / 2;
936 Msgs = (struct Msg *) calloc ((size_t) (nMsgs + 2), sizeof *Msgs);
938 padios (NULL, "unable to allocate Msgs structure");
945 nmsgs = nMsgs + n + MAXFOLDER / 2;
946 Msgs = (struct Msg *) mh_xrealloc ((char *) Msgs, (size_t) (nmsgs + 2) * sizeof *Msgs);
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 (!mh_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)
1548 unsigned char c, *cp;
1552 pp = cmdp->args[argp++] = cmdp->line;
1553 cmdp->redirect = NULL;
1554 cmdp->direction = STDIO;
1555 cmdp->stream = NULL;
1557 for (cp = buffer; (c = *cp); cp++) {
1563 fmt2peer (RC_EOF, "null command");
1567 while ((c = *cp++)) {
1574 cmdp->args[argp++] = pp;
1581 switch (c = *cp++) {
1583 padvise (NULL, "unmatched \"");
1588 if ((c = *cp++) == 0)
1599 if ((c = *cp++) == 0) {
1601 padvise (NULL, "the newline character can not be quoted");
1611 if (pp == cmdp->line) {
1612 padvise (NULL, "invalid null command");
1615 if (*cmdp->args[argp - 1] == 0)
1617 cmdp->direction = c == '>' ? CRTIO : PIPIO;
1618 if (cmdp->direction == CRTIO && (c = *cp) == '>') {
1619 cmdp->direction = APPIO;
1622 cmdp->redirect = pp + 1;/* sigh */
1623 for (; (c = *cp); cp++)
1627 padvise (NULL, cmdp->direction != PIPIO
1628 ? "missing name for redirect"
1629 : "invalid null command");
1632 strcpy (cmdp->redirect, cp);
1633 if (cmdp->direction != PIPIO) {
1635 if (isspace (*cp)) {
1636 padvise (NULL, "bad name for redirect");
1639 if (expand (cmdp->redirect) == NOTOK)
1648 cmdp->args[argp] = NULL;
1655 expand (char *redirect)
1661 if (*redirect != '~')
1664 if ((cp = strchr(pp = redirect + 1, '/')))
1669 if ((pw = getpwnam (pp)))
1672 padvise (NULL, "unknown user: %s", pp);
1676 snprintf (path, sizeof(path), "%s/%s", pp, cp ? cp : "");
1677 strcpy (redirect, path);
1683 init_io (struct Cmd *cmdp, int vio)
1690 result = initaux_io (cmdp);
1698 initaux_io (struct Cmd *cmdp)
1702 switch (cmdp->direction) {
1708 mode = cmdp->direction == CRTIO ? "write" : "append";
1709 if ((cmdp->stream = fopen (cmdp->redirect, mode)) == NULL) {
1710 padvise (cmdp->redirect, "unable to %s ", mode);
1711 cmdp->direction = STDIO;
1717 if ((cmdp->stream = popen (cmdp->redirect, "w")) == NULL) {
1718 padvise (cmdp->redirect, "unable to pipe");
1719 cmdp->direction = STDIO;
1722 SIGNAL (SIGPIPE, pipeser);
1727 padios (NULL, "unknown redirection for command");
1731 if (dup2 (fileno (cmdp->stream), fileno (stdout)) == NOTOK)
1732 padios ("standard output", "unable to dup2");
1740 fin_io (struct Cmd *cmdp, int vio)
1752 finaux_io (struct Cmd *cmdp)
1754 switch (cmdp->direction) {
1761 close (fileno (stdout));
1762 if (ferror (stdout))
1763 padvise (NULL, "problems writing %s", cmdp->redirect);
1764 fclose (cmdp->stream);
1769 close (fileno (stdout));
1770 pclose (cmdp->stream);
1771 SIGNAL (SIGPIPE, SIG_DFL);
1775 padios (NULL, "unknown redirection for command");
1778 if (dup2 (fileno (sp), fileno (stdout)) == NOTOK)
1779 padios ("standard output", "unable to dup2");
1782 cmdp->direction = STDIO;
1791 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
1792 unset_selected (mp, msgnum);
1793 mp->lowsel = mp->hghsel = mp->numsel = 0;
1801 folder_free (mp); /* free folder/message structure */
1813 seq_setcur (struct msgs *mp, int msgnum)
1815 if (mp->curmsg == msgnum)
1818 if (mp->curmsg && Msgs[mp->curmsg].m_scanl) {
1819 free (Msgs[mp->curmsg].m_scanl);
1820 Msgs[mp->curmsg].m_scanl = NULL;
1822 if (Msgs[msgnum].m_scanl) {
1823 free (Msgs[msgnum].m_scanl);
1824 Msgs[msgnum].m_scanl = NULL;
1827 mp->curmsg = msgnum;
1835 #ifndef RELIABLE_SIGNALS
1836 SIGNAL (SIGINT, intrser);
1844 longjmp (sigenv, NOTOK);
1852 #ifndef RELIABLE_SIGNALS
1853 SIGNAL (SIGPIPE, pipeser);
1856 if (broken_pipe++ == 0)
1857 fprintf (stderr, "broken pipe\n");
1863 longjmp (sigenv, NOTOK);
1871 #ifndef RELIABLE_SIGNALS
1872 SIGNAL (SIGQUIT, quitser);
1880 longjmp (sigenv, NOTOK);
1888 longjmp (peerenv, DONE);
1897 struct record rcs, *rc;
1902 switch (peer2rc (rc)) {
1905 while (isspace (*bp))
1907 if (sscanf (bp, "%d", &vrsn) != 1) {
1909 fmt2peer (RC_ERR, "bad init \"%s\"", rc->rc_data);
1912 if (vrsn != RC_VRSN) {
1913 fmt2peer (RC_ERR, "version %d unsupported", vrsn);
1917 while (*bp && !isspace (*bp))
1919 while (isspace (*bp))
1921 if (sscanf (bp, "%d", &numwins) != 1 || numwins <= 0)
1926 for (i = 1; i <= numwins; i++) {
1927 while (*bp && !isspace (*bp))
1929 while (isspace (*bp))
1931 if (sscanf (bp, "%d", &windows[i]) != 1 || windows[i] <= 0)
1934 rc2peer (RC_ACK, 0, NULL);
1938 padios (NULL, "%s", rc->rc_data);
1941 fmt2peer (RC_ERR, "pINI protocol screw-up");
1942 done (1); /* NOTREACHED */
1945 return 1; /* dead code to satisfy the compiler */
1950 pQRY (char *str, int scansw)
1952 if (pQRY1 (scansw) == NOTOK || pQRY2 () == NOTOK)
1955 rc2peer (RC_EOF, 0, NULL);
1964 static int lastlow = 0,
1969 oldhgh = mp->hghmsg;
1970 if (check_folder (scansw) && oldhgh < mp->hghmsg) {
1971 switch (winX (STATUS)) {
1976 printf ("new messages have arrived!");
1979 _exit (0); /* NOTREACHED */
1982 lastlow = lastcur = lasthgh = lastnum = 0;
1986 switch (winX (DISPLAY)) {
1991 scanrange (oldhgh + 1, mp->hghmsg);
1994 _exit (0); /* NOTREACHED */
2003 switch (winX (STATUS)) {
2008 printf ("%s: gap in ID:s, last seen %d, lowest present %d\n",
2009 myname ? myname : fmsh ? fmsh : mp->foldpath, gap - 1,
2010 readid (mp->lowmsg));
2013 _exit (0); /* NOTREACHED */
2020 if (mp->lowmsg != lastlow
2021 || mp->curmsg != lastcur
2022 || mp->hghmsg != lasthgh
2023 || mp->nummsg != lastnum)
2024 switch (winX (STATUS)) {
2032 _exit (0); /* NOTREACHED */
2035 lastlow = mp->lowmsg;
2036 lastcur = mp->curmsg;
2037 lasthgh = mp->hghmsg;
2038 lastnum = mp->nummsg;
2049 int i, j, k, msgnum, n;
2055 if (mp->nummsg == 0 && mp->nummsg != num)
2056 switch (winX (SCAN)) {
2064 _exit (0); /* NOTREACHED */
2073 j = (k = windows[SCAN]) / 2;
2074 for (msgnum = mp->curmsg; msgnum <= mp->hghmsg; msgnum++)
2075 if (does_exist (mp, msgnum))
2079 k = i >= k ? 1 : k - i;
2086 for (msgnum = mp->curmsg; msgnum >= mp->lowmsg; msgnum--)
2087 if (does_exist (mp, msgnum)) {
2094 for (msgnum = mp->curmsg + 1; msgnum <= mp->hghmsg; msgnum++)
2095 if (does_exist (mp, msgnum)) {
2099 if (n++ >= windows[SCAN])
2105 && does_exist (mp, lo)
2106 && does_exist (mp, hi)
2108 || (lo == mp->curmsg && lo == mp->lowmsg))
2110 || (hi == mp->curmsg && hi == mp->hghmsg))
2111 && hi - lo == j - i)
2114 if (mp->curmsg != cur || modified)
2115 switch (winN (NULLCMD, SCAN, 0)) {
2123 scanrange (lo = i, hi = j);
2134 pCMD (char *str, struct swit *sw, struct Cmd *cmdp)
2139 switch (winX (DISPLAY)) {
2144 printf ("commands:\n");
2145 print_sw (ALL, sw, "");
2146 printf ("type ``quit'' to leave %s\n", invo_name);
2149 _exit (0); /* NOTREACHED */
2152 rc2peer (RC_EOF, 0, NULL);
2156 if (parse (str, cmdp) == NOTOK)
2159 switch (i = smatch (cmdp->args[0], sw)) {
2161 switch (winX (DISPLAY)) {
2166 ambigsw (cmdp->args[0], sw);
2169 _exit (0); /* NOTREACHED */
2172 rc2peer (RC_EOF, 0, NULL);
2178 "say what: ``%s'' -- type ? (or help) for help",
2193 switch (setjmp (peerenv)) {
2195 SIGNAL (SIGALRM, alrmser);
2198 status = peerwait ();
2212 struct record rcs, *rc;
2217 switch (peer2rc (rc)) {
2220 rc2peer (RC_FIN, 0, NULL);
2224 advise (NULL, "%s", rc->rc_data);
2228 fmt2peer (RC_FIN, "pLOOP protocol screw-up");
2235 ttyNaux (struct Cmd *cmdp, char *s)
2237 struct record rcs, *rc;
2242 if (cmdp && init_io (cmdp, vmh) == NOTOK)
2245 /* XXX: fseek() too tricky for our own good */
2247 fseek (fp, 0L, SEEK_SET);
2250 switch (rc2rc (RC_TTY, s ? strlen (s) : 0, s, rc)) {
2252 vmhtty = OK; /* fall */
2257 padios (NULL, "%s", rc->rc_data);/* NOTREACHED */
2260 fmt2peer (RC_ERR, "pTTY protocol screw-up");
2261 done (1); /* NOTREACHED */
2265 SIGNAL (SIGTSTP, tstat);
2272 ttyR (struct Cmd *cmdp)
2274 struct record rcs, *rc;
2279 SIGNAL (SIGTSTP, SIG_IGN);
2291 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2293 rc2peer (RC_EOF, 0, NULL);
2297 padios (NULL, "%s", rc->rc_data);/* NOTREACHED */
2300 fmt2peer (RC_ERR, "pTTY protocol screw-up");
2301 done (1); /* NOTREACHED */
2304 return 1; /* dead code to satisfy compiler */
2309 winN (struct Cmd *cmdp, int n, int eof)
2312 char buffer[BUFSIZ];
2313 struct record rcs, *rc;
2316 if (vmhpid == NOTOK)
2321 /* XXX: fseek() too tricky for our own good */
2323 fseek (fp, 0L, SEEK_SET);
2327 snprintf (buffer, sizeof(buffer), "%d", n);
2328 switch (str2rc (RC_WIN, buffer, rc)) {
2336 padios (NULL, "%s", rc->rc_data);
2339 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2343 if (pipe (pd) == NOTOK) {
2344 err2peer (RC_ERR, "pipe", "unable to");
2348 switch (vmhpid = fork()) {
2350 err2peer (RC_ERR, "fork", "unable to");
2357 SIGNAL (SIGPIPE, SIG_IGN);
2358 while ((i = read (pd[0], buffer, sizeof buffer)) > 0)
2359 switch (rc2rc (RC_DATA, i, buffer, rc)) {
2367 advise (NULL, "%s", rc->rc_data);
2371 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2375 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2378 rc2peer (RC_EOF, 0, NULL);
2383 advise (NULL, "%s", rc->rc_data);
2388 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2393 err2peer (RC_ERR, "pipe", "error reading from");
2395 _exit (i != NOTOK ? i : 1);
2398 if ((vmhfd0 = dup (fileno (stdin))) == NOTOK)
2399 padios ("standard input", "unable to dup");
2400 if ((vmhfd1 = dup (fileno (stdout))) == NOTOK)
2401 padios ("standard output", "unable to dup");
2402 if ((vmhfd2 = dup (fileno (stderr))) == NOTOK)
2403 padios ("diagnostic output", "unable to dup");
2406 if ((i = open ("/dev/null", O_RDONLY)) != NOTOK && i != fileno (stdin)) {
2407 dup2 (i, fileno (stdin));
2412 if (dup2 (pd[1], fileno (stdout)) == NOTOK)
2413 padios ("standard output", "unable to dup2");
2417 if (dup2 (pd[1], fileno (stderr)) == NOTOK)
2418 padios ("diagnostic output", "unable to dup2");
2421 if (cmdp && init_io (cmdp, 0) == NOTOK)
2423 pstat = SIGNAL (SIGPIPE, pipeser);
2435 winR (struct Cmd *cmdp)
2445 if (dup2 (vmhfd0, fileno (stdin)) == NOTOK)
2446 padios ("standard input", "unable to dup2");
2451 if (dup2 (vmhfd1, fileno (stdout)) == NOTOK)
2452 padios ("standard output", "unable to dup2");
2457 if (dup2 (vmhfd2, fileno (stderr)) == NOTOK)
2458 padios ("diagnostic output", "unable to dup2");
2462 SIGNAL (SIGPIPE, pstat);
2464 if ((status = pidwait (vmhpid, OK)) == 2)
2468 return (status == 0 ? OK : NOTOK);
2476 char buffer[BUFSIZ];
2477 struct record rcs, *rc;
2482 /* XXX: fseek() too tricky for our own good */
2484 fseek (fp, 0L, SEEK_SET);
2486 snprintf (buffer, sizeof(buffer), "%d", n);
2487 switch (str2rc (RC_WIN, buffer, rc)) {
2495 padios (NULL, "%s", rc->rc_data);
2498 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2502 if (pipe (pd) == NOTOK) {
2503 err2peer (RC_ERR, "pipe", "unable to");
2507 switch (pid = fork ()) {
2509 err2peer (RC_ERR, "fork", "unable to");
2515 close (fileno (stdin));
2516 if ((i = open ("/dev/null", O_RDONLY)) != NOTOK && i != fileno (stdin)) {
2517 dup2 (i, fileno (stdin));
2520 dup2 (pd[1], fileno (stdout));
2521 dup2 (pd[1], fileno (stderr));
2529 while ((i = read (pd[0], buffer, sizeof buffer)) > 0)
2530 switch (rc2rc (RC_DATA, i, buffer, rc)) {
2540 padios (NULL, "%s", rc->rc_data);
2543 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2547 switch (rc2rc (RC_EOF, 0, NULL, rc)) {
2552 padios (NULL, "%s", rc->rc_data);
2555 fmt2peer (RC_ERR, "pWIN protocol screw-up");
2559 err2peer (RC_ERR, "pipe", "error reading from");
2563 return (i != NOTOK ? pid : NOTOK);
2569 padios (char *what, char *fmt, ...)
2575 verr2peer (RC_FIN, what, fmt, ap);
2578 advertise (what, NULL, fmt, ap);
2587 padvise (char *what, char *fmt, ...)
2593 verr2peer (RC_ERR, what, fmt, ap);
2595 advertise (what, NULL, fmt, ap);