]>
diplodocus.org Git - nmh/blob - uip/prompter.c
7923e84b316508ccb9fc60fdc43f2212b0c3abfe
3 * prompter.c -- simple prompting editor front-end
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
27 #define PROMPTER_SWITCHES \
28 X("erase chr", 0, ERASESW) \
29 X("kill chr", 0, KILLSW) \
30 X("prepend", 0, PREPSW) \
31 X("noprepend", 0, NPREPSW) \
32 X("rapid", 0, RAPDSW) \
33 X("norapid", 0, NRAPDSW) \
34 X("body", -4, BODYSW) \
35 X("nobody", -6, NBODYSW) \
36 X("doteof", 0, DOTSW) \
37 X("nodoteof", 0, NDOTSW) \
38 X("version", 0, VERSIONSW) \
39 X("help", 0, HELPSW) \
41 #define X(sw, minchars, id) id,
42 DEFINE_SWITCH_ENUM(PROMPTER
);
45 #define X(sw, minchars, id) { sw, minchars, id },
46 DEFINE_SWITCH_ARRAY(PROMPTER
, switches
);
50 static struct termios tio
;
51 #define ERASE tio.c_cc[VERASE]
52 #define KILL tio.c_cc[VKILL]
53 #define INTR tio.c_cc[VINTR]
55 static int wtuser
= 0;
56 static int sigint
= 0;
57 static jmp_buf sigenv
;
62 int getln (char *, int);
63 static int chrcnv (char *);
64 static void chrdsp (char *, char);
65 static void intrser (int);
69 main (int argc
, char **argv
)
71 int body
= 1, prepend
= 1, rapid
= 0;
72 int doteof
= 0, fdi
, fdo
, i
, state
;
73 char *cp
, *drft
= NULL
, *erasep
= NULL
;
74 char *killp
= NULL
, name
[NAMESZ
], field
[BUFSIZ
];
76 char **arguments
, **argp
;
79 m_getfld_state_t gstate
= 0;
81 setlocale(LC_ALL
, "");
82 invo_name
= r1bindex (argv
[0], '/');
84 /* read user profile/context */
87 arguments
= getarguments (invo_name
, argc
, argv
, 1);
90 while ((cp
= *argp
++))
92 switch (smatch (++cp
, switches
)) {
94 ambigsw (cp
, switches
);
97 adios (NULL
, "-%s unknown", cp
);
100 snprintf (buffer
, sizeof(buffer
), "%s [switches] file",
102 print_help (buffer
, switches
, 1);
105 print_version(invo_name
);
109 if (!(erasep
= *argp
++) || *erasep
== '-')
110 adios (NULL
, "missing argument to %s", argp
[-2]);
113 if (!(killp
= *argp
++) || *killp
== '-')
114 adios (NULL
, "missing argument to %s", argp
[-2]);
151 adios (NULL
, "usage: %s [switches] file", invo_name
);
152 if ((in
= fopen (drft
, "r")) == NULL
)
153 adios (drft
, "unable to open");
155 tmpfil
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
156 if (tmpfil
== NULL
) adios("prompter", "unable to create temporary file");
157 chmod (tmpfil
, 0600);
160 * Are we changing the kill or erase character?
162 if (killp
|| erasep
) {
163 cc_t save_erase
, save_kill
;
165 /* get the current terminal attributes */
168 /* save original kill, erase character for later */
172 /* set new kill, erase character in terminal structure */
173 KILL
= killp
? chrcnv (killp
) : save_kill
;
174 ERASE
= erasep
? chrcnv (erasep
) : save_erase
;
176 /* set the new terminal attributes */
177 tcsetattr(0, TCSADRAIN
, &tio
);
179 /* print out new kill erase characters */
180 chrdsp ("erase", ERASE
);
181 chrdsp (", kill", KILL
);
182 chrdsp (", intr", INTR
);
187 * We set the kill and erase character back to original
188 * setup in terminal structure so we can easily
189 * restore it upon exit.
196 SIGNAL2 (SIGINT
, intrser
);
199 * Loop through the lines of the draft skeleton.
202 int fieldsz
= sizeof field
;
203 switch (state
= m_getfld (&gstate
, name
, field
, &fieldsz
, in
)) {
207 * Check if the value of field contains anything
208 * other than space or tab.
210 for (cp
= field
; *cp
; cp
++)
211 if (*cp
!= ' ' && *cp
!= '\t')
214 /* If so, just add header line to draft */
215 if (*cp
++ != '\n' || *cp
!= 0) {
216 printf ("%s:%s", name
, field
);
217 fprintf (out
, "%s:%s", name
, field
);
218 while (state
== FLDPLUS
) {
219 fieldsz
= sizeof field
;
220 state
= m_getfld (&gstate
, name
, field
, &fieldsz
, in
);
221 printf ("%s", field
);
222 fprintf (out
, "%s", field
);
225 /* Else, get value of header field */
226 printf ("%s: ", name
);
228 i
= getln (field
, sizeof(field
));
231 if (killp
|| erasep
) {
232 tcsetattr(0, TCSADRAIN
, &tio
);
237 if (i
!= 0 || (field
[0] != '\n' && field
[0] != 0)) {
238 fprintf (out
, "%s:", name
);
240 if (field
[0] != ' ' && field
[0] != '\t')
242 fprintf (out
, "%s", field
);
244 && (i
= getln (field
, sizeof(field
))) >= 0);
256 fprintf (out
, "--------\n");
257 if (field
[0] == 0 || !prepend
)
258 printf ("--------\n");
260 if (prepend
&& body
) {
261 printf ("\n--------Enter initial text\n\n");
264 getln (buffer
, sizeof(buffer
));
265 if (doteof
&& buffer
[0] == '.' && buffer
[1] == '\n')
269 fprintf (out
, "%s", buffer
);
274 fprintf (out
, "%s", field
);
275 if (!rapid
&& !sigint
)
276 printf ("%s", field
);
277 } while (state
== BODY
&&
278 (fieldsz
= sizeof field
,
279 state
= m_getfld (&gstate
, name
, field
, &fieldsz
, in
)));
280 if (prepend
|| !body
)
283 printf ("\n--------Enter additional text\n\n");
288 getln (field
, sizeof(field
));
289 if (doteof
&& field
[0] == '.' && field
[1] == '\n')
293 fprintf (out
, "%s", field
);
298 adios (NULL
, "skeleton is poorly formatted");
302 m_getfld_state_destroy (&gstate
);
305 printf ("--------\n");
310 SIGNAL (SIGINT
, SIG_IGN
);
312 if (killp
|| erasep
) {
313 tcsetattr(0, TCSADRAIN
, &tio
);
316 if ((fdi
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
317 adios (tmpfil
, "unable to re-open");
318 if ((fdo
= creat (drft
, m_gmprot ())) == NOTOK
)
319 adios (drft
, "unable to write");
320 cpydata (fdi
, fdo
, tmpfil
, drft
);
325 context_save (); /* save the context file */
332 getln (char *buffer
, int n
)
340 switch (setjmp (sigenv
)) {
355 switch (c
= getchar ()) {
358 longjmp (sigenv
, DONE
);
361 if (cp
[-1] == QUOTE
) {
386 longjmp (sigenv
, NOTOK
);
394 return (*cp
!= QUOTE
? *cp
: m_atoi (++cp
));
399 chrdsp (char *s
, char c
)
402 if (c
< ' ' || c
== 0177)
403 printf ("^%c", c
^ 0100);