]>
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/m_gmprot.h"
10 #include "sbr/m_getfld.h"
11 #include "sbr/getarguments.h"
12 #include "sbr/smatch.h"
13 #include "sbr/cpydata.h"
14 #include "sbr/m_atoi.h"
15 #include "sbr/context_save.h"
16 #include "sbr/ambigsw.h"
17 #include "sbr/print_version.h"
18 #include "sbr/print_help.h"
19 #include "sbr/error.h"
21 #include "h/signals.h"
24 #include "sbr/m_mktemp.h"
31 #define PROMPTER_SWITCHES \
32 X("erase chr", 0, ERASESW) \
33 X("kill chr", 0, KILLSW) \
34 X("prepend", 0, PREPSW) \
35 X("noprepend", 0, NPREPSW) \
36 X("rapid", 0, RAPDSW) \
37 X("norapid", 0, NRAPDSW) \
38 X("body", -4, BODYSW) \
39 X("nobody", -6, NBODYSW) \
40 X("doteof", 0, DOTSW) \
41 X("nodoteof", 0, NDOTSW) \
42 X("version", 0, VERSIONSW) \
43 X("help", 0, HELPSW) \
45 #define X(sw, minchars, id) id,
46 DEFINE_SWITCH_ENUM(PROMPTER
);
49 #define X(sw, minchars, id) { sw, minchars, id },
50 DEFINE_SWITCH_ARRAY(PROMPTER
, switches
);
53 static struct termios tio
;
57 static jmp_buf sigenv
;
62 static int getln (char *, int);
63 static int chrcnv (char *);
64 static void chrdsp (char *, char);
65 static void intrser (int);
69 main (int argc
, char **argv
)
75 int fdi
, fdo
, i
, state
;
76 char *cp
, *drft
= NULL
, *erasep
= NULL
;
77 char *killp
= NULL
, name
[NAMESZ
], field
[NMH_BUFSIZ
];
79 char **arguments
, **argp
;
82 m_getfld_state_t gstate
;
84 if (nmh_init(argv
[0], true, false)) { return 1; }
86 arguments
= getarguments (invo_name
, argc
, argv
, 1);
89 while ((cp
= *argp
++))
91 switch (smatch (++cp
, switches
)) {
93 ambigsw (cp
, switches
);
96 die("-%s unknown", cp
);
99 snprintf (buffer
, sizeof(buffer
), "%s [switches] file",
101 print_help (buffer
, switches
, 1);
104 print_version(invo_name
);
108 if (!(erasep
= *argp
++) || *erasep
== '-')
109 die("missing argument to %s", argp
[-2]);
112 if (!(killp
= *argp
++) || *killp
== '-')
113 die("missing argument to %s", argp
[-2]);
150 die("usage: %s [switches] file", invo_name
);
151 if ((in
= fopen (drft
, "r")) == NULL
)
152 adios (drft
, "unable to open");
154 if ((tmpfil
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
155 die("unable to create temporary file in %s", get_temp_dir());
159 * Are we changing the kill or erase character?
161 if (killp
|| erasep
) {
162 cc_t save_erase
, save_kill
;
164 /* get the current terminal attributes */
167 /* save original kill, erase character for later */
168 save_kill
= tio
.c_cc
[VKILL
];
169 save_erase
= tio
.c_cc
[VERASE
];
171 /* set new kill, erase character in terminal structure */
172 tio
.c_cc
[VKILL
] = killp
? chrcnv (killp
) : save_kill
;
173 tio
.c_cc
[VERASE
] = erasep
? chrcnv (erasep
) : save_erase
;
175 /* set the new terminal attributes */
176 tcsetattr(0, TCSADRAIN
, &tio
);
178 /* print out new kill erase characters */
179 chrdsp ("erase", tio
.c_cc
[VERASE
]);
180 chrdsp (", kill", tio
.c_cc
[VKILL
]);
181 chrdsp (", intr", tio
.c_cc
[VINTR
]);
186 * We set the kill and erase character back to original
187 * setup in terminal structure so we can easily
188 * restore it upon exit.
190 tio
.c_cc
[VKILL
] = save_kill
;
191 tio
.c_cc
[VERASE
] = save_erase
;
195 SIGNAL2 (SIGINT
, intrser
);
198 * Loop through the lines of the draft skeleton.
200 gstate
= m_getfld_state_init(in
);
202 int fieldsz
= sizeof field
;
203 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
207 * Check if the value of field contains anything
208 * other than space or tab.
210 for (cp
= field
; *cp
; cp
++)
211 if (*cp
!= ' ' && *cp
!= '\t')
214 /* If so, just add header line to draft */
215 if (*cp
++ != '\n' || *cp
!= 0) {
216 printf ("%s:%s", name
, field
);
217 fprintf (out
, "%s:%s", name
, field
);
218 while (state
== FLDPLUS
) {
219 fieldsz
= sizeof field
;
220 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
221 fputs(field
, stdout
);
225 /* Else, get value of header field */
226 printf ("%s: ", name
);
228 i
= getln (field
, sizeof(field
));
231 if (killp
|| erasep
) {
232 tcsetattr(0, TCSADRAIN
, &tio
);
234 (void) m_unlink (tmpfil
);
237 if (i
!= 0 || (field
[0] != '\n' && field
[0] != 0)) {
238 fprintf (out
, "%s:", name
);
240 if (field
[0] != ' ' && field
[0] != '\t')
244 && (i
= getln (field
, sizeof(field
))) >= 0);
256 fprintf (out
, "--------\n");
257 if (field
[0] == 0 || !prepend
)
260 if (prepend
&& body
) {
261 puts("\n--------Enter initial text\n");
264 getln (buffer
, sizeof(buffer
));
265 if (doteof
&& buffer
[0] == '.' && buffer
[1] == '\n')
275 if (!rapid
&& !sigint
)
276 fputs(field
, stdout
);
277 } while (state
== BODY
&&
278 (fieldsz
= sizeof field
,
279 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)));
280 if (prepend
|| !body
)
282 puts("\n--------Enter additional text\n");
287 getln (field
, sizeof(field
));
288 if (doteof
&& field
[0] == '.' && field
[1] == '\n')
297 die("skeleton is poorly formatted");
301 m_getfld_state_destroy (&gstate
);
309 SIGNAL (SIGINT
, SIG_IGN
);
311 if (killp
|| erasep
) {
312 tcsetattr(0, TCSADRAIN
, &tio
);
315 if ((fdi
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
316 adios (tmpfil
, "unable to re-open");
317 if ((fdo
= creat (drft
, m_gmprot ())) == NOTOK
)
318 adios (drft
, "unable to write");
319 cpydata (fdi
, fdo
, tmpfil
, drft
);
322 (void) m_unlink (tmpfil
);
324 context_save (); /* save the context file */
331 getln (char *buffer
, int n
)
339 switch (setjmp (sigenv
)) {
357 switch (c
= getchar ()) {
361 longjmp (sigenv
, DONE
);
395 longjmp (sigenv
, NOTOK
);
403 return *cp
!= QUOTE
? *cp
: m_atoi(++cp
);
408 chrdsp (char *s
, char c
)
411 if (c
< ' ' || c
== 0177)
412 printf ("^%c", c
^ 0100);