2 * Trivial program to load VT220 Function keys with strings,
3 * note that the values only get sent when the key is shifted
4 * (shoulda been an option to flip the shift set like the Z19!)
6 * Typing no args gives help, basically pairs of keyname/value
9 * Author, Author: Barry Shein, Boston University
12 {1} 30-Oct-85 Kenneth J. Lester (ken) at ektools
14 Added the necessary code to read an initialization file. This
15 should make it easier to used this program. Also added code
16 that will set-up the terminal in vt200 (this saves the user the
17 trouble of checking if the set-up is in vt200).
19 Restructed the main function to use getopt, for argument
22 Alterated usage function to include new "i" option (init file)
25 -hm minor modifications for pcvt 2.0 release
27 $FreeBSD: src/usr.sbin/pcvt/userkeys/vt220keys.c,v 1.5.6.1 2001/05/12 10:11:42 kris Exp $
28 $DragonFly: src/usr.sbin/pcvt/userkeys/Attic/vt220keys.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
37 * The default toupper() macro is stupid, will toupper anything
43 #define toupper(c) (islower(c) ? ((c)-' ') : c)
47 #define INITFILE ".vt220rc"
80 /* these are defined in the getopt routine */
81 extern char *optarg; /* argument give to an option */
82 extern int optind; /* argv index after option processing */
84 int option; /* option character returned by getopt */
85 int initf = 0; /* read initialization file */
86 int lockf = 0; /* lock keys after loading strings */
87 int clearf = 0; /* clear all keys before loading strings */
89 strlcpy(prog, *argv, sizeof(prog)); /* store program name */
91 if(argc == 1) usage(); /* program requires options */
94 while ((option = getopt(argc, argv, "cli")) != -1)
111 printf("\033[62;1\"p"); /* vt200 7 bits */
113 printf("\033[62;2\"p"); /* vt200 8 bits */
115 if(clearf) clearkeys();
117 if (initf) getinit();
119 /* process {key, key string} pairs. Note optind is index to argv
120 for first pair. By adding 1 to optind insures that a pair exists
121 i.e. the last key has a key string. */
123 while(optind + 1 < argc)
125 dokey(argv[optind], argv[optind+1]);
129 if(lockf) lockkeys();
134 /****************************************************************************/
137 * Load the VT220 SHIFT-FNKEY value, the basic pattern is
138 * "\EP1;1|"+KEYNAME+"/"+VAL_AS_HEX+"\E\\"
139 * that is, literally what is in quotes (w/o quotes) then the
140 * name of the key from the keytable above (a numeric string)
141 * then a slash, then the string value as hex pairs then ESC-BACKSLASH
143 * Note: you can gang together key defns with semicolons but that
144 * would complicate things, especially error handling, so do it all
145 * for each pair, who cares, really.
148 dokey(nm,val) char *nm, *val;
151 register struct keynames *kp;
153 for(scr = nm; *scr = toupper(*scr); scr++)
155 for(kp = keys; kp->name != NULL; kp++)
156 if(strcmp(nm,kp->name) == 0) {
157 printf("%cP1;1|%s/",ESC,kp->string);
158 while(*val) printf("%02x",*val++);
163 fprintf(stderr,"Bad key name: %s\n",nm);
164 usage(); /* bad key name, give up */
167 /****************************************************************************/
171 printf("%cP0;1|%c\\",ESC,ESC);
175 /****************************************************************************/
179 printf("%cP1;0|%c\\",ESC,ESC);
183 /****************************************************************************/
189 fprintf(stderr,"Usage: %s [-cil] [keyname string keyname string...]\n\n",prog);
190 fprintf(stderr,"The following options are available\n");
191 fprintf(stderr,"\t-c\tclears keys first\n");
192 fprintf(stderr,"\t-l\t[sets then] locks further setting\n");
193 fprintf(stderr,"\t-i\tfirst read initialization file $HOME/%s\n",INITFILE);
194 fprintf(stderr,"(note that the only way to unlock is via Set-Up)\n\n");
195 fprintf(stderr,"Keyname is one of:\n\t");
196 for(i=0; keys[i].name != NULL; i++)
197 fprintf(stderr,"%s ",keys[i].name);
198 fprintf(stderr,"\nKeyname is SHIFTED function key that sends the string\n\n");
199 fprintf(stderr,"Strings may need quoting to protect from shell\n");
200 fprintf(stderr,"You must specify an option or key,string pairs\n\n");
204 /****************************************************************************/
206 /* This routine process the INITFILE. This file expects lines in the format
208 <ws> keyname ws string
210 Where ws is white space (spaces or tabs) and <ws> is optional white space.
211 The string may include spaces or tabs and need not be quoted. If the
212 string has the sequence of "\n" then a newline character is included in
222 #include <sys/types.h>
223 #include <sys/stat.h>
227 char *home; /* user's home directory */
228 char path[BUFSIZ]; /* full path name of init file */
229 char buf[BUFSIZ]; /* buffer to hold 1 line from init file */
230 char key[BUFSIZ]; /* buffer, to hold specified fcn key */
231 char keystr[BUFSIZ]; /* string associated with fcn key */
232 char *ptr; /* pointer to transverse buf */
233 int i, j; /* array indices */
234 int statflag; /* whether init file is regular & readable */
235 struct stat statbuf; /* stat of the init file */
236 FILE *fp; /* file pointer to init file */
238 /* construct full path name for init file */
239 home = getenv("HOME");
240 snprintf(path, sizeof(path), "%s/%s", home, INITFILE);
242 /* check status if init file */
243 if (stat(path, &statbuf) != -1)
245 statflag = statbuf.st_mode & S_IFREG && statbuf.st_mode & S_IREAD;
246 if (!statflag || (fp = fopen(path, "r")) == NULL)
248 fprintf(stderr, "couldn't open initalization file: %s\n", path);
252 /* process lines from init file */
253 while (fgets(buf, BUFSIZ, fp) != NULL)
255 /* variable initializations */
257 key[0] = '\0'; keystr[0] = '\0';
260 while (*ptr == ' ' || *ptr == '\t') ptr++; /*skip whitespace*/
262 if (*ptr == '\n') break; /* we hit an emtpy line */
264 while (!isspace(*ptr) && *ptr != '\0') /* get keyname */
266 key[i] = '\0'; /* place EOS in buffer */
268 while (*ptr == ' ' || *ptr == '\t') ptr++; /*skip whitespace*/
270 while (*ptr != '\n' && *ptr != '\0') /* get string */
272 /* check if string is to include newline i.e. \n */
273 if (*ptr == '\\' && *(ptr+1) == 'n')
282 keystr[j] = '\0'; /* place EOS in buffer */
283 dokey(key, keystr); /* load key with string */
288 fprintf(stderr, "init file %s not found\n\n", path);