]>
diplodocus.org Git - nmh/blob - uip/prompter.c
03b3dc094ccef0f88e67b6ec8f11f5664f457970
2 * prompter.c -- simple prompting editor front-end
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
11 #include <h/signals.h>
26 #define PROMPTER_SWITCHES \
27 X("erase chr", 0, ERASESW) \
28 X("kill chr", 0, KILLSW) \
29 X("prepend", 0, PREPSW) \
30 X("noprepend", 0, NPREPSW) \
31 X("rapid", 0, RAPDSW) \
32 X("norapid", 0, NRAPDSW) \
33 X("body", -4, BODYSW) \
34 X("nobody", -6, NBODYSW) \
35 X("doteof", 0, DOTSW) \
36 X("nodoteof", 0, NDOTSW) \
37 X("version", 0, VERSIONSW) \
38 X("help", 0, HELPSW) \
40 #define X(sw, minchars, id) id,
41 DEFINE_SWITCH_ENUM(PROMPTER
);
44 #define X(sw, minchars, id) { sw, minchars, id },
45 DEFINE_SWITCH_ARRAY(PROMPTER
, switches
);
49 static struct termios tio
;
50 #define ERASE tio.c_cc[VERASE]
51 #define KILL tio.c_cc[VKILL]
52 #define INTR tio.c_cc[VINTR]
54 static int wtuser
= 0;
55 static int sigint
= 0;
56 static jmp_buf sigenv
;
61 int getln (char *, int);
62 static int chrcnv (char *);
63 static void chrdsp (char *, char);
64 static void intrser (int);
68 main (int argc
, char **argv
)
70 int body
= 1, prepend
= 1, rapid
= 0;
71 int doteof
= 0, fdi
, fdo
, i
, state
;
72 char *cp
, *drft
= NULL
, *erasep
= NULL
;
73 char *killp
= NULL
, name
[NAMESZ
], field
[BUFSIZ
];
75 char **arguments
, **argp
;
78 m_getfld_state_t gstate
= 0;
80 if (nmh_init(argv
[0], 2)) { return 1; }
82 arguments
= getarguments (invo_name
, argc
, argv
, 1);
85 while ((cp
= *argp
++))
87 switch (smatch (++cp
, switches
)) {
89 ambigsw (cp
, switches
);
92 adios (NULL
, "-%s unknown", cp
);
95 snprintf (buffer
, sizeof(buffer
), "%s [switches] file",
97 print_help (buffer
, switches
, 1);
100 print_version(invo_name
);
104 if (!(erasep
= *argp
++) || *erasep
== '-')
105 adios (NULL
, "missing argument to %s", argp
[-2]);
108 if (!(killp
= *argp
++) || *killp
== '-')
109 adios (NULL
, "missing argument to %s", argp
[-2]);
146 adios (NULL
, "usage: %s [switches] file", invo_name
);
147 if ((in
= fopen (drft
, "r")) == NULL
)
148 adios (drft
, "unable to open");
150 if ((tmpfil
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
151 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
155 * Are we changing the kill or erase character?
157 if (killp
|| erasep
) {
158 cc_t save_erase
, save_kill
;
160 /* get the current terminal attributes */
163 /* save original kill, erase character for later */
167 /* set new kill, erase character in terminal structure */
168 KILL
= killp
? chrcnv (killp
) : save_kill
;
169 ERASE
= erasep
? chrcnv (erasep
) : save_erase
;
171 /* set the new terminal attributes */
172 tcsetattr(0, TCSADRAIN
, &tio
);
174 /* print out new kill erase characters */
175 chrdsp ("erase", ERASE
);
176 chrdsp (", kill", KILL
);
177 chrdsp (", intr", INTR
);
182 * We set the kill and erase character back to original
183 * setup in terminal structure so we can easily
184 * restore it upon exit.
191 SIGNAL2 (SIGINT
, intrser
);
194 * Loop through the lines of the draft skeleton.
197 int fieldsz
= sizeof field
;
198 switch (state
= m_getfld (&gstate
, name
, field
, &fieldsz
, in
)) {
202 * Check if the value of field contains anything
203 * other than space or tab.
205 for (cp
= field
; *cp
; cp
++)
206 if (*cp
!= ' ' && *cp
!= '\t')
209 /* If so, just add header line to draft */
210 if (*cp
++ != '\n' || *cp
!= 0) {
211 printf ("%s:%s", name
, field
);
212 fprintf (out
, "%s:%s", name
, field
);
213 while (state
== FLDPLUS
) {
214 fieldsz
= sizeof field
;
215 state
= m_getfld (&gstate
, name
, field
, &fieldsz
, in
);
216 fputs(field
, stdout
);
220 /* Else, get value of header field */
221 printf ("%s: ", name
);
223 i
= getln (field
, sizeof(field
));
226 if (killp
|| erasep
) {
227 tcsetattr(0, TCSADRAIN
, &tio
);
229 (void) m_unlink (tmpfil
);
232 if (i
!= 0 || (field
[0] != '\n' && field
[0] != 0)) {
233 fprintf (out
, "%s:", name
);
235 if (field
[0] != ' ' && field
[0] != '\t')
239 && (i
= getln (field
, sizeof(field
))) >= 0);
251 fprintf (out
, "--------\n");
252 if (field
[0] == 0 || !prepend
)
255 if (prepend
&& body
) {
256 printf ("\n--------Enter initial text\n\n");
259 getln (buffer
, sizeof(buffer
));
260 if (doteof
&& buffer
[0] == '.' && buffer
[1] == '\n')
270 if (!rapid
&& !sigint
)
271 fputs(field
, stdout
);
272 } while (state
== BODY
&&
273 (fieldsz
= sizeof field
,
274 state
= m_getfld (&gstate
, name
, field
, &fieldsz
, in
)));
275 if (prepend
|| !body
)
278 printf ("\n--------Enter additional text\n\n");
283 getln (field
, sizeof(field
));
284 if (doteof
&& field
[0] == '.' && field
[1] == '\n')
293 adios (NULL
, "skeleton is poorly formatted");
297 m_getfld_state_destroy (&gstate
);
305 SIGNAL (SIGINT
, SIG_IGN
);
307 if (killp
|| erasep
) {
308 tcsetattr(0, TCSADRAIN
, &tio
);
311 if ((fdi
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
312 adios (tmpfil
, "unable to re-open");
313 if ((fdo
= creat (drft
, m_gmprot ())) == NOTOK
)
314 adios (drft
, "unable to write");
315 cpydata (fdi
, fdo
, tmpfil
, drft
);
318 (void) m_unlink (tmpfil
);
320 context_save (); /* save the context file */
327 getln (char *buffer
, int n
)
331 static int quoting
= 0;
335 switch (setjmp (sigenv
)) {
353 switch (c
= getchar ()) {
357 longjmp (sigenv
, DONE
);
391 longjmp (sigenv
, NOTOK
);
399 return (*cp
!= QUOTE
? *cp
: m_atoi (++cp
));
404 chrdsp (char *s
, char c
)
407 if (c
< ' ' || c
== 0177)
408 printf ("^%c", c
^ 0100);