Annotation of rpl/src/instructions_p8.c, revision 1.6

1.1       bertrand    1: /*
                      2: ================================================================================
1.6     ! bertrand    3:   RPL/2 (R) version 4.1.6
1.5       bertrand    4:   Copyright (C) 1989-2012 Dr. BERTRAND Joël
1.1       bertrand    5: 
                      6:   This file is part of RPL/2.
                      7: 
                      8:   RPL/2 is free software; you can redistribute it and/or modify it
                      9:   under the terms of the CeCILL V2 License as published by the french
                     10:   CEA, CNRS and INRIA.
                     11:  
                     12:   RPL/2 is distributed in the hope that it will be useful, but WITHOUT
                     13:   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
                     14:   FITNESS FOR A PARTICULAR PURPOSE.  See the CeCILL V2 License
                     15:   for more details.
                     16:  
                     17:   You should have received a copy of the CeCILL License
                     18:   along with RPL/2. If not, write to info@cecill.info.
                     19: ================================================================================
                     20: */
                     21: 
                     22: 
                     23: #include "rpl-conv.h"
                     24: 
                     25: 
                     26: /*
                     27: ================================================================================
                     28:   Fonction 'poll'
                     29: ================================================================================
                     30:   Entrées : pointeur sur une structure struct_processus
                     31: --------------------------------------------------------------------------------
                     32:   Sorties :
                     33: --------------------------------------------------------------------------------
                     34:   Effets de bord : néant
                     35: ================================================================================
                     36: */
                     37: 
                     38: void
                     39: instruction_poll(struct_processus *s_etat_processus)
                     40: {
1.2       bertrand   41:    int                     erreur;
1.1       bertrand   42:    int                     ios;
                     43:    int                     timeout;
                     44: 
                     45:    logical1                drapeau;
                     46: 
                     47:    struct_liste_chainee    *l_element_courant;
                     48: 
                     49:    struct_objet            *s_objet_argument_1;
                     50:    struct_objet            *s_objet_argument_2;
                     51:    struct_objet            *s_objet_argument_3;
                     52:    struct_objet            *s_objet_resultat;
                     53: 
                     54:    struct pollfd           s_poll;
                     55: 
                     56:    unsigned char           *registre;
                     57: 
                     58:    if ((*s_etat_processus).affichage_arguments == 'Y')
                     59:    {
                     60:        printf("\n  POLL ");
                     61: 
                     62:        if ((*s_etat_processus).langue == 'F')
                     63:        {
                     64:            printf("(attente d'un événement sur un fichier ou une socket)\n\n");
                     65:        }
                     66:        else
                     67:        {
                     68:            printf("(wait for event on file or socket)\n\n");
                     69:        }
                     70: 
                     71:        if ((*s_etat_processus).langue == 'F')
                     72:        {
                     73:            printf("  Utilisation :\n\n");
                     74:        }
                     75:        else
                     76:        {
                     77:            printf("  Usage:\n\n");
                     78:        }
                     79: 
1.3       bertrand   80:        printf("    FILE { \"POLLIN\" \"POLLOUT\" } TIMEOUT POLL\n\n");
1.1       bertrand   81:        printf("    3: %s, %s\n", d_SCK, d_FCH);
                     82:        printf("    2: %s\n", d_LST);
                     83:        printf("    1: %s, %s\n", d_INT, d_REL);
                     84:        printf("->  1: %s\n", d_INT);
                     85: 
                     86:        return;
                     87:    }
                     88:    else if ((*s_etat_processus).test_instruction == 'Y')
                     89:    {
                     90:        (*s_etat_processus).nombre_arguments = -1;
                     91:        return;
                     92:    }
                     93: 
                     94:    if (test_cfsf(s_etat_processus, 31) == d_vrai)
                     95:    {
                     96:        if (empilement_pile_last(s_etat_processus, 3) == d_erreur)
                     97:        {
                     98:            return;
                     99:        }
                    100:    }
                    101: 
                    102:    if (depilement(s_etat_processus, &((*s_etat_processus).l_base_pile),
                    103:            &s_objet_argument_1) == d_erreur)
                    104:    {
                    105:        (*s_etat_processus).erreur_execution = d_ex_manque_argument;
                    106:        return;
                    107:    }
                    108: 
                    109:    if (depilement(s_etat_processus, &((*s_etat_processus).l_base_pile),
                    110:            &s_objet_argument_2) == d_erreur)
                    111:    {
1.2       bertrand  112:        liberation(s_etat_processus, s_objet_argument_1);
1.1       bertrand  113: 
                    114:        (*s_etat_processus).erreur_execution = d_ex_manque_argument;
                    115:        return;
                    116:    }
                    117: 
                    118:    if (depilement(s_etat_processus, &((*s_etat_processus).l_base_pile),
                    119:            &s_objet_argument_3) == d_erreur)
                    120:    {
1.2       bertrand  121:        liberation(s_etat_processus, s_objet_argument_1);
                    122:        liberation(s_etat_processus, s_objet_argument_2);
1.1       bertrand  123: 
                    124:        (*s_etat_processus).erreur_execution = d_ex_manque_argument;
                    125:        return;
                    126:    }
                    127: 
                    128:    if (((*s_objet_argument_3).type == SCK) ||
                    129:            ((*s_objet_argument_3).type == FCH))
                    130:    {
                    131:        if ((*s_objet_argument_3).type == SCK)
                    132:        {
                    133:            s_poll.fd = (*((struct_socket *)
1.4       bertrand  134:                    (*s_objet_argument_3).objet)).socket;
1.1       bertrand  135:        }
                    136:        else
                    137:        {
                    138:            s_poll.fd = (*((struct_fichier *) 
1.4       bertrand  139:                    (*s_objet_argument_3).objet)).descripteur;
1.1       bertrand  140:        }
                    141: 
                    142:        if ((*s_objet_argument_2).type != LST)
                    143:        {
                    144:            liberation(s_etat_processus, s_objet_argument_1);
                    145:            liberation(s_etat_processus, s_objet_argument_2);
                    146:            liberation(s_etat_processus, s_objet_argument_3);
                    147: 
                    148:            (*s_etat_processus).erreur_execution = d_ex_erreur_type_argument;
                    149:            return;
                    150:        }
                    151: 
                    152:        l_element_courant = (*s_objet_argument_2).objet;
                    153:        s_poll.events = 0;
                    154: 
                    155:        while(l_element_courant != NULL)
                    156:        {
                    157:            if ((*(*l_element_courant).donnee).type != CHN)
                    158:            {
                    159:                liberation(s_etat_processus, s_objet_argument_1);
                    160:                liberation(s_etat_processus, s_objet_argument_2);
                    161:                liberation(s_etat_processus, s_objet_argument_3);
                    162: 
                    163:                (*s_etat_processus).erreur_execution =
                    164:                        d_ex_erreur_type_argument;
                    165:                return;
                    166:            }
                    167: 
                    168:            if ((registre = conversion_majuscule((unsigned char *)
                    169:                    (*(*l_element_courant).donnee).objet)) == NULL)
                    170:            {
                    171:                (*s_etat_processus).erreur_systeme = d_es_allocation_memoire;
                    172:                return;
                    173:            }
                    174: 
                    175:            if (strcmp(registre, "POLLIN") == 0)
                    176:            {
                    177:                s_poll.events |= POLLIN;
                    178:            }
1.2       bertrand  179:            else if (strcmp(registre, "POLLOUT") == 0)
1.1       bertrand  180:            {
                    181:                s_poll.events |= POLLOUT;
                    182:            }
                    183:            else
                    184:            {
                    185:                free(registre);
                    186: 
                    187:                liberation(s_etat_processus, s_objet_argument_1);
                    188:                liberation(s_etat_processus, s_objet_argument_2);
                    189:                liberation(s_etat_processus, s_objet_argument_3);
                    190: 
                    191:                (*s_etat_processus).erreur_execution =
                    192:                        d_ex_erreur_parametre_fichier;
                    193:                return;
                    194:            }
                    195: 
                    196:            free(registre);
                    197:            l_element_courant = (*l_element_courant).suivant;
                    198:        }
                    199: 
                    200:        s_poll.revents = 0;
                    201: 
                    202:        if ((*s_objet_argument_1).type == INT)
                    203:        {
                    204:            timeout = (*((integer8 *) (*s_objet_argument_1).objet)) * 1000L;
                    205:        }
                    206:        else if ((*s_objet_argument_1).type == REL)
                    207:        {
                    208:            timeout = (int) ((*((real8 *) (*s_objet_argument_1).objet))
                    209:                    * 1000L);
                    210:        }
                    211:        else
                    212:        {
                    213:            liberation(s_etat_processus, s_objet_argument_1);
                    214:            liberation(s_etat_processus, s_objet_argument_2);
                    215:            liberation(s_etat_processus, s_objet_argument_3);
                    216: 
                    217:            (*s_etat_processus).erreur_execution =
                    218:                    d_ex_erreur_type_argument;
                    219:            return;
                    220:        }
                    221: 
                    222:        do
                    223:        {
                    224:            drapeau = d_vrai;
                    225: 
                    226: #          ifndef SEMAPHORES_NOMMES
                    227:            if (sem_post(&((*s_etat_processus).semaphore_fork)) != 0)
                    228: #          else
                    229:            if (sem_post((*s_etat_processus).semaphore_fork) != 0)
                    230: #          endif
                    231:            {
                    232:                (*s_etat_processus).erreur_systeme = d_es_processus;
                    233:                return;
                    234:            }
                    235: 
                    236:            if ((ios = poll(&s_poll, 1, timeout)) < 0)
                    237:            {
                    238:                erreur = errno;
                    239: 
                    240: #              ifndef SEMAPHORES_NOMMES
                    241:                while(sem_wait(&((*s_etat_processus)
                    242:                        .semaphore_fork)) != 0)
                    243: #              else
                    244:                while(sem_wait((*s_etat_processus).semaphore_fork) != 0)
                    245: #              endif
                    246: 
                    247:                if (erreur != EINTR)
                    248:                {
                    249:                    liberation(s_etat_processus, s_objet_argument_1);
                    250:                    liberation(s_etat_processus, s_objet_argument_2);
                    251:                    liberation(s_etat_processus, s_objet_argument_3);
                    252: 
                    253:                    (*s_etat_processus).erreur_execution =
                    254:                            d_ex_erreur_acces_fichier;
                    255:                    return;
                    256:                }
                    257: 
                    258:                scrutation_injection(s_etat_processus);
                    259: 
                    260:                if ((*s_etat_processus).var_volatile_requete_arret != 0)
                    261:                {
                    262:                    drapeau = d_vrai;
                    263:                }
                    264:                else
                    265:                {
                    266:                    drapeau = d_faux;
                    267:                }
                    268:            }
                    269:            else
                    270:            {
                    271: #              ifndef SEMAPHORES_NOMMES
                    272:                    while(sem_wait(&((*s_etat_processus)
                    273:                            .semaphore_fork)) != 0)
                    274: #              else
                    275:                    while(sem_wait((*s_etat_processus).semaphore_fork) != 0)
                    276: #              endif
                    277:                {
                    278:                    if (errno != EINTR)
                    279:                    {
                    280:                        (*s_etat_processus).erreur_systeme =
                    281:                                d_es_processus;
                    282:                        return;
                    283:                    }
                    284:                }
                    285:            }
                    286:        } while(drapeau == d_faux);
                    287: 
1.4       bertrand  288:        if ((s_objet_resultat = allocation(s_etat_processus, INT)) == NULL)
                    289:        {
                    290:            (*s_etat_processus).erreur_systeme = d_es_allocation_memoire;
                    291:            return;
                    292:        }
                    293: 
1.1       bertrand  294:        if (ios > 0)
                    295:        {
                    296:            // Sortie sur un événement
                    297:            (*((integer8 *) (*s_objet_resultat).objet)) = -1;
                    298:        }
                    299:        else
                    300:        {
                    301:            // Sortie sur timeout
                    302:            (*((integer8 *) (*s_objet_resultat).objet)) = 0;
                    303:        }
                    304: 
                    305:        liberation(s_etat_processus, s_objet_argument_1);
                    306:        liberation(s_etat_processus, s_objet_argument_2);
                    307:        liberation(s_etat_processus, s_objet_argument_3);
                    308: 
                    309:        if (empilement(s_etat_processus, &((*s_etat_processus).l_base_pile),
                    310:                s_objet_resultat) == d_erreur)
                    311:        {
                    312:            return;
                    313:        }
                    314:    }
                    315:    else
                    316:    {
                    317:        liberation(s_etat_processus, s_objet_argument_1);
                    318:        liberation(s_etat_processus, s_objet_argument_2);
                    319:        liberation(s_etat_processus, s_objet_argument_3);
                    320: 
                    321:        (*s_etat_processus).erreur_execution = d_ex_erreur_type_argument;
                    322:        return;
                    323:    }
                    324: 
                    325:    return;
                    326: }
                    327: 
                    328: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>