]>
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.
10 #include <h/signals.h>
13 #include "sbr/m_mktemp.h"
20 #define PROMPTER_SWITCHES \
21 X("erase chr", 0, ERASESW) \
22 X("kill chr", 0, KILLSW) \
23 X("prepend", 0, PREPSW) \
24 X("noprepend", 0, NPREPSW) \
25 X("rapid", 0, RAPDSW) \
26 X("norapid", 0, NRAPDSW) \
27 X("body", -4, BODYSW) \
28 X("nobody", -6, NBODYSW) \
29 X("doteof", 0, DOTSW) \
30 X("nodoteof", 0, NDOTSW) \
31 X("version", 0, VERSIONSW) \
32 X("help", 0, HELPSW) \
34 #define X(sw, minchars, id) id,
35 DEFINE_SWITCH_ENUM(PROMPTER
);
38 #define X(sw, minchars, id) { sw, minchars, id },
39 DEFINE_SWITCH_ARRAY(PROMPTER
, switches
);
42 static struct termios tio
;
46 static jmp_buf sigenv
;
51 static int getln (char *, int);
52 static int chrcnv (char *);
53 static void chrdsp (char *, char);
54 static void intrser (int);
58 main (int argc
, char **argv
)
64 int fdi
, fdo
, i
, state
;
65 char *cp
, *drft
= NULL
, *erasep
= NULL
;
66 char *killp
= NULL
, name
[NAMESZ
], field
[NMH_BUFSIZ
];
68 char **arguments
, **argp
;
71 m_getfld_state_t gstate
;
73 if (nmh_init(argv
[0], true, false)) { return 1; }
75 arguments
= getarguments (invo_name
, argc
, argv
, 1);
78 while ((cp
= *argp
++))
80 switch (smatch (++cp
, switches
)) {
82 ambigsw (cp
, switches
);
85 die("-%s unknown", cp
);
88 snprintf (buffer
, sizeof(buffer
), "%s [switches] file",
90 print_help (buffer
, switches
, 1);
93 print_version(invo_name
);
97 if (!(erasep
= *argp
++) || *erasep
== '-')
98 die("missing argument to %s", argp
[-2]);
101 if (!(killp
= *argp
++) || *killp
== '-')
102 die("missing argument to %s", argp
[-2]);
139 die("usage: %s [switches] file", invo_name
);
140 if ((in
= fopen (drft
, "r")) == NULL
)
141 adios (drft
, "unable to open");
143 if ((tmpfil
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
144 die("unable to create temporary file in %s", get_temp_dir());
148 * Are we changing the kill or erase character?
150 if (killp
|| erasep
) {
151 cc_t save_erase
, save_kill
;
153 /* get the current terminal attributes */
156 /* save original kill, erase character for later */
157 save_kill
= tio
.c_cc
[VKILL
];
158 save_erase
= tio
.c_cc
[VERASE
];
160 /* set new kill, erase character in terminal structure */
161 tio
.c_cc
[VKILL
] = killp
? chrcnv (killp
) : save_kill
;
162 tio
.c_cc
[VERASE
] = erasep
? chrcnv (erasep
) : save_erase
;
164 /* set the new terminal attributes */
165 tcsetattr(0, TCSADRAIN
, &tio
);
167 /* print out new kill erase characters */
168 chrdsp ("erase", tio
.c_cc
[VERASE
]);
169 chrdsp (", kill", tio
.c_cc
[VKILL
]);
170 chrdsp (", intr", tio
.c_cc
[VINTR
]);
175 * We set the kill and erase character back to original
176 * setup in terminal structure so we can easily
177 * restore it upon exit.
179 tio
.c_cc
[VKILL
] = save_kill
;
180 tio
.c_cc
[VERASE
] = save_erase
;
184 SIGNAL2 (SIGINT
, intrser
);
187 * Loop through the lines of the draft skeleton.
189 gstate
= m_getfld_state_init(in
);
191 int fieldsz
= sizeof field
;
192 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
196 * Check if the value of field contains anything
197 * other than space or tab.
199 for (cp
= field
; *cp
; cp
++)
200 if (*cp
!= ' ' && *cp
!= '\t')
203 /* If so, just add header line to draft */
204 if (*cp
++ != '\n' || *cp
!= 0) {
205 printf ("%s:%s", name
, field
);
206 fprintf (out
, "%s:%s", name
, field
);
207 while (state
== FLDPLUS
) {
208 fieldsz
= sizeof field
;
209 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
210 fputs(field
, stdout
);
214 /* Else, get value of header field */
215 printf ("%s: ", name
);
217 i
= getln (field
, sizeof(field
));
220 if (killp
|| erasep
) {
221 tcsetattr(0, TCSADRAIN
, &tio
);
223 (void) m_unlink (tmpfil
);
226 if (i
!= 0 || (field
[0] != '\n' && field
[0] != 0)) {
227 fprintf (out
, "%s:", name
);
229 if (field
[0] != ' ' && field
[0] != '\t')
233 && (i
= getln (field
, sizeof(field
))) >= 0);
245 fprintf (out
, "--------\n");
246 if (field
[0] == 0 || !prepend
)
249 if (prepend
&& body
) {
250 puts("\n--------Enter initial text\n");
253 getln (buffer
, sizeof(buffer
));
254 if (doteof
&& buffer
[0] == '.' && buffer
[1] == '\n')
264 if (!rapid
&& !sigint
)
265 fputs(field
, stdout
);
266 } while (state
== BODY
&&
267 (fieldsz
= sizeof field
,
268 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)));
269 if (prepend
|| !body
)
271 puts("\n--------Enter additional text\n");
276 getln (field
, sizeof(field
));
277 if (doteof
&& field
[0] == '.' && field
[1] == '\n')
286 die("skeleton is poorly formatted");
290 m_getfld_state_destroy (&gstate
);
298 SIGNAL (SIGINT
, SIG_IGN
);
300 if (killp
|| erasep
) {
301 tcsetattr(0, TCSADRAIN
, &tio
);
304 if ((fdi
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
305 adios (tmpfil
, "unable to re-open");
306 if ((fdo
= creat (drft
, m_gmprot ())) == NOTOK
)
307 adios (drft
, "unable to write");
308 cpydata (fdi
, fdo
, tmpfil
, drft
);
311 (void) m_unlink (tmpfil
);
313 context_save (); /* save the context file */
320 getln (char *buffer
, int n
)
328 switch (setjmp (sigenv
)) {
346 switch (c
= getchar ()) {
350 longjmp (sigenv
, DONE
);
384 longjmp (sigenv
, NOTOK
);
392 return *cp
!= QUOTE
? *cp
: m_atoi(++cp
);
397 chrdsp (char *s
, char c
)
400 if (c
< ' ' || c
== 0177)
401 printf ("^%c", c
^ 0100);