This contains my bachelors thesis and associated tex files, code snippets and maybe more. Topic: Data Movement in Heterogeneous Memories with Intel Data Streaming Accelerator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

389 lines
11 KiB

  1. /* Notes */ /*{{{C}}}*//*{{{*/
  2. /*
  3. This program is GNU software, copyright 1997-2004
  4. Michael Haardt <michael@moria.de>.
  5. This program is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2 of the License, or (at your
  8. option) any later version.
  9. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program. If not, write to the Free Software Foundation, Inc.,
  15. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. /*}}}*/
  18. /* #includes */ /*{{{*/
  19. #undef _POSIX_SOURCE
  20. #define _POSIX_SOURCE 1
  21. #undef _POSIX_C_SOURCE
  22. #define _POSIX_C_SOURCE 2
  23. #include "config.h"
  24. #include <sys/types.h>
  25. #include <assert.h>
  26. #include <ctype.h>
  27. #include <errno.h>
  28. #include <limits.h>
  29. #include <locale.h>
  30. #ifdef HAVE_GETTEXT
  31. #include <libintl.h>
  32. #define _(String) gettext(String)
  33. #else
  34. #define _(String) String
  35. #endif
  36. #include <regex.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <unistd.h>
  41. #include "getopt.h"
  42. #include "misc.h"
  43. #include "sentence.h"
  44. /*}}}*/
  45. /* types */ /*{{{*/
  46. struct badPhrase
  47. {
  48. char *phrase;
  49. regex_t phrase_r;
  50. char *suggest;
  51. };
  52. /*}}}*/
  53. static int doubleWords=1;
  54. static char phraseLanguage[32];
  55. static struct badPhrase *badPhrases=(struct badPhrase *)0;
  56. static int badPhraseCapacity=0;
  57. static int badPhraseSize=0;
  58. static int sentences,hits;
  59. static void loadPhrases(const char *file) /*{{{*/
  60. {
  61. FILE *fp;
  62. char ln[1024];
  63. char *tab;
  64. size_t l;
  65. int fix,j;
  66. if ((fp=fopen(file,"r"))==(FILE*)0)
  67. {
  68. fprintf(stderr,_("diction: Opening `%s' failed (%s).\n"),file,strerror(errno));
  69. exit(1);
  70. }
  71. while (fgets(ln,sizeof(ln),fp))
  72. {
  73. l=strlen(ln);
  74. if (l && ln[l-1]=='\n') ln[--l]='\0';
  75. if (ln[0])
  76. {
  77. int err;
  78. if (badPhraseSize==badPhraseCapacity) /* enlarge capacity */ /*{{{*/
  79. {
  80. if ((badPhrases=realloc(badPhrases,(badPhraseCapacity=3*(badPhraseCapacity+32))*sizeof(struct badPhrase)))==(struct badPhrase*)0)
  81. {
  82. fprintf(stderr,_("diction: out of memory.\n"));
  83. exit(2);
  84. }
  85. }
  86. /*}}}*/
  87. if ((tab=strchr(ln,'\t')))
  88. {
  89. *tab='\0';
  90. ++tab;
  91. }
  92. if ((badPhrases[badPhraseSize].phrase=malloc(strlen(ln)+1))==(char*)0)
  93. {
  94. fprintf(stderr,_("diction: out of memory.\n"));
  95. exit(2);
  96. }
  97. strcpy(badPhrases[badPhraseSize].phrase,ln);
  98. #if 0
  99. if ((err=regcomp(&badPhrases[badPhraseSize].phrase_r,ln,REG_EXTENDED))!=0)
  100. {
  101. char errmsg[1024];
  102. regerror(err,&badPhrases[badPhraseSize].phrase_r,errmsg,sizeof(errmsg));
  103. fprintf(stderr,_("diction: Compiling regular expression `%s' failed (%s).\n"),ln,errmsg);
  104. exit(2);
  105. }
  106. #endif
  107. if (tab)
  108. {
  109. if ((badPhrases[badPhraseSize].suggest=malloc(strlen(tab)+1))==(char*)0)
  110. {
  111. fprintf(stderr,_("diction: out of memory.\n"));
  112. exit(2);
  113. }
  114. strcpy(badPhrases[badPhraseSize].suggest,tab);
  115. }
  116. else badPhrases[badPhraseSize].suggest=(char*)0;
  117. ++badPhraseSize;
  118. }
  119. }
  120. /* resolve =phrase explainations */ /*{{{*/
  121. for (fix=0; fix<badPhraseSize; ++fix)
  122. {
  123. if (badPhrases[fix].suggest && *badPhrases[fix].suggest=='=')
  124. {
  125. for (j=0; j<badPhraseSize; ++j)
  126. {
  127. if (j!=fix && strcmp(badPhrases[j].phrase,badPhrases[fix].suggest+1)==0)
  128. {
  129. free(badPhrases[fix].suggest);
  130. badPhrases[fix].suggest=badPhrases[j].suggest;
  131. break;
  132. }
  133. if (j==badPhraseSize)
  134. {
  135. fprintf(stderr,"diction: Warning: Unable to resolve %s.\n",badPhrases[fix].suggest);
  136. }
  137. }
  138. }
  139. }
  140. /*}}}*/
  141. }
  142. /*}}}*/
  143. static void diction(const char *sent, size_t length, const char *file, int line) /*{{{*/
  144. {
  145. const char *lastout=sent;
  146. const char *s=sent;
  147. const char *end=sent+length;
  148. const char *lastWord=(const char*)0;
  149. int j;
  150. if (length==0) return;
  151. while (s<end)
  152. {
  153. /* check for bad phrase */ /*{{{*/
  154. for (j=0; j<badPhraseSize; ++j)
  155. {
  156. const struct badPhrase *bp;
  157. const char *badword,*str;
  158. bp=&badPhrases[j];
  159. badword=bp->phrase;
  160. if (*badword==' ') /* beginning of sentence or word */
  161. {
  162. if (s>sent && isalpha(*(s-1))) continue;
  163. ++badword;
  164. }
  165. str=s;
  166. while ((*badword==tolower(*str) || *badword==*str) && *badword && *str) { ++badword; ++str; }
  167. if ((*badword=='\0' && !isalpha(*str)) || (*badword=='~' && isalpha(*str)))
  168. {
  169. if (bp->suggest && *bp->suggest!='!')
  170. {
  171. ++hits;
  172. if (lastout==sent) printf("%s:%d: ",file,line);
  173. while (lastout<s) putc(*lastout++,stdout);
  174. putc('[',stdout);
  175. while (lastout<str) putc(*lastout++,stdout);
  176. if (bp->suggest)
  177. {
  178. putc(' ',stdout);
  179. putc('-',stdout);
  180. putc('>',stdout);
  181. putc(' ',stdout);
  182. fputs(bp->suggest,stdout);
  183. }
  184. putc(']',stdout);
  185. }
  186. s=str-1;
  187. break;
  188. }
  189. }
  190. /*}}}*/
  191. /* check for double words */ /*{{{*/
  192. if (doubleWords)
  193. {
  194. const char *badword,*str;
  195. if (s>sent && !isalpha(*(s-1)))
  196. {
  197. /* move back to end of last word */
  198. badword=s-1;
  199. while (badword>=sent && !isalpha(*badword)) --badword;
  200. if (badword>sent)
  201. {
  202. /* move back to begin of last word */
  203. while (badword>=sent && isalpha(*badword)) --badword;
  204. if (!isalpha(*badword)) ++badword;
  205. str=s;
  206. while (*badword==*str && badword<s && isalpha(*str)) { ++badword; ++str; }
  207. if (badword<s && !isalpha(*badword) && !isalpha(*str))
  208. {
  209. if (lastout==sent) printf("%s:%d: ",file,line);
  210. while (lastout<s) putc(*lastout++,stdout);
  211. putc('[',stdout);
  212. while (lastout<str) putc(*lastout++,stdout);
  213. putc(' ',stdout);
  214. putc('-',stdout);
  215. putc('>',stdout);
  216. putc(' ',stdout);
  217. fputs(_("Double word."),stdout);
  218. putc(']',stdout);
  219. lastWord=s;
  220. s=str-1;
  221. }
  222. }
  223. }
  224. }
  225. /*}}}*/
  226. ++s;
  227. }
  228. ++sentences;
  229. if (lastout!=sent)
  230. {
  231. while (lastout<end) putc(*lastout++,stdout);
  232. putc('\n',stdout);
  233. putc('\n',stdout);
  234. }
  235. }
  236. /*}}}*/
  237. static void print_usage(FILE *handle) /*{{{*/
  238. {
  239. fputs(_("\
  240. Usage: diction [-d] [-f file [-n|-L language]] [file ...]\n\
  241. diction [--ignore-double-words]\n\
  242. [--file file [--no-default-file|--language]] [file ...]\n\
  243. diction --version\n"),handle);
  244. }
  245. /*}}}*/
  246. int main(int argc, char *argv[]) /*{{{*/
  247. {
  248. int usage=0,c;
  249. char *userPhrases=(char*)0,*e;
  250. char defaultPhrases[_POSIX_PATH_MAX];
  251. static struct option lopts[]=
  252. {
  253. { "ignore-double-words", no_argument, 0, 'd' },
  254. { "file", required_argument, 0, 'f' },
  255. { "help", no_argument, 0, 'h' },
  256. { "version", no_argument, 0, 'v' },
  257. { "language", required_argument, 0, 'L' },
  258. { "no-default-file", no_argument, 0, 'n' },
  259. { (const char*)0, 0, 0, '\0' }
  260. };
  261. /* init locale */ /*{{{*/
  262. setlocale(LC_ALL,"");
  263. #ifdef HAVE_GETTEXT
  264. bindtextdomain("diction",LOCALEDIR);
  265. textdomain("diction");
  266. #endif
  267. e=getenv("LC_MESSAGES");
  268. if (e==(char*)0) e=getenv("LC_ALL");
  269. if (e==(char*)0) e=getenv("LANG");
  270. if (e)
  271. {
  272. strncpy(phraseLanguage,e,sizeof(phraseLanguage)-1);
  273. phraseLanguage[sizeof(phraseLanguage)-1]='\0';
  274. if (strstr(phraseLanguage,".."))
  275. {
  276. fprintf(stderr,_("diction: Invalid string `..' in default phrase language `%s'.\n"),phraseLanguage);
  277. exit(2);
  278. }
  279. else
  280. {
  281. snprintf(defaultPhrases,sizeof(defaultPhrases),SHAREDIR "/diction/%s",e);
  282. if (access(defaultPhrases,R_OK)!=0)
  283. {
  284. phraseLanguage[5]='\0';
  285. snprintf(defaultPhrases,sizeof(defaultPhrases),SHAREDIR "/diction/%s",phraseLanguage);
  286. if (access(defaultPhrases,R_OK)!=0)
  287. {
  288. phraseLanguage[2]='\0';
  289. snprintf(defaultPhrases,sizeof(defaultPhrases),SHAREDIR "/diction/%s",phraseLanguage);
  290. if (access(defaultPhrases,R_OK)!=0)
  291. {
  292. strcpy(phraseLanguage,"C");
  293. }
  294. }
  295. }
  296. }
  297. }
  298. else strcpy(phraseLanguage,"C");
  299. /*}}}*/
  300. /* parse options */ /*{{{*/
  301. strcpy(defaultPhrases,SHAREDIR "/diction/");
  302. while ((c=getopt_long(argc,argv,"df:nL:h",lopts,(int*)0))!=EOF) switch(c)
  303. {
  304. case 'd': doubleWords=0; break;
  305. case 'f': userPhrases=optarg; break;
  306. case 'n': defaultPhrases[0]='\0'; break;
  307. case 'L': strncpy(phraseLanguage,optarg,sizeof(phraseLanguage)-1); phraseLanguage[sizeof(phraseLanguage)-1]='\0'; break;
  308. case 'v': printf("GNU diction " VERSION "\n"); exit(0);
  309. case 'h': usage=2; break;
  310. default: usage=1; break;
  311. }
  312. if (defaultPhrases[0]) strcat(defaultPhrases,phraseLanguage);
  313. if (usage==1 || (userPhrases==(char*)0 && defaultPhrases[0]=='\0'))
  314. {
  315. print_usage(stderr);
  316. fputs("\n",stderr);
  317. fputs(_("Try `diction -h' or `diction --help' for more information.\n"),stderr);
  318. exit(1);
  319. }
  320. if (usage==2)
  321. {
  322. print_usage(stdout);
  323. fputs("\n",stdout);
  324. fputs(_("\
  325. Print wordy and commonly misused phrases in sentences.\n\n\
  326. -d, --ignore-double-words do not complain about double words\n\
  327. -f, --file also read the specified database\n\
  328. -n, --no-default-file do not read the default phrase file\n\
  329. -L, --language set document language\n\
  330. -h, --help print this message\n\
  331. --version print the version\n"),stdout);
  332. fputs("\n",stdout);
  333. fputs(_("Report bugs to <michael@moria.de>.\n"),stdout);
  334. exit(0);
  335. }
  336. /*}}}*/
  337. if (defaultPhrases[0]) loadPhrases(defaultPhrases);
  338. if (userPhrases) loadPhrases(userPhrases);
  339. sentences=0;
  340. hits=0;
  341. if (optind==argc) sentence("diction",stdin,"(stdin)",diction,phraseLanguage);
  342. else while (optind<argc)
  343. {
  344. FILE *fp;
  345. if ((fp=fopen(argv[optind],"r"))==(FILE*)0)
  346. {
  347. fprintf(stderr,_("diction: Opening `%s' failed (%s).\n"),argv[optind],strerror(errno));
  348. }
  349. else
  350. {
  351. sentence("diction",fp,argv[optind],diction,phraseLanguage);
  352. fclose(fp);
  353. }
  354. ++optind;
  355. }
  356. if (sentences==0)
  357. {
  358. printf(_("No sentences found.\n"));
  359. }
  360. else
  361. {
  362. if (hits==0) printf(_("No phrases "));
  363. else if (hits==1) printf(_("1 phrase "));
  364. else printf(_("%d phrases "),hits);
  365. if (sentences==1) printf(_("in 1 sentence found.\n"));
  366. else printf(_("in %d sentences found.\n"),sentences);
  367. }
  368. exit(0);
  369. }
  370. /*}}}*/