Link with C++ linker
[oweals/cde.git] / cde / programs / dtterm / DtTermSyntax.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 #ifndef lint
24 #ifdef  VERBOSE_REV_INFO
25 static char rcs_id[] = "$XConsortium: DtTermSyntax.c /main/4 1996/05/16 11:22:57 ageorge $";
26 #endif  /* VERBOSE_REV_INFO */
27 #endif  /* lint */
28
29 /*                                                                      *
30  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
31  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
32  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
33  * (c) Copyright 1993, 1994 Novell, Inc.                                *
34  */
35
36 #include "TermHeader.h"
37 #include "TermPrimMessageCatI.h"
38 struct _options {
39     char *opt;
40     char *desc;
41     struct _options *next;
42 };
43
44 static struct _options *optHead;
45 static struct _options *messageHead;
46
47 static char *options_default[] = {
48 "-/+132                         enable/disable 80<->132 column escape seq",
49 "-/+aw                          enable/disable autowrap",
50 "-/+bs                          turn off/on Term background is select color",
51 "-display displayname           X server to contact",
52 "-e command args                command to execute",
53 "-fb fontset                    bold text font",
54 "-fn fontset                    normal text font",
55 "-geometry geom                 size (in characters) and position of window",
56 "-help                          print out this message",
57 "-/+iconic                      start/do not start iconic",
58 "-/+j                           enable/disable jump scroll",
59 "-/+kshMode                     enable/disable ksh mode",
60 "-/+l                           enable/disable logging",
61 "-lf filename                   logging filename",
62 "-/+ls                          enable/disable login shell",
63 "-/+map                         enable/disable map window on pty output",
64 "-/+mb                          enable/disable margin bell",
65 "-ms color                      pointer color",
66 "-n string                      specify icon name",
67 "-name string                   client instance, icon, and title strings",
68 "-nb distance                   specify distance for right margin bell",
69 "-/+rw                          enable/disable reverse wrap",
70 "-/+sb                          enable/disable scroll bar",
71 "-/+sf                          enable/disable SUN function keys",
72 "-sl number[s]                  number of scrolled lines [screens] to save",
73 "-ti name                       string used for programmatic identification",
74 "-title string                  title string for window",
75 "-tm string                     terminal mode keywords and characters",
76 "-tn name                       TERM environment variable name",
77 "-usage                         print out this message",
78 "-/+vb                          enable/disable visual bell",
79 "-xrm resourcestring            additional resource specifications",
80 "-C                             console mode",
81 "-Sxxd                          slave mode on \"ttyxx\" file descriptor \"d\"",
82 "-Sxxx.d                        slave mode on \"ttyxxx\" file descriptor \"d\"",
83 "End-Of-List",
84 };
85
86 static char *message_defaults[] = {
87 "The -e option, if given must appear at the end of the command line,",
88 "otherwise the user's default shell will be started.  Options that start",
89 "with a plus sign (+) restore the default.",
90 "End-Of-List",
91 };
92
93 static void GetUsage()
94 {
95     register struct _options *optPtr;
96     register int i;
97     register char *c;
98     char *c2;
99     int num_messages;
100     optHead = (struct _options *) 0;
101     optPtr  = (struct _options *) 0;
102
103     for (i = 1; ; i++) {
104         /* 
105         ** get the next option...
106         */
107         num_messages = i;
108         c2 = GETMESSAGE(NL_SETN_Syntax, i, options_default[i-1]);
109         c = XtMalloc(strlen(c2) + 1);
110         (void) strcpy(c, c2);
111         /* 
112         ** check and see if we are at the end of the list...
113         */
114         if (!strcmp(c, "End-Of-List"))
115             break;
116
117         /* 
118         ** allocate the next entry...
119         */
120         if (!optHead) {
121             optHead = (struct _options *) malloc(sizeof(struct _options));
122             optPtr = optHead;
123         } else {
124             optPtr->next = (struct _options *) malloc(sizeof(struct _options));
125             optPtr = optPtr->next;
126         }
127
128 #ifdef  DKS
129         /* 
130         ** did we run out of malloc space...
131         */
132         if (!optPtr) {
133             errno = 0;
134 #ifdef    _VUE_NO_PROTO
135             SysError(HPT_MALLOC4);
136 #else  /* _VUE_NO_PROTO */
137             SysError(HPT_MALLOC4, NULL);
138 #endif /* _VUE_NO_PROTO */
139         }
140 #endif  /* DKS */
141
142         /* 
143         ** there is no next element yet...
144         */
145         optPtr->next = (struct _options *) 0;
146
147         /* 
148         ** the first part of the string is the option...
149         */
150         optPtr->opt = c;
151         /* 
152         ** find a tab...
153         */
154         while (*c && ('\t' != *c))
155             (void) c++;
156         /* 
157         ** this marks the end of the option...
158         */
159         if (*c)
160             *c++ = '\0';
161         /* 
162         ** skip over any more tabs...
163         */
164         while (*c && ('\t' == *c))
165             (void) c++;
166         /* 
167         ** and this is the beginning of the option desc..
168         */
169         optPtr->desc = c;
170     }
171
172     messageHead = (struct _options *) 0;
173     optPtr      = (struct _options *) 0;
174
175     for (i = num_messages + 1; ; i++) {
176         /* 
177         ** get the next message string...
178         */
179         c2 = GETMESSAGE(NL_SETN_Syntax, i,message_defaults[i - num_messages - 1]);
180         c = XtMalloc(strlen(c2) + 1);
181         (void) strcpy(c, c2);
182         /* 
183         ** check and see if we are at the end of the list...
184         */
185         if (!strcmp(c, "End-Of-List"))
186             break;
187
188         /* 
189         ** allocate the next entry...
190         */
191         if (!messageHead) {
192             messageHead = (struct _options *) malloc(sizeof(struct _options));
193             optPtr = messageHead;
194         } else {
195             optPtr->next = (struct _options *) malloc(sizeof(struct _options));
196             optPtr = optPtr->next;
197         }
198
199 #ifdef  DKS
200         /* 
201         ** did we run out of malloc space...
202         */
203         if (!optPtr) {
204             errno = 0;
205 #ifdef    _VUE_NO_PROTO
206             SysError(HPT_MALLOC5);
207 #else  /* _VUE_NO_PROTO */
208             SysError(HPT_MALLOC5, NULL);
209 #endif /* _VUE_NO_PROTO */
210         }
211 #endif  /* DKS */
212
213         /* 
214         ** there is no next element yet...
215         */
216         optPtr->next = (struct _options *) 0;
217
218         /* 
219         ** the entire string is the "desc"...
220         */
221         optPtr->desc = c;
222     }
223 }
224
225 void Syntax(char *programName, char *badOption)
226 {
227     register struct _options    *optPtr;
228     int                          col;
229     int                          cols;
230     char                        *c;
231     char                        *fmt;
232     int                          fmtlen;
233     char                         buffer[BUFSIZ];
234
235     /* 
236     ** get the usage message string...
237     */
238     GetUsage();
239
240     /* suppress codecenter "Assignment in conditional 'if' expression."
241      * warning...
242      */
243     /*SUPPRESS 624*/
244     if (c = getenv("COLUMNS"))  cols = atoi(c);
245     else                        cols = 80;
246
247     (void) fprintf(stderr, (GETMESSAGE(NL_SETN_Syntax,47,
248                    "%s:  bad command line option \"%s\"\r\n\n")),
249                    programName, badOption);
250
251     (void) fprintf(stderr, (GETMESSAGE(NL_SETN_Syntax,48, "usage:  %s")),
252                    programName);
253     col = 8 + strlen(programName);
254     /* 
255     ** now that we are NLSized, we need to figure out the width that the
256     ** format string adds to each option...
257     */
258
259     c = GETMESSAGE(NL_SETN_Syntax,50, " [%s]");
260     fmt = XtMalloc(strlen(c) + 1);
261     (void) strcpy(fmt, c);
262     (void) sprintf(buffer, fmt, "");
263     fmtlen = strlen(buffer);
264
265     for (optPtr = optHead; optPtr; optPtr = optPtr->next) {
266         /*DKS*DKS*DKS*
267         ** the following 3 assumes that the msg_catalog doesn't add more than
268         ** 2 characters to the string...
269         */
270         int len = fmtlen + strlen(optPtr->opt);
271         if (col + len >= cols) {
272             (void) fprintf(stderr, (GETMESSAGE(NL_SETN_Syntax,49, "\r\n   ")));
273             col = 3;
274         }
275         (void) fprintf(stderr, fmt, optPtr->opt);
276         col += len;
277     }
278     (void) fprintf(stderr, (GETMESSAGE(NL_SETN_Syntax,51,
279                    "\r\n\nType \"%s -help\" for a full description.\r\n\n")),
280                    programName);
281
282     (void) exit(1);
283 }
284
285 void Help(char *programName)
286 {
287     register struct _options     *optPtr;
288     int                           width = 0;
289
290     /* 
291     ** get the usage message string...
292     */
293     GetUsage();
294
295     (void) fprintf(stderr, (GETMESSAGE(NL_SETN_Syntax,52, "usage:\n")));
296     (void) fprintf(stderr, (GETMESSAGE(NL_SETN_Syntax,53,
297                    "\t%s [-options ...] [-e command args]\n\n")),
298                    programName);
299     (void) fprintf(stderr, (GETMESSAGE(NL_SETN_Syntax,54,
300                    "where options include:\n")));
301     for (optPtr = optHead; optPtr; optPtr = optPtr->next)
302         if (strlen(optPtr->opt) > (size_t) width) width = strlen(optPtr->opt);
303
304     for (optPtr = optHead; optPtr; optPtr = optPtr->next)
305         (void) fprintf(stderr, "    %-*s  %s\n", width, optPtr->opt,
306                        optPtr->desc);
307     
308     (void) fprintf(stderr, "\n");
309     for (optPtr = messageHead; optPtr; optPtr = optPtr->next)
310         (void) fprintf(stderr, "%s\n", optPtr->desc);
311
312     (void) fprintf(stderr, "\n");
313     (void) exit(0);
314 }