re-fresh
[nedit-bw.git] / ttyname.patch
blob340f60190ba784b5fb2809cf9cb34191b5bc7358
1 ---
3 source/nc.c | 59 ++++++++++++++++++++++++++++++++++++++++----------------
4 source/server.c | 33 ++++++++++++++++++++++++++++---
5 2 files changed, 73 insertions(+), 19 deletions(-)
7 diff --quilt old/source/nc.c new/source/nc.c
8 --- old/source/nc.c
9 +++ new/source/nc.c
10 @@ -631,7 +631,7 @@ static CommandLine processCommandLine(in
12 static void parseCommandLine(int argc, char **argv, CommandLine *commandLine)
14 -#define MAX_RECORD_HEADER_LENGTH 64
15 +#define MAX_RECORD_HEADER_LENGTH 128
16 char name[MAXPATHLEN], path[MAXPATHLEN];
17 const char *toDoCommand = "", *langMode = "", *geometry = "";
18 char *commandString, *outPtr;
19 @@ -639,9 +639,14 @@ static void parseCommandLine(int argc, c
20 int i, lineArg, nRead, charsWritten, opts = True;
21 int fileCount = 0, group = 0, isTabbed;
22 int doInNew = False;
24 char *filePrefix = "";
25 long filePrefixLen = 0;
26 + char *ttyname1 = "";
27 + long ttyname1Len = 0;
28 + char *ttyname2 = "";
29 + long ttyname2Len = 0;
30 + const char *ttynameTmp;
32 #ifndef VMS
33 if (strcmp(GetNameOfHost(LOCAL_NAME), GetNameOfHost(NEDIT_NAME)))
35 @@ -651,19 +656,26 @@ static void parseCommandLine(int argc, c
36 sprintf(filePrefix, "%s@%s:",
37 GetUserName(LOCAL_NAME), GetNameOfHost(LOCAL_NAME));
39 + else {
40 + if ((ttynameTmp = ttyname(fileno(stdout)))) {
41 + ttyname1Len = strlen(ttynameTmp);
42 + ttyname1 = XtMalloc(ttyname1Len + 1);
43 + strcpy(ttyname1, ttynameTmp);
44 + }
45 + if ((ttynameTmp = ttyname(fileno(stderr)))) {
46 + ttyname2Len = strlen(ttynameTmp);
47 + ttyname2 = XtMalloc(ttyname2Len + 1);
48 + strcpy(ttyname2, ttynameTmp);
49 + }
50 + }
51 #endif
53 /* Allocate a string for output, for the maximum possible length. The
54 maximum length is calculated by assuming every argument is a file,
55 and a complete record of maximum length is created for it */
56 - for (i=1; i<argc; i++) {
57 + for (i=0; i<argc; i++) {
58 length += MAX_RECORD_HEADER_LENGTH + strlen(argv[i]) + MAXPATHLEN
59 - + filePrefixLen;
60 - }
61 - /* In case of no arguments, must still allocate space for one record header */
62 - if (length < MAX_RECORD_HEADER_LENGTH)
63 - {
64 - length = MAX_RECORD_HEADER_LENGTH;
65 + + filePrefixLen + ttyname1Len + ttyname2Len;
67 commandString = XtMalloc(length+1);
69 @@ -780,8 +792,8 @@ static void parseCommandLine(int argc, c
72 /* See below for casts */
73 - sprintf(outPtr, "%d %d %d %d %d %d %ld %ld %ld %ld\n"
74 - "%s%s\n%s\n%s\n%s\n%n",
75 + sprintf(outPtr, "%d %d %d %d %d %d %ld %ld %ld %ld 0 0\n"
76 + "%s%s\n%s\n%s\n%s\n\n\n%n",
77 lineNum, read, create, iconic, tabbed,
78 !!Preferences.waitForClose,
79 filePrefixLen + (long) strlen(path),
80 @@ -826,11 +838,13 @@ static void parseCommandLine(int argc, c
81 The "long" cast on strlen() is necessary because size_t
82 is 64 bit on Alphas, and 32-bit on most others. There is
83 no printf format specifier for "size_t", thanx, ANSI. */
84 - sprintf(outPtr, "%d %d %d %d %d %d %ld %ld %ld %ld\n%n", lineNum,
85 - read, create, iconic, isTabbed, !!Preferences.waitForClose,
86 + sprintf(outPtr, "%d %d %d %d %d %d %ld %ld %ld %ld %ld %ld\n%n",
87 + lineNum, read, create, iconic, isTabbed,
88 + !!Preferences.waitForClose,
89 filePrefixLen + (long) strlen(path),
90 (long) strlen(toDoCommand), (long) strlen(langMode),
91 - (long) strlen(geometry), &charsWritten);
92 + (long) strlen(geometry), ttyname1Len, ttyname2Len,
93 + &charsWritten);
94 outPtr += charsWritten;
95 strcpy(outPtr, filePrefix);
96 outPtr += filePrefixLen;
97 @@ -846,6 +860,12 @@ static void parseCommandLine(int argc, c
98 strcpy(outPtr, geometry);
99 outPtr += strlen(geometry);
100 *outPtr++ = '\n';
101 + strcpy(outPtr, ttyname1);
102 + outPtr += ttyname1Len;
103 + *outPtr++ = '\n';
104 + strcpy(outPtr, ttyname2);
105 + outPtr += ttyname2Len;
106 + *outPtr++ = '\n';
107 toDoCommand = "";
109 /* Create the file open atoms for the paths supplied */
110 @@ -891,10 +911,11 @@ static void parseCommandLine(int argc, c
111 /* TODO: what about other platforms? */
112 #endif /* VMS */
114 - sprintf(outPtr, "0 0 %d %d %d 0 %ld %ld %ld %ld\n%n",
115 + sprintf(outPtr, "0 0 %d %d %d 0 %ld %ld %ld %ld %ld %ld\n%n",
116 doInNew, iconic, tabbed, -(filePrefixLen + length),
117 (long) strlen(toDoCommand), (long) strlen(langMode),
118 - (long) strlen(geometry), &charsWritten);
119 + (long) strlen(geometry), ttyname1Len, ttyname2Len,
120 + &charsWritten);
121 outPtr += charsWritten;
122 strcpy(outPtr, filePrefix);
123 outPtr += filePrefixLen;
124 @@ -910,6 +931,12 @@ static void parseCommandLine(int argc, c
125 strcpy(outPtr, geometry);
126 outPtr += strlen(geometry);
127 *outPtr++ = '\n';
128 + strcpy(outPtr, ttyname1);
129 + outPtr += ttyname1Len;
130 + *outPtr++ = '\n';
131 + strcpy(outPtr, ttyname2);
132 + outPtr += ttyname2Len;
133 + *outPtr++ = '\n';
136 if (filePrefixLen > 0)
137 diff --quilt old/source/server.c new/source/server.c
138 --- old/source/server.c
139 +++ new/source/server.c
140 @@ -50,6 +50,7 @@ static const char CVSID[] = "$Id: server
141 #include <stdlib.h>
142 #include <string.h>
143 #include <limits.h>
144 +#include <errno.h>
145 #ifdef VMS
146 #include <lib$routines.h>
147 #include ssdef
148 @@ -390,6 +391,8 @@ static void processServerCommandString(c
149 WindowInfo *window, *lastFile = NULL;
150 long currentDesktop = QueryCurrentDesktop(TheDisplay,
151 RootWindow(TheDisplay, DefaultScreen(TheDisplay)));
152 + long ttyname1Len, ttyname2Len;
153 + char *ttyname1, *ttyname2;
155 /* If the command string is empty, put up an empty, Untitled window
156 (or just pop one up if it already exists) */
157 @@ -427,10 +430,11 @@ static void processServerCommandString(c
158 command both followed by newlines. This bit of code reads the
159 header, and converts the newlines following the filename and do
160 command to nulls to terminate the filename and doCommand strings */
161 - itemsRead = sscanf(inPtr, "%d %d %d %d %d %d %d %d %d %d%n", &lineNum,
162 + itemsRead = sscanf(inPtr, "%d %d %d %d %d %d %d %d %d %d %ld %ld%n", &lineNum,
163 &readFlag, &createFlag, &iconicFlag, &tabbed, &clientWaits,
164 - &fileLen, &doLen, &lmLen, &geomLen, &charsRead);
165 - if (itemsRead != 10)
166 + &fileLen, &doLen, &lmLen, &geomLen, &ttyname1Len, &ttyname2Len,
167 + &charsRead);
168 + if (itemsRead != 12)
169 goto readError;
170 inPtr += charsRead + 1;
171 if (fileLen >= 0 && inPtr - string + fileLen > stringLen)
172 @@ -455,7 +459,30 @@ static void processServerCommandString(c
173 geometry = inPtr;
174 inPtr += geomLen;
175 *inPtr++ = '\0';
176 + if (inPtr - string + ttyname1Len > stringLen)
177 + goto readError;
178 + ttyname1 = inPtr;
179 + inPtr += ttyname1Len;
180 + *inPtr++ = '\0';
181 + if (inPtr - string + ttyname2Len > stringLen)
182 + goto readError;
183 + ttyname2 = inPtr;
184 + inPtr += ttyname2Len;
185 + *inPtr++ = '\0';
187 + if (ttyname1Len) {
188 + errno = 0;
189 + stdout = freopen(ttyname1, "a", stdout);
190 + if (stdout && errno == 0)
191 + setvbuf(stdout, NULL, _IOLBF, 0);
193 + if (ttyname2Len) {
194 + errno = 0;
195 + stderr = freopen(ttyname2, "a", stderr);
196 + if (stderr && errno == 0)
197 + setvbuf(stderr, NULL, _IONBF, 0);
200 fullname = PreOpenHook(NULL, requestname);
202 /* An empty file name means: