]>
diplodocus.org Git - nmh/blob - uip/msh.c
3 * msh.c -- The nmh shell
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 * Keep more status information in maildrop map
17 #include <h/signals.h>
18 #include <h/dropsbr.h>
19 #include <h/fmt_scan.h>
20 #include <h/scansbr.h>
33 #define QUOTE '\\' /* sigh */
35 #define MSH_SWITCHES \
36 X("idstart number", -7, IDSW) /* interface from bbc */ \
37 X("idstop number", -6, FDSW) /* .. */ \
38 X("idquit number", -6, QDSW) /* .. */ \
39 X("idname BBoard", -6, NMSW) /* .. */ \
40 X("prompt string", 0, PRMPTSW) \
41 X("scan", 0, SCANSW) \
42 X("noscan", 0, NSCANSW) \
43 X("vmhread fd", -7, READSW) \
44 X("vmhwrite fd", -8, WRITESW) \
45 X("popread fd", -7, PREADSW) \
46 X("popwrite fd", -8, PWRITSW) \
47 X("topcur", 0, TCURSW) \
48 X("notopcur", 0, NTCURSW) \
49 X("version", 0, VERSIONSW) \
50 X("help", 0, HELPSW) \
52 #define X(sw, minchars, id) id,
53 DEFINE_SWITCH_ENUM(MSH
);
56 #define X(sw, minchars, id) { sw, minchars, id },
57 DEFINE_SWITCH_ARRAY(MSH
, switches
);
60 static int mbx_style
= MMDF_FORMAT
;
65 char*fmsh
= NULL
; /* folder instead of file */
66 int modified
; /* command modified folder */
67 struct msgs
*mp
; /* used a lot */
69 struct Msg
*Msgs
= NULL
; /* Msgs[0] not used */
70 static FILE *fp
; /* input file */
71 static FILE *yp
= NULL
; /* temporary file */
72 static int mode
; /* mode of file */
73 static int numfds
= 0; /* number of files cached */
74 static int maxfds
= 0; /* number of files cached to be cached */
75 static time_t mtime
= (time_t) 0; /* mtime of file */
80 #define ALARM ((unsigned int) 10)
81 #define ttyN(c) ttyNaux ((c), NULL)
85 static int vmhpid
= OK
;
90 static int vmhtty
= NOTOK
;
97 static int topcur
= 0;
99 static int numwins
= 0;
100 static int windows
[NWIN
+ 1];
102 static jmp_buf peerenv
;
107 static int pfd
= NOTOK
; /* fd parent is reading from */
108 static int ppid
= 0; /* pid of parent */
113 int interactive
; /* running from a /dev/tty */
114 int redirected
; /* re-directing output */
115 FILE *sp
= NULL
; /* original stdout */
117 char *cmd_name
; /* command being run */
118 char myfilter
[BUFSIZ
]; /* path to mhl.forward */
120 static char *myprompt
= "(%s) ";/* prompting string */
125 static int gap
; /* gap in BBoard-ID:s */
126 static char *myname
= NULL
; /* BBoard name */
127 char *BBoard_ID
= "BBoard-ID"; /* BBoard-ID constant */
132 SIGNAL_HANDLER istat
; /* original SIGINT */
133 static SIGNAL_HANDLER pstat
; /* current SIGPIPE */
134 SIGNAL_HANDLER qstat
; /* original SIGQUIT */
137 SIGNAL_HANDLER tstat
; /* original SIGTSTP */
140 int interrupted
; /* SIGINT detected */
141 int broken_pipe
; /* SIGPIPE detected */
142 int told_to_quit
; /* SIGQUIT detected */
147 void fsetup (char *);
151 void display_info (int);
154 void seq_setcur (struct msgs
*, int);
155 void padios (char *, char *, ...);
156 void padvise (char *, char *, ...);
158 extern m_getfld_state_t gstate
; /* use the gstate in scansbr.c */
164 static void msh (int);
165 static int read_map (char *, long);
166 static int read_file (long, int);
168 static void m_gMsgs (int);
169 static int check_folder (int);
170 static void scanrange (int, int);
171 static void scanstring (char *);
172 static void write_ids (void);
173 static void quit (void);
174 static int getargs (char *, struct swit
*, struct Cmd
*);
175 static int getcmds (struct swit
*, struct Cmd
*, int);
176 static int parse (char *, struct Cmd
*);
177 static int init_io (struct Cmd
*, int);
178 static int initaux_io (struct Cmd
*);
179 static void fin_io (struct Cmd
*, int);
180 static void finaux_io (struct Cmd
*);
181 static void m_init (void);
182 static void intrser (int);
183 static void pipeser (int);
184 static void quitser (int);
185 static void alrmser (int);
186 static int pINI (void);
187 static int pQRY (char *, int);
188 static int pQRY1 (int);
189 static int pQRY2 (void);
190 static int pCMD (char *, struct swit
*, struct Cmd
*);
191 static int pFIN (void);
192 static int peerwait (void);
193 static int ttyNaux (struct Cmd
*, char *);
194 static int ttyR (struct Cmd
*);
195 static int winN (struct Cmd
*, int, int);
196 static int winR (struct Cmd
*);
197 static int winX (int);
201 main (int argc
, char **argv
)
203 int id
= 0, scansw
= 0, vmh1
= 0, vmh2
= 0;
204 char *cp
, *file
= NULL
, *folder
= NULL
;
205 char **argp
, **arguments
, buf
[BUFSIZ
];
208 setlocale(LC_ALL
, "");
210 invo_name
= r1bindex (argv
[0], '/');
212 /* read user profile/context */
215 mts_init (invo_name
);
216 arguments
= getarguments (invo_name
, argc
,argv
, 1);
219 while ((cp
= *argp
++)) {
221 switch (smatch (++cp
, switches
)) {
223 ambigsw (cp
, switches
);
226 adios (NULL
, "-%s unknown", cp
);
229 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
230 print_help (buf
, switches
, 1);
233 print_version(invo_name
);
237 if (!(cp
= *argp
++) || *cp
== '-')
238 adios (NULL
, "missing argument to %s", argp
[-2]);
239 if ((id
= atoi (cp
)) < 1)
240 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
243 if (!(cp
= *argp
++) || *cp
== '-')
244 adios (NULL
, "missing argument to %s", argp
[-2]);
245 if ((pfd
= atoi (cp
)) <= 1)
246 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
249 if (!(cp
= *argp
++) || *cp
== '-')
250 adios (NULL
, "missing argument to %s", argp
[-2]);
251 if ((ppid
= atoi (cp
)) <= 1)
252 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
255 if (!(myname
= *argp
++) || *myname
== '-')
256 adios (NULL
, "missing argument to %s", argp
[-2]);
267 if (!(myprompt
= *argp
++) || *myprompt
== '-')
268 adios (NULL
, "missing argument to %s", argp
[-2]);
272 if (!(cp
= *argp
++) || *cp
== '-')
273 adios (NULL
, "missing argument to %s", argp
[-2]);
274 if ((vmh1
= atoi (cp
)) < 1)
275 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
278 if (!(cp
= *argp
++) || *cp
== '-')
279 adios (NULL
, "missing argument to %s", argp
[-2]);
280 if ((vmh2
= atoi (cp
)) < 1)
281 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
285 if (!(cp
= *argp
++) || *cp
== '-')
286 adios (NULL
, "missing argument to %s", argp
[-2]);
289 if (!(cp
= *argp
++) || *cp
== '-')
290 adios (NULL
, "missing argument to %s", argp
[-2]);
300 if (*cp
== '+' || *cp
== '@') {
302 adios (NULL
, "only one folder at a time!");
304 folder
= pluspath (cp
);
308 adios (NULL
, "only one file at a time!");
313 if (!file
&& !folder
)
316 adios (NULL
, "use a file or a folder, not both");
317 strncpy (myfilter
, etcpath (mhlforward
), sizeof(myfilter
));
320 ioctl (pfd
, FIOCLEX
, NULL
);
323 istat
= SIGNAL2 (SIGINT
, intrser
);
324 qstat
= SIGNAL2 (SIGQUIT
, quitser
);
326 sc_width (); /* MAGIC... */
328 if ((vmh
= vmh1
&& vmh2
)) {
331 SIGNAL (SIGINT
, SIG_IGN
);
332 SIGNAL (SIGQUIT
, SIG_IGN
);
334 tstat
= SIGNAL (SIGTSTP
, SIG_IGN
);
343 display_info (id
> 0 ? scansw
: 0);
345 msh (id
> 0 ? scansw
: 0);
355 #define MSHCMDS_SWITCHES \
356 X("advance", -7, ADVCMD) \
357 X("ali", 0, ALICMD) \
358 X("burst", 0, EXPLCMD) \
359 X("comp", 0, COMPCMD) \
360 X("dist", 0, DISTCMD) \
361 X("exit", 0, EXITCMD) \
362 X("folder", 0, FOLDCMD) \
363 X("forw", 0, FORWCMD) \
364 X("help", 0, HELPCMD) \
366 X("mark", 0, MARKCMD) \
367 X("mhmail", 0, MAILCMD) \
368 X("mhn", 0, MHNCMD) \
369 X("msgchk", 0, MSGKCMD) \
370 X("next", 0, NEXTCMD) \
371 X("packf", 0, PACKCMD) \
372 X("pick", 0, PICKCMD) \
373 X("prev", 0, PREVCMD) \
374 X("quit", 0, QUITCMD) \
375 X("refile", 0, FILECMD) \
376 X("repl", 0, REPLCMD) \
377 X("rmm", 0, RMMCMD) \
378 X("scan", 0, SCANCMD) \
379 X("send", 0, SENDCMD) \
380 X("show", 0, SHOWCMD) \
381 X("sortm", 0, SORTCMD) \
382 X("whatnow", 0, WHATCMD) \
383 X("whom", 0, WHOMCMD) \
385 #define X(sw, minchars, id) id,
386 DEFINE_SWITCH_ENUM(MSHCMDS
);
389 #define X(sw, minchars, id) { sw, minchars, id },
390 DEFINE_SWITCH_ARRAY(MSHCMDS
, mshcmds
);
398 register char *cp
, **ap
;
399 char prompt
[BUFSIZ
], *vec
[MAXARGS
];
401 register struct Cmd
*cmdp
;
402 static int once_only
= ADVCMD
;
404 snprintf (prompt
, sizeof(prompt
), myprompt
, invo_name
);
413 if ((i
= getcmds (mshcmds
, cmdp
, scansw
)) == EOF
) {
418 check_folder (scansw
);
419 if ((i
= getargs (prompt
, mshcmds
, cmdp
)) == EOF
) {
424 cmd_name
= mshcmds
[i
].sw
;
432 if (once_only
== ADVCMD
)
433 once_only
= i
= SHOWCMD
;
435 i
= mp
->curmsg
!= mp
->hghmsg
? NEXTCMD
: EXITCMD
;
436 cmd_name
= mshcmds
[i
].sw
;
442 case FORWCMD
: /* sigh */
452 if ((cp
= context_find (cmd_name
))) {
454 ap
= brkstring (cp
, " ", "\n");
455 ap
= copyip (ap
, vec
, MAXARGS
);
466 copyip (cmdp
->args
+ 1, ap
, MAXARGS
);
470 if (!vmh
&& init_io (cmdp
, vmh
) == NOTOK
) {
476 redirected
= vmh
|| cmdp
->direction
!= STDIO
;
487 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
488 forkcmd (vec
, cmd_name
);
492 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
497 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
503 || (filehak (vec
) == OK
? ttyN (cmdp
)
504 : winN (cmdp
, DISPLAY
, 1)) != NOTOK
)
509 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
514 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
519 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
525 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
530 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
537 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
543 || (packhak (vec
) == OK
? ttyN (cmdp
)
544 : winN (cmdp
, DISPLAY
, 1)) != NOTOK
)
549 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
554 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
559 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
564 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
569 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
574 padios (NULL
, "no dispatch for %s", cmd_name
);
596 fsetup (char *folder
)
602 maildir
= m_maildir (folder
);
603 if (chdir (maildir
) == NOTOK
)
604 padios (maildir
, "unable to change directory to");
606 /* read folder and create message structure */
607 if (!(mp
= folder_read (folder
)))
608 padios (NULL
, "unable to read folder %s", folder
);
610 /* check for empty folder */
612 padios (NULL
, "no messages in %s", folder
);
615 mtime
= stat (mp
->foldpath
, &st
) != NOTOK
? st
.st_mtime
: 0;
617 m_gMsgs (mp
->hghmsg
);
619 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++) {
620 Msgs
[msgnum
].m_bboard_id
= 0;
621 Msgs
[msgnum
].m_top
= NOTOK
;
622 Msgs
[msgnum
].m_start
= Msgs
[msgnum
].m_stop
= 0L;
623 Msgs
[msgnum
].m_scanl
= NULL
;
628 fmsh
= getcpy (folder
);
630 maxfds
= OPEN_MAX
/ 2;
632 if ((maxfds
-= 2) < 1)
642 if ((fp
= fopen (file
, "r")) == NULL
)
643 padios (file
, "unable to read");
645 ioctl (fileno (fp
), FIOCLEX
, NULL
);
647 if (fstat (fileno (fp
), &st
) != NOTOK
) {
648 mode
= (int) (st
.st_mode
& 0777), mtime
= st
.st_mtime
;
649 msgp
= read_map (file
, (long) st
.st_size
);
652 mode
= m_gmprot (), mtime
= 0;
656 if ((msgp
= read_file (msgp
? Msgs
[msgp
].m_stop
: 0L, msgp
+ 1)) < 1)
657 padios (NULL
, "no messages in %s", myname
? myname
: file
);
659 if (!(mp
= (struct msgs
*) calloc ((size_t) 1, sizeof(*mp
))))
660 padios (NULL
, "unable to allocate folder storage");
662 if (!(mp
->msgstats
= calloc ((size_t) msgp
+ 3, sizeof(*(mp
->msgstats
)))))
663 padios (NULL
, "unable to allocate message status storage");
669 mp
->foldpath
= getcpy (myname
? myname
: file
);
670 clear_folder_flags (mp
);
673 if (st
.st_uid
!= getuid () || access (file
, W_OK
) == NOTOK
)
677 mp
->hghoff
= mp
->hghmsg
+ 1;
679 for (i
= mp
->lowmsg
; i
<= mp
->hghmsg
; i
++) {
680 clear_msg_flags (mp
, i
);
685 mp
->msgattrs
[0] = getcpy ("unseen");
686 mp
->msgattrs
[1] = NULL
;
688 scan_detect_mbox_style (fp
); /* the MAGIC invocation */
697 read_map (char *file
, long size
)
699 register int i
, msgp
;
700 register struct drop
*dp
, *mp
;
703 if ((i
= map_read (file
, size
, &rp
, 1)) == 0)
709 for (dp
= rp
+ 1; i
-- > 0; msgp
++, dp
++) {
710 mp
= &Msgs
[msgp
].m_drop
;
712 mp
->d_size
= dp
->d_size
;
713 mp
->d_start
= dp
->d_start
;
714 mp
->d_stop
= dp
->d_stop
;
715 Msgs
[msgp
].m_scanl
= NULL
;
724 read_file (long pos
, int msgp
)
727 register struct drop
*dp
, *mp
;
730 if ((i
= mbx_read (fp
, pos
, &rp
, 1)) <= 0)
733 m_gMsgs ((msgp
- 1) + i
);
735 for (dp
= rp
; i
-- > 0; msgp
++, dp
++) {
736 mp
= &Msgs
[msgp
].m_drop
;
738 mp
->d_size
= dp
->d_size
;
739 mp
->d_start
= dp
->d_start
;
740 mp
->d_stop
= dp
->d_stop
;
741 Msgs
[msgp
].m_scanl
= NULL
;
755 nMsgs
= n
+ MAXFOLDER
/ 2;
756 Msgs
= (struct Msg
*) calloc ((size_t) (nMsgs
+ 2), sizeof *Msgs
);
758 padios (NULL
, "unable to allocate Msgs structure");
765 nmsgs
= nMsgs
+ n
+ MAXFOLDER
/ 2;
766 Msgs
= (struct Msg
*) mh_xrealloc ((char *) Msgs
, (size_t) (nmsgs
+ 2) * sizeof *Msgs
);
767 memset((char *) (Msgs
+ nMsgs
+ 2), 0, (size_t) ((nmsgs
- nMsgs
) * sizeof *Msgs
));
774 msh_ready (int msgnum
, int full
)
787 if ((fd
= Msgs
[msgnum
].m_top
) == NOTOK
) {
788 if (numfds
>= maxfds
)
789 for (msgp
= mp
->lowmsg
; msgp
<= mp
->hghmsg
; msgp
++)
790 if (Msgs
[msgp
].m_top
!= NOTOK
) {
791 close (Msgs
[msgp
].m_top
);
792 Msgs
[msgp
].m_top
= NOTOK
;
797 if ((fd
= open (cp
= m_name (msgnum
), O_RDONLY
)) == NOTOK
)
798 padios (cp
, "unable to open message");
799 Msgs
[msgnum
].m_top
= fd
;
803 if ((fd
= dup (fd
)) == NOTOK
)
804 padios ("cached message", "unable to dup");
805 if ((yp
= fdopen (fd
, "r")) == NULL
)
806 padios (NULL
, "unable to fdopen cached message");
807 fseek (yp
, 0L, SEEK_SET
);
811 scan_reset_m_getfld_state ();
812 scan_eom_action ((int (*)()) 0); /* XXX */
813 fseek (fp
, Msgs
[msgnum
].m_start
, SEEK_SET
);
819 check_folder (int scansw
)
821 int seqnum
, i
, low
, hgh
, msgp
;
825 if (stat (mp
->foldpath
, &st
) == NOTOK
)
826 padios (mp
->foldpath
, "unable to stat");
827 if (mtime
== st
.st_mtime
)
831 low
= mp
->hghmsg
+ 1;
832 folder_free (mp
); /* free folder/message structure */
834 if (!(mp
= folder_read (fmsh
)))
835 padios (NULL
, "unable to re-read folder %s", fmsh
);
839 for (msgp
= mp
->lowmsg
; msgp
<= mp
->hghmsg
; msgp
++) {
840 if (Msgs
[msgp
].m_top
!= NOTOK
) {
841 close (Msgs
[msgp
].m_top
);
842 Msgs
[msgp
].m_top
= NOTOK
;
845 if (Msgs
[msgp
].m_scanl
) {
846 free (Msgs
[msgp
].m_scanl
);
847 Msgs
[msgp
].m_scanl
= NULL
;
853 if (modified
|| low
> hgh
)
857 if (fstat (fileno (fp
), &st
) == NOTOK
)
858 padios (mp
->foldpath
, "unable to fstat");
859 if (mtime
== st
.st_mtime
)
861 mode
= (int) (st
.st_mode
& 0777);
864 if ((msgp
= read_file (Msgs
[mp
->hghmsg
].m_stop
, mp
->hghmsg
+ 1)) < 1)
865 padios (NULL
, "no messages in %s", mp
->foldpath
); /* XXX */
866 if (msgp
>= MAXFOLDER
)
867 padios (NULL
, "more than %d messages in %s", MAXFOLDER
,
869 if (msgp
<= mp
->hghmsg
)
872 if (!(mp
= folder_realloc (mp
, mp
->lowoff
, msgp
)))
873 padios (NULL
, "unable to allocate folder storage");
875 low
= mp
->hghmsg
+ 1, hgh
= msgp
;
876 seqnum
= scansw
? seq_getnum (mp
, "unseen") : -1;
877 for (i
= mp
->hghmsg
+ 1; i
<= msgp
; i
++) {
880 add_sequence(mp
, seqnum
, i
);
890 advise (NULL
, "new messages have arrived!\007");
892 scanrange (low
, hgh
);
899 scanrange (int low
, int hgh
)
903 snprintf (buffer
, sizeof(buffer
), "%d-%d", low
, hgh
);
909 scanstring (char *arg
)
911 char *cp
, **ap
, *vec
[MAXARGS
];
914 * This should be replace with a call to getarguments()
916 if ((cp
= context_find (cmd_name
= "scan"))) {
918 ap
= brkstring (cp
, " ", "\n");
919 ap
= copyip (ap
, vec
, MAXARGS
);
935 register int cur
, seqnum
, i
=0, msgnum
;
938 seq_setcur (mp
, mp
->lowmsg
);
939 if (id
<= 0 || (seqnum
= seq_getnum (mp
, "unseen")) == -1)
942 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
943 add_sequence(mp
, seqnum
, msgnum
);
948 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
949 if (does_exist(mp
, msgnum
)) /* FIX */
950 if ((i
= readid (msgnum
)) > 0 && i
< id
) {
952 clear_sequence(mp
, seqnum
, msgnum
);
955 for (i
= mp
->lowmsg
; i
< msgnum
; i
++)
956 clear_sequence(mp
, seqnum
, i
);
958 if (cur
> mp
->hghmsg
)
961 seq_setcur (mp
, cur
);
964 if ((gap
= 1 < id
&& id
< (i
= readid (mp
->lowmsg
)) ? id
: 0) && !vmh
)
965 advise (NULL
, "gap in ID:s, last seen %d, lowest present %d\n",
974 char *bp
, buf
[BUFSIZ
], name
[NAMESZ
];
977 if (Msgs
[msgnum
].m_bboard_id
)
978 return Msgs
[msgnum
].m_bboard_id
;
980 zp
= msh_ready (msgnum
, 0);
982 int bufsz
= sizeof buf
;
983 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, zp
)) {
986 if (!mh_strcasecmp (name
, BBoard_ID
)) {
988 while (state
== FLDPLUS
) {
990 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, zp
);
996 return (Msgs
[msgnum
].m_bboard_id
= i
);
1000 while (state
== FLDPLUS
) {
1002 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, zp
);
1014 display_info (int scansw
)
1018 interactive
= isatty (fileno (stdout
));
1020 if ((sd
= dup (fileno (stdout
))) == NOTOK
)
1021 padios ("standard output", "unable to dup");
1023 ioctl (sd
, FIOCLEX
, NULL
);
1024 #endif /* FIOCLEX */
1025 if ((sp
= fdopen (sd
, "w")) == NULL
)
1026 padios ("standard output", "unable to fdopen");
1029 m_putenv ("mhfolder", mp
->foldpath
);
1034 printf ("Reading ");
1035 if (SOprintf ("%s", myname
))
1036 printf ("%s", myname
);
1037 printf (", currently at message %d of %d\n",
1038 mp
->curmsg
, mp
->hghmsg
);
1041 printf ("Reading ");
1043 printf ("+%s", fmsh
);
1045 printf ("%s", mp
->foldpath
);
1046 printf (", currently at message %d of %d\n",
1047 mp
->curmsg
, mp
->hghmsg
);
1050 if (((seqnum
= seq_getnum (mp
, "unseen")) != -1)
1052 && in_sequence(mp
, seqnum
, mp
->hghmsg
))
1053 scanstring ("unseen");
1060 int i
= 0, seqnum
, msgnum
;
1066 if ((seqnum
= seq_getnum (mp
, "unseen")) != -1)
1067 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
1068 if (!in_sequence(mp
, seqnum
, msgnum
)) {
1069 if (Msgs
[msgnum
].m_bboard_id
== 0)
1071 if ((i
= Msgs
[msgnum
].m_bboard_id
) > 0)
1075 snprintf (buffer
, sizeof(buffer
), "%d %d\n", i
, Msgs
[mp
->hghmsg
].m_bboard_id
);
1076 write (pfd
, buffer
, sizeof(buffer
));
1086 char *cp
, tmpfil
[BUFSIZ
];
1087 char map1
[BUFSIZ
], map2
[BUFSIZ
];
1091 if (!(mp
->msgflags
& MODIFIED
) || is_readonly(mp
) || fmsh
) {
1093 rc2peer (RC_FIN
, 0, NULL
);
1098 ttyNaux (NULLCMD
, "FAST");
1100 if ((dp
= lkfopen (mp
->foldpath
, "r")) == NULL
) {
1101 advise (mp
->foldpath
, "unable to lock");
1108 if (fstat (fileno (dp
), &st
) == NOTOK
) {
1109 advise (mp
->foldpath
, "unable to stat");
1112 if (mtime
!= st
.st_mtime
) {
1113 advise (NULL
, "new messages have arrived, no update");
1116 mode
= (int) (st
.st_mode
& 0777);
1118 if (mp
->nummsg
== 0) {
1119 cp
= concat ("Zero file \"", mp
->foldpath
, "\"? ", NULL
);
1120 if (getanswer (cp
)) {
1121 if ((i
= creat (mp
->foldpath
, mode
)) != NOTOK
)
1124 advise (mp
->foldpath
, "error zero'ing");
1125 unlink (map_name (mp
->foldpath
));/* XXX */
1130 cp
= concat ("Update file \"", mp
->foldpath
, "\"? ", NULL
);
1131 if (!getanswer (cp
))
1133 strncpy (tmpfil
, m_backup (mp
->foldpath
), sizeof(tmpfil
));
1134 if ((md
= mbx_open (tmpfil
, mbx_style
, st
.st_uid
, st
.st_gid
, mode
)) == NOTOK
) {
1135 advise (tmpfil
, "unable to open");
1139 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1140 if (does_exist(mp
, msgnum
) && pack (tmpfil
, md
, msgnum
) == NOTOK
) {
1141 mbx_close (tmpfil
, md
);
1143 unlink (map_name (tmpfil
));
1146 mbx_close (tmpfil
, md
);
1148 if (rename (tmpfil
, mp
->foldpath
) == NOTOK
)
1149 admonish (mp
->foldpath
, "unable to rename %s to", tmpfil
);
1151 strncpy (map1
, map_name (tmpfil
), sizeof(map1
));
1152 strncpy (map2
, map_name (mp
->foldpath
), sizeof(map2
));
1154 if (rename (map1
, map2
) == NOTOK
) {
1155 admonish (map2
, "unable to rename %s to", map1
);
1164 lkfclose (dp
, mp
->foldpath
);
1173 getargs (char *prompt
, struct swit
*sw
, struct Cmd
*cmdp
)
1177 static char buffer
[BUFSIZ
];
1183 printf ("%s", prompt
);
1186 for (cp
= buffer
; (i
= getchar ()) != '\n';) {
1187 if (interrupted
&& !told_to_quit
) {
1192 if (told_to_quit
|| i
== EOF
) {
1195 kill (ppid
, SIGEMT
);
1197 kill (ppid
, SIGTERM
);
1201 if (cp
< &buffer
[sizeof buffer
- 2])
1208 if (buffer
[0] == '?') {
1209 printf ("commands:\n");
1210 print_sw (ALL
, sw
, "", stdout
);
1211 printf ("type CTRL-D or use ``quit'' to leave %s\n",
1216 if (parse (buffer
, cmdp
) == NOTOK
)
1219 switch (i
= smatch (cmdp
->args
[0], sw
)) {
1221 ambigsw (cmdp
->args
[0], sw
);
1224 printf ("say what: ``%s'' -- type ? (or help) for help\n",
1235 getcmds (struct swit
*sw
, struct Cmd
*cmdp
, int scansw
)
1238 struct record rcs
, *rc
;
1244 switch (peer2rc (rc
)) {
1246 pQRY (rc
->rc_data
, scansw
);
1250 if ((i
= pCMD (rc
->rc_data
, sw
, cmdp
)) != NOTOK
)
1257 kill (ppid
, SIGEMT
);
1259 kill (ppid
, SIGTERM
);
1264 padios (NULL
, "%s", rc
->rc_data
);
1267 fmt2peer (RC_ERR
, "pLOOP protocol screw-up");
1274 parse (char *buffer
, struct Cmd
*cmdp
)
1280 pp
= cmdp
->args
[argp
++] = cmdp
->line
;
1281 cmdp
->redirect
= NULL
;
1282 cmdp
->direction
= STDIO
;
1283 cmdp
->stream
= NULL
;
1285 for (cp
= buffer
; (c
= *cp
); cp
++) {
1286 if (!isspace ((unsigned char) c
))
1291 fmt2peer (RC_EOF
, "null command");
1295 while ((c
= *cp
++)) {
1296 if (isspace ((unsigned char) c
)) {
1297 while (isspace ((unsigned char) c
))
1302 cmdp
->args
[argp
++] = pp
;
1309 switch (c
= *cp
++) {
1311 padvise (NULL
, "unmatched \"");
1316 if ((c
= *cp
++) == 0)
1327 if ((c
= *cp
++) == 0) {
1329 padvise (NULL
, "the newline character can not be quoted");
1339 if (pp
== cmdp
->line
) {
1340 padvise (NULL
, "invalid null command");
1343 if (*cmdp
->args
[argp
- 1] == 0)
1345 cmdp
->direction
= c
== '>' ? CRTIO
: PIPIO
;
1346 if (cmdp
->direction
== CRTIO
&& (c
= *cp
) == '>') {
1347 cmdp
->direction
= APPIO
;
1350 cmdp
->redirect
= pp
+ 1;/* sigh */
1351 for (; (c
= *cp
); cp
++)
1352 if (!isspace ((unsigned char) c
))
1355 padvise (NULL
, cmdp
->direction
!= PIPIO
1356 ? "missing name for redirect"
1357 : "invalid null command");
1360 strcpy (cmdp
->redirect
, cp
);
1361 if (cmdp
->direction
!= PIPIO
) {
1363 if (isspace ((unsigned char) *cp
)) {
1364 padvise (NULL
, "bad name for redirect");
1367 if (expand (cmdp
->redirect
) == NOTOK
)
1376 cmdp
->args
[argp
] = NULL
;
1383 expand (char *redirect
)
1389 if (*redirect
!= '~')
1392 if ((cp
= strchr(pp
= redirect
+ 1, '/')))
1397 if ((pw
= getpwnam (pp
)))
1400 padvise (NULL
, "unknown user: %s", pp
);
1404 snprintf (path
, sizeof(path
), "%s/%s", pp
, cp
? cp
: "");
1405 strcpy (redirect
, path
);
1411 init_io (struct Cmd
*cmdp
, int vio
)
1418 result
= initaux_io (cmdp
);
1426 initaux_io (struct Cmd
*cmdp
)
1430 switch (cmdp
->direction
) {
1436 mode
= cmdp
->direction
== CRTIO
? "write" : "append";
1437 if ((cmdp
->stream
= fopen (cmdp
->redirect
, mode
)) == NULL
) {
1438 padvise (cmdp
->redirect
, "unable to %s ", mode
);
1439 cmdp
->direction
= STDIO
;
1445 if ((cmdp
->stream
= popen (cmdp
->redirect
, "w")) == NULL
) {
1446 padvise (cmdp
->redirect
, "unable to pipe");
1447 cmdp
->direction
= STDIO
;
1450 SIGNAL (SIGPIPE
, pipeser
);
1455 padios (NULL
, "unknown redirection for command");
1459 if (dup2 (fileno (cmdp
->stream
), fileno (stdout
)) == NOTOK
)
1460 padios ("standard output", "unable to dup2");
1468 fin_io (struct Cmd
*cmdp
, int vio
)
1480 finaux_io (struct Cmd
*cmdp
)
1482 switch (cmdp
->direction
) {
1489 close (fileno (stdout
));
1490 if (ferror (stdout
))
1491 padvise (NULL
, "problems writing %s", cmdp
->redirect
);
1492 fclose (cmdp
->stream
);
1497 close (fileno (stdout
));
1498 pclose (cmdp
->stream
);
1499 SIGNAL (SIGPIPE
, SIG_DFL
);
1503 padios (NULL
, "unknown redirection for command");
1506 if (dup2 (fileno (sp
), fileno (stdout
)) == NOTOK
)
1507 padios ("standard output", "unable to dup2");
1510 cmdp
->direction
= STDIO
;
1519 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1520 unset_selected (mp
, msgnum
);
1521 mp
->lowsel
= mp
->hghsel
= mp
->numsel
= 0;
1529 folder_free (mp
); /* free folder/message structure */
1535 seq_setcur (struct msgs
*mp
, int msgnum
)
1537 if (mp
->curmsg
== msgnum
)
1540 if (mp
->curmsg
&& Msgs
[mp
->curmsg
].m_scanl
) {
1541 free (Msgs
[mp
->curmsg
].m_scanl
);
1542 Msgs
[mp
->curmsg
].m_scanl
= NULL
;
1544 if (Msgs
[msgnum
].m_scanl
) {
1545 free (Msgs
[msgnum
].m_scanl
);
1546 Msgs
[msgnum
].m_scanl
= NULL
;
1549 mp
->curmsg
= msgnum
;
1567 if (broken_pipe
++ == 0)
1568 fprintf (stderr
, "broken pipe\n");
1587 longjmp (peerenv
, DONE
);
1596 struct record rcs
, *rc
;
1601 switch (peer2rc (rc
)) {
1604 while (isspace ((unsigned char) *bp
))
1606 if (sscanf (bp
, "%d", &vrsn
) != 1) {
1608 fmt2peer (RC_ERR
, "bad init \"%s\"", rc
->rc_data
);
1611 if (vrsn
!= RC_VRSN
) {
1612 fmt2peer (RC_ERR
, "version %d unsupported", vrsn
);
1616 while (*bp
&& !isspace ((unsigned char) *bp
))
1618 while (isspace ((unsigned char) *bp
))
1620 if (sscanf (bp
, "%d", &numwins
) != 1 || numwins
<= 0)
1625 for (i
= 1; i
<= numwins
; i
++) {
1626 while (*bp
&& !isspace ((unsigned char) *bp
))
1628 while (isspace ((unsigned char) *bp
))
1630 if (sscanf (bp
, "%d", &windows
[i
]) != 1 || windows
[i
] <= 0)
1633 rc2peer (RC_ACK
, 0, NULL
);
1637 padios (NULL
, "%s", rc
->rc_data
);
1640 fmt2peer (RC_ERR
, "pINI protocol screw-up");
1641 done (1); /* NOTREACHED */
1644 return 1; /* dead code to satisfy the compiler */
1649 pQRY (char *str
, int scansw
)
1652 if (pQRY1 (scansw
) == NOTOK
|| pQRY2 () == NOTOK
)
1655 rc2peer (RC_EOF
, 0, NULL
);
1664 static int lastlow
= 0,
1669 oldhgh
= mp
->hghmsg
;
1670 if (check_folder (scansw
) && oldhgh
< mp
->hghmsg
) {
1671 switch (winX (STATUS
)) {
1676 printf ("new messages have arrived!");
1679 _exit (0); /* NOTREACHED */
1682 lastlow
= lastcur
= lasthgh
= lastnum
= 0;
1686 switch (winX (DISPLAY
)) {
1691 scanrange (oldhgh
+ 1, mp
->hghmsg
);
1694 _exit (0); /* NOTREACHED */
1703 switch (winX (STATUS
)) {
1708 printf ("%s: gap in ID:s, last seen %d, lowest present %d\n",
1709 myname
? myname
: fmsh
? fmsh
: mp
->foldpath
, gap
- 1,
1710 readid (mp
->lowmsg
));
1713 _exit (0); /* NOTREACHED */
1720 if (mp
->lowmsg
!= lastlow
1721 || mp
->curmsg
!= lastcur
1722 || mp
->hghmsg
!= lasthgh
1723 || mp
->nummsg
!= lastnum
)
1724 switch (winX (STATUS
)) {
1732 _exit (0); /* NOTREACHED */
1735 lastlow
= mp
->lowmsg
;
1736 lastcur
= mp
->curmsg
;
1737 lasthgh
= mp
->hghmsg
;
1738 lastnum
= mp
->nummsg
;
1749 int i
, j
, k
, msgnum
, n
;
1755 if (mp
->nummsg
== 0 && mp
->nummsg
!= num
)
1756 switch (winX (SCAN
)) {
1764 _exit (0); /* NOTREACHED */
1773 j
= (k
= windows
[SCAN
]) / 2;
1774 for (msgnum
= mp
->curmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1775 if (does_exist (mp
, msgnum
))
1779 k
= i
>= k
? 1 : k
- i
;
1786 for (msgnum
= mp
->curmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
1787 if (does_exist (mp
, msgnum
)) {
1794 for (msgnum
= mp
->curmsg
+ 1; msgnum
<= mp
->hghmsg
; msgnum
++)
1795 if (does_exist (mp
, msgnum
)) {
1799 if (n
++ >= windows
[SCAN
])
1805 && does_exist (mp
, lo
)
1806 && does_exist (mp
, hi
)
1808 || (lo
== mp
->curmsg
&& lo
== mp
->lowmsg
))
1810 || (hi
== mp
->curmsg
&& hi
== mp
->hghmsg
))
1811 && hi
- lo
== j
- i
)
1814 if (mp
->curmsg
!= cur
|| modified
)
1815 switch (winN (NULLCMD
, SCAN
, 0)) {
1823 scanrange (lo
= i
, hi
= j
);
1834 pCMD (char *str
, struct swit
*sw
, struct Cmd
*cmdp
)
1839 switch (winX (DISPLAY
)) {
1844 printf ("commands:\n");
1845 print_sw (ALL
, sw
, "", stdout
);
1846 printf ("type ``quit'' to leave %s\n", invo_name
);
1849 _exit (0); /* NOTREACHED */
1852 rc2peer (RC_EOF
, 0, NULL
);
1856 if (parse (str
, cmdp
) == NOTOK
)
1859 switch (i
= smatch (cmdp
->args
[0], sw
)) {
1861 switch (winX (DISPLAY
)) {
1866 ambigsw (cmdp
->args
[0], sw
);
1869 _exit (0); /* NOTREACHED */
1872 rc2peer (RC_EOF
, 0, NULL
);
1878 "say what: ``%s'' -- type ? (or help) for help",
1893 switch (setjmp (peerenv
)) {
1895 SIGNAL (SIGALRM
, alrmser
);
1898 status
= peerwait ();
1912 struct record rcs
, *rc
;
1917 switch (peer2rc (rc
)) {
1920 rc2peer (RC_FIN
, 0, NULL
);
1924 advise (NULL
, "%s", rc
->rc_data
);
1928 fmt2peer (RC_FIN
, "pLOOP protocol screw-up");
1935 ttyNaux (struct Cmd
*cmdp
, char *s
)
1937 struct record rcs
, *rc
;
1942 if (cmdp
&& init_io (cmdp
, vmh
) == NOTOK
)
1945 /* XXX: fseek() too tricky for our own good */
1947 fseek (fp
, 0L, SEEK_SET
);
1950 switch (rc2rc (RC_TTY
, s
? strlen (s
) : 0, s
, rc
)) {
1952 vmhtty
= OK
; /* fall */
1957 padios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
1960 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
1961 done (1); /* NOTREACHED */
1965 SIGNAL (SIGTSTP
, tstat
);
1972 ttyR (struct Cmd
*cmdp
)
1974 struct record rcs
, *rc
;
1979 SIGNAL (SIGTSTP
, SIG_IGN
);
1991 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
1993 rc2peer (RC_EOF
, 0, NULL
);
1997 padios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
2000 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
2001 done (1); /* NOTREACHED */
2004 return 1; /* dead code to satisfy compiler */
2009 winN (struct Cmd
*cmdp
, int n
, int eof
)
2012 char buffer
[BUFSIZ
];
2013 struct record rcs
, *rc
;
2016 if (vmhpid
== NOTOK
)
2021 /* XXX: fseek() too tricky for our own good */
2023 fseek (fp
, 0L, SEEK_SET
);
2027 snprintf (buffer
, sizeof(buffer
), "%d", n
);
2028 switch (str2rc (RC_WIN
, buffer
, rc
)) {
2036 padios (NULL
, "%s", rc
->rc_data
);
2039 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2043 if (pipe (pd
) == NOTOK
) {
2044 err2peer (RC_ERR
, "pipe", "unable to");
2048 switch (vmhpid
= fork()) {
2050 err2peer (RC_ERR
, "fork", "unable to");
2057 SIGNAL (SIGPIPE
, SIG_IGN
);
2058 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
2059 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
2067 advise (NULL
, "%s", rc
->rc_data
);
2071 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2075 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2078 rc2peer (RC_EOF
, 0, NULL
);
2083 advise (NULL
, "%s", rc
->rc_data
);
2088 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2093 err2peer (RC_ERR
, "pipe", "error reading from");
2095 _exit (i
!= NOTOK
? i
: 1);
2098 if ((vmhfd0
= dup (fileno (stdin
))) == NOTOK
)
2099 padios ("standard input", "unable to dup");
2100 if ((vmhfd1
= dup (fileno (stdout
))) == NOTOK
)
2101 padios ("standard output", "unable to dup");
2102 if ((vmhfd2
= dup (fileno (stderr
))) == NOTOK
)
2103 padios ("diagnostic output", "unable to dup");
2106 if ((i
= open ("/dev/null", O_RDONLY
)) != NOTOK
&& i
!= fileno (stdin
)) {
2107 dup2 (i
, fileno (stdin
));
2112 if (dup2 (pd
[1], fileno (stdout
)) == NOTOK
)
2113 padios ("standard output", "unable to dup2");
2117 if (dup2 (pd
[1], fileno (stderr
)) == NOTOK
)
2118 padios ("diagnostic output", "unable to dup2");
2121 if (cmdp
&& init_io (cmdp
, 0) == NOTOK
)
2123 pstat
= SIGNAL (SIGPIPE
, pipeser
);
2135 winR (struct Cmd
*cmdp
)
2145 if (dup2 (vmhfd0
, fileno (stdin
)) == NOTOK
)
2146 padios ("standard input", "unable to dup2");
2151 if (dup2 (vmhfd1
, fileno (stdout
)) == NOTOK
)
2152 padios ("standard output", "unable to dup2");
2157 if (dup2 (vmhfd2
, fileno (stderr
)) == NOTOK
)
2158 padios ("diagnostic output", "unable to dup2");
2162 SIGNAL (SIGPIPE
, pstat
);
2164 if ((status
= pidwait (vmhpid
, OK
)) == 2)
2168 return (status
== 0 ? OK
: NOTOK
);
2176 char buffer
[BUFSIZ
];
2177 struct record rcs
, *rc
;
2182 /* XXX: fseek() too tricky for our own good */
2184 fseek (fp
, 0L, SEEK_SET
);
2186 snprintf (buffer
, sizeof(buffer
), "%d", n
);
2187 switch (str2rc (RC_WIN
, buffer
, rc
)) {
2195 padios (NULL
, "%s", rc
->rc_data
);
2198 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2202 if (pipe (pd
) == NOTOK
) {
2203 err2peer (RC_ERR
, "pipe", "unable to");
2207 switch (pid
= fork ()) {
2209 err2peer (RC_ERR
, "fork", "unable to");
2215 close (fileno (stdin
));
2216 if ((i
= open ("/dev/null", O_RDONLY
)) != NOTOK
&& i
!= fileno (stdin
)) {
2217 dup2 (i
, fileno (stdin
));
2220 dup2 (pd
[1], fileno (stdout
));
2221 dup2 (pd
[1], fileno (stderr
));
2229 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
2230 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
2240 padios (NULL
, "%s", rc
->rc_data
);
2243 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2247 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2252 padios (NULL
, "%s", rc
->rc_data
);
2255 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2259 err2peer (RC_ERR
, "pipe", "error reading from");
2263 return (i
!= NOTOK
? pid
: NOTOK
);
2269 padios (char *what
, char *fmt
, ...)
2275 verr2peer (RC_FIN
, what
, fmt
, ap
);
2278 advertise (what
, NULL
, fmt
, ap
);
2287 padvise (char *what
, char *fmt
, ...)
2293 verr2peer (RC_ERR
, what
, fmt
, ap
);
2295 advertise (what
, NULL
, fmt
, ap
);