File: main.c

package info (click to toggle)
cgoban 1.9.14-18
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 3,448 kB
  • sloc: ansic: 36,037; sh: 707; makefile: 259
file content (139 lines) | stat: -rw-r--r-- 3,375 bytes parent folder | download | duplicates (5)
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
/*
 * $Source: /cvsroot/cgoban1/cgoban1/src/main.c,v $
 * $Revision: 1.2 $
 * $Date: 2000/02/26 23:03:46 $
 *
 * src/main.c, part of Complete Goban (game program)
 * Copyright � 1995-2000 William Shubert
 * See "configure.h.in" for more copyright information.
 */

#include <wms.h>
#include <wms/clp.h>
#include <wms/rnd.h>
#include <but/but.h>
#include <abut/abut.h>
#include <abut/msg.h>
#include <wms/str.h>
#include "cgoban.h"
#include "control.h"
#include "main.h"
#include "editBoard.h"
#include "crwin.h"
#include "client/setup.h"
#include "arena.h"


/**********************************************************************
 * Forward Declarations
 **********************************************************************/
static void  stealth(int exitVal);
#if  !DEBUG
static RETSIGTYPE  sigHandler();
#endif  /* !DEBUG */


int  main(int argc, char *argv[], char *envp[])  {
  Cgoban  *cg;
  int  retVal = 0;
  Control  *ctrl;
  EditBoard  *e;
  CliSetup  *client;

  cg = cgoban_create(argc, argv, envp);
  if (cg == NULL)  {
    fprintf(stderr, "cgoban: Couldn't open X display.\n");
    return(1);
  }
  if (clp_getInt(cg->clp, "arena.games") > 0)  {
    arena(cg,
	  clp_getStr(cg->clp, "arena.prog1"),
	  clp_getStr(cg->clp, "arena.prog2"),
	  clp_getInt(cg->clp, "arena.size"),
	  (float)clp_getDouble(cg->clp, "arena.komi"),
	  clp_getInt(cg->clp, "arena.handicap"));
    exit(0);
  }

#if  !DEBUG
  signal(SIGTERM, sigHandler);  /* Catch these signals gracefully. */
  signal(SIGINT, sigHandler);
  signal(SIGHUP, sigHandler);
#endif
  /*
   * We have to always catch SIGPIPE.  Otherwise the GMP will freak out
   *   on us.
   */
  signal(SIGPIPE, SIG_IGN);  /* Seems like you can't catch a SIGPIPE. */
#ifdef  DEBUG
#define  STEALTH_VALID  (!(DEBUG))
#else
#define  STEALTH_VALID  1
#endif
  if (clp_getStr(cg->clp, "edit")[0])  {
    if ((e = editBoard_create(cg, clp_getStr(cg->clp, "edit"))) == NULL)  {
      cg->env->minWindows = 0;
      retVal = 1;
    } else  {
      crwin_create(cg, e->goban->win, 4);
      control_create(cg, TRUE);
    }
  } else if (clp_getBool(cg->clp, "nngs"))  {
    if ((client = cliSetup_create(cg, cliServer_nngs)) == NULL)  {
      cg->env->minWindows = 0;
      retVal = 1;
    } else  {
      crwin_create(cg, client->login->win, 3);
      control_create(cg, TRUE);
    }
  } else if (clp_getBool(cg->clp, "igs"))  {
    if ((client = cliSetup_create(cg, cliServer_igs)) == NULL)  {
      cg->env->minWindows = 0;
      retVal = 1;
    } else  {
      crwin_create(cg, client->login->win, 3);
      control_create(cg, TRUE);
    }
  } else  {
    ctrl = control_create(cg, clp_getBool(cg->clp, "iconic"));
    crwin_create(cg, ctrl->win, 3);
  }
  if (clp_getBool(cg->clp, "stealth") && STEALTH_VALID)
    stealth(0);
  butEnv_events(cg->env);
  cgoban_destroy(cg);

  return(retVal);
}


static void  stealth(int exitVal)  {
  int pid;
#ifdef  TIOCNOTTY
  int  tty;
#endif
  
  pid = fork();
  if (pid < 0)  {
    perror("cgoban: fork() failed");
    return;
  } else if (pid > 0)
    /* Parent just exits. */
    exit(0);
  /* Go stealth (ditch our controlling tty). */
#ifdef  TIOCNOTTY
  tty = open("/dev/tty", 0);
  if (tty < 0)
    return;
  ioctl(tty, TIOCNOTTY, 0);
  close(tty);
#endif
}


#if  !DEBUG
static RETSIGTYPE  sigHandler()  {
  fprintf(stderr, "Signal caught.\n");
  abort();
}
#endif  /* !DEBUG */