]>
diplodocus.org Git - nmh/blob - uip/msh.c
75371bad378e34917e4dace2a46b97942c7fc5e0
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>
32 #define QUOTE '\\' /* sigh */
34 #define MSH_SWITCHES \
35 X("idstart number", -7, IDSW) /* interface from bbc */ \
36 X("idstop number", -6, FDSW) /* .. */ \
37 X("idquit number", -6, QDSW) /* .. */ \
38 X("idname BBoard", -6, NMSW) /* .. */ \
39 X("prompt string", 0, PRMPTSW) \
40 X("scan", 0, SCANSW) \
41 X("noscan", 0, NSCANSW) \
42 X("vmhread fd", -7, READSW) \
43 X("vmhwrite fd", -8, WRITESW) \
44 X("popread fd", -7, PREADSW) \
45 X("popwrite fd", -8, PWRITSW) \
46 X("topcur", 0, TCURSW) \
47 X("notopcur", 0, NTCURSW) \
48 X("version", 0, VERSIONSW) \
49 X("help", 0, HELPSW) \
51 #define X(sw, minchars, id) id,
52 DEFINE_SWITCH_ENUM(MSH
);
55 #define X(sw, minchars, id) { sw, minchars, id },
56 DEFINE_SWITCH_ARRAY(MSH
, switches
);
59 static int mbx_style
= MMDF_FORMAT
;
64 char*fmsh
= NULL
; /* folder instead of file */
65 int modified
; /* command modified folder */
66 struct msgs
*mp
; /* used a lot */
68 struct Msg
*Msgs
= NULL
; /* Msgs[0] not used */
69 static FILE *fp
; /* input file */
70 static FILE *yp
= NULL
; /* temporary file */
71 static int mode
; /* mode of file */
72 static int numfds
= 0; /* number of files cached */
73 static int maxfds
= 0; /* number of files cached to be cached */
74 static time_t mtime
= (time_t) 0; /* mtime of file */
79 #define ALARM ((unsigned int) 10)
80 #define ttyN(c) ttyNaux ((c), NULL)
84 static int vmhpid
= OK
;
89 static int vmhtty
= NOTOK
;
96 static int topcur
= 0;
98 static int numwins
= 0;
99 static int windows
[NWIN
+ 1];
101 static jmp_buf peerenv
;
106 static int pfd
= NOTOK
; /* fd parent is reading from */
107 static int ppid
= 0; /* pid of parent */
112 int interactive
; /* running from a /dev/tty */
113 int redirected
; /* re-directing output */
114 FILE *sp
= NULL
; /* original stdout */
116 char *cmd_name
; /* command being run */
117 char myfilter
[BUFSIZ
]; /* path to mhl.forward */
119 static char *myprompt
= "(%s) ";/* prompting string */
124 static int gap
; /* gap in BBoard-ID:s */
125 static char *myname
= NULL
; /* BBoard name */
126 char *BBoard_ID
= "BBoard-ID"; /* BBoard-ID constant */
131 SIGNAL_HANDLER istat
; /* original SIGINT */
132 static SIGNAL_HANDLER pstat
; /* current SIGPIPE */
133 SIGNAL_HANDLER qstat
; /* original SIGQUIT */
136 SIGNAL_HANDLER tstat
; /* original SIGTSTP */
139 int interrupted
; /* SIGINT detected */
140 int broken_pipe
; /* SIGPIPE detected */
141 int told_to_quit
; /* SIGQUIT detected */
146 void fsetup (char *);
150 void display_info (int);
153 void seq_setcur (struct msgs
*, int);
154 void padios (char *, char *, ...);
155 void padvise (char *, char *, ...);
157 extern m_getfld_state_t gstate
; /* use the gstate in scansbr.c */
163 static void msh (int);
164 static int read_map (char *, long);
165 static int read_file (long, int);
167 static void m_gMsgs (int);
168 static int check_folder (int);
169 static void scanrange (int, int);
170 static void scanstring (char *);
171 static void write_ids (void);
172 static void quit (void);
173 static int getargs (char *, struct swit
*, struct Cmd
*);
174 static int getcmds (struct swit
*, struct Cmd
*, int);
175 static int parse (char *, struct Cmd
*);
176 static int init_io (struct Cmd
*, int);
177 static int initaux_io (struct Cmd
*);
178 static void fin_io (struct Cmd
*, int);
179 static void finaux_io (struct Cmd
*);
180 static void m_init (void);
181 static void intrser (int);
182 static void pipeser (int);
183 static void quitser (int);
184 static void alrmser (int);
185 static int pINI (void);
186 static int pQRY (char *, int);
187 static int pQRY1 (int);
188 static int pQRY2 (void);
189 static int pCMD (char *, struct swit
*, struct Cmd
*);
190 static int pFIN (void);
191 static int peerwait (void);
192 static int ttyNaux (struct Cmd
*, char *);
193 static int ttyR (struct Cmd
*);
194 static int winN (struct Cmd
*, int, int);
195 static int winR (struct Cmd
*);
196 static int winX (int);
200 main (int argc
, char **argv
)
202 int id
= 0, scansw
= 0, vmh1
= 0, vmh2
= 0;
203 char *cp
, *file
= NULL
, *folder
= NULL
;
204 char **argp
, **arguments
, buf
[BUFSIZ
];
206 setlocale(LC_ALL
, "");
207 invo_name
= r1bindex (argv
[0], '/');
209 /* read user profile/context */
212 mts_init (invo_name
);
213 arguments
= getarguments (invo_name
, argc
,argv
, 1);
216 while ((cp
= *argp
++)) {
218 switch (smatch (++cp
, switches
)) {
220 ambigsw (cp
, switches
);
223 adios (NULL
, "-%s unknown", cp
);
226 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
227 print_help (buf
, switches
, 1);
230 print_version(invo_name
);
234 if (!(cp
= *argp
++) || *cp
== '-')
235 adios (NULL
, "missing argument to %s", argp
[-2]);
236 if ((id
= atoi (cp
)) < 1)
237 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
240 if (!(cp
= *argp
++) || *cp
== '-')
241 adios (NULL
, "missing argument to %s", argp
[-2]);
242 if ((pfd
= atoi (cp
)) <= 1)
243 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
246 if (!(cp
= *argp
++) || *cp
== '-')
247 adios (NULL
, "missing argument to %s", argp
[-2]);
248 if ((ppid
= atoi (cp
)) <= 1)
249 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
252 if (!(myname
= *argp
++) || *myname
== '-')
253 adios (NULL
, "missing argument to %s", argp
[-2]);
264 if (!(myprompt
= *argp
++) || *myprompt
== '-')
265 adios (NULL
, "missing argument to %s", argp
[-2]);
269 if (!(cp
= *argp
++) || *cp
== '-')
270 adios (NULL
, "missing argument to %s", argp
[-2]);
271 if ((vmh1
= atoi (cp
)) < 1)
272 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
275 if (!(cp
= *argp
++) || *cp
== '-')
276 adios (NULL
, "missing argument to %s", argp
[-2]);
277 if ((vmh2
= atoi (cp
)) < 1)
278 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
282 if (!(cp
= *argp
++) || *cp
== '-')
283 adios (NULL
, "missing argument to %s", argp
[-2]);
286 if (!(cp
= *argp
++) || *cp
== '-')
287 adios (NULL
, "missing argument to %s", argp
[-2]);
297 if (*cp
== '+' || *cp
== '@') {
299 adios (NULL
, "only one folder at a time!");
301 folder
= pluspath (cp
);
305 adios (NULL
, "only one file at a time!");
310 if (!file
&& !folder
)
313 adios (NULL
, "use a file or a folder, not both");
314 strncpy (myfilter
, etcpath (mhlforward
), sizeof(myfilter
));
317 ioctl (pfd
, FIOCLEX
, NULL
);
320 istat
= SIGNAL2 (SIGINT
, intrser
);
321 qstat
= SIGNAL2 (SIGQUIT
, quitser
);
323 sc_width (); /* MAGIC... */
325 if ((vmh
= vmh1
&& vmh2
)) {
328 SIGNAL (SIGINT
, SIG_IGN
);
329 SIGNAL (SIGQUIT
, SIG_IGN
);
331 tstat
= SIGNAL (SIGTSTP
, SIG_IGN
);
340 display_info (id
> 0 ? scansw
: 0);
342 msh (id
> 0 ? scansw
: 0);
352 #define MSHCMDS_SWITCHES \
353 X("advance", -7, ADVCMD) \
354 X("ali", 0, ALICMD) \
355 X("burst", 0, EXPLCMD) \
356 X("comp", 0, COMPCMD) \
357 X("dist", 0, DISTCMD) \
358 X("exit", 0, EXITCMD) \
359 X("folder", 0, FOLDCMD) \
360 X("forw", 0, FORWCMD) \
361 X("help", 0, HELPCMD) \
363 X("mark", 0, MARKCMD) \
364 X("mhmail", 0, MAILCMD) \
365 X("mhn", 0, MHNCMD) \
366 X("msgchk", 0, MSGKCMD) \
367 X("next", 0, NEXTCMD) \
368 X("packf", 0, PACKCMD) \
369 X("pick", 0, PICKCMD) \
370 X("prev", 0, PREVCMD) \
371 X("quit", 0, QUITCMD) \
372 X("refile", 0, FILECMD) \
373 X("repl", 0, REPLCMD) \
374 X("rmm", 0, RMMCMD) \
375 X("scan", 0, SCANCMD) \
376 X("send", 0, SENDCMD) \
377 X("show", 0, SHOWCMD) \
378 X("sortm", 0, SORTCMD) \
379 X("whatnow", 0, WHATCMD) \
380 X("whom", 0, WHOMCMD) \
382 #define X(sw, minchars, id) id,
383 DEFINE_SWITCH_ENUM(MSHCMDS
);
386 #define X(sw, minchars, id) { sw, minchars, id },
387 DEFINE_SWITCH_ARRAY(MSHCMDS
, mshcmds
);
395 register char *cp
, **ap
;
396 char prompt
[BUFSIZ
], *vec
[MAXARGS
];
398 register struct Cmd
*cmdp
;
399 static int once_only
= ADVCMD
;
401 snprintf (prompt
, sizeof(prompt
), myprompt
, invo_name
);
410 if ((i
= getcmds (mshcmds
, cmdp
, scansw
)) == EOF
) {
415 check_folder (scansw
);
416 if ((i
= getargs (prompt
, mshcmds
, cmdp
)) == EOF
) {
421 cmd_name
= mshcmds
[i
].sw
;
429 if (once_only
== ADVCMD
)
430 once_only
= i
= SHOWCMD
;
432 i
= mp
->curmsg
!= mp
->hghmsg
? NEXTCMD
: EXITCMD
;
433 cmd_name
= mshcmds
[i
].sw
;
439 case FORWCMD
: /* sigh */
449 if ((cp
= context_find (cmd_name
))) {
451 ap
= brkstring (cp
, " ", "\n");
452 ap
= copyip (ap
, vec
, MAXARGS
);
463 copyip (cmdp
->args
+ 1, ap
, MAXARGS
);
467 if (!vmh
&& init_io (cmdp
, vmh
) == NOTOK
) {
473 redirected
= vmh
|| cmdp
->direction
!= STDIO
;
484 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
485 forkcmd (vec
, cmd_name
);
489 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
494 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
500 || (filehak (vec
) == OK
? ttyN (cmdp
)
501 : winN (cmdp
, DISPLAY
, 1)) != NOTOK
)
506 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
511 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
516 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
522 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
527 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
534 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
540 || (packhak (vec
) == OK
? ttyN (cmdp
)
541 : winN (cmdp
, DISPLAY
, 1)) != NOTOK
)
546 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
551 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
556 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
561 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
566 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
571 padios (NULL
, "no dispatch for %s", cmd_name
);
593 fsetup (char *folder
)
599 maildir
= m_maildir (folder
);
600 if (chdir (maildir
) == NOTOK
)
601 padios (maildir
, "unable to change directory to");
603 /* read folder and create message structure */
604 if (!(mp
= folder_read (folder
, 0)))
605 padios (NULL
, "unable to read folder %s", folder
);
607 /* check for empty folder */
609 padios (NULL
, "no messages in %s", folder
);
612 mtime
= stat (mp
->foldpath
, &st
) != NOTOK
? st
.st_mtime
: 0;
614 m_gMsgs (mp
->hghmsg
);
616 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++) {
617 Msgs
[msgnum
].m_bboard_id
= 0;
618 Msgs
[msgnum
].m_top
= NOTOK
;
619 Msgs
[msgnum
].m_start
= Msgs
[msgnum
].m_stop
= 0L;
620 Msgs
[msgnum
].m_scanl
= NULL
;
625 fmsh
= getcpy (folder
);
627 maxfds
= OPEN_MAX
/ 2;
629 if ((maxfds
-= 2) < 1)
639 if ((fp
= fopen (file
, "r")) == NULL
)
640 padios (file
, "unable to read");
642 ioctl (fileno (fp
), FIOCLEX
, NULL
);
644 if (fstat (fileno (fp
), &st
) != NOTOK
) {
645 mode
= (int) (st
.st_mode
& 0777), mtime
= st
.st_mtime
;
646 msgp
= read_map (file
, (long) st
.st_size
);
649 mode
= m_gmprot (), mtime
= 0;
653 if ((msgp
= read_file (msgp
? Msgs
[msgp
].m_stop
: 0L, msgp
+ 1)) < 1)
654 padios (NULL
, "no messages in %s", myname
? myname
: file
);
656 if (!(mp
= (struct msgs
*) calloc ((size_t) 1, sizeof(*mp
))))
657 padios (NULL
, "unable to allocate folder storage");
659 if (!(mp
->msgstats
= calloc ((size_t) msgp
+ 3, sizeof(*(mp
->msgstats
)))))
660 padios (NULL
, "unable to allocate message status storage");
666 mp
->foldpath
= getcpy (myname
? myname
: file
);
667 clear_folder_flags (mp
);
670 if (st
.st_uid
!= getuid () || access (file
, W_OK
) == NOTOK
)
674 mp
->hghoff
= mp
->hghmsg
+ 1;
676 for (i
= mp
->lowmsg
; i
<= mp
->hghmsg
; i
++) {
677 clear_msg_flags (mp
, i
);
682 svector_push_back (mp
->msgattrs
, getcpy ("unseen"));
684 scan_detect_mbox_style (fp
); /* the MAGIC invocation */
693 read_map (char *file
, long size
)
695 register int i
, msgp
;
696 register struct drop
*dp
, *mp
;
699 if ((i
= map_read (file
, size
, &rp
, 1)) == 0)
705 for (dp
= rp
+ 1; i
-- > 0; msgp
++, dp
++) {
706 mp
= &Msgs
[msgp
].m_drop
;
708 mp
->d_size
= dp
->d_size
;
709 mp
->d_start
= dp
->d_start
;
710 mp
->d_stop
= dp
->d_stop
;
711 Msgs
[msgp
].m_scanl
= NULL
;
720 read_file (long pos
, int msgp
)
723 register struct drop
*dp
, *mp
;
726 if ((i
= mbx_read (fp
, pos
, &rp
, 1)) <= 0)
729 m_gMsgs ((msgp
- 1) + i
);
731 for (dp
= rp
; i
-- > 0; msgp
++, dp
++) {
732 mp
= &Msgs
[msgp
].m_drop
;
734 mp
->d_size
= dp
->d_size
;
735 mp
->d_start
= dp
->d_start
;
736 mp
->d_stop
= dp
->d_stop
;
737 Msgs
[msgp
].m_scanl
= NULL
;
751 nMsgs
= n
+ MAXFOLDER
/ 2;
752 Msgs
= (struct Msg
*) calloc ((size_t) (nMsgs
+ 2), sizeof *Msgs
);
754 padios (NULL
, "unable to allocate Msgs structure");
761 nmsgs
= nMsgs
+ n
+ MAXFOLDER
/ 2;
762 Msgs
= (struct Msg
*) mh_xrealloc ((char *) Msgs
, (size_t) (nmsgs
+ 2) * sizeof *Msgs
);
763 memset((char *) (Msgs
+ nMsgs
+ 2), 0, (size_t) ((nmsgs
- nMsgs
) * sizeof *Msgs
));
770 msh_ready (int msgnum
, int full
)
783 if ((fd
= Msgs
[msgnum
].m_top
) == NOTOK
) {
784 if (numfds
>= maxfds
)
785 for (msgp
= mp
->lowmsg
; msgp
<= mp
->hghmsg
; msgp
++)
786 if (Msgs
[msgp
].m_top
!= NOTOK
) {
787 close (Msgs
[msgp
].m_top
);
788 Msgs
[msgp
].m_top
= NOTOK
;
793 if ((fd
= open (cp
= m_name (msgnum
), O_RDONLY
)) == NOTOK
)
794 padios (cp
, "unable to open message");
795 Msgs
[msgnum
].m_top
= fd
;
799 if ((fd
= dup (fd
)) == NOTOK
)
800 padios ("cached message", "unable to dup");
801 if ((yp
= fdopen (fd
, "r")) == NULL
)
802 padios (NULL
, "unable to fdopen cached message");
803 fseek (yp
, 0L, SEEK_SET
);
807 scan_reset_m_getfld_state ();
808 scan_eom_action ((int (*)()) 0); /* XXX */
809 fseek (fp
, Msgs
[msgnum
].m_start
, SEEK_SET
);
815 check_folder (int scansw
)
817 int seqnum
, i
, low
, hgh
, msgp
;
821 if (stat (mp
->foldpath
, &st
) == NOTOK
)
822 padios (mp
->foldpath
, "unable to stat");
823 if (mtime
== st
.st_mtime
)
827 low
= mp
->hghmsg
+ 1;
828 folder_free (mp
); /* free folder/message structure */
830 if (!(mp
= folder_read (fmsh
, 0)))
831 padios (NULL
, "unable to re-read folder %s", fmsh
);
835 for (msgp
= mp
->lowmsg
; msgp
<= mp
->hghmsg
; msgp
++) {
836 if (Msgs
[msgp
].m_top
!= NOTOK
) {
837 close (Msgs
[msgp
].m_top
);
838 Msgs
[msgp
].m_top
= NOTOK
;
841 if (Msgs
[msgp
].m_scanl
) {
842 free (Msgs
[msgp
].m_scanl
);
843 Msgs
[msgp
].m_scanl
= NULL
;
849 if (modified
|| low
> hgh
)
853 if (fstat (fileno (fp
), &st
) == NOTOK
)
854 padios (mp
->foldpath
, "unable to fstat");
855 if (mtime
== st
.st_mtime
)
857 mode
= (int) (st
.st_mode
& 0777);
860 if ((msgp
= read_file (Msgs
[mp
->hghmsg
].m_stop
, mp
->hghmsg
+ 1)) < 1)
861 padios (NULL
, "no messages in %s", mp
->foldpath
); /* XXX */
862 if (msgp
>= MAXFOLDER
)
863 padios (NULL
, "more than %d messages in %s", MAXFOLDER
,
865 if (msgp
<= mp
->hghmsg
)
868 if (!(mp
= folder_realloc (mp
, mp
->lowoff
, msgp
)))
869 padios (NULL
, "unable to allocate folder storage");
871 low
= mp
->hghmsg
+ 1, hgh
= msgp
;
872 seqnum
= scansw
? seq_getnum (mp
, "unseen") : -1;
873 for (i
= mp
->hghmsg
+ 1; i
<= msgp
; i
++) {
876 add_sequence(mp
, seqnum
, i
);
886 advise (NULL
, "new messages have arrived!\007");
888 scanrange (low
, hgh
);
895 scanrange (int low
, int hgh
)
899 snprintf (buffer
, sizeof(buffer
), "%d-%d", low
, hgh
);
905 scanstring (char *arg
)
907 char *cp
, **ap
, *vec
[MAXARGS
];
910 * This should be replace with a call to getarguments()
912 if ((cp
= context_find (cmd_name
= "scan"))) {
914 ap
= brkstring (cp
, " ", "\n");
915 ap
= copyip (ap
, vec
, MAXARGS
);
931 register int cur
, seqnum
, i
=0, msgnum
;
934 seq_setcur (mp
, mp
->lowmsg
);
935 if (id
<= 0 || (seqnum
= seq_getnum (mp
, "unseen")) == -1)
938 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
939 add_sequence(mp
, seqnum
, msgnum
);
944 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
945 if (does_exist(mp
, msgnum
)) /* FIX */
946 if ((i
= readid (msgnum
)) > 0 && i
< id
) {
948 clear_sequence(mp
, seqnum
, msgnum
);
951 for (i
= mp
->lowmsg
; i
< msgnum
; i
++)
952 clear_sequence(mp
, seqnum
, i
);
954 if (cur
> mp
->hghmsg
)
957 seq_setcur (mp
, cur
);
960 if ((gap
= 1 < id
&& id
< (i
= readid (mp
->lowmsg
)) ? id
: 0) && !vmh
)
961 advise (NULL
, "gap in ID:s, last seen %d, lowest present %d\n",
970 char *bp
, buf
[BUFSIZ
], name
[NAMESZ
];
973 if (Msgs
[msgnum
].m_bboard_id
)
974 return Msgs
[msgnum
].m_bboard_id
;
976 zp
= msh_ready (msgnum
, 0);
978 int bufsz
= sizeof buf
;
979 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, zp
)) {
982 if (!strcasecmp (name
, BBoard_ID
)) {
984 while (state
== FLDPLUS
) {
986 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, zp
);
992 return (Msgs
[msgnum
].m_bboard_id
= i
);
996 while (state
== FLDPLUS
) {
998 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, zp
);
1010 display_info (int scansw
)
1014 interactive
= isatty (fileno (stdout
));
1016 if ((sd
= dup (fileno (stdout
))) == NOTOK
)
1017 padios ("standard output", "unable to dup");
1019 ioctl (sd
, FIOCLEX
, NULL
);
1020 #endif /* FIOCLEX */
1021 if ((sp
= fdopen (sd
, "w")) == NULL
)
1022 padios ("standard output", "unable to fdopen");
1025 m_putenv ("mhfolder", mp
->foldpath
);
1030 printf ("Reading ");
1031 if (SOprintf ("%s", myname
))
1032 printf ("%s", myname
);
1033 printf (", currently at message %d of %d\n",
1034 mp
->curmsg
, mp
->hghmsg
);
1037 printf ("Reading ");
1039 printf ("+%s", fmsh
);
1041 printf ("%s", mp
->foldpath
);
1042 printf (", currently at message %d of %d\n",
1043 mp
->curmsg
, mp
->hghmsg
);
1046 if (((seqnum
= seq_getnum (mp
, "unseen")) != -1)
1048 && in_sequence(mp
, seqnum
, mp
->hghmsg
))
1049 scanstring ("unseen");
1056 int i
= 0, seqnum
, msgnum
;
1062 if ((seqnum
= seq_getnum (mp
, "unseen")) != -1)
1063 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
1064 if (!in_sequence(mp
, seqnum
, msgnum
)) {
1065 if (Msgs
[msgnum
].m_bboard_id
== 0)
1067 if ((i
= Msgs
[msgnum
].m_bboard_id
) > 0)
1071 snprintf (buffer
, sizeof(buffer
), "%d %d\n", i
, Msgs
[mp
->hghmsg
].m_bboard_id
);
1072 write (pfd
, buffer
, sizeof(buffer
));
1082 char *cp
, tmpfil
[BUFSIZ
];
1083 char map1
[BUFSIZ
], map2
[BUFSIZ
];
1087 if (!(mp
->msgflags
& MODIFIED
) || is_readonly(mp
) || fmsh
) {
1089 rc2peer (RC_FIN
, 0, NULL
);
1094 ttyNaux (NULLCMD
, "FAST");
1096 if ((dp
= lkfopendata (mp
->foldpath
, "r")) == NULL
) {
1097 advise (mp
->foldpath
, "unable to lock");
1104 if (fstat (fileno (dp
), &st
) == NOTOK
) {
1105 advise (mp
->foldpath
, "unable to stat");
1108 if (mtime
!= st
.st_mtime
) {
1109 advise (NULL
, "new messages have arrived, no update");
1112 mode
= (int) (st
.st_mode
& 0777);
1114 if (mp
->nummsg
== 0) {
1115 cp
= concat ("Zero file \"", mp
->foldpath
, "\"? ", NULL
);
1116 if (getanswer (cp
)) {
1117 if ((i
= creat (mp
->foldpath
, mode
)) != NOTOK
)
1120 advise (mp
->foldpath
, "error zero'ing");
1121 unlink (map_name (mp
->foldpath
));/* XXX */
1126 cp
= concat ("Update file \"", mp
->foldpath
, "\"? ", NULL
);
1127 if (!getanswer (cp
))
1129 strncpy (tmpfil
, m_backup (mp
->foldpath
), sizeof(tmpfil
));
1130 if ((md
= mbx_open (tmpfil
, mbx_style
, st
.st_uid
, st
.st_gid
, mode
)) == NOTOK
) {
1131 advise (tmpfil
, "unable to open");
1135 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1136 if (does_exist(mp
, msgnum
) && pack (tmpfil
, md
, msgnum
) == NOTOK
) {
1137 mbx_close (tmpfil
, md
);
1139 unlink (map_name (tmpfil
));
1142 mbx_close (tmpfil
, md
);
1144 if (rename (tmpfil
, mp
->foldpath
) == NOTOK
)
1145 admonish (mp
->foldpath
, "unable to rename %s to", tmpfil
);
1147 strncpy (map1
, map_name (tmpfil
), sizeof(map1
));
1148 strncpy (map2
, map_name (mp
->foldpath
), sizeof(map2
));
1150 if (rename (map1
, map2
) == NOTOK
) {
1151 admonish (map2
, "unable to rename %s to", map1
);
1160 lkfclosedata (dp
, mp
->foldpath
);
1169 getargs (char *prompt
, struct swit
*sw
, struct Cmd
*cmdp
)
1173 static char buffer
[BUFSIZ
];
1179 printf ("%s", prompt
);
1182 for (cp
= buffer
; (i
= getchar ()) != '\n';) {
1183 if (interrupted
&& !told_to_quit
) {
1188 if (told_to_quit
|| i
== EOF
) {
1191 kill (ppid
, SIGEMT
);
1193 kill (ppid
, SIGTERM
);
1197 if (cp
< &buffer
[sizeof buffer
- 2])
1204 if (buffer
[0] == '?') {
1205 printf ("commands:\n");
1206 print_sw (ALL
, sw
, "", stdout
);
1207 printf ("type CTRL-D or use ``quit'' to leave %s\n",
1212 if (parse (buffer
, cmdp
) == NOTOK
)
1215 switch (i
= smatch (cmdp
->args
[0], sw
)) {
1217 ambigsw (cmdp
->args
[0], sw
);
1220 printf ("say what: ``%s'' -- type ? (or help) for help\n",
1231 getcmds (struct swit
*sw
, struct Cmd
*cmdp
, int scansw
)
1234 struct record rcs
, *rc
;
1240 switch (peer2rc (rc
)) {
1242 pQRY (rc
->rc_data
, scansw
);
1246 if ((i
= pCMD (rc
->rc_data
, sw
, cmdp
)) != NOTOK
)
1253 kill (ppid
, SIGEMT
);
1255 kill (ppid
, SIGTERM
);
1260 padios (NULL
, "%s", rc
->rc_data
);
1263 fmt2peer (RC_ERR
, "pLOOP protocol screw-up");
1270 parse (char *buffer
, struct Cmd
*cmdp
)
1276 pp
= cmdp
->args
[argp
++] = cmdp
->line
;
1277 cmdp
->redirect
= NULL
;
1278 cmdp
->direction
= STDIO
;
1279 cmdp
->stream
= NULL
;
1281 for (cp
= buffer
; (c
= *cp
); cp
++) {
1282 if (!isspace ((unsigned char) c
))
1287 fmt2peer (RC_EOF
, "null command");
1291 while ((c
= *cp
++)) {
1292 if (isspace ((unsigned char) c
)) {
1293 while (isspace ((unsigned char) c
))
1298 cmdp
->args
[argp
++] = pp
;
1305 switch (c
= *cp
++) {
1307 padvise (NULL
, "unmatched \"");
1312 if ((c
= *cp
++) == 0)
1323 if ((c
= *cp
++) == 0) {
1325 padvise (NULL
, "the newline character can not be quoted");
1335 if (pp
== cmdp
->line
) {
1336 padvise (NULL
, "invalid null command");
1339 if (*cmdp
->args
[argp
- 1] == 0)
1341 cmdp
->direction
= c
== '>' ? CRTIO
: PIPIO
;
1342 if (cmdp
->direction
== CRTIO
&& (c
= *cp
) == '>') {
1343 cmdp
->direction
= APPIO
;
1346 cmdp
->redirect
= pp
+ 1;/* sigh */
1347 for (; (c
= *cp
); cp
++)
1348 if (!isspace ((unsigned char) c
))
1351 padvise (NULL
, cmdp
->direction
!= PIPIO
1352 ? "missing name for redirect"
1353 : "invalid null command");
1356 strcpy (cmdp
->redirect
, cp
);
1357 if (cmdp
->direction
!= PIPIO
) {
1359 if (isspace ((unsigned char) *cp
)) {
1360 padvise (NULL
, "bad name for redirect");
1363 if (expand (cmdp
->redirect
) == NOTOK
)
1372 cmdp
->args
[argp
] = NULL
;
1379 expand (char *redirect
)
1385 if (*redirect
!= '~')
1388 if ((cp
= strchr(pp
= redirect
+ 1, '/')))
1393 if ((pw
= getpwnam (pp
)))
1396 padvise (NULL
, "unknown user: %s", pp
);
1400 snprintf (path
, sizeof(path
), "%s/%s", pp
, cp
? cp
: "");
1401 strcpy (redirect
, path
);
1407 init_io (struct Cmd
*cmdp
, int vio
)
1414 result
= initaux_io (cmdp
);
1422 initaux_io (struct Cmd
*cmdp
)
1426 switch (cmdp
->direction
) {
1432 mode
= cmdp
->direction
== CRTIO
? "write" : "append";
1433 if ((cmdp
->stream
= fopen (cmdp
->redirect
, mode
)) == NULL
) {
1434 padvise (cmdp
->redirect
, "unable to %s ", mode
);
1435 cmdp
->direction
= STDIO
;
1441 if ((cmdp
->stream
= popen (cmdp
->redirect
, "w")) == NULL
) {
1442 padvise (cmdp
->redirect
, "unable to pipe");
1443 cmdp
->direction
= STDIO
;
1446 SIGNAL (SIGPIPE
, pipeser
);
1451 padios (NULL
, "unknown redirection for command");
1455 if (dup2 (fileno (cmdp
->stream
), fileno (stdout
)) == NOTOK
)
1456 padios ("standard output", "unable to dup2");
1464 fin_io (struct Cmd
*cmdp
, int vio
)
1476 finaux_io (struct Cmd
*cmdp
)
1478 switch (cmdp
->direction
) {
1485 close (fileno (stdout
));
1486 if (ferror (stdout
))
1487 padvise (NULL
, "problems writing %s", cmdp
->redirect
);
1488 fclose (cmdp
->stream
);
1493 close (fileno (stdout
));
1494 pclose (cmdp
->stream
);
1495 SIGNAL (SIGPIPE
, SIG_DFL
);
1499 padios (NULL
, "unknown redirection for command");
1502 if (dup2 (fileno (sp
), fileno (stdout
)) == NOTOK
)
1503 padios ("standard output", "unable to dup2");
1506 cmdp
->direction
= STDIO
;
1515 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1516 unset_selected (mp
, msgnum
);
1517 mp
->lowsel
= mp
->hghsel
= mp
->numsel
= 0;
1525 folder_free (mp
); /* free folder/message structure */
1531 seq_setcur (struct msgs
*mp
, int msgnum
)
1533 if (mp
->curmsg
== msgnum
)
1536 if (mp
->curmsg
&& Msgs
[mp
->curmsg
].m_scanl
) {
1537 free (Msgs
[mp
->curmsg
].m_scanl
);
1538 Msgs
[mp
->curmsg
].m_scanl
= NULL
;
1540 if (Msgs
[msgnum
].m_scanl
) {
1541 free (Msgs
[msgnum
].m_scanl
);
1542 Msgs
[msgnum
].m_scanl
= NULL
;
1545 mp
->curmsg
= msgnum
;
1563 if (broken_pipe
++ == 0)
1564 fprintf (stderr
, "broken pipe\n");
1583 longjmp (peerenv
, DONE
);
1592 struct record rcs
, *rc
;
1597 switch (peer2rc (rc
)) {
1600 while (isspace ((unsigned char) *bp
))
1602 if (sscanf (bp
, "%d", &vrsn
) != 1) {
1604 fmt2peer (RC_ERR
, "bad init \"%s\"", rc
->rc_data
);
1607 if (vrsn
!= RC_VRSN
) {
1608 fmt2peer (RC_ERR
, "version %d unsupported", vrsn
);
1612 while (*bp
&& !isspace ((unsigned char) *bp
))
1614 while (isspace ((unsigned char) *bp
))
1616 if (sscanf (bp
, "%d", &numwins
) != 1 || numwins
<= 0)
1621 for (i
= 1; i
<= numwins
; i
++) {
1622 while (*bp
&& !isspace ((unsigned char) *bp
))
1624 while (isspace ((unsigned char) *bp
))
1626 if (sscanf (bp
, "%d", &windows
[i
]) != 1 || windows
[i
] <= 0)
1629 rc2peer (RC_ACK
, 0, NULL
);
1633 padios (NULL
, "%s", rc
->rc_data
);
1636 fmt2peer (RC_ERR
, "pINI protocol screw-up");
1637 done (1); /* NOTREACHED */
1640 return 1; /* dead code to satisfy the compiler */
1645 pQRY (char *str
, int scansw
)
1648 if (pQRY1 (scansw
) == NOTOK
|| pQRY2 () == NOTOK
)
1651 rc2peer (RC_EOF
, 0, NULL
);
1660 static int lastlow
= 0,
1665 oldhgh
= mp
->hghmsg
;
1666 if (check_folder (scansw
) && oldhgh
< mp
->hghmsg
) {
1667 switch (winX (STATUS
)) {
1672 printf ("new messages have arrived!");
1675 _exit (0); /* NOTREACHED */
1678 lastlow
= lastcur
= lasthgh
= lastnum
= 0;
1682 switch (winX (DISPLAY
)) {
1687 scanrange (oldhgh
+ 1, mp
->hghmsg
);
1690 _exit (0); /* NOTREACHED */
1699 switch (winX (STATUS
)) {
1704 printf ("%s: gap in ID:s, last seen %d, lowest present %d\n",
1705 myname
? myname
: fmsh
? fmsh
: mp
->foldpath
, gap
- 1,
1706 readid (mp
->lowmsg
));
1709 _exit (0); /* NOTREACHED */
1716 if (mp
->lowmsg
!= lastlow
1717 || mp
->curmsg
!= lastcur
1718 || mp
->hghmsg
!= lasthgh
1719 || mp
->nummsg
!= lastnum
)
1720 switch (winX (STATUS
)) {
1728 _exit (0); /* NOTREACHED */
1731 lastlow
= mp
->lowmsg
;
1732 lastcur
= mp
->curmsg
;
1733 lasthgh
= mp
->hghmsg
;
1734 lastnum
= mp
->nummsg
;
1745 int i
, j
, k
, msgnum
, n
;
1751 if (mp
->nummsg
== 0 && mp
->nummsg
!= num
)
1752 switch (winX (SCAN
)) {
1760 _exit (0); /* NOTREACHED */
1769 j
= (k
= windows
[SCAN
]) / 2;
1770 for (msgnum
= mp
->curmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1771 if (does_exist (mp
, msgnum
))
1775 k
= i
>= k
? 1 : k
- i
;
1782 for (msgnum
= mp
->curmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
1783 if (does_exist (mp
, msgnum
)) {
1790 for (msgnum
= mp
->curmsg
+ 1; msgnum
<= mp
->hghmsg
; msgnum
++)
1791 if (does_exist (mp
, msgnum
)) {
1795 if (n
++ >= windows
[SCAN
])
1801 && does_exist (mp
, lo
)
1802 && does_exist (mp
, hi
)
1804 || (lo
== mp
->curmsg
&& lo
== mp
->lowmsg
))
1806 || (hi
== mp
->curmsg
&& hi
== mp
->hghmsg
))
1807 && hi
- lo
== j
- i
)
1810 if (mp
->curmsg
!= cur
|| modified
)
1811 switch (winN (NULLCMD
, SCAN
, 0)) {
1819 scanrange (lo
= i
, hi
= j
);
1830 pCMD (char *str
, struct swit
*sw
, struct Cmd
*cmdp
)
1835 switch (winX (DISPLAY
)) {
1840 printf ("commands:\n");
1841 print_sw (ALL
, sw
, "", stdout
);
1842 printf ("type ``quit'' to leave %s\n", invo_name
);
1845 _exit (0); /* NOTREACHED */
1848 rc2peer (RC_EOF
, 0, NULL
);
1852 if (parse (str
, cmdp
) == NOTOK
)
1855 switch (i
= smatch (cmdp
->args
[0], sw
)) {
1857 switch (winX (DISPLAY
)) {
1862 ambigsw (cmdp
->args
[0], sw
);
1865 _exit (0); /* NOTREACHED */
1868 rc2peer (RC_EOF
, 0, NULL
);
1874 "say what: ``%s'' -- type ? (or help) for help",
1889 switch (setjmp (peerenv
)) {
1891 SIGNAL (SIGALRM
, alrmser
);
1894 status
= peerwait ();
1908 struct record rcs
, *rc
;
1913 switch (peer2rc (rc
)) {
1916 rc2peer (RC_FIN
, 0, NULL
);
1920 advise (NULL
, "%s", rc
->rc_data
);
1924 fmt2peer (RC_FIN
, "pLOOP protocol screw-up");
1931 ttyNaux (struct Cmd
*cmdp
, char *s
)
1933 struct record rcs
, *rc
;
1938 if (cmdp
&& init_io (cmdp
, vmh
) == NOTOK
)
1941 /* XXX: fseek() too tricky for our own good */
1943 fseek (fp
, 0L, SEEK_SET
);
1946 switch (rc2rc (RC_TTY
, s
? strlen (s
) : 0, s
, rc
)) {
1948 vmhtty
= OK
; /* fall */
1953 padios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
1956 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
1957 done (1); /* NOTREACHED */
1961 SIGNAL (SIGTSTP
, tstat
);
1968 ttyR (struct Cmd
*cmdp
)
1970 struct record rcs
, *rc
;
1975 SIGNAL (SIGTSTP
, SIG_IGN
);
1987 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
1989 rc2peer (RC_EOF
, 0, NULL
);
1993 padios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
1996 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
1997 done (1); /* NOTREACHED */
2000 return 1; /* dead code to satisfy compiler */
2005 winN (struct Cmd
*cmdp
, int n
, int eof
)
2008 char buffer
[BUFSIZ
];
2009 struct record rcs
, *rc
;
2012 if (vmhpid
== NOTOK
)
2017 /* XXX: fseek() too tricky for our own good */
2019 fseek (fp
, 0L, SEEK_SET
);
2023 snprintf (buffer
, sizeof(buffer
), "%d", n
);
2024 switch (str2rc (RC_WIN
, buffer
, rc
)) {
2032 padios (NULL
, "%s", rc
->rc_data
);
2035 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2039 if (pipe (pd
) == NOTOK
) {
2040 err2peer (RC_ERR
, "pipe", "unable to");
2044 switch (vmhpid
= fork()) {
2046 err2peer (RC_ERR
, "fork", "unable to");
2053 SIGNAL (SIGPIPE
, SIG_IGN
);
2054 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
2055 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
2063 advise (NULL
, "%s", rc
->rc_data
);
2067 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2071 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2074 rc2peer (RC_EOF
, 0, NULL
);
2079 advise (NULL
, "%s", rc
->rc_data
);
2084 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2089 err2peer (RC_ERR
, "pipe", "error reading from");
2091 _exit (i
!= NOTOK
? i
: 1);
2094 if ((vmhfd0
= dup (fileno (stdin
))) == NOTOK
)
2095 padios ("standard input", "unable to dup");
2096 if ((vmhfd1
= dup (fileno (stdout
))) == NOTOK
)
2097 padios ("standard output", "unable to dup");
2098 if ((vmhfd2
= dup (fileno (stderr
))) == NOTOK
)
2099 padios ("diagnostic output", "unable to dup");
2102 if ((i
= open ("/dev/null", O_RDONLY
)) != NOTOK
&& i
!= fileno (stdin
)) {
2103 dup2 (i
, fileno (stdin
));
2108 if (dup2 (pd
[1], fileno (stdout
)) == NOTOK
)
2109 padios ("standard output", "unable to dup2");
2113 if (dup2 (pd
[1], fileno (stderr
)) == NOTOK
)
2114 padios ("diagnostic output", "unable to dup2");
2117 if (cmdp
&& init_io (cmdp
, 0) == NOTOK
)
2119 pstat
= SIGNAL (SIGPIPE
, pipeser
);
2131 winR (struct Cmd
*cmdp
)
2141 if (dup2 (vmhfd0
, fileno (stdin
)) == NOTOK
)
2142 padios ("standard input", "unable to dup2");
2147 if (dup2 (vmhfd1
, fileno (stdout
)) == NOTOK
)
2148 padios ("standard output", "unable to dup2");
2153 if (dup2 (vmhfd2
, fileno (stderr
)) == NOTOK
)
2154 padios ("diagnostic output", "unable to dup2");
2158 SIGNAL (SIGPIPE
, pstat
);
2160 if ((status
= pidwait (vmhpid
, OK
)) == 2)
2164 return (status
== 0 ? OK
: NOTOK
);
2172 char buffer
[BUFSIZ
];
2173 struct record rcs
, *rc
;
2178 /* XXX: fseek() too tricky for our own good */
2180 fseek (fp
, 0L, SEEK_SET
);
2182 snprintf (buffer
, sizeof(buffer
), "%d", n
);
2183 switch (str2rc (RC_WIN
, buffer
, rc
)) {
2191 padios (NULL
, "%s", rc
->rc_data
);
2194 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2198 if (pipe (pd
) == NOTOK
) {
2199 err2peer (RC_ERR
, "pipe", "unable to");
2203 switch (pid
= fork ()) {
2205 err2peer (RC_ERR
, "fork", "unable to");
2211 close (fileno (stdin
));
2212 if ((i
= open ("/dev/null", O_RDONLY
)) != NOTOK
&& i
!= fileno (stdin
)) {
2213 dup2 (i
, fileno (stdin
));
2216 dup2 (pd
[1], fileno (stdout
));
2217 dup2 (pd
[1], fileno (stderr
));
2225 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
2226 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
2236 padios (NULL
, "%s", rc
->rc_data
);
2239 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2243 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2248 padios (NULL
, "%s", rc
->rc_data
);
2251 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2255 err2peer (RC_ERR
, "pipe", "error reading from");
2259 return (i
!= NOTOK
? pid
: NOTOK
);
2265 padios (char *what
, char *fmt
, ...)
2271 verr2peer (RC_FIN
, what
, fmt
, ap
);
2274 advertise (what
, NULL
, fmt
, ap
);
2283 padvise (char *what
, char *fmt
, ...)
2289 verr2peer (RC_ERR
, what
, fmt
, ap
);
2291 advertise (what
, NULL
, fmt
, ap
);