]>
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/smatch.h"
10 #include "sbr/cpydata.h"
11 #include "sbr/m_atoi.h"
12 #include "sbr/context_save.h"
13 #include "sbr/ambigsw.h"
14 #include "sbr/print_version.h"
15 #include "sbr/print_help.h"
16 #include "sbr/error.h"
18 #include "h/signals.h"
21 #include "sbr/m_mktemp.h"
28 #define PROMPTER_SWITCHES \
29 X("erase chr", 0, ERASESW) \
30 X("kill chr", 0, KILLSW) \
31 X("prepend", 0, PREPSW) \
32 X("noprepend", 0, NPREPSW) \
33 X("rapid", 0, RAPDSW) \
34 X("norapid", 0, NRAPDSW) \
35 X("body", -4, BODYSW) \
36 X("nobody", -6, NBODYSW) \
37 X("doteof", 0, DOTSW) \
38 X("nodoteof", 0, NDOTSW) \
39 X("version", 0, VERSIONSW) \
40 X("help", 0, HELPSW) \
42 #define X(sw, minchars, id) id,
43 DEFINE_SWITCH_ENUM(PROMPTER
);
46 #define X(sw, minchars, id) { sw, minchars, id },
47 DEFINE_SWITCH_ARRAY(PROMPTER
, switches
);
50 static struct termios tio
;
54 static jmp_buf sigenv
;
59 static int getln (char *, int);
60 static int chrcnv (char *);
61 static void chrdsp (char *, char);
62 static void intrser (int);
66 main (int argc
, char **argv
)
72 int fdi
, fdo
, i
, state
;
73 char *cp
, *drft
= NULL
, *erasep
= NULL
;
74 char *killp
= NULL
, name
[NAMESZ
], field
[NMH_BUFSIZ
];
76 char **arguments
, **argp
;
79 m_getfld_state_t gstate
;
81 if (nmh_init(argv
[0], true, false)) { return 1; }
83 arguments
= getarguments (invo_name
, argc
, argv
, 1);
86 while ((cp
= *argp
++))
88 switch (smatch (++cp
, switches
)) {
90 ambigsw (cp
, switches
);
93 die("-%s unknown", cp
);
96 snprintf (buffer
, sizeof(buffer
), "%s [switches] file",
98 print_help (buffer
, switches
, 1);
101 print_version(invo_name
);
105 if (!(erasep
= *argp
++) || *erasep
== '-')
106 die("missing argument to %s", argp
[-2]);
109 if (!(killp
= *argp
++) || *killp
== '-')
110 die("missing argument to %s", argp
[-2]);
147 die("usage: %s [switches] file", invo_name
);
148 if ((in
= fopen (drft
, "r")) == NULL
)
149 adios (drft
, "unable to open");
151 if ((tmpfil
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
152 die("unable to create temporary file in %s", get_temp_dir());
156 * Are we changing the kill or erase character?
158 if (killp
|| erasep
) {
159 cc_t save_erase
, save_kill
;
161 /* get the current terminal attributes */
164 /* save original kill, erase character for later */
165 save_kill
= tio
.c_cc
[VKILL
];
166 save_erase
= tio
.c_cc
[VERASE
];
168 /* set new kill, erase character in terminal structure */
169 tio
.c_cc
[VKILL
] = killp
? chrcnv (killp
) : save_kill
;
170 tio
.c_cc
[VERASE
] = erasep
? chrcnv (erasep
) : save_erase
;
172 /* set the new terminal attributes */
173 tcsetattr(0, TCSADRAIN
, &tio
);
175 /* print out new kill erase characters */
176 chrdsp ("erase", tio
.c_cc
[VERASE
]);
177 chrdsp (", kill", tio
.c_cc
[VKILL
]);
178 chrdsp (", intr", tio
.c_cc
[VINTR
]);
183 * We set the kill and erase character back to original
184 * setup in terminal structure so we can easily
185 * restore it upon exit.
187 tio
.c_cc
[VKILL
] = save_kill
;
188 tio
.c_cc
[VERASE
] = save_erase
;
192 SIGNAL2 (SIGINT
, intrser
);
195 * Loop through the lines of the draft skeleton.
197 gstate
= m_getfld_state_init(in
);
199 int fieldsz
= sizeof field
;
200 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
204 * Check if the value of field contains anything
205 * other than space or tab.
207 for (cp
= field
; *cp
; cp
++)
208 if (*cp
!= ' ' && *cp
!= '\t')
211 /* If so, just add header line to draft */
212 if (*cp
++ != '\n' || *cp
!= 0) {
213 printf ("%s:%s", name
, field
);
214 fprintf (out
, "%s:%s", name
, field
);
215 while (state
== FLDPLUS
) {
216 fieldsz
= sizeof field
;
217 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
218 fputs(field
, stdout
);
222 /* Else, get value of header field */
223 printf ("%s: ", name
);
225 i
= getln (field
, sizeof(field
));
228 if (killp
|| erasep
) {
229 tcsetattr(0, TCSADRAIN
, &tio
);
231 (void) m_unlink (tmpfil
);
234 if (i
!= 0 || (field
[0] != '\n' && field
[0] != 0)) {
235 fprintf (out
, "%s:", name
);
237 if (field
[0] != ' ' && field
[0] != '\t')
241 && (i
= getln (field
, sizeof(field
))) >= 0);
253 fprintf (out
, "--------\n");
254 if (field
[0] == 0 || !prepend
)
257 if (prepend
&& body
) {
258 puts("\n--------Enter initial text\n");
261 getln (buffer
, sizeof(buffer
));
262 if (doteof
&& buffer
[0] == '.' && buffer
[1] == '\n')
272 if (!rapid
&& !sigint
)
273 fputs(field
, stdout
);
274 } while (state
== BODY
&&
275 (fieldsz
= sizeof field
,
276 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)));
277 if (prepend
|| !body
)
279 puts("\n--------Enter additional text\n");
284 getln (field
, sizeof(field
));
285 if (doteof
&& field
[0] == '.' && field
[1] == '\n')
294 die("skeleton is poorly formatted");
298 m_getfld_state_destroy (&gstate
);
306 SIGNAL (SIGINT
, SIG_IGN
);
308 if (killp
|| erasep
) {
309 tcsetattr(0, TCSADRAIN
, &tio
);
312 if ((fdi
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
313 adios (tmpfil
, "unable to re-open");
314 if ((fdo
= creat (drft
, m_gmprot ())) == NOTOK
)
315 adios (drft
, "unable to write");
316 cpydata (fdi
, fdo
, tmpfil
, drft
);
319 (void) m_unlink (tmpfil
);
321 context_save (); /* save the context file */
328 getln (char *buffer
, int n
)
336 switch (setjmp (sigenv
)) {
354 switch (c
= getchar ()) {
358 longjmp (sigenv
, DONE
);
392 longjmp (sigenv
, NOTOK
);
400 return *cp
!= QUOTE
? *cp
: m_atoi(++cp
);
405 chrdsp (char *s
, char c
)
408 if (c
< ' ' || c
== 0177)
409 printf ("^%c", c
^ 0100);