File: xtel.c

package info (click to toggle)
xtel 3.1.4-4
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,988 kB
  • ctags: 4,359
  • sloc: ansic: 14,674; makefile: 3,931; sh: 41
file content (635 lines) | stat: -rw-r--r-- 16,897 bytes parent folder | download
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
/*	
 *   xtel - Emulateur MINITEL sous X11
 *
 *   Copyright (C) 1991-1994  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: xtel.c,v 1.16 1997/07/03 08:24:47 pierre Exp $";

/*
 * programme principal 
 */

#define EXTERN
#include "xtel.h"
#include <X11/Shell.h>

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

#include "patchlevel.h"
#include "bitmaps/xtel.bit"
#include "bitmaps/teleph.xbm"
#include "bitmaps/teleph_mask.xbm"

#ifndef NO_XPM
#include <X11/xpm.h>
#include "pixmaps/xtel.xpm"
#endif /* NO_XPM */

static Colormap current_cmap;
static Cursor teleph_cursor;
static Pixmap pixmap_icone = None;
static Boolean flag_execution = False;
static char *procedure_executee = NULL;
static char *copyright_string = "(C)1991-97 LECTRA SYSTEMES & Pierre Ficheux";
static XColor black, white;

/* liste des noms des boutons MINITEL */
struct definition_touche touches[] = {
                          {"videotex", "V"},
                          {"teleinfo_ascii", "A"},
                          {"teleinfo_fr", "F"},
			  {"blanc", NULL},
                          {"envoi", "A"},
			  {"retour", "B"},
			  {"repetition", "C"},
			  {"guide", "D"},
			  {"annulation",  "E"},
			  {"sommaire", "F"},
			  {"correction", "G"},
			  {"suite", "H"},
			  {"blanc", NULL},
			  {"connexion_fin", "I"},
			};

/*
 * Options (ressources) propres a XTEL
 *
 *	-serveur	*serveur		specifie la machine serveur
 *	-service	*service		specifie le service
 *	-petite		*petiteFonte		taille de la fonte
 *	-ng		*nGris			force les niveaux de gris 
 *			*commandeImpression	comme son nom l'indique
 *			*nomService		nom du service tcp
 *      -low		*lowMemory		pas de jolis boutons
 * 	-private	*privateColormap	colormap privee
 */
static Boolean defaultFalse = False;

#define XtNserveur	"serveur"
#define XtCServeur	"Serveur"

#define XtNpetiteFonte	"petiteFonte"
#define XtCPetiteFonte	"PetiteFonte"

#define XtNlowMemory	"lowMemory"
#define XtCLowMemory	"LowMemory"

#define XtNnGris	"nGris"
#define XtCNGris	"NGris"

#define XtNcommandeImpression 		"commandeImpression"
#define XtCCommandeImpression 		"CommandeImpression"

#define XtNcommandeImpressionAscii 	"commandeImpressionAscii"
#define XtCCommandeImpressionAscii 	"CommandeImpressionAscii"

#define XtNnomService			"nomService"
#define XtCNomService			"NomService"

#define XtNboutonRaccrocher		"boutonRaccrocher"
#define XtCBoutonRaccrocher		"BoutonRaccrocher"

#define XtNprivateColormap		"privateColormap"
#define XtCPrivateColormap		"PrivateColormap"

#define XtNstring1			"string1"
#define XtNstring2			"string2"
#define XtNstring3			"string3"
#define XtNstring4			"string4"
#define XtNstring5			"string5"
#define XtNstring6			"string6"
#define XtNstring7			"string7"
#define XtNstring8			"string8"
#define XtNstring9			"string9"
#define XtNerreur1			"erreur1"

static XtResource ressources[] = {
    {
	XtNserveur,
	XtCServeur,
	XtRString, sizeof(String),
	XtOffsetOf(ressources_xtel, serveur),
	XtRString, "ondee"
	},
    {
	XtNnGris,
	XtCNGris,
	XtRBoolean, sizeof(Boolean),
	XtOffsetOf(ressources_xtel, nGris),
	XtRBoolean, (XtPointer) &defaultFalse
	},
    {
	XtNpetiteFonte,
	XtCPetiteFonte,
	XtRBoolean, sizeof(Boolean),
	XtOffsetOf(ressources_xtel, petiteFonte),
	XtRBoolean, (XtPointer) &defaultFalse
	},
    {
	XtNcommandeImpression,
	XtCCommandeImpression,
	XtRString, sizeof(String),
	XtOffsetOf(ressources_xtel, commandeImpression),
	XtRString, "lpr -h %s"
	},
    {
	XtNcommandeImpressionAscii,
	XtCCommandeImpressionAscii,
	XtRString, sizeof(String),
	XtOffsetOf(ressources_xtel, commandeImpressionAscii),
	XtRString, "lpr -h %s"
	},

    {
	XtNnomService,
	XtCNomService,
	XtRString, sizeof(String),
	XtOffsetOf(ressources_xtel, nomService),
	XtRString, "xtel"
	},
    {
	XtNboutonRaccrocher,
	XtCBoutonRaccrocher,
	XtRBoolean, sizeof(Boolean),
	XtOffsetOf(ressources_xtel, boutonRaccrocher),
	XtRBoolean, (XtPointer) &defaultFalse
	},
    {
	XtNlowMemory,
	XtCLowMemory,
	XtRBoolean, sizeof(Boolean),
	XtOffsetOf(ressources_xtel, lowMemory),
	XtRBoolean, (XtPointer) &defaultFalse
	},
    {
	XtNprivateColormap,
	XtCPrivateColormap,
	XtRBoolean, sizeof(Boolean),
	XtOffsetOf(ressources_xtel, privateColormap),
	XtRBoolean, (XtPointer) &defaultFalse
	},
    {
	XtNstring1,
	XtCString,
	XtRString, sizeof(String),
	XtOffsetOf(ressources_xtel, string[0]),
	XtRString, (XtPointer) "String 0"
	},
    {
	XtNstring2,
	XtCString,
	XtRString, sizeof(String),
	XtOffsetOf(ressources_xtel, string[1]),
	XtRString, (XtPointer) "String 1"
	},
    {
	XtNstring3,
	XtCString,
	XtRString, sizeof(String),
	XtOffsetOf(ressources_xtel, string[2]),
	XtRString, (XtPointer) "String 2"
	},
    {
	XtNstring4,
	XtCString,
	XtRString, sizeof(String),
	XtOffsetOf(ressources_xtel, string[3]),
	XtRString, (XtPointer) "String 3"
	},
    {
	XtNstring5,
	XtCString,
	XtRString, sizeof(String),
	XtOffsetOf(ressources_xtel, string[4]),
	XtRString, (XtPointer) "String 4"
	},
    {
	XtNstring6,
	XtCString,
	XtRString, sizeof(String),
	XtOffsetOf(ressources_xtel, string[5]),
	XtRString, (XtPointer) "String 5"
	},
    {
	XtNstring7,
	XtCString,
	XtRString, sizeof(String),
	XtOffsetOf(ressources_xtel, string[6]),
	XtRString, (XtPointer) "String 6"
	},
    {
	XtNstring8,
	XtCString,
	XtRString, sizeof(String),
	XtOffsetOf(ressources_xtel, string[7]),
	XtRString, (XtPointer) "String 7"
	},
    {
	XtNstring9,
	XtCString,
	XtRString, sizeof(String),
	XtOffsetOf(ressources_xtel, string[8]),
	XtRString, (XtPointer) "String 8"
	},
    {
	XtNerreur1,
	XtCString,
	XtRString, sizeof(String),
	XtOffsetOf(ressources_xtel, erreur[0]),
	XtRString, (XtPointer) "Erreur 0"
	}
  };

/*
 * Sauve l'ecran
 */
void sauve_l_ecran (w, pevent, params, nb_params)
Widget w;
XEvent *pevent;
String  *params;
Cardinal nb_params;
{
    videotexSauveEcran (ecran_minitel);
}

/* 
 * fonction d'init 
 */
void init_xtel ()
{
    videotexInit (ecran_minitel);
    clavier_minitel_valide (False);
    XFlush (XtDisplay(ecran_minitel));
}

/* Handler d'erreur X */
static int xtel_X_error_handler (display, err)
Display *display;
XErrorEvent *err;
{
    char msg[80];
    
    XGetErrorText (display, err->error_code, msg, 80);
    fprintf (stderr, "\n\nXTel ===> Erreur X: %s\n\n", msg);
    fprintf (stderr, "\tVerifiez votre environnement X, si ce comportement s'avere\n");
    fprintf (stderr, "\tetre un bug de XTel, executez XTel sous debugger, posez un\n");
    fprintf (stderr, "\tpoint d'arret sur la fonction \"xtel_X_error_handler\" et\n");
    fprintf (stderr, "\tenvoyez le detail de la pile d'appels a \"pierre@rd.lectra.fr\".\n\n\tMerci et joyeuses Paques.\n\n");
    
}


#ifndef NO_XPM

/* Construction d'icone XPM, pique dans xpaint ! */
static void SetIconImage(w)
Widget w;
{
    static Pixmap	icon = None;
    Window		iconWindow;
    Screen		*screen = XtScreen(w);
    Display		*dpy = XtDisplay(w);
    XpmAttributes xpma;
	
    xpma.valuemask = XpmReturnInfos;

    /*
     **  Build the icon
     */
    iconWindow = XCreateSimpleWindow(dpy, RootWindowOfScreen(screen), 0, 0, 1, 1, 0, black_pixel, black_pixel);
    XpmCreatePixmapFromData(dpy, iconWindow, xtel_xpm, &icon, NULL, &xpma);
    if (icon) {
	XResizeWindow (dpy, iconWindow, xpma.width, xpma.height);
	XSetWindowBackgroundPixmap (dpy, iconWindow, icon);
	XtVaSetValues (w, XtNiconWindow, iconWindow, NULL);
    }
}

#endif /* NO_XPM */

void Usage ()
{
    fprintf (stderr, "Options disponibles:\n");
    fprintf (stderr, "-ng\n\tForce le mode niveaux de gris\n");
    fprintf (stderr, "-petit\n\tSelectionne la petite fonte\n");
    fprintf (stderr, "-br\n\tValide le bouton pour raccrocher\n");
    fprintf (stderr, "-service service_xtel\n\tSelectionne le nom TCP du service XTel\n");
    fprintf (stderr, "-serveur serveur_xtel\n\tSelectionne le nom du serveur XTel\n");
    fprintf (stderr, "-low\n\tPas de jolis boutons\n");
    fprintf (stderr, "--exec-procedure, -e nom_procedure\n\tExecute une procedure\n");
    fprintf (stderr, "-private\n\tForce une colormap privee\n");

    exit (1);
}

static void execute_procedure (w, proc, pevent)
Widget w;
char *proc;
XEvent *pevent;
{
    Boolean fc;
    int i;

    XtVaGetValues (w, XtNconnecte, &fc, NULL);

    if (!(fc || flag_execution)) {
	flag_execution = True;
	
	/* Composition du service */
	start_procedure ((i = get_proc_index(proc)));
	init_xtel ();
	connexion_service (procedures[i].service);
    }
}

/* Affichage/effacement du telephone de connexion */
void affiche_telephone ()
{
#ifdef LOW_MEMORY
    XDefineCursor (XtDisplay(ecran_minitel), XtWindow(ecran_minitel), teleph_cursor);
#else
    if (!rsc_xtel.lowMemory)
	XtMapWidget (teleph);
    XDefineCursor (XtDisplay(ecran_minitel), XtWindow(ecran_minitel), teleph_cursor);
#endif
}

void efface_telephone ()
{
#ifdef LOW_MEMORY
    XUndefineCursor (XtDisplay(ecran_minitel), XtWindow(ecran_minitel));
#else
    if (!rsc_xtel.lowMemory)
	XtUnmapWidget (teleph);
    XUndefineCursor (XtDisplay(ecran_minitel), XtWindow(ecran_minitel));
#endif
}

/* Creation du curseur */
void create_teleph_cursor (top)
Widget top;
{
    Pixmap shape, mask;
    Display *dpy = XtDisplay(top);

    shape = XCreatePixmapFromBitmapData (dpy, DefaultRootWindow(dpy), teleph_bits, teleph_width, teleph_height, 1, 0, 1);
    mask = XCreatePixmapFromBitmapData (dpy, DefaultRootWindow(dpy), teleph_mask_bits, teleph_mask_width, teleph_mask_height, 1, 0, 1);
    teleph_cursor = XCreatePixmapCursor (dpy, shape, mask, &black, &white, 0, 0);
}

/*
 * Programme principal
 */
main(ac, av)
int ac;
char **av;
{
    unsigned char c;
    static char l;
    char buf[256];
    struct passwd *pw;
    struct stat statb;
    Boolean flag_point_xtel = False;
    XColor exact;
    int i;
    Widget topLevel;

    if (prototype_xtel == 0) {
	if (PATCHLEVEL != 0)
	    printf ("XTEL %d.%d.%d, Emulateur MINITEL %s\n", version_xtel, revision_xtel, PATCHLEVEL, copyright_string);
	else
	    printf ("XTEL %d.%d, Emulateur MINITEL %s\n", version_xtel, revision_xtel, copyright_string);
    }
    else {
	if (PATCHLEVEL != 0)
	    printf ("XTEL %d.%d.%db%d [beta], Emulateur MINITEL %s\n", version_xtel, revision_xtel, PATCHLEVEL, prototype_xtel, copyright_string);
	else
	    printf ("XTEL %d.%db%d [beta], Emulateur MINITEL %s\n", version_xtel, revision_xtel, prototype_xtel, copyright_string);
    }

    taille_zone_enregistrement = 1000;
    mode_emulation = MODE_VIDEOTEX;

#ifdef NO_NETWORK
    unaddr.sun_family = AF_UNIX;
    strcpy (unaddr.sun_path, XTEL_UNIX_PATH);
#endif /* NO_NETWORK */

    /*
     * Allocation de la zone d'enregistrement par defaut
     */

    if ((zone_enregistrement = malloc (taille_zone_enregistrement)) == NULL) {
	perror ("malloc");
	exit (1);
    }

    XtSetLanguageProc (NULL, NULL, NULL);
    if ((topLevel = init_toolkit (&ac, av)) == NULL) {
	fprintf (stderr, "Erreur initialisation Toolkit, dommage...\n");
	exit (1);
    }

    XSetErrorHandler (xtel_X_error_handler);

    /*
     * Options annexes
     */
    i = 1;
    while (i < ac) {
	if (strcmp (av[i], "-e") * strcmp (av[i], "--exec-procedure") == 0)
	    procedure_executee = av[++i];
	else if (av[i][0] != '-') {
	    strcpy (nom_fichier_charge, av[i]);
	}
	else
	    Usage ();

	i++;
    }

    sprintf (buf, "%s-%d.%d", xtel_basename(av[0]), version_xtel, revision_xtel);
    XtVaSetValues (topLevel, XtNtitle, buf, XtNiconName, buf, NULL);

    /*
     * lecture des ressources specifiques
     */
    XtGetApplicationResources (topLevel, &rsc_xtel, ressources, XtNumber(ressources), NULL, 0);

    /* Colormap */
    if (rsc_xtel.privateColormap) {
	current_cmap = XCopyColormapAndFree (XtDisplay(topLevel), DefaultColormapOfScreen (XtScreen (topLevel)));
	XtVaSetValues (topLevel, XtNcolormap, current_cmap, NULL);
    }
    else
	current_cmap = DefaultColormapOfScreen (XtScreen (topLevel));

    /* Noir et blanc */
    if (!XAllocNamedColor (XtDisplay(topLevel), current_cmap, "white", &white, &exact)) {
	fprintf (stderr, "Erreur XAllocNamedColor white\n");
    }
    else
	white_pixel = white.pixel;

    if (!XAllocNamedColor (XtDisplay(topLevel), current_cmap, "black", &black, &exact)) {
	fprintf (stderr, "Erreur XAllocNamedColor black\n");
    }
    else
	black_pixel = black.pixel;

    /* 
     * Lecture des services disponibles 
     */
#ifdef NO_NETWORK
    if ((socket_xteld = socket (AF_UNIX, SOCK_STREAM, 0))  < 0) {
	perror ("socket");
	exit (1);
    }

    if (connect (socket_xteld, (struct sockaddr *)&unaddr, sizeof(unaddr)) < 0) {
	perror ("connect");
	exit (1);
    }
#else
    if ((socket_xteld = c_clientbyname(rsc_xtel.serveur, rsc_xtel.nomService)) < 0) {
	fprintf (stderr, "Erreur de connexion au serveur XTEL...\n");
	exit (1);
    }
#endif /* NO_NETWORK

    /* Transmet le nom d'utilisateur */
    if ((pw = getpwuid(getuid())) == NULL) {
	perror ("getpwuid");
	exit(1);
    }
    l = strlen (pw->pw_name);
    write (socket_xteld, &l, 1);
    write (socket_xteld, pw->pw_name, l);

    nb_services = 0;

    for(;;) {
	write (socket_xteld, CHAINE_COMMANDE_SERVICE_SUIVANT, 1);
	read (socket_xteld, &c, 1);

	if (c == VALEUR_REPONSE_PLUS_DE_SERVICE)
	    break;

	else { /* transmet la longueur de la chaine */
	    int l = c;

	    if (l != 0) {
		read (socket_xteld, buf, l);
		buf[l] = 0;
		definition_services[nb_services].nom_service = build_name (buf);
		entree_compose[nb_services] = definition_services[nb_services].nom_service;
		write (socket_xteld, CHAINE_COMMANDE_NOM_UUCP, 1);
		read (socket_xteld, &c, 1);
		l = c;
		definition_services[nb_services].nom_uucp = (char *) calloc (1, l+1);
		read (socket_xteld, definition_services[nb_services].nom_uucp, l);
		definition_services[nb_services].nom_uucp[l] = 0;
#ifdef DEBUG
		printf ("%d %s %s\n", nb_services, definition_services[nb_services].nom_service ,definition_services[nb_services].nom_uucp);
#endif
		nb_services++;
	    }
	    else {
#ifdef DEBUG
		fprintf (stderr, "entree %d interdite\n", nb_services);
#endif
	    }
	}
    }

    close (socket_xteld);

    /*
     * Procedures et protocoles
     */
    sprintf (buf, "%s/.xtel", getenv ("HOME"));
    flag_point_xtel = (stat (buf, &statb) == 0);

    /* Procedures */

    /* Ouvre le fichier $HOME/.xtelproc */
    if (flag_point_xtel)
	sprintf (buf, "%s/.xtel/procedures", getenv ("HOME"));
    else
	sprintf (buf, "%s/.xtelproc", getenv ("HOME"));
    nb_procedures = init_procedures (buf);

    /* Verifie la procedure a executer si il y lieu */
    if (procedure_executee && get_proc_index (procedure_executee) < 0) {
	fprintf (stderr, "procedure %s inexistante :-(\n", procedure_executee);
	exit (1);
    }

    /*
     * Protocoles de telechargement
     */
    if (flag_point_xtel)
	sprintf (buf, "%s/.xtel/protocols", getenv ("HOME"));
    else
	sprintf (buf, "%s/.xtelproto", getenv ("HOME"));
    nb_protocoles = init_protocoles (buf, &protocoles[0]);


    /* 
     * Initialisation des widgets 
     */
    if ((ecran_minitel = init_widgets (topLevel)) == NULL) {
	fprintf (stderr, "Erreur initialisation widgets\n");
	exit (1);
    }

    /* Creation du curseur telephone */
    create_teleph_cursor (topLevel);

    /*
     * Contruction de l'icone
     */
    XtVaGetValues(topLevel, XtNiconPixmap, &pixmap_icone, NULL);
    if (pixmap_icone == None) {
#ifdef NO_XPM
	XtVaSetValues (topLevel, XtNiconPixmap, XCreateBitmapFromData(XtDisplay(topLevel), XtScreen(topLevel)->root,  xtel_bits, xtel_width, xtel_height), NULL);
#else
	SetIconImage (topLevel);
#endif /* NO_XPM */
    }

    XtRealizeWidget(topLevel);

    XtAddCallback (ecran_minitel, XtNmodeCallback, (XtCallbackProc)selection_mode_emulation, (XtPointer)"A");
    XtAddCallback (ecran_minitel, XtNenregCallback, (XtCallbackProc)enregistre_caractere, (XtPointer)NULL);
    init_xtel ();

    XtAddEventHandler (topLevel, LeaveWindowMask, False, (XtEventHandler)sauve_l_ecran, NULL);
    if (procedure_executee)
	XtAddEventHandler (ecran_minitel, ExposureMask, False, (XtEventHandler)execute_procedure, (XtPointer)procedure_executee);

    if (nom_fichier_charge[0]) {
	if (!Charge ())
	    debut_lecteur ();
    }

    /* attente evenements */
    XtAppMainLoop (app_context);
 }