]>
diplodocus.org Git - nmh/blob - uip/prompter.c
1 /* prompter.c -- simple prompting editor front-end
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
9 #include "sbr/context_save.h"
10 #include "sbr/ambigsw.h"
11 #include "sbr/print_version.h"
12 #include "sbr/print_help.h"
13 #include "sbr/error.h"
15 #include "h/signals.h"
18 #include "sbr/m_mktemp.h"
25 #define PROMPTER_SWITCHES \
26 X("erase chr", 0, ERASESW) \
27 X("kill chr", 0, KILLSW) \
28 X("prepend", 0, PREPSW) \
29 X("noprepend", 0, NPREPSW) \
30 X("rapid", 0, RAPDSW) \
31 X("norapid", 0, NRAPDSW) \
32 X("body", -4, BODYSW) \
33 X("nobody", -6, NBODYSW) \
34 X("doteof", 0, DOTSW) \
35 X("nodoteof", 0, NDOTSW) \
36 X("version", 0, VERSIONSW) \
37 X("help", 0, HELPSW) \
39 #define X(sw, minchars, id) id,
40 DEFINE_SWITCH_ENUM(PROMPTER
);
43 #define X(sw, minchars, id) { sw, minchars, id },
44 DEFINE_SWITCH_ARRAY(PROMPTER
, switches
);
47 static struct termios tio
;
51 static jmp_buf sigenv
;
56 static int getln (char *, int);
57 static int chrcnv (char *);
58 static void chrdsp (char *, char);
59 static void intrser (int);
63 main (int argc
, char **argv
)
69 int fdi
, fdo
, i
, state
;
70 char *cp
, *drft
= NULL
, *erasep
= NULL
;
71 char *killp
= NULL
, name
[NAMESZ
], field
[NMH_BUFSIZ
];
73 char **arguments
, **argp
;
76 m_getfld_state_t gstate
;
78 if (nmh_init(argv
[0], true, false)) { return 1; }
80 arguments
= getarguments (invo_name
, argc
, argv
, 1);
83 while ((cp
= *argp
++))
85 switch (smatch (++cp
, switches
)) {
87 ambigsw (cp
, switches
);
90 die("-%s unknown", cp
);
93 snprintf (buffer
, sizeof(buffer
), "%s [switches] file",
95 print_help (buffer
, switches
, 1);
98 print_version(invo_name
);
102 if (!(erasep
= *argp
++) || *erasep
== '-')
103 die("missing argument to %s", argp
[-2]);
106 if (!(killp
= *argp
++) || *killp
== '-')
107 die("missing argument to %s", argp
[-2]);
144 die("usage: %s [switches] file", invo_name
);
145 if ((in
= fopen (drft
, "r")) == NULL
)
146 adios (drft
, "unable to open");
148 if ((tmpfil
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
149 die("unable to create temporary file in %s", get_temp_dir());
153 * Are we changing the kill or erase character?
155 if (killp
|| erasep
) {
156 cc_t save_erase
, save_kill
;
158 /* get the current terminal attributes */
161 /* save original kill, erase character for later */
162 save_kill
= tio
.c_cc
[VKILL
];
163 save_erase
= tio
.c_cc
[VERASE
];
165 /* set new kill, erase character in terminal structure */
166 tio
.c_cc
[VKILL
] = killp
? chrcnv (killp
) : save_kill
;
167 tio
.c_cc
[VERASE
] = erasep
? chrcnv (erasep
) : save_erase
;
169 /* set the new terminal attributes */
170 tcsetattr(0, TCSADRAIN
, &tio
);
172 /* print out new kill erase characters */
173 chrdsp ("erase", tio
.c_cc
[VERASE
]);
174 chrdsp (", kill", tio
.c_cc
[VKILL
]);
175 chrdsp (", intr", tio
.c_cc
[VINTR
]);
180 * We set the kill and erase character back to original
181 * setup in terminal structure so we can easily
182 * restore it upon exit.
184 tio
.c_cc
[VKILL
] = save_kill
;
185 tio
.c_cc
[VERASE
] = save_erase
;
189 SIGNAL2 (SIGINT
, intrser
);
192 * Loop through the lines of the draft skeleton.
194 gstate
= m_getfld_state_init(in
);
196 int fieldsz
= sizeof field
;
197 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
201 * Check if the value of field contains anything
202 * other than space or tab.
204 for (cp
= field
; *cp
; cp
++)
205 if (*cp
!= ' ' && *cp
!= '\t')
208 /* If so, just add header line to draft */
209 if (*cp
++ != '\n' || *cp
!= 0) {
210 printf ("%s:%s", name
, field
);
211 fprintf (out
, "%s:%s", name
, field
);
212 while (state
== FLDPLUS
) {
213 fieldsz
= sizeof field
;
214 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
215 fputs(field
, stdout
);
219 /* Else, get value of header field */
220 printf ("%s: ", name
);
222 i
= getln (field
, sizeof(field
));
225 if (killp
|| erasep
) {
226 tcsetattr(0, TCSADRAIN
, &tio
);
228 (void) m_unlink (tmpfil
);
231 if (i
!= 0 || (field
[0] != '\n' && field
[0] != 0)) {
232 fprintf (out
, "%s:", name
);
234 if (field
[0] != ' ' && field
[0] != '\t')
238 && (i
= getln (field
, sizeof(field
))) >= 0);
250 fprintf (out
, "--------\n");
251 if (field
[0] == 0 || !prepend
)
254 if (prepend
&& body
) {
255 puts("\n--------Enter initial text\n");
258 getln (buffer
, sizeof(buffer
));
259 if (doteof
&& buffer
[0] == '.' && buffer
[1] == '\n')
269 if (!rapid
&& !sigint
)
270 fputs(field
, stdout
);
271 } while (state
== BODY
&&
272 (fieldsz
= sizeof field
,
273 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)));
274 if (prepend
|| !body
)
276 puts("\n--------Enter additional text\n");
281 getln (field
, sizeof(field
));
282 if (doteof
&& field
[0] == '.' && field
[1] == '\n')
291 die("skeleton is poorly formatted");
295 m_getfld_state_destroy (&gstate
);
303 SIGNAL (SIGINT
, SIG_IGN
);
305 if (killp
|| erasep
) {
306 tcsetattr(0, TCSADRAIN
, &tio
);
309 if ((fdi
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
310 adios (tmpfil
, "unable to re-open");
311 if ((fdo
= creat (drft
, m_gmprot ())) == NOTOK
)
312 adios (drft
, "unable to write");
313 cpydata (fdi
, fdo
, tmpfil
, drft
);
316 (void) m_unlink (tmpfil
);
318 context_save (); /* save the context file */
325 getln (char *buffer
, int n
)
333 switch (setjmp (sigenv
)) {
351 switch (c
= getchar ()) {
355 longjmp (sigenv
, DONE
);
389 longjmp (sigenv
, NOTOK
);
397 return *cp
!= QUOTE
? *cp
: m_atoi(++cp
);
402 chrdsp (char *s
, char c
)
405 if (c
< ' ' || c
== 0177)
406 printf ("^%c", c
^ 0100);