• [^] # Le script

    Posté par . En réponse au message De l'usage des #include. Évalué à 0.

    Voici le script, même si c'est un peu "goret" :


    #!/bin/bash
    #
    # check_headers.sh - Check that the headers are mandatory in your C sources.
    #
    # Conventions :
    # #include <stdlib.h> /* NULL */
    # #include <stdio.h> /* fread(), fwrite() */
    #
    # Then, it will check that functions fread() and fwrite(), and macro NULL
    # are indeed used in the current source file.
    # Note that functions/macros are separated using a comma.
    #
    # Author : Jean-Romain "Lockness" PAC <L0ckness@yahoo.fr>
    # Date : February, 8th, 2005
    # License : GPLv2 (see http://www.gnu.org/copyleft/gpl.html(...) for more details)
    #

    usage()
    {
    echo "Usage: 0ドル FILE"
    exit 1
    }

    if [ $# != 1 ]
    then
    usage
    fi

    if [ ! -f "1ドル" ]
    then
    echo "0ドル: unable to open \"1ドル\""
    exit 1
    fi

    LIST_HEADERS=$(grep "#include[\ ]*[<|\"].*\.h[>|\"].*" "1ドル")
    NB_LINES=$(echo "$LIST_HEADERS" | wc -l)

    TOKENS=""
    for NUMLINE in `seq $NB_LINES`
    do
    LINE=$(echo "$LIST_HEADERS" | head -$NUMLINE | tail -1)
    LINETOKENS="$LINE"

    # Removing left side
    LINETOKENS="$(echo "$LINETOKENS" | sed 's/.*\/\*[ ]*//g')"

    # Removing right side
    LINETOKENS=`echo "$LINETOKENS" | sed 's/[ ]*\*\/.*//g'`

    # Removing blank spaces
    LINETOKENS=`echo "$LINETOKENS" | sed 's/[ ]*//g'`

    # Removing left bracket (not to have pb with arguments after for matching)
    LINETOKENS=`echo "$LINETOKENS" | sed 's/(//g'`

    # Removing right bracket (not to have pb with arguments after for matching)
    LINETOKENS=`echo "$LINETOKENS" | sed 's/)//g'`

    NEWTOKENS=$(echo "$LINETOKENS" | tr "," "\n")
    TOKENS=$(echo -e "$TOKENS\n$NEWTOKENS")
    done


    for SEARCHED_TOKEN in `echo "$TOKENS"`
    do
    NB_EVENTS=$(grep "$SEARCHED_TOKEN" "1ドル" | wc -l)

    # Sure that there is a least one token (this of the include line)
    if [ ! $NB_EVENTS -gt 1 ]
    then
    echo "Token [$SEARCHED_TOKEN] not found in the file."
    fi
    done

    exit 0


    Si on teste sur ce fichier :

    #include <stdio.h> /* fread(), fwrite() */
    #include <stdlib.h> /* NULL */
    #include <lib_pas_necessaire.h> /* MACRO_PAS_UTILISEE */

    int main(void)
    {
    int a=5;
    int *b = NULL;

    fread(bidon);

    return (0);
    }


    On obtient :

    lockness@enzo:~/labo/include $ ./check_headers.sh source.c
    Token [fwrite] not found in the file.
    Token [MACRO_PAS_UTILISEE] not found in the file.


    Donc ça marche plutôt bien :)

    Si vous voulez améliorer le script (notamment pour dire si un include est complètement inutile (vérifier qu'aucun des tokens n'est présent dans le fichier)), libre à vous !