]>
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>
12 #include "sbr/m_mktemp.h"
19 #define PROMPTER_SWITCHES \
20 X("erase chr", 0, ERASESW) \
21 X("kill chr", 0, KILLSW) \
22 X("prepend", 0, PREPSW) \
23 X("noprepend", 0, NPREPSW) \
24 X("rapid", 0, RAPDSW) \
25 X("norapid", 0, NRAPDSW) \
26 X("body", -4, BODYSW) \
27 X("nobody", -6, NBODYSW) \
28 X("doteof", 0, DOTSW) \
29 X("nodoteof", 0, NDOTSW) \
30 X("version", 0, VERSIONSW) \
31 X("help", 0, HELPSW) \
33 #define X(sw, minchars, id) id,
34 DEFINE_SWITCH_ENUM(PROMPTER
);
37 #define X(sw, minchars, id) { sw, minchars, id },
38 DEFINE_SWITCH_ARRAY(PROMPTER
, switches
);
41 static struct termios tio
;
43 static int wtuser
= 0;
44 static int sigint
= 0;
45 static jmp_buf sigenv
;
50 static int getln (char *, int);
51 static int chrcnv (char *);
52 static void chrdsp (char *, char);
53 static void intrser (int);
57 main (int argc
, char **argv
)
59 int body
= 1, prepend
= 1, rapid
= 0;
60 int doteof
= 0, fdi
, fdo
, i
, state
;
61 char *cp
, *drft
= NULL
, *erasep
= NULL
;
62 char *killp
= NULL
, name
[NAMESZ
], field
[NMH_BUFSIZ
];
64 char **arguments
, **argp
;
67 m_getfld_state_t gstate
;
69 if (nmh_init(argv
[0], 2)) { return 1; }
71 arguments
= getarguments (invo_name
, argc
, argv
, 1);
74 while ((cp
= *argp
++))
76 switch (smatch (++cp
, switches
)) {
78 ambigsw (cp
, switches
);
81 adios (NULL
, "-%s unknown", cp
);
84 snprintf (buffer
, sizeof(buffer
), "%s [switches] file",
86 print_help (buffer
, switches
, 1);
89 print_version(invo_name
);
93 if (!(erasep
= *argp
++) || *erasep
== '-')
94 adios (NULL
, "missing argument to %s", argp
[-2]);
97 if (!(killp
= *argp
++) || *killp
== '-')
98 adios (NULL
, "missing argument to %s", argp
[-2]);
135 adios (NULL
, "usage: %s [switches] file", invo_name
);
136 if ((in
= fopen (drft
, "r")) == NULL
)
137 adios (drft
, "unable to open");
139 if ((tmpfil
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
140 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
144 * Are we changing the kill or erase character?
146 if (killp
|| erasep
) {
147 cc_t save_erase
, save_kill
;
149 /* get the current terminal attributes */
152 /* save original kill, erase character for later */
153 save_kill
= tio
.c_cc
[VKILL
];
154 save_erase
= tio
.c_cc
[VERASE
];
156 /* set new kill, erase character in terminal structure */
157 tio
.c_cc
[VKILL
] = killp
? chrcnv (killp
) : save_kill
;
158 tio
.c_cc
[VERASE
] = erasep
? chrcnv (erasep
) : save_erase
;
160 /* set the new terminal attributes */
161 tcsetattr(0, TCSADRAIN
, &tio
);
163 /* print out new kill erase characters */
164 chrdsp ("erase", tio
.c_cc
[VERASE
]);
165 chrdsp (", kill", tio
.c_cc
[VKILL
]);
166 chrdsp (", intr", tio
.c_cc
[VINTR
]);
171 * We set the kill and erase character back to original
172 * setup in terminal structure so we can easily
173 * restore it upon exit.
175 tio
.c_cc
[VKILL
] = save_kill
;
176 tio
.c_cc
[VERASE
] = save_erase
;
180 SIGNAL2 (SIGINT
, intrser
);
183 * Loop through the lines of the draft skeleton.
185 gstate
= m_getfld_state_init(in
);
187 int fieldsz
= sizeof field
;
188 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
192 * Check if the value of field contains anything
193 * other than space or tab.
195 for (cp
= field
; *cp
; cp
++)
196 if (*cp
!= ' ' && *cp
!= '\t')
199 /* If so, just add header line to draft */
200 if (*cp
++ != '\n' || *cp
!= 0) {
201 printf ("%s:%s", name
, field
);
202 fprintf (out
, "%s:%s", name
, field
);
203 while (state
== FLDPLUS
) {
204 fieldsz
= sizeof field
;
205 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
206 fputs(field
, stdout
);
210 /* Else, get value of header field */
211 printf ("%s: ", name
);
213 i
= getln (field
, sizeof(field
));
216 if (killp
|| erasep
) {
217 tcsetattr(0, TCSADRAIN
, &tio
);
219 (void) m_unlink (tmpfil
);
222 if (i
!= 0 || (field
[0] != '\n' && field
[0] != 0)) {
223 fprintf (out
, "%s:", name
);
225 if (field
[0] != ' ' && field
[0] != '\t')
229 && (i
= getln (field
, sizeof(field
))) >= 0);
241 fprintf (out
, "--------\n");
242 if (field
[0] == 0 || !prepend
)
245 if (prepend
&& body
) {
246 puts("\n--------Enter initial text\n");
249 getln (buffer
, sizeof(buffer
));
250 if (doteof
&& buffer
[0] == '.' && buffer
[1] == '\n')
260 if (!rapid
&& !sigint
)
261 fputs(field
, stdout
);
262 } while (state
== BODY
&&
263 (fieldsz
= sizeof field
,
264 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)));
265 if (prepend
|| !body
)
267 puts("\n--------Enter additional text\n");
272 getln (field
, sizeof(field
));
273 if (doteof
&& field
[0] == '.' && field
[1] == '\n')
282 adios (NULL
, "skeleton is poorly formatted");
286 m_getfld_state_destroy (&gstate
);
294 SIGNAL (SIGINT
, SIG_IGN
);
296 if (killp
|| erasep
) {
297 tcsetattr(0, TCSADRAIN
, &tio
);
300 if ((fdi
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
301 adios (tmpfil
, "unable to re-open");
302 if ((fdo
= creat (drft
, m_gmprot ())) == NOTOK
)
303 adios (drft
, "unable to write");
304 cpydata (fdi
, fdo
, tmpfil
, drft
);
307 (void) m_unlink (tmpfil
);
309 context_save (); /* save the context file */
316 getln (char *buffer
, int n
)
320 static int quoting
= 0;
324 switch (setjmp (sigenv
)) {
342 switch (c
= getchar ()) {
346 longjmp (sigenv
, DONE
);
380 longjmp (sigenv
, NOTOK
);
388 return (*cp
!= QUOTE
? *cp
: m_atoi (++cp
));
393 chrdsp (char *s
, char c
)
396 if (c
< ' ' || c
== 0177)
397 printf ("^%c", c
^ 0100);