]>
diplodocus.org Git - nmh/blob - uip/prompter.c
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
];
75 char buffer
[BUFSIZ
], tmpfil
[BUFSIZ
];
76 char **arguments
, **argp
;
79 m_getfld_state_t gstate
= 0;
82 setlocale(LC_ALL
, "");
84 invo_name
= r1bindex (argv
[0], '/');
86 /* read user profile/context */
89 arguments
= getarguments (invo_name
, argc
, argv
, 1);
92 while ((cp
= *argp
++))
94 switch (smatch (++cp
, switches
)) {
96 ambigsw (cp
, switches
);
99 adios (NULL
, "-%s unknown", cp
);
102 snprintf (buffer
, sizeof(buffer
), "%s [switches] file",
104 print_help (buffer
, switches
, 1);
107 print_version(invo_name
);
111 if (!(erasep
= *argp
++) || *erasep
== '-')
112 adios (NULL
, "missing argument to %s", argp
[-2]);
115 if (!(killp
= *argp
++) || *killp
== '-')
116 adios (NULL
, "missing argument to %s", argp
[-2]);
153 adios (NULL
, "usage: %s [switches] file", invo_name
);
154 if ((in
= fopen (drft
, "r")) == NULL
)
155 adios (drft
, "unable to open");
157 tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
158 if (tfile
== NULL
) adios("prompter", "unable to create temporary file");
159 chmod (tmpfil
, 0600);
160 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
163 * Are we changing the kill or erase character?
165 if (killp
|| erasep
) {
166 cc_t save_erase
, save_kill
;
168 /* get the current terminal attributes */
171 /* save original kill, erase character for later */
175 /* set new kill, erase character in terminal structure */
176 KILL
= killp
? chrcnv (killp
) : save_kill
;
177 ERASE
= erasep
? chrcnv (erasep
) : save_erase
;
179 /* set the new terminal attributes */
180 tcsetattr(0, TCSADRAIN
, &tio
);
182 /* print out new kill erase characters */
183 chrdsp ("erase", ERASE
);
184 chrdsp (", kill", KILL
);
185 chrdsp (", intr", INTR
);
190 * We set the kill and erase character back to original
191 * setup in terminal structure so we can easily
192 * restore it upon exit.
199 SIGNAL2 (SIGINT
, intrser
);
202 * Loop through the lines of the draft skeleton.
205 int fieldsz
= sizeof field
;
206 switch (state
= m_getfld (&gstate
, name
, field
, &fieldsz
, in
)) {
210 * Check if the value of field contains anything
211 * other than space or tab.
213 for (cp
= field
; *cp
; cp
++)
214 if (*cp
!= ' ' && *cp
!= '\t')
217 /* If so, just add header line to draft */
218 if (*cp
++ != '\n' || *cp
!= 0) {
219 printf ("%s:%s", name
, field
);
220 fprintf (out
, "%s:%s", name
, field
);
221 while (state
== FLDPLUS
) {
222 fieldsz
= sizeof field
;
223 state
= m_getfld (&gstate
, name
, field
, &fieldsz
, in
);
224 printf ("%s", field
);
225 fprintf (out
, "%s", field
);
228 /* Else, get value of header field */
229 printf ("%s: ", name
);
231 i
= getln (field
, sizeof(field
));
234 if (killp
|| erasep
) {
235 tcsetattr(0, TCSADRAIN
, &tio
);
240 if (i
!= 0 || (field
[0] != '\n' && field
[0] != 0)) {
241 fprintf (out
, "%s:", name
);
243 if (field
[0] != ' ' && field
[0] != '\t')
245 fprintf (out
, "%s", field
);
247 && (i
= getln (field
, sizeof(field
))) >= 0);
259 fprintf (out
, "--------\n");
260 if (field
[0] == 0 || !prepend
)
261 printf ("--------\n");
263 if (prepend
&& body
) {
264 printf ("\n--------Enter initial text\n\n");
267 getln (buffer
, sizeof(buffer
));
268 if (doteof
&& buffer
[0] == '.' && buffer
[1] == '\n')
272 fprintf (out
, "%s", buffer
);
277 fprintf (out
, "%s", field
);
278 if (!rapid
&& !sigint
)
279 printf ("%s", field
);
280 } while (state
== BODY
&&
281 (fieldsz
= sizeof field
,
282 state
= m_getfld (&gstate
, name
, field
, &fieldsz
, in
)));
283 if (prepend
|| !body
)
286 printf ("\n--------Enter additional text\n\n");
291 getln (field
, sizeof(field
));
292 if (doteof
&& field
[0] == '.' && field
[1] == '\n')
296 fprintf (out
, "%s", field
);
301 adios (NULL
, "skeleton is poorly formatted");
305 m_getfld_state_destroy (&gstate
);
308 printf ("--------\n");
313 SIGNAL (SIGINT
, SIG_IGN
);
315 if (killp
|| erasep
) {
316 tcsetattr(0, TCSADRAIN
, &tio
);
319 if ((fdi
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
320 adios (tmpfil
, "unable to re-open");
321 if ((fdo
= creat (drft
, m_gmprot ())) == NOTOK
)
322 adios (drft
, "unable to write");
323 cpydata (fdi
, fdo
, tmpfil
, drft
);
328 context_save (); /* save the context file */
335 getln (char *buffer
, int n
)
343 switch (setjmp (sigenv
)) {
358 switch (c
= getchar ()) {
361 longjmp (sigenv
, DONE
);
364 if (cp
[-1] == QUOTE
) {
389 longjmp (sigenv
, NOTOK
);
397 return (*cp
!= QUOTE
? *cp
: m_atoi (++cp
));
402 chrdsp (char *s
, char c
)
405 if (c
< ' ' || c
== 0177)
406 printf ("^%c", c
^ 0100);