[go: up one dir, main page]

Menu

[ffec3f]: / source / nc.c  Maximize  Restore  History

Download this file

1012 lines (903 with data), 35.9 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
/*******************************************************************************
* *
* nc.c -- Nirvana Editor client program for nedit server processes *
* *
* Copyright (C) 1999 Mark Edel *
* *
* This is free software; you can redistribute it and/or modify it under the *
* terms of the GNU General Public License as published by the Free Software *
* Foundation; either version 2 of the License, or (at your option) any later *
* version. In addition, you may distribute version of this program linked to *
* Motif or Open Motif. See README for details. *
* *
* This software is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
* for more details. *
* *
* You should have received a copy of the GNU General Public License along with *
* software; if not, write to the Free Software Foundation, Inc., 59 Temple *
* Place, Suite 330, Boston, MA 02111-1307 USA *
* *
* Nirvana Text Editor *
* November, 1995 *
* *
* Written by Mark Edel *
* *
*******************************************************************************/
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#include "server_common.h"
#include "../util/fileUtils.h"
#include "../util/utils.h"
#include "../util/prefFile.h"
#include "../util/system.h"
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#ifdef VMS
#include <lib$routines.h>
#include descrip
#include ssdef
#include syidef
#include "../util/VMSparam.h"
#include "../util/VMSutils.h"
#else
#ifndef __MVS__
#include <sys/param.h>
#endif
#include <sys/types.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <pwd.h>
#include "../util/clearcase.h"
#endif /* VMS */
#ifdef __EMX__
#include <process.h>
#endif
#include <X11/Intrinsic.h>
#include <X11/Xatom.h>
#ifdef HAVE_DEBUG_H
#include "../debug.h"
#endif
#define APP_NAME "nc"
#define APP_CLASS "NEditClient"
#define PROPERTY_CHANGE_TIMEOUT (Preferences.timeOut * 1000) /* milliseconds */
#define SERVER_START_TIMEOUT (Preferences.timeOut * 3000) /* milliseconds */
#define REQUEST_TIMEOUT (Preferences.timeOut * 1000) /* milliseconds */
#define FILE_OPEN_TIMEOUT (Preferences.timeOut * 3000) /* milliseconds */
typedef struct
{
char* shell;
char* serverRequest;
} CommandLine;
static void timeOutProc(Boolean *timeOutReturn, XtIntervalId *id);
static int startServer(const char *message, const char *commandLine);
static CommandLine processCommandLine(int argc, char** argv);
static void parseCommandLine(int argc, char **arg, CommandLine *cmdLine);
static void nextArg(int argc, char **argv, int *argIndex);
static void copyCommandLineArg(CommandLine *cmdLine, const char *arg);
static void printNcVersion(void);
static Boolean findExistingServer(XtAppContext context,
Window rootWindow,
Atom serverExistsAtom);
static void startNewServer(XtAppContext context,
Window rootWindow,
char* commandLine,
Atom serverExistsAtom);
static void waitUntilRequestProcessed(XtAppContext context,
Window rootWindow,
char* commandString,
Atom serverRequestAtom);
static void waitUntilFilesOpenedOrClosed(XtAppContext context,
Window rootWindow);
Display *TheDisplay;
XtAppContext AppContext;
static Atom currentWaitForAtom;
static Atom noAtom = (Atom)(-1);
static const char cmdLineHelp[] =
#ifdef VMS
"[Sorry, no on-line help available.]\n"; /* Why is that ? */
#else
"Usage: nc [-read] [-create]\n"
" [-line n | +n] [-do command] [-lm languagemode]\n"
" [-svrname name] [-svrcmd command]\n"
" [-ask] [-noask] [-timeout seconds]\n"
" [-geometry geometry | -g geometry] [-icon | -iconic]\n"
" [-tabbed] [-untabbed] [-group] [-wait]\n"
" [-V | -version] [-h|-help]\n"
" [-xrm resourcestring] [-display [host]:server[.screen]]\n"
" [--] [file...]\n";
#endif /*VMS*/
/* Structure to hold X Resource values */
static struct {
int autoStart;
char serverCmd[2*MAXPATHLEN]; /* holds executable name + flags */
char serverName[MAXPATHLEN];
int waitForClose;
int timeOut;
} Preferences;
/* Application resources */
static PrefDescripRec PrefDescrip[] = {
{"autoStart", "AutoStart", PREF_BOOLEAN, "True",
&Preferences.autoStart, NULL, True},
{"serverCommand", "ServerCommand", PREF_STRING, "nedit -server",
Preferences.serverCmd, (void *)sizeof(Preferences.serverCmd), False},
{"serverName", "serverName", PREF_STRING, "", Preferences.serverName,
(void *)sizeof(Preferences.serverName), False},
{"waitForClose", "WaitForClose", PREF_BOOLEAN, "False",
&Preferences.waitForClose, NULL, False},
{"timeOut", "TimeOut", PREF_INT, "10",
&Preferences.timeOut, NULL, False}
};
/* Resource related command line options */
static XrmOptionDescRec OpTable[] = {
{"-ask", ".autoStart", XrmoptionNoArg, (caddr_t)"False"},
{"-noask", ".autoStart", XrmoptionNoArg, (caddr_t)"True"},
{"-svrname", ".serverName", XrmoptionSepArg, (caddr_t)NULL},
{"-svrcmd", ".serverCommand", XrmoptionSepArg, (caddr_t)NULL},
{"-wait", ".waitForClose", XrmoptionNoArg, (caddr_t)"True"},
{"-timeout", ".timeOut", XrmoptionSepArg, (caddr_t)NULL}
};
/* Struct to hold info about files being opened and edited. */
typedef struct _FileListEntry {
Atom waitForFileOpenAtom;
Atom waitForFileClosedAtom;
char* path;
struct _FileListEntry *next;
} FileListEntry;
typedef struct {
int waitForOpenCount;
int waitForCloseCount;
FileListEntry* fileList;
} FileListHead;
static FileListHead fileListHead;
static void setPropertyValue(Atom atom) {
XChangeProperty(TheDisplay,
RootWindow(TheDisplay, DefaultScreen(TheDisplay)),
atom, XA_STRING,
8, PropModeReplace,
(unsigned char *)"True", 4);
}
/* Add another entry to the file entry list, if it doesn't exist yet. */
static void addToFileList(const char *path)
{
FileListEntry *item;
/* see if the file already exists in the list */
for (item = fileListHead.fileList; item; item = item->next) {
if (!strcmp(item->path, path))
break;
}
/* Add the atom to the head of the file list if it wasn't found. */
if (item == 0) {
item = malloc(sizeof(item[0]));
item->waitForFileOpenAtom = None;
item->waitForFileClosedAtom = None;
item->path = (char*)malloc(strlen(path)+1);
strcpy(item->path, path);
item->next = fileListHead.fileList;
fileListHead.fileList = item;
}
}
/* Creates the properties for the various paths */
static void createWaitProperties(void)
{
FileListEntry *item;
for (item = fileListHead.fileList; item; item = item->next) {
fileListHead.waitForOpenCount++;
item->waitForFileOpenAtom =
CreateServerFileOpenAtom(Preferences.serverName, item->path);
setPropertyValue(item->waitForFileOpenAtom);
if (Preferences.waitForClose == True) {
fileListHead.waitForCloseCount++;
item->waitForFileClosedAtom =
CreateServerFileClosedAtom(Preferences.serverName,
item->path,
False);
setPropertyValue(item->waitForFileClosedAtom);
}
}
}
int main(int argc, char **argv)
{
XtAppContext context;
Window rootWindow;
CommandLine commandLine;
Atom serverExistsAtom, serverRequestAtom;
XrmDatabase prefDB;
Boolean serverExists;
/* Initialize toolkit and get an application context */
XtToolkitInitialize();
AppContext = context = XtCreateApplicationContext();
#ifdef VMS
/* Convert the command line to Unix style */
ConvertVMSCommandLine(&argc, &argv);
#endif /*VMS*/
#ifdef __EMX__
/* expand wildcards if necessary */
_wildcard(&argc, &argv);
#endif
/* Read the preferences command line into a database (note that we
don't support the .nc file anymore) */
prefDB = CreatePreferencesDatabase(NULL, APP_CLASS,
OpTable, XtNumber(OpTable), (unsigned *)&argc, argv);
/* Process the command line before calling XtOpenDisplay, because the
latter consumes certain command line arguments that we still need
(-icon, -geometry ...) */
commandLine = processCommandLine(argc, argv);
/* Open the display and find the root window */
TheDisplay = XtOpenDisplay (context, NULL, APP_NAME, APP_CLASS, NULL,
0, &argc, argv);
if (!TheDisplay) {
XtWarning ("nc: Can't open display\n");
exit(EXIT_FAILURE);
}
rootWindow = RootWindow(TheDisplay, DefaultScreen(TheDisplay));
/* Read the application resources into the Preferences data structure */
RestorePreferences(prefDB, XtDatabase(TheDisplay), APP_NAME,
APP_CLASS, PrefDescrip, XtNumber(PrefDescrip));
/* Make sure that the time out unit is at least 1 second and not too
large either (overflow!). */
if (Preferences.timeOut < 1) {
Preferences.timeOut = 1;
} else if (Preferences.timeOut > 1000) {
Preferences.timeOut = 1000;
}
#ifndef VMS
/* For Clearcase users who have not set a server name, use the clearcase
view name. Clearcase views make files with the same absolute path names
but different contents (and therefore can't be edited in the same nedit
session). This should have no bad side-effects for non-clearcase users */
if (Preferences.serverName[0] == '\0') {
const char* viewTag = GetClearCaseViewTag();
if (viewTag != NULL && strlen(viewTag) < MAXPATHLEN) {
strcpy(Preferences.serverName, viewTag);
}
}
#endif /* VMS */
/* Create the wait properties for the various files. */
createWaitProperties();
/* Monitor the properties on the root window */
XSelectInput(TheDisplay, rootWindow, PropertyChangeMask);
/* Create the server property atoms on the current DISPLAY. */
CreateServerPropertyAtoms(Preferences.serverName,
&serverExistsAtom,
&serverRequestAtom);
serverExists = findExistingServer(context,
rootWindow,
serverExistsAtom);
if (serverExists == False)
startNewServer(context, rootWindow, commandLine.shell, serverExistsAtom);
waitUntilRequestProcessed(context,
rootWindow,
commandLine.serverRequest,
serverRequestAtom);
waitUntilFilesOpenedOrClosed(context, rootWindow);
XtCloseDisplay(TheDisplay);
XtFree(commandLine.shell);
XtFree(commandLine.serverRequest);
return 0;
}
/*
** Xt timer procedure for timeouts on NEdit server requests
*/
static void timeOutProc(Boolean *timeOutReturn, XtIntervalId *id)
{
/* NOTE: XtAppNextEvent() does call this routine but
** doesn't return unless there are more events.
** Hence, we generate this (synthetic) event to break the deadlock
*/
Window rootWindow = RootWindow(TheDisplay, DefaultScreen(TheDisplay));
if (currentWaitForAtom != noAtom) {
XChangeProperty(TheDisplay, rootWindow, currentWaitForAtom, XA_STRING,
8, PropModeReplace, (unsigned char *)"", strlen(""));
}
/* Flag that the timeout has occurred. */
*timeOutReturn = True;
}
static Boolean findExistingServer(XtAppContext context,
Window rootWindow,
Atom serverExistsAtom)
{
Boolean serverExists = True;
unsigned char *propValue;
int getFmt;
Atom dummyAtom;
unsigned long dummyULong, nItems;
/* See if there might be a server (not a guaranty), by translating the
root window property NEDIT_SERVER_EXISTS_<user>_<host> */
if (XGetWindowProperty(TheDisplay, rootWindow, serverExistsAtom, 0,
INT_MAX, False, XA_STRING, &dummyAtom, &getFmt, &nItems,
&dummyULong, &propValue) != Success || nItems == 0) {
serverExists = False;
} else {
Boolean timeOut = False;
XtIntervalId timerId;
XFree(propValue);
/* Remove the server exists property to make sure the server is
** running. If it is running it will get recreated.
*/
XDeleteProperty(TheDisplay, rootWindow, serverExistsAtom);
XSync(TheDisplay, False);
timerId = XtAppAddTimeOut(context,
PROPERTY_CHANGE_TIMEOUT,
(XtTimerCallbackProc)timeOutProc,
&timeOut);
currentWaitForAtom = serverExistsAtom;
while (!timeOut) {
/* NOTE: XtAppNextEvent() does call the timeout routine but
** doesn't return unless there are more events. */
XEvent event;
const XPropertyEvent *e = (const XPropertyEvent *)&event;
XtAppNextEvent(context, &event);
/* We will get a PropertyNewValue when the server recreates
** the server exists atom. */
if (e->type == PropertyNotify &&
e->window == rootWindow &&
e->atom == serverExistsAtom) {
if (e->state == PropertyNewValue) {
break;
}
}
XtDispatchEvent(&event);
}
/* Start a new server if the timeout expired. The server exists
** property was not recreated. */
if (timeOut) {
serverExists = False;
} else {
XtRemoveTimeOut(timerId);
}
}
return(serverExists);
}
static void startNewServer(XtAppContext context,
Window rootWindow,
char* commandLine,
Atom serverExistsAtom)
{
Boolean timeOut = False;
XtIntervalId timerId;
/* Add back the server name resource from the command line or resource
database to the command line for starting the server. If -svrcmd
appeared on the original command line, it was removed by
CreatePreferencesDatabase before the command line was recorded
in commandLine.shell. Moreover, if no server name was specified, it
may have defaulted to the ClearCase view tag. */
if (Preferences.serverName[0] != '\0') {
strcat(commandLine, " -svrname ");
strcat(commandLine, Preferences.serverName);
}
switch (startServer("No servers available, start one? (y|n) [y]: ",
commandLine))
{
case -1: /* Start failed */
XtCloseDisplay(TheDisplay);
exit(EXIT_FAILURE);
break;
case -2: /* Start canceled by user */
XtCloseDisplay(TheDisplay);
exit(EXIT_SUCCESS);
break;
}
/* Set up a timeout proc in case the server is dead. The standard
selection timeout is probably a good guess at how long to wait
for this style of inter-client communication as well */
timerId = XtAppAddTimeOut(context,
SERVER_START_TIMEOUT,
(XtTimerCallbackProc)timeOutProc,
&timeOut);
currentWaitForAtom = serverExistsAtom;
/* Wait for the server to start */
while (!timeOut) {
XEvent event;
const XPropertyEvent *e = (const XPropertyEvent *)&event;
/* NOTE: XtAppNextEvent() does call the timeout routine but
** doesn't return unless there are more events. */
XtAppNextEvent(context, &event);
/* We will get a PropertyNewValue when the server updates
** the server exists atom. If the property is deleted the
** the server must have died. */
if (e->type == PropertyNotify &&
e->window == rootWindow &&
e->atom == serverExistsAtom) {
if (e->state == PropertyNewValue) {
break;
} else if (e->state == PropertyDelete) {
fprintf(stderr, "%s: The server failed to start.\n", APP_NAME);
XtCloseDisplay(TheDisplay);
exit(EXIT_FAILURE);
}
}
XtDispatchEvent(&event);
}
/* Exit if the timeout expired. */
if (timeOut) {
fprintf(stderr, "%s: The server failed to start (time-out).\n", APP_NAME);
XtCloseDisplay(TheDisplay);
exit(EXIT_FAILURE);
} else {
XtRemoveTimeOut(timerId);
}
}
/*
** Prompt the user about starting a server, with "message", then start server
*/
static int startServer(const char *message, const char *commandLineArgs)
{
char c, *commandLine;
#ifdef VMS
int spawnFlags = 1 /* + 1<<3 */; /* NOWAIT, NOKEYPAD */
int spawn_sts;
struct dsc$descriptor_s *cmdDesc;
char *nulDev = "NL:";
struct dsc$descriptor_s *nulDevDesc;
#else
int sysrc;
#endif /* !VMS */
/* prompt user whether to start server */
if (!Preferences.autoStart) {
printf("%s", message);
do {
c = getc(stdin);
} while (c == ' ' || c == '\t');
if (c != 'Y' && c != 'y' && c != '\n')
return (-2);
}
/* start the server */
#ifdef VMS
commandLine = XtMalloc(strlen(Preferences.serverCmd) +
strlen(commandLineArgs) + 3);
sprintf(commandLine, "%s %s", Preferences.serverCmd, commandLineArgs);
cmdDesc = NulStrToDesc(commandLine); /* build command descriptor */
nulDevDesc = NulStrToDesc(nulDev); /* build "NL:" descriptor */
spawn_sts = lib$spawn(cmdDesc, nulDevDesc, 0, &spawnFlags, 0,0,0,0,0,0,0,0);
XtFree(commandLine);
if (spawn_sts != SS$_NORMAL) {
fprintf(stderr, "Error return from lib$spawn: %d\n", spawn_sts);
fprintf(stderr, "Nedit server not started.\n");
return (-1);
} else {
FreeStrDesc(cmdDesc);
return 0;
}
#else
#if defined(__EMX__) /* OS/2 */
/* Unfortunately system() calls a shell determined by the environment
variables COMSPEC and EMXSHELL. We have to figure out which one. */
{
char *sh_spec, *sh, *base;
char *CMD_EXE="cmd.exe";
commandLine = XtMalloc(strlen(Preferences.serverCmd) +
strlen(commandLineArgs) + 15);
sh = getenv ("EMXSHELL");
if (sh == NULL)
sh = getenv ("COMSPEC");
if (sh == NULL)
sh = CMD_EXE;
sh_spec=XtNewString(sh);
base=_getname(sh_spec);
_remext(base);
if (stricmp (base, "cmd") == 0 || stricmp (base, "4os2") == 0) {
sprintf(commandLine, "start /C /MIN %s %s",
Preferences.serverCmd, commandLineArgs);
} else {
sprintf(commandLine, "%s %s &",
Preferences.serverCmd, commandLineArgs);
}
XtFree(sh_spec);
}
#else /* Unix */
commandLine = XtMalloc(strlen(Preferences.serverCmd) +
strlen(commandLineArgs) + 3);
sprintf(commandLine, "%s %s&", Preferences.serverCmd, commandLineArgs);
#endif
sysrc=system(commandLine);
XtFree(commandLine);
if (sysrc==0)
return 0;
else
return (-1);
#endif /* !VMS */
}
/* Reconstruct the command line in string commandLine in case we have to
* start a server (nc command line args parallel nedit's). Include
* -svrname if nc wants a named server, so nedit will match. Special
* characters are protected from the shell by escaping EVERYTHING with \
*/
static CommandLine processCommandLine(int argc, char** argv)
{
CommandLine commandLine;
int i;
int length = 0;
for (i=1; i<argc; i++) {
length += 1 + strlen(argv[i])*4 + 2;
}
commandLine.shell = XtMalloc(length+1 + 9 + MAXPATHLEN);
*commandLine.shell = '\0';
/* Convert command line arguments into a command string for the server */
parseCommandLine(argc, argv, &commandLine);
if (commandLine.serverRequest == NULL) {
fprintf(stderr, "nc: Invalid commandline argument\n");
exit(EXIT_FAILURE);
}
return(commandLine);
}
/*
** Converts command line into a command string suitable for passing to
** the server
*/
static void parseCommandLine(int argc, char **argv, CommandLine *commandLine)
{
#define MAX_RECORD_HEADER_LENGTH 38
char name[MAXPATHLEN], path[MAXPATHLEN];
const char *toDoCommand = "", *langMode = "", *geometry = "";
char *commandString, *outPtr;
int lineNum = 0, read = 0, create = 0, iconic = 0, tabbed = -1, length = 0;
int i, lineArg, nRead, charsWritten, opts = True;
int fileCount = 0, group = 0, isTabbed;
/* Allocate a string for output, for the maximum possible length. The
maximum length is calculated by assuming every argument is a file,
and a complete record of maximum length is created for it */
for (i=1; i<argc; i++) {
length += MAX_RECORD_HEADER_LENGTH + strlen(argv[i]) + MAXPATHLEN;
}
/* In case of no arguments, must still allocate space for one record header */
if (length < MAX_RECORD_HEADER_LENGTH)
{
length = MAX_RECORD_HEADER_LENGTH;
}
commandString = XtMalloc(length+1);
/* Parse the arguments and write the output string */
outPtr = commandString;
for (i=1; i<argc; i++) {
if (opts && !strcmp(argv[i], "--")) {
opts = False; /* treat all remaining arguments as filenames */
continue;
} else if (opts && !strcmp(argv[i], "-do")) {
nextArg(argc, argv, &i);
toDoCommand = argv[i];
} else if (opts && !strcmp(argv[i], "-lm")) {
copyCommandLineArg(commandLine, argv[i]);
nextArg(argc, argv, &i);
langMode = argv[i];
copyCommandLineArg(commandLine, argv[i]);
} else if (opts && (!strcmp(argv[i], "-g") ||
!strcmp(argv[i], "-geometry"))) {
copyCommandLineArg(commandLine, argv[i]);
nextArg(argc, argv, &i);
geometry = argv[i];
copyCommandLineArg(commandLine, argv[i]);
} else if (opts && !strcmp(argv[i], "-read")) {
read = 1;
} else if (opts && !strcmp(argv[i], "-create")) {
create = 1;
} else if (opts && !strcmp(argv[i], "-tabbed")) {
tabbed = 1;
group = 0; /* override -group option */
} else if (opts && !strcmp(argv[i], "-untabbed")) {
tabbed = 0;
group = 0; /* override -group option */
} else if (opts && !strcmp(argv[i], "-group")) {
group = 2; /* 2: start new group, 1: in group */
} else if (opts && (!strcmp(argv[i], "-iconic") ||
!strcmp(argv[i], "-icon"))) {
iconic = 1;
copyCommandLineArg(commandLine, argv[i]);
} else if (opts && !strcmp(argv[i], "-line")) {
nextArg(argc, argv, &i);
nRead = sscanf(argv[i], "%d", &lineArg);
if (nRead != 1)
fprintf(stderr, "nc: argument to line should be a number\n");
else
lineNum = lineArg;
} else if (opts && (*argv[i] == '+')) {
nRead = sscanf((argv[i]+1), "%d", &lineArg);
if (nRead != 1)
fprintf(stderr, "nc: argument to + should be a number\n");
else
lineNum = lineArg;
} else if (opts && (!strcmp(argv[i], "-ask") || !strcmp(argv[i], "-noask"))) {
; /* Ignore resource-based arguments which are processed later */
} else if (opts && (!strcmp(argv[i], "-svrname") ||
!strcmp(argv[i], "-svrcmd"))) {
nextArg(argc, argv, &i); /* Ignore rsrc args with data */
} else if (opts && (!strcmp(argv[i], "-xrm") ||
!strcmp(argv[i], "-display"))) {
copyCommandLineArg(commandLine, argv[i]);
nextArg(argc, argv, &i); /* Ignore rsrc args with data */
copyCommandLineArg(commandLine, argv[i]);
} else if (opts && (!strcmp(argv[i], "-version") || !strcmp(argv[i], "-V"))) {
printNcVersion();
exit(EXIT_SUCCESS);
} else if (opts && (!strcmp(argv[i], "-h") ||
!strcmp(argv[i], "-help"))) {
fprintf(stderr, "%s", cmdLineHelp);
exit(EXIT_SUCCESS);
} else if (opts && (*argv[i] == '-')) {
#ifdef VMS
*argv[i] = '/';
#endif /*VMS*/
fprintf(stderr, "nc: Unrecognized option %s\n%s", argv[i],
cmdLineHelp);
exit(EXIT_FAILURE);
} else {
#ifdef VMS
int numFiles, j, oldLength;
char **nameList = NULL, *newCommandString;
/* Use VMS's LIB$FILESCAN for filename in argv[i] to process */
/* wildcards and to obtain a full VMS file specification */
numFiles = VMSFileScan(argv[i], &nameList, NULL, INCLUDE_FNF);
/* Should we warn the user if he provided a -line or -do switch
and a wildcard pattern that expands to more than one file? */
/* for each expanded file name do: */
for (j = 0; j < numFiles; ++j) {
oldLength = outPtr-commandString;
newCommandString = XtMalloc(oldLength+length+1);
strncpy(newCommandString, commandString, oldLength);
XtFree(commandString);
commandString = newCommandString;
outPtr = newCommandString + oldLength;
if (ParseFilename(nameList[j], name, path) != 0) {
/* An Error, most likely too long paths/strings given */
commandLine->serverRequest = NULL;
return;
}
strcat(path, name);
/* determine if file is to be openned in new tab, by
factoring the options -group, -tabbed & -untabbed */
if (group == 2) {
isTabbed = 0; /* start a new window for new group */
group = 1; /* next file will be within group */
}
else if (group == 1) {
isTabbed = 1; /* new tab for file in group */
}
else {
isTabbed = tabbed; /* not in group */
}
/* See below for casts */
sprintf(outPtr, "%d %d %d %d %d %ld %ld %ld %ld\n%s\n%s\n%s\n%s\n%n",
lineNum, read, create, iconic, tabbed, (long) strlen(path),
(long) strlen(toDoCommand), (long) strlen(langMode),
(long) strlen(geometry),
path, toDoCommand, langMode, geometry, &charsWritten);
outPtr += charsWritten;
free(nameList[j]);
/* Create the file open atoms for the paths supplied */
addToFileList(path);
fileCount++;
}
if (nameList != NULL)
free(nameList);
#else
if (ParseFilename(argv[i], name, path) != 0) {
/* An Error, most likely too long paths/strings given */
commandLine->serverRequest = NULL;
return;
}
strcat(path, name);
/* determine if file is to be openned in new tab, by
factoring the options -group, -tabbed & -untabbed */
if (group == 2) {
isTabbed = 0; /* start a new window for new group */
group = 1; /* next file will be within group */
}
else if (group == 1) {
isTabbed = 1; /* new tab for file in group */
}
else {
isTabbed = tabbed; /* not in group */
}
/* SunOS 4 acc or acc and/or its runtime library has a bug
such that %n fails (segv) if it follows a string in a
printf or sprintf. The silly code below avoids this.
The "long" cast on strlen() is necessary because size_t
is 64 bit on Alphas, and 32-bit on most others. There is
no printf format specifier for "size_t", thanx, ANSI. */
sprintf(outPtr, "%d %d %d %d %d %ld %ld %ld %ld\n%n", lineNum,
read, create, iconic, isTabbed, (long) strlen(path),
(long) strlen(toDoCommand), (long) strlen(langMode),
(long) strlen(geometry), &charsWritten);
outPtr += charsWritten;
strcpy(outPtr, path);
outPtr += strlen(path);
*outPtr++ = '\n';
strcpy(outPtr, toDoCommand);
outPtr += strlen(toDoCommand);
*outPtr++ = '\n';
strcpy(outPtr, langMode);
outPtr += strlen(langMode);
*outPtr++ = '\n';
strcpy(outPtr, geometry);
outPtr += strlen(geometry);
*outPtr++ = '\n';
/* Create the file open atoms for the paths supplied */
addToFileList(path);
fileCount++;
#endif /* VMS */
/* These switches only affect the next filename argument, not all */
toDoCommand = "";
lineNum = 0;
}
}
#ifdef VMS
VMSFileScanDone();
#endif /*VMS*/
/* If there's an un-written -do command, or we are to open a new window,
* or user has requested iconic state, but not provided a file name,
* create a server request with an empty file name and requested
* iconic state (and optional language mode and geometry).
*/
if (toDoCommand[0] != '\0' || fileCount == 0) {
sprintf(outPtr, "0 0 0 %d %d 0 %ld %ld %ld\n\n%n", iconic, tabbed,
(long) strlen(toDoCommand),
(long) strlen(langMode), (long) strlen(geometry), &charsWritten);
outPtr += charsWritten;
strcpy(outPtr, toDoCommand);
outPtr += strlen(toDoCommand);
*outPtr++ = '\n';
strcpy(outPtr, langMode);
outPtr += strlen(langMode);
*outPtr++ = '\n';
strcpy(outPtr, geometry);
outPtr += strlen(geometry);
*outPtr++ = '\n';
}
*outPtr = '\0';
commandLine->serverRequest = commandString;
}
static void waitUntilRequestProcessed(XtAppContext context,
Window rootWindow,
char* commandString,
Atom serverRequestAtom)
{
XtIntervalId timerId;
Boolean timeOut = False;
/* Set the NEDIT_SERVER_REQUEST_<user>_<host> property on the root
window to activate the server */
XChangeProperty(TheDisplay, rootWindow, serverRequestAtom, XA_STRING, 8,
PropModeReplace, (unsigned char *)commandString,
strlen(commandString));
/* Set up a timeout proc in case the server is dead. The standard
selection timeout is probably a good guess at how long to wait
for this style of inter-client communication as well */
timerId = XtAppAddTimeOut(context,
REQUEST_TIMEOUT,
(XtTimerCallbackProc)timeOutProc,
&timeOut);
currentWaitForAtom = serverRequestAtom;
/* Wait for the property to be deleted to know the request was processed */
while (!timeOut) {
XEvent event;
const XPropertyEvent *e = (const XPropertyEvent *)&event;
XtAppNextEvent(context, &event);
if (e->window == rootWindow &&
e->atom == serverRequestAtom &&
e->state == PropertyDelete)
break;
XtDispatchEvent(&event);
}
/* Exit if the timeout expired. */
if (timeOut) {
fprintf(stderr, "%s: The server did not respond to the request.\n", APP_NAME);
XtCloseDisplay(TheDisplay);
exit(EXIT_FAILURE);
} else {
XtRemoveTimeOut(timerId);
}
}
static void waitUntilFilesOpenedOrClosed(XtAppContext context,
Window rootWindow)
{
XtIntervalId timerId;
Boolean timeOut = False;
/* Set up a timeout proc so we don't wait forever if the server is dead.
The standard selection timeout is probably a good guess at how
long to wait for this style of inter-client communication as
well */
timerId = XtAppAddTimeOut(context, FILE_OPEN_TIMEOUT,
(XtTimerCallbackProc)timeOutProc, &timeOut);
currentWaitForAtom = noAtom;
/* Wait for all of the windows to be opened by server,
* and closed if -wait was supplied */
while (fileListHead.fileList) {
XEvent event;
const XPropertyEvent *e = (const XPropertyEvent *)&event;
XtAppNextEvent(context, &event);
/* Update the fileList and check if all files have been closed. */
if (e->type == PropertyNotify && e->window == rootWindow) {
FileListEntry *item;
if (e->state == PropertyDelete) {
for (item = fileListHead.fileList; item; item = item->next) {
if (e->atom == item->waitForFileOpenAtom) {
/* The 'waitForFileOpen' property is deleted when the file is opened */
fileListHead.waitForOpenCount--;
item->waitForFileOpenAtom = None;
/* Reset the timer while we wait for all files to be opened. */
XtRemoveTimeOut(timerId);
timerId = XtAppAddTimeOut(context, FILE_OPEN_TIMEOUT,
(XtTimerCallbackProc)timeOutProc, &timeOut);
} else if (e->atom == item->waitForFileClosedAtom) {
/* When file is opened in -wait mode the property
* is deleted when the file is closed.
*/
fileListHead.waitForCloseCount--;
item->waitForFileClosedAtom = None;
}
}
if (fileListHead.waitForOpenCount == 0 && !timeOut) {
XtRemoveTimeOut(timerId);
}
if (fileListHead.waitForOpenCount == 0 &&
fileListHead.waitForCloseCount == 0) {
break;
}
}
}
/* We are finished if we are only waiting for files to open and
** the file open timeout has expired. */
if (!Preferences.waitForClose && timeOut) {
break;
}
XtDispatchEvent(&event);
}
}
static void nextArg(int argc, char **argv, int *argIndex)
{
if (*argIndex + 1 >= argc) {
#ifdef VMS
*argv[*argIndex] = '/';
#endif /*VMS*/
fprintf(stderr, "nc: %s requires an argument\n%s",
argv[*argIndex], cmdLineHelp);
exit(EXIT_FAILURE);
}
(*argIndex)++;
}
/* Copies a given nc command line argument to the server startup command
** line (-icon, -geometry, -xrm, ...) Special characters are protected from
** the shell by escaping EVERYTHING with \
** Note that the .shell string in the command line structure is large enough
** to hold the escaped characters.
*/
static void copyCommandLineArg(CommandLine *commandLine, const char *arg)
{
const char *c;
char *outPtr = commandLine->shell + strlen(commandLine->shell);
#if defined(VMS) || defined(__EMX__)
/* Non-Unix shells don't want/need esc */
for (c=arg; *c!='\0'; c++) {
*outPtr++ = *c;
}
*outPtr++ = ' ';
*outPtr = '\0';
#else
*outPtr++ = '\'';
for (c=arg; *c!='\0'; c++) {
if (*c == '\'') {
*outPtr++ = '\'';
*outPtr++ = '\\';
}
*outPtr++ = *c;
if (*c == '\'') {
*outPtr++ = '\'';
}
}
*outPtr++ = '\'';
*outPtr++ = ' ';
*outPtr = '\0';
#endif /* VMS */
}
/* Print version of 'nc' */
static void printNcVersion(void ) {
static const char *const ncHelpText = \
"nc (NEdit) Version 5.7 (January 2017)\n\n\
Built on: %s, %s, %s\n\
Built at: %s, %s\n";
fprintf(stdout, ncHelpText,
COMPILE_OS, COMPILE_MACHINE, COMPILE_COMPILER,
__DATE__, __TIME__);
}