File: single.c

package info (click to toggle)
the 3.1-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 4,500 kB
  • ctags: 5,421
  • sloc: ansic: 66,327; sh: 2,745; makefile: 526
file content (667 lines) | stat: -rw-r--r-- 22,217 bytes parent folder | download | duplicates (2)
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
/***********************************************************************/
/* SINGLE.C - Processing for single input mode                         */
/***********************************************************************/
/*
 * THE - The Hessling Editor. A text editor similar to VM/CMS xedit.
 * Copyright (C) 1991-2001 Mark Hessling
 *
 * This program 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 any later version.
 *
 * This program 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 this program; if not, write to:
 *
 *    The Free Software Foundation, Inc.
 *    675 Mass Ave,
 *    Cambridge, MA 02139 USA.
 *
 *
 * If you make modifications to this software that you feel increases
 * it usefulness for the rest of the community, please email the
 * changes, enhancements, bug fixes as well as any and all ideas to me.
 * This software is going to be maintained and enhanced as deemed
 * necessary by the community.
 *
 * Mark Hessling,  M.Hessling@qut.edu.au  http://www.lightlink.com/hessling/
 */

static char RCSid[] = "$Id: single.c,v 1.3 2002/06/23 04:14:16 mark Exp $";

#include <the.h>
#include <proto.h>

#ifdef THE_SINGLE_INSTANCE_ENABLED

#ifdef WIN32
#include <windows.h> 
 
#define MY_PIPE_CONNECT 0
#define MY_PIPE_READ    1
HANDLE hSemCommandCount, hPipeRead, hThread;
int pipe_state;
BOOL fPendingIO;

/***********************************************************************/
LONG PipeHandlerThread( LPVOID lpThreadData )
/***********************************************************************/
{
   LONG prev,err;
   char LoadError[256];
   HANDLE hPipe = (HANDLE)fifo_fd;
   bool first=TRUE;

   TRACE_FUNCTION("single.c:  PipeHandlerThread");

   for ( ; ; )
   {
      if ( first = FALSE )
      {
         /*
          * Not the first time through, so wait until the parent thread
          * indicates we have processed all the input from the other
          * process. Wait for the event to be signalled and then
          * disconnect us from the named pipe.
          */
         WaitForSingleObject( hPipeRead, INFINITE );
         DisconnectNamedPipe( hPipe );
      }
      first = FALSE;
      if ( ConnectNamedPipe( hPipe, NULL ) )
      {
         /*
          * We have a client connected, tell the parent thread to read the
          * named pipe.
          */
         SetEvent( hPipeRead );
         if ( ReleaseSemaphore( hSemCommandCount, 1, &prev ) == FALSE )
         {
            display_error( 0, (CHARTYPE *)"Incrementing semaphore failed.", FALSE );
            FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT), LoadError, 256, NULL ) ;
            display_error( 0, LoadError, FALSE );
         }
      }
      else
      {
         /*
          * An error occured. is it that the client disconnected ?
          */
         err = GetLastError();
         switch( err )
         {
            case ERROR_PIPE_CONNECTED:
               break;
            case ERROR_NO_DATA:
               /*
                * Client has closed their connection...disconnect our end
                */
               DisconnectNamedPipe( hPipe );
               break;
            default:
               sprintf( LoadError, "Error Number: %d", err );
               display_error( 0, (CHARTYPE *)LoadError, FALSE );
               FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT), LoadError, 256, NULL ) ;
               display_error( 0, LoadError, FALSE );
               break;
         }
      }
   }
   TRACE_RETURN();
   return 0;
}

/***********************************************************************/
int initialise_fifo( LINE *first_file_name, LINETYPE startup_line, LENGTHTYPE startup_column, bool ro )
/***********************************************************************/
{
   LINE *current_file_name;
   char buf[MAX_FILE_NAME+1];
   char LoadError[256];
   char *ronly;
   HANDLE hPipe;
   DWORD cbWritten,dwThreadID;
   int am_client = 0, rc;
   BOOL fSuccess;

   TRACE_FUNCTION("single.c:  initialise_fifo");

   sprintf( buf, "\\\\.\\pipe\\%s", fifo_name );

   /*
    * Try and open the existing pipe...
    */
   hPipe = CreateFile( buf,
                       GENERIC_WRITE,
                       0,
                       NULL,
                       OPEN_EXISTING,
                       0,
                       NULL);
   if ( hPipe != INVALID_HANDLE_VALUE )
   {
      /*
       * The pipe exists; we are the client
       */
      fifo_fd = (unsigned long)hPipe;

      /*
       * Must determine how many files are being edited. This gets sent to
       * the server to know when to release the client details
       */
      current_file_name = first_file_name;
      trec_len = 0;
      while( current_file_name != NULL )
      {
         if ( ( rc = splitpath( current_file_name->line ) ) == RC_OK )
         {
            trec_len++;
         }
         current_file_name = current_file_name->next;
      }
      if ( !WriteFile( hPipe, &trec_len, sizeof( trec_len ), &cbWritten, NULL ) )
      {
         FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT), LoadError, 256, NULL ) ;
         display_error( 0, LoadError, FALSE );
         TRACE_RETURN();
         return 1;
      }
      current_file_name = first_file_name;
      while( current_file_name != NULL )
      {
         if ( ( rc = splitpath( current_file_name->line ) ) != RC_OK )
         {
            display_error( 10, current_file_name->line, FALSE );
         }
         else
         {
            /*
             * If started with -r, set READONLY FORCE
             */
            if ( ro )
               ronly = "#readonly force";
            else
               ronly = "";
            /*
             * If line and/or column specified on command line, use
             * them to reposition file...
             */
            if ( startup_line != 0L
            ||   startup_column != 0 )
            {
               trec_len = sprintf( trec, "x %s%s#cursor goto %ld %d%s",
                                   sp_path, sp_fname,
                                   (startup_line) ? startup_line : 1,
                                   (startup_column) ? startup_column : 1,
                                   ronly );
            }
            else
            {
               trec_len = sprintf( trec, "x %s%s%s", sp_path, sp_fname, ronly );
            }
            /*
             * Write command length...
             */
            if ( !WriteFile( hPipe, &trec_len, sizeof( trec_len ), &cbWritten, NULL ) )
            {
               FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT), LoadError, 256, NULL ) ;
               display_error( 0, LoadError, FALSE );
               am_client = 1;
               break;
            }
            /*
             * Write command. Only 1 command allowed as server expects
             * a single command and then disconnetcs the client.
             */
            if ( !WriteFile( hPipe, trec, trec_len, &cbWritten, NULL ) )
            {
               FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT), LoadError, 256, NULL ) ;
               display_error( 0, LoadError, FALSE );
               am_client = 1;
               break;
            }
         }
         current_file_name = current_file_name->next;
      }
      Sleep( 1000 );  /* ??? */
      CloseHandle( hPipe );
      /*
       * We are the client, so return with 1 to enable the caller to stop
       */
      am_client = 1;
   }
   else
   {
      /*
       * The pipe needs to be created here...
       */
      hPipe = CreateNamedPipe( buf,
                               PIPE_ACCESS_INBOUND,
                               PIPE_TYPE_BYTE | PIPE_WAIT,
                               1,
                               MAX_COMMAND_LENGTH,
                               MAX_COMMAND_LENGTH,
                               0,
                               NULL );
      if ( hPipe == INVALID_HANDLE_VALUE )
      {
         display_error( 0, (CHARTYPE *)"Warning: Unable to run in single instance mode:", FALSE );
         FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT), LoadError, 256, NULL ) ;
         display_error( 0, LoadError, FALSE );
         am_client = 1;
      }
      else
      {
         fifo_fd = (unsigned long)hPipe;
         /*
          * Create a semaphore on which the parent thread will wait...
          */

         hPipeRead = CreateEvent( NULL, TRUE, FALSE, NULL );

         hSemCommandCount = CreateSemaphore( NULL, 0, 1024, NULL );
         if ( hSemCommandCount == NULL )
         {
            display_error( 0, (CHARTYPE *)"Warning: Unable to run in single instance mode:", FALSE );
            FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT), LoadError, 256, NULL ) ;
            display_error( 0, LoadError, FALSE );
            am_client = 1;
         }
         else
         {
            hThread = CreateThread( NULL,              // security attributes
                                    0,                 // initial stack size
                                    (LPTHREAD_START_ROUTINE) PipeHandlerThread,
                                    NULL,              // argument
                                    CREATE_SUSPENDED,  // creation flag
                                    &dwThreadID );     // new thread ID
            if ( !hThread )
            {
               display_error( 0, (CHARTYPE *)"Warning: Unable to run in single instance mode:", FALSE );
               FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT), LoadError, 256, NULL ) ;
               display_error( 0, LoadError, FALSE );
               am_client = 1;
            }
            else
            {
               ResumeThread( hThread );
               /*
                * We are the server, so return with 0 to enable the caller to continue...
                */
               single_instance_server = TRUE;
            }
         }
      }
   }
   TRACE_RETURN();
   return am_client;
}

/***********************************************************************/
int process_fifo_input( int key )
/***********************************************************************/
{
   int fd,num_files,i;
   HANDLE hEvents[2],hPipe;
   DWORD dwWait,cbRead;
   char LoadError[256];
   bool le_status=CURRENT_VIEW->linend_status;
   CHARTYPE le_value=CURRENT_VIEW->linend_value;
   VIEW_DETAILS *le_view;
   BOOL fSuccess;

   TRACE_FUNCTION("single.c:  process_fifo_input");

   hPipe = (HANDLE)fifo_fd;
   if ( key == -1 )
   {
      for ( ; ; )
      {
         /*
          * Add curses input and the input fifo
          */
         hEvents[0] = (HANDLE)PDC_get_input_fd();
         if ( hEvents[0] == 0 )
         {
            /*
             * We don't have a valid handle to wait on.
             */
            TRACE_RETURN();
            return key;
         }
         hEvents[1] = hSemCommandCount; /* semaphore associated with the named pipe */
   
         dwWait = WaitForMultipleObjects( 2,
                                          hEvents,
                                          FALSE,
                                          INFINITE);
    
         if ( dwWait == WAIT_FAILED )
         {
            FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT), LoadError, 256, NULL ) ;
            display_error( 0, LoadError, FALSE );
         }
         else
         {
            fd = dwWait - WAIT_OBJECT_0; /* determines which pipe */
            if ( fd == 0 )
            {
               /*
                * A key is ready...
                * We have to increment the semaphore, because we have
                * decremented it by waiting on it. Isn't this crap!
                */
               ReleaseSemaphore( hEvents[0], 1, NULL );
               TRACE_RETURN();
               return (-1);
            }
            else if ( fd == 1 )
            {
               /*
                * To get here we must be in a state to read from the pipe...
                * Read the number of files coming down the pipe...
                */
               fSuccess = ReadFile( hPipe, (LPVOID)&trec_len, sizeof(trec_len), &cbRead, NULL );
               if ( !fSuccess || cbRead == 0 )
               {
                  TRACE_RETURN();
                  return key;
               }
               num_files = (int)trec_len;
               /*
                * save view and LINEND details to reset later
                */
               le_status = CURRENT_VIEW->linend_status;
               le_value = CURRENT_VIEW->linend_value;
               le_view = CURRENT_VIEW;
               /*
                * Read each file from the pipe...
                */
               for ( i = 0; i < num_files; i++ )
               {
                  if ( !ReadFile( hPipe, (LPVOID)&trec_len, sizeof(trec_len), &cbRead, NULL ) )
                  {
                     TRACE_RETURN();
                     le_view->linend_status = le_status;
                     le_view->linend_value = le_value;
                     return key;
                  }
                  /*
                   * Sleep for 100 milliseconds to ensure the remainder of the
                   * data is in the fifo. Yuck!
                   */
                  napms( 100 );
                  if ( !ReadFile( hPipe, (LPVOID)trec, trec_len*sizeof(CHARTYPE), &cbRead, NULL ) )
                  {
                     TRACE_RETURN();
                     le_view->linend_status = le_status;
                     le_view->linend_value = le_value;
                     return key;
                  }
                  trec[trec_len] = '\0';
                  CURRENT_VIEW->linend_status = TRUE;
                  CURRENT_VIEW->linend_value = '#';
                  (void)command_line( trec, TRUE );
               }
               ResetEvent( hPipeRead );
               le_view->linend_status = le_status;
               le_view->linend_value = le_value;
               key = 0;
               break;
            }
         }
      }
   }
   TRACE_RETURN();
   return key;
}

/***********************************************************************/
void close_fifo( void )
/***********************************************************************/
{
   TRACE_FUNCTION("single.c:  close_fifo");
   TerminateThread( hThread, 0 );
   DisconnectNamedPipe( (HANDLE)fifo_fd );
   CloseHandle( (HANDLE)fifo_fd );
   CloseHandle( (HANDLE)hPipeRead );
   CloseHandle( (HANDLE)hSemCommandCount );
   TRACE_RETURN();
}

#elif defined(HAVE_SELECT) && defined(HAVE_MKFIFO)

/*
 * Most Unixen; determine where select() is
 */
# if defined(HAVE_SYS_SELECT_H)
#   include <sys/select.h>
# elif defined(HAVE_SYS_SOCKET_H)
#   include <sys/socket.h>
# elif defined(SELECT_IN_TIME_H)
#   include <time.h>
# endif

/***********************************************************************/
#ifdef HAVE_PROTO
int initialise_fifo( LINE *first_file_name, LINETYPE startup_line, LENGTHTYPE startup_column, bool ro )
#else
int initialise_fifo( first_file_name, startup_line, startup_column, ro )
LINE *first_file_name;
LINETYPE startup_line;
LENGTHTYPE startup_column;
bool ro;
#endif
/***********************************************************************/
{
   LINE *current_file_name;
   int am_client = 0, rc;
   char *ronly;

   TRACE_FUNCTION("single.c:  initialise_fifo");

   if ( file_exists( fifo_name ) )
   {
      /*
       * The FIFO exists, so we assume we are the client here...
       */
      if ( file_writable( fifo_name ) )
      {
         fifo_fd = open( fifo_name, O_WRONLY );
         if ( fifo_fd == (-1) )
         {
            display_error( 0, (CHARTYPE *)"Warning: Unable to run in single instance mode:", FALSE );
            display_error( 0, strerror( errno ), FALSE );
         }
         else
         {
            current_file_name = first_file_name;
            while( current_file_name != NULL )
            {
               if ( ( rc = splitpath( current_file_name->line ) ) != RC_OK )
               {
                  display_error( 10, current_file_name->line, FALSE );
               }
               else
               {
                  /*
                   * If started with -r, set READONLY FORCE
                   */
                  if ( ro )
                     ronly = "#readonly force";
                  else
                     ronly = "";
                  /*
                   * If line and/or column specified on command line, use
                   * them to reposition file...
                   */
                  if ( startup_line != 0L
                  ||   startup_column != 0 )
                  {
                     trec_len = sprintf( trec, "x %s%s#cursor goto %ld %d%s",
                                   sp_path, sp_fname,
                                   (startup_line) ? startup_line : 1,
                                   (startup_column) ? startup_column : 1,
                                   ronly );
                  }
                  else
                  {
                     trec_len = sprintf( trec, "x %s%s%s", sp_path, sp_fname, ronly );
                  }
                  if ( write( fifo_fd, &trec_len, sizeof( trec_len ) ) == (-1) )
                  {
                     display_error( 0, strerror( errno ), FALSE );
                  }
                  if ( write( fifo_fd, trec, trec_len ) == (-1) )
                  {
                     display_error( 0, strerror( errno ), FALSE );
                  }
               }
               current_file_name = current_file_name->next;
            }
            close( fifo_fd );
            /*
             * We are the client, so return with 1 to enable the caller to stop
             */
            am_client = 1;
         }
      }
      else
      {
         display_error( 0, (CHARTYPE *)"Warning: Unable to run in single instance mode:", FALSE );
         display_error( 0, strerror( errno ), FALSE );
      }
   }
   else
   {
      /*
       * The FIFO doesn't exists, so we assume we are the server here...
       */
      if ( mkfifo( (DEFCHAR*)fifo_name, S_IWUSR | S_IRUSR ) == (-1) )
      {
         display_error( 0, (CHARTYPE *)"Warning: Unable to run in single instance mode:", FALSE );
         display_error( 0, strerror( errno ), FALSE );
      }
      else
      {
         fifo_fd = open( (DEFCHAR*)fifo_name, O_RDWR );
         if ( fifo_fd == -1 )
         {
            display_error( 0, (CHARTYPE *)"Warning: Unable to run in single instance mode:", FALSE );
            display_error( 0, strerror( errno ), FALSE );
         }
         else
         {
            single_instance_server = TRUE;
         }
      }
      /*
       * We are the server, so return with 0 to enable the caller to continue...
       */
   }
   TRACE_RETURN();
   return am_client;
}

/***********************************************************************/
#ifdef HAVE_PROTO
int process_fifo_input( int key )
#else
int process_fifo_input( key )
int key;
#endif
/***********************************************************************/
{
   int s;
   fd_set readfds;
   int curses_fd;
   bool le_status=CURRENT_VIEW->linend_status;
   CHARTYPE le_value=CURRENT_VIEW->linend_value;
   VIEW_DETAILS *le_view;

   TRACE_FUNCTION("single.c:  process_fifo_input");

   if ( key == -1 )
   {
      /*
       *
       * Add curses input and the input fifo
       */
#ifdef XCURSES
      curses_fd = PDC_get_input_fd();
#else
      curses_fd = fileno( stdin );
#endif
      FD_ZERO ( &readfds );
      FD_SET ( curses_fd, &readfds );
      FD_SET ( fifo_fd, &readfds );
      if ( ( s = select ( FD_SETSIZE, &readfds, NULL, NULL, NULL ) ) < 0 )
      {
         TRACE_RETURN();
         return key;
      }
      if ( s == 0 ) /* no requests pending - should never happen!*/ 
      {
         TRACE_RETURN();
         return key;
      }
      if ( FD_ISSET ( fifo_fd, &readfds ) )
      {
         if ( read( fifo_fd, (char *)&trec_len, sizeof(trec_len) ) < 0 )
         {
            TRACE_RETURN();
            return key;
         }
         /*
          * Sleep for 100 milliseconds to ensure the remainder of the
          * data is in the fifo. Yuck!
          */
         napms( 100 );
         if ( read( fifo_fd, trec, trec_len*sizeof(CHARTYPE) ) < 0 )
         {
            TRACE_RETURN();
            return key;
         }
         /*
          * Save the current linend. 
          * Force LINEND to # (which is what initialise_lifo() sets).
          * Run the command
          * set LINEND back to default.
          */
         trec[trec_len] = '\0';
         le_status = CURRENT_VIEW->linend_status;
         le_value = CURRENT_VIEW->linend_value;
         le_view = CURRENT_VIEW;
         CURRENT_VIEW->linend_status = TRUE;
         CURRENT_VIEW->linend_value = '#';
         (void)command_line( trec, TRUE );
         le_view->linend_status = le_status;
         le_view->linend_value = le_value;
         key = 0;
      }
   }
   TRACE_RETURN();
   return key;
}

/***********************************************************************/
#ifdef HAVE_PROTO
void close_fifo( void )
#else
void close_fifo( )
#endif
/***********************************************************************/
{
   TRACE_FUNCTION("single.c:  close_fifo");
   close( fifo_fd );
   remove_file( fifo_name );
   TRACE_RETURN();
}
#endif

#endif