]>
diplodocus.org Git - nmh/blob - uip/prompter.c
d0e885adc01ecc433f7539b4deea9a19e6902f19
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/ambigsw.h"
10 #include "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/error.h"
14 #include "h/signals.h"
17 #include "sbr/m_mktemp.h"
24 #define PROMPTER_SWITCHES \
25 X("erase chr", 0, ERASESW) \
26 X("kill chr", 0, KILLSW) \
27 X("prepend", 0, PREPSW) \
28 X("noprepend", 0, NPREPSW) \
29 X("rapid", 0, RAPDSW) \
30 X("norapid", 0, NRAPDSW) \
31 X("body", -4, BODYSW) \
32 X("nobody", -6, NBODYSW) \
33 X("doteof", 0, DOTSW) \
34 X("nodoteof", 0, NDOTSW) \
35 X("version", 0, VERSIONSW) \
36 X("help", 0, HELPSW) \
38 #define X(sw, minchars, id) id,
39 DEFINE_SWITCH_ENUM(PROMPTER
);
42 #define X(sw, minchars, id) { sw, minchars, id },
43 DEFINE_SWITCH_ARRAY(PROMPTER
, switches
);
46 static struct termios tio
;
50 static jmp_buf sigenv
;
55 static int getln (char *, int);
56 static int chrcnv (char *);
57 static void chrdsp (char *, char);
58 static void intrser (int);
62 main (int argc
, char **argv
)
68 int fdi
, fdo
, i
, state
;
69 char *cp
, *drft
= NULL
, *erasep
= NULL
;
70 char *killp
= NULL
, name
[NAMESZ
], field
[NMH_BUFSIZ
];
72 char **arguments
, **argp
;
75 m_getfld_state_t gstate
;
77 if (nmh_init(argv
[0], true, false)) { return 1; }
79 arguments
= getarguments (invo_name
, argc
, argv
, 1);
82 while ((cp
= *argp
++))
84 switch (smatch (++cp
, switches
)) {
86 ambigsw (cp
, switches
);
89 die("-%s unknown", cp
);
92 snprintf (buffer
, sizeof(buffer
), "%s [switches] file",
94 print_help (buffer
, switches
, 1);
97 print_version(invo_name
);
101 if (!(erasep
= *argp
++) || *erasep
== '-')
102 die("missing argument to %s", argp
[-2]);
105 if (!(killp
= *argp
++) || *killp
== '-')
106 die("missing argument to %s", argp
[-2]);
143 die("usage: %s [switches] file", invo_name
);
144 if ((in
= fopen (drft
, "r")) == NULL
)
145 adios (drft
, "unable to open");
147 if ((tmpfil
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
148 die("unable to create temporary file in %s", get_temp_dir());
152 * Are we changing the kill or erase character?
154 if (killp
|| erasep
) {
155 cc_t save_erase
, save_kill
;
157 /* get the current terminal attributes */
160 /* save original kill, erase character for later */
161 save_kill
= tio
.c_cc
[VKILL
];
162 save_erase
= tio
.c_cc
[VERASE
];
164 /* set new kill, erase character in terminal structure */
165 tio
.c_cc
[VKILL
] = killp
? chrcnv (killp
) : save_kill
;
166 tio
.c_cc
[VERASE
] = erasep
? chrcnv (erasep
) : save_erase
;
168 /* set the new terminal attributes */
169 tcsetattr(0, TCSADRAIN
, &tio
);
171 /* print out new kill erase characters */
172 chrdsp ("erase", tio
.c_cc
[VERASE
]);
173 chrdsp (", kill", tio
.c_cc
[VKILL
]);
174 chrdsp (", intr", tio
.c_cc
[VINTR
]);
179 * We set the kill and erase character back to original
180 * setup in terminal structure so we can easily
181 * restore it upon exit.
183 tio
.c_cc
[VKILL
] = save_kill
;
184 tio
.c_cc
[VERASE
] = save_erase
;
188 SIGNAL2 (SIGINT
, intrser
);
191 * Loop through the lines of the draft skeleton.
193 gstate
= m_getfld_state_init(in
);
195 int fieldsz
= sizeof field
;
196 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
200 * Check if the value of field contains anything
201 * other than space or tab.
203 for (cp
= field
; *cp
; cp
++)
204 if (*cp
!= ' ' && *cp
!= '\t')
207 /* If so, just add header line to draft */
208 if (*cp
++ != '\n' || *cp
!= 0) {
209 printf ("%s:%s", name
, field
);
210 fprintf (out
, "%s:%s", name
, field
);
211 while (state
== FLDPLUS
) {
212 fieldsz
= sizeof field
;
213 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
214 fputs(field
, stdout
);
218 /* Else, get value of header field */
219 printf ("%s: ", name
);
221 i
= getln (field
, sizeof(field
));
224 if (killp
|| erasep
) {
225 tcsetattr(0, TCSADRAIN
, &tio
);
227 (void) m_unlink (tmpfil
);
230 if (i
!= 0 || (field
[0] != '\n' && field
[0] != 0)) {
231 fprintf (out
, "%s:", name
);
233 if (field
[0] != ' ' && field
[0] != '\t')
237 && (i
= getln (field
, sizeof(field
))) >= 0);
249 fprintf (out
, "--------\n");
250 if (field
[0] == 0 || !prepend
)
253 if (prepend
&& body
) {
254 puts("\n--------Enter initial text\n");
257 getln (buffer
, sizeof(buffer
));
258 if (doteof
&& buffer
[0] == '.' && buffer
[1] == '\n')
268 if (!rapid
&& !sigint
)
269 fputs(field
, stdout
);
270 } while (state
== BODY
&&
271 (fieldsz
= sizeof field
,
272 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)));
273 if (prepend
|| !body
)
275 puts("\n--------Enter additional text\n");
280 getln (field
, sizeof(field
));
281 if (doteof
&& field
[0] == '.' && field
[1] == '\n')
290 die("skeleton is poorly formatted");
294 m_getfld_state_destroy (&gstate
);
302 SIGNAL (SIGINT
, SIG_IGN
);
304 if (killp
|| erasep
) {
305 tcsetattr(0, TCSADRAIN
, &tio
);
308 if ((fdi
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
309 adios (tmpfil
, "unable to re-open");
310 if ((fdo
= creat (drft
, m_gmprot ())) == NOTOK
)
311 adios (drft
, "unable to write");
312 cpydata (fdi
, fdo
, tmpfil
, drft
);
315 (void) m_unlink (tmpfil
);
317 context_save (); /* save the context file */
324 getln (char *buffer
, int n
)
332 switch (setjmp (sigenv
)) {
350 switch (c
= getchar ()) {
354 longjmp (sigenv
, DONE
);
388 longjmp (sigenv
, NOTOK
);
396 return *cp
!= QUOTE
? *cp
: m_atoi(++cp
);
401 chrdsp (char *s
, char c
)
404 if (c
< ' ' || c
== 0177)
405 printf ("^%c", c
^ 0100);