]>
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>
29 static struct swit switches
[] = {
58 static struct termios tio
;
59 #define ERASE tio.c_cc[VERASE]
60 #define KILL tio.c_cc[VKILL]
61 #define INTR tio.c_cc[VINTR]
63 static int wtuser
= 0;
64 static int sigint
= 0;
65 static jmp_buf sigenv
;
70 int getln (char *, int);
71 static int chrcnv (char *);
72 static void chrdsp (char *, char);
73 static void intrser (int);
77 main (int argc
, char **argv
)
79 int body
= 1, prepend
= 1, rapid
= 0;
80 int doteof
= 0, fdi
, fdo
, i
, state
;
81 char *cp
, *drft
= NULL
, *erasep
= NULL
;
82 char *killp
= NULL
, name
[NAMESZ
], field
[BUFSIZ
];
83 char buffer
[BUFSIZ
], tmpfil
[BUFSIZ
];
84 char **arguments
, **argp
;
87 m_getfld_state_t gstate
;
90 setlocale(LC_ALL
, "");
92 invo_name
= r1bindex (argv
[0], '/');
94 /* read user profile/context */
97 arguments
= getarguments (invo_name
, argc
, argv
, 1);
100 while ((cp
= *argp
++))
102 switch (smatch (++cp
, switches
)) {
104 ambigsw (cp
, switches
);
107 adios (NULL
, "-%s unknown", cp
);
110 snprintf (buffer
, sizeof(buffer
), "%s [switches] file",
112 print_help (buffer
, switches
, 1);
115 print_version(invo_name
);
119 if (!(erasep
= *argp
++) || *erasep
== '-')
120 adios (NULL
, "missing argument to %s", argp
[-2]);
123 if (!(killp
= *argp
++) || *killp
== '-')
124 adios (NULL
, "missing argument to %s", argp
[-2]);
161 adios (NULL
, "usage: %s [switches] file", invo_name
);
162 if ((in
= fopen (drft
, "r")) == NULL
)
163 adios (drft
, "unable to open");
165 tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
166 if (tfile
== NULL
) adios("prompter", "unable to create temporary file");
167 chmod (tmpfil
, 0600);
168 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
171 * Are we changing the kill or erase character?
173 if (killp
|| erasep
) {
174 cc_t save_erase
, save_kill
;
176 /* get the current terminal attributes */
179 /* save original kill, erase character for later */
183 /* set new kill, erase character in terminal structure */
184 KILL
= killp
? chrcnv (killp
) : save_kill
;
185 ERASE
= erasep
? chrcnv (erasep
) : save_erase
;
187 /* set the new terminal attributes */
188 tcsetattr(0, TCSADRAIN
, &tio
);
190 /* print out new kill erase characters */
191 chrdsp ("erase", ERASE
);
192 chrdsp (", kill", KILL
);
193 chrdsp (", intr", INTR
);
198 * We set the kill and erase character back to original
199 * setup in terminal structure so we can easily
200 * restore it upon exit.
207 SIGNAL2 (SIGINT
, intrser
);
210 * Loop through the lines of the draft skeleton.
212 m_getfld_state_init (&gstate
);
214 int fieldsz
= sizeof field
;
215 switch (state
= m_getfld (gstate
, name
, field
, &fieldsz
, in
)) {
219 * Check if the value of field contains anything
220 * other than space or tab.
222 for (cp
= field
; *cp
; cp
++)
223 if (*cp
!= ' ' && *cp
!= '\t')
226 /* If so, just add header line to draft */
227 if (*cp
++ != '\n' || *cp
!= 0) {
228 printf ("%s:%s", name
, field
);
229 fprintf (out
, "%s:%s", name
, field
);
230 while (state
== FLDPLUS
) {
231 fieldsz
= sizeof field
;
232 state
= m_getfld (gstate
, name
, field
, &fieldsz
, in
);
233 printf ("%s", field
);
234 fprintf (out
, "%s", field
);
237 /* Else, get value of header field */
238 printf ("%s: ", name
);
240 i
= getln (field
, sizeof(field
));
243 if (killp
|| erasep
) {
244 tcsetattr(0, TCSADRAIN
, &tio
);
249 if (i
!= 0 || (field
[0] != '\n' && field
[0] != 0)) {
250 fprintf (out
, "%s:", name
);
252 if (field
[0] != ' ' && field
[0] != '\t')
254 fprintf (out
, "%s", field
);
256 && (i
= getln (field
, sizeof(field
))) >= 0);
268 fprintf (out
, "--------\n");
269 if (field
[0] == 0 || !prepend
)
270 printf ("--------\n");
272 if (prepend
&& body
) {
273 printf ("\n--------Enter initial text\n\n");
276 getln (buffer
, sizeof(buffer
));
277 if (doteof
&& buffer
[0] == '.' && buffer
[1] == '\n')
281 fprintf (out
, "%s", buffer
);
286 fprintf (out
, "%s", field
);
287 if (!rapid
&& !sigint
)
288 printf ("%s", field
);
289 } while (state
== BODY
&&
290 (fieldsz
= sizeof field
,
291 state
= m_getfld (gstate
, name
, field
, &fieldsz
, in
)));
292 if (prepend
|| !body
)
295 printf ("\n--------Enter additional text\n\n");
300 getln (field
, sizeof(field
));
301 if (doteof
&& field
[0] == '.' && field
[1] == '\n')
305 fprintf (out
, "%s", field
);
310 adios (NULL
, "skeleton is poorly formatted");
314 m_getfld_state_destroy (&gstate
);
317 printf ("--------\n");
322 SIGNAL (SIGINT
, SIG_IGN
);
324 if (killp
|| erasep
) {
325 tcsetattr(0, TCSADRAIN
, &tio
);
328 if ((fdi
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
329 adios (tmpfil
, "unable to re-open");
330 if ((fdo
= creat (drft
, m_gmprot ())) == NOTOK
)
331 adios (drft
, "unable to write");
332 cpydata (fdi
, fdo
, tmpfil
, drft
);
337 context_save (); /* save the context file */
344 getln (char *buffer
, int n
)
352 switch (setjmp (sigenv
)) {
367 switch (c
= getchar ()) {
370 longjmp (sigenv
, DONE
);
373 if (cp
[-1] == QUOTE
) {
398 longjmp (sigenv
, NOTOK
);
406 return (*cp
!= QUOTE
? *cp
: m_atoi (++cp
));
411 chrdsp (char *s
, char c
)
414 if (c
< ' ' || c
== 0177)
415 printf ("^%c", c
^ 0100);