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 $
36 * The default toupper() macro is stupid, will toupper anything
42 #define toupper(c) (islower(c) ? ((c)-' ') : c)
46 #define INITFILE ".vt220rc"
79 /* these are defined in the getopt routine */
80 extern char *optarg; /* argument give to an option */
81 extern int optind; /* argv index after option processing */
83 int option; /* option character returned by getopt */
84 int initf = 0; /* read initialization file */
85 int lockf = 0; /* lock keys after loading strings */
86 int clearf = 0; /* clear all keys before loading strings */
88 strlcpy(prog, *argv, sizeof(prog)); /* store program name */
90 if(argc == 1) usage(); /* program requires options */
93 while ((option = getopt(argc, argv, "cli")) != -1)
110 printf("\033[62;1\"p"); /* vt200 7 bits */
112 printf("\033[62;2\"p"); /* vt200 8 bits */
114 if(clearf) clearkeys();
116 if (initf) getinit();
118 /* process {key, key string} pairs. Note optind is index to argv
119 for first pair. By adding 1 to optind insures that a pair exists
120 i.e. the last key has a key string. */
122 while(optind + 1 < argc)
124 dokey(argv[optind], argv[optind+1]);
128 if(lockf) lockkeys();
133 /****************************************************************************/
136 * Load the VT220 SHIFT-FNKEY value, the basic pattern is
137 * "\EP1;1|"+KEYNAME+"/"+VAL_AS_HEX+"\E\\"
138 * that is, literally what is in quotes (w/o quotes) then the
139 * name of the key from the keytable above (a numeric string)
140 * then a slash, then the string value as hex pairs then ESC-BACKSLASH
142 * Note: you can gang together key defns with semicolons but that
143 * would complicate things, especially error handling, so do it all
144 * for each pair, who cares, really.
147 dokey(nm,val) char *nm, *val;
150 register struct keynames *kp;
152 for(scr = nm; *scr = toupper(*scr); scr++)
154 for(kp = keys; kp->name != NULL; kp++)
155 if(strcmp(nm,kp->name) == 0) {
156 printf("%cP1;1|%s/",ESC,kp->string);
157 while(*val) printf("%02x",*val++);
162 fprintf(stderr,"Bad key name: %s\n",nm);
163 usage(); /* bad key name, give up */
166 /****************************************************************************/
170 printf("%cP0;1|%c\\",ESC,ESC);
174 /****************************************************************************/
178 printf("%cP1;0|%c\\",ESC,ESC);
182 /****************************************************************************/
188 fprintf(stderr,"Usage: %s [-cil] [keyname string keyname string...]\n\n",prog);
189 fprintf(stderr,"The following options are available\n");
190 fprintf(stderr,"\t-c\tclears keys first\n");
191 fprintf(stderr,"\t-l\t[sets then] locks further setting\n");
192 fprintf(stderr,"\t-i\tfirst read initialization file $HOME/%s\n",INITFILE);
193 fprintf(stderr,"(note that the only way to unlock is via Set-Up)\n\n");
194 fprintf(stderr,"Keyname is one of:\n\t");
195 for(i=0; keys[i].name != NULL; i++)
196 fprintf(stderr,"%s ",keys[i].name);
197 fprintf(stderr,"\nKeyname is SHIFTED function key that sends the string\n\n");
198 fprintf(stderr,"Strings may need quoting to protect from shell\n");
199 fprintf(stderr,"You must specify an option or key,string pairs\n\n");
203 /****************************************************************************/
205 /* This routine process the INITFILE. This file expects lines in the format
207 <ws> keyname ws string
209 Where ws is white space (spaces or tabs) and <ws> is optional white space.
210 The string may include spaces or tabs and need not be quoted. If the
211 string has the sequence of "\n" then a newline character is included in
221 #include <sys/types.h>
222 #include <sys/stat.h>
226 char *home; /* user's home directory */
227 char path[BUFSIZ]; /* full path name of init file */
228 char buf[BUFSIZ]; /* buffer to hold 1 line from init file */
229 char key[BUFSIZ]; /* buffer, to hold specified fcn key */
230 char keystr[BUFSIZ]; /* string associated with fcn key */
231 char *ptr; /* pointer to transverse buf */
232 int i, j; /* array indices */
233 int statflag; /* whether init file is regular & readable */
234 struct stat statbuf; /* stat of the init file */
235 FILE *fp; /* file pointer to init file */
237 /* construct full path name for init file */
238 home = getenv("HOME");
239 snprintf(path, sizeof(path), "%s/%s", home, INITFILE);
241 /* check status if init file */
242 if (stat(path, &statbuf) != -1)
244 statflag = statbuf.st_mode & S_IFREG && statbuf.st_mode & S_IREAD;
245 if (!statflag || (fp = fopen(path, "r")) == NULL)
247 fprintf(stderr, "couldn't open initalization file: %s\n", path);
251 /* process lines from init file */
252 while (fgets(buf, BUFSIZ, fp) != NULL)
254 /* variable initializations */
256 key[0] = '\0'; keystr[0] = '\0';
259 while (*ptr == ' ' || *ptr == '\t') ptr++; /*skip whitespace*/
261 if (*ptr == '\n') break; /* we hit an emtpy line */
263 while (!isspace(*ptr) && *ptr != '\0') /* get keyname */
265 key[i] = '\0'; /* place EOS in buffer */
267 while (*ptr == ' ' || *ptr == '\t') ptr++; /*skip whitespace*/
269 while (*ptr != '\n' && *ptr != '\0') /* get string */
271 /* check if string is to include newline i.e. \n */
272 if (*ptr == '\\' && *(ptr+1) == 'n')
281 keystr[j] = '\0'; /* place EOS in buffer */
282 dokey(key, keystr); /* load key with string */
287 fprintf(stderr, "init file %s not found\n\n", path);