]>
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 static struct swit switches
[] = {
37 { "idstart number", -7 }, /* interface from bbc */
39 { "idstop number", -6 }, /* .. */
41 { "idquit number", -6 }, /* .. */
43 { "idname BBoard", -6 }, /* .. */
45 { "prompt string", 0 },
53 { "vmhwrite fd", -8 },
57 { "popwrite fd", -8 },
69 static int mbx_style
= MMDF_FORMAT
;
74 char*fmsh
= NULL
; /* folder instead of file */
75 int modified
; /* command modified folder */
76 struct msgs
*mp
; /* used a lot */
78 struct Msg
*Msgs
= NULL
; /* Msgs[0] not used */
79 static FILE *fp
; /* input file */
80 static FILE *yp
= NULL
; /* temporary file */
81 static int mode
; /* mode of file */
82 static int numfds
= 0; /* number of files cached */
83 static int maxfds
= 0; /* number of files cached to be cached */
84 static time_t mtime
= (time_t) 0; /* mtime of file */
89 #define ALARM ((unsigned int) 10)
90 #define ttyN(c) ttyNaux ((c), NULL)
94 static int vmhpid
= OK
;
99 static int vmhtty
= NOTOK
;
106 static int topcur
= 0;
108 static int numwins
= 0;
109 static int windows
[NWIN
+ 1];
111 static jmp_buf peerenv
;
116 static int pfd
= NOTOK
; /* fd parent is reading from */
117 static int ppid
= 0; /* pid of parent */
122 int interactive
; /* running from a /dev/tty */
123 int redirected
; /* re-directing output */
124 FILE *sp
= NULL
; /* original stdout */
126 char *cmd_name
; /* command being run */
127 char myfilter
[BUFSIZ
]; /* path to mhl.forward */
129 static char *myprompt
= "(%s) ";/* prompting string */
134 static int gap
; /* gap in BBoard-ID:s */
135 static char *myname
= NULL
; /* BBoard name */
136 char *BBoard_ID
= "BBoard-ID"; /* BBoard-ID constant */
141 SIGNAL_HANDLER istat
; /* original SIGINT */
142 static SIGNAL_HANDLER pstat
; /* current SIGPIPE */
143 SIGNAL_HANDLER qstat
; /* original SIGQUIT */
146 SIGNAL_HANDLER tstat
; /* original SIGTSTP */
149 int interrupted
; /* SIGINT detected */
150 int broken_pipe
; /* SIGPIPE detected */
151 int told_to_quit
; /* SIGQUIT detected */
153 extern m_getfld_state_t gstate
;
158 void fsetup (char *);
162 void display_info (int);
165 void seq_setcur (struct msgs
*, int);
166 void padios (char *, char *, ...);
167 void padvise (char *, char *, ...);
173 static void msh (int);
174 static int read_map (char *, long);
175 static int read_file (long, int);
177 static void m_gMsgs (int);
178 static int check_folder (int);
179 static void scanrange (int, int);
180 static void scanstring (char *);
181 static void write_ids (void);
182 static void quit (void);
183 static int getargs (char *, struct swit
*, struct Cmd
*);
184 static int getcmds (struct swit
*, struct Cmd
*, int);
185 static int parse (char *, struct Cmd
*);
186 static int init_io (struct Cmd
*, int);
187 static int initaux_io (struct Cmd
*);
188 static void fin_io (struct Cmd
*, int);
189 static void finaux_io (struct Cmd
*);
190 static void m_init (void);
191 static void intrser (int);
192 static void pipeser (int);
193 static void quitser (int);
194 static void alrmser (int);
195 static int pINI (void);
196 static int pQRY (char *, int);
197 static int pQRY1 (int);
198 static int pQRY2 (void);
199 static int pCMD (char *, struct swit
*, struct Cmd
*);
200 static int pFIN (void);
201 static int peerwait (void);
202 static int ttyNaux (struct Cmd
*, char *);
203 static int ttyR (struct Cmd
*);
204 static int winN (struct Cmd
*, int, int);
205 static int winR (struct Cmd
*);
206 static int winX (int);
210 main (int argc
, char **argv
)
212 int id
= 0, scansw
= 0, vmh1
= 0, vmh2
= 0;
213 char *cp
, *file
= NULL
, *folder
= NULL
;
214 char **argp
, **arguments
, buf
[BUFSIZ
];
217 setlocale(LC_ALL
, "");
219 invo_name
= r1bindex (argv
[0], '/');
221 /* read user profile/context */
224 mts_init (invo_name
);
225 arguments
= getarguments (invo_name
, argc
,argv
, 1);
228 while ((cp
= *argp
++)) {
230 switch (smatch (++cp
, switches
)) {
232 ambigsw (cp
, switches
);
235 adios (NULL
, "-%s unknown", cp
);
238 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
239 print_help (buf
, switches
, 1);
242 print_version(invo_name
);
246 if (!(cp
= *argp
++) || *cp
== '-')
247 adios (NULL
, "missing argument to %s", argp
[-2]);
248 if ((id
= atoi (cp
)) < 1)
249 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
252 if (!(cp
= *argp
++) || *cp
== '-')
253 adios (NULL
, "missing argument to %s", argp
[-2]);
254 if ((pfd
= atoi (cp
)) <= 1)
255 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
258 if (!(cp
= *argp
++) || *cp
== '-')
259 adios (NULL
, "missing argument to %s", argp
[-2]);
260 if ((ppid
= atoi (cp
)) <= 1)
261 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
264 if (!(myname
= *argp
++) || *myname
== '-')
265 adios (NULL
, "missing argument to %s", argp
[-2]);
276 if (!(myprompt
= *argp
++) || *myprompt
== '-')
277 adios (NULL
, "missing argument to %s", argp
[-2]);
281 if (!(cp
= *argp
++) || *cp
== '-')
282 adios (NULL
, "missing argument to %s", argp
[-2]);
283 if ((vmh1
= atoi (cp
)) < 1)
284 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
287 if (!(cp
= *argp
++) || *cp
== '-')
288 adios (NULL
, "missing argument to %s", argp
[-2]);
289 if ((vmh2
= atoi (cp
)) < 1)
290 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
294 if (!(cp
= *argp
++) || *cp
== '-')
295 adios (NULL
, "missing argument to %s", argp
[-2]);
298 if (!(cp
= *argp
++) || *cp
== '-')
299 adios (NULL
, "missing argument to %s", argp
[-2]);
309 if (*cp
== '+' || *cp
== '@') {
311 adios (NULL
, "only one folder at a time!");
313 folder
= pluspath (cp
);
317 adios (NULL
, "only one file at a time!");
322 if (!file
&& !folder
)
325 adios (NULL
, "use a file or a folder, not both");
326 strncpy (myfilter
, etcpath (mhlforward
), sizeof(myfilter
));
329 ioctl (pfd
, FIOCLEX
, NULL
);
332 istat
= SIGNAL2 (SIGINT
, intrser
);
333 qstat
= SIGNAL2 (SIGQUIT
, quitser
);
335 sc_width (); /* MAGIC... */
337 if ((vmh
= vmh1
&& vmh2
)) {
340 SIGNAL (SIGINT
, SIG_IGN
);
341 SIGNAL (SIGQUIT
, SIG_IGN
);
343 tstat
= SIGNAL (SIGTSTP
, SIG_IGN
);
347 m_getfld_state_init (&gstate
);
353 display_info (id
> 0 ? scansw
: 0);
355 msh (id
> 0 ? scansw
: 0);
356 m_getfld_state_destroy (&gstate
);
365 static struct swit mshcmds
[] = {
430 register char *cp
, **ap
;
431 char prompt
[BUFSIZ
], *vec
[MAXARGS
];
433 register struct Cmd
*cmdp
;
434 static int once_only
= ADVCMD
;
436 snprintf (prompt
, sizeof(prompt
), myprompt
, invo_name
);
445 if ((i
= getcmds (mshcmds
, cmdp
, scansw
)) == EOF
) {
450 check_folder (scansw
);
451 if ((i
= getargs (prompt
, mshcmds
, cmdp
)) == EOF
) {
456 cmd_name
= mshcmds
[i
].sw
;
464 if (once_only
== ADVCMD
)
465 once_only
= i
= SHOWCMD
;
467 i
= mp
->curmsg
!= mp
->hghmsg
? NEXTCMD
: EXITCMD
;
468 cmd_name
= mshcmds
[i
].sw
;
474 case FORWCMD
: /* sigh */
484 if ((cp
= context_find (cmd_name
))) {
486 ap
= brkstring (cp
, " ", "\n");
487 ap
= copyip (ap
, vec
, MAXARGS
);
498 copyip (cmdp
->args
+ 1, ap
, MAXARGS
);
502 if (!vmh
&& init_io (cmdp
, vmh
) == NOTOK
) {
508 redirected
= vmh
|| cmdp
->direction
!= STDIO
;
519 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
520 forkcmd (vec
, cmd_name
);
524 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
529 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
535 || (filehak (vec
) == OK
? ttyN (cmdp
)
536 : winN (cmdp
, DISPLAY
, 1)) != NOTOK
)
541 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
546 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
551 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
557 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
562 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
569 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
575 || (packhak (vec
) == OK
? ttyN (cmdp
)
576 : winN (cmdp
, DISPLAY
, 1)) != NOTOK
)
581 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
586 if (!vmh
|| ttyN (cmdp
) != NOTOK
)
591 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
596 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
601 if (!vmh
|| winN (cmdp
, DISPLAY
, 1) != NOTOK
)
606 padios (NULL
, "no dispatch for %s", cmd_name
);
628 fsetup (char *folder
)
634 maildir
= m_maildir (folder
);
635 if (chdir (maildir
) == NOTOK
)
636 padios (maildir
, "unable to change directory to");
638 /* read folder and create message structure */
639 if (!(mp
= folder_read (folder
)))
640 padios (NULL
, "unable to read folder %s", folder
);
642 /* check for empty folder */
644 padios (NULL
, "no messages in %s", folder
);
647 mtime
= stat (mp
->foldpath
, &st
) != NOTOK
? st
.st_mtime
: 0;
649 m_gMsgs (mp
->hghmsg
);
651 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++) {
652 Msgs
[msgnum
].m_bboard_id
= 0;
653 Msgs
[msgnum
].m_top
= NOTOK
;
654 Msgs
[msgnum
].m_start
= Msgs
[msgnum
].m_stop
= 0L;
655 Msgs
[msgnum
].m_scanl
= NULL
;
660 fmsh
= getcpy (folder
);
662 maxfds
= OPEN_MAX
/ 2;
664 if ((maxfds
-= 2) < 1)
674 if ((fp
= fopen (file
, "r")) == NULL
)
675 padios (file
, "unable to read");
677 ioctl (fileno (fp
), FIOCLEX
, NULL
);
679 if (fstat (fileno (fp
), &st
) != NOTOK
) {
680 mode
= (int) (st
.st_mode
& 0777), mtime
= st
.st_mtime
;
681 msgp
= read_map (file
, (long) st
.st_size
);
684 mode
= m_gmprot (), mtime
= 0;
688 if ((msgp
= read_file (msgp
? Msgs
[msgp
].m_stop
: 0L, msgp
+ 1)) < 1)
689 padios (NULL
, "no messages in %s", myname
? myname
: file
);
691 if (!(mp
= (struct msgs
*) calloc ((size_t) 1, sizeof(*mp
))))
692 padios (NULL
, "unable to allocate folder storage");
694 if (!(mp
->msgstats
= calloc ((size_t) msgp
+ 3, sizeof(*(mp
->msgstats
)))))
695 padios (NULL
, "unable to allocate message status storage");
701 mp
->foldpath
= getcpy (myname
? myname
: file
);
702 clear_folder_flags (mp
);
705 if (st
.st_uid
!= getuid () || access (file
, W_OK
) == NOTOK
)
709 mp
->hghoff
= mp
->hghmsg
+ 1;
711 for (i
= mp
->lowmsg
; i
<= mp
->hghmsg
; i
++) {
712 clear_msg_flags (mp
, i
);
717 mp
->msgattrs
[0] = getcpy ("unseen");
718 mp
->msgattrs
[1] = NULL
;
720 m_unknown (gstate
, fp
); /* the MAGIC invocation */
729 read_map (char *file
, long size
)
731 register int i
, msgp
;
732 register struct drop
*dp
, *mp
;
735 if ((i
= map_read (file
, size
, &rp
, 1)) == 0)
741 for (dp
= rp
+ 1; i
-- > 0; msgp
++, dp
++) {
742 mp
= &Msgs
[msgp
].m_drop
;
744 mp
->d_size
= dp
->d_size
;
745 mp
->d_start
= dp
->d_start
;
746 mp
->d_stop
= dp
->d_stop
;
747 Msgs
[msgp
].m_scanl
= NULL
;
756 read_file (long pos
, int msgp
)
759 register struct drop
*dp
, *mp
;
762 if ((i
= mbx_read (fp
, pos
, &rp
, 1)) <= 0)
765 m_gMsgs ((msgp
- 1) + i
);
767 for (dp
= rp
; i
-- > 0; msgp
++, dp
++) {
768 mp
= &Msgs
[msgp
].m_drop
;
770 mp
->d_size
= dp
->d_size
;
771 mp
->d_start
= dp
->d_start
;
772 mp
->d_stop
= dp
->d_stop
;
773 Msgs
[msgp
].m_scanl
= NULL
;
787 nMsgs
= n
+ MAXFOLDER
/ 2;
788 Msgs
= (struct Msg
*) calloc ((size_t) (nMsgs
+ 2), sizeof *Msgs
);
790 padios (NULL
, "unable to allocate Msgs structure");
797 nmsgs
= nMsgs
+ n
+ MAXFOLDER
/ 2;
798 Msgs
= (struct Msg
*) mh_xrealloc ((char *) Msgs
, (size_t) (nmsgs
+ 2) * sizeof *Msgs
);
799 memset((char *) (Msgs
+ nMsgs
+ 2), 0, (size_t) ((nmsgs
- nMsgs
) * sizeof *Msgs
));
806 msh_ready (int msgnum
, int full
)
819 if ((fd
= Msgs
[msgnum
].m_top
) == NOTOK
) {
820 if (numfds
>= maxfds
)
821 for (msgp
= mp
->lowmsg
; msgp
<= mp
->hghmsg
; msgp
++)
822 if (Msgs
[msgp
].m_top
!= NOTOK
) {
823 close (Msgs
[msgp
].m_top
);
824 Msgs
[msgp
].m_top
= NOTOK
;
829 if ((fd
= open (cp
= m_name (msgnum
), O_RDONLY
)) == NOTOK
)
830 padios (cp
, "unable to open message");
831 Msgs
[msgnum
].m_top
= fd
;
835 if ((fd
= dup (fd
)) == NOTOK
)
836 padios ("cached message", "unable to dup");
837 if ((yp
= fdopen (fd
, "r")) == NULL
)
838 padios (NULL
, "unable to fdopen cached message");
839 fseek (yp
, 0L, SEEK_SET
);
843 m_eomsbr (gstate
, (int (*)()) 0); /* XXX */
844 fseek (fp
, Msgs
[msgnum
].m_start
, SEEK_SET
);
850 check_folder (int scansw
)
852 int seqnum
, i
, low
, hgh
, msgp
;
856 if (stat (mp
->foldpath
, &st
) == NOTOK
)
857 padios (mp
->foldpath
, "unable to stat");
858 if (mtime
== st
.st_mtime
)
862 low
= mp
->hghmsg
+ 1;
863 folder_free (mp
); /* free folder/message structure */
865 if (!(mp
= folder_read (fmsh
)))
866 padios (NULL
, "unable to re-read folder %s", fmsh
);
870 for (msgp
= mp
->lowmsg
; msgp
<= mp
->hghmsg
; msgp
++) {
871 if (Msgs
[msgp
].m_top
!= NOTOK
) {
872 close (Msgs
[msgp
].m_top
);
873 Msgs
[msgp
].m_top
= NOTOK
;
876 if (Msgs
[msgp
].m_scanl
) {
877 free (Msgs
[msgp
].m_scanl
);
878 Msgs
[msgp
].m_scanl
= NULL
;
884 if (modified
|| low
> hgh
)
888 if (fstat (fileno (fp
), &st
) == NOTOK
)
889 padios (mp
->foldpath
, "unable to fstat");
890 if (mtime
== st
.st_mtime
)
892 mode
= (int) (st
.st_mode
& 0777);
895 if ((msgp
= read_file (Msgs
[mp
->hghmsg
].m_stop
, mp
->hghmsg
+ 1)) < 1)
896 padios (NULL
, "no messages in %s", mp
->foldpath
); /* XXX */
897 if (msgp
>= MAXFOLDER
)
898 padios (NULL
, "more than %d messages in %s", MAXFOLDER
,
900 if (msgp
<= mp
->hghmsg
)
903 if (!(mp
= folder_realloc (mp
, mp
->lowoff
, msgp
)))
904 padios (NULL
, "unable to allocate folder storage");
906 low
= mp
->hghmsg
+ 1, hgh
= msgp
;
907 seqnum
= scansw
? seq_getnum (mp
, "unseen") : -1;
908 for (i
= mp
->hghmsg
+ 1; i
<= msgp
; i
++) {
911 add_sequence(mp
, seqnum
, i
);
921 advise (NULL
, "new messages have arrived!\007");
923 scanrange (low
, hgh
);
930 scanrange (int low
, int hgh
)
934 snprintf (buffer
, sizeof(buffer
), "%d-%d", low
, hgh
);
940 scanstring (char *arg
)
942 char *cp
, **ap
, *vec
[MAXARGS
];
945 * This should be replace with a call to getarguments()
947 if ((cp
= context_find (cmd_name
= "scan"))) {
949 ap
= brkstring (cp
, " ", "\n");
950 ap
= copyip (ap
, vec
, MAXARGS
);
966 register int cur
, seqnum
, i
=0, msgnum
;
969 seq_setcur (mp
, mp
->lowmsg
);
970 if (id
<= 0 || (seqnum
= seq_getnum (mp
, "unseen")) == -1)
973 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
974 add_sequence(mp
, seqnum
, msgnum
);
979 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
980 if (does_exist(mp
, msgnum
)) /* FIX */
981 if ((i
= readid (msgnum
)) > 0 && i
< id
) {
983 clear_sequence(mp
, seqnum
, msgnum
);
986 for (i
= mp
->lowmsg
; i
< msgnum
; i
++)
987 clear_sequence(mp
, seqnum
, i
);
989 if (cur
> mp
->hghmsg
)
992 seq_setcur (mp
, cur
);
995 if ((gap
= 1 < id
&& id
< (i
= readid (mp
->lowmsg
)) ? id
: 0) && !vmh
)
996 advise (NULL
, "gap in ID:s, last seen %d, lowest present %d\n",
1005 char *bp
, buf
[BUFSIZ
], name
[NAMESZ
];
1008 if (Msgs
[msgnum
].m_bboard_id
)
1009 return Msgs
[msgnum
].m_bboard_id
;
1011 zp
= msh_ready (msgnum
, 0);
1013 int bufsz
= sizeof buf
;
1014 switch (state
= m_getfld (gstate
, name
, buf
, &bufsz
, zp
)) {
1017 if (!mh_strcasecmp (name
, BBoard_ID
)) {
1019 while (state
== FLDPLUS
) {
1021 state
= m_getfld (gstate
, name
, buf
, &bufsz
, zp
);
1027 return (Msgs
[msgnum
].m_bboard_id
= i
);
1031 while (state
== FLDPLUS
)
1033 state
= m_getfld (gstate
, name
, buf
, &bufsz
, zp
);
1044 display_info (int scansw
)
1048 interactive
= isatty (fileno (stdout
));
1050 if ((sd
= dup (fileno (stdout
))) == NOTOK
)
1051 padios ("standard output", "unable to dup");
1053 ioctl (sd
, FIOCLEX
, NULL
);
1054 #endif /* FIOCLEX */
1055 if ((sp
= fdopen (sd
, "w")) == NULL
)
1056 padios ("standard output", "unable to fdopen");
1059 m_putenv ("mhfolder", mp
->foldpath
);
1064 printf ("Reading ");
1065 if (SOprintf ("%s", myname
))
1066 printf ("%s", myname
);
1067 printf (", currently at message %d of %d\n",
1068 mp
->curmsg
, mp
->hghmsg
);
1071 printf ("Reading ");
1073 printf ("+%s", fmsh
);
1075 printf ("%s", mp
->foldpath
);
1076 printf (", currently at message %d of %d\n",
1077 mp
->curmsg
, mp
->hghmsg
);
1080 if (((seqnum
= seq_getnum (mp
, "unseen")) != -1)
1082 && in_sequence(mp
, seqnum
, mp
->hghmsg
))
1083 scanstring ("unseen");
1090 int i
= 0, seqnum
, msgnum
;
1096 if ((seqnum
= seq_getnum (mp
, "unseen")) != -1)
1097 for (msgnum
= mp
->hghmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
1098 if (!in_sequence(mp
, seqnum
, msgnum
)) {
1099 if (Msgs
[msgnum
].m_bboard_id
== 0)
1101 if ((i
= Msgs
[msgnum
].m_bboard_id
) > 0)
1105 snprintf (buffer
, sizeof(buffer
), "%d %d\n", i
, Msgs
[mp
->hghmsg
].m_bboard_id
);
1106 write (pfd
, buffer
, sizeof(buffer
));
1116 char *cp
, tmpfil
[BUFSIZ
];
1117 char map1
[BUFSIZ
], map2
[BUFSIZ
];
1121 if (!(mp
->msgflags
& MODIFIED
) || is_readonly(mp
) || fmsh
) {
1123 rc2peer (RC_FIN
, 0, NULL
);
1128 ttyNaux (NULLCMD
, "FAST");
1130 if ((dp
= lkfopen (mp
->foldpath
, "r")) == NULL
) {
1131 advise (mp
->foldpath
, "unable to lock");
1138 if (fstat (fileno (dp
), &st
) == NOTOK
) {
1139 advise (mp
->foldpath
, "unable to stat");
1142 if (mtime
!= st
.st_mtime
) {
1143 advise (NULL
, "new messages have arrived, no update");
1146 mode
= (int) (st
.st_mode
& 0777);
1148 if (mp
->nummsg
== 0) {
1149 cp
= concat ("Zero file \"", mp
->foldpath
, "\"? ", NULL
);
1150 if (getanswer (cp
)) {
1151 if ((i
= creat (mp
->foldpath
, mode
)) != NOTOK
)
1154 advise (mp
->foldpath
, "error zero'ing");
1155 unlink (map_name (mp
->foldpath
));/* XXX */
1160 cp
= concat ("Update file \"", mp
->foldpath
, "\"? ", NULL
);
1161 if (!getanswer (cp
))
1163 strncpy (tmpfil
, m_backup (mp
->foldpath
), sizeof(tmpfil
));
1164 if ((md
= mbx_open (tmpfil
, mbx_style
, st
.st_uid
, st
.st_gid
, mode
)) == NOTOK
) {
1165 advise (tmpfil
, "unable to open");
1169 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1170 if (does_exist(mp
, msgnum
) && pack (tmpfil
, md
, msgnum
) == NOTOK
) {
1171 mbx_close (tmpfil
, md
);
1173 unlink (map_name (tmpfil
));
1176 mbx_close (tmpfil
, md
);
1178 if (rename (tmpfil
, mp
->foldpath
) == NOTOK
)
1179 admonish (mp
->foldpath
, "unable to rename %s to", tmpfil
);
1181 strncpy (map1
, map_name (tmpfil
), sizeof(map1
));
1182 strncpy (map2
, map_name (mp
->foldpath
), sizeof(map2
));
1184 if (rename (map1
, map2
) == NOTOK
) {
1185 admonish (map2
, "unable to rename %s to", map1
);
1194 lkfclose (dp
, mp
->foldpath
);
1203 getargs (char *prompt
, struct swit
*sw
, struct Cmd
*cmdp
)
1207 static char buffer
[BUFSIZ
];
1213 printf ("%s", prompt
);
1216 for (cp
= buffer
; (i
= getchar ()) != '\n';) {
1217 if (interrupted
&& !told_to_quit
) {
1222 if (told_to_quit
|| i
== EOF
) {
1225 kill (ppid
, SIGEMT
);
1227 kill (ppid
, SIGTERM
);
1231 if (cp
< &buffer
[sizeof buffer
- 2])
1238 if (buffer
[0] == '?') {
1239 printf ("commands:\n");
1240 print_sw (ALL
, sw
, "", stdout
);
1241 printf ("type CTRL-D or use ``quit'' to leave %s\n",
1246 if (parse (buffer
, cmdp
) == NOTOK
)
1249 switch (i
= smatch (cmdp
->args
[0], sw
)) {
1251 ambigsw (cmdp
->args
[0], sw
);
1254 printf ("say what: ``%s'' -- type ? (or help) for help\n",
1265 getcmds (struct swit
*sw
, struct Cmd
*cmdp
, int scansw
)
1268 struct record rcs
, *rc
;
1274 switch (peer2rc (rc
)) {
1276 pQRY (rc
->rc_data
, scansw
);
1280 if ((i
= pCMD (rc
->rc_data
, sw
, cmdp
)) != NOTOK
)
1287 kill (ppid
, SIGEMT
);
1289 kill (ppid
, SIGTERM
);
1294 padios (NULL
, "%s", rc
->rc_data
);
1297 fmt2peer (RC_ERR
, "pLOOP protocol screw-up");
1304 parse (char *buffer
, struct Cmd
*cmdp
)
1307 unsigned char c
, *cp
;
1311 pp
= cmdp
->args
[argp
++] = cmdp
->line
;
1312 cmdp
->redirect
= NULL
;
1313 cmdp
->direction
= STDIO
;
1314 cmdp
->stream
= NULL
;
1316 for (cp
= buffer
; (c
= *cp
); cp
++) {
1322 fmt2peer (RC_EOF
, "null command");
1326 while ((c
= *cp
++)) {
1333 cmdp
->args
[argp
++] = pp
;
1340 switch (c
= *cp
++) {
1342 padvise (NULL
, "unmatched \"");
1347 if ((c
= *cp
++) == 0)
1358 if ((c
= *cp
++) == 0) {
1360 padvise (NULL
, "the newline character can not be quoted");
1370 if (pp
== cmdp
->line
) {
1371 padvise (NULL
, "invalid null command");
1374 if (*cmdp
->args
[argp
- 1] == 0)
1376 cmdp
->direction
= c
== '>' ? CRTIO
: PIPIO
;
1377 if (cmdp
->direction
== CRTIO
&& (c
= *cp
) == '>') {
1378 cmdp
->direction
= APPIO
;
1381 cmdp
->redirect
= pp
+ 1;/* sigh */
1382 for (; (c
= *cp
); cp
++)
1386 padvise (NULL
, cmdp
->direction
!= PIPIO
1387 ? "missing name for redirect"
1388 : "invalid null command");
1391 strcpy (cmdp
->redirect
, cp
);
1392 if (cmdp
->direction
!= PIPIO
) {
1394 if (isspace (*cp
)) {
1395 padvise (NULL
, "bad name for redirect");
1398 if (expand (cmdp
->redirect
) == NOTOK
)
1407 cmdp
->args
[argp
] = NULL
;
1414 expand (char *redirect
)
1420 if (*redirect
!= '~')
1423 if ((cp
= strchr(pp
= redirect
+ 1, '/')))
1428 if ((pw
= getpwnam (pp
)))
1431 padvise (NULL
, "unknown user: %s", pp
);
1435 snprintf (path
, sizeof(path
), "%s/%s", pp
, cp
? cp
: "");
1436 strcpy (redirect
, path
);
1442 init_io (struct Cmd
*cmdp
, int vio
)
1449 result
= initaux_io (cmdp
);
1457 initaux_io (struct Cmd
*cmdp
)
1461 switch (cmdp
->direction
) {
1467 mode
= cmdp
->direction
== CRTIO
? "write" : "append";
1468 if ((cmdp
->stream
= fopen (cmdp
->redirect
, mode
)) == NULL
) {
1469 padvise (cmdp
->redirect
, "unable to %s ", mode
);
1470 cmdp
->direction
= STDIO
;
1476 if ((cmdp
->stream
= popen (cmdp
->redirect
, "w")) == NULL
) {
1477 padvise (cmdp
->redirect
, "unable to pipe");
1478 cmdp
->direction
= STDIO
;
1481 SIGNAL (SIGPIPE
, pipeser
);
1486 padios (NULL
, "unknown redirection for command");
1490 if (dup2 (fileno (cmdp
->stream
), fileno (stdout
)) == NOTOK
)
1491 padios ("standard output", "unable to dup2");
1499 fin_io (struct Cmd
*cmdp
, int vio
)
1511 finaux_io (struct Cmd
*cmdp
)
1513 switch (cmdp
->direction
) {
1520 close (fileno (stdout
));
1521 if (ferror (stdout
))
1522 padvise (NULL
, "problems writing %s", cmdp
->redirect
);
1523 fclose (cmdp
->stream
);
1528 close (fileno (stdout
));
1529 pclose (cmdp
->stream
);
1530 SIGNAL (SIGPIPE
, SIG_DFL
);
1534 padios (NULL
, "unknown redirection for command");
1537 if (dup2 (fileno (sp
), fileno (stdout
)) == NOTOK
)
1538 padios ("standard output", "unable to dup2");
1541 cmdp
->direction
= STDIO
;
1550 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1551 unset_selected (mp
, msgnum
);
1552 mp
->lowsel
= mp
->hghsel
= mp
->numsel
= 0;
1560 folder_free (mp
); /* free folder/message structure */
1566 seq_setcur (struct msgs
*mp
, int msgnum
)
1568 if (mp
->curmsg
== msgnum
)
1571 if (mp
->curmsg
&& Msgs
[mp
->curmsg
].m_scanl
) {
1572 free (Msgs
[mp
->curmsg
].m_scanl
);
1573 Msgs
[mp
->curmsg
].m_scanl
= NULL
;
1575 if (Msgs
[msgnum
].m_scanl
) {
1576 free (Msgs
[msgnum
].m_scanl
);
1577 Msgs
[msgnum
].m_scanl
= NULL
;
1580 mp
->curmsg
= msgnum
;
1598 if (broken_pipe
++ == 0)
1599 fprintf (stderr
, "broken pipe\n");
1618 longjmp (peerenv
, DONE
);
1627 struct record rcs
, *rc
;
1632 switch (peer2rc (rc
)) {
1635 while (isspace (*bp
))
1637 if (sscanf (bp
, "%d", &vrsn
) != 1) {
1639 fmt2peer (RC_ERR
, "bad init \"%s\"", rc
->rc_data
);
1642 if (vrsn
!= RC_VRSN
) {
1643 fmt2peer (RC_ERR
, "version %d unsupported", vrsn
);
1647 while (*bp
&& !isspace (*bp
))
1649 while (isspace (*bp
))
1651 if (sscanf (bp
, "%d", &numwins
) != 1 || numwins
<= 0)
1656 for (i
= 1; i
<= numwins
; i
++) {
1657 while (*bp
&& !isspace (*bp
))
1659 while (isspace (*bp
))
1661 if (sscanf (bp
, "%d", &windows
[i
]) != 1 || windows
[i
] <= 0)
1664 rc2peer (RC_ACK
, 0, NULL
);
1668 padios (NULL
, "%s", rc
->rc_data
);
1671 fmt2peer (RC_ERR
, "pINI protocol screw-up");
1672 done (1); /* NOTREACHED */
1675 return 1; /* dead code to satisfy the compiler */
1680 pQRY (char *str
, int scansw
)
1683 if (pQRY1 (scansw
) == NOTOK
|| pQRY2 () == NOTOK
)
1686 rc2peer (RC_EOF
, 0, NULL
);
1695 static int lastlow
= 0,
1700 oldhgh
= mp
->hghmsg
;
1701 if (check_folder (scansw
) && oldhgh
< mp
->hghmsg
) {
1702 switch (winX (STATUS
)) {
1707 printf ("new messages have arrived!");
1710 _exit (0); /* NOTREACHED */
1713 lastlow
= lastcur
= lasthgh
= lastnum
= 0;
1717 switch (winX (DISPLAY
)) {
1722 scanrange (oldhgh
+ 1, mp
->hghmsg
);
1725 _exit (0); /* NOTREACHED */
1734 switch (winX (STATUS
)) {
1739 printf ("%s: gap in ID:s, last seen %d, lowest present %d\n",
1740 myname
? myname
: fmsh
? fmsh
: mp
->foldpath
, gap
- 1,
1741 readid (mp
->lowmsg
));
1744 _exit (0); /* NOTREACHED */
1751 if (mp
->lowmsg
!= lastlow
1752 || mp
->curmsg
!= lastcur
1753 || mp
->hghmsg
!= lasthgh
1754 || mp
->nummsg
!= lastnum
)
1755 switch (winX (STATUS
)) {
1763 _exit (0); /* NOTREACHED */
1766 lastlow
= mp
->lowmsg
;
1767 lastcur
= mp
->curmsg
;
1768 lasthgh
= mp
->hghmsg
;
1769 lastnum
= mp
->nummsg
;
1780 int i
, j
, k
, msgnum
, n
;
1786 if (mp
->nummsg
== 0 && mp
->nummsg
!= num
)
1787 switch (winX (SCAN
)) {
1795 _exit (0); /* NOTREACHED */
1804 j
= (k
= windows
[SCAN
]) / 2;
1805 for (msgnum
= mp
->curmsg
; msgnum
<= mp
->hghmsg
; msgnum
++)
1806 if (does_exist (mp
, msgnum
))
1810 k
= i
>= k
? 1 : k
- i
;
1817 for (msgnum
= mp
->curmsg
; msgnum
>= mp
->lowmsg
; msgnum
--)
1818 if (does_exist (mp
, msgnum
)) {
1825 for (msgnum
= mp
->curmsg
+ 1; msgnum
<= mp
->hghmsg
; msgnum
++)
1826 if (does_exist (mp
, msgnum
)) {
1830 if (n
++ >= windows
[SCAN
])
1836 && does_exist (mp
, lo
)
1837 && does_exist (mp
, hi
)
1839 || (lo
== mp
->curmsg
&& lo
== mp
->lowmsg
))
1841 || (hi
== mp
->curmsg
&& hi
== mp
->hghmsg
))
1842 && hi
- lo
== j
- i
)
1845 if (mp
->curmsg
!= cur
|| modified
)
1846 switch (winN (NULLCMD
, SCAN
, 0)) {
1854 scanrange (lo
= i
, hi
= j
);
1865 pCMD (char *str
, struct swit
*sw
, struct Cmd
*cmdp
)
1870 switch (winX (DISPLAY
)) {
1875 printf ("commands:\n");
1876 print_sw (ALL
, sw
, "", stdout
);
1877 printf ("type ``quit'' to leave %s\n", invo_name
);
1880 _exit (0); /* NOTREACHED */
1883 rc2peer (RC_EOF
, 0, NULL
);
1887 if (parse (str
, cmdp
) == NOTOK
)
1890 switch (i
= smatch (cmdp
->args
[0], sw
)) {
1892 switch (winX (DISPLAY
)) {
1897 ambigsw (cmdp
->args
[0], sw
);
1900 _exit (0); /* NOTREACHED */
1903 rc2peer (RC_EOF
, 0, NULL
);
1909 "say what: ``%s'' -- type ? (or help) for help",
1924 switch (setjmp (peerenv
)) {
1926 SIGNAL (SIGALRM
, alrmser
);
1929 status
= peerwait ();
1943 struct record rcs
, *rc
;
1948 switch (peer2rc (rc
)) {
1951 rc2peer (RC_FIN
, 0, NULL
);
1955 advise (NULL
, "%s", rc
->rc_data
);
1959 fmt2peer (RC_FIN
, "pLOOP protocol screw-up");
1966 ttyNaux (struct Cmd
*cmdp
, char *s
)
1968 struct record rcs
, *rc
;
1973 if (cmdp
&& init_io (cmdp
, vmh
) == NOTOK
)
1976 /* XXX: fseek() too tricky for our own good */
1978 fseek (fp
, 0L, SEEK_SET
);
1981 switch (rc2rc (RC_TTY
, s
? strlen (s
) : 0, s
, rc
)) {
1983 vmhtty
= OK
; /* fall */
1988 padios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
1991 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
1992 done (1); /* NOTREACHED */
1996 SIGNAL (SIGTSTP
, tstat
);
2003 ttyR (struct Cmd
*cmdp
)
2005 struct record rcs
, *rc
;
2010 SIGNAL (SIGTSTP
, SIG_IGN
);
2022 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2024 rc2peer (RC_EOF
, 0, NULL
);
2028 padios (NULL
, "%s", rc
->rc_data
);/* NOTREACHED */
2031 fmt2peer (RC_ERR
, "pTTY protocol screw-up");
2032 done (1); /* NOTREACHED */
2035 return 1; /* dead code to satisfy compiler */
2040 winN (struct Cmd
*cmdp
, int n
, int eof
)
2043 char buffer
[BUFSIZ
];
2044 struct record rcs
, *rc
;
2047 if (vmhpid
== NOTOK
)
2052 /* XXX: fseek() too tricky for our own good */
2054 fseek (fp
, 0L, SEEK_SET
);
2058 snprintf (buffer
, sizeof(buffer
), "%d", n
);
2059 switch (str2rc (RC_WIN
, buffer
, rc
)) {
2067 padios (NULL
, "%s", rc
->rc_data
);
2070 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2074 if (pipe (pd
) == NOTOK
) {
2075 err2peer (RC_ERR
, "pipe", "unable to");
2079 switch (vmhpid
= fork()) {
2081 err2peer (RC_ERR
, "fork", "unable to");
2088 SIGNAL (SIGPIPE
, SIG_IGN
);
2089 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
2090 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
2098 advise (NULL
, "%s", rc
->rc_data
);
2102 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2106 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2109 rc2peer (RC_EOF
, 0, NULL
);
2114 advise (NULL
, "%s", rc
->rc_data
);
2119 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2124 err2peer (RC_ERR
, "pipe", "error reading from");
2126 _exit (i
!= NOTOK
? i
: 1);
2129 if ((vmhfd0
= dup (fileno (stdin
))) == NOTOK
)
2130 padios ("standard input", "unable to dup");
2131 if ((vmhfd1
= dup (fileno (stdout
))) == NOTOK
)
2132 padios ("standard output", "unable to dup");
2133 if ((vmhfd2
= dup (fileno (stderr
))) == NOTOK
)
2134 padios ("diagnostic output", "unable to dup");
2137 if ((i
= open ("/dev/null", O_RDONLY
)) != NOTOK
&& i
!= fileno (stdin
)) {
2138 dup2 (i
, fileno (stdin
));
2143 if (dup2 (pd
[1], fileno (stdout
)) == NOTOK
)
2144 padios ("standard output", "unable to dup2");
2148 if (dup2 (pd
[1], fileno (stderr
)) == NOTOK
)
2149 padios ("diagnostic output", "unable to dup2");
2152 if (cmdp
&& init_io (cmdp
, 0) == NOTOK
)
2154 pstat
= SIGNAL (SIGPIPE
, pipeser
);
2166 winR (struct Cmd
*cmdp
)
2176 if (dup2 (vmhfd0
, fileno (stdin
)) == NOTOK
)
2177 padios ("standard input", "unable to dup2");
2182 if (dup2 (vmhfd1
, fileno (stdout
)) == NOTOK
)
2183 padios ("standard output", "unable to dup2");
2188 if (dup2 (vmhfd2
, fileno (stderr
)) == NOTOK
)
2189 padios ("diagnostic output", "unable to dup2");
2193 SIGNAL (SIGPIPE
, pstat
);
2195 if ((status
= pidwait (vmhpid
, OK
)) == 2)
2199 return (status
== 0 ? OK
: NOTOK
);
2207 char buffer
[BUFSIZ
];
2208 struct record rcs
, *rc
;
2213 /* XXX: fseek() too tricky for our own good */
2215 fseek (fp
, 0L, SEEK_SET
);
2217 snprintf (buffer
, sizeof(buffer
), "%d", n
);
2218 switch (str2rc (RC_WIN
, buffer
, rc
)) {
2226 padios (NULL
, "%s", rc
->rc_data
);
2229 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2233 if (pipe (pd
) == NOTOK
) {
2234 err2peer (RC_ERR
, "pipe", "unable to");
2238 switch (pid
= fork ()) {
2240 err2peer (RC_ERR
, "fork", "unable to");
2246 close (fileno (stdin
));
2247 if ((i
= open ("/dev/null", O_RDONLY
)) != NOTOK
&& i
!= fileno (stdin
)) {
2248 dup2 (i
, fileno (stdin
));
2251 dup2 (pd
[1], fileno (stdout
));
2252 dup2 (pd
[1], fileno (stderr
));
2260 while ((i
= read (pd
[0], buffer
, sizeof buffer
)) > 0)
2261 switch (rc2rc (RC_DATA
, i
, buffer
, rc
)) {
2271 padios (NULL
, "%s", rc
->rc_data
);
2274 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2278 switch (rc2rc (RC_EOF
, 0, NULL
, rc
)) {
2283 padios (NULL
, "%s", rc
->rc_data
);
2286 fmt2peer (RC_ERR
, "pWIN protocol screw-up");
2290 err2peer (RC_ERR
, "pipe", "error reading from");
2294 return (i
!= NOTOK
? pid
: NOTOK
);
2300 padios (char *what
, char *fmt
, ...)
2306 verr2peer (RC_FIN
, what
, fmt
, ap
);
2309 advertise (what
, NULL
, fmt
, ap
);
2318 padvise (char *what
, char *fmt
, ...)
2324 verr2peer (RC_ERR
, what
, fmt
, ap
);
2326 advertise (what
, NULL
, fmt
, ap
);