#include <stdio.h>
#define QUOTECHAR '\''
main (void) {
int c ;
enum state {OUTWORD, INQUOTE, INWORD} s = OUTWORD ;
while ((c=getchar()) != EOF) {
switch (c) {
case ' ':
switch (s) {
case OUTWORD: case INQUOTE:
putchar(c) ; break ;
case INWORD:
putchar(QUOTECHAR) ; putchar(c) ; s=OUTWORD ; break ;
}
break ;
case '"':
switch (s) {
case OUTWORD:
putchar(QUOTECHAR) ; s=INQUOTE ; break ;
case INQUOTE:
putchar(QUOTECHAR) ; s=OUTWORD ; break ;
case INWORD:
putchar(QUOTECHAR) ; break ;
}
break ;
case '\n':
switch (s) {
case OUTWORD: case INQUOTE:
putchar(c) ; break ;
case INWORD:
putchar(QUOTECHAR) ; putchar(c) ; s=OUTWORD ; break ;
}
break ;
case QUOTECHAR:
switch (s) {
case OUTWORD:
printf ("%s", "'\\'") ; s=INWORD ; break ;
case INQUOTE:
case INWORD:
printf ("%s", "\\'") ; break ;
}
break ;
default:
if (s == OUTWORD) { s=INWORD ; putchar(QUOTECHAR) ;}
putchar(c) ;
}
}
if ((s == INQUOTE) || (s == INWORD)) putchar(QUOTECHAR) ;
}
Pour construire la commande