]>
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
;
44 static int wtuser
= 0;
45 static int sigint
= 0;
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
)
60 int body
= 1, prepend
= 1, rapid
= 0;
61 int doteof
= 0, fdi
, fdo
, i
, state
;
62 char *cp
, *drft
= NULL
, *erasep
= NULL
;
63 char *killp
= NULL
, name
[NAMESZ
], field
[NMH_BUFSIZ
];
65 char **arguments
, **argp
;
68 m_getfld_state_t gstate
;
70 if (nmh_init(argv
[0], 2)) { return 1; }
72 arguments
= getarguments (invo_name
, argc
, argv
, 1);
75 while ((cp
= *argp
++))
77 switch (smatch (++cp
, switches
)) {
79 ambigsw (cp
, switches
);
82 adios (NULL
, "-%s unknown", cp
);
85 snprintf (buffer
, sizeof(buffer
), "%s [switches] file",
87 print_help (buffer
, switches
, 1);
90 print_version(invo_name
);
94 if (!(erasep
= *argp
++) || *erasep
== '-')
95 adios (NULL
, "missing argument to %s", argp
[-2]);
98 if (!(killp
= *argp
++) || *killp
== '-')
99 adios (NULL
, "missing argument to %s", argp
[-2]);
136 adios (NULL
, "usage: %s [switches] file", invo_name
);
137 if ((in
= fopen (drft
, "r")) == NULL
)
138 adios (drft
, "unable to open");
140 if ((tmpfil
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
141 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
145 * Are we changing the kill or erase character?
147 if (killp
|| erasep
) {
148 cc_t save_erase
, save_kill
;
150 /* get the current terminal attributes */
153 /* save original kill, erase character for later */
154 save_kill
= tio
.c_cc
[VKILL
];
155 save_erase
= tio
.c_cc
[VERASE
];
157 /* set new kill, erase character in terminal structure */
158 tio
.c_cc
[VKILL
] = killp
? chrcnv (killp
) : save_kill
;
159 tio
.c_cc
[VERASE
] = erasep
? chrcnv (erasep
) : save_erase
;
161 /* set the new terminal attributes */
162 tcsetattr(0, TCSADRAIN
, &tio
);
164 /* print out new kill erase characters */
165 chrdsp ("erase", tio
.c_cc
[VERASE
]);
166 chrdsp (", kill", tio
.c_cc
[VKILL
]);
167 chrdsp (", intr", tio
.c_cc
[VINTR
]);
172 * We set the kill and erase character back to original
173 * setup in terminal structure so we can easily
174 * restore it upon exit.
176 tio
.c_cc
[VKILL
] = save_kill
;
177 tio
.c_cc
[VERASE
] = save_erase
;
181 SIGNAL2 (SIGINT
, intrser
);
184 * Loop through the lines of the draft skeleton.
186 gstate
= m_getfld_state_init(in
);
188 int fieldsz
= sizeof field
;
189 switch (state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)) {
193 * Check if the value of field contains anything
194 * other than space or tab.
196 for (cp
= field
; *cp
; cp
++)
197 if (*cp
!= ' ' && *cp
!= '\t')
200 /* If so, just add header line to draft */
201 if (*cp
++ != '\n' || *cp
!= 0) {
202 printf ("%s:%s", name
, field
);
203 fprintf (out
, "%s:%s", name
, field
);
204 while (state
== FLDPLUS
) {
205 fieldsz
= sizeof field
;
206 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
);
207 fputs(field
, stdout
);
211 /* Else, get value of header field */
212 printf ("%s: ", name
);
214 i
= getln (field
, sizeof(field
));
217 if (killp
|| erasep
) {
218 tcsetattr(0, TCSADRAIN
, &tio
);
220 (void) m_unlink (tmpfil
);
223 if (i
!= 0 || (field
[0] != '\n' && field
[0] != 0)) {
224 fprintf (out
, "%s:", name
);
226 if (field
[0] != ' ' && field
[0] != '\t')
230 && (i
= getln (field
, sizeof(field
))) >= 0);
242 fprintf (out
, "--------\n");
243 if (field
[0] == 0 || !prepend
)
246 if (prepend
&& body
) {
247 puts("\n--------Enter initial text\n");
250 getln (buffer
, sizeof(buffer
));
251 if (doteof
&& buffer
[0] == '.' && buffer
[1] == '\n')
261 if (!rapid
&& !sigint
)
262 fputs(field
, stdout
);
263 } while (state
== BODY
&&
264 (fieldsz
= sizeof field
,
265 state
= m_getfld2(&gstate
, name
, field
, &fieldsz
)));
266 if (prepend
|| !body
)
268 puts("\n--------Enter additional text\n");
273 getln (field
, sizeof(field
));
274 if (doteof
&& field
[0] == '.' && field
[1] == '\n')
283 adios (NULL
, "skeleton is poorly formatted");
287 m_getfld_state_destroy (&gstate
);
295 SIGNAL (SIGINT
, SIG_IGN
);
297 if (killp
|| erasep
) {
298 tcsetattr(0, TCSADRAIN
, &tio
);
301 if ((fdi
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
302 adios (tmpfil
, "unable to re-open");
303 if ((fdo
= creat (drft
, m_gmprot ())) == NOTOK
)
304 adios (drft
, "unable to write");
305 cpydata (fdi
, fdo
, tmpfil
, drft
);
308 (void) m_unlink (tmpfil
);
310 context_save (); /* save the context file */
317 getln (char *buffer
, int n
)
321 static int quoting
= 0;
325 switch (setjmp (sigenv
)) {
343 switch (c
= getchar ()) {
347 longjmp (sigenv
, DONE
);
381 longjmp (sigenv
, NOTOK
);
389 return *cp
!= QUOTE
? *cp
: m_atoi(++cp
);
394 chrdsp (char *s
, char c
)
397 if (c
< ' ' || c
== 0177)
398 printf ("^%c", c
^ 0100);