1 /* xtermset: change some settings of an xterm from the command line */
2 
3 /*
4 ** Copyright (C) 2000 Breyten J. Ernsting <bje@dds.nl>
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 **
20 */
21 
22 /* this was orginally 'setxtitle' in the splitvt package,
23  * created by Sam Lantinga(slouken@cs.ucdavis.edu)
24  * rewritten by Breyten Ernsting (bje@dds.nl)
25  * and again by Decklin Foster (decklin@home.com) */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <assert.h>
31 
32 #include "xtermset.h"
33 
main(int argc,char * argv[])34 int main(int argc, char *argv[])
35 {
36     int i,style,code,switches[2];
37     char fn[2][MAXFNCHARS-1],c,*tok;
38     char line[MAXOPTCHARS+10]; /* ? */
39     FILE *rcfile=NULL;
40     argument *argptr = NULL;
41 
42     for(i=0;i<3;i++) {
43        switches[i]=0;
44        fn[i][0]='\0';
45     }
46 
47     if(argc<=2) { /* one command line option, make gnu comliant */
48        if(argc==1) {
49 	  version();
50           usage();
51           exit(1);
52        } else {
53 	  if(strcmp(argv[1],"--help")==0) {
54 	     version();
55              usage();
56 	     gplinfo();
57              exit(0);
58           } else if (strcmp(argv[1],"--version")==0) {
59 	     version();
60              exit(0);
61 	  }
62        }
63     }
64 
65     term_loadall();
66 
67     for (i=1; i<argc; i++) {
68 	switch (style=nextopt(argv[i], &code)) {
69 	    case BasicOption:
70                 MALLOCOPTION
71 		CHECKFORCOMMAND
72 		break;
73 	    case GeomOption:
74                 MALLOCOPTION
75 		CHECKFORCOMMAND
76 		break;
77             case ShortOption:
78                 MALLOCOPTION
79                 break;
80 	    case SwitchOption:
81                 MALLOCOPTION
82 	        switches[code]=1;
83                 break;
84             case ComplexSwitchOption:
85                 switches[code]=1;
86                 if ((i + 1 < argc) && (strncmp(argv[i+1],"-",1)!=0)) {
87                     strcpy(fn[code],argv[++i]);
88 		}
89                 break;
90 	    case UnknownOption:
91 	        version();
92 		usage();
93                 argument_freeall();
94                 term_freeall();
95 		exit(2);
96 	}
97     }
98 
99     if(switches[cswDefault]) {
100         rcfile = open_rc_file(fn[cswDefault],"r");
101         if(rcfile) {
102            while(!feof(rcfile)) {
103               line[0]='\0';
104               for(i=0;!feof(rcfile);i++) {
105                  c=getc(rcfile);
106                  if ((c==EOF)||(c=='\n')) break;
107                  line[i]=c;
108               }
109               line[i]='\0';
110               if((line[0]=='#')||(strlen(line)==0))
111                  continue;
112               tok=strtok(line," ");
113               if(tok==NULL) continue;
114               style=atoi(tok);
115               tok=strtok(NULL," ");
116               if(tok==NULL) continue;
117               code=atoi(tok);
118 
119 	      argument_first();
120 	      argptr = argument_find(style,code);
121               if(argptr==NULL) {
122                  MALLOCOPTION
123                  if((tok=strtok(NULL,"\n"))!=NULL)
124                     strcpy(argptr->param,tok);
125               }
126 
127               switch(argptr->style) {
128                  case SwitchOption:
129                     switches[code]=1;
130                     break;
131               }
132            }
133            fclose(rcfile);
134         }
135     }
136 
137     if(switches[cswStore])
138         rcfile = open_rc_file(fn[cswStore],"w");
139 
140     if(!switches[0]) switches[0] = is_good_term();
141 
142     argptr = argument_first();
143     while(argptr) {
144        if(switches[cswStore] && (rcfile!=NULL)) {
145            fprintf(rcfile,"%d %d",current->style,current->command);
146            if(strlen(argptr->param)>0)
147                fprintf(rcfile," %s",argptr->param);
148            fprintf(rcfile,"\n");
149        }
150 
151        switch (argptr->style) {
152 	   case BasicOption:
153 	       if(strlen(argptr->param)>0) {
154                    CHECK {
155                        printf(ESC "]%d;%s" BEL, argptr->command,argptr->param);
156 		   }
157 	       }
158 	       break;
159            case GeomOption:
160                if(strlen(argptr->param)>0) {
161 	           CHECK {
162 		       set_geom(argptr->param);
163 		   }
164                }
165 	       break;
166 	case ShortOption:
167 	       CHECK {
168 		   printf(ESC "[%dt", argptr->command);
169                }
170 	       break;
171        }
172        argptr = argument_next();
173     }
174 
175     if(switches[cswStore])
176         fclose(rcfile);
177 
178     term_freeall();
179     argument_freeall();
180 
181     exit(0);
182 }
183 
version()184 void version() {
185     printf("GPF " PACKAGE " " VERSION "\n");
186 }
187 
usage()188 void usage()
189 {
190     printf("\n  [-store [<filename>]] [-default [<filename>]]\n");
191     printf("  [-force|f] [-title|T window-title] [-n icon-name]\n");
192     printf("  [-fg color] [-bg color] [-cr color]\n");
193     printf("  [-mousefg color] [-mousebg color]\n");
194     printf("  [-tekfg color] [-tekbg color] [-hc color]\n");
195     printf("  [-fn|font font-spec] [-geom size-spec]\n");
196     printf("  [-iconify] [-restore] [-refresh]\n\n");
197     printf("Report bugs to bje@dds.nl.\n");
198 }
199 
gplinfo()200 void gplinfo() {
201   printf("\nCopyright (C) 2000 Breyten J. Ernsting <bje@dds.nl>\n\n");
202   printf("This program is free software; you can redistribute it and/or modify\n");
203   printf("it under the terms of the GNU General Public License as published by\n");
204   printf("the Free Software Foundation; either version 2 of the License, or\n");
205   printf("(at your option) any later version.\n\n");
206   printf("This program is distributed in the hope that it will be useful,\n");
207   printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
208   printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
209   printf("GNU General Public License for more details.\n\n");
210   printf("You should have received a copy of the GNU General Public License\n");
211   printf("along with this program; if not, write to the Free Software\n");
212   printf("Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n\n");
213 }
214 
nextopt(char * arg,int * code_return)215 int nextopt(char *arg, int *code_return)
216 {
217     OPTION("title", boTitle, BasicOption)
218     OPTION("T", boTitle, BasicOption)
219     OPTION("n", boIconName, BasicOption)
220     OPTION("fg", boForeGround, BasicOption)
221     OPTION("bg", boBackGround, BasicOption)
222     OPTION("cr", boTextCursor, BasicOption)
223     OPTION("mousefg", boMouseForeGround, BasicOption)
224     OPTION("mousebg", boMouseBackGround, BasicOption)
225     OPTION("tekfg", boTEKForeGround, BasicOption)
226     OPTION("tekbg", boTEKBackGround, BasicOption)
227     OPTION("hc", boHighlightCursor, BasicOption)
228     OPTION("font", boFont, BasicOption)
229     OPTION("fn", boFont, BasicOption)
230     OPTION("geom", goGeom, GeomOption)
231     OPTION("geometry", goGeom, GeomOption)
232     OPTION("iconify", soIconify, ShortOption)
233     OPTION("restore", soRestore, ShortOption)
234     OPTION("refresh", soRefresh, ShortOption)
235     OPTION("force",swForce, SwitchOption)
236     OPTION("f",swForce,SwitchOption)
237     OPTION("store",cswStore,ComplexSwitchOption)
238     OPTION("default",cswDefault,ComplexSwitchOption)
239     return UnknownOption;
240 }
241 
set_geom(char * geometry)242 void set_geom(char *geometry)
243 {
244     char *x, *y, *width, *height;
245 
246     if (strncmp(geometry, "+", 1) == 0) {
247 	x = strtok(geometry, "+");
248 	y = strtok(NULL, "");
249 	printf(ESC "[3;%s;%st", x, y);
250     } else {
251 	width = strtok(geometry, "x");
252 	height = strtok(NULL, "+");
253 	printf(ESC "[8;%s;%st", height, width);
254 	if ((x = strtok(NULL, "+"))) {
255 	    y = strtok(NULL, "");
256 	    printf(ESC "[3;%s;%st", x, y);
257 	}
258     }
259 }
260 
open_rc_file(char * fn,char * mode)261 FILE *open_rc_file(char *fn,char *mode)
262 {
263     char fname[MAXFNCHARS-1];
264     if(strlen(fn)>0) {
265         strcpy(fname,fn);
266     } else {
267         strcpy(fname,strcat(strcat(getenv("HOME"),"/"),RCFILE));
268     }
269     return fopen(fname,mode);
270 }
271