File: ligne.c

package info (click to toggle)
xtel 3.3.0-9
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,936 kB
  • ctags: 1,677
  • sloc: ansic: 15,677; sh: 225; makefile: 65
file content (305 lines) | stat: -rw-r--r-- 7,852 bytes parent folder | download | duplicates (9)
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
/*	
 *   xtel - Emulateur MINITEL sous X11
 *
 *   Copyright (C) 1991-1996  Lectra Systemes & Pierre Ficheux
 *
 *   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
 *   (at your option) 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.
 */
static char rcsid[] = "$Id: ligne.c,v 1.11 2001/02/11 00:01:34 pierre Exp $";

/*
 * fonctions de traitement de la ligne MODEM
 */

#include "xtel.h"
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>

#include "Timer.h"

static char erreur, compte_erreur, message_erreur[80];
static char buf_temps_maxi[4];
static int temps_maxi;
static XtIntervalId bip_id;

/* Signale la deconnexion dans 30 s */
static XtTimerCallbackProc bip_bip (w, id)
Widget w;
XtIntervalId *id;
{
  XKeyboardControl kb;
  XKeyboardState kbs;
  Display *display = XtDisplay(ecran_minitel);
  int pitch[3]    = {200, 400, 500};
  int duration[3] = { 50, 100, 100}, i;

  XGetKeyboardControl (display, &kbs);

  for (i = 0; i < 3; i++) {
	kb.bell_pitch = pitch[i];
	kb.bell_duration = duration[i];
	XChangeKeyboardControl(display, KBBellPitch | KBBellDuration, &kb);
  
	XBell(display, 100);
	XFlush(display);
	/* Pas de usleep() sur HP/UX < 10.10 ??? */
#ifdef hpux
	sleep (1);
#else
	usleep(100000L);
#endif
  }

  kb.bell_pitch = kbs.bell_pitch;
  kb.bell_duration = kbs.bell_duration;

  XChangeKeyboardControl (display, KBBellPitch | KBBellDuration, &kb);
}

/* Pour quitter... */
void ce_n_est_qu_un_au_revoir ()
{
  if (mode_emulation != MODE_VIDEOTEX)
    zigouille_xterm_teleinfo ();

  XFlush (XtDisplay(ecran_minitel));
  printf ("Bye...\n");
  exit (0);
}

/* 
 * Enregistre un caractere dans la zone d'enregistrement 
 */
void enregistre_caractere (w, client_data, s)
Widget w;
XtPointer client_data;
char *s;
{
    if (flag_enregistrement) {
	/*
	 * Si il ne reste plue de place dans la zone 
	 * d'enregistrement, alloue 1000 octets de +
	 */
	if (cpt_buffer && !(cpt_buffer % 1000)) {
	    taille_zone_enregistrement += 1000;
	    zone_enregistrement = realloc (zone_enregistrement, taille_zone_enregistrement);
	}
	
	*(zone_enregistrement+cpt_buffer) = *s;
	cpt_buffer++;
    }
}

/*
 * fonction de lecture de la ligne
 */

/* ARGSUSED */
void fonction_lecture_ligne(client_data, fid, id)
XtPointer client_data;	/* non utilise */
int *fid;
XtInputId *id;
{
    unsigned char c;
    Boolean flag_connexion;
    int n;

    XtVaGetValues (ecran_minitel, XtNconnecte, &flag_connexion, NULL);

    n = read (*fid, &c, 1);

    if (flag_connexion) {
	/* Deconnexion supposee */
	if (n <= 0 || c == VALEUR_REPONSE_DECONNEXION) {
	    XtVaSetValues (ecran_minitel, XtNconnecte, False, NULL);
#ifndef LOW_MEMORY
	    if (!low_memory) {
		timerResetTimer (timer_minitel);
		XtVaSetValues (timer_minitel, XtNtimerMode, False, NULL);
	    }
#endif
	    flag_connexion = False;
	    
	    /* invalide les boutons */
	    clavier_minitel_valide (False);
	    
	    /* valide la composition et le lecteur */
	    composition_valide (True);
	    
	    if (nom_fichier_charge[0] != 0)
		lecteur_valide (True);
	    
	    XFlush (XtDisplay(ecran_minitel));
	    
	    XtVaSetValues (ecran_minitel, XtNfdConnexion, -1, NULL);
	    XtRemoveInput (input_id);

	    if (bip_id) {
		XtRemoveTimeOut (bip_id);
		bip_id = 0;
	    }

	    close (socket_xteld);
	    if (sortie_violente)
		ce_n_est_qu_un_au_revoir ();
	}
	else {
	    if (nb_procedures)
		run_procedure (c, socket_xteld);

	    enregistre_caractere (ecran_minitel, NULL, &c);
	    efface_telephone ();

	    /* decodage */
	    if (mode_emulation != MODE_VIDEOTEX) {
	      int retour_videotex = test_retour_videotex ( c,1) ;

	      if ( ( retour_videotex == 1 ) || ( retour_videotex == 2 ) ) {
		    selection_mode_emulation (ecran_minitel, "V", NULL);
		    return;
	      } else {
		char s[8];
		int n;
		if (mode_emulation == MODE_TELEINFO_FR)
		  c = conversion_teleinfo_fr (c,1);
		n = snprintf (s, sizeof(s), "%lc", (unsigned char) c);
		if (n != -1)
		  write (fd_teleinfo, s, n);
	      }
	    } else {
	      videotexDecode (ecran_minitel, c);
	    }
	}
    } 
    /* Non connecte */
    else { 
	if (erreur) {
	    if (c == VALEUR_REPONSE_FIN_ERREUR) {

		erreur = 0;

		if (!flag_connexion) {
		    /* efface le petit telephone */
		    efface_telephone ();

		    /* valide la composition et le lecteur */
		    composition_valide (True);
		    if (nom_fichier_charge[0] != 0)
			lecteur_valide (True);

		    XtVaSetValues (ecran_minitel, XtNfdConnexion, -1, NULL);
		    XtRemoveInput (input_id);	
		    close (socket_xteld);	    
		}

		affiche_erreur (message_erreur, message_erreur[compte_erreur-1]);
	    }
	    else
		message_erreur[compte_erreur++] = c;
	}
	else {
	    if (c == VALEUR_REPONSE_CONNEXION) {
		XtVaSetValues (ecran_minitel, XtNconnecte, True, NULL);
#ifndef LOW_MEMORY
		if (!low_memory) {
		  if (temps_maxi > 0) {
		    XtVaSetValues (timer_minitel, XtNtimerMode, True, XtNcountDown, True, XtNmin, temps_maxi/60, XtNsec, temps_maxi%60, NULL);
		    temps_maxi = 0;
		  }
		  else
		    XtVaSetValues (timer_minitel, XtNtimerMode, True, XtNcountDown, False, XtNmin, 0, XtNsec, 0, NULL);
		  
		    timerResetTimer (timer_minitel);
		    timerStartTimer (timer_minitel);
		}
#endif
		efface_telephone ();
		flag_connexion = True;
	    
		/* valide les boutons MINITEL */
		clavier_minitel_valide (True);
		XFlush (XtDisplay(ecran_minitel));
	    }
	    else if (c == VALEUR_REPONSE_DEBUT_ERREUR) {
		erreur = 1;
		compte_erreur = 0;
	    }
	    /* VALEUR_TEMPS_MAXI, puis longueur + temps_maxi */
	    else if (c == VALEUR_TEMPS_MAXI) {
	      read (*fid, &c, 1);
	      read (*fid, buf_temps_maxi, c);
	      buf_temps_maxi[c] = 0;
	      temps_maxi = atoi(buf_temps_maxi);
	      if (temps_maxi > 30)
		bip_id = XtAppAddTimeOut (XtWidgetToApplicationContext(ecran_minitel), (unsigned long) (temps_maxi - 30) * 1000, (XtTimerCallbackProc) bip_bip, NULL);

	    }
	}
    }
}

/* 
 * Connexion a un service 
 */
void connexion_service (service)
char *service;
{
    char l;
    struct passwd *pw;

#ifdef DEBUG
    printf ("connexion au service %s\n", service);
#endif

    /* affiche le petit telephone */
    affiche_telephone ();

    /* invalide la composition et le lecteur */
    composition_valide (False);
    lecteur_valide (False);

    /* Ouvre la socket serveur */
    if ((socket_xteld = c_clientbyname(rsc_xtel.serveur, rsc_xtel.nomService)) < 0) {
      fprintf (stderr, "Erreur de connexion au serveur XTEL...\n");
      return;
    }

    /* Transmet le nom d'utilisateur */
    if ((pw = getpwuid(getuid())) == NULL) {
	perror ("getpwuid");
	return;
    }

    l = strlen (pw->pw_name);
    write (socket_xteld, &l, 1);
    write (socket_xteld, pw->pw_name, l);

    /* nouvelle entree = socket XTELD */
    XtVaSetValues (ecran_minitel, XtNfdConnexion, socket_xteld, NULL);
    input_id = XtAppAddInput(app_context, socket_xteld, (XtPointer)XtInputReadMask, (XtInputCallbackProc)fonction_lecture_ligne, NULL);

    /* commande de connexion a XTELD */
    if (!service) {
	write (socket_xteld, CHAINE_COMMANDE_CONNEXION_M1, 1);
    }
    else {
	write (socket_xteld, CHAINE_COMMANDE_DEMANDE_CONNEXION, 1);
	l = strlen (service);
	write (socket_xteld, &l, 1);
	write (socket_xteld, service, l);
    }
}